Exemple #1
0
    def test_empty__init__(self):
        self.con_hash = ConsistentHash()
        for obj in self.objects:
            node = self.con_hash.get_node(obj)

            if node is not None:
                raise AssertionError(
                    'Should have received an exception when hashing using an empty LUT'
                )

        self.con_hash.add_nodes(self.init_nodes)

        for obj in self.objects:
            node = self.con_hash.get_node(obj)
            self.hit_nums[node] = self.hit_nums.get(node, 0) + 1

        distribution = self.show_nodes_balance()

        self.validate_distribution(
            distribution, {
                '192.168.0.101:11212': (23, 27),
                '192.168.0.102:11212': (23, 27),
                '192.168.0.103:11212': (23, 27),
                '192.168.0.104:11212': (23, 27),
            })
Exemple #2
0
    def test_add_nodes_unicode(self):
        self.con_hash = ConsistentHash({
            u'192.168.0.101:11212': 1,
            u'192.168.0.102:11212': 1,
            u'192.168.0.103:11212': 1,
            u'192.168.0.104:11212': 1,
        })
        # Add nodes to hashing ring
        add_nodes = u'192.168.0.105:11212'
        self.con_hash.add_nodes(add_nodes)
        # Get nodes from hashing ring
        for obj in self.objects:
            node = self.con_hash.get_node(obj)
            self.hit_nums[node] = self.hit_nums.get(node, 0) + 1
        distribution = self.show_nodes_balance()

        self.validate_distribution(
            distribution, {
                '192.168.0.105:11212': (17, 23),
                '192.168.0.102:11212': (17, 23),
                '192.168.0.104:11212': (17, 23),
                '192.168.0.101:11212': (17, 23),
                '192.168.0.103:11212': (17, 23),
            })
        print('->The {nodes} added!!!'.format(nodes=add_nodes))
Exemple #3
0
 def __init__(self, *args, **kwargs):
     super(KetamaMemcacheClient, self).__init__(*args, **kwargs)
     if self.servers:
         # ConsistentHash expects dictionary of servers and associated weights
         s = {
             str(server).split(":", 1)[1]: server.weight
             for server in self.servers
         }
         self.consistent_hash = ConsistentHash(s)
Exemple #4
0
    def test___init__(self):
        self.con_hash = ConsistentHash(self.init_nodes)
        # Get nodes from hashing ring
        for obj in self.objs:
            node = self.con_hash.get_node(obj)
            self.hit_nums[node] = self.hit_nums.get(node, 0) + 1
        distribution = self.show_nodes_balance()

        self.validate_distribution(distribution, {
            '192.168.0.101:11212':(23, 27),
            '192.168.0.102:11212':(23, 27),
            '192.168.0.103:11212':(23, 27),
            '192.168.0.104:11212':(23, 27)
        })
Exemple #5
0
    def test_del_nodes(self):
        self.con_hash = ConsistentHash(self.init_nodes)
        # del_nodes = self.nodes[0:2]
        del_nodes = ['192.168.0.102:11212', '192.168.0.104:11212']
        # Delete the nodes from hashing ring
        self.con_hash.del_nodes(del_nodes)
        # Get nodes from hashing ring after deleting
        for obj in self.objs:
            node = self.con_hash.get_node(obj)
            self.hit_nums[node] = self.hit_nums.get(node, 0) + 1
        distribution = self.show_nodes_balance()

        self.validate_distribution(distribution, {
            '192.168.0.101:11212': (48, 52),
            '192.168.0.103:11212': (48, 52)
        })
        print('->The {nodes} deleted!!!'.format(nodes=del_nodes))
Exemple #6
0
    def test_sample_hash_output(self):
        ConsistentHash.interleave_count = 40
        # Test backward compatibility with version 1.0
        samples = {
            '35132097': 'B',
            '25291004': 'D',
            '48182416': 'F',
            '45818378': 'H',
            '52733021': 'A',
            '94027025': 'I',
            '18116713': 'F',
            '75531098': 'J',
            '99011825': 'F',
            '99371754': 'A',
            '19630740': 'D',
            '87823770': 'G',
            '32160063': 'A',
            '28054420': 'E',
            '75904283': 'H',
            '08458048': 'E',
            '51583844': 'I',
            '16226754': 'B',
            '95450503': 'E',
            '47557476': 'C',
            '38808589': 'A',
        }

        hash_ring = ConsistentHash(
            objects=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'])
        for node, output in samples.items():
            result = hash_ring.get_node(node)
            if result != output:
                raise AssertionError(
                    'Expected node does not match actual node. Expected: {}. Got: {}'
                    .format(
                        output,
                        result,
                    ))