Debian Codegraph @

bin/ct-upmap-remapped

raw symbols SCIP
1 #!/usr/bin/env python3
2 #
3 # DISCLAIMER: THIS SCRIPT COMES WITH NO WARRANTY OR GUARANTEE
4 # OF ANY KIND.
5 #
6 # DISCLAIMER 2: THIS TOOL USES A CEPH FEATURE MARKED "(developers only)"
7 # YOU SHOULD NOT RUN THIS UNLESS YOU KNOW EXACTLY HOW THOSE
8 # FUNCTIONALITIES WORK.
9 #
10 # upmap-remapped.py
11 #
12 # Usage (print only): ./upmap-remapped.py
13 # Usage (production): ./upmap-remapped.py | sh
14 #
15 # Optional to ignore PGs that are backfilling and not backfill+wait:
16 # Usage: ./upmap-remapped.py --ignore-backfilling
17 #
18 # This tool will use ceph's pg-upmap-items functionality to
19 # quickly modify all PGs which are currently remapped to become
20 # active+clean. I use it in combination with the ceph-mgr upmap
21 # balancer and the norebalance state for these use-cases:
22 #
23 # - Change crush rules or tunables.
24 # - Adding capacity (add new host, rack, ...).
25 #
26 # In general, the correct procedure for using this script is:
27 #
28 # 1. Backup your osdmaps, crush maps, ...
29 # 2. Set the norebalance flag.
30 # 3. Make your change (tunables, add osds, etc...)
31 # 4. Run this script a few times. (Remember to | sh)
32 # 5. Cluster should now be 100% active+clean.
33 # 6. Unset the norebalance flag.
34 # 7. The ceph-mgr balancer in upmap mode should now gradually
35 # remove the upmap-items entries which were created by this
36 # tool.
37 #
38 # Hacked by: Dan van der Ster <[email protected]>
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 # if replicated, remove indirect mappings
73 # e.g. ceph osd pg-upmap-items 4.5fd 603 383 499 804 804 530 &
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 # start here
93
94 # discover remapped pgs
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 # nautilus compat
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 # discover existing upmaps
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 # discover pools replicated or erasure
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 # discover if each pg is already upmapped
127 has_upmap = {}
128 for pg in upmaps:
129 pgid = str(pg['pgid'])
130 has_upmap[pgid] = True
131
132 # handle each remapped pg
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')