Exemplo n.º 1
0
    def test_consistent_hash_3_machines_3_replicas(self):
        ch = ConsistentHash(3, 3)

        self.assertEqual(ch.get_machine('key'), 1)
        self.assertEqual(ch.get_machine('key2'), 0)
        self.assertEqual(ch.get_machine('key3'), 0)
        self.assertEqual(ch.get_machine('key4'), 1)
        self.assertEqual(ch.get_machine('key5'), 1)
        self.assertEqual(ch.get_machine('key6'), 2)
Exemplo n.º 2
0
    def test_consistent_hash_200_machines_20_replicas(self):
        ch = ConsistentHash(200, 20)

        self.assertEqual(ch.get_machine('key'), 93)
        self.assertEqual(ch.get_machine('key2'), 79)
        self.assertEqual(ch.get_machine('key3'), 164)
        self.assertEqual(ch.get_machine('key4'), 141)
        self.assertEqual(ch.get_machine('key5'), 22)
        self.assertEqual(ch.get_machine('key6'), 121)
Exemplo n.º 3
0
    def test_consistent_hash_one_replica(self):
        ch = ConsistentHash(1, 1)

        self.assertEqual(ch.get_machine('key'), 0)
        self.assertEqual(ch.get_machine('key2'), 0)
        self.assertEqual(ch.get_machine('key3'), 0)
        self.assertEqual(ch.get_machine('key4'), 0)
        self.assertEqual(ch.get_machine('key5'), 0)
        self.assertEqual(ch.get_machine('key6'), 0)
Exemplo n.º 4
0
    def get_node_by_key(self, key):
        """Gets the right machine based on the ky.

        :param key: The key to be set. It will be used to define which
        node that key should go.
        :returns: A `brainer.node.client.NodeClient` object.
        """
        if not self._nodes:
            raise ZeroNodeError

        hashing = ConsistentHash(len(self._nodes))
        node_number = hashing.get_machine(key)
        node_id = self._nodes[node_number]
        if self._debug:
            log.msg('Machine {} ({}) picked for key {}'.format(
                node_id, node_number, key))
        return self._nodes_connections[node_id]