def test_getting_replicas(self): tokens = [MD5Token(str(i)) for i in range(0, (2**127 - 1), 2**125)] hosts = [ Host("ip%d" % i, SimpleConvictionPolicy) for i in range(len(tokens)) ] token_to_primary_replica = dict(zip(tokens, hosts)) keyspace = KeyspaceMetadata("ks", True, "SimpleStrategy", {"replication_factor": "1"}) metadata = Mock(spec=Metadata, keyspaces={'ks': keyspace}) token_map = TokenMap(MD5Token, token_to_primary_replica, tokens, metadata) # tokens match node tokens exactly for i, token in enumerate(tokens): expected_host = hosts[(i + 1) % len(hosts)] replicas = token_map.get_replicas("ks", token) self.assertEqual(set(replicas), set([expected_host])) # shift the tokens back by one for token, expected_host in zip(tokens, hosts): replicas = token_map.get_replicas("ks", MD5Token(str(token.value - 1))) self.assertEqual(set(replicas), set([expected_host])) # shift the tokens forward by one for i, token in enumerate(tokens): replicas = token_map.get_replicas("ks", MD5Token(str(token.value + 1))) expected_host = hosts[(i + 1) % len(hosts)] self.assertEqual(set(replicas), set([expected_host]))
def test_getting_replicas(self): tokens = [MD5Token(str(i)) for i in range(0, (2 ** 127 - 1), 2 ** 125)] hosts = [Host("ip%d" % i, SimpleConvictionPolicy) for i in range(len(tokens))] tokens_to_hosts = dict((t, set([h])) for t, h in zip(tokens, hosts)) token_map = TokenMap(MD5Token, tokens_to_hosts, tokens) # tokens match node tokens exactly for token, expected_host in zip(tokens, hosts): replicas = token_map.get_replicas(token) self.assertEqual(replicas, set([expected_host])) # shift the tokens back by one for token, expected_host in zip(tokens[1:], hosts[1:]): replicas = token_map.get_replicas(MD5Token(str(token.value - 1))) self.assertEqual(replicas, set([expected_host])) # shift the tokens forward by one for i, token in enumerate(tokens): replicas = token_map.get_replicas(MD5Token(str(token.value + 1))) expected_host = hosts[(i + 1) % len(hosts)] self.assertEqual(replicas, set([expected_host]))
def test_getting_replicas(self): tokens = [MD5Token(str(i)) for i in range(0, (2 ** 127 - 1), 2 ** 125)] hosts = [Host("ip%d" % i, SimpleConvictionPolicy) for i in range(len(tokens))] token_to_primary_replica = dict(zip(tokens, hosts)) keyspace = KeyspaceMetadata("ks", True, "SimpleStrategy", {"replication_factor": "1"}) token_map = TokenMap(MD5Token, token_to_primary_replica, tokens, [keyspace]) # tokens match node tokens exactly for token, expected_host in zip(tokens, hosts): replicas = token_map.get_replicas("ks", token) self.assertEqual(replicas, set([expected_host])) # shift the tokens back by one for token, expected_host in zip(tokens[1:], hosts[1:]): replicas = token_map.get_replicas("ks", MD5Token(str(token.value - 1))) self.assertEqual(replicas, set([expected_host])) # shift the tokens forward by one for i, token in enumerate(tokens): replicas = token_map.get_replicas("ks", MD5Token(str(token.value + 1))) expected_host = hosts[(i + 1) % len(hosts)] self.assertEqual(replicas, set([expected_host]))
def _get_replicas(self, token_klass): tokens = [token_klass(i) for i in range(0, (2 ** 127 - 1), 2 ** 125)] hosts = [Host("ip%d" % i, SimpleConvictionPolicy) for i in range(len(tokens))] token_to_primary_replica = dict(zip(tokens, hosts)) keyspace = KeyspaceMetadata("ks", True, "SimpleStrategy", {"replication_factor": "1"}) metadata = Mock(spec=Metadata, keyspaces={'ks': keyspace}) token_map = TokenMap(token_klass, token_to_primary_replica, tokens, metadata) # tokens match node tokens exactly for token, expected_host in zip(tokens, hosts): replicas = token_map.get_replicas("ks", token) self.assertEqual(set(replicas), {expected_host}) # shift the tokens back by one for token, expected_host in zip(tokens, hosts): replicas = token_map.get_replicas("ks", token_klass(token.value - 1)) self.assertEqual(set(replicas), {expected_host}) # shift the tokens forward by one for i, token in enumerate(tokens): replicas = token_map.get_replicas("ks", token_klass(token.value + 1)) expected_host = hosts[(i + 1) % len(hosts)] self.assertEqual(set(replicas), {expected_host})