]> git.proxmox.com Git - ceph.git/blame - ceph/src/osd/OSDMap.cc
update source to 12.2.11
[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);
91327a77 1569 bool remove = false;
7c673cae
FG
1570 if (vectors_equal(raw_up, pg.second)) {
1571 ldout(cct, 10) << __func__ << " removing pg_temp " << pg.first << " "
1572 << pg.second << " that matches raw_up mapping" << dendl;
91327a77
AA
1573 remove = true;
1574 }
1575 // oversized pg_temp?
1576 if (pg.second.size() > tmpmap.get_pg_pool(pg.first.pool())->get_size()) {
1577 ldout(cct, 10) << __func__ << " removing pg_temp " << pg.first << " "
1578 << pg.second << " exceeds pool size" << dendl;
1579 remove = true;
1580 }
1581 if (remove) {
7c673cae
FG
1582 if (osdmap.pg_temp->count(pg.first))
1583 pending_inc->new_pg_temp[pg.first].clear();
1584 else
1585 pending_inc->new_pg_temp.erase(pg.first);
1586 }
1587 }
1588
1589 for (auto &pg : *tmpmap.primary_temp) {
1590 // primary down?
1591 if (tmpmap.is_down(pg.second)) {
1592 ldout(cct, 10) << __func__ << " removing primary_temp " << pg.first
1593 << " to down " << pg.second << dendl;
1594 pending_inc->new_primary_temp[pg.first] = -1;
1595 continue;
1596 }
1597 // redundant primary_temp?
1598 vector<int> real_up, templess_up;
1599 int real_primary, templess_primary;
1600 pg_t pgid = pg.first;
1601 tmpmap.pg_to_acting_osds(pgid, &real_up, &real_primary);
1602 tmpmap.pg_to_raw_up(pgid, &templess_up, &templess_primary);
1603 if (real_primary == templess_primary){
1604 ldout(cct, 10) << __func__ << " removing primary_temp "
1605 << pgid << " -> " << real_primary
1606 << " (unnecessary/redundant)" << dendl;
1607 if (osdmap.primary_temp->count(pgid))
1608 pending_inc->new_primary_temp[pgid] = -1;
1609 else
1610 pending_inc->new_primary_temp.erase(pgid);
1611 }
1612 }
1613}
1614
94b18763
FG
1615void OSDMap::maybe_remove_pg_upmaps(CephContext *cct,
1616 const OSDMap& osdmap,
1617 Incremental *pending_inc)
1618{
1619 ldout(cct, 10) << __func__ << dendl;
1620 OSDMap tmpmap;
1621 tmpmap.deepish_copy_from(osdmap);
1622 tmpmap.apply_incremental(*pending_inc);
28e407b8
AA
1623 set<pg_t> to_check;
1624 set<pg_t> to_cancel;
1625 map<int, map<int, float>> rule_weight_map;
94b18763
FG
1626
1627 for (auto& p : tmpmap.pg_upmap) {
28e407b8
AA
1628 to_check.insert(p.first);
1629 }
1630 for (auto& p : tmpmap.pg_upmap_items) {
1631 to_check.insert(p.first);
1632 }
1633 for (auto& p : pending_inc->new_pg_upmap) {
1634 to_check.insert(p.first);
1635 }
1636 for (auto& p : pending_inc->new_pg_upmap_items) {
1637 to_check.insert(p.first);
1638 }
1639 for (auto& pg : to_check) {
f64942e4
AA
1640 if (!tmpmap.pg_exists(pg)) {
1641 ldout(cct, 0) << __func__ << " pg " << pg << " is gone" << dendl;
1642 to_cancel.insert(pg);
94b18763
FG
1643 continue;
1644 }
f64942e4 1645 auto crush_rule = tmpmap.get_pg_pool_crush_rule(pg);
28e407b8
AA
1646 map<int, float> weight_map;
1647 auto it = rule_weight_map.find(crush_rule);
1648 if (it == rule_weight_map.end()) {
1649 auto r = tmpmap.crush->get_rule_weight_osd_map(crush_rule, &weight_map);
1650 if (r < 0) {
1651 lderr(cct) << __func__ << " unable to get crush weight_map for "
1652 << "crush_rule " << crush_rule << dendl;
1653 continue;
1654 }
1655 rule_weight_map[crush_rule] = weight_map;
1656 } else {
1657 weight_map = it->second;
1658 }
94b18763
FG
1659 auto type = tmpmap.crush->get_rule_failure_domain(crush_rule);
1660 if (type < 0) {
1661 lderr(cct) << __func__ << " unable to load failure-domain-type of pg "
28e407b8 1662 << pg << dendl;
94b18763
FG
1663 continue;
1664 }
28e407b8 1665 ldout(cct, 10) << __func__ << " pg " << pg
94b18763 1666 << " crush-rule-id " << crush_rule
28e407b8 1667 << " weight_map " << weight_map
94b18763
FG
1668 << " failure-domain-type " << type
1669 << dendl;
1670 vector<int> raw;
1671 int primary;
28e407b8 1672 tmpmap.pg_to_raw_up(pg, &raw, &primary);
94b18763 1673 set<int> parents;
94b18763 1674 for (auto osd : raw) {
f64942e4
AA
1675 // skip non-existent/down osd for erasure-coded PGs
1676 if (osd == CRUSH_ITEM_NONE)
1677 continue;
28e407b8
AA
1678 if (type > 0) {
1679 auto parent = tmpmap.crush->get_parent_of_type(osd, type, crush_rule);
f64942e4
AA
1680 if (parent < 0) {
1681 auto r = parents.insert(parent);
1682 if (!r.second) {
1683 // two up-set osds come from same parent
1684 to_cancel.insert(pg);
1685 break;
1686 }
1687 } else {
28e407b8
AA
1688 lderr(cct) << __func__ << " unable to get parent of raw osd."
1689 << osd << " of pg " << pg
1690 << dendl;
f64942e4 1691 // continue to do checks below
28e407b8
AA
1692 }
1693 }
1694 // the above check validates collision only
1695 // below we continue to check against crush-topology changing..
1696 auto it = weight_map.find(osd);
1697 if (it == weight_map.end()) {
1698 // osd is gone or has been moved out of the specific crush-tree
1699 to_cancel.insert(pg);
94b18763
FG
1700 break;
1701 }
28e407b8
AA
1702 auto adjusted_weight = tmpmap.get_weightf(it->first) * it->second;
1703 if (adjusted_weight == 0) {
1704 // osd is out/crush-out
1705 to_cancel.insert(pg);
94b18763
FG
1706 break;
1707 }
1708 }
28e407b8
AA
1709 }
1710 for (auto &pg: to_cancel) {
1711 { // pg_upmap
1712 auto it = pending_inc->new_pg_upmap.find(pg);
94b18763 1713 if (it != pending_inc->new_pg_upmap.end()) {
28e407b8
AA
1714 ldout(cct, 10) << __func__ << " cancel invalid pending "
1715 << "pg_upmap entry "
1716 << it->first << "->" << it->second
1717 << dendl;
94b18763
FG
1718 pending_inc->new_pg_upmap.erase(it);
1719 }
28e407b8
AA
1720 if (osdmap.pg_upmap.count(pg)) {
1721 ldout(cct, 10) << __func__ << " cancel invalid pg_upmap entry "
1722 << osdmap.pg_upmap.find(pg)->first << "->"
1723 << osdmap.pg_upmap.find(pg)->second
1724 << dendl;
1725 pending_inc->old_pg_upmap.insert(pg);
94b18763
FG
1726 }
1727 }
28e407b8
AA
1728 { // pg_upmap_items
1729 auto it = pending_inc->new_pg_upmap_items.find(pg);
94b18763 1730 if (it != pending_inc->new_pg_upmap_items.end()) {
28e407b8
AA
1731 ldout(cct, 10) << __func__ << " cancel invalid pending "
1732 << "pg_upmap_items entry "
1733 << it->first << "->" << it->second
1734 << dendl;
94b18763
FG
1735 pending_inc->new_pg_upmap_items.erase(it);
1736 }
28e407b8
AA
1737 if (osdmap.pg_upmap_items.count(pg)) {
1738 ldout(cct, 10) << __func__ << " cancel invalid "
1739 << "pg_upmap_items entry "
1740 << osdmap.pg_upmap_items.find(pg)->first << "->"
1741 << osdmap.pg_upmap_items.find(pg)->second
1742 << dendl;
1743 pending_inc->old_pg_upmap_items.insert(pg);
94b18763
FG
1744 }
1745 }
1746 }
f64942e4 1747 tmpmap.clean_pg_upmaps(cct, pending_inc);
94b18763
FG
1748}
1749
7c673cae
FG
1750int OSDMap::apply_incremental(const Incremental &inc)
1751{
1752 new_blacklist_entries = false;
1753 if (inc.epoch == 1)
1754 fsid = inc.fsid;
1755 else if (inc.fsid != fsid)
1756 return -EINVAL;
1757
1758 assert(inc.epoch == epoch+1);
1759
1760 epoch++;
1761 modified = inc.modified;
1762
1763 // full map?
1764 if (inc.fullmap.length()) {
1765 bufferlist bl(inc.fullmap);
1766 decode(bl);
1767 return 0;
1768 }
1769
1770 // nope, incremental.
31f18b77 1771 if (inc.new_flags >= 0) {
7c673cae 1772 flags = inc.new_flags;
31f18b77
FG
1773 // the below is just to cover a newly-upgraded luminous mon
1774 // cluster that has to set require_jewel_osds or
1775 // require_kraken_osds before the osds can be upgraded to
1776 // luminous.
1777 if (flags & CEPH_OSDMAP_REQUIRE_KRAKEN) {
1778 if (require_osd_release < CEPH_RELEASE_KRAKEN) {
1779 require_osd_release = CEPH_RELEASE_KRAKEN;
1780 }
1781 } else if (flags & CEPH_OSDMAP_REQUIRE_JEWEL) {
1782 if (require_osd_release < CEPH_RELEASE_JEWEL) {
1783 require_osd_release = CEPH_RELEASE_JEWEL;
1784 }
1785 }
1786 }
7c673cae
FG
1787
1788 if (inc.new_max_osd >= 0)
1789 set_max_osd(inc.new_max_osd);
1790
1791 if (inc.new_pool_max != -1)
1792 pool_max = inc.new_pool_max;
1793
1794 for (const auto &pool : inc.new_pools) {
1795 pools[pool.first] = pool.second;
1796 pools[pool.first].last_change = epoch;
1797 }
1798
1799 for (const auto &pname : inc.new_pool_names) {
1800 auto pool_name_entry = pool_name.find(pname.first);
1801 if (pool_name_entry != pool_name.end()) {
1802 name_pool.erase(pool_name_entry->second);
1803 pool_name_entry->second = pname.second;
1804 } else {
1805 pool_name[pname.first] = pname.second;
1806 }
1807 name_pool[pname.second] = pname.first;
1808 }
1809
1810 for (const auto &pool : inc.old_pools) {
1811 pools.erase(pool);
1812 name_pool.erase(pool_name[pool]);
1813 pool_name.erase(pool);
1814 }
1815
1816 for (const auto &weight : inc.new_weight) {
1817 set_weight(weight.first, weight.second);
1818
1819 // if we are marking in, clear the AUTOOUT and NEW bits, and clear
1820 // xinfo old_weight.
1821 if (weight.second) {
1822 osd_state[weight.first] &= ~(CEPH_OSD_AUTOOUT | CEPH_OSD_NEW);
1823 osd_xinfo[weight.first].old_weight = 0;
1824 }
1825 }
1826
1827 for (const auto &primary_affinity : inc.new_primary_affinity) {
1828 set_primary_affinity(primary_affinity.first, primary_affinity.second);
1829 }
1830
1831 // erasure_code_profiles
1832 for (const auto &profile : inc.old_erasure_code_profiles)
1833 erasure_code_profiles.erase(profile);
1834
1835 for (const auto &profile : inc.new_erasure_code_profiles) {
1836 set_erasure_code_profile(profile.first, profile.second);
1837 }
1838
1839 // up/down
1840 for (const auto &state : inc.new_state) {
1841 const auto osd = state.first;
1842 int s = state.second ? state.second : CEPH_OSD_UP;
1843 if ((osd_state[osd] & CEPH_OSD_UP) &&
1844 (s & CEPH_OSD_UP)) {
1845 osd_info[osd].down_at = epoch;
1846 osd_xinfo[osd].down_stamp = modified;
1847 }
1848 if ((osd_state[osd] & CEPH_OSD_EXISTS) &&
1849 (s & CEPH_OSD_EXISTS)) {
1850 // osd is destroyed; clear out anything interesting.
1851 (*osd_uuid)[osd] = uuid_d();
1852 osd_info[osd] = osd_info_t();
1853 osd_xinfo[osd] = osd_xinfo_t();
1854 set_primary_affinity(osd, CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1855 osd_addrs->client_addr[osd].reset(new entity_addr_t());
1856 osd_addrs->cluster_addr[osd].reset(new entity_addr_t());
1857 osd_addrs->hb_front_addr[osd].reset(new entity_addr_t());
1858 osd_addrs->hb_back_addr[osd].reset(new entity_addr_t());
1859 osd_state[osd] = 0;
1860 } else {
1861 osd_state[osd] ^= s;
1862 }
1863 }
1864
1865 for (const auto &client : inc.new_up_client) {
1866 osd_state[client.first] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
1867 osd_addrs->client_addr[client.first].reset(new entity_addr_t(client.second));
1868 if (inc.new_hb_back_up.empty())
1869 osd_addrs->hb_back_addr[client.first].reset(new entity_addr_t(client.second)); //this is a backward-compatibility hack
1870 else
1871 osd_addrs->hb_back_addr[client.first].reset(
1872 new entity_addr_t(inc.new_hb_back_up.find(client.first)->second));
1873 const auto j = inc.new_hb_front_up.find(client.first);
1874 if (j != inc.new_hb_front_up.end())
1875 osd_addrs->hb_front_addr[client.first].reset(new entity_addr_t(j->second));
1876 else
1877 osd_addrs->hb_front_addr[client.first].reset();
1878
1879 osd_info[client.first].up_from = epoch;
1880 }
1881
1882 for (const auto &cluster : inc.new_up_cluster)
1883 osd_addrs->cluster_addr[cluster.first].reset(new entity_addr_t(cluster.second));
1884
1885 // info
1886 for (const auto &thru : inc.new_up_thru)
1887 osd_info[thru.first].up_thru = thru.second;
1888
1889 for (const auto &interval : inc.new_last_clean_interval) {
1890 osd_info[interval.first].last_clean_begin = interval.second.first;
1891 osd_info[interval.first].last_clean_end = interval.second.second;
1892 }
1893
1894 for (const auto &lost : inc.new_lost)
1895 osd_info[lost.first].lost_at = lost.second;
1896
1897 // xinfo
1898 for (const auto &xinfo : inc.new_xinfo)
1899 osd_xinfo[xinfo.first] = xinfo.second;
1900
1901 // uuid
1902 for (const auto &uuid : inc.new_uuid)
1903 (*osd_uuid)[uuid.first] = uuid.second;
1904
1905 // pg rebuild
1906 for (const auto &pg : inc.new_pg_temp) {
1907 if (pg.second.empty())
1908 pg_temp->erase(pg.first);
1909 else
31f18b77
FG
1910 pg_temp->set(pg.first, pg.second);
1911 }
1912 if (!inc.new_pg_temp.empty()) {
1913 // make sure pg_temp is efficiently stored
1914 pg_temp->rebuild();
7c673cae
FG
1915 }
1916
1917 for (const auto &pg : inc.new_primary_temp) {
1918 if (pg.second == -1)
1919 primary_temp->erase(pg.first);
1920 else
1921 (*primary_temp)[pg.first] = pg.second;
1922 }
1923
1924 for (auto& p : inc.new_pg_upmap) {
1925 pg_upmap[p.first] = p.second;
1926 }
1927 for (auto& pg : inc.old_pg_upmap) {
1928 pg_upmap.erase(pg);
1929 }
1930 for (auto& p : inc.new_pg_upmap_items) {
1931 pg_upmap_items[p.first] = p.second;
1932 }
1933 for (auto& pg : inc.old_pg_upmap_items) {
1934 pg_upmap_items.erase(pg);
1935 }
1936
1937 // blacklist
1938 if (!inc.new_blacklist.empty()) {
1939 blacklist.insert(inc.new_blacklist.begin(),inc.new_blacklist.end());
1940 new_blacklist_entries = true;
1941 }
1942 for (const auto &addr : inc.old_blacklist)
1943 blacklist.erase(addr);
1944
1945 // cluster snapshot?
1946 if (inc.cluster_snapshot.length()) {
1947 cluster_snapshot = inc.cluster_snapshot;
1948 cluster_snapshot_epoch = inc.epoch;
1949 } else {
1950 cluster_snapshot.clear();
1951 cluster_snapshot_epoch = 0;
1952 }
1953
1954 if (inc.new_nearfull_ratio >= 0) {
1955 nearfull_ratio = inc.new_nearfull_ratio;
1956 }
1957 if (inc.new_backfillfull_ratio >= 0) {
1958 backfillfull_ratio = inc.new_backfillfull_ratio;
1959 }
1960 if (inc.new_full_ratio >= 0) {
1961 full_ratio = inc.new_full_ratio;
1962 }
31f18b77 1963 if (inc.new_require_min_compat_client > 0) {
7c673cae
FG
1964 require_min_compat_client = inc.new_require_min_compat_client;
1965 }
31f18b77
FG
1966 if (inc.new_require_osd_release >= 0) {
1967 require_osd_release = inc.new_require_osd_release;
1968 if (require_osd_release >= CEPH_RELEASE_LUMINOUS) {
1969 flags &= ~(CEPH_OSDMAP_LEGACY_REQUIRE_FLAGS);
c07f9fc5 1970 flags |= CEPH_OSDMAP_RECOVERY_DELETES;
31f18b77
FG
1971 }
1972 }
7c673cae
FG
1973
1974 // do new crush map last (after up/down stuff)
1975 if (inc.crush.length()) {
1976 bufferlist bl(inc.crush);
1977 auto blp = bl.begin();
1978 crush.reset(new CrushWrapper);
1979 crush->decode(blp);
31f18b77
FG
1980 if (require_osd_release >= CEPH_RELEASE_LUMINOUS) {
1981 // only increment if this is a luminous-encoded osdmap, lest
1982 // the mon's crush_version diverge from what the osds or others
1983 // are decoding and applying on their end. if we won't encode
1984 // it in the canonical version, don't change it.
1985 ++crush_version;
1986 }
7c673cae
FG
1987 }
1988
1989 calc_num_osds();
1990 _calc_up_osd_features();
1991 return 0;
1992}
1993
1994// mapping
1995int OSDMap::map_to_pg(
1996 int64_t poolid,
1997 const string& name,
1998 const string& key,
1999 const string& nspace,
2000 pg_t *pg) const
2001{
2002 // calculate ps (placement seed)
2003 const pg_pool_t *pool = get_pg_pool(poolid);
2004 if (!pool)
2005 return -ENOENT;
2006 ps_t ps;
2007 if (!key.empty())
2008 ps = pool->hash_key(key, nspace);
2009 else
2010 ps = pool->hash_key(name, nspace);
2011 *pg = pg_t(ps, poolid);
2012 return 0;
2013}
2014
2015int OSDMap::object_locator_to_pg(
2016 const object_t& oid, const object_locator_t& loc, pg_t &pg) const
2017{
2018 if (loc.hash >= 0) {
2019 if (!get_pg_pool(loc.get_pool())) {
2020 return -ENOENT;
2021 }
2022 pg = pg_t(loc.hash, loc.get_pool());
2023 return 0;
2024 }
2025 return map_to_pg(loc.get_pool(), oid.name, loc.key, loc.nspace, &pg);
2026}
2027
2028ceph_object_layout OSDMap::make_object_layout(
2029 object_t oid, int pg_pool, string nspace) const
2030{
2031 object_locator_t loc(pg_pool, nspace);
2032
2033 ceph_object_layout ol;
2034 pg_t pgid = object_locator_to_pg(oid, loc);
2035 ol.ol_pgid = pgid.get_old_pg().v;
2036 ol.ol_stripe_unit = 0;
2037 return ol;
2038}
2039
2040void OSDMap::_remove_nonexistent_osds(const pg_pool_t& pool,
2041 vector<int>& osds) const
2042{
2043 if (pool.can_shift_osds()) {
2044 unsigned removed = 0;
2045 for (unsigned i = 0; i < osds.size(); i++) {
2046 if (!exists(osds[i])) {
2047 removed++;
2048 continue;
2049 }
2050 if (removed) {
2051 osds[i - removed] = osds[i];
2052 }
2053 }
2054 if (removed)
2055 osds.resize(osds.size() - removed);
2056 } else {
2057 for (auto& osd : osds) {
2058 if (!exists(osd))
2059 osd = CRUSH_ITEM_NONE;
2060 }
2061 }
2062}
2063
31f18b77 2064void OSDMap::_pg_to_raw_osds(
7c673cae
FG
2065 const pg_pool_t& pool, pg_t pg,
2066 vector<int> *osds,
2067 ps_t *ppps) const
2068{
2069 // map to osds[]
2070 ps_t pps = pool.raw_pg_to_pps(pg); // placement ps
2071 unsigned size = pool.get_size();
2072
2073 // what crush rule?
31f18b77 2074 int ruleno = crush->find_rule(pool.get_crush_rule(), pool.get_type(), size);
7c673cae
FG
2075 if (ruleno >= 0)
2076 crush->do_rule(ruleno, pps, *osds, size, osd_weight, pg.pool());
2077
2078 _remove_nonexistent_osds(pool, *osds);
2079
2080 if (ppps)
2081 *ppps = pps;
7c673cae
FG
2082}
2083
2084int OSDMap::_pick_primary(const vector<int>& osds) const
2085{
2086 for (auto osd : osds) {
2087 if (osd != CRUSH_ITEM_NONE) {
2088 return osd;
2089 }
2090 }
2091 return -1;
2092}
2093
224ce89b 2094void OSDMap::_apply_upmap(const pg_pool_t& pi, pg_t raw_pg, vector<int> *raw) const
7c673cae
FG
2095{
2096 pg_t pg = pi.raw_pg_to_pg(raw_pg);
2097 auto p = pg_upmap.find(pg);
2098 if (p != pg_upmap.end()) {
2099 // make sure targets aren't marked out
2100 for (auto osd : p->second) {
91327a77
AA
2101 if (osd != CRUSH_ITEM_NONE && osd < max_osd && osd >= 0 &&
2102 osd_weight[osd] == 0) {
7c673cae
FG
2103 // reject/ignore the explicit mapping
2104 return;
2105 }
2106 }
2107 *raw = vector<int>(p->second.begin(), p->second.end());
224ce89b 2108 // continue to check and apply pg_upmap_items if any
7c673cae
FG
2109 }
2110
2111 auto q = pg_upmap_items.find(pg);
2112 if (q != pg_upmap_items.end()) {
181888fb
FG
2113 // NOTE: this approach does not allow a bidirectional swap,
2114 // e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1].
2115 for (auto& r : q->second) {
2116 // make sure the replacement value doesn't already appear
2117 bool exists = false;
2118 ssize_t pos = -1;
2119 for (unsigned i = 0; i < raw->size(); ++i) {
2120 int osd = (*raw)[i];
2121 if (osd == r.second) {
2122 exists = true;
2123 break;
2124 }
2125 // ignore mapping if target is marked out (or invalid osd id)
2126 if (osd == r.first &&
2127 pos < 0 &&
2128 !(r.second != CRUSH_ITEM_NONE && r.second < max_osd &&
91327a77 2129 r.second >= 0 && osd_weight[r.second] == 0)) {
181888fb
FG
2130 pos = i;
2131 }
2132 }
2133 if (!exists && pos >= 0) {
2134 (*raw)[pos] = r.second;
7c673cae
FG
2135 }
2136 }
2137 }
2138}
2139
2140// pg -> (up osd list)
2141void OSDMap::_raw_to_up_osds(const pg_pool_t& pool, const vector<int>& raw,
2142 vector<int> *up) const
2143{
2144 if (pool.can_shift_osds()) {
2145 // shift left
2146 up->clear();
2147 up->reserve(raw.size());
2148 for (unsigned i=0; i<raw.size(); i++) {
2149 if (!exists(raw[i]) || is_down(raw[i]))
2150 continue;
2151 up->push_back(raw[i]);
2152 }
2153 } else {
2154 // set down/dne devices to NONE
2155 up->resize(raw.size());
2156 for (int i = raw.size() - 1; i >= 0; --i) {
2157 if (!exists(raw[i]) || is_down(raw[i])) {
2158 (*up)[i] = CRUSH_ITEM_NONE;
2159 } else {
2160 (*up)[i] = raw[i];
2161 }
2162 }
2163 }
2164}
2165
2166void OSDMap::_apply_primary_affinity(ps_t seed,
2167 const pg_pool_t& pool,
2168 vector<int> *osds,
2169 int *primary) const
2170{
2171 // do we have any non-default primary_affinity values for these osds?
2172 if (!osd_primary_affinity)
2173 return;
2174
2175 bool any = false;
2176 for (const auto osd : *osds) {
2177 if (osd != CRUSH_ITEM_NONE &&
2178 (*osd_primary_affinity)[osd] != CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2179 any = true;
2180 break;
2181 }
2182 }
2183 if (!any)
2184 return;
2185
2186 // pick the primary. feed both the seed (for the pg) and the osd
2187 // into the hash/rng so that a proportional fraction of an osd's pgs
2188 // get rejected as primary.
2189 int pos = -1;
2190 for (unsigned i = 0; i < osds->size(); ++i) {
2191 int o = (*osds)[i];
2192 if (o == CRUSH_ITEM_NONE)
2193 continue;
2194 unsigned a = (*osd_primary_affinity)[o];
2195 if (a < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2196 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2197 seed, o) >> 16) >= a) {
2198 // we chose not to use this primary. note it anyway as a
2199 // fallback in case we don't pick anyone else, but keep looking.
2200 if (pos < 0)
2201 pos = i;
2202 } else {
2203 pos = i;
2204 break;
2205 }
2206 }
2207 if (pos < 0)
2208 return;
2209
2210 *primary = (*osds)[pos];
2211
2212 if (pool.can_shift_osds() && pos > 0) {
2213 // move the new primary to the front.
2214 for (int i = pos; i > 0; --i) {
2215 (*osds)[i] = (*osds)[i-1];
2216 }
2217 (*osds)[0] = *primary;
2218 }
2219}
2220
2221void OSDMap::_get_temp_osds(const pg_pool_t& pool, pg_t pg,
2222 vector<int> *temp_pg, int *temp_primary) const
2223{
2224 pg = pool.raw_pg_to_pg(pg);
2225 const auto p = pg_temp->find(pg);
2226 temp_pg->clear();
2227 if (p != pg_temp->end()) {
2228 for (unsigned i=0; i<p->second.size(); i++) {
2229 if (!exists(p->second[i]) || is_down(p->second[i])) {
2230 if (pool.can_shift_osds()) {
2231 continue;
2232 } else {
2233 temp_pg->push_back(CRUSH_ITEM_NONE);
2234 }
2235 } else {
2236 temp_pg->push_back(p->second[i]);
2237 }
2238 }
2239 }
2240 const auto &pp = primary_temp->find(pg);
2241 *temp_primary = -1;
2242 if (pp != primary_temp->end()) {
2243 *temp_primary = pp->second;
2244 } else if (!temp_pg->empty()) { // apply pg_temp's primary
2245 for (unsigned i = 0; i < temp_pg->size(); ++i) {
2246 if ((*temp_pg)[i] != CRUSH_ITEM_NONE) {
2247 *temp_primary = (*temp_pg)[i];
2248 break;
2249 }
2250 }
2251 }
2252}
2253
31f18b77 2254void OSDMap::pg_to_raw_osds(pg_t pg, vector<int> *raw, int *primary) const
7c673cae
FG
2255{
2256 *primary = -1;
2257 raw->clear();
2258 const pg_pool_t *pool = get_pg_pool(pg.pool());
2259 if (!pool)
31f18b77
FG
2260 return;
2261 _pg_to_raw_osds(*pool, pg, raw, NULL);
7c673cae
FG
2262 if (primary)
2263 *primary = _pick_primary(*raw);
7c673cae
FG
2264}
2265
2266void OSDMap::pg_to_raw_up(pg_t pg, vector<int> *up, int *primary) const
2267{
2268 const pg_pool_t *pool = get_pg_pool(pg.pool());
2269 if (!pool) {
2270 if (primary)
2271 *primary = -1;
2272 if (up)
2273 up->clear();
2274 return;
2275 }
2276 vector<int> raw;
2277 ps_t pps;
2278 _pg_to_raw_osds(*pool, pg, &raw, &pps);
224ce89b 2279 _apply_upmap(*pool, pg, &raw);
7c673cae
FG
2280 _raw_to_up_osds(*pool, raw, up);
2281 *primary = _pick_primary(raw);
2282 _apply_primary_affinity(pps, *pool, up, primary);
2283}
31f18b77 2284
7c673cae
FG
2285void OSDMap::_pg_to_up_acting_osds(
2286 const pg_t& pg, vector<int> *up, int *up_primary,
2287 vector<int> *acting, int *acting_primary,
2288 bool raw_pg_to_pg) const
2289{
2290 const pg_pool_t *pool = get_pg_pool(pg.pool());
2291 if (!pool ||
2292 (!raw_pg_to_pg && pg.ps() >= pool->get_pg_num())) {
2293 if (up)
2294 up->clear();
2295 if (up_primary)
2296 *up_primary = -1;
2297 if (acting)
2298 acting->clear();
2299 if (acting_primary)
2300 *acting_primary = -1;
2301 return;
2302 }
2303 vector<int> raw;
2304 vector<int> _up;
2305 vector<int> _acting;
2306 int _up_primary;
2307 int _acting_primary;
2308 ps_t pps;
2309 _get_temp_osds(*pool, pg, &_acting, &_acting_primary);
2310 if (_acting.empty() || up || up_primary) {
2311 _pg_to_raw_osds(*pool, pg, &raw, &pps);
224ce89b 2312 _apply_upmap(*pool, pg, &raw);
7c673cae
FG
2313 _raw_to_up_osds(*pool, raw, &_up);
2314 _up_primary = _pick_primary(_up);
2315 _apply_primary_affinity(pps, *pool, &_up, &_up_primary);
2316 if (_acting.empty()) {
2317 _acting = _up;
2318 if (_acting_primary == -1) {
2319 _acting_primary = _up_primary;
2320 }
2321 }
2322
2323 if (up)
2324 up->swap(_up);
2325 if (up_primary)
2326 *up_primary = _up_primary;
2327 }
2328
2329 if (acting)
2330 acting->swap(_acting);
2331 if (acting_primary)
2332 *acting_primary = _acting_primary;
2333}
2334
2335int OSDMap::calc_pg_rank(int osd, const vector<int>& acting, int nrep)
2336{
2337 if (!nrep)
2338 nrep = acting.size();
2339 for (int i=0; i<nrep; i++)
2340 if (acting[i] == osd)
2341 return i;
2342 return -1;
2343}
2344
2345int OSDMap::calc_pg_role(int osd, const vector<int>& acting, int nrep)
2346{
2347 return calc_pg_rank(osd, acting, nrep);
2348}
2349
2350bool OSDMap::primary_changed(
2351 int oldprimary,
2352 const vector<int> &oldacting,
2353 int newprimary,
2354 const vector<int> &newacting)
2355{
2356 if (oldacting.empty() && newacting.empty())
2357 return false; // both still empty
2358 if (oldacting.empty() ^ newacting.empty())
2359 return true; // was empty, now not, or vice versa
2360 if (oldprimary != newprimary)
2361 return true; // primary changed
2362 if (calc_pg_rank(oldprimary, oldacting) !=
2363 calc_pg_rank(newprimary, newacting))
2364 return true;
2365 return false; // same primary (tho replicas may have changed)
2366}
2367
28e407b8
AA
2368uint64_t OSDMap::get_encoding_features() const
2369{
2370 uint64_t f = SIGNIFICANT_FEATURES;
2371 if (require_osd_release < CEPH_RELEASE_LUMINOUS) {
2372 f &= ~(CEPH_FEATURE_SERVER_LUMINOUS |
2373 CEPH_FEATURE_CRUSH_CHOOSE_ARGS);
2374 }
2375 if (require_osd_release < CEPH_RELEASE_KRAKEN) {
2376 f &= ~(CEPH_FEATURE_SERVER_KRAKEN |
1adf2230 2377 CEPH_FEATURE_MSG_ADDR2);
28e407b8
AA
2378 }
2379 if (require_osd_release < CEPH_RELEASE_JEWEL) {
2380 f &= ~(CEPH_FEATURE_SERVER_JEWEL |
1adf2230
AA
2381 CEPH_FEATURE_NEW_OSDOP_ENCODING |
2382 CEPH_FEATURE_CRUSH_TUNABLES5);
28e407b8
AA
2383 }
2384 return f;
2385}
7c673cae
FG
2386
2387// serialize, unserialize
2388void OSDMap::encode_client_old(bufferlist& bl) const
2389{
2390 __u16 v = 5;
2391 ::encode(v, bl);
2392
2393 // base
2394 ::encode(fsid, bl);
2395 ::encode(epoch, bl);
2396 ::encode(created, bl);
2397 ::encode(modified, bl);
2398
2399 // for ::encode(pools, bl);
2400 __u32 n = pools.size();
2401 ::encode(n, bl);
2402
2403 for (const auto &pool : pools) {
2404 n = pool.first;
2405 ::encode(n, bl);
2406 ::encode(pool.second, bl, 0);
2407 }
2408 // for ::encode(pool_name, bl);
2409 n = pool_name.size();
2410 ::encode(n, bl);
2411 for (const auto &pname : pool_name) {
2412 n = pname.first;
2413 ::encode(n, bl);
2414 ::encode(pname.second, bl);
2415 }
2416 // for ::encode(pool_max, bl);
2417 n = pool_max;
2418 ::encode(n, bl);
2419
2420 ::encode(flags, bl);
2421
2422 ::encode(max_osd, bl);
31f18b77
FG
2423 {
2424 uint32_t n = osd_state.size();
2425 ::encode(n, bl);
2426 for (auto s : osd_state) {
2427 ::encode((uint8_t)s, bl);
2428 }
2429 }
7c673cae
FG
2430 ::encode(osd_weight, bl);
2431 ::encode(osd_addrs->client_addr, bl, 0);
2432
2433 // for ::encode(pg_temp, bl);
2434 n = pg_temp->size();
2435 ::encode(n, bl);
2436 for (const auto pg : *pg_temp) {
2437 old_pg_t opg = pg.first.get_old_pg();
2438 ::encode(opg, bl);
2439 ::encode(pg.second, bl);
2440 }
2441
2442 // crush
2443 bufferlist cbl;
2444 crush->encode(cbl, 0 /* legacy (no) features */);
2445 ::encode(cbl, bl);
2446}
2447
2448void OSDMap::encode_classic(bufferlist& bl, uint64_t features) const
2449{
2450 if ((features & CEPH_FEATURE_PGID64) == 0) {
2451 encode_client_old(bl);
2452 return;
2453 }
2454
2455 __u16 v = 6;
2456 ::encode(v, bl);
2457
2458 // base
2459 ::encode(fsid, bl);
2460 ::encode(epoch, bl);
2461 ::encode(created, bl);
2462 ::encode(modified, bl);
2463
2464 ::encode(pools, bl, features);
2465 ::encode(pool_name, bl);
2466 ::encode(pool_max, bl);
2467
2468 ::encode(flags, bl);
2469
2470 ::encode(max_osd, bl);
31f18b77
FG
2471 {
2472 uint32_t n = osd_state.size();
2473 ::encode(n, bl);
2474 for (auto s : osd_state) {
2475 ::encode((uint8_t)s, bl);
2476 }
2477 }
7c673cae
FG
2478 ::encode(osd_weight, bl);
2479 ::encode(osd_addrs->client_addr, bl, features);
2480
2481 ::encode(*pg_temp, bl);
2482
2483 // crush
2484 bufferlist cbl;
2485 crush->encode(cbl, 0 /* legacy (no) features */);
2486 ::encode(cbl, bl);
2487
2488 // extended
2489 __u16 ev = 10;
2490 ::encode(ev, bl);
2491 ::encode(osd_addrs->hb_back_addr, bl, features);
2492 ::encode(osd_info, bl);
2493 ::encode(blacklist, bl, features);
2494 ::encode(osd_addrs->cluster_addr, bl, features);
2495 ::encode(cluster_snapshot_epoch, bl);
2496 ::encode(cluster_snapshot, bl);
2497 ::encode(*osd_uuid, bl);
2498 ::encode(osd_xinfo, bl);
2499 ::encode(osd_addrs->hb_front_addr, bl, features);
2500}
2501
2502void OSDMap::encode(bufferlist& bl, uint64_t features) const
2503{
2504 if ((features & CEPH_FEATURE_OSDMAP_ENC) == 0) {
2505 encode_classic(bl, features);
2506 return;
2507 }
2508
2509 // only a select set of callers should *ever* be encoding new
2510 // OSDMaps. others should be passing around the canonical encoded
2511 // buffers from on high. select out those callers by passing in an
2512 // "impossible" feature bit.
2513 assert(features & CEPH_FEATURE_RESERVED);
2514 features &= ~CEPH_FEATURE_RESERVED;
2515
2516 size_t start_offset = bl.length();
2517 size_t tail_offset;
2518 buffer::list::iterator crc_it;
2519
2520 // meta-encoding: how we include client-used and osd-specific data
2521 ENCODE_START(8, 7, bl);
2522
2523 {
28e407b8
AA
2524 // NOTE: any new encoding dependencies must be reflected by
2525 // SIGNIFICANT_FEATURES
31f18b77
FG
2526 uint8_t v = 6;
2527 if (!HAVE_FEATURE(features, SERVER_LUMINOUS)) {
7c673cae
FG
2528 v = 3;
2529 }
2530 ENCODE_START(v, 1, bl); // client-usable data
2531 // base
2532 ::encode(fsid, bl);
2533 ::encode(epoch, bl);
2534 ::encode(created, bl);
2535 ::encode(modified, bl);
2536
2537 ::encode(pools, bl, features);
2538 ::encode(pool_name, bl);
2539 ::encode(pool_max, bl);
2540
31f18b77
FG
2541 if (v < 4) {
2542 decltype(flags) f = flags;
2543 if (require_osd_release >= CEPH_RELEASE_LUMINOUS)
c07f9fc5 2544 f |= CEPH_OSDMAP_REQUIRE_LUMINOUS | CEPH_OSDMAP_RECOVERY_DELETES;
31f18b77
FG
2545 else if (require_osd_release == CEPH_RELEASE_KRAKEN)
2546 f |= CEPH_OSDMAP_REQUIRE_KRAKEN;
2547 else if (require_osd_release == CEPH_RELEASE_JEWEL)
2548 f |= CEPH_OSDMAP_REQUIRE_JEWEL;
2549 ::encode(f, bl);
2550 } else {
2551 ::encode(flags, bl);
2552 }
7c673cae
FG
2553
2554 ::encode(max_osd, bl);
31f18b77
FG
2555 if (v >= 5) {
2556 ::encode(osd_state, bl);
2557 } else {
2558 uint32_t n = osd_state.size();
2559 ::encode(n, bl);
2560 for (auto s : osd_state) {
2561 ::encode((uint8_t)s, bl);
2562 }
2563 }
7c673cae
FG
2564 ::encode(osd_weight, bl);
2565 ::encode(osd_addrs->client_addr, bl, features);
2566
2567 ::encode(*pg_temp, bl);
2568 ::encode(*primary_temp, bl);
2569 if (osd_primary_affinity) {
2570 ::encode(*osd_primary_affinity, bl);
2571 } else {
2572 vector<__u32> v;
2573 ::encode(v, bl);
2574 }
2575
2576 // crush
2577 bufferlist cbl;
2578 crush->encode(cbl, features);
2579 ::encode(cbl, bl);
2580 ::encode(erasure_code_profiles, bl);
2581
2582 if (v >= 4) {
2583 ::encode(pg_upmap, bl);
2584 ::encode(pg_upmap_items, bl);
2585 } else {
2586 assert(pg_upmap.empty());
2587 assert(pg_upmap_items.empty());
2588 }
31f18b77
FG
2589 if (v >= 6) {
2590 ::encode(crush_version, bl);
2591 }
7c673cae
FG
2592 ENCODE_FINISH(bl); // client-usable data
2593 }
2594
2595 {
28e407b8
AA
2596 // NOTE: any new encoding dependencies must be reflected by
2597 // SIGNIFICANT_FEATURES
31f18b77 2598 uint8_t target_v = 5;
7c673cae
FG
2599 if (!HAVE_FEATURE(features, SERVER_LUMINOUS)) {
2600 target_v = 1;
2601 }
2602 ENCODE_START(target_v, 1, bl); // extended, osd-only data
2603 ::encode(osd_addrs->hb_back_addr, bl, features);
2604 ::encode(osd_info, bl);
2605 {
2606 // put this in a sorted, ordered map<> so that we encode in a
2607 // deterministic order.
2608 map<entity_addr_t,utime_t> blacklist_map;
2609 for (const auto &addr : blacklist)
2610 blacklist_map.insert(make_pair(addr.first, addr.second));
2611 ::encode(blacklist_map, bl, features);
2612 }
2613 ::encode(osd_addrs->cluster_addr, bl, features);
2614 ::encode(cluster_snapshot_epoch, bl);
2615 ::encode(cluster_snapshot, bl);
2616 ::encode(*osd_uuid, bl);
2617 ::encode(osd_xinfo, bl);
2618 ::encode(osd_addrs->hb_front_addr, bl, features);
2619 if (target_v >= 2) {
2620 ::encode(nearfull_ratio, bl);
2621 ::encode(full_ratio, bl);
2622 ::encode(backfillfull_ratio, bl);
31f18b77
FG
2623 }
2624 // 4 was string-based new_require_min_compat_client
2625 if (target_v >= 5) {
7c673cae 2626 ::encode(require_min_compat_client, bl);
31f18b77 2627 ::encode(require_osd_release, bl);
7c673cae
FG
2628 }
2629 ENCODE_FINISH(bl); // osd-only data
2630 }
2631
2632 ::encode((uint32_t)0, bl); // dummy crc
2633 crc_it = bl.end();
2634 crc_it.advance(-4);
2635 tail_offset = bl.length();
2636
2637 ENCODE_FINISH(bl); // meta-encoding wrapper
2638
2639 // fill in crc
2640 bufferlist front;
2641 front.substr_of(bl, start_offset, crc_it.get_off() - start_offset);
2642 crc = front.crc32c(-1);
2643 if (tail_offset < bl.length()) {
2644 bufferlist tail;
2645 tail.substr_of(bl, tail_offset, bl.length() - tail_offset);
2646 crc = tail.crc32c(crc);
2647 }
2648 ceph_le32 crc_le;
2649 crc_le = crc;
2650 crc_it.copy_in(4, (char*)&crc_le);
2651 crc_defined = true;
2652}
2653
2654void OSDMap::decode(bufferlist& bl)
2655{
2656 auto p = bl.begin();
2657 decode(p);
2658}
2659
2660void OSDMap::decode_classic(bufferlist::iterator& p)
2661{
2662 __u32 n, t;
2663 __u16 v;
2664 ::decode(v, p);
2665
2666 // base
2667 ::decode(fsid, p);
2668 ::decode(epoch, p);
2669 ::decode(created, p);
2670 ::decode(modified, p);
2671
2672 if (v < 6) {
2673 if (v < 4) {
2674 int32_t max_pools = 0;
2675 ::decode(max_pools, p);
2676 pool_max = max_pools;
2677 }
2678 pools.clear();
2679 ::decode(n, p);
2680 while (n--) {
2681 ::decode(t, p);
2682 ::decode(pools[t], p);
2683 }
2684 if (v == 4) {
2685 ::decode(n, p);
2686 pool_max = n;
2687 } else if (v == 5) {
2688 pool_name.clear();
2689 ::decode(n, p);
2690 while (n--) {
2691 ::decode(t, p);
2692 ::decode(pool_name[t], p);
2693 }
2694 ::decode(n, p);
2695 pool_max = n;
2696 }
2697 } else {
2698 ::decode(pools, p);
2699 ::decode(pool_name, p);
2700 ::decode(pool_max, p);
2701 }
2702 // kludge around some old bug that zeroed out pool_max (#2307)
2703 if (pools.size() && pool_max < pools.rbegin()->first) {
2704 pool_max = pools.rbegin()->first;
2705 }
2706
2707 ::decode(flags, p);
2708
2709 ::decode(max_osd, p);
31f18b77
FG
2710 {
2711 vector<uint8_t> os;
2712 ::decode(os, p);
2713 osd_state.resize(os.size());
2714 for (unsigned i = 0; i < os.size(); ++i) {
2715 osd_state[i] = os[i];
2716 }
2717 }
7c673cae
FG
2718 ::decode(osd_weight, p);
2719 ::decode(osd_addrs->client_addr, p);
2720 if (v <= 5) {
2721 pg_temp->clear();
2722 ::decode(n, p);
2723 while (n--) {
2724 old_pg_t opg;
2725 ::decode_raw(opg, p);
31f18b77
FG
2726 mempool::osdmap::vector<int32_t> v;
2727 ::decode(v, p);
2728 pg_temp->set(pg_t(opg), v);
7c673cae
FG
2729 }
2730 } else {
2731 ::decode(*pg_temp, p);
2732 }
2733
2734 // crush
2735 bufferlist cbl;
2736 ::decode(cbl, p);
2737 auto cblp = cbl.begin();
2738 crush->decode(cblp);
2739
2740 // extended
2741 __u16 ev = 0;
2742 if (v >= 5)
2743 ::decode(ev, p);
2744 ::decode(osd_addrs->hb_back_addr, p);
2745 ::decode(osd_info, p);
2746 if (v < 5)
2747 ::decode(pool_name, p);
2748
2749 ::decode(blacklist, p);
2750 if (ev >= 6)
2751 ::decode(osd_addrs->cluster_addr, p);
2752 else
2753 osd_addrs->cluster_addr.resize(osd_addrs->client_addr.size());
2754
2755 if (ev >= 7) {
2756 ::decode(cluster_snapshot_epoch, p);
2757 ::decode(cluster_snapshot, p);
2758 }
2759
2760 if (ev >= 8) {
2761 ::decode(*osd_uuid, p);
2762 } else {
2763 osd_uuid->resize(max_osd);
2764 }
2765 if (ev >= 9)
2766 ::decode(osd_xinfo, p);
2767 else
2768 osd_xinfo.resize(max_osd);
2769
2770 if (ev >= 10)
2771 ::decode(osd_addrs->hb_front_addr, p);
2772 else
2773 osd_addrs->hb_front_addr.resize(osd_addrs->hb_back_addr.size());
2774
2775 osd_primary_affinity.reset();
2776
2777 post_decode();
2778}
2779
2780void OSDMap::decode(bufferlist::iterator& bl)
2781{
2782 /**
2783 * Older encodings of the OSDMap had a single struct_v which
2784 * covered the whole encoding, and was prior to our modern
2785 * stuff which includes a compatv and a size. So if we see
2786 * a struct_v < 7, we must rewind to the beginning and use our
2787 * classic decoder.
2788 */
2789 size_t start_offset = bl.get_off();
2790 size_t tail_offset = 0;
2791 bufferlist crc_front, crc_tail;
2792
2793 DECODE_START_LEGACY_COMPAT_LEN(8, 7, 7, bl); // wrapper
2794 if (struct_v < 7) {
2795 int struct_v_size = sizeof(struct_v);
2796 bl.advance(-struct_v_size);
2797 decode_classic(bl);
2798 return;
2799 }
2800 /**
2801 * Since we made it past that hurdle, we can use our normal paths.
2802 */
2803 {
31f18b77 2804 DECODE_START(6, bl); // client-usable data
7c673cae
FG
2805 // base
2806 ::decode(fsid, bl);
2807 ::decode(epoch, bl);
2808 ::decode(created, bl);
2809 ::decode(modified, bl);
2810
2811 ::decode(pools, bl);
2812 ::decode(pool_name, bl);
2813 ::decode(pool_max, bl);
2814
2815 ::decode(flags, bl);
2816
2817 ::decode(max_osd, bl);
31f18b77
FG
2818 if (struct_v >= 5) {
2819 ::decode(osd_state, bl);
2820 } else {
2821 vector<uint8_t> os;
2822 ::decode(os, bl);
2823 osd_state.resize(os.size());
2824 for (unsigned i = 0; i < os.size(); ++i) {
2825 osd_state[i] = os[i];
2826 }
2827 }
7c673cae
FG
2828 ::decode(osd_weight, bl);
2829 ::decode(osd_addrs->client_addr, bl);
2830
2831 ::decode(*pg_temp, bl);
2832 ::decode(*primary_temp, bl);
2833 if (struct_v >= 2) {
2834 osd_primary_affinity.reset(new mempool::osdmap::vector<__u32>);
2835 ::decode(*osd_primary_affinity, bl);
2836 if (osd_primary_affinity->empty())
2837 osd_primary_affinity.reset();
2838 } else {
2839 osd_primary_affinity.reset();
2840 }
2841
2842 // crush
2843 bufferlist cbl;
2844 ::decode(cbl, bl);
2845 auto cblp = cbl.begin();
2846 crush->decode(cblp);
2847 if (struct_v >= 3) {
2848 ::decode(erasure_code_profiles, bl);
2849 } else {
2850 erasure_code_profiles.clear();
2851 }
2852 if (struct_v >= 4) {
2853 ::decode(pg_upmap, bl);
2854 ::decode(pg_upmap_items, bl);
2855 } else {
2856 pg_upmap.clear();
2857 pg_upmap_items.clear();
2858 }
31f18b77
FG
2859 if (struct_v >= 6) {
2860 ::decode(crush_version, bl);
2861 }
7c673cae
FG
2862 DECODE_FINISH(bl); // client-usable data
2863 }
2864
2865 {
31f18b77 2866 DECODE_START(5, bl); // extended, osd-only data
7c673cae
FG
2867 ::decode(osd_addrs->hb_back_addr, bl);
2868 ::decode(osd_info, bl);
2869 ::decode(blacklist, bl);
2870 ::decode(osd_addrs->cluster_addr, bl);
2871 ::decode(cluster_snapshot_epoch, bl);
2872 ::decode(cluster_snapshot, bl);
2873 ::decode(*osd_uuid, bl);
2874 ::decode(osd_xinfo, bl);
2875 ::decode(osd_addrs->hb_front_addr, bl);
2876 if (struct_v >= 2) {
2877 ::decode(nearfull_ratio, bl);
2878 ::decode(full_ratio, bl);
2879 } else {
2880 nearfull_ratio = 0;
2881 full_ratio = 0;
2882 }
2883 if (struct_v >= 3) {
2884 ::decode(backfillfull_ratio, bl);
2885 } else {
2886 backfillfull_ratio = 0;
2887 }
31f18b77
FG
2888 if (struct_v == 4) {
2889 string r;
2890 ::decode(r, bl);
2891 if (r.length())
2892 require_min_compat_client = ceph_release_from_name(r.c_str());
2893 }
2894 if (struct_v >= 5) {
7c673cae 2895 ::decode(require_min_compat_client, bl);
31f18b77
FG
2896 ::decode(require_osd_release, bl);
2897 if (require_osd_release >= CEPH_RELEASE_LUMINOUS) {
2898 flags &= ~(CEPH_OSDMAP_LEGACY_REQUIRE_FLAGS);
c07f9fc5 2899 flags |= CEPH_OSDMAP_RECOVERY_DELETES;
31f18b77
FG
2900 }
2901 } else {
2902 if (flags & CEPH_OSDMAP_REQUIRE_LUMINOUS) {
2903 // only for compat with post-kraken pre-luminous test clusters
2904 require_osd_release = CEPH_RELEASE_LUMINOUS;
2905 flags &= ~(CEPH_OSDMAP_LEGACY_REQUIRE_FLAGS);
c07f9fc5 2906 flags |= CEPH_OSDMAP_RECOVERY_DELETES;
31f18b77
FG
2907 } else if (flags & CEPH_OSDMAP_REQUIRE_KRAKEN) {
2908 require_osd_release = CEPH_RELEASE_KRAKEN;
2909 } else if (flags & CEPH_OSDMAP_REQUIRE_JEWEL) {
2910 require_osd_release = CEPH_RELEASE_JEWEL;
2911 } else {
2912 require_osd_release = 0;
2913 }
2914 }
7c673cae
FG
2915 DECODE_FINISH(bl); // osd-only data
2916 }
2917
2918 if (struct_v >= 8) {
2919 crc_front.substr_of(bl.get_bl(), start_offset, bl.get_off() - start_offset);
2920 ::decode(crc, bl);
2921 tail_offset = bl.get_off();
2922 crc_defined = true;
2923 } else {
2924 crc_defined = false;
2925 crc = 0;
2926 }
2927
2928 DECODE_FINISH(bl); // wrapper
2929
2930 if (tail_offset) {
2931 // verify crc
2932 uint32_t actual = crc_front.crc32c(-1);
2933 if (tail_offset < bl.get_off()) {
2934 bufferlist tail;
2935 tail.substr_of(bl.get_bl(), tail_offset, bl.get_off() - tail_offset);
2936 actual = tail.crc32c(actual);
2937 }
2938 if (crc != actual) {
2939 ostringstream ss;
2940 ss << "bad crc, actual " << actual << " != expected " << crc;
2941 string s = ss.str();
2942 throw buffer::malformed_input(s.c_str());
2943 }
2944 }
2945
2946 post_decode();
2947}
2948
2949void OSDMap::post_decode()
2950{
2951 // index pool names
2952 name_pool.clear();
2953 for (const auto &pname : pool_name) {
2954 name_pool[pname.second] = pname.first;
2955 }
2956
2957 calc_num_osds();
2958 _calc_up_osd_features();
2959}
2960
2961void OSDMap::dump_erasure_code_profiles(
2962 const mempool::osdmap::map<string,map<string,string>>& profiles,
2963 Formatter *f)
2964{
2965 f->open_object_section("erasure_code_profiles");
2966 for (const auto &profile : profiles) {
2967 f->open_object_section(profile.first.c_str());
2968 for (const auto &profm : profile.second) {
2969 f->dump_string(profm.first.c_str(), profm.second.c_str());
2970 }
2971 f->close_section();
2972 }
2973 f->close_section();
2974}
2975
2976void OSDMap::dump(Formatter *f) const
2977{
2978 f->dump_int("epoch", get_epoch());
2979 f->dump_stream("fsid") << get_fsid();
2980 f->dump_stream("created") << get_created();
2981 f->dump_stream("modified") << get_modified();
2982 f->dump_string("flags", get_flag_string());
31f18b77 2983 f->dump_unsigned("crush_version", get_crush_version());
7c673cae
FG
2984 f->dump_float("full_ratio", full_ratio);
2985 f->dump_float("backfillfull_ratio", backfillfull_ratio);
2986 f->dump_float("nearfull_ratio", nearfull_ratio);
2987 f->dump_string("cluster_snapshot", get_cluster_snapshot());
2988 f->dump_int("pool_max", get_pool_max());
2989 f->dump_int("max_osd", get_max_osd());
31f18b77
FG
2990 f->dump_string("require_min_compat_client",
2991 ceph_release_name(require_min_compat_client));
2992 f->dump_string("min_compat_client",
2993 ceph_release_name(get_min_compat_client()));
2994 f->dump_string("require_osd_release",
2995 ceph_release_name(require_osd_release));
7c673cae
FG
2996
2997 f->open_array_section("pools");
2998 for (const auto &pool : pools) {
2999 std::string name("<unknown>");
3000 const auto &pni = pool_name.find(pool.first);
3001 if (pni != pool_name.end())
3002 name = pni->second;
3003 f->open_object_section("pool");
3004 f->dump_int("pool", pool.first);
3005 f->dump_string("pool_name", name);
3006 pool.second.dump(f);
3007 f->close_section();
3008 }
3009 f->close_section();
3010
3011 f->open_array_section("osds");
3012 for (int i=0; i<get_max_osd(); i++)
3013 if (exists(i)) {
3014 f->open_object_section("osd_info");
3015 f->dump_int("osd", i);
3016 f->dump_stream("uuid") << get_uuid(i);
3017 f->dump_int("up", is_up(i));
3018 f->dump_int("in", is_in(i));
3019 f->dump_float("weight", get_weightf(i));
3020 f->dump_float("primary_affinity", get_primary_affinityf(i));
3021 get_info(i).dump(f);
3022 f->dump_stream("public_addr") << get_addr(i);
3023 f->dump_stream("cluster_addr") << get_cluster_addr(i);
3024 f->dump_stream("heartbeat_back_addr") << get_hb_back_addr(i);
3025 f->dump_stream("heartbeat_front_addr") << get_hb_front_addr(i);
3026
3027 set<string> st;
3028 get_state(i, st);
3029 f->open_array_section("state");
3030 for (const auto &state : st)
3031 f->dump_string("state", state);
3032 f->close_section();
3033
3034 f->close_section();
3035 }
3036 f->close_section();
3037
3038 f->open_array_section("osd_xinfo");
3039 for (int i=0; i<get_max_osd(); i++) {
3040 if (exists(i)) {
3041 f->open_object_section("xinfo");
3042 f->dump_int("osd", i);
3043 osd_xinfo[i].dump(f);
3044 f->close_section();
3045 }
3046 }
3047 f->close_section();
3048
3049 f->open_array_section("pg_upmap");
3050 for (auto& p : pg_upmap) {
3051 f->open_object_section("mapping");
3052 f->dump_stream("pgid") << p.first;
3053 f->open_array_section("osds");
3054 for (auto q : p.second) {
3055 f->dump_int("osd", q);
3056 }
3057 f->close_section();
3058 f->close_section();
3059 }
3060 f->close_section();
3061 f->open_array_section("pg_upmap_items");
3062 for (auto& p : pg_upmap_items) {
3063 f->open_object_section("mapping");
3064 f->dump_stream("pgid") << p.first;
3065 f->open_array_section("mappings");
3066 for (auto& q : p.second) {
3067 f->open_object_section("mapping");
3068 f->dump_int("from", q.first);
3069 f->dump_int("to", q.second);
3070 f->close_section();
3071 }
3072 f->close_section();
3073 f->close_section();
3074 }
3075 f->close_section();
3076 f->open_array_section("pg_temp");
31f18b77 3077 pg_temp->dump(f);
7c673cae
FG
3078 f->close_section();
3079
3080 f->open_array_section("primary_temp");
3081 for (const auto &pg : *primary_temp) {
3082 f->dump_stream("pgid") << pg.first;
3083 f->dump_int("osd", pg.second);
3084 }
3085 f->close_section(); // primary_temp
3086
3087 f->open_object_section("blacklist");
3088 for (const auto &addr : blacklist) {
3089 stringstream ss;
3090 ss << addr.first;
3091 f->dump_stream(ss.str().c_str()) << addr.second;
3092 }
3093 f->close_section();
3094
3095 dump_erasure_code_profiles(erasure_code_profiles, f);
3096}
3097
3098void OSDMap::generate_test_instances(list<OSDMap*>& o)
3099{
3100 o.push_back(new OSDMap);
3101
3102 CephContext *cct = new CephContext(CODE_ENVIRONMENT_UTILITY);
3103 o.push_back(new OSDMap);
3104 uuid_d fsid;
224ce89b 3105 o.back()->build_simple(cct, 1, fsid, 16);
7c673cae
FG
3106 o.back()->created = o.back()->modified = utime_t(1, 2); // fix timestamp
3107 o.back()->blacklist[entity_addr_t()] = utime_t(5, 6);
3108 cct->put();
3109}
3110
3111string OSDMap::get_flag_string(unsigned f)
3112{
3113 string s;
3114 if ( f& CEPH_OSDMAP_NEARFULL)
3115 s += ",nearfull";
3116 if (f & CEPH_OSDMAP_FULL)
3117 s += ",full";
3118 if (f & CEPH_OSDMAP_PAUSERD)
3119 s += ",pauserd";
3120 if (f & CEPH_OSDMAP_PAUSEWR)
3121 s += ",pausewr";
3122 if (f & CEPH_OSDMAP_PAUSEREC)
3123 s += ",pauserec";
3124 if (f & CEPH_OSDMAP_NOUP)
3125 s += ",noup";
3126 if (f & CEPH_OSDMAP_NODOWN)
3127 s += ",nodown";
3128 if (f & CEPH_OSDMAP_NOOUT)
3129 s += ",noout";
3130 if (f & CEPH_OSDMAP_NOIN)
3131 s += ",noin";
3132 if (f & CEPH_OSDMAP_NOBACKFILL)
3133 s += ",nobackfill";
3134 if (f & CEPH_OSDMAP_NOREBALANCE)
3135 s += ",norebalance";
3136 if (f & CEPH_OSDMAP_NORECOVER)
3137 s += ",norecover";
3138 if (f & CEPH_OSDMAP_NOSCRUB)
3139 s += ",noscrub";
3140 if (f & CEPH_OSDMAP_NODEEP_SCRUB)
3141 s += ",nodeep-scrub";
3142 if (f & CEPH_OSDMAP_NOTIERAGENT)
3143 s += ",notieragent";
3144 if (f & CEPH_OSDMAP_SORTBITWISE)
3145 s += ",sortbitwise";
3146 if (f & CEPH_OSDMAP_REQUIRE_JEWEL)
3147 s += ",require_jewel_osds";
3148 if (f & CEPH_OSDMAP_REQUIRE_KRAKEN)
3149 s += ",require_kraken_osds";
3150 if (f & CEPH_OSDMAP_REQUIRE_LUMINOUS)
3151 s += ",require_luminous_osds";
c07f9fc5
FG
3152 if (f & CEPH_OSDMAP_RECOVERY_DELETES)
3153 s += ",recovery_deletes";
181888fb
FG
3154 if (f & CEPH_OSDMAP_PURGED_SNAPDIRS)
3155 s += ",purged_snapdirs";
f64942e4
AA
3156 if (f & CEPH_OSDMAP_PGLOG_HARDLIMIT)
3157 s += ",pglog_hardlimit";
7c673cae
FG
3158 if (s.length())
3159 s.erase(0, 1);
3160 return s;
3161}
3162
3163string OSDMap::get_flag_string() const
3164{
3165 return get_flag_string(flags);
3166}
3167
7c673cae
FG
3168void OSDMap::print_pools(ostream& out) const
3169{
3170 for (const auto &pool : pools) {
3171 std::string name("<unknown>");
3172 const auto &pni = pool_name.find(pool.first);
3173 if (pni != pool_name.end())
3174 name = pni->second;
3175 out << "pool " << pool.first
3176 << " '" << name
3177 << "' " << pool.second << "\n";
3178
3179 for (const auto &snap : pool.second.snaps)
3180 out << "\tsnap " << snap.second.snapid << " '" << snap.second.name << "' " << snap.second.stamp << "\n";
3181
3182 if (!pool.second.removed_snaps.empty())
3183 out << "\tremoved_snaps " << pool.second.removed_snaps << "\n";
3184 }
3185 out << std::endl;
3186}
3187
3188void OSDMap::print(ostream& out) const
3189{
3190 out << "epoch " << get_epoch() << "\n"
3191 << "fsid " << get_fsid() << "\n"
3192 << "created " << get_created() << "\n"
3193 << "modified " << get_modified() << "\n";
3194
3195 out << "flags " << get_flag_string() << "\n";
31f18b77 3196 out << "crush_version " << get_crush_version() << "\n";
7c673cae
FG
3197 out << "full_ratio " << full_ratio << "\n";
3198 out << "backfillfull_ratio " << backfillfull_ratio << "\n";
3199 out << "nearfull_ratio " << nearfull_ratio << "\n";
31f18b77
FG
3200 if (require_min_compat_client > 0) {
3201 out << "require_min_compat_client "
3202 << ceph_release_name(require_min_compat_client) << "\n";
7c673cae 3203 }
31f18b77
FG
3204 out << "min_compat_client " << ceph_release_name(get_min_compat_client())
3205 << "\n";
224ce89b
WB
3206 if (require_osd_release > 0) {
3207 out << "require_osd_release " << ceph_release_name(require_osd_release)
3208 << "\n";
3209 }
7c673cae
FG
3210 if (get_cluster_snapshot().length())
3211 out << "cluster_snapshot " << get_cluster_snapshot() << "\n";
3212 out << "\n";
3213
3214 print_pools(out);
3215
3216 out << "max_osd " << get_max_osd() << "\n";
3217 for (int i=0; i<get_max_osd(); i++) {
3218 if (exists(i)) {
3219 out << "osd." << i;
3220 out << (is_up(i) ? " up ":" down");
3221 out << (is_in(i) ? " in ":" out");
3222 out << " weight " << get_weightf(i);
3223 if (get_primary_affinity(i) != CEPH_OSD_DEFAULT_PRIMARY_AFFINITY)
3224 out << " primary_affinity " << get_primary_affinityf(i);
3225 const osd_info_t& info(get_info(i));
3226 out << " " << info;
3227 out << " " << get_addr(i) << " " << get_cluster_addr(i) << " " << get_hb_back_addr(i)
3228 << " " << get_hb_front_addr(i);
3229 set<string> st;
3230 get_state(i, st);
3231 out << " " << st;
3232 if (!get_uuid(i).is_zero())
3233 out << " " << get_uuid(i);
3234 out << "\n";
3235 }
3236 }
3237 out << std::endl;
3238
3239 for (auto& p : pg_upmap) {
3240 out << "pg_upmap " << p.first << " " << p.second << "\n";
3241 }
3242 for (auto& p : pg_upmap_items) {
3243 out << "pg_upmap_items " << p.first << " " << p.second << "\n";
3244 }
3245
3246 for (const auto pg : *pg_temp)
3247 out << "pg_temp " << pg.first << " " << pg.second << "\n";
3248
3249 for (const auto pg : *primary_temp)
3250 out << "primary_temp " << pg.first << " " << pg.second << "\n";
3251
3252 for (const auto &addr : blacklist)
3253 out << "blacklist " << addr.first << " expires " << addr.second << "\n";
3254
3255 // ignore pg_swap_primary
3256}
3257
3258class OSDTreePlainDumper : public CrushTreeDumper::Dumper<TextTable> {
3259public:
3260 typedef CrushTreeDumper::Dumper<TextTable> Parent;
31f18b77
FG
3261
3262 OSDTreePlainDumper(const CrushWrapper *crush, const OSDMap *osdmap_,
3263 unsigned f)
c07f9fc5 3264 : Parent(crush, osdmap_->get_pool_names()), osdmap(osdmap_), filter(f) { }
31f18b77
FG
3265
3266 bool should_dump_leaf(int i) const override {
c07f9fc5
FG
3267 if (!filter) {
3268 return true; // normal case
3269 }
3270 if (((filter & OSDMap::DUMP_UP) && osdmap->is_up(i)) ||
3271 ((filter & OSDMap::DUMP_DOWN) && osdmap->is_down(i)) ||
3272 ((filter & OSDMap::DUMP_IN) && osdmap->is_in(i)) ||
3273 ((filter & OSDMap::DUMP_OUT) && osdmap->is_out(i)) ||
3274 ((filter & OSDMap::DUMP_DESTROYED) && osdmap->is_destroyed(i))) {
3275 return true;
31f18b77 3276 }
c07f9fc5 3277 return false;
31f18b77
FG
3278 }
3279
3280 bool should_dump_empty_bucket() const override {
3281 return !filter;
3282 }
7c673cae
FG
3283
3284 void dump(TextTable *tbl) {
3285 tbl->define_column("ID", TextTable::LEFT, TextTable::RIGHT);
224ce89b 3286 tbl->define_column("CLASS", TextTable::LEFT, TextTable::RIGHT);
7c673cae
FG
3287 tbl->define_column("WEIGHT", TextTable::LEFT, TextTable::RIGHT);
3288 tbl->define_column("TYPE NAME", TextTable::LEFT, TextTable::LEFT);
c07f9fc5 3289 tbl->define_column("STATUS", TextTable::LEFT, TextTable::RIGHT);
7c673cae 3290 tbl->define_column("REWEIGHT", TextTable::LEFT, TextTable::RIGHT);
224ce89b 3291 tbl->define_column("PRI-AFF", TextTable::LEFT, TextTable::RIGHT);
7c673cae
FG
3292
3293 Parent::dump(tbl);
3294
3295 for (int i = 0; i < osdmap->get_max_osd(); i++) {
31f18b77 3296 if (osdmap->exists(i) && !is_touched(i) && should_dump_leaf(i)) {
c07f9fc5 3297 dump_item(CrushTreeDumper::Item(i, 0, 0, 0), tbl);
31f18b77 3298 }
7c673cae
FG
3299 }
3300 }
3301
3302protected:
3303 void dump_item(const CrushTreeDumper::Item &qi, TextTable *tbl) override {
224ce89b
WB
3304 const char *c = crush->get_item_class(qi.id);
3305 if (!c)
3306 c = "";
7c673cae 3307 *tbl << qi.id
224ce89b 3308 << c
7c673cae
FG
3309 << weightf_t(qi.weight);
3310
3311 ostringstream name;
3312 for (int k = 0; k < qi.depth; k++)
3313 name << " ";
3314 if (qi.is_bucket()) {
3315 name << crush->get_type_name(crush->get_bucket_type(qi.id)) << " "
3316 << crush->get_item_name(qi.id);
3317 } else {
3318 name << "osd." << qi.id;
3319 }
3320 *tbl << name.str();
3321
3322 if (!qi.is_bucket()) {
3323 if (!osdmap->exists(qi.id)) {
3324 *tbl << "DNE"
3325 << 0;
3326 } else {
c07f9fc5
FG
3327 string s;
3328 if (osdmap->is_up(qi.id)) {
3329 s = "up";
3330 } else if (osdmap->is_destroyed(qi.id)) {
3331 s = "destroyed";
3332 } else {
3333 s = "down";
3334 }
3335 *tbl << s
7c673cae
FG
3336 << weightf_t(osdmap->get_weightf(qi.id))
3337 << weightf_t(osdmap->get_primary_affinityf(qi.id));
3338 }
3339 }
3340 *tbl << TextTable::endrow;
3341 }
3342
3343private:
3344 const OSDMap *osdmap;
31f18b77 3345 const unsigned filter;
7c673cae
FG
3346};
3347
3348class OSDTreeFormattingDumper : public CrushTreeDumper::FormattingDumper {
3349public:
3350 typedef CrushTreeDumper::FormattingDumper Parent;
3351
31f18b77
FG
3352 OSDTreeFormattingDumper(const CrushWrapper *crush, const OSDMap *osdmap_,
3353 unsigned f)
c07f9fc5 3354 : Parent(crush, osdmap_->get_pool_names()), osdmap(osdmap_), filter(f) { }
31f18b77
FG
3355
3356 bool should_dump_leaf(int i) const override {
c07f9fc5
FG
3357 if (!filter) {
3358 return true; // normal case
3359 }
3360 if (((filter & OSDMap::DUMP_UP) && osdmap->is_up(i)) ||
3361 ((filter & OSDMap::DUMP_DOWN) && osdmap->is_down(i)) ||
3362 ((filter & OSDMap::DUMP_IN) && osdmap->is_in(i)) ||
3363 ((filter & OSDMap::DUMP_OUT) && osdmap->is_out(i)) ||
3364 ((filter & OSDMap::DUMP_DESTROYED) && osdmap->is_destroyed(i))) {
3365 return true;
31f18b77 3366 }
c07f9fc5 3367 return false;
31f18b77
FG
3368 }
3369
3370 bool should_dump_empty_bucket() const override {
3371 return !filter;
3372 }
7c673cae
FG
3373
3374 void dump(Formatter *f) {
3375 f->open_array_section("nodes");
3376 Parent::dump(f);
3377 f->close_section();
3378 f->open_array_section("stray");
3379 for (int i = 0; i < osdmap->get_max_osd(); i++) {
31f18b77 3380 if (osdmap->exists(i) && !is_touched(i) && should_dump_leaf(i))
c07f9fc5 3381 dump_item(CrushTreeDumper::Item(i, 0, 0, 0), f);
7c673cae
FG
3382 }
3383 f->close_section();
3384 }
3385
3386protected:
3387 void dump_item_fields(const CrushTreeDumper::Item &qi, Formatter *f) override {
3388 Parent::dump_item_fields(qi, f);
3389 if (!qi.is_bucket())
3390 {
c07f9fc5
FG
3391 string s;
3392 if (osdmap->is_up(qi.id)) {
3393 s = "up";
3394 } else if (osdmap->is_destroyed(qi.id)) {
3395 s = "destroyed";
3396 } else {
3397 s = "down";
3398 }
7c673cae 3399 f->dump_unsigned("exists", (int)osdmap->exists(qi.id));
c07f9fc5 3400 f->dump_string("status", s);
7c673cae
FG
3401 f->dump_float("reweight", osdmap->get_weightf(qi.id));
3402 f->dump_float("primary_affinity", osdmap->get_primary_affinityf(qi.id));
3403 }
3404 }
3405
3406private:
3407 const OSDMap *osdmap;
31f18b77 3408 const unsigned filter;
7c673cae
FG
3409};
3410
31f18b77 3411void OSDMap::print_tree(Formatter *f, ostream *out, unsigned filter) const
7c673cae 3412{
31f18b77
FG
3413 if (f) {
3414 OSDTreeFormattingDumper(crush.get(), this, filter).dump(f);
3415 } else {
7c673cae
FG
3416 assert(out);
3417 TextTable tbl;
31f18b77 3418 OSDTreePlainDumper(crush.get(), this, filter).dump(&tbl);
7c673cae
FG
3419 *out << tbl;
3420 }
3421}
3422
224ce89b
WB
3423void OSDMap::print_summary(Formatter *f, ostream& out,
3424 const string& prefix) const
7c673cae
FG
3425{
3426 if (f) {
3427 f->open_object_section("osdmap");
3428 f->dump_int("epoch", get_epoch());
3429 f->dump_int("num_osds", get_num_osds());
3430 f->dump_int("num_up_osds", get_num_up_osds());
3431 f->dump_int("num_in_osds", get_num_in_osds());
3432 f->dump_bool("full", test_flag(CEPH_OSDMAP_FULL) ? true : false);
3433 f->dump_bool("nearfull", test_flag(CEPH_OSDMAP_NEARFULL) ? true : false);
3434 f->dump_unsigned("num_remapped_pgs", get_num_pg_temp());
3435 f->close_section();
3436 } else {
31f18b77 3437 out << get_num_osds() << " osds: "
7c673cae
FG
3438 << get_num_up_osds() << " up, "
3439 << get_num_in_osds() << " in";
3440 if (get_num_pg_temp())
3441 out << "; " << get_num_pg_temp() << " remapped pgs";
3442 out << "\n";
3443 uint64_t important_flags = flags & ~CEPH_OSDMAP_SEMIHIDDEN_FLAGS;
3444 if (important_flags)
224ce89b 3445 out << prefix << "flags " << get_flag_string(important_flags) << "\n";
7c673cae
FG
3446 }
3447}
3448
3449void OSDMap::print_oneline_summary(ostream& out) const
3450{
3451 out << "e" << get_epoch() << ": "
31f18b77 3452 << get_num_osds() << " total, "
7c673cae
FG
3453 << get_num_up_osds() << " up, "
3454 << get_num_in_osds() << " in";
3455 if (test_flag(CEPH_OSDMAP_FULL))
3456 out << " full";
3457 else if (test_flag(CEPH_OSDMAP_NEARFULL))
3458 out << " nearfull";
3459}
3460
3efd9988 3461bool OSDMap::crush_rule_in_use(int rule_id) const
7c673cae
FG
3462{
3463 for (const auto &pool : pools) {
3efd9988 3464 if (pool.second.crush_rule == rule_id)
7c673cae
FG
3465 return true;
3466 }
3467 return false;
3468}
3469
3efd9988
FG
3470int OSDMap::validate_crush_rules(CrushWrapper *newcrush,
3471 ostream *ss) const
3472{
3473 for (auto& i : pools) {
3474 auto& pool = i.second;
3475 int ruleno = pool.get_crush_rule();
3476 if (!newcrush->rule_exists(ruleno)) {
3477 *ss << "pool " << i.first << " references crush_rule " << ruleno
3478 << " but it is not present";
3479 return -EINVAL;
3480 }
3481 if (newcrush->get_rule_mask_ruleset(ruleno) != ruleno) {
3482 *ss << "rule " << ruleno << " mask ruleset does not match rule id";
3483 return -EINVAL;
3484 }
3485 if (newcrush->get_rule_mask_type(ruleno) != (int)pool.get_type()) {
3486 *ss << "pool " << i.first << " type does not match rule " << ruleno;
3487 return -EINVAL;
3488 }
3489 if (pool.get_size() < (int)newcrush->get_rule_mask_min_size(ruleno) ||
3490 pool.get_size() > (int)newcrush->get_rule_mask_max_size(ruleno)) {
3491 *ss << "pool " << i.first << " size " << pool.get_size() << " does not"
3492 << " fall within rule " << ruleno
3493 << " min_size " << newcrush->get_rule_mask_min_size(ruleno)
3494 << " and max_size " << newcrush->get_rule_mask_max_size(ruleno);
3495 return -EINVAL;
3496 }
3497 }
3498 return 0;
3499}
3500
224ce89b
WB
3501int OSDMap::build_simple_optioned(CephContext *cct, epoch_t e, uuid_d &fsid,
3502 int nosd, int pg_bits, int pgp_bits,
3503 bool default_pool)
7c673cae 3504{
224ce89b
WB
3505 ldout(cct, 10) << "build_simple on " << nosd
3506 << " osds" << dendl;
7c673cae
FG
3507 epoch = e;
3508 set_fsid(fsid);
3509 created = modified = ceph_clock_now();
3510
3511 if (nosd >= 0) {
3512 set_max_osd(nosd);
3513 } else {
3514 // count osds
3515 int maxosd = 0;
3516 const md_config_t *conf = cct->_conf;
3517 vector<string> sections;
3518 conf->get_all_sections(sections);
3519
3520 for (auto &section : sections) {
3521 if (section.find("osd.") != 0)
3522 continue;
3523
3524 const char *begin = section.c_str() + 4;
3525 char *end = (char*)begin;
3526 int o = strtol(begin, &end, 10);
3527 if (*end != '\0')
3528 continue;
3529
3530 if (o > cct->_conf->mon_max_osd) {
3531 lderr(cct) << "[osd." << o << "] in config has id > mon_max_osd " << cct->_conf->mon_max_osd << dendl;
3532 return -ERANGE;
3533 }
3534
3535 if (o > maxosd)
3536 maxosd = o;
3537 }
3538
3539 set_max_osd(maxosd + 1);
3540 }
3541
7c673cae
FG
3542
3543 stringstream ss;
3544 int r;
3545 if (nosd >= 0)
3546 r = build_simple_crush_map(cct, *crush, nosd, &ss);
3547 else
3548 r = build_simple_crush_map_from_conf(cct, *crush, &ss);
3549 assert(r == 0);
3550
3551 int poolbase = get_max_osd() ? get_max_osd() : 1;
3552
d2e6a577 3553 const int default_replicated_rule = crush->get_osd_pool_default_crush_replicated_ruleset(cct);
31f18b77 3554 assert(default_replicated_rule >= 0);
7c673cae 3555
224ce89b
WB
3556 if (default_pool) {
3557 // pgp_num <= pg_num
3558 if (pgp_bits > pg_bits)
3559 pgp_bits = pg_bits;
3560
3561 vector<string> pool_names;
3562 pool_names.push_back("rbd");
3563 for (auto &plname : pool_names) {
3564 int64_t pool = ++pool_max;
3565 pools[pool].type = pg_pool_t::TYPE_REPLICATED;
3566 pools[pool].flags = cct->_conf->osd_pool_default_flags;
3567 if (cct->_conf->osd_pool_default_flag_hashpspool)
3568 pools[pool].set_flag(pg_pool_t::FLAG_HASHPSPOOL);
3569 if (cct->_conf->osd_pool_default_flag_nodelete)
3570 pools[pool].set_flag(pg_pool_t::FLAG_NODELETE);
3571 if (cct->_conf->osd_pool_default_flag_nopgchange)
3572 pools[pool].set_flag(pg_pool_t::FLAG_NOPGCHANGE);
3573 if (cct->_conf->osd_pool_default_flag_nosizechange)
3574 pools[pool].set_flag(pg_pool_t::FLAG_NOSIZECHANGE);
3575 pools[pool].size = cct->_conf->osd_pool_default_size;
3576 pools[pool].min_size = cct->_conf->get_osd_pool_default_min_size();
3577 pools[pool].crush_rule = default_replicated_rule;
3578 pools[pool].object_hash = CEPH_STR_HASH_RJENKINS;
3579 pools[pool].set_pg_num(poolbase << pg_bits);
3580 pools[pool].set_pgp_num(poolbase << pgp_bits);
3581 pools[pool].last_change = epoch;
c07f9fc5
FG
3582 pools[pool].application_metadata.insert(
3583 {pg_pool_t::APPLICATION_NAME_RBD, {}});
224ce89b
WB
3584 pool_name[pool] = plname;
3585 name_pool[plname] = pool;
3586 }
7c673cae
FG
3587 }
3588
3589 for (int i=0; i<get_max_osd(); i++) {
3590 set_state(i, 0);
3591 set_weight(i, CEPH_OSD_OUT);
3592 }
3593
3594 map<string,string> profile_map;
3595 r = get_erasure_code_profile_default(cct, profile_map, &ss);
3596 if (r < 0) {
3597 lderr(cct) << ss.str() << dendl;
3598 return r;
3599 }
3600 set_erasure_code_profile("default", profile_map);
3601 return 0;
3602}
3603
3604int OSDMap::get_erasure_code_profile_default(CephContext *cct,
3605 map<string,string> &profile_map,
3606 ostream *ss)
3607{
3608 int r = get_json_str_map(cct->_conf->osd_pool_default_erasure_code_profile,
3609 *ss,
3610 &profile_map);
3611 return r;
3612}
3613
3614int OSDMap::_build_crush_types(CrushWrapper& crush)
3615{
3616 crush.set_type_name(0, "osd");
3617 crush.set_type_name(1, "host");
3618 crush.set_type_name(2, "chassis");
3619 crush.set_type_name(3, "rack");
3620 crush.set_type_name(4, "row");
3621 crush.set_type_name(5, "pdu");
3622 crush.set_type_name(6, "pod");
3623 crush.set_type_name(7, "room");
3624 crush.set_type_name(8, "datacenter");
3625 crush.set_type_name(9, "region");
3626 crush.set_type_name(10, "root");
3627 return 10;
3628}
3629
3630int OSDMap::build_simple_crush_map(CephContext *cct, CrushWrapper& crush,
3631 int nosd, ostream *ss)
3632{
3633 crush.create();
3634
3635 // root
3636 int root_type = _build_crush_types(crush);
3637 int rootid;
3638 int r = crush.add_bucket(0, 0, CRUSH_HASH_DEFAULT,
3639 root_type, 0, NULL, NULL, &rootid);
3640 assert(r == 0);
3641 crush.set_item_name(rootid, "default");
3642
3643 for (int o=0; o<nosd; o++) {
3644 map<string,string> loc;
3645 loc["host"] = "localhost";
3646 loc["rack"] = "localrack";
3647 loc["root"] = "default";
3648 ldout(cct, 10) << " adding osd." << o << " at " << loc << dendl;
3649 char name[32];
3650 snprintf(name, sizeof(name), "osd.%d", o);
3651 crush.insert_item(cct, o, 1.0, name, loc);
3652 }
3653
31f18b77 3654 build_simple_crush_rules(cct, crush, "default", ss);
7c673cae
FG
3655
3656 crush.finalize();
3657
3658 return 0;
3659}
3660
3661int OSDMap::build_simple_crush_map_from_conf(CephContext *cct,
3662 CrushWrapper& crush,
3663 ostream *ss)
3664{
3665 const md_config_t *conf = cct->_conf;
3666
3667 crush.create();
3668
3669 // root
3670 int root_type = _build_crush_types(crush);
3671 int rootid;
3672 int r = crush.add_bucket(0, 0,
3673 CRUSH_HASH_DEFAULT,
3674 root_type, 0, NULL, NULL, &rootid);
3675 assert(r == 0);
3676 crush.set_item_name(rootid, "default");
3677
3678 // add osds
3679 vector<string> sections;
3680 conf->get_all_sections(sections);
3681
3682 for (auto &section : sections) {
3683 if (section.find("osd.") != 0)
3684 continue;
3685
3686 const char *begin = section.c_str() + 4;
3687 char *end = (char*)begin;
3688 int o = strtol(begin, &end, 10);
3689 if (*end != '\0')
3690 continue;
3691
3692 string host, rack, row, room, dc, pool;
3693 vector<string> sectiontmp;
3694 sectiontmp.push_back("osd");
3695 sectiontmp.push_back(section);
3696 conf->get_val_from_conf_file(sectiontmp, "host", host, false);
3697 conf->get_val_from_conf_file(sectiontmp, "rack", rack, false);
3698 conf->get_val_from_conf_file(sectiontmp, "row", row, false);
3699 conf->get_val_from_conf_file(sectiontmp, "room", room, false);
3700 conf->get_val_from_conf_file(sectiontmp, "datacenter", dc, false);
3701 conf->get_val_from_conf_file(sectiontmp, "root", pool, false);
3702
3703 if (host.length() == 0)
3704 host = "unknownhost";
3705 if (rack.length() == 0)
3706 rack = "unknownrack";
3707
3708 map<string,string> loc;
3709 loc["host"] = host;
3710 loc["rack"] = rack;
3711 if (row.size())
3712 loc["row"] = row;
3713 if (room.size())
3714 loc["room"] = room;
3715 if (dc.size())
3716 loc["datacenter"] = dc;
3717 loc["root"] = "default";
3718
3719 ldout(cct, 5) << " adding osd." << o << " at " << loc << dendl;
3720 crush.insert_item(cct, o, 1.0, section, loc);
3721 }
3722
31f18b77 3723 build_simple_crush_rules(cct, crush, "default", ss);
7c673cae
FG
3724
3725 crush.finalize();
3726
3727 return 0;
3728}
3729
3730
31f18b77
FG
3731int OSDMap::build_simple_crush_rules(
3732 CephContext *cct,
3733 CrushWrapper& crush,
3734 const string& root,
3735 ostream *ss)
7c673cae 3736{
31f18b77 3737 int crush_rule = crush.get_osd_pool_default_crush_replicated_ruleset(cct);
7c673cae
FG
3738 string failure_domain =
3739 crush.get_type_name(cct->_conf->osd_crush_chooseleaf_type);
3740
7c673cae 3741 int r;
31f18b77 3742 r = crush.add_simple_rule_at(
224ce89b 3743 "replicated_rule", root, failure_domain, "",
31f18b77
FG
3744 "firstn", pg_pool_t::TYPE_REPLICATED,
3745 crush_rule, ss);
7c673cae
FG
3746 if (r < 0)
3747 return r;
3748 // do not add an erasure rule by default or else we will implicitly
3749 // require the crush_v2 feature of clients
3750 return 0;
3751}
3752
3753int OSDMap::summarize_mapping_stats(
3754 OSDMap *newmap,
3755 const set<int64_t> *pools,
3756 std::string *out,
3757 Formatter *f) const
3758{
3759 set<int64_t> ls;
3760 if (pools) {
3761 ls = *pools;
3762 } else {
3763 for (auto &p : get_pools())
3764 ls.insert(p.first);
3765 }
3766
3767 unsigned total_pg = 0;
3768 unsigned moved_pg = 0;
3769 vector<unsigned> base_by_osd(get_max_osd(), 0);
3770 vector<unsigned> new_by_osd(get_max_osd(), 0);
3771 for (int64_t pool_id : ls) {
3772 const pg_pool_t *pi = get_pg_pool(pool_id);
31f18b77
FG
3773 vector<int> up, up2;
3774 int up_primary;
7c673cae
FG
3775 for (unsigned ps = 0; ps < pi->get_pg_num(); ++ps) {
3776 pg_t pgid(ps, pool_id, -1);
3777 total_pg += pi->get_size();
31f18b77 3778 pg_to_up_acting_osds(pgid, &up, &up_primary, nullptr, nullptr);
7c673cae
FG
3779 for (int osd : up) {
3780 if (osd >= 0 && osd < get_max_osd())
3781 ++base_by_osd[osd];
3782 }
3783 if (newmap) {
31f18b77 3784 newmap->pg_to_up_acting_osds(pgid, &up2, &up_primary, nullptr, nullptr);
7c673cae
FG
3785 for (int osd : up2) {
3786 if (osd >= 0 && osd < get_max_osd())
3787 ++new_by_osd[osd];
3788 }
3789 if (pi->type == pg_pool_t::TYPE_ERASURE) {
3790 for (unsigned i=0; i<up.size(); ++i) {
3791 if (up[i] != up2[i]) {
3792 ++moved_pg;
3793 }
3794 }
3795 } else if (pi->type == pg_pool_t::TYPE_REPLICATED) {
3796 for (int osd : up) {
3797 if (std::find(up2.begin(), up2.end(), osd) == up2.end()) {
3798 ++moved_pg;
3799 }
3800 }
3801 } else {
3802 assert(0 == "unhandled pool type");
3803 }
3804 }
3805 }
3806 }
3807
3808 unsigned num_up_in = 0;
3809 for (int osd = 0; osd < get_max_osd(); ++osd) {
3810 if (is_up(osd) && is_in(osd))
3811 ++num_up_in;
3812 }
3813 if (!num_up_in) {
3814 return -EINVAL;
3815 }
3816
3817 float avg_pg = (float)total_pg / (float)num_up_in;
3818 float base_stddev = 0, new_stddev = 0;
3819 int min = -1, max = -1;
3820 unsigned min_base_pg = 0, max_base_pg = 0;
3821 unsigned min_new_pg = 0, max_new_pg = 0;
3822 for (int osd = 0; osd < get_max_osd(); ++osd) {
3823 if (is_up(osd) && is_in(osd)) {
3824 float base_diff = (float)base_by_osd[osd] - avg_pg;
3825 base_stddev += base_diff * base_diff;
3826 float new_diff = (float)new_by_osd[osd] - avg_pg;
3827 new_stddev += new_diff * new_diff;
3828 if (min < 0 || base_by_osd[osd] < min_base_pg) {
3829 min = osd;
3830 min_base_pg = base_by_osd[osd];
3831 min_new_pg = new_by_osd[osd];
3832 }
3833 if (max < 0 || base_by_osd[osd] > max_base_pg) {
3834 max = osd;
3835 max_base_pg = base_by_osd[osd];
3836 max_new_pg = new_by_osd[osd];
3837 }
3838 }
3839 }
3840 base_stddev = sqrt(base_stddev / num_up_in);
3841 new_stddev = sqrt(new_stddev / num_up_in);
3842
3843 float edev = sqrt(avg_pg * (1.0 - (1.0 / (double)num_up_in)));
3844
3845 ostringstream ss;
3846 if (f)
3847 f->open_object_section("utilization");
3848 if (newmap) {
3849 if (f) {
3850 f->dump_unsigned("moved_pgs", moved_pg);
3851 f->dump_unsigned("total_pgs", total_pg);
3852 } else {
3853 float percent = 0;
3854 if (total_pg)
3855 percent = (float)moved_pg * 100.0 / (float)total_pg;
3856 ss << "moved " << moved_pg << " / " << total_pg
3857 << " (" << percent << "%)\n";
3858 }
3859 }
3860 if (f) {
3861 f->dump_float("avg_pgs", avg_pg);
3862 f->dump_float("std_dev", base_stddev);
3863 f->dump_float("expected_baseline_std_dev", edev);
3864 if (newmap)
3865 f->dump_float("new_std_dev", new_stddev);
3866 } else {
3867 ss << "avg " << avg_pg << "\n";
3868 ss << "stddev " << base_stddev;
3869 if (newmap)
3870 ss << " -> " << new_stddev;
3871 ss << " (expected baseline " << edev << ")\n";
3872 }
3873 if (min >= 0) {
3874 if (f) {
3875 f->dump_unsigned("min_osd", min);
3876 f->dump_unsigned("min_osd_pgs", min_base_pg);
3877 if (newmap)
3878 f->dump_unsigned("new_min_osd_pgs", min_new_pg);
3879 } else {
3880 ss << "min osd." << min << " with " << min_base_pg;
3881 if (newmap)
3882 ss << " -> " << min_new_pg;
3883 ss << " pgs (" << (float)min_base_pg / avg_pg;
3884 if (newmap)
3885 ss << " -> " << (float)min_new_pg / avg_pg;
3886 ss << " * mean)\n";
3887 }
3888 }
3889 if (max >= 0) {
3890 if (f) {
3891 f->dump_unsigned("max_osd", max);
3892 f->dump_unsigned("max_osd_pgs", max_base_pg);
3893 if (newmap)
3894 f->dump_unsigned("new_max_osd_pgs", max_new_pg);
3895 } else {
3896 ss << "max osd." << max << " with " << max_base_pg;
3897 if (newmap)
3898 ss << " -> " << max_new_pg;
3899 ss << " pgs (" << (float)max_base_pg / avg_pg;
3900 if (newmap)
3901 ss << " -> " << (float)max_new_pg / avg_pg;
3902 ss << " * mean)\n";
3903 }
3904 }
3905 if (f)
3906 f->close_section();
3907 if (out)
3908 *out = ss.str();
3909 return 0;
3910}
3911
3912
3913int OSDMap::clean_pg_upmaps(
3914 CephContext *cct,
f64942e4 3915 Incremental *pending_inc) const
7c673cae
FG
3916{
3917 ldout(cct, 10) << __func__ << dendl;
3918 int changed = 0;
3919 for (auto& p : pg_upmap) {
3920 vector<int> raw;
3921 int primary;
3922 pg_to_raw_osds(p.first, &raw, &primary);
3923 if (vectors_equal(raw, p.second)) {
3924 ldout(cct, 10) << " removing redundant pg_upmap " << p.first << " "
3925 << p.second << dendl;
3926 pending_inc->old_pg_upmap.insert(p.first);
3927 ++changed;
3928 }
3929 }
3930 for (auto& p : pg_upmap_items) {
3931 vector<int> raw;
3932 int primary;
3933 pg_to_raw_osds(p.first, &raw, &primary);
3934 mempool::osdmap::vector<pair<int,int>> newmap;
3935 for (auto& q : p.second) {
f64942e4
AA
3936 if (std::find(raw.begin(), raw.end(), q.first) == raw.end()) {
3937 // cancel mapping if source osd does not exist anymore
3938 continue;
3939 }
3940 if (q.second != CRUSH_ITEM_NONE && q.second < max_osd &&
3941 q.second >= 0 && osd_weight[q.second] == 0) {
3942 // cancel mapping if target osd is out
3943 continue;
7c673cae 3944 }
f64942e4 3945 newmap.push_back(q);
7c673cae
FG
3946 }
3947 if (newmap.empty()) {
3948 ldout(cct, 10) << " removing no-op pg_upmap_items " << p.first << " "
3949 << p.second << dendl;
3950 pending_inc->old_pg_upmap_items.insert(p.first);
3951 ++changed;
3952 } else if (newmap != p.second) {
3953 ldout(cct, 10) << " simplifying partially no-op pg_upmap_items "
3954 << p.first << " " << p.second << " -> " << newmap << dendl;
3955 pending_inc->new_pg_upmap_items[p.first] = newmap;
3956 ++changed;
3957 }
3958 }
3959 return changed;
3960}
3961
3962bool OSDMap::try_pg_upmap(
3963 CephContext *cct,
3964 pg_t pg, ///< pg to potentially remap
3965 const set<int>& overfull, ///< osds we'd want to evacuate
3966 const vector<int>& underfull, ///< osds to move to, in order of preference
3967 vector<int> *orig,
3968 vector<int> *out) ///< resulting alternative mapping
3969{
3970 const pg_pool_t *pool = get_pg_pool(pg.pool());
3971 if (!pool)
3972 return false;
31f18b77 3973 int rule = crush->find_rule(pool->get_crush_rule(), pool->get_type(),
7c673cae
FG
3974 pool->get_size());
3975 if (rule < 0)
3976 return false;
3977
3978 // get original mapping
3979 _pg_to_raw_osds(*pool, pg, orig, NULL);
3980
3981 // make sure there is something there to remap
3982 bool any = false;
3983 for (auto osd : *orig) {
3984 if (overfull.count(osd)) {
3985 any = true;
3986 break;
3987 }
3988 }
3989 if (!any) {
3990 return false;
3991 }
3992
3993 int r = crush->try_remap_rule(
3994 cct,
3995 rule,
3996 pool->get_size(),
3997 overfull, underfull,
3998 *orig,
3999 out);
4000 if (r < 0)
4001 return false;
4002 if (*out == *orig)
4003 return false;
4004 return true;
4005}
4006
4007int OSDMap::calc_pg_upmaps(
4008 CephContext *cct,
31f18b77 4009 float max_deviation_ratio,
7c673cae 4010 int max,
31f18b77 4011 const set<int64_t>& only_pools_orig,
7c673cae
FG
4012 OSDMap::Incremental *pending_inc)
4013{
31f18b77
FG
4014 set<int64_t> only_pools;
4015 if (only_pools_orig.empty()) {
4016 for (auto& i : pools) {
4017 only_pools.insert(i.first);
4018 }
4019 } else {
4020 only_pools = only_pools_orig;
4021 }
7c673cae
FG
4022 OSDMap tmp;
4023 tmp.deepish_copy_from(*this);
31f18b77
FG
4024 float start_deviation = 0;
4025 float end_deviation = 0;
7c673cae
FG
4026 int num_changed = 0;
4027 while (true) {
4028 map<int,set<pg_t>> pgs_by_osd;
4029 int total_pgs = 0;
31f18b77
FG
4030 float osd_weight_total = 0;
4031 map<int,float> osd_weight;
7c673cae
FG
4032 for (auto& i : pools) {
4033 if (!only_pools.empty() && !only_pools.count(i.first))
4034 continue;
4035 for (unsigned ps = 0; ps < i.second.get_pg_num(); ++ps) {
4036 pg_t pg(ps, i.first);
4037 vector<int> up;
4038 tmp.pg_to_up_acting_osds(pg, &up, nullptr, nullptr, nullptr);
4039 for (auto osd : up) {
4040 if (osd != CRUSH_ITEM_NONE)
4041 pgs_by_osd[osd].insert(pg);
4042 }
4043 }
4044 total_pgs += i.second.get_size() * i.second.get_pg_num();
31f18b77
FG
4045
4046 map<int,float> pmap;
4047 int ruleno = tmp.crush->find_rule(i.second.get_crush_rule(),
4048 i.second.get_type(),
4049 i.second.get_size());
4050 tmp.crush->get_rule_weight_osd_map(ruleno, &pmap);
4051 ldout(cct,30) << __func__ << " pool " << i.first << " ruleno " << ruleno << dendl;
4052 for (auto p : pmap) {
3efd9988 4053 auto adjusted_weight = tmp.get_weightf(p.first) * p.second;
94b18763
FG
4054 if (adjusted_weight == 0) {
4055 continue;
4056 }
3efd9988
FG
4057 osd_weight[p.first] += adjusted_weight;
4058 osd_weight_total += adjusted_weight;
31f18b77 4059 }
7c673cae 4060 }
31f18b77
FG
4061 for (auto& i : osd_weight) {
4062 int pgs = 0;
4063 auto p = pgs_by_osd.find(i.first);
4064 if (p != pgs_by_osd.end())
4065 pgs = p->second.size();
4066 else
4067 pgs_by_osd.emplace(i.first, set<pg_t>());
4068 ldout(cct, 20) << " osd." << i.first << " weight " << i.second
4069 << " pgs " << pgs << dendl;
7c673cae
FG
4070 }
4071
224ce89b
WB
4072 if (osd_weight_total == 0) {
4073 lderr(cct) << __func__ << " abort due to osd_weight_total == 0" << dendl;
4074 break;
4075 }
7c673cae
FG
4076 float pgs_per_weight = total_pgs / osd_weight_total;
4077 ldout(cct, 10) << " osd_weight_total " << osd_weight_total << dendl;
4078 ldout(cct, 10) << " pgs_per_weight " << pgs_per_weight << dendl;
4079
4080 // osd deviation
31f18b77 4081 float total_deviation = 0;
7c673cae
FG
4082 map<int,float> osd_deviation; // osd, deviation(pgs)
4083 multimap<float,int> deviation_osd; // deviation(pgs), osd
4084 set<int> overfull;
4085 for (auto& i : pgs_by_osd) {
f64942e4
AA
4086 // make sure osd is still there (belongs to this crush-tree)
4087 assert(osd_weight.count(i.first));
7c673cae
FG
4088 float target = osd_weight[i.first] * pgs_per_weight;
4089 float deviation = (float)i.second.size() - target;
4090 ldout(cct, 20) << " osd." << i.first
4091 << "\tpgs " << i.second.size()
4092 << "\ttarget " << target
4093 << "\tdeviation " << deviation
4094 << dendl;
4095 osd_deviation[i.first] = deviation;
4096 deviation_osd.insert(make_pair(deviation, i.first));
31f18b77 4097 if (deviation >= 1.0)
7c673cae 4098 overfull.insert(i.first);
31f18b77
FG
4099 total_deviation += abs(deviation);
4100 }
4101 if (num_changed == 0) {
4102 start_deviation = total_deviation;
7c673cae 4103 }
31f18b77 4104 end_deviation = total_deviation;
7c673cae
FG
4105
4106 // build underfull, sorted from least-full to most-average
4107 vector<int> underfull;
4108 for (auto i = deviation_osd.begin();
4109 i != deviation_osd.end();
4110 ++i) {
4111 if (i->first >= -.999)
4112 break;
4113 underfull.push_back(i->second);
4114 }
31f18b77
FG
4115 ldout(cct, 10) << " total_deviation " << total_deviation
4116 << " overfull " << overfull
7c673cae
FG
4117 << " underfull " << underfull << dendl;
4118 if (overfull.empty() || underfull.empty())
4119 break;
4120
4121 // pick fullest
4122 bool restart = false;
4123 for (auto p = deviation_osd.rbegin(); p != deviation_osd.rend(); ++p) {
4124 int osd = p->second;
31f18b77 4125 float deviation = p->first;
7c673cae 4126 float target = osd_weight[osd] * pgs_per_weight;
224ce89b 4127 assert(target > 0);
31f18b77 4128 if (deviation/target < max_deviation_ratio) {
7c673cae
FG
4129 ldout(cct, 10) << " osd." << osd
4130 << " target " << target
4131 << " deviation " << deviation
31f18b77
FG
4132 << " -> ratio " << deviation/target
4133 << " < max ratio " << max_deviation_ratio << dendl;
7c673cae
FG
4134 break;
4135 }
4136 int num_to_move = deviation;
4137 ldout(cct, 10) << " osd." << osd << " move " << num_to_move << dendl;
4138 if (num_to_move < 1)
4139 break;
4140
4141 set<pg_t>& pgs = pgs_by_osd[osd];
4142
4143 // look for remaps we can un-remap
4144 for (auto pg : pgs) {
4145 auto p = tmp.pg_upmap_items.find(pg);
4146 if (p != tmp.pg_upmap_items.end()) {
4147 for (auto q : p->second) {
4148 if (q.second == osd) {
4149 ldout(cct, 10) << " dropping pg_upmap_items " << pg
4150 << " " << p->second << dendl;
4151 tmp.pg_upmap_items.erase(p);
4152 pending_inc->old_pg_upmap_items.insert(pg);
4153 ++num_changed;
4154 restart = true;
4155 }
4156 }
4157 }
4158 if (restart)
4159 break;
4160 } // pg loop
4161 if (restart)
4162 break;
4163
4164 for (auto pg : pgs) {
4165 if (tmp.pg_upmap.count(pg) ||
4166 tmp.pg_upmap_items.count(pg)) {
4167 ldout(cct, 20) << " already remapped " << pg << dendl;
4168 continue;
4169 }
4170 ldout(cct, 10) << " trying " << pg << dendl;
4171 vector<int> orig, out;
4172 if (!try_pg_upmap(cct, pg, overfull, underfull, &orig, &out)) {
4173 continue;
4174 }
4175 ldout(cct, 10) << " " << pg << " " << orig << " -> " << out << dendl;
4176 if (orig.size() != out.size()) {
4177 continue;
4178 }
4179 assert(orig != out);
4180 auto& rmi = tmp.pg_upmap_items[pg];
4181 for (unsigned i = 0; i < out.size(); ++i) {
4182 if (orig[i] != out[i]) {
4183 rmi.push_back(make_pair(orig[i], out[i]));
4184 }
4185 }
4186 pending_inc->new_pg_upmap_items[pg] = rmi;
4187 ldout(cct, 10) << " " << pg << " pg_upmap_items " << rmi << dendl;
4188 restart = true;
4189 ++num_changed;
4190 break;
4191 } // pg loop
4192 if (restart)
4193 break;
4194 } // osd loop
4195
4196 if (!restart) {
4197 ldout(cct, 10) << " failed to find any changes to make" << dendl;
4198 break;
4199 }
4200 if (--max == 0) {
4201 ldout(cct, 10) << " hit max iterations, stopping" << dendl;
4202 break;
4203 }
4204 }
31f18b77
FG
4205 ldout(cct, 10) << " start deviation " << start_deviation << dendl;
4206 ldout(cct, 10) << " end deviation " << end_deviation << dendl;
7c673cae
FG
4207 return num_changed;
4208}
31f18b77
FG
4209
4210int OSDMap::get_osds_by_bucket_name(const string &name, set<int> *osds) const
4211{
4212 return crush->get_leaves(name, osds);
4213}
4214
3efd9988
FG
4215// get pools whose crush rules might reference the given osd
4216void OSDMap::get_pool_ids_by_osd(CephContext *cct,
4217 int osd,
4218 set<int64_t> *pool_ids) const
4219{
4220 assert(pool_ids);
4221 set<int> raw_rules;
4222 int r = crush->get_rules_by_osd(osd, &raw_rules);
4223 if (r < 0) {
4224 lderr(cct) << __func__ << " get_rules_by_osd failed: " << cpp_strerror(r)
4225 << dendl;
4226 assert(r >= 0);
4227 }
4228 set<int> rules;
4229 for (auto &i: raw_rules) {
4230 // exclude any dead rule
4231 if (crush_rule_in_use(i)) {
4232 rules.insert(i);
4233 }
4234 }
4235 for (auto &r: rules) {
4236 get_pool_ids_by_rule(r, pool_ids);
4237 }
4238}
4239
31f18b77
FG
4240template <typename F>
4241class OSDUtilizationDumper : public CrushTreeDumper::Dumper<F> {
4242public:
4243 typedef CrushTreeDumper::Dumper<F> Parent;
4244
4245 OSDUtilizationDumper(const CrushWrapper *crush, const OSDMap *osdmap_,
4246 const PGStatService *pgs_, bool tree_) :
c07f9fc5 4247 Parent(crush, osdmap_->get_pool_names()),
31f18b77
FG
4248 osdmap(osdmap_),
4249 pgs(pgs_),
4250 tree(tree_),
4251 average_util(average_utilization()),
4252 min_var(-1),
4253 max_var(-1),
4254 stddev(0),
4255 sum(0) {
4256 }
4257
4258protected:
4259 void dump_stray(F *f) {
4260 for (int i = 0; i < osdmap->get_max_osd(); i++) {
4261 if (osdmap->exists(i) && !this->is_touched(i))
c07f9fc5 4262 dump_item(CrushTreeDumper::Item(i, 0, 0, 0), f);
31f18b77
FG
4263 }
4264 }
4265
4266 void dump_item(const CrushTreeDumper::Item &qi, F *f) override {
4267 if (!tree && qi.is_bucket())
4268 return;
4269
4270 float reweight = qi.is_bucket() ? -1 : osdmap->get_weightf(qi.id);
4271 int64_t kb = 0, kb_used = 0, kb_avail = 0;
4272 double util = 0;
4273 if (get_bucket_utilization(qi.id, &kb, &kb_used, &kb_avail))
4274 if (kb_used && kb)
4275 util = 100.0 * (double)kb_used / (double)kb;
4276
4277 double var = 1.0;
4278 if (average_util)
4279 var = util / average_util;
4280
4281 size_t num_pgs = qi.is_bucket() ? 0 : pgs->get_num_pg_by_osd(qi.id);
4282
4283 dump_item(qi, reweight, kb, kb_used, kb_avail, util, var, num_pgs, f);
4284
4285 if (!qi.is_bucket() && reweight > 0) {
4286 if (min_var < 0 || var < min_var)
4287 min_var = var;
4288 if (max_var < 0 || var > max_var)
4289 max_var = var;
4290
4291 double dev = util - average_util;
4292 dev *= dev;
4293 stddev += reweight * dev;
4294 sum += reweight;
4295 }
4296 }
4297
4298 virtual void dump_item(const CrushTreeDumper::Item &qi,
4299 float &reweight,
4300 int64_t kb,
4301 int64_t kb_used,
4302 int64_t kb_avail,
4303 double& util,
4304 double& var,
4305 const size_t num_pgs,
4306 F *f) = 0;
4307
4308 double dev() {
4309 return sum > 0 ? sqrt(stddev / sum) : 0;
4310 }
4311
4312 double average_utilization() {
4313 int64_t kb = 0, kb_used = 0;
4314 for (int i = 0; i < osdmap->get_max_osd(); i++) {
4315 if (!osdmap->exists(i) || osdmap->get_weight(i) == 0)
4316 continue;
4317 int64_t kb_i, kb_used_i, kb_avail_i;
4318 if (get_osd_utilization(i, &kb_i, &kb_used_i, &kb_avail_i)) {
4319 kb += kb_i;
4320 kb_used += kb_used_i;
4321 }
4322 }
4323 return kb > 0 ? 100.0 * (double)kb_used / (double)kb : 0;
4324 }
4325
4326 bool get_osd_utilization(int id, int64_t* kb, int64_t* kb_used,
4327 int64_t* kb_avail) const {
4328 const osd_stat_t *p = pgs->get_osd_stat(id);
4329 if (!p) return false;
4330 *kb = p->kb;
4331 *kb_used = p->kb_used;
4332 *kb_avail = p->kb_avail;
4333 return *kb > 0;
4334 }
4335
4336 bool get_bucket_utilization(int id, int64_t* kb, int64_t* kb_used,
4337 int64_t* kb_avail) const {
4338 if (id >= 0) {
4339 if (osdmap->is_out(id)) {
4340 *kb = 0;
4341 *kb_used = 0;
4342 *kb_avail = 0;
4343 return true;
4344 }
4345 return get_osd_utilization(id, kb, kb_used, kb_avail);
4346 }
4347
4348 *kb = 0;
4349 *kb_used = 0;
4350 *kb_avail = 0;
4351
4352 for (int k = osdmap->crush->get_bucket_size(id) - 1; k >= 0; k--) {
4353 int item = osdmap->crush->get_bucket_item(id, k);
4354 int64_t kb_i = 0, kb_used_i = 0, kb_avail_i = 0;
4355 if (!get_bucket_utilization(item, &kb_i, &kb_used_i, &kb_avail_i))
4356 return false;
4357 *kb += kb_i;
4358 *kb_used += kb_used_i;
4359 *kb_avail += kb_avail_i;
4360 }
4361 return *kb > 0;
4362 }
4363
4364protected:
4365 const OSDMap *osdmap;
4366 const PGStatService *pgs;
4367 bool tree;
4368 double average_util;
4369 double min_var;
4370 double max_var;
4371 double stddev;
4372 double sum;
4373};
4374
4375
4376class OSDUtilizationPlainDumper : public OSDUtilizationDumper<TextTable> {
4377public:
4378 typedef OSDUtilizationDumper<TextTable> Parent;
4379
4380 OSDUtilizationPlainDumper(const CrushWrapper *crush, const OSDMap *osdmap,
4381 const PGStatService *pgs, bool tree) :
4382 Parent(crush, osdmap, pgs, tree) {}
4383
4384 void dump(TextTable *tbl) {
4385 tbl->define_column("ID", TextTable::LEFT, TextTable::RIGHT);
224ce89b 4386 tbl->define_column("CLASS", TextTable::LEFT, TextTable::RIGHT);
31f18b77
FG
4387 tbl->define_column("WEIGHT", TextTable::LEFT, TextTable::RIGHT);
4388 tbl->define_column("REWEIGHT", TextTable::LEFT, TextTable::RIGHT);
4389 tbl->define_column("SIZE", TextTable::LEFT, TextTable::RIGHT);
4390 tbl->define_column("USE", TextTable::LEFT, TextTable::RIGHT);
4391 tbl->define_column("AVAIL", TextTable::LEFT, TextTable::RIGHT);
4392 tbl->define_column("%USE", TextTable::LEFT, TextTable::RIGHT);
4393 tbl->define_column("VAR", TextTable::LEFT, TextTable::RIGHT);
4394 tbl->define_column("PGS", TextTable::LEFT, TextTable::RIGHT);
4395 if (tree)
4396 tbl->define_column("TYPE NAME", TextTable::LEFT, TextTable::LEFT);
4397
4398 Parent::dump(tbl);
4399
4400 dump_stray(tbl);
4401
224ce89b
WB
4402 *tbl << ""
4403 << ""
4404 << "" << "TOTAL"
1adf2230
AA
4405 << byte_u_t(pgs->get_osd_sum().kb << 10)
4406 << byte_u_t(pgs->get_osd_sum().kb_used << 10)
4407 << byte_u_t(pgs->get_osd_sum().kb_avail << 10)
31f18b77
FG
4408 << lowprecision_t(average_util)
4409 << ""
4410 << TextTable::endrow;
4411 }
4412
4413protected:
4414 struct lowprecision_t {
4415 float v;
4416 explicit lowprecision_t(float _v) : v(_v) {}
4417 };
4418 friend std::ostream &operator<<(ostream& out, const lowprecision_t& v);
4419
4420 using OSDUtilizationDumper<TextTable>::dump_item;
4421 void dump_item(const CrushTreeDumper::Item &qi,
4422 float &reweight,
4423 int64_t kb,
4424 int64_t kb_used,
4425 int64_t kb_avail,
4426 double& util,
4427 double& var,
4428 const size_t num_pgs,
4429 TextTable *tbl) override {
224ce89b
WB
4430 const char *c = crush->get_item_class(qi.id);
4431 if (!c)
4432 c = "";
31f18b77 4433 *tbl << qi.id
224ce89b 4434 << c
31f18b77
FG
4435 << weightf_t(qi.weight)
4436 << weightf_t(reweight)
1adf2230
AA
4437 << byte_u_t(kb << 10)
4438 << byte_u_t(kb_used << 10)
4439 << byte_u_t(kb_avail << 10)
31f18b77
FG
4440 << lowprecision_t(util)
4441 << lowprecision_t(var);
4442
4443 if (qi.is_bucket()) {
4444 *tbl << "-";
4445 } else {
4446 *tbl << num_pgs;
4447 }
4448
4449 if (tree) {
4450 ostringstream name;
4451 for (int k = 0; k < qi.depth; k++)
4452 name << " ";
4453 if (qi.is_bucket()) {
4454 int type = crush->get_bucket_type(qi.id);
4455 name << crush->get_type_name(type) << " "
4456 << crush->get_item_name(qi.id);
4457 } else {
4458 name << "osd." << qi.id;
4459 }
4460 *tbl << name.str();
4461 }
4462
4463 *tbl << TextTable::endrow;
4464 }
4465
4466public:
4467 string summary() {
4468 ostringstream out;
4469 out << "MIN/MAX VAR: " << lowprecision_t(min_var)
4470 << "/" << lowprecision_t(max_var) << " "
4471 << "STDDEV: " << lowprecision_t(dev());
4472 return out.str();
4473 }
4474};
4475
4476ostream& operator<<(ostream& out,
4477 const OSDUtilizationPlainDumper::lowprecision_t& v)
4478{
4479 if (v.v < -0.01) {
4480 return out << "-";
4481 } else if (v.v < 0.001) {
4482 return out << "0";
4483 } else {
4484 std::streamsize p = out.precision();
4485 return out << std::fixed << std::setprecision(2) << v.v << std::setprecision(p);
4486 }
4487}
4488
4489class OSDUtilizationFormatDumper : public OSDUtilizationDumper<Formatter> {
4490public:
4491 typedef OSDUtilizationDumper<Formatter> Parent;
4492
4493 OSDUtilizationFormatDumper(const CrushWrapper *crush, const OSDMap *osdmap,
4494 const PGStatService *pgs, bool tree) :
4495 Parent(crush, osdmap, pgs, tree) {}
4496
4497 void dump(Formatter *f) {
4498 f->open_array_section("nodes");
4499 Parent::dump(f);
4500 f->close_section();
4501
4502 f->open_array_section("stray");
4503 dump_stray(f);
4504 f->close_section();
4505 }
4506
4507protected:
4508 using OSDUtilizationDumper<Formatter>::dump_item;
4509 void dump_item(const CrushTreeDumper::Item &qi,
4510 float &reweight,
4511 int64_t kb,
4512 int64_t kb_used,
4513 int64_t kb_avail,
4514 double& util,
4515 double& var,
4516 const size_t num_pgs,
4517 Formatter *f) override {
4518 f->open_object_section("item");
c07f9fc5 4519 CrushTreeDumper::dump_item_fields(crush, weight_set_names, qi, f);
31f18b77
FG
4520 f->dump_float("reweight", reweight);
4521 f->dump_int("kb", kb);
4522 f->dump_int("kb_used", kb_used);
4523 f->dump_int("kb_avail", kb_avail);
4524 f->dump_float("utilization", util);
4525 f->dump_float("var", var);
4526 f->dump_unsigned("pgs", num_pgs);
4527 CrushTreeDumper::dump_bucket_children(crush, qi, f);
4528 f->close_section();
4529 }
4530
4531public:
4532 void summary(Formatter *f) {
4533 f->open_object_section("summary");
4534 f->dump_int("total_kb", pgs->get_osd_sum().kb);
4535 f->dump_int("total_kb_used", pgs->get_osd_sum().kb_used);
4536 f->dump_int("total_kb_avail", pgs->get_osd_sum().kb_avail);
4537 f->dump_float("average_utilization", average_util);
4538 f->dump_float("min_var", min_var);
4539 f->dump_float("max_var", max_var);
4540 f->dump_float("dev", dev());
4541 f->close_section();
4542 }
4543};
4544
4545void print_osd_utilization(const OSDMap& osdmap,
4546 const PGStatService *pgstat,
4547 ostream& out,
4548 Formatter *f,
4549 bool tree)
4550{
4551 const CrushWrapper *crush = osdmap.crush.get();
4552 if (f) {
4553 f->open_object_section("df");
4554 OSDUtilizationFormatDumper d(crush, &osdmap, pgstat, tree);
4555 d.dump(f);
4556 d.summary(f);
4557 f->close_section();
4558 f->flush(out);
4559 } else {
4560 OSDUtilizationPlainDumper d(crush, &osdmap, pgstat, tree);
4561 TextTable tbl;
4562 d.dump(&tbl);
4563 out << tbl << d.summary() << "\n";
4564 }
4565}
224ce89b
WB
4566
4567void OSDMap::check_health(health_check_map_t *checks) const
4568{
4569 int num_osds = get_num_osds();
4570
4571 // OSD_DOWN
4572 // OSD_$subtree_DOWN
4573 // OSD_ORPHAN
4574 if (num_osds >= 0) {
4575 int num_in_osds = 0;
4576 int num_down_in_osds = 0;
4577 set<int> osds;
4578 set<int> down_in_osds;
4579 set<int> up_in_osds;
4580 set<int> subtree_up;
4581 unordered_map<int, set<int> > subtree_type_down;
4582 unordered_map<int, int> num_osds_subtree;
4583 int max_type = crush->get_max_type_id();
4584
4585 for (int i = 0; i < get_max_osd(); i++) {
4586 if (!exists(i)) {
4587 if (crush->item_exists(i)) {
4588 osds.insert(i);
4589 }
4590 continue;
4591 }
4592 if (is_out(i))
4593 continue;
4594 ++num_in_osds;
4595 if (down_in_osds.count(i) || up_in_osds.count(i))
4596 continue;
4597 if (!is_up(i)) {
4598 down_in_osds.insert(i);
4599 int parent_id = 0;
4600 int current = i;
4601 for (int type = 0; type <= max_type; type++) {
4602 if (!crush->get_type_name(type))
4603 continue;
4604 int r = crush->get_immediate_parent_id(current, &parent_id);
4605 if (r == -ENOENT)
4606 break;
4607 // break early if this parent is already marked as up
4608 if (subtree_up.count(parent_id))
4609 break;
4610 type = crush->get_bucket_type(parent_id);
4611 if (!subtree_type_is_down(
4612 g_ceph_context, parent_id, type,
4613 &down_in_osds, &up_in_osds, &subtree_up, &subtree_type_down))
4614 break;
4615 current = parent_id;
4616 }
4617 }
4618 }
4619
4620 // calculate the number of down osds in each down subtree and
4621 // store it in num_osds_subtree
4622 for (int type = 1; type <= max_type; type++) {
4623 if (!crush->get_type_name(type))
4624 continue;
4625 for (auto j = subtree_type_down[type].begin();
4626 j != subtree_type_down[type].end();
4627 ++j) {
4628 list<int> children;
4629 int num = 0;
4630 int num_children = crush->get_children(*j, &children);
4631 if (num_children == 0)
4632 continue;
4633 for (auto l = children.begin(); l != children.end(); ++l) {
4634 if (*l >= 0) {
4635 ++num;
4636 } else if (num_osds_subtree[*l] > 0) {
4637 num = num + num_osds_subtree[*l];
4638 }
4639 }
4640 num_osds_subtree[*j] = num;
4641 }
4642 }
4643 num_down_in_osds = down_in_osds.size();
4644 assert(num_down_in_osds <= num_in_osds);
4645 if (num_down_in_osds > 0) {
4646 // summary of down subtree types and osds
4647 for (int type = max_type; type > 0; type--) {
4648 if (!crush->get_type_name(type))
4649 continue;
4650 if (subtree_type_down[type].size() > 0) {
4651 ostringstream ss;
4652 ss << subtree_type_down[type].size() << " "
4653 << crush->get_type_name(type);
4654 if (subtree_type_down[type].size() > 1) {
4655 ss << "s";
4656 }
4657 int sum_down_osds = 0;
4658 for (auto j = subtree_type_down[type].begin();
4659 j != subtree_type_down[type].end();
4660 ++j) {
4661 sum_down_osds = sum_down_osds + num_osds_subtree[*j];
4662 }
4663 ss << " (" << sum_down_osds << " osds) down";
4664 string err = string("OSD_") +
4665 string(crush->get_type_name(type)) + "_DOWN";
4666 boost::to_upper(err);
4667 auto& d = checks->add(err, HEALTH_WARN, ss.str());
4668 for (auto j = subtree_type_down[type].rbegin();
4669 j != subtree_type_down[type].rend();
4670 ++j) {
4671 ostringstream ss;
4672 ss << crush->get_type_name(type);
4673 ss << " ";
4674 ss << crush->get_item_name(*j);
4675 // at the top level, do not print location
4676 if (type != max_type) {
4677 ss << " (";
4678 ss << crush->get_full_location_ordered_string(*j);
4679 ss << ")";
4680 }
4681 int num = num_osds_subtree[*j];
4682 ss << " (" << num << " osds)";
4683 ss << " is down";
4684 d.detail.push_back(ss.str());
4685 }
4686 }
4687 }
4688 ostringstream ss;
4689 ss << down_in_osds.size() << " osds down";
4690 auto& d = checks->add("OSD_DOWN", HEALTH_WARN, ss.str());
4691 for (auto it = down_in_osds.begin(); it != down_in_osds.end(); ++it) {
4692 ostringstream ss;
4693 ss << "osd." << *it << " (";
4694 ss << crush->get_full_location_ordered_string(*it);
4695 ss << ") is down";
4696 d.detail.push_back(ss.str());
4697 }
4698 }
4699
4700 if (!osds.empty()) {
4701 ostringstream ss;
4702 ss << osds.size() << " osds exist in the crush map but not in the osdmap";
4703 auto& d = checks->add("OSD_ORPHAN", HEALTH_WARN, ss.str());
4704 for (auto osd : osds) {
4705 ostringstream ss;
4706 ss << "osd." << osd << " exists in crush map but not in osdmap";
4707 d.detail.push_back(ss.str());
4708 }
4709 }
4710 }
4711
4712 // OSD_OUT_OF_ORDER_FULL
4713 {
4714 // An osd could configure failsafe ratio, to something different
4715 // but for now assume it is the same here.
4716 float fsr = g_conf->osd_failsafe_full_ratio;
4717 if (fsr > 1.0) fsr /= 100;
4718 float fr = get_full_ratio();
4719 float br = get_backfillfull_ratio();
4720 float nr = get_nearfull_ratio();
4721
4722 list<string> detail;
4723 // These checks correspond to how OSDService::check_full_status() in an OSD
4724 // handles the improper setting of these values.
4725 if (br < nr) {
4726 ostringstream ss;
4727 ss << "backfillfull_ratio (" << br
4728 << ") < nearfull_ratio (" << nr << "), increased";
4729 detail.push_back(ss.str());
4730 br = nr;
4731 }
4732 if (fr < br) {
4733 ostringstream ss;
4734 ss << "full_ratio (" << fr << ") < backfillfull_ratio (" << br
4735 << "), increased";
4736 detail.push_back(ss.str());
4737 fr = br;
4738 }
4739 if (fsr < fr) {
4740 ostringstream ss;
4741 ss << "osd_failsafe_full_ratio (" << fsr << ") < full_ratio (" << fr
4742 << "), increased";
4743 detail.push_back(ss.str());
4744 }
4745 if (!detail.empty()) {
4746 auto& d = checks->add("OSD_OUT_OF_ORDER_FULL", HEALTH_ERR,
4747 "full ratio(s) out of order");
4748 d.detail.swap(detail);
4749 }
4750 }
4751
4752 // OSD_FULL
4753 // OSD_NEARFULL
4754 // OSD_BACKFILLFULL
4755 // OSD_FAILSAFE_FULL
4756 {
4757 set<int> full, backfillfull, nearfull;
4758 get_full_osd_counts(&full, &backfillfull, &nearfull);
4759 if (full.size()) {
4760 ostringstream ss;
4761 ss << full.size() << " full osd(s)";
4762 auto& d = checks->add("OSD_FULL", HEALTH_ERR, ss.str());
4763 for (auto& i: full) {
4764 ostringstream ss;
4765 ss << "osd." << i << " is full";
4766 d.detail.push_back(ss.str());
4767 }
4768 }
4769 if (backfillfull.size()) {
4770 ostringstream ss;
4771 ss << backfillfull.size() << " backfillfull osd(s)";
4772 auto& d = checks->add("OSD_BACKFILLFULL", HEALTH_WARN, ss.str());
4773 for (auto& i: backfillfull) {
4774 ostringstream ss;
4775 ss << "osd." << i << " is backfill full";
4776 d.detail.push_back(ss.str());
4777 }
4778 }
4779 if (nearfull.size()) {
4780 ostringstream ss;
4781 ss << nearfull.size() << " nearfull osd(s)";
4782 auto& d = checks->add("OSD_NEARFULL", HEALTH_WARN, ss.str());
4783 for (auto& i: nearfull) {
4784 ostringstream ss;
4785 ss << "osd." << i << " is near full";
4786 d.detail.push_back(ss.str());
4787 }
4788 }
4789 }
4790
4791 // OSDMAP_FLAGS
4792 {
4793 // warn about flags
4794 uint64_t warn_flags =
3efd9988 4795 CEPH_OSDMAP_NEARFULL |
224ce89b
WB
4796 CEPH_OSDMAP_FULL |
4797 CEPH_OSDMAP_PAUSERD |
4798 CEPH_OSDMAP_PAUSEWR |
4799 CEPH_OSDMAP_PAUSEREC |
4800 CEPH_OSDMAP_NOUP |
4801 CEPH_OSDMAP_NODOWN |
4802 CEPH_OSDMAP_NOIN |
4803 CEPH_OSDMAP_NOOUT |
4804 CEPH_OSDMAP_NOBACKFILL |
4805 CEPH_OSDMAP_NORECOVER |
4806 CEPH_OSDMAP_NOSCRUB |
4807 CEPH_OSDMAP_NODEEP_SCRUB |
4808 CEPH_OSDMAP_NOTIERAGENT |
4809 CEPH_OSDMAP_NOREBALANCE;
4810 if (test_flag(warn_flags)) {
4811 ostringstream ss;
4812 ss << get_flag_string(get_flags() & warn_flags)
4813 << " flag(s) set";
4814 checks->add("OSDMAP_FLAGS", HEALTH_WARN, ss.str());
4815 }
4816 }
4817
4818 // OSD_FLAGS
4819 {
4820 list<string> detail;
4821 const unsigned flags =
4822 CEPH_OSD_NOUP |
4823 CEPH_OSD_NOIN |
4824 CEPH_OSD_NODOWN |
4825 CEPH_OSD_NOOUT;
4826 for (int i = 0; i < max_osd; ++i) {
4827 if (osd_state[i] & flags) {
4828 ostringstream ss;
4829 set<string> states;
4830 OSDMap::calc_state_set(osd_state[i] & flags, states);
4831 ss << "osd." << i << " has flags " << states;
4832 detail.push_back(ss.str());
4833 }
4834 }
4835 if (!detail.empty()) {
4836 ostringstream ss;
4837 ss << detail.size() << " osd(s) have {NOUP,NODOWN,NOIN,NOOUT} flags set";
4838 auto& d = checks->add("OSD_FLAGS", HEALTH_WARN, ss.str());
4839 d.detail.swap(detail);
4840 }
4841 }
4842
4843 // OLD_CRUSH_TUNABLES
4844 if (g_conf->mon_warn_on_legacy_crush_tunables) {
4845 string min = crush->get_min_required_version();
4846 if (min < g_conf->mon_crush_min_required_version) {
4847 ostringstream ss;
4848 ss << "crush map has legacy tunables (require " << min
4849 << ", min is " << g_conf->mon_crush_min_required_version << ")";
4850 auto& d = checks->add("OLD_CRUSH_TUNABLES", HEALTH_WARN, ss.str());
4851 d.detail.push_back("see http://docs.ceph.com/docs/master/rados/operations/crush-map/#tunables");
4852 }
4853 }
4854
4855 // OLD_CRUSH_STRAW_CALC_VERSION
4856 if (g_conf->mon_warn_on_crush_straw_calc_version_zero) {
4857 if (crush->get_straw_calc_version() == 0) {
4858 ostringstream ss;
4859 ss << "crush map has straw_calc_version=0";
4860 auto& d = checks->add("OLD_CRUSH_STRAW_CALC_VERSION", HEALTH_WARN, ss.str());
4861 d.detail.push_back(
4862 "see http://docs.ceph.com/docs/master/rados/operations/crush-map/#tunables");
4863 }
4864 }
4865
4866 // CACHE_POOL_NO_HIT_SET
4867 if (g_conf->mon_warn_on_cache_pools_without_hit_sets) {
4868 list<string> detail;
4869 for (map<int64_t, pg_pool_t>::const_iterator p = pools.begin();
4870 p != pools.end();
4871 ++p) {
4872 const pg_pool_t& info = p->second;
4873 if (info.cache_mode_requires_hit_set() &&
4874 info.hit_set_params.get_type() == HitSet::TYPE_NONE) {
4875 ostringstream ss;
4876 ss << "pool '" << get_pool_name(p->first)
4877 << "' with cache_mode " << info.get_cache_mode_name()
4878 << " needs hit_set_type to be set but it is not";
4879 detail.push_back(ss.str());
4880 }
4881 }
4882 if (!detail.empty()) {
4883 ostringstream ss;
4884 ss << detail.size() << " cache pools are missing hit_sets";
4885 auto& d = checks->add("CACHE_POOL_NO_HIT_SET", HEALTH_WARN, ss.str());
4886 d.detail.swap(detail);
4887 }
4888 }
4889
4890 // OSD_NO_SORTBITWISE
4891 if (!test_flag(CEPH_OSDMAP_SORTBITWISE) &&
4892 (get_up_osd_features() &
4893 CEPH_FEATURE_OSD_BITWISE_HOBJ_SORT)) {
4894 ostringstream ss;
4895 ss << "no legacy OSD present but 'sortbitwise' flag is not set";
4896 checks->add("OSD_NO_SORTBITWISE", HEALTH_WARN, ss.str());
4897 }
4898
4899 // OSD_UPGRADE_FINISHED
4900 // none of these (yet) since we don't run until luminous upgrade is done.
4901
3efd9988 4902 // POOL_NEARFULL/BACKFILLFULL/FULL
224ce89b 4903 {
3efd9988 4904 list<string> full_detail, backfillfull_detail, nearfull_detail;
224ce89b
WB
4905 for (auto it : get_pools()) {
4906 const pg_pool_t &pool = it.second;
3efd9988 4907 const string& pool_name = get_pool_name(it.first);
224ce89b 4908 if (pool.has_flag(pg_pool_t::FLAG_FULL)) {
224ce89b 4909 stringstream ss;
3efd9988
FG
4910 if (pool.has_flag(pg_pool_t::FLAG_FULL_NO_QUOTA)) {
4911 // may run out of space too,
4912 // but we want EQUOTA taking precedence
4913 ss << "pool '" << pool_name << "' is full (no quota)";
4914 } else {
4915 ss << "pool '" << pool_name << "' is full (no space)";
4916 }
4917 full_detail.push_back(ss.str());
4918 } else if (pool.has_flag(pg_pool_t::FLAG_BACKFILLFULL)) {
4919 stringstream ss;
4920 ss << "pool '" << pool_name << "' is backfillfull";
4921 backfillfull_detail.push_back(ss.str());
4922 } else if (pool.has_flag(pg_pool_t::FLAG_NEARFULL)) {
4923 stringstream ss;
4924 ss << "pool '" << pool_name << "' is nearfull";
4925 nearfull_detail.push_back(ss.str());
224ce89b
WB
4926 }
4927 }
3efd9988 4928 if (!full_detail.empty()) {
224ce89b 4929 ostringstream ss;
3efd9988 4930 ss << full_detail.size() << " pool(s) full";
224ce89b 4931 auto& d = checks->add("POOL_FULL", HEALTH_WARN, ss.str());
3efd9988
FG
4932 d.detail.swap(full_detail);
4933 }
4934 if (!backfillfull_detail.empty()) {
4935 ostringstream ss;
4936 ss << backfillfull_detail.size() << " pool(s) backfillfull";
4937 auto& d = checks->add("POOL_BACKFILLFULL", HEALTH_WARN, ss.str());
4938 d.detail.swap(backfillfull_detail);
4939 }
4940 if (!nearfull_detail.empty()) {
4941 ostringstream ss;
4942 ss << nearfull_detail.size() << " pool(s) nearfull";
4943 auto& d = checks->add("POOL_NEARFULL", HEALTH_WARN, ss.str());
4944 d.detail.swap(nearfull_detail);
224ce89b
WB
4945 }
4946 }
4947}
35e4c445
FG
4948
4949int OSDMap::parse_osd_id_list(const vector<string>& ls, set<int> *out,
4950 ostream *ss) const
4951{
4952 out->clear();
4953 for (auto i = ls.begin(); i != ls.end(); ++i) {
4954 if (i == ls.begin() &&
4955 (*i == "any" || *i == "all" || *i == "*")) {
4956 get_all_osds(*out);
4957 break;
4958 }
4959 long osd = parse_osd_id(i->c_str(), ss);
4960 if (osd < 0) {
4961 *ss << "invalid osd id '" << *i << "'";
4962 return -EINVAL;
4963 }
4964 out->insert(osd);
4965 }
4966 return 0;
4967}