]> git.proxmox.com Git - ceph.git/blob - ceph/src/osd/OSDMap.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / osd / OSDMap.h
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
18
19 #ifndef CEPH_OSDMAP_H
20 #define CEPH_OSDMAP_H
21
22 /*
23 * describe properties of the OSD cluster.
24 * disks, disk groups, total # osds,
25 *
26 */
27 #include <vector>
28 #include <list>
29 #include <set>
30 #include <map>
31 #include <memory>
32
33 #include <boost/smart_ptr/local_shared_ptr.hpp>
34 #include "include/btree_map.h"
35 #include "include/common_fwd.h"
36 #include "include/types.h"
37 #include "common/ceph_releases.h"
38 #include "osd_types.h"
39
40 //#include "include/ceph_features.h"
41 #include "crush/CrushWrapper.h"
42
43 // forward declaration
44 class CrushWrapper;
45 class health_check_map_t;
46
47 /*
48 * we track up to two intervals during which the osd was alive and
49 * healthy. the most recent is [up_from,up_thru), where up_thru is
50 * the last epoch the osd is known to have _started_. i.e., a lower
51 * bound on the actual osd death. down_at (if it is > up_from) is an
52 * upper bound on the actual osd death.
53 *
54 * the second is the last_clean interval [begin,end). in that case,
55 * the last interval is the last epoch known to have been either
56 * _finished_, or during which the osd cleanly shut down. when
57 * possible, we push this forward to the epoch the osd was eventually
58 * marked down.
59 *
60 * the lost_at is used to allow build_prior to proceed without waiting
61 * for an osd to recover. In certain cases, progress may be blocked
62 * because an osd is down that may contain updates (i.e., a pg may have
63 * gone rw during an interval). If the osd can't be brought online, we
64 * can force things to proceed knowing that we _might_ be losing some
65 * acked writes. If the osd comes back to life later, that's fine to,
66 * but those writes will still be lost (the divergent objects will be
67 * thrown out).
68 */
69 struct osd_info_t {
70 epoch_t last_clean_begin; // last interval that ended with a clean osd shutdown
71 epoch_t last_clean_end;
72 epoch_t up_from; // epoch osd marked up
73 epoch_t up_thru; // lower bound on actual osd death (if > up_from)
74 epoch_t down_at; // upper bound on actual osd death (if > up_from)
75 epoch_t lost_at; // last epoch we decided data was "lost"
76
77 osd_info_t() : last_clean_begin(0), last_clean_end(0),
78 up_from(0), up_thru(0), down_at(0), lost_at(0) {}
79
80 void dump(ceph::Formatter *f) const;
81 void encode(ceph::buffer::list& bl) const;
82 void decode(ceph::buffer::list::const_iterator& bl);
83 static void generate_test_instances(std::list<osd_info_t*>& o);
84 };
85 WRITE_CLASS_ENCODER(osd_info_t)
86
87 std::ostream& operator<<(std::ostream& out, const osd_info_t& info);
88
89 struct osd_xinfo_t {
90 utime_t down_stamp; ///< timestamp when we were last marked down
91 float laggy_probability; ///< encoded as __u32: 0 = definitely not laggy, 0xffffffff definitely laggy
92 __u32 laggy_interval; ///< average interval between being marked laggy and recovering
93 uint64_t features; ///< features supported by this osd we should know about
94 __u32 old_weight; ///< weight prior to being auto marked out
95 utime_t last_purged_snaps_scrub; ///< last scrub of purged_snaps
96 epoch_t dead_epoch = 0; ///< last epoch we were confirmed dead (not just down)
97
98 osd_xinfo_t() : laggy_probability(0), laggy_interval(0),
99 features(0), old_weight(0) {}
100
101 void dump(ceph::Formatter *f) const;
102 void encode(ceph::buffer::list& bl, uint64_t features) const;
103 void decode(ceph::buffer::list::const_iterator& bl);
104 static void generate_test_instances(std::list<osd_xinfo_t*>& o);
105 };
106 WRITE_CLASS_ENCODER_FEATURES(osd_xinfo_t)
107
108 std::ostream& operator<<(std::ostream& out, const osd_xinfo_t& xi);
109
110
111 struct PGTempMap {
112 #if 1
113 ceph::buffer::list data;
114 typedef btree::btree_map<pg_t,ceph_le32*> map_t;
115 map_t map;
116
117 void encode(ceph::buffer::list& bl) const {
118 using ceph::encode;
119 uint32_t n = map.size();
120 encode(n, bl);
121 for (auto &p : map) {
122 encode(p.first, bl);
123 bl.append((char*)p.second, (*p.second + 1) * sizeof(ceph_le32));
124 }
125 }
126 void decode(ceph::buffer::list::const_iterator& p) {
127 using ceph::decode;
128 data.clear();
129 map.clear();
130 uint32_t n;
131 decode(n, p);
132 if (!n)
133 return;
134 auto pstart = p;
135 size_t start_off = pstart.get_off();
136 std::vector<std::pair<pg_t,size_t>> offsets;
137 offsets.resize(n);
138 for (unsigned i=0; i<n; ++i) {
139 pg_t pgid;
140 decode(pgid, p);
141 offsets[i].first = pgid;
142 offsets[i].second = p.get_off() - start_off;
143 uint32_t vn;
144 decode(vn, p);
145 p += vn * sizeof(int32_t);
146 }
147 size_t len = p.get_off() - start_off;
148 pstart.copy(len, data);
149 if (data.get_num_buffers() > 1) {
150 data.rebuild();
151 }
152 //map.reserve(n);
153 char *start = data.c_str();
154 for (auto i : offsets) {
155 map.insert(map.end(), std::make_pair(i.first, (ceph_le32*)(start + i.second)));
156 }
157 }
158 void rebuild() {
159 ceph::buffer::list bl;
160 encode(bl);
161 auto p = std::cbegin(bl);
162 decode(p);
163 }
164 friend bool operator==(const PGTempMap& l, const PGTempMap& r) {
165 return
166 l.map.size() == r.map.size() &&
167 l.data.contents_equal(r.data);
168 }
169
170 class iterator {
171 map_t::const_iterator it;
172 map_t::const_iterator end;
173 std::pair<pg_t,std::vector<int32_t>> current;
174 void init_current() {
175 if (it != end) {
176 current.first = it->first;
177 ceph_assert(it->second);
178 current.second.resize(*it->second);
179 ceph_le32 *p = it->second + 1;
180 for (uint32_t n = 0; n < *it->second; ++n, ++p) {
181 current.second[n] = *p;
182 }
183 }
184 }
185 public:
186 iterator(map_t::const_iterator p,
187 map_t::const_iterator e)
188 : it(p), end(e) {
189 init_current();
190 }
191
192 const std::pair<pg_t,std::vector<int32_t>>& operator*() const {
193 return current;
194 }
195 const std::pair<pg_t,std::vector<int32_t>>* operator->() const {
196 return &current;
197 }
198 friend bool operator==(const iterator& l, const iterator& r) {
199 return l.it == r.it;
200 }
201 friend bool operator!=(const iterator& l, const iterator& r) {
202 return l.it != r.it;
203 }
204 iterator& operator++() {
205 ++it;
206 if (it != end)
207 init_current();
208 return *this;
209 }
210 iterator operator++(int) {
211 iterator r = *this;
212 ++it;
213 if (it != end)
214 init_current();
215 return r;
216 }
217 };
218 iterator begin() const {
219 return iterator(map.begin(), map.end());
220 }
221 iterator end() const {
222 return iterator(map.end(), map.end());
223 }
224 iterator find(pg_t pgid) const {
225 return iterator(map.find(pgid), map.end());
226 }
227 size_t size() const {
228 return map.size();
229 }
230 size_t count(pg_t pgid) const {
231 return map.count(pgid);
232 }
233 void erase(pg_t pgid) {
234 map.erase(pgid);
235 }
236 void clear() {
237 map.clear();
238 data.clear();
239 }
240 void set(pg_t pgid, const mempool::osdmap::vector<int32_t>& v) {
241 using ceph::encode;
242 size_t need = sizeof(ceph_le32) * (1 + v.size());
243 if (need < data.get_append_buffer_unused_tail_length()) {
244 ceph::buffer::ptr z(data.get_append_buffer_unused_tail_length());
245 z.zero();
246 data.append(z.c_str(), z.length());
247 }
248 encode(v, data);
249 map[pgid] = (ceph_le32*)(data.back().end_c_str()) - (1 + v.size());
250 }
251 mempool::osdmap::vector<int32_t> get(pg_t pgid) {
252 mempool::osdmap::vector<int32_t> v;
253 ceph_le32 *p = map[pgid];
254 size_t n = *p++;
255 v.resize(n);
256 for (size_t i = 0; i < n; ++i, ++p) {
257 v[i] = *p;
258 }
259 return v;
260 }
261 #else
262 // trivial implementation
263 mempool::osdmap::map<pg_t,mempool::osdmap::vector<int32_t> > pg_temp;
264
265 void encode(ceph::buffer::list& bl) const {
266 encode(pg_temp, bl);
267 }
268 void decode(ceph::buffer::list::const_iterator& p) {
269 decode(pg_temp, p);
270 }
271 friend bool operator==(const PGTempMap& l, const PGTempMap& r) {
272 return
273 l.pg_temp.size() == r.pg_temp.size() &&
274 l.pg_temp == r.pg_temp;
275 }
276
277 class iterator {
278 mempool::osdmap::map<pg_t,mempool::osdmap::vector<int32_t> >::const_iterator it;
279 public:
280 iterator(mempool::osdmap::map<pg_t,
281 mempool::osdmap::vector<int32_t> >::const_iterator p)
282 : it(p) {}
283
284 std::pair<pg_t,const mempool::osdmap::vector<int32_t>&> operator*() const {
285 return *it;
286 }
287 const std::pair<const pg_t,mempool::osdmap::vector<int32_t>>* operator->() const {
288 return &*it;
289 }
290 friend bool operator==(const iterator& l, const iterator& r) {
291 return l.it == r.it;
292 }
293 friend bool operator!=(const iterator& l, const iterator& r) {
294 return l.it != r.it;
295 }
296 iterator& operator++() {
297 ++it;
298 return *this;
299 }
300 iterator operator++(int) {
301 iterator r = *this;
302 ++it;
303 return r;
304 }
305 };
306 iterator begin() const {
307 return iterator(pg_temp.cbegin());
308 }
309 iterator end() const {
310 return iterator(pg_temp.cend());
311 }
312 iterator find(pg_t pgid) const {
313 return iterator(pg_temp.find(pgid));
314 }
315 size_t size() const {
316 return pg_temp.size();
317 }
318 size_t count(pg_t pgid) const {
319 return pg_temp.count(pgid);
320 }
321 void erase(pg_t pgid) {
322 pg_temp.erase(pgid);
323 }
324 void clear() {
325 pg_temp.clear();
326 }
327 void set(pg_t pgid, const mempool::osdmap::vector<int32_t>& v) {
328 pg_temp[pgid] = v;
329 }
330 const mempool::osdmap::vector<int32_t>& get(pg_t pgid) {
331 return pg_temp.at(pgid);
332 }
333 #endif
334 void dump(ceph::Formatter *f) const {
335 for (const auto &pg : *this) {
336 f->open_object_section("osds");
337 f->dump_stream("pgid") << pg.first;
338 f->open_array_section("osds");
339 for (const auto osd : pg.second)
340 f->dump_int("osd", osd);
341 f->close_section();
342 f->close_section();
343 }
344 }
345 };
346 WRITE_CLASS_ENCODER(PGTempMap)
347
348 /** OSDMap
349 */
350 class OSDMap {
351 public:
352 MEMPOOL_CLASS_HELPERS();
353
354 class Incremental {
355 public:
356 MEMPOOL_CLASS_HELPERS();
357
358 /// feature bits we were encoded with. the subsequent OSDMap
359 /// encoding should match.
360 uint64_t encode_features;
361 uuid_d fsid;
362 epoch_t epoch; // new epoch; we are a diff from epoch-1 to epoch
363 utime_t modified;
364 int64_t new_pool_max; //incremented by the OSDMonitor on each pool create
365 int32_t new_flags;
366 ceph_release_t new_require_osd_release{0xff};
367 uint32_t new_stretch_bucket_count{0};
368 uint32_t new_degraded_stretch_mode{0};
369 uint32_t new_recovering_stretch_mode{0};
370 int32_t new_stretch_mode_bucket{0};
371 bool stretch_mode_enabled{false};
372 bool change_stretch_mode{false};
373
374 // full (rare)
375 ceph::buffer::list fullmap; // in lieu of below.
376 ceph::buffer::list crush;
377
378 // incremental
379 int32_t new_max_osd;
380 mempool::osdmap::map<int64_t,pg_pool_t> new_pools;
381 mempool::osdmap::map<int64_t,std::string> new_pool_names;
382 mempool::osdmap::set<int64_t> old_pools;
383 mempool::osdmap::map<std::string,std::map<std::string,std::string> > new_erasure_code_profiles;
384 mempool::osdmap::vector<std::string> old_erasure_code_profiles;
385 mempool::osdmap::map<int32_t,entity_addrvec_t> new_up_client;
386 mempool::osdmap::map<int32_t,entity_addrvec_t> new_up_cluster;
387 mempool::osdmap::map<int32_t,uint32_t> new_state; // XORed onto previous state.
388 mempool::osdmap::map<int32_t,uint32_t> new_weight;
389 mempool::osdmap::map<pg_t,mempool::osdmap::vector<int32_t> > new_pg_temp; // [] to remove
390 mempool::osdmap::map<pg_t, int32_t> new_primary_temp; // [-1] to remove
391 mempool::osdmap::map<int32_t,uint32_t> new_primary_affinity;
392 mempool::osdmap::map<int32_t,epoch_t> new_up_thru;
393 mempool::osdmap::map<int32_t,std::pair<epoch_t,epoch_t> > new_last_clean_interval;
394 mempool::osdmap::map<int32_t,epoch_t> new_lost;
395 mempool::osdmap::map<int32_t,uuid_d> new_uuid;
396 mempool::osdmap::map<int32_t,osd_xinfo_t> new_xinfo;
397
398 mempool::osdmap::map<entity_addr_t,utime_t> new_blocklist;
399 mempool::osdmap::vector<entity_addr_t> old_blocklist;
400 mempool::osdmap::map<int32_t, entity_addrvec_t> new_hb_back_up;
401 mempool::osdmap::map<int32_t, entity_addrvec_t> new_hb_front_up;
402
403 mempool::osdmap::map<pg_t,mempool::osdmap::vector<int32_t>> new_pg_upmap;
404 mempool::osdmap::map<pg_t,mempool::osdmap::vector<std::pair<int32_t,int32_t>>> new_pg_upmap_items;
405 mempool::osdmap::set<pg_t> old_pg_upmap, old_pg_upmap_items;
406 mempool::osdmap::map<int64_t, snap_interval_set_t> new_removed_snaps;
407 mempool::osdmap::map<int64_t, snap_interval_set_t> new_purged_snaps;
408
409 mempool::osdmap::map<int32_t,uint32_t> new_crush_node_flags;
410 mempool::osdmap::map<int32_t,uint32_t> new_device_class_flags;
411
412 std::string cluster_snapshot;
413
414 float new_nearfull_ratio = -1;
415 float new_backfillfull_ratio = -1;
416 float new_full_ratio = -1;
417
418 ceph_release_t new_require_min_compat_client{0xff};
419
420 utime_t new_last_up_change, new_last_in_change;
421
422 mutable bool have_crc; ///< crc values are defined
423 uint32_t full_crc; ///< crc of the resulting OSDMap
424 mutable uint32_t inc_crc; ///< crc of this incremental
425
426 int get_net_marked_out(const OSDMap *previous) const;
427 int get_net_marked_down(const OSDMap *previous) const;
428 int identify_osd(uuid_d u) const;
429
430 void encode_client_old(ceph::buffer::list& bl) const;
431 void encode_classic(ceph::buffer::list& bl, uint64_t features) const;
432 void encode(ceph::buffer::list& bl, uint64_t features=CEPH_FEATURES_ALL) const;
433 void decode_classic(ceph::buffer::list::const_iterator &p);
434 void decode(ceph::buffer::list::const_iterator &bl);
435 void dump(ceph::Formatter *f) const;
436 static void generate_test_instances(std::list<Incremental*>& o);
437
438 explicit Incremental(epoch_t e=0) :
439 encode_features(0),
440 epoch(e), new_pool_max(-1), new_flags(-1), new_max_osd(-1),
441 have_crc(false), full_crc(0), inc_crc(0) {
442 }
443 explicit Incremental(ceph::buffer::list &bl) {
444 auto p = std::cbegin(bl);
445 decode(p);
446 }
447 explicit Incremental(ceph::buffer::list::const_iterator &p) {
448 decode(p);
449 }
450
451 pg_pool_t *get_new_pool(int64_t pool, const pg_pool_t *orig) {
452 if (new_pools.count(pool) == 0)
453 new_pools[pool] = *orig;
454 return &new_pools[pool];
455 }
456 bool has_erasure_code_profile(const std::string &name) const {
457 auto i = new_erasure_code_profiles.find(name);
458 return i != new_erasure_code_profiles.end();
459 }
460 void set_erasure_code_profile(const std::string &name,
461 const std::map<std::string,std::string>& profile) {
462 new_erasure_code_profiles[name] = profile;
463 }
464 mempool::osdmap::map<std::string,std::map<std::string,std::string>> get_erasure_code_profiles() const {
465 return new_erasure_code_profiles;
466 }
467
468 /// propagate update pools' (snap and other) metadata to any of their tiers
469 int propagate_base_properties_to_tiers(CephContext *cct, const OSDMap &base);
470
471 /// filter out osds with any pending state changing
472 size_t get_pending_state_osds(std::vector<int> *osds) {
473 ceph_assert(osds);
474 osds->clear();
475
476 for (auto &p : new_state) {
477 osds->push_back(p.first);
478 }
479
480 return osds->size();
481 }
482
483 bool pending_osd_has_state(int osd, unsigned state) {
484 return new_state.count(osd) && (new_state[osd] & state) != 0;
485 }
486
487 bool pending_osd_state_set(int osd, unsigned state) {
488 if (pending_osd_has_state(osd, state))
489 return false;
490 new_state[osd] |= state;
491 return true;
492 }
493
494 // cancel the specified pending osd state if there is any
495 // return ture on success, false otherwise.
496 bool pending_osd_state_clear(int osd, unsigned state) {
497 if (!pending_osd_has_state(osd, state)) {
498 // never has been set or already has been cancelled.
499 return false;
500 }
501
502 new_state[osd] &= ~state;
503 if (!new_state[osd]) {
504 // all flags cleared
505 new_state.erase(osd);
506 }
507 return true;
508 }
509
510 bool in_new_removed_snaps(int64_t pool, snapid_t snap) const {
511 auto p = new_removed_snaps.find(pool);
512 if (p == new_removed_snaps.end()) {
513 return false;
514 }
515 return p->second.contains(snap);
516 }
517 };
518
519 private:
520 uuid_d fsid;
521 epoch_t epoch; // what epoch of the osd cluster descriptor is this
522 utime_t created, modified; // epoch start time
523 int32_t pool_max; // the largest pool num, ever
524
525 uint32_t flags;
526
527 int num_osd; // not saved; see calc_num_osds
528 int num_up_osd; // not saved; see calc_num_osds
529 int num_in_osd; // not saved; see calc_num_osds
530
531 int32_t max_osd;
532 std::vector<uint32_t> osd_state;
533
534 mempool::osdmap::map<int32_t,uint32_t> crush_node_flags; // crush node -> CEPH_OSD_* flags
535 mempool::osdmap::map<int32_t,uint32_t> device_class_flags; // device class -> CEPH_OSD_* flags
536
537 utime_t last_up_change, last_in_change;
538
539 // These features affect OSDMap[::Incremental] encoding, or the
540 // encoding of some type embedded therein (CrushWrapper, something
541 // from osd_types, etc.).
542 static constexpr uint64_t SIGNIFICANT_FEATURES =
543 CEPH_FEATUREMASK_PGID64 |
544 CEPH_FEATUREMASK_PGPOOL3 |
545 CEPH_FEATUREMASK_OSDENC |
546 CEPH_FEATUREMASK_OSDMAP_ENC |
547 CEPH_FEATUREMASK_OSD_POOLRESEND |
548 CEPH_FEATUREMASK_NEW_OSDOP_ENCODING |
549 CEPH_FEATUREMASK_MSG_ADDR2 |
550 CEPH_FEATUREMASK_CRUSH_TUNABLES5 |
551 CEPH_FEATUREMASK_CRUSH_CHOOSE_ARGS |
552 CEPH_FEATUREMASK_SERVER_LUMINOUS |
553 CEPH_FEATUREMASK_SERVER_MIMIC |
554 CEPH_FEATUREMASK_SERVER_NAUTILUS |
555 CEPH_FEATUREMASK_SERVER_OCTOPUS;
556
557 struct addrs_s {
558 mempool::osdmap::vector<std::shared_ptr<entity_addrvec_t> > client_addrs;
559 mempool::osdmap::vector<std::shared_ptr<entity_addrvec_t> > cluster_addrs;
560 mempool::osdmap::vector<std::shared_ptr<entity_addrvec_t> > hb_back_addrs;
561 mempool::osdmap::vector<std::shared_ptr<entity_addrvec_t> > hb_front_addrs;
562 };
563 std::shared_ptr<addrs_s> osd_addrs;
564
565 entity_addrvec_t _blank_addrvec;
566
567 mempool::osdmap::vector<__u32> osd_weight; // 16.16 fixed point, 0x10000 = "in", 0 = "out"
568 mempool::osdmap::vector<osd_info_t> osd_info;
569 std::shared_ptr<PGTempMap> pg_temp; // temp pg mapping (e.g. while we rebuild)
570 std::shared_ptr< mempool::osdmap::map<pg_t,int32_t > > primary_temp; // temp primary mapping (e.g. while we rebuild)
571 std::shared_ptr< mempool::osdmap::vector<__u32> > osd_primary_affinity; ///< 16.16 fixed point, 0x10000 = baseline
572
573 // remap (post-CRUSH, pre-up)
574 mempool::osdmap::map<pg_t,mempool::osdmap::vector<int32_t>> pg_upmap; ///< remap pg
575 mempool::osdmap::map<pg_t,mempool::osdmap::vector<std::pair<int32_t,int32_t>>> pg_upmap_items; ///< remap osds in up set
576
577 mempool::osdmap::map<int64_t,pg_pool_t> pools;
578 mempool::osdmap::map<int64_t,std::string> pool_name;
579 mempool::osdmap::map<std::string, std::map<std::string,std::string>> erasure_code_profiles;
580 mempool::osdmap::map<std::string,int64_t, std::less<>> name_pool;
581
582 std::shared_ptr< mempool::osdmap::vector<uuid_d> > osd_uuid;
583 mempool::osdmap::vector<osd_xinfo_t> osd_xinfo;
584
585 mempool::osdmap::unordered_map<entity_addr_t,utime_t> blocklist;
586
587 /// queue of snaps to remove
588 mempool::osdmap::map<int64_t, snap_interval_set_t> removed_snaps_queue;
589
590 /// removed_snaps additions this epoch
591 mempool::osdmap::map<int64_t, snap_interval_set_t> new_removed_snaps;
592
593 /// removed_snaps removals this epoch
594 mempool::osdmap::map<int64_t, snap_interval_set_t> new_purged_snaps;
595
596 epoch_t cluster_snapshot_epoch;
597 std::string cluster_snapshot;
598 bool new_blocklist_entries;
599
600 float full_ratio = 0, backfillfull_ratio = 0, nearfull_ratio = 0;
601
602 /// min compat client we want to support
603 ceph_release_t require_min_compat_client{ceph_release_t::unknown};
604
605 public:
606 /// require osds to run at least this release
607 ceph_release_t require_osd_release{ceph_release_t::unknown};
608
609 private:
610 mutable uint64_t cached_up_osd_features;
611
612 mutable bool crc_defined;
613 mutable uint32_t crc;
614
615 void _calc_up_osd_features();
616
617 public:
618 bool have_crc() const { return crc_defined; }
619 uint32_t get_crc() const { return crc; }
620
621 std::shared_ptr<CrushWrapper> crush; // hierarchical map
622 bool stretch_mode_enabled; // we are in stretch mode, requiring multiple sites
623 uint32_t stretch_bucket_count; // number of sites we expect to be in
624 uint32_t degraded_stretch_mode; // 0 if not degraded; else count of up sites
625 uint32_t recovering_stretch_mode; // 0 if not recovering; else 1
626 int32_t stretch_mode_bucket; // the bucket type we're stretched across
627 private:
628 uint32_t crush_version = 1;
629
630 friend class OSDMonitor;
631
632 public:
633 OSDMap() : epoch(0),
634 pool_max(0),
635 flags(0),
636 num_osd(0), num_up_osd(0), num_in_osd(0),
637 max_osd(0),
638 osd_addrs(std::make_shared<addrs_s>()),
639 pg_temp(std::make_shared<PGTempMap>()),
640 primary_temp(std::make_shared<mempool::osdmap::map<pg_t,int32_t>>()),
641 osd_uuid(std::make_shared<mempool::osdmap::vector<uuid_d>>()),
642 cluster_snapshot_epoch(0),
643 new_blocklist_entries(false),
644 cached_up_osd_features(0),
645 crc_defined(false), crc(0),
646 crush(std::make_shared<CrushWrapper>()),
647 stretch_mode_enabled(false), stretch_bucket_count(0),
648 degraded_stretch_mode(0), recovering_stretch_mode(0), stretch_mode_bucket(0) {
649 }
650
651 private:
652 OSDMap(const OSDMap& other) = default;
653 OSDMap& operator=(const OSDMap& other) = default;
654 public:
655
656 /// return feature mask subset that is relevant to OSDMap encoding
657 static uint64_t get_significant_features(uint64_t features) {
658 return SIGNIFICANT_FEATURES & features;
659 }
660
661 uint64_t get_encoding_features() const;
662
663 void deepish_copy_from(const OSDMap& o) {
664 *this = o;
665 primary_temp.reset(new mempool::osdmap::map<pg_t,int32_t>(*o.primary_temp));
666 pg_temp.reset(new PGTempMap(*o.pg_temp));
667 osd_uuid.reset(new mempool::osdmap::vector<uuid_d>(*o.osd_uuid));
668
669 if (o.osd_primary_affinity)
670 osd_primary_affinity.reset(new mempool::osdmap::vector<__u32>(*o.osd_primary_affinity));
671
672 // NOTE: this still references shared entity_addrvec_t's.
673 osd_addrs.reset(new addrs_s(*o.osd_addrs));
674
675 // NOTE: we do not copy crush. note that apply_incremental will
676 // allocate a new CrushWrapper, though.
677 }
678
679 // map info
680 const uuid_d& get_fsid() const { return fsid; }
681 void set_fsid(uuid_d& f) { fsid = f; }
682
683 epoch_t get_epoch() const { return epoch; }
684 void inc_epoch() { epoch++; }
685
686 void set_epoch(epoch_t e);
687
688 uint32_t get_crush_version() const {
689 return crush_version;
690 }
691
692 /* stamps etc */
693 const utime_t& get_created() const { return created; }
694 const utime_t& get_modified() const { return modified; }
695
696 bool is_blocklisted(const entity_addr_t& a) const;
697 bool is_blocklisted(const entity_addrvec_t& a) const;
698 void get_blocklist(std::list<std::pair<entity_addr_t,utime_t > > *bl) const;
699 void get_blocklist(std::set<entity_addr_t> *bl) const;
700
701 std::string get_cluster_snapshot() const {
702 if (cluster_snapshot_epoch == epoch)
703 return cluster_snapshot;
704 return std::string();
705 }
706
707 float get_full_ratio() const {
708 return full_ratio;
709 }
710 float get_backfillfull_ratio() const {
711 return backfillfull_ratio;
712 }
713 float get_nearfull_ratio() const {
714 return nearfull_ratio;
715 }
716 void get_full_pools(CephContext *cct,
717 std::set<int64_t> *full,
718 std::set<int64_t> *backfillfull,
719 std::set<int64_t> *nearfull) const;
720 void get_full_osd_counts(std::set<int> *full, std::set<int> *backfill,
721 std::set<int> *nearfull) const;
722
723
724 /***** cluster state *****/
725 /* osds */
726 int get_max_osd() const { return max_osd; }
727 void set_max_osd(int m);
728
729 unsigned get_num_osds() const {
730 return num_osd;
731 }
732 unsigned get_num_up_osds() const {
733 return num_up_osd;
734 }
735 unsigned get_num_in_osds() const {
736 return num_in_osd;
737 }
738 /// recalculate cached values for get_num{,_up,_in}_osds
739 int calc_num_osds();
740
741 void get_all_osds(std::set<int32_t>& ls) const;
742 void get_up_osds(std::set<int32_t>& ls) const;
743 void get_out_existing_osds(std::set<int32_t>& ls) const;
744 unsigned get_num_pg_temp() const {
745 return pg_temp->size();
746 }
747
748 int get_flags() const { return flags; }
749 bool test_flag(int f) const { return flags & f; }
750 void set_flag(int f) { flags |= f; }
751 void clear_flag(int f) { flags &= ~f; }
752
753 void get_flag_set(std::set<std::string> *flagset) const;
754
755 static void calc_state_set(int state, std::set<std::string>& st);
756
757 int get_state(int o) const {
758 ceph_assert(o < max_osd);
759 return osd_state[o];
760 }
761 int get_state(int o, std::set<std::string>& st) const {
762 ceph_assert(o < max_osd);
763 unsigned t = osd_state[o];
764 calc_state_set(t, st);
765 return osd_state[o];
766 }
767 void set_state(int o, unsigned s) {
768 ceph_assert(o < max_osd);
769 osd_state[o] = s;
770 }
771 void set_weight(int o, unsigned w) {
772 ceph_assert(o < max_osd);
773 osd_weight[o] = w;
774 if (w)
775 osd_state[o] |= CEPH_OSD_EXISTS;
776 }
777 unsigned get_weight(int o) const {
778 ceph_assert(o < max_osd);
779 return osd_weight[o];
780 }
781 float get_weightf(int o) const {
782 return (float)get_weight(o) / (float)CEPH_OSD_IN;
783 }
784 void adjust_osd_weights(const std::map<int,double>& weights, Incremental& inc) const;
785
786 void set_primary_affinity(int o, int w) {
787 ceph_assert(o < max_osd);
788 if (!osd_primary_affinity)
789 osd_primary_affinity.reset(
790 new mempool::osdmap::vector<__u32>(
791 max_osd, CEPH_OSD_DEFAULT_PRIMARY_AFFINITY));
792 (*osd_primary_affinity)[o] = w;
793 }
794 unsigned get_primary_affinity(int o) const {
795 ceph_assert(o < max_osd);
796 if (!osd_primary_affinity)
797 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
798 return (*osd_primary_affinity)[o];
799 }
800 float get_primary_affinityf(int o) const {
801 return (float)get_primary_affinity(o) / (float)CEPH_OSD_MAX_PRIMARY_AFFINITY;
802 }
803
804 bool has_erasure_code_profile(const std::string &name) const {
805 auto i = erasure_code_profiles.find(name);
806 return i != erasure_code_profiles.end();
807 }
808 int get_erasure_code_profile_default(CephContext *cct,
809 std::map<std::string,std::string> &profile_map,
810 std::ostream *ss);
811 void set_erasure_code_profile(const std::string &name,
812 const std::map<std::string,std::string>& profile) {
813 erasure_code_profiles[name] = profile;
814 }
815 const std::map<std::string,std::string> &get_erasure_code_profile(
816 const std::string &name) const {
817 static std::map<std::string,std::string> empty;
818 auto i = erasure_code_profiles.find(name);
819 if (i == erasure_code_profiles.end())
820 return empty;
821 else
822 return i->second;
823 }
824 const mempool::osdmap::map<std::string,std::map<std::string,std::string>> &get_erasure_code_profiles() const {
825 return erasure_code_profiles;
826 }
827
828 bool exists(int osd) const {
829 //assert(osd >= 0);
830 return osd >= 0 && osd < max_osd && (osd_state[osd] & CEPH_OSD_EXISTS);
831 }
832
833 bool is_destroyed(int osd) const {
834 return exists(osd) && (osd_state[osd] & CEPH_OSD_DESTROYED);
835 }
836
837 bool is_up(int osd) const {
838 return exists(osd) && (osd_state[osd] & CEPH_OSD_UP);
839 }
840
841 bool has_been_up_since(int osd, epoch_t epoch) const {
842 return is_up(osd) && get_up_from(osd) <= epoch;
843 }
844
845 bool is_down(int osd) const {
846 return !is_up(osd);
847 }
848
849 bool is_stop(int osd) const {
850 return exists(osd) && is_down(osd) &&
851 (osd_state[osd] & CEPH_OSD_STOP);
852 }
853
854 bool is_out(int osd) const {
855 return !exists(osd) || get_weight(osd) == CEPH_OSD_OUT;
856 }
857
858 bool is_in(int osd) const {
859 return !is_out(osd);
860 }
861
862 bool is_dead(int osd) const {
863 if (!exists(osd)) {
864 return false; // unclear if they know they are removed from map
865 }
866 return get_xinfo(osd).dead_epoch > get_info(osd).up_from;
867 }
868
869 unsigned get_osd_crush_node_flags(int osd) const;
870 unsigned get_crush_node_flags(int id) const;
871 unsigned get_device_class_flags(int id) const;
872
873 bool is_noup_by_osd(int osd) const {
874 return exists(osd) && (osd_state[osd] & CEPH_OSD_NOUP);
875 }
876
877 bool is_nodown_by_osd(int osd) const {
878 return exists(osd) && (osd_state[osd] & CEPH_OSD_NODOWN);
879 }
880
881 bool is_noin_by_osd(int osd) const {
882 return exists(osd) && (osd_state[osd] & CEPH_OSD_NOIN);
883 }
884
885 bool is_noout_by_osd(int osd) const {
886 return exists(osd) && (osd_state[osd] & CEPH_OSD_NOOUT);
887 }
888
889 bool is_noup(int osd) const {
890 if (test_flag(CEPH_OSDMAP_NOUP)) // global?
891 return true;
892 if (is_noup_by_osd(osd)) // by osd?
893 return true;
894 if (get_osd_crush_node_flags(osd) & CEPH_OSD_NOUP) // by crush-node?
895 return true;
896 if (auto class_id = crush->get_item_class_id(osd); class_id >= 0 &&
897 get_device_class_flags(class_id) & CEPH_OSD_NOUP) // by device-class?
898 return true;
899 return false;
900 }
901
902 bool is_nodown(int osd) const {
903 if (test_flag(CEPH_OSDMAP_NODOWN))
904 return true;
905 if (is_nodown_by_osd(osd))
906 return true;
907 if (get_osd_crush_node_flags(osd) & CEPH_OSD_NODOWN)
908 return true;
909 if (auto class_id = crush->get_item_class_id(osd); class_id >= 0 &&
910 get_device_class_flags(class_id) & CEPH_OSD_NODOWN)
911 return true;
912 return false;
913 }
914
915 bool is_noin(int osd) const {
916 if (test_flag(CEPH_OSDMAP_NOIN))
917 return true;
918 if (is_noin_by_osd(osd))
919 return true;
920 if (get_osd_crush_node_flags(osd) & CEPH_OSD_NOIN)
921 return true;
922 if (auto class_id = crush->get_item_class_id(osd); class_id >= 0 &&
923 get_device_class_flags(class_id) & CEPH_OSD_NOIN)
924 return true;
925 return false;
926 }
927
928 bool is_noout(int osd) const {
929 if (test_flag(CEPH_OSDMAP_NOOUT))
930 return true;
931 if (is_noout_by_osd(osd))
932 return true;
933 if (get_osd_crush_node_flags(osd) & CEPH_OSD_NOOUT)
934 return true;
935 if (auto class_id = crush->get_item_class_id(osd); class_id >= 0 &&
936 get_device_class_flags(class_id) & CEPH_OSD_NOOUT)
937 return true;
938 return false;
939 }
940
941 /**
942 * check if an entire crush subtree is down
943 */
944 bool subtree_is_down(int id, std::set<int> *down_cache) const;
945 bool containing_subtree_is_down(CephContext *cct, int osd, int subtree_type, std::set<int> *down_cache) const;
946
947 bool subtree_type_is_down(CephContext *cct, int id, int subtree_type, std::set<int> *down_in_osds, std::set<int> *up_in_osds,
948 std::set<int> *subtree_up, std::unordered_map<int, std::set<int> > *subtree_type_down) const;
949
950 int identify_osd(const entity_addr_t& addr) const;
951 int identify_osd(const uuid_d& u) const;
952 int identify_osd_on_all_channels(const entity_addr_t& addr) const;
953
954 bool have_addr(const entity_addr_t& addr) const {
955 return identify_osd(addr) >= 0;
956 }
957 int find_osd_on_ip(const entity_addr_t& ip) const;
958
959 const entity_addrvec_t& get_addrs(int osd) const {
960 ceph_assert(exists(osd));
961 return osd_addrs->client_addrs[osd] ?
962 *osd_addrs->client_addrs[osd] : _blank_addrvec;
963 }
964 const entity_addrvec_t& get_most_recent_addrs(int osd) const {
965 return get_addrs(osd);
966 }
967 const entity_addrvec_t &get_cluster_addrs(int osd) const {
968 ceph_assert(exists(osd));
969 return osd_addrs->cluster_addrs[osd] ?
970 *osd_addrs->cluster_addrs[osd] : _blank_addrvec;
971 }
972 const entity_addrvec_t &get_hb_back_addrs(int osd) const {
973 ceph_assert(exists(osd));
974 return osd_addrs->hb_back_addrs[osd] ?
975 *osd_addrs->hb_back_addrs[osd] : _blank_addrvec;
976 }
977 const entity_addrvec_t &get_hb_front_addrs(int osd) const {
978 ceph_assert(exists(osd));
979 return osd_addrs->hb_front_addrs[osd] ?
980 *osd_addrs->hb_front_addrs[osd] : _blank_addrvec;
981 }
982
983 const uuid_d& get_uuid(int osd) const {
984 ceph_assert(exists(osd));
985 return (*osd_uuid)[osd];
986 }
987
988 const epoch_t& get_up_from(int osd) const {
989 ceph_assert(exists(osd));
990 return osd_info[osd].up_from;
991 }
992 const epoch_t& get_up_thru(int osd) const {
993 ceph_assert(exists(osd));
994 return osd_info[osd].up_thru;
995 }
996 const epoch_t& get_down_at(int osd) const {
997 ceph_assert(exists(osd));
998 return osd_info[osd].down_at;
999 }
1000 const osd_info_t& get_info(int osd) const {
1001 ceph_assert(osd < max_osd);
1002 return osd_info[osd];
1003 }
1004
1005 const osd_xinfo_t& get_xinfo(int osd) const {
1006 ceph_assert(osd < max_osd);
1007 return osd_xinfo[osd];
1008 }
1009
1010 int get_next_up_osd_after(int n) const {
1011 if (get_max_osd() == 0)
1012 return -1;
1013 for (int i = n + 1; i != n; ++i) {
1014 if (i >= get_max_osd())
1015 i = 0;
1016 if (i == n)
1017 break;
1018 if (is_up(i))
1019 return i;
1020 }
1021 return -1;
1022 }
1023
1024 int get_previous_up_osd_before(int n) const {
1025 if (get_max_osd() == 0)
1026 return -1;
1027 for (int i = n - 1; i != n; --i) {
1028 if (i < 0)
1029 i = get_max_osd() - 1;
1030 if (i == n)
1031 break;
1032 if (is_up(i))
1033 return i;
1034 }
1035 return -1;
1036 }
1037
1038
1039 void get_random_up_osds_by_subtree(int n, // whoami
1040 std::string &subtree,
1041 int limit, // how many
1042 std::set<int> skip,
1043 std::set<int> *want) const;
1044
1045 /**
1046 * get feature bits required by the current structure
1047 *
1048 * @param entity_type [in] what entity type we are asking about
1049 * @param mask [out] std::set of all possible map-related features we could std::set
1050 * @return feature bits used by this map
1051 */
1052 uint64_t get_features(int entity_type, uint64_t *mask) const;
1053
1054 /**
1055 * get oldest *client* version (firefly, hammer, etc.) that can connect given
1056 * the feature bits required (according to get_features()).
1057 */
1058 ceph_release_t get_min_compat_client() const;
1059
1060 /**
1061 * gets the required minimum *client* version that can connect to the cluster.
1062 */
1063 ceph_release_t get_require_min_compat_client() const;
1064
1065 /**
1066 * get intersection of features supported by up osds
1067 */
1068 uint64_t get_up_osd_features() const;
1069
1070 void get_upmap_pgs(std::vector<pg_t> *upmap_pgs) const;
1071 bool check_pg_upmaps(
1072 CephContext *cct,
1073 const std::vector<pg_t>& to_check,
1074 std::vector<pg_t> *to_cancel,
1075 std::map<pg_t, mempool::osdmap::vector<std::pair<int,int>>> *to_remap) const;
1076 void clean_pg_upmaps(
1077 CephContext *cct,
1078 Incremental *pending_inc,
1079 const std::vector<pg_t>& to_cancel,
1080 const std::map<pg_t, mempool::osdmap::vector<std::pair<int,int>>>& to_remap) const;
1081 bool clean_pg_upmaps(CephContext *cct, Incremental *pending_inc) const;
1082
1083 int apply_incremental(const Incremental &inc);
1084
1085 /// try to re-use/reference addrs in oldmap from newmap
1086 static void dedup(const OSDMap *oldmap, OSDMap *newmap);
1087
1088 static void clean_temps(CephContext *cct,
1089 const OSDMap& oldmap,
1090 const OSDMap& nextmap,
1091 Incremental *pending_inc);
1092
1093 // serialize, unserialize
1094 private:
1095 void encode_client_old(ceph::buffer::list& bl) const;
1096 void encode_classic(ceph::buffer::list& bl, uint64_t features) const;
1097 void decode_classic(ceph::buffer::list::const_iterator& p);
1098 void post_decode();
1099 public:
1100 void encode(ceph::buffer::list& bl, uint64_t features=CEPH_FEATURES_ALL) const;
1101 void decode(ceph::buffer::list& bl);
1102 void decode(ceph::buffer::list::const_iterator& bl);
1103
1104
1105 /**** mapping facilities ****/
1106 int map_to_pg(
1107 int64_t pool,
1108 const std::string& name,
1109 const std::string& key,
1110 const std::string& nspace,
1111 pg_t *pg) const;
1112 int object_locator_to_pg(const object_t& oid, const object_locator_t& loc,
1113 pg_t &pg) const;
1114 pg_t object_locator_to_pg(const object_t& oid,
1115 const object_locator_t& loc) const {
1116 pg_t pg;
1117 int ret = object_locator_to_pg(oid, loc, pg);
1118 ceph_assert(ret == 0);
1119 return pg;
1120 }
1121
1122
1123 static object_locator_t file_to_object_locator(const file_layout_t& layout) {
1124 return object_locator_t(layout.pool_id, layout.pool_ns);
1125 }
1126
1127 ceph_object_layout file_to_object_layout(object_t oid,
1128 file_layout_t& layout) const {
1129 return make_object_layout(oid, layout.pool_id, layout.pool_ns);
1130 }
1131
1132 ceph_object_layout make_object_layout(object_t oid, int pg_pool,
1133 std::string nspace) const;
1134
1135 int get_pg_num(int pg_pool) const
1136 {
1137 const pg_pool_t *pool = get_pg_pool(pg_pool);
1138 ceph_assert(NULL != pool);
1139 return pool->get_pg_num();
1140 }
1141
1142 bool pg_exists(pg_t pgid) const {
1143 const pg_pool_t *p = get_pg_pool(pgid.pool());
1144 return p && pgid.ps() < p->get_pg_num();
1145 }
1146
1147 int get_pg_pool_min_size(pg_t pgid) const {
1148 if (!pg_exists(pgid)) {
1149 return -ENOENT;
1150 }
1151 const pg_pool_t *p = get_pg_pool(pgid.pool());
1152 ceph_assert(p);
1153 return p->get_min_size();
1154 }
1155
1156 int get_pg_pool_size(pg_t pgid) const {
1157 if (!pg_exists(pgid)) {
1158 return -ENOENT;
1159 }
1160 const pg_pool_t *p = get_pg_pool(pgid.pool());
1161 ceph_assert(p);
1162 return p->get_size();
1163 }
1164
1165 int get_pg_pool_crush_rule(pg_t pgid) const {
1166 if (!pg_exists(pgid)) {
1167 return -ENOENT;
1168 }
1169 const pg_pool_t *p = get_pg_pool(pgid.pool());
1170 ceph_assert(p);
1171 return p->get_crush_rule();
1172 }
1173
1174 private:
1175 /// pg -> (raw osd std::list)
1176 void _pg_to_raw_osds(
1177 const pg_pool_t& pool, pg_t pg,
1178 std::vector<int> *osds,
1179 ps_t *ppps) const;
1180 int _pick_primary(const std::vector<int>& osds) const;
1181 void _remove_nonexistent_osds(const pg_pool_t& pool, std::vector<int>& osds) const;
1182
1183 void _apply_primary_affinity(ps_t seed, const pg_pool_t& pool,
1184 std::vector<int> *osds, int *primary) const;
1185
1186 /// apply pg_upmap[_items] mappings
1187 void _apply_upmap(const pg_pool_t& pi, pg_t pg, std::vector<int> *raw) const;
1188
1189 /// pg -> (up osd std::list)
1190 void _raw_to_up_osds(const pg_pool_t& pool, const std::vector<int>& raw,
1191 std::vector<int> *up) const;
1192
1193
1194 /**
1195 * Get the pg and primary temp, if they are specified.
1196 * @param temp_pg [out] Will be empty or contain the temp PG mapping on return
1197 * @param temp_primary [out] Will be the value in primary_temp, or a value derived
1198 * from the pg_temp (if specified), or -1 if you should use the calculated (up_)primary.
1199 */
1200 void _get_temp_osds(const pg_pool_t& pool, pg_t pg,
1201 std::vector<int> *temp_pg, int *temp_primary) const;
1202
1203 /**
1204 * map to up and acting. Fills in whatever fields are non-NULL.
1205 */
1206 void _pg_to_up_acting_osds(const pg_t& pg, std::vector<int> *up, int *up_primary,
1207 std::vector<int> *acting, int *acting_primary,
1208 bool raw_pg_to_pg = true) const;
1209
1210 public:
1211 /***
1212 * This is suitable only for looking at raw CRUSH outputs. It skips
1213 * applying the temp and up checks and should not be used
1214 * by anybody for data mapping purposes.
1215 * raw and primary must be non-NULL
1216 */
1217 void pg_to_raw_osds(pg_t pg, std::vector<int> *raw, int *primary) const;
1218 void pg_to_raw_upmap(pg_t pg, std::vector<int> *raw,
1219 std::vector<int> *raw_upmap) const;
1220 /// map a pg to its acting set. @return acting set size
1221 void pg_to_acting_osds(const pg_t& pg, std::vector<int> *acting,
1222 int *acting_primary) const {
1223 _pg_to_up_acting_osds(pg, NULL, NULL, acting, acting_primary);
1224 }
1225 void pg_to_acting_osds(pg_t pg, std::vector<int>& acting) const {
1226 return pg_to_acting_osds(pg, &acting, NULL);
1227 }
1228 /**
1229 * This does not apply temp overrides and should not be used
1230 * by anybody for data mapping purposes. Specify both pointers.
1231 */
1232 void pg_to_raw_up(pg_t pg, std::vector<int> *up, int *primary) const;
1233 /**
1234 * map a pg to its acting set as well as its up set. You must use
1235 * the acting set for data mapping purposes, but some users will
1236 * also find the up set useful for things like deciding what to
1237 * set as pg_temp.
1238 * Each of these pointers must be non-NULL.
1239 */
1240 void pg_to_up_acting_osds(pg_t pg, std::vector<int> *up, int *up_primary,
1241 std::vector<int> *acting, int *acting_primary) const {
1242 _pg_to_up_acting_osds(pg, up, up_primary, acting, acting_primary);
1243 }
1244 void pg_to_up_acting_osds(pg_t pg, std::vector<int>& up, std::vector<int>& acting) const {
1245 int up_primary, acting_primary;
1246 pg_to_up_acting_osds(pg, &up, &up_primary, &acting, &acting_primary);
1247 }
1248 bool pg_is_ec(pg_t pg) const {
1249 auto i = pools.find(pg.pool());
1250 ceph_assert(i != pools.end());
1251 return i->second.is_erasure();
1252 }
1253 bool get_primary_shard(const pg_t& pgid, spg_t *out) const {
1254 auto i = get_pools().find(pgid.pool());
1255 if (i == get_pools().end()) {
1256 return false;
1257 }
1258 if (!i->second.is_erasure()) {
1259 *out = spg_t(pgid);
1260 return true;
1261 }
1262 int primary;
1263 std::vector<int> acting;
1264 pg_to_acting_osds(pgid, &acting, &primary);
1265 for (uint8_t i = 0; i < acting.size(); ++i) {
1266 if (acting[i] == primary) {
1267 *out = spg_t(pgid, shard_id_t(i));
1268 return true;
1269 }
1270 }
1271 return false;
1272 }
1273 bool get_primary_shard(const pg_t& pgid, int *primary, spg_t *out) const {
1274 auto i = get_pools().find(pgid.pool());
1275 if (i == get_pools().end()) {
1276 return false;
1277 }
1278 std::vector<int> acting;
1279 pg_to_acting_osds(pgid, &acting, primary);
1280 if (i->second.is_erasure()) {
1281 for (uint8_t i = 0; i < acting.size(); ++i) {
1282 if (acting[i] == *primary) {
1283 *out = spg_t(pgid, shard_id_t(i));
1284 return true;
1285 }
1286 }
1287 } else {
1288 *out = spg_t(pgid);
1289 return true;
1290 }
1291 return false;
1292 }
1293
1294 bool in_removed_snaps_queue(int64_t pool, snapid_t snap) const {
1295 auto p = removed_snaps_queue.find(pool);
1296 if (p == removed_snaps_queue.end()) {
1297 return false;
1298 }
1299 return p->second.contains(snap);
1300 }
1301
1302 const mempool::osdmap::map<int64_t,snap_interval_set_t>&
1303 get_removed_snaps_queue() const {
1304 return removed_snaps_queue;
1305 }
1306 const mempool::osdmap::map<int64_t,snap_interval_set_t>&
1307 get_new_removed_snaps() const {
1308 return new_removed_snaps;
1309 }
1310 const mempool::osdmap::map<int64_t,snap_interval_set_t>&
1311 get_new_purged_snaps() const {
1312 return new_purged_snaps;
1313 }
1314
1315 int64_t lookup_pg_pool_name(std::string_view name) const {
1316 auto p = name_pool.find(name);
1317 if (p == name_pool.end())
1318 return -ENOENT;
1319 return p->second;
1320 }
1321
1322 int64_t get_pool_max() const {
1323 return pool_max;
1324 }
1325 const mempool::osdmap::map<int64_t,pg_pool_t>& get_pools() const {
1326 return pools;
1327 }
1328 mempool::osdmap::map<int64_t,pg_pool_t>& get_pools() {
1329 return pools;
1330 }
1331 void get_pool_ids_by_rule(int rule_id, std::set<int64_t> *pool_ids) const {
1332 ceph_assert(pool_ids);
1333 for (auto &p: pools) {
1334 if (p.second.get_crush_rule() == rule_id) {
1335 pool_ids->insert(p.first);
1336 }
1337 }
1338 }
1339 void get_pool_ids_by_osd(CephContext *cct,
1340 int osd,
1341 std::set<int64_t> *pool_ids) const;
1342 const std::string& get_pool_name(int64_t p) const {
1343 auto i = pool_name.find(p);
1344 ceph_assert(i != pool_name.end());
1345 return i->second;
1346 }
1347 const mempool::osdmap::map<int64_t,std::string>& get_pool_names() const {
1348 return pool_name;
1349 }
1350 bool have_pg_pool(int64_t p) const {
1351 return pools.count(p);
1352 }
1353 const pg_pool_t* get_pg_pool(int64_t p) const {
1354 auto i = pools.find(p);
1355 if (i != pools.end())
1356 return &i->second;
1357 return NULL;
1358 }
1359 unsigned get_pg_size(pg_t pg) const {
1360 auto p = pools.find(pg.pool());
1361 ceph_assert(p != pools.end());
1362 return p->second.get_size();
1363 }
1364 int get_pg_type(pg_t pg) const {
1365 auto p = pools.find(pg.pool());
1366 ceph_assert(p != pools.end());
1367 return p->second.get_type();
1368 }
1369 int get_pool_crush_rule(int64_t pool_id) const {
1370 auto pool = get_pg_pool(pool_id);
1371 if (!pool)
1372 return -ENOENT;
1373 return pool->get_crush_rule();
1374 }
1375
1376
1377 pg_t raw_pg_to_pg(pg_t pg) const {
1378 auto p = pools.find(pg.pool());
1379 ceph_assert(p != pools.end());
1380 return p->second.raw_pg_to_pg(pg);
1381 }
1382
1383 // pg -> acting primary osd
1384 int get_pg_acting_primary(pg_t pg) const {
1385 int primary = -1;
1386 _pg_to_up_acting_osds(pg, nullptr, nullptr, nullptr, &primary);
1387 return primary;
1388 }
1389
1390 /*
1391 * check whether an spg_t maps to a particular osd
1392 */
1393 bool is_up_acting_osd_shard(spg_t pg, int osd) const {
1394 std::vector<int> up, acting;
1395 _pg_to_up_acting_osds(pg.pgid, &up, NULL, &acting, NULL, false);
1396 if (calc_pg_role(pg_shard_t(osd, pg.shard), acting) >= 0 ||
1397 calc_pg_role(pg_shard_t(osd, pg.shard), up) >= 0) {
1398 return true;
1399 }
1400 return false;
1401 }
1402
1403
1404 static int calc_pg_role_broken(int osd, const std::vector<int>& acting, int nrep=0);
1405 static int calc_pg_role(pg_shard_t who, const std::vector<int>& acting);
1406 static bool primary_changed_broken(
1407 int oldprimary,
1408 const std::vector<int> &oldacting,
1409 int newprimary,
1410 const std::vector<int> &newacting);
1411
1412 /* rank is -1 (stray), 0 (primary), 1,2,3,... (replica) */
1413 int get_pg_acting_role(spg_t pg, int osd) const {
1414 std::vector<int> group;
1415 pg_to_acting_osds(pg.pgid, group);
1416 return calc_pg_role(pg_shard_t(osd, pg.shard), group);
1417 }
1418
1419 bool try_pg_upmap(
1420 CephContext *cct,
1421 pg_t pg, ///< pg to potentially remap
1422 const std::set<int>& overfull, ///< osds we'd want to evacuate
1423 const std::vector<int>& underfull, ///< osds to move to, in order of preference
1424 const std::vector<int>& more_underfull, ///< less full osds to move to, in order of preference
1425 std::vector<int> *orig,
1426 std::vector<int> *out); ///< resulting alternative mapping
1427
1428 int calc_pg_upmaps(
1429 CephContext *cct,
1430 uint32_t max_deviation, ///< max deviation from target (value >= 1)
1431 int max_iterations, ///< max iterations to run
1432 const std::set<int64_t>& pools, ///< [optional] restrict to pool
1433 Incremental *pending_inc
1434 );
1435
1436 int get_osds_by_bucket_name(const std::string &name, std::set<int> *osds) const;
1437
1438 bool have_pg_upmaps(pg_t pg) const {
1439 return pg_upmap.count(pg) ||
1440 pg_upmap_items.count(pg);
1441 }
1442
1443 bool check_full(const std::set<pg_shard_t> &missing_on) const {
1444 for (auto shard : missing_on) {
1445 if (get_state(shard.osd) & CEPH_OSD_FULL)
1446 return true;
1447 }
1448 return false;
1449 }
1450
1451 /*
1452 * handy helpers to build simple maps...
1453 */
1454 /**
1455 * Build an OSD map suitable for basic usage. If **num_osd** is >= 0
1456 * it will be initialized with the specified number of OSDs in a
1457 * single host. If **num_osd** is < 0 the layout of the OSD map will
1458 * be built by reading the content of the configuration file.
1459 *
1460 * @param cct [in] in core ceph context
1461 * @param e [in] initial epoch
1462 * @param fsid [in] id of the cluster
1463 * @param num_osd [in] number of OSDs if >= 0 or read from conf if < 0
1464 * @return **0** on success, negative errno on error.
1465 */
1466 private:
1467 int build_simple_optioned(CephContext *cct, epoch_t e, uuid_d &fsid,
1468 int num_osd, int pg_bits, int pgp_bits,
1469 bool default_pool);
1470 public:
1471 int build_simple(CephContext *cct, epoch_t e, uuid_d &fsid,
1472 int num_osd) {
1473 return build_simple_optioned(cct, e, fsid, num_osd, 0, 0, false);
1474 }
1475 int build_simple_with_pool(CephContext *cct, epoch_t e, uuid_d &fsid,
1476 int num_osd, int pg_bits, int pgp_bits) {
1477 return build_simple_optioned(cct, e, fsid, num_osd,
1478 pg_bits, pgp_bits, true);
1479 }
1480 static int _build_crush_types(CrushWrapper& crush);
1481 static int build_simple_crush_map(CephContext *cct, CrushWrapper& crush,
1482 int num_osd, std::ostream *ss);
1483 static int build_simple_crush_map_from_conf(CephContext *cct,
1484 CrushWrapper& crush,
1485 std::ostream *ss);
1486 static int build_simple_crush_rules(
1487 CephContext *cct, CrushWrapper& crush,
1488 const std::string& root,
1489 std::ostream *ss);
1490
1491 bool crush_rule_in_use(int rule_id) const;
1492
1493 int validate_crush_rules(CrushWrapper *crush, std::ostream *ss) const;
1494
1495 void clear_temp() {
1496 pg_temp->clear();
1497 primary_temp->clear();
1498 }
1499
1500 private:
1501 void print_osd_line(int cur, std::ostream *out, ceph::Formatter *f) const;
1502 public:
1503 void print(std::ostream& out) const;
1504 void print_osd(int id, std::ostream& out) const;
1505 void print_osds(std::ostream& out) const;
1506 void print_pools(std::ostream& out) const;
1507 void print_summary(ceph::Formatter *f, std::ostream& out,
1508 const std::string& prefix, bool extra=false) const;
1509 void print_oneline_summary(std::ostream& out) const;
1510
1511 enum {
1512 DUMP_IN = 1, // only 'in' osds
1513 DUMP_OUT = 2, // only 'out' osds
1514 DUMP_UP = 4, // only 'up' osds
1515 DUMP_DOWN = 8, // only 'down' osds
1516 DUMP_DESTROYED = 16, // only 'destroyed' osds
1517 };
1518 void print_tree(ceph::Formatter *f, std::ostream *out,
1519 unsigned dump_flags=0, std::string bucket="") const;
1520
1521 int summarize_mapping_stats(
1522 OSDMap *newmap,
1523 const std::set<int64_t> *pools,
1524 std::string *out,
1525 ceph::Formatter *f) const;
1526
1527 std::string get_flag_string() const;
1528 static std::string get_flag_string(unsigned flags);
1529 static void dump_erasure_code_profiles(
1530 const mempool::osdmap::map<std::string,std::map<std::string,std::string> > &profiles,
1531 ceph::Formatter *f);
1532 void dump(ceph::Formatter *f) const;
1533 void dump_osd(int id, ceph::Formatter *f) const;
1534 void dump_osds(ceph::Formatter *f) const;
1535 static void generate_test_instances(std::list<OSDMap*>& o);
1536 bool check_new_blocklist_entries() const { return new_blocklist_entries; }
1537
1538 void check_health(CephContext *cct, health_check_map_t *checks) const;
1539
1540 int parse_osd_id_list(const std::vector<std::string>& ls,
1541 std::set<int> *out,
1542 std::ostream *ss) const;
1543
1544 float pool_raw_used_rate(int64_t poolid) const;
1545
1546 };
1547 WRITE_CLASS_ENCODER_FEATURES(OSDMap)
1548 WRITE_CLASS_ENCODER_FEATURES(OSDMap::Incremental)
1549
1550 #ifdef WITH_SEASTAR
1551 using OSDMapRef = boost::local_shared_ptr<const OSDMap>;
1552 #else
1553 using OSDMapRef = std::shared_ptr<const OSDMap>;
1554 #endif
1555
1556
1557 inline std::ostream& operator<<(std::ostream& out, const OSDMap& m) {
1558 m.print_oneline_summary(out);
1559 return out;
1560 }
1561
1562 class PGMap;
1563
1564 void print_osd_utilization(const OSDMap& osdmap,
1565 const PGMap& pgmap,
1566 std::ostream& out,
1567 ceph::Formatter *f,
1568 bool tree,
1569 const std::string& filter);
1570
1571 #endif