Created
February 28, 2018 15:38
-
-
Save clayg/e347f44dc7ce6bfdbe2db473e874fb46 to your computer and use it in GitHub Desktop.
saio rebalance simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from argparse import ArgumentParser | |
from swift.common.ring import RingData | |
import random | |
parser = ArgumentParser() | |
parser.add_argument('ring', help='the ring') | |
parser.add_argument('part', type=int, help='the part') | |
parser.add_argument('replica', nargs='?', type=int, help='the replica') | |
parser.add_argument('--dev-id', type=int, help='the dev_id') | |
def main(): | |
args = parser.parse_args() | |
rd = RingData.load(args.ring) | |
all_dev_ids = set(d['id'] for d in rd.devs if d is not None) | |
in_use_dev_ids = [parts[args.part] for parts in rd._replica2part2dev_id] | |
if args.dev_id is None: | |
dev_id = random.choice(list(all_dev_ids - set(in_use_dev_ids))) | |
else: | |
if args.dev_id in in_use_dev_ids: | |
raise Exception('part %r already uses %d in %s' % ( | |
args.part, args.dev_id, in_use_dev_ids)) | |
dev_id = args.dev_id | |
if args.replica is None: | |
replica = random.randint(0, len(in_use_dev_ids) - 1) | |
else: | |
replica = args.replica | |
rd._replica2part2dev_id[replica][args.part] = dev_id | |
rd.save(args.ring) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment