diff --git a/swift/common/ring/builder.py b/swift/common/ring/builder.py index d70d6d2cea..3b87d81e45 100644 --- a/swift/common/ring/builder.py +++ b/swift/common/ring/builder.py @@ -772,7 +772,7 @@ class RingBuilder(object): required = (wanted[tier] - weighted[tier]) / weighted[tier] self.logger.debug('%(tier)s wants %(wanted)s and is weighted for ' '%(weight)s so therefore requires %(required)s ' - 'overload', {'tier': tier, + 'overload', {'tier': pretty_dev(dev), 'wanted': wanted[tier], 'weight': weighted[tier], 'required': required}) diff --git a/swift/common/ring/utils.py b/swift/common/ring/utils.py index 2ca41495ba..9be819bc32 100644 --- a/swift/common/ring/utils.py +++ b/swift/common/ring/utils.py @@ -642,6 +642,15 @@ def dispersion_report(builder, search_filter=None, verbose=False): } +def format_device(region=None, zone=None, ip=None, device=None, **kwargs): + """ + Convert device dict or tier attributes to a representative string. + + :returns: a string, the normalized format of a device tier + """ + return "r%sz%s-%s/%s" % (region, zone, ip, device) + + def get_tier_name(tier, builder): if len(tier) == 1: return "r%s" % (tier[0], ) @@ -651,8 +660,8 @@ def get_tier_name(tier, builder): return "r%sz%s-%s" % (tier[0], tier[1], tier[2]) if len(tier) == 4: device = builder.devs[tier[3]] or {} - return "r%sz%s-%s/%s" % (tier[0], tier[1], tier[2], - device.get('device', 'IDd%s' % tier[3])) + return format_device(tier[0], tier[1], tier[2], device.get( + 'device', 'IDd%s' % tier[3])) def validate_device_name(device_name): @@ -663,7 +672,4 @@ def validate_device_name(device_name): def pretty_dev(device): - return "r%sz%s-%s/%s" % (device.get('region'), - device.get('zone'), - device.get('ip'), - device.get('id')) + return format_device(**device) diff --git a/test/unit/common/ring/test_utils.py b/test/unit/common/ring/test_utils.py index c40ce85470..a18a8808d2 100644 --- a/test/unit/common/ring/test_utils.py +++ b/test/unit/common/ring/test_utils.py @@ -26,7 +26,7 @@ from swift.common.ring.utils import (tiers_for_dev, build_tier_tree, validate_args, parse_args, parse_builder_ring_filename_args, build_dev_from_opts, dispersion_report, - parse_address) + parse_address, get_tier_name, pretty_dev) class TestUtils(unittest.TestCase): @@ -690,6 +690,21 @@ class TestUtils(unittest.TestCase): self.assertEqual(port, 6200) self.assertEqual(rest, 'R127.0.0.1:6200/sda1_some meta data') + def test_normalized_device_tier_names(self): + rb = ring.RingBuilder(8, 3, 0) + rb.add_dev({ + 'region': 1, + 'zone': 1, + 'ip': '127.0.0.1', + 'port': 6011, + 'device': 'd1', + 'weight': 0.0, + }) + dev = rb.devs[0] + expected = 'r1z1-127.0.0.1/d1' + self.assertEqual(expected, get_tier_name(tiers_for_dev(dev)[-1], rb)) + self.assertEqual(expected, pretty_dev(dev)) + if __name__ == '__main__': unittest.main()