def test_chr_get_nodes(self): hosts = [("127.0.0.1", "cache0"), ("127.0.0.1", "cache1"), ("127.0.0.1", "cache2")] hashring = ConsistentHashRing(hosts) node = hashring.get_nodes('hosts.worker1.cpu') self.assertEqual(node, [('127.0.0.1', 'cache2'), ('127.0.0.1', 'cache0'), ('127.0.0.1', 'cache1')])
def test_chr_remove_node_missing(self): hosts = [("127.0.0.1", "cache0"), ("127.0.0.1", "cache1"), ("127.0.0.1", "cache2")] hashring = ConsistentHashRing(hosts) self.assertEqual(hashring.nodes, set(hosts)) hashring.remove_node(("127.0.0.1", "cache4")) self.assertEqual(hashring.nodes, set(hosts)) self.assertEqual(hashring.nodes_len, 3)
def test_chr_add_node_duplicate(self): hosts = [("127.0.0.1", "cache0"), ("127.0.0.1", "cache1"), ("127.0.0.1", "cache2")] hashring = ConsistentHashRing(hosts) self.assertEqual(hashring.nodes, set(hosts)) hashring.add_node(("127.0.0.1", "cache2")) self.assertEqual(hashring.nodes, set(hosts)) self.assertEqual(hashring.nodes_len, 3)
def test_chr_compute_ring_position(self): hosts = [("127.0.0.1", "cache0"), ("127.0.0.1", "cache1"), ("127.0.0.1", "cache2")] hashring = ConsistentHashRing(hosts) self.assertEqual(hashring.compute_ring_position('hosts.worker1.cpu'), 64833) self.assertEqual(hashring.compute_ring_position('hosts.worker2.cpu'), 38509)
def test_chr_get_node_fnv1a(self): hosts = [("127.0.0.1", "ba603c36342304ed77953f84ac4d357b"), ("127.0.0.2", "5dd63865534f84899c6e5594dba6749a"), ("127.0.0.3", "866a18b81f2dc4649517a1df13e26f28")] hashring = ConsistentHashRing(hosts, hash_type='fnv1a_ch') self.assertEqual(hashring.get_node('hosts.worker1.cpu'), ('127.0.0.1', 'ba603c36342304ed77953f84ac4d357b')) self.assertEqual(hashring.get_node('hosts.worker2.cpu'), ('127.0.0.3', '866a18b81f2dc4649517a1df13e26f28'))
def test_chr_compute_ring_position_fnv1a(self): hosts = [("127.0.0.1", "ba603c36342304ed77953f84ac4d357b"), ("127.0.0.2", "5dd63865534f84899c6e5594dba6749a"), ("127.0.0.3", "866a18b81f2dc4649517a1df13e26f28")] hashring = ConsistentHashRing(hosts, hash_type='fnv1a_ch') self.assertEqual(hashring.compute_ring_position('hosts.worker1.cpu'), 59573) self.assertEqual(hashring.compute_ring_position('hosts.worker2.cpu'), 35749)
def __init__(self, hosts, timeout): self.hosts = [(server, instance) for (server, port, instance) in hosts] self.ports = dict( ((server, instance), port) for (server, port, instance) in hosts) self.timeout = float(timeout) self.hashRing = ConsistentHashRing(self.hosts) self.connections = {} # Create a connection pool for each host for host in self.hosts: self.connections[host] = set()
def __init__(self, hosts, timeout): self.hosts = [ (server, instance) for (server, port, instance) in hosts ] self.ports = dict( ((server, instance), port) for (server, port, instance) in hosts ) self.timeout = float(timeout) servers = set([server for (server, port, instance) in hosts]) if len(servers) < settings.REPLICATION_FACTOR: raise Exception("REPLICATION_FACTOR=%d cannot exceed servers=%d" % (settings.REPLICATION_FACTOR, len(servers))) self.hash_ring = ConsistentHashRing(self.hosts) self.keyfunc = load_keyfunc() self.connections = {} self.last_failure = {} # Create a connection pool for each host for host in self.hosts: self.connections[host] = set()
instances = [] unwelcome_instances = [] for arg in sys.argv[1:]: unwelcome = False if arg.startswith('-'): arg = arg[1:] unwelcome = True instance = tuple(arg.split(':', 2)) instances.append(instance) if unwelcome: unwelcome_instances.append(instance) if 0 == len(instances): print('Usage: python whisper-clean.py [-]<address>:<instance>[...]') sys.exit(1) ring = ConsistentHashRing(instances) for dirname, dirnames, filenames in os.walk('/var/lib/graphite/whisper'): for filename in filenames: pathname = os.path.join(dirname, filename) basename, ext = os.path.splitext(filename) if '.wsp' != ext: print('skipping %s' % os.path.relpath(pathname, '/var/lib/graphite/whisper')) if ring.get_node( os.path.relpath(os.path.join(dirname, basename), '/var/lib/graphite/whisper').replace( '/', '.')) in unwelcome_instances: print('unlinking %s' % pathname) os.unlink(pathname)