Skip to content

Instantly share code, notes, and snippets.

@clayg
Created January 13, 2017 05:56
Show Gist options
  • Save clayg/90143abc1c34e259752bf333f485a37e to your computer and use it in GitHub Desktop.
Save clayg/90143abc1c34e259752bf333f485a37e to your computer and use it in GitHub Desktop.
import os
import errno
from collections import defaultdict
from swift.common.storage_policy import POLICIES
from swift.obj.diskfile import get_data_dir
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-v', '--verbose', help='line oriented output',
default=False, action='store_true')
parser.add_argument('devices', help='root of devices tree for node',
default='/srv/node')
parser.add_argument('--policy-index', help='index of policy to check',
default=0, type=int)
parser.add_argument('--limit', help='max number of handoff parts to output',
default=None, type=int)
args = parser.parse_args()
device_root = args.devices
policy = POLICIES[args.policy_index]
policy.load_ring('/etc/swift')
r = policy.object_ring
dev2parts = defaultdict(set)
for replica, part2dev in enumerate(r._replica2part2dev_id):
for part, device_id in enumerate(part2dev):
dev2parts[r.devs[device_id]['device']].add(part)
# print dev2parts
handoffs = defaultdict(set)
device_dirs = os.listdir(device_root)
data_dir = get_data_dir(policy)
for device_dir in device_dirs:
parts_dir = os.path.join(device_root, device_dir, data_dir)
try:
parts = os.listdir(parts_dir)
except OSError as e:
if e.errno == errno.ENOENT:
continue
else:
raise
for part in parts:
if not part.isdigit():
continue
part = int(part)
if part in dev2parts[device_dir]:
continue
handoffs[device_dir].add(part)
for device, parts in handoffs.items():
if args.limit is not None:
parts = list(parts)[:args.limit]
if args.verbose:
print os.path.join(device_root, device)
for part in parts:
print ' ', part
else:
print '-d %s -p %s' % (device, ','.join(str(p) for p in parts))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment