Merge "builder.add_devs gets next id if not provided"

This commit is contained in:
Jenkins 2012-09-18 18:53:43 +00:00 committed by Gerrit Code Review
commit b0a2f795f4
3 changed files with 20 additions and 11 deletions

View File

@ -269,18 +269,17 @@ swift-ring-builder <builder_file> add
print "The on-disk ring builder is unchanged.\n"
exit(EXIT_ERROR)
next_dev_id = 0
if builder.devs:
next_dev_id = max(d['id'] for d in builder.devs if d) + 1
builder.add_dev({'id': next_dev_id, 'zone': zone, 'ip': ip,
'port': port, 'device': device_name,
'weight': weight, 'meta': meta})
builder.add_dev({'zone': zone, 'ip': ip, 'port': port,
'device': device_name, 'weight': weight,
'meta': meta})
new_dev = builder.search_devs(
'z%s-%s:%s/%s' % (zone, ip, port, device_name))[0]['id']
if ':' in ip:
print 'Device z%s-[%s]:%s/%s_"%s" with %s weight got id %s' % \
(zone, ip, port, device_name, meta, weight, next_dev_id)
(zone, ip, port, device_name, meta, weight, new_dev)
else:
print 'Device z%s-%s:%s/%s_"%s" with %s weight got id %s' % \
(zone, ip, port, device_name, meta, weight, next_dev_id)
(zone, ip, port, device_name, meta, weight, new_dev)
pickle.dump(builder.to_dict(), open(argv[1], 'wb'), protocol=2)
exit(EXIT_SUCCESS)

View File

@ -20,7 +20,6 @@ import cPickle as pickle
from array import array
from sys import modules
from collections import defaultdict
from random import randint, shuffle
from time import time
@ -204,7 +203,8 @@ class RingBuilder(object):
following keys:
====== ===============================================================
id unique integer identifier amongst devices
id unique integer identifier amongst devices. Defaults to the next
id if the 'id' key is not provided in the dict
weight a float of the relative weight of this device as compared to
others; this indicates how many partitions the builder will try
to assign to this device
@ -224,6 +224,10 @@ class RingBuilder(object):
:param dev: device dict
"""
if 'id' not in dev:
dev['id'] = 0
if self.devs:
dev['id'] = max(d['id'] for d in self.devs if d) + 1
if dev['id'] < len(self.devs) and self.devs[dev['id']] is not None:
raise exceptions.DuplicateDeviceError(
'Duplicate device id: %d' % dev['id'])

View File

@ -74,6 +74,13 @@ class TestRingBuilder(unittest.TestCase):
{'id': 0, 'zone': 0, 'weight': 1, 'ip': '127.0.0.1', 'port': 10000}
rb.add_dev(dev)
self.assertRaises(exceptions.DuplicateDeviceError, rb.add_dev, dev)
rb = ring.RingBuilder(8, 3, 1)
#test add new dev with no id
rb.add_dev({'zone': 0, 'weight': 1, 'ip': '127.0.0.1', 'port': 6000})
self.assertEquals(rb.devs[0]['id'], 0)
#test add another dev with no id
rb.add_dev({'zone': 3, 'weight': 1, 'ip': '127.0.0.1', 'port': 6000})
self.assertEquals(rb.devs[1]['id'], 1)
def test_set_dev_weight(self):
rb = ring.RingBuilder(8, 3, 1)
@ -536,7 +543,6 @@ class TestRingBuilder(unittest.TestCase):
no_meta_builder = rb
for dev in no_meta_builder.devs:
del(dev['meta'])
print no_meta_builder.devs
fake_pickle.return_value = no_meta_builder
pickle.load = fake_pickle
builder = RingBuilder.load('fake.builder', open=fake_open)