本文整理汇总了Python中swift.common.ring.RingBuilder类的典型用法代码示例。如果您正苦于以下问题:Python RingBuilder类的具体用法?Python RingBuilder怎么用?Python RingBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RingBuilder类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: create
def create():
"""
swift-ring-builder <builder_file> create <part_power> <replicas>
<min_part_hours>
Creates <builder_file> with 2^<part_power> partitions and <replicas>.
<min_part_hours> is number of hours to restrict moving a partition more
than once.
"""
if len(argv) < 6:
print(Commands.create.__doc__.strip())
exit(EXIT_ERROR)
# 1、生成RingBuilder对象实例
builder = RingBuilder(int(argv[3]), float(argv[4]), int(argv[5]))
# 2、创建备份目录
backup_dir = pathjoin(dirname(builder_file), 'backups')
try:
mkdir(backup_dir)
except OSError as err:
if err.errno != EEXIST:
raise
# 3、保存原始数据到备份目录,以及/etc/swift目录中
builder.save(pathjoin(backup_dir,
'%d.' % time() + basename(builder_file)))
builder.save(builder_file)
exit(EXIT_SUCCESS)
开发者ID:revoer,项目名称:keystone-8.0.0,代码行数:27,代码来源:ringbuilder.py
示例2: test_load
def test_load(self):
rb = ring.RingBuilder(8, 3, 1)
devs = [{'id': 0, 'region': 0, 'zone': 0, 'weight': 1,
'ip': '127.0.0.0', 'port': 10000, 'device': 'sda1',
'meta': 'meta0'},
{'id': 1, 'region': 0, 'zone': 1, 'weight': 1,
'ip': '127.0.0.1', 'port': 10001, 'device': 'sdb1',
'meta': 'meta1'},
{'id': 2, 'region': 0, 'zone': 2, 'weight': 2,
'ip': '127.0.0.2', 'port': 10002, 'device': 'sdc1',
'meta': 'meta2'},
{'id': 3, 'region': 0, 'zone': 3, 'weight': 2,
'ip': '127.0.0.3', 'port': 10003, 'device': 'sdd1'}]
for d in devs:
rb.add_dev(d)
rb.rebalance()
real_pickle = pickle.load
try:
#test a legit builder
fake_pickle = Mock(return_value=rb)
fake_open = Mock(return_value=None)
pickle.load = fake_pickle
builder = RingBuilder.load('fake.builder', open=fake_open)
self.assertEquals(fake_pickle.call_count, 1)
fake_open.assert_has_calls([mock_call('fake.builder', 'rb')])
self.assertEquals(builder, rb)
fake_pickle.reset_mock()
fake_open.reset_mock()
#test old style builder
fake_pickle.return_value = rb.to_dict()
pickle.load = fake_pickle
builder = RingBuilder.load('fake.builder', open=fake_open)
fake_open.assert_has_calls([mock_call('fake.builder', 'rb')])
self.assertEquals(builder.devs, rb.devs)
fake_pickle.reset_mock()
fake_open.reset_mock()
#test old devs but no meta
no_meta_builder = rb
for dev in no_meta_builder.devs:
del(dev['meta'])
fake_pickle.return_value = no_meta_builder
pickle.load = fake_pickle
builder = RingBuilder.load('fake.builder', open=fake_open)
fake_open.assert_has_calls([mock_call('fake.builder', 'rb')])
self.assertEquals(builder.devs, rb.devs)
fake_pickle.reset_mock()
finally:
pickle.load = real_pickle
开发者ID:ganenlovecode,项目名称:swift,代码行数:51,代码来源:test_builder.py
示例3: test_set_info
def test_set_info(self):
for search_value in self.search_values:
self.create_sample_ring()
argv = ["", self.tmpfile, "set_info", search_value,
"127.0.1.1:8000/sda1_other meta data"]
self.assertRaises(SystemExit, swift.cli.ringbuilder.main, argv)
# Check that device was created with given data
ring = RingBuilder.load(self.tmpfile)
dev = [d for d in ring.devs if d['id'] == 0][0]
self.assertEqual(dev['ip'], '127.0.1.1')
self.assertEqual(dev['port'], 8000)
self.assertEqual(dev['device'], 'sda1')
self.assertEqual(dev['meta'], 'other meta data')
# Check that second device in ring is not affected
dev = [d for d in ring.devs if d['id'] == 1][0]
self.assertEqual(dev['ip'], '127.0.0.2')
self.assertEqual(dev['port'], 6001)
self.assertEqual(dev['device'], 'sda2')
self.assertEqual(dev['meta'], '')
# Final check, rebalance and check ring is ok
ring.rebalance()
self.assertTrue(ring.validate())
开发者ID:steveruckdashel,项目名称:swift,代码行数:26,代码来源:test_ringbuilder.py
示例4: __init__
def __init__(self, builder_file, swiftdir='/etc/swift',
backup_dir_name='backups', verbose=False):
self.verbose = verbose
self.builder_file = builder_file
self.swiftdir = swiftdir
self.backup_dir = pathjoin(self.swiftdir, backup_dir_name)
self.builder = RingBuilder.load(builder_file)
开发者ID:pandemicsyn,项目名称:swiftscout,代码行数:7,代码来源:drivescout.py
示例5: add_to_ring
def add_to_ring(self, builder_type, body, lasthash, start_response, env):
""" Handle a add device post """
with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
self.verify_current_hash(self.bf_path[builder_type], lasthash)
builder = RingBuilder.load(self.bf_path[builder_type])
ring_modified = False
try:
for device in body['devices']:
sleep() # so we don't starve/block
if not self._is_existing_dev(builder, device['ip'],
int(device['port']),
device['device']):
self._add_device(builder, int(device['zone']),
device['ip'], int(device['port']),
device['device'],
float(device['weight']),
device['meta'])
ring_modified = True
except (AttributeError, KeyError, ValueError, TypeError) as err:
return self.return_response(False, lasthash,
"Malformed request.",
start_response, env)
if ring_modified:
newmd5 = self.write_builder(builder,
self.bf_path[builder_type])
return self.return_response(True, newmd5, None,
start_response, env)
else:
return self.return_response(False, lasthash,
'Ring remains unchanged.',
start_response, env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:31,代码来源:middleware.py
示例6: change_meta
def change_meta(self, builder_type, dev_meta, lasthash, start_response,
env):
""" Change meta info for devices
:param builder_type: the builder_type to use when loading the builder
:param dev_meta: a dict of device id and meta info
:param lasthash: the hash to use when verifying state
"""
with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
self.verify_current_hash(self.bf_path[builder_type], lasthash)
builder = RingBuilder.load(self.bf_path[builder_type])
try:
modified = False
for dev_id in dev_meta:
sleep() # so we don't starve/block
for device in builder.devs:
if not device:
continue
if device['id'] == int(dev_id):
modified = True
device['meta'] = '%s' % dev_meta[dev_id]
if modified:
newmd5 = self.write_builder(builder,
self.bf_path[builder_type])
return self.return_response(True, newmd5, None,
start_response, env)
else:
return self.return_response(False, lasthash,
'Invalid dev id %s.' % dev_id,
start_response, env)
except ValueError as err:
return self.return_response(False, lasthash, str(err),
start_response, env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:33,代码来源:middleware.py
示例7: change_weight
def change_weight(self, builder_type, dev_weights, lasthash,
start_response, env):
""" Change weight of devices
:param builder_type: the builder_type to use when loading the builder
:param dev_weights: a dict of device id and weight
:param lasthash: the hash to use when verifying state
"""
with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
self.verify_current_hash(self.bf_path[builder_type], lasthash)
builder = RingBuilder.load(self.bf_path[builder_type])
for dev_id in dev_weights:
sleep() # so we don't starve/block
try:
builder.set_dev_weight(int(dev_id),
float(dev_weights[dev_id]))
except (IndexError, TypeError):
return self.return_response(False, lasthash,
'Invalid dev id %s.' % dev_id,
start_response, env)
except ValueError as err:
return self.return_response(False, lasthash, str(err),
start_response, env)
newmd5 = self.write_builder(builder, self.bf_path[builder_type])
return self.return_response(True, newmd5, None, start_response,
env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:26,代码来源:middleware.py
示例8: remove_devs
def remove_devs(self, builder_type, devices, lasthash, start_response,
env):
""" remove devices from the builder
:params builder_type: the builder_type to use when loading the builder
:params devices: list of device ids to be removed.
:params lasthash: the hash to use when verifying state
"""
with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
self.verify_current_hash(self.bf_path[builder_type], lasthash)
builder = RingBuilder.load(self.bf_path[builder_type])
if not isinstance(devices, list):
return self.return_response(False, lasthash,
'Malformed request.',
start_response, env)
for dev_id in devices:
sleep() # so we don't starve/block
try:
builder.remove_dev(int(dev_id))
except (IndexError, TypeError):
return self.return_response(False, lasthash,
'Invalid dev id %s.' % dev_id,
start_response, env)
except RingBuilderError as err:
return self.return_response(False, lasthash,
'Error removing %s - %s.' %
(dev_id, err),
start_response, env)
except ValueError as err:
return self.return_response(False, lasthash, str(err),
start_response, env)
newmd5 = self.write_builder(builder, self.bf_path[builder_type])
return self.return_response(True, newmd5, None, start_response,
env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:34,代码来源:middleware.py
示例9: test_remove_device
def test_remove_device(self):
for search_value in self.search_values:
self.create_sample_ring()
argv = ["", self.tmpfile, "remove", search_value]
self.assertRaises(SystemExit, swift.cli.ringbuilder.main, argv)
ring = RingBuilder.load(self.tmpfile)
# Check that weight was set to 0
dev = [d for d in ring.devs if d['id'] == 0][0]
self.assertEqual(dev['weight'], 0)
# Check that device is in list of devices to be removed
dev = [d for d in ring._remove_devs if d['id'] == 0][0]
self.assertEqual(dev['region'], 0)
self.assertEqual(dev['zone'], 0)
self.assertEqual(dev['ip'], '127.0.0.1')
self.assertEqual(dev['port'], 6000)
self.assertEqual(dev['device'], 'sda1')
self.assertEqual(dev['weight'], 0)
self.assertEqual(dev['replication_ip'], '127.0.0.1')
self.assertEqual(dev['replication_port'], 6000)
self.assertEqual(dev['meta'], 'some meta data')
# Check that second device in ring is not affected
dev = [d for d in ring.devs if d['id'] == 1][0]
self.assertEqual(dev['weight'], 100)
self.assertFalse([d for d in ring._remove_devs if d['id'] == 1])
# Final check, rebalance and check ring is ok
ring.rebalance()
self.assertTrue(ring.validate())
开发者ID:steveruckdashel,项目名称:swift,代码行数:31,代码来源:test_ringbuilder.py
示例10: rebalance
def rebalance(self, builder_type, lasthash, start_response, env):
""" rebalance a ring
note: rebalance doesn't yield.
"""
with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
self.verify_current_hash(self.bf_path[builder_type], lasthash)
builder = RingBuilder.load(self.bf_path[builder_type])
devs_changed = builder.devs_changed
try:
last_balance = builder.get_balance()
parts, balance = builder.rebalance()
except RingBuilderError, err:
self.logger.exception(_("Error during ring validation."))
return self.return_response(False, None, err.message,
start_response, env)
if not parts:
msg = 'Either none need to be assigned or none can be due ' \
'to min_part_hours [%s].' % builder.min_part_hours
self.logger.error(_(msg))
return self.return_response(False, None, msg, start_response,
env)
if not devs_changed and abs(last_balance - balance) < 1:
msg = 'Refusing to save rebalance. Did not change at least 1%.'
self.logger.error(_(msg))
return self.return_response(False, None, msg, start_response,
env)
try:
builder.validate()
except RingValidationError, err:
self.logger.exception(_("Error during ring validation."))
return self.return_response(False, None, err.message,
start_response, env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:33,代码来源:middleware.py
示例11: test_validate
def test_validate(self):
self.create_sample_ring()
ring = RingBuilder.load(self.tmpfile)
ring.rebalance()
ring.save(self.tmpfile)
argv = ["", self.tmpfile, "validate"]
self.assertRaises(SystemExit, swift.cli.ringbuilder.main, argv)
开发者ID:steveruckdashel,项目名称:swift,代码行数:7,代码来源:test_ringbuilder.py
示例12: test_create_ring
def test_create_ring(self):
argv = ["", self.tmpfile, "create", "6", "3.14159265359", "1"]
self.assertRaises(SystemExit, swift.cli.ringbuilder.main, argv)
ring = RingBuilder.load(self.tmpfile)
self.assertEqual(ring.part_power, 6)
self.assertEqual(ring.replicas, 3.14159265359)
self.assertEqual(ring.min_part_hours, 1)
开发者ID:steveruckdashel,项目名称:swift,代码行数:7,代码来源:test_ringbuilder.py
示例13: write_ring
def write_ring(args, devices, builderfile):
# Make an educated guess about the used port. These are the defaults for
# TripleO-based deployments in Mitaka
builder_fname = os.path.basename(builderfile)
if 'account' in builder_fname:
port = 6002
elif 'container' in builder_fname:
port = 6001
elif 'object' in builder_fname:
port = 6000
else:
port = 6000
logging.debug('Set port for new devices to %d' % port)
if not os.path.isfile(builderfile):
logging.info(
'%s not found, creating new builder file', builderfile)
rb = RingBuilder(args.part_power, args.replicas, args.min_part_hours)
else:
logging.info('Using existing builder file %s', builderfile)
rb = RingBuilder.load(builderfile)
# Add all missing devices
for dev in devices:
_dev = rb.search_devs({'meta': dev['meta']})
if not _dev:
dev['weight'] = float(dev.get('size')) / 10**9
dev['region'] = 1
dev['zone'] = 1
dev['port'] = port
dev['replication_ip'] = dev['ip']
dev['replication_port'] = dev['port']
rb.add_dev(dev)
logging.info('Added device %s / %s', dev['ip'], dev['device'])
else:
logging.info(
'Ignoring existing device %s / %s', dev['ip'], dev['device'])
try:
rb.rebalance()
except RingValidationError as exc:
logging.error(exc)
rb.save(builderfile)
ring_file = os.path.splitext(builderfile)[0] + '.ring.gz'
ring_data = rb.get_ring()
ring_data.save(ring_file)
return [builderfile, ring_file]
开发者ID:cschwede,项目名称:tripleo-swift-ring-tool,代码行数:47,代码来源:tripleo_swift_ring_tool.py
示例14: storage_policies
def storage_policies(request):
"""
Creates a storage policy to swift with an specific ring.
Allows create replication storage policies and erasure code storage policies
"""
if request.method == "GET":
try:
r = get_redis_connection()
except RedisError:
return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR)
keys = r.keys("storage-policy:*")
storage_policy_list = []
for key in keys:
storage_policy = r.hgetall(key)
to_json_bools(storage_policy, 'deprecated', 'default', 'deployed')
storage_policy['id'] = str(key).split(':')[-1]
storage_policy['devices'] = json.loads(storage_policy['devices'])
storage_policy_list.append(storage_policy)
return JSONResponse(storage_policy_list, status=status.HTTP_200_OK)
if request.method == "POST":
try:
r = get_redis_connection()
except RedisError:
return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR)
data = JSONParser().parse(request)
if data['policy_type'] == 'EC':
data['replicas'] = int(data['ec_num_data_fragments']) + int(data['ec_num_parity_fragments'])
try:
sp_id = str(r.incr('storage-policies:id'))
key = 'storage-policy:' + sp_id
ring = RingBuilder(int(data['partition_power']), int(data['replicas']), int(data['time']))
ring.save(get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, sp_id))
r.hmset(key, data)
except:
return JSONResponse('Error creating the Storage Policy', status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return JSONResponse('Account created successfully', status=status.HTTP_201_CREATED)
return JSONResponse('Only HTTP POST requests allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
开发者ID:Crystal-SDS,项目名称:Crystal-Controller,代码行数:47,代码来源:views.py
示例15: test_load
def test_load(self):
rb = ring.RingBuilder(8, 3, 1)
devs = [
{"id": 0, "zone": 0, "weight": 1, "ip": "127.0.0.0", "port": 10000, "device": "sda1", "meta": "meta0"},
{"id": 1, "zone": 1, "weight": 1, "ip": "127.0.0.1", "port": 10001, "device": "sdb1", "meta": "meta1"},
{"id": 2, "zone": 2, "weight": 2, "ip": "127.0.0.2", "port": 10002, "device": "sdc1", "meta": "meta2"},
{"id": 3, "zone": 3, "weight": 2, "ip": "127.0.0.3", "port": 10003, "device": "sdd1"},
]
for d in devs:
rb.add_dev(d)
rb.rebalance()
real_pickle = pickle.load
try:
# test a legit builder
fake_pickle = Mock(return_value=rb)
fake_open = Mock(return_value=None)
pickle.load = fake_pickle
builder = RingBuilder.load("fake.builder", open=fake_open)
self.assertEquals(fake_pickle.call_count, 1)
fake_open.assert_has_calls([mock_call("fake.builder", "rb")])
self.assertEquals(builder, rb)
fake_pickle.reset_mock()
fake_open.reset_mock()
# test old style builder
fake_pickle.return_value = rb.to_dict()
pickle.load = fake_pickle
builder = RingBuilder.load("fake.builder", open=fake_open)
fake_open.assert_has_calls([mock_call("fake.builder", "rb")])
self.assertEquals(builder.devs, rb.devs)
fake_pickle.reset_mock()
fake_open.reset_mock()
# test old devs but no meta
no_meta_builder = rb
for dev in no_meta_builder.devs:
del (dev["meta"])
fake_pickle.return_value = no_meta_builder
pickle.load = fake_pickle
builder = RingBuilder.load("fake.builder", open=fake_open)
fake_open.assert_has_calls([mock_call("fake.builder", "rb")])
self.assertEquals(builder.devs, rb.devs)
fake_pickle.reset_mock()
finally:
pickle.load = real_pickle
开发者ID:ChristopherMacGown,项目名称:swift,代码行数:46,代码来源:test_builder.py
示例16: create_sample_ring
def create_sample_ring(self):
""" Create a sample ring with two devices
At least two devices are needed to test removing
a device, since removing the last device of a ring
is not allowed """
# Ensure there is no existing test builder file because
# create_sample_ring() might be used more than once in a single test
try:
os.remove(self.tmpfile)
except OSError:
pass
ring = RingBuilder(6, 3, 1)
ring.add_dev(
{
"weight": 100.0,
"region": 0,
"zone": 0,
"ip": "127.0.0.1",
"port": 6000,
"device": "sda1",
"meta": "some meta data",
}
)
ring.add_dev({"weight": 100.0, "region": 1, "zone": 1, "ip": "127.0.0.2", "port": 6001, "device": "sda2"})
ring.save(self.tmpfile)
开发者ID:heemanshu,项目名称:swift_juno,代码行数:28,代码来源:test_ringbuilder.py
示例17: create_sample_ring
def create_sample_ring(self):
""" Create a sample ring with two devices
At least two devices are needed to test removing
a device, since removing the last device of a ring
is not allowed """
# Ensure there is no existing test builder file because
# create_sample_ring() might be used more than once in a single test
try:
os.remove(self.tmpfile)
except OSError:
pass
ring = RingBuilder(6, 3, 1)
ring.add_dev({'weight': 100.0,
'region': 0,
'zone': 0,
'ip': '127.0.0.1',
'port': 6000,
'device': 'sda1',
'meta': 'some meta data',
})
ring.add_dev({'weight': 100.0,
'region': 1,
'zone': 1,
'ip': '127.0.0.2',
'port': 6001,
'device': 'sda2'
})
ring.save(self.tmpfile)
开发者ID:steveruckdashel,项目名称:swift,代码行数:31,代码来源:test_ringbuilder.py
示例18: gen_builder
def gen_builder(self, balanced=False):
builder = RingBuilder(18, 3, 1)
for i in xrange(self.device_count):
zone = i
ipaddr = "1.1.1.1"
port = 6010
device_name = "sd%s" % i
weight = 100.0
meta = "meta for %s" % i
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': ipaddr,
'port': int(port), 'device': device_name,
'weight': weight, 'meta': meta})
if balanced:
builder.rebalance()
return builder
开发者ID:leoh0,项目名称:swift-ring-master,代码行数:18,代码来源:test_ringmasterwsgi.py
示例19: main
def main(arguments=None):
global argv, backup_dir, builder, builder_file, ring_file
if arguments:
argv = arguments
else:
argv = sys_argv
if len(argv) < 2:
print "swift-ring-builder %(MAJOR_VERSION)s.%(MINOR_VERSION)s\n" % \
globals()
print Commands.default.__doc__.strip()
print
cmds = [c for c, f in Commands.__dict__.iteritems()
if f.__doc__ and c[0] != '_' and c != 'default']
cmds.sort()
for cmd in cmds:
print Commands.__dict__[cmd].__doc__.strip()
print
print parse_search_value.__doc__.strip()
print
for line in wrap(' '.join(cmds), 79, initial_indent='Quick list: ',
subsequent_indent=' '):
print line
print('Exit codes: 0 = operation successful\n'
' 1 = operation completed with warnings\n'
' 2 = error')
exit(EXIT_SUCCESS)
builder_file, ring_file = parse_builder_ring_filename_args(argv)
if exists(builder_file):
builder = RingBuilder.load(builder_file)
elif len(argv) < 3 or argv[2] not in('create', 'write_builder'):
print 'Ring Builder file does not exist: %s' % argv[1]
exit(EXIT_ERROR)
backup_dir = pathjoin(dirname(argv[1]), 'backups')
try:
mkdir(backup_dir)
except OSError as err:
if err.errno != EEXIST:
raise
if len(argv) == 2:
command = "default"
else:
command = argv[2]
if argv[0].endswith('-safe'):
try:
with lock_parent_directory(abspath(argv[1]), 15):
Commands.__dict__.get(command, Commands.unknown.im_func)()
except exceptions.LockTimeout:
print "Ring/builder dir currently locked."
exit(2)
else:
Commands.__dict__.get(command, Commands.unknown.im_func)()
开发者ID:701,项目名称:swift,代码行数:56,代码来源:ringbuilder.py
示例20: list_devices
def list_devices(self, builder_type, start_response, env):
""" list ALL devices in the ring
:params builder_type: the builder_type to use when loading the builder
:returns: list of boolean status, md5sum of the current ring, and all
builder.devs
"""
with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
builder = RingBuilder.load(self.bf_path[builder_type])
current_md5sum = self._get_md5sum(self.bf_path[builder_type])
return self.return_response(True, current_md5sum, builder.devs,
start_response, env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:12,代码来源:middleware.py
注:本文中的swift.common.ring.RingBuilder类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论