| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
import json, subprocess, sys |
| 42 |
|
| 43 |
def eprint(*args, **kwargs): |
| 44 |
print(*args, file=sys.stderr, **kwargs) |
| 45 |
|
| 46 |
try: |
| 47 |
OSDS = json.loads(subprocess.getoutput('ceph osd ls -f json')) |
| 48 |
DF = json.loads(subprocess.getoutput('ceph osd df -f json | jq .nodes')) |
| 49 |
except ValueError: |
| 50 |
eprint('Error loading OSD IDs') |
| 51 |
sys.exit(1) |
| 52 |
|
| 53 |
ignore_backfilling = False |
| 54 |
for arg in sys.argv[1:]: |
| 55 |
if arg == "--ignore-backfilling": |
| 56 |
eprint ("All actively backfilling PGs will be ignored.") |
| 57 |
ignore_backfilling = True |
| 58 |
|
| 59 |
def crush_weight(id): |
| 60 |
for o in DF: |
| 61 |
if o['id'] == id: |
| 62 |
return o['crush_weight'] * o['reweight'] |
| 63 |
return 0 |
| 64 |
|
| 65 |
def gen_upmap(up, acting, replicated=False): |
| 66 |
assert(len(up) == len(acting)) |
| 67 |
pairs = [] |
| 68 |
for p in zip(up, acting): |
| 69 |
if p[0] != p[1] and p[0] in OSDS and crush_weight(p[1]) > 0: |
| 70 |
pairs.append(p) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
if replicated: |
| 75 |
p = list(pairs) |
| 76 |
u = set([x[0] for x in p]) |
| 77 |
a = set([x[1] for x in p]) |
| 78 |
pairs = list(zip(u-a, a-u)) |
| 79 |
return pairs |
| 80 |
|
| 81 |
def upmap_pg_items(pgid, mapping): |
| 82 |
if len(mapping): |
| 83 |
print('ceph osd pg-upmap-items %s ' % pgid, end='') |
| 84 |
for pair in mapping: |
| 85 |
print('%s %s ' % pair, end='') |
| 86 |
print('&') |
| 87 |
|
| 88 |
def rm_upmap_pg_items(pgid): |
| 89 |
print('ceph osd rm-pg-upmap-items %s &' % pgid) |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
try: |
| 96 |
remapped_json = subprocess.getoutput('ceph pg ls remapped -f json') |
| 97 |
remapped = json.loads(remapped_json) |
| 98 |
except ValueError: |
| 99 |
eprint('Error loading remapped pgs') |
| 100 |
sys.exit(1) |
| 101 |
|
| 102 |
|
| 103 |
try: |
| 104 |
_remapped = remapped['pg_stats'] |
| 105 |
remapped = _remapped |
| 106 |
except KeyError: |
| 107 |
eprint("There are no remapped PGs") |
| 108 |
sys.exit(0) |
| 109 |
|
| 110 |
|
| 111 |
osd_dump_json = subprocess.getoutput('ceph osd dump -f json') |
| 112 |
osd_dump = json.loads(osd_dump_json) |
| 113 |
upmaps = osd_dump['pg_upmap_items'] |
| 114 |
|
| 115 |
|
| 116 |
pool_type = {} |
| 117 |
try: |
| 118 |
for line in subprocess.getoutput('ceph osd pool ls detail').split('\n'): |
| 119 |
if 'pool' in line: |
| 120 |
x = line.split(' ') |
| 121 |
pool_type[x[1]] = x[3] |
| 122 |
except: |
| 123 |
eprint('Error parsing pool types') |
| 124 |
sys.exit(1) |
| 125 |
|
| 126 |
|
| 127 |
has_upmap = {} |
| 128 |
for pg in upmaps: |
| 129 |
pgid = str(pg['pgid']) |
| 130 |
has_upmap[pgid] = True |
| 131 |
|
| 132 |
|
| 133 |
print('while ceph status | grep -q "peering\|activating"; do sleep 2; done') |
| 134 |
num = 0 |
| 135 |
for pg in remapped: |
| 136 |
if num == 50: |
| 137 |
print('wait; sleep 4; while ceph status | grep -q "peering\|activating"; do sleep 2; done') |
| 138 |
num = 0 |
| 139 |
|
| 140 |
if ignore_backfilling: |
| 141 |
if "backfilling" in pg['state']: |
| 142 |
continue |
| 143 |
|
| 144 |
pgid = pg['pgid'] |
| 145 |
|
| 146 |
try: |
| 147 |
if has_upmap[pgid]: |
| 148 |
rm_upmap_pg_items(pgid) |
| 149 |
num += 1 |
| 150 |
continue |
| 151 |
except KeyError: |
| 152 |
pass |
| 153 |
|
| 154 |
up = pg['up'] |
| 155 |
acting = pg['acting'] |
| 156 |
pool = pgid.split('.')[0] |
| 157 |
if pool_type[pool] == 'replicated': |
| 158 |
try: |
| 159 |
pairs = gen_upmap(up, acting, replicated=True) |
| 160 |
except: |
| 161 |
continue |
| 162 |
elif pool_type[pool] == 'erasure': |
| 163 |
try: |
| 164 |
pairs = gen_upmap(up, acting) |
| 165 |
except: |
| 166 |
continue |
| 167 |
else: |
| 168 |
eprint('Unknown pool type for %s' % pool) |
| 169 |
sys.exit(1) |
| 170 |
upmap_pg_items(pgid, pairs) |
| 171 |
num += 1 |
| 172 |
|
| 173 |
print('wait; sleep 4; while ceph status | grep -q "peering\|activating"; do sleep 2; done') |