From 31a2ef1935133f224169a1315a91a2d9e9775d9a Mon Sep 17 00:00:00 2001 From: Luis Pabon Date: Thu, 30 May 2013 17:31:26 -0400 Subject: Remove account name from being saved in the object Instead we save the account in the a list, where the index to the account is the partition number. Change-Id: Ie4abefee48a3b237306a1e301ffa798e24e3f1db Signed-off-by: Luis Pabon Reviewed-on: http://review.gluster.org/5120 Reviewed-by: Peter Portante Tested-by: Peter Portante --- gluster/swift/common/ring.py | 43 ++++++++++++++++++++++++++++++++----------- test/unit/common/test_ring.py | 14 ++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/gluster/swift/common/ring.py b/gluster/swift/common/ring.py index f88af4e..a3209e2 100644 --- a/gluster/swift/common/ring.py +++ b/gluster/swift/common/ring.py @@ -38,12 +38,30 @@ if not reseller_prefix.endswith('_'): class Ring(ring.Ring): + + def __init__(self, *args, **kwargs): + self.false_node = {'zone': 1, 'weight': 100.0, 'ip': '127.0.0.1', + 'id': 0, 'meta': '', 'device': 'volume_not_in_ring', + 'port': 6012} + self.account_list = [] + ring.Ring.__init__(self, *args, **kwargs) + def _get_part_nodes(self, part): seen_ids = set() - nodes = [dev for dev in self._devs if dev['device'] == self.acc_name - and not (dev['id'] in seen_ids or seen_ids.add(dev['id']))] - if not nodes: - nodes = [self.false_node] + + try: + account = self.account_list[part] + except IndexError: + return [self.false_node] + else: + nodes = [] + for dev in self._devs: + if dev['device'] == account: + if dev['id'] not in seen_ids: + seen_ids.add(dev['id']) + nodes.append(dev) + if not nodes: + nodes = [self.false_node] return nodes def get_part_nodes(self, part): @@ -85,15 +103,18 @@ class Ring(ring.Ring): hardware description ====== =============================================================== """ - self.false_node = {'zone': 1, 'weight': 100.0, 'ip': '127.0.0.1', - 'id': 0, 'meta': '', 'device': 'volume_not_in_ring', - 'port': 6012} if account.startswith(reseller_prefix): - self.acc_name = account.replace(reseller_prefix, '', 1) - else: - self.acc_name = account + account = account.replace(reseller_prefix, '', 1) + + # Save the account name in the table + # This makes part be the index of the location of the account + # in the list + try: + part = self.account_list.index(account) + except ValueError: + self.account_list.append(account) + part = self.account_list.index(account) - part = 0 return part, self._get_part_nodes(part) def get_more_nodes(self, part): diff --git a/test/unit/common/test_ring.py b/test/unit/common/test_ring.py index 8b7509c..4cbb28c 100644 --- a/test/unit/common/test_ring.py +++ b/test/unit/common/test_ring.py @@ -53,3 +53,17 @@ class TestRing(unittest.TestCase): def test_second_device_with_reseller_prefix(self): part, node = self.ring.get_nodes('AUTH_iops') assert node[0]['device'] == 'iops' + + def test_partition_id_for_multiple_accounts(self): + test_part, test_node = self.ring.get_nodes('test') + iops_part, iops_node = self.ring.get_nodes('iops') + self.assertNotEqual(test_part, iops_part) + self.assertEqual(test_node, self.ring.get_part_nodes(test_part)) + self.assertEqual(iops_node, self.ring.get_part_nodes(iops_part)) + self.assertNotEqual(test_node, self.ring.get_part_nodes(iops_part)) + self.assertNotEqual(iops_node, self.ring.get_part_nodes(test_part)) + + def test_invalid_partition(self): + nodes = self.ring.get_part_nodes(0) + self.assertEqual(nodes[0]['device'], 'volume_not_in_ring') + -- cgit