]> git.proxmox.com Git - ceph.git/blame - ceph/src/osd/OSDMap.cc
import ceph nautilus 14.2.2
[ceph.git] / ceph / src / osd / OSDMap.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 * Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
8 *
9 * Author: Loic Dachary <loic@dachary.org>
10 *
11 * This is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License version 2.1, as published by the Free Software
14 * Foundation. See file COPYING.
15 *
16 */
17
11fdf7f2
TL
18#include <algorithm>
19#include <optional>
20#include <random>
21
224ce89b
WB
22#include <boost/algorithm/string.hpp>
23
7c673cae 24#include "OSDMap.h"
7c673cae 25#include "common/config.h"
3efd9988 26#include "common/errno.h"
7c673cae
FG
27#include "common/Formatter.h"
28#include "common/TextTable.h"
11fdf7f2 29#include "global/global_context.h"
7c673cae
FG
30#include "include/ceph_features.h"
31#include "include/str_map.h"
32
33#include "common/code_environment.h"
224ce89b 34#include "mon/health_check.h"
7c673cae
FG
35
36#include "crush/CrushTreeDumper.h"
37#include "common/Clock.h"
11fdf7f2
TL
38#include "mon/PGMap.h"
39
7c673cae
FG
40#define dout_subsys ceph_subsys_osd
41
42MEMPOOL_DEFINE_OBJECT_FACTORY(OSDMap, osdmap, osdmap);
43MEMPOOL_DEFINE_OBJECT_FACTORY(OSDMap::Incremental, osdmap_inc, osdmap);
44
45
46// ----------------------------------
47// osd_info_t
48
49void osd_info_t::dump(Formatter *f) const
50{
51 f->dump_int("last_clean_begin", last_clean_begin);
52 f->dump_int("last_clean_end", last_clean_end);
53 f->dump_int("up_from", up_from);
54 f->dump_int("up_thru", up_thru);
55 f->dump_int("down_at", down_at);
56 f->dump_int("lost_at", lost_at);
57}
58
59void osd_info_t::encode(bufferlist& bl) const
60{
11fdf7f2 61 using ceph::encode;
7c673cae 62 __u8 struct_v = 1;
11fdf7f2
TL
63 encode(struct_v, bl);
64 encode(last_clean_begin, bl);
65 encode(last_clean_end, bl);
66 encode(up_from, bl);
67 encode(up_thru, bl);
68 encode(down_at, bl);
69 encode(lost_at, bl);
7c673cae
FG
70}
71
11fdf7f2 72void osd_info_t::decode(bufferlist::const_iterator& bl)
7c673cae 73{
11fdf7f2 74 using ceph::decode;
7c673cae 75 __u8 struct_v;
11fdf7f2
TL
76 decode(struct_v, bl);
77 decode(last_clean_begin, bl);
78 decode(last_clean_end, bl);
79 decode(up_from, bl);
80 decode(up_thru, bl);
81 decode(down_at, bl);
82 decode(lost_at, bl);
7c673cae
FG
83}
84
85void osd_info_t::generate_test_instances(list<osd_info_t*>& o)
86{
87 o.push_back(new osd_info_t);
88 o.push_back(new osd_info_t);
89 o.back()->last_clean_begin = 1;
90 o.back()->last_clean_end = 2;
91 o.back()->up_from = 30;
92 o.back()->up_thru = 40;
93 o.back()->down_at = 5;
94 o.back()->lost_at = 6;
95}
96
97ostream& operator<<(ostream& out, const osd_info_t& info)
98{
99 out << "up_from " << info.up_from
100 << " up_thru " << info.up_thru
101 << " down_at " << info.down_at
102 << " last_clean_interval [" << info.last_clean_begin << "," << info.last_clean_end << ")";
103 if (info.lost_at)
104 out << " lost_at " << info.lost_at;
105 return out;
106}
107
108// ----------------------------------
109// osd_xinfo_t
110
111void osd_xinfo_t::dump(Formatter *f) const
112{
113 f->dump_stream("down_stamp") << down_stamp;
114 f->dump_float("laggy_probability", laggy_probability);
115 f->dump_int("laggy_interval", laggy_interval);
116 f->dump_int("features", features);
117 f->dump_unsigned("old_weight", old_weight);
118}
119
120void osd_xinfo_t::encode(bufferlist& bl) const
121{
122 ENCODE_START(3, 1, bl);
11fdf7f2 123 encode(down_stamp, bl);
7c673cae 124 __u32 lp = laggy_probability * 0xfffffffful;
11fdf7f2
TL
125 encode(lp, bl);
126 encode(laggy_interval, bl);
127 encode(features, bl);
128 encode(old_weight, bl);
7c673cae
FG
129 ENCODE_FINISH(bl);
130}
131
11fdf7f2 132void osd_xinfo_t::decode(bufferlist::const_iterator& bl)
7c673cae
FG
133{
134 DECODE_START(3, bl);
11fdf7f2 135 decode(down_stamp, bl);
7c673cae 136 __u32 lp;
11fdf7f2 137 decode(lp, bl);
7c673cae 138 laggy_probability = (float)lp / (float)0xffffffff;
11fdf7f2 139 decode(laggy_interval, bl);
7c673cae 140 if (struct_v >= 2)
11fdf7f2 141 decode(features, bl);
7c673cae
FG
142 else
143 features = 0;
144 if (struct_v >= 3)
11fdf7f2 145 decode(old_weight, bl);
7c673cae
FG
146 else
147 old_weight = 0;
148 DECODE_FINISH(bl);
149}
150
151void osd_xinfo_t::generate_test_instances(list<osd_xinfo_t*>& o)
152{
153 o.push_back(new osd_xinfo_t);
154 o.push_back(new osd_xinfo_t);
155 o.back()->down_stamp = utime_t(2, 3);
156 o.back()->laggy_probability = .123;
157 o.back()->laggy_interval = 123456;
158 o.back()->old_weight = 0x7fff;
159}
160
161ostream& operator<<(ostream& out, const osd_xinfo_t& xi)
162{
163 return out << "down_stamp " << xi.down_stamp
164 << " laggy_probability " << xi.laggy_probability
165 << " laggy_interval " << xi.laggy_interval
166 << " old_weight " << xi.old_weight;
167}
168
169// ----------------------------------
170// OSDMap::Incremental
171
172int OSDMap::Incremental::get_net_marked_out(const OSDMap *previous) const
173{
174 int n = 0;
175 for (auto &weight : new_weight) {
176 if (weight.second == CEPH_OSD_OUT && !previous->is_out(weight.first))
177 n++; // marked out
178 else if (weight.second != CEPH_OSD_OUT && previous->is_out(weight.first))
179 n--; // marked in
180 }
181 return n;
182}
183
184int OSDMap::Incremental::get_net_marked_down(const OSDMap *previous) const
185{
186 int n = 0;
187 for (auto &state : new_state) { //
188 if (state.second & CEPH_OSD_UP) {
189 if (previous->is_up(state.first))
190 n++; // marked down
191 else
192 n--; // marked up
193 }
194 }
195 return n;
196}
197
198int OSDMap::Incremental::identify_osd(uuid_d u) const
199{
200 for (auto &uuid : new_uuid)
201 if (uuid.second == u)
202 return uuid.first;
203 return -1;
204}
205
206int OSDMap::Incremental::propagate_snaps_to_tiers(CephContext *cct,
207 const OSDMap& osdmap)
208{
11fdf7f2 209 ceph_assert(epoch == osdmap.get_epoch() + 1);
7c673cae
FG
210
211 for (auto &new_pool : new_pools) {
212 if (!new_pool.second.tiers.empty()) {
213 pg_pool_t& base = new_pool.second;
214
11fdf7f2
TL
215 auto new_rem_it = new_removed_snaps.find(new_pool.first);
216
7c673cae
FG
217 for (const auto &tier_pool : base.tiers) {
218 const auto &r = new_pools.find(tier_pool);
219 pg_pool_t *tier = 0;
220 if (r == new_pools.end()) {
221 const pg_pool_t *orig = osdmap.get_pg_pool(tier_pool);
222 if (!orig) {
223 lderr(cct) << __func__ << " no pool " << tier_pool << dendl;
224 return -EIO;
225 }
226 tier = get_new_pool(tier_pool, orig);
227 } else {
228 tier = &r->second;
229 }
230 if (tier->tier_of != new_pool.first) {
231 lderr(cct) << __func__ << " " << r->first << " tier_of != " << new_pool.first << dendl;
232 return -EIO;
233 }
234
235 ldout(cct, 10) << __func__ << " from " << new_pool.first << " to "
236 << tier_pool << dendl;
237 tier->snap_seq = base.snap_seq;
238 tier->snap_epoch = base.snap_epoch;
239 tier->snaps = base.snaps;
240 tier->removed_snaps = base.removed_snaps;
11fdf7f2
TL
241 tier->flags |= base.flags & (pg_pool_t::FLAG_SELFMANAGED_SNAPS|
242 pg_pool_t::FLAG_POOL_SNAPS);
243
244 if (new_rem_it != new_removed_snaps.end()) {
245 new_removed_snaps[tier_pool] = new_rem_it->second;
246 }
7c673cae
FG
247 }
248 }
249 }
250 return 0;
251}
252
28e407b8
AA
253// ----------------------------------
254// OSDMap
7c673cae
FG
255
256bool OSDMap::subtree_is_down(int id, set<int> *down_cache) const
257{
258 if (id >= 0)
259 return is_down(id);
260
261 if (down_cache &&
262 down_cache->count(id)) {
263 return true;
264 }
265
266 list<int> children;
267 crush->get_children(id, &children);
268 for (const auto &child : children) {
269 if (!subtree_is_down(child, down_cache)) {
270 return false;
271 }
272 }
273 if (down_cache) {
274 down_cache->insert(id);
275 }
276 return true;
277}
278
279bool OSDMap::containing_subtree_is_down(CephContext *cct, int id, int subtree_type, set<int> *down_cache) const
280{
281 // use a stack-local down_cache if we didn't get one from the
282 // caller. then at least this particular call will avoid duplicated
283 // work.
284 set<int> local_down_cache;
285 if (!down_cache) {
286 down_cache = &local_down_cache;
287 }
288
289 int current = id;
290 while (true) {
291 int type;
292 if (current >= 0) {
293 type = 0;
294 } else {
295 type = crush->get_bucket_type(current);
296 }
11fdf7f2 297 ceph_assert(type >= 0);
7c673cae
FG
298
299 if (!subtree_is_down(current, down_cache)) {
300 ldout(cct, 30) << "containing_subtree_is_down(" << id << ") = false" << dendl;
301 return false;
302 }
303
304 // is this a big enough subtree to be marked as down?
305 if (type >= subtree_type) {
306 ldout(cct, 30) << "containing_subtree_is_down(" << id << ") = true ... " << type << " >= " << subtree_type << dendl;
307 return true;
308 }
309
310 int r = crush->get_immediate_parent_id(current, &current);
311 if (r < 0) {
312 return false;
313 }
314 }
315}
316
224ce89b
WB
317bool OSDMap::subtree_type_is_down(
318 CephContext *cct,
319 int id,
320 int subtree_type,
321 set<int> *down_in_osds,
322 set<int> *up_in_osds,
323 set<int> *subtree_up,
324 unordered_map<int, set<int> > *subtree_type_down) const
31f18b77
FG
325{
326 if (id >= 0) {
327 bool is_down_ret = is_down(id);
328 if (!is_out(id)) {
329 if (is_down_ret) {
330 down_in_osds->insert(id);
331 } else {
332 up_in_osds->insert(id);
333 }
334 }
335 return is_down_ret;
336 }
337
338 if (subtree_type_down &&
339 (*subtree_type_down)[subtree_type].count(id)) {
340 return true;
341 }
342
343 list<int> children;
344 crush->get_children(id, &children);
345 for (const auto &child : children) {
224ce89b
WB
346 if (!subtree_type_is_down(
347 cct, child, crush->get_bucket_type(child),
348 down_in_osds, up_in_osds, subtree_up, subtree_type_down)) {
31f18b77
FG
349 subtree_up->insert(id);
350 return false;
351 }
352 }
353 if (subtree_type_down) {
354 (*subtree_type_down)[subtree_type].insert(id);
355 }
356 return true;
357}
358
7c673cae
FG
359void OSDMap::Incremental::encode_client_old(bufferlist& bl) const
360{
11fdf7f2 361 using ceph::encode;
7c673cae 362 __u16 v = 5;
11fdf7f2
TL
363 encode(v, bl);
364 encode(fsid, bl);
365 encode(epoch, bl);
366 encode(modified, bl);
7c673cae 367 int32_t new_t = new_pool_max;
11fdf7f2
TL
368 encode(new_t, bl);
369 encode(new_flags, bl);
370 encode(fullmap, bl);
371 encode(crush, bl);
7c673cae 372
11fdf7f2
TL
373 encode(new_max_osd, bl);
374 // for encode(new_pools, bl);
7c673cae 375 __u32 n = new_pools.size();
11fdf7f2 376 encode(n, bl);
7c673cae
FG
377 for (const auto &new_pool : new_pools) {
378 n = new_pool.first;
11fdf7f2
TL
379 encode(n, bl);
380 encode(new_pool.second, bl, 0);
7c673cae 381 }
11fdf7f2 382 // for encode(new_pool_names, bl);
7c673cae 383 n = new_pool_names.size();
11fdf7f2 384 encode(n, bl);
7c673cae
FG
385
386 for (const auto &new_pool_name : new_pool_names) {
387 n = new_pool_name.first;
11fdf7f2
TL
388 encode(n, bl);
389 encode(new_pool_name.second, bl);
7c673cae 390 }
11fdf7f2 391 // for encode(old_pools, bl);
7c673cae 392 n = old_pools.size();
11fdf7f2 393 encode(n, bl);
7c673cae
FG
394 for (auto &old_pool : old_pools) {
395 n = old_pool;
11fdf7f2 396 encode(n, bl);
7c673cae 397 }
11fdf7f2 398 encode(new_up_client, bl, 0);
31f18b77
FG
399 {
400 // legacy is map<int32_t,uint8_t>
401 uint32_t n = new_state.size();
11fdf7f2 402 encode(n, bl);
31f18b77 403 for (auto p : new_state) {
11fdf7f2
TL
404 encode(p.first, bl);
405 encode((uint8_t)p.second, bl);
31f18b77
FG
406 }
407 }
11fdf7f2
TL
408 encode(new_weight, bl);
409 // for encode(new_pg_temp, bl);
7c673cae 410 n = new_pg_temp.size();
11fdf7f2 411 encode(n, bl);
7c673cae
FG
412
413 for (const auto &pg_temp : new_pg_temp) {
414 old_pg_t opg = pg_temp.first.get_old_pg();
11fdf7f2
TL
415 encode(opg, bl);
416 encode(pg_temp.second, bl);
7c673cae
FG
417 }
418}
419
420void OSDMap::Incremental::encode_classic(bufferlist& bl, uint64_t features) const
421{
11fdf7f2 422 using ceph::encode;
7c673cae
FG
423 if ((features & CEPH_FEATURE_PGID64) == 0) {
424 encode_client_old(bl);
425 return;
426 }
427
428 // base
429 __u16 v = 6;
11fdf7f2
TL
430 encode(v, bl);
431 encode(fsid, bl);
432 encode(epoch, bl);
433 encode(modified, bl);
434 encode(new_pool_max, bl);
435 encode(new_flags, bl);
436 encode(fullmap, bl);
437 encode(crush, bl);
438
439 encode(new_max_osd, bl);
440 encode(new_pools, bl, features);
441 encode(new_pool_names, bl);
442 encode(old_pools, bl);
443 encode(new_up_client, bl, features);
31f18b77
FG
444 {
445 uint32_t n = new_state.size();
11fdf7f2 446 encode(n, bl);
31f18b77 447 for (auto p : new_state) {
11fdf7f2
TL
448 encode(p.first, bl);
449 encode((uint8_t)p.second, bl);
31f18b77
FG
450 }
451 }
11fdf7f2
TL
452 encode(new_weight, bl);
453 encode(new_pg_temp, bl);
7c673cae
FG
454
455 // extended
456 __u16 ev = 10;
11fdf7f2
TL
457 encode(ev, bl);
458 encode(new_hb_back_up, bl, features);
459 encode(new_up_thru, bl);
460 encode(new_last_clean_interval, bl);
461 encode(new_lost, bl);
462 encode(new_blacklist, bl, features);
463 encode(old_blacklist, bl, features);
464 encode(new_up_cluster, bl, features);
465 encode(cluster_snapshot, bl);
466 encode(new_uuid, bl);
467 encode(new_xinfo, bl);
468 encode(new_hb_front_up, bl, features);
469}
470
471template<class T>
472static void encode_addrvec_map_as_addr(const T& m, bufferlist& bl, uint64_t f)
473{
474 uint32_t n = m.size();
475 encode(n, bl);
476 for (auto& i : m) {
477 encode(i.first, bl);
478 encode(i.second.legacy_addr(), bl, f);
479 }
480}
481
482template<class T>
483static void encode_addrvec_pvec_as_addr(const T& m, bufferlist& bl, uint64_t f)
484{
485 uint32_t n = m.size();
486 encode(n, bl);
487 for (auto& i : m) {
488 if (i) {
489 encode(i->legacy_addr(), bl, f);
490 } else {
491 encode(entity_addr_t(), bl, f);
492 }
493 }
7c673cae
FG
494}
495
11fdf7f2
TL
496/* for a description of osdmap incremental versions, and when they were
497 * introduced, please refer to
498 * doc/dev/osd_internals/osdmap_versions.txt
499 */
7c673cae
FG
500void OSDMap::Incremental::encode(bufferlist& bl, uint64_t features) const
501{
11fdf7f2 502 using ceph::encode;
7c673cae
FG
503 if ((features & CEPH_FEATURE_OSDMAP_ENC) == 0) {
504 encode_classic(bl, features);
505 return;
506 }
507
508 // only a select set of callers should *ever* be encoding new
509 // OSDMaps. others should be passing around the canonical encoded
510 // buffers from on high. select out those callers by passing in an
511 // "impossible" feature bit.
11fdf7f2 512 ceph_assert(features & CEPH_FEATURE_RESERVED);
7c673cae
FG
513 features &= ~CEPH_FEATURE_RESERVED;
514
515 size_t start_offset = bl.length();
516 size_t tail_offset;
11fdf7f2
TL
517 size_t crc_offset;
518 std::optional<buffer::list::contiguous_filler> crc_filler;
7c673cae
FG
519
520 // meta-encoding: how we include client-used and osd-specific data
521 ENCODE_START(8, 7, bl);
522
523 {
11fdf7f2 524 uint8_t v = 8;
7c673cae
FG
525 if (!HAVE_FEATURE(features, SERVER_LUMINOUS)) {
526 v = 3;
11fdf7f2
TL
527 } else if (!HAVE_FEATURE(features, SERVER_MIMIC)) {
528 v = 5;
529 } else if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) {
530 v = 6;
7c673cae
FG
531 }
532 ENCODE_START(v, 1, bl); // client-usable data
11fdf7f2
TL
533 encode(fsid, bl);
534 encode(epoch, bl);
535 encode(modified, bl);
536 encode(new_pool_max, bl);
537 encode(new_flags, bl);
538 encode(fullmap, bl);
539 encode(crush, bl);
540
541 encode(new_max_osd, bl);
542 encode(new_pools, bl, features);
543 encode(new_pool_names, bl);
544 encode(old_pools, bl);
545 if (v >= 7) {
546 encode(new_up_client, bl, features);
547 } else {
548 encode_addrvec_map_as_addr(new_up_client, bl, features);
549 }
31f18b77 550 if (v >= 5) {
11fdf7f2 551 encode(new_state, bl);
31f18b77
FG
552 } else {
553 uint32_t n = new_state.size();
11fdf7f2 554 encode(n, bl);
31f18b77 555 for (auto p : new_state) {
11fdf7f2
TL
556 encode(p.first, bl);
557 encode((uint8_t)p.second, bl);
31f18b77
FG
558 }
559 }
11fdf7f2
TL
560 encode(new_weight, bl);
561 encode(new_pg_temp, bl);
562 encode(new_primary_temp, bl);
563 encode(new_primary_affinity, bl);
564 encode(new_erasure_code_profiles, bl);
565 encode(old_erasure_code_profiles, bl);
7c673cae 566 if (v >= 4) {
11fdf7f2
TL
567 encode(new_pg_upmap, bl);
568 encode(old_pg_upmap, bl);
569 encode(new_pg_upmap_items, bl);
570 encode(old_pg_upmap_items, bl);
571 }
572 if (v >= 6) {
573 encode(new_removed_snaps, bl);
574 encode(new_purged_snaps, bl);
575 }
576 if (v >= 8) {
577 encode(new_last_up_change, bl);
578 encode(new_last_in_change, bl);
7c673cae
FG
579 }
580 ENCODE_FINISH(bl); // client-usable data
581 }
582
583 {
81eedcae 584 uint8_t target_v = 9;
7c673cae
FG
585 if (!HAVE_FEATURE(features, SERVER_LUMINOUS)) {
586 target_v = 2;
11fdf7f2
TL
587 } else if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) {
588 target_v = 6;
7c673cae
FG
589 }
590 ENCODE_START(target_v, 1, bl); // extended, osd-only data
11fdf7f2
TL
591 if (target_v < 7) {
592 encode_addrvec_map_as_addr(new_hb_back_up, bl, features);
593 } else {
594 encode(new_hb_back_up, bl, features);
595 }
596 encode(new_up_thru, bl);
597 encode(new_last_clean_interval, bl);
598 encode(new_lost, bl);
599 encode(new_blacklist, bl, features);
600 encode(old_blacklist, bl, features);
601 if (target_v < 7) {
602 encode_addrvec_map_as_addr(new_up_cluster, bl, features);
603 } else {
604 encode(new_up_cluster, bl, features);
605 }
606 encode(cluster_snapshot, bl);
607 encode(new_uuid, bl);
608 encode(new_xinfo, bl);
609 if (target_v < 7) {
610 encode_addrvec_map_as_addr(new_hb_front_up, bl, features);
611 } else {
612 encode(new_hb_front_up, bl, features);
613 }
614 encode(features, bl); // NOTE: features arg, not the member
7c673cae 615 if (target_v >= 3) {
11fdf7f2
TL
616 encode(new_nearfull_ratio, bl);
617 encode(new_full_ratio, bl);
618 encode(new_backfillfull_ratio, bl);
31f18b77
FG
619 }
620 // 5 was string-based new_require_min_compat_client
621 if (target_v >= 6) {
11fdf7f2
TL
622 encode(new_require_min_compat_client, bl);
623 encode(new_require_osd_release, bl);
7c673cae 624 }
81eedcae
TL
625 if (target_v >= 8) {
626 encode(new_crush_node_flags, bl);
627 }
628 if (target_v >= 9) {
629 encode(new_device_class_flags, bl);
630 }
7c673cae
FG
631 ENCODE_FINISH(bl); // osd-only data
632 }
633
11fdf7f2
TL
634 crc_offset = bl.length();
635 crc_filler = bl.append_hole(sizeof(uint32_t));
7c673cae
FG
636 tail_offset = bl.length();
637
11fdf7f2 638 encode(full_crc, bl);
7c673cae
FG
639
640 ENCODE_FINISH(bl); // meta-encoding wrapper
641
642 // fill in crc
643 bufferlist front;
11fdf7f2 644 front.substr_of(bl, start_offset, crc_offset - start_offset);
7c673cae
FG
645 inc_crc = front.crc32c(-1);
646 bufferlist tail;
647 tail.substr_of(bl, tail_offset, bl.length() - tail_offset);
648 inc_crc = tail.crc32c(inc_crc);
649 ceph_le32 crc_le;
650 crc_le = inc_crc;
11fdf7f2 651 crc_filler->copy_in(4u, (char*)&crc_le);
7c673cae
FG
652 have_crc = true;
653}
654
11fdf7f2 655void OSDMap::Incremental::decode_classic(bufferlist::const_iterator &p)
7c673cae 656{
11fdf7f2 657 using ceph::decode;
7c673cae
FG
658 __u32 n, t;
659 // base
660 __u16 v;
11fdf7f2
TL
661 decode(v, p);
662 decode(fsid, p);
663 decode(epoch, p);
664 decode(modified, p);
7c673cae 665 if (v == 4 || v == 5) {
11fdf7f2 666 decode(n, p);
7c673cae
FG
667 new_pool_max = n;
668 } else if (v >= 6)
11fdf7f2
TL
669 decode(new_pool_max, p);
670 decode(new_flags, p);
671 decode(fullmap, p);
672 decode(crush, p);
7c673cae 673
11fdf7f2 674 decode(new_max_osd, p);
7c673cae
FG
675 if (v < 6) {
676 new_pools.clear();
11fdf7f2 677 decode(n, p);
7c673cae 678 while (n--) {
11fdf7f2
TL
679 decode(t, p);
680 decode(new_pools[t], p);
7c673cae
FG
681 }
682 } else {
11fdf7f2 683 decode(new_pools, p);
7c673cae
FG
684 }
685 if (v == 5) {
686 new_pool_names.clear();
11fdf7f2 687 decode(n, p);
7c673cae 688 while (n--) {
11fdf7f2
TL
689 decode(t, p);
690 decode(new_pool_names[t], p);
7c673cae
FG
691 }
692 } else if (v >= 6) {
11fdf7f2 693 decode(new_pool_names, p);
7c673cae
FG
694 }
695 if (v < 6) {
696 old_pools.clear();
11fdf7f2 697 decode(n, p);
7c673cae 698 while (n--) {
11fdf7f2 699 decode(t, p);
7c673cae
FG
700 old_pools.insert(t);
701 }
702 } else {
11fdf7f2 703 decode(old_pools, p);
7c673cae 704 }
11fdf7f2 705 decode(new_up_client, p);
31f18b77
FG
706 {
707 map<int32_t,uint8_t> ns;
11fdf7f2 708 decode(ns, p);
31f18b77
FG
709 for (auto q : ns) {
710 new_state[q.first] = q.second;
711 }
712 }
11fdf7f2 713 decode(new_weight, p);
7c673cae
FG
714
715 if (v < 6) {
716 new_pg_temp.clear();
11fdf7f2 717 decode(n, p);
7c673cae
FG
718 while (n--) {
719 old_pg_t opg;
720 ::decode_raw(opg, p);
11fdf7f2 721 decode(new_pg_temp[pg_t(opg)], p);
7c673cae
FG
722 }
723 } else {
11fdf7f2 724 decode(new_pg_temp, p);
7c673cae
FG
725 }
726
727 // decode short map, too.
728 if (v == 5 && p.end())
729 return;
730
731 // extended
732 __u16 ev = 0;
733 if (v >= 5)
11fdf7f2
TL
734 decode(ev, p);
735 decode(new_hb_back_up, p);
7c673cae 736 if (v < 5)
11fdf7f2
TL
737 decode(new_pool_names, p);
738 decode(new_up_thru, p);
739 decode(new_last_clean_interval, p);
740 decode(new_lost, p);
741 decode(new_blacklist, p);
742 decode(old_blacklist, p);
7c673cae 743 if (ev >= 6)
11fdf7f2 744 decode(new_up_cluster, p);
7c673cae 745 if (ev >= 7)
11fdf7f2 746 decode(cluster_snapshot, p);
7c673cae 747 if (ev >= 8)
11fdf7f2 748 decode(new_uuid, p);
7c673cae 749 if (ev >= 9)
11fdf7f2 750 decode(new_xinfo, p);
7c673cae 751 if (ev >= 10)
11fdf7f2 752 decode(new_hb_front_up, p);
7c673cae
FG
753}
754
11fdf7f2
TL
755/* for a description of osdmap incremental versions, and when they were
756 * introduced, please refer to
757 * doc/dev/osd_internals/osdmap_versions.txt
758 */
759void OSDMap::Incremental::decode(bufferlist::const_iterator& bl)
7c673cae 760{
11fdf7f2 761 using ceph::decode;
7c673cae
FG
762 /**
763 * Older encodings of the Incremental had a single struct_v which
764 * covered the whole encoding, and was prior to our modern
765 * stuff which includes a compatv and a size. So if we see
766 * a struct_v < 7, we must rewind to the beginning and use our
767 * classic decoder.
768 */
769 size_t start_offset = bl.get_off();
770 size_t tail_offset = 0;
771 bufferlist crc_front, crc_tail;
772
773 DECODE_START_LEGACY_COMPAT_LEN(8, 7, 7, bl); // wrapper
774 if (struct_v < 7) {
11fdf7f2 775 bl.seek(start_offset);
7c673cae
FG
776 decode_classic(bl);
777 encode_features = 0;
778 if (struct_v >= 6)
779 encode_features = CEPH_FEATURE_PGID64;
780 else
781 encode_features = 0;
782 return;
783 }
784 {
11fdf7f2
TL
785 DECODE_START(8, bl); // client-usable data
786 decode(fsid, bl);
787 decode(epoch, bl);
788 decode(modified, bl);
789 decode(new_pool_max, bl);
790 decode(new_flags, bl);
791 decode(fullmap, bl);
792 decode(crush, bl);
793
794 decode(new_max_osd, bl);
795 decode(new_pools, bl);
796 decode(new_pool_names, bl);
797 decode(old_pools, bl);
798 decode(new_up_client, bl);
31f18b77 799 if (struct_v >= 5) {
11fdf7f2 800 decode(new_state, bl);
31f18b77
FG
801 } else {
802 map<int32_t,uint8_t> ns;
11fdf7f2 803 decode(ns, bl);
31f18b77
FG
804 for (auto q : ns) {
805 new_state[q.first] = q.second;
806 }
807 }
11fdf7f2
TL
808 decode(new_weight, bl);
809 decode(new_pg_temp, bl);
810 decode(new_primary_temp, bl);
7c673cae 811 if (struct_v >= 2)
11fdf7f2 812 decode(new_primary_affinity, bl);
7c673cae
FG
813 else
814 new_primary_affinity.clear();
815 if (struct_v >= 3) {
11fdf7f2
TL
816 decode(new_erasure_code_profiles, bl);
817 decode(old_erasure_code_profiles, bl);
7c673cae
FG
818 } else {
819 new_erasure_code_profiles.clear();
820 old_erasure_code_profiles.clear();
821 }
822 if (struct_v >= 4) {
11fdf7f2
TL
823 decode(new_pg_upmap, bl);
824 decode(old_pg_upmap, bl);
825 decode(new_pg_upmap_items, bl);
826 decode(old_pg_upmap_items, bl);
827 }
828 if (struct_v >= 6) {
829 decode(new_removed_snaps, bl);
830 decode(new_purged_snaps, bl);
831 }
832 if (struct_v >= 8) {
833 decode(new_last_up_change, bl);
834 decode(new_last_in_change, bl);
7c673cae
FG
835 }
836 DECODE_FINISH(bl); // client-usable data
837 }
838
839 {
81eedcae 840 DECODE_START(9, bl); // extended, osd-only data
11fdf7f2
TL
841 decode(new_hb_back_up, bl);
842 decode(new_up_thru, bl);
843 decode(new_last_clean_interval, bl);
844 decode(new_lost, bl);
845 decode(new_blacklist, bl);
846 decode(old_blacklist, bl);
847 decode(new_up_cluster, bl);
848 decode(cluster_snapshot, bl);
849 decode(new_uuid, bl);
850 decode(new_xinfo, bl);
851 decode(new_hb_front_up, bl);
7c673cae 852 if (struct_v >= 2)
11fdf7f2 853 decode(encode_features, bl);
7c673cae
FG
854 else
855 encode_features = CEPH_FEATURE_PGID64 | CEPH_FEATURE_OSDMAP_ENC;
856 if (struct_v >= 3) {
11fdf7f2
TL
857 decode(new_nearfull_ratio, bl);
858 decode(new_full_ratio, bl);
7c673cae
FG
859 } else {
860 new_nearfull_ratio = -1;
861 new_full_ratio = -1;
862 }
863 if (struct_v >= 4) {
11fdf7f2 864 decode(new_backfillfull_ratio, bl);
7c673cae
FG
865 } else {
866 new_backfillfull_ratio = -1;
867 }
31f18b77
FG
868 if (struct_v == 5) {
869 string r;
11fdf7f2 870 decode(r, bl);
31f18b77
FG
871 if (r.length()) {
872 new_require_min_compat_client = ceph_release_from_name(r.c_str());
873 }
874 }
875 if (struct_v >= 6) {
11fdf7f2
TL
876 decode(new_require_min_compat_client, bl);
877 decode(new_require_osd_release, bl);
31f18b77
FG
878 } else {
879 if (new_flags >= 0 && (new_flags & CEPH_OSDMAP_REQUIRE_LUMINOUS)) {
880 // only for compat with post-kraken pre-luminous test clusters
881 new_require_osd_release = CEPH_RELEASE_LUMINOUS;
882 new_flags &= ~(CEPH_OSDMAP_LEGACY_REQUIRE_FLAGS);
883 } else if (new_flags >= 0 && (new_flags & CEPH_OSDMAP_REQUIRE_KRAKEN)) {
884 new_require_osd_release = CEPH_RELEASE_KRAKEN;
885 } else if (new_flags >= 0 && (new_flags & CEPH_OSDMAP_REQUIRE_JEWEL)) {
886 new_require_osd_release = CEPH_RELEASE_JEWEL;
887 } else {
888 new_require_osd_release = -1;
889 }
890 }
81eedcae
TL
891 if (struct_v >= 8) {
892 decode(new_crush_node_flags, bl);
893 }
894 if (struct_v >= 9) {
895 decode(new_device_class_flags, bl);
896 }
7c673cae
FG
897 DECODE_FINISH(bl); // osd-only data
898 }
899
900 if (struct_v >= 8) {
901 have_crc = true;
902 crc_front.substr_of(bl.get_bl(), start_offset, bl.get_off() - start_offset);
11fdf7f2 903 decode(inc_crc, bl);
7c673cae 904 tail_offset = bl.get_off();
11fdf7f2 905 decode(full_crc, bl);
7c673cae
FG
906 } else {
907 have_crc = false;
908 full_crc = 0;
909 inc_crc = 0;
910 }
911
912 DECODE_FINISH(bl); // wrapper
913
914 if (have_crc) {
915 // verify crc
916 uint32_t actual = crc_front.crc32c(-1);
917 if (tail_offset < bl.get_off()) {
918 bufferlist tail;
919 tail.substr_of(bl.get_bl(), tail_offset, bl.get_off() - tail_offset);
920 actual = tail.crc32c(actual);
921 }
922 if (inc_crc != actual) {
923 ostringstream ss;
924 ss << "bad crc, actual " << actual << " != expected " << inc_crc;
925 string s = ss.str();
926 throw buffer::malformed_input(s.c_str());
927 }
928 }
929}
930
931void OSDMap::Incremental::dump(Formatter *f) const
932{
933 f->dump_int("epoch", epoch);
934 f->dump_stream("fsid") << fsid;
935 f->dump_stream("modified") << modified;
11fdf7f2
TL
936 f->dump_stream("new_last_up_change") << new_last_up_change;
937 f->dump_stream("new_last_in_change") << new_last_in_change;
7c673cae
FG
938 f->dump_int("new_pool_max", new_pool_max);
939 f->dump_int("new_flags", new_flags);
940 f->dump_float("new_full_ratio", new_full_ratio);
941 f->dump_float("new_nearfull_ratio", new_nearfull_ratio);
942 f->dump_float("new_backfillfull_ratio", new_backfillfull_ratio);
31f18b77
FG
943 f->dump_int("new_require_min_compat_client", new_require_min_compat_client);
944 f->dump_int("new_require_osd_release", new_require_osd_release);
7c673cae
FG
945
946 if (fullmap.length()) {
947 f->open_object_section("full_map");
948 OSDMap full;
949 bufferlist fbl = fullmap; // kludge around constness.
11fdf7f2 950 auto p = fbl.cbegin();
7c673cae
FG
951 full.decode(p);
952 full.dump(f);
953 f->close_section();
954 }
955 if (crush.length()) {
956 f->open_object_section("crush");
957 CrushWrapper c;
958 bufferlist tbl = crush; // kludge around constness.
11fdf7f2 959 auto p = tbl.cbegin();
7c673cae
FG
960 c.decode(p);
961 c.dump(f);
962 f->close_section();
963 }
964
965 f->dump_int("new_max_osd", new_max_osd);
966
967 f->open_array_section("new_pools");
968
969 for (const auto &new_pool : new_pools) {
970 f->open_object_section("pool");
971 f->dump_int("pool", new_pool.first);
972 new_pool.second.dump(f);
973 f->close_section();
974 }
975 f->close_section();
976 f->open_array_section("new_pool_names");
977
978 for (const auto &new_pool_name : new_pool_names) {
979 f->open_object_section("pool_name");
980 f->dump_int("pool", new_pool_name.first);
981 f->dump_string("name", new_pool_name.second);
982 f->close_section();
983 }
984 f->close_section();
985 f->open_array_section("old_pools");
986
987 for (const auto &old_pool : old_pools)
988 f->dump_int("pool", old_pool);
989 f->close_section();
990
991 f->open_array_section("new_up_osds");
992
993 for (const auto &upclient : new_up_client) {
994 f->open_object_section("osd");
995 f->dump_int("osd", upclient.first);
11fdf7f2
TL
996 f->dump_stream("public_addr") << upclient.second.legacy_addr();
997 f->dump_object("public_addrs", upclient.second);
998 if (auto p = new_up_cluster.find(upclient.first);
999 p != new_up_cluster.end()) {
1000 f->dump_stream("cluster_addr") << p->second.legacy_addr();
1001 f->dump_object("cluster_addrs", p->second);
1002 }
1003 if (auto p = new_hb_back_up.find(upclient.first);
1004 p != new_hb_back_up.end()) {
1005 f->dump_object("heartbeat_back_addrs", p->second);
1006 }
1007 if (auto p = new_hb_front_up.find(upclient.first);
1008 p != new_hb_front_up.end()) {
1009 f->dump_object("heartbeat_front_addrs", p->second);
1010 }
7c673cae
FG
1011 f->close_section();
1012 }
1013 f->close_section();
1014
1015 f->open_array_section("new_weight");
1016
1017 for (const auto &weight : new_weight) {
1018 f->open_object_section("osd");
1019 f->dump_int("osd", weight.first);
1020 f->dump_int("weight", weight.second);
1021 f->close_section();
1022 }
1023 f->close_section();
1024
1025 f->open_array_section("osd_state_xor");
1026 for (const auto &ns : new_state) {
1027 f->open_object_section("osd");
1028 f->dump_int("osd", ns.first);
1029 set<string> st;
1030 calc_state_set(new_state.find(ns.first)->second, st);
1031 f->open_array_section("state_xor");
1032 for (auto &state : st)
1033 f->dump_string("state", state);
1034 f->close_section();
c07f9fc5 1035 f->close_section();
7c673cae
FG
1036 }
1037 f->close_section();
1038
1039 f->open_array_section("new_pg_temp");
1040
1041 for (const auto &pg_temp : new_pg_temp) {
1042 f->open_object_section("pg");
1043 f->dump_stream("pgid") << pg_temp.first;
1044 f->open_array_section("osds");
1045
1046 for (const auto &osd : pg_temp.second)
1047 f->dump_int("osd", osd);
1048 f->close_section();
1049 f->close_section();
1050 }
1051 f->close_section();
1052
1053 f->open_array_section("primary_temp");
1054
1055 for (const auto &primary_temp : new_primary_temp) {
1056 f->dump_stream("pgid") << primary_temp.first;
1057 f->dump_int("osd", primary_temp.second);
1058 }
1059 f->close_section(); // primary_temp
1060
1061 f->open_array_section("new_pg_upmap");
1062 for (auto& i : new_pg_upmap) {
1063 f->open_object_section("mapping");
1064 f->dump_stream("pgid") << i.first;
1065 f->open_array_section("osds");
1066 for (auto osd : i.second) {
1067 f->dump_int("osd", osd);
1068 }
1069 f->close_section();
1070 f->close_section();
1071 }
1072 f->close_section();
1073 f->open_array_section("old_pg_upmap");
1074 for (auto& i : old_pg_upmap) {
1075 f->dump_stream("pgid") << i;
1076 }
1077 f->close_section();
1078
1079 f->open_array_section("new_pg_upmap_items");
1080 for (auto& i : new_pg_upmap_items) {
1081 f->open_object_section("mapping");
1082 f->dump_stream("pgid") << i.first;
1083 f->open_array_section("mappings");
1084 for (auto& p : i.second) {
1085 f->open_object_section("mapping");
1086 f->dump_int("from", p.first);
1087 f->dump_int("to", p.second);
1088 f->close_section();
1089 }
1090 f->close_section();
1091 f->close_section();
1092 }
1093 f->close_section();
1094 f->open_array_section("old_pg_upmap_items");
1095 for (auto& i : old_pg_upmap_items) {
1096 f->dump_stream("pgid") << i;
1097 }
1098 f->close_section();
1099
1100 f->open_array_section("new_up_thru");
1101
1102 for (const auto &up_thru : new_up_thru) {
1103 f->open_object_section("osd");
1104 f->dump_int("osd", up_thru.first);
1105 f->dump_int("up_thru", up_thru.second);
1106 f->close_section();
1107 }
1108 f->close_section();
1109
1110 f->open_array_section("new_lost");
1111
1112 for (const auto &lost : new_lost) {
1113 f->open_object_section("osd");
1114 f->dump_int("osd", lost.first);
1115 f->dump_int("epoch_lost", lost.second);
1116 f->close_section();
1117 }
1118 f->close_section();
1119
1120 f->open_array_section("new_last_clean_interval");
1121
1122 for (const auto &last_clean_interval : new_last_clean_interval) {
1123 f->open_object_section("osd");
1124 f->dump_int("osd", last_clean_interval.first);
1125 f->dump_int("first", last_clean_interval.second.first);
1126 f->dump_int("last", last_clean_interval.second.second);
1127 f->close_section();
1128 }
1129 f->close_section();
1130
1131 f->open_array_section("new_blacklist");
1132 for (const auto &blist : new_blacklist) {
1133 stringstream ss;
1134 ss << blist.first;
1135 f->dump_stream(ss.str().c_str()) << blist.second;
1136 }
1137 f->close_section();
1138 f->open_array_section("old_blacklist");
1139 for (const auto &blist : old_blacklist)
1140 f->dump_stream("addr") << blist;
1141 f->close_section();
1142
1143 f->open_array_section("new_xinfo");
1144 for (const auto &xinfo : new_xinfo) {
1145 f->open_object_section("xinfo");
1146 f->dump_int("osd", xinfo.first);
1147 xinfo.second.dump(f);
1148 f->close_section();
1149 }
1150 f->close_section();
1151
1152 if (cluster_snapshot.size())
1153 f->dump_string("cluster_snapshot", cluster_snapshot);
1154
1155 f->open_array_section("new_uuid");
1156 for (const auto &uuid : new_uuid) {
1157 f->open_object_section("osd");
1158 f->dump_int("osd", uuid.first);
1159 f->dump_stream("uuid") << uuid.second;
1160 f->close_section();
1161 }
1162 f->close_section();
1163
1164 OSDMap::dump_erasure_code_profiles(new_erasure_code_profiles, f);
1165 f->open_array_section("old_erasure_code_profiles");
1166 for (const auto &erasure_code_profile : old_erasure_code_profiles) {
1167 f->dump_string("old", erasure_code_profile.c_str());
1168 }
1169 f->close_section();
11fdf7f2
TL
1170
1171 f->open_array_section("new_removed_snaps");
1172 for (auto& p : new_removed_snaps) {
1173 f->open_object_section("pool");
1174 f->dump_int("pool", p.first);
1175 f->open_array_section("snaps");
1176 for (auto q = p.second.begin(); q != p.second.end(); ++q) {
1177 f->open_object_section("interval");
1178 f->dump_unsigned("begin", q.get_start());
1179 f->dump_unsigned("length", q.get_len());
1180 f->close_section();
1181 }
1182 f->close_section();
1183 f->close_section();
1184 }
1185 f->close_section();
1186 f->open_array_section("new_purged_snaps");
1187 for (auto& p : new_purged_snaps) {
1188 f->open_object_section("pool");
1189 f->dump_int("pool", p.first);
1190 f->open_array_section("snaps");
1191 for (auto q = p.second.begin(); q != p.second.end(); ++q) {
1192 f->open_object_section("interval");
1193 f->dump_unsigned("begin", q.get_start());
1194 f->dump_unsigned("length", q.get_len());
1195 f->close_section();
1196 }
1197 f->close_section();
1198 f->close_section();
1199 }
81eedcae
TL
1200 f->open_array_section("new_crush_node_flags");
1201 for (auto& i : new_crush_node_flags) {
1202 f->open_object_section("node");
1203 f->dump_int("id", i.first);
1204 set<string> st;
1205 calc_state_set(i.second, st);
1206 for (auto& j : st) {
1207 f->dump_string("flag", j);
1208 }
1209 f->close_section();
1210 }
1211 f->close_section();
1212 f->open_array_section("new_device_class_flags");
1213 for (auto& i : new_device_class_flags) {
1214 f->open_object_section("device_class");
1215 f->dump_int("id", i.first);
1216 set<string> st;
1217 calc_state_set(i.second, st);
1218 for (auto& j : st) {
1219 f->dump_string("flag", j);
1220 }
1221 f->close_section();
1222 }
1223 f->close_section();
11fdf7f2 1224 f->close_section();
7c673cae
FG
1225}
1226
1227void OSDMap::Incremental::generate_test_instances(list<Incremental*>& o)
1228{
1229 o.push_back(new Incremental);
1230}
1231
1232// ----------------------------------
1233// OSDMap
1234
1235void OSDMap::set_epoch(epoch_t e)
1236{
1237 epoch = e;
1238 for (auto &pool : pools)
1239 pool.second.last_change = e;
1240}
1241
11fdf7f2 1242bool OSDMap::is_blacklisted(const entity_addr_t& orig) const
7c673cae 1243{
11fdf7f2 1244 if (blacklist.empty()) {
7c673cae 1245 return false;
11fdf7f2
TL
1246 }
1247
1248 // all blacklist entries are type ANY for nautilus+
1249 // FIXME: avoid this copy!
1250 entity_addr_t a = orig;
1251 if (require_osd_release < CEPH_RELEASE_NAUTILUS) {
1252 a.set_type(entity_addr_t::TYPE_LEGACY);
1253 } else {
1254 a.set_type(entity_addr_t::TYPE_ANY);
1255 }
7c673cae
FG
1256
1257 // this specific instance?
11fdf7f2 1258 if (blacklist.count(a)) {
7c673cae 1259 return true;
11fdf7f2 1260 }
7c673cae
FG
1261
1262 // is entire ip blacklisted?
1263 if (a.is_ip()) {
11fdf7f2
TL
1264 a.set_port(0);
1265 a.set_nonce(0);
1266 if (blacklist.count(a)) {
1267 return true;
1268 }
1269 }
1270
1271 return false;
1272}
1273
1274bool OSDMap::is_blacklisted(const entity_addrvec_t& av) const
1275{
1276 if (blacklist.empty())
1277 return false;
1278
1279 for (auto& a : av.v) {
1280 if (is_blacklisted(a)) {
7c673cae
FG
1281 return true;
1282 }
1283 }
1284
1285 return false;
1286}
1287
1288void OSDMap::get_blacklist(list<pair<entity_addr_t,utime_t> > *bl) const
1289{
1290 std::copy(blacklist.begin(), blacklist.end(), std::back_inserter(*bl));
1291}
1292
31f18b77
FG
1293void OSDMap::get_blacklist(std::set<entity_addr_t> *bl) const
1294{
1295 for (const auto &i : blacklist) {
1296 bl->insert(i.first);
1297 }
1298}
1299
7c673cae
FG
1300void OSDMap::set_max_osd(int m)
1301{
1302 int o = max_osd;
1303 max_osd = m;
1304 osd_state.resize(m);
1305 osd_weight.resize(m);
1306 for (; o<max_osd; o++) {
1307 osd_state[o] = 0;
1308 osd_weight[o] = CEPH_OSD_OUT;
1309 }
1310 osd_info.resize(m);
1311 osd_xinfo.resize(m);
11fdf7f2
TL
1312 osd_addrs->client_addrs.resize(m);
1313 osd_addrs->cluster_addrs.resize(m);
1314 osd_addrs->hb_back_addrs.resize(m);
1315 osd_addrs->hb_front_addrs.resize(m);
7c673cae
FG
1316 osd_uuid->resize(m);
1317 if (osd_primary_affinity)
1318 osd_primary_affinity->resize(m, CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1319
1320 calc_num_osds();
1321}
1322
1323int OSDMap::calc_num_osds()
1324{
1325 num_osd = 0;
1326 num_up_osd = 0;
1327 num_in_osd = 0;
1328 for (int i=0; i<max_osd; i++) {
1329 if (osd_state[i] & CEPH_OSD_EXISTS) {
1330 ++num_osd;
1331 if (osd_state[i] & CEPH_OSD_UP) {
1332 ++num_up_osd;
1333 }
1334 if (get_weight(i) != CEPH_OSD_OUT) {
1335 ++num_in_osd;
1336 }
1337 }
1338 }
1339 return num_osd;
1340}
1341
3efd9988
FG
1342void OSDMap::get_full_pools(CephContext *cct,
1343 set<int64_t> *full,
1344 set<int64_t> *backfillfull,
1345 set<int64_t> *nearfull) const
7c673cae 1346{
11fdf7f2
TL
1347 ceph_assert(full);
1348 ceph_assert(backfillfull);
1349 ceph_assert(nearfull);
3efd9988
FG
1350 full->clear();
1351 backfillfull->clear();
1352 nearfull->clear();
1353
1354 vector<int> full_osds;
1355 vector<int> backfillfull_osds;
1356 vector<int> nearfull_osds;
7c673cae
FG
1357 for (int i = 0; i < max_osd; ++i) {
1358 if (exists(i) && is_up(i) && is_in(i)) {
1359 if (osd_state[i] & CEPH_OSD_FULL)
3efd9988 1360 full_osds.push_back(i);
7c673cae 1361 else if (osd_state[i] & CEPH_OSD_BACKFILLFULL)
3efd9988 1362 backfillfull_osds.push_back(i);
7c673cae 1363 else if (osd_state[i] & CEPH_OSD_NEARFULL)
3efd9988 1364 nearfull_osds.push_back(i);
7c673cae
FG
1365 }
1366 }
3efd9988
FG
1367
1368 for (auto i: full_osds) {
1369 get_pool_ids_by_osd(cct, i, full);
1370 }
1371 for (auto i: backfillfull_osds) {
1372 get_pool_ids_by_osd(cct, i, backfillfull);
1373 }
1374 for (auto i: nearfull_osds) {
1375 get_pool_ids_by_osd(cct, i, nearfull);
1376 }
7c673cae
FG
1377}
1378
31f18b77
FG
1379void OSDMap::get_full_osd_counts(set<int> *full, set<int> *backfill,
1380 set<int> *nearfull) const
1381{
1382 full->clear();
1383 backfill->clear();
1384 nearfull->clear();
1385 for (int i = 0; i < max_osd; ++i) {
1386 if (exists(i) && is_up(i) && is_in(i)) {
1387 if (osd_state[i] & CEPH_OSD_FULL)
1388 full->emplace(i);
1389 else if (osd_state[i] & CEPH_OSD_BACKFILLFULL)
1390 backfill->emplace(i);
1391 else if (osd_state[i] & CEPH_OSD_NEARFULL)
1392 nearfull->emplace(i);
1393 }
1394 }
1395}
1396
7c673cae
FG
1397void OSDMap::get_all_osds(set<int32_t>& ls) const
1398{
1399 for (int i=0; i<max_osd; i++)
1400 if (exists(i))
1401 ls.insert(i);
1402}
1403
1404void OSDMap::get_up_osds(set<int32_t>& ls) const
1405{
1406 for (int i = 0; i < max_osd; i++) {
1407 if (is_up(i))
1408 ls.insert(i);
1409 }
1410}
1411
81eedcae 1412void OSDMap::get_out_existing_osds(set<int32_t>& ls) const
31f18b77
FG
1413{
1414 for (int i = 0; i < max_osd; i++) {
81eedcae 1415 if (exists(i) && get_weight(i) == CEPH_OSD_OUT)
31f18b77
FG
1416 ls.insert(i);
1417 }
1418}
1419
11fdf7f2
TL
1420void OSDMap::get_flag_set(set<string> *flagset) const
1421{
1422 for (unsigned i = 0; i < sizeof(flags) * 8; ++i) {
1423 if (flags & (1<<i)) {
1424 flagset->insert(get_flag_string(flags & (1<<i)));
1425 }
1426 }
1427}
1428
7c673cae
FG
1429void OSDMap::calc_state_set(int state, set<string>& st)
1430{
1431 unsigned t = state;
1432 for (unsigned s = 1; t; s <<= 1) {
1433 if (t & s) {
1434 t &= ~s;
1435 st.insert(ceph_osd_state_name(s));
1436 }
1437 }
1438}
1439
1440void OSDMap::adjust_osd_weights(const map<int,double>& weights, Incremental& inc) const
1441{
1442 float max = 0;
1443 for (const auto &weight : weights) {
1444 if (weight.second > max)
1445 max = weight.second;
1446 }
1447
1448 for (const auto &weight : weights) {
1449 inc.new_weight[weight.first] = (unsigned)((weight.second / max) * CEPH_OSD_IN);
1450 }
1451}
1452
1453int OSDMap::identify_osd(const entity_addr_t& addr) const
1454{
1455 for (int i=0; i<max_osd; i++)
11fdf7f2
TL
1456 if (exists(i) && (get_addrs(i).contains(addr) ||
1457 get_cluster_addrs(i).contains(addr)))
7c673cae
FG
1458 return i;
1459 return -1;
1460}
1461
1462int OSDMap::identify_osd(const uuid_d& u) const
1463{
1464 for (int i=0; i<max_osd; i++)
1465 if (exists(i) && get_uuid(i) == u)
1466 return i;
1467 return -1;
1468}
1469
1470int OSDMap::identify_osd_on_all_channels(const entity_addr_t& addr) const
1471{
1472 for (int i=0; i<max_osd; i++)
11fdf7f2
TL
1473 if (exists(i) && (get_addrs(i).contains(addr) ||
1474 get_cluster_addrs(i).contains(addr) ||
1475 get_hb_back_addrs(i).contains(addr) ||
1476 get_hb_front_addrs(i).contains(addr)))
7c673cae
FG
1477 return i;
1478 return -1;
1479}
1480
1481int OSDMap::find_osd_on_ip(const entity_addr_t& ip) const
1482{
1483 for (int i=0; i<max_osd; i++)
11fdf7f2
TL
1484 if (exists(i) && (get_addrs(i).is_same_host(ip) ||
1485 get_cluster_addrs(i).is_same_host(ip)))
7c673cae
FG
1486 return i;
1487 return -1;
1488}
1489
1490
1491uint64_t OSDMap::get_features(int entity_type, uint64_t *pmask) const
1492{
1493 uint64_t features = 0; // things we actually have
1494 uint64_t mask = 0; // things we could have
1495
1496 if (crush->has_nondefault_tunables())
1497 features |= CEPH_FEATURE_CRUSH_TUNABLES;
1498 if (crush->has_nondefault_tunables2())
1499 features |= CEPH_FEATURE_CRUSH_TUNABLES2;
1500 if (crush->has_nondefault_tunables3())
1501 features |= CEPH_FEATURE_CRUSH_TUNABLES3;
1502 if (crush->has_v4_buckets())
1503 features |= CEPH_FEATURE_CRUSH_V4;
1504 if (crush->has_nondefault_tunables5())
1505 features |= CEPH_FEATURE_CRUSH_TUNABLES5;
c07f9fc5
FG
1506 if (crush->has_incompat_choose_args()) {
1507 features |= CEPH_FEATUREMASK_CRUSH_CHOOSE_ARGS;
1508 }
7c673cae
FG
1509 mask |= CEPH_FEATURES_CRUSH;
1510
1511 if (!pg_upmap.empty() || !pg_upmap_items.empty())
1512 features |= CEPH_FEATUREMASK_OSDMAP_PG_UPMAP;
1513 mask |= CEPH_FEATUREMASK_OSDMAP_PG_UPMAP;
1514
1515 for (auto &pool: pools) {
1516 if (pool.second.has_flag(pg_pool_t::FLAG_HASHPSPOOL)) {
1517 features |= CEPH_FEATURE_OSDHASHPSPOOL;
1518 }
7c673cae
FG
1519 if (!pool.second.tiers.empty() ||
1520 pool.second.is_tier()) {
1521 features |= CEPH_FEATURE_OSD_CACHEPOOL;
1522 }
31f18b77 1523 int ruleid = crush->find_rule(pool.second.get_crush_rule(),
7c673cae
FG
1524 pool.second.get_type(),
1525 pool.second.get_size());
1526 if (ruleid >= 0) {
1527 if (crush->is_v2_rule(ruleid))
1528 features |= CEPH_FEATURE_CRUSH_V2;
1529 if (crush->is_v3_rule(ruleid))
1530 features |= CEPH_FEATURE_CRUSH_TUNABLES3;
1531 if (crush->is_v5_rule(ruleid))
1532 features |= CEPH_FEATURE_CRUSH_TUNABLES5;
1533 }
1534 }
7c673cae 1535 mask |= CEPH_FEATURE_OSDHASHPSPOOL | CEPH_FEATURE_OSD_CACHEPOOL;
7c673cae
FG
1536
1537 if (osd_primary_affinity) {
1538 for (int i = 0; i < max_osd; ++i) {
1539 if ((*osd_primary_affinity)[i] != CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
1540 features |= CEPH_FEATURE_OSD_PRIMARY_AFFINITY;
1541 break;
1542 }
1543 }
1544 }
1545 mask |= CEPH_FEATURE_OSD_PRIMARY_AFFINITY;
1546
1547 if (entity_type == CEPH_ENTITY_TYPE_OSD) {
1548 const uint64_t jewel_features = CEPH_FEATURE_SERVER_JEWEL;
31f18b77 1549 if (require_osd_release >= CEPH_RELEASE_JEWEL) {
7c673cae
FG
1550 features |= jewel_features;
1551 }
1552 mask |= jewel_features;
1553
1554 const uint64_t kraken_features = CEPH_FEATUREMASK_SERVER_KRAKEN
1555 | CEPH_FEATURE_MSG_ADDR2;
31f18b77 1556 if (require_osd_release >= CEPH_RELEASE_KRAKEN) {
7c673cae
FG
1557 features |= kraken_features;
1558 }
1559 mask |= kraken_features;
1560 }
1561
11fdf7f2
TL
1562 if (require_min_compat_client >= CEPH_RELEASE_NAUTILUS) {
1563 // if min_compat_client is >= nautilus, require v2 cephx signatures
1564 // from everyone
1565 features |= CEPH_FEATUREMASK_CEPHX_V2;
1566 } else if (require_osd_release >= CEPH_RELEASE_NAUTILUS &&
1567 entity_type == CEPH_ENTITY_TYPE_OSD) {
1568 // if osds are >= nautilus, at least require the signatures from them
1569 features |= CEPH_FEATUREMASK_CEPHX_V2;
1570 }
1571 mask |= CEPH_FEATUREMASK_CEPHX_V2;
1572
7c673cae
FG
1573 if (pmask)
1574 *pmask = mask;
1575 return features;
1576}
1577
31f18b77 1578uint8_t OSDMap::get_min_compat_client() const
7c673cae
FG
1579{
1580 uint64_t f = get_features(CEPH_ENTITY_TYPE_CLIENT, nullptr);
1581
1582 if (HAVE_FEATURE(f, OSDMAP_PG_UPMAP) || // v12.0.0-1733-g27d6f43
31f18b77
FG
1583 HAVE_FEATURE(f, CRUSH_CHOOSE_ARGS)) { // v12.0.1-2172-gef1ef28
1584 return CEPH_RELEASE_LUMINOUS; // v12.2.0
7c673cae
FG
1585 }
1586 if (HAVE_FEATURE(f, CRUSH_TUNABLES5)) { // v10.0.0-612-g043a737
31f18b77 1587 return CEPH_RELEASE_JEWEL; // v10.2.0
7c673cae
FG
1588 }
1589 if (HAVE_FEATURE(f, CRUSH_V4)) { // v0.91-678-g325fc56
31f18b77 1590 return CEPH_RELEASE_HAMMER; // v0.94.0
7c673cae
FG
1591 }
1592 if (HAVE_FEATURE(f, OSD_PRIMARY_AFFINITY) || // v0.76-553-gf825624
1593 HAVE_FEATURE(f, CRUSH_TUNABLES3) || // v0.76-395-ge20a55d
7c673cae 1594 HAVE_FEATURE(f, OSD_CACHEPOOL)) { // v0.67-401-gb91c1c5
31f18b77 1595 return CEPH_RELEASE_FIREFLY; // v0.80.0
7c673cae
FG
1596 }
1597 if (HAVE_FEATURE(f, CRUSH_TUNABLES2) || // v0.54-684-g0cc47ff
1598 HAVE_FEATURE(f, OSDHASHPSPOOL)) { // v0.57-398-g8cc2b0f
31f18b77 1599 return CEPH_RELEASE_DUMPLING; // v0.67.0
7c673cae
FG
1600 }
1601 if (HAVE_FEATURE(f, CRUSH_TUNABLES)) { // v0.48argonaut-206-g6f381af
31f18b77 1602 return CEPH_RELEASE_ARGONAUT; // v0.48argonaut-206-g6f381af
7c673cae 1603 }
31f18b77 1604 return CEPH_RELEASE_ARGONAUT; // v0.48argonaut-206-g6f381af
7c673cae
FG
1605}
1606
11fdf7f2
TL
1607uint8_t OSDMap::get_require_min_compat_client() const
1608{
1609 return require_min_compat_client;
1610}
1611
7c673cae
FG
1612void OSDMap::_calc_up_osd_features()
1613{
1614 bool first = true;
1615 cached_up_osd_features = 0;
1616 for (int osd = 0; osd < max_osd; ++osd) {
1617 if (!is_up(osd))
1618 continue;
1619 const osd_xinfo_t &xi = get_xinfo(osd);
3efd9988
FG
1620 if (xi.features == 0)
1621 continue; // bogus xinfo, maybe #20751 or similar, skipping
7c673cae
FG
1622 if (first) {
1623 cached_up_osd_features = xi.features;
1624 first = false;
1625 } else {
1626 cached_up_osd_features &= xi.features;
1627 }
1628 }
1629}
1630
1631uint64_t OSDMap::get_up_osd_features() const
1632{
1633 return cached_up_osd_features;
1634}
1635
1636void OSDMap::dedup(const OSDMap *o, OSDMap *n)
1637{
11fdf7f2 1638 using ceph::encode;
7c673cae
FG
1639 if (o->epoch == n->epoch)
1640 return;
1641
1642 int diff = 0;
1643
1644 // do addrs match?
1645 if (o->max_osd != n->max_osd)
1646 diff++;
1647 for (int i = 0; i < o->max_osd && i < n->max_osd; i++) {
11fdf7f2
TL
1648 if ( n->osd_addrs->client_addrs[i] && o->osd_addrs->client_addrs[i] &&
1649 *n->osd_addrs->client_addrs[i] == *o->osd_addrs->client_addrs[i])
1650 n->osd_addrs->client_addrs[i] = o->osd_addrs->client_addrs[i];
7c673cae
FG
1651 else
1652 diff++;
11fdf7f2
TL
1653 if ( n->osd_addrs->cluster_addrs[i] && o->osd_addrs->cluster_addrs[i] &&
1654 *n->osd_addrs->cluster_addrs[i] == *o->osd_addrs->cluster_addrs[i])
1655 n->osd_addrs->cluster_addrs[i] = o->osd_addrs->cluster_addrs[i];
7c673cae
FG
1656 else
1657 diff++;
11fdf7f2
TL
1658 if ( n->osd_addrs->hb_back_addrs[i] && o->osd_addrs->hb_back_addrs[i] &&
1659 *n->osd_addrs->hb_back_addrs[i] == *o->osd_addrs->hb_back_addrs[i])
1660 n->osd_addrs->hb_back_addrs[i] = o->osd_addrs->hb_back_addrs[i];
7c673cae
FG
1661 else
1662 diff++;
11fdf7f2
TL
1663 if ( n->osd_addrs->hb_front_addrs[i] && o->osd_addrs->hb_front_addrs[i] &&
1664 *n->osd_addrs->hb_front_addrs[i] == *o->osd_addrs->hb_front_addrs[i])
1665 n->osd_addrs->hb_front_addrs[i] = o->osd_addrs->hb_front_addrs[i];
7c673cae
FG
1666 else
1667 diff++;
1668 }
1669 if (diff == 0) {
1670 // zoinks, no differences at all!
1671 n->osd_addrs = o->osd_addrs;
1672 }
1673
1674 // does crush match?
1675 bufferlist oc, nc;
11fdf7f2
TL
1676 encode(*o->crush, oc, CEPH_FEATURES_SUPPORTED_DEFAULT);
1677 encode(*n->crush, nc, CEPH_FEATURES_SUPPORTED_DEFAULT);
7c673cae
FG
1678 if (oc.contents_equal(nc)) {
1679 n->crush = o->crush;
1680 }
1681
1682 // does pg_temp match?
31f18b77
FG
1683 if (*o->pg_temp == *n->pg_temp)
1684 n->pg_temp = o->pg_temp;
7c673cae
FG
1685
1686 // does primary_temp match?
1687 if (o->primary_temp->size() == n->primary_temp->size()) {
1688 if (*o->primary_temp == *n->primary_temp)
1689 n->primary_temp = o->primary_temp;
1690 }
1691
1692 // do uuids match?
1693 if (o->osd_uuid->size() == n->osd_uuid->size() &&
1694 *o->osd_uuid == *n->osd_uuid)
1695 n->osd_uuid = o->osd_uuid;
1696}
1697
1698void OSDMap::clean_temps(CephContext *cct,
11fdf7f2
TL
1699 const OSDMap& oldmap,
1700 const OSDMap& nextmap,
1701 Incremental *pending_inc)
7c673cae
FG
1702{
1703 ldout(cct, 10) << __func__ << dendl;
7c673cae 1704
11fdf7f2 1705 for (auto pg : *nextmap.pg_temp) {
7c673cae
FG
1706 // if pool does not exist, remove any existing pg_temps associated with
1707 // it. we don't care about pg_temps on the pending_inc either; if there
1708 // are new_pg_temp entries on the pending, clear them out just as well.
11fdf7f2 1709 if (!nextmap.have_pg_pool(pg.first.pool())) {
7c673cae
FG
1710 ldout(cct, 10) << __func__ << " removing pg_temp " << pg.first
1711 << " for nonexistent pool " << pg.first.pool() << dendl;
1712 pending_inc->new_pg_temp[pg.first].clear();
1713 continue;
1714 }
1715 // all osds down?
1716 unsigned num_up = 0;
1717 for (auto o : pg.second) {
11fdf7f2 1718 if (!nextmap.is_down(o)) {
7c673cae
FG
1719 ++num_up;
1720 break;
1721 }
1722 }
1723 if (num_up == 0) {
1724 ldout(cct, 10) << __func__ << " removing pg_temp " << pg.first
1725 << " with all down osds" << pg.second << dendl;
1726 pending_inc->new_pg_temp[pg.first].clear();
1727 continue;
1728 }
1729 // redundant pg_temp?
1730 vector<int> raw_up;
1731 int primary;
11fdf7f2 1732 nextmap.pg_to_raw_up(pg.first, &raw_up, &primary);
91327a77 1733 bool remove = false;
11fdf7f2 1734 if (raw_up == pg.second) {
7c673cae
FG
1735 ldout(cct, 10) << __func__ << " removing pg_temp " << pg.first << " "
1736 << pg.second << " that matches raw_up mapping" << dendl;
91327a77
AA
1737 remove = true;
1738 }
1739 // oversized pg_temp?
11fdf7f2 1740 if (pg.second.size() > nextmap.get_pg_pool(pg.first.pool())->get_size()) {
91327a77
AA
1741 ldout(cct, 10) << __func__ << " removing pg_temp " << pg.first << " "
1742 << pg.second << " exceeds pool size" << dendl;
1743 remove = true;
1744 }
1745 if (remove) {
11fdf7f2 1746 if (oldmap.pg_temp->count(pg.first))
7c673cae
FG
1747 pending_inc->new_pg_temp[pg.first].clear();
1748 else
1749 pending_inc->new_pg_temp.erase(pg.first);
1750 }
1751 }
1752
11fdf7f2 1753 for (auto &pg : *nextmap.primary_temp) {
7c673cae 1754 // primary down?
11fdf7f2 1755 if (nextmap.is_down(pg.second)) {
7c673cae
FG
1756 ldout(cct, 10) << __func__ << " removing primary_temp " << pg.first
1757 << " to down " << pg.second << dendl;
1758 pending_inc->new_primary_temp[pg.first] = -1;
1759 continue;
1760 }
1761 // redundant primary_temp?
1762 vector<int> real_up, templess_up;
1763 int real_primary, templess_primary;
1764 pg_t pgid = pg.first;
11fdf7f2
TL
1765 nextmap.pg_to_acting_osds(pgid, &real_up, &real_primary);
1766 nextmap.pg_to_raw_up(pgid, &templess_up, &templess_primary);
7c673cae
FG
1767 if (real_primary == templess_primary){
1768 ldout(cct, 10) << __func__ << " removing primary_temp "
1769 << pgid << " -> " << real_primary
1770 << " (unnecessary/redundant)" << dendl;
11fdf7f2 1771 if (oldmap.primary_temp->count(pgid))
7c673cae
FG
1772 pending_inc->new_primary_temp[pgid] = -1;
1773 else
1774 pending_inc->new_primary_temp.erase(pgid);
1775 }
1776 }
1777}
1778
94b18763 1779void OSDMap::maybe_remove_pg_upmaps(CephContext *cct,
11fdf7f2
TL
1780 const OSDMap& oldmap,
1781 const OSDMap& nextmap,
94b18763
FG
1782 Incremental *pending_inc)
1783{
1784 ldout(cct, 10) << __func__ << dendl;
28e407b8
AA
1785 set<pg_t> to_check;
1786 set<pg_t> to_cancel;
1787 map<int, map<int, float>> rule_weight_map;
94b18763 1788
11fdf7f2 1789 for (auto& p : nextmap.pg_upmap) {
28e407b8
AA
1790 to_check.insert(p.first);
1791 }
11fdf7f2 1792 for (auto& p : nextmap.pg_upmap_items) {
28e407b8
AA
1793 to_check.insert(p.first);
1794 }
1795 for (auto& p : pending_inc->new_pg_upmap) {
1796 to_check.insert(p.first);
1797 }
1798 for (auto& p : pending_inc->new_pg_upmap_items) {
1799 to_check.insert(p.first);
1800 }
1801 for (auto& pg : to_check) {
11fdf7f2
TL
1802 const pg_pool_t *pi = nextmap.get_pg_pool(pg.pool());
1803 if (!pi || pg.ps() >= pi->get_pg_num_pending()) {
1804 ldout(cct, 0) << __func__ << " pg " << pg << " is gone or merge source"
1805 << dendl;
1806 to_cancel.insert(pg);
1807 continue;
1808 }
1809 if (pi->is_pending_merge(pg, nullptr)) {
1810 ldout(cct, 0) << __func__ << " pg " << pg << " is pending merge"
1811 << dendl;
f64942e4 1812 to_cancel.insert(pg);
94b18763
FG
1813 continue;
1814 }
a8e16298
TL
1815 vector<int> raw_up;
1816 int primary;
11fdf7f2 1817 nextmap.pg_to_raw_up(pg, &raw_up, &primary);
a8e16298
TL
1818 vector<int> up;
1819 up.reserve(raw_up.size());
1820 for (auto osd : raw_up) {
1821 // skip non-existent/down osd for erasure-coded PGs
1822 if (osd == CRUSH_ITEM_NONE)
1823 continue;
1824 up.push_back(osd);
1825 }
11fdf7f2
TL
1826 auto crush_rule = nextmap.get_pg_pool_crush_rule(pg);
1827 auto r = nextmap.crush->verify_upmap(cct,
1828 crush_rule,
1829 nextmap.get_pg_pool_size(pg),
1830 up);
a8e16298
TL
1831 if (r < 0) {
1832 ldout(cct, 0) << __func__ << " verify_upmap of pg " << pg
1833 << " returning " << r
1834 << dendl;
1835 to_cancel.insert(pg);
1836 continue;
1837 }
1838 // below we check against crush-topology changing..
28e407b8
AA
1839 map<int, float> weight_map;
1840 auto it = rule_weight_map.find(crush_rule);
1841 if (it == rule_weight_map.end()) {
11fdf7f2 1842 auto r = nextmap.crush->get_rule_weight_osd_map(crush_rule, &weight_map);
28e407b8
AA
1843 if (r < 0) {
1844 lderr(cct) << __func__ << " unable to get crush weight_map for "
1845 << "crush_rule " << crush_rule << dendl;
1846 continue;
1847 }
1848 rule_weight_map[crush_rule] = weight_map;
1849 } else {
1850 weight_map = it->second;
1851 }
28e407b8 1852 ldout(cct, 10) << __func__ << " pg " << pg
28e407b8 1853 << " weight_map " << weight_map
94b18763 1854 << dendl;
a8e16298 1855 for (auto osd : up) {
28e407b8
AA
1856 auto it = weight_map.find(osd);
1857 if (it == weight_map.end()) {
1858 // osd is gone or has been moved out of the specific crush-tree
1859 to_cancel.insert(pg);
94b18763
FG
1860 break;
1861 }
11fdf7f2 1862 auto adjusted_weight = nextmap.get_weightf(it->first) * it->second;
28e407b8
AA
1863 if (adjusted_weight == 0) {
1864 // osd is out/crush-out
1865 to_cancel.insert(pg);
94b18763
FG
1866 break;
1867 }
1868 }
28e407b8
AA
1869 }
1870 for (auto &pg: to_cancel) {
1871 { // pg_upmap
1872 auto it = pending_inc->new_pg_upmap.find(pg);
94b18763 1873 if (it != pending_inc->new_pg_upmap.end()) {
28e407b8
AA
1874 ldout(cct, 10) << __func__ << " cancel invalid pending "
1875 << "pg_upmap entry "
1876 << it->first << "->" << it->second
1877 << dendl;
94b18763
FG
1878 pending_inc->new_pg_upmap.erase(it);
1879 }
11fdf7f2 1880 if (oldmap.pg_upmap.count(pg)) {
28e407b8 1881 ldout(cct, 10) << __func__ << " cancel invalid pg_upmap entry "
11fdf7f2
TL
1882 << oldmap.pg_upmap.find(pg)->first << "->"
1883 << oldmap.pg_upmap.find(pg)->second
28e407b8
AA
1884 << dendl;
1885 pending_inc->old_pg_upmap.insert(pg);
94b18763
FG
1886 }
1887 }
28e407b8
AA
1888 { // pg_upmap_items
1889 auto it = pending_inc->new_pg_upmap_items.find(pg);
94b18763 1890 if (it != pending_inc->new_pg_upmap_items.end()) {
28e407b8
AA
1891 ldout(cct, 10) << __func__ << " cancel invalid pending "
1892 << "pg_upmap_items entry "
1893 << it->first << "->" << it->second
1894 << dendl;
94b18763
FG
1895 pending_inc->new_pg_upmap_items.erase(it);
1896 }
11fdf7f2 1897 if (oldmap.pg_upmap_items.count(pg)) {
28e407b8
AA
1898 ldout(cct, 10) << __func__ << " cancel invalid "
1899 << "pg_upmap_items entry "
11fdf7f2
TL
1900 << oldmap.pg_upmap_items.find(pg)->first << "->"
1901 << oldmap.pg_upmap_items.find(pg)->second
28e407b8
AA
1902 << dendl;
1903 pending_inc->old_pg_upmap_items.insert(pg);
94b18763
FG
1904 }
1905 }
1906 }
11fdf7f2 1907 nextmap.clean_pg_upmaps(cct, pending_inc);
94b18763
FG
1908}
1909
7c673cae
FG
1910int OSDMap::apply_incremental(const Incremental &inc)
1911{
1912 new_blacklist_entries = false;
1913 if (inc.epoch == 1)
1914 fsid = inc.fsid;
1915 else if (inc.fsid != fsid)
1916 return -EINVAL;
1917
11fdf7f2 1918 ceph_assert(inc.epoch == epoch+1);
7c673cae
FG
1919
1920 epoch++;
1921 modified = inc.modified;
1922
1923 // full map?
1924 if (inc.fullmap.length()) {
1925 bufferlist bl(inc.fullmap);
1926 decode(bl);
1927 return 0;
1928 }
1929
1930 // nope, incremental.
31f18b77 1931 if (inc.new_flags >= 0) {
7c673cae 1932 flags = inc.new_flags;
31f18b77
FG
1933 // the below is just to cover a newly-upgraded luminous mon
1934 // cluster that has to set require_jewel_osds or
1935 // require_kraken_osds before the osds can be upgraded to
1936 // luminous.
1937 if (flags & CEPH_OSDMAP_REQUIRE_KRAKEN) {
1938 if (require_osd_release < CEPH_RELEASE_KRAKEN) {
1939 require_osd_release = CEPH_RELEASE_KRAKEN;
1940 }
1941 } else if (flags & CEPH_OSDMAP_REQUIRE_JEWEL) {
1942 if (require_osd_release < CEPH_RELEASE_JEWEL) {
1943 require_osd_release = CEPH_RELEASE_JEWEL;
1944 }
1945 }
1946 }
7c673cae
FG
1947
1948 if (inc.new_max_osd >= 0)
1949 set_max_osd(inc.new_max_osd);
1950
1951 if (inc.new_pool_max != -1)
1952 pool_max = inc.new_pool_max;
1953
1954 for (const auto &pool : inc.new_pools) {
1955 pools[pool.first] = pool.second;
1956 pools[pool.first].last_change = epoch;
1957 }
1958
11fdf7f2
TL
1959 new_removed_snaps = inc.new_removed_snaps;
1960 new_purged_snaps = inc.new_purged_snaps;
1961 for (auto p = new_removed_snaps.begin();
1962 p != new_removed_snaps.end();
1963 ++p) {
1964 removed_snaps_queue[p->first].union_of(p->second);
1965 }
1966 for (auto p = new_purged_snaps.begin();
1967 p != new_purged_snaps.end();
1968 ++p) {
1969 auto q = removed_snaps_queue.find(p->first);
1970 ceph_assert(q != removed_snaps_queue.end());
1971 q->second.subtract(p->second);
1972 if (q->second.empty()) {
1973 removed_snaps_queue.erase(q);
1974 }
1975 }
1976
1977 if (inc.new_last_up_change != utime_t()) {
1978 last_up_change = inc.new_last_up_change;
1979 }
1980 if (inc.new_last_in_change != utime_t()) {
1981 last_in_change = inc.new_last_in_change;
1982 }
1983
7c673cae
FG
1984 for (const auto &pname : inc.new_pool_names) {
1985 auto pool_name_entry = pool_name.find(pname.first);
1986 if (pool_name_entry != pool_name.end()) {
1987 name_pool.erase(pool_name_entry->second);
1988 pool_name_entry->second = pname.second;
1989 } else {
1990 pool_name[pname.first] = pname.second;
1991 }
1992 name_pool[pname.second] = pname.first;
1993 }
1994
1995 for (const auto &pool : inc.old_pools) {
1996 pools.erase(pool);
1997 name_pool.erase(pool_name[pool]);
1998 pool_name.erase(pool);
1999 }
2000
2001 for (const auto &weight : inc.new_weight) {
2002 set_weight(weight.first, weight.second);
2003
2004 // if we are marking in, clear the AUTOOUT and NEW bits, and clear
2005 // xinfo old_weight.
2006 if (weight.second) {
2007 osd_state[weight.first] &= ~(CEPH_OSD_AUTOOUT | CEPH_OSD_NEW);
2008 osd_xinfo[weight.first].old_weight = 0;
2009 }
2010 }
2011
2012 for (const auto &primary_affinity : inc.new_primary_affinity) {
2013 set_primary_affinity(primary_affinity.first, primary_affinity.second);
2014 }
2015
2016 // erasure_code_profiles
2017 for (const auto &profile : inc.old_erasure_code_profiles)
2018 erasure_code_profiles.erase(profile);
2019
2020 for (const auto &profile : inc.new_erasure_code_profiles) {
2021 set_erasure_code_profile(profile.first, profile.second);
2022 }
2023
2024 // up/down
2025 for (const auto &state : inc.new_state) {
2026 const auto osd = state.first;
2027 int s = state.second ? state.second : CEPH_OSD_UP;
2028 if ((osd_state[osd] & CEPH_OSD_UP) &&
2029 (s & CEPH_OSD_UP)) {
2030 osd_info[osd].down_at = epoch;
2031 osd_xinfo[osd].down_stamp = modified;
2032 }
2033 if ((osd_state[osd] & CEPH_OSD_EXISTS) &&
2034 (s & CEPH_OSD_EXISTS)) {
2035 // osd is destroyed; clear out anything interesting.
2036 (*osd_uuid)[osd] = uuid_d();
2037 osd_info[osd] = osd_info_t();
2038 osd_xinfo[osd] = osd_xinfo_t();
2039 set_primary_affinity(osd, CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
11fdf7f2
TL
2040 osd_addrs->client_addrs[osd].reset(new entity_addrvec_t());
2041 osd_addrs->cluster_addrs[osd].reset(new entity_addrvec_t());
2042 osd_addrs->hb_front_addrs[osd].reset(new entity_addrvec_t());
2043 osd_addrs->hb_back_addrs[osd].reset(new entity_addrvec_t());
7c673cae
FG
2044 osd_state[osd] = 0;
2045 } else {
2046 osd_state[osd] ^= s;
2047 }
2048 }
2049
2050 for (const auto &client : inc.new_up_client) {
2051 osd_state[client.first] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
11fdf7f2
TL
2052 osd_addrs->client_addrs[client.first].reset(
2053 new entity_addrvec_t(client.second));
2054 osd_addrs->hb_back_addrs[client.first].reset(
2055 new entity_addrvec_t(inc.new_hb_back_up.find(client.first)->second));
2056 osd_addrs->hb_front_addrs[client.first].reset(
2057 new entity_addrvec_t(inc.new_hb_front_up.find(client.first)->second));
7c673cae
FG
2058
2059 osd_info[client.first].up_from = epoch;
2060 }
2061
2062 for (const auto &cluster : inc.new_up_cluster)
11fdf7f2
TL
2063 osd_addrs->cluster_addrs[cluster.first].reset(
2064 new entity_addrvec_t(cluster.second));
7c673cae
FG
2065
2066 // info
2067 for (const auto &thru : inc.new_up_thru)
2068 osd_info[thru.first].up_thru = thru.second;
2069
2070 for (const auto &interval : inc.new_last_clean_interval) {
2071 osd_info[interval.first].last_clean_begin = interval.second.first;
2072 osd_info[interval.first].last_clean_end = interval.second.second;
2073 }
2074
2075 for (const auto &lost : inc.new_lost)
2076 osd_info[lost.first].lost_at = lost.second;
2077
2078 // xinfo
2079 for (const auto &xinfo : inc.new_xinfo)
2080 osd_xinfo[xinfo.first] = xinfo.second;
2081
2082 // uuid
2083 for (const auto &uuid : inc.new_uuid)
2084 (*osd_uuid)[uuid.first] = uuid.second;
2085
2086 // pg rebuild
2087 for (const auto &pg : inc.new_pg_temp) {
2088 if (pg.second.empty())
2089 pg_temp->erase(pg.first);
2090 else
31f18b77
FG
2091 pg_temp->set(pg.first, pg.second);
2092 }
2093 if (!inc.new_pg_temp.empty()) {
2094 // make sure pg_temp is efficiently stored
2095 pg_temp->rebuild();
7c673cae
FG
2096 }
2097
2098 for (const auto &pg : inc.new_primary_temp) {
2099 if (pg.second == -1)
2100 primary_temp->erase(pg.first);
2101 else
2102 (*primary_temp)[pg.first] = pg.second;
2103 }
2104
2105 for (auto& p : inc.new_pg_upmap) {
2106 pg_upmap[p.first] = p.second;
2107 }
2108 for (auto& pg : inc.old_pg_upmap) {
2109 pg_upmap.erase(pg);
2110 }
2111 for (auto& p : inc.new_pg_upmap_items) {
2112 pg_upmap_items[p.first] = p.second;
2113 }
2114 for (auto& pg : inc.old_pg_upmap_items) {
2115 pg_upmap_items.erase(pg);
2116 }
2117
2118 // blacklist
2119 if (!inc.new_blacklist.empty()) {
2120 blacklist.insert(inc.new_blacklist.begin(),inc.new_blacklist.end());
2121 new_blacklist_entries = true;
2122 }
2123 for (const auto &addr : inc.old_blacklist)
2124 blacklist.erase(addr);
2125
81eedcae
TL
2126 for (auto& i : inc.new_crush_node_flags) {
2127 if (i.second) {
2128 crush_node_flags[i.first] = i.second;
2129 } else {
2130 crush_node_flags.erase(i.first);
2131 }
2132 }
2133
2134 for (auto& i : inc.new_device_class_flags) {
2135 if (i.second) {
2136 device_class_flags[i.first] = i.second;
2137 } else {
2138 device_class_flags.erase(i.first);
2139 }
2140 }
2141
7c673cae
FG
2142 // cluster snapshot?
2143 if (inc.cluster_snapshot.length()) {
2144 cluster_snapshot = inc.cluster_snapshot;
2145 cluster_snapshot_epoch = inc.epoch;
2146 } else {
2147 cluster_snapshot.clear();
2148 cluster_snapshot_epoch = 0;
2149 }
2150
2151 if (inc.new_nearfull_ratio >= 0) {
2152 nearfull_ratio = inc.new_nearfull_ratio;
2153 }
2154 if (inc.new_backfillfull_ratio >= 0) {
2155 backfillfull_ratio = inc.new_backfillfull_ratio;
2156 }
2157 if (inc.new_full_ratio >= 0) {
2158 full_ratio = inc.new_full_ratio;
2159 }
31f18b77 2160 if (inc.new_require_min_compat_client > 0) {
7c673cae
FG
2161 require_min_compat_client = inc.new_require_min_compat_client;
2162 }
31f18b77
FG
2163 if (inc.new_require_osd_release >= 0) {
2164 require_osd_release = inc.new_require_osd_release;
2165 if (require_osd_release >= CEPH_RELEASE_LUMINOUS) {
2166 flags &= ~(CEPH_OSDMAP_LEGACY_REQUIRE_FLAGS);
c07f9fc5 2167 flags |= CEPH_OSDMAP_RECOVERY_DELETES;
31f18b77
FG
2168 }
2169 }
7c673cae 2170
11fdf7f2
TL
2171 if (inc.new_require_osd_release >= 0) {
2172 require_osd_release = inc.new_require_osd_release;
2173 if (require_osd_release >= CEPH_RELEASE_NAUTILUS) {
2174 flags |= CEPH_OSDMAP_PGLOG_HARDLIMIT;
2175 }
2176 }
7c673cae
FG
2177 // do new crush map last (after up/down stuff)
2178 if (inc.crush.length()) {
2179 bufferlist bl(inc.crush);
11fdf7f2 2180 auto blp = bl.cbegin();
7c673cae
FG
2181 crush.reset(new CrushWrapper);
2182 crush->decode(blp);
31f18b77
FG
2183 if (require_osd_release >= CEPH_RELEASE_LUMINOUS) {
2184 // only increment if this is a luminous-encoded osdmap, lest
2185 // the mon's crush_version diverge from what the osds or others
2186 // are decoding and applying on their end. if we won't encode
2187 // it in the canonical version, don't change it.
2188 ++crush_version;
2189 }
81eedcae
TL
2190 for (auto it = device_class_flags.begin();
2191 it != device_class_flags.end();) {
2192 const char* class_name = crush->get_class_name(it->first);
2193 if (!class_name) // device class is gone
2194 it = device_class_flags.erase(it);
2195 else
2196 it++;
2197 }
7c673cae
FG
2198 }
2199
2200 calc_num_osds();
2201 _calc_up_osd_features();
2202 return 0;
2203}
2204
2205// mapping
2206int OSDMap::map_to_pg(
2207 int64_t poolid,
2208 const string& name,
2209 const string& key,
2210 const string& nspace,
2211 pg_t *pg) const
2212{
2213 // calculate ps (placement seed)
2214 const pg_pool_t *pool = get_pg_pool(poolid);
2215 if (!pool)
2216 return -ENOENT;
2217 ps_t ps;
2218 if (!key.empty())
2219 ps = pool->hash_key(key, nspace);
2220 else
2221 ps = pool->hash_key(name, nspace);
2222 *pg = pg_t(ps, poolid);
2223 return 0;
2224}
2225
2226int OSDMap::object_locator_to_pg(
2227 const object_t& oid, const object_locator_t& loc, pg_t &pg) const
2228{
2229 if (loc.hash >= 0) {
2230 if (!get_pg_pool(loc.get_pool())) {
2231 return -ENOENT;
2232 }
2233 pg = pg_t(loc.hash, loc.get_pool());
2234 return 0;
2235 }
2236 return map_to_pg(loc.get_pool(), oid.name, loc.key, loc.nspace, &pg);
2237}
2238
2239ceph_object_layout OSDMap::make_object_layout(
2240 object_t oid, int pg_pool, string nspace) const
2241{
2242 object_locator_t loc(pg_pool, nspace);
2243
2244 ceph_object_layout ol;
2245 pg_t pgid = object_locator_to_pg(oid, loc);
2246 ol.ol_pgid = pgid.get_old_pg().v;
2247 ol.ol_stripe_unit = 0;
2248 return ol;
2249}
2250
2251void OSDMap::_remove_nonexistent_osds(const pg_pool_t& pool,
2252 vector<int>& osds) const
2253{
2254 if (pool.can_shift_osds()) {
2255 unsigned removed = 0;
2256 for (unsigned i = 0; i < osds.size(); i++) {
2257 if (!exists(osds[i])) {
2258 removed++;
2259 continue;
2260 }
2261 if (removed) {
2262 osds[i - removed] = osds[i];
2263 }
2264 }
2265 if (removed)
2266 osds.resize(osds.size() - removed);
2267 } else {
2268 for (auto& osd : osds) {
2269 if (!exists(osd))
2270 osd = CRUSH_ITEM_NONE;
2271 }
2272 }
2273}
2274
31f18b77 2275void OSDMap::_pg_to_raw_osds(
7c673cae
FG
2276 const pg_pool_t& pool, pg_t pg,
2277 vector<int> *osds,
2278 ps_t *ppps) const
2279{
2280 // map to osds[]
2281 ps_t pps = pool.raw_pg_to_pps(pg); // placement ps
2282 unsigned size = pool.get_size();
2283
2284 // what crush rule?
31f18b77 2285 int ruleno = crush->find_rule(pool.get_crush_rule(), pool.get_type(), size);
7c673cae
FG
2286 if (ruleno >= 0)
2287 crush->do_rule(ruleno, pps, *osds, size, osd_weight, pg.pool());
2288
2289 _remove_nonexistent_osds(pool, *osds);
2290
2291 if (ppps)
2292 *ppps = pps;
7c673cae
FG
2293}
2294
2295int OSDMap::_pick_primary(const vector<int>& osds) const
2296{
2297 for (auto osd : osds) {
2298 if (osd != CRUSH_ITEM_NONE) {
2299 return osd;
2300 }
2301 }
2302 return -1;
2303}
2304
224ce89b 2305void OSDMap::_apply_upmap(const pg_pool_t& pi, pg_t raw_pg, vector<int> *raw) const
7c673cae
FG
2306{
2307 pg_t pg = pi.raw_pg_to_pg(raw_pg);
2308 auto p = pg_upmap.find(pg);
2309 if (p != pg_upmap.end()) {
2310 // make sure targets aren't marked out
2311 for (auto osd : p->second) {
91327a77
AA
2312 if (osd != CRUSH_ITEM_NONE && osd < max_osd && osd >= 0 &&
2313 osd_weight[osd] == 0) {
7c673cae
FG
2314 // reject/ignore the explicit mapping
2315 return;
2316 }
2317 }
2318 *raw = vector<int>(p->second.begin(), p->second.end());
224ce89b 2319 // continue to check and apply pg_upmap_items if any
7c673cae
FG
2320 }
2321
2322 auto q = pg_upmap_items.find(pg);
2323 if (q != pg_upmap_items.end()) {
181888fb
FG
2324 // NOTE: this approach does not allow a bidirectional swap,
2325 // e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1].
2326 for (auto& r : q->second) {
2327 // make sure the replacement value doesn't already appear
2328 bool exists = false;
2329 ssize_t pos = -1;
2330 for (unsigned i = 0; i < raw->size(); ++i) {
2331 int osd = (*raw)[i];
2332 if (osd == r.second) {
2333 exists = true;
2334 break;
2335 }
2336 // ignore mapping if target is marked out (or invalid osd id)
2337 if (osd == r.first &&
2338 pos < 0 &&
2339 !(r.second != CRUSH_ITEM_NONE && r.second < max_osd &&
91327a77 2340 r.second >= 0 && osd_weight[r.second] == 0)) {
181888fb
FG
2341 pos = i;
2342 }
2343 }
2344 if (!exists && pos >= 0) {
2345 (*raw)[pos] = r.second;
7c673cae
FG
2346 }
2347 }
2348 }
2349}
2350
2351// pg -> (up osd list)
2352void OSDMap::_raw_to_up_osds(const pg_pool_t& pool, const vector<int>& raw,
2353 vector<int> *up) const
2354{
2355 if (pool.can_shift_osds()) {
2356 // shift left
2357 up->clear();
2358 up->reserve(raw.size());
2359 for (unsigned i=0; i<raw.size(); i++) {
2360 if (!exists(raw[i]) || is_down(raw[i]))
2361 continue;
2362 up->push_back(raw[i]);
2363 }
2364 } else {
2365 // set down/dne devices to NONE
2366 up->resize(raw.size());
2367 for (int i = raw.size() - 1; i >= 0; --i) {
2368 if (!exists(raw[i]) || is_down(raw[i])) {
2369 (*up)[i] = CRUSH_ITEM_NONE;
2370 } else {
2371 (*up)[i] = raw[i];
2372 }
2373 }
2374 }
2375}
2376
2377void OSDMap::_apply_primary_affinity(ps_t seed,
2378 const pg_pool_t& pool,
2379 vector<int> *osds,
2380 int *primary) const
2381{
2382 // do we have any non-default primary_affinity values for these osds?
2383 if (!osd_primary_affinity)
2384 return;
2385
2386 bool any = false;
2387 for (const auto osd : *osds) {
2388 if (osd != CRUSH_ITEM_NONE &&
2389 (*osd_primary_affinity)[osd] != CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2390 any = true;
2391 break;
2392 }
2393 }
2394 if (!any)
2395 return;
2396
2397 // pick the primary. feed both the seed (for the pg) and the osd
2398 // into the hash/rng so that a proportional fraction of an osd's pgs
2399 // get rejected as primary.
2400 int pos = -1;
2401 for (unsigned i = 0; i < osds->size(); ++i) {
2402 int o = (*osds)[i];
2403 if (o == CRUSH_ITEM_NONE)
2404 continue;
2405 unsigned a = (*osd_primary_affinity)[o];
2406 if (a < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2407 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2408 seed, o) >> 16) >= a) {
2409 // we chose not to use this primary. note it anyway as a
2410 // fallback in case we don't pick anyone else, but keep looking.
2411 if (pos < 0)
2412 pos = i;
2413 } else {
2414 pos = i;
2415 break;
2416 }
2417 }
2418 if (pos < 0)
2419 return;
2420
2421 *primary = (*osds)[pos];
2422
2423 if (pool.can_shift_osds() && pos > 0) {
2424 // move the new primary to the front.
2425 for (int i = pos; i > 0; --i) {
2426 (*osds)[i] = (*osds)[i-1];
2427 }
2428 (*osds)[0] = *primary;
2429 }
2430}
2431
2432void OSDMap::_get_temp_osds(const pg_pool_t& pool, pg_t pg,
2433 vector<int> *temp_pg, int *temp_primary) const
2434{
2435 pg = pool.raw_pg_to_pg(pg);
2436 const auto p = pg_temp->find(pg);
2437 temp_pg->clear();
2438 if (p != pg_temp->end()) {
2439 for (unsigned i=0; i<p->second.size(); i++) {
2440 if (!exists(p->second[i]) || is_down(p->second[i])) {
2441 if (pool.can_shift_osds()) {
2442 continue;
2443 } else {
2444 temp_pg->push_back(CRUSH_ITEM_NONE);
2445 }
2446 } else {
2447 temp_pg->push_back(p->second[i]);
2448 }
2449 }
2450 }
2451 const auto &pp = primary_temp->find(pg);
2452 *temp_primary = -1;
2453 if (pp != primary_temp->end()) {
2454 *temp_primary = pp->second;
2455 } else if (!temp_pg->empty()) { // apply pg_temp's primary
2456 for (unsigned i = 0; i < temp_pg->size(); ++i) {
2457 if ((*temp_pg)[i] != CRUSH_ITEM_NONE) {
2458 *temp_primary = (*temp_pg)[i];
2459 break;
2460 }
2461 }
2462 }
2463}
2464
31f18b77 2465void OSDMap::pg_to_raw_osds(pg_t pg, vector<int> *raw, int *primary) const
7c673cae 2466{
7c673cae 2467 const pg_pool_t *pool = get_pg_pool(pg.pool());
11fdf7f2
TL
2468 if (!pool) {
2469 *primary = -1;
2470 raw->clear();
31f18b77 2471 return;
11fdf7f2 2472 }
31f18b77 2473 _pg_to_raw_osds(*pool, pg, raw, NULL);
11fdf7f2 2474 *primary = _pick_primary(*raw);
7c673cae
FG
2475}
2476
a8e16298
TL
2477void OSDMap::pg_to_raw_upmap(pg_t pg, vector<int> *raw_upmap) const
2478{
2479 auto pool = get_pg_pool(pg.pool());
2480 if (!pool) {
2481 raw_upmap->clear();
2482 return;
2483 }
2484 _pg_to_raw_osds(*pool, pg, raw_upmap, NULL);
2485 _apply_upmap(*pool, pg, raw_upmap);
2486}
2487
7c673cae
FG
2488void OSDMap::pg_to_raw_up(pg_t pg, vector<int> *up, int *primary) const
2489{
2490 const pg_pool_t *pool = get_pg_pool(pg.pool());
2491 if (!pool) {
11fdf7f2
TL
2492 *primary = -1;
2493 up->clear();
7c673cae
FG
2494 return;
2495 }
2496 vector<int> raw;
2497 ps_t pps;
2498 _pg_to_raw_osds(*pool, pg, &raw, &pps);
224ce89b 2499 _apply_upmap(*pool, pg, &raw);
7c673cae
FG
2500 _raw_to_up_osds(*pool, raw, up);
2501 *primary = _pick_primary(raw);
2502 _apply_primary_affinity(pps, *pool, up, primary);
2503}
31f18b77 2504
7c673cae
FG
2505void OSDMap::_pg_to_up_acting_osds(
2506 const pg_t& pg, vector<int> *up, int *up_primary,
2507 vector<int> *acting, int *acting_primary,
2508 bool raw_pg_to_pg) const
2509{
2510 const pg_pool_t *pool = get_pg_pool(pg.pool());
2511 if (!pool ||
2512 (!raw_pg_to_pg && pg.ps() >= pool->get_pg_num())) {
2513 if (up)
2514 up->clear();
2515 if (up_primary)
2516 *up_primary = -1;
2517 if (acting)
2518 acting->clear();
2519 if (acting_primary)
2520 *acting_primary = -1;
2521 return;
2522 }
2523 vector<int> raw;
2524 vector<int> _up;
2525 vector<int> _acting;
2526 int _up_primary;
2527 int _acting_primary;
2528 ps_t pps;
2529 _get_temp_osds(*pool, pg, &_acting, &_acting_primary);
2530 if (_acting.empty() || up || up_primary) {
2531 _pg_to_raw_osds(*pool, pg, &raw, &pps);
224ce89b 2532 _apply_upmap(*pool, pg, &raw);
7c673cae
FG
2533 _raw_to_up_osds(*pool, raw, &_up);
2534 _up_primary = _pick_primary(_up);
2535 _apply_primary_affinity(pps, *pool, &_up, &_up_primary);
2536 if (_acting.empty()) {
2537 _acting = _up;
2538 if (_acting_primary == -1) {
2539 _acting_primary = _up_primary;
2540 }
2541 }
2542
2543 if (up)
2544 up->swap(_up);
2545 if (up_primary)
2546 *up_primary = _up_primary;
2547 }
2548
2549 if (acting)
2550 acting->swap(_acting);
2551 if (acting_primary)
2552 *acting_primary = _acting_primary;
2553}
2554
2555int OSDMap::calc_pg_rank(int osd, const vector<int>& acting, int nrep)
2556{
2557 if (!nrep)
2558 nrep = acting.size();
2559 for (int i=0; i<nrep; i++)
2560 if (acting[i] == osd)
2561 return i;
2562 return -1;
2563}
2564
2565int OSDMap::calc_pg_role(int osd, const vector<int>& acting, int nrep)
2566{
2567 return calc_pg_rank(osd, acting, nrep);
2568}
2569
2570bool OSDMap::primary_changed(
2571 int oldprimary,
2572 const vector<int> &oldacting,
2573 int newprimary,
2574 const vector<int> &newacting)
2575{
2576 if (oldacting.empty() && newacting.empty())
2577 return false; // both still empty
2578 if (oldacting.empty() ^ newacting.empty())
2579 return true; // was empty, now not, or vice versa
2580 if (oldprimary != newprimary)
2581 return true; // primary changed
2582 if (calc_pg_rank(oldprimary, oldacting) !=
2583 calc_pg_rank(newprimary, newacting))
2584 return true;
2585 return false; // same primary (tho replicas may have changed)
2586}
2587
28e407b8
AA
2588uint64_t OSDMap::get_encoding_features() const
2589{
2590 uint64_t f = SIGNIFICANT_FEATURES;
11fdf7f2
TL
2591 if (require_osd_release < CEPH_RELEASE_NAUTILUS) {
2592 f &= ~CEPH_FEATURE_SERVER_NAUTILUS;
2593 }
2594 if (require_osd_release < CEPH_RELEASE_MIMIC) {
2595 f &= ~CEPH_FEATURE_SERVER_MIMIC;
2596 }
28e407b8
AA
2597 if (require_osd_release < CEPH_RELEASE_LUMINOUS) {
2598 f &= ~(CEPH_FEATURE_SERVER_LUMINOUS |
2599 CEPH_FEATURE_CRUSH_CHOOSE_ARGS);
2600 }
2601 if (require_osd_release < CEPH_RELEASE_KRAKEN) {
2602 f &= ~(CEPH_FEATURE_SERVER_KRAKEN |
1adf2230 2603 CEPH_FEATURE_MSG_ADDR2);
28e407b8
AA
2604 }
2605 if (require_osd_release < CEPH_RELEASE_JEWEL) {
2606 f &= ~(CEPH_FEATURE_SERVER_JEWEL |
1adf2230
AA
2607 CEPH_FEATURE_NEW_OSDOP_ENCODING |
2608 CEPH_FEATURE_CRUSH_TUNABLES5);
28e407b8
AA
2609 }
2610 return f;
2611}
7c673cae
FG
2612
2613// serialize, unserialize
2614void OSDMap::encode_client_old(bufferlist& bl) const
2615{
11fdf7f2 2616 using ceph::encode;
7c673cae 2617 __u16 v = 5;
11fdf7f2 2618 encode(v, bl);
7c673cae
FG
2619
2620 // base
11fdf7f2
TL
2621 encode(fsid, bl);
2622 encode(epoch, bl);
2623 encode(created, bl);
2624 encode(modified, bl);
7c673cae 2625
11fdf7f2 2626 // for encode(pools, bl);
7c673cae 2627 __u32 n = pools.size();
11fdf7f2 2628 encode(n, bl);
7c673cae
FG
2629
2630 for (const auto &pool : pools) {
2631 n = pool.first;
11fdf7f2
TL
2632 encode(n, bl);
2633 encode(pool.second, bl, 0);
7c673cae 2634 }
11fdf7f2 2635 // for encode(pool_name, bl);
7c673cae 2636 n = pool_name.size();
11fdf7f2 2637 encode(n, bl);
7c673cae
FG
2638 for (const auto &pname : pool_name) {
2639 n = pname.first;
11fdf7f2
TL
2640 encode(n, bl);
2641 encode(pname.second, bl);
7c673cae 2642 }
11fdf7f2 2643 // for encode(pool_max, bl);
7c673cae 2644 n = pool_max;
11fdf7f2 2645 encode(n, bl);
7c673cae 2646
11fdf7f2 2647 encode(flags, bl);
7c673cae 2648
11fdf7f2 2649 encode(max_osd, bl);
31f18b77
FG
2650 {
2651 uint32_t n = osd_state.size();
11fdf7f2 2652 encode(n, bl);
31f18b77 2653 for (auto s : osd_state) {
11fdf7f2 2654 encode((uint8_t)s, bl);
31f18b77
FG
2655 }
2656 }
11fdf7f2
TL
2657 encode(osd_weight, bl);
2658 encode(osd_addrs->client_addrs, bl, 0);
7c673cae 2659
11fdf7f2 2660 // for encode(pg_temp, bl);
7c673cae 2661 n = pg_temp->size();
11fdf7f2 2662 encode(n, bl);
7c673cae
FG
2663 for (const auto pg : *pg_temp) {
2664 old_pg_t opg = pg.first.get_old_pg();
11fdf7f2
TL
2665 encode(opg, bl);
2666 encode(pg.second, bl);
7c673cae
FG
2667 }
2668
2669 // crush
2670 bufferlist cbl;
2671 crush->encode(cbl, 0 /* legacy (no) features */);
11fdf7f2 2672 encode(cbl, bl);
7c673cae
FG
2673}
2674
2675void OSDMap::encode_classic(bufferlist& bl, uint64_t features) const
2676{
11fdf7f2 2677 using ceph::encode;
7c673cae
FG
2678 if ((features & CEPH_FEATURE_PGID64) == 0) {
2679 encode_client_old(bl);
2680 return;
2681 }
2682
2683 __u16 v = 6;
11fdf7f2 2684 encode(v, bl);
7c673cae
FG
2685
2686 // base
11fdf7f2
TL
2687 encode(fsid, bl);
2688 encode(epoch, bl);
2689 encode(created, bl);
2690 encode(modified, bl);
7c673cae 2691
11fdf7f2
TL
2692 encode(pools, bl, features);
2693 encode(pool_name, bl);
2694 encode(pool_max, bl);
7c673cae 2695
11fdf7f2 2696 encode(flags, bl);
7c673cae 2697
11fdf7f2 2698 encode(max_osd, bl);
31f18b77
FG
2699 {
2700 uint32_t n = osd_state.size();
11fdf7f2 2701 encode(n, bl);
31f18b77 2702 for (auto s : osd_state) {
11fdf7f2 2703 encode((uint8_t)s, bl);
31f18b77
FG
2704 }
2705 }
11fdf7f2
TL
2706 encode(osd_weight, bl);
2707 encode(osd_addrs->client_addrs, bl, features);
7c673cae 2708
11fdf7f2 2709 encode(*pg_temp, bl);
7c673cae
FG
2710
2711 // crush
2712 bufferlist cbl;
2713 crush->encode(cbl, 0 /* legacy (no) features */);
11fdf7f2 2714 encode(cbl, bl);
7c673cae
FG
2715
2716 // extended
2717 __u16 ev = 10;
11fdf7f2
TL
2718 encode(ev, bl);
2719 encode(osd_addrs->hb_back_addrs, bl, features);
2720 encode(osd_info, bl);
2721 encode(blacklist, bl, features);
2722 encode(osd_addrs->cluster_addrs, bl, features);
2723 encode(cluster_snapshot_epoch, bl);
2724 encode(cluster_snapshot, bl);
2725 encode(*osd_uuid, bl);
2726 encode(osd_xinfo, bl);
2727 encode(osd_addrs->hb_front_addrs, bl, features);
7c673cae
FG
2728}
2729
11fdf7f2
TL
2730/* for a description of osdmap versions, and when they were introduced, please
2731 * refer to
2732 * doc/dev/osd_internals/osdmap_versions.txt
2733 */
7c673cae
FG
2734void OSDMap::encode(bufferlist& bl, uint64_t features) const
2735{
11fdf7f2 2736 using ceph::encode;
7c673cae
FG
2737 if ((features & CEPH_FEATURE_OSDMAP_ENC) == 0) {
2738 encode_classic(bl, features);
2739 return;
2740 }
2741
2742 // only a select set of callers should *ever* be encoding new
2743 // OSDMaps. others should be passing around the canonical encoded
2744 // buffers from on high. select out those callers by passing in an
2745 // "impossible" feature bit.
11fdf7f2 2746 ceph_assert(features & CEPH_FEATURE_RESERVED);
7c673cae
FG
2747 features &= ~CEPH_FEATURE_RESERVED;
2748
2749 size_t start_offset = bl.length();
2750 size_t tail_offset;
11fdf7f2
TL
2751 size_t crc_offset;
2752 std::optional<buffer::list::contiguous_filler> crc_filler;
7c673cae
FG
2753
2754 // meta-encoding: how we include client-used and osd-specific data
2755 ENCODE_START(8, 7, bl);
2756
2757 {
28e407b8
AA
2758 // NOTE: any new encoding dependencies must be reflected by
2759 // SIGNIFICANT_FEATURES
11fdf7f2 2760 uint8_t v = 9;
31f18b77 2761 if (!HAVE_FEATURE(features, SERVER_LUMINOUS)) {
7c673cae 2762 v = 3;
11fdf7f2
TL
2763 } else if (!HAVE_FEATURE(features, SERVER_MIMIC)) {
2764 v = 6;
2765 } else if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) {
2766 v = 7;
7c673cae
FG
2767 }
2768 ENCODE_START(v, 1, bl); // client-usable data
2769 // base
11fdf7f2
TL
2770 encode(fsid, bl);
2771 encode(epoch, bl);
2772 encode(created, bl);
2773 encode(modified, bl);
7c673cae 2774
11fdf7f2
TL
2775 encode(pools, bl, features);
2776 encode(pool_name, bl);
2777 encode(pool_max, bl);
7c673cae 2778
31f18b77
FG
2779 if (v < 4) {
2780 decltype(flags) f = flags;
2781 if (require_osd_release >= CEPH_RELEASE_LUMINOUS)
c07f9fc5 2782 f |= CEPH_OSDMAP_REQUIRE_LUMINOUS | CEPH_OSDMAP_RECOVERY_DELETES;
31f18b77
FG
2783 else if (require_osd_release == CEPH_RELEASE_KRAKEN)
2784 f |= CEPH_OSDMAP_REQUIRE_KRAKEN;
2785 else if (require_osd_release == CEPH_RELEASE_JEWEL)
2786 f |= CEPH_OSDMAP_REQUIRE_JEWEL;
11fdf7f2 2787 encode(f, bl);
31f18b77 2788 } else {
11fdf7f2 2789 encode(flags, bl);
31f18b77 2790 }
7c673cae 2791
11fdf7f2 2792 encode(max_osd, bl);
31f18b77 2793 if (v >= 5) {
11fdf7f2 2794 encode(osd_state, bl);
31f18b77
FG
2795 } else {
2796 uint32_t n = osd_state.size();
11fdf7f2 2797 encode(n, bl);
31f18b77 2798 for (auto s : osd_state) {
11fdf7f2 2799 encode((uint8_t)s, bl);
31f18b77
FG
2800 }
2801 }
11fdf7f2
TL
2802 encode(osd_weight, bl);
2803 if (v >= 8) {
2804 encode(osd_addrs->client_addrs, bl, features);
2805 } else {
2806 encode_addrvec_pvec_as_addr(osd_addrs->client_addrs, bl, features);
2807 }
7c673cae 2808
11fdf7f2
TL
2809 encode(*pg_temp, bl);
2810 encode(*primary_temp, bl);
7c673cae 2811 if (osd_primary_affinity) {
11fdf7f2 2812 encode(*osd_primary_affinity, bl);
7c673cae
FG
2813 } else {
2814 vector<__u32> v;
11fdf7f2 2815 encode(v, bl);
7c673cae
FG
2816 }
2817
2818 // crush
2819 bufferlist cbl;
2820 crush->encode(cbl, features);
11fdf7f2
TL
2821 encode(cbl, bl);
2822 encode(erasure_code_profiles, bl);
7c673cae
FG
2823
2824 if (v >= 4) {
11fdf7f2
TL
2825 encode(pg_upmap, bl);
2826 encode(pg_upmap_items, bl);
7c673cae 2827 } else {
11fdf7f2
TL
2828 ceph_assert(pg_upmap.empty());
2829 ceph_assert(pg_upmap_items.empty());
7c673cae 2830 }
31f18b77 2831 if (v >= 6) {
11fdf7f2
TL
2832 encode(crush_version, bl);
2833 }
2834 if (v >= 7) {
2835 encode(new_removed_snaps, bl);
2836 encode(new_purged_snaps, bl);
2837 }
2838 if (v >= 9) {
2839 encode(last_up_change, bl);
2840 encode(last_in_change, bl);
31f18b77 2841 }
7c673cae
FG
2842 ENCODE_FINISH(bl); // client-usable data
2843 }
2844
2845 {
28e407b8
AA
2846 // NOTE: any new encoding dependencies must be reflected by
2847 // SIGNIFICANT_FEATURES
81eedcae 2848 uint8_t target_v = 9;
7c673cae
FG
2849 if (!HAVE_FEATURE(features, SERVER_LUMINOUS)) {
2850 target_v = 1;
11fdf7f2
TL
2851 } else if (!HAVE_FEATURE(features, SERVER_MIMIC)) {
2852 target_v = 5;
2853 } else if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) {
2854 target_v = 6;
7c673cae
FG
2855 }
2856 ENCODE_START(target_v, 1, bl); // extended, osd-only data
11fdf7f2
TL
2857 if (target_v < 7) {
2858 encode_addrvec_pvec_as_addr(osd_addrs->hb_back_addrs, bl, features);
2859 } else {
2860 encode(osd_addrs->hb_back_addrs, bl, features);
2861 }
2862 encode(osd_info, bl);
7c673cae
FG
2863 {
2864 // put this in a sorted, ordered map<> so that we encode in a
2865 // deterministic order.
2866 map<entity_addr_t,utime_t> blacklist_map;
2867 for (const auto &addr : blacklist)
2868 blacklist_map.insert(make_pair(addr.first, addr.second));
11fdf7f2
TL
2869 encode(blacklist_map, bl, features);
2870 }
2871 if (target_v < 7) {
2872 encode_addrvec_pvec_as_addr(osd_addrs->cluster_addrs, bl, features);
2873 } else {
2874 encode(osd_addrs->cluster_addrs, bl, features);
2875 }
2876 encode(cluster_snapshot_epoch, bl);
2877 encode(cluster_snapshot, bl);
2878 encode(*osd_uuid, bl);
2879 encode(osd_xinfo, bl);
2880 if (target_v < 7) {
2881 encode_addrvec_pvec_as_addr(osd_addrs->hb_front_addrs, bl, features);
2882 } else {
2883 encode(osd_addrs->hb_front_addrs, bl, features);
2884 }
7c673cae 2885 if (target_v >= 2) {
11fdf7f2
TL
2886 encode(nearfull_ratio, bl);
2887 encode(full_ratio, bl);
2888 encode(backfillfull_ratio, bl);
31f18b77
FG
2889 }
2890 // 4 was string-based new_require_min_compat_client
2891 if (target_v >= 5) {
11fdf7f2
TL
2892 encode(require_min_compat_client, bl);
2893 encode(require_osd_release, bl);
2894 }
2895 if (target_v >= 6) {
2896 encode(removed_snaps_queue, bl);
7c673cae 2897 }
81eedcae
TL
2898 if (target_v >= 8) {
2899 encode(crush_node_flags, bl);
2900 }
2901 if (target_v >= 9) {
2902 encode(device_class_flags, bl);
2903 }
7c673cae
FG
2904 ENCODE_FINISH(bl); // osd-only data
2905 }
2906
11fdf7f2
TL
2907 crc_offset = bl.length();
2908 crc_filler = bl.append_hole(sizeof(uint32_t));
7c673cae
FG
2909 tail_offset = bl.length();
2910
2911 ENCODE_FINISH(bl); // meta-encoding wrapper
2912
2913 // fill in crc
2914 bufferlist front;
11fdf7f2 2915 front.substr_of(bl, start_offset, crc_offset - start_offset);
7c673cae
FG
2916 crc = front.crc32c(-1);
2917 if (tail_offset < bl.length()) {
2918 bufferlist tail;
2919 tail.substr_of(bl, tail_offset, bl.length() - tail_offset);
2920 crc = tail.crc32c(crc);
2921 }
2922 ceph_le32 crc_le;
2923 crc_le = crc;
11fdf7f2 2924 crc_filler->copy_in(4, (char*)&crc_le);
7c673cae
FG
2925 crc_defined = true;
2926}
2927
11fdf7f2
TL
2928/* for a description of osdmap versions, and when they were introduced, please
2929 * refer to
2930 * doc/dev/osd_internals/osdmap_versions.txt
2931 */
7c673cae
FG
2932void OSDMap::decode(bufferlist& bl)
2933{
11fdf7f2 2934 auto p = bl.cbegin();
7c673cae
FG
2935 decode(p);
2936}
2937
11fdf7f2 2938void OSDMap::decode_classic(bufferlist::const_iterator& p)
7c673cae 2939{
11fdf7f2 2940 using ceph::decode;
7c673cae
FG
2941 __u32 n, t;
2942 __u16 v;
11fdf7f2 2943 decode(v, p);
7c673cae
FG
2944
2945 // base
11fdf7f2
TL
2946 decode(fsid, p);
2947 decode(epoch, p);
2948 decode(created, p);
2949 decode(modified, p);
7c673cae
FG
2950
2951 if (v < 6) {
2952 if (v < 4) {
2953 int32_t max_pools = 0;
11fdf7f2 2954 decode(max_pools, p);
7c673cae
FG
2955 pool_max = max_pools;
2956 }
2957 pools.clear();
11fdf7f2 2958 decode(n, p);
7c673cae 2959 while (n--) {
11fdf7f2
TL
2960 decode(t, p);
2961 decode(pools[t], p);
7c673cae
FG
2962 }
2963 if (v == 4) {
11fdf7f2 2964 decode(n, p);
7c673cae
FG
2965 pool_max = n;
2966 } else if (v == 5) {
2967 pool_name.clear();
11fdf7f2 2968 decode(n, p);
7c673cae 2969 while (n--) {
11fdf7f2
TL
2970 decode(t, p);
2971 decode(pool_name[t], p);
7c673cae 2972 }
11fdf7f2 2973 decode(n, p);
7c673cae
FG
2974 pool_max = n;
2975 }
2976 } else {
11fdf7f2
TL
2977 decode(pools, p);
2978 decode(pool_name, p);
2979 decode(pool_max, p);
7c673cae
FG
2980 }
2981 // kludge around some old bug that zeroed out pool_max (#2307)
2982 if (pools.size() && pool_max < pools.rbegin()->first) {
2983 pool_max = pools.rbegin()->first;
2984 }
2985
11fdf7f2 2986 decode(flags, p);
7c673cae 2987
11fdf7f2 2988 decode(max_osd, p);
31f18b77
FG
2989 {
2990 vector<uint8_t> os;
11fdf7f2 2991 decode(os, p);
31f18b77
FG
2992 osd_state.resize(os.size());
2993 for (unsigned i = 0; i < os.size(); ++i) {
2994 osd_state[i] = os[i];
2995 }
2996 }
11fdf7f2
TL
2997 decode(osd_weight, p);
2998 decode(osd_addrs->client_addrs, p);
7c673cae
FG
2999 if (v <= 5) {
3000 pg_temp->clear();
11fdf7f2 3001 decode(n, p);
7c673cae
FG
3002 while (n--) {
3003 old_pg_t opg;
3004 ::decode_raw(opg, p);
31f18b77 3005 mempool::osdmap::vector<int32_t> v;
11fdf7f2 3006 decode(v, p);
31f18b77 3007 pg_temp->set(pg_t(opg), v);
7c673cae
FG
3008 }
3009 } else {
11fdf7f2 3010 decode(*pg_temp, p);
7c673cae
FG
3011 }
3012
3013 // crush
3014 bufferlist cbl;
11fdf7f2
TL
3015 decode(cbl, p);
3016 auto cblp = cbl.cbegin();
7c673cae
FG
3017 crush->decode(cblp);
3018
3019 // extended
3020 __u16 ev = 0;
3021 if (v >= 5)
11fdf7f2
TL
3022 decode(ev, p);
3023 decode(osd_addrs->hb_back_addrs, p);
3024 decode(osd_info, p);
7c673cae 3025 if (v < 5)
11fdf7f2 3026 decode(pool_name, p);
7c673cae 3027
11fdf7f2 3028 decode(blacklist, p);
7c673cae 3029 if (ev >= 6)
11fdf7f2 3030 decode(osd_addrs->cluster_addrs, p);
7c673cae 3031 else
11fdf7f2 3032 osd_addrs->cluster_addrs.resize(osd_addrs->client_addrs.size());
7c673cae
FG
3033
3034 if (ev >= 7) {
11fdf7f2
TL
3035 decode(cluster_snapshot_epoch, p);
3036 decode(cluster_snapshot, p);
7c673cae
FG
3037 }
3038
3039 if (ev >= 8) {
11fdf7f2 3040 decode(*osd_uuid, p);
7c673cae
FG
3041 } else {
3042 osd_uuid->resize(max_osd);
3043 }
3044 if (ev >= 9)
11fdf7f2 3045 decode(osd_xinfo, p);
7c673cae
FG
3046 else
3047 osd_xinfo.resize(max_osd);
3048
3049 if (ev >= 10)
11fdf7f2 3050 decode(osd_addrs->hb_front_addrs, p);
7c673cae 3051 else
11fdf7f2 3052 osd_addrs->hb_front_addrs.resize(osd_addrs->hb_back_addrs.size());
7c673cae
FG
3053
3054 osd_primary_affinity.reset();
3055
3056 post_decode();
3057}
3058
11fdf7f2 3059void OSDMap::decode(bufferlist::const_iterator& bl)
7c673cae 3060{
11fdf7f2 3061 using ceph::decode;
7c673cae
FG
3062 /**
3063 * Older encodings of the OSDMap had a single struct_v which
3064 * covered the whole encoding, and was prior to our modern
3065 * stuff which includes a compatv and a size. So if we see
3066 * a struct_v < 7, we must rewind to the beginning and use our
3067 * classic decoder.
3068 */
3069 size_t start_offset = bl.get_off();
3070 size_t tail_offset = 0;
3071 bufferlist crc_front, crc_tail;
3072
3073 DECODE_START_LEGACY_COMPAT_LEN(8, 7, 7, bl); // wrapper
3074 if (struct_v < 7) {
11fdf7f2 3075 bl.seek(start_offset);
7c673cae
FG
3076 decode_classic(bl);
3077 return;
3078 }
3079 /**
3080 * Since we made it past that hurdle, we can use our normal paths.
3081 */
3082 {
11fdf7f2 3083 DECODE_START(9, bl); // client-usable data
7c673cae 3084 // base
11fdf7f2
TL
3085 decode(fsid, bl);
3086 decode(epoch, bl);
3087 decode(created, bl);
3088 decode(modified, bl);
7c673cae 3089
11fdf7f2
TL
3090 decode(pools, bl);
3091 decode(pool_name, bl);
3092 decode(pool_max, bl);
7c673cae 3093
11fdf7f2 3094 decode(flags, bl);
7c673cae 3095
11fdf7f2 3096 decode(max_osd, bl);
31f18b77 3097 if (struct_v >= 5) {
11fdf7f2 3098 decode(osd_state, bl);
31f18b77
FG
3099 } else {
3100 vector<uint8_t> os;
11fdf7f2 3101 decode(os, bl);
31f18b77
FG
3102 osd_state.resize(os.size());
3103 for (unsigned i = 0; i < os.size(); ++i) {
3104 osd_state[i] = os[i];
3105 }
3106 }
11fdf7f2
TL
3107 decode(osd_weight, bl);
3108 decode(osd_addrs->client_addrs, bl);
7c673cae 3109
11fdf7f2
TL
3110 decode(*pg_temp, bl);
3111 decode(*primary_temp, bl);
3112 // dates back to firefly. version increased from 2 to 3 still in firefly.
3113 // do we really still need to keep this around? even for old clients?
7c673cae
FG
3114 if (struct_v >= 2) {
3115 osd_primary_affinity.reset(new mempool::osdmap::vector<__u32>);
11fdf7f2 3116 decode(*osd_primary_affinity, bl);
7c673cae
FG
3117 if (osd_primary_affinity->empty())
3118 osd_primary_affinity.reset();
3119 } else {
3120 osd_primary_affinity.reset();
3121 }
3122
3123 // crush
3124 bufferlist cbl;
11fdf7f2
TL
3125 decode(cbl, bl);
3126 auto cblp = cbl.cbegin();
7c673cae 3127 crush->decode(cblp);
11fdf7f2
TL
3128 // added in firefly; version increased in luminous, so it affects
3129 // giant, hammer, infernallis, jewel, and kraken. probably should be left
3130 // alone until we require clients to be all luminous?
7c673cae 3131 if (struct_v >= 3) {
11fdf7f2 3132 decode(erasure_code_profiles, bl);
7c673cae
FG
3133 } else {
3134 erasure_code_profiles.clear();
3135 }
11fdf7f2
TL
3136 // version increased from 3 to 4 still in luminous, so same as above
3137 // applies.
7c673cae 3138 if (struct_v >= 4) {
11fdf7f2
TL
3139 decode(pg_upmap, bl);
3140 decode(pg_upmap_items, bl);
7c673cae
FG
3141 } else {
3142 pg_upmap.clear();
3143 pg_upmap_items.clear();
3144 }
11fdf7f2
TL
3145 // again, version increased from 5 to 6 still in luminous, so above
3146 // applies.
31f18b77 3147 if (struct_v >= 6) {
11fdf7f2
TL
3148 decode(crush_version, bl);
3149 }
3150 // version increase from 6 to 7 in mimic
3151 if (struct_v >= 7) {
3152 decode(new_removed_snaps, bl);
3153 decode(new_purged_snaps, bl);
3154 }
3155 // version increase from 7 to 8, 8 to 9, in nautilus.
3156 if (struct_v >= 9) {
3157 decode(last_up_change, bl);
3158 decode(last_in_change, bl);
31f18b77 3159 }
7c673cae
FG
3160 DECODE_FINISH(bl); // client-usable data
3161 }
3162
3163 {
81eedcae 3164 DECODE_START(9, bl); // extended, osd-only data
11fdf7f2
TL
3165 decode(osd_addrs->hb_back_addrs, bl);
3166 decode(osd_info, bl);
3167 decode(blacklist, bl);
3168 decode(osd_addrs->cluster_addrs, bl);
3169 decode(cluster_snapshot_epoch, bl);
3170 decode(cluster_snapshot, bl);
3171 decode(*osd_uuid, bl);
3172 decode(osd_xinfo, bl);
3173 decode(osd_addrs->hb_front_addrs, bl);
3174 //
7c673cae 3175 if (struct_v >= 2) {
11fdf7f2
TL
3176 decode(nearfull_ratio, bl);
3177 decode(full_ratio, bl);
7c673cae
FG
3178 } else {
3179 nearfull_ratio = 0;
3180 full_ratio = 0;
3181 }
3182 if (struct_v >= 3) {
11fdf7f2 3183 decode(backfillfull_ratio, bl);
7c673cae
FG
3184 } else {
3185 backfillfull_ratio = 0;
3186 }
31f18b77
FG
3187 if (struct_v == 4) {
3188 string r;
11fdf7f2 3189 decode(r, bl);
31f18b77
FG
3190 if (r.length())
3191 require_min_compat_client = ceph_release_from_name(r.c_str());
3192 }
3193 if (struct_v >= 5) {
11fdf7f2
TL
3194 decode(require_min_compat_client, bl);
3195 decode(require_osd_release, bl);
3196 if (require_osd_release >= CEPH_RELEASE_NAUTILUS) {
3197 flags |= CEPH_OSDMAP_PGLOG_HARDLIMIT;
3198 }
31f18b77
FG
3199 if (require_osd_release >= CEPH_RELEASE_LUMINOUS) {
3200 flags &= ~(CEPH_OSDMAP_LEGACY_REQUIRE_FLAGS);
c07f9fc5 3201 flags |= CEPH_OSDMAP_RECOVERY_DELETES;
31f18b77
FG
3202 }
3203 } else {
3204 if (flags & CEPH_OSDMAP_REQUIRE_LUMINOUS) {
3205 // only for compat with post-kraken pre-luminous test clusters
3206 require_osd_release = CEPH_RELEASE_LUMINOUS;
3207 flags &= ~(CEPH_OSDMAP_LEGACY_REQUIRE_FLAGS);
c07f9fc5 3208 flags |= CEPH_OSDMAP_RECOVERY_DELETES;
31f18b77
FG
3209 } else if (flags & CEPH_OSDMAP_REQUIRE_KRAKEN) {
3210 require_osd_release = CEPH_RELEASE_KRAKEN;
3211 } else if (flags & CEPH_OSDMAP_REQUIRE_JEWEL) {
3212 require_osd_release = CEPH_RELEASE_JEWEL;
3213 } else {
3214 require_osd_release = 0;
3215 }
3216 }
11fdf7f2
TL
3217 if (struct_v >= 6) {
3218 decode(removed_snaps_queue, bl);
3219 }
81eedcae
TL
3220 if (struct_v >= 8) {
3221 decode(crush_node_flags, bl);
3222 } else {
3223 crush_node_flags.clear();
3224 }
3225 if (struct_v >= 9) {
3226 decode(device_class_flags, bl);
3227 } else {
3228 device_class_flags.clear();
3229 }
7c673cae
FG
3230 DECODE_FINISH(bl); // osd-only data
3231 }
3232
3233 if (struct_v >= 8) {
3234 crc_front.substr_of(bl.get_bl(), start_offset, bl.get_off() - start_offset);
11fdf7f2 3235 decode(crc, bl);
7c673cae
FG
3236 tail_offset = bl.get_off();
3237 crc_defined = true;
3238 } else {
3239 crc_defined = false;
3240 crc = 0;
3241 }
3242
3243 DECODE_FINISH(bl); // wrapper
3244
3245 if (tail_offset) {
3246 // verify crc
3247 uint32_t actual = crc_front.crc32c(-1);
3248 if (tail_offset < bl.get_off()) {
3249 bufferlist tail;
3250 tail.substr_of(bl.get_bl(), tail_offset, bl.get_off() - tail_offset);
3251 actual = tail.crc32c(actual);
3252 }
3253 if (crc != actual) {
3254 ostringstream ss;
3255 ss << "bad crc, actual " << actual << " != expected " << crc;
3256 string s = ss.str();
3257 throw buffer::malformed_input(s.c_str());
3258 }
3259 }
3260
3261 post_decode();
3262}
3263
3264void OSDMap::post_decode()
3265{
3266 // index pool names
3267 name_pool.clear();
3268 for (const auto &pname : pool_name) {
3269 name_pool[pname.second] = pname.first;
3270 }
3271
3272 calc_num_osds();
3273 _calc_up_osd_features();
3274}
3275
3276void OSDMap::dump_erasure_code_profiles(
3277 const mempool::osdmap::map<string,map<string,string>>& profiles,
3278 Formatter *f)
3279{
3280 f->open_object_section("erasure_code_profiles");
3281 for (const auto &profile : profiles) {
3282 f->open_object_section(profile.first.c_str());
3283 for (const auto &profm : profile.second) {
3284 f->dump_string(profm.first.c_str(), profm.second.c_str());
3285 }
3286 f->close_section();
3287 }
3288 f->close_section();
3289}
3290
3291void OSDMap::dump(Formatter *f) const
3292{
3293 f->dump_int("epoch", get_epoch());
3294 f->dump_stream("fsid") << get_fsid();
3295 f->dump_stream("created") << get_created();
3296 f->dump_stream("modified") << get_modified();
11fdf7f2
TL
3297 f->dump_stream("last_up_change") << last_up_change;
3298 f->dump_stream("last_in_change") << last_in_change;
7c673cae 3299 f->dump_string("flags", get_flag_string());
11fdf7f2
TL
3300 f->dump_unsigned("flags_num", flags);
3301 f->open_array_section("flags_set");
3302 set<string> flagset;
3303 get_flag_set(&flagset);
3304 for (auto p : flagset) {
3305 f->dump_string("flag", p);
3306 }
3307 f->close_section();
31f18b77 3308 f->dump_unsigned("crush_version", get_crush_version());
7c673cae
FG
3309 f->dump_float("full_ratio", full_ratio);
3310 f->dump_float("backfillfull_ratio", backfillfull_ratio);
3311 f->dump_float("nearfull_ratio", nearfull_ratio);
3312 f->dump_string("cluster_snapshot", get_cluster_snapshot());
3313 f->dump_int("pool_max", get_pool_max());
3314 f->dump_int("max_osd", get_max_osd());
31f18b77
FG
3315 f->dump_string("require_min_compat_client",
3316 ceph_release_name(require_min_compat_client));
3317 f->dump_string("min_compat_client",
3318 ceph_release_name(get_min_compat_client()));
3319 f->dump_string("require_osd_release",
3320 ceph_release_name(require_osd_release));
7c673cae
FG
3321
3322 f->open_array_section("pools");
3323 for (const auto &pool : pools) {
3324 std::string name("<unknown>");
3325 const auto &pni = pool_name.find(pool.first);
3326 if (pni != pool_name.end())
3327 name = pni->second;
3328 f->open_object_section("pool");
3329 f->dump_int("pool", pool.first);
3330 f->dump_string("pool_name", name);
3331 pool.second.dump(f);
3332 f->close_section();
3333 }
3334 f->close_section();
3335
3336 f->open_array_section("osds");
3337 for (int i=0; i<get_max_osd(); i++)
3338 if (exists(i)) {
3339 f->open_object_section("osd_info");
3340 f->dump_int("osd", i);
3341 f->dump_stream("uuid") << get_uuid(i);
3342 f->dump_int("up", is_up(i));
3343 f->dump_int("in", is_in(i));
3344 f->dump_float("weight", get_weightf(i));
3345 f->dump_float("primary_affinity", get_primary_affinityf(i));
3346 get_info(i).dump(f);
11fdf7f2
TL
3347 f->dump_object("public_addrs", get_addrs(i));
3348 f->dump_object("cluster_addrs", get_cluster_addrs(i));
3349 f->dump_object("heartbeat_back_addrs", get_hb_back_addrs(i));
3350 f->dump_object("heartbeat_front_addrs", get_hb_front_addrs(i));
3351 // compat
3352 f->dump_stream("public_addr") << get_addrs(i).get_legacy_str();
3353 f->dump_stream("cluster_addr") << get_cluster_addrs(i).get_legacy_str();
3354 f->dump_stream("heartbeat_back_addr")
3355 << get_hb_back_addrs(i).get_legacy_str();
3356 f->dump_stream("heartbeat_front_addr")
3357 << get_hb_front_addrs(i).get_legacy_str();
7c673cae
FG
3358
3359 set<string> st;
3360 get_state(i, st);
3361 f->open_array_section("state");
3362 for (const auto &state : st)
3363 f->dump_string("state", state);
3364 f->close_section();
3365
3366 f->close_section();
3367 }
3368 f->close_section();
3369
3370 f->open_array_section("osd_xinfo");
3371 for (int i=0; i<get_max_osd(); i++) {
3372 if (exists(i)) {
3373 f->open_object_section("xinfo");
3374 f->dump_int("osd", i);
3375 osd_xinfo[i].dump(f);
3376 f->close_section();
3377 }
3378 }
3379 f->close_section();
3380
3381 f->open_array_section("pg_upmap");
3382 for (auto& p : pg_upmap) {
3383 f->open_object_section("mapping");
3384 f->dump_stream("pgid") << p.first;
3385 f->open_array_section("osds");
3386 for (auto q : p.second) {
3387 f->dump_int("osd", q);
3388 }
3389 f->close_section();
3390 f->close_section();
3391 }
3392 f->close_section();
3393 f->open_array_section("pg_upmap_items");
3394 for (auto& p : pg_upmap_items) {
3395 f->open_object_section("mapping");
3396 f->dump_stream("pgid") << p.first;
3397 f->open_array_section("mappings");
3398 for (auto& q : p.second) {
3399 f->open_object_section("mapping");
3400 f->dump_int("from", q.first);
3401 f->dump_int("to", q.second);
3402 f->close_section();
3403 }
3404 f->close_section();
3405 f->close_section();
3406 }
3407 f->close_section();
3408 f->open_array_section("pg_temp");
31f18b77 3409 pg_temp->dump(f);
7c673cae
FG
3410 f->close_section();
3411
3412 f->open_array_section("primary_temp");
3413 for (const auto &pg : *primary_temp) {
3414 f->dump_stream("pgid") << pg.first;
3415 f->dump_int("osd", pg.second);
3416 }
3417 f->close_section(); // primary_temp
3418
3419 f->open_object_section("blacklist");
3420 for (const auto &addr : blacklist) {
3421 stringstream ss;
3422 ss << addr.first;
3423 f->dump_stream(ss.str().c_str()) << addr.second;
3424 }
3425 f->close_section();
3426
3427 dump_erasure_code_profiles(erasure_code_profiles, f);
11fdf7f2
TL
3428
3429 f->open_array_section("removed_snaps_queue");
3430 for (auto& p : removed_snaps_queue) {
3431 f->open_object_section("pool");
3432 f->dump_int("pool", p.first);
3433 f->open_array_section("snaps");
3434 for (auto q = p.second.begin(); q != p.second.end(); ++q) {
3435 f->open_object_section("interval");
3436 f->dump_unsigned("begin", q.get_start());
3437 f->dump_unsigned("length", q.get_len());
3438 f->close_section();
3439 }
3440 f->close_section();
3441 f->close_section();
3442 }
3443 f->close_section();
3444 f->open_array_section("new_removed_snaps");
3445 for (auto& p : new_removed_snaps) {
3446 f->open_object_section("pool");
3447 f->dump_int("pool", p.first);
3448 f->open_array_section("snaps");
3449 for (auto q = p.second.begin(); q != p.second.end(); ++q) {
3450 f->open_object_section("interval");
3451 f->dump_unsigned("begin", q.get_start());
3452 f->dump_unsigned("length", q.get_len());
3453 f->close_section();
3454 }
3455 f->close_section();
3456 f->close_section();
3457 }
3458 f->close_section();
3459 f->open_array_section("new_purged_snaps");
3460 for (auto& p : new_purged_snaps) {
3461 f->open_object_section("pool");
3462 f->dump_int("pool", p.first);
3463 f->open_array_section("snaps");
3464 for (auto q = p.second.begin(); q != p.second.end(); ++q) {
3465 f->open_object_section("interval");
3466 f->dump_unsigned("begin", q.get_start());
3467 f->dump_unsigned("length", q.get_len());
3468 f->close_section();
3469 }
3470 f->close_section();
3471 f->close_section();
3472 }
3473 f->close_section();
81eedcae
TL
3474 f->open_object_section("crush_node_flags");
3475 for (auto& i : crush_node_flags) {
3476 string s = crush->item_exists(i.first) ? crush->get_item_name(i.first)
3477 : stringify(i.first);
3478 f->open_array_section(s.c_str());
3479 set<string> st;
3480 calc_state_set(i.second, st);
3481 for (auto& j : st) {
3482 f->dump_string("flag", j);
3483 }
3484 f->close_section();
3485 }
3486 f->close_section();
3487 f->open_object_section("device_class_flags");
3488 for (auto& i : device_class_flags) {
3489 const char* class_name = crush->get_class_name(i.first);
3490 string s = class_name ? class_name : stringify(i.first);
3491 f->open_array_section(s.c_str());
3492 set<string> st;
3493 calc_state_set(i.second, st);
3494 for (auto& j : st) {
3495 f->dump_string("flag", j);
3496 }
3497 f->close_section();
3498 }
3499 f->close_section();
7c673cae
FG
3500}
3501
3502void OSDMap::generate_test_instances(list<OSDMap*>& o)
3503{
3504 o.push_back(new OSDMap);
3505
3506 CephContext *cct = new CephContext(CODE_ENVIRONMENT_UTILITY);
3507 o.push_back(new OSDMap);
3508 uuid_d fsid;
224ce89b 3509 o.back()->build_simple(cct, 1, fsid, 16);
7c673cae
FG
3510 o.back()->created = o.back()->modified = utime_t(1, 2); // fix timestamp
3511 o.back()->blacklist[entity_addr_t()] = utime_t(5, 6);
3512 cct->put();
3513}
3514
3515string OSDMap::get_flag_string(unsigned f)
3516{
3517 string s;
3518 if ( f& CEPH_OSDMAP_NEARFULL)
3519 s += ",nearfull";
3520 if (f & CEPH_OSDMAP_FULL)
3521 s += ",full";
3522 if (f & CEPH_OSDMAP_PAUSERD)
3523 s += ",pauserd";
3524 if (f & CEPH_OSDMAP_PAUSEWR)
3525 s += ",pausewr";
3526 if (f & CEPH_OSDMAP_PAUSEREC)
3527 s += ",pauserec";
3528 if (f & CEPH_OSDMAP_NOUP)
3529 s += ",noup";
3530 if (f & CEPH_OSDMAP_NODOWN)
3531 s += ",nodown";
3532 if (f & CEPH_OSDMAP_NOOUT)
3533 s += ",noout";
3534 if (f & CEPH_OSDMAP_NOIN)
3535 s += ",noin";
3536 if (f & CEPH_OSDMAP_NOBACKFILL)
3537 s += ",nobackfill";
3538 if (f & CEPH_OSDMAP_NOREBALANCE)
3539 s += ",norebalance";
3540 if (f & CEPH_OSDMAP_NORECOVER)
3541 s += ",norecover";
3542 if (f & CEPH_OSDMAP_NOSCRUB)
3543 s += ",noscrub";
3544 if (f & CEPH_OSDMAP_NODEEP_SCRUB)
3545 s += ",nodeep-scrub";
3546 if (f & CEPH_OSDMAP_NOTIERAGENT)
3547 s += ",notieragent";
11fdf7f2
TL
3548 if (f & CEPH_OSDMAP_NOSNAPTRIM)
3549 s += ",nosnaptrim";
7c673cae
FG
3550 if (f & CEPH_OSDMAP_SORTBITWISE)
3551 s += ",sortbitwise";
3552 if (f & CEPH_OSDMAP_REQUIRE_JEWEL)
3553 s += ",require_jewel_osds";
3554 if (f & CEPH_OSDMAP_REQUIRE_KRAKEN)
3555 s += ",require_kraken_osds";
3556 if (f & CEPH_OSDMAP_REQUIRE_LUMINOUS)
3557 s += ",require_luminous_osds";
c07f9fc5
FG
3558 if (f & CEPH_OSDMAP_RECOVERY_DELETES)
3559 s += ",recovery_deletes";
181888fb
FG
3560 if (f & CEPH_OSDMAP_PURGED_SNAPDIRS)
3561 s += ",purged_snapdirs";
f64942e4
AA
3562 if (f & CEPH_OSDMAP_PGLOG_HARDLIMIT)
3563 s += ",pglog_hardlimit";
7c673cae
FG
3564 if (s.length())
3565 s.erase(0, 1);
3566 return s;
3567}
3568
3569string OSDMap::get_flag_string() const
3570{
3571 return get_flag_string(flags);
3572}
3573
7c673cae
FG
3574void OSDMap::print_pools(ostream& out) const
3575{
3576 for (const auto &pool : pools) {
3577 std::string name("<unknown>");
3578 const auto &pni = pool_name.find(pool.first);
3579 if (pni != pool_name.end())
3580 name = pni->second;
3581 out << "pool " << pool.first
3582 << " '" << name
3583 << "' " << pool.second << "\n";
3584
3585 for (const auto &snap : pool.second.snaps)
3586 out << "\tsnap " << snap.second.snapid << " '" << snap.second.name << "' " << snap.second.stamp << "\n";
3587
3588 if (!pool.second.removed_snaps.empty())
3589 out << "\tremoved_snaps " << pool.second.removed_snaps << "\n";
11fdf7f2
TL
3590 auto p = removed_snaps_queue.find(pool.first);
3591 if (p != removed_snaps_queue.end()) {
3592 out << "\tremoved_snaps_queue " << p->second << "\n";
3593 }
7c673cae
FG
3594 }
3595 out << std::endl;
3596}
3597
3598void OSDMap::print(ostream& out) const
3599{
3600 out << "epoch " << get_epoch() << "\n"
3601 << "fsid " << get_fsid() << "\n"
3602 << "created " << get_created() << "\n"
3603 << "modified " << get_modified() << "\n";
3604
3605 out << "flags " << get_flag_string() << "\n";
31f18b77 3606 out << "crush_version " << get_crush_version() << "\n";
7c673cae
FG
3607 out << "full_ratio " << full_ratio << "\n";
3608 out << "backfillfull_ratio " << backfillfull_ratio << "\n";
3609 out << "nearfull_ratio " << nearfull_ratio << "\n";
31f18b77
FG
3610 if (require_min_compat_client > 0) {
3611 out << "require_min_compat_client "
3612 << ceph_release_name(require_min_compat_client) << "\n";
7c673cae 3613 }
31f18b77
FG
3614 out << "min_compat_client " << ceph_release_name(get_min_compat_client())
3615 << "\n";
224ce89b
WB
3616 if (require_osd_release > 0) {
3617 out << "require_osd_release " << ceph_release_name(require_osd_release)
3618 << "\n";
3619 }
7c673cae
FG
3620 if (get_cluster_snapshot().length())
3621 out << "cluster_snapshot " << get_cluster_snapshot() << "\n";
3622 out << "\n";
3623
3624 print_pools(out);
3625
3626 out << "max_osd " << get_max_osd() << "\n";
3627 for (int i=0; i<get_max_osd(); i++) {
3628 if (exists(i)) {
3629 out << "osd." << i;
3630 out << (is_up(i) ? " up ":" down");
3631 out << (is_in(i) ? " in ":" out");
3632 out << " weight " << get_weightf(i);
3633 if (get_primary_affinity(i) != CEPH_OSD_DEFAULT_PRIMARY_AFFINITY)
3634 out << " primary_affinity " << get_primary_affinityf(i);
3635 const osd_info_t& info(get_info(i));
3636 out << " " << info;
11fdf7f2 3637 out << " " << get_addrs(i) << " " << get_cluster_addrs(i);
7c673cae
FG
3638 set<string> st;
3639 get_state(i, st);
3640 out << " " << st;
3641 if (!get_uuid(i).is_zero())
3642 out << " " << get_uuid(i);
3643 out << "\n";
3644 }
3645 }
3646 out << std::endl;
3647
3648 for (auto& p : pg_upmap) {
3649 out << "pg_upmap " << p.first << " " << p.second << "\n";
3650 }
3651 for (auto& p : pg_upmap_items) {
3652 out << "pg_upmap_items " << p.first << " " << p.second << "\n";
3653 }
3654
3655 for (const auto pg : *pg_temp)
3656 out << "pg_temp " << pg.first << " " << pg.second << "\n";
3657
3658 for (const auto pg : *primary_temp)
3659 out << "primary_temp " << pg.first << " " << pg.second << "\n";
3660
3661 for (const auto &addr : blacklist)
3662 out << "blacklist " << addr.first << " expires " << addr.second << "\n";
7c673cae
FG
3663}
3664
3665class OSDTreePlainDumper : public CrushTreeDumper::Dumper<TextTable> {
3666public:
3667 typedef CrushTreeDumper::Dumper<TextTable> Parent;
31f18b77
FG
3668
3669 OSDTreePlainDumper(const CrushWrapper *crush, const OSDMap *osdmap_,
3670 unsigned f)
c07f9fc5 3671 : Parent(crush, osdmap_->get_pool_names()), osdmap(osdmap_), filter(f) { }
31f18b77
FG
3672
3673 bool should_dump_leaf(int i) const override {
c07f9fc5
FG
3674 if (!filter) {
3675 return true; // normal case
3676 }
3677 if (((filter & OSDMap::DUMP_UP) && osdmap->is_up(i)) ||
3678 ((filter & OSDMap::DUMP_DOWN) && osdmap->is_down(i)) ||
3679 ((filter & OSDMap::DUMP_IN) && osdmap->is_in(i)) ||
3680 ((filter & OSDMap::DUMP_OUT) && osdmap->is_out(i)) ||
3681 ((filter & OSDMap::DUMP_DESTROYED) && osdmap->is_destroyed(i))) {
3682 return true;
31f18b77 3683 }
c07f9fc5 3684 return false;
31f18b77
FG
3685 }
3686
3687 bool should_dump_empty_bucket() const override {
3688 return !filter;
3689 }
7c673cae 3690
11fdf7f2 3691 void init_table(TextTable *tbl) {
7c673cae 3692 tbl->define_column("ID", TextTable::LEFT, TextTable::RIGHT);
224ce89b 3693 tbl->define_column("CLASS", TextTable::LEFT, TextTable::RIGHT);
7c673cae
FG
3694 tbl->define_column("WEIGHT", TextTable::LEFT, TextTable::RIGHT);
3695 tbl->define_column("TYPE NAME", TextTable::LEFT, TextTable::LEFT);
c07f9fc5 3696 tbl->define_column("STATUS", TextTable::LEFT, TextTable::RIGHT);
7c673cae 3697 tbl->define_column("REWEIGHT", TextTable::LEFT, TextTable::RIGHT);
224ce89b 3698 tbl->define_column("PRI-AFF", TextTable::LEFT, TextTable::RIGHT);
11fdf7f2
TL
3699 }
3700 void dump(TextTable *tbl, string& bucket) {
3701 init_table(tbl);
7c673cae 3702
11fdf7f2
TL
3703 if (!bucket.empty()) {
3704 set_root(bucket);
3705 Parent::dump(tbl);
3706 } else {
3707 Parent::dump(tbl);
3708 for (int i = 0; i < osdmap->get_max_osd(); i++) {
3709 if (osdmap->exists(i) && !is_touched(i) && should_dump_leaf(i)) {
3710 dump_item(CrushTreeDumper::Item(i, 0, 0, 0), tbl);
3711 }
31f18b77 3712 }
7c673cae
FG
3713 }
3714 }
3715
3716protected:
3717 void dump_item(const CrushTreeDumper::Item &qi, TextTable *tbl) override {
224ce89b
WB
3718 const char *c = crush->get_item_class(qi.id);
3719 if (!c)
3720 c = "";
7c673cae 3721 *tbl << qi.id
224ce89b 3722 << c
7c673cae
FG
3723 << weightf_t(qi.weight);
3724
3725 ostringstream name;
3726 for (int k = 0; k < qi.depth; k++)
3727 name << " ";
3728 if (qi.is_bucket()) {
3729 name << crush->get_type_name(crush->get_bucket_type(qi.id)) << " "
3730 << crush->get_item_name(qi.id);
3731 } else {
3732 name << "osd." << qi.id;
3733 }
3734 *tbl << name.str();
3735
3736 if (!qi.is_bucket()) {
3737 if (!osdmap->exists(qi.id)) {
3738 *tbl << "DNE"
3739 << 0;
3740 } else {
c07f9fc5
FG
3741 string s;
3742 if (osdmap->is_up(qi.id)) {
3743 s = "up";
3744 } else if (osdmap->is_destroyed(qi.id)) {
3745 s = "destroyed";
3746 } else {
3747 s = "down";
3748 }
3749 *tbl << s
7c673cae
FG
3750 << weightf_t(osdmap->get_weightf(qi.id))
3751 << weightf_t(osdmap->get_primary_affinityf(qi.id));
3752 }
3753 }
3754 *tbl << TextTable::endrow;
3755 }
3756
3757private:
3758 const OSDMap *osdmap;
31f18b77 3759 const unsigned filter;
7c673cae
FG
3760};
3761
3762class OSDTreeFormattingDumper : public CrushTreeDumper::FormattingDumper {
3763public:
3764 typedef CrushTreeDumper::FormattingDumper Parent;
3765
31f18b77
FG
3766 OSDTreeFormattingDumper(const CrushWrapper *crush, const OSDMap *osdmap_,
3767 unsigned f)
c07f9fc5 3768 : Parent(crush, osdmap_->get_pool_names()), osdmap(osdmap_), filter(f) { }
31f18b77
FG
3769
3770 bool should_dump_leaf(int i) const override {
c07f9fc5
FG
3771 if (!filter) {
3772 return true; // normal case
3773 }
3774 if (((filter & OSDMap::DUMP_UP) && osdmap->is_up(i)) ||
3775 ((filter & OSDMap::DUMP_DOWN) && osdmap->is_down(i)) ||
3776 ((filter & OSDMap::DUMP_IN) && osdmap->is_in(i)) ||
3777 ((filter & OSDMap::DUMP_OUT) && osdmap->is_out(i)) ||
3778 ((filter & OSDMap::DUMP_DESTROYED) && osdmap->is_destroyed(i))) {
3779 return true;
31f18b77 3780 }
c07f9fc5 3781 return false;
31f18b77
FG
3782 }
3783
3784 bool should_dump_empty_bucket() const override {
3785 return !filter;
3786 }
7c673cae 3787
11fdf7f2
TL
3788 void dump(Formatter *f, string& bucket) {
3789 if (!bucket.empty()) {
3790 set_root(bucket);
3791 f->open_array_section("nodes");
3792 Parent::dump(f);
3793 f->close_section();
3794 } else {
3795 f->open_array_section("nodes");
3796 Parent::dump(f);
3797 f->close_section();
3798 f->open_array_section("stray");
3799 for (int i = 0; i < osdmap->get_max_osd(); i++) {
3800 if (osdmap->exists(i) && !is_touched(i) && should_dump_leaf(i))
3801 dump_item(CrushTreeDumper::Item(i, 0, 0, 0), f);
3802 }
3803 f->close_section();
7c673cae 3804 }
7c673cae
FG
3805 }
3806
3807protected:
3808 void dump_item_fields(const CrushTreeDumper::Item &qi, Formatter *f) override {
3809 Parent::dump_item_fields(qi, f);
3810 if (!qi.is_bucket())
3811 {
c07f9fc5
FG
3812 string s;
3813 if (osdmap->is_up(qi.id)) {
3814 s = "up";
3815 } else if (osdmap->is_destroyed(qi.id)) {
3816 s = "destroyed";
3817 } else {
3818 s = "down";
3819 }
7c673cae 3820 f->dump_unsigned("exists", (int)osdmap->exists(qi.id));
c07f9fc5 3821 f->dump_string("status", s);
7c673cae
FG
3822 f->dump_float("reweight", osdmap->get_weightf(qi.id));
3823 f->dump_float("primary_affinity", osdmap->get_primary_affinityf(qi.id));
3824 }
3825 }
3826
3827private:
3828 const OSDMap *osdmap;
31f18b77 3829 const unsigned filter;
7c673cae
FG
3830};
3831
11fdf7f2 3832void OSDMap::print_tree(Formatter *f, ostream *out, unsigned filter, string bucket) const
7c673cae 3833{
31f18b77 3834 if (f) {
11fdf7f2 3835 OSDTreeFormattingDumper(crush.get(), this, filter).dump(f, bucket);
31f18b77 3836 } else {
11fdf7f2 3837 ceph_assert(out);
7c673cae 3838 TextTable tbl;
11fdf7f2 3839 OSDTreePlainDumper(crush.get(), this, filter).dump(&tbl, bucket);
7c673cae
FG
3840 *out << tbl;
3841 }
3842}
3843
224ce89b 3844void OSDMap::print_summary(Formatter *f, ostream& out,
11fdf7f2 3845 const string& prefix, bool extra) const
7c673cae
FG
3846{
3847 if (f) {
3848 f->open_object_section("osdmap");
3849 f->dump_int("epoch", get_epoch());
3850 f->dump_int("num_osds", get_num_osds());
3851 f->dump_int("num_up_osds", get_num_up_osds());
3852 f->dump_int("num_in_osds", get_num_in_osds());
3853 f->dump_bool("full", test_flag(CEPH_OSDMAP_FULL) ? true : false);
3854 f->dump_bool("nearfull", test_flag(CEPH_OSDMAP_NEARFULL) ? true : false);
3855 f->dump_unsigned("num_remapped_pgs", get_num_pg_temp());
3856 f->close_section();
3857 } else {
11fdf7f2 3858 utime_t now = ceph_clock_now();
31f18b77 3859 out << get_num_osds() << " osds: "
11fdf7f2
TL
3860 << get_num_up_osds() << " up";
3861 if (last_up_change != utime_t()) {
3862 out << " (since " << utimespan_str(now - last_up_change) << ")";
3863 }
3864 out << ", " << get_num_in_osds() << " in";
3865 if (last_in_change != utime_t()) {
3866 out << " (since " << utimespan_str(now - last_in_change) << ")";
3867 }
3868 if (extra)
3869 out << "; epoch: e" << get_epoch();
7c673cae
FG
3870 if (get_num_pg_temp())
3871 out << "; " << get_num_pg_temp() << " remapped pgs";
3872 out << "\n";
3873 uint64_t important_flags = flags & ~CEPH_OSDMAP_SEMIHIDDEN_FLAGS;
3874 if (important_flags)
224ce89b 3875 out << prefix << "flags " << get_flag_string(important_flags) << "\n";
7c673cae
FG
3876 }
3877}
3878
3879void OSDMap::print_oneline_summary(ostream& out) const
3880{
3881 out << "e" << get_epoch() << ": "
31f18b77 3882 << get_num_osds() << " total, "
7c673cae
FG
3883 << get_num_up_osds() << " up, "
3884 << get_num_in_osds() << " in";
3885 if (test_flag(CEPH_OSDMAP_FULL))
11fdf7f2 3886 out << "; full flag set";
7c673cae 3887 else if (test_flag(CEPH_OSDMAP_NEARFULL))
11fdf7f2 3888 out << "; nearfull flag set";
7c673cae
FG
3889}
3890
3efd9988 3891bool OSDMap::crush_rule_in_use(int rule_id) const
7c673cae
FG
3892{
3893 for (const auto &pool : pools) {
3efd9988 3894 if (pool.second.crush_rule == rule_id)
7c673cae
FG
3895 return true;
3896 }
3897 return false;
3898}
3899
3efd9988
FG
3900int OSDMap::validate_crush_rules(CrushWrapper *newcrush,
3901 ostream *ss) const
3902{
3903 for (auto& i : pools) {
3904 auto& pool = i.second;
3905 int ruleno = pool.get_crush_rule();
3906 if (!newcrush->rule_exists(ruleno)) {
3907 *ss << "pool " << i.first << " references crush_rule " << ruleno
3908 << " but it is not present";
3909 return -EINVAL;
3910 }
3911 if (newcrush->get_rule_mask_ruleset(ruleno) != ruleno) {
3912 *ss << "rule " << ruleno << " mask ruleset does not match rule id";
3913 return -EINVAL;
3914 }
3915 if (newcrush->get_rule_mask_type(ruleno) != (int)pool.get_type()) {
3916 *ss << "pool " << i.first << " type does not match rule " << ruleno;
3917 return -EINVAL;
3918 }
11fdf7f2
TL
3919 int poolsize = pool.get_size();
3920 if (poolsize < newcrush->get_rule_mask_min_size(ruleno) ||
3921 poolsize > newcrush->get_rule_mask_max_size(ruleno)) {
3922 *ss << "pool " << i.first << " size " << poolsize << " does not"
3efd9988
FG
3923 << " fall within rule " << ruleno
3924 << " min_size " << newcrush->get_rule_mask_min_size(ruleno)
3925 << " and max_size " << newcrush->get_rule_mask_max_size(ruleno);
3926 return -EINVAL;
3927 }
3928 }
3929 return 0;
3930}
3931
224ce89b
WB
3932int OSDMap::build_simple_optioned(CephContext *cct, epoch_t e, uuid_d &fsid,
3933 int nosd, int pg_bits, int pgp_bits,
3934 bool default_pool)
7c673cae 3935{
224ce89b
WB
3936 ldout(cct, 10) << "build_simple on " << nosd
3937 << " osds" << dendl;
7c673cae
FG
3938 epoch = e;
3939 set_fsid(fsid);
3940 created = modified = ceph_clock_now();
3941
3942 if (nosd >= 0) {
3943 set_max_osd(nosd);
3944 } else {
3945 // count osds
3946 int maxosd = 0;
11fdf7f2 3947 const auto& conf = cct->_conf;
7c673cae 3948 vector<string> sections;
11fdf7f2 3949 conf.get_all_sections(sections);
7c673cae
FG
3950
3951 for (auto &section : sections) {
3952 if (section.find("osd.") != 0)
3953 continue;
3954
3955 const char *begin = section.c_str() + 4;
3956 char *end = (char*)begin;
3957 int o = strtol(begin, &end, 10);
3958 if (*end != '\0')
3959 continue;
3960
3961 if (o > cct->_conf->mon_max_osd) {
3962 lderr(cct) << "[osd." << o << "] in config has id > mon_max_osd " << cct->_conf->mon_max_osd << dendl;
3963 return -ERANGE;
3964 }
3965
3966 if (o > maxosd)
3967 maxosd = o;
3968 }
3969
3970 set_max_osd(maxosd + 1);
3971 }
3972
7c673cae
FG
3973
3974 stringstream ss;
3975 int r;
3976 if (nosd >= 0)
3977 r = build_simple_crush_map(cct, *crush, nosd, &ss);
3978 else
3979 r = build_simple_crush_map_from_conf(cct, *crush, &ss);
11fdf7f2 3980 ceph_assert(r == 0);
7c673cae
FG
3981
3982 int poolbase = get_max_osd() ? get_max_osd() : 1;
3983
d2e6a577 3984 const int default_replicated_rule = crush->get_osd_pool_default_crush_replicated_ruleset(cct);
11fdf7f2 3985 ceph_assert(default_replicated_rule >= 0);
7c673cae 3986
224ce89b
WB
3987 if (default_pool) {
3988 // pgp_num <= pg_num
3989 if (pgp_bits > pg_bits)
3990 pgp_bits = pg_bits;
3991
3992 vector<string> pool_names;
3993 pool_names.push_back("rbd");
3994 for (auto &plname : pool_names) {
3995 int64_t pool = ++pool_max;
3996 pools[pool].type = pg_pool_t::TYPE_REPLICATED;
3997 pools[pool].flags = cct->_conf->osd_pool_default_flags;
3998 if (cct->_conf->osd_pool_default_flag_hashpspool)
3999 pools[pool].set_flag(pg_pool_t::FLAG_HASHPSPOOL);
4000 if (cct->_conf->osd_pool_default_flag_nodelete)
4001 pools[pool].set_flag(pg_pool_t::FLAG_NODELETE);
4002 if (cct->_conf->osd_pool_default_flag_nopgchange)
4003 pools[pool].set_flag(pg_pool_t::FLAG_NOPGCHANGE);
4004 if (cct->_conf->osd_pool_default_flag_nosizechange)
4005 pools[pool].set_flag(pg_pool_t::FLAG_NOSIZECHANGE);
11fdf7f2
TL
4006 pools[pool].size = cct->_conf.get_val<uint64_t>("osd_pool_default_size");
4007 pools[pool].min_size = cct->_conf.get_osd_pool_default_min_size(
4008 pools[pool].size);
224ce89b
WB
4009 pools[pool].crush_rule = default_replicated_rule;
4010 pools[pool].object_hash = CEPH_STR_HASH_RJENKINS;
4011 pools[pool].set_pg_num(poolbase << pg_bits);
4012 pools[pool].set_pgp_num(poolbase << pgp_bits);
11fdf7f2
TL
4013 pools[pool].set_pg_num_target(poolbase << pg_bits);
4014 pools[pool].set_pgp_num_target(poolbase << pgp_bits);
224ce89b 4015 pools[pool].last_change = epoch;
c07f9fc5
FG
4016 pools[pool].application_metadata.insert(
4017 {pg_pool_t::APPLICATION_NAME_RBD, {}});
11fdf7f2
TL
4018 auto m = pg_pool_t::get_pg_autoscale_mode_by_name(
4019 cct->_conf.get_val<string>("osd_pool_default_pg_autoscale_mode"));
4020 pools[pool].pg_autoscale_mode = m >= 0 ? m : 0;
224ce89b
WB
4021 pool_name[pool] = plname;
4022 name_pool[plname] = pool;
4023 }
7c673cae
FG
4024 }
4025
4026 for (int i=0; i<get_max_osd(); i++) {
4027 set_state(i, 0);
4028 set_weight(i, CEPH_OSD_OUT);
4029 }
4030
4031 map<string,string> profile_map;
4032 r = get_erasure_code_profile_default(cct, profile_map, &ss);
4033 if (r < 0) {
4034 lderr(cct) << ss.str() << dendl;
4035 return r;
4036 }
4037 set_erasure_code_profile("default", profile_map);
4038 return 0;
4039}
4040
4041int OSDMap::get_erasure_code_profile_default(CephContext *cct,
4042 map<string,string> &profile_map,
4043 ostream *ss)
4044{
11fdf7f2 4045 int r = get_json_str_map(cct->_conf.get_val<string>("osd_pool_default_erasure_code_profile"),
7c673cae
FG
4046 *ss,
4047 &profile_map);
4048 return r;
4049}
4050
4051int OSDMap::_build_crush_types(CrushWrapper& crush)
4052{
4053 crush.set_type_name(0, "osd");
4054 crush.set_type_name(1, "host");
4055 crush.set_type_name(2, "chassis");
4056 crush.set_type_name(3, "rack");
4057 crush.set_type_name(4, "row");
4058 crush.set_type_name(5, "pdu");
4059 crush.set_type_name(6, "pod");
4060 crush.set_type_name(7, "room");
4061 crush.set_type_name(8, "datacenter");
11fdf7f2
TL
4062 crush.set_type_name(9, "zone");
4063 crush.set_type_name(10, "region");
4064 crush.set_type_name(11, "root");
4065 return 11;
7c673cae
FG
4066}
4067
4068int OSDMap::build_simple_crush_map(CephContext *cct, CrushWrapper& crush,
4069 int nosd, ostream *ss)
4070{
4071 crush.create();
4072
4073 // root
4074 int root_type = _build_crush_types(crush);
4075 int rootid;
4076 int r = crush.add_bucket(0, 0, CRUSH_HASH_DEFAULT,
4077 root_type, 0, NULL, NULL, &rootid);
11fdf7f2 4078 ceph_assert(r == 0);
7c673cae
FG
4079 crush.set_item_name(rootid, "default");
4080
4081 for (int o=0; o<nosd; o++) {
4082 map<string,string> loc;
4083 loc["host"] = "localhost";
4084 loc["rack"] = "localrack";
4085 loc["root"] = "default";
4086 ldout(cct, 10) << " adding osd." << o << " at " << loc << dendl;
4087 char name[32];
4088 snprintf(name, sizeof(name), "osd.%d", o);
4089 crush.insert_item(cct, o, 1.0, name, loc);
4090 }
4091
31f18b77 4092 build_simple_crush_rules(cct, crush, "default", ss);
7c673cae
FG
4093
4094 crush.finalize();
4095
4096 return 0;
4097}
4098
4099int OSDMap::build_simple_crush_map_from_conf(CephContext *cct,
4100 CrushWrapper& crush,
4101 ostream *ss)
4102{
11fdf7f2 4103 const auto& conf = cct->_conf;
7c673cae
FG
4104
4105 crush.create();
4106
4107 // root
4108 int root_type = _build_crush_types(crush);
4109 int rootid;
4110 int r = crush.add_bucket(0, 0,
4111 CRUSH_HASH_DEFAULT,
4112 root_type, 0, NULL, NULL, &rootid);
11fdf7f2 4113 ceph_assert(r == 0);
7c673cae
FG
4114 crush.set_item_name(rootid, "default");
4115
4116 // add osds
4117 vector<string> sections;
11fdf7f2 4118 conf.get_all_sections(sections);
7c673cae
FG
4119
4120 for (auto &section : sections) {
4121 if (section.find("osd.") != 0)
4122 continue;
4123
4124 const char *begin = section.c_str() + 4;
4125 char *end = (char*)begin;
4126 int o = strtol(begin, &end, 10);
4127 if (*end != '\0')
4128 continue;
4129
4130 string host, rack, row, room, dc, pool;
4131 vector<string> sectiontmp;
4132 sectiontmp.push_back("osd");
4133 sectiontmp.push_back(section);
11fdf7f2
TL
4134 conf.get_val_from_conf_file(sectiontmp, "host", host, false);
4135 conf.get_val_from_conf_file(sectiontmp, "rack", rack, false);
4136 conf.get_val_from_conf_file(sectiontmp, "row", row, false);
4137 conf.get_val_from_conf_file(sectiontmp, "room", room, false);
4138 conf.get_val_from_conf_file(sectiontmp, "datacenter", dc, false);
4139 conf.get_val_from_conf_file(sectiontmp, "root", pool, false);
7c673cae
FG
4140
4141 if (host.length() == 0)
4142 host = "unknownhost";
4143 if (rack.length() == 0)
4144 rack = "unknownrack";
4145
4146 map<string,string> loc;
4147 loc["host"] = host;
4148 loc["rack"] = rack;
4149 if (row.size())
4150 loc["row"] = row;
4151 if (room.size())
4152 loc["room"] = room;
4153 if (dc.size())
4154 loc["datacenter"] = dc;
4155 loc["root"] = "default";
4156
4157 ldout(cct, 5) << " adding osd." << o << " at " << loc << dendl;
4158 crush.insert_item(cct, o, 1.0, section, loc);
4159 }
4160
31f18b77 4161 build_simple_crush_rules(cct, crush, "default", ss);
7c673cae
FG
4162
4163 crush.finalize();
4164
4165 return 0;
4166}
4167
4168
31f18b77
FG
4169int OSDMap::build_simple_crush_rules(
4170 CephContext *cct,
4171 CrushWrapper& crush,
4172 const string& root,
4173 ostream *ss)
7c673cae 4174{
31f18b77 4175 int crush_rule = crush.get_osd_pool_default_crush_replicated_ruleset(cct);
7c673cae
FG
4176 string failure_domain =
4177 crush.get_type_name(cct->_conf->osd_crush_chooseleaf_type);
4178
7c673cae 4179 int r;
31f18b77 4180 r = crush.add_simple_rule_at(
224ce89b 4181 "replicated_rule", root, failure_domain, "",
31f18b77
FG
4182 "firstn", pg_pool_t::TYPE_REPLICATED,
4183 crush_rule, ss);
7c673cae
FG
4184 if (r < 0)
4185 return r;
4186 // do not add an erasure rule by default or else we will implicitly
4187 // require the crush_v2 feature of clients
4188 return 0;
4189}
4190
4191int OSDMap::summarize_mapping_stats(
4192 OSDMap *newmap,
4193 const set<int64_t> *pools,
4194 std::string *out,
4195 Formatter *f) const
4196{
4197 set<int64_t> ls;
4198 if (pools) {
4199 ls = *pools;
4200 } else {
4201 for (auto &p : get_pools())
4202 ls.insert(p.first);
4203 }
4204
4205 unsigned total_pg = 0;
4206 unsigned moved_pg = 0;
4207 vector<unsigned> base_by_osd(get_max_osd(), 0);
4208 vector<unsigned> new_by_osd(get_max_osd(), 0);
4209 for (int64_t pool_id : ls) {
4210 const pg_pool_t *pi = get_pg_pool(pool_id);
31f18b77
FG
4211 vector<int> up, up2;
4212 int up_primary;
7c673cae 4213 for (unsigned ps = 0; ps < pi->get_pg_num(); ++ps) {
11fdf7f2 4214 pg_t pgid(ps, pool_id);
7c673cae 4215 total_pg += pi->get_size();
31f18b77 4216 pg_to_up_acting_osds(pgid, &up, &up_primary, nullptr, nullptr);
7c673cae
FG
4217 for (int osd : up) {
4218 if (osd >= 0 && osd < get_max_osd())
4219 ++base_by_osd[osd];
4220 }
4221 if (newmap) {
31f18b77 4222 newmap->pg_to_up_acting_osds(pgid, &up2, &up_primary, nullptr, nullptr);
7c673cae
FG
4223 for (int osd : up2) {
4224 if (osd >= 0 && osd < get_max_osd())
4225 ++new_by_osd[osd];
4226 }
4227 if (pi->type == pg_pool_t::TYPE_ERASURE) {
4228 for (unsigned i=0; i<up.size(); ++i) {
4229 if (up[i] != up2[i]) {
4230 ++moved_pg;
4231 }
4232 }
4233 } else if (pi->type == pg_pool_t::TYPE_REPLICATED) {
4234 for (int osd : up) {
4235 if (std::find(up2.begin(), up2.end(), osd) == up2.end()) {
4236 ++moved_pg;
4237 }
4238 }
4239 } else {
11fdf7f2 4240 ceph_abort_msg("unhandled pool type");
7c673cae
FG
4241 }
4242 }
4243 }
4244 }
4245
4246 unsigned num_up_in = 0;
4247 for (int osd = 0; osd < get_max_osd(); ++osd) {
4248 if (is_up(osd) && is_in(osd))
4249 ++num_up_in;
4250 }
4251 if (!num_up_in) {
4252 return -EINVAL;
4253 }
4254
4255 float avg_pg = (float)total_pg / (float)num_up_in;
4256 float base_stddev = 0, new_stddev = 0;
4257 int min = -1, max = -1;
4258 unsigned min_base_pg = 0, max_base_pg = 0;
4259 unsigned min_new_pg = 0, max_new_pg = 0;
4260 for (int osd = 0; osd < get_max_osd(); ++osd) {
4261 if (is_up(osd) && is_in(osd)) {
4262 float base_diff = (float)base_by_osd[osd] - avg_pg;
4263 base_stddev += base_diff * base_diff;
4264 float new_diff = (float)new_by_osd[osd] - avg_pg;
4265 new_stddev += new_diff * new_diff;
4266 if (min < 0 || base_by_osd[osd] < min_base_pg) {
4267 min = osd;
4268 min_base_pg = base_by_osd[osd];
4269 min_new_pg = new_by_osd[osd];
4270 }
4271 if (max < 0 || base_by_osd[osd] > max_base_pg) {
4272 max = osd;
4273 max_base_pg = base_by_osd[osd];
4274 max_new_pg = new_by_osd[osd];
4275 }
4276 }
4277 }
4278 base_stddev = sqrt(base_stddev / num_up_in);
4279 new_stddev = sqrt(new_stddev / num_up_in);
4280
4281 float edev = sqrt(avg_pg * (1.0 - (1.0 / (double)num_up_in)));
4282
4283 ostringstream ss;
4284 if (f)
4285 f->open_object_section("utilization");
4286 if (newmap) {
4287 if (f) {
4288 f->dump_unsigned("moved_pgs", moved_pg);
4289 f->dump_unsigned("total_pgs", total_pg);
4290 } else {
4291 float percent = 0;
4292 if (total_pg)
4293 percent = (float)moved_pg * 100.0 / (float)total_pg;
4294 ss << "moved " << moved_pg << " / " << total_pg
4295 << " (" << percent << "%)\n";
4296 }
4297 }
4298 if (f) {
4299 f->dump_float("avg_pgs", avg_pg);
4300 f->dump_float("std_dev", base_stddev);
4301 f->dump_float("expected_baseline_std_dev", edev);
4302 if (newmap)
4303 f->dump_float("new_std_dev", new_stddev);
4304 } else {
4305 ss << "avg " << avg_pg << "\n";
4306 ss << "stddev " << base_stddev;
4307 if (newmap)
4308 ss << " -> " << new_stddev;
4309 ss << " (expected baseline " << edev << ")\n";
4310 }
4311 if (min >= 0) {
4312 if (f) {
4313 f->dump_unsigned("min_osd", min);
4314 f->dump_unsigned("min_osd_pgs", min_base_pg);
4315 if (newmap)
4316 f->dump_unsigned("new_min_osd_pgs", min_new_pg);
4317 } else {
4318 ss << "min osd." << min << " with " << min_base_pg;
4319 if (newmap)
4320 ss << " -> " << min_new_pg;
4321 ss << " pgs (" << (float)min_base_pg / avg_pg;
4322 if (newmap)
4323 ss << " -> " << (float)min_new_pg / avg_pg;
4324 ss << " * mean)\n";
4325 }
4326 }
4327 if (max >= 0) {
4328 if (f) {
4329 f->dump_unsigned("max_osd", max);
4330 f->dump_unsigned("max_osd_pgs", max_base_pg);
4331 if (newmap)
4332 f->dump_unsigned("new_max_osd_pgs", max_new_pg);
4333 } else {
4334 ss << "max osd." << max << " with " << max_base_pg;
4335 if (newmap)
4336 ss << " -> " << max_new_pg;
4337 ss << " pgs (" << (float)max_base_pg / avg_pg;
4338 if (newmap)
4339 ss << " -> " << (float)max_new_pg / avg_pg;
4340 ss << " * mean)\n";
4341 }
4342 }
4343 if (f)
4344 f->close_section();
4345 if (out)
4346 *out = ss.str();
4347 return 0;
4348}
4349
4350
4351int OSDMap::clean_pg_upmaps(
4352 CephContext *cct,
f64942e4 4353 Incremental *pending_inc) const
7c673cae
FG
4354{
4355 ldout(cct, 10) << __func__ << dendl;
4356 int changed = 0;
4357 for (auto& p : pg_upmap) {
4358 vector<int> raw;
4359 int primary;
4360 pg_to_raw_osds(p.first, &raw, &primary);
11fdf7f2 4361 if (raw == p.second) {
7c673cae
FG
4362 ldout(cct, 10) << " removing redundant pg_upmap " << p.first << " "
4363 << p.second << dendl;
4364 pending_inc->old_pg_upmap.insert(p.first);
4365 ++changed;
4366 }
4367 }
4368 for (auto& p : pg_upmap_items) {
4369 vector<int> raw;
4370 int primary;
4371 pg_to_raw_osds(p.first, &raw, &primary);
4372 mempool::osdmap::vector<pair<int,int>> newmap;
4373 for (auto& q : p.second) {
f64942e4
AA
4374 if (std::find(raw.begin(), raw.end(), q.first) == raw.end()) {
4375 // cancel mapping if source osd does not exist anymore
4376 continue;
4377 }
4378 if (q.second != CRUSH_ITEM_NONE && q.second < max_osd &&
4379 q.second >= 0 && osd_weight[q.second] == 0) {
4380 // cancel mapping if target osd is out
4381 continue;
7c673cae 4382 }
f64942e4 4383 newmap.push_back(q);
7c673cae
FG
4384 }
4385 if (newmap.empty()) {
4386 ldout(cct, 10) << " removing no-op pg_upmap_items " << p.first << " "
4387 << p.second << dendl;
4388 pending_inc->old_pg_upmap_items.insert(p.first);
4389 ++changed;
4390 } else if (newmap != p.second) {
4391 ldout(cct, 10) << " simplifying partially no-op pg_upmap_items "
4392 << p.first << " " << p.second << " -> " << newmap << dendl;
4393 pending_inc->new_pg_upmap_items[p.first] = newmap;
4394 ++changed;
4395 }
4396 }
4397 return changed;
4398}
4399
4400bool OSDMap::try_pg_upmap(
4401 CephContext *cct,
4402 pg_t pg, ///< pg to potentially remap
4403 const set<int>& overfull, ///< osds we'd want to evacuate
4404 const vector<int>& underfull, ///< osds to move to, in order of preference
4405 vector<int> *orig,
4406 vector<int> *out) ///< resulting alternative mapping
4407{
4408 const pg_pool_t *pool = get_pg_pool(pg.pool());
4409 if (!pool)
4410 return false;
31f18b77 4411 int rule = crush->find_rule(pool->get_crush_rule(), pool->get_type(),
7c673cae
FG
4412 pool->get_size());
4413 if (rule < 0)
4414 return false;
4415
7c673cae
FG
4416 // make sure there is something there to remap
4417 bool any = false;
4418 for (auto osd : *orig) {
4419 if (overfull.count(osd)) {
4420 any = true;
4421 break;
4422 }
4423 }
4424 if (!any) {
4425 return false;
4426 }
4427
4428 int r = crush->try_remap_rule(
4429 cct,
4430 rule,
4431 pool->get_size(),
4432 overfull, underfull,
4433 *orig,
4434 out);
4435 if (r < 0)
4436 return false;
4437 if (*out == *orig)
4438 return false;
4439 return true;
4440}
4441
4442int OSDMap::calc_pg_upmaps(
4443 CephContext *cct,
31f18b77 4444 float max_deviation_ratio,
7c673cae 4445 int max,
a8e16298 4446 const set<int64_t>& only_pools,
7c673cae
FG
4447 OSDMap::Incremental *pending_inc)
4448{
a8e16298 4449 ldout(cct, 10) << __func__ << " pools " << only_pools << dendl;
7c673cae
FG
4450 OSDMap tmp;
4451 tmp.deepish_copy_from(*this);
4452 int num_changed = 0;
a8e16298
TL
4453 map<int,set<pg_t>> pgs_by_osd;
4454 int total_pgs = 0;
4455 float osd_weight_total = 0;
4456 map<int,float> osd_weight;
4457 for (auto& i : pools) {
4458 if (!only_pools.empty() && !only_pools.count(i.first))
4459 continue;
4460 for (unsigned ps = 0; ps < i.second.get_pg_num(); ++ps) {
4461 pg_t pg(ps, i.first);
4462 vector<int> up;
4463 tmp.pg_to_up_acting_osds(pg, &up, nullptr, nullptr, nullptr);
4464 ldout(cct, 20) << __func__ << " " << pg << " up " << up << dendl;
4465 for (auto osd : up) {
4466 if (osd != CRUSH_ITEM_NONE)
4467 pgs_by_osd[osd].insert(pg);
7c673cae 4468 }
a8e16298
TL
4469 }
4470 total_pgs += i.second.get_size() * i.second.get_pg_num();
4471
4472 map<int,float> pmap;
4473 int ruleno = tmp.crush->find_rule(i.second.get_crush_rule(),
4474 i.second.get_type(),
4475 i.second.get_size());
4476 tmp.crush->get_rule_weight_osd_map(ruleno, &pmap);
4477 ldout(cct,20) << __func__ << " pool " << i.first
4478 << " ruleno " << ruleno
4479 << " weight-map " << pmap
4480 << dendl;
4481 for (auto p : pmap) {
4482 auto adjusted_weight = tmp.get_weightf(p.first) * p.second;
4483 if (adjusted_weight == 0) {
4484 continue;
31f18b77 4485 }
a8e16298
TL
4486 osd_weight[p.first] += adjusted_weight;
4487 osd_weight_total += adjusted_weight;
7c673cae 4488 }
a8e16298
TL
4489 }
4490 for (auto& i : osd_weight) {
4491 int pgs = 0;
4492 auto p = pgs_by_osd.find(i.first);
4493 if (p != pgs_by_osd.end())
31f18b77 4494 pgs = p->second.size();
a8e16298 4495 else
31f18b77 4496 pgs_by_osd.emplace(i.first, set<pg_t>());
a8e16298 4497 ldout(cct, 20) << " osd." << i.first << " weight " << i.second
31f18b77 4498 << " pgs " << pgs << dendl;
a8e16298
TL
4499 }
4500 if (osd_weight_total == 0) {
4501 lderr(cct) << __func__ << " abort due to osd_weight_total == 0" << dendl;
4502 return 0;
4503 }
4504 float pgs_per_weight = total_pgs / osd_weight_total;
4505 ldout(cct, 10) << " osd_weight_total " << osd_weight_total << dendl;
4506 ldout(cct, 10) << " pgs_per_weight " << pgs_per_weight << dendl;
7c673cae 4507
a8e16298
TL
4508 if (max <= 0) {
4509 lderr(cct) << __func__ << " abort due to max <= 0" << dendl;
4510 return 0;
4511 }
4512 float decay_factor = 1.0 / float(max);
4513 float stddev = 0;
4514 map<int,float> osd_deviation; // osd, deviation(pgs)
4515 multimap<float,int> deviation_osd; // deviation(pgs), osd
4516 for (auto& i : pgs_by_osd) {
4517 // make sure osd is still there (belongs to this crush-tree)
4518 ceph_assert(osd_weight.count(i.first));
4519 float target = osd_weight[i.first] * pgs_per_weight;
4520 float deviation = (float)i.second.size() - target;
4521 ldout(cct, 20) << " osd." << i.first
4522 << "\tpgs " << i.second.size()
4523 << "\ttarget " << target
4524 << "\tdeviation " << deviation
4525 << dendl;
4526 osd_deviation[i.first] = deviation;
4527 deviation_osd.insert(make_pair(deviation, i.first));
4528 stddev += deviation * deviation;
4529 }
11fdf7f2 4530 if (stddev <= cct->_conf.get_val<double>("osd_calc_pg_upmaps_max_stddev")) {
a8e16298
TL
4531 ldout(cct, 10) << __func__ << " distribution is almost perfect"
4532 << dendl;
4533 return 0;
4534 }
4535 bool skip_overfull = false;
4536 auto aggressive =
11fdf7f2 4537 cct->_conf.get_val<bool>("osd_calc_pg_upmaps_aggressively");
a8e16298 4538 auto local_fallback_retries =
11fdf7f2 4539 cct->_conf.get_val<uint64_t>("osd_calc_pg_upmaps_local_fallback_retries");
a8e16298
TL
4540 while (max--) {
4541 // build overfull and underfull
4542 set<int> overfull;
4543 vector<int> underfull;
4544 float decay = 0;
4545 int decay_count = 0;
4546 while (overfull.empty()) {
4547 for (auto i = deviation_osd.rbegin(); i != deviation_osd.rend(); i++) {
4548 if (i->first >= (1.0 - decay))
4549 overfull.insert(i->second);
4550 }
4551 if (!overfull.empty())
4552 break;
4553 decay_count++;
4554 decay = decay_factor * decay_count;
4555 if (decay >= 1.0)
4556 break;
4557 ldout(cct, 30) << " decay_factor = " << decay_factor
4558 << " decay_count = " << decay_count
4559 << " decay (overfull) = " << decay
4560 << dendl;
4561 }
4562 if (overfull.empty()) {
4563 lderr(cct) << __func__ << " failed to build overfull" << dendl;
224ce89b
WB
4564 break;
4565 }
7c673cae 4566
a8e16298
TL
4567 decay = 0;
4568 decay_count = 0;
4569 while (underfull.empty()) {
4570 for (auto i = deviation_osd.begin(); i != deviation_osd.end(); i++) {
4571 if (i->first >= (-.999 + decay))
4572 break;
4573 underfull.push_back(i->second);
4574 }
4575 if (!underfull.empty())
4576 break;
4577 decay_count++;
4578 decay = decay_factor * decay_count;
4579 if (decay >= .999)
4580 break;
4581 ldout(cct, 30) << " decay_factor = " << decay_factor
4582 << " decay_count = " << decay_count
4583 << " decay (underfull) = " << decay
4584 << dendl;
7c673cae 4585 }
a8e16298
TL
4586 if (underfull.empty()) {
4587 lderr(cct) << __func__ << " failed to build underfull" << dendl;
7c673cae 4588 break;
a8e16298 4589 }
7c673cae 4590
a8e16298
TL
4591 ldout(cct, 10) << " overfull " << overfull
4592 << " underfull " << underfull
4593 << dendl;
4594 set<pg_t> to_skip;
4595 uint64_t local_fallback_retried = 0;
4596
4597 retry:
4598
4599 set<pg_t> to_unmap;
4600 map<pg_t, mempool::osdmap::vector<pair<int32_t,int32_t>>> to_upmap;
4601 auto temp_pgs_by_osd = pgs_by_osd;
4602 // always start with fullest, break if we find any changes to make
7c673cae 4603 for (auto p = deviation_osd.rbegin(); p != deviation_osd.rend(); ++p) {
a8e16298
TL
4604 if (skip_overfull) {
4605 ldout(cct, 10) << " skipping overfull " << dendl;
4606 break; // fall through to check underfull
4607 }
7c673cae 4608 int osd = p->second;
31f18b77 4609 float deviation = p->first;
7c673cae 4610 float target = osd_weight[osd] * pgs_per_weight;
a8e16298
TL
4611 ceph_assert(target > 0);
4612 float deviation_ratio = deviation / target;
4613 if (deviation_ratio < max_deviation_ratio) {
7c673cae 4614 ldout(cct, 10) << " osd." << osd
a8e16298
TL
4615 << " target " << target
4616 << " deviation " << deviation
4617 << " -> ratio " << deviation_ratio
4618 << " < max ratio " << max_deviation_ratio
4619 << dendl;
7c673cae
FG
4620 break;
4621 }
7c673cae 4622
a8e16298
TL
4623 vector<pg_t> pgs;
4624 pgs.reserve(pgs_by_osd[osd].size());
4625 for (auto& pg : pgs_by_osd[osd]) {
4626 if (to_skip.count(pg))
4627 continue;
4628 pgs.push_back(pg);
4629 }
4630 if (aggressive) {
4631 // shuffle PG list so they all get equal (in)attention
4632 std::random_device rd;
4633 std::default_random_engine rng{rd()};
4634 std::shuffle(pgs.begin(), pgs.end(), rng);
4635 }
7c673cae
FG
4636 // look for remaps we can un-remap
4637 for (auto pg : pgs) {
4638 auto p = tmp.pg_upmap_items.find(pg);
a8e16298
TL
4639 if (p == tmp.pg_upmap_items.end())
4640 continue;
4641 mempool::osdmap::vector<pair<int32_t,int32_t>> new_upmap_items;
4642 for (auto q : p->second) {
4643 if (q.second == osd) {
4644 ldout(cct, 10) << " will try dropping existing"
4645 << " remapping pair "
4646 << q.first << " -> " << q.second
4647 << " which remapped " << pg
4648 << " into overfull osd." << osd
4649 << dendl;
4650 temp_pgs_by_osd[q.second].erase(pg);
4651 temp_pgs_by_osd[q.first].insert(pg);
4652 } else {
4653 new_upmap_items.push_back(q);
4654 }
4655 }
4656 if (new_upmap_items.empty()) {
4657 // drop whole item
4658 ldout(cct, 10) << " existing pg_upmap_items " << p->second
4659 << " remapped " << pg << " into overfull osd." << osd
4660 << ", will try cancelling it entirely"
4661 << dendl;
4662 to_unmap.insert(pg);
4663 goto test_change;
4664 } else if (new_upmap_items.size() != p->second.size()) {
4665 // drop single remapping pair, updating
4666 ceph_assert(new_upmap_items.size() < p->second.size());
4667 ldout(cct, 10) << " existing pg_upmap_items " << p->second
4668 << " remapped " << pg << " into overfull osd." << osd
4669 << ", new_pg_upmap_items now " << new_upmap_items
4670 << dendl;
4671 to_upmap[pg] = new_upmap_items;
4672 goto test_change;
4673 }
4674 }
7c673cae 4675
a8e16298 4676 // try upmap
7c673cae 4677 for (auto pg : pgs) {
a8e16298
TL
4678 auto temp_it = tmp.pg_upmap.find(pg);
4679 if (temp_it != tmp.pg_upmap.end()) {
4680 // leave pg_upmap alone
4681 // it must be specified by admin since balancer does not
4682 // support pg_upmap yet
4683 ldout(cct, 10) << " " << pg << " already has pg_upmap "
4684 << temp_it->second << ", skipping"
4685 << dendl;
7c673cae
FG
4686 continue;
4687 }
a8e16298
TL
4688 auto pg_pool_size = tmp.get_pg_pool_size(pg);
4689 mempool::osdmap::vector<pair<int32_t,int32_t>> new_upmap_items;
4690 set<int> existing;
4691 auto it = tmp.pg_upmap_items.find(pg);
4692 if (it != tmp.pg_upmap_items.end() &&
4693 it->second.size() >= (size_t)pg_pool_size) {
4694 ldout(cct, 10) << " " << pg << " already has full-size pg_upmap_items "
4695 << it->second << ", skipping"
4696 << dendl;
4697 continue;
4698 } else if (it != tmp.pg_upmap_items.end()) {
4699 ldout(cct, 10) << " " << pg << " already has pg_upmap_items "
4700 << it->second
4701 << dendl;
4702 new_upmap_items = it->second;
4703 // build existing too (for dedup)
4704 for (auto i : it->second) {
4705 existing.insert(i.first);
4706 existing.insert(i.second);
4707 }
4708 // fall through
4709 // to see if we can append more remapping pairs
4710 }
4711 ldout(cct, 10) << " trying " << pg << dendl;
7c673cae 4712 vector<int> orig, out;
a8e16298 4713 tmp.pg_to_raw_upmap(pg, &orig); // including existing upmaps too
7c673cae
FG
4714 if (!try_pg_upmap(cct, pg, overfull, underfull, &orig, &out)) {
4715 continue;
4716 }
a8e16298 4717 ldout(cct, 10) << " " << pg << " " << orig << " -> " << out << dendl;
7c673cae
FG
4718 if (orig.size() != out.size()) {
4719 continue;
4720 }
a8e16298 4721 ceph_assert(orig != out);
7c673cae 4722 for (unsigned i = 0; i < out.size(); ++i) {
a8e16298
TL
4723 if (orig[i] == out[i])
4724 continue; // skip invalid remappings
4725 if (existing.count(orig[i]) || existing.count(out[i]))
4726 continue; // we want new remappings only!
4727 ldout(cct, 10) << " will try adding new remapping pair "
4728 << orig[i] << " -> " << out[i] << " for " << pg
4729 << dendl;
4730 existing.insert(orig[i]);
4731 existing.insert(out[i]);
4732 temp_pgs_by_osd[orig[i]].erase(pg);
4733 temp_pgs_by_osd[out[i]].insert(pg);
4734 ceph_assert(new_upmap_items.size() < (size_t)pg_pool_size);
4735 new_upmap_items.push_back(make_pair(orig[i], out[i]));
4736 // append new remapping pairs slowly
4737 // This way we can make sure that each tiny change will
4738 // definitely make distribution of PGs converging to
4739 // the perfect status.
4740 to_upmap[pg] = new_upmap_items;
4741 goto test_change;
7c673cae 4742 }
a8e16298
TL
4743 }
4744 }
7c673cae 4745
a8e16298
TL
4746 ceph_assert(!(to_unmap.size() || to_upmap.size()));
4747 ldout(cct, 10) << " failed to find any changes for overfull osds"
4748 << dendl;
4749 for (auto& p : deviation_osd) {
4750 if (std::find(underfull.begin(), underfull.end(), p.second) ==
4751 underfull.end())
4752 break;
4753 int osd = p.second;
4754 float deviation = p.first;
4755 float target = osd_weight[osd] * pgs_per_weight;
4756 ceph_assert(target > 0);
4757 float deviation_ratio = abs(deviation / target);
4758 if (deviation_ratio < max_deviation_ratio) {
4759 // respect max_deviation_ratio too
4760 ldout(cct, 10) << " osd." << osd
4761 << " target " << target
4762 << " deviation " << deviation
4763 << " -> absolute ratio " << deviation_ratio
4764 << " < max ratio " << max_deviation_ratio
4765 << dendl;
4766 break;
4767 }
4768 // look for remaps we can un-remap
4769 vector<pair<pg_t,
4770 mempool::osdmap::vector<pair<int32_t,int32_t>>>> candidates;
4771 candidates.reserve(tmp.pg_upmap_items.size());
4772 for (auto& i : tmp.pg_upmap_items) {
4773 if (to_skip.count(i.first))
4774 continue;
4775 if (!only_pools.empty() && !only_pools.count(i.first.pool()))
4776 continue;
4777 candidates.push_back(make_pair(i.first, i.second));
4778 }
4779 if (aggressive) {
4780 // shuffle candidates so they all get equal (in)attention
4781 std::random_device rd;
4782 std::default_random_engine rng{rd()};
4783 std::shuffle(candidates.begin(), candidates.end(), rng);
4784 }
4785 for (auto& i : candidates) {
4786 auto pg = i.first;
4787 mempool::osdmap::vector<pair<int32_t,int32_t>> new_upmap_items;
4788 for (auto& j : i.second) {
4789 if (j.first == osd) {
4790 ldout(cct, 10) << " will try dropping existing"
4791 << " remapping pair "
4792 << j.first << " -> " << j.second
4793 << " which remapped " << pg
4794 << " out from underfull osd." << osd
4795 << dendl;
4796 temp_pgs_by_osd[j.second].erase(pg);
4797 temp_pgs_by_osd[j.first].insert(pg);
4798 } else {
4799 new_upmap_items.push_back(j);
4800 }
4801 }
4802 if (new_upmap_items.empty()) {
4803 // drop whole item
4804 ldout(cct, 10) << " existing pg_upmap_items " << i.second
4805 << " remapped " << pg
4806 << " out from underfull osd." << osd
4807 << ", will try cancelling it entirely"
4808 << dendl;
4809 to_unmap.insert(pg);
4810 goto test_change;
4811 } else if (new_upmap_items.size() != i.second.size()) {
4812 // drop single remapping pair, updating
4813 ceph_assert(new_upmap_items.size() < i.second.size());
4814 ldout(cct, 10) << " existing pg_upmap_items " << i.second
4815 << " remapped " << pg
4816 << " out from underfull osd." << osd
4817 << ", new_pg_upmap_items now " << new_upmap_items
4818 << dendl;
4819 to_upmap[pg] = new_upmap_items;
4820 goto test_change;
4821 }
4822 }
7c673cae 4823 }
a8e16298
TL
4824
4825 ceph_assert(!(to_unmap.size() || to_upmap.size()));
4826 ldout(cct, 10) << " failed to find any changes for underfull osds"
4827 << dendl;
4828 if (!aggressive) {
4829 ldout(cct, 10) << " break due to aggressive mode not enabled" << dendl;
4830 break;
4831 } else if (!skip_overfull) {
4832 // safe to quit because below here we know
4833 // we've done checking both overfull and underfull osds..
4834 ldout(cct, 10) << " break due to not being able to find any"
4835 << " further optimizations"
4836 << dendl;
7c673cae
FG
4837 break;
4838 }
a8e16298
TL
4839 // restart with fullest and do exhaustive searching
4840 skip_overfull = false;
4841 continue;
4842
4843 test_change:
4844
4845 // test change, apply if change is good
4846 ceph_assert(to_unmap.size() || to_upmap.size());
4847 float new_stddev = 0;
4848 map<int,float> temp_osd_deviation;
4849 multimap<float,int> temp_deviation_osd;
4850 for (auto& i : temp_pgs_by_osd) {
4851 // make sure osd is still there (belongs to this crush-tree)
4852 ceph_assert(osd_weight.count(i.first));
4853 float target = osd_weight[i.first] * pgs_per_weight;
4854 float deviation = (float)i.second.size() - target;
4855 ldout(cct, 20) << " osd." << i.first
4856 << "\tpgs " << i.second.size()
4857 << "\ttarget " << target
4858 << "\tdeviation " << deviation
4859 << dendl;
4860 temp_osd_deviation[i.first] = deviation;
4861 temp_deviation_osd.insert(make_pair(deviation, i.first));
4862 new_stddev += deviation * deviation;
4863 }
4864 ldout(cct, 10) << " stddev " << stddev << " -> " << new_stddev << dendl;
4865 if (new_stddev >= stddev) {
4866 if (!aggressive) {
4867 ldout(cct, 10) << " break because stddev is not decreasing"
4868 << " and aggressive mode is not enabled"
4869 << dendl;
4870 break;
4871 }
4872 local_fallback_retried++;
4873 if (local_fallback_retried >= local_fallback_retries) {
4874 // does not make progress
4875 // flip *skip_overfull* so both overfull and underfull
4876 // get equal (in)attention
4877 skip_overfull = !skip_overfull;
4878 ldout(cct, 10) << " hit local_fallback_retries "
4879 << local_fallback_retries
4880 << dendl;
4881 continue;
4882 }
4883 for (auto& i : to_unmap)
4884 to_skip.insert(i);
4885 for (auto& i : to_upmap)
4886 to_skip.insert(i.first);
4887 ldout(cct, 20) << " local_fallback_retried " << local_fallback_retried
4888 << " to_skip " << to_skip
4889 << dendl;
4890 goto retry;
4891 }
4892
4893 // ready to go
4894 ceph_assert(new_stddev < stddev);
4895 stddev = new_stddev;
4896 pgs_by_osd = temp_pgs_by_osd;
4897 osd_deviation = temp_osd_deviation;
4898 deviation_osd = temp_deviation_osd;
4899 for (auto& i : to_unmap) {
4900 ldout(cct, 10) << " unmap pg " << i << dendl;
4901 ceph_assert(tmp.pg_upmap_items.count(i));
4902 tmp.pg_upmap_items.erase(i);
4903 pending_inc->old_pg_upmap_items.insert(i);
4904 ++num_changed;
4905 }
4906 for (auto& i : to_upmap) {
4907 ldout(cct, 10) << " upmap pg " << i.first
4908 << " new pg_upmap_items " << i.second
4909 << dendl;
4910 tmp.pg_upmap_items[i.first] = i.second;
4911 pending_inc->new_pg_upmap_items[i.first] = i.second;
4912 ++num_changed;
4913 }
7c673cae 4914 }
a8e16298 4915 ldout(cct, 10) << " num_changed = " << num_changed << dendl;
7c673cae
FG
4916 return num_changed;
4917}
31f18b77
FG
4918
4919int OSDMap::get_osds_by_bucket_name(const string &name, set<int> *osds) const
4920{
4921 return crush->get_leaves(name, osds);
4922}
4923
3efd9988
FG
4924// get pools whose crush rules might reference the given osd
4925void OSDMap::get_pool_ids_by_osd(CephContext *cct,
4926 int osd,
4927 set<int64_t> *pool_ids) const
4928{
11fdf7f2 4929 ceph_assert(pool_ids);
3efd9988
FG
4930 set<int> raw_rules;
4931 int r = crush->get_rules_by_osd(osd, &raw_rules);
4932 if (r < 0) {
4933 lderr(cct) << __func__ << " get_rules_by_osd failed: " << cpp_strerror(r)
4934 << dendl;
11fdf7f2 4935 ceph_assert(r >= 0);
3efd9988
FG
4936 }
4937 set<int> rules;
4938 for (auto &i: raw_rules) {
4939 // exclude any dead rule
4940 if (crush_rule_in_use(i)) {
4941 rules.insert(i);
4942 }
4943 }
4944 for (auto &r: rules) {
4945 get_pool_ids_by_rule(r, pool_ids);
4946 }
4947}
4948
31f18b77
FG
4949template <typename F>
4950class OSDUtilizationDumper : public CrushTreeDumper::Dumper<F> {
4951public:
4952 typedef CrushTreeDumper::Dumper<F> Parent;
4953
4954 OSDUtilizationDumper(const CrushWrapper *crush, const OSDMap *osdmap_,
11fdf7f2
TL
4955 const PGMap& pgmap_, bool tree_,
4956 const string& class_name_,
4957 const string& item_name_) :
c07f9fc5 4958 Parent(crush, osdmap_->get_pool_names()),
31f18b77 4959 osdmap(osdmap_),
11fdf7f2 4960 pgmap(pgmap_),
31f18b77 4961 tree(tree_),
11fdf7f2
TL
4962 class_name(class_name_),
4963 item_name(item_name_),
31f18b77
FG
4964 min_var(-1),
4965 max_var(-1),
4966 stddev(0),
4967 sum(0) {
11fdf7f2
TL
4968 if (osdmap->crush->name_exists(item_name)) {
4969 // filter out items we are allowed to dump
4970 auto item_id = osdmap->crush->get_item_id(item_name);
4971 allowed.insert(item_id);
4972 osdmap->crush->get_all_children(item_id, &allowed);
4973 }
4974 average_util = average_utilization();
31f18b77
FG
4975 }
4976
4977protected:
11fdf7f2
TL
4978
4979 bool should_dump(int id) const {
4980 if (!allowed.empty() && !allowed.count(id)) // filter by name
4981 return false;
4982 if (id >= 0 && !class_name.empty()) {
4983 const char* item_class_name = osdmap->crush->get_item_class(id);
4984 if (!item_class_name || // not bound to a class yet
4985 item_class_name != class_name) // or already bound to
4986 // a different class
4987 return false;
4988 }
4989 return true;
4990 }
4991
4992 set<int> get_dumped_osds() {
4993 if (class_name.empty() && item_name.empty()) {
4994 // old way, all
4995 return {};
4996 }
4997 return dumped_osds;
4998 }
4999
31f18b77
FG
5000 void dump_stray(F *f) {
5001 for (int i = 0; i < osdmap->get_max_osd(); i++) {
5002 if (osdmap->exists(i) && !this->is_touched(i))
c07f9fc5 5003 dump_item(CrushTreeDumper::Item(i, 0, 0, 0), f);
31f18b77
FG
5004 }
5005 }
5006
5007 void dump_item(const CrushTreeDumper::Item &qi, F *f) override {
5008 if (!tree && qi.is_bucket())
5009 return;
11fdf7f2
TL
5010 if (!should_dump(qi.id))
5011 return;
31f18b77 5012
11fdf7f2
TL
5013 if (!qi.is_bucket())
5014 dumped_osds.insert(qi.id);
31f18b77 5015 float reweight = qi.is_bucket() ? -1 : osdmap->get_weightf(qi.id);
11fdf7f2
TL
5016 int64_t kb = 0, kb_used = 0, kb_used_data = 0, kb_used_omap = 0,
5017 kb_used_meta = 0, kb_avail = 0;
31f18b77 5018 double util = 0;
11fdf7f2
TL
5019 if (get_bucket_utilization(qi.id, &kb, &kb_used, &kb_used_data,
5020 &kb_used_omap, &kb_used_meta, &kb_avail))
31f18b77
FG
5021 if (kb_used && kb)
5022 util = 100.0 * (double)kb_used / (double)kb;
5023
5024 double var = 1.0;
5025 if (average_util)
5026 var = util / average_util;
5027
11fdf7f2 5028 size_t num_pgs = qi.is_bucket() ? 0 : pgmap.get_num_pg_by_osd(qi.id);
31f18b77 5029
11fdf7f2
TL
5030 dump_item(qi, reweight, kb, kb_used,
5031 kb_used_data, kb_used_omap, kb_used_meta,
5032 kb_avail, util, var, num_pgs, f);
31f18b77
FG
5033
5034 if (!qi.is_bucket() && reweight > 0) {
5035 if (min_var < 0 || var < min_var)
5036 min_var = var;
5037 if (max_var < 0 || var > max_var)
5038 max_var = var;
5039
5040 double dev = util - average_util;
5041 dev *= dev;
5042 stddev += reweight * dev;
5043 sum += reweight;
5044 }
5045 }
5046
5047 virtual void dump_item(const CrushTreeDumper::Item &qi,
5048 float &reweight,
5049 int64_t kb,
5050 int64_t kb_used,
11fdf7f2
TL
5051 int64_t kb_used_data,
5052 int64_t kb_used_omap,
5053 int64_t kb_used_meta,
31f18b77
FG
5054 int64_t kb_avail,
5055 double& util,
5056 double& var,
5057 const size_t num_pgs,
5058 F *f) = 0;
5059
5060 double dev() {
5061 return sum > 0 ? sqrt(stddev / sum) : 0;
5062 }
5063
5064 double average_utilization() {
5065 int64_t kb = 0, kb_used = 0;
5066 for (int i = 0; i < osdmap->get_max_osd(); i++) {
11fdf7f2
TL
5067 if (!osdmap->exists(i) ||
5068 osdmap->get_weight(i) == 0 ||
5069 !should_dump(i))
31f18b77 5070 continue;
11fdf7f2
TL
5071 int64_t kb_i, kb_used_i, kb_used_data_i, kb_used_omap_i, kb_used_meta_i,
5072 kb_avail_i;
5073 if (get_osd_utilization(i, &kb_i, &kb_used_i, &kb_used_data_i,
5074 &kb_used_omap_i, &kb_used_meta_i, &kb_avail_i)) {
31f18b77
FG
5075 kb += kb_i;
5076 kb_used += kb_used_i;
5077 }
5078 }
5079 return kb > 0 ? 100.0 * (double)kb_used / (double)kb : 0;
5080 }
5081
5082 bool get_osd_utilization(int id, int64_t* kb, int64_t* kb_used,
11fdf7f2
TL
5083 int64_t* kb_used_data,
5084 int64_t* kb_used_omap,
5085 int64_t* kb_used_meta,
31f18b77 5086 int64_t* kb_avail) const {
11fdf7f2 5087 const osd_stat_t *p = pgmap.get_osd_stat(id);
31f18b77 5088 if (!p) return false;
11fdf7f2
TL
5089 *kb = p->statfs.kb();
5090 *kb_used = p->statfs.kb_used_raw();
5091 *kb_used_data = p->statfs.kb_used_data();
5092 *kb_used_omap = p->statfs.kb_used_omap();
5093 *kb_used_meta = p->statfs.kb_used_internal_metadata();
5094 *kb_avail = p->statfs.kb_avail();
5095
31f18b77
FG
5096 return *kb > 0;
5097 }
5098
5099 bool get_bucket_utilization(int id, int64_t* kb, int64_t* kb_used,
11fdf7f2
TL
5100 int64_t* kb_used_data,
5101 int64_t* kb_used_omap,
5102 int64_t* kb_used_meta,
31f18b77
FG
5103 int64_t* kb_avail) const {
5104 if (id >= 0) {
11fdf7f2 5105 if (osdmap->is_out(id) || !should_dump(id)) {
31f18b77
FG
5106 *kb = 0;
5107 *kb_used = 0;
11fdf7f2
TL
5108 *kb_used_data = 0;
5109 *kb_used_omap = 0;
5110 *kb_used_meta = 0;
31f18b77
FG
5111 *kb_avail = 0;
5112 return true;
5113 }
11fdf7f2
TL
5114 return get_osd_utilization(id, kb, kb_used, kb_used_data,
5115 kb_used_omap, kb_used_meta, kb_avail);
31f18b77
FG
5116 }
5117
5118 *kb = 0;
5119 *kb_used = 0;
11fdf7f2
TL
5120 *kb_used_data = 0;
5121 *kb_used_omap = 0;
5122 *kb_used_meta = 0;
31f18b77
FG
5123 *kb_avail = 0;
5124
5125 for (int k = osdmap->crush->get_bucket_size(id) - 1; k >= 0; k--) {
5126 int item = osdmap->crush->get_bucket_item(id, k);
11fdf7f2
TL
5127 int64_t kb_i = 0, kb_used_i = 0, kb_used_data_i = 0,
5128 kb_used_omap_i = 0, kb_used_meta_i = 0, kb_avail_i = 0;
5129 if (!get_bucket_utilization(item, &kb_i, &kb_used_i,
5130 &kb_used_data_i, &kb_used_omap_i,
5131 &kb_used_meta_i, &kb_avail_i))
31f18b77
FG
5132 return false;
5133 *kb += kb_i;
5134 *kb_used += kb_used_i;
11fdf7f2
TL
5135 *kb_used_data += kb_used_data_i;
5136 *kb_used_omap += kb_used_omap_i;
5137 *kb_used_meta += kb_used_meta_i;
31f18b77
FG
5138 *kb_avail += kb_avail_i;
5139 }
5140 return *kb > 0;
5141 }
5142
5143protected:
5144 const OSDMap *osdmap;
11fdf7f2 5145 const PGMap& pgmap;
31f18b77 5146 bool tree;
11fdf7f2
TL
5147 const string class_name;
5148 const string item_name;
31f18b77
FG
5149 double average_util;
5150 double min_var;
5151 double max_var;
5152 double stddev;
5153 double sum;
11fdf7f2
TL
5154 set<int> allowed;
5155 set<int> dumped_osds;
31f18b77
FG
5156};
5157
5158
5159class OSDUtilizationPlainDumper : public OSDUtilizationDumper<TextTable> {
5160public:
5161 typedef OSDUtilizationDumper<TextTable> Parent;
5162
5163 OSDUtilizationPlainDumper(const CrushWrapper *crush, const OSDMap *osdmap,
11fdf7f2
TL
5164 const PGMap& pgmap, bool tree,
5165 const string& class_name,
5166 const string& item_name) :
5167 Parent(crush, osdmap, pgmap, tree, class_name, item_name) {}
31f18b77
FG
5168
5169 void dump(TextTable *tbl) {
5170 tbl->define_column("ID", TextTable::LEFT, TextTable::RIGHT);
224ce89b 5171 tbl->define_column("CLASS", TextTable::LEFT, TextTable::RIGHT);
31f18b77
FG
5172 tbl->define_column("WEIGHT", TextTable::LEFT, TextTable::RIGHT);
5173 tbl->define_column("REWEIGHT", TextTable::LEFT, TextTable::RIGHT);
5174 tbl->define_column("SIZE", TextTable::LEFT, TextTable::RIGHT);
11fdf7f2
TL
5175 tbl->define_column("RAW USE", TextTable::LEFT, TextTable::RIGHT);
5176 tbl->define_column("DATA", TextTable::LEFT, TextTable::RIGHT);
5177 tbl->define_column("OMAP", TextTable::LEFT, TextTable::RIGHT);
5178 tbl->define_column("META", TextTable::LEFT, TextTable::RIGHT);
31f18b77
FG
5179 tbl->define_column("AVAIL", TextTable::LEFT, TextTable::RIGHT);
5180 tbl->define_column("%USE", TextTable::LEFT, TextTable::RIGHT);
5181 tbl->define_column("VAR", TextTable::LEFT, TextTable::RIGHT);
5182 tbl->define_column("PGS", TextTable::LEFT, TextTable::RIGHT);
11fdf7f2 5183 tbl->define_column("STATUS", TextTable::LEFT, TextTable::RIGHT);
31f18b77
FG
5184 if (tree)
5185 tbl->define_column("TYPE NAME", TextTable::LEFT, TextTable::LEFT);
5186
5187 Parent::dump(tbl);
5188
5189 dump_stray(tbl);
5190
11fdf7f2 5191 auto sum = pgmap.get_osd_sum(get_dumped_osds());
224ce89b
WB
5192 *tbl << ""
5193 << ""
5194 << "" << "TOTAL"
11fdf7f2
TL
5195 << byte_u_t(sum.statfs.total)
5196 << byte_u_t(sum.statfs.get_used_raw())
5197 << byte_u_t(sum.statfs.allocated)
5198 << byte_u_t(sum.statfs.omap_allocated)
5199 << byte_u_t(sum.statfs.internal_metadata)
5200 << byte_u_t(sum.statfs.available)
31f18b77
FG
5201 << lowprecision_t(average_util)
5202 << ""
5203 << TextTable::endrow;
5204 }
5205
5206protected:
5207 struct lowprecision_t {
5208 float v;
5209 explicit lowprecision_t(float _v) : v(_v) {}
5210 };
5211 friend std::ostream &operator<<(ostream& out, const lowprecision_t& v);
5212
5213 using OSDUtilizationDumper<TextTable>::dump_item;
5214 void dump_item(const CrushTreeDumper::Item &qi,
5215 float &reweight,
5216 int64_t kb,
5217 int64_t kb_used,
11fdf7f2
TL
5218 int64_t kb_used_data,
5219 int64_t kb_used_omap,
5220 int64_t kb_used_meta,
31f18b77
FG
5221 int64_t kb_avail,
5222 double& util,
5223 double& var,
5224 const size_t num_pgs,
5225 TextTable *tbl) override {
224ce89b
WB
5226 const char *c = crush->get_item_class(qi.id);
5227 if (!c)
5228 c = "";
31f18b77 5229 *tbl << qi.id
224ce89b 5230 << c
31f18b77
FG
5231 << weightf_t(qi.weight)
5232 << weightf_t(reweight)
1adf2230
AA
5233 << byte_u_t(kb << 10)
5234 << byte_u_t(kb_used << 10)
11fdf7f2
TL
5235 << byte_u_t(kb_used_data << 10)
5236 << byte_u_t(kb_used_omap << 10)
5237 << byte_u_t(kb_used_meta << 10)
1adf2230 5238 << byte_u_t(kb_avail << 10)
31f18b77
FG
5239 << lowprecision_t(util)
5240 << lowprecision_t(var);
5241
5242 if (qi.is_bucket()) {
5243 *tbl << "-";
11fdf7f2 5244 *tbl << "";
31f18b77
FG
5245 } else {
5246 *tbl << num_pgs;
11fdf7f2
TL
5247 if (osdmap->is_up(qi.id)) {
5248 *tbl << "up";
5249 } else if (osdmap->is_destroyed(qi.id)) {
5250 *tbl << "destroyed";
5251 } else {
5252 *tbl << "down";
5253 }
31f18b77
FG
5254 }
5255
5256 if (tree) {
5257 ostringstream name;
5258 for (int k = 0; k < qi.depth; k++)
5259 name << " ";
5260 if (qi.is_bucket()) {
5261 int type = crush->get_bucket_type(qi.id);
5262 name << crush->get_type_name(type) << " "
5263 << crush->get_item_name(qi.id);
5264 } else {
5265 name << "osd." << qi.id;
5266 }
5267 *tbl << name.str();
5268 }
5269
5270 *tbl << TextTable::endrow;
5271 }
5272
5273public:
5274 string summary() {
5275 ostringstream out;
5276 out << "MIN/MAX VAR: " << lowprecision_t(min_var)
5277 << "/" << lowprecision_t(max_var) << " "
5278 << "STDDEV: " << lowprecision_t(dev());
5279 return out.str();
5280 }
5281};
5282
5283ostream& operator<<(ostream& out,
5284 const OSDUtilizationPlainDumper::lowprecision_t& v)
5285{
5286 if (v.v < -0.01) {
5287 return out << "-";
5288 } else if (v.v < 0.001) {
5289 return out << "0";
5290 } else {
5291 std::streamsize p = out.precision();
5292 return out << std::fixed << std::setprecision(2) << v.v << std::setprecision(p);
5293 }
5294}
5295
5296class OSDUtilizationFormatDumper : public OSDUtilizationDumper<Formatter> {
5297public:
5298 typedef OSDUtilizationDumper<Formatter> Parent;
5299
5300 OSDUtilizationFormatDumper(const CrushWrapper *crush, const OSDMap *osdmap,
11fdf7f2
TL
5301 const PGMap& pgmap, bool tree,
5302 const string& class_name,
5303 const string& item_name) :
5304 Parent(crush, osdmap, pgmap, tree, class_name, item_name) {}
31f18b77
FG
5305
5306 void dump(Formatter *f) {
5307 f->open_array_section("nodes");
5308 Parent::dump(f);
5309 f->close_section();
5310
5311 f->open_array_section("stray");
5312 dump_stray(f);
5313 f->close_section();
5314 }
5315
5316protected:
5317 using OSDUtilizationDumper<Formatter>::dump_item;
5318 void dump_item(const CrushTreeDumper::Item &qi,
11fdf7f2
TL
5319 float &reweight,
5320 int64_t kb,
5321 int64_t kb_used,
5322 int64_t kb_used_data,
5323 int64_t kb_used_omap,
5324 int64_t kb_used_meta,
5325 int64_t kb_avail,
5326 double& util,
5327 double& var,
5328 const size_t num_pgs,
5329 Formatter *f) override {
31f18b77 5330 f->open_object_section("item");
c07f9fc5 5331 CrushTreeDumper::dump_item_fields(crush, weight_set_names, qi, f);
31f18b77
FG
5332 f->dump_float("reweight", reweight);
5333 f->dump_int("kb", kb);
5334 f->dump_int("kb_used", kb_used);
11fdf7f2
TL
5335 f->dump_int("kb_used_data", kb_used_data);
5336 f->dump_int("kb_used_omap", kb_used_omap);
5337 f->dump_int("kb_used_meta", kb_used_meta);
31f18b77
FG
5338 f->dump_int("kb_avail", kb_avail);
5339 f->dump_float("utilization", util);
5340 f->dump_float("var", var);
5341 f->dump_unsigned("pgs", num_pgs);
11fdf7f2
TL
5342 if (!qi.is_bucket()) {
5343 if (osdmap->is_up(qi.id)) {
5344 f->dump_string("status", "up");
5345 } else if (osdmap->is_destroyed(qi.id)) {
5346 f->dump_string("status", "destroyed");
5347 } else {
5348 f->dump_string("status", "down");
5349 }
5350 }
31f18b77
FG
5351 CrushTreeDumper::dump_bucket_children(crush, qi, f);
5352 f->close_section();
5353 }
5354
5355public:
5356 void summary(Formatter *f) {
5357 f->open_object_section("summary");
11fdf7f2
TL
5358 auto sum = pgmap.get_osd_sum(get_dumped_osds());
5359 auto& s = sum.statfs;
5360
5361 f->dump_int("total_kb", s.kb());
5362 f->dump_int("total_kb_used", s.kb_used_raw());
5363 f->dump_int("total_kb_used_data", s.kb_used_data());
5364 f->dump_int("total_kb_used_omap", s.kb_used_omap());
5365 f->dump_int("total_kb_used_meta", s.kb_used_internal_metadata());
5366 f->dump_int("total_kb_avail", s.kb_avail());
31f18b77
FG
5367 f->dump_float("average_utilization", average_util);
5368 f->dump_float("min_var", min_var);
5369 f->dump_float("max_var", max_var);
5370 f->dump_float("dev", dev());
5371 f->close_section();
5372 }
5373};
5374
5375void print_osd_utilization(const OSDMap& osdmap,
11fdf7f2
TL
5376 const PGMap& pgmap,
5377 ostream& out,
5378 Formatter *f,
5379 bool tree,
5380 const string& class_name,
5381 const string& item_name)
31f18b77
FG
5382{
5383 const CrushWrapper *crush = osdmap.crush.get();
5384 if (f) {
5385 f->open_object_section("df");
11fdf7f2
TL
5386 OSDUtilizationFormatDumper d(crush, &osdmap, pgmap, tree,
5387 class_name, item_name);
31f18b77
FG
5388 d.dump(f);
5389 d.summary(f);
5390 f->close_section();
5391 f->flush(out);
5392 } else {
11fdf7f2
TL
5393 OSDUtilizationPlainDumper d(crush, &osdmap, pgmap, tree,
5394 class_name, item_name);
31f18b77
FG
5395 TextTable tbl;
5396 d.dump(&tbl);
5397 out << tbl << d.summary() << "\n";
5398 }
5399}
224ce89b
WB
5400
5401void OSDMap::check_health(health_check_map_t *checks) const
5402{
5403 int num_osds = get_num_osds();
5404
5405 // OSD_DOWN
5406 // OSD_$subtree_DOWN
5407 // OSD_ORPHAN
5408 if (num_osds >= 0) {
5409 int num_in_osds = 0;
5410 int num_down_in_osds = 0;
5411 set<int> osds;
5412 set<int> down_in_osds;
5413 set<int> up_in_osds;
5414 set<int> subtree_up;
5415 unordered_map<int, set<int> > subtree_type_down;
5416 unordered_map<int, int> num_osds_subtree;
5417 int max_type = crush->get_max_type_id();
5418
5419 for (int i = 0; i < get_max_osd(); i++) {
5420 if (!exists(i)) {
5421 if (crush->item_exists(i)) {
5422 osds.insert(i);
5423 }
5424 continue;
5425 }
5426 if (is_out(i))
5427 continue;
5428 ++num_in_osds;
5429 if (down_in_osds.count(i) || up_in_osds.count(i))
5430 continue;
5431 if (!is_up(i)) {
5432 down_in_osds.insert(i);
5433 int parent_id = 0;
5434 int current = i;
5435 for (int type = 0; type <= max_type; type++) {
5436 if (!crush->get_type_name(type))
5437 continue;
5438 int r = crush->get_immediate_parent_id(current, &parent_id);
5439 if (r == -ENOENT)
5440 break;
5441 // break early if this parent is already marked as up
5442 if (subtree_up.count(parent_id))
5443 break;
5444 type = crush->get_bucket_type(parent_id);
5445 if (!subtree_type_is_down(
5446 g_ceph_context, parent_id, type,
5447 &down_in_osds, &up_in_osds, &subtree_up, &subtree_type_down))
5448 break;
5449 current = parent_id;
5450 }
5451 }
5452 }
5453
5454 // calculate the number of down osds in each down subtree and
5455 // store it in num_osds_subtree
5456 for (int type = 1; type <= max_type; type++) {
5457 if (!crush->get_type_name(type))
5458 continue;
5459 for (auto j = subtree_type_down[type].begin();
5460 j != subtree_type_down[type].end();
5461 ++j) {
5462 list<int> children;
5463 int num = 0;
5464 int num_children = crush->get_children(*j, &children);
5465 if (num_children == 0)
5466 continue;
5467 for (auto l = children.begin(); l != children.end(); ++l) {
5468 if (*l >= 0) {
5469 ++num;
5470 } else if (num_osds_subtree[*l] > 0) {
5471 num = num + num_osds_subtree[*l];
5472 }
5473 }
5474 num_osds_subtree[*j] = num;
5475 }
5476 }
5477 num_down_in_osds = down_in_osds.size();
11fdf7f2 5478 ceph_assert(num_down_in_osds <= num_in_osds);
224ce89b
WB
5479 if (num_down_in_osds > 0) {
5480 // summary of down subtree types and osds
5481 for (int type = max_type; type > 0; type--) {
5482 if (!crush->get_type_name(type))
5483 continue;
5484 if (subtree_type_down[type].size() > 0) {
5485 ostringstream ss;
5486 ss << subtree_type_down[type].size() << " "
5487 << crush->get_type_name(type);
5488 if (subtree_type_down[type].size() > 1) {
5489 ss << "s";
5490 }
5491 int sum_down_osds = 0;
5492 for (auto j = subtree_type_down[type].begin();
5493 j != subtree_type_down[type].end();
5494 ++j) {
5495 sum_down_osds = sum_down_osds + num_osds_subtree[*j];
5496 }
5497 ss << " (" << sum_down_osds << " osds) down";
5498 string err = string("OSD_") +
5499 string(crush->get_type_name(type)) + "_DOWN";
5500 boost::to_upper(err);
5501 auto& d = checks->add(err, HEALTH_WARN, ss.str());
5502 for (auto j = subtree_type_down[type].rbegin();
5503 j != subtree_type_down[type].rend();
5504 ++j) {
5505 ostringstream ss;
5506 ss << crush->get_type_name(type);
5507 ss << " ";
5508 ss << crush->get_item_name(*j);
5509 // at the top level, do not print location
5510 if (type != max_type) {
5511 ss << " (";
5512 ss << crush->get_full_location_ordered_string(*j);
5513 ss << ")";
5514 }
5515 int num = num_osds_subtree[*j];
5516 ss << " (" << num << " osds)";
5517 ss << " is down";
5518 d.detail.push_back(ss.str());
5519 }
5520 }
5521 }
5522 ostringstream ss;
5523 ss << down_in_osds.size() << " osds down";
5524 auto& d = checks->add("OSD_DOWN", HEALTH_WARN, ss.str());
5525 for (auto it = down_in_osds.begin(); it != down_in_osds.end(); ++it) {
5526 ostringstream ss;
5527 ss << "osd." << *it << " (";
5528 ss << crush->get_full_location_ordered_string(*it);
5529 ss << ") is down";
5530 d.detail.push_back(ss.str());
5531 }
5532 }
5533
5534 if (!osds.empty()) {
5535 ostringstream ss;
5536 ss << osds.size() << " osds exist in the crush map but not in the osdmap";
5537 auto& d = checks->add("OSD_ORPHAN", HEALTH_WARN, ss.str());
5538 for (auto osd : osds) {
5539 ostringstream ss;
5540 ss << "osd." << osd << " exists in crush map but not in osdmap";
5541 d.detail.push_back(ss.str());
5542 }
5543 }
5544 }
5545
5546 // OSD_OUT_OF_ORDER_FULL
5547 {
5548 // An osd could configure failsafe ratio, to something different
5549 // but for now assume it is the same here.
11fdf7f2 5550 float fsr = g_conf()->osd_failsafe_full_ratio;
224ce89b
WB
5551 if (fsr > 1.0) fsr /= 100;
5552 float fr = get_full_ratio();
5553 float br = get_backfillfull_ratio();
5554 float nr = get_nearfull_ratio();
5555
5556 list<string> detail;
5557 // These checks correspond to how OSDService::check_full_status() in an OSD
5558 // handles the improper setting of these values.
5559 if (br < nr) {
5560 ostringstream ss;
5561 ss << "backfillfull_ratio (" << br
5562 << ") < nearfull_ratio (" << nr << "), increased";
5563 detail.push_back(ss.str());
5564 br = nr;
5565 }
5566 if (fr < br) {
5567 ostringstream ss;
5568 ss << "full_ratio (" << fr << ") < backfillfull_ratio (" << br
5569 << "), increased";
5570 detail.push_back(ss.str());
5571 fr = br;
5572 }
5573 if (fsr < fr) {
5574 ostringstream ss;
5575 ss << "osd_failsafe_full_ratio (" << fsr << ") < full_ratio (" << fr
5576 << "), increased";
5577 detail.push_back(ss.str());
5578 }
5579 if (!detail.empty()) {
5580 auto& d = checks->add("OSD_OUT_OF_ORDER_FULL", HEALTH_ERR,
5581 "full ratio(s) out of order");
5582 d.detail.swap(detail);
5583 }
5584 }
5585
5586 // OSD_FULL
5587 // OSD_NEARFULL
5588 // OSD_BACKFILLFULL
5589 // OSD_FAILSAFE_FULL
5590 {
5591 set<int> full, backfillfull, nearfull;
5592 get_full_osd_counts(&full, &backfillfull, &nearfull);
5593 if (full.size()) {
5594 ostringstream ss;
5595 ss << full.size() << " full osd(s)";
5596 auto& d = checks->add("OSD_FULL", HEALTH_ERR, ss.str());
5597 for (auto& i: full) {
5598 ostringstream ss;
5599 ss << "osd." << i << " is full";
5600 d.detail.push_back(ss.str());
5601 }
5602 }
5603 if (backfillfull.size()) {
5604 ostringstream ss;
5605 ss << backfillfull.size() << " backfillfull osd(s)";
5606 auto& d = checks->add("OSD_BACKFILLFULL", HEALTH_WARN, ss.str());
5607 for (auto& i: backfillfull) {
5608 ostringstream ss;
5609 ss << "osd." << i << " is backfill full";
5610 d.detail.push_back(ss.str());
5611 }
5612 }
5613 if (nearfull.size()) {
5614 ostringstream ss;
5615 ss << nearfull.size() << " nearfull osd(s)";
5616 auto& d = checks->add("OSD_NEARFULL", HEALTH_WARN, ss.str());
5617 for (auto& i: nearfull) {
5618 ostringstream ss;
5619 ss << "osd." << i << " is near full";
5620 d.detail.push_back(ss.str());
5621 }
5622 }
5623 }
5624
5625 // OSDMAP_FLAGS
5626 {
5627 // warn about flags
5628 uint64_t warn_flags =
3efd9988 5629 CEPH_OSDMAP_NEARFULL |
224ce89b
WB
5630 CEPH_OSDMAP_FULL |
5631 CEPH_OSDMAP_PAUSERD |
5632 CEPH_OSDMAP_PAUSEWR |
5633 CEPH_OSDMAP_PAUSEREC |
5634 CEPH_OSDMAP_NOUP |
5635 CEPH_OSDMAP_NODOWN |
5636 CEPH_OSDMAP_NOIN |
5637 CEPH_OSDMAP_NOOUT |
5638 CEPH_OSDMAP_NOBACKFILL |
5639 CEPH_OSDMAP_NORECOVER |
5640 CEPH_OSDMAP_NOSCRUB |
5641 CEPH_OSDMAP_NODEEP_SCRUB |
5642 CEPH_OSDMAP_NOTIERAGENT |
11fdf7f2 5643 CEPH_OSDMAP_NOSNAPTRIM |
224ce89b
WB
5644 CEPH_OSDMAP_NOREBALANCE;
5645 if (test_flag(warn_flags)) {
5646 ostringstream ss;
5647 ss << get_flag_string(get_flags() & warn_flags)
5648 << " flag(s) set";
5649 checks->add("OSDMAP_FLAGS", HEALTH_WARN, ss.str());
5650 }
5651 }
5652
5653 // OSD_FLAGS
5654 {
5655 list<string> detail;
5656 const unsigned flags =
5657 CEPH_OSD_NOUP |
5658 CEPH_OSD_NOIN |
5659 CEPH_OSD_NODOWN |
5660 CEPH_OSD_NOOUT;
5661 for (int i = 0; i < max_osd; ++i) {
5662 if (osd_state[i] & flags) {
5663 ostringstream ss;
5664 set<string> states;
5665 OSDMap::calc_state_set(osd_state[i] & flags, states);
5666 ss << "osd." << i << " has flags " << states;
5667 detail.push_back(ss.str());
5668 }
5669 }
81eedcae
TL
5670 for (auto& i : crush_node_flags) {
5671 if (i.second && crush->item_exists(i.first)) {
5672 ostringstream ss;
5673 set<string> states;
5674 OSDMap::calc_state_set(i.second, states);
5675 int t = i.first >= 0 ? 0 : crush->get_bucket_type(i.first);
5676 const char *tn = crush->get_type_name(t);
5677 ss << (tn ? tn : "node") << " "
5678 << crush->get_item_name(i.first) << " has flags " << states;
5679 detail.push_back(ss.str());
5680 }
5681 }
5682 for (auto& i : device_class_flags) {
5683 const char* class_name = crush->get_class_name(i.first);
5684 if (i.second && class_name) {
5685 ostringstream ss;
5686 set<string> states;
5687 OSDMap::calc_state_set(i.second, states);
5688 ss << "device class '" << class_name << "' has flags " << states;
5689 detail.push_back(ss.str());
5690 }
5691 }
224ce89b
WB
5692 if (!detail.empty()) {
5693 ostringstream ss;
81eedcae 5694 ss << detail.size() << " OSDs or CRUSH {nodes, device-classes} have {NOUP,NODOWN,NOIN,NOOUT} flags set";
224ce89b
WB
5695 auto& d = checks->add("OSD_FLAGS", HEALTH_WARN, ss.str());
5696 d.detail.swap(detail);
5697 }
5698 }
5699
5700 // OLD_CRUSH_TUNABLES
11fdf7f2 5701 if (g_conf()->mon_warn_on_legacy_crush_tunables) {
224ce89b 5702 string min = crush->get_min_required_version();
11fdf7f2 5703 if (min < g_conf()->mon_crush_min_required_version) {
224ce89b
WB
5704 ostringstream ss;
5705 ss << "crush map has legacy tunables (require " << min
11fdf7f2 5706 << ", min is " << g_conf()->mon_crush_min_required_version << ")";
224ce89b
WB
5707 auto& d = checks->add("OLD_CRUSH_TUNABLES", HEALTH_WARN, ss.str());
5708 d.detail.push_back("see http://docs.ceph.com/docs/master/rados/operations/crush-map/#tunables");
5709 }
5710 }
5711
5712 // OLD_CRUSH_STRAW_CALC_VERSION
11fdf7f2 5713 if (g_conf()->mon_warn_on_crush_straw_calc_version_zero) {
224ce89b
WB
5714 if (crush->get_straw_calc_version() == 0) {
5715 ostringstream ss;
5716 ss << "crush map has straw_calc_version=0";
5717 auto& d = checks->add("OLD_CRUSH_STRAW_CALC_VERSION", HEALTH_WARN, ss.str());
5718 d.detail.push_back(
5719 "see http://docs.ceph.com/docs/master/rados/operations/crush-map/#tunables");
5720 }
5721 }
5722
5723 // CACHE_POOL_NO_HIT_SET
11fdf7f2 5724 if (g_conf()->mon_warn_on_cache_pools_without_hit_sets) {
224ce89b
WB
5725 list<string> detail;
5726 for (map<int64_t, pg_pool_t>::const_iterator p = pools.begin();
5727 p != pools.end();
5728 ++p) {
5729 const pg_pool_t& info = p->second;
5730 if (info.cache_mode_requires_hit_set() &&
5731 info.hit_set_params.get_type() == HitSet::TYPE_NONE) {
5732 ostringstream ss;
5733 ss << "pool '" << get_pool_name(p->first)
5734 << "' with cache_mode " << info.get_cache_mode_name()
5735 << " needs hit_set_type to be set but it is not";
5736 detail.push_back(ss.str());
5737 }
5738 }
5739 if (!detail.empty()) {
5740 ostringstream ss;
5741 ss << detail.size() << " cache pools are missing hit_sets";
5742 auto& d = checks->add("CACHE_POOL_NO_HIT_SET", HEALTH_WARN, ss.str());
5743 d.detail.swap(detail);
5744 }
5745 }
5746
5747 // OSD_NO_SORTBITWISE
11fdf7f2 5748 if (!test_flag(CEPH_OSDMAP_SORTBITWISE)) {
224ce89b 5749 ostringstream ss;
11fdf7f2 5750 ss << "'sortbitwise' flag is not set";
224ce89b
WB
5751 checks->add("OSD_NO_SORTBITWISE", HEALTH_WARN, ss.str());
5752 }
5753
5754 // OSD_UPGRADE_FINISHED
5755 // none of these (yet) since we don't run until luminous upgrade is done.
5756
3efd9988 5757 // POOL_NEARFULL/BACKFILLFULL/FULL
224ce89b 5758 {
3efd9988 5759 list<string> full_detail, backfillfull_detail, nearfull_detail;
224ce89b
WB
5760 for (auto it : get_pools()) {
5761 const pg_pool_t &pool = it.second;
3efd9988 5762 const string& pool_name = get_pool_name(it.first);
224ce89b 5763 if (pool.has_flag(pg_pool_t::FLAG_FULL)) {
224ce89b 5764 stringstream ss;
11fdf7f2 5765 if (pool.has_flag(pg_pool_t::FLAG_FULL_QUOTA)) {
3efd9988
FG
5766 // may run out of space too,
5767 // but we want EQUOTA taking precedence
11fdf7f2 5768 ss << "pool '" << pool_name << "' is full (running out of quota)";
3efd9988
FG
5769 } else {
5770 ss << "pool '" << pool_name << "' is full (no space)";
5771 }
5772 full_detail.push_back(ss.str());
5773 } else if (pool.has_flag(pg_pool_t::FLAG_BACKFILLFULL)) {
5774 stringstream ss;
5775 ss << "pool '" << pool_name << "' is backfillfull";
5776 backfillfull_detail.push_back(ss.str());
5777 } else if (pool.has_flag(pg_pool_t::FLAG_NEARFULL)) {
5778 stringstream ss;
5779 ss << "pool '" << pool_name << "' is nearfull";
5780 nearfull_detail.push_back(ss.str());
224ce89b
WB
5781 }
5782 }
3efd9988 5783 if (!full_detail.empty()) {
224ce89b 5784 ostringstream ss;
3efd9988 5785 ss << full_detail.size() << " pool(s) full";
224ce89b 5786 auto& d = checks->add("POOL_FULL", HEALTH_WARN, ss.str());
3efd9988
FG
5787 d.detail.swap(full_detail);
5788 }
5789 if (!backfillfull_detail.empty()) {
5790 ostringstream ss;
5791 ss << backfillfull_detail.size() << " pool(s) backfillfull";
5792 auto& d = checks->add("POOL_BACKFILLFULL", HEALTH_WARN, ss.str());
5793 d.detail.swap(backfillfull_detail);
5794 }
5795 if (!nearfull_detail.empty()) {
5796 ostringstream ss;
5797 ss << nearfull_detail.size() << " pool(s) nearfull";
5798 auto& d = checks->add("POOL_NEARFULL", HEALTH_WARN, ss.str());
5799 d.detail.swap(nearfull_detail);
224ce89b
WB
5800 }
5801 }
5802}
35e4c445
FG
5803
5804int OSDMap::parse_osd_id_list(const vector<string>& ls, set<int> *out,
5805 ostream *ss) const
5806{
5807 out->clear();
5808 for (auto i = ls.begin(); i != ls.end(); ++i) {
5809 if (i == ls.begin() &&
5810 (*i == "any" || *i == "all" || *i == "*")) {
5811 get_all_osds(*out);
5812 break;
5813 }
5814 long osd = parse_osd_id(i->c_str(), ss);
5815 if (osd < 0) {
5816 *ss << "invalid osd id '" << *i << "'";
5817 return -EINVAL;
5818 }
5819 out->insert(osd);
5820 }
5821 return 0;
5822}
11fdf7f2
TL
5823
5824void OSDMap::get_random_up_osds_by_subtree(int n, // whoami
5825 string &subtree,
5826 int limit, // how many
5827 set<int> skip,
5828 set<int> *want) const {
5829 if (limit <= 0)
5830 return;
5831 int subtree_type = crush->get_type_id(subtree);
5832 if (subtree_type < 1)
5833 return;
5834 vector<int> subtrees;
5835 crush->get_subtree_of_type(subtree_type, &subtrees);
5836 std::random_device rd;
5837 std::default_random_engine rng{rd()};
5838 std::shuffle(subtrees.begin(), subtrees.end(), rng);
5839 for (auto s : subtrees) {
5840 if (limit <= 0)
5841 break;
5842 if (crush->subtree_contains(s, n))
5843 continue;
5844 vector<int> osds;
5845 crush->get_children_of_type(s, 0, &osds);
5846 if (osds.empty())
5847 continue;
5848 vector<int> up_osds;
5849 for (auto o : osds) {
5850 if (is_up(o) && !skip.count(o))
5851 up_osds.push_back(o);
5852 }
5853 if (up_osds.empty())
5854 continue;
5855 auto it = up_osds.begin();
5856 std::advance(it, (n % up_osds.size()));
5857 want->insert(*it);
5858 --limit;
5859 }
5860}
5861
5862float OSDMap::pool_raw_used_rate(int64_t poolid) const
5863{
5864 const pg_pool_t *pool = get_pg_pool(poolid);
5865 assert(pool != nullptr);
5866
5867 switch (pool->get_type()) {
5868 case pg_pool_t::TYPE_REPLICATED:
5869 return pool->get_size();
5870 break;
5871 case pg_pool_t::TYPE_ERASURE:
5872 {
5873 auto& ecp =
5874 get_erasure_code_profile(pool->erasure_code_profile);
5875 auto pm = ecp.find("m");
5876 auto pk = ecp.find("k");
5877 if (pm != ecp.end() && pk != ecp.end()) {
5878 int k = atoi(pk->second.c_str());
5879 int m = atoi(pm->second.c_str());
5880 int mk = m + k;
5881 ceph_assert(mk != 0);
5882 ceph_assert(k != 0);
5883 return (float)mk / k;
5884 } else {
5885 return 0.0;
5886 }
5887 }
5888 break;
5889 default:
5890 ceph_abort_msg("unrecognized pool type");
5891 }
5892}
81eedcae
TL
5893
5894unsigned OSDMap::get_osd_crush_node_flags(int osd) const
5895{
5896 unsigned flags = 0;
5897 if (!crush_node_flags.empty()) {
5898 // the map will contain type -> name
5899 std::map<std::string,std::string> ploc = crush->get_full_location(osd);
5900 for (auto& i : ploc) {
5901 int id = crush->get_item_id(i.second);
5902 auto p = crush_node_flags.find(id);
5903 if (p != crush_node_flags.end()) {
5904 flags |= p->second;
5905 }
5906 }
5907 }
5908 return flags;
5909}
5910
5911unsigned OSDMap::get_crush_node_flags(int id) const
5912{
5913 unsigned flags = 0;
5914 auto it = crush_node_flags.find(id);
5915 if (it != crush_node_flags.end())
5916 flags = it->second;
5917 return flags;
5918}
5919
5920unsigned OSDMap::get_device_class_flags(int id) const
5921{
5922 unsigned flags = 0;
5923 auto it = device_class_flags.find(id);
5924 if (it != device_class_flags.end())
5925 flags = it->second;
5926 return flags;
5927}