]> git.proxmox.com Git - ceph.git/blob - ceph/src/osd/osd_types.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / osd / osd_types.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 #ifndef CEPH_OSD_TYPES_H
19 #define CEPH_OSD_TYPES_H
20
21 #include <atomic>
22 #include <sstream>
23 #include <cstdio>
24 #include <memory>
25 #include <string_view>
26
27 #include <boost/scoped_ptr.hpp>
28 #include <boost/optional/optional_io.hpp>
29 #include <boost/variant.hpp>
30 #include <boost/smart_ptr/local_shared_ptr.hpp>
31
32 #include "include/rados/rados_types.hpp"
33 #include "include/mempool.h"
34
35 #include "msg/msg_types.h"
36 #include "include/compat.h"
37 #include "include/types.h"
38 #include "include/utime.h"
39 #include "include/CompatSet.h"
40 #include "common/ceph_context.h"
41 #include "common/histogram.h"
42 #include "include/interval_set.h"
43 #include "include/inline_memory.h"
44 #include "common/Formatter.h"
45 #include "common/bloom_filter.hpp"
46 #include "common/hobject.h"
47 #include "common/snap_types.h"
48 #include "HitSet.h"
49 #include "Watch.h"
50 #include "include/cmp.h"
51 #include "librados/ListObjectImpl.h"
52 #include "compressor/Compressor.h"
53 #include "osd_perf_counters.h"
54
55 #define CEPH_OSD_ONDISK_MAGIC "ceph osd volume v026"
56
57 #define CEPH_OSD_FEATURE_INCOMPAT_BASE CompatSet::Feature(1, "initial feature set(~v.18)")
58 #define CEPH_OSD_FEATURE_INCOMPAT_PGINFO CompatSet::Feature(2, "pginfo object")
59 #define CEPH_OSD_FEATURE_INCOMPAT_OLOC CompatSet::Feature(3, "object locator")
60 #define CEPH_OSD_FEATURE_INCOMPAT_LEC CompatSet::Feature(4, "last_epoch_clean")
61 #define CEPH_OSD_FEATURE_INCOMPAT_CATEGORIES CompatSet::Feature(5, "categories")
62 #define CEPH_OSD_FEATURE_INCOMPAT_HOBJECTPOOL CompatSet::Feature(6, "hobjectpool")
63 #define CEPH_OSD_FEATURE_INCOMPAT_BIGINFO CompatSet::Feature(7, "biginfo")
64 #define CEPH_OSD_FEATURE_INCOMPAT_LEVELDBINFO CompatSet::Feature(8, "leveldbinfo")
65 #define CEPH_OSD_FEATURE_INCOMPAT_LEVELDBLOG CompatSet::Feature(9, "leveldblog")
66 #define CEPH_OSD_FEATURE_INCOMPAT_SNAPMAPPER CompatSet::Feature(10, "snapmapper")
67 #define CEPH_OSD_FEATURE_INCOMPAT_SHARDS CompatSet::Feature(11, "sharded objects")
68 #define CEPH_OSD_FEATURE_INCOMPAT_HINTS CompatSet::Feature(12, "transaction hints")
69 #define CEPH_OSD_FEATURE_INCOMPAT_PGMETA CompatSet::Feature(13, "pg meta object")
70 #define CEPH_OSD_FEATURE_INCOMPAT_MISSING CompatSet::Feature(14, "explicit missing set")
71 #define CEPH_OSD_FEATURE_INCOMPAT_FASTINFO CompatSet::Feature(15, "fastinfo pg attr")
72 #define CEPH_OSD_FEATURE_INCOMPAT_RECOVERY_DELETES CompatSet::Feature(16, "deletes in missing set")
73 #define CEPH_OSD_FEATURE_INCOMPAT_SNAPMAPPER2 CompatSet::Feature(17, "new snapmapper key structure")
74
75
76 /// pool priority range set by user
77 #define OSD_POOL_PRIORITY_MAX 10
78 #define OSD_POOL_PRIORITY_MIN -OSD_POOL_PRIORITY_MAX
79
80 /// min recovery priority for MBackfillReserve
81 #define OSD_RECOVERY_PRIORITY_MIN 0
82
83 /// base backfill priority for MBackfillReserve
84 #define OSD_BACKFILL_PRIORITY_BASE 100
85
86 /// base backfill priority for MBackfillReserve (degraded PG)
87 #define OSD_BACKFILL_DEGRADED_PRIORITY_BASE 140
88
89 /// base recovery priority for MBackfillReserve
90 #define OSD_RECOVERY_PRIORITY_BASE 180
91
92 /// base backfill priority for MBackfillReserve (inactive PG)
93 #define OSD_BACKFILL_INACTIVE_PRIORITY_BASE 220
94
95 /// base recovery priority for MRecoveryReserve (inactive PG)
96 #define OSD_RECOVERY_INACTIVE_PRIORITY_BASE 220
97
98 /// max manually/automatically set recovery priority for MBackfillReserve
99 #define OSD_RECOVERY_PRIORITY_MAX 253
100
101 /// backfill priority for MBackfillReserve, when forced manually
102 #define OSD_BACKFILL_PRIORITY_FORCED 254
103
104 /// recovery priority for MRecoveryReserve, when forced manually
105 #define OSD_RECOVERY_PRIORITY_FORCED 255
106
107 /// priority for pg deletion when osd is not fullish
108 #define OSD_DELETE_PRIORITY_NORMAL 179
109
110 /// priority for pg deletion when osd is approaching full
111 #define OSD_DELETE_PRIORITY_FULLISH 219
112
113 /// priority when more full
114 #define OSD_DELETE_PRIORITY_FULL 255
115
116 static std::map<int, int> max_prio_map = {
117 {OSD_BACKFILL_PRIORITY_BASE, OSD_BACKFILL_DEGRADED_PRIORITY_BASE - 1},
118 {OSD_BACKFILL_DEGRADED_PRIORITY_BASE, OSD_RECOVERY_PRIORITY_BASE - 1},
119 {OSD_RECOVERY_PRIORITY_BASE, OSD_BACKFILL_INACTIVE_PRIORITY_BASE - 1},
120 {OSD_RECOVERY_INACTIVE_PRIORITY_BASE, OSD_RECOVERY_PRIORITY_MAX},
121 {OSD_BACKFILL_INACTIVE_PRIORITY_BASE, OSD_RECOVERY_PRIORITY_MAX}
122 };
123
124 typedef hobject_t collection_list_handle_t;
125
126 /// convert a single CPEH_OSD_FLAG_* to a std::string
127 const char *ceph_osd_flag_name(unsigned flag);
128 /// convert a single CEPH_OSD_OF_FLAG_* to a std::string
129 const char *ceph_osd_op_flag_name(unsigned flag);
130
131 /// convert CEPH_OSD_FLAG_* op flags to a std::string
132 std::string ceph_osd_flag_string(unsigned flags);
133 /// conver CEPH_OSD_OP_FLAG_* op flags to a std::string
134 std::string ceph_osd_op_flag_string(unsigned flags);
135 /// conver CEPH_OSD_ALLOC_HINT_FLAG_* op flags to a std::string
136 std::string ceph_osd_alloc_hint_flag_string(unsigned flags);
137
138 typedef std::map<std::string,std::string> osd_alert_list_t;
139 /// map osd id -> alert_list_t
140 typedef std::map<int, osd_alert_list_t> osd_alerts_t;
141 void dump(ceph::Formatter* f, const osd_alerts_t& alerts);
142
143
144 typedef interval_set<
145 snapid_t,
146 mempool::osdmap::flat_map> snap_interval_set_t;
147
148
149 /**
150 * osd request identifier
151 *
152 * caller name + incarnation# + tid to unique identify this request.
153 */
154 struct osd_reqid_t {
155 entity_name_t name; // who
156 ceph_tid_t tid;
157 int32_t inc; // incarnation
158
159 osd_reqid_t()
160 : tid(0), inc(0)
161 {}
162 osd_reqid_t(const entity_name_t& a, int i, ceph_tid_t t)
163 : name(a), tid(t), inc(i)
164 {}
165
166 DENC(osd_reqid_t, v, p) {
167 DENC_START(2, 2, p);
168 denc(v.name, p);
169 denc(v.tid, p);
170 denc(v.inc, p);
171 DENC_FINISH(p);
172 }
173 void dump(ceph::Formatter *f) const;
174 static void generate_test_instances(std::list<osd_reqid_t*>& o);
175 };
176 WRITE_CLASS_DENC(osd_reqid_t)
177
178
179
180 struct pg_shard_t {
181 static const int32_t NO_OSD = 0x7fffffff;
182 int32_t osd;
183 shard_id_t shard;
184 pg_shard_t() : osd(-1), shard(shard_id_t::NO_SHARD) {}
185 explicit pg_shard_t(int osd) : osd(osd), shard(shard_id_t::NO_SHARD) {}
186 pg_shard_t(int osd, shard_id_t shard) : osd(osd), shard(shard) {}
187 bool is_undefined() const {
188 return osd == -1;
189 }
190 std::string get_osd() const { return (osd == NO_OSD ? "NONE" : std::to_string(osd)); }
191 void encode(ceph::buffer::list &bl) const;
192 void decode(ceph::buffer::list::const_iterator &bl);
193 void dump(ceph::Formatter *f) const {
194 f->dump_unsigned("osd", osd);
195 if (shard != shard_id_t::NO_SHARD) {
196 f->dump_unsigned("shard", shard);
197 }
198 }
199 };
200 WRITE_CLASS_ENCODER(pg_shard_t)
201 WRITE_EQ_OPERATORS_2(pg_shard_t, osd, shard)
202 WRITE_CMP_OPERATORS_2(pg_shard_t, osd, shard)
203 std::ostream& operator<<(std::ostream &lhs, const pg_shard_t &rhs);
204
205 using HobjToShardSetMapping = std::map<hobject_t, std::set<pg_shard_t>>;
206
207 class IsPGRecoverablePredicate {
208 public:
209 /**
210 * have encodes the shards available
211 */
212 virtual bool operator()(const std::set<pg_shard_t> &have) const = 0;
213 virtual ~IsPGRecoverablePredicate() {}
214 };
215
216 class IsPGReadablePredicate {
217 public:
218 /**
219 * have encodes the shards available
220 */
221 virtual bool operator()(const std::set<pg_shard_t> &have) const = 0;
222 virtual ~IsPGReadablePredicate() {}
223 };
224
225 inline std::ostream& operator<<(std::ostream& out, const osd_reqid_t& r) {
226 return out << r.name << "." << r.inc << ":" << r.tid;
227 }
228
229 inline bool operator==(const osd_reqid_t& l, const osd_reqid_t& r) {
230 return (l.name == r.name) && (l.inc == r.inc) && (l.tid == r.tid);
231 }
232 inline bool operator!=(const osd_reqid_t& l, const osd_reqid_t& r) {
233 return (l.name != r.name) || (l.inc != r.inc) || (l.tid != r.tid);
234 }
235 inline bool operator<(const osd_reqid_t& l, const osd_reqid_t& r) {
236 return (l.name < r.name) || (l.inc < r.inc) ||
237 (l.name == r.name && l.inc == r.inc && l.tid < r.tid);
238 }
239 inline bool operator<=(const osd_reqid_t& l, const osd_reqid_t& r) {
240 return (l.name < r.name) || (l.inc < r.inc) ||
241 (l.name == r.name && l.inc == r.inc && l.tid <= r.tid);
242 }
243 inline bool operator>(const osd_reqid_t& l, const osd_reqid_t& r) { return !(l <= r); }
244 inline bool operator>=(const osd_reqid_t& l, const osd_reqid_t& r) { return !(l < r); }
245
246 namespace std {
247 template<> struct hash<osd_reqid_t> {
248 size_t operator()(const osd_reqid_t &r) const {
249 static hash<uint64_t> H;
250 return H(r.name.num() ^ r.tid ^ r.inc);
251 }
252 };
253 } // namespace std
254
255
256 // -----
257
258 // a locator constrains the placement of an object. mainly, which pool
259 // does it go in.
260 struct object_locator_t {
261 // You specify either the hash or the key -- not both
262 std::int64_t pool; ///< pool id
263 std::string key; ///< key string (if non-empty)
264 std::string nspace; ///< namespace
265 std::int64_t hash; ///< hash position (if >= 0)
266
267 explicit object_locator_t()
268 : pool(-1), hash(-1) {}
269 explicit object_locator_t(int64_t po)
270 : pool(po), hash(-1) {}
271 explicit object_locator_t(int64_t po, int64_t ps)
272 : pool(po), hash(ps) {}
273 explicit object_locator_t(int64_t po, std::string_view ns)
274 : pool(po), nspace(ns), hash(-1) {}
275 explicit object_locator_t(int64_t po, std::string_view ns, int64_t ps)
276 : pool(po), nspace(ns), hash(ps) {}
277 explicit object_locator_t(int64_t po, std::string_view ns, std::string_view s)
278 : pool(po), key(s), nspace(ns), hash(-1) {}
279 explicit object_locator_t(const hobject_t& soid)
280 : pool(soid.pool), key(soid.get_key()), nspace(soid.nspace), hash(-1) {}
281
282 int64_t get_pool() const {
283 return pool;
284 }
285
286 void clear() {
287 pool = -1;
288 key = "";
289 nspace = "";
290 hash = -1;
291 }
292
293 bool empty() const {
294 return pool == -1;
295 }
296
297 void encode(ceph::buffer::list& bl) const;
298 void decode(ceph::buffer::list::const_iterator& p);
299 void dump(ceph::Formatter *f) const;
300 static void generate_test_instances(std::list<object_locator_t*>& o);
301 };
302 WRITE_CLASS_ENCODER(object_locator_t)
303
304 inline bool operator==(const object_locator_t& l, const object_locator_t& r) {
305 return l.pool == r.pool && l.key == r.key && l.nspace == r.nspace && l.hash == r.hash;
306 }
307 inline bool operator!=(const object_locator_t& l, const object_locator_t& r) {
308 return !(l == r);
309 }
310
311 inline std::ostream& operator<<(std::ostream& out, const object_locator_t& loc)
312 {
313 out << "@" << loc.pool;
314 if (loc.nspace.length())
315 out << ";" << loc.nspace;
316 if (loc.key.length())
317 out << ":" << loc.key;
318 return out;
319 }
320
321 struct request_redirect_t {
322 private:
323 object_locator_t redirect_locator; ///< this is authoritative
324 std::string redirect_object; ///< If non-empty, the request goes to this object name
325
326 friend std::ostream& operator<<(std::ostream& out, const request_redirect_t& redir);
327 public:
328
329 request_redirect_t() {}
330 explicit request_redirect_t(const object_locator_t& orig, int64_t rpool) :
331 redirect_locator(orig) { redirect_locator.pool = rpool; }
332 explicit request_redirect_t(const object_locator_t& rloc) :
333 redirect_locator(rloc) {}
334 explicit request_redirect_t(const object_locator_t& orig,
335 const std::string& robj) :
336 redirect_locator(orig), redirect_object(robj) {}
337
338 bool empty() const { return redirect_locator.empty() &&
339 redirect_object.empty(); }
340
341 void combine_with_locator(object_locator_t& orig, std::string& obj) const {
342 orig = redirect_locator;
343 if (!redirect_object.empty())
344 obj = redirect_object;
345 }
346
347 void encode(ceph::buffer::list& bl) const;
348 void decode(ceph::buffer::list::const_iterator& bl);
349 void dump(ceph::Formatter *f) const;
350 static void generate_test_instances(std::list<request_redirect_t*>& o);
351 };
352 WRITE_CLASS_ENCODER(request_redirect_t)
353
354 inline std::ostream& operator<<(std::ostream& out, const request_redirect_t& redir) {
355 out << "object " << redir.redirect_object << ", locator{" << redir.redirect_locator << "}";
356 return out;
357 }
358
359 // Internal OSD op flags - set by the OSD based on the op types
360 enum {
361 CEPH_OSD_RMW_FLAG_READ = (1 << 1),
362 CEPH_OSD_RMW_FLAG_WRITE = (1 << 2),
363 CEPH_OSD_RMW_FLAG_CLASS_READ = (1 << 3),
364 CEPH_OSD_RMW_FLAG_CLASS_WRITE = (1 << 4),
365 CEPH_OSD_RMW_FLAG_PGOP = (1 << 5),
366 CEPH_OSD_RMW_FLAG_CACHE = (1 << 6),
367 CEPH_OSD_RMW_FLAG_FORCE_PROMOTE = (1 << 7),
368 CEPH_OSD_RMW_FLAG_SKIP_HANDLE_CACHE = (1 << 8),
369 CEPH_OSD_RMW_FLAG_SKIP_PROMOTE = (1 << 9),
370 CEPH_OSD_RMW_FLAG_RWORDERED = (1 << 10),
371 CEPH_OSD_RMW_FLAG_RETURNVEC = (1 << 11),
372 };
373
374
375 // pg stuff
376
377 #define OSD_SUPERBLOCK_GOBJECT ghobject_t(hobject_t(sobject_t(object_t("osd_superblock"), 0)))
378
379 // placement seed (a hash value)
380 typedef uint32_t ps_t;
381
382 // old (v1) pg_t encoding (wrap old struct ceph_pg)
383 struct old_pg_t {
384 ceph_pg v;
385 void encode(ceph::buffer::list& bl) const {
386 ceph::encode_raw(v, bl);
387 }
388 void decode(ceph::buffer::list::const_iterator& bl) {
389 ceph::decode_raw(v, bl);
390 }
391 };
392 WRITE_CLASS_ENCODER(old_pg_t)
393
394 // placement group id
395 struct pg_t {
396 uint64_t m_pool;
397 uint32_t m_seed;
398
399 pg_t() : m_pool(0), m_seed(0) {}
400 pg_t(ps_t seed, uint64_t pool) :
401 m_pool(pool), m_seed(seed) {}
402 // cppcheck-suppress noExplicitConstructor
403 pg_t(const ceph_pg& cpg) :
404 m_pool(cpg.pool), m_seed(cpg.ps) {}
405
406 // cppcheck-suppress noExplicitConstructor
407 pg_t(const old_pg_t& opg) {
408 *this = opg.v;
409 }
410
411 old_pg_t get_old_pg() const {
412 old_pg_t o;
413 ceph_assert(m_pool < 0xffffffffull);
414 o.v.pool = m_pool;
415 o.v.ps = m_seed;
416 o.v.preferred = (__s16)-1;
417 return o;
418 }
419
420 ps_t ps() const {
421 return m_seed;
422 }
423 int64_t pool() const {
424 return m_pool;
425 }
426
427 static const uint8_t calc_name_buf_size = 36; // max length for max values len("18446744073709551615.ffffffff") + future suffix len("_head") + '\0'
428 char *calc_name(char *buf, const char *suffix_backwords) const;
429
430 void set_ps(ps_t p) {
431 m_seed = p;
432 }
433 void set_pool(uint64_t p) {
434 m_pool = p;
435 }
436
437 pg_t get_parent() const;
438 pg_t get_ancestor(unsigned old_pg_num) const;
439
440 int print(char *o, int maxlen) const;
441 bool parse(const char *s);
442
443 bool is_split(unsigned old_pg_num, unsigned new_pg_num, std::set<pg_t> *pchildren) const;
444
445 bool is_merge_source(unsigned old_pg_num, unsigned new_pg_num, pg_t *parent) const;
446 bool is_merge_target(unsigned old_pg_num, unsigned new_pg_num) const {
447 return ps() < new_pg_num && is_split(new_pg_num, old_pg_num, nullptr);
448 }
449
450 /**
451 * Returns b such that for all object o:
452 * ~((~0)<<b) & o.hash) == 0 iff o is in the pg for *this
453 */
454 unsigned get_split_bits(unsigned pg_num) const;
455
456 bool contains(int bits, const ghobject_t& oid) const {
457 return
458 (int64_t)m_pool == oid.hobj.get_logical_pool() &&
459 oid.match(bits, ps());
460 }
461 bool contains(int bits, const hobject_t& oid) const {
462 return
463 (int64_t)m_pool == oid.get_logical_pool() &&
464 oid.match(bits, ps());
465 }
466
467 hobject_t get_hobj_start() const;
468 hobject_t get_hobj_end(unsigned pg_num) const;
469
470 // strong ordering is supported
471 inline int compare(const pg_t& p) const noexcept {
472 if (auto delta = pool() - p.pool(); delta != 0) {
473 return delta;
474 } else if (ps() < p.ps()) {
475 return -1;
476 } else if (ps() > p.ps()) {
477 return 1;
478 } else {
479 return 0;
480 }
481 }
482
483 void encode(ceph::buffer::list& bl) const {
484 using ceph::encode;
485 __u8 v = 1;
486 encode(v, bl);
487 encode(m_pool, bl);
488 encode(m_seed, bl);
489 encode((int32_t)-1, bl); // was preferred
490 }
491 void decode(ceph::buffer::list::const_iterator& bl) {
492 using ceph::decode;
493 __u8 v;
494 decode(v, bl);
495 decode(m_pool, bl);
496 decode(m_seed, bl);
497 bl += sizeof(int32_t); // was preferred
498 }
499 void decode_old(ceph::buffer::list::const_iterator& bl) {
500 using ceph::decode;
501 old_pg_t opg;
502 decode(opg, bl);
503 *this = opg;
504 }
505 void dump(ceph::Formatter *f) const;
506 static void generate_test_instances(std::list<pg_t*>& o);
507 };
508 WRITE_CLASS_ENCODER(pg_t)
509
510 inline bool operator<(const pg_t& l, const pg_t& r) {
511 return l.compare(r) < 0;
512 }
513 inline bool operator<=(const pg_t& l, const pg_t& r) {
514 return l.compare(r) <= 0;
515 }
516 inline bool operator==(const pg_t& l, const pg_t& r) {
517 return l.compare(r) == 0;
518 }
519 inline bool operator!=(const pg_t& l, const pg_t& r) {
520 return l.compare(r) != 0;
521 }
522 inline bool operator>(const pg_t& l, const pg_t& r) {
523 return l.compare(r) > 0;
524 }
525 inline bool operator>=(const pg_t& l, const pg_t& r) {
526 return l.compare(r) >= 0;
527 }
528
529 std::ostream& operator<<(std::ostream& out, const pg_t &pg);
530
531 namespace std {
532 template<> struct hash< pg_t >
533 {
534 size_t operator()( const pg_t& x ) const
535 {
536 static hash<uint32_t> H;
537 // xor (s32)-1 in there to preserve original m_preferred result (paranoia!)
538 return H((x.pool() & 0xffffffff) ^ (x.pool() >> 32) ^ x.ps() ^ (int32_t)(-1));
539 }
540 };
541 } // namespace std
542
543 struct spg_t {
544 pg_t pgid;
545 shard_id_t shard;
546 spg_t() : shard(shard_id_t::NO_SHARD) {}
547 spg_t(pg_t pgid, shard_id_t shard) : pgid(pgid), shard(shard) {}
548 explicit spg_t(pg_t pgid) : pgid(pgid), shard(shard_id_t::NO_SHARD) {}
549 unsigned get_split_bits(unsigned pg_num) const {
550 return pgid.get_split_bits(pg_num);
551 }
552 spg_t get_parent() const {
553 return spg_t(pgid.get_parent(), shard);
554 }
555 ps_t ps() const {
556 return pgid.ps();
557 }
558 uint64_t pool() const {
559 return pgid.pool();
560 }
561 void reset_shard(shard_id_t s) {
562 shard = s;
563 }
564
565 static const uint8_t calc_name_buf_size = pg_t::calc_name_buf_size + 4; // 36 + len('s') + len("255");
566 char *calc_name(char *buf, const char *suffix_backwords) const;
567
568 bool parse(const char *s);
569 bool parse(const std::string& s) {
570 return parse(s.c_str());
571 }
572
573 spg_t get_ancestor(unsigned old_pg_num) const {
574 return spg_t(pgid.get_ancestor(old_pg_num), shard);
575 }
576
577 bool is_split(unsigned old_pg_num, unsigned new_pg_num,
578 std::set<spg_t> *pchildren) const {
579 std::set<pg_t> _children;
580 std::set<pg_t> *children = pchildren ? &_children : NULL;
581 bool is_split = pgid.is_split(old_pg_num, new_pg_num, children);
582 if (pchildren && is_split) {
583 for (std::set<pg_t>::iterator i = _children.begin();
584 i != _children.end();
585 ++i) {
586 pchildren->insert(spg_t(*i, shard));
587 }
588 }
589 return is_split;
590 }
591 bool is_merge_target(unsigned old_pg_num, unsigned new_pg_num) const {
592 return pgid.is_merge_target(old_pg_num, new_pg_num);
593 }
594 bool is_merge_source(unsigned old_pg_num, unsigned new_pg_num,
595 spg_t *parent) const {
596 spg_t out = *this;
597 bool r = pgid.is_merge_source(old_pg_num, new_pg_num, &out.pgid);
598 if (r && parent) {
599 *parent = out;
600 }
601 return r;
602 }
603
604 bool is_no_shard() const {
605 return shard == shard_id_t::NO_SHARD;
606 }
607
608 ghobject_t make_pgmeta_oid() const {
609 return ghobject_t::make_pgmeta(pgid.pool(), pgid.ps(), shard);
610 }
611
612 void encode(ceph::buffer::list &bl) const {
613 ENCODE_START(1, 1, bl);
614 encode(pgid, bl);
615 encode(shard, bl);
616 ENCODE_FINISH(bl);
617 }
618 void decode(ceph::buffer::list::const_iterator& bl) {
619 DECODE_START(1, bl);
620 decode(pgid, bl);
621 decode(shard, bl);
622 DECODE_FINISH(bl);
623 }
624
625 ghobject_t make_temp_ghobject(const std::string& name) const {
626 return ghobject_t(
627 hobject_t(object_t(name), "", CEPH_NOSNAP,
628 pgid.ps(),
629 hobject_t::get_temp_pool(pgid.pool()),
630 ""),
631 ghobject_t::NO_GEN,
632 shard);
633 }
634
635 unsigned hash_to_shard(unsigned num_shards) const {
636 return ps() % num_shards;
637 }
638 };
639 WRITE_CLASS_ENCODER(spg_t)
640 WRITE_EQ_OPERATORS_2(spg_t, pgid, shard)
641 WRITE_CMP_OPERATORS_2(spg_t, pgid, shard)
642
643 namespace std {
644 template<> struct hash< spg_t >
645 {
646 size_t operator()( const spg_t& x ) const
647 {
648 static hash<uint32_t> H;
649 return H(hash<pg_t>()(x.pgid) ^ x.shard);
650 }
651 };
652 } // namespace std
653
654 std::ostream& operator<<(std::ostream& out, const spg_t &pg);
655
656 // ----------------------
657
658 class coll_t {
659 enum type_t : uint8_t {
660 TYPE_META = 0,
661 TYPE_LEGACY_TEMP = 1, /* no longer used */
662 TYPE_PG = 2,
663 TYPE_PG_TEMP = 3,
664 };
665 type_t type;
666 spg_t pgid;
667 uint64_t removal_seq; // note: deprecated, not encoded
668
669 char _str_buff[spg_t::calc_name_buf_size];
670 char *_str;
671
672 void calc_str();
673
674 coll_t(type_t t, spg_t p, uint64_t r)
675 : type(t), pgid(p), removal_seq(r) {
676 calc_str();
677 }
678
679 friend class denc_coll_t;
680 public:
681 coll_t() : type(TYPE_META), removal_seq(0)
682 {
683 calc_str();
684 }
685
686 coll_t(const coll_t& other)
687 : type(other.type), pgid(other.pgid), removal_seq(other.removal_seq) {
688 calc_str();
689 }
690
691 explicit coll_t(spg_t pgid)
692 : type(TYPE_PG), pgid(pgid), removal_seq(0)
693 {
694 calc_str();
695 }
696
697 coll_t& operator=(const coll_t& rhs)
698 {
699 this->type = rhs.type;
700 this->pgid = rhs.pgid;
701 this->removal_seq = rhs.removal_seq;
702 this->calc_str();
703 return *this;
704 }
705
706 // named constructors
707 static coll_t meta() {
708 return coll_t();
709 }
710 static coll_t pg(spg_t p) {
711 return coll_t(p);
712 }
713
714 const std::string to_str() const {
715 return std::string(_str);
716 }
717 const char *c_str() const {
718 return _str;
719 }
720
721 bool parse(const std::string& s);
722
723 int operator<(const coll_t &rhs) const {
724 return type < rhs.type ||
725 (type == rhs.type && pgid < rhs.pgid);
726 }
727
728 bool is_meta() const {
729 return type == TYPE_META;
730 }
731 bool is_pg_prefix(spg_t *pgid_) const {
732 if (type == TYPE_PG || type == TYPE_PG_TEMP) {
733 *pgid_ = pgid;
734 return true;
735 }
736 return false;
737 }
738 bool is_pg() const {
739 return type == TYPE_PG;
740 }
741 bool is_pg(spg_t *pgid_) const {
742 if (type == TYPE_PG) {
743 *pgid_ = pgid;
744 return true;
745 }
746 return false;
747 }
748 bool is_temp() const {
749 return type == TYPE_PG_TEMP;
750 }
751 bool is_temp(spg_t *pgid_) const {
752 if (type == TYPE_PG_TEMP) {
753 *pgid_ = pgid;
754 return true;
755 }
756 return false;
757 }
758 int64_t pool() const {
759 return pgid.pool();
760 }
761
762 void encode(ceph::buffer::list& bl) const;
763 void decode(ceph::buffer::list::const_iterator& bl);
764 size_t encoded_size() const;
765
766 inline bool operator==(const coll_t& rhs) const {
767 // only compare type if meta
768 if (type != rhs.type)
769 return false;
770 if (type == TYPE_META)
771 return true;
772 return type == rhs.type && pgid == rhs.pgid;
773 }
774 inline bool operator!=(const coll_t& rhs) const {
775 return !(*this == rhs);
776 }
777
778 // get a TEMP collection that corresponds to the current collection,
779 // which we presume is a pg collection.
780 coll_t get_temp() const {
781 ceph_assert(type == TYPE_PG);
782 return coll_t(TYPE_PG_TEMP, pgid, 0);
783 }
784
785 ghobject_t get_min_hobj() const {
786 ghobject_t o;
787 switch (type) {
788 case TYPE_PG:
789 o.hobj.pool = pgid.pool();
790 o.set_shard(pgid.shard);
791 break;
792 case TYPE_META:
793 o.hobj.pool = -1;
794 break;
795 default:
796 break;
797 }
798 return o;
799 }
800
801 unsigned hash_to_shard(unsigned num_shards) const {
802 if (type == TYPE_PG)
803 return pgid.hash_to_shard(num_shards);
804 return 0; // whatever.
805 }
806
807 void dump(ceph::Formatter *f) const;
808 static void generate_test_instances(std::list<coll_t*>& o);
809 };
810
811 WRITE_CLASS_ENCODER(coll_t)
812
813 inline std::ostream& operator<<(std::ostream& out, const coll_t& c) {
814 out << c.to_str();
815 return out;
816 }
817
818 namespace std {
819 template<> struct hash<coll_t> {
820 size_t operator()(const coll_t &c) const {
821 size_t h = 0;
822 std::string str(c.to_str());
823 std::string::const_iterator end(str.end());
824 for (std::string::const_iterator s = str.begin(); s != end; ++s) {
825 h += *s;
826 h += (h << 10);
827 h ^= (h >> 6);
828 }
829 h += (h << 3);
830 h ^= (h >> 11);
831 h += (h << 15);
832 return h;
833 }
834 };
835 } // namespace std
836
837 inline std::ostream& operator<<(std::ostream& out, const ceph_object_layout &ol)
838 {
839 out << pg_t(ol.ol_pgid);
840 int su = ol.ol_stripe_unit;
841 if (su)
842 out << ".su=" << su;
843 return out;
844 }
845
846 struct denc_coll_t {
847 coll_t coll;
848
849 auto &get_type() const { return coll.type; }
850 auto &get_type() { return coll.type; }
851 auto &get_pgid() const { return coll.pgid; }
852 auto &get_pgid() { return coll.pgid; }
853
854 denc_coll_t() = default;
855 denc_coll_t(const denc_coll_t &) = default;
856 denc_coll_t(denc_coll_t &&) = default;
857
858 denc_coll_t &operator=(const denc_coll_t &) = default;
859 denc_coll_t &operator=(denc_coll_t &&) = default;
860
861 explicit denc_coll_t(const coll_t &coll) : coll(coll) {}
862 operator coll_t() const {
863 return coll;
864 }
865
866 bool operator<(const denc_coll_t &rhs) const {
867 return coll < rhs.coll;
868 }
869
870 DENC(denc_coll_t, v, p) {
871 DENC_START(1, 1, p);
872 denc(v.get_type(), p);
873 denc(v.get_pgid().pgid.m_pool, p);
874 denc(v.get_pgid().pgid.m_seed, p);
875 denc(v.get_pgid().shard.id, p);
876 DENC_FINISH(p);
877 }
878 };
879 WRITE_CLASS_DENC(denc_coll_t)
880
881
882 // compound rados version type
883 /* WARNING: If add member in eversion_t, please make sure the encode/decode function
884 * work well. For little-endian machine, we should make sure there is no padding
885 * in 32-bit machine and 64-bit machine.
886 */
887 class eversion_t {
888 public:
889 version_t version;
890 epoch_t epoch;
891 __u32 __pad;
892 eversion_t() : version(0), epoch(0), __pad(0) {}
893 eversion_t(epoch_t e, version_t v) : version(v), epoch(e), __pad(0) {}
894
895 // cppcheck-suppress noExplicitConstructor
896 eversion_t(const ceph_eversion& ce) :
897 version(ce.version),
898 epoch(ce.epoch),
899 __pad(0) { }
900
901 explicit eversion_t(ceph::buffer::list& bl) : __pad(0) { decode(bl); }
902
903 static const eversion_t& max() {
904 static const eversion_t max(-1,-1);
905 return max;
906 }
907
908 operator ceph_eversion() {
909 ceph_eversion c;
910 c.epoch = epoch;
911 c.version = version;
912 return c;
913 }
914
915 std::string get_key_name() const;
916
917 // key must point to the beginning of a block of 32 chars
918 inline void get_key_name(char* key) const {
919 // Below is equivalent of sprintf("%010u.%020llu");
920 key[31] = 0;
921 ritoa<uint64_t, 10, 20>(version, key + 31);
922 key[10] = '.';
923 ritoa<uint32_t, 10, 10>(epoch, key + 10);
924 }
925
926 void encode(ceph::buffer::list &bl) const {
927 #if defined(CEPH_LITTLE_ENDIAN)
928 bl.append((char *)this, sizeof(version_t) + sizeof(epoch_t));
929 #else
930 using ceph::encode;
931 encode(version, bl);
932 encode(epoch, bl);
933 #endif
934 }
935 void decode(ceph::buffer::list::const_iterator &bl) {
936 #if defined(CEPH_LITTLE_ENDIAN)
937 bl.copy(sizeof(version_t) + sizeof(epoch_t), (char *)this);
938 #else
939 using ceph::decode;
940 decode(version, bl);
941 decode(epoch, bl);
942 #endif
943 }
944 void decode(ceph::buffer::list& bl) {
945 auto p = std::cbegin(bl);
946 decode(p);
947 }
948 };
949 WRITE_CLASS_ENCODER(eversion_t)
950
951 inline bool operator==(const eversion_t& l, const eversion_t& r) {
952 return (l.epoch == r.epoch) && (l.version == r.version);
953 }
954 inline bool operator!=(const eversion_t& l, const eversion_t& r) {
955 return (l.epoch != r.epoch) || (l.version != r.version);
956 }
957 inline bool operator<(const eversion_t& l, const eversion_t& r) {
958 return (l.epoch == r.epoch) ? (l.version < r.version):(l.epoch < r.epoch);
959 }
960 inline bool operator<=(const eversion_t& l, const eversion_t& r) {
961 return (l.epoch == r.epoch) ? (l.version <= r.version):(l.epoch <= r.epoch);
962 }
963 inline bool operator>(const eversion_t& l, const eversion_t& r) {
964 return (l.epoch == r.epoch) ? (l.version > r.version):(l.epoch > r.epoch);
965 }
966 inline bool operator>=(const eversion_t& l, const eversion_t& r) {
967 return (l.epoch == r.epoch) ? (l.version >= r.version):(l.epoch >= r.epoch);
968 }
969 inline std::ostream& operator<<(std::ostream& out, const eversion_t& e) {
970 return out << e.epoch << "'" << e.version;
971 }
972
973 /**
974 * objectstore_perf_stat_t
975 *
976 * current perf information about the osd
977 */
978 struct objectstore_perf_stat_t {
979 // cur_op_latency is in ns since double add/sub are not associative
980 uint64_t os_commit_latency_ns;
981 uint64_t os_apply_latency_ns;
982
983 objectstore_perf_stat_t() :
984 os_commit_latency_ns(0), os_apply_latency_ns(0) {}
985
986 bool operator==(const objectstore_perf_stat_t &r) const {
987 return os_commit_latency_ns == r.os_commit_latency_ns &&
988 os_apply_latency_ns == r.os_apply_latency_ns;
989 }
990
991 void add(const objectstore_perf_stat_t &o) {
992 os_commit_latency_ns += o.os_commit_latency_ns;
993 os_apply_latency_ns += o.os_apply_latency_ns;
994 }
995 void sub(const objectstore_perf_stat_t &o) {
996 os_commit_latency_ns -= o.os_commit_latency_ns;
997 os_apply_latency_ns -= o.os_apply_latency_ns;
998 }
999 void dump(ceph::Formatter *f) const;
1000 void encode(ceph::buffer::list &bl, uint64_t features) const;
1001 void decode(ceph::buffer::list::const_iterator &bl);
1002 static void generate_test_instances(std::list<objectstore_perf_stat_t*>& o);
1003 };
1004 WRITE_CLASS_ENCODER_FEATURES(objectstore_perf_stat_t)
1005
1006 /*
1007 * pg states
1008 */
1009 #define PG_STATE_CREATING (1ULL << 0) // creating
1010 #define PG_STATE_ACTIVE (1ULL << 1) // i am active. (primary: replicas too)
1011 #define PG_STATE_CLEAN (1ULL << 2) // peers are complete, clean of stray replicas.
1012 #define PG_STATE_DOWN (1ULL << 4) // a needed replica is down, PG offline
1013 #define PG_STATE_RECOVERY_UNFOUND (1ULL << 5) // recovery stopped due to unfound
1014 #define PG_STATE_BACKFILL_UNFOUND (1ULL << 6) // backfill stopped due to unfound
1015 #define PG_STATE_PREMERGE (1ULL << 7) // i am prepare to merging
1016 #define PG_STATE_SCRUBBING (1ULL << 8) // scrubbing
1017 //#define PG_STATE_SCRUBQ (1ULL << 9) // queued for scrub
1018 #define PG_STATE_DEGRADED (1ULL << 10) // pg contains objects with reduced redundancy
1019 #define PG_STATE_INCONSISTENT (1ULL << 11) // pg replicas are inconsistent (but shouldn't be)
1020 #define PG_STATE_PEERING (1ULL << 12) // pg is (re)peering
1021 #define PG_STATE_REPAIR (1ULL << 13) // pg should repair on next scrub
1022 #define PG_STATE_RECOVERING (1ULL << 14) // pg is recovering/migrating objects
1023 #define PG_STATE_BACKFILL_WAIT (1ULL << 15) // [active] reserving backfill
1024 #define PG_STATE_INCOMPLETE (1ULL << 16) // incomplete content, peering failed.
1025 #define PG_STATE_STALE (1ULL << 17) // our state for this pg is stale, unknown.
1026 #define PG_STATE_REMAPPED (1ULL << 18) // pg is explicitly remapped to different OSDs than CRUSH
1027 #define PG_STATE_DEEP_SCRUB (1ULL << 19) // deep scrub: check CRC32 on files
1028 #define PG_STATE_BACKFILLING (1ULL << 20) // [active] backfilling pg content
1029 #define PG_STATE_BACKFILL_TOOFULL (1ULL << 21) // backfill can't proceed: too full
1030 #define PG_STATE_RECOVERY_WAIT (1ULL << 22) // waiting for recovery reservations
1031 #define PG_STATE_UNDERSIZED (1ULL << 23) // pg acting < pool size
1032 #define PG_STATE_ACTIVATING (1ULL << 24) // pg is peered but not yet active
1033 #define PG_STATE_PEERED (1ULL << 25) // peered, cannot go active, can recover
1034 #define PG_STATE_SNAPTRIM (1ULL << 26) // trimming snaps
1035 #define PG_STATE_SNAPTRIM_WAIT (1ULL << 27) // queued to trim snaps
1036 #define PG_STATE_RECOVERY_TOOFULL (1ULL << 28) // recovery can't proceed: too full
1037 #define PG_STATE_SNAPTRIM_ERROR (1ULL << 29) // error stopped trimming snaps
1038 #define PG_STATE_FORCED_RECOVERY (1ULL << 30) // force recovery of this pg before any other
1039 #define PG_STATE_FORCED_BACKFILL (1ULL << 31) // force backfill of this pg before any other
1040 #define PG_STATE_FAILED_REPAIR (1ULL << 32) // A repair failed to fix all errors
1041 #define PG_STATE_LAGGY (1ULL << 33) // PG is laggy/unreabable due to slow/delayed pings
1042 #define PG_STATE_WAIT (1ULL << 34) // PG is waiting for prior intervals' readable period to expire
1043
1044 std::string pg_state_string(uint64_t state);
1045 std::string pg_vector_string(const std::vector<int32_t> &a);
1046 std::optional<uint64_t> pg_string_state(const std::string& state);
1047
1048
1049 /*
1050 * pool_snap_info_t
1051 *
1052 * attributes for a single pool snapshot.
1053 */
1054 struct pool_snap_info_t {
1055 snapid_t snapid;
1056 utime_t stamp;
1057 std::string name;
1058
1059 void dump(ceph::Formatter *f) const;
1060 void encode(ceph::buffer::list& bl, uint64_t features) const;
1061 void decode(ceph::buffer::list::const_iterator& bl);
1062 static void generate_test_instances(std::list<pool_snap_info_t*>& o);
1063 };
1064 WRITE_CLASS_ENCODER_FEATURES(pool_snap_info_t)
1065
1066 inline std::ostream& operator<<(std::ostream& out, const pool_snap_info_t& si) {
1067 return out << si.snapid << '(' << si.name << ' ' << si.stamp << ')';
1068 }
1069
1070
1071 /*
1072 * pool_opts_t
1073 *
1074 * pool options.
1075 */
1076
1077 class pool_opts_t {
1078 public:
1079 enum key_t {
1080 SCRUB_MIN_INTERVAL,
1081 SCRUB_MAX_INTERVAL,
1082 DEEP_SCRUB_INTERVAL,
1083 RECOVERY_PRIORITY,
1084 RECOVERY_OP_PRIORITY,
1085 SCRUB_PRIORITY,
1086 COMPRESSION_MODE,
1087 COMPRESSION_ALGORITHM,
1088 COMPRESSION_REQUIRED_RATIO,
1089 COMPRESSION_MAX_BLOB_SIZE,
1090 COMPRESSION_MIN_BLOB_SIZE,
1091 CSUM_TYPE,
1092 CSUM_MAX_BLOCK,
1093 CSUM_MIN_BLOCK,
1094 FINGERPRINT_ALGORITHM,
1095 PG_NUM_MIN, // min pg_num
1096 PG_NUM_MAX, // max pg_num
1097 TARGET_SIZE_BYTES, // total bytes in pool
1098 TARGET_SIZE_RATIO, // fraction of total cluster
1099 PG_AUTOSCALE_BIAS,
1100 READ_LEASE_INTERVAL,
1101 DEDUP_TIER,
1102 DEDUP_CHUNK_ALGORITHM,
1103 DEDUP_CDC_CHUNK_SIZE,
1104 };
1105
1106 enum type_t {
1107 STR,
1108 INT,
1109 DOUBLE,
1110 };
1111
1112 struct opt_desc_t {
1113 key_t key;
1114 type_t type;
1115
1116 opt_desc_t(key_t k, type_t t) : key(k), type(t) {}
1117
1118 bool operator==(const opt_desc_t& rhs) const {
1119 return key == rhs.key && type == rhs.type;
1120 }
1121 };
1122
1123 typedef boost::variant<std::string,int64_t,double> value_t;
1124
1125 static bool is_opt_name(const std::string& name);
1126 static opt_desc_t get_opt_desc(const std::string& name);
1127
1128 pool_opts_t() : opts() {}
1129
1130 bool is_set(key_t key) const;
1131
1132 template<typename T>
1133 void set(key_t key, const T &val) {
1134 value_t value = val;
1135 opts[key] = value;
1136 }
1137
1138 template<typename T>
1139 bool get(key_t key, T *val) const {
1140 opts_t::const_iterator i = opts.find(key);
1141 if (i == opts.end()) {
1142 return false;
1143 }
1144 *val = boost::get<T>(i->second);
1145 return true;
1146 }
1147
1148 template<typename T>
1149 T value_or(key_t key, T&& default_value) const {
1150 auto i = opts.find(key);
1151 if (i == opts.end()) {
1152 return std::forward<T>(default_value);
1153 }
1154 return boost::get<T>(i->second);
1155 }
1156
1157 const value_t& get(key_t key) const;
1158
1159 bool unset(key_t key);
1160
1161 void dump(const std::string& name, ceph::Formatter *f) const;
1162
1163 void dump(ceph::Formatter *f) const;
1164 void encode(ceph::buffer::list &bl, uint64_t features) const;
1165 void decode(ceph::buffer::list::const_iterator &bl);
1166
1167 private:
1168 typedef std::map<key_t, value_t> opts_t;
1169 opts_t opts;
1170
1171 friend std::ostream& operator<<(std::ostream& out, const pool_opts_t& opts);
1172 };
1173 WRITE_CLASS_ENCODER_FEATURES(pool_opts_t)
1174
1175 struct pg_merge_meta_t {
1176 pg_t source_pgid;
1177 epoch_t ready_epoch = 0;
1178 epoch_t last_epoch_started = 0;
1179 epoch_t last_epoch_clean = 0;
1180 eversion_t source_version;
1181 eversion_t target_version;
1182
1183 void encode(ceph::buffer::list& bl) const {
1184 ENCODE_START(1, 1, bl);
1185 encode(source_pgid, bl);
1186 encode(ready_epoch, bl);
1187 encode(last_epoch_started, bl);
1188 encode(last_epoch_clean, bl);
1189 encode(source_version, bl);
1190 encode(target_version, bl);
1191 ENCODE_FINISH(bl);
1192 }
1193 void decode(ceph::buffer::list::const_iterator& p) {
1194 DECODE_START(1, p);
1195 decode(source_pgid, p);
1196 decode(ready_epoch, p);
1197 decode(last_epoch_started, p);
1198 decode(last_epoch_clean, p);
1199 decode(source_version, p);
1200 decode(target_version, p);
1201 DECODE_FINISH(p);
1202 }
1203 void dump(ceph::Formatter *f) const {
1204 f->dump_stream("source_pgid") << source_pgid;
1205 f->dump_unsigned("ready_epoch", ready_epoch);
1206 f->dump_unsigned("last_epoch_started", last_epoch_started);
1207 f->dump_unsigned("last_epoch_clean", last_epoch_clean);
1208 f->dump_stream("source_version") << source_version;
1209 f->dump_stream("target_version") << target_version;
1210 }
1211 };
1212 WRITE_CLASS_ENCODER(pg_merge_meta_t)
1213
1214 class OSDMap;
1215
1216 /*
1217 * pg_pool
1218 */
1219 struct pg_pool_t {
1220 static const char *APPLICATION_NAME_CEPHFS;
1221 static const char *APPLICATION_NAME_RBD;
1222 static const char *APPLICATION_NAME_RGW;
1223
1224 enum {
1225 TYPE_REPLICATED = 1, // replication
1226 //TYPE_RAID4 = 2, // raid4 (never implemented)
1227 TYPE_ERASURE = 3, // erasure-coded
1228 };
1229 static constexpr uint32_t pg_CRUSH_ITEM_NONE = 0x7fffffff; /* can't import crush.h here */
1230 static std::string_view get_type_name(int t) {
1231 switch (t) {
1232 case TYPE_REPLICATED: return "replicated";
1233 //case TYPE_RAID4: return "raid4";
1234 case TYPE_ERASURE: return "erasure";
1235 default: return "???";
1236 }
1237 }
1238 std::string_view get_type_name() const {
1239 return get_type_name(type);
1240 }
1241
1242 enum {
1243 FLAG_HASHPSPOOL = 1<<0, // hash pg seed and pool together (instead of adding)
1244 FLAG_FULL = 1<<1, // pool is full
1245 FLAG_EC_OVERWRITES = 1<<2, // enables overwrites, once enabled, cannot be disabled
1246 FLAG_INCOMPLETE_CLONES = 1<<3, // may have incomplete clones (bc we are/were an overlay)
1247 FLAG_NODELETE = 1<<4, // pool can't be deleted
1248 FLAG_NOPGCHANGE = 1<<5, // pool's pg and pgp num can't be changed
1249 FLAG_NOSIZECHANGE = 1<<6, // pool's size and min size can't be changed
1250 FLAG_WRITE_FADVISE_DONTNEED = 1<<7, // write mode with LIBRADOS_OP_FLAG_FADVISE_DONTNEED
1251 FLAG_NOSCRUB = 1<<8, // block periodic scrub
1252 FLAG_NODEEP_SCRUB = 1<<9, // block periodic deep-scrub
1253 FLAG_FULL_QUOTA = 1<<10, // pool is currently running out of quota, will set FLAG_FULL too
1254 FLAG_NEARFULL = 1<<11, // pool is nearfull
1255 FLAG_BACKFILLFULL = 1<<12, // pool is backfillfull
1256 FLAG_SELFMANAGED_SNAPS = 1<<13, // pool uses selfmanaged snaps
1257 FLAG_POOL_SNAPS = 1<<14, // pool has pool snaps
1258 FLAG_CREATING = 1<<15, // initial pool PGs are being created
1259 FLAG_EIO = 1<<16, // return EIO for all client ops
1260 FLAG_BULK = 1<<17, //pool is large
1261 };
1262
1263 static const char *get_flag_name(uint64_t f) {
1264 switch (f) {
1265 case FLAG_HASHPSPOOL: return "hashpspool";
1266 case FLAG_FULL: return "full";
1267 case FLAG_EC_OVERWRITES: return "ec_overwrites";
1268 case FLAG_INCOMPLETE_CLONES: return "incomplete_clones";
1269 case FLAG_NODELETE: return "nodelete";
1270 case FLAG_NOPGCHANGE: return "nopgchange";
1271 case FLAG_NOSIZECHANGE: return "nosizechange";
1272 case FLAG_WRITE_FADVISE_DONTNEED: return "write_fadvise_dontneed";
1273 case FLAG_NOSCRUB: return "noscrub";
1274 case FLAG_NODEEP_SCRUB: return "nodeep-scrub";
1275 case FLAG_FULL_QUOTA: return "full_quota";
1276 case FLAG_NEARFULL: return "nearfull";
1277 case FLAG_BACKFILLFULL: return "backfillfull";
1278 case FLAG_SELFMANAGED_SNAPS: return "selfmanaged_snaps";
1279 case FLAG_POOL_SNAPS: return "pool_snaps";
1280 case FLAG_CREATING: return "creating";
1281 case FLAG_EIO: return "eio";
1282 case FLAG_BULK: return "bulk";
1283 default: return "???";
1284 }
1285 }
1286 static std::string get_flags_string(uint64_t f) {
1287 std::string s;
1288 for (unsigned n=0; f && n<64; ++n) {
1289 if (f & (1ull << n)) {
1290 if (s.length())
1291 s += ",";
1292 s += get_flag_name(1ull << n);
1293 }
1294 }
1295 return s;
1296 }
1297 std::string get_flags_string() const {
1298 return get_flags_string(flags);
1299 }
1300 static uint64_t get_flag_by_name(const std::string& name) {
1301 if (name == "hashpspool")
1302 return FLAG_HASHPSPOOL;
1303 if (name == "full")
1304 return FLAG_FULL;
1305 if (name == "ec_overwrites")
1306 return FLAG_EC_OVERWRITES;
1307 if (name == "incomplete_clones")
1308 return FLAG_INCOMPLETE_CLONES;
1309 if (name == "nodelete")
1310 return FLAG_NODELETE;
1311 if (name == "nopgchange")
1312 return FLAG_NOPGCHANGE;
1313 if (name == "nosizechange")
1314 return FLAG_NOSIZECHANGE;
1315 if (name == "write_fadvise_dontneed")
1316 return FLAG_WRITE_FADVISE_DONTNEED;
1317 if (name == "noscrub")
1318 return FLAG_NOSCRUB;
1319 if (name == "nodeep-scrub")
1320 return FLAG_NODEEP_SCRUB;
1321 if (name == "full_quota")
1322 return FLAG_FULL_QUOTA;
1323 if (name == "nearfull")
1324 return FLAG_NEARFULL;
1325 if (name == "backfillfull")
1326 return FLAG_BACKFILLFULL;
1327 if (name == "selfmanaged_snaps")
1328 return FLAG_SELFMANAGED_SNAPS;
1329 if (name == "pool_snaps")
1330 return FLAG_POOL_SNAPS;
1331 if (name == "creating")
1332 return FLAG_CREATING;
1333 if (name == "eio")
1334 return FLAG_EIO;
1335 if (name == "bulk")
1336 return FLAG_BULK;
1337 return 0;
1338 }
1339
1340 /// converts the acting/up vector to a set of pg shards
1341 void convert_to_pg_shards(const std::vector<int> &from, std::set<pg_shard_t>* to) const;
1342
1343 typedef enum {
1344 CACHEMODE_NONE = 0, ///< no caching
1345 CACHEMODE_WRITEBACK = 1, ///< write to cache, flush later
1346 CACHEMODE_FORWARD = 2, ///< forward if not in cache
1347 CACHEMODE_READONLY = 3, ///< handle reads, forward writes [not strongly consistent]
1348 CACHEMODE_READFORWARD = 4, ///< forward reads, write to cache flush later
1349 CACHEMODE_READPROXY = 5, ///< proxy reads, write to cache flush later
1350 CACHEMODE_PROXY = 6, ///< proxy if not in cache
1351 } cache_mode_t;
1352 static const char *get_cache_mode_name(cache_mode_t m) {
1353 switch (m) {
1354 case CACHEMODE_NONE: return "none";
1355 case CACHEMODE_WRITEBACK: return "writeback";
1356 case CACHEMODE_FORWARD: return "forward";
1357 case CACHEMODE_READONLY: return "readonly";
1358 case CACHEMODE_READFORWARD: return "readforward";
1359 case CACHEMODE_READPROXY: return "readproxy";
1360 case CACHEMODE_PROXY: return "proxy";
1361 default: return "unknown";
1362 }
1363 }
1364 static cache_mode_t get_cache_mode_from_str(const std::string& s) {
1365 if (s == "none")
1366 return CACHEMODE_NONE;
1367 if (s == "writeback")
1368 return CACHEMODE_WRITEBACK;
1369 if (s == "forward")
1370 return CACHEMODE_FORWARD;
1371 if (s == "readonly")
1372 return CACHEMODE_READONLY;
1373 if (s == "readforward")
1374 return CACHEMODE_READFORWARD;
1375 if (s == "readproxy")
1376 return CACHEMODE_READPROXY;
1377 if (s == "proxy")
1378 return CACHEMODE_PROXY;
1379 return (cache_mode_t)-1;
1380 }
1381 const char *get_cache_mode_name() const {
1382 return get_cache_mode_name(cache_mode);
1383 }
1384 bool cache_mode_requires_hit_set() const {
1385 switch (cache_mode) {
1386 case CACHEMODE_NONE:
1387 case CACHEMODE_FORWARD:
1388 case CACHEMODE_READONLY:
1389 case CACHEMODE_PROXY:
1390 return false;
1391 case CACHEMODE_WRITEBACK:
1392 case CACHEMODE_READFORWARD:
1393 case CACHEMODE_READPROXY:
1394 return true;
1395 default:
1396 ceph_abort_msg("implement me");
1397 }
1398 }
1399
1400 enum class pg_autoscale_mode_t : uint8_t {
1401 OFF = 0,
1402 WARN = 1,
1403 ON = 2,
1404 UNKNOWN = UINT8_MAX,
1405 };
1406 static const char *get_pg_autoscale_mode_name(pg_autoscale_mode_t m) {
1407 switch (m) {
1408 case pg_autoscale_mode_t::OFF: return "off";
1409 case pg_autoscale_mode_t::ON: return "on";
1410 case pg_autoscale_mode_t::WARN: return "warn";
1411 default: return "???";
1412 }
1413 }
1414 static pg_autoscale_mode_t get_pg_autoscale_mode_by_name(const std::string& m) {
1415 if (m == "off") {
1416 return pg_autoscale_mode_t::OFF;
1417 }
1418 if (m == "warn") {
1419 return pg_autoscale_mode_t::WARN;
1420 }
1421 if (m == "on") {
1422 return pg_autoscale_mode_t::ON;
1423 }
1424 return pg_autoscale_mode_t::UNKNOWN;
1425 }
1426
1427 utime_t create_time;
1428 uint64_t flags = 0; ///< FLAG_*
1429 __u8 type = 0; ///< TYPE_*
1430 __u8 size = 0, min_size = 0; ///< number of osds in each pg
1431 __u8 crush_rule = 0; ///< crush placement rule
1432 __u8 object_hash = 0; ///< hash mapping object name to ps
1433 pg_autoscale_mode_t pg_autoscale_mode = pg_autoscale_mode_t::UNKNOWN;
1434
1435 private:
1436 __u32 pg_num = 0, pgp_num = 0; ///< number of pgs
1437 __u32 pg_num_pending = 0; ///< pg_num we are about to merge down to
1438 __u32 pg_num_target = 0; ///< pg_num we should converge toward
1439 __u32 pgp_num_target = 0; ///< pgp_num we should converge toward
1440
1441 public:
1442 std::map<std::string, std::string> properties; ///< OBSOLETE
1443 std::string erasure_code_profile; ///< name of the erasure code profile in OSDMap
1444 epoch_t last_change = 0; ///< most recent epoch changed, exclusing snapshot changes
1445 // If non-zero, require OSDs in at least this many different instances...
1446 uint32_t peering_crush_bucket_count = 0;
1447 // of this bucket type...
1448 uint32_t peering_crush_bucket_barrier = 0;
1449 // including this one
1450 int32_t peering_crush_mandatory_member = pg_CRUSH_ITEM_NONE;
1451 // The per-bucket replica count is calculated with this "target"
1452 // instead of the above crush_bucket_count. This means we can maintain a
1453 // target size of 4 without attempting to place them all in 1 DC
1454 uint32_t peering_crush_bucket_target = 0;
1455 /// last epoch that forced clients to resend
1456 epoch_t last_force_op_resend = 0;
1457 /// last epoch that forced clients to resend (pre-nautilus clients only)
1458 epoch_t last_force_op_resend_prenautilus = 0;
1459 /// last epoch that forced clients to resend (pre-luminous clients only)
1460 epoch_t last_force_op_resend_preluminous = 0;
1461
1462 /// metadata for the most recent PG merge
1463 pg_merge_meta_t last_pg_merge_meta;
1464
1465 snapid_t snap_seq = 0; ///< seq for per-pool snapshot
1466 epoch_t snap_epoch = 0; ///< osdmap epoch of last snap
1467 uint64_t auid = 0; ///< who owns the pg
1468
1469 uint64_t quota_max_bytes = 0; ///< maximum number of bytes for this pool
1470 uint64_t quota_max_objects = 0; ///< maximum number of objects for this pool
1471
1472 /*
1473 * Pool snaps (global to this pool). These define a SnapContext for
1474 * the pool, unless the client manually specifies an alternate
1475 * context.
1476 */
1477 std::map<snapid_t, pool_snap_info_t> snaps;
1478 /*
1479 * Alternatively, if we are defining non-pool snaps (e.g. via the
1480 * Ceph MDS), we must track @removed_snaps (since @snaps is not
1481 * used). Snaps and removed_snaps are to be used exclusive of each
1482 * other!
1483 */
1484 interval_set<snapid_t> removed_snaps;
1485
1486 unsigned pg_num_mask = 0, pgp_num_mask = 0;
1487
1488 std::set<uint64_t> tiers; ///< pools that are tiers of us
1489 int64_t tier_of = -1; ///< pool for which we are a tier
1490 // Note that write wins for read+write ops
1491 int64_t read_tier = -1; ///< pool/tier for objecter to direct reads to
1492 int64_t write_tier = -1; ///< pool/tier for objecter to direct writes to
1493 cache_mode_t cache_mode = CACHEMODE_NONE; ///< cache pool mode
1494
1495 bool is_tier() const { return tier_of >= 0; }
1496 bool has_tiers() const { return !tiers.empty(); }
1497 void clear_tier() {
1498 tier_of = -1;
1499 clear_read_tier();
1500 clear_write_tier();
1501 clear_tier_tunables();
1502 }
1503 bool has_read_tier() const { return read_tier >= 0; }
1504 void clear_read_tier() { read_tier = -1; }
1505 bool has_write_tier() const { return write_tier >= 0; }
1506 void clear_write_tier() { write_tier = -1; }
1507 void clear_tier_tunables() {
1508 if (cache_mode != CACHEMODE_NONE)
1509 flags |= FLAG_INCOMPLETE_CLONES;
1510 cache_mode = CACHEMODE_NONE;
1511
1512 target_max_bytes = 0;
1513 target_max_objects = 0;
1514 cache_target_dirty_ratio_micro = 0;
1515 cache_target_dirty_high_ratio_micro = 0;
1516 cache_target_full_ratio_micro = 0;
1517 hit_set_params = HitSet::Params();
1518 hit_set_period = 0;
1519 hit_set_count = 0;
1520 hit_set_grade_decay_rate = 0;
1521 hit_set_search_last_n = 0;
1522 grade_table.resize(0);
1523 }
1524
1525 bool is_stretch_pool() const {
1526 return peering_crush_bucket_count != 0;
1527 }
1528
1529 bool stretch_set_can_peer(const std::set<int>& want, const OSDMap& osdmap,
1530 std::ostream *out) const;
1531 bool stretch_set_can_peer(const std::vector<int>& want, const OSDMap& osdmap,
1532 std::ostream *out) const {
1533 if (!is_stretch_pool()) return true;
1534 std::set<int> swant;
1535 for (auto i : want) swant.insert(i);
1536 return stretch_set_can_peer(swant, osdmap, out);
1537 }
1538
1539 uint64_t target_max_bytes = 0; ///< tiering: target max pool size
1540 uint64_t target_max_objects = 0; ///< tiering: target max pool size
1541
1542 uint32_t cache_target_dirty_ratio_micro = 0; ///< cache: fraction of target to leave dirty
1543 uint32_t cache_target_dirty_high_ratio_micro = 0; ///< cache: fraction of target to flush with high speed
1544 uint32_t cache_target_full_ratio_micro = 0; ///< cache: fraction of target to fill before we evict in earnest
1545
1546 uint32_t cache_min_flush_age = 0; ///< minimum age (seconds) before we can flush
1547 uint32_t cache_min_evict_age = 0; ///< minimum age (seconds) before we can evict
1548
1549 HitSet::Params hit_set_params; ///< The HitSet params to use on this pool
1550 uint32_t hit_set_period = 0; ///< periodicity of HitSet segments (seconds)
1551 uint32_t hit_set_count = 0; ///< number of periods to retain
1552 bool use_gmt_hitset = true; ///< use gmt to name the hitset archive object
1553 uint32_t min_read_recency_for_promote = 0; ///< minimum number of HitSet to check before promote on read
1554 uint32_t min_write_recency_for_promote = 0; ///< minimum number of HitSet to check before promote on write
1555 uint32_t hit_set_grade_decay_rate = 0; ///< current hit_set has highest priority on objects
1556 ///< temperature count,the follow hit_set's priority decay
1557 ///< by this params than pre hit_set
1558 uint32_t hit_set_search_last_n = 0; ///< accumulate atmost N hit_sets for temperature
1559
1560 uint32_t stripe_width = 0; ///< erasure coded stripe size in bytes
1561
1562 uint64_t expected_num_objects = 0; ///< expected number of objects on this pool, a value of 0 indicates
1563 ///< user does not specify any expected value
1564 bool fast_read = false; ///< whether turn on fast read on the pool or not
1565
1566 pool_opts_t opts; ///< options
1567
1568 typedef enum {
1569 TYPE_FINGERPRINT_NONE = 0,
1570 TYPE_FINGERPRINT_SHA1 = 1,
1571 TYPE_FINGERPRINT_SHA256 = 2,
1572 TYPE_FINGERPRINT_SHA512 = 3,
1573 } fingerprint_t;
1574 static fingerprint_t get_fingerprint_from_str(const std::string& s) {
1575 if (s == "none")
1576 return TYPE_FINGERPRINT_NONE;
1577 if (s == "sha1")
1578 return TYPE_FINGERPRINT_SHA1;
1579 if (s == "sha256")
1580 return TYPE_FINGERPRINT_SHA256;
1581 if (s == "sha512")
1582 return TYPE_FINGERPRINT_SHA512;
1583 return (fingerprint_t)-1;
1584 }
1585 const fingerprint_t get_fingerprint_type() const {
1586 std::string fp_str;
1587 opts.get(pool_opts_t::FINGERPRINT_ALGORITHM, &fp_str);
1588 return get_fingerprint_from_str(fp_str);
1589 }
1590 const char *get_fingerprint_name() const {
1591 std::string fp_str;
1592 fingerprint_t fp_t;
1593 opts.get(pool_opts_t::FINGERPRINT_ALGORITHM, &fp_str);
1594 fp_t = get_fingerprint_from_str(fp_str);
1595 return get_fingerprint_name(fp_t);
1596 }
1597 static const char *get_fingerprint_name(fingerprint_t m) {
1598 switch (m) {
1599 case TYPE_FINGERPRINT_NONE: return "none";
1600 case TYPE_FINGERPRINT_SHA1: return "sha1";
1601 case TYPE_FINGERPRINT_SHA256: return "sha256";
1602 case TYPE_FINGERPRINT_SHA512: return "sha512";
1603 default: return "unknown";
1604 }
1605 }
1606
1607 typedef enum {
1608 TYPE_DEDUP_CHUNK_NONE = 0,
1609 TYPE_DEDUP_CHUNK_FASTCDC = 1,
1610 TYPE_DEDUP_CHUNK_FIXEDCDC = 2,
1611 } dedup_chunk_algo_t;
1612 static dedup_chunk_algo_t get_dedup_chunk_algorithm_from_str(const std::string& s) {
1613 if (s == "none")
1614 return TYPE_DEDUP_CHUNK_NONE;
1615 if (s == "fastcdc")
1616 return TYPE_DEDUP_CHUNK_FASTCDC;
1617 if (s == "fixed")
1618 return TYPE_DEDUP_CHUNK_FIXEDCDC;
1619 return (dedup_chunk_algo_t)-1;
1620 }
1621 const dedup_chunk_algo_t get_dedup_chunk_algorithm_type() const {
1622 std::string algo_str;
1623 opts.get(pool_opts_t::DEDUP_CHUNK_ALGORITHM, &algo_str);
1624 return get_dedup_chunk_algorithm_from_str(algo_str);
1625 }
1626 const char *get_dedup_chunk_algorithm_name() const {
1627 std::string dedup_chunk_algo_str;
1628 dedup_chunk_algo_t dedup_chunk_algo_t;
1629 opts.get(pool_opts_t::DEDUP_CHUNK_ALGORITHM, &dedup_chunk_algo_str);
1630 dedup_chunk_algo_t = get_dedup_chunk_algorithm_from_str(dedup_chunk_algo_str);
1631 return get_dedup_chunk_algorithm_name(dedup_chunk_algo_t);
1632 }
1633 static const char *get_dedup_chunk_algorithm_name(dedup_chunk_algo_t m) {
1634 switch (m) {
1635 case TYPE_DEDUP_CHUNK_NONE: return "none";
1636 case TYPE_DEDUP_CHUNK_FASTCDC: return "fastcdc";
1637 case TYPE_DEDUP_CHUNK_FIXEDCDC: return "fixed";
1638 default: return "unknown";
1639 }
1640 }
1641
1642 int64_t get_dedup_tier() const {
1643 int64_t tier_id = 0;
1644 opts.get(pool_opts_t::DEDUP_TIER, &tier_id);
1645 return tier_id;
1646 }
1647 int64_t get_dedup_cdc_chunk_size() const {
1648 int64_t chunk_size = 0;
1649 opts.get(pool_opts_t::DEDUP_CDC_CHUNK_SIZE, &chunk_size);
1650 return chunk_size;
1651 }
1652
1653 /// application -> key/value metadata
1654 std::map<std::string, std::map<std::string, std::string>> application_metadata;
1655
1656 private:
1657 std::vector<uint32_t> grade_table;
1658
1659 public:
1660 uint32_t get_grade(unsigned i) const {
1661 if (grade_table.size() <= i)
1662 return 0;
1663 return grade_table[i];
1664 }
1665 void calc_grade_table() {
1666 unsigned v = 1000000;
1667 grade_table.resize(hit_set_count);
1668 for (unsigned i = 0; i < hit_set_count; i++) {
1669 v = v * (1 - (hit_set_grade_decay_rate / 100.0));
1670 grade_table[i] = v;
1671 }
1672 }
1673
1674 pg_pool_t() = default;
1675
1676 void dump(ceph::Formatter *f) const;
1677
1678 const utime_t &get_create_time() const { return create_time; }
1679 uint64_t get_flags() const { return flags; }
1680 bool has_flag(uint64_t f) const { return flags & f; }
1681 void set_flag(uint64_t f) { flags |= f; }
1682 void unset_flag(uint64_t f) { flags &= ~f; }
1683
1684 bool require_rollback() const {
1685 return is_erasure();
1686 }
1687
1688 /// true if incomplete clones may be present
1689 bool allow_incomplete_clones() const {
1690 return cache_mode != CACHEMODE_NONE || has_flag(FLAG_INCOMPLETE_CLONES);
1691 }
1692
1693 unsigned get_type() const { return type; }
1694 unsigned get_size() const { return size; }
1695 unsigned get_min_size() const { return min_size; }
1696 int get_crush_rule() const { return crush_rule; }
1697 int get_object_hash() const { return object_hash; }
1698 const char *get_object_hash_name() const {
1699 return ceph_str_hash_name(get_object_hash());
1700 }
1701 epoch_t get_last_change() const { return last_change; }
1702 epoch_t get_last_force_op_resend() const { return last_force_op_resend; }
1703 epoch_t get_last_force_op_resend_prenautilus() const {
1704 return last_force_op_resend_prenautilus;
1705 }
1706 epoch_t get_last_force_op_resend_preluminous() const {
1707 return last_force_op_resend_preluminous;
1708 }
1709 epoch_t get_snap_epoch() const { return snap_epoch; }
1710 snapid_t get_snap_seq() const { return snap_seq; }
1711 uint64_t get_auid() const { return auid; }
1712
1713 void set_snap_seq(snapid_t s) { snap_seq = s; }
1714 void set_snap_epoch(epoch_t e) { snap_epoch = e; }
1715
1716 void set_stripe_width(uint32_t s) { stripe_width = s; }
1717 uint32_t get_stripe_width() const { return stripe_width; }
1718
1719 bool is_replicated() const { return get_type() == TYPE_REPLICATED; }
1720 bool is_erasure() const { return get_type() == TYPE_ERASURE; }
1721
1722 bool supports_omap() const {
1723 return !(get_type() == TYPE_ERASURE);
1724 }
1725
1726 bool requires_aligned_append() const {
1727 return is_erasure() && !has_flag(FLAG_EC_OVERWRITES);
1728 }
1729 uint64_t required_alignment() const { return stripe_width; }
1730
1731 bool allows_ecoverwrites() const {
1732 return has_flag(FLAG_EC_OVERWRITES);
1733 }
1734
1735 bool can_shift_osds() const {
1736 switch (get_type()) {
1737 case TYPE_REPLICATED:
1738 return true;
1739 case TYPE_ERASURE:
1740 return false;
1741 default:
1742 ceph_abort_msg("unhandled pool type");
1743 }
1744 }
1745
1746 unsigned get_pg_num() const { return pg_num; }
1747 unsigned get_pgp_num() const { return pgp_num; }
1748 unsigned get_pg_num_target() const { return pg_num_target; }
1749 unsigned get_pgp_num_target() const { return pgp_num_target; }
1750 unsigned get_pg_num_pending() const { return pg_num_pending; }
1751
1752 unsigned get_pg_num_mask() const { return pg_num_mask; }
1753 unsigned get_pgp_num_mask() const { return pgp_num_mask; }
1754
1755 // if pg_num is not a multiple of two, pgs are not equally sized.
1756 // return, for a given pg, the fraction (denominator) of the total
1757 // pool size that it represents.
1758 unsigned get_pg_num_divisor(pg_t pgid) const;
1759
1760 bool is_pending_merge(pg_t pgid, bool *target) const;
1761
1762 void set_pg_num(int p) {
1763 pg_num = p;
1764 pg_num_pending = p;
1765 calc_pg_masks();
1766 }
1767 void set_pgp_num(int p) {
1768 pgp_num = p;
1769 calc_pg_masks();
1770 }
1771 void set_pg_num_pending(int p) {
1772 pg_num_pending = p;
1773 calc_pg_masks();
1774 }
1775 void set_pg_num_target(int p) {
1776 pg_num_target = p;
1777 }
1778 void set_pgp_num_target(int p) {
1779 pgp_num_target = p;
1780 }
1781 void dec_pg_num(pg_t source_pgid,
1782 epoch_t ready_epoch,
1783 eversion_t source_version,
1784 eversion_t target_version,
1785 epoch_t last_epoch_started,
1786 epoch_t last_epoch_clean) {
1787 --pg_num;
1788 last_pg_merge_meta.source_pgid = source_pgid;
1789 last_pg_merge_meta.ready_epoch = ready_epoch;
1790 last_pg_merge_meta.source_version = source_version;
1791 last_pg_merge_meta.target_version = target_version;
1792 last_pg_merge_meta.last_epoch_started = last_epoch_started;
1793 last_pg_merge_meta.last_epoch_clean = last_epoch_clean;
1794 calc_pg_masks();
1795 }
1796
1797 void set_quota_max_bytes(uint64_t m) {
1798 quota_max_bytes = m;
1799 }
1800 uint64_t get_quota_max_bytes() {
1801 return quota_max_bytes;
1802 }
1803
1804 void set_quota_max_objects(uint64_t m) {
1805 quota_max_objects = m;
1806 }
1807 uint64_t get_quota_max_objects() {
1808 return quota_max_objects;
1809 }
1810
1811 void set_last_force_op_resend(uint64_t t) {
1812 last_force_op_resend = t;
1813 last_force_op_resend_prenautilus = t;
1814 last_force_op_resend_preluminous = t;
1815 }
1816
1817 void calc_pg_masks();
1818
1819 /*
1820 * we have two snap modes:
1821 * - pool global snaps
1822 * - snap existence/non-existence defined by snaps[] and snap_seq
1823 * - user managed snaps
1824 * - removal governed by removed_snaps
1825 *
1826 * we know which mode we're using based on whether removed_snaps is empty.
1827 * If nothing has been created, both functions report false.
1828 */
1829 bool is_pool_snaps_mode() const;
1830 bool is_unmanaged_snaps_mode() const;
1831 bool is_removed_snap(snapid_t s) const;
1832
1833 snapid_t snap_exists(std::string_view s) const;
1834 void add_snap(const char *n, utime_t stamp);
1835 uint64_t add_unmanaged_snap(bool preoctopus_compat);
1836 void remove_snap(snapid_t s);
1837 void remove_unmanaged_snap(snapid_t s, bool preoctopus_compat);
1838
1839 SnapContext get_snap_context() const;
1840
1841 /// hash a object name+namespace key to a hash position
1842 uint32_t hash_key(const std::string& key, const std::string& ns) const;
1843
1844 /// round a hash position down to a pg num
1845 uint32_t raw_hash_to_pg(uint32_t v) const;
1846
1847 /*
1848 * map a raw pg (with full precision ps) into an actual pg, for storage
1849 */
1850 pg_t raw_pg_to_pg(pg_t pg) const;
1851
1852 /*
1853 * map raw pg (full precision ps) into a placement seed. include
1854 * pool id in that value so that different pools don't use the same
1855 * seeds.
1856 */
1857 ps_t raw_pg_to_pps(pg_t pg) const;
1858
1859 /// choose a random hash position within a pg
1860 uint32_t get_random_pg_position(pg_t pgid, uint32_t seed) const;
1861
1862 void encode(ceph::buffer::list& bl, uint64_t features) const;
1863 void decode(ceph::buffer::list::const_iterator& bl);
1864
1865 static void generate_test_instances(std::list<pg_pool_t*>& o);
1866 };
1867 WRITE_CLASS_ENCODER_FEATURES(pg_pool_t)
1868
1869 std::ostream& operator<<(std::ostream& out, const pg_pool_t& p);
1870
1871
1872 /**
1873 * a summation of object stats
1874 *
1875 * This is just a container for object stats; we don't know what for.
1876 *
1877 * If you add members in object_stat_sum_t, you should make sure there are
1878 * not padding among these members.
1879 * You should also modify the padding_check function.
1880
1881 */
1882 struct object_stat_sum_t {
1883 /**************************************************************************
1884 * WARNING: be sure to update operator==, floor, and split when
1885 * adding/removing fields!
1886 **************************************************************************/
1887 int64_t num_bytes; // in bytes
1888 int64_t num_objects;
1889 int64_t num_object_clones;
1890 int64_t num_object_copies; // num_objects * num_replicas
1891 int64_t num_objects_missing_on_primary;
1892 int64_t num_objects_degraded;
1893 int64_t num_objects_unfound;
1894 int64_t num_rd;
1895 int64_t num_rd_kb;
1896 int64_t num_wr;
1897 int64_t num_wr_kb;
1898 int64_t num_scrub_errors; // total deep and shallow scrub errors
1899 int64_t num_objects_recovered;
1900 int64_t num_bytes_recovered;
1901 int64_t num_keys_recovered;
1902 int64_t num_shallow_scrub_errors;
1903 int64_t num_deep_scrub_errors;
1904 int64_t num_objects_dirty;
1905 int64_t num_whiteouts;
1906 int64_t num_objects_omap;
1907 int64_t num_objects_hit_set_archive;
1908 int64_t num_objects_misplaced;
1909 int64_t num_bytes_hit_set_archive;
1910 int64_t num_flush;
1911 int64_t num_flush_kb;
1912 int64_t num_evict;
1913 int64_t num_evict_kb;
1914 int64_t num_promote;
1915 int32_t num_flush_mode_high; // 1 when in high flush mode, otherwise 0
1916 int32_t num_flush_mode_low; // 1 when in low flush mode, otherwise 0
1917 int32_t num_evict_mode_some; // 1 when in evict some mode, otherwise 0
1918 int32_t num_evict_mode_full; // 1 when in evict full mode, otherwise 0
1919 int64_t num_objects_pinned;
1920 int64_t num_objects_missing;
1921 int64_t num_legacy_snapsets; ///< upper bound on pre-luminous-style SnapSets
1922 int64_t num_large_omap_objects = 0;
1923 int64_t num_objects_manifest = 0;
1924 int64_t num_omap_bytes = 0;
1925 int64_t num_omap_keys = 0;
1926 int64_t num_objects_repaired = 0;
1927
1928 object_stat_sum_t()
1929 : num_bytes(0),
1930 num_objects(0), num_object_clones(0), num_object_copies(0),
1931 num_objects_missing_on_primary(0), num_objects_degraded(0),
1932 num_objects_unfound(0),
1933 num_rd(0), num_rd_kb(0), num_wr(0), num_wr_kb(0),
1934 num_scrub_errors(0),
1935 num_objects_recovered(0),
1936 num_bytes_recovered(0),
1937 num_keys_recovered(0),
1938 num_shallow_scrub_errors(0),
1939 num_deep_scrub_errors(0),
1940 num_objects_dirty(0),
1941 num_whiteouts(0),
1942 num_objects_omap(0),
1943 num_objects_hit_set_archive(0),
1944 num_objects_misplaced(0),
1945 num_bytes_hit_set_archive(0),
1946 num_flush(0),
1947 num_flush_kb(0),
1948 num_evict(0),
1949 num_evict_kb(0),
1950 num_promote(0),
1951 num_flush_mode_high(0), num_flush_mode_low(0),
1952 num_evict_mode_some(0), num_evict_mode_full(0),
1953 num_objects_pinned(0),
1954 num_objects_missing(0),
1955 num_legacy_snapsets(0)
1956 {}
1957
1958 void floor(int64_t f) {
1959 #define FLOOR(x) if (x < f) x = f
1960 FLOOR(num_bytes);
1961 FLOOR(num_objects);
1962 FLOOR(num_object_clones);
1963 FLOOR(num_object_copies);
1964 FLOOR(num_objects_missing_on_primary);
1965 FLOOR(num_objects_missing);
1966 FLOOR(num_objects_degraded);
1967 FLOOR(num_objects_misplaced);
1968 FLOOR(num_objects_unfound);
1969 FLOOR(num_rd);
1970 FLOOR(num_rd_kb);
1971 FLOOR(num_wr);
1972 FLOOR(num_wr_kb);
1973 FLOOR(num_large_omap_objects);
1974 FLOOR(num_objects_manifest);
1975 FLOOR(num_omap_bytes);
1976 FLOOR(num_omap_keys);
1977 FLOOR(num_shallow_scrub_errors);
1978 FLOOR(num_deep_scrub_errors);
1979 num_scrub_errors = num_shallow_scrub_errors + num_deep_scrub_errors;
1980 FLOOR(num_objects_recovered);
1981 FLOOR(num_bytes_recovered);
1982 FLOOR(num_keys_recovered);
1983 FLOOR(num_objects_dirty);
1984 FLOOR(num_whiteouts);
1985 FLOOR(num_objects_omap);
1986 FLOOR(num_objects_hit_set_archive);
1987 FLOOR(num_bytes_hit_set_archive);
1988 FLOOR(num_flush);
1989 FLOOR(num_flush_kb);
1990 FLOOR(num_evict);
1991 FLOOR(num_evict_kb);
1992 FLOOR(num_promote);
1993 FLOOR(num_flush_mode_high);
1994 FLOOR(num_flush_mode_low);
1995 FLOOR(num_evict_mode_some);
1996 FLOOR(num_evict_mode_full);
1997 FLOOR(num_objects_pinned);
1998 FLOOR(num_legacy_snapsets);
1999 FLOOR(num_objects_repaired);
2000 #undef FLOOR
2001 }
2002
2003 void split(std::vector<object_stat_sum_t> &out) const {
2004 #define SPLIT(PARAM) \
2005 for (unsigned i = 0; i < out.size(); ++i) { \
2006 out[i].PARAM = PARAM / out.size(); \
2007 if (i < (PARAM % out.size())) { \
2008 out[i].PARAM++; \
2009 } \
2010 }
2011 #define SPLIT_PRESERVE_NONZERO(PARAM) \
2012 for (unsigned i = 0; i < out.size(); ++i) { \
2013 if (PARAM) \
2014 out[i].PARAM = 1 + PARAM / out.size(); \
2015 else \
2016 out[i].PARAM = 0; \
2017 }
2018
2019 SPLIT(num_bytes);
2020 SPLIT(num_objects);
2021 SPLIT(num_object_clones);
2022 SPLIT(num_object_copies);
2023 SPLIT(num_objects_missing_on_primary);
2024 SPLIT(num_objects_missing);
2025 SPLIT(num_objects_degraded);
2026 SPLIT(num_objects_misplaced);
2027 SPLIT(num_objects_unfound);
2028 SPLIT(num_rd);
2029 SPLIT(num_rd_kb);
2030 SPLIT(num_wr);
2031 SPLIT(num_wr_kb);
2032 SPLIT(num_large_omap_objects);
2033 SPLIT(num_objects_manifest);
2034 SPLIT(num_omap_bytes);
2035 SPLIT(num_omap_keys);
2036 SPLIT(num_objects_repaired);
2037 SPLIT_PRESERVE_NONZERO(num_shallow_scrub_errors);
2038 SPLIT_PRESERVE_NONZERO(num_deep_scrub_errors);
2039 for (unsigned i = 0; i < out.size(); ++i) {
2040 out[i].num_scrub_errors = out[i].num_shallow_scrub_errors +
2041 out[i].num_deep_scrub_errors;
2042 }
2043 SPLIT(num_objects_recovered);
2044 SPLIT(num_bytes_recovered);
2045 SPLIT(num_keys_recovered);
2046 SPLIT(num_objects_dirty);
2047 SPLIT(num_whiteouts);
2048 SPLIT(num_objects_omap);
2049 SPLIT(num_objects_hit_set_archive);
2050 SPLIT(num_bytes_hit_set_archive);
2051 SPLIT(num_flush);
2052 SPLIT(num_flush_kb);
2053 SPLIT(num_evict);
2054 SPLIT(num_evict_kb);
2055 SPLIT(num_promote);
2056 SPLIT(num_flush_mode_high);
2057 SPLIT(num_flush_mode_low);
2058 SPLIT(num_evict_mode_some);
2059 SPLIT(num_evict_mode_full);
2060 SPLIT(num_objects_pinned);
2061 SPLIT_PRESERVE_NONZERO(num_legacy_snapsets);
2062 #undef SPLIT
2063 #undef SPLIT_PRESERVE_NONZERO
2064 }
2065
2066 void clear() {
2067 // FIPS zeroization audit 20191117: this memset is not security related.
2068 memset(this, 0, sizeof(*this));
2069 }
2070
2071 void calc_copies(int nrep) {
2072 num_object_copies = nrep * num_objects;
2073 }
2074
2075 bool is_zero() const {
2076 return mem_is_zero((char*)this, sizeof(*this));
2077 }
2078
2079 void add(const object_stat_sum_t& o);
2080 void sub(const object_stat_sum_t& o);
2081
2082 void dump(ceph::Formatter *f) const;
2083 void padding_check() {
2084 static_assert(
2085 sizeof(object_stat_sum_t) ==
2086 sizeof(num_bytes) +
2087 sizeof(num_objects) +
2088 sizeof(num_object_clones) +
2089 sizeof(num_object_copies) +
2090 sizeof(num_objects_missing_on_primary) +
2091 sizeof(num_objects_degraded) +
2092 sizeof(num_objects_unfound) +
2093 sizeof(num_rd) +
2094 sizeof(num_rd_kb) +
2095 sizeof(num_wr) +
2096 sizeof(num_wr_kb) +
2097 sizeof(num_scrub_errors) +
2098 sizeof(num_large_omap_objects) +
2099 sizeof(num_objects_manifest) +
2100 sizeof(num_omap_bytes) +
2101 sizeof(num_omap_keys) +
2102 sizeof(num_objects_repaired) +
2103 sizeof(num_objects_recovered) +
2104 sizeof(num_bytes_recovered) +
2105 sizeof(num_keys_recovered) +
2106 sizeof(num_shallow_scrub_errors) +
2107 sizeof(num_deep_scrub_errors) +
2108 sizeof(num_objects_dirty) +
2109 sizeof(num_whiteouts) +
2110 sizeof(num_objects_omap) +
2111 sizeof(num_objects_hit_set_archive) +
2112 sizeof(num_objects_misplaced) +
2113 sizeof(num_bytes_hit_set_archive) +
2114 sizeof(num_flush) +
2115 sizeof(num_flush_kb) +
2116 sizeof(num_evict) +
2117 sizeof(num_evict_kb) +
2118 sizeof(num_promote) +
2119 sizeof(num_flush_mode_high) +
2120 sizeof(num_flush_mode_low) +
2121 sizeof(num_evict_mode_some) +
2122 sizeof(num_evict_mode_full) +
2123 sizeof(num_objects_pinned) +
2124 sizeof(num_objects_missing) +
2125 sizeof(num_legacy_snapsets)
2126 ,
2127 "object_stat_sum_t have padding");
2128 }
2129 void encode(ceph::buffer::list& bl) const;
2130 void decode(ceph::buffer::list::const_iterator& bl);
2131 static void generate_test_instances(std::list<object_stat_sum_t*>& o);
2132 };
2133 WRITE_CLASS_ENCODER(object_stat_sum_t)
2134
2135 bool operator==(const object_stat_sum_t& l, const object_stat_sum_t& r);
2136
2137 /**
2138 * a collection of object stat sums
2139 *
2140 * This is a collection of stat sums over different categories.
2141 */
2142 struct object_stat_collection_t {
2143 /**************************************************************************
2144 * WARNING: be sure to update the operator== when adding/removing fields! *
2145 **************************************************************************/
2146 object_stat_sum_t sum;
2147
2148 void calc_copies(int nrep) {
2149 sum.calc_copies(nrep);
2150 }
2151
2152 void dump(ceph::Formatter *f) const;
2153 void encode(ceph::buffer::list& bl) const;
2154 void decode(ceph::buffer::list::const_iterator& bl);
2155 static void generate_test_instances(std::list<object_stat_collection_t*>& o);
2156
2157 bool is_zero() const {
2158 return sum.is_zero();
2159 }
2160
2161 void clear() {
2162 sum.clear();
2163 }
2164
2165 void floor(int64_t f) {
2166 sum.floor(f);
2167 }
2168
2169 void add(const object_stat_sum_t& o) {
2170 sum.add(o);
2171 }
2172
2173 void add(const object_stat_collection_t& o) {
2174 sum.add(o.sum);
2175 }
2176 void sub(const object_stat_collection_t& o) {
2177 sum.sub(o.sum);
2178 }
2179 };
2180 WRITE_CLASS_ENCODER(object_stat_collection_t)
2181
2182 inline bool operator==(const object_stat_collection_t& l,
2183 const object_stat_collection_t& r) {
2184 return l.sum == r.sum;
2185 }
2186
2187 enum class scrub_level_t : bool { shallow = false, deep = true };
2188 enum class scrub_type_t : bool { not_repair = false, do_repair = true };
2189
2190 /// is there a scrub in our future?
2191 enum class pg_scrub_sched_status_t : uint16_t {
2192 unknown, ///< status not reported yet
2193 not_queued, ///< not in the OSD's scrub queue. Probably not active.
2194 active, ///< scrubbing
2195 scheduled, ///< scheduled for a scrub at an already determined time
2196 queued ///< queued to be scrubbed
2197 };
2198
2199 struct pg_scrubbing_status_t {
2200 utime_t m_scheduled_at{};
2201 int32_t m_duration_seconds{0}; // relevant when scrubbing
2202 pg_scrub_sched_status_t m_sched_status{pg_scrub_sched_status_t::unknown};
2203 bool m_is_active{false};
2204 scrub_level_t m_is_deep{scrub_level_t::shallow};
2205 bool m_is_periodic{true};
2206 };
2207
2208 bool operator==(const pg_scrubbing_status_t& l, const pg_scrubbing_status_t& r);
2209
2210 /** pg_stat
2211 * aggregate stats for a single PG.
2212 */
2213 struct pg_stat_t {
2214 /**************************************************************************
2215 * WARNING: be sure to update the operator== when adding/removing fields! *
2216 **************************************************************************/
2217 eversion_t version;
2218 version_t reported_seq; // sequence number
2219 epoch_t reported_epoch; // epoch of this report
2220 uint64_t state;
2221 utime_t last_fresh; // last reported
2222 utime_t last_change; // new state != previous state
2223 utime_t last_active; // state & PG_STATE_ACTIVE
2224 utime_t last_peered; // state & PG_STATE_ACTIVE || state & PG_STATE_PEERED
2225 utime_t last_clean; // state & PG_STATE_CLEAN
2226 utime_t last_unstale; // (state & PG_STATE_STALE) == 0
2227 utime_t last_undegraded; // (state & PG_STATE_DEGRADED) == 0
2228 utime_t last_fullsized; // (state & PG_STATE_UNDERSIZED) == 0
2229
2230 eversion_t log_start; // (log_start,version]
2231 eversion_t ondisk_log_start; // there may be more on disk
2232
2233 epoch_t created;
2234 epoch_t last_epoch_clean;
2235 pg_t parent;
2236 __u32 parent_split_bits;
2237
2238 eversion_t last_scrub;
2239 eversion_t last_deep_scrub;
2240 utime_t last_scrub_stamp;
2241 utime_t last_deep_scrub_stamp;
2242 utime_t last_clean_scrub_stamp;
2243 int32_t last_scrub_duration{0};
2244
2245 object_stat_collection_t stats;
2246
2247 int64_t log_size;
2248 int64_t ondisk_log_size; // >= active_log_size
2249 int64_t objects_scrubbed;
2250
2251 std::vector<int32_t> up, acting;
2252 std::vector<pg_shard_t> avail_no_missing;
2253 std::map< std::set<pg_shard_t>, int32_t > object_location_counts;
2254 epoch_t mapping_epoch;
2255
2256 std::vector<int32_t> blocked_by; ///< osds on which the pg is blocked
2257
2258 interval_set<snapid_t> purged_snaps; ///< recently removed snaps that we've purged
2259
2260 utime_t last_became_active;
2261 utime_t last_became_peered;
2262
2263 /// up, acting primaries
2264 int32_t up_primary;
2265 int32_t acting_primary;
2266
2267 // snaptrimq.size() is 64bit, but let's be serious - anything over 50k is
2268 // absurd already, so cap it to 2^32 and save 4 bytes at the same time
2269 uint32_t snaptrimq_len;
2270
2271 pg_scrubbing_status_t scrub_sched_status;
2272
2273 bool stats_invalid:1;
2274 /// true if num_objects_dirty is not accurate (because it was not
2275 /// maintained starting from pool creation)
2276 bool dirty_stats_invalid:1;
2277 bool omap_stats_invalid:1;
2278 bool hitset_stats_invalid:1;
2279 bool hitset_bytes_stats_invalid:1;
2280 bool pin_stats_invalid:1;
2281 bool manifest_stats_invalid:1;
2282
2283 pg_stat_t()
2284 : reported_seq(0),
2285 reported_epoch(0),
2286 state(0),
2287 created(0), last_epoch_clean(0),
2288 parent_split_bits(0),
2289 log_size(0), ondisk_log_size(0),
2290 objects_scrubbed(0),
2291 mapping_epoch(0),
2292 up_primary(-1),
2293 acting_primary(-1),
2294 snaptrimq_len(0),
2295 stats_invalid(false),
2296 dirty_stats_invalid(false),
2297 omap_stats_invalid(false),
2298 hitset_stats_invalid(false),
2299 hitset_bytes_stats_invalid(false),
2300 pin_stats_invalid(false),
2301 manifest_stats_invalid(false)
2302 { }
2303
2304 epoch_t get_effective_last_epoch_clean() const {
2305 if (state & PG_STATE_CLEAN) {
2306 // we are clean as of this report, and should thus take the
2307 // reported epoch
2308 return reported_epoch;
2309 } else {
2310 return last_epoch_clean;
2311 }
2312 }
2313
2314 std::pair<epoch_t, version_t> get_version_pair() const {
2315 return { reported_epoch, reported_seq };
2316 }
2317
2318 void floor(int64_t f) {
2319 stats.floor(f);
2320 if (log_size < f)
2321 log_size = f;
2322 if (ondisk_log_size < f)
2323 ondisk_log_size = f;
2324 if (snaptrimq_len < f)
2325 snaptrimq_len = f;
2326 }
2327
2328 void add_sub_invalid_flags(const pg_stat_t& o) {
2329 // adding (or subtracting!) invalid stats render our stats invalid too
2330 stats_invalid |= o.stats_invalid;
2331 dirty_stats_invalid |= o.dirty_stats_invalid;
2332 omap_stats_invalid |= o.omap_stats_invalid;
2333 hitset_stats_invalid |= o.hitset_stats_invalid;
2334 hitset_bytes_stats_invalid |= o.hitset_bytes_stats_invalid;
2335 pin_stats_invalid |= o.pin_stats_invalid;
2336 manifest_stats_invalid |= o.manifest_stats_invalid;
2337 }
2338 void add(const pg_stat_t& o) {
2339 stats.add(o.stats);
2340 log_size += o.log_size;
2341 ondisk_log_size += o.ondisk_log_size;
2342 snaptrimq_len = std::min((uint64_t)snaptrimq_len + o.snaptrimq_len,
2343 (uint64_t)(1ull << 31));
2344 add_sub_invalid_flags(o);
2345 }
2346 void sub(const pg_stat_t& o) {
2347 stats.sub(o.stats);
2348 log_size -= o.log_size;
2349 ondisk_log_size -= o.ondisk_log_size;
2350 if (o.snaptrimq_len < snaptrimq_len) {
2351 snaptrimq_len -= o.snaptrimq_len;
2352 } else {
2353 snaptrimq_len = 0;
2354 }
2355 add_sub_invalid_flags(o);
2356 }
2357
2358 bool is_acting_osd(int32_t osd, bool primary) const;
2359 void dump(ceph::Formatter *f) const;
2360 void dump_brief(ceph::Formatter *f) const;
2361 std::string dump_scrub_schedule() const;
2362 void encode(ceph::buffer::list &bl) const;
2363 void decode(ceph::buffer::list::const_iterator &bl);
2364 static void generate_test_instances(std::list<pg_stat_t*>& o);
2365 };
2366 WRITE_CLASS_ENCODER(pg_stat_t)
2367
2368 bool operator==(const pg_stat_t& l, const pg_stat_t& r);
2369
2370 /** store_statfs_t
2371 * ObjectStore full statfs information
2372 */
2373 struct store_statfs_t
2374 {
2375 uint64_t total = 0; ///< Total bytes
2376 uint64_t available = 0; ///< Free bytes available
2377 uint64_t internally_reserved = 0; ///< Bytes reserved for internal purposes
2378
2379 int64_t allocated = 0; ///< Bytes allocated by the store
2380
2381 int64_t data_stored = 0; ///< Bytes actually stored by the user
2382 int64_t data_compressed = 0; ///< Bytes stored after compression
2383 int64_t data_compressed_allocated = 0; ///< Bytes allocated for compressed data
2384 int64_t data_compressed_original = 0; ///< Bytes that were compressed
2385
2386 int64_t omap_allocated = 0; ///< approx usage of omap data
2387 int64_t internal_metadata = 0; ///< approx usage of internal metadata
2388
2389 void reset() {
2390 *this = store_statfs_t();
2391 }
2392 void floor(int64_t f) {
2393 #define FLOOR(x) if (int64_t(x) < f) x = f
2394 FLOOR(total);
2395 FLOOR(available);
2396 FLOOR(internally_reserved);
2397 FLOOR(allocated);
2398 FLOOR(data_stored);
2399 FLOOR(data_compressed);
2400 FLOOR(data_compressed_allocated);
2401 FLOOR(data_compressed_original);
2402
2403 FLOOR(omap_allocated);
2404 FLOOR(internal_metadata);
2405 #undef FLOOR
2406 }
2407
2408 bool operator ==(const store_statfs_t& other) const;
2409 bool is_zero() const {
2410 return *this == store_statfs_t();
2411 }
2412
2413 uint64_t get_used() const {
2414 return total - available - internally_reserved;
2415 }
2416
2417 // this accumulates both actually used and statfs's internally_reserved
2418 uint64_t get_used_raw() const {
2419 return total - available;
2420 }
2421
2422 float get_used_raw_ratio() const {
2423 if (total) {
2424 return (float)get_used_raw() / (float)total;
2425 } else {
2426 return 0.0;
2427 }
2428 }
2429
2430 // helpers to ease legacy code porting
2431 uint64_t kb_avail() const {
2432 return available >> 10;
2433 }
2434 uint64_t kb() const {
2435 return total >> 10;
2436 }
2437 uint64_t kb_used() const {
2438 return (total - available - internally_reserved) >> 10;
2439 }
2440 uint64_t kb_used_raw() const {
2441 return get_used_raw() >> 10;
2442 }
2443
2444 uint64_t kb_used_data() const {
2445 return allocated >> 10;
2446 }
2447 uint64_t kb_used_omap() const {
2448 return omap_allocated >> 10;
2449 }
2450
2451 uint64_t kb_used_internal_metadata() const {
2452 return internal_metadata >> 10;
2453 }
2454
2455 void add(const store_statfs_t& o) {
2456 total += o.total;
2457 available += o.available;
2458 internally_reserved += o.internally_reserved;
2459 allocated += o.allocated;
2460 data_stored += o.data_stored;
2461 data_compressed += o.data_compressed;
2462 data_compressed_allocated += o.data_compressed_allocated;
2463 data_compressed_original += o.data_compressed_original;
2464 omap_allocated += o.omap_allocated;
2465 internal_metadata += o.internal_metadata;
2466 }
2467 void sub(const store_statfs_t& o) {
2468 total -= o.total;
2469 available -= o.available;
2470 internally_reserved -= o.internally_reserved;
2471 allocated -= o.allocated;
2472 data_stored -= o.data_stored;
2473 data_compressed -= o.data_compressed;
2474 data_compressed_allocated -= o.data_compressed_allocated;
2475 data_compressed_original -= o.data_compressed_original;
2476 omap_allocated -= o.omap_allocated;
2477 internal_metadata -= o.internal_metadata;
2478 }
2479 void dump(ceph::Formatter *f) const;
2480 DENC(store_statfs_t, v, p) {
2481 DENC_START(1, 1, p);
2482 denc(v.total, p);
2483 denc(v.available, p);
2484 denc(v.internally_reserved, p);
2485 denc(v.allocated, p);
2486 denc(v.data_stored, p);
2487 denc(v.data_compressed, p);
2488 denc(v.data_compressed_allocated, p);
2489 denc(v.data_compressed_original, p);
2490 denc(v.omap_allocated, p);
2491 denc(v.internal_metadata, p);
2492 DENC_FINISH(p);
2493 }
2494 static void generate_test_instances(std::list<store_statfs_t*>& o);
2495 };
2496 WRITE_CLASS_DENC(store_statfs_t)
2497
2498 std::ostream &operator<<(std::ostream &lhs, const store_statfs_t &rhs);
2499
2500 /** osd_stat
2501 * aggregate stats for an osd
2502 */
2503 struct osd_stat_t {
2504 store_statfs_t statfs;
2505 std::vector<int> hb_peers;
2506 int32_t snap_trim_queue_len, num_snap_trimming;
2507 uint64_t num_shards_repaired;
2508
2509 pow2_hist_t op_queue_age_hist;
2510
2511 objectstore_perf_stat_t os_perf_stat;
2512 osd_alerts_t os_alerts;
2513
2514 epoch_t up_from = 0;
2515 uint64_t seq = 0;
2516
2517 uint32_t num_pgs = 0;
2518
2519 uint32_t num_osds = 0;
2520 uint32_t num_per_pool_osds = 0;
2521 uint32_t num_per_pool_omap_osds = 0;
2522
2523 struct Interfaces {
2524 uint32_t last_update; // in seconds
2525 uint32_t back_pingtime[3];
2526 uint32_t back_min[3];
2527 uint32_t back_max[3];
2528 uint32_t back_last;
2529 uint32_t front_pingtime[3];
2530 uint32_t front_min[3];
2531 uint32_t front_max[3];
2532 uint32_t front_last;
2533 };
2534 std::map<int, Interfaces> hb_pingtime; ///< map of osd id to Interfaces
2535
2536 osd_stat_t() : snap_trim_queue_len(0), num_snap_trimming(0),
2537 num_shards_repaired(0) {}
2538
2539 void add(const osd_stat_t& o) {
2540 statfs.add(o.statfs);
2541 snap_trim_queue_len += o.snap_trim_queue_len;
2542 num_snap_trimming += o.num_snap_trimming;
2543 num_shards_repaired += o.num_shards_repaired;
2544 op_queue_age_hist.add(o.op_queue_age_hist);
2545 os_perf_stat.add(o.os_perf_stat);
2546 num_pgs += o.num_pgs;
2547 num_osds += o.num_osds;
2548 num_per_pool_osds += o.num_per_pool_osds;
2549 num_per_pool_omap_osds += o.num_per_pool_omap_osds;
2550 for (const auto& a : o.os_alerts) {
2551 auto& target = os_alerts[a.first];
2552 for (auto& i : a.second) {
2553 target.emplace(i.first, i.second);
2554 }
2555 }
2556 }
2557 void sub(const osd_stat_t& o) {
2558 statfs.sub(o.statfs);
2559 snap_trim_queue_len -= o.snap_trim_queue_len;
2560 num_snap_trimming -= o.num_snap_trimming;
2561 num_shards_repaired -= o.num_shards_repaired;
2562 op_queue_age_hist.sub(o.op_queue_age_hist);
2563 os_perf_stat.sub(o.os_perf_stat);
2564 num_pgs -= o.num_pgs;
2565 num_osds -= o.num_osds;
2566 num_per_pool_osds -= o.num_per_pool_osds;
2567 num_per_pool_omap_osds -= o.num_per_pool_omap_osds;
2568 for (const auto& a : o.os_alerts) {
2569 auto& target = os_alerts[a.first];
2570 for (auto& i : a.second) {
2571 target.erase(i.first);
2572 }
2573 if (target.empty()) {
2574 os_alerts.erase(a.first);
2575 }
2576 }
2577 }
2578 void dump(ceph::Formatter *f, bool with_net = true) const;
2579 void dump_ping_time(ceph::Formatter *f) const;
2580 void encode(ceph::buffer::list &bl, uint64_t features) const;
2581 void decode(ceph::buffer::list::const_iterator &bl);
2582 static void generate_test_instances(std::list<osd_stat_t*>& o);
2583 };
2584 WRITE_CLASS_ENCODER_FEATURES(osd_stat_t)
2585
2586 inline bool operator==(const osd_stat_t& l, const osd_stat_t& r) {
2587 return l.statfs == r.statfs &&
2588 l.snap_trim_queue_len == r.snap_trim_queue_len &&
2589 l.num_snap_trimming == r.num_snap_trimming &&
2590 l.num_shards_repaired == r.num_shards_repaired &&
2591 l.hb_peers == r.hb_peers &&
2592 l.op_queue_age_hist == r.op_queue_age_hist &&
2593 l.os_perf_stat == r.os_perf_stat &&
2594 l.num_pgs == r.num_pgs &&
2595 l.num_osds == r.num_osds &&
2596 l.num_per_pool_osds == r.num_per_pool_osds &&
2597 l.num_per_pool_omap_osds == r.num_per_pool_omap_osds;
2598 }
2599 inline bool operator!=(const osd_stat_t& l, const osd_stat_t& r) {
2600 return !(l == r);
2601 }
2602
2603 inline std::ostream& operator<<(std::ostream& out, const osd_stat_t& s) {
2604 return out << "osd_stat(" << s.statfs << ", "
2605 << "peers " << s.hb_peers
2606 << " op hist " << s.op_queue_age_hist.h
2607 << ")";
2608 }
2609
2610 /*
2611 * summation over an entire pool
2612 */
2613 struct pool_stat_t {
2614 object_stat_collection_t stats;
2615 store_statfs_t store_stats;
2616 int64_t log_size;
2617 int64_t ondisk_log_size; // >= active_log_size
2618 int32_t up; ///< number of up replicas or shards
2619 int32_t acting; ///< number of acting replicas or shards
2620 int32_t num_store_stats; ///< amount of store_stats accumulated
2621
2622 pool_stat_t() : log_size(0), ondisk_log_size(0), up(0), acting(0),
2623 num_store_stats(0)
2624 { }
2625
2626 void floor(int64_t f) {
2627 stats.floor(f);
2628 store_stats.floor(f);
2629 if (log_size < f)
2630 log_size = f;
2631 if (ondisk_log_size < f)
2632 ondisk_log_size = f;
2633 if (up < f)
2634 up = f;
2635 if (acting < f)
2636 acting = f;
2637 if (num_store_stats < f)
2638 num_store_stats = f;
2639 }
2640
2641 void add(const store_statfs_t& o) {
2642 store_stats.add(o);
2643 ++num_store_stats;
2644 }
2645 void sub(const store_statfs_t& o) {
2646 store_stats.sub(o);
2647 --num_store_stats;
2648 }
2649
2650 void add(const pg_stat_t& o) {
2651 stats.add(o.stats);
2652 log_size += o.log_size;
2653 ondisk_log_size += o.ondisk_log_size;
2654 up += o.up.size();
2655 acting += o.acting.size();
2656 }
2657 void sub(const pg_stat_t& o) {
2658 stats.sub(o.stats);
2659 log_size -= o.log_size;
2660 ondisk_log_size -= o.ondisk_log_size;
2661 up -= o.up.size();
2662 acting -= o.acting.size();
2663 }
2664
2665 bool is_zero() const {
2666 return (stats.is_zero() &&
2667 store_stats.is_zero() &&
2668 log_size == 0 &&
2669 ondisk_log_size == 0 &&
2670 up == 0 &&
2671 acting == 0 &&
2672 num_store_stats == 0);
2673 }
2674
2675 // helper accessors to retrieve used/netto bytes depending on the
2676 // collection method: new per-pool objectstore report or legacy PG
2677 // summation at OSD.
2678 // In legacy mode used and netto values are the same. But for new per-pool
2679 // collection 'used' provides amount of space ALLOCATED at all related OSDs
2680 // and 'netto' is amount of stored user data.
2681 uint64_t get_allocated_data_bytes(bool per_pool) const {
2682 if (per_pool) {
2683 return store_stats.allocated;
2684 } else {
2685 // legacy mode, use numbers from 'stats'
2686 return stats.sum.num_bytes + stats.sum.num_bytes_hit_set_archive;
2687 }
2688 }
2689 uint64_t get_allocated_omap_bytes(bool per_pool_omap) const {
2690 if (per_pool_omap) {
2691 return store_stats.omap_allocated;
2692 } else {
2693 // omap is not broken out by pool by nautilus bluestore; report the
2694 // scrub value. this will be imprecise in that it won't account for
2695 // any storage overhead/efficiency.
2696 return stats.sum.num_omap_bytes;
2697 }
2698 }
2699 uint64_t get_user_data_bytes(float raw_used_rate, ///< space amp factor
2700 bool per_pool) const {
2701 // NOTE: we need the space amp factor so that we can work backwards from
2702 // the raw utilization to the amount of data that the user actually stored.
2703 if (per_pool) {
2704 return raw_used_rate ? store_stats.data_stored / raw_used_rate : 0;
2705 } else {
2706 // legacy mode, use numbers from 'stats'. note that we do NOT use the
2707 // raw_used_rate factor here because we are working from the PG stats
2708 // directly.
2709 return stats.sum.num_bytes + stats.sum.num_bytes_hit_set_archive;
2710 }
2711 }
2712 uint64_t get_user_omap_bytes(float raw_used_rate, ///< space amp factor
2713 bool per_pool_omap) const {
2714 if (per_pool_omap) {
2715 return raw_used_rate ? store_stats.omap_allocated / raw_used_rate : 0;
2716 } else {
2717 // omap usage is lazily reported during scrub; this value may lag.
2718 return stats.sum.num_omap_bytes;
2719 }
2720 }
2721
2722 void dump(ceph::Formatter *f) const;
2723 void encode(ceph::buffer::list &bl, uint64_t features) const;
2724 void decode(ceph::buffer::list::const_iterator &bl);
2725 static void generate_test_instances(std::list<pool_stat_t*>& o);
2726 };
2727 WRITE_CLASS_ENCODER_FEATURES(pool_stat_t)
2728
2729
2730 // -----------------------------------------
2731
2732 /**
2733 * pg_hit_set_info_t - information about a single recorded HitSet
2734 *
2735 * Track basic metadata about a HitSet, like the number of insertions
2736 * and the time range it covers.
2737 */
2738 struct pg_hit_set_info_t {
2739 utime_t begin, end; ///< time interval
2740 eversion_t version; ///< version this HitSet object was written
2741 bool using_gmt; ///< use gmt for creating the hit_set archive object name
2742
2743 friend bool operator==(const pg_hit_set_info_t& l,
2744 const pg_hit_set_info_t& r) {
2745 return
2746 l.begin == r.begin &&
2747 l.end == r.end &&
2748 l.version == r.version &&
2749 l.using_gmt == r.using_gmt;
2750 }
2751
2752 explicit pg_hit_set_info_t(bool using_gmt = true)
2753 : using_gmt(using_gmt) {}
2754
2755 void encode(ceph::buffer::list &bl) const;
2756 void decode(ceph::buffer::list::const_iterator &bl);
2757 void dump(ceph::Formatter *f) const;
2758 static void generate_test_instances(std::list<pg_hit_set_info_t*>& o);
2759 };
2760 WRITE_CLASS_ENCODER(pg_hit_set_info_t)
2761
2762 /**
2763 * pg_hit_set_history_t - information about a history of hitsets
2764 *
2765 * Include information about the currently accumulating hit set as well
2766 * as archived/historical ones.
2767 */
2768 struct pg_hit_set_history_t {
2769 eversion_t current_last_update; ///< last version inserted into current set
2770 std::list<pg_hit_set_info_t> history; ///< archived sets, sorted oldest -> newest
2771
2772 friend bool operator==(const pg_hit_set_history_t& l,
2773 const pg_hit_set_history_t& r) {
2774 return
2775 l.current_last_update == r.current_last_update &&
2776 l.history == r.history;
2777 }
2778
2779 void encode(ceph::buffer::list &bl) const;
2780 void decode(ceph::buffer::list::const_iterator &bl);
2781 void dump(ceph::Formatter *f) const;
2782 static void generate_test_instances(std::list<pg_hit_set_history_t*>& o);
2783 };
2784 WRITE_CLASS_ENCODER(pg_hit_set_history_t)
2785
2786
2787 // -----------------------------------------
2788
2789 /**
2790 * pg_history_t - information about recent pg peering/mapping history
2791 *
2792 * This is aggressively shared between OSDs to bound the amount of past
2793 * history they need to worry about.
2794 */
2795 struct pg_history_t {
2796 epoch_t epoch_created = 0; // epoch in which *pg* was created (pool or pg)
2797 epoch_t epoch_pool_created = 0; // epoch in which *pool* was created
2798 // (note: may be pg creation epoch for
2799 // pre-luminous clusters)
2800 epoch_t last_epoch_started = 0;; // lower bound on last epoch started (anywhere, not necessarily locally)
2801 // https://docs.ceph.com/docs/master/dev/osd_internals/last_epoch_started/
2802 epoch_t last_interval_started = 0;; // first epoch of last_epoch_started interval
2803 epoch_t last_epoch_clean = 0;; // lower bound on last epoch the PG was completely clean.
2804 epoch_t last_interval_clean = 0;; // first epoch of last_epoch_clean interval
2805 epoch_t last_epoch_split = 0;; // as parent or child
2806 epoch_t last_epoch_marked_full = 0;; // pool or cluster
2807
2808 /**
2809 * In the event of a map discontinuity, same_*_since may reflect the first
2810 * map the osd has seen in the new map sequence rather than the actual start
2811 * of the interval. This is ok since a discontinuity at epoch e means there
2812 * must have been a clean interval between e and now and that we cannot be
2813 * in the active set during the interval containing e.
2814 */
2815 epoch_t same_up_since = 0;; // same acting set since
2816 epoch_t same_interval_since = 0;; // same acting AND up set since
2817 epoch_t same_primary_since = 0;; // same primary at least back through this epoch.
2818
2819 eversion_t last_scrub;
2820 eversion_t last_deep_scrub;
2821 utime_t last_scrub_stamp;
2822 utime_t last_deep_scrub_stamp;
2823 utime_t last_clean_scrub_stamp;
2824
2825 /// upper bound on how long prior interval readable (relative to encode time)
2826 ceph::timespan prior_readable_until_ub = ceph::timespan::zero();
2827
2828 friend bool operator==(const pg_history_t& l, const pg_history_t& r) {
2829 return
2830 l.epoch_created == r.epoch_created &&
2831 l.epoch_pool_created == r.epoch_pool_created &&
2832 l.last_epoch_started == r.last_epoch_started &&
2833 l.last_interval_started == r.last_interval_started &&
2834 l.last_epoch_clean == r.last_epoch_clean &&
2835 l.last_interval_clean == r.last_interval_clean &&
2836 l.last_epoch_split == r.last_epoch_split &&
2837 l.last_epoch_marked_full == r.last_epoch_marked_full &&
2838 l.same_up_since == r.same_up_since &&
2839 l.same_interval_since == r.same_interval_since &&
2840 l.same_primary_since == r.same_primary_since &&
2841 l.last_scrub == r.last_scrub &&
2842 l.last_deep_scrub == r.last_deep_scrub &&
2843 l.last_scrub_stamp == r.last_scrub_stamp &&
2844 l.last_deep_scrub_stamp == r.last_deep_scrub_stamp &&
2845 l.last_clean_scrub_stamp == r.last_clean_scrub_stamp &&
2846 l.prior_readable_until_ub == r.prior_readable_until_ub;
2847 }
2848
2849 pg_history_t() {}
2850 pg_history_t(epoch_t created, utime_t stamp)
2851 : epoch_created(created),
2852 epoch_pool_created(created),
2853 same_up_since(created),
2854 same_interval_since(created),
2855 same_primary_since(created),
2856 last_scrub_stamp(stamp),
2857 last_deep_scrub_stamp(stamp),
2858 last_clean_scrub_stamp(stamp) {}
2859
2860 bool merge(const pg_history_t &other) {
2861 // Here, we only update the fields which cannot be calculated from the OSDmap.
2862 bool modified = false;
2863 if (epoch_created < other.epoch_created) {
2864 epoch_created = other.epoch_created;
2865 modified = true;
2866 }
2867 if (epoch_pool_created < other.epoch_pool_created) {
2868 // FIXME: for jewel compat only; this should either be 0 or always the
2869 // same value across all pg instances.
2870 epoch_pool_created = other.epoch_pool_created;
2871 modified = true;
2872 }
2873 if (last_epoch_started < other.last_epoch_started) {
2874 last_epoch_started = other.last_epoch_started;
2875 modified = true;
2876 }
2877 if (last_interval_started < other.last_interval_started) {
2878 last_interval_started = other.last_interval_started;
2879 // if we are learning about a newer *started* interval, our
2880 // readable_until_ub is obsolete
2881 prior_readable_until_ub = other.prior_readable_until_ub;
2882 modified = true;
2883 } else if (other.last_interval_started == last_interval_started &&
2884 other.prior_readable_until_ub < prior_readable_until_ub) {
2885 // if other is the *same* interval, than pull our upper bound in
2886 // if they have a tighter bound.
2887 prior_readable_until_ub = other.prior_readable_until_ub;
2888 modified = true;
2889 }
2890 if (last_epoch_clean < other.last_epoch_clean) {
2891 last_epoch_clean = other.last_epoch_clean;
2892 modified = true;
2893 }
2894 if (last_interval_clean < other.last_interval_clean) {
2895 last_interval_clean = other.last_interval_clean;
2896 modified = true;
2897 }
2898 if (last_epoch_split < other.last_epoch_split) {
2899 last_epoch_split = other.last_epoch_split;
2900 modified = true;
2901 }
2902 if (last_epoch_marked_full < other.last_epoch_marked_full) {
2903 last_epoch_marked_full = other.last_epoch_marked_full;
2904 modified = true;
2905 }
2906 if (other.last_scrub > last_scrub) {
2907 last_scrub = other.last_scrub;
2908 modified = true;
2909 }
2910 if (other.last_scrub_stamp > last_scrub_stamp) {
2911 last_scrub_stamp = other.last_scrub_stamp;
2912 modified = true;
2913 }
2914 if (other.last_deep_scrub > last_deep_scrub) {
2915 last_deep_scrub = other.last_deep_scrub;
2916 modified = true;
2917 }
2918 if (other.last_deep_scrub_stamp > last_deep_scrub_stamp) {
2919 last_deep_scrub_stamp = other.last_deep_scrub_stamp;
2920 modified = true;
2921 }
2922 if (other.last_clean_scrub_stamp > last_clean_scrub_stamp) {
2923 last_clean_scrub_stamp = other.last_clean_scrub_stamp;
2924 modified = true;
2925 }
2926 return modified;
2927 }
2928
2929 void encode(ceph::buffer::list& bl) const;
2930 void decode(ceph::buffer::list::const_iterator& p);
2931 void dump(ceph::Formatter *f) const;
2932 static void generate_test_instances(std::list<pg_history_t*>& o);
2933
2934 ceph::signedspan refresh_prior_readable_until_ub(
2935 ceph::signedspan now, ///< now, relative to osd startup_time
2936 ceph::signedspan ub) { ///< ub, relative to osd startup_time
2937 if (now >= ub) {
2938 // prior interval(s) are unreadable; we can zero the upper bound
2939 prior_readable_until_ub = ceph::signedspan::zero();
2940 return ceph::signedspan::zero();
2941 } else {
2942 prior_readable_until_ub = ub - now;
2943 return ub;
2944 }
2945 }
2946 ceph::signedspan get_prior_readable_until_ub(ceph::signedspan now) {
2947 if (prior_readable_until_ub == ceph::signedspan::zero()) {
2948 return ceph::signedspan::zero();
2949 }
2950 return now + prior_readable_until_ub;
2951 }
2952 };
2953 WRITE_CLASS_ENCODER(pg_history_t)
2954
2955 inline std::ostream& operator<<(std::ostream& out, const pg_history_t& h) {
2956 out << "ec=" << h.epoch_created << "/" << h.epoch_pool_created
2957 << " lis/c=" << h.last_interval_started
2958 << "/" << h.last_interval_clean
2959 << " les/c/f=" << h.last_epoch_started << "/" << h.last_epoch_clean
2960 << "/" << h.last_epoch_marked_full
2961 << " sis=" << h.same_interval_since;
2962 if (h.prior_readable_until_ub != ceph::timespan::zero()) {
2963 out << " pruub=" << h.prior_readable_until_ub;
2964 }
2965 return out;
2966 }
2967
2968
2969 /**
2970 * pg_info_t - summary of PG statistics.
2971 *
2972 * some notes:
2973 * - last_complete implies we have all objects that existed as of that
2974 * stamp, OR a newer object, OR have already applied a later delete.
2975 * - if last_complete >= log.tail, then we know pg contents thru log.head.
2976 * otherwise, we have no idea what the pg is supposed to contain.
2977 */
2978 struct pg_info_t {
2979 spg_t pgid;
2980 eversion_t last_update; ///< last object version applied to store.
2981 eversion_t last_complete; ///< last version pg was complete through.
2982 epoch_t last_epoch_started; ///< last epoch at which this pg started on this osd
2983 epoch_t last_interval_started; ///< first epoch of last_epoch_started interval
2984
2985 version_t last_user_version; ///< last user object version applied to store
2986
2987 eversion_t log_tail; ///< oldest log entry.
2988
2989 hobject_t last_backfill; ///< objects >= this and < last_complete may be missing
2990
2991 interval_set<snapid_t> purged_snaps;
2992
2993 pg_stat_t stats;
2994
2995 pg_history_t history;
2996 pg_hit_set_history_t hit_set;
2997
2998 friend bool operator==(const pg_info_t& l, const pg_info_t& r) {
2999 return
3000 l.pgid == r.pgid &&
3001 l.last_update == r.last_update &&
3002 l.last_complete == r.last_complete &&
3003 l.last_epoch_started == r.last_epoch_started &&
3004 l.last_interval_started == r.last_interval_started &&
3005 l.last_user_version == r.last_user_version &&
3006 l.log_tail == r.log_tail &&
3007 l.last_backfill == r.last_backfill &&
3008 l.purged_snaps == r.purged_snaps &&
3009 l.stats == r.stats &&
3010 l.history == r.history &&
3011 l.hit_set == r.hit_set;
3012 }
3013
3014 pg_info_t()
3015 : last_epoch_started(0),
3016 last_interval_started(0),
3017 last_user_version(0),
3018 last_backfill(hobject_t::get_max())
3019 { }
3020 // cppcheck-suppress noExplicitConstructor
3021 pg_info_t(spg_t p)
3022 : pgid(p),
3023 last_epoch_started(0),
3024 last_interval_started(0),
3025 last_user_version(0),
3026 last_backfill(hobject_t::get_max())
3027 { }
3028
3029 void set_last_backfill(hobject_t pos) {
3030 last_backfill = pos;
3031 }
3032
3033 bool is_empty() const { return last_update.version == 0; }
3034 bool dne() const { return history.epoch_created == 0; }
3035
3036 bool has_missing() const { return last_complete != last_update; }
3037 bool is_incomplete() const { return !last_backfill.is_max(); }
3038
3039 void encode(ceph::buffer::list& bl) const;
3040 void decode(ceph::buffer::list::const_iterator& p);
3041 void dump(ceph::Formatter *f) const;
3042 static void generate_test_instances(std::list<pg_info_t*>& o);
3043 };
3044 WRITE_CLASS_ENCODER(pg_info_t)
3045
3046 inline std::ostream& operator<<(std::ostream& out, const pg_info_t& pgi)
3047 {
3048 out << pgi.pgid << "(";
3049 if (pgi.dne())
3050 out << " DNE";
3051 if (pgi.is_empty())
3052 out << " empty";
3053 else {
3054 out << " v " << pgi.last_update;
3055 if (pgi.last_complete != pgi.last_update)
3056 out << " lc " << pgi.last_complete;
3057 out << " (" << pgi.log_tail << "," << pgi.last_update << "]";
3058 }
3059 if (pgi.is_incomplete())
3060 out << " lb " << pgi.last_backfill;
3061 //out << " c " << pgi.epoch_created;
3062 out << " local-lis/les=" << pgi.last_interval_started
3063 << "/" << pgi.last_epoch_started;
3064 out << " n=" << pgi.stats.stats.sum.num_objects;
3065 out << " " << pgi.history
3066 << ")";
3067 return out;
3068 }
3069
3070 /**
3071 * pg_fast_info_t - common pg_info_t fields
3072 *
3073 * These are the fields of pg_info_t (and children) that are updated for
3074 * most IO operations.
3075 *
3076 * ** WARNING **
3077 * Because we rely on these fields to be applied to the normal
3078 * info struct, adding a new field here that is not also new in info
3079 * means that we must set an incompat OSD feature bit!
3080 */
3081 struct pg_fast_info_t {
3082 eversion_t last_update;
3083 eversion_t last_complete;
3084 version_t last_user_version;
3085 struct { // pg_stat_t stats
3086 eversion_t version;
3087 version_t reported_seq;
3088 utime_t last_fresh;
3089 utime_t last_active;
3090 utime_t last_peered;
3091 utime_t last_clean;
3092 utime_t last_unstale;
3093 utime_t last_undegraded;
3094 utime_t last_fullsized;
3095 int64_t log_size; // (also ondisk_log_size, which has the same value)
3096 struct { // object_stat_collection_t stats;
3097 struct { // objct_stat_sum_t sum
3098 int64_t num_bytes; // in bytes
3099 int64_t num_objects;
3100 int64_t num_object_copies;
3101 int64_t num_rd;
3102 int64_t num_rd_kb;
3103 int64_t num_wr;
3104 int64_t num_wr_kb;
3105 int64_t num_objects_dirty;
3106 } sum;
3107 } stats;
3108 } stats;
3109
3110 void populate_from(const pg_info_t& info) {
3111 last_update = info.last_update;
3112 last_complete = info.last_complete;
3113 last_user_version = info.last_user_version;
3114 stats.version = info.stats.version;
3115 stats.reported_seq = info.stats.reported_seq;
3116 stats.last_fresh = info.stats.last_fresh;
3117 stats.last_active = info.stats.last_active;
3118 stats.last_peered = info.stats.last_peered;
3119 stats.last_clean = info.stats.last_clean;
3120 stats.last_unstale = info.stats.last_unstale;
3121 stats.last_undegraded = info.stats.last_undegraded;
3122 stats.last_fullsized = info.stats.last_fullsized;
3123 stats.log_size = info.stats.log_size;
3124 stats.stats.sum.num_bytes = info.stats.stats.sum.num_bytes;
3125 stats.stats.sum.num_objects = info.stats.stats.sum.num_objects;
3126 stats.stats.sum.num_object_copies = info.stats.stats.sum.num_object_copies;
3127 stats.stats.sum.num_rd = info.stats.stats.sum.num_rd;
3128 stats.stats.sum.num_rd_kb = info.stats.stats.sum.num_rd_kb;
3129 stats.stats.sum.num_wr = info.stats.stats.sum.num_wr;
3130 stats.stats.sum.num_wr_kb = info.stats.stats.sum.num_wr_kb;
3131 stats.stats.sum.num_objects_dirty = info.stats.stats.sum.num_objects_dirty;
3132 }
3133
3134 bool try_apply_to(pg_info_t* info) {
3135 if (last_update <= info->last_update)
3136 return false;
3137 info->last_update = last_update;
3138 info->last_complete = last_complete;
3139 info->last_user_version = last_user_version;
3140 info->stats.version = stats.version;
3141 info->stats.reported_seq = stats.reported_seq;
3142 info->stats.last_fresh = stats.last_fresh;
3143 info->stats.last_active = stats.last_active;
3144 info->stats.last_peered = stats.last_peered;
3145 info->stats.last_clean = stats.last_clean;
3146 info->stats.last_unstale = stats.last_unstale;
3147 info->stats.last_undegraded = stats.last_undegraded;
3148 info->stats.last_fullsized = stats.last_fullsized;
3149 info->stats.log_size = stats.log_size;
3150 info->stats.ondisk_log_size = stats.log_size;
3151 info->stats.stats.sum.num_bytes = stats.stats.sum.num_bytes;
3152 info->stats.stats.sum.num_objects = stats.stats.sum.num_objects;
3153 info->stats.stats.sum.num_object_copies = stats.stats.sum.num_object_copies;
3154 info->stats.stats.sum.num_rd = stats.stats.sum.num_rd;
3155 info->stats.stats.sum.num_rd_kb = stats.stats.sum.num_rd_kb;
3156 info->stats.stats.sum.num_wr = stats.stats.sum.num_wr;
3157 info->stats.stats.sum.num_wr_kb = stats.stats.sum.num_wr_kb;
3158 info->stats.stats.sum.num_objects_dirty = stats.stats.sum.num_objects_dirty;
3159 return true;
3160 }
3161
3162 void encode(ceph::buffer::list& bl) const {
3163 ENCODE_START(1, 1, bl);
3164 encode(last_update, bl);
3165 encode(last_complete, bl);
3166 encode(last_user_version, bl);
3167 encode(stats.version, bl);
3168 encode(stats.reported_seq, bl);
3169 encode(stats.last_fresh, bl);
3170 encode(stats.last_active, bl);
3171 encode(stats.last_peered, bl);
3172 encode(stats.last_clean, bl);
3173 encode(stats.last_unstale, bl);
3174 encode(stats.last_undegraded, bl);
3175 encode(stats.last_fullsized, bl);
3176 encode(stats.log_size, bl);
3177 encode(stats.stats.sum.num_bytes, bl);
3178 encode(stats.stats.sum.num_objects, bl);
3179 encode(stats.stats.sum.num_object_copies, bl);
3180 encode(stats.stats.sum.num_rd, bl);
3181 encode(stats.stats.sum.num_rd_kb, bl);
3182 encode(stats.stats.sum.num_wr, bl);
3183 encode(stats.stats.sum.num_wr_kb, bl);
3184 encode(stats.stats.sum.num_objects_dirty, bl);
3185 ENCODE_FINISH(bl);
3186 }
3187 void decode(ceph::buffer::list::const_iterator& p) {
3188 DECODE_START(1, p);
3189 decode(last_update, p);
3190 decode(last_complete, p);
3191 decode(last_user_version, p);
3192 decode(stats.version, p);
3193 decode(stats.reported_seq, p);
3194 decode(stats.last_fresh, p);
3195 decode(stats.last_active, p);
3196 decode(stats.last_peered, p);
3197 decode(stats.last_clean, p);
3198 decode(stats.last_unstale, p);
3199 decode(stats.last_undegraded, p);
3200 decode(stats.last_fullsized, p);
3201 decode(stats.log_size, p);
3202 decode(stats.stats.sum.num_bytes, p);
3203 decode(stats.stats.sum.num_objects, p);
3204 decode(stats.stats.sum.num_object_copies, p);
3205 decode(stats.stats.sum.num_rd, p);
3206 decode(stats.stats.sum.num_rd_kb, p);
3207 decode(stats.stats.sum.num_wr, p);
3208 decode(stats.stats.sum.num_wr_kb, p);
3209 decode(stats.stats.sum.num_objects_dirty, p);
3210 DECODE_FINISH(p);
3211 }
3212 };
3213 WRITE_CLASS_ENCODER(pg_fast_info_t)
3214
3215
3216 /**
3217 * PastIntervals -- information needed to determine the PriorSet and
3218 * the might_have_unfound set
3219 */
3220 class PastIntervals {
3221 #ifdef WITH_SEASTAR
3222 using OSDMapRef = boost::local_shared_ptr<const OSDMap>;
3223 #else
3224 using OSDMapRef = std::shared_ptr<const OSDMap>;
3225 #endif
3226 public:
3227 struct pg_interval_t {
3228 std::vector<int32_t> up, acting;
3229 epoch_t first, last;
3230 bool maybe_went_rw;
3231 int32_t primary;
3232 int32_t up_primary;
3233
3234 pg_interval_t()
3235 : first(0), last(0),
3236 maybe_went_rw(false),
3237 primary(-1),
3238 up_primary(-1)
3239 {}
3240
3241 pg_interval_t(
3242 std::vector<int32_t> &&up,
3243 std::vector<int32_t> &&acting,
3244 epoch_t first,
3245 epoch_t last,
3246 bool maybe_went_rw,
3247 int32_t primary,
3248 int32_t up_primary)
3249 : up(up), acting(acting), first(first), last(last),
3250 maybe_went_rw(maybe_went_rw), primary(primary), up_primary(up_primary)
3251 {}
3252
3253 void encode(ceph::buffer::list& bl) const;
3254 void decode(ceph::buffer::list::const_iterator& bl);
3255 void dump(ceph::Formatter *f) const;
3256 static void generate_test_instances(std::list<pg_interval_t*>& o);
3257 };
3258
3259 PastIntervals();
3260 PastIntervals(PastIntervals &&rhs) = default;
3261 PastIntervals &operator=(PastIntervals &&rhs) = default;
3262
3263 PastIntervals(const PastIntervals &rhs);
3264 PastIntervals &operator=(const PastIntervals &rhs);
3265
3266 class interval_rep {
3267 public:
3268 virtual size_t size() const = 0;
3269 virtual bool empty() const = 0;
3270 virtual void clear() = 0;
3271 virtual std::pair<epoch_t, epoch_t> get_bounds() const = 0;
3272 virtual std::set<pg_shard_t> get_all_participants(
3273 bool ec_pool) const = 0;
3274 virtual void add_interval(bool ec_pool, const pg_interval_t &interval) = 0;
3275 virtual std::unique_ptr<interval_rep> clone() const = 0;
3276 virtual std::ostream &print(std::ostream &out) const = 0;
3277 virtual void encode(ceph::buffer::list &bl) const = 0;
3278 virtual void decode(ceph::buffer::list::const_iterator &bl) = 0;
3279 virtual void dump(ceph::Formatter *f) const = 0;
3280 virtual void iterate_mayberw_back_to(
3281 epoch_t les,
3282 std::function<void(epoch_t, const std::set<pg_shard_t> &)> &&f) const = 0;
3283
3284 virtual bool has_full_intervals() const { return false; }
3285 virtual void iterate_all_intervals(
3286 std::function<void(const pg_interval_t &)> &&f) const {
3287 ceph_assert(!has_full_intervals());
3288 ceph_abort_msg("not valid for this implementation");
3289 }
3290 virtual void adjust_start_backwards(epoch_t last_epoch_clean) = 0;
3291
3292 virtual ~interval_rep() {}
3293 };
3294 friend class pi_compact_rep;
3295 private:
3296
3297 std::unique_ptr<interval_rep> past_intervals;
3298
3299 explicit PastIntervals(interval_rep *rep) : past_intervals(rep) {}
3300
3301 public:
3302 void add_interval(bool ec_pool, const pg_interval_t &interval) {
3303 ceph_assert(past_intervals);
3304 return past_intervals->add_interval(ec_pool, interval);
3305 }
3306
3307 void encode(ceph::buffer::list &bl) const {
3308 ENCODE_START(1, 1, bl);
3309 if (past_intervals) {
3310 __u8 type = 2;
3311 encode(type, bl);
3312 past_intervals->encode(bl);
3313 } else {
3314 encode((__u8)0, bl);
3315 }
3316 ENCODE_FINISH(bl);
3317 }
3318
3319 void decode(ceph::buffer::list::const_iterator &bl);
3320
3321 void dump(ceph::Formatter *f) const {
3322 ceph_assert(past_intervals);
3323 past_intervals->dump(f);
3324 }
3325 static void generate_test_instances(std::list<PastIntervals *> & o);
3326
3327 /**
3328 * Determines whether there is an interval change
3329 */
3330 static bool is_new_interval(
3331 int old_acting_primary,
3332 int new_acting_primary,
3333 const std::vector<int> &old_acting,
3334 const std::vector<int> &new_acting,
3335 int old_up_primary,
3336 int new_up_primary,
3337 const std::vector<int> &old_up,
3338 const std::vector<int> &new_up,
3339 int old_size,
3340 int new_size,
3341 int old_min_size,
3342 int new_min_size,
3343 unsigned old_pg_num,
3344 unsigned new_pg_num,
3345 unsigned old_pg_num_pending,
3346 unsigned new_pg_num_pending,
3347 bool old_sort_bitwise,
3348 bool new_sort_bitwise,
3349 bool old_recovery_deletes,
3350 bool new_recovery_deletes,
3351 uint32_t old_crush_count,
3352 uint32_t new_crush_count,
3353 uint32_t old_crush_target,
3354 uint32_t new_crush_target,
3355 uint32_t old_crush_barrier,
3356 uint32_t new_crush_barrier,
3357 int32_t old_crush_member,
3358 int32_t new_crush_member,
3359 pg_t pgid
3360 );
3361
3362 /**
3363 * Determines whether there is an interval change
3364 */
3365 static bool is_new_interval(
3366 int old_acting_primary, ///< [in] primary as of lastmap
3367 int new_acting_primary, ///< [in] primary as of lastmap
3368 const std::vector<int> &old_acting, ///< [in] acting as of lastmap
3369 const std::vector<int> &new_acting, ///< [in] acting as of osdmap
3370 int old_up_primary, ///< [in] up primary of lastmap
3371 int new_up_primary, ///< [in] up primary of osdmap
3372 const std::vector<int> &old_up, ///< [in] up as of lastmap
3373 const std::vector<int> &new_up, ///< [in] up as of osdmap
3374 const OSDMap *osdmap, ///< [in] current map
3375 const OSDMap *lastmap, ///< [in] last map
3376 pg_t pgid ///< [in] pgid for pg
3377 );
3378
3379 /**
3380 * Integrates a new map into *past_intervals, returns true
3381 * if an interval was closed out.
3382 */
3383 static bool check_new_interval(
3384 int old_acting_primary, ///< [in] primary as of lastmap
3385 int new_acting_primary, ///< [in] primary as of osdmap
3386 const std::vector<int> &old_acting, ///< [in] acting as of lastmap
3387 const std::vector<int> &new_acting, ///< [in] acting as of osdmap
3388 int old_up_primary, ///< [in] up primary of lastmap
3389 int new_up_primary, ///< [in] up primary of osdmap
3390 const std::vector<int> &old_up, ///< [in] up as of lastmap
3391 const std::vector<int> &new_up, ///< [in] up as of osdmap
3392 epoch_t same_interval_since, ///< [in] as of osdmap
3393 epoch_t last_epoch_clean, ///< [in] current
3394 const OSDMap *osdmap, ///< [in] current map
3395 const OSDMap *lastmap, ///< [in] last map
3396 pg_t pgid, ///< [in] pgid for pg
3397 const IsPGRecoverablePredicate &could_have_gone_active, ///< [in] predicate whether the pg can be active
3398 PastIntervals *past_intervals, ///< [out] intervals
3399 std::ostream *out = 0 ///< [out] debug ostream
3400 );
3401 static bool check_new_interval(
3402 int old_acting_primary, ///< [in] primary as of lastmap
3403 int new_acting_primary, ///< [in] primary as of osdmap
3404 const std::vector<int> &old_acting, ///< [in] acting as of lastmap
3405 const std::vector<int> &new_acting, ///< [in] acting as of osdmap
3406 int old_up_primary, ///< [in] up primary of lastmap
3407 int new_up_primary, ///< [in] up primary of osdmap
3408 const std::vector<int> &old_up, ///< [in] up as of lastmap
3409 const std::vector<int> &new_up, ///< [in] up as of osdmap
3410 epoch_t same_interval_since, ///< [in] as of osdmap
3411 epoch_t last_epoch_clean, ///< [in] current
3412 OSDMapRef osdmap, ///< [in] current map
3413 OSDMapRef lastmap, ///< [in] last map
3414 pg_t pgid, ///< [in] pgid for pg
3415 const IsPGRecoverablePredicate &could_have_gone_active, ///< [in] predicate whether the pg can be active
3416 PastIntervals *past_intervals, ///< [out] intervals
3417 std::ostream *out = 0 ///< [out] debug ostream
3418 ) {
3419 return check_new_interval(
3420 old_acting_primary, new_acting_primary,
3421 old_acting, new_acting,
3422 old_up_primary, new_up_primary,
3423 old_up, new_up,
3424 same_interval_since, last_epoch_clean,
3425 osdmap.get(), lastmap.get(),
3426 pgid,
3427 could_have_gone_active,
3428 past_intervals,
3429 out);
3430 }
3431
3432 friend std::ostream& operator<<(std::ostream& out, const PastIntervals &i);
3433
3434 template <typename F>
3435 void iterate_mayberw_back_to(
3436 epoch_t les,
3437 F &&f) const {
3438 ceph_assert(past_intervals);
3439 past_intervals->iterate_mayberw_back_to(les, std::forward<F>(f));
3440 }
3441 void clear() {
3442 ceph_assert(past_intervals);
3443 past_intervals->clear();
3444 }
3445
3446 /**
3447 * Should return a value which gives an indication of the amount
3448 * of state contained
3449 */
3450 size_t size() const {
3451 ceph_assert(past_intervals);
3452 return past_intervals->size();
3453 }
3454
3455 bool empty() const {
3456 ceph_assert(past_intervals);
3457 return past_intervals->empty();
3458 }
3459
3460 void swap(PastIntervals &other) {
3461 using std::swap;
3462 swap(other.past_intervals, past_intervals);
3463 }
3464
3465 /**
3466 * Return all shards which have been in the acting set back to the
3467 * latest epoch to which we have trimmed except for pg_whoami
3468 */
3469 std::set<pg_shard_t> get_might_have_unfound(
3470 pg_shard_t pg_whoami,
3471 bool ec_pool) const {
3472 ceph_assert(past_intervals);
3473 auto ret = past_intervals->get_all_participants(ec_pool);
3474 ret.erase(pg_whoami);
3475 return ret;
3476 }
3477
3478 /**
3479 * Return all shards which we might want to talk to for peering
3480 */
3481 std::set<pg_shard_t> get_all_probe(
3482 bool ec_pool) const {
3483 ceph_assert(past_intervals);
3484 return past_intervals->get_all_participants(ec_pool);
3485 }
3486
3487 /* Return the set of epochs [start, end) represented by the
3488 * past_interval set.
3489 */
3490 std::pair<epoch_t, epoch_t> get_bounds() const {
3491 ceph_assert(past_intervals);
3492 return past_intervals->get_bounds();
3493 }
3494
3495 void adjust_start_backwards(epoch_t last_epoch_clean) {
3496 ceph_assert(past_intervals);
3497 past_intervals->adjust_start_backwards(last_epoch_clean);
3498 }
3499
3500 enum osd_state_t {
3501 UP,
3502 DOWN,
3503 DNE,
3504 LOST
3505 };
3506 struct PriorSet {
3507 bool ec_pool = false;
3508 std::set<pg_shard_t> probe; ///< current+prior OSDs we need to probe.
3509 std::set<int> down; ///< down osds that would normally be in @a probe and might be interesting.
3510 std::map<int, epoch_t> blocked_by; ///< current lost_at values for any OSDs in cur set for which (re)marking them lost would affect cur set
3511
3512 bool pg_down = false; ///< some down osds are included in @a cur; the DOWN pg state bit should be set.
3513 const IsPGRecoverablePredicate* pcontdec = nullptr;
3514
3515 PriorSet() = default;
3516 PriorSet(PriorSet &&) = default;
3517 PriorSet &operator=(PriorSet &&) = default;
3518
3519 PriorSet &operator=(const PriorSet &) = delete;
3520 PriorSet(const PriorSet &) = delete;
3521
3522 bool operator==(const PriorSet &rhs) const {
3523 return (ec_pool == rhs.ec_pool) &&
3524 (probe == rhs.probe) &&
3525 (down == rhs.down) &&
3526 (blocked_by == rhs.blocked_by) &&
3527 (pg_down == rhs.pg_down);
3528 }
3529
3530 bool affected_by_map(
3531 const OSDMap &osdmap,
3532 const DoutPrefixProvider *dpp) const;
3533
3534 // For verifying tests
3535 PriorSet(
3536 bool ec_pool,
3537 std::set<pg_shard_t> probe,
3538 std::set<int> down,
3539 std::map<int, epoch_t> blocked_by,
3540 bool pg_down,
3541 const IsPGRecoverablePredicate *pcontdec)
3542 : ec_pool(ec_pool), probe(probe), down(down), blocked_by(blocked_by),
3543 pg_down(pg_down), pcontdec(pcontdec) {}
3544
3545 private:
3546 template <typename F>
3547 PriorSet(
3548 const PastIntervals &past_intervals,
3549 bool ec_pool,
3550 epoch_t last_epoch_started,
3551 const IsPGRecoverablePredicate *c,
3552 F f,
3553 const std::vector<int> &up,
3554 const std::vector<int> &acting,
3555 const DoutPrefixProvider *dpp);
3556
3557 friend class PastIntervals;
3558 };
3559
3560 template <typename... Args>
3561 PriorSet get_prior_set(Args&&... args) const {
3562 return PriorSet(*this, std::forward<Args>(args)...);
3563 }
3564 };
3565 WRITE_CLASS_ENCODER(PastIntervals)
3566
3567 std::ostream& operator<<(std::ostream& out, const PastIntervals::pg_interval_t& i);
3568 std::ostream& operator<<(std::ostream& out, const PastIntervals &i);
3569 std::ostream& operator<<(std::ostream& out, const PastIntervals::PriorSet &i);
3570
3571 template <typename F>
3572 PastIntervals::PriorSet::PriorSet(
3573 const PastIntervals &past_intervals,
3574 bool ec_pool,
3575 epoch_t last_epoch_started,
3576 const IsPGRecoverablePredicate *c,
3577 F f,
3578 const std::vector<int> &up,
3579 const std::vector<int> &acting,
3580 const DoutPrefixProvider *dpp)
3581 : ec_pool(ec_pool), pg_down(false), pcontdec(c)
3582 {
3583 /*
3584 * We have to be careful to gracefully deal with situations like
3585 * so. Say we have a power outage or something that takes out both
3586 * OSDs, but the monitor doesn't mark them down in the same epoch.
3587 * The history may look like
3588 *
3589 * 1: A B
3590 * 2: B
3591 * 3: let's say B dies for good, too (say, from the power spike)
3592 * 4: A
3593 *
3594 * which makes it look like B may have applied updates to the PG
3595 * that we need in order to proceed. This sucks...
3596 *
3597 * To minimize the risk of this happening, we CANNOT go active if
3598 * _any_ OSDs in the prior set are down until we send an MOSDAlive
3599 * to the monitor such that the OSDMap sets osd_up_thru to an epoch.
3600 * Then, we have something like
3601 *
3602 * 1: A B
3603 * 2: B up_thru[B]=0
3604 * 3:
3605 * 4: A
3606 *
3607 * -> we can ignore B, bc it couldn't have gone active (alive_thru
3608 * still 0).
3609 *
3610 * or,
3611 *
3612 * 1: A B
3613 * 2: B up_thru[B]=0
3614 * 3: B up_thru[B]=2
3615 * 4:
3616 * 5: A
3617 *
3618 * -> we must wait for B, bc it was alive through 2, and could have
3619 * written to the pg.
3620 *
3621 * If B is really dead, then an administrator will need to manually
3622 * intervene by marking the OSD as "lost."
3623 */
3624
3625 // Include current acting and up nodes... not because they may
3626 // contain old data (this interval hasn't gone active, obviously),
3627 // but because we want their pg_info to inform choose_acting(), and
3628 // so that we know what they do/do not have explicitly before
3629 // sending them any new info/logs/whatever.
3630 for (unsigned i = 0; i < acting.size(); i++) {
3631 if (acting[i] != pg_pool_t::pg_CRUSH_ITEM_NONE)
3632 probe.insert(pg_shard_t(acting[i], ec_pool ? shard_id_t(i) : shard_id_t::NO_SHARD));
3633 }
3634 // It may be possible to exclude the up nodes, but let's keep them in
3635 // there for now.
3636 for (unsigned i = 0; i < up.size(); i++) {
3637 if (up[i] != pg_pool_t::pg_CRUSH_ITEM_NONE)
3638 probe.insert(pg_shard_t(up[i], ec_pool ? shard_id_t(i) : shard_id_t::NO_SHARD));
3639 }
3640
3641 std::set<pg_shard_t> all_probe = past_intervals.get_all_probe(ec_pool);
3642 ldpp_dout(dpp, 10) << "build_prior all_probe " << all_probe << dendl;
3643 for (auto &&i: all_probe) {
3644 switch (f(0, i.osd, nullptr)) {
3645 case UP: {
3646 probe.insert(i);
3647 break;
3648 }
3649 case DNE:
3650 case LOST:
3651 case DOWN: {
3652 down.insert(i.osd);
3653 break;
3654 }
3655 }
3656 }
3657
3658 past_intervals.iterate_mayberw_back_to(
3659 last_epoch_started,
3660 [&](epoch_t start, const std::set<pg_shard_t> &acting) {
3661 ldpp_dout(dpp, 10) << "build_prior maybe_rw interval:" << start
3662 << ", acting: " << acting << dendl;
3663
3664 // look at candidate osds during this interval. each falls into
3665 // one of three categories: up, down (but potentially
3666 // interesting), or lost (down, but we won't wait for it).
3667 std::set<pg_shard_t> up_now;
3668 std::map<int, epoch_t> candidate_blocked_by;
3669 // any candidates down now (that might have useful data)
3670 bool any_down_now = false;
3671
3672 // consider ACTING osds
3673 for (auto &&so: acting) {
3674 epoch_t lost_at = 0;
3675 switch (f(start, so.osd, &lost_at)) {
3676 case UP: {
3677 // include past acting osds if they are up.
3678 up_now.insert(so);
3679 break;
3680 }
3681 case DNE: {
3682 ldpp_dout(dpp, 10) << "build_prior prior osd." << so.osd
3683 << " no longer exists" << dendl;
3684 break;
3685 }
3686 case LOST: {
3687 ldpp_dout(dpp, 10) << "build_prior prior osd." << so.osd
3688 << " is down, but lost_at " << lost_at << dendl;
3689 up_now.insert(so);
3690 break;
3691 }
3692 case DOWN: {
3693 ldpp_dout(dpp, 10) << "build_prior prior osd." << so.osd
3694 << " is down" << dendl;
3695 candidate_blocked_by[so.osd] = lost_at;
3696 any_down_now = true;
3697 break;
3698 }
3699 }
3700 }
3701
3702 // if not enough osds survived this interval, and we may have gone rw,
3703 // then we need to wait for one of those osds to recover to
3704 // ensure that we haven't lost any information.
3705 if (!(*pcontdec)(up_now) && any_down_now) {
3706 // fixme: how do we identify a "clean" shutdown anyway?
3707 ldpp_dout(dpp, 10) << "build_prior possibly went active+rw,"
3708 << " insufficient up; including down osds" << dendl;
3709 ceph_assert(!candidate_blocked_by.empty());
3710 pg_down = true;
3711 blocked_by.insert(
3712 candidate_blocked_by.begin(),
3713 candidate_blocked_by.end());
3714 }
3715 });
3716
3717 ldpp_dout(dpp, 10) << "build_prior final: probe " << probe
3718 << " down " << down
3719 << " blocked_by " << blocked_by
3720 << (pg_down ? " pg_down":"")
3721 << dendl;
3722 }
3723
3724 struct pg_notify_t {
3725 epoch_t query_epoch;
3726 epoch_t epoch_sent;
3727 pg_info_t info;
3728 shard_id_t to;
3729 shard_id_t from;
3730 PastIntervals past_intervals;
3731 pg_notify_t() :
3732 query_epoch(0), epoch_sent(0), to(shard_id_t::NO_SHARD),
3733 from(shard_id_t::NO_SHARD) {}
3734 pg_notify_t(
3735 shard_id_t to,
3736 shard_id_t from,
3737 epoch_t query_epoch,
3738 epoch_t epoch_sent,
3739 const pg_info_t &info,
3740 const PastIntervals& pi)
3741 : query_epoch(query_epoch),
3742 epoch_sent(epoch_sent),
3743 info(info), to(to), from(from),
3744 past_intervals(pi) {
3745 ceph_assert(from == info.pgid.shard);
3746 }
3747 void encode(ceph::buffer::list &bl) const;
3748 void decode(ceph::buffer::list::const_iterator &p);
3749 void dump(ceph::Formatter *f) const;
3750 static void generate_test_instances(std::list<pg_notify_t*> &o);
3751 };
3752 WRITE_CLASS_ENCODER(pg_notify_t)
3753 std::ostream &operator<<(std::ostream &lhs, const pg_notify_t &notify);
3754
3755
3756 /**
3757 * pg_query_t - used to ask a peer for information about a pg.
3758 *
3759 * note: if version=0, type=LOG, then we just provide our full log.
3760 */
3761 struct pg_query_t {
3762 enum {
3763 INFO = 0,
3764 LOG = 1,
3765 MISSING = 4,
3766 FULLLOG = 5,
3767 };
3768 std::string_view get_type_name() const {
3769 switch (type) {
3770 case INFO: return "info";
3771 case LOG: return "log";
3772 case MISSING: return "missing";
3773 case FULLLOG: return "fulllog";
3774 default: return "???";
3775 }
3776 }
3777
3778 __s32 type;
3779 eversion_t since;
3780 pg_history_t history;
3781 epoch_t epoch_sent;
3782 shard_id_t to;
3783 shard_id_t from;
3784
3785 pg_query_t() : type(-1), epoch_sent(0), to(shard_id_t::NO_SHARD),
3786 from(shard_id_t::NO_SHARD) {}
3787 pg_query_t(
3788 int t,
3789 shard_id_t to,
3790 shard_id_t from,
3791 const pg_history_t& h,
3792 epoch_t epoch_sent)
3793 : type(t),
3794 history(h),
3795 epoch_sent(epoch_sent),
3796 to(to), from(from) {
3797 ceph_assert(t != LOG);
3798 }
3799 pg_query_t(
3800 int t,
3801 shard_id_t to,
3802 shard_id_t from,
3803 eversion_t s,
3804 const pg_history_t& h,
3805 epoch_t epoch_sent)
3806 : type(t), since(s), history(h),
3807 epoch_sent(epoch_sent), to(to), from(from) {
3808 ceph_assert(t == LOG);
3809 }
3810
3811 void encode(ceph::buffer::list &bl, uint64_t features) const;
3812 void decode(ceph::buffer::list::const_iterator &bl);
3813
3814 void dump(ceph::Formatter *f) const;
3815 static void generate_test_instances(std::list<pg_query_t*>& o);
3816 };
3817 WRITE_CLASS_ENCODER_FEATURES(pg_query_t)
3818
3819 inline std::ostream& operator<<(std::ostream& out, const pg_query_t& q) {
3820 out << "query(" << q.get_type_name() << " " << q.since;
3821 if (q.type == pg_query_t::LOG)
3822 out << " " << q.history;
3823 out << " epoch_sent " << q.epoch_sent;
3824 out << ")";
3825 return out;
3826 }
3827
3828 /**
3829 * pg_lease_t - readable lease metadata, from primary -> non-primary
3830 *
3831 * This metadata serves to increase either or both of the lease expiration
3832 * and upper bound on the non-primary.
3833 */
3834 struct pg_lease_t {
3835 /// pg readable_until value; replicas must not be readable beyond this
3836 ceph::signedspan readable_until = ceph::signedspan::zero();
3837
3838 /// upper bound on any acting osd's readable_until
3839 ceph::signedspan readable_until_ub = ceph::signedspan::zero();
3840
3841 /// duration of the lease (in case clock deltas aren't available)
3842 ceph::signedspan interval = ceph::signedspan::zero();
3843
3844 pg_lease_t() {}
3845 pg_lease_t(ceph::signedspan ru, ceph::signedspan ruub,
3846 ceph::signedspan i)
3847 : readable_until(ru),
3848 readable_until_ub(ruub),
3849 interval(i) {}
3850
3851 void encode(ceph::buffer::list &bl) const;
3852 void decode(ceph::buffer::list::const_iterator &bl);
3853 void dump(ceph::Formatter *f) const;
3854 static void generate_test_instances(std::list<pg_lease_t*>& o);
3855
3856 friend std::ostream& operator<<(std::ostream& out, const pg_lease_t& l) {
3857 return out << "pg_lease(ru " << l.readable_until
3858 << " ub " << l.readable_until_ub
3859 << " int " << l.interval << ")";
3860 }
3861 };
3862 WRITE_CLASS_ENCODER(pg_lease_t)
3863
3864 /**
3865 * pg_lease_ack_t - lease ack, from non-primary -> primary
3866 *
3867 * This metadata acknowledges to the primary what a non-primary's noted
3868 * upper bound is.
3869 */
3870 struct pg_lease_ack_t {
3871 /// highest upper bound non-primary has recorded (primary's clock)
3872 ceph::signedspan readable_until_ub = ceph::signedspan::zero();
3873
3874 pg_lease_ack_t() {}
3875 pg_lease_ack_t(ceph::signedspan ub)
3876 : readable_until_ub(ub) {}
3877
3878 void encode(ceph::buffer::list &bl) const;
3879 void decode(ceph::buffer::list::const_iterator &bl);
3880 void dump(ceph::Formatter *f) const;
3881 static void generate_test_instances(std::list<pg_lease_ack_t*>& o);
3882
3883 friend std::ostream& operator<<(std::ostream& out, const pg_lease_ack_t& l) {
3884 return out << "pg_lease_ack(ruub " << l.readable_until_ub << ")";
3885 }
3886 };
3887 WRITE_CLASS_ENCODER(pg_lease_ack_t)
3888
3889
3890
3891 class PGBackend;
3892 class ObjectModDesc {
3893 bool can_local_rollback;
3894 bool rollback_info_completed;
3895
3896 // version required to decode, reflected in encode/decode version
3897 __u8 max_required_version = 1;
3898 public:
3899 class Visitor {
3900 public:
3901 virtual void append(uint64_t old_offset) {}
3902 virtual void setattrs(std::map<std::string, std::optional<ceph::buffer::list>> &attrs) {}
3903 virtual void rmobject(version_t old_version) {}
3904 /**
3905 * Used to support the unfound_lost_delete log event: if the stashed
3906 * version exists, we unstash it, otherwise, we do nothing. This way
3907 * each replica rolls back to whatever state it had prior to the attempt
3908 * at mark unfound lost delete
3909 */
3910 virtual void try_rmobject(version_t old_version) {
3911 rmobject(old_version);
3912 }
3913 virtual void create() {}
3914 virtual void update_snaps(const std::set<snapid_t> &old_snaps) {}
3915 virtual void rollback_extents(
3916 version_t gen,
3917 const std::vector<std::pair<uint64_t, uint64_t> > &extents) {}
3918 virtual ~Visitor() {}
3919 };
3920 void visit(Visitor *visitor) const;
3921 mutable ceph::buffer::list bl;
3922 enum ModID {
3923 APPEND = 1,
3924 SETATTRS = 2,
3925 DELETE = 3,
3926 CREATE = 4,
3927 UPDATE_SNAPS = 5,
3928 TRY_DELETE = 6,
3929 ROLLBACK_EXTENTS = 7
3930 };
3931 ObjectModDesc() : can_local_rollback(true), rollback_info_completed(false) {
3932 bl.reassign_to_mempool(mempool::mempool_osd_pglog);
3933 }
3934 void claim(ObjectModDesc &other) {
3935 bl = std::move(other.bl);
3936 can_local_rollback = other.can_local_rollback;
3937 rollback_info_completed = other.rollback_info_completed;
3938 }
3939 void claim_append(ObjectModDesc &other) {
3940 if (!can_local_rollback || rollback_info_completed)
3941 return;
3942 if (!other.can_local_rollback) {
3943 mark_unrollbackable();
3944 return;
3945 }
3946 bl.claim_append(other.bl);
3947 rollback_info_completed = other.rollback_info_completed;
3948 }
3949 void swap(ObjectModDesc &other) {
3950 bl.swap(other.bl);
3951
3952 using std::swap;
3953 swap(other.can_local_rollback, can_local_rollback);
3954 swap(other.rollback_info_completed, rollback_info_completed);
3955 swap(other.max_required_version, max_required_version);
3956 }
3957 void append_id(ModID id) {
3958 using ceph::encode;
3959 uint8_t _id(id);
3960 encode(_id, bl);
3961 }
3962 void append(uint64_t old_size) {
3963 if (!can_local_rollback || rollback_info_completed)
3964 return;
3965 ENCODE_START(1, 1, bl);
3966 append_id(APPEND);
3967 encode(old_size, bl);
3968 ENCODE_FINISH(bl);
3969 }
3970 void setattrs(std::map<std::string, std::optional<ceph::buffer::list>> &old_attrs) {
3971 if (!can_local_rollback || rollback_info_completed)
3972 return;
3973 ENCODE_START(1, 1, bl);
3974 append_id(SETATTRS);
3975 encode(old_attrs, bl);
3976 ENCODE_FINISH(bl);
3977 }
3978 bool rmobject(version_t deletion_version) {
3979 if (!can_local_rollback || rollback_info_completed)
3980 return false;
3981 ENCODE_START(1, 1, bl);
3982 append_id(DELETE);
3983 encode(deletion_version, bl);
3984 ENCODE_FINISH(bl);
3985 rollback_info_completed = true;
3986 return true;
3987 }
3988 bool try_rmobject(version_t deletion_version) {
3989 if (!can_local_rollback || rollback_info_completed)
3990 return false;
3991 ENCODE_START(1, 1, bl);
3992 append_id(TRY_DELETE);
3993 encode(deletion_version, bl);
3994 ENCODE_FINISH(bl);
3995 rollback_info_completed = true;
3996 return true;
3997 }
3998 void create() {
3999 if (!can_local_rollback || rollback_info_completed)
4000 return;
4001 rollback_info_completed = true;
4002 ENCODE_START(1, 1, bl);
4003 append_id(CREATE);
4004 ENCODE_FINISH(bl);
4005 }
4006 void update_snaps(const std::set<snapid_t> &old_snaps) {
4007 if (!can_local_rollback || rollback_info_completed)
4008 return;
4009 ENCODE_START(1, 1, bl);
4010 append_id(UPDATE_SNAPS);
4011 encode(old_snaps, bl);
4012 ENCODE_FINISH(bl);
4013 }
4014 void rollback_extents(
4015 version_t gen, const std::vector<std::pair<uint64_t, uint64_t> > &extents) {
4016 ceph_assert(can_local_rollback);
4017 ceph_assert(!rollback_info_completed);
4018 if (max_required_version < 2)
4019 max_required_version = 2;
4020 ENCODE_START(2, 2, bl);
4021 append_id(ROLLBACK_EXTENTS);
4022 encode(gen, bl);
4023 encode(extents, bl);
4024 ENCODE_FINISH(bl);
4025 }
4026
4027 // cannot be rolled back
4028 void mark_unrollbackable() {
4029 can_local_rollback = false;
4030 bl.clear();
4031 }
4032 bool can_rollback() const {
4033 return can_local_rollback;
4034 }
4035 bool empty() const {
4036 return can_local_rollback && (bl.length() == 0);
4037 }
4038
4039 bool requires_kraken() const {
4040 return max_required_version >= 2;
4041 }
4042
4043 /**
4044 * Create fresh copy of bl bytes to avoid keeping large buffers around
4045 * in the case that bl contains ptrs which point into a much larger
4046 * message buffer
4047 */
4048 void trim_bl() const {
4049 if (bl.length() > 0)
4050 bl.rebuild();
4051 }
4052 void encode(ceph::buffer::list &bl) const;
4053 void decode(ceph::buffer::list::const_iterator &bl);
4054 void dump(ceph::Formatter *f) const;
4055 static void generate_test_instances(std::list<ObjectModDesc*>& o);
4056 };
4057 WRITE_CLASS_ENCODER(ObjectModDesc)
4058
4059 class ObjectCleanRegions {
4060 private:
4061 bool new_object;
4062 bool clean_omap;
4063 interval_set<uint64_t> clean_offsets;
4064 static std::atomic<uint32_t> max_num_intervals;
4065
4066 /**
4067 * trim the number of intervals if clean_offsets.num_intervals()
4068 * exceeds the given upbound max_num_intervals
4069 * etc. max_num_intervals=2, clean_offsets:{[5~10], [20~5]}
4070 * then new interval [30~10] will evict out the shortest one [20~5]
4071 * finally, clean_offsets becomes {[5~10], [30~10]}
4072 */
4073 void trim();
4074 friend std::ostream& operator<<(std::ostream& out, const ObjectCleanRegions& ocr);
4075 public:
4076 ObjectCleanRegions() : new_object(false), clean_omap(true) {
4077 clean_offsets.insert(0, (uint64_t)-1);
4078 }
4079 ObjectCleanRegions(uint64_t offset, uint64_t len, bool co)
4080 : new_object(false), clean_omap(co) {
4081 clean_offsets.insert(offset, len);
4082 }
4083 bool operator==(const ObjectCleanRegions &orc) const {
4084 return new_object == orc.new_object && clean_omap == orc.clean_omap && clean_offsets == orc.clean_offsets;
4085 }
4086 static void set_max_num_intervals(uint32_t num);
4087 void merge(const ObjectCleanRegions &other);
4088 void mark_data_region_dirty(uint64_t offset, uint64_t len);
4089 void mark_omap_dirty();
4090 void mark_object_new();
4091 void mark_fully_dirty();
4092 interval_set<uint64_t> get_dirty_regions() const;
4093 bool omap_is_dirty() const;
4094 bool object_is_exist() const;
4095 bool is_clean_region(uint64_t offset, uint64_t len) const;
4096
4097 void encode(ceph::buffer::list &bl) const;
4098 void decode(ceph::buffer::list::const_iterator &bl);
4099 void dump(ceph::Formatter *f) const;
4100 static void generate_test_instances(std::list<ObjectCleanRegions*>& o);
4101 };
4102 WRITE_CLASS_ENCODER(ObjectCleanRegions)
4103 std::ostream& operator<<(std::ostream& out, const ObjectCleanRegions& ocr);
4104
4105
4106 struct OSDOp {
4107 ceph_osd_op op;
4108 sobject_t soid;
4109
4110 ceph::buffer::list indata, outdata;
4111 errorcode32_t rval = 0;
4112
4113 OSDOp() {
4114 // FIPS zeroization audit 20191115: this memset clean for security
4115 memset(&op, 0, sizeof(ceph_osd_op));
4116 }
4117
4118 OSDOp(const int op_code) {
4119 // FIPS zeroization audit 20191115: this memset clean for security
4120 memset(&op, 0, sizeof(ceph_osd_op));
4121 op.op = op_code;
4122 }
4123
4124 /**
4125 * split a ceph::buffer::list into constituent indata members of a vector of OSDOps
4126 *
4127 * @param ops [out] vector of OSDOps
4128 * @param in [in] combined data buffer
4129 */
4130 template<typename V>
4131 static void split_osd_op_vector_in_data(V& ops,
4132 ceph::buffer::list& in) {
4133 ceph::buffer::list::iterator datap = in.begin();
4134 for (unsigned i = 0; i < ops.size(); i++) {
4135 if (ops[i].op.payload_len) {
4136 datap.copy(ops[i].op.payload_len, ops[i].indata);
4137 }
4138 }
4139 }
4140
4141 /**
4142 * merge indata members of a vector of OSDOp into a single ceph::buffer::list
4143 *
4144 * Notably this also encodes certain other OSDOp data into the data
4145 * buffer, including the sobject_t soid.
4146 *
4147 * @param ops [in] vector of OSDOps
4148 * @param out [out] combined data buffer
4149 */
4150 template<typename V>
4151 static void merge_osd_op_vector_in_data(V& ops, ceph::buffer::list& out) {
4152 for (unsigned i = 0; i < ops.size(); i++) {
4153 if (ops[i].indata.length()) {
4154 ops[i].op.payload_len = ops[i].indata.length();
4155 out.append(ops[i].indata);
4156 }
4157 }
4158 }
4159
4160 /**
4161 * split a ceph::buffer::list into constituent outdata members of a vector of OSDOps
4162 *
4163 * @param ops [out] vector of OSDOps
4164 * @param in [in] combined data buffer
4165 */
4166 static void split_osd_op_vector_out_data(std::vector<OSDOp>& ops, ceph::buffer::list& in);
4167
4168 /**
4169 * merge outdata members of a vector of OSDOps into a single ceph::buffer::list
4170 *
4171 * @param ops [in] vector of OSDOps
4172 * @param out [out] combined data buffer
4173 */
4174 static void merge_osd_op_vector_out_data(std::vector<OSDOp>& ops, ceph::buffer::list& out);
4175
4176 /**
4177 * Clear data as much as possible, leave minimal data for historical op dump
4178 *
4179 * @param ops [in] vector of OSDOps
4180 */
4181 template<typename V>
4182 static void clear_data(V& ops) {
4183 for (unsigned i = 0; i < ops.size(); i++) {
4184 OSDOp& op = ops[i];
4185 op.outdata.clear();
4186 if (ceph_osd_op_type_attr(op.op.op) &&
4187 op.op.xattr.name_len &&
4188 op.indata.length() >= op.op.xattr.name_len) {
4189 ceph::buffer::list bl;
4190 bl.push_back(ceph::buffer::ptr_node::create(op.op.xattr.name_len));
4191 bl.begin().copy_in(op.op.xattr.name_len, op.indata);
4192 op.indata = std::move(bl);
4193 } else if (ceph_osd_op_type_exec(op.op.op) &&
4194 op.op.cls.class_len &&
4195 op.indata.length() >
4196 (op.op.cls.class_len + op.op.cls.method_len)) {
4197 __u8 len = op.op.cls.class_len + op.op.cls.method_len;
4198 ceph::buffer::list bl;
4199 bl.push_back(ceph::buffer::ptr_node::create(len));
4200 bl.begin().copy_in(len, op.indata);
4201 op.indata = std::move(bl);
4202 } else {
4203 op.indata.clear();
4204 }
4205 }
4206 }
4207 };
4208 std::ostream& operator<<(std::ostream& out, const OSDOp& op);
4209
4210 struct pg_log_op_return_item_t {
4211 int32_t rval;
4212 ceph::buffer::list bl;
4213 void encode(ceph::buffer::list& p) const {
4214 using ceph::encode;
4215 encode(rval, p);
4216 encode(bl, p);
4217 }
4218 void decode(ceph::buffer::list::const_iterator& p) {
4219 using ceph::decode;
4220 decode(rval, p);
4221 decode(bl, p);
4222 }
4223 void dump(ceph::Formatter *f) const {
4224 f->dump_int("rval", rval);
4225 f->dump_unsigned("bl_length", bl.length());
4226 }
4227 friend bool operator==(const pg_log_op_return_item_t& lhs,
4228 const pg_log_op_return_item_t& rhs) {
4229 return lhs.rval == rhs.rval &&
4230 lhs.bl.contents_equal(rhs.bl);
4231 }
4232 friend bool operator!=(const pg_log_op_return_item_t& lhs,
4233 const pg_log_op_return_item_t& rhs) {
4234 return !(lhs == rhs);
4235 }
4236 friend std::ostream& operator<<(std::ostream& out, const pg_log_op_return_item_t& i) {
4237 return out << "r=" << i.rval << "+" << i.bl.length() << "b";
4238 }
4239 };
4240 WRITE_CLASS_ENCODER(pg_log_op_return_item_t)
4241
4242 /**
4243 * pg_log_entry_t - single entry/event in pg log
4244 *
4245 */
4246 struct pg_log_entry_t {
4247 enum {
4248 MODIFY = 1, // some unspecified modification (but not *all* modifications)
4249 CLONE = 2, // cloned object from head
4250 DELETE = 3, // deleted object
4251 //BACKLOG = 4, // event invented by generate_backlog [obsolete]
4252 LOST_REVERT = 5, // lost new version, revert to an older version.
4253 LOST_DELETE = 6, // lost new version, revert to no object (deleted).
4254 LOST_MARK = 7, // lost new version, now EIO
4255 PROMOTE = 8, // promoted object from another tier
4256 CLEAN = 9, // mark an object clean
4257 ERROR = 10, // write that returned an error
4258 };
4259 static const char *get_op_name(int op) {
4260 switch (op) {
4261 case MODIFY:
4262 return "modify";
4263 case PROMOTE:
4264 return "promote";
4265 case CLONE:
4266 return "clone";
4267 case DELETE:
4268 return "delete";
4269 case LOST_REVERT:
4270 return "l_revert";
4271 case LOST_DELETE:
4272 return "l_delete";
4273 case LOST_MARK:
4274 return "l_mark";
4275 case CLEAN:
4276 return "clean";
4277 case ERROR:
4278 return "error";
4279 default:
4280 return "unknown";
4281 }
4282 }
4283 const char *get_op_name() const {
4284 return get_op_name(op);
4285 }
4286
4287 // describes state for a locally-rollbackable entry
4288 ObjectModDesc mod_desc;
4289 ceph::buffer::list snaps; // only for clone entries
4290 hobject_t soid;
4291 osd_reqid_t reqid; // caller+tid to uniquely identify request
4292 mempool::osd_pglog::vector<std::pair<osd_reqid_t, version_t> > extra_reqids;
4293
4294 /// map extra_reqids by index to error return code (if any)
4295 mempool::osd_pglog::map<uint32_t, int> extra_reqid_return_codes;
4296
4297 eversion_t version, prior_version, reverting_to;
4298 version_t user_version; // the user version for this entry
4299 utime_t mtime; // this is the _user_ mtime, mind you
4300 int32_t return_code; // only stored for ERRORs for dup detection
4301
4302 std::vector<pg_log_op_return_item_t> op_returns;
4303
4304 __s32 op;
4305 bool invalid_hash; // only when decoding sobject_t based entries
4306 bool invalid_pool; // only when decoding pool-less hobject based entries
4307 ObjectCleanRegions clean_regions;
4308
4309 pg_log_entry_t()
4310 : user_version(0), return_code(0), op(0),
4311 invalid_hash(false), invalid_pool(false) {
4312 snaps.reassign_to_mempool(mempool::mempool_osd_pglog);
4313 }
4314 pg_log_entry_t(int _op, const hobject_t& _soid,
4315 const eversion_t& v, const eversion_t& pv,
4316 version_t uv,
4317 const osd_reqid_t& rid, const utime_t& mt,
4318 int return_code)
4319 : soid(_soid), reqid(rid), version(v), prior_version(pv), user_version(uv),
4320 mtime(mt), return_code(return_code), op(_op),
4321 invalid_hash(false), invalid_pool(false) {
4322 snaps.reassign_to_mempool(mempool::mempool_osd_pglog);
4323 }
4324
4325 bool is_clone() const { return op == CLONE; }
4326 bool is_modify() const { return op == MODIFY; }
4327 bool is_promote() const { return op == PROMOTE; }
4328 bool is_clean() const { return op == CLEAN; }
4329 bool is_lost_revert() const { return op == LOST_REVERT; }
4330 bool is_lost_delete() const { return op == LOST_DELETE; }
4331 bool is_lost_mark() const { return op == LOST_MARK; }
4332 bool is_error() const { return op == ERROR; }
4333
4334 bool is_update() const {
4335 return
4336 is_clone() || is_modify() || is_promote() || is_clean() ||
4337 is_lost_revert() || is_lost_mark();
4338 }
4339 bool is_delete() const {
4340 return op == DELETE || op == LOST_DELETE;
4341 }
4342
4343 bool can_rollback() const {
4344 return mod_desc.can_rollback();
4345 }
4346
4347 void mark_unrollbackable() {
4348 mod_desc.mark_unrollbackable();
4349 }
4350
4351 bool requires_kraken() const {
4352 return mod_desc.requires_kraken();
4353 }
4354
4355 // Errors are only used for dup detection, whereas
4356 // the index by objects is used by recovery, copy_get,
4357 // and other facilities that don't expect or need to
4358 // be aware of error entries.
4359 bool object_is_indexed() const {
4360 return !is_error();
4361 }
4362
4363 bool reqid_is_indexed() const {
4364 return reqid != osd_reqid_t() &&
4365 (op == MODIFY || op == DELETE || op == ERROR);
4366 }
4367
4368 void set_op_returns(const std::vector<OSDOp>& ops) {
4369 op_returns.resize(ops.size());
4370 for (unsigned i = 0; i < ops.size(); ++i) {
4371 op_returns[i].rval = ops[i].rval;
4372 op_returns[i].bl = ops[i].outdata;
4373 }
4374 }
4375
4376 std::string get_key_name() const;
4377 void encode_with_checksum(ceph::buffer::list& bl) const;
4378 void decode_with_checksum(ceph::buffer::list::const_iterator& p);
4379
4380 void encode(ceph::buffer::list &bl) const;
4381 void decode(ceph::buffer::list::const_iterator &bl);
4382 void dump(ceph::Formatter *f) const;
4383 static void generate_test_instances(std::list<pg_log_entry_t*>& o);
4384
4385 };
4386 WRITE_CLASS_ENCODER(pg_log_entry_t)
4387
4388 std::ostream& operator<<(std::ostream& out, const pg_log_entry_t& e);
4389
4390 struct pg_log_dup_t {
4391 osd_reqid_t reqid; // caller+tid to uniquely identify request
4392 eversion_t version;
4393 version_t user_version; // the user version for this entry
4394 int32_t return_code; // only stored for ERRORs for dup detection
4395
4396 std::vector<pg_log_op_return_item_t> op_returns;
4397
4398 pg_log_dup_t()
4399 : user_version(0), return_code(0)
4400 {}
4401 explicit pg_log_dup_t(const pg_log_entry_t& entry)
4402 : reqid(entry.reqid), version(entry.version),
4403 user_version(entry.user_version),
4404 return_code(entry.return_code),
4405 op_returns(entry.op_returns)
4406 {}
4407 pg_log_dup_t(const eversion_t& v, version_t uv,
4408 const osd_reqid_t& rid, int return_code)
4409 : reqid(rid), version(v), user_version(uv),
4410 return_code(return_code)
4411 {}
4412
4413 std::string get_key_name() const;
4414 void encode(ceph::buffer::list &bl) const;
4415 void decode(ceph::buffer::list::const_iterator &bl);
4416 void dump(ceph::Formatter *f) const;
4417 static void generate_test_instances(std::list<pg_log_dup_t*>& o);
4418
4419 bool operator==(const pg_log_dup_t &rhs) const {
4420 return reqid == rhs.reqid &&
4421 version == rhs.version &&
4422 user_version == rhs.user_version &&
4423 return_code == rhs.return_code &&
4424 op_returns == rhs.op_returns;
4425 }
4426 bool operator!=(const pg_log_dup_t &rhs) const {
4427 return !(*this == rhs);
4428 }
4429
4430 friend std::ostream& operator<<(std::ostream& out, const pg_log_dup_t& e);
4431 };
4432 WRITE_CLASS_ENCODER(pg_log_dup_t)
4433
4434 std::ostream& operator<<(std::ostream& out, const pg_log_dup_t& e);
4435
4436 /**
4437 * pg_log_t - incremental log of recent pg changes.
4438 *
4439 * serves as a recovery queue for recent changes.
4440 */
4441 struct pg_log_t {
4442 /*
4443 * head - newest entry (update|delete)
4444 * tail - entry previous to oldest (update|delete) for which we have
4445 * complete negative information.
4446 * i.e. we can infer pg contents for any store whose last_update >= tail.
4447 */
4448 eversion_t head; // newest entry
4449 eversion_t tail; // version prior to oldest
4450
4451 protected:
4452 // We can rollback rollback-able entries > can_rollback_to
4453 eversion_t can_rollback_to;
4454
4455 // always <= can_rollback_to, indicates how far stashed rollback
4456 // data can be found
4457 eversion_t rollback_info_trimmed_to;
4458
4459 public:
4460 // the actual log
4461 mempool::osd_pglog::list<pg_log_entry_t> log;
4462
4463 // entries just for dup op detection ordered oldest to newest
4464 mempool::osd_pglog::list<pg_log_dup_t> dups;
4465
4466 pg_log_t() = default;
4467 pg_log_t(const eversion_t &last_update,
4468 const eversion_t &log_tail,
4469 const eversion_t &can_rollback_to,
4470 const eversion_t &rollback_info_trimmed_to,
4471 mempool::osd_pglog::list<pg_log_entry_t> &&entries,
4472 mempool::osd_pglog::list<pg_log_dup_t> &&dup_entries)
4473 : head(last_update), tail(log_tail), can_rollback_to(can_rollback_to),
4474 rollback_info_trimmed_to(rollback_info_trimmed_to),
4475 log(std::move(entries)), dups(std::move(dup_entries)) {}
4476 pg_log_t(const eversion_t &last_update,
4477 const eversion_t &log_tail,
4478 const eversion_t &can_rollback_to,
4479 const eversion_t &rollback_info_trimmed_to,
4480 const std::list<pg_log_entry_t> &entries,
4481 const std::list<pg_log_dup_t> &dup_entries)
4482 : head(last_update), tail(log_tail), can_rollback_to(can_rollback_to),
4483 rollback_info_trimmed_to(rollback_info_trimmed_to) {
4484 for (auto &&entry: entries) {
4485 log.push_back(entry);
4486 }
4487 for (auto &&entry: dup_entries) {
4488 dups.push_back(entry);
4489 }
4490 }
4491
4492 void clear() {
4493 eversion_t z;
4494 rollback_info_trimmed_to = can_rollback_to = head = tail = z;
4495 log.clear();
4496 dups.clear();
4497 }
4498
4499 eversion_t get_rollback_info_trimmed_to() const {
4500 return rollback_info_trimmed_to;
4501 }
4502 eversion_t get_can_rollback_to() const {
4503 return can_rollback_to;
4504 }
4505
4506
4507 pg_log_t split_out_child(pg_t child_pgid, unsigned split_bits) {
4508 mempool::osd_pglog::list<pg_log_entry_t> oldlog, childlog;
4509 oldlog.swap(log);
4510
4511 eversion_t old_tail;
4512 unsigned mask = ~((~0)<<split_bits);
4513 for (auto i = oldlog.begin();
4514 i != oldlog.end();
4515 ) {
4516 if ((i->soid.get_hash() & mask) == child_pgid.m_seed) {
4517 childlog.push_back(*i);
4518 } else {
4519 log.push_back(*i);
4520 }
4521 oldlog.erase(i++);
4522 }
4523
4524 // osd_reqid is unique, so it doesn't matter if there are extra
4525 // dup entries in each pg. To avoid storing oid with the dup
4526 // entries, just copy the whole list.
4527 auto childdups(dups);
4528
4529 return pg_log_t(
4530 head,
4531 tail,
4532 can_rollback_to,
4533 rollback_info_trimmed_to,
4534 std::move(childlog),
4535 std::move(childdups));
4536 }
4537
4538 mempool::osd_pglog::list<pg_log_entry_t> rewind_from_head(eversion_t newhead) {
4539 ceph_assert(newhead >= tail);
4540
4541 mempool::osd_pglog::list<pg_log_entry_t>::iterator p = log.end();
4542 mempool::osd_pglog::list<pg_log_entry_t> divergent;
4543 while (true) {
4544 if (p == log.begin()) {
4545 // yikes, the whole thing is divergent!
4546 using std::swap;
4547 swap(divergent, log);
4548 break;
4549 }
4550 --p;
4551 if (p->version.version <= newhead.version) {
4552 /*
4553 * look at eversion.version here. we want to avoid a situation like:
4554 * our log: 100'10 (0'0) m 10000004d3a.00000000/head by client4225.1:18529
4555 * new log: 122'10 (0'0) m 10000004d3a.00000000/head by client4225.1:18529
4556 * lower_bound = 100'9
4557 * i.e, same request, different version. If the eversion.version is > the
4558 * lower_bound, we it is divergent.
4559 */
4560 ++p;
4561 divergent.splice(divergent.begin(), log, p, log.end());
4562 break;
4563 }
4564 ceph_assert(p->version > newhead);
4565 }
4566 head = newhead;
4567
4568 if (can_rollback_to > newhead)
4569 can_rollback_to = newhead;
4570
4571 if (rollback_info_trimmed_to > newhead)
4572 rollback_info_trimmed_to = newhead;
4573
4574 return divergent;
4575 }
4576
4577 void merge_from(const std::vector<pg_log_t*>& slogs, eversion_t last_update) {
4578 log.clear();
4579
4580 // sort and merge dups
4581 std::multimap<eversion_t,pg_log_dup_t> sorted;
4582 for (auto& d : dups) {
4583 sorted.emplace(d.version, d);
4584 }
4585 for (auto l : slogs) {
4586 for (auto& d : l->dups) {
4587 sorted.emplace(d.version, d);
4588 }
4589 }
4590 dups.clear();
4591 for (auto& i : sorted) {
4592 dups.push_back(i.second);
4593 }
4594
4595 head = last_update;
4596 tail = last_update;
4597 can_rollback_to = last_update;
4598 rollback_info_trimmed_to = last_update;
4599 }
4600
4601 bool empty() const {
4602 return log.empty();
4603 }
4604
4605 bool null() const {
4606 return head.version == 0 && head.epoch == 0;
4607 }
4608
4609 uint64_t approx_size() const {
4610 return head.version - tail.version;
4611 }
4612
4613 static void filter_log(spg_t import_pgid, const OSDMap &curmap,
4614 const std::string &hit_set_namespace, const pg_log_t &in,
4615 pg_log_t &out, pg_log_t &reject);
4616
4617 /**
4618 * copy entries from the tail of another pg_log_t
4619 *
4620 * @param other pg_log_t to copy from
4621 * @param from copy entries after this version
4622 */
4623 void copy_after(CephContext* cct, const pg_log_t &other, eversion_t from);
4624
4625 /**
4626 * copy up to N entries
4627 *
4628 * @param other source log
4629 * @param max max number of entries to copy
4630 */
4631 void copy_up_to(CephContext* cct, const pg_log_t &other, int max);
4632
4633 std::ostream& print(std::ostream& out) const;
4634
4635 void encode(ceph::buffer::list &bl) const;
4636 void decode(ceph::buffer::list::const_iterator &bl, int64_t pool = -1);
4637 void dump(ceph::Formatter *f) const;
4638 static void generate_test_instances(std::list<pg_log_t*>& o);
4639 };
4640 WRITE_CLASS_ENCODER(pg_log_t)
4641
4642 inline std::ostream& operator<<(std::ostream& out, const pg_log_t& log)
4643 {
4644 out << "log((" << log.tail << "," << log.head << "], crt="
4645 << log.get_can_rollback_to() << ")";
4646 return out;
4647 }
4648
4649
4650 /**
4651 * pg_missing_t - summary of missing objects.
4652 *
4653 * kept in memory, as a supplement to pg_log_t
4654 * also used to pass missing info in messages.
4655 */
4656 struct pg_missing_item {
4657 eversion_t need, have;
4658 ObjectCleanRegions clean_regions;
4659 enum missing_flags_t {
4660 FLAG_NONE = 0,
4661 FLAG_DELETE = 1,
4662 } flags;
4663 pg_missing_item() : flags(FLAG_NONE) {}
4664 explicit pg_missing_item(eversion_t n) : need(n), flags(FLAG_NONE) {} // have no old version
4665 pg_missing_item(eversion_t n, eversion_t h, bool is_delete=false, bool old_style = false) :
4666 need(n), have(h) {
4667 set_delete(is_delete);
4668 if (old_style)
4669 clean_regions.mark_fully_dirty();
4670 }
4671
4672 void encode(ceph::buffer::list& bl, uint64_t features) const {
4673 using ceph::encode;
4674 if (HAVE_FEATURE(features, SERVER_OCTOPUS)) {
4675 // encoding a zeroed eversion_t to differentiate between OSD_RECOVERY_DELETES、
4676 // SERVER_OCTOPUS and legacy unversioned encoding - a need value of 0'0 is not
4677 // possible. This can be replaced with the legacy encoding
4678 encode(eversion_t(), bl);
4679 encode(eversion_t(-1, -1), bl);
4680 encode(need, bl);
4681 encode(have, bl);
4682 encode(static_cast<uint8_t>(flags), bl);
4683 encode(clean_regions, bl);
4684 } else {
4685 encode(eversion_t(), bl);
4686 encode(need, bl);
4687 encode(have, bl);
4688 encode(static_cast<uint8_t>(flags), bl);
4689 }
4690 }
4691 void decode(ceph::buffer::list::const_iterator& bl) {
4692 using ceph::decode;
4693 eversion_t e, l;
4694 decode(e, bl);
4695 decode(l, bl);
4696 if(l == eversion_t(-1, -1)) {
4697 // support all
4698 decode(need, bl);
4699 decode(have, bl);
4700 uint8_t f;
4701 decode(f, bl);
4702 flags = static_cast<missing_flags_t>(f);
4703 decode(clean_regions, bl);
4704 } else {
4705 // support OSD_RECOVERY_DELETES
4706 need = l;
4707 decode(have, bl);
4708 uint8_t f;
4709 decode(f, bl);
4710 flags = static_cast<missing_flags_t>(f);
4711 clean_regions.mark_fully_dirty();
4712 }
4713 }
4714
4715 void set_delete(bool is_delete) {
4716 flags = is_delete ? FLAG_DELETE : FLAG_NONE;
4717 }
4718
4719 bool is_delete() const {
4720 return (flags & FLAG_DELETE) == FLAG_DELETE;
4721 }
4722
4723 std::string flag_str() const {
4724 if (flags == FLAG_NONE) {
4725 return "none";
4726 } else {
4727 return "delete";
4728 }
4729 }
4730
4731 void dump(ceph::Formatter *f) const {
4732 f->dump_stream("need") << need;
4733 f->dump_stream("have") << have;
4734 f->dump_stream("flags") << flag_str();
4735 f->dump_stream("clean_regions") << clean_regions;
4736 }
4737 static void generate_test_instances(std::list<pg_missing_item*>& o) {
4738 o.push_back(new pg_missing_item);
4739 o.push_back(new pg_missing_item);
4740 o.back()->need = eversion_t(1, 2);
4741 o.back()->have = eversion_t(1, 1);
4742 o.push_back(new pg_missing_item);
4743 o.back()->need = eversion_t(3, 5);
4744 o.back()->have = eversion_t(3, 4);
4745 o.back()->clean_regions.mark_data_region_dirty(4096, 8192);
4746 o.back()->clean_regions.mark_omap_dirty();
4747 o.back()->flags = FLAG_DELETE;
4748 }
4749 bool operator==(const pg_missing_item &rhs) const {
4750 return need == rhs.need && have == rhs.have && flags == rhs.flags;
4751 }
4752 bool operator!=(const pg_missing_item &rhs) const {
4753 return !(*this == rhs);
4754 }
4755 };
4756 WRITE_CLASS_ENCODER_FEATURES(pg_missing_item)
4757 std::ostream& operator<<(std::ostream& out, const pg_missing_item &item);
4758
4759 class pg_missing_const_i {
4760 public:
4761 virtual const std::map<hobject_t, pg_missing_item> &
4762 get_items() const = 0;
4763 virtual const std::map<version_t, hobject_t> &get_rmissing() const = 0;
4764 virtual bool get_may_include_deletes() const = 0;
4765 virtual unsigned int num_missing() const = 0;
4766 virtual bool have_missing() const = 0;
4767 virtual bool is_missing(const hobject_t& oid, pg_missing_item *out = nullptr) const = 0;
4768 virtual bool is_missing(const hobject_t& oid, eversion_t v) const = 0;
4769 virtual ~pg_missing_const_i() {}
4770 };
4771
4772
4773 template <bool Track>
4774 class ChangeTracker {
4775 public:
4776 void changed(const hobject_t &obj) {}
4777 template <typename F>
4778 void get_changed(F &&f) const {}
4779 void flush() {}
4780 bool is_clean() const {
4781 return true;
4782 }
4783 };
4784 template <>
4785 class ChangeTracker<true> {
4786 std::set<hobject_t> _changed;
4787 public:
4788 void changed(const hobject_t &obj) {
4789 _changed.insert(obj);
4790 }
4791 template <typename F>
4792 void get_changed(F &&f) const {
4793 for (auto const &i: _changed) {
4794 f(i);
4795 }
4796 }
4797 void flush() {
4798 _changed.clear();
4799 }
4800 bool is_clean() const {
4801 return _changed.empty();
4802 }
4803 };
4804
4805 template <bool TrackChanges>
4806 class pg_missing_set : public pg_missing_const_i {
4807 using item = pg_missing_item;
4808 std::map<hobject_t, item> missing; // oid -> (need v, have v)
4809 std::map<version_t, hobject_t> rmissing; // v -> oid
4810 ChangeTracker<TrackChanges> tracker;
4811
4812 public:
4813 pg_missing_set() = default;
4814
4815 template <typename missing_type>
4816 pg_missing_set(const missing_type &m) {
4817 missing = m.get_items();
4818 rmissing = m.get_rmissing();
4819 may_include_deletes = m.get_may_include_deletes();
4820 for (auto &&i: missing)
4821 tracker.changed(i.first);
4822 }
4823
4824 bool may_include_deletes = false;
4825
4826 const std::map<hobject_t, item> &get_items() const override {
4827 return missing;
4828 }
4829 const std::map<version_t, hobject_t> &get_rmissing() const override {
4830 return rmissing;
4831 }
4832 bool get_may_include_deletes() const override {
4833 return may_include_deletes;
4834 }
4835 unsigned int num_missing() const override {
4836 return missing.size();
4837 }
4838 bool have_missing() const override {
4839 return !missing.empty();
4840 }
4841 void merge(const pg_log_entry_t& e) {
4842 auto miter = missing.find(e.soid);
4843 if (miter != missing.end() && miter->second.have != eversion_t() && e.version > miter->second.have)
4844 miter->second.clean_regions.merge(e.clean_regions);
4845 }
4846 bool is_missing(const hobject_t& oid, pg_missing_item *out = nullptr) const override {
4847 auto iter = missing.find(oid);
4848 if (iter == missing.end())
4849 return false;
4850 if (out)
4851 *out = iter->second;
4852 return true;
4853 }
4854 bool is_missing(const hobject_t& oid, eversion_t v) const override {
4855 std::map<hobject_t, item>::const_iterator m =
4856 missing.find(oid);
4857 if (m == missing.end())
4858 return false;
4859 const item &item(m->second);
4860 if (item.need > v)
4861 return false;
4862 return true;
4863 }
4864 eversion_t get_oldest_need() const {
4865 if (missing.empty()) {
4866 return eversion_t();
4867 }
4868 auto it = missing.find(rmissing.begin()->second);
4869 ceph_assert(it != missing.end());
4870 return it->second.need;
4871 }
4872
4873 void claim(pg_missing_set&& o) {
4874 static_assert(!TrackChanges, "Can't use claim with TrackChanges");
4875 missing = std::move(o.missing);
4876 rmissing = std::move(o.rmissing);
4877 }
4878
4879 /*
4880 * this needs to be called in log order as we extend the log. it
4881 * assumes missing is accurate up through the previous log entry.
4882 */
4883 void add_next_event(const pg_log_entry_t& e) {
4884 std::map<hobject_t, item>::iterator missing_it;
4885 missing_it = missing.find(e.soid);
4886 bool is_missing_divergent_item = missing_it != missing.end();
4887 if (e.prior_version == eversion_t() || e.is_clone()) {
4888 // new object.
4889 if (is_missing_divergent_item) { // use iterator
4890 rmissing.erase(missing_it->second.need.version);
4891 // .have = nil
4892 missing_it->second = item(e.version, eversion_t(), e.is_delete());
4893 missing_it->second.clean_regions.mark_fully_dirty();
4894 } else {
4895 // create new element in missing map
4896 // .have = nil
4897 missing[e.soid] = item(e.version, eversion_t(), e.is_delete());
4898 missing[e.soid].clean_regions.mark_fully_dirty();
4899 }
4900 } else if (is_missing_divergent_item) {
4901 // already missing (prior).
4902 rmissing.erase((missing_it->second).need.version);
4903 missing_it->second.need = e.version; // leave .have unchanged.
4904 missing_it->second.set_delete(e.is_delete());
4905 if (e.is_lost_revert())
4906 missing_it->second.clean_regions.mark_fully_dirty();
4907 else
4908 missing_it->second.clean_regions.merge(e.clean_regions);
4909 } else {
4910 // not missing, we must have prior_version (if any)
4911 ceph_assert(!is_missing_divergent_item);
4912 missing[e.soid] = item(e.version, e.prior_version, e.is_delete());
4913 if (e.is_lost_revert())
4914 missing[e.soid].clean_regions.mark_fully_dirty();
4915 else
4916 missing[e.soid].clean_regions = e.clean_regions;
4917 }
4918 rmissing[e.version.version] = e.soid;
4919 tracker.changed(e.soid);
4920 }
4921
4922 void revise_need(hobject_t oid, eversion_t need, bool is_delete) {
4923 auto p = missing.find(oid);
4924 if (p != missing.end()) {
4925 rmissing.erase((p->second).need.version);
4926 p->second.need = need; // do not adjust .have
4927 p->second.set_delete(is_delete);
4928 p->second.clean_regions.mark_fully_dirty();
4929 } else {
4930 missing[oid] = item(need, eversion_t(), is_delete);
4931 missing[oid].clean_regions.mark_fully_dirty();
4932 }
4933 rmissing[need.version] = oid;
4934
4935 tracker.changed(oid);
4936 }
4937
4938 void revise_have(hobject_t oid, eversion_t have) {
4939 auto p = missing.find(oid);
4940 if (p != missing.end()) {
4941 tracker.changed(oid);
4942 (p->second).have = have;
4943 }
4944 }
4945
4946 void mark_fully_dirty(const hobject_t& oid) {
4947 auto p = missing.find(oid);
4948 if (p != missing.end()) {
4949 tracker.changed(oid);
4950 (p->second).clean_regions.mark_fully_dirty();
4951 }
4952 }
4953
4954 void add(const hobject_t& oid, eversion_t need, eversion_t have,
4955 bool is_delete) {
4956 missing[oid] = item(need, have, is_delete, true);
4957 rmissing[need.version] = oid;
4958 tracker.changed(oid);
4959 }
4960
4961 void add(const hobject_t& oid, pg_missing_item&& item) {
4962 rmissing[item.need.version] = oid;
4963 missing.insert({oid, std::move(item)});
4964 tracker.changed(oid);
4965 }
4966
4967 void rm(const hobject_t& oid, eversion_t v) {
4968 std::map<hobject_t, item>::iterator p = missing.find(oid);
4969 if (p != missing.end() && p->second.need <= v)
4970 rm(p);
4971 }
4972
4973 void rm(std::map<hobject_t, item>::const_iterator m) {
4974 tracker.changed(m->first);
4975 rmissing.erase(m->second.need.version);
4976 missing.erase(m);
4977 }
4978
4979 void got(const hobject_t& oid, eversion_t v) {
4980 std::map<hobject_t, item>::iterator p = missing.find(oid);
4981 ceph_assert(p != missing.end());
4982 ceph_assert(p->second.need <= v || p->second.is_delete());
4983 got(p);
4984 }
4985
4986 void got(std::map<hobject_t, item>::const_iterator m) {
4987 tracker.changed(m->first);
4988 rmissing.erase(m->second.need.version);
4989 missing.erase(m);
4990 }
4991
4992 void split_into(
4993 pg_t child_pgid,
4994 unsigned split_bits,
4995 pg_missing_set *omissing) {
4996 omissing->may_include_deletes = may_include_deletes;
4997 unsigned mask = ~((~0)<<split_bits);
4998 for (std::map<hobject_t, item>::iterator i = missing.begin();
4999 i != missing.end();
5000 ) {
5001 if ((i->first.get_hash() & mask) == child_pgid.m_seed) {
5002 omissing->add(i->first, i->second.need, i->second.have,
5003 i->second.is_delete());
5004 rm(i++);
5005 } else {
5006 ++i;
5007 }
5008 }
5009 }
5010
5011 void clear() {
5012 for (auto const &i: missing)
5013 tracker.changed(i.first);
5014 missing.clear();
5015 rmissing.clear();
5016 }
5017
5018 void encode(ceph::buffer::list &bl, uint64_t features) const {
5019 ENCODE_START(5, 2, bl)
5020 encode(missing, bl, features);
5021 encode(may_include_deletes, bl);
5022 ENCODE_FINISH(bl);
5023 }
5024 void decode(ceph::buffer::list::const_iterator &bl, int64_t pool = -1) {
5025 for (auto const &i: missing)
5026 tracker.changed(i.first);
5027 DECODE_START_LEGACY_COMPAT_LEN(5, 2, 2, bl);
5028 decode(missing, bl);
5029 if (struct_v >= 4) {
5030 decode(may_include_deletes, bl);
5031 }
5032 DECODE_FINISH(bl);
5033
5034 if (struct_v < 3) {
5035 // Handle hobject_t upgrade
5036 std::map<hobject_t, item> tmp;
5037 for (std::map<hobject_t, item>::iterator i =
5038 missing.begin();
5039 i != missing.end();
5040 ) {
5041 if (!i->first.is_max() && i->first.pool == -1) {
5042 hobject_t to_insert(i->first);
5043 to_insert.pool = pool;
5044 tmp[to_insert] = i->second;
5045 missing.erase(i++);
5046 } else {
5047 ++i;
5048 }
5049 }
5050 missing.insert(tmp.begin(), tmp.end());
5051 }
5052
5053 for (std::map<hobject_t,item>::iterator it =
5054 missing.begin();
5055 it != missing.end();
5056 ++it)
5057 rmissing[it->second.need.version] = it->first;
5058 for (auto const &i: missing)
5059 tracker.changed(i.first);
5060 }
5061 void dump(ceph::Formatter *f) const {
5062 f->open_array_section("missing");
5063 for (std::map<hobject_t,item>::const_iterator p =
5064 missing.begin(); p != missing.end(); ++p) {
5065 f->open_object_section("item");
5066 f->dump_stream("object") << p->first;
5067 p->second.dump(f);
5068 f->close_section();
5069 }
5070 f->close_section();
5071 f->dump_bool("may_include_deletes", may_include_deletes);
5072 }
5073 template <typename F>
5074 void filter_objects(F &&f) {
5075 for (auto i = missing.begin(); i != missing.end();) {
5076 if (f(i->first)) {
5077 rm(i++);
5078 } else {
5079 ++i;
5080 }
5081 }
5082 }
5083 static void generate_test_instances(std::list<pg_missing_set*>& o) {
5084 o.push_back(new pg_missing_set);
5085 o.back()->may_include_deletes = true;
5086 o.push_back(new pg_missing_set);
5087 o.back()->add(
5088 hobject_t(object_t("foo"), "foo", 123, 456, 0, ""),
5089 eversion_t(5, 6), eversion_t(5, 1), false);
5090 o.back()->may_include_deletes = true;
5091 o.push_back(new pg_missing_set);
5092 o.back()->add(
5093 hobject_t(object_t("foo"), "foo", 123, 456, 0, ""),
5094 eversion_t(5, 6), eversion_t(5, 1), true);
5095 o.back()->may_include_deletes = true;
5096 }
5097 template <typename F>
5098 void get_changed(F &&f) const {
5099 tracker.get_changed(f);
5100 }
5101 void flush() {
5102 tracker.flush();
5103 }
5104 bool is_clean() const {
5105 return tracker.is_clean();
5106 }
5107 template <typename missing_t>
5108 bool debug_verify_from_init(
5109 const missing_t &init_missing,
5110 std::ostream *oss) const {
5111 if (!TrackChanges)
5112 return true;
5113 auto check_missing(init_missing.get_items());
5114 tracker.get_changed([&](const hobject_t &hoid) {
5115 check_missing.erase(hoid);
5116 if (missing.count(hoid)) {
5117 check_missing.insert(*(missing.find(hoid)));
5118 }
5119 });
5120 bool ok = true;
5121 if (check_missing.size() != missing.size()) {
5122 if (oss) {
5123 *oss << "Size mismatch, check: " << check_missing.size()
5124 << ", actual: " << missing.size() << "\n";
5125 }
5126 ok = false;
5127 }
5128 for (auto &i: missing) {
5129 if (!check_missing.count(i.first)) {
5130 if (oss)
5131 *oss << "check_missing missing " << i.first << "\n";
5132 ok = false;
5133 } else if (check_missing[i.first] != i.second) {
5134 if (oss)
5135 *oss << "check_missing missing item mismatch on " << i.first
5136 << ", check: " << check_missing[i.first]
5137 << ", actual: " << i.second << "\n";
5138 ok = false;
5139 }
5140 }
5141 if (oss && !ok) {
5142 *oss << "check_missing: " << check_missing << "\n";
5143 std::set<hobject_t> changed;
5144 tracker.get_changed([&](const hobject_t &hoid) { changed.insert(hoid); });
5145 *oss << "changed: " << changed << "\n";
5146 }
5147 return ok;
5148 }
5149 };
5150 template <bool TrackChanges>
5151 void encode(
5152 const pg_missing_set<TrackChanges> &c, ceph::buffer::list &bl, uint64_t features=0) {
5153 ENCODE_DUMP_PRE();
5154 c.encode(bl, features);
5155 ENCODE_DUMP_POST(cl);
5156 }
5157 template <bool TrackChanges>
5158 void decode(pg_missing_set<TrackChanges> &c, ceph::buffer::list::const_iterator &p) {
5159 c.decode(p);
5160 }
5161 template <bool TrackChanges>
5162 std::ostream& operator<<(std::ostream& out, const pg_missing_set<TrackChanges> &missing)
5163 {
5164 out << "missing(" << missing.num_missing()
5165 << " may_include_deletes = " << missing.may_include_deletes;
5166 //if (missing.num_lost()) out << ", " << missing.num_lost() << " lost";
5167 out << ")";
5168 return out;
5169 }
5170
5171 using pg_missing_t = pg_missing_set<false>;
5172 using pg_missing_tracker_t = pg_missing_set<true>;
5173
5174
5175
5176
5177 /**
5178 * pg list objects response format
5179 *
5180 */
5181
5182 template<typename T>
5183 struct pg_nls_response_template {
5184 collection_list_handle_t handle;
5185 std::vector<T> entries;
5186
5187 void encode(ceph::buffer::list& bl) const {
5188 ENCODE_START(1, 1, bl);
5189 encode(handle, bl);
5190 __u32 n = (__u32)entries.size();
5191 encode(n, bl);
5192 for (auto i = entries.begin(); i != entries.end(); ++i) {
5193 encode(i->nspace, bl);
5194 encode(i->oid, bl);
5195 encode(i->locator, bl);
5196 }
5197 ENCODE_FINISH(bl);
5198 }
5199 void decode(ceph::buffer::list::const_iterator& bl) {
5200 DECODE_START(1, bl);
5201 decode(handle, bl);
5202 __u32 n;
5203 decode(n, bl);
5204 entries.clear();
5205 while (n--) {
5206 T i;
5207 decode(i.nspace, bl);
5208 decode(i.oid, bl);
5209 decode(i.locator, bl);
5210 entries.push_back(i);
5211 }
5212 DECODE_FINISH(bl);
5213 }
5214 void dump(ceph::Formatter *f) const {
5215 f->dump_stream("handle") << handle;
5216 f->open_array_section("entries");
5217 for (auto p = entries.begin(); p != entries.end(); ++p) {
5218 f->open_object_section("object");
5219 f->dump_string("namespace", p->nspace);
5220 f->dump_string("object", p->oid);
5221 f->dump_string("key", p->locator);
5222 f->close_section();
5223 }
5224 f->close_section();
5225 }
5226 static void generate_test_instances(std::list<pg_nls_response_template<T>*>& o) {
5227 o.push_back(new pg_nls_response_template<T>);
5228 o.push_back(new pg_nls_response_template<T>);
5229 o.back()->handle = hobject_t(object_t("hi"), "key", 1, 2, -1, "");
5230 o.back()->entries.push_back(librados::ListObjectImpl("", "one", ""));
5231 o.back()->entries.push_back(librados::ListObjectImpl("", "two", "twokey"));
5232 o.back()->entries.push_back(librados::ListObjectImpl("", "three", ""));
5233 o.push_back(new pg_nls_response_template<T>);
5234 o.back()->handle = hobject_t(object_t("hi"), "key", 3, 4, -1, "");
5235 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1one", ""));
5236 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1two", "n1twokey"));
5237 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1three", ""));
5238 o.push_back(new pg_nls_response_template<T>);
5239 o.back()->handle = hobject_t(object_t("hi"), "key", 5, 6, -1, "");
5240 o.back()->entries.push_back(librados::ListObjectImpl("", "one", ""));
5241 o.back()->entries.push_back(librados::ListObjectImpl("", "two", "twokey"));
5242 o.back()->entries.push_back(librados::ListObjectImpl("", "three", ""));
5243 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1one", ""));
5244 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1two", "n1twokey"));
5245 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1three", ""));
5246 }
5247 };
5248
5249 using pg_nls_response_t = pg_nls_response_template<librados::ListObjectImpl>;
5250
5251 WRITE_CLASS_ENCODER(pg_nls_response_t)
5252
5253 // For backwards compatibility with older OSD requests
5254 struct pg_ls_response_t {
5255 collection_list_handle_t handle;
5256 std::list<std::pair<object_t, std::string> > entries;
5257
5258 void encode(ceph::buffer::list& bl) const {
5259 using ceph::encode;
5260 __u8 v = 1;
5261 encode(v, bl);
5262 encode(handle, bl);
5263 encode(entries, bl);
5264 }
5265 void decode(ceph::buffer::list::const_iterator& bl) {
5266 using ceph::decode;
5267 __u8 v;
5268 decode(v, bl);
5269 ceph_assert(v == 1);
5270 decode(handle, bl);
5271 decode(entries, bl);
5272 }
5273 void dump(ceph::Formatter *f) const {
5274 f->dump_stream("handle") << handle;
5275 f->open_array_section("entries");
5276 for (std::list<std::pair<object_t, std::string> >::const_iterator p = entries.begin(); p != entries.end(); ++p) {
5277 f->open_object_section("object");
5278 f->dump_stream("object") << p->first;
5279 f->dump_string("key", p->second);
5280 f->close_section();
5281 }
5282 f->close_section();
5283 }
5284 static void generate_test_instances(std::list<pg_ls_response_t*>& o) {
5285 o.push_back(new pg_ls_response_t);
5286 o.push_back(new pg_ls_response_t);
5287 o.back()->handle = hobject_t(object_t("hi"), "key", 1, 2, -1, "");
5288 o.back()->entries.push_back(std::make_pair(object_t("one"), std::string()));
5289 o.back()->entries.push_back(std::make_pair(object_t("two"), std::string("twokey")));
5290 }
5291 };
5292
5293 WRITE_CLASS_ENCODER(pg_ls_response_t)
5294
5295 /**
5296 * object_copy_cursor_t
5297 */
5298 struct object_copy_cursor_t {
5299 uint64_t data_offset;
5300 std::string omap_offset;
5301 bool attr_complete;
5302 bool data_complete;
5303 bool omap_complete;
5304
5305 object_copy_cursor_t()
5306 : data_offset(0),
5307 attr_complete(false),
5308 data_complete(false),
5309 omap_complete(false)
5310 {}
5311
5312 bool is_initial() const {
5313 return !attr_complete && data_offset == 0 && omap_offset.empty();
5314 }
5315 bool is_complete() const {
5316 return attr_complete && data_complete && omap_complete;
5317 }
5318
5319 static void generate_test_instances(std::list<object_copy_cursor_t*>& o);
5320 void encode(ceph::buffer::list& bl) const;
5321 void decode(ceph::buffer::list::const_iterator &bl);
5322 void dump(ceph::Formatter *f) const;
5323 };
5324 WRITE_CLASS_ENCODER(object_copy_cursor_t)
5325
5326 /**
5327 * object_copy_data_t
5328 *
5329 * Return data from a copy request. The semantics are a little strange
5330 * as a result of the encoding's heritage.
5331 *
5332 * In particular, the sender unconditionally fills in the cursor (from what
5333 * it receives and sends), the size, and the mtime, but is responsible for
5334 * figuring out whether it should put any data in the attrs, data, or
5335 * omap members (corresponding to xattrs, object data, and the omap entries)
5336 * based on external data (the client includes a max amount to return with
5337 * the copy request). The client then looks into the attrs, data, and/or omap
5338 * based on the contents of the cursor.
5339 */
5340 struct object_copy_data_t {
5341 enum {
5342 FLAG_DATA_DIGEST = 1<<0,
5343 FLAG_OMAP_DIGEST = 1<<1,
5344 };
5345 object_copy_cursor_t cursor;
5346 uint64_t size;
5347 utime_t mtime;
5348 uint32_t data_digest, omap_digest;
5349 uint32_t flags;
5350 std::map<std::string, ceph::buffer::list, std::less<>> attrs;
5351 ceph::buffer::list data;
5352 ceph::buffer::list omap_header;
5353 ceph::buffer::list omap_data;
5354
5355 /// which snaps we are defined for (if a snap and not the head)
5356 std::vector<snapid_t> snaps;
5357 /// latest snap seq for the object (if head)
5358 snapid_t snap_seq;
5359
5360 /// recent reqids on this object
5361 mempool::osd_pglog::vector<std::pair<osd_reqid_t, version_t> > reqids;
5362
5363 /// map reqids by index to error return code (if any)
5364 mempool::osd_pglog::map<uint32_t, int> reqid_return_codes;
5365
5366 uint64_t truncate_seq;
5367 uint64_t truncate_size;
5368
5369 public:
5370 object_copy_data_t() :
5371 size((uint64_t)-1), data_digest(-1),
5372 omap_digest(-1), flags(0),
5373 truncate_seq(0),
5374 truncate_size(0) {}
5375
5376 static void generate_test_instances(std::list<object_copy_data_t*>& o);
5377 void encode(ceph::buffer::list& bl, uint64_t features) const;
5378 void decode(ceph::buffer::list::const_iterator& bl);
5379 void dump(ceph::Formatter *f) const;
5380 };
5381 WRITE_CLASS_ENCODER_FEATURES(object_copy_data_t)
5382
5383 /**
5384 * pg creation info
5385 */
5386 struct pg_create_t {
5387 epoch_t created; // epoch pg created
5388 pg_t parent; // split from parent (if != pg_t())
5389 __s32 split_bits;
5390
5391 pg_create_t()
5392 : created(0), split_bits(0) {}
5393 pg_create_t(unsigned c, pg_t p, int s)
5394 : created(c), parent(p), split_bits(s) {}
5395
5396 void encode(ceph::buffer::list &bl) const;
5397 void decode(ceph::buffer::list::const_iterator &bl);
5398 void dump(ceph::Formatter *f) const;
5399 static void generate_test_instances(std::list<pg_create_t*>& o);
5400 };
5401 WRITE_CLASS_ENCODER(pg_create_t)
5402
5403 // -----------------------------------------
5404
5405 class ObjectExtent {
5406 /**
5407 * ObjectExtents are used for specifying IO behavior against RADOS
5408 * objects when one is using the ObjectCacher.
5409 *
5410 * To use this in a real system, *every member* must be filled
5411 * out correctly. In particular, make sure to initialize the
5412 * oloc correctly, as its default values are deliberate poison
5413 * and will cause internal ObjectCacher asserts.
5414 *
5415 * Similarly, your buffer_extents vector *must* specify a total
5416 * size equal to your length. If the buffer_extents inadvertently
5417 * contain less space than the length member specifies, you
5418 * will get unintelligible asserts deep in the ObjectCacher.
5419 *
5420 * If you are trying to do testing and don't care about actual
5421 * RADOS function, the simplest thing to do is to initialize
5422 * the ObjectExtent (truncate_size can be 0), create a single entry
5423 * in buffer_extents matching the length, and set oloc.pool to 0.
5424 */
5425 public:
5426 object_t oid; // object id
5427 uint64_t objectno;
5428 uint64_t offset; // in object
5429 uint64_t length; // in object
5430 uint64_t truncate_size; // in object
5431
5432 object_locator_t oloc; // object locator (pool etc)
5433
5434 std::vector<std::pair<uint64_t,uint64_t> > buffer_extents; // off -> len. extents in buffer being mapped (may be fragmented bc of striping!)
5435
5436 ObjectExtent() : objectno(0), offset(0), length(0), truncate_size(0) {}
5437 ObjectExtent(object_t o, uint64_t ono, uint64_t off, uint64_t l, uint64_t ts) :
5438 oid(o), objectno(ono), offset(off), length(l), truncate_size(ts) { }
5439 };
5440
5441 inline std::ostream& operator<<(std::ostream& out, const ObjectExtent &ex)
5442 {
5443 return out << "extent("
5444 << ex.oid << " (" << ex.objectno << ") in " << ex.oloc
5445 << " " << ex.offset << "~" << ex.length
5446 << " -> " << ex.buffer_extents
5447 << ")";
5448 }
5449
5450
5451 // ---------------------------------------
5452
5453 class OSDSuperblock {
5454 public:
5455 uuid_d cluster_fsid, osd_fsid;
5456 int32_t whoami = -1; // my role in this fs.
5457 epoch_t current_epoch = 0; // most recent epoch
5458 epoch_t oldest_map = 0, newest_map = 0; // oldest/newest maps we have.
5459 double weight = 0.0;
5460
5461 CompatSet compat_features;
5462
5463 // last interval over which i mounted and was then active
5464 epoch_t mounted = 0; // last epoch i mounted
5465 epoch_t clean_thru = 0; // epoch i was active and clean thru
5466
5467 epoch_t purged_snaps_last = 0;
5468 utime_t last_purged_snaps_scrub;
5469
5470 void encode(ceph::buffer::list &bl) const;
5471 void decode(ceph::buffer::list::const_iterator &bl);
5472 void dump(ceph::Formatter *f) const;
5473 static void generate_test_instances(std::list<OSDSuperblock*>& o);
5474 };
5475 WRITE_CLASS_ENCODER(OSDSuperblock)
5476
5477 inline std::ostream& operator<<(std::ostream& out, const OSDSuperblock& sb)
5478 {
5479 return out << "sb(" << sb.cluster_fsid
5480 << " osd." << sb.whoami
5481 << " " << sb.osd_fsid
5482 << " e" << sb.current_epoch
5483 << " [" << sb.oldest_map << "," << sb.newest_map << "]"
5484 << " lci=[" << sb.mounted << "," << sb.clean_thru << "]"
5485 << ")";
5486 }
5487
5488
5489 // -------
5490
5491
5492
5493
5494
5495
5496 /*
5497 * attached to object head. describes most recent snap context, and
5498 * set of existing clones.
5499 */
5500 struct SnapSet {
5501 snapid_t seq;
5502 // NOTE: this is for pre-octopus compatibility only! remove in Q release
5503 std::vector<snapid_t> snaps; // descending
5504 std::vector<snapid_t> clones; // ascending
5505 std::map<snapid_t, interval_set<uint64_t> > clone_overlap; // overlap w/ next newest
5506 std::map<snapid_t, uint64_t> clone_size;
5507 std::map<snapid_t, std::vector<snapid_t>> clone_snaps; // descending
5508
5509 SnapSet() : seq(0) {}
5510 explicit SnapSet(ceph::buffer::list& bl) {
5511 auto p = std::cbegin(bl);
5512 decode(p);
5513 }
5514
5515 /// populate SnapSet from a librados::snap_set_t
5516 void from_snap_set(const librados::snap_set_t& ss, bool legacy);
5517
5518 /// get space accounted to clone
5519 uint64_t get_clone_bytes(snapid_t clone) const;
5520
5521 void encode(ceph::buffer::list& bl) const;
5522 void decode(ceph::buffer::list::const_iterator& bl);
5523 void dump(ceph::Formatter *f) const;
5524 static void generate_test_instances(std::list<SnapSet*>& o);
5525
5526 SnapContext get_ssc_as_of(snapid_t as_of) const {
5527 SnapContext out;
5528 out.seq = as_of;
5529 for (auto p = clone_snaps.rbegin();
5530 p != clone_snaps.rend();
5531 ++p) {
5532 for (auto snap : p->second) {
5533 if (snap <= as_of) {
5534 out.snaps.push_back(snap);
5535 }
5536 }
5537 }
5538 return out;
5539 }
5540
5541
5542 SnapSet get_filtered(const pg_pool_t &pinfo) const;
5543 void filter(const pg_pool_t &pinfo);
5544 };
5545 WRITE_CLASS_ENCODER(SnapSet)
5546
5547 std::ostream& operator<<(std::ostream& out, const SnapSet& cs);
5548
5549
5550
5551 #define OI_ATTR "_"
5552 #define SS_ATTR "snapset"
5553
5554 struct watch_info_t {
5555 uint64_t cookie;
5556 uint32_t timeout_seconds;
5557 entity_addr_t addr;
5558
5559 watch_info_t() : cookie(0), timeout_seconds(0) { }
5560 watch_info_t(uint64_t c, uint32_t t, const entity_addr_t& a) : cookie(c), timeout_seconds(t), addr(a) {}
5561
5562 void encode(ceph::buffer::list& bl, uint64_t features) const;
5563 void decode(ceph::buffer::list::const_iterator& bl);
5564 void dump(ceph::Formatter *f) const;
5565 static void generate_test_instances(std::list<watch_info_t*>& o);
5566 };
5567 WRITE_CLASS_ENCODER_FEATURES(watch_info_t)
5568
5569 static inline bool operator==(const watch_info_t& l, const watch_info_t& r) {
5570 return l.cookie == r.cookie && l.timeout_seconds == r.timeout_seconds
5571 && l.addr == r.addr;
5572 }
5573
5574 static inline std::ostream& operator<<(std::ostream& out, const watch_info_t& w) {
5575 return out << "watch(cookie " << w.cookie << " " << w.timeout_seconds << "s"
5576 << " " << w.addr << ")";
5577 }
5578
5579 struct notify_info_t {
5580 uint64_t cookie;
5581 uint64_t notify_id;
5582 uint32_t timeout;
5583 ceph::buffer::list bl;
5584 };
5585
5586 static inline std::ostream& operator<<(std::ostream& out, const notify_info_t& n) {
5587 return out << "notify(cookie " << n.cookie
5588 << " notify" << n.notify_id
5589 << " " << n.timeout << "s)";
5590 }
5591
5592 class object_ref_delta_t {
5593 std::map<hobject_t, int> ref_delta;
5594
5595 public:
5596 object_ref_delta_t() = default;
5597 object_ref_delta_t(const object_ref_delta_t &) = default;
5598 object_ref_delta_t(object_ref_delta_t &&) = default;
5599
5600 object_ref_delta_t(decltype(ref_delta) &&ref_delta)
5601 : ref_delta(std::move(ref_delta)) {}
5602 object_ref_delta_t(const decltype(ref_delta) &ref_delta)
5603 : ref_delta(ref_delta) {}
5604
5605 object_ref_delta_t &operator=(const object_ref_delta_t &) = default;
5606 object_ref_delta_t &operator=(object_ref_delta_t &&) = default;
5607
5608 void dec_ref(const hobject_t &hoid, unsigned num=1) {
5609 mut_ref(hoid, -num);
5610 }
5611 void inc_ref(const hobject_t &hoid, unsigned num=1) {
5612 mut_ref(hoid, num);
5613 }
5614 void mut_ref(const hobject_t &hoid, int num) {
5615 [[maybe_unused]] auto [iter, _] = ref_delta.try_emplace(hoid, 0);
5616 iter->second += num;
5617 if (iter->second == 0)
5618 ref_delta.erase(iter);
5619 }
5620
5621 auto begin() const { return ref_delta.begin(); }
5622 auto end() const { return ref_delta.end(); }
5623 auto find(hobject_t &key) const { return ref_delta.find(key); }
5624
5625 bool operator==(const object_ref_delta_t &rhs) const {
5626 return ref_delta == rhs.ref_delta;
5627 }
5628 bool operator!=(const object_ref_delta_t &rhs) const {
5629 return !(*this == rhs);
5630 }
5631 bool is_empty() {
5632 return ref_delta.empty();
5633 }
5634 uint64_t size() {
5635 return ref_delta.size();
5636 }
5637 friend std::ostream& operator<<(std::ostream& out, const object_ref_delta_t & ci);
5638 };
5639
5640 struct chunk_info_t {
5641 typedef enum {
5642 FLAG_DIRTY = 1,
5643 FLAG_MISSING = 2,
5644 FLAG_HAS_REFERENCE = 4,
5645 FLAG_HAS_FINGERPRINT = 8,
5646 } cflag_t;
5647 uint32_t offset;
5648 uint32_t length;
5649 hobject_t oid;
5650 cflag_t flags; // FLAG_*
5651
5652 chunk_info_t() : offset(0), length(0), flags((cflag_t)0) { }
5653 chunk_info_t(uint32_t offset, uint32_t length, hobject_t oid) :
5654 offset(offset), length(length), oid(oid), flags((cflag_t)0) { }
5655
5656 static std::string get_flag_string(uint64_t flags) {
5657 std::string r;
5658 if (flags & FLAG_DIRTY) {
5659 r += "|dirty";
5660 }
5661 if (flags & FLAG_MISSING) {
5662 r += "|missing";
5663 }
5664 if (flags & FLAG_HAS_REFERENCE) {
5665 r += "|has_reference";
5666 }
5667 if (flags & FLAG_HAS_FINGERPRINT) {
5668 r += "|has_fingerprint";
5669 }
5670 if (r.length())
5671 return r.substr(1);
5672 return r;
5673 }
5674 bool test_flag(cflag_t f) const {
5675 return (flags & f) == f;
5676 }
5677 void set_flag(cflag_t f) {
5678 flags = (cflag_t)(flags | f);
5679 }
5680 void set_flags(cflag_t f) {
5681 flags = f;
5682 }
5683 void clear_flag(cflag_t f) {
5684 flags = (cflag_t)(flags & ~f);
5685 }
5686 void clear_flags() {
5687 flags = (cflag_t)0;
5688 }
5689 bool is_dirty() const {
5690 return test_flag(FLAG_DIRTY);
5691 }
5692 bool is_missing() const {
5693 return test_flag(FLAG_MISSING);
5694 }
5695 bool has_reference() const {
5696 return test_flag(FLAG_HAS_REFERENCE);
5697 }
5698 bool has_fingerprint() const {
5699 return test_flag(FLAG_HAS_FINGERPRINT);
5700 }
5701 void encode(ceph::buffer::list &bl) const;
5702 void decode(ceph::buffer::list::const_iterator &bl);
5703 void dump(ceph::Formatter *f) const;
5704 friend std::ostream& operator<<(std::ostream& out, const chunk_info_t& ci);
5705 bool operator==(const chunk_info_t& cit) const;
5706 bool operator!=(const chunk_info_t& cit) const {
5707 return !(cit == *this);
5708 }
5709 };
5710 WRITE_CLASS_ENCODER(chunk_info_t)
5711 std::ostream& operator<<(std::ostream& out, const chunk_info_t& ci);
5712
5713 struct object_info_t;
5714 struct object_manifest_t {
5715 enum {
5716 TYPE_NONE = 0,
5717 TYPE_REDIRECT = 1,
5718 TYPE_CHUNKED = 2,
5719 };
5720 uint8_t type; // redirect, chunked, ...
5721 hobject_t redirect_target;
5722 std::map<uint64_t, chunk_info_t> chunk_map;
5723
5724 object_manifest_t() : type(0) { }
5725 object_manifest_t(uint8_t type, const hobject_t& redirect_target)
5726 : type(type), redirect_target(redirect_target) { }
5727
5728 bool is_empty() const {
5729 return type == TYPE_NONE;
5730 }
5731 bool is_redirect() const {
5732 return type == TYPE_REDIRECT;
5733 }
5734 bool is_chunked() const {
5735 return type == TYPE_CHUNKED;
5736 }
5737 static std::string_view get_type_name(uint8_t m) {
5738 switch (m) {
5739 case TYPE_NONE: return "none";
5740 case TYPE_REDIRECT: return "redirect";
5741 case TYPE_CHUNKED: return "chunked";
5742 default: return "unknown";
5743 }
5744 }
5745 std::string_view get_type_name() const {
5746 return get_type_name(type);
5747 }
5748 void clear() {
5749 type = 0;
5750 redirect_target = hobject_t();
5751 chunk_map.clear();
5752 }
5753
5754 /**
5755 * calc_refs_to_inc_on_set
5756 *
5757 * Takes a manifest and returns the set of refs to
5758 * increment upon set-chunk
5759 *
5760 * l should be nullptr if there are no clones, or
5761 * l and g may each be null if the corresponding clone does not exist.
5762 * *this contains the set of new references to set
5763 *
5764 */
5765 void calc_refs_to_inc_on_set(
5766 const object_manifest_t* g, ///< [in] manifest for clone > *this
5767 const object_manifest_t* l, ///< [in] manifest for clone < *this
5768 object_ref_delta_t &delta ///< [out] set of refs to drop
5769 ) const;
5770
5771 /**
5772 * calc_refs_to_drop_on_modify
5773 *
5774 * Takes a manifest and returns the set of refs to
5775 * drop upon modification
5776 *
5777 * l should be nullptr if there are no clones, or
5778 * l may be null if the corresponding clone does not exist.
5779 *
5780 */
5781 void calc_refs_to_drop_on_modify(
5782 const object_manifest_t* l, ///< [in] manifest for previous clone
5783 const ObjectCleanRegions& clean_regions, ///< [in] clean regions
5784 object_ref_delta_t &delta ///< [out] set of refs to drop
5785 ) const;
5786
5787 /**
5788 * calc_refs_to_drop_on_removal
5789 *
5790 * Takes the two adjacent manifests and returns the set of refs to
5791 * drop upon removal of the clone containing *this.
5792 *
5793 * g should be nullptr if *this is on HEAD, l should be nullptr if
5794 * *this is on the oldest clone (or head if there are no clones).
5795 */
5796 void calc_refs_to_drop_on_removal(
5797 const object_manifest_t* g, ///< [in] manifest for clone > *this
5798 const object_manifest_t* l, ///< [in] manifest for clone < *this
5799 object_ref_delta_t &delta ///< [out] set of refs to drop
5800 ) const;
5801
5802 static void generate_test_instances(std::list<object_manifest_t*>& o);
5803 void encode(ceph::buffer::list &bl) const;
5804 void decode(ceph::buffer::list::const_iterator &bl);
5805 void dump(ceph::Formatter *f) const;
5806 friend std::ostream& operator<<(std::ostream& out, const object_info_t& oi);
5807 };
5808 WRITE_CLASS_ENCODER(object_manifest_t)
5809 std::ostream& operator<<(std::ostream& out, const object_manifest_t& oi);
5810
5811 struct object_info_t {
5812 hobject_t soid;
5813 eversion_t version, prior_version;
5814 version_t user_version;
5815 osd_reqid_t last_reqid;
5816
5817 uint64_t size;
5818 utime_t mtime;
5819 utime_t local_mtime; // local mtime
5820
5821 // note: these are currently encoded into a total 16 bits; see
5822 // encode()/decode() for the weirdness.
5823 typedef enum {
5824 FLAG_LOST = 1<<0,
5825 FLAG_WHITEOUT = 1<<1, // object logically does not exist
5826 FLAG_DIRTY = 1<<2, // object has been modified since last flushed or undirtied
5827 FLAG_OMAP = 1<<3, // has (or may have) some/any omap data
5828 FLAG_DATA_DIGEST = 1<<4, // has data crc
5829 FLAG_OMAP_DIGEST = 1<<5, // has omap crc
5830 FLAG_CACHE_PIN = 1<<6, // pin the object in cache tier
5831 FLAG_MANIFEST = 1<<7, // has manifest
5832 FLAG_USES_TMAP = 1<<8, // deprecated; no longer used
5833 FLAG_REDIRECT_HAS_REFERENCE = 1<<9, // has reference
5834 } flag_t;
5835
5836 flag_t flags;
5837
5838 static std::string get_flag_string(flag_t flags) {
5839 std::string s;
5840 std::vector<std::string> sv = get_flag_vector(flags);
5841 for (auto ss : sv) {
5842 s += std::string("|") + ss;
5843 }
5844 if (s.length())
5845 return s.substr(1);
5846 return s;
5847 }
5848 static std::vector<std::string> get_flag_vector(flag_t flags) {
5849 std::vector<std::string> sv;
5850 if (flags & FLAG_LOST)
5851 sv.insert(sv.end(), "lost");
5852 if (flags & FLAG_WHITEOUT)
5853 sv.insert(sv.end(), "whiteout");
5854 if (flags & FLAG_DIRTY)
5855 sv.insert(sv.end(), "dirty");
5856 if (flags & FLAG_USES_TMAP)
5857 sv.insert(sv.end(), "uses_tmap");
5858 if (flags & FLAG_OMAP)
5859 sv.insert(sv.end(), "omap");
5860 if (flags & FLAG_DATA_DIGEST)
5861 sv.insert(sv.end(), "data_digest");
5862 if (flags & FLAG_OMAP_DIGEST)
5863 sv.insert(sv.end(), "omap_digest");
5864 if (flags & FLAG_CACHE_PIN)
5865 sv.insert(sv.end(), "cache_pin");
5866 if (flags & FLAG_MANIFEST)
5867 sv.insert(sv.end(), "manifest");
5868 if (flags & FLAG_REDIRECT_HAS_REFERENCE)
5869 sv.insert(sv.end(), "redirect_has_reference");
5870 return sv;
5871 }
5872 std::string get_flag_string() const {
5873 return get_flag_string(flags);
5874 }
5875
5876 uint64_t truncate_seq, truncate_size;
5877
5878 std::map<std::pair<uint64_t, entity_name_t>, watch_info_t> watchers;
5879
5880 // opportunistic checksums; may or may not be present
5881 __u32 data_digest; ///< data crc32c
5882 __u32 omap_digest; ///< omap crc32c
5883
5884 // alloc hint attribute
5885 uint64_t expected_object_size, expected_write_size;
5886 uint32_t alloc_hint_flags;
5887
5888 struct object_manifest_t manifest;
5889
5890 void copy_user_bits(const object_info_t& other);
5891
5892 bool test_flag(flag_t f) const {
5893 return (flags & f) == f;
5894 }
5895 void set_flag(flag_t f) {
5896 flags = (flag_t)(flags | f);
5897 }
5898 void clear_flag(flag_t f) {
5899 flags = (flag_t)(flags & ~f);
5900 }
5901 bool is_lost() const {
5902 return test_flag(FLAG_LOST);
5903 }
5904 bool is_whiteout() const {
5905 return test_flag(FLAG_WHITEOUT);
5906 }
5907 bool is_dirty() const {
5908 return test_flag(FLAG_DIRTY);
5909 }
5910 bool is_omap() const {
5911 return test_flag(FLAG_OMAP);
5912 }
5913 bool is_data_digest() const {
5914 return test_flag(FLAG_DATA_DIGEST);
5915 }
5916 bool is_omap_digest() const {
5917 return test_flag(FLAG_OMAP_DIGEST);
5918 }
5919 bool is_cache_pinned() const {
5920 return test_flag(FLAG_CACHE_PIN);
5921 }
5922 bool has_manifest() const {
5923 return test_flag(FLAG_MANIFEST);
5924 }
5925 void set_data_digest(__u32 d) {
5926 set_flag(FLAG_DATA_DIGEST);
5927 data_digest = d;
5928 }
5929 void set_omap_digest(__u32 d) {
5930 set_flag(FLAG_OMAP_DIGEST);
5931 omap_digest = d;
5932 }
5933 void clear_data_digest() {
5934 clear_flag(FLAG_DATA_DIGEST);
5935 data_digest = -1;
5936 }
5937 void clear_omap_digest() {
5938 clear_flag(FLAG_OMAP_DIGEST);
5939 omap_digest = -1;
5940 }
5941 void new_object() {
5942 clear_data_digest();
5943 clear_omap_digest();
5944 }
5945
5946 void encode(ceph::buffer::list& bl, uint64_t features) const;
5947 void decode(ceph::buffer::list::const_iterator& bl);
5948 void decode(const ceph::buffer::list& bl) {
5949 auto p = std::cbegin(bl);
5950 decode(p);
5951 }
5952
5953 void encode_no_oid(ceph::buffer::list& bl, uint64_t features) {
5954 // TODO: drop soid field and remove the denc no_oid methods
5955 auto tmp_oid = hobject_t(hobject_t::get_max());
5956 tmp_oid.swap(soid);
5957 encode(bl, features);
5958 soid = tmp_oid;
5959 }
5960 void decode_no_oid(ceph::buffer::list::const_iterator& bl) {
5961 decode(bl);
5962 ceph_assert(soid.is_max());
5963 }
5964 void decode_no_oid(const ceph::buffer::list& bl) {
5965 auto p = std::cbegin(bl);
5966 decode_no_oid(p);
5967 }
5968 void decode_no_oid(const ceph::buffer::list& bl, const hobject_t& _soid) {
5969 auto p = std::cbegin(bl);
5970 decode_no_oid(p);
5971 soid = _soid;
5972 }
5973
5974 void dump(ceph::Formatter *f) const;
5975 static void generate_test_instances(std::list<object_info_t*>& o);
5976
5977 explicit object_info_t()
5978 : user_version(0), size(0), flags((flag_t)0),
5979 truncate_seq(0), truncate_size(0),
5980 data_digest(-1), omap_digest(-1),
5981 expected_object_size(0), expected_write_size(0),
5982 alloc_hint_flags(0)
5983 {}
5984
5985 explicit object_info_t(const hobject_t& s)
5986 : soid(s),
5987 user_version(0), size(0), flags((flag_t)0),
5988 truncate_seq(0), truncate_size(0),
5989 data_digest(-1), omap_digest(-1),
5990 expected_object_size(0), expected_write_size(0),
5991 alloc_hint_flags(0)
5992 {}
5993
5994 explicit object_info_t(const ceph::buffer::list& bl) {
5995 decode(bl);
5996 }
5997
5998 explicit object_info_t(const ceph::buffer::list& bl, const hobject_t& _soid) {
5999 decode_no_oid(bl);
6000 soid = _soid;
6001 }
6002 };
6003 WRITE_CLASS_ENCODER_FEATURES(object_info_t)
6004
6005 std::ostream& operator<<(std::ostream& out, const object_info_t& oi);
6006
6007
6008
6009 // Object recovery
6010 struct ObjectRecoveryInfo {
6011 hobject_t soid;
6012 eversion_t version;
6013 uint64_t size;
6014 object_info_t oi;
6015 SnapSet ss; // only populated if soid is_snap()
6016 interval_set<uint64_t> copy_subset;
6017 std::map<hobject_t, interval_set<uint64_t>> clone_subset;
6018 bool object_exist;
6019
6020 ObjectRecoveryInfo() : size(0), object_exist(true) { }
6021
6022 static void generate_test_instances(std::list<ObjectRecoveryInfo*>& o);
6023 void encode(ceph::buffer::list &bl, uint64_t features) const;
6024 void decode(ceph::buffer::list::const_iterator &bl, int64_t pool = -1);
6025 std::ostream &print(std::ostream &out) const;
6026 void dump(ceph::Formatter *f) const;
6027 };
6028 WRITE_CLASS_ENCODER_FEATURES(ObjectRecoveryInfo)
6029 std::ostream& operator<<(std::ostream& out, const ObjectRecoveryInfo &inf);
6030
6031 struct ObjectRecoveryProgress {
6032 uint64_t data_recovered_to;
6033 std::string omap_recovered_to;
6034 bool first;
6035 bool data_complete;
6036 bool omap_complete;
6037 bool error = false;
6038
6039 ObjectRecoveryProgress()
6040 : data_recovered_to(0),
6041 first(true),
6042 data_complete(false), omap_complete(false) { }
6043
6044 bool is_complete(const ObjectRecoveryInfo& info) const {
6045 return (data_recovered_to >= (
6046 info.copy_subset.empty() ?
6047 0 : info.copy_subset.range_end())) &&
6048 omap_complete;
6049 }
6050
6051 static void generate_test_instances(std::list<ObjectRecoveryProgress*>& o);
6052 void encode(ceph::buffer::list &bl) const;
6053 void decode(ceph::buffer::list::const_iterator &bl);
6054 std::ostream &print(std::ostream &out) const;
6055 void dump(ceph::Formatter *f) const;
6056 };
6057 WRITE_CLASS_ENCODER(ObjectRecoveryProgress)
6058 std::ostream& operator<<(std::ostream& out, const ObjectRecoveryProgress &prog);
6059
6060 struct PushReplyOp {
6061 hobject_t soid;
6062
6063 static void generate_test_instances(std::list<PushReplyOp*>& o);
6064 void encode(ceph::buffer::list &bl) const;
6065 void decode(ceph::buffer::list::const_iterator &bl);
6066 std::ostream &print(std::ostream &out) const;
6067 void dump(ceph::Formatter *f) const;
6068
6069 uint64_t cost(CephContext *cct) const;
6070 };
6071 WRITE_CLASS_ENCODER(PushReplyOp)
6072 std::ostream& operator<<(std::ostream& out, const PushReplyOp &op);
6073
6074 struct PullOp {
6075 hobject_t soid;
6076
6077 ObjectRecoveryInfo recovery_info;
6078 ObjectRecoveryProgress recovery_progress;
6079
6080 static void generate_test_instances(std::list<PullOp*>& o);
6081 void encode(ceph::buffer::list &bl, uint64_t features) const;
6082 void decode(ceph::buffer::list::const_iterator &bl);
6083 std::ostream &print(std::ostream &out) const;
6084 void dump(ceph::Formatter *f) const;
6085
6086 uint64_t cost(CephContext *cct) const;
6087 };
6088 WRITE_CLASS_ENCODER_FEATURES(PullOp)
6089 std::ostream& operator<<(std::ostream& out, const PullOp &op);
6090
6091 struct PushOp {
6092 hobject_t soid;
6093 eversion_t version;
6094 ceph::buffer::list data;
6095 interval_set<uint64_t> data_included;
6096 ceph::buffer::list omap_header;
6097 std::map<std::string, ceph::buffer::list> omap_entries;
6098 std::map<std::string, ceph::buffer::list, std::less<>> attrset;
6099
6100 ObjectRecoveryInfo recovery_info;
6101 ObjectRecoveryProgress before_progress;
6102 ObjectRecoveryProgress after_progress;
6103
6104 static void generate_test_instances(std::list<PushOp*>& o);
6105 void encode(ceph::buffer::list &bl, uint64_t features) const;
6106 void decode(ceph::buffer::list::const_iterator &bl);
6107 std::ostream &print(std::ostream &out) const;
6108 void dump(ceph::Formatter *f) const;
6109
6110 uint64_t cost(CephContext *cct) const;
6111 };
6112 WRITE_CLASS_ENCODER_FEATURES(PushOp)
6113 std::ostream& operator<<(std::ostream& out, const PushOp &op);
6114
6115 /*
6116 * summarize pg contents for purposes of a scrub
6117 */
6118 struct ScrubMap {
6119 struct object {
6120 std::map<std::string, ceph::buffer::ptr, std::less<>> attrs;
6121 uint64_t size;
6122 __u32 omap_digest; ///< omap crc32c
6123 __u32 digest; ///< data crc32c
6124 bool negative:1;
6125 bool digest_present:1;
6126 bool omap_digest_present:1;
6127 bool read_error:1;
6128 bool stat_error:1;
6129 bool ec_hash_mismatch:1;
6130 bool ec_size_mismatch:1;
6131 bool large_omap_object_found:1;
6132 uint64_t large_omap_object_key_count = 0;
6133 uint64_t large_omap_object_value_size = 0;
6134 uint64_t object_omap_bytes = 0;
6135 uint64_t object_omap_keys = 0;
6136
6137 object() :
6138 // Init invalid size so it won't match if we get a stat EIO error
6139 size(-1), omap_digest(0), digest(0),
6140 negative(false), digest_present(false), omap_digest_present(false),
6141 read_error(false), stat_error(false), ec_hash_mismatch(false),
6142 ec_size_mismatch(false), large_omap_object_found(false) {}
6143
6144 void encode(ceph::buffer::list& bl) const;
6145 void decode(ceph::buffer::list::const_iterator& bl);
6146 void dump(ceph::Formatter *f) const;
6147 static void generate_test_instances(std::list<object*>& o);
6148 };
6149 WRITE_CLASS_ENCODER(object)
6150
6151 std::map<hobject_t,object> objects;
6152 eversion_t valid_through;
6153 eversion_t incr_since;
6154 bool has_large_omap_object_errors:1;
6155 bool has_omap_keys:1;
6156
6157 void merge_incr(const ScrubMap &l);
6158 void clear_from(const hobject_t& start) {
6159 objects.erase(objects.lower_bound(start), objects.end());
6160 }
6161 void insert(const ScrubMap &r) {
6162 objects.insert(r.objects.begin(), r.objects.end());
6163 }
6164 void swap(ScrubMap &r) {
6165 using std::swap;
6166 swap(objects, r.objects);
6167 swap(valid_through, r.valid_through);
6168 swap(incr_since, r.incr_since);
6169 }
6170
6171 void encode(ceph::buffer::list& bl) const;
6172 void decode(ceph::buffer::list::const_iterator& bl, int64_t pool=-1);
6173 void dump(ceph::Formatter *f) const;
6174 static void generate_test_instances(std::list<ScrubMap*>& o);
6175 };
6176 WRITE_CLASS_ENCODER(ScrubMap::object)
6177 WRITE_CLASS_ENCODER(ScrubMap)
6178
6179 struct ScrubMapBuilder {
6180 bool deep = false;
6181 std::vector<hobject_t> ls;
6182 size_t pos = 0;
6183 int64_t data_pos = 0;
6184 std::string omap_pos;
6185 int ret = 0;
6186 ceph::buffer::hash data_hash, omap_hash; ///< accumulatinng hash value
6187 uint64_t omap_keys = 0;
6188 uint64_t omap_bytes = 0;
6189
6190 bool empty() {
6191 return ls.empty();
6192 }
6193 bool done() {
6194 return pos >= ls.size();
6195 }
6196 void reset() {
6197 *this = ScrubMapBuilder();
6198 }
6199
6200 bool data_done() {
6201 return data_pos < 0;
6202 }
6203
6204 void next_object() {
6205 ++pos;
6206 data_pos = 0;
6207 omap_pos.clear();
6208 omap_keys = 0;
6209 omap_bytes = 0;
6210 }
6211
6212 friend std::ostream& operator<<(std::ostream& out, const ScrubMapBuilder& pos) {
6213 out << "(" << pos.pos << "/" << pos.ls.size();
6214 if (pos.pos < pos.ls.size()) {
6215 out << " " << pos.ls[pos.pos];
6216 }
6217 if (pos.data_pos < 0) {
6218 out << " byte " << pos.data_pos;
6219 }
6220 if (!pos.omap_pos.empty()) {
6221 out << " key " << pos.omap_pos;
6222 }
6223 if (pos.deep) {
6224 out << " deep";
6225 }
6226 if (pos.ret) {
6227 out << " ret " << pos.ret;
6228 }
6229 return out << ")";
6230 }
6231 };
6232
6233 struct watch_item_t {
6234 entity_name_t name;
6235 uint64_t cookie;
6236 uint32_t timeout_seconds;
6237 entity_addr_t addr;
6238
6239 watch_item_t() : cookie(0), timeout_seconds(0) { }
6240 watch_item_t(entity_name_t name, uint64_t cookie, uint32_t timeout,
6241 const entity_addr_t& addr)
6242 : name(name), cookie(cookie), timeout_seconds(timeout),
6243 addr(addr) { }
6244
6245 void encode(ceph::buffer::list &bl, uint64_t features) const {
6246 ENCODE_START(2, 1, bl);
6247 encode(name, bl);
6248 encode(cookie, bl);
6249 encode(timeout_seconds, bl);
6250 encode(addr, bl, features);
6251 ENCODE_FINISH(bl);
6252 }
6253 void decode(ceph::buffer::list::const_iterator &bl) {
6254 DECODE_START(2, bl);
6255 decode(name, bl);
6256 decode(cookie, bl);
6257 decode(timeout_seconds, bl);
6258 if (struct_v >= 2) {
6259 decode(addr, bl);
6260 }
6261 DECODE_FINISH(bl);
6262 }
6263 void dump(ceph::Formatter *f) const {
6264 f->dump_stream("watcher") << name;
6265 f->dump_int("cookie", cookie);
6266 f->dump_int("timeout", timeout_seconds);
6267 f->open_object_section("addr");
6268 addr.dump(f);
6269 f->close_section();
6270 }
6271 static void generate_test_instances(std::list<watch_item_t*>& o) {
6272 entity_addr_t ea;
6273 ea.set_type(entity_addr_t::TYPE_LEGACY);
6274 ea.set_nonce(1000);
6275 ea.set_family(AF_INET);
6276 ea.set_in4_quad(0, 127);
6277 ea.set_in4_quad(1, 0);
6278 ea.set_in4_quad(2, 0);
6279 ea.set_in4_quad(3, 1);
6280 ea.set_port(1024);
6281 o.push_back(new watch_item_t(entity_name_t(entity_name_t::TYPE_CLIENT, 1), 10, 30, ea));
6282 ea.set_nonce(1001);
6283 ea.set_in4_quad(3, 2);
6284 ea.set_port(1025);
6285 o.push_back(new watch_item_t(entity_name_t(entity_name_t::TYPE_CLIENT, 2), 20, 60, ea));
6286 }
6287 };
6288 WRITE_CLASS_ENCODER_FEATURES(watch_item_t)
6289
6290 struct obj_watch_item_t {
6291 hobject_t obj;
6292 watch_item_t wi;
6293 };
6294
6295 /**
6296 * obj list watch response format
6297 *
6298 */
6299 struct obj_list_watch_response_t {
6300 std::list<watch_item_t> entries;
6301
6302 void encode(ceph::buffer::list& bl, uint64_t features) const {
6303 ENCODE_START(1, 1, bl);
6304 encode(entries, bl, features);
6305 ENCODE_FINISH(bl);
6306 }
6307 void decode(ceph::buffer::list::const_iterator& bl) {
6308 DECODE_START(1, bl);
6309 decode(entries, bl);
6310 DECODE_FINISH(bl);
6311 }
6312 void dump(ceph::Formatter *f) const {
6313 f->open_array_section("entries");
6314 for (std::list<watch_item_t>::const_iterator p = entries.begin(); p != entries.end(); ++p) {
6315 f->open_object_section("watch");
6316 p->dump(f);
6317 f->close_section();
6318 }
6319 f->close_section();
6320 }
6321 static void generate_test_instances(std::list<obj_list_watch_response_t*>& o) {
6322 entity_addr_t ea;
6323 o.push_back(new obj_list_watch_response_t);
6324 o.push_back(new obj_list_watch_response_t);
6325 std::list<watch_item_t*> test_watchers;
6326 watch_item_t::generate_test_instances(test_watchers);
6327 for (auto &e : test_watchers) {
6328 o.back()->entries.push_back(*e);
6329 delete e;
6330 }
6331 }
6332 };
6333 WRITE_CLASS_ENCODER_FEATURES(obj_list_watch_response_t)
6334
6335 struct clone_info {
6336 snapid_t cloneid;
6337 std::vector<snapid_t> snaps; // ascending
6338 std::vector< std::pair<uint64_t,uint64_t> > overlap;
6339 uint64_t size;
6340
6341 clone_info() : cloneid(CEPH_NOSNAP), size(0) {}
6342
6343 void encode(ceph::buffer::list& bl) const {
6344 ENCODE_START(1, 1, bl);
6345 encode(cloneid, bl);
6346 encode(snaps, bl);
6347 encode(overlap, bl);
6348 encode(size, bl);
6349 ENCODE_FINISH(bl);
6350 }
6351 void decode(ceph::buffer::list::const_iterator& bl) {
6352 DECODE_START(1, bl);
6353 decode(cloneid, bl);
6354 decode(snaps, bl);
6355 decode(overlap, bl);
6356 decode(size, bl);
6357 DECODE_FINISH(bl);
6358 }
6359 void dump(ceph::Formatter *f) const {
6360 if (cloneid == CEPH_NOSNAP)
6361 f->dump_string("cloneid", "HEAD");
6362 else
6363 f->dump_unsigned("cloneid", cloneid.val);
6364 f->open_array_section("snapshots");
6365 for (std::vector<snapid_t>::const_iterator p = snaps.begin(); p != snaps.end(); ++p) {
6366 f->open_object_section("snap");
6367 f->dump_unsigned("id", p->val);
6368 f->close_section();
6369 }
6370 f->close_section();
6371 f->open_array_section("overlaps");
6372 for (std::vector< std::pair<uint64_t,uint64_t> >::const_iterator q = overlap.begin();
6373 q != overlap.end(); ++q) {
6374 f->open_object_section("overlap");
6375 f->dump_unsigned("offset", q->first);
6376 f->dump_unsigned("length", q->second);
6377 f->close_section();
6378 }
6379 f->close_section();
6380 f->dump_unsigned("size", size);
6381 }
6382 static void generate_test_instances(std::list<clone_info*>& o) {
6383 o.push_back(new clone_info);
6384 o.push_back(new clone_info);
6385 o.back()->cloneid = 1;
6386 o.back()->snaps.push_back(1);
6387 o.back()->overlap.push_back(std::pair<uint64_t,uint64_t>(0,4096));
6388 o.back()->overlap.push_back(std::pair<uint64_t,uint64_t>(8192,4096));
6389 o.back()->size = 16384;
6390 o.push_back(new clone_info);
6391 o.back()->cloneid = CEPH_NOSNAP;
6392 o.back()->size = 32768;
6393 }
6394 };
6395 WRITE_CLASS_ENCODER(clone_info)
6396
6397 /**
6398 * obj list snaps response format
6399 *
6400 */
6401 struct obj_list_snap_response_t {
6402 std::vector<clone_info> clones; // ascending
6403 snapid_t seq;
6404
6405 void encode(ceph::buffer::list& bl) const {
6406 ENCODE_START(2, 1, bl);
6407 encode(clones, bl);
6408 encode(seq, bl);
6409 ENCODE_FINISH(bl);
6410 }
6411 void decode(ceph::buffer::list::const_iterator& bl) {
6412 DECODE_START(2, bl);
6413 decode(clones, bl);
6414 if (struct_v >= 2)
6415 decode(seq, bl);
6416 else
6417 seq = CEPH_NOSNAP;
6418 DECODE_FINISH(bl);
6419 }
6420 void dump(ceph::Formatter *f) const {
6421 f->open_array_section("clones");
6422 for (std::vector<clone_info>::const_iterator p = clones.begin(); p != clones.end(); ++p) {
6423 f->open_object_section("clone");
6424 p->dump(f);
6425 f->close_section();
6426 }
6427 f->dump_unsigned("seq", seq);
6428 f->close_section();
6429 }
6430 static void generate_test_instances(std::list<obj_list_snap_response_t*>& o) {
6431 o.push_back(new obj_list_snap_response_t);
6432 o.push_back(new obj_list_snap_response_t);
6433 clone_info cl;
6434 cl.cloneid = 1;
6435 cl.snaps.push_back(1);
6436 cl.overlap.push_back(std::pair<uint64_t,uint64_t>(0,4096));
6437 cl.overlap.push_back(std::pair<uint64_t,uint64_t>(8192,4096));
6438 cl.size = 16384;
6439 o.back()->clones.push_back(cl);
6440 cl.cloneid = CEPH_NOSNAP;
6441 cl.snaps.clear();
6442 cl.overlap.clear();
6443 cl.size = 32768;
6444 o.back()->clones.push_back(cl);
6445 o.back()->seq = 123;
6446 }
6447 };
6448
6449 WRITE_CLASS_ENCODER(obj_list_snap_response_t)
6450
6451 // PromoteCounter
6452
6453 struct PromoteCounter {
6454 std::atomic<unsigned long long> attempts{0};
6455 std::atomic<unsigned long long> objects{0};
6456 std::atomic<unsigned long long> bytes{0};
6457
6458 void attempt() {
6459 attempts++;
6460 }
6461
6462 void finish(uint64_t size) {
6463 objects++;
6464 bytes += size;
6465 }
6466
6467 void sample_and_attenuate(uint64_t *a, uint64_t *o, uint64_t *b) {
6468 *a = attempts;
6469 *o = objects;
6470 *b = bytes;
6471 attempts = *a / 2;
6472 objects = *o / 2;
6473 bytes = *b / 2;
6474 }
6475 };
6476
6477 struct pool_pg_num_history_t {
6478 /// last epoch updated
6479 epoch_t epoch = 0;
6480 /// poolid -> epoch -> pg_num
6481 std::map<int64_t, std::map<epoch_t,uint32_t>> pg_nums;
6482 /// pair(epoch, poolid)
6483 std::set<std::pair<epoch_t,int64_t>> deleted_pools;
6484
6485 void log_pg_num_change(epoch_t epoch, int64_t pool, uint32_t pg_num) {
6486 pg_nums[pool][epoch] = pg_num;
6487 }
6488 void log_pool_delete(epoch_t epoch, int64_t pool) {
6489 deleted_pools.insert(std::make_pair(epoch, pool));
6490 }
6491
6492 /// prune history based on oldest osdmap epoch in the cluster
6493 void prune(epoch_t oldest_epoch) {
6494 auto i = deleted_pools.begin();
6495 while (i != deleted_pools.end()) {
6496 if (i->first >= oldest_epoch) {
6497 break;
6498 }
6499 pg_nums.erase(i->second);
6500 i = deleted_pools.erase(i);
6501 }
6502 for (auto& j : pg_nums) {
6503 auto k = j.second.lower_bound(oldest_epoch);
6504 // keep this and the entry before it (just to be paranoid)
6505 if (k != j.second.begin()) {
6506 --k;
6507 j.second.erase(j.second.begin(), k);
6508 }
6509 }
6510 }
6511
6512 void encode(ceph::buffer::list& bl) const {
6513 ENCODE_START(1, 1, bl);
6514 encode(epoch, bl);
6515 encode(pg_nums, bl);
6516 encode(deleted_pools, bl);
6517 ENCODE_FINISH(bl);
6518 }
6519 void decode(ceph::buffer::list::const_iterator& p) {
6520 DECODE_START(1, p);
6521 decode(epoch, p);
6522 decode(pg_nums, p);
6523 decode(deleted_pools, p);
6524 DECODE_FINISH(p);
6525 }
6526 void dump(ceph::Formatter *f) const {
6527 f->dump_unsigned("epoch", epoch);
6528 f->open_object_section("pools");
6529 for (auto& i : pg_nums) {
6530 f->open_object_section("pool");
6531 f->dump_unsigned("pool_id", i.first);
6532 f->open_array_section("changes");
6533 for (auto& j : i.second) {
6534 f->open_object_section("change");
6535 f->dump_unsigned("epoch", j.first);
6536 f->dump_unsigned("pg_num", j.second);
6537 f->close_section();
6538 }
6539 f->close_section();
6540 f->close_section();
6541 }
6542 f->close_section();
6543 f->open_array_section("deleted_pools");
6544 for (auto& i : deleted_pools) {
6545 f->open_object_section("deletion");
6546 f->dump_unsigned("pool_id", i.second);
6547 f->dump_unsigned("epoch", i.first);
6548 f->close_section();
6549 }
6550 f->close_section();
6551 }
6552 static void generate_test_instances(std::list<pool_pg_num_history_t*>& ls) {
6553 ls.push_back(new pool_pg_num_history_t);
6554 }
6555 friend std::ostream& operator<<(std::ostream& out, const pool_pg_num_history_t& h) {
6556 return out << "pg_num_history(e" << h.epoch
6557 << " pg_nums " << h.pg_nums
6558 << " deleted_pools " << h.deleted_pools
6559 << ")";
6560 }
6561 };
6562 WRITE_CLASS_ENCODER(pool_pg_num_history_t)
6563
6564 // prefix pgmeta_oid keys with _ so that PGLog::read_log_and_missing() can
6565 // easily skip them
6566 static const std::string_view infover_key = "_infover";
6567 static const std::string_view info_key = "_info";
6568 static const std::string_view biginfo_key = "_biginfo";
6569 static const std::string_view epoch_key = "_epoch";
6570 static const std::string_view fastinfo_key = "_fastinfo";
6571
6572 static const __u8 pg_latest_struct_v = 10;
6573 // v10 is the new past_intervals encoding
6574 // v9 was fastinfo_key addition
6575 // v8 was the move to a per-pg pgmeta object
6576 // v7 was SnapMapper addition in 86658392516d5175b2756659ef7ffaaf95b0f8ad
6577 // (first appeared in cuttlefish).
6578 static const __u8 pg_compat_struct_v = 10;
6579
6580 int prepare_info_keymap(
6581 CephContext* cct,
6582 std::map<std::string,ceph::buffer::list> *km,
6583 std::string *key_to_remove,
6584 epoch_t epoch,
6585 pg_info_t &info,
6586 pg_info_t &last_written_info,
6587 PastIntervals &past_intervals,
6588 bool dirty_big_info,
6589 bool dirty_epoch,
6590 bool try_fast_info,
6591 PerfCounters *logger = nullptr,
6592 DoutPrefixProvider *dpp = nullptr);
6593
6594 namespace ceph::os {
6595 class Transaction;
6596 };
6597
6598 void create_pg_collection(
6599 ceph::os::Transaction& t, spg_t pgid, int bits);
6600
6601 void init_pg_ondisk(
6602 ceph::os::Transaction& t, spg_t pgid, const pg_pool_t *pool);
6603
6604 // omap specific stats
6605 struct omap_stat_t {
6606 int large_omap_objects;
6607 int64_t omap_bytes;
6608 int64_t omap_keys;
6609 };
6610
6611 // filter for pg listings
6612 class PGLSFilter {
6613 CephContext* cct;
6614 protected:
6615 std::string xattr;
6616 public:
6617 PGLSFilter();
6618 virtual ~PGLSFilter();
6619 virtual bool filter(const hobject_t &obj,
6620 const ceph::buffer::list& xattr_data) const = 0;
6621
6622 /**
6623 * Arguments passed from the RADOS client. Implementations must
6624 * handle any encoding errors, and return an appropriate error code,
6625 * or 0 on valid input.
6626 */
6627 virtual int init(ceph::buffer::list::const_iterator &params) = 0;
6628
6629 /**
6630 * xattr key, or empty string. If non-empty, this xattr will be fetched
6631 * and the value passed into ::filter
6632 */
6633 virtual const std::string& get_xattr() const { return xattr; }
6634
6635 /**
6636 * If true, objects without the named xattr (if xattr name is not empty)
6637 * will be rejected without calling ::filter
6638 */
6639 virtual bool reject_empty_xattr() const { return true; }
6640 };
6641
6642 class PGLSPlainFilter : public PGLSFilter {
6643 std::string val;
6644 public:
6645 int init(ceph::buffer::list::const_iterator &params) override;
6646 ~PGLSPlainFilter() override {}
6647 bool filter(const hobject_t& obj,
6648 const ceph::buffer::list& xattr_data) const override;
6649 };
6650
6651 // alias name for this structure:
6652 using missing_map_t = std::map<hobject_t,
6653 std::pair<std::optional<uint32_t>,
6654 std::optional<uint32_t>>>;
6655
6656 #endif