Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@matthewoliver
Last active January 19, 2018 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewoliver/ce5172dbf1bcf8143184686517ad67ca to your computer and use it in GitHub Desktop.
Save matthewoliver/ce5172dbf1bcf8143184686517ad67ca to your computer and use it in GitHub Desktop.
import sys
from swift.common.ring.builder import RingBuilder
from swift.common.ring import Ring
import optparse
class PartDiff(object):
def _load_file(self, filename):
if filename.endswith('.gz'):
builder = Ring(filename)
builder.parts = builder.partition_count
builder.replicas = float(builder.replica_count)
builder._replica2part2dev = builder._replica2part2dev_id
else:
builder = RingBuilder.load(filename)
return builder
def _get_max_dev_id(self, builder):
return max(map(lambda x: x['id'], builder.devs))
def main(self):
usage = "%prog [--verbose] [--diffs] <builder1> <builder2>"
args = optparse.OptionParser(usage)
args.add_option('--verbose', '-v', action="store_true",
help="Print entire table")
args.add_option('--diffs', '-d', action="store_true",
help="Print only the diffs")
options, arguments = args.parse_args()
if len(arguments) != 2:
args.print_help()
sys.exit(1)
builder1, builder2 = [self._load_file(builder) for builder in
arguments]
if not builder1.replicas == builder2.replicas:
print('Error: Replica counts differ')
sys.exit(2)
max_part_len = len(str(max(builder1.parts, builder2.parts)))
max_dev_len = len(str(max(self._get_max_dev_id(builder1),
self._get_max_dev_id(builder2))))
err_part = '({:>%d})' % max_dev_len
good_part = ' {:>%d} ' % max_dev_len
diff_dev_count = 0
if options.verbose or options.diffs:
# print the header
print('b1: %s\nb2: %s\n' % (arguments[0], arguments[1]))
builder_len = (max_dev_len + 2) * builder1.replicas
num_dash = max_part_len + 4
num_dash += builder_len * 2
head_line = '|{:^%d}|{:^%d}|{:^%d}|' % (max_part_len, builder_len,
builder_len)
print('-' * int(num_dash))
print(head_line.format('P', 'b1', 'b2'))
print('-' * int(num_dash))
for part, (b1r, b2r) in enumerate(zip(zip(*builder1._replica2part2dev),
zip(*builder2._replica2part2dev))):
b1data = []
b2data = []
errors = 0
for a, b in zip(b1r, b2r):
if a == b:
part_str = good_part
else:
errors += 1
part_str = err_part
b1data.append(part_str.format(a))
b2data.append(part_str.format(b))
str_data = "|{:>0%d}|%s|%s|" % \
(max_part_len, "".join(b1data), "".join(b2data))
if errors and options.diffs:
print(str_data.format(part))
elif options.verbose:
print(str_data.format(part))
diff_dev_count += errors
replicanth_diff = diff_dev_count / (builder1.parts * builder1.replicas)
if options.diffs or options.verbose:
print('-' * int(num_dash))
print('')
print("Number of diffs: %s (%.2f%%)" % (diff_dev_count,
replicanth_diff * 100))
if __name__ == "__main__":
partdiff = PartDiff()
partdiff.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment