Extend device tier pretty print normalization

* use pretty device tier normalization in additional debug message
* refactor pretty device tier normalization for consistency
* unittest asserting consistency

Change-Id: Ide32e35ff9387f1cc1e4997eb133314d06b794f3
This commit is contained in:
Clay Gerrard 2017-06-28 15:40:22 -07:00
parent 62509cc8f4
commit 806b18c6f5
3 changed files with 29 additions and 8 deletions

View File

@ -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})

View File

@ -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)

View File

@ -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()