Fix a couple bugs; rework the rework a bit more; PEP8

This commit is contained in:
gholt
2011-01-04 12:04:18 -08:00
parent c97e7b6054
commit 68cc8fba9d

View File

@@ -20,6 +20,7 @@ from gzip import GzipFile
from os import mkdir
from os.path import basename, dirname, exists, join as pathjoin
from sys import argv, exit
from textwrap import wrap
from time import time
from swift.common.ring import RingBuilder
@@ -29,11 +30,12 @@ MAJOR_VERSION = 1
MINOR_VERSION = 2
EXIT_RING_CHANGED = 0
EXIT_RING_UNCHANGED = 1
EXIT_ERROR = 2
EXIT_ERROR = 2
def search_devs(builder, search_value):
"""The <search-value> can be of the form:
"""
The <search-value> can be of the form:
d<device_id>z<zone>-<ip>:<port>/<device_name>_<meta>
Any part is optional, but you must include at least one part.
Examples:
@@ -50,8 +52,8 @@ def search_devs(builder, search_value):
d74z1-1.2.3.4:5678/sdb1_"snet: 5.6.7.8"
Nerd explanation:
All items require their single character prefix except the ip, in which
case the - is optional unless the device id or zone is also included."""
case the - is optional unless the device id or zone is also included.
"""
orig_search_value = search_value
match = []
if search_value.startswith('d'):
@@ -90,7 +92,8 @@ def search_devs(builder, search_value):
match.append(('meta', search_value[1:]))
search_value = ''
if search_value:
raise ValueError('Invalid <search-value>: %s' % repr(orig_search_value))
raise ValueError('Invalid <search-value>: %s' %
repr(orig_search_value))
devs = []
for dev in builder.devs:
if not dev:
@@ -107,20 +110,22 @@ def search_devs(builder, search_value):
return devs
class commands:
class Commands:
def unknown():
print 'Unknown command: %s' % argv[2]
exit(EXIT_ERROR)
print 'Unknown command: %s' % argv[2]
exit(EXIT_ERROR)
def create():
"""swift-ring-builder <builder_file> create <part_power> <replicas> <min_part_hours>
"""
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."""
than once.
"""
if len(argv) < 6:
print commands.create.__doc__
print Commands.create.__doc__.strip()
exit(EXIT_RING_UNCHANGED)
builder = RingBuilder(int(argv[3]), int(argv[4]), int(argv[5]))
backup_dir = pathjoin(dirname(argv[1]), 'backups')
@@ -135,8 +140,10 @@ class commands:
exit(EXIT_RING_CHANGED)
def default():
"""swift-ring-builder <builder_file>
Shows information about the ring and the devices within."""
"""
swift-ring-builder <builder_file>
Shows information about the ring and the devices within.
"""
print '%s, build version %d' % (argv[1], builder.version)
zones = 0
balance = 0
@@ -171,11 +178,14 @@ class commands:
exit(EXIT_RING_UNCHANGED)
def search():
"""swift-ring-builder <builder_file> search <search-value>
Shows information about matching devices."""
"""
swift-ring-builder <builder_file> search <search-value>
Shows information about matching devices.
"""
if len(argv) < 4:
print commands.search.__doc__ + "\n\n" + search_devs.__doc__
print Commands.search.__doc__.strip()
print
print search_devs.__doc__.strip()
exit(EXIT_RING_UNCHANGED)
devs = search_devs(builder, argv[3])
if not devs:
@@ -200,14 +210,16 @@ class commands:
dev['meta'])
exit(EXIT_RING_UNCHANGED)
def add():
"""swift-ring-builder <builder_file> add z<zone>-<ip>:<port>/<device_name>_<meta> <wght>
def add():
"""
swift-ring-builder <builder_file> add z<zone>-<ip>:<port>/<device_name>_<meta>
<wght>
Adds a device to the ring with the given information. No partitions will be
assigned to the new device until after running 'rebalance'. This is so you
can make multiple device changes and rebalance them all just once."""
can make multiple device changes and rebalance them all just once.
"""
if len(argv) < 5:
print commands.add.__doc__
print Commands.add.__doc__.strip()
exit(EXIT_RING_UNCHANGED)
if not argv[3].startswith('z'):
@@ -273,13 +285,16 @@ class commands:
exit(EXIT_RING_UNCHANGED)
def set_weight():
"""swift-ring-builder <builder_file> set_weight <search-value> <weight>
"""
swift-ring-builder <builder_file> set_weight <search-value> <weight>
Resets the device's weight. No partitions will be reassigned to or from the
device until after running 'rebalance'. This is so you can make multiple
device changes and rebalance them all just once."""
device changes and rebalance them all just once.
"""
if len(argv) != 5:
print commands.set_weight.__doc__ + "\n\n" + search_devs.__doc__
print Commands.set_weight.__doc__.strip()
print
print search_devs.__doc__.strip()
exit(EXIT_RING_UNCHANGED)
devs = search_devs(builder, argv[3])
weight = float(argv[4])
@@ -303,16 +318,20 @@ class commands:
exit(EXIT_RING_UNCHANGED)
def set_info():
"""swift-ring-builder <builder_file> set_info <search-value>
<ip>:<port>/<device_name>_<meta>
"""
swift-ring-builder <builder_file> set_info <search-value>
<ip>:<port>/<device_name>_<meta>
Resets the device's information. This information isn't used to assign
partitions, so you can use 'write_ring' afterward to rewrite the current
ring with the newer device information. Any of the parts are optional
in the final <ip>:<port>/<device_name>_<meta> parameter; just give what you
want to change. For instance set_info d74 _"snet: 5.6.7.8" would just
update the meta data for device id 74."""
update the meta data for device id 74.
"""
if len(argv) != 5:
print commands.set_info.__doc__ + "\n\n" + search_devs.__doc__
print Commands.set_info.__doc__.strip()
print
print search_devs.__doc__.strip()
exit(EXIT_RING_UNCHANGED)
devs = search_devs(builder, argv[3])
change_value = argv[4]
@@ -378,15 +397,19 @@ class commands:
exit(EXIT_RING_UNCHANGED)
def remove():
"""swift-ring-builder <builder_file> remove <search-value>
"""
swift-ring-builder <builder_file> remove <search-value>
Removes the device(s) from the ring. This should normally just be used for
a device that has failed. For a device you wish to decommission, it's best
to set its weight to 0, wait for it to drain all its data, then use this
remove command. This will not take effect until after running 'rebalance'.
This is so you can make multiple device changes and rebalance them all just
once."""
once.
"""
if len(argv) < 4:
print commands.remove.__doc__ + "\n\n" + search_devs.__doc__
print Commands.remove.__doc__.strip()
print
print search_devs.__doc__.strip()
exit(EXIT_RING_UNCHANGED)
devs = search_devs(builder, argv[3])
if not devs:
@@ -404,15 +427,17 @@ class commands:
for dev in devs:
builder.remove_dev(dev['id'])
print 'd%(id)sz%(zone)s-%(ip)s:%(port)s/%(device)s_"%(meta)s" ' \
'marked for removal and will be removed next rebalance.' % dev
'marked for removal and will be removed next rebalance.' \
% dev
pickle.dump(builder, open(argv[1], 'wb'), protocol=2)
exit(EXIT_RING_UNCHANGED)
def rebalance():
"""swift-ring-builder <builder_file> rebalance
"""
swift-ring-builder <builder_file> rebalance
Attempts to rebalance the ring by reassigning partitions that haven't been
recently reassigned."""
recently reassigned.
"""
devs_changed = builder.devs_changed
last_balance = builder.get_balance()
parts, balance = builder.rebalance()
@@ -446,25 +471,29 @@ class commands:
exit(EXIT_RING_CHANGED)
def validate():
"""swift-ring-builder <builder_file> validate
Just runs the validation routines on the ring."""
"""
swift-ring-builder <builder_file> validate
Just runs the validation routines on the ring.
"""
builder.validate()
exit(EXIT_RING_UNCHANGED)
def write_ring():
"""swift-ring-builder <builder_file> write_ring
"""
swift-ring-builder <builder_file> write_ring
Just rewrites the distributable ring file. This is done automatically after
a successful rebalance, so really this is only useful after one or more
'set_info' calls when no rebalance is needed but you want to send out the
new device information."""
new device information.
"""
ring_data = builder.get_ring()
if not ring_data._replica2part2dev_id:
if ring_data.devs:
print 'Warning: Writing a ring with no partition assignments but with devices; did you forget to run "rebalance"?'
else:
print 'Warning: Writing an empty ring'
if ring_data.devs:
print 'Warning: Writing a ring with no partition ' \
'assignments but with devices; did you forget to run ' \
'"rebalance"?'
else:
print 'Warning: Writing an empty ring'
pickle.dump(ring_data,
GzipFile(pathjoin(backup_dir, '%d.' % time() +
basename(ring_file)), 'wb'), protocol=2)
@@ -477,13 +506,14 @@ class commands:
exit(EXIT_RING_UNCHANGED)
def set_min_part_hours():
"""swift-ring-builder <builder_file> set_min_part_hours <hours>
"""
swift-ring-builder <builder_file> set_min_part_hours <hours>
Changes the <min_part_hours> to the given <hours>. This should be set to
however long a full replication/update cycle takes. We're working on a way
to determine this more easily than scanning logs."""
to determine this more easily than scanning logs.
"""
if len(argv) < 4:
print commands.set_min_part_hours.__doc__
print Commands.set_min_part_hours.__doc__.strip()
exit(EXIT_RING_UNCHANGED)
builder.change_min_part_hours(int(argv[3]))
print 'The minimum number of hours before a partition can be ' \
@@ -494,19 +524,23 @@ class commands:
if __name__ == '__main__':
if len(argv) < 2:
print """swift-ring-builder %(MAJOR_VERSION)s.%(MINOR_VERSION)s
Quick list: create search add set_weight set_info remove rebalance write_ring
set_min_part_hours
Exit codes: 0 = ring changed, 1 = ring did not change, 2 = error
""" % globals()
cmds = commands.__dict__.keys()
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:
doc = commands.__dict__[cmd].__doc__
if doc is not None: print doc,"\n"
print search_devs.__doc__
print Commands.__dict__[cmd].__doc__.strip()
print
print search_devs.__doc__.strip()
print
for line in wrap(' '.join(cmds), 79, initial_indent='Quick list: ',
subsequent_indent=' '):
print line
print 'Exit codes: 0 = ring changed, 1 = ring did not change, ' \
'2 = error'
exit(EXIT_RING_UNCHANGED)
if exists(argv[1]):
@@ -531,8 +565,7 @@ Exit codes: 0 = ring changed, 1 = ring did not change, 2 = error
ring_file += '.ring.gz'
if len(argv) == 2:
command = "default"
command = "default"
else:
command = argv[2]
commands.__dict__.get(command, commands.unknown)()
command = argv[2]
Commands.__dict__.get(command, Commands.unknown)()