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