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