def test_basic(): def test(k): data = {} for i in xrange(1000): tower = k.get_node('a%s' % i) data.setdefault(tower, 0) data[tower] += 1 return [ k.get_node('Apple'), k.get_node('Hello'), k.get_node('Data'), k.get_node('Computer') ] k = Ketama([ '192.168.0.1:6000', '192.168.0.1:6001', '192.168.0.1:6002', '192.168.0.1:6003', '192.168.0.1:6004', '192.168.0.1:6005', '192.168.0.1:6006', '192.168.0.1:6008', '192.168.0.1:6007' ]) assert test(k) == ['192.168.0.1:6002', '192.168.0.1:6007', '192.168.0.1:6004', '192.168.0.1:6004'] k.remove_node('192.168.0.1:6007') assert test(k) == ['192.168.0.1:6002', '192.168.0.1:6000', '192.168.0.1:6004', '192.168.0.1:6004'] k.add_node('192.168.0.1:6007') assert test(k) == ['192.168.0.1:6002', '192.168.0.1:6007', '192.168.0.1:6004', '192.168.0.1:6004']
def test_basic(): def test(k): data = {} for i in six.range(1000): tower = k.get_node('a%s' % i) data.setdefault(tower, 0) data[tower] += 1 return [ k.get_node('Apple'), k.get_node('Hello'), k.get_node('Data'), k.get_node('Computer') ] k = Ketama([ '192.168.0.1:6000', '192.168.0.1:6001', '192.168.0.1:6002', '192.168.0.1:6003', '192.168.0.1:6004', '192.168.0.1:6005', '192.168.0.1:6006', '192.168.0.1:6008', '192.168.0.1:6007' ]) assert test(k) == ['192.168.0.1:6002', '192.168.0.1:6007', '192.168.0.1:6004', '192.168.0.1:6004'] k.remove_node('192.168.0.1:6007') assert test(k) == ['192.168.0.1:6002', '192.168.0.1:6000', '192.168.0.1:6004', '192.168.0.1:6004'] k.add_node('192.168.0.1:6007') assert test(k) == ['192.168.0.1:6002', '192.168.0.1:6007', '192.168.0.1:6004', '192.168.0.1:6004']
class ConsistentHashingRouter(BaseRouter): """Router that returns the host_id based on a consistent hashing algorithm. The consistent hashing algorithm only works if a key argument is provided. """ def __init__(self, cluster): BaseRouter.__init__(self, cluster) self._host_id_id_map = dict(self.cluster.hosts.items()) self._hash = Ketama(self._host_id_id_map.values()) def get_host_for_key(self, key): rv = self._hash.get_node(key) if rv is None: raise UnroutableCommand('Did not find a suitable ' 'host for the key.') return rv
def test_basic(): def test(k): data = {} for i in range(1000): tower = k.get_node("a%s" % i) data.setdefault(tower, 0) data[tower] += 1 return [ k.get_node("Apple"), k.get_node("Hello"), k.get_node("Data"), k.get_node("Computer"), ] k = Ketama( [ "192.168.0.1:6000", "192.168.0.1:6001", "192.168.0.1:6002", "192.168.0.1:6003", "192.168.0.1:6004", "192.168.0.1:6005", "192.168.0.1:6006", "192.168.0.1:6008", "192.168.0.1:6007", ] ) assert test(k) == [ "192.168.0.1:6002", "192.168.0.1:6007", "192.168.0.1:6004", "192.168.0.1:6004", ] k.remove_node("192.168.0.1:6007") assert test(k) == [ "192.168.0.1:6002", "192.168.0.1:6000", "192.168.0.1:6004", "192.168.0.1:6004", ] k.add_node("192.168.0.1:6007") assert test(k) == [ "192.168.0.1:6002", "192.168.0.1:6007", "192.168.0.1:6004", "192.168.0.1:6004", ]
class ConsistentHashingRouter(BaseRouter): """Router that returns the host_id based on a consistent hashing algorithm. The consistent hashing algorithm only works if a key argument is provided. This router requires that the hosts are gapless which means that the IDs for N hosts range from 0 to N-1. """ def __init__(self, cluster): BaseRouter.__init__(self, cluster) self._host_id_id_map = dict(self.cluster.hosts.items()) self._hash = Ketama(self._host_id_id_map.values()) assert_gapless_hosts(self.cluster.hosts) def get_host_for_key(self, key): rv = self._hash.get_node(key) if rv is None: raise UnroutableCommand("Did not find a suitable " "host for the key.") return rv
class ConsistentHashingRouter(BaseRouter): """Router that returns the host_id based on a consistent hashing algorithm. The consistent hashing algorithm only works if a key argument is provided. This router requires that the hosts are gapless which means that the IDs for N hosts range from 0 to N-1. """ def __init__(self, cluster): BaseRouter.__init__(self, cluster) self._host_id_id_map = dict(self.cluster.hosts.items()) self._hash = Ketama(self._host_id_id_map.values()) assert_gapless_hosts(self.cluster.hosts) def get_host_for_key(self, key): rv = self._hash.get_node(key) if rv is None: raise UnroutableCommand('Did not find a suitable ' 'host for the key.') return rv
def __init__(self, cluster): BaseRouter.__init__(self, cluster) self._host_id_id_map = dict(self.cluster.hosts.items()) self._hash = Ketama(self._host_id_id_map.values()) assert_gapless_hosts(self.cluster.hosts)