]> git.proxmox.com Git - ceph.git/blob - ceph/src/osd/osd_types.h
add subtree-ish sources for 12.0.3
[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 <sstream>
22 #include <stdio.h>
23 #include <memory>
24 #include <boost/scoped_ptr.hpp>
25 #include <boost/optional/optional_io.hpp>
26 #include <boost/variant.hpp>
27
28 #include "include/rados/rados_types.hpp"
29 #include "include/mempool.h"
30
31 #include "msg/msg_types.h"
32 #include "include/types.h"
33 #include "include/utime.h"
34 #include "include/CompatSet.h"
35 #include "common/histogram.h"
36 #include "include/interval_set.h"
37 #include "include/inline_memory.h"
38 #include "common/Formatter.h"
39 #include "common/bloom_filter.hpp"
40 #include "common/hobject.h"
41 #include "common/snap_types.h"
42 #include "HitSet.h"
43 #include "Watch.h"
44 #include "include/cmp.h"
45 #include "librados/ListObjectImpl.h"
46 #include "compressor/Compressor.h"
47 #include <atomic>
48
49 #define CEPH_OSD_ONDISK_MAGIC "ceph osd volume v026"
50
51 #define CEPH_OSD_FEATURE_INCOMPAT_BASE CompatSet::Feature(1, "initial feature set(~v.18)")
52 #define CEPH_OSD_FEATURE_INCOMPAT_PGINFO CompatSet::Feature(2, "pginfo object")
53 #define CEPH_OSD_FEATURE_INCOMPAT_OLOC CompatSet::Feature(3, "object locator")
54 #define CEPH_OSD_FEATURE_INCOMPAT_LEC CompatSet::Feature(4, "last_epoch_clean")
55 #define CEPH_OSD_FEATURE_INCOMPAT_CATEGORIES CompatSet::Feature(5, "categories")
56 #define CEPH_OSD_FEATURE_INCOMPAT_HOBJECTPOOL CompatSet::Feature(6, "hobjectpool")
57 #define CEPH_OSD_FEATURE_INCOMPAT_BIGINFO CompatSet::Feature(7, "biginfo")
58 #define CEPH_OSD_FEATURE_INCOMPAT_LEVELDBINFO CompatSet::Feature(8, "leveldbinfo")
59 #define CEPH_OSD_FEATURE_INCOMPAT_LEVELDBLOG CompatSet::Feature(9, "leveldblog")
60 #define CEPH_OSD_FEATURE_INCOMPAT_SNAPMAPPER CompatSet::Feature(10, "snapmapper")
61 #define CEPH_OSD_FEATURE_INCOMPAT_SHARDS CompatSet::Feature(11, "sharded objects")
62 #define CEPH_OSD_FEATURE_INCOMPAT_HINTS CompatSet::Feature(12, "transaction hints")
63 #define CEPH_OSD_FEATURE_INCOMPAT_PGMETA CompatSet::Feature(13, "pg meta object")
64 #define CEPH_OSD_FEATURE_INCOMPAT_MISSING CompatSet::Feature(14, "explicit missing set")
65 #define CEPH_OSD_FEATURE_INCOMPAT_FASTINFO CompatSet::Feature(15, "fastinfo pg attr")
66
67
68 /// min recovery priority for MBackfillReserve
69 #define OSD_RECOVERY_PRIORITY_MIN 0
70
71 /// base backfill priority for MBackfillReserve
72 #define OSD_BACKFILL_PRIORITY_BASE 100
73
74 /// base backfill priority for MBackfillReserve (degraded PG)
75 #define OSD_BACKFILL_DEGRADED_PRIORITY_BASE 140
76
77 /// base recovery priority for MBackfillReserve
78 #define OSD_RECOVERY_PRIORITY_BASE 180
79
80 /// base backfill priority for MBackfillReserve (inactive PG)
81 #define OSD_BACKFILL_INACTIVE_PRIORITY_BASE 220
82
83 /// max recovery priority for MBackfillReserve
84 #define OSD_RECOVERY_PRIORITY_MAX 255
85
86
87 typedef hobject_t collection_list_handle_t;
88
89 /// convert a single CPEH_OSD_FLAG_* to a string
90 const char *ceph_osd_flag_name(unsigned flag);
91 /// convert a single CEPH_OSD_OF_FLAG_* to a string
92 const char *ceph_osd_op_flag_name(unsigned flag);
93
94 /// convert CEPH_OSD_FLAG_* op flags to a string
95 string ceph_osd_flag_string(unsigned flags);
96 /// conver CEPH_OSD_OP_FLAG_* op flags to a string
97 string ceph_osd_op_flag_string(unsigned flags);
98 /// conver CEPH_OSD_ALLOC_HINT_FLAG_* op flags to a string
99 string ceph_osd_alloc_hint_flag_string(unsigned flags);
100
101
102 /**
103 * osd request identifier
104 *
105 * caller name + incarnation# + tid to unique identify this request.
106 */
107 struct osd_reqid_t {
108 entity_name_t name; // who
109 ceph_tid_t tid;
110 int32_t inc; // incarnation
111
112 osd_reqid_t()
113 : tid(0), inc(0) {}
114 osd_reqid_t(const entity_name_t& a, int i, ceph_tid_t t)
115 : name(a), tid(t), inc(i) {}
116
117 DENC(osd_reqid_t, v, p) {
118 DENC_START(2, 2, p);
119 denc(v.name, p);
120 denc(v.tid, p);
121 denc(v.inc, p);
122 DENC_FINISH(p);
123 }
124 void dump(Formatter *f) const;
125 static void generate_test_instances(list<osd_reqid_t*>& o);
126 };
127 WRITE_CLASS_DENC(osd_reqid_t)
128
129
130
131 struct pg_shard_t {
132 int32_t osd;
133 shard_id_t shard;
134 pg_shard_t() : osd(-1), shard(shard_id_t::NO_SHARD) {}
135 explicit pg_shard_t(int osd) : osd(osd), shard(shard_id_t::NO_SHARD) {}
136 pg_shard_t(int osd, shard_id_t shard) : osd(osd), shard(shard) {}
137 bool is_undefined() const {
138 return osd == -1;
139 }
140 void encode(bufferlist &bl) const;
141 void decode(bufferlist::iterator &bl);
142 void dump(Formatter *f) const {
143 f->dump_unsigned("osd", osd);
144 if (shard != shard_id_t::NO_SHARD) {
145 f->dump_unsigned("shard", shard);
146 }
147 }
148 };
149 WRITE_CLASS_ENCODER(pg_shard_t)
150 WRITE_EQ_OPERATORS_2(pg_shard_t, osd, shard)
151 WRITE_CMP_OPERATORS_2(pg_shard_t, osd, shard)
152 ostream &operator<<(ostream &lhs, const pg_shard_t &rhs);
153
154 class IsPGRecoverablePredicate {
155 public:
156 /**
157 * have encodes the shards available
158 */
159 virtual bool operator()(const set<pg_shard_t> &have) const = 0;
160 virtual ~IsPGRecoverablePredicate() {}
161 };
162
163 class IsPGReadablePredicate {
164 public:
165 /**
166 * have encodes the shards available
167 */
168 virtual bool operator()(const set<pg_shard_t> &have) const = 0;
169 virtual ~IsPGReadablePredicate() {}
170 };
171
172 inline ostream& operator<<(ostream& out, const osd_reqid_t& r) {
173 return out << r.name << "." << r.inc << ":" << r.tid;
174 }
175
176 inline bool operator==(const osd_reqid_t& l, const osd_reqid_t& r) {
177 return (l.name == r.name) && (l.inc == r.inc) && (l.tid == r.tid);
178 }
179 inline bool operator!=(const osd_reqid_t& l, const osd_reqid_t& r) {
180 return (l.name != r.name) || (l.inc != r.inc) || (l.tid != r.tid);
181 }
182 inline bool operator<(const osd_reqid_t& l, const osd_reqid_t& r) {
183 return (l.name < r.name) || (l.inc < r.inc) ||
184 (l.name == r.name && l.inc == r.inc && l.tid < r.tid);
185 }
186 inline bool operator<=(const osd_reqid_t& l, const osd_reqid_t& r) {
187 return (l.name < r.name) || (l.inc < r.inc) ||
188 (l.name == r.name && l.inc == r.inc && l.tid <= r.tid);
189 }
190 inline bool operator>(const osd_reqid_t& l, const osd_reqid_t& r) { return !(l <= r); }
191 inline bool operator>=(const osd_reqid_t& l, const osd_reqid_t& r) { return !(l < r); }
192
193 namespace std {
194 template<> struct hash<osd_reqid_t> {
195 size_t operator()(const osd_reqid_t &r) const {
196 static hash<uint64_t> H;
197 return H(r.name.num() ^ r.tid ^ r.inc);
198 }
199 };
200 } // namespace std
201
202
203 // -----
204
205 // a locator constrains the placement of an object. mainly, which pool
206 // does it go in.
207 struct object_locator_t {
208 // You specify either the hash or the key -- not both
209 int64_t pool; ///< pool id
210 string key; ///< key string (if non-empty)
211 string nspace; ///< namespace
212 int64_t hash; ///< hash position (if >= 0)
213
214 explicit object_locator_t()
215 : pool(-1), hash(-1) {}
216 explicit object_locator_t(int64_t po)
217 : pool(po), hash(-1) {}
218 explicit object_locator_t(int64_t po, int64_t ps)
219 : pool(po), hash(ps) {}
220 explicit object_locator_t(int64_t po, string ns)
221 : pool(po), nspace(ns), hash(-1) {}
222 explicit object_locator_t(int64_t po, string ns, int64_t ps)
223 : pool(po), nspace(ns), hash(ps) {}
224 explicit object_locator_t(int64_t po, string ns, string s)
225 : pool(po), key(s), nspace(ns), hash(-1) {}
226 explicit object_locator_t(const hobject_t& soid)
227 : pool(soid.pool), key(soid.get_key()), nspace(soid.nspace), hash(-1) {}
228
229 int64_t get_pool() const {
230 return pool;
231 }
232
233 void clear() {
234 pool = -1;
235 key = "";
236 nspace = "";
237 hash = -1;
238 }
239
240 bool empty() const {
241 return pool == -1;
242 }
243
244 void encode(bufferlist& bl) const;
245 void decode(bufferlist::iterator& p);
246 void dump(Formatter *f) const;
247 static void generate_test_instances(list<object_locator_t*>& o);
248 };
249 WRITE_CLASS_ENCODER(object_locator_t)
250
251 inline bool operator==(const object_locator_t& l, const object_locator_t& r) {
252 return l.pool == r.pool && l.key == r.key && l.nspace == r.nspace && l.hash == r.hash;
253 }
254 inline bool operator!=(const object_locator_t& l, const object_locator_t& r) {
255 return !(l == r);
256 }
257
258 inline ostream& operator<<(ostream& out, const object_locator_t& loc)
259 {
260 out << "@" << loc.pool;
261 if (loc.nspace.length())
262 out << ";" << loc.nspace;
263 if (loc.key.length())
264 out << ":" << loc.key;
265 return out;
266 }
267
268 struct request_redirect_t {
269 private:
270 object_locator_t redirect_locator; ///< this is authoritative
271 string redirect_object; ///< If non-empty, the request goes to this object name
272 bufferlist osd_instructions; ///< a bufferlist for the OSDs, passed but not interpreted by clients
273
274 friend ostream& operator<<(ostream& out, const request_redirect_t& redir);
275 public:
276
277 request_redirect_t() {}
278 explicit request_redirect_t(const object_locator_t& orig, int64_t rpool) :
279 redirect_locator(orig) { redirect_locator.pool = rpool; }
280 explicit request_redirect_t(const object_locator_t& rloc) :
281 redirect_locator(rloc) {}
282 explicit request_redirect_t(const object_locator_t& orig,
283 const string& robj) :
284 redirect_locator(orig), redirect_object(robj) {}
285
286 void set_instructions(const bufferlist& bl) { osd_instructions = bl; }
287 const bufferlist& get_instructions() { return osd_instructions; }
288
289 bool empty() const { return redirect_locator.empty() &&
290 redirect_object.empty(); }
291
292 void combine_with_locator(object_locator_t& orig, string& obj) const {
293 orig = redirect_locator;
294 if (!redirect_object.empty())
295 obj = redirect_object;
296 }
297
298 void encode(bufferlist& bl) const;
299 void decode(bufferlist::iterator& bl);
300 void dump(Formatter *f) const;
301 static void generate_test_instances(list<request_redirect_t*>& o);
302 };
303 WRITE_CLASS_ENCODER(request_redirect_t)
304
305 inline ostream& operator<<(ostream& out, const request_redirect_t& redir) {
306 out << "object " << redir.redirect_object << ", locator{" << redir.redirect_locator << "}";
307 return out;
308 }
309
310 // Internal OSD op flags - set by the OSD based on the op types
311 enum {
312 CEPH_OSD_RMW_FLAG_READ = (1 << 1),
313 CEPH_OSD_RMW_FLAG_WRITE = (1 << 2),
314 CEPH_OSD_RMW_FLAG_CLASS_READ = (1 << 3),
315 CEPH_OSD_RMW_FLAG_CLASS_WRITE = (1 << 4),
316 CEPH_OSD_RMW_FLAG_PGOP = (1 << 5),
317 CEPH_OSD_RMW_FLAG_CACHE = (1 << 6),
318 CEPH_OSD_RMW_FLAG_FORCE_PROMOTE = (1 << 7),
319 CEPH_OSD_RMW_FLAG_SKIP_HANDLE_CACHE = (1 << 8),
320 CEPH_OSD_RMW_FLAG_SKIP_PROMOTE = (1 << 9),
321 CEPH_OSD_RMW_FLAG_RWORDERED = (1 << 10),
322 };
323
324
325 // pg stuff
326
327 #define OSD_SUPERBLOCK_GOBJECT ghobject_t(hobject_t(sobject_t(object_t("osd_superblock"), 0)))
328
329 // placement seed (a hash value)
330 typedef uint32_t ps_t;
331
332 // old (v1) pg_t encoding (wrap old struct ceph_pg)
333 struct old_pg_t {
334 ceph_pg v;
335 void encode(bufferlist& bl) const {
336 ::encode_raw(v, bl);
337 }
338 void decode(bufferlist::iterator& bl) {
339 ::decode_raw(v, bl);
340 }
341 };
342 WRITE_CLASS_ENCODER(old_pg_t)
343
344 // placement group id
345 struct pg_t {
346 uint64_t m_pool;
347 uint32_t m_seed;
348 int32_t m_preferred;
349
350 pg_t() : m_pool(0), m_seed(0), m_preferred(-1) {}
351 pg_t(ps_t seed, uint64_t pool, int pref=-1) :
352 m_pool(pool), m_seed(seed), m_preferred(pref) {}
353 // cppcheck-suppress noExplicitConstructor
354 pg_t(const ceph_pg& cpg) :
355 m_pool(cpg.pool), m_seed(cpg.ps), m_preferred((__s16)cpg.preferred) {}
356
357 // cppcheck-suppress noExplicitConstructor
358 pg_t(const old_pg_t& opg) {
359 *this = opg.v;
360 }
361
362 old_pg_t get_old_pg() const {
363 old_pg_t o;
364 assert(m_pool < 0xffffffffull);
365 o.v.pool = m_pool;
366 o.v.ps = m_seed;
367 o.v.preferred = (__s16)m_preferred;
368 return o;
369 }
370
371 ps_t ps() const {
372 return m_seed;
373 }
374 uint64_t pool() const {
375 return m_pool;
376 }
377 int32_t preferred() const {
378 return m_preferred;
379 }
380
381 static const uint8_t calc_name_buf_size = 36; // max length for max values len("18446744073709551615.ffffffff") + future suffix len("_head") + '\0'
382 char *calc_name(char *buf, const char *suffix_backwords) const;
383
384 void set_ps(ps_t p) {
385 m_seed = p;
386 }
387 void set_pool(uint64_t p) {
388 m_pool = p;
389 }
390 void set_preferred(int32_t osd) {
391 m_preferred = osd;
392 }
393
394 pg_t get_parent() const;
395 pg_t get_ancestor(unsigned old_pg_num) const;
396
397 int print(char *o, int maxlen) const;
398 bool parse(const char *s);
399
400 bool is_split(unsigned old_pg_num, unsigned new_pg_num, set<pg_t> *pchildren) const;
401
402 /**
403 * Returns b such that for all object o:
404 * ~((~0)<<b) & o.hash) == 0 iff o is in the pg for *this
405 */
406 unsigned get_split_bits(unsigned pg_num) const;
407
408 bool contains(int bits, const ghobject_t& oid) {
409 return oid.match(bits, ps());
410 }
411 bool contains(int bits, const hobject_t& oid) {
412 return oid.match(bits, ps());
413 }
414
415 hobject_t get_hobj_start() const;
416 hobject_t get_hobj_end(unsigned pg_num) const;
417
418 void encode(bufferlist& bl) const {
419 __u8 v = 1;
420 ::encode(v, bl);
421 ::encode(m_pool, bl);
422 ::encode(m_seed, bl);
423 ::encode(m_preferred, bl);
424 }
425 void decode(bufferlist::iterator& bl) {
426 __u8 v;
427 ::decode(v, bl);
428 ::decode(m_pool, bl);
429 ::decode(m_seed, bl);
430 ::decode(m_preferred, bl);
431 }
432 void decode_old(bufferlist::iterator& bl) {
433 old_pg_t opg;
434 ::decode(opg, bl);
435 *this = opg;
436 }
437 void dump(Formatter *f) const;
438 static void generate_test_instances(list<pg_t*>& o);
439 };
440 WRITE_CLASS_ENCODER(pg_t)
441
442 inline bool operator<(const pg_t& l, const pg_t& r) {
443 return l.pool() < r.pool() ||
444 (l.pool() == r.pool() && (l.preferred() < r.preferred() ||
445 (l.preferred() == r.preferred() && (l.ps() < r.ps()))));
446 }
447 inline bool operator<=(const pg_t& l, const pg_t& r) {
448 return l.pool() < r.pool() ||
449 (l.pool() == r.pool() && (l.preferred() < r.preferred() ||
450 (l.preferred() == r.preferred() && (l.ps() <= r.ps()))));
451 }
452 inline bool operator==(const pg_t& l, const pg_t& r) {
453 return l.pool() == r.pool() &&
454 l.preferred() == r.preferred() &&
455 l.ps() == r.ps();
456 }
457 inline bool operator!=(const pg_t& l, const pg_t& r) {
458 return l.pool() != r.pool() ||
459 l.preferred() != r.preferred() ||
460 l.ps() != r.ps();
461 }
462 inline bool operator>(const pg_t& l, const pg_t& r) {
463 return l.pool() > r.pool() ||
464 (l.pool() == r.pool() && (l.preferred() > r.preferred() ||
465 (l.preferred() == r.preferred() && (l.ps() > r.ps()))));
466 }
467 inline bool operator>=(const pg_t& l, const pg_t& r) {
468 return l.pool() > r.pool() ||
469 (l.pool() == r.pool() && (l.preferred() > r.preferred() ||
470 (l.preferred() == r.preferred() && (l.ps() >= r.ps()))));
471 }
472
473 ostream& operator<<(ostream& out, const pg_t &pg);
474
475 namespace std {
476 template<> struct hash< pg_t >
477 {
478 size_t operator()( const pg_t& x ) const
479 {
480 static hash<uint32_t> H;
481 return H((x.pool() & 0xffffffff) ^ (x.pool() >> 32) ^ x.ps() ^ x.preferred());
482 }
483 };
484 } // namespace std
485
486 struct spg_t {
487 pg_t pgid;
488 shard_id_t shard;
489 spg_t() : shard(shard_id_t::NO_SHARD) {}
490 spg_t(pg_t pgid, shard_id_t shard) : pgid(pgid), shard(shard) {}
491 explicit spg_t(pg_t pgid) : pgid(pgid), shard(shard_id_t::NO_SHARD) {}
492 unsigned get_split_bits(unsigned pg_num) const {
493 return pgid.get_split_bits(pg_num);
494 }
495 spg_t get_parent() const {
496 return spg_t(pgid.get_parent(), shard);
497 }
498 ps_t ps() const {
499 return pgid.ps();
500 }
501 uint64_t pool() const {
502 return pgid.pool();
503 }
504 int32_t preferred() const {
505 return pgid.preferred();
506 }
507
508 static const uint8_t calc_name_buf_size = pg_t::calc_name_buf_size + 4; // 36 + len('s') + len("255");
509 char *calc_name(char *buf, const char *suffix_backwords) const;
510
511 bool parse(const char *s);
512 bool parse(const std::string& s) {
513 return parse(s.c_str());
514 }
515 bool is_split(unsigned old_pg_num, unsigned new_pg_num,
516 set<spg_t> *pchildren) const {
517 set<pg_t> _children;
518 set<pg_t> *children = pchildren ? &_children : NULL;
519 bool is_split = pgid.is_split(old_pg_num, new_pg_num, children);
520 if (pchildren && is_split) {
521 for (set<pg_t>::iterator i = _children.begin();
522 i != _children.end();
523 ++i) {
524 pchildren->insert(spg_t(*i, shard));
525 }
526 }
527 return is_split;
528 }
529 bool is_no_shard() const {
530 return shard == shard_id_t::NO_SHARD;
531 }
532
533 ghobject_t make_pgmeta_oid() const {
534 return ghobject_t::make_pgmeta(pgid.pool(), pgid.ps(), shard);
535 }
536
537 void encode(bufferlist &bl) const {
538 ENCODE_START(1, 1, bl);
539 ::encode(pgid, bl);
540 ::encode(shard, bl);
541 ENCODE_FINISH(bl);
542 }
543 void decode(bufferlist::iterator &bl) {
544 DECODE_START(1, bl);
545 ::decode(pgid, bl);
546 ::decode(shard, bl);
547 DECODE_FINISH(bl);
548 }
549
550 ghobject_t make_temp_ghobject(const string& name) const {
551 return ghobject_t(
552 hobject_t(object_t(name), "", CEPH_NOSNAP,
553 pgid.ps(),
554 hobject_t::POOL_TEMP_START - pgid.pool(), ""),
555 ghobject_t::NO_GEN,
556 shard);
557 }
558
559 unsigned hash_to_shard(unsigned num_shards) const {
560 return ps() % num_shards;
561 }
562 };
563 WRITE_CLASS_ENCODER(spg_t)
564 WRITE_EQ_OPERATORS_2(spg_t, pgid, shard)
565 WRITE_CMP_OPERATORS_2(spg_t, pgid, shard)
566
567 namespace std {
568 template<> struct hash< spg_t >
569 {
570 size_t operator()( const spg_t& x ) const
571 {
572 static hash<uint32_t> H;
573 return H(hash<pg_t>()(x.pgid) ^ x.shard);
574 }
575 };
576 } // namespace std
577
578 ostream& operator<<(ostream& out, const spg_t &pg);
579
580 // ----------------------
581
582 class coll_t {
583 enum type_t {
584 TYPE_META = 0,
585 TYPE_LEGACY_TEMP = 1, /* no longer used */
586 TYPE_PG = 2,
587 TYPE_PG_TEMP = 3,
588 };
589 type_t type;
590 spg_t pgid;
591 uint64_t removal_seq; // note: deprecated, not encoded
592
593 char _str_buff[spg_t::calc_name_buf_size];
594 char *_str;
595
596 void calc_str();
597
598 coll_t(type_t t, spg_t p, uint64_t r)
599 : type(t), pgid(p), removal_seq(r) {
600 calc_str();
601 }
602
603 public:
604 coll_t() : type(TYPE_META), removal_seq(0)
605 {
606 calc_str();
607 }
608
609 coll_t(const coll_t& other)
610 : type(other.type), pgid(other.pgid), removal_seq(other.removal_seq) {
611 calc_str();
612 }
613
614 explicit coll_t(spg_t pgid)
615 : type(TYPE_PG), pgid(pgid), removal_seq(0)
616 {
617 calc_str();
618 }
619
620 coll_t& operator=(const coll_t& rhs)
621 {
622 this->type = rhs.type;
623 this->pgid = rhs.pgid;
624 this->removal_seq = rhs.removal_seq;
625 this->calc_str();
626 return *this;
627 }
628
629 // named constructors
630 static coll_t meta() {
631 return coll_t();
632 }
633 static coll_t pg(spg_t p) {
634 return coll_t(p);
635 }
636
637 const std::string to_str() const {
638 return string(_str);
639 }
640 const char *c_str() const {
641 return _str;
642 }
643
644 bool parse(const std::string& s);
645
646 int operator<(const coll_t &rhs) const {
647 return type < rhs.type ||
648 (type == rhs.type && pgid < rhs.pgid);
649 }
650
651 bool is_meta() const {
652 return type == TYPE_META;
653 }
654 bool is_pg_prefix(spg_t *pgid_) const {
655 if (type == TYPE_PG || type == TYPE_PG_TEMP) {
656 *pgid_ = pgid;
657 return true;
658 }
659 return false;
660 }
661 bool is_pg() const {
662 return type == TYPE_PG;
663 }
664 bool is_pg(spg_t *pgid_) const {
665 if (type == TYPE_PG) {
666 *pgid_ = pgid;
667 return true;
668 }
669 return false;
670 }
671 bool is_temp() const {
672 return type == TYPE_PG_TEMP;
673 }
674 bool is_temp(spg_t *pgid_) const {
675 if (type == TYPE_PG_TEMP) {
676 *pgid_ = pgid;
677 return true;
678 }
679 return false;
680 }
681
682 void encode(bufferlist& bl) const;
683 void decode(bufferlist::iterator& bl);
684 size_t encoded_size() const;
685
686 inline bool operator==(const coll_t& rhs) const {
687 // only compare type if meta
688 if (type != rhs.type)
689 return false;
690 if (type == TYPE_META)
691 return true;
692 return type == rhs.type && pgid == rhs.pgid;
693 }
694 inline bool operator!=(const coll_t& rhs) const {
695 return !(*this == rhs);
696 }
697
698 // get a TEMP collection that corresponds to the current collection,
699 // which we presume is a pg collection.
700 coll_t get_temp() const {
701 assert(type == TYPE_PG);
702 return coll_t(TYPE_PG_TEMP, pgid, 0);
703 }
704
705 ghobject_t get_min_hobj() const {
706 ghobject_t o;
707 switch (type) {
708 case TYPE_PG:
709 o.hobj.pool = pgid.pool();
710 o.set_shard(pgid.shard);
711 break;
712 case TYPE_META:
713 o.hobj.pool = -1;
714 break;
715 default:
716 break;
717 }
718 return o;
719 }
720
721 unsigned hash_to_shard(unsigned num_shards) const {
722 if (type == TYPE_PG)
723 return pgid.hash_to_shard(num_shards);
724 return 0; // whatever.
725 }
726
727 void dump(Formatter *f) const;
728 static void generate_test_instances(list<coll_t*>& o);
729 };
730
731 WRITE_CLASS_ENCODER(coll_t)
732
733 inline ostream& operator<<(ostream& out, const coll_t& c) {
734 out << c.to_str();
735 return out;
736 }
737
738 namespace std {
739 template<> struct hash<coll_t> {
740 size_t operator()(const coll_t &c) const {
741 size_t h = 0;
742 string str(c.to_str());
743 std::string::const_iterator end(str.end());
744 for (std::string::const_iterator s = str.begin(); s != end; ++s) {
745 h += *s;
746 h += (h << 10);
747 h ^= (h >> 6);
748 }
749 h += (h << 3);
750 h ^= (h >> 11);
751 h += (h << 15);
752 return h;
753 }
754 };
755 } // namespace std
756
757 inline ostream& operator<<(ostream& out, const ceph_object_layout &ol)
758 {
759 out << pg_t(ol.ol_pgid);
760 int su = ol.ol_stripe_unit;
761 if (su)
762 out << ".su=" << su;
763 return out;
764 }
765
766
767
768 // compound rados version type
769 /* WARNING: If add member in eversion_t, please make sure the encode/decode function
770 * work well. For little-endian machine, we should make sure there is no padding
771 * in 32-bit machine and 64-bit machine.
772 */
773 class eversion_t {
774 public:
775 version_t version;
776 epoch_t epoch;
777 __u32 __pad;
778 eversion_t() : version(0), epoch(0), __pad(0) {}
779 eversion_t(epoch_t e, version_t v) : version(v), epoch(e), __pad(0) {}
780
781 // cppcheck-suppress noExplicitConstructor
782 eversion_t(const ceph_eversion& ce) :
783 version(ce.version),
784 epoch(ce.epoch),
785 __pad(0) { }
786
787 explicit eversion_t(bufferlist& bl) : __pad(0) { decode(bl); }
788
789 static eversion_t max() {
790 eversion_t max;
791 max.version -= 1;
792 max.epoch -= 1;
793 return max;
794 }
795
796 operator ceph_eversion() {
797 ceph_eversion c;
798 c.epoch = epoch;
799 c.version = version;
800 return c;
801 }
802
803 string get_key_name() const;
804
805 void encode(bufferlist &bl) const {
806 #if defined(CEPH_LITTLE_ENDIAN)
807 bl.append((char *)this, sizeof(version_t) + sizeof(epoch_t));
808 #else
809 ::encode(version, bl);
810 ::encode(epoch, bl);
811 #endif
812 }
813 void decode(bufferlist::iterator &bl) {
814 #if defined(CEPH_LITTLE_ENDIAN)
815 bl.copy(sizeof(version_t) + sizeof(epoch_t), (char *)this);
816 #else
817 ::decode(version, bl);
818 ::decode(epoch, bl);
819 #endif
820 }
821 void decode(bufferlist& bl) {
822 bufferlist::iterator p = bl.begin();
823 decode(p);
824 }
825 };
826 WRITE_CLASS_ENCODER(eversion_t)
827
828 inline bool operator==(const eversion_t& l, const eversion_t& r) {
829 return (l.epoch == r.epoch) && (l.version == r.version);
830 }
831 inline bool operator!=(const eversion_t& l, const eversion_t& r) {
832 return (l.epoch != r.epoch) || (l.version != r.version);
833 }
834 inline bool operator<(const eversion_t& l, const eversion_t& r) {
835 return (l.epoch == r.epoch) ? (l.version < r.version):(l.epoch < r.epoch);
836 }
837 inline bool operator<=(const eversion_t& l, const eversion_t& r) {
838 return (l.epoch == r.epoch) ? (l.version <= r.version):(l.epoch <= r.epoch);
839 }
840 inline bool operator>(const eversion_t& l, const eversion_t& r) {
841 return (l.epoch == r.epoch) ? (l.version > r.version):(l.epoch > r.epoch);
842 }
843 inline bool operator>=(const eversion_t& l, const eversion_t& r) {
844 return (l.epoch == r.epoch) ? (l.version >= r.version):(l.epoch >= r.epoch);
845 }
846 inline ostream& operator<<(ostream& out, const eversion_t& e) {
847 return out << e.epoch << "'" << e.version;
848 }
849
850 /**
851 * objectstore_perf_stat_t
852 *
853 * current perf information about the osd
854 */
855 struct objectstore_perf_stat_t {
856 // cur_op_latency is in ms since double add/sub are not associative
857 uint32_t os_commit_latency;
858 uint32_t os_apply_latency;
859
860 objectstore_perf_stat_t() :
861 os_commit_latency(0), os_apply_latency(0) {}
862
863 bool operator==(const objectstore_perf_stat_t &r) const {
864 return os_commit_latency == r.os_commit_latency &&
865 os_apply_latency == r.os_apply_latency;
866 }
867
868 void add(const objectstore_perf_stat_t &o) {
869 os_commit_latency += o.os_commit_latency;
870 os_apply_latency += o.os_apply_latency;
871 }
872 void sub(const objectstore_perf_stat_t &o) {
873 os_commit_latency -= o.os_commit_latency;
874 os_apply_latency -= o.os_apply_latency;
875 }
876 void dump(Formatter *f) const;
877 void encode(bufferlist &bl) const;
878 void decode(bufferlist::iterator &bl);
879 static void generate_test_instances(std::list<objectstore_perf_stat_t*>& o);
880 };
881 WRITE_CLASS_ENCODER(objectstore_perf_stat_t)
882
883 /** osd_stat
884 * aggregate stats for an osd
885 */
886 struct osd_stat_t {
887 int64_t kb, kb_used, kb_avail;
888 vector<int> hb_peers;
889 int32_t snap_trim_queue_len, num_snap_trimming;
890
891 pow2_hist_t op_queue_age_hist;
892
893 objectstore_perf_stat_t os_perf_stat;
894
895 osd_stat_t() : kb(0), kb_used(0), kb_avail(0),
896 snap_trim_queue_len(0), num_snap_trimming(0) {}
897
898 void add(const osd_stat_t& o) {
899 kb += o.kb;
900 kb_used += o.kb_used;
901 kb_avail += o.kb_avail;
902 snap_trim_queue_len += o.snap_trim_queue_len;
903 num_snap_trimming += o.num_snap_trimming;
904 op_queue_age_hist.add(o.op_queue_age_hist);
905 os_perf_stat.add(o.os_perf_stat);
906 }
907 void sub(const osd_stat_t& o) {
908 kb -= o.kb;
909 kb_used -= o.kb_used;
910 kb_avail -= o.kb_avail;
911 snap_trim_queue_len -= o.snap_trim_queue_len;
912 num_snap_trimming -= o.num_snap_trimming;
913 op_queue_age_hist.sub(o.op_queue_age_hist);
914 os_perf_stat.sub(o.os_perf_stat);
915 }
916
917 void dump(Formatter *f) const;
918 void encode(bufferlist &bl) const;
919 void decode(bufferlist::iterator &bl);
920 static void generate_test_instances(std::list<osd_stat_t*>& o);
921 };
922 WRITE_CLASS_ENCODER(osd_stat_t)
923
924 inline bool operator==(const osd_stat_t& l, const osd_stat_t& r) {
925 return l.kb == r.kb &&
926 l.kb_used == r.kb_used &&
927 l.kb_avail == r.kb_avail &&
928 l.snap_trim_queue_len == r.snap_trim_queue_len &&
929 l.num_snap_trimming == r.num_snap_trimming &&
930 l.hb_peers == r.hb_peers &&
931 l.op_queue_age_hist == r.op_queue_age_hist &&
932 l.os_perf_stat == r.os_perf_stat;
933 }
934 inline bool operator!=(const osd_stat_t& l, const osd_stat_t& r) {
935 return !(l == r);
936 }
937
938
939
940 inline ostream& operator<<(ostream& out, const osd_stat_t& s) {
941 return out << "osd_stat(" << kb_t(s.kb_used) << " used, "
942 << kb_t(s.kb_avail) << " avail, "
943 << kb_t(s.kb) << " total, "
944 << "peers " << s.hb_peers
945 << " op hist " << s.op_queue_age_hist.h
946 << ")";
947 }
948
949
950 /*
951 * pg states
952 */
953 #define PG_STATE_CREATING (1<<0) // creating
954 #define PG_STATE_ACTIVE (1<<1) // i am active. (primary: replicas too)
955 #define PG_STATE_CLEAN (1<<2) // peers are complete, clean of stray replicas.
956 #define PG_STATE_DOWN (1<<4) // a needed replica is down, PG offline
957 //#define PG_STATE_REPLAY (1<<5) // crashed, waiting for replay
958 //#define PG_STATE_STRAY (1<<6) // i must notify the primary i exist.
959 //#define PG_STATE_SPLITTING (1<<7) // i am splitting
960 #define PG_STATE_SCRUBBING (1<<8) // scrubbing
961 //#define PG_STATE_SCRUBQ (1<<9) // queued for scrub
962 #define PG_STATE_DEGRADED (1<<10) // pg contains objects with reduced redundancy
963 #define PG_STATE_INCONSISTENT (1<<11) // pg replicas are inconsistent (but shouldn't be)
964 #define PG_STATE_PEERING (1<<12) // pg is (re)peering
965 #define PG_STATE_REPAIR (1<<13) // pg should repair on next scrub
966 #define PG_STATE_RECOVERING (1<<14) // pg is recovering/migrating objects
967 #define PG_STATE_BACKFILL_WAIT (1<<15) // [active] reserving backfill
968 #define PG_STATE_INCOMPLETE (1<<16) // incomplete content, peering failed.
969 #define PG_STATE_STALE (1<<17) // our state for this pg is stale, unknown.
970 #define PG_STATE_REMAPPED (1<<18) // pg is explicitly remapped to different OSDs than CRUSH
971 #define PG_STATE_DEEP_SCRUB (1<<19) // deep scrub: check CRC32 on files
972 #define PG_STATE_BACKFILL (1<<20) // [active] backfilling pg content
973 #define PG_STATE_BACKFILL_TOOFULL (1<<21) // backfill can't proceed: too full
974 #define PG_STATE_RECOVERY_WAIT (1<<22) // waiting for recovery reservations
975 #define PG_STATE_UNDERSIZED (1<<23) // pg acting < pool size
976 #define PG_STATE_ACTIVATING (1<<24) // pg is peered but not yet active
977 #define PG_STATE_PEERED (1<<25) // peered, cannot go active, can recover
978 #define PG_STATE_SNAPTRIM (1<<26) // trimming snaps
979 #define PG_STATE_SNAPTRIM_WAIT (1<<27) // queued to trim snaps
980 #define PG_STATE_RECOVERY_TOOFULL (1<<28) // recovery can't proceed: too full
981
982 std::string pg_state_string(int state);
983 std::string pg_vector_string(const vector<int32_t> &a);
984 int pg_string_state(const std::string& state);
985
986
987 /*
988 * pool_snap_info_t
989 *
990 * attributes for a single pool snapshot.
991 */
992 struct pool_snap_info_t {
993 snapid_t snapid;
994 utime_t stamp;
995 string name;
996
997 void dump(Formatter *f) const;
998 void encode(bufferlist& bl, uint64_t features) const;
999 void decode(bufferlist::iterator& bl);
1000 static void generate_test_instances(list<pool_snap_info_t*>& o);
1001 };
1002 WRITE_CLASS_ENCODER_FEATURES(pool_snap_info_t)
1003
1004 inline ostream& operator<<(ostream& out, const pool_snap_info_t& si) {
1005 return out << si.snapid << '(' << si.name << ' ' << si.stamp << ')';
1006 }
1007
1008
1009 /*
1010 * pool_opts_t
1011 *
1012 * pool options.
1013 */
1014
1015 class pool_opts_t {
1016 public:
1017 enum key_t {
1018 SCRUB_MIN_INTERVAL,
1019 SCRUB_MAX_INTERVAL,
1020 DEEP_SCRUB_INTERVAL,
1021 RECOVERY_PRIORITY,
1022 RECOVERY_OP_PRIORITY,
1023 SCRUB_PRIORITY,
1024 COMPRESSION_MODE,
1025 COMPRESSION_ALGORITHM,
1026 COMPRESSION_REQUIRED_RATIO,
1027 COMPRESSION_MAX_BLOB_SIZE,
1028 COMPRESSION_MIN_BLOB_SIZE,
1029 CSUM_TYPE,
1030 CSUM_MAX_BLOCK,
1031 CSUM_MIN_BLOCK,
1032 };
1033
1034 enum type_t {
1035 STR,
1036 INT,
1037 DOUBLE,
1038 };
1039
1040 struct opt_desc_t {
1041 key_t key;
1042 type_t type;
1043
1044 opt_desc_t(key_t k, type_t t) : key(k), type(t) {}
1045
1046 bool operator==(const opt_desc_t& rhs) const {
1047 return key == rhs.key && type == rhs.type;
1048 }
1049 };
1050
1051 typedef boost::variant<std::string,int,double> value_t;
1052
1053 static bool is_opt_name(const std::string& name);
1054 static opt_desc_t get_opt_desc(const std::string& name);
1055
1056 pool_opts_t() : opts() {}
1057
1058 bool is_set(key_t key) const;
1059
1060 template<typename T>
1061 void set(key_t key, const T &val) {
1062 value_t value = val;
1063 opts[key] = value;
1064 }
1065
1066 template<typename T>
1067 bool get(key_t key, T *val) const {
1068 opts_t::const_iterator i = opts.find(key);
1069 if (i == opts.end()) {
1070 return false;
1071 }
1072 *val = boost::get<T>(i->second);
1073 return true;
1074 }
1075
1076 const value_t& get(key_t key) const;
1077
1078 bool unset(key_t key);
1079
1080 void dump(const std::string& name, Formatter *f) const;
1081
1082 void dump(Formatter *f) const;
1083 void encode(bufferlist &bl) const;
1084 void decode(bufferlist::iterator &bl);
1085
1086 private:
1087 typedef std::map<key_t, value_t> opts_t;
1088 opts_t opts;
1089
1090 friend ostream& operator<<(ostream& out, const pool_opts_t& opts);
1091 };
1092 WRITE_CLASS_ENCODER(pool_opts_t)
1093
1094 /*
1095 * pg_pool
1096 */
1097 struct pg_pool_t {
1098 enum {
1099 TYPE_REPLICATED = 1, // replication
1100 //TYPE_RAID4 = 2, // raid4 (never implemented)
1101 TYPE_ERASURE = 3, // erasure-coded
1102 };
1103 static const char *get_type_name(int t) {
1104 switch (t) {
1105 case TYPE_REPLICATED: return "replicated";
1106 //case TYPE_RAID4: return "raid4";
1107 case TYPE_ERASURE: return "erasure";
1108 default: return "???";
1109 }
1110 }
1111 const char *get_type_name() const {
1112 return get_type_name(type);
1113 }
1114 static const char* get_default_type() {
1115 return "replicated";
1116 }
1117
1118 enum {
1119 FLAG_HASHPSPOOL = 1<<0, // hash pg seed and pool together (instead of adding)
1120 FLAG_FULL = 1<<1, // pool is full
1121 FLAG_EC_OVERWRITES = 1<<2, // enables overwrites, once enabled, cannot be disabled
1122 FLAG_INCOMPLETE_CLONES = 1<<3, // may have incomplete clones (bc we are/were an overlay)
1123 FLAG_NODELETE = 1<<4, // pool can't be deleted
1124 FLAG_NOPGCHANGE = 1<<5, // pool's pg and pgp num can't be changed
1125 FLAG_NOSIZECHANGE = 1<<6, // pool's size and min size can't be changed
1126 FLAG_WRITE_FADVISE_DONTNEED = 1<<7, // write mode with LIBRADOS_OP_FLAG_FADVISE_DONTNEED
1127 FLAG_NOSCRUB = 1<<8, // block periodic scrub
1128 FLAG_NODEEP_SCRUB = 1<<9, // block periodic deep-scrub
1129 };
1130
1131 static const char *get_flag_name(int f) {
1132 switch (f) {
1133 case FLAG_HASHPSPOOL: return "hashpspool";
1134 case FLAG_FULL: return "full";
1135 case FLAG_EC_OVERWRITES: return "ec_overwrites";
1136 case FLAG_INCOMPLETE_CLONES: return "incomplete_clones";
1137 case FLAG_NODELETE: return "nodelete";
1138 case FLAG_NOPGCHANGE: return "nopgchange";
1139 case FLAG_NOSIZECHANGE: return "nosizechange";
1140 case FLAG_WRITE_FADVISE_DONTNEED: return "write_fadvise_dontneed";
1141 case FLAG_NOSCRUB: return "noscrub";
1142 case FLAG_NODEEP_SCRUB: return "nodeep-scrub";
1143 default: return "???";
1144 }
1145 }
1146 static string get_flags_string(uint64_t f) {
1147 string s;
1148 for (unsigned n=0; f && n<64; ++n) {
1149 if (f & (1ull << n)) {
1150 if (s.length())
1151 s += ",";
1152 s += get_flag_name(1ull << n);
1153 }
1154 }
1155 return s;
1156 }
1157 string get_flags_string() const {
1158 return get_flags_string(flags);
1159 }
1160 static uint64_t get_flag_by_name(const string& name) {
1161 if (name == "hashpspool")
1162 return FLAG_HASHPSPOOL;
1163 if (name == "full")
1164 return FLAG_FULL;
1165 if (name == "ec_overwrites")
1166 return FLAG_EC_OVERWRITES;
1167 if (name == "incomplete_clones")
1168 return FLAG_INCOMPLETE_CLONES;
1169 if (name == "nodelete")
1170 return FLAG_NODELETE;
1171 if (name == "nopgchange")
1172 return FLAG_NOPGCHANGE;
1173 if (name == "nosizechange")
1174 return FLAG_NOSIZECHANGE;
1175 if (name == "write_fadvise_dontneed")
1176 return FLAG_WRITE_FADVISE_DONTNEED;
1177 if (name == "noscrub")
1178 return FLAG_NOSCRUB;
1179 if (name == "nodeep-scrub")
1180 return FLAG_NODEEP_SCRUB;
1181 return 0;
1182 }
1183
1184 /// converts the acting/up vector to a set of pg shards
1185 void convert_to_pg_shards(const vector<int> &from, set<pg_shard_t>* to) const;
1186
1187 typedef enum {
1188 CACHEMODE_NONE = 0, ///< no caching
1189 CACHEMODE_WRITEBACK = 1, ///< write to cache, flush later
1190 CACHEMODE_FORWARD = 2, ///< forward if not in cache
1191 CACHEMODE_READONLY = 3, ///< handle reads, forward writes [not strongly consistent]
1192 CACHEMODE_READFORWARD = 4, ///< forward reads, write to cache flush later
1193 CACHEMODE_READPROXY = 5, ///< proxy reads, write to cache flush later
1194 CACHEMODE_PROXY = 6, ///< proxy if not in cache
1195 } cache_mode_t;
1196 static const char *get_cache_mode_name(cache_mode_t m) {
1197 switch (m) {
1198 case CACHEMODE_NONE: return "none";
1199 case CACHEMODE_WRITEBACK: return "writeback";
1200 case CACHEMODE_FORWARD: return "forward";
1201 case CACHEMODE_READONLY: return "readonly";
1202 case CACHEMODE_READFORWARD: return "readforward";
1203 case CACHEMODE_READPROXY: return "readproxy";
1204 case CACHEMODE_PROXY: return "proxy";
1205 default: return "unknown";
1206 }
1207 }
1208 static cache_mode_t get_cache_mode_from_str(const string& s) {
1209 if (s == "none")
1210 return CACHEMODE_NONE;
1211 if (s == "writeback")
1212 return CACHEMODE_WRITEBACK;
1213 if (s == "forward")
1214 return CACHEMODE_FORWARD;
1215 if (s == "readonly")
1216 return CACHEMODE_READONLY;
1217 if (s == "readforward")
1218 return CACHEMODE_READFORWARD;
1219 if (s == "readproxy")
1220 return CACHEMODE_READPROXY;
1221 if (s == "proxy")
1222 return CACHEMODE_PROXY;
1223 return (cache_mode_t)-1;
1224 }
1225 const char *get_cache_mode_name() const {
1226 return get_cache_mode_name(cache_mode);
1227 }
1228 bool cache_mode_requires_hit_set() const {
1229 switch (cache_mode) {
1230 case CACHEMODE_NONE:
1231 case CACHEMODE_FORWARD:
1232 case CACHEMODE_READONLY:
1233 case CACHEMODE_PROXY:
1234 return false;
1235 case CACHEMODE_WRITEBACK:
1236 case CACHEMODE_READFORWARD:
1237 case CACHEMODE_READPROXY:
1238 return true;
1239 default:
1240 assert(0 == "implement me");
1241 }
1242 }
1243
1244 uint64_t flags; ///< FLAG_*
1245 __u8 type; ///< TYPE_*
1246 __u8 size, min_size; ///< number of osds in each pg
1247 __u8 crush_ruleset; ///< crush placement rule set
1248 __u8 object_hash; ///< hash mapping object name to ps
1249 private:
1250 __u32 pg_num, pgp_num; ///< number of pgs
1251
1252
1253 public:
1254 map<string,string> properties; ///< OBSOLETE
1255 string erasure_code_profile; ///< name of the erasure code profile in OSDMap
1256 epoch_t last_change; ///< most recent epoch changed, exclusing snapshot changes
1257 epoch_t last_force_op_resend; ///< last epoch that forced clients to resend
1258 /// last epoch that forced clients to resend (pre-luminous clients only)
1259 epoch_t last_force_op_resend_preluminous;
1260 snapid_t snap_seq; ///< seq for per-pool snapshot
1261 epoch_t snap_epoch; ///< osdmap epoch of last snap
1262 uint64_t auid; ///< who owns the pg
1263 __u32 crash_replay_interval; ///< seconds to allow clients to replay ACKed but unCOMMITted requests
1264
1265 uint64_t quota_max_bytes; ///< maximum number of bytes for this pool
1266 uint64_t quota_max_objects; ///< maximum number of objects for this pool
1267
1268 /*
1269 * Pool snaps (global to this pool). These define a SnapContext for
1270 * the pool, unless the client manually specifies an alternate
1271 * context.
1272 */
1273 map<snapid_t, pool_snap_info_t> snaps;
1274 /*
1275 * Alternatively, if we are defining non-pool snaps (e.g. via the
1276 * Ceph MDS), we must track @removed_snaps (since @snaps is not
1277 * used). Snaps and removed_snaps are to be used exclusive of each
1278 * other!
1279 */
1280 interval_set<snapid_t> removed_snaps;
1281
1282 unsigned pg_num_mask, pgp_num_mask;
1283
1284 set<uint64_t> tiers; ///< pools that are tiers of us
1285 int64_t tier_of; ///< pool for which we are a tier
1286 // Note that write wins for read+write ops
1287 int64_t read_tier; ///< pool/tier for objecter to direct reads to
1288 int64_t write_tier; ///< pool/tier for objecter to direct writes to
1289 cache_mode_t cache_mode; ///< cache pool mode
1290
1291 bool is_tier() const { return tier_of >= 0; }
1292 bool has_tiers() const { return !tiers.empty(); }
1293 void clear_tier() {
1294 tier_of = -1;
1295 clear_read_tier();
1296 clear_write_tier();
1297 clear_tier_tunables();
1298 }
1299 bool has_read_tier() const { return read_tier >= 0; }
1300 void clear_read_tier() { read_tier = -1; }
1301 bool has_write_tier() const { return write_tier >= 0; }
1302 void clear_write_tier() { write_tier = -1; }
1303 void clear_tier_tunables() {
1304 if (cache_mode != CACHEMODE_NONE)
1305 flags |= FLAG_INCOMPLETE_CLONES;
1306 cache_mode = CACHEMODE_NONE;
1307
1308 target_max_bytes = 0;
1309 target_max_objects = 0;
1310 cache_target_dirty_ratio_micro = 0;
1311 cache_target_dirty_high_ratio_micro = 0;
1312 cache_target_full_ratio_micro = 0;
1313 hit_set_params = HitSet::Params();
1314 hit_set_period = 0;
1315 hit_set_count = 0;
1316 hit_set_grade_decay_rate = 0;
1317 hit_set_search_last_n = 0;
1318 grade_table.resize(0);
1319 }
1320
1321 uint64_t target_max_bytes; ///< tiering: target max pool size
1322 uint64_t target_max_objects; ///< tiering: target max pool size
1323
1324 uint32_t cache_target_dirty_ratio_micro; ///< cache: fraction of target to leave dirty
1325 uint32_t cache_target_dirty_high_ratio_micro; ///<cache: fraction of target to flush with high speed
1326 uint32_t cache_target_full_ratio_micro; ///< cache: fraction of target to fill before we evict in earnest
1327
1328 uint32_t cache_min_flush_age; ///< minimum age (seconds) before we can flush
1329 uint32_t cache_min_evict_age; ///< minimum age (seconds) before we can evict
1330
1331 HitSet::Params hit_set_params; ///< The HitSet params to use on this pool
1332 uint32_t hit_set_period; ///< periodicity of HitSet segments (seconds)
1333 uint32_t hit_set_count; ///< number of periods to retain
1334 bool use_gmt_hitset; ///< use gmt to name the hitset archive object
1335 uint32_t min_read_recency_for_promote; ///< minimum number of HitSet to check before promote on read
1336 uint32_t min_write_recency_for_promote; ///< minimum number of HitSet to check before promote on write
1337 uint32_t hit_set_grade_decay_rate; ///< current hit_set has highest priority on objects
1338 ///temperature count,the follow hit_set's priority decay
1339 ///by this params than pre hit_set
1340 uint32_t hit_set_search_last_n; ///<accumulate atmost N hit_sets for temperature
1341
1342 uint32_t stripe_width; ///< erasure coded stripe size in bytes
1343
1344 uint64_t expected_num_objects; ///< expected number of objects on this pool, a value of 0 indicates
1345 ///< user does not specify any expected value
1346 bool fast_read; ///< whether turn on fast read on the pool or not
1347
1348 pool_opts_t opts; ///< options
1349
1350 private:
1351 vector<uint32_t> grade_table;
1352
1353 public:
1354 uint32_t get_grade(unsigned i) const {
1355 if (grade_table.size() <= i)
1356 return 0;
1357 return grade_table[i];
1358 }
1359 void calc_grade_table() {
1360 unsigned v = 1000000;
1361 grade_table.resize(hit_set_count);
1362 for (unsigned i = 0; i < hit_set_count; i++) {
1363 v = v * (1 - (hit_set_grade_decay_rate / 100.0));
1364 grade_table[i] = v;
1365 }
1366 }
1367
1368 pg_pool_t()
1369 : flags(0), type(0), size(0), min_size(0),
1370 crush_ruleset(0), object_hash(0),
1371 pg_num(0), pgp_num(0),
1372 last_change(0),
1373 last_force_op_resend(0),
1374 last_force_op_resend_preluminous(0),
1375 snap_seq(0), snap_epoch(0),
1376 auid(0),
1377 crash_replay_interval(0),
1378 quota_max_bytes(0), quota_max_objects(0),
1379 pg_num_mask(0), pgp_num_mask(0),
1380 tier_of(-1), read_tier(-1), write_tier(-1),
1381 cache_mode(CACHEMODE_NONE),
1382 target_max_bytes(0), target_max_objects(0),
1383 cache_target_dirty_ratio_micro(0),
1384 cache_target_dirty_high_ratio_micro(0),
1385 cache_target_full_ratio_micro(0),
1386 cache_min_flush_age(0),
1387 cache_min_evict_age(0),
1388 hit_set_params(),
1389 hit_set_period(0),
1390 hit_set_count(0),
1391 use_gmt_hitset(true),
1392 min_read_recency_for_promote(0),
1393 min_write_recency_for_promote(0),
1394 hit_set_grade_decay_rate(0),
1395 hit_set_search_last_n(0),
1396 stripe_width(0),
1397 expected_num_objects(0),
1398 fast_read(false),
1399 opts()
1400 { }
1401
1402 void dump(Formatter *f) const;
1403
1404 uint64_t get_flags() const { return flags; }
1405 bool has_flag(uint64_t f) const { return flags & f; }
1406 void set_flag(uint64_t f) { flags |= f; }
1407 void unset_flag(uint64_t f) { flags &= ~f; }
1408
1409 bool ec_pool() const {
1410 return type == TYPE_ERASURE;
1411 }
1412 bool require_rollback() const {
1413 return ec_pool();
1414 }
1415
1416 /// true if incomplete clones may be present
1417 bool allow_incomplete_clones() const {
1418 return cache_mode != CACHEMODE_NONE || has_flag(FLAG_INCOMPLETE_CLONES);
1419 }
1420
1421 unsigned get_type() const { return type; }
1422 unsigned get_size() const { return size; }
1423 unsigned get_min_size() const { return min_size; }
1424 int get_crush_ruleset() const { return crush_ruleset; }
1425 int get_object_hash() const { return object_hash; }
1426 const char *get_object_hash_name() const {
1427 return ceph_str_hash_name(get_object_hash());
1428 }
1429 epoch_t get_last_change() const { return last_change; }
1430 epoch_t get_last_force_op_resend() const { return last_force_op_resend; }
1431 epoch_t get_last_force_op_resend_preluminous() const {
1432 return last_force_op_resend_preluminous;
1433 }
1434 epoch_t get_snap_epoch() const { return snap_epoch; }
1435 snapid_t get_snap_seq() const { return snap_seq; }
1436 uint64_t get_auid() const { return auid; }
1437 unsigned get_crash_replay_interval() const { return crash_replay_interval; }
1438
1439 void set_snap_seq(snapid_t s) { snap_seq = s; }
1440 void set_snap_epoch(epoch_t e) { snap_epoch = e; }
1441
1442 void set_stripe_width(uint32_t s) { stripe_width = s; }
1443 uint32_t get_stripe_width() const { return stripe_width; }
1444
1445 bool is_replicated() const { return get_type() == TYPE_REPLICATED; }
1446 bool is_erasure() const { return get_type() == TYPE_ERASURE; }
1447
1448 bool supports_omap() const {
1449 return !(get_type() == TYPE_ERASURE);
1450 }
1451
1452 bool requires_aligned_append() const {
1453 return is_erasure() && !has_flag(FLAG_EC_OVERWRITES);
1454 }
1455 uint64_t required_alignment() const { return stripe_width; }
1456
1457 bool allows_ecoverwrites() const {
1458 return has_flag(FLAG_EC_OVERWRITES);
1459 }
1460
1461 bool can_shift_osds() const {
1462 switch (get_type()) {
1463 case TYPE_REPLICATED:
1464 return true;
1465 case TYPE_ERASURE:
1466 return false;
1467 default:
1468 assert(0 == "unhandled pool type");
1469 }
1470 }
1471
1472 unsigned get_pg_num() const { return pg_num; }
1473 unsigned get_pgp_num() const { return pgp_num; }
1474
1475 unsigned get_pg_num_mask() const { return pg_num_mask; }
1476 unsigned get_pgp_num_mask() const { return pgp_num_mask; }
1477
1478 // if pg_num is not a multiple of two, pgs are not equally sized.
1479 // return, for a given pg, the fraction (denominator) of the total
1480 // pool size that it represents.
1481 unsigned get_pg_num_divisor(pg_t pgid) const;
1482
1483 void set_pg_num(int p) {
1484 pg_num = p;
1485 calc_pg_masks();
1486 }
1487 void set_pgp_num(int p) {
1488 pgp_num = p;
1489 calc_pg_masks();
1490 }
1491
1492 void set_quota_max_bytes(uint64_t m) {
1493 quota_max_bytes = m;
1494 }
1495 uint64_t get_quota_max_bytes() {
1496 return quota_max_bytes;
1497 }
1498
1499 void set_quota_max_objects(uint64_t m) {
1500 quota_max_objects = m;
1501 }
1502 uint64_t get_quota_max_objects() {
1503 return quota_max_objects;
1504 }
1505
1506 void set_last_force_op_resend(uint64_t t) {
1507 last_force_op_resend = t;
1508 last_force_op_resend_preluminous = t;
1509 }
1510
1511 void calc_pg_masks();
1512
1513 /*
1514 * we have two snap modes:
1515 * - pool global snaps
1516 * - snap existence/non-existence defined by snaps[] and snap_seq
1517 * - user managed snaps
1518 * - removal governed by removed_snaps
1519 *
1520 * we know which mode we're using based on whether removed_snaps is empty.
1521 * If nothing has been created, both functions report false.
1522 */
1523 bool is_pool_snaps_mode() const;
1524 bool is_unmanaged_snaps_mode() const;
1525 bool is_removed_snap(snapid_t s) const;
1526
1527 /*
1528 * build set of known-removed sets from either pool snaps or
1529 * explicit removed_snaps set.
1530 */
1531 void build_removed_snaps(interval_set<snapid_t>& rs) const;
1532 snapid_t snap_exists(const char *s) const;
1533 void add_snap(const char *n, utime_t stamp);
1534 void add_unmanaged_snap(uint64_t& snapid);
1535 void remove_snap(snapid_t s);
1536 void remove_unmanaged_snap(snapid_t s);
1537
1538 SnapContext get_snap_context() const;
1539
1540 /// hash a object name+namespace key to a hash position
1541 uint32_t hash_key(const string& key, const string& ns) const;
1542
1543 /// round a hash position down to a pg num
1544 uint32_t raw_hash_to_pg(uint32_t v) const;
1545
1546 /*
1547 * map a raw pg (with full precision ps) into an actual pg, for storage
1548 */
1549 pg_t raw_pg_to_pg(pg_t pg) const;
1550
1551 /*
1552 * map raw pg (full precision ps) into a placement seed. include
1553 * pool id in that value so that different pools don't use the same
1554 * seeds.
1555 */
1556 ps_t raw_pg_to_pps(pg_t pg) const;
1557
1558 /// choose a random hash position within a pg
1559 uint32_t get_random_pg_position(pg_t pgid, uint32_t seed) const;
1560
1561 void encode(bufferlist& bl, uint64_t features) const;
1562 void decode(bufferlist::iterator& bl);
1563
1564 static void generate_test_instances(list<pg_pool_t*>& o);
1565 };
1566 WRITE_CLASS_ENCODER_FEATURES(pg_pool_t)
1567
1568 ostream& operator<<(ostream& out, const pg_pool_t& p);
1569
1570
1571 /**
1572 * a summation of object stats
1573 *
1574 * This is just a container for object stats; we don't know what for.
1575 *
1576 * If you add members in object_stat_sum_t, you should make sure there are
1577 * not padding among these members.
1578 * You should also modify the padding_check function.
1579
1580 */
1581 struct object_stat_sum_t {
1582 /**************************************************************************
1583 * WARNING: be sure to update operator==, floor, and split when
1584 * adding/removing fields!
1585 **************************************************************************/
1586 int64_t num_bytes; // in bytes
1587 int64_t num_objects;
1588 int64_t num_object_clones;
1589 int64_t num_object_copies; // num_objects * num_replicas
1590 int64_t num_objects_missing_on_primary;
1591 int64_t num_objects_degraded;
1592 int64_t num_objects_unfound;
1593 int64_t num_rd;
1594 int64_t num_rd_kb;
1595 int64_t num_wr;
1596 int64_t num_wr_kb;
1597 int64_t num_scrub_errors; // total deep and shallow scrub errors
1598 int64_t num_objects_recovered;
1599 int64_t num_bytes_recovered;
1600 int64_t num_keys_recovered;
1601 int64_t num_shallow_scrub_errors;
1602 int64_t num_deep_scrub_errors;
1603 int64_t num_objects_dirty;
1604 int64_t num_whiteouts;
1605 int64_t num_objects_omap;
1606 int64_t num_objects_hit_set_archive;
1607 int64_t num_objects_misplaced;
1608 int64_t num_bytes_hit_set_archive;
1609 int64_t num_flush;
1610 int64_t num_flush_kb;
1611 int64_t num_evict;
1612 int64_t num_evict_kb;
1613 int64_t num_promote;
1614 int32_t num_flush_mode_high; // 1 when in high flush mode, otherwise 0
1615 int32_t num_flush_mode_low; // 1 when in low flush mode, otherwise 0
1616 int32_t num_evict_mode_some; // 1 when in evict some mode, otherwise 0
1617 int32_t num_evict_mode_full; // 1 when in evict full mode, otherwise 0
1618 int64_t num_objects_pinned;
1619 int64_t num_objects_missing;
1620 int64_t num_legacy_snapsets; ///< upper bound on pre-luminous-style SnapSets
1621
1622 object_stat_sum_t()
1623 : num_bytes(0),
1624 num_objects(0), num_object_clones(0), num_object_copies(0),
1625 num_objects_missing_on_primary(0), num_objects_degraded(0),
1626 num_objects_unfound(0),
1627 num_rd(0), num_rd_kb(0), num_wr(0), num_wr_kb(0),
1628 num_scrub_errors(0),
1629 num_objects_recovered(0),
1630 num_bytes_recovered(0),
1631 num_keys_recovered(0),
1632 num_shallow_scrub_errors(0),
1633 num_deep_scrub_errors(0),
1634 num_objects_dirty(0),
1635 num_whiteouts(0),
1636 num_objects_omap(0),
1637 num_objects_hit_set_archive(0),
1638 num_objects_misplaced(0),
1639 num_bytes_hit_set_archive(0),
1640 num_flush(0),
1641 num_flush_kb(0),
1642 num_evict(0),
1643 num_evict_kb(0),
1644 num_promote(0),
1645 num_flush_mode_high(0), num_flush_mode_low(0),
1646 num_evict_mode_some(0), num_evict_mode_full(0),
1647 num_objects_pinned(0),
1648 num_objects_missing(0),
1649 num_legacy_snapsets(0)
1650 {}
1651
1652 void floor(int64_t f) {
1653 #define FLOOR(x) if (x < f) x = f
1654 FLOOR(num_bytes);
1655 FLOOR(num_objects);
1656 FLOOR(num_object_clones);
1657 FLOOR(num_object_copies);
1658 FLOOR(num_objects_missing_on_primary);
1659 FLOOR(num_objects_missing);
1660 FLOOR(num_objects_degraded);
1661 FLOOR(num_objects_misplaced);
1662 FLOOR(num_objects_unfound);
1663 FLOOR(num_rd);
1664 FLOOR(num_rd_kb);
1665 FLOOR(num_wr);
1666 FLOOR(num_wr_kb);
1667 FLOOR(num_scrub_errors);
1668 FLOOR(num_shallow_scrub_errors);
1669 FLOOR(num_deep_scrub_errors);
1670 FLOOR(num_objects_recovered);
1671 FLOOR(num_bytes_recovered);
1672 FLOOR(num_keys_recovered);
1673 FLOOR(num_objects_dirty);
1674 FLOOR(num_whiteouts);
1675 FLOOR(num_objects_omap);
1676 FLOOR(num_objects_hit_set_archive);
1677 FLOOR(num_bytes_hit_set_archive);
1678 FLOOR(num_flush);
1679 FLOOR(num_flush_kb);
1680 FLOOR(num_evict);
1681 FLOOR(num_evict_kb);
1682 FLOOR(num_promote);
1683 FLOOR(num_flush_mode_high);
1684 FLOOR(num_flush_mode_low);
1685 FLOOR(num_evict_mode_some);
1686 FLOOR(num_evict_mode_full);
1687 FLOOR(num_objects_pinned);
1688 FLOOR(num_legacy_snapsets);
1689 #undef FLOOR
1690 }
1691
1692 void split(vector<object_stat_sum_t> &out) const {
1693 #define SPLIT(PARAM) \
1694 for (unsigned i = 0; i < out.size(); ++i) { \
1695 out[i].PARAM = PARAM / out.size(); \
1696 if (i < (PARAM % out.size())) { \
1697 out[i].PARAM++; \
1698 } \
1699 }
1700 #define SPLIT_PRESERVE_NONZERO(PARAM) \
1701 for (unsigned i = 0; i < out.size(); ++i) { \
1702 if (PARAM) \
1703 out[i].PARAM = 1 + PARAM / out.size(); \
1704 else \
1705 out[i].PARAM = 0; \
1706 }
1707
1708 SPLIT(num_bytes);
1709 SPLIT(num_objects);
1710 SPLIT(num_object_clones);
1711 SPLIT(num_object_copies);
1712 SPLIT(num_objects_missing_on_primary);
1713 SPLIT(num_objects_missing);
1714 SPLIT(num_objects_degraded);
1715 SPLIT(num_objects_misplaced);
1716 SPLIT(num_objects_unfound);
1717 SPLIT(num_rd);
1718 SPLIT(num_rd_kb);
1719 SPLIT(num_wr);
1720 SPLIT(num_wr_kb);
1721 SPLIT(num_scrub_errors);
1722 SPLIT(num_shallow_scrub_errors);
1723 SPLIT(num_deep_scrub_errors);
1724 SPLIT(num_objects_recovered);
1725 SPLIT(num_bytes_recovered);
1726 SPLIT(num_keys_recovered);
1727 SPLIT(num_objects_dirty);
1728 SPLIT(num_whiteouts);
1729 SPLIT(num_objects_omap);
1730 SPLIT(num_objects_hit_set_archive);
1731 SPLIT(num_bytes_hit_set_archive);
1732 SPLIT(num_flush);
1733 SPLIT(num_flush_kb);
1734 SPLIT(num_evict);
1735 SPLIT(num_evict_kb);
1736 SPLIT(num_promote);
1737 SPLIT(num_flush_mode_high);
1738 SPLIT(num_flush_mode_low);
1739 SPLIT(num_evict_mode_some);
1740 SPLIT(num_evict_mode_full);
1741 SPLIT(num_objects_pinned);
1742 SPLIT_PRESERVE_NONZERO(num_legacy_snapsets);
1743 #undef SPLIT
1744 #undef SPLIT_PRESERVE_NONZERO
1745 }
1746
1747 void clear() {
1748 memset(this, 0, sizeof(*this));
1749 }
1750
1751 void calc_copies(int nrep) {
1752 num_object_copies = nrep * num_objects;
1753 }
1754
1755 bool is_zero() const {
1756 return mem_is_zero((char*)this, sizeof(*this));
1757 }
1758
1759 void add(const object_stat_sum_t& o);
1760 void sub(const object_stat_sum_t& o);
1761
1762 void dump(Formatter *f) const;
1763 void padding_check() {
1764 static_assert(
1765 sizeof(object_stat_sum_t) ==
1766 sizeof(num_bytes) +
1767 sizeof(num_objects) +
1768 sizeof(num_object_clones) +
1769 sizeof(num_object_copies) +
1770 sizeof(num_objects_missing_on_primary) +
1771 sizeof(num_objects_degraded) +
1772 sizeof(num_objects_unfound) +
1773 sizeof(num_rd) +
1774 sizeof(num_rd_kb) +
1775 sizeof(num_wr) +
1776 sizeof(num_wr_kb) +
1777 sizeof(num_scrub_errors) +
1778 sizeof(num_objects_recovered) +
1779 sizeof(num_bytes_recovered) +
1780 sizeof(num_keys_recovered) +
1781 sizeof(num_shallow_scrub_errors) +
1782 sizeof(num_deep_scrub_errors) +
1783 sizeof(num_objects_dirty) +
1784 sizeof(num_whiteouts) +
1785 sizeof(num_objects_omap) +
1786 sizeof(num_objects_hit_set_archive) +
1787 sizeof(num_objects_misplaced) +
1788 sizeof(num_bytes_hit_set_archive) +
1789 sizeof(num_flush) +
1790 sizeof(num_flush_kb) +
1791 sizeof(num_evict) +
1792 sizeof(num_evict_kb) +
1793 sizeof(num_promote) +
1794 sizeof(num_flush_mode_high) +
1795 sizeof(num_flush_mode_low) +
1796 sizeof(num_evict_mode_some) +
1797 sizeof(num_evict_mode_full) +
1798 sizeof(num_objects_pinned) +
1799 sizeof(num_objects_missing) +
1800 sizeof(num_legacy_snapsets)
1801 ,
1802 "object_stat_sum_t have padding");
1803 }
1804 void encode(bufferlist& bl) const;
1805 void decode(bufferlist::iterator& bl);
1806 static void generate_test_instances(list<object_stat_sum_t*>& o);
1807 };
1808 WRITE_CLASS_ENCODER(object_stat_sum_t)
1809
1810 bool operator==(const object_stat_sum_t& l, const object_stat_sum_t& r);
1811
1812 /**
1813 * a collection of object stat sums
1814 *
1815 * This is a collection of stat sums over different categories.
1816 */
1817 struct object_stat_collection_t {
1818 /**************************************************************************
1819 * WARNING: be sure to update the operator== when adding/removing fields! *
1820 **************************************************************************/
1821 object_stat_sum_t sum;
1822
1823 void calc_copies(int nrep) {
1824 sum.calc_copies(nrep);
1825 }
1826
1827 void dump(Formatter *f) const;
1828 void encode(bufferlist& bl) const;
1829 void decode(bufferlist::iterator& bl);
1830 static void generate_test_instances(list<object_stat_collection_t*>& o);
1831
1832 bool is_zero() const {
1833 return sum.is_zero();
1834 }
1835
1836 void clear() {
1837 sum.clear();
1838 }
1839
1840 void floor(int64_t f) {
1841 sum.floor(f);
1842 }
1843
1844 void add(const object_stat_sum_t& o) {
1845 sum.add(o);
1846 }
1847
1848 void add(const object_stat_collection_t& o) {
1849 sum.add(o.sum);
1850 }
1851 void sub(const object_stat_collection_t& o) {
1852 sum.sub(o.sum);
1853 }
1854 };
1855 WRITE_CLASS_ENCODER(object_stat_collection_t)
1856
1857 inline bool operator==(const object_stat_collection_t& l,
1858 const object_stat_collection_t& r) {
1859 return l.sum == r.sum;
1860 }
1861
1862
1863 /** pg_stat
1864 * aggregate stats for a single PG.
1865 */
1866 struct pg_stat_t {
1867 /**************************************************************************
1868 * WARNING: be sure to update the operator== when adding/removing fields! *
1869 **************************************************************************/
1870 eversion_t version;
1871 version_t reported_seq; // sequence number
1872 epoch_t reported_epoch; // epoch of this report
1873 __u32 state;
1874 utime_t last_fresh; // last reported
1875 utime_t last_change; // new state != previous state
1876 utime_t last_active; // state & PG_STATE_ACTIVE
1877 utime_t last_peered; // state & PG_STATE_ACTIVE || state & PG_STATE_PEERED
1878 utime_t last_clean; // state & PG_STATE_CLEAN
1879 utime_t last_unstale; // (state & PG_STATE_STALE) == 0
1880 utime_t last_undegraded; // (state & PG_STATE_DEGRADED) == 0
1881 utime_t last_fullsized; // (state & PG_STATE_UNDERSIZED) == 0
1882
1883 eversion_t log_start; // (log_start,version]
1884 eversion_t ondisk_log_start; // there may be more on disk
1885
1886 epoch_t created;
1887 epoch_t last_epoch_clean;
1888 pg_t parent;
1889 __u32 parent_split_bits;
1890
1891 eversion_t last_scrub;
1892 eversion_t last_deep_scrub;
1893 utime_t last_scrub_stamp;
1894 utime_t last_deep_scrub_stamp;
1895 utime_t last_clean_scrub_stamp;
1896
1897 object_stat_collection_t stats;
1898
1899 int64_t log_size;
1900 int64_t ondisk_log_size; // >= active_log_size
1901
1902 vector<int32_t> up, acting;
1903 epoch_t mapping_epoch;
1904
1905 vector<int32_t> blocked_by; ///< osds on which the pg is blocked
1906
1907 utime_t last_became_active;
1908 utime_t last_became_peered;
1909
1910 /// up, acting primaries
1911 int32_t up_primary;
1912 int32_t acting_primary;
1913
1914 bool stats_invalid:1;
1915 /// true if num_objects_dirty is not accurate (because it was not
1916 /// maintained starting from pool creation)
1917 bool dirty_stats_invalid:1;
1918 bool omap_stats_invalid:1;
1919 bool hitset_stats_invalid:1;
1920 bool hitset_bytes_stats_invalid:1;
1921 bool pin_stats_invalid:1;
1922
1923 pg_stat_t()
1924 : reported_seq(0),
1925 reported_epoch(0),
1926 state(0),
1927 created(0), last_epoch_clean(0),
1928 parent_split_bits(0),
1929 log_size(0), ondisk_log_size(0),
1930 mapping_epoch(0),
1931 up_primary(-1),
1932 acting_primary(-1),
1933 stats_invalid(false),
1934 dirty_stats_invalid(false),
1935 omap_stats_invalid(false),
1936 hitset_stats_invalid(false),
1937 hitset_bytes_stats_invalid(false),
1938 pin_stats_invalid(false)
1939 { }
1940
1941 epoch_t get_effective_last_epoch_clean() const {
1942 if (state & PG_STATE_CLEAN) {
1943 // we are clean as of this report, and should thus take the
1944 // reported epoch
1945 return reported_epoch;
1946 } else {
1947 return last_epoch_clean;
1948 }
1949 }
1950
1951 pair<epoch_t, version_t> get_version_pair() const {
1952 return make_pair(reported_epoch, reported_seq);
1953 }
1954
1955 void floor(int64_t f) {
1956 stats.floor(f);
1957 if (log_size < f)
1958 log_size = f;
1959 if (ondisk_log_size < f)
1960 ondisk_log_size = f;
1961 }
1962
1963 void add(const pg_stat_t& o) {
1964 stats.add(o.stats);
1965 log_size += o.log_size;
1966 ondisk_log_size += o.ondisk_log_size;
1967 }
1968 void sub(const pg_stat_t& o) {
1969 stats.sub(o.stats);
1970 log_size -= o.log_size;
1971 ondisk_log_size -= o.ondisk_log_size;
1972 }
1973
1974 bool is_acting_osd(int32_t osd, bool primary) const;
1975 void dump(Formatter *f) const;
1976 void dump_brief(Formatter *f) const;
1977 void encode(bufferlist &bl) const;
1978 void decode(bufferlist::iterator &bl);
1979 static void generate_test_instances(list<pg_stat_t*>& o);
1980 };
1981 WRITE_CLASS_ENCODER(pg_stat_t)
1982
1983 bool operator==(const pg_stat_t& l, const pg_stat_t& r);
1984
1985 /*
1986 * summation over an entire pool
1987 */
1988 struct pool_stat_t {
1989 object_stat_collection_t stats;
1990 int64_t log_size;
1991 int64_t ondisk_log_size; // >= active_log_size
1992 int32_t up; ///< number of up replicas or shards
1993 int32_t acting; ///< number of acting replicas or shards
1994
1995 pool_stat_t() : log_size(0), ondisk_log_size(0), up(0), acting(0)
1996 { }
1997
1998 void floor(int64_t f) {
1999 stats.floor(f);
2000 if (log_size < f)
2001 log_size = f;
2002 if (ondisk_log_size < f)
2003 ondisk_log_size = f;
2004 if (up < f)
2005 up = f;
2006 if (acting < f)
2007 acting = f;
2008 }
2009
2010 void add(const pg_stat_t& o) {
2011 stats.add(o.stats);
2012 log_size += o.log_size;
2013 ondisk_log_size += o.ondisk_log_size;
2014 up += o.up.size();
2015 acting += o.acting.size();
2016 }
2017 void sub(const pg_stat_t& o) {
2018 stats.sub(o.stats);
2019 log_size -= o.log_size;
2020 ondisk_log_size -= o.ondisk_log_size;
2021 up -= o.up.size();
2022 acting -= o.acting.size();
2023 }
2024
2025 bool is_zero() const {
2026 return (stats.is_zero() &&
2027 log_size == 0 &&
2028 ondisk_log_size == 0 &&
2029 up == 0 &&
2030 acting == 0);
2031 }
2032
2033 void dump(Formatter *f) const;
2034 void encode(bufferlist &bl, uint64_t features) const;
2035 void decode(bufferlist::iterator &bl);
2036 static void generate_test_instances(list<pool_stat_t*>& o);
2037 };
2038 WRITE_CLASS_ENCODER_FEATURES(pool_stat_t)
2039
2040
2041 // -----------------------------------------
2042
2043 /**
2044 * pg_hit_set_info_t - information about a single recorded HitSet
2045 *
2046 * Track basic metadata about a HitSet, like the nubmer of insertions
2047 * and the time range it covers.
2048 */
2049 struct pg_hit_set_info_t {
2050 utime_t begin, end; ///< time interval
2051 eversion_t version; ///< version this HitSet object was written
2052 bool using_gmt; ///< use gmt for creating the hit_set archive object name
2053
2054 friend bool operator==(const pg_hit_set_info_t& l,
2055 const pg_hit_set_info_t& r) {
2056 return
2057 l.begin == r.begin &&
2058 l.end == r.end &&
2059 l.version == r.version &&
2060 l.using_gmt == r.using_gmt;
2061 }
2062
2063 explicit pg_hit_set_info_t(bool using_gmt = true)
2064 : using_gmt(using_gmt) {}
2065
2066 void encode(bufferlist &bl) const;
2067 void decode(bufferlist::iterator &bl);
2068 void dump(Formatter *f) const;
2069 static void generate_test_instances(list<pg_hit_set_info_t*>& o);
2070 };
2071 WRITE_CLASS_ENCODER(pg_hit_set_info_t)
2072
2073 /**
2074 * pg_hit_set_history_t - information about a history of hitsets
2075 *
2076 * Include information about the currently accumulating hit set as well
2077 * as archived/historical ones.
2078 */
2079 struct pg_hit_set_history_t {
2080 eversion_t current_last_update; ///< last version inserted into current set
2081 list<pg_hit_set_info_t> history; ///< archived sets, sorted oldest -> newest
2082
2083 friend bool operator==(const pg_hit_set_history_t& l,
2084 const pg_hit_set_history_t& r) {
2085 return
2086 l.current_last_update == r.current_last_update &&
2087 l.history == r.history;
2088 }
2089
2090 void encode(bufferlist &bl) const;
2091 void decode(bufferlist::iterator &bl);
2092 void dump(Formatter *f) const;
2093 static void generate_test_instances(list<pg_hit_set_history_t*>& o);
2094 };
2095 WRITE_CLASS_ENCODER(pg_hit_set_history_t)
2096
2097
2098 // -----------------------------------------
2099
2100 /**
2101 * pg_history_t - information about recent pg peering/mapping history
2102 *
2103 * This is aggressively shared between OSDs to bound the amount of past
2104 * history they need to worry about.
2105 */
2106 struct pg_history_t {
2107 epoch_t epoch_created; // epoch in which PG was created
2108 epoch_t last_epoch_started; // lower bound on last epoch started (anywhere, not necessarily locally)
2109 epoch_t last_interval_started; // first epoch of last_epoch_started interval
2110 epoch_t last_epoch_clean; // lower bound on last epoch the PG was completely clean.
2111 epoch_t last_interval_clean; // first epoch of last_epoch_clean interval
2112 epoch_t last_epoch_split; // as parent
2113 epoch_t last_epoch_marked_full; // pool or cluster
2114
2115 /**
2116 * In the event of a map discontinuity, same_*_since may reflect the first
2117 * map the osd has seen in the new map sequence rather than the actual start
2118 * of the interval. This is ok since a discontinuity at epoch e means there
2119 * must have been a clean interval between e and now and that we cannot be
2120 * in the active set during the interval containing e.
2121 */
2122 epoch_t same_up_since; // same acting set since
2123 epoch_t same_interval_since; // same acting AND up set since
2124 epoch_t same_primary_since; // same primary at least back through this epoch.
2125
2126 eversion_t last_scrub;
2127 eversion_t last_deep_scrub;
2128 utime_t last_scrub_stamp;
2129 utime_t last_deep_scrub_stamp;
2130 utime_t last_clean_scrub_stamp;
2131
2132 friend bool operator==(const pg_history_t& l, const pg_history_t& r) {
2133 return
2134 l.epoch_created == r.epoch_created &&
2135 l.last_epoch_started == r.last_epoch_started &&
2136 l.last_interval_started == r.last_interval_started &&
2137 l.last_epoch_clean == r.last_epoch_clean &&
2138 l.last_interval_clean == r.last_interval_clean &&
2139 l.last_epoch_split == r.last_epoch_split &&
2140 l.last_epoch_marked_full == r.last_epoch_marked_full &&
2141 l.same_up_since == r.same_up_since &&
2142 l.same_interval_since == r.same_interval_since &&
2143 l.same_primary_since == r.same_primary_since &&
2144 l.last_scrub == r.last_scrub &&
2145 l.last_deep_scrub == r.last_deep_scrub &&
2146 l.last_scrub_stamp == r.last_scrub_stamp &&
2147 l.last_deep_scrub_stamp == r.last_deep_scrub_stamp &&
2148 l.last_clean_scrub_stamp == r.last_clean_scrub_stamp;
2149 }
2150
2151 pg_history_t()
2152 : epoch_created(0),
2153 last_epoch_started(0),
2154 last_interval_started(0),
2155 last_epoch_clean(0),
2156 last_interval_clean(0),
2157 last_epoch_split(0),
2158 last_epoch_marked_full(0),
2159 same_up_since(0), same_interval_since(0), same_primary_since(0) {}
2160
2161 bool merge(const pg_history_t &other) {
2162 // Here, we only update the fields which cannot be calculated from the OSDmap.
2163 bool modified = false;
2164 if (epoch_created < other.epoch_created) {
2165 epoch_created = other.epoch_created;
2166 modified = true;
2167 }
2168 if (last_epoch_started < other.last_epoch_started) {
2169 last_epoch_started = other.last_epoch_started;
2170 modified = true;
2171 }
2172 if (last_interval_started < other.last_interval_started) {
2173 last_interval_started = other.last_interval_started;
2174 modified = true;
2175 }
2176 if (last_epoch_clean < other.last_epoch_clean) {
2177 last_epoch_clean = other.last_epoch_clean;
2178 modified = true;
2179 }
2180 if (last_interval_clean < other.last_interval_clean) {
2181 last_interval_clean = other.last_interval_clean;
2182 modified = true;
2183 }
2184 if (last_epoch_split < other.last_epoch_split) {
2185 last_epoch_split = other.last_epoch_split;
2186 modified = true;
2187 }
2188 if (last_epoch_marked_full < other.last_epoch_marked_full) {
2189 last_epoch_marked_full = other.last_epoch_marked_full;
2190 modified = true;
2191 }
2192 if (other.last_scrub > last_scrub) {
2193 last_scrub = other.last_scrub;
2194 modified = true;
2195 }
2196 if (other.last_scrub_stamp > last_scrub_stamp) {
2197 last_scrub_stamp = other.last_scrub_stamp;
2198 modified = true;
2199 }
2200 if (other.last_deep_scrub > last_deep_scrub) {
2201 last_deep_scrub = other.last_deep_scrub;
2202 modified = true;
2203 }
2204 if (other.last_deep_scrub_stamp > last_deep_scrub_stamp) {
2205 last_deep_scrub_stamp = other.last_deep_scrub_stamp;
2206 modified = true;
2207 }
2208 if (other.last_clean_scrub_stamp > last_clean_scrub_stamp) {
2209 last_clean_scrub_stamp = other.last_clean_scrub_stamp;
2210 modified = true;
2211 }
2212 return modified;
2213 }
2214
2215 void encode(bufferlist& bl) const;
2216 void decode(bufferlist::iterator& p);
2217 void dump(Formatter *f) const;
2218 static void generate_test_instances(list<pg_history_t*>& o);
2219 };
2220 WRITE_CLASS_ENCODER(pg_history_t)
2221
2222 inline ostream& operator<<(ostream& out, const pg_history_t& h) {
2223 return out << "ec=" << h.epoch_created
2224 << " lis/c " << h.last_interval_started
2225 << "/" << h.last_interval_clean
2226 << " les/c/f " << h.last_epoch_started << "/" << h.last_epoch_clean
2227 << "/" << h.last_epoch_marked_full
2228 << " " << h.same_up_since
2229 << "/" << h.same_interval_since
2230 << "/" << h.same_primary_since;
2231 }
2232
2233
2234 /**
2235 * pg_info_t - summary of PG statistics.
2236 *
2237 * some notes:
2238 * - last_complete implies we have all objects that existed as of that
2239 * stamp, OR a newer object, OR have already applied a later delete.
2240 * - if last_complete >= log.bottom, then we know pg contents thru log.head.
2241 * otherwise, we have no idea what the pg is supposed to contain.
2242 */
2243 struct pg_info_t {
2244 spg_t pgid;
2245 eversion_t last_update; ///< last object version applied to store.
2246 eversion_t last_complete; ///< last version pg was complete through.
2247 epoch_t last_epoch_started; ///< last epoch at which this pg started on this osd
2248 epoch_t last_interval_started; ///< first epoch of last_epoch_started interval
2249
2250 version_t last_user_version; ///< last user object version applied to store
2251
2252 eversion_t log_tail; ///< oldest log entry.
2253
2254 hobject_t last_backfill; ///< objects >= this and < last_complete may be missing
2255 bool last_backfill_bitwise; ///< true if last_backfill reflects a bitwise (vs nibblewise) sort
2256
2257 interval_set<snapid_t> purged_snaps;
2258
2259 pg_stat_t stats;
2260
2261 pg_history_t history;
2262 pg_hit_set_history_t hit_set;
2263
2264 friend bool operator==(const pg_info_t& l, const pg_info_t& r) {
2265 return
2266 l.pgid == r.pgid &&
2267 l.last_update == r.last_update &&
2268 l.last_complete == r.last_complete &&
2269 l.last_epoch_started == r.last_epoch_started &&
2270 l.last_interval_started == r.last_interval_started &&
2271 l.last_user_version == r.last_user_version &&
2272 l.log_tail == r.log_tail &&
2273 l.last_backfill == r.last_backfill &&
2274 l.last_backfill_bitwise == r.last_backfill_bitwise &&
2275 l.purged_snaps == r.purged_snaps &&
2276 l.stats == r.stats &&
2277 l.history == r.history &&
2278 l.hit_set == r.hit_set;
2279 }
2280
2281 pg_info_t()
2282 : last_epoch_started(0),
2283 last_interval_started(0),
2284 last_user_version(0),
2285 last_backfill(hobject_t::get_max()),
2286 last_backfill_bitwise(false)
2287 { }
2288 // cppcheck-suppress noExplicitConstructor
2289 pg_info_t(spg_t p)
2290 : pgid(p),
2291 last_epoch_started(0),
2292 last_interval_started(0),
2293 last_user_version(0),
2294 last_backfill(hobject_t::get_max()),
2295 last_backfill_bitwise(false)
2296 { }
2297
2298 void set_last_backfill(hobject_t pos) {
2299 last_backfill = pos;
2300 last_backfill_bitwise = true;
2301 }
2302
2303 bool is_empty() const { return last_update.version == 0; }
2304 bool dne() const { return history.epoch_created == 0; }
2305
2306 bool is_incomplete() const { return !last_backfill.is_max(); }
2307
2308 void encode(bufferlist& bl) const;
2309 void decode(bufferlist::iterator& p);
2310 void dump(Formatter *f) const;
2311 bool overlaps_with(const pg_info_t &oinfo) const {
2312 return last_update > oinfo.log_tail ?
2313 oinfo.last_update >= log_tail :
2314 last_update >= oinfo.log_tail;
2315 }
2316 static void generate_test_instances(list<pg_info_t*>& o);
2317 };
2318 WRITE_CLASS_ENCODER(pg_info_t)
2319
2320 inline ostream& operator<<(ostream& out, const pg_info_t& pgi)
2321 {
2322 out << pgi.pgid << "(";
2323 if (pgi.dne())
2324 out << " DNE";
2325 if (pgi.is_empty())
2326 out << " empty";
2327 else {
2328 out << " v " << pgi.last_update;
2329 if (pgi.last_complete != pgi.last_update)
2330 out << " lc " << pgi.last_complete;
2331 out << " (" << pgi.log_tail << "," << pgi.last_update << "]";
2332 }
2333 if (pgi.is_incomplete())
2334 out << " lb " << pgi.last_backfill
2335 << (pgi.last_backfill_bitwise ? " (bitwise)" : " (NIBBLEWISE)");
2336 //out << " c " << pgi.epoch_created;
2337 out << " local-lis/les=" << pgi.last_interval_started
2338 << "/" << pgi.last_epoch_started;
2339 out << " n=" << pgi.stats.stats.sum.num_objects;
2340 out << " " << pgi.history
2341 << ")";
2342 return out;
2343 }
2344
2345 /**
2346 * pg_fast_info_t - common pg_info_t fields
2347 *
2348 * These are the fields of pg_info_t (and children) that are updated for
2349 * most IO operations.
2350 *
2351 * ** WARNING **
2352 * Because we rely on these fields to be applied to the normal
2353 * info struct, adding a new field here that is not also new in info
2354 * means that we must set an incompat OSD feature bit!
2355 */
2356 struct pg_fast_info_t {
2357 eversion_t last_update;
2358 eversion_t last_complete;
2359 version_t last_user_version;
2360 struct { // pg_stat_t stats
2361 eversion_t version;
2362 version_t reported_seq;
2363 utime_t last_fresh;
2364 utime_t last_active;
2365 utime_t last_peered;
2366 utime_t last_clean;
2367 utime_t last_unstale;
2368 utime_t last_undegraded;
2369 utime_t last_fullsized;
2370 int64_t log_size; // (also ondisk_log_size, which has the same value)
2371 struct { // object_stat_collection_t stats;
2372 struct { // objct_stat_sum_t sum
2373 int64_t num_bytes; // in bytes
2374 int64_t num_objects;
2375 int64_t num_object_copies;
2376 int64_t num_rd;
2377 int64_t num_rd_kb;
2378 int64_t num_wr;
2379 int64_t num_wr_kb;
2380 int64_t num_objects_dirty;
2381 } sum;
2382 } stats;
2383 } stats;
2384
2385 void populate_from(const pg_info_t& info) {
2386 last_update = info.last_update;
2387 last_complete = info.last_complete;
2388 last_user_version = info.last_user_version;
2389 stats.version = info.stats.version;
2390 stats.reported_seq = info.stats.reported_seq;
2391 stats.last_fresh = info.stats.last_fresh;
2392 stats.last_active = info.stats.last_active;
2393 stats.last_peered = info.stats.last_peered;
2394 stats.last_clean = info.stats.last_clean;
2395 stats.last_unstale = info.stats.last_unstale;
2396 stats.last_undegraded = info.stats.last_undegraded;
2397 stats.last_fullsized = info.stats.last_fullsized;
2398 stats.log_size = info.stats.log_size;
2399 stats.stats.sum.num_bytes = info.stats.stats.sum.num_bytes;
2400 stats.stats.sum.num_objects = info.stats.stats.sum.num_objects;
2401 stats.stats.sum.num_object_copies = info.stats.stats.sum.num_object_copies;
2402 stats.stats.sum.num_rd = info.stats.stats.sum.num_rd;
2403 stats.stats.sum.num_rd_kb = info.stats.stats.sum.num_rd_kb;
2404 stats.stats.sum.num_wr = info.stats.stats.sum.num_wr;
2405 stats.stats.sum.num_wr_kb = info.stats.stats.sum.num_wr_kb;
2406 stats.stats.sum.num_objects_dirty = info.stats.stats.sum.num_objects_dirty;
2407 }
2408
2409 bool try_apply_to(pg_info_t* info) {
2410 if (last_update <= info->last_update)
2411 return false;
2412 info->last_update = last_update;
2413 info->last_complete = last_complete;
2414 info->last_user_version = last_user_version;
2415 info->stats.version = stats.version;
2416 info->stats.reported_seq = stats.reported_seq;
2417 info->stats.last_fresh = stats.last_fresh;
2418 info->stats.last_active = stats.last_active;
2419 info->stats.last_peered = stats.last_peered;
2420 info->stats.last_clean = stats.last_clean;
2421 info->stats.last_unstale = stats.last_unstale;
2422 info->stats.last_undegraded = stats.last_undegraded;
2423 info->stats.last_fullsized = stats.last_fullsized;
2424 info->stats.log_size = stats.log_size;
2425 info->stats.ondisk_log_size = stats.log_size;
2426 info->stats.stats.sum.num_bytes = stats.stats.sum.num_bytes;
2427 info->stats.stats.sum.num_objects = stats.stats.sum.num_objects;
2428 info->stats.stats.sum.num_object_copies = stats.stats.sum.num_object_copies;
2429 info->stats.stats.sum.num_rd = stats.stats.sum.num_rd;
2430 info->stats.stats.sum.num_rd_kb = stats.stats.sum.num_rd_kb;
2431 info->stats.stats.sum.num_wr = stats.stats.sum.num_wr;
2432 info->stats.stats.sum.num_wr_kb = stats.stats.sum.num_wr_kb;
2433 info->stats.stats.sum.num_objects_dirty = stats.stats.sum.num_objects_dirty;
2434 return true;
2435 }
2436
2437 void encode(bufferlist& bl) const {
2438 ENCODE_START(1, 1, bl);
2439 ::encode(last_update, bl);
2440 ::encode(last_complete, bl);
2441 ::encode(last_user_version, bl);
2442 ::encode(stats.version, bl);
2443 ::encode(stats.reported_seq, bl);
2444 ::encode(stats.last_fresh, bl);
2445 ::encode(stats.last_active, bl);
2446 ::encode(stats.last_peered, bl);
2447 ::encode(stats.last_clean, bl);
2448 ::encode(stats.last_unstale, bl);
2449 ::encode(stats.last_undegraded, bl);
2450 ::encode(stats.last_fullsized, bl);
2451 ::encode(stats.log_size, bl);
2452 ::encode(stats.stats.sum.num_bytes, bl);
2453 ::encode(stats.stats.sum.num_objects, bl);
2454 ::encode(stats.stats.sum.num_object_copies, bl);
2455 ::encode(stats.stats.sum.num_rd, bl);
2456 ::encode(stats.stats.sum.num_rd_kb, bl);
2457 ::encode(stats.stats.sum.num_wr, bl);
2458 ::encode(stats.stats.sum.num_wr_kb, bl);
2459 ::encode(stats.stats.sum.num_objects_dirty, bl);
2460 ENCODE_FINISH(bl);
2461 }
2462 void decode(bufferlist::iterator& p) {
2463 DECODE_START(1, p);
2464 ::decode(last_update, p);
2465 ::decode(last_complete, p);
2466 ::decode(last_user_version, p);
2467 ::decode(stats.version, p);
2468 ::decode(stats.reported_seq, p);
2469 ::decode(stats.last_fresh, p);
2470 ::decode(stats.last_active, p);
2471 ::decode(stats.last_peered, p);
2472 ::decode(stats.last_clean, p);
2473 ::decode(stats.last_unstale, p);
2474 ::decode(stats.last_undegraded, p);
2475 ::decode(stats.last_fullsized, p);
2476 ::decode(stats.log_size, p);
2477 ::decode(stats.stats.sum.num_bytes, p);
2478 ::decode(stats.stats.sum.num_objects, p);
2479 ::decode(stats.stats.sum.num_object_copies, p);
2480 ::decode(stats.stats.sum.num_rd, p);
2481 ::decode(stats.stats.sum.num_rd_kb, p);
2482 ::decode(stats.stats.sum.num_wr, p);
2483 ::decode(stats.stats.sum.num_wr_kb, p);
2484 ::decode(stats.stats.sum.num_objects_dirty, p);
2485 DECODE_FINISH(p);
2486 }
2487 };
2488 WRITE_CLASS_ENCODER(pg_fast_info_t)
2489
2490
2491 struct pg_notify_t {
2492 epoch_t query_epoch;
2493 epoch_t epoch_sent;
2494 pg_info_t info;
2495 shard_id_t to;
2496 shard_id_t from;
2497 pg_notify_t() :
2498 query_epoch(0), epoch_sent(0), to(shard_id_t::NO_SHARD),
2499 from(shard_id_t::NO_SHARD) {}
2500 pg_notify_t(
2501 shard_id_t to,
2502 shard_id_t from,
2503 epoch_t query_epoch,
2504 epoch_t epoch_sent,
2505 const pg_info_t &info)
2506 : query_epoch(query_epoch),
2507 epoch_sent(epoch_sent),
2508 info(info), to(to), from(from) {
2509 assert(from == info.pgid.shard);
2510 }
2511 void encode(bufferlist &bl) const;
2512 void decode(bufferlist::iterator &p);
2513 void dump(Formatter *f) const;
2514 static void generate_test_instances(list<pg_notify_t*> &o);
2515 };
2516 WRITE_CLASS_ENCODER(pg_notify_t)
2517 ostream &operator<<(ostream &lhs, const pg_notify_t &notify);
2518
2519
2520 class OSDMap;
2521 /**
2522 * PastIntervals -- information needed to determine the PriorSet and
2523 * the might_have_unfound set
2524 */
2525 class PastIntervals {
2526 public:
2527 struct pg_interval_t {
2528 vector<int32_t> up, acting;
2529 epoch_t first, last;
2530 bool maybe_went_rw;
2531 int32_t primary;
2532 int32_t up_primary;
2533
2534 pg_interval_t()
2535 : first(0), last(0),
2536 maybe_went_rw(false),
2537 primary(-1),
2538 up_primary(-1)
2539 {}
2540
2541 pg_interval_t(
2542 vector<int32_t> &&up,
2543 vector<int32_t> &&acting,
2544 epoch_t first,
2545 epoch_t last,
2546 bool maybe_went_rw,
2547 int32_t primary,
2548 int32_t up_primary)
2549 : up(up), acting(acting), first(first), last(last),
2550 maybe_went_rw(maybe_went_rw), primary(primary), up_primary(up_primary)
2551 {}
2552
2553 void encode(bufferlist& bl) const;
2554 void decode(bufferlist::iterator& bl);
2555 void dump(Formatter *f) const;
2556 static void generate_test_instances(list<pg_interval_t*>& o);
2557 };
2558
2559 PastIntervals() = default;
2560 PastIntervals(bool ec_pool, const OSDMap &osdmap) : PastIntervals() {
2561 update_type_from_map(ec_pool, osdmap);
2562 }
2563 PastIntervals(bool ec_pool, bool compact) : PastIntervals() {
2564 update_type(ec_pool, compact);
2565 }
2566 PastIntervals(PastIntervals &&rhs) = default;
2567 PastIntervals &operator=(PastIntervals &&rhs) = default;
2568
2569 PastIntervals(const PastIntervals &rhs);
2570 PastIntervals &operator=(const PastIntervals &rhs);
2571
2572 class interval_rep {
2573 public:
2574 virtual size_t size() const = 0;
2575 virtual bool empty() const = 0;
2576 virtual void clear() = 0;
2577 virtual pair<epoch_t, epoch_t> get_bounds() const = 0;
2578 virtual set<pg_shard_t> get_all_participants(
2579 bool ec_pool) const = 0;
2580 virtual void add_interval(bool ec_pool, const pg_interval_t &interval) = 0;
2581 virtual unique_ptr<interval_rep> clone() const = 0;
2582 virtual ostream &print(ostream &out) const = 0;
2583 virtual void encode(bufferlist &bl) const = 0;
2584 virtual void decode(bufferlist::iterator &bl) = 0;
2585 virtual void dump(Formatter *f) const = 0;
2586 virtual bool is_classic() const = 0;
2587 virtual void iterate_mayberw_back_to(
2588 bool ec_pool,
2589 epoch_t les,
2590 std::function<void(epoch_t, const set<pg_shard_t> &)> &&f) const = 0;
2591
2592 virtual bool has_full_intervals() const { return false; }
2593 virtual void iterate_all_intervals(
2594 std::function<void(const pg_interval_t &)> &&f) const {
2595 assert(!has_full_intervals());
2596 assert(0 == "not valid for this implementation");
2597 }
2598
2599 virtual ~interval_rep() {}
2600 };
2601 friend class pi_simple_rep;
2602 friend class pi_compact_rep;
2603 private:
2604
2605 unique_ptr<interval_rep> past_intervals;
2606
2607 PastIntervals(interval_rep *rep) : past_intervals(rep) {}
2608
2609 public:
2610 void add_interval(bool ec_pool, const pg_interval_t &interval) {
2611 assert(past_intervals);
2612 return past_intervals->add_interval(ec_pool, interval);
2613 }
2614
2615 bool is_classic() const {
2616 assert(past_intervals);
2617 return past_intervals->is_classic();
2618 }
2619
2620 void encode(bufferlist &bl) const {
2621 ENCODE_START(1, 1, bl);
2622 if (past_intervals) {
2623 __u8 type = is_classic() ? 1 : 2;
2624 ::encode(type, bl);
2625 past_intervals->encode(bl);
2626 } else {
2627 ::encode((__u8)0, bl);
2628 }
2629 ENCODE_FINISH(bl);
2630 }
2631 void encode_classic(bufferlist &bl) const {
2632 if (past_intervals) {
2633 assert(past_intervals->is_classic());
2634 past_intervals->encode(bl);
2635 } else {
2636 // it's a map<>
2637 ::encode((uint32_t)0, bl);
2638 }
2639 }
2640
2641 void decode(bufferlist::iterator &bl);
2642 void decode_classic(bufferlist::iterator &bl);
2643
2644 void dump(Formatter *f) const {
2645 assert(past_intervals);
2646 past_intervals->dump(f);
2647 }
2648 static void generate_test_instances(list<PastIntervals *> & o);
2649
2650 /**
2651 * Determines whether there is an interval change
2652 */
2653 static bool is_new_interval(
2654 int old_acting_primary,
2655 int new_acting_primary,
2656 const vector<int> &old_acting,
2657 const vector<int> &new_acting,
2658 int old_up_primary,
2659 int new_up_primary,
2660 const vector<int> &old_up,
2661 const vector<int> &new_up,
2662 int old_size,
2663 int new_size,
2664 int old_min_size,
2665 int new_min_size,
2666 unsigned old_pg_num,
2667 unsigned new_pg_num,
2668 bool old_sort_bitwise,
2669 bool new_sort_bitwise,
2670 pg_t pgid
2671 );
2672
2673 /**
2674 * Determines whether there is an interval change
2675 */
2676 static bool is_new_interval(
2677 int old_acting_primary, ///< [in] primary as of lastmap
2678 int new_acting_primary, ///< [in] primary as of lastmap
2679 const vector<int> &old_acting, ///< [in] acting as of lastmap
2680 const vector<int> &new_acting, ///< [in] acting as of osdmap
2681 int old_up_primary, ///< [in] up primary of lastmap
2682 int new_up_primary, ///< [in] up primary of osdmap
2683 const vector<int> &old_up, ///< [in] up as of lastmap
2684 const vector<int> &new_up, ///< [in] up as of osdmap
2685 ceph::shared_ptr<const OSDMap> osdmap, ///< [in] current map
2686 ceph::shared_ptr<const OSDMap> lastmap, ///< [in] last map
2687 pg_t pgid ///< [in] pgid for pg
2688 );
2689
2690 /**
2691 * Integrates a new map into *past_intervals, returns true
2692 * if an interval was closed out.
2693 */
2694 static bool check_new_interval(
2695 int old_acting_primary, ///< [in] primary as of lastmap
2696 int new_acting_primary, ///< [in] primary as of osdmap
2697 const vector<int> &old_acting, ///< [in] acting as of lastmap
2698 const vector<int> &new_acting, ///< [in] acting as of osdmap
2699 int old_up_primary, ///< [in] up primary of lastmap
2700 int new_up_primary, ///< [in] up primary of osdmap
2701 const vector<int> &old_up, ///< [in] up as of lastmap
2702 const vector<int> &new_up, ///< [in] up as of osdmap
2703 epoch_t same_interval_since, ///< [in] as of osdmap
2704 epoch_t last_epoch_clean, ///< [in] current
2705 ceph::shared_ptr<const OSDMap> osdmap, ///< [in] current map
2706 ceph::shared_ptr<const OSDMap> lastmap, ///< [in] last map
2707 pg_t pgid, ///< [in] pgid for pg
2708 IsPGRecoverablePredicate *could_have_gone_active, /// [in] predicate whether the pg can be active
2709 PastIntervals *past_intervals, ///< [out] intervals
2710 ostream *out = 0 ///< [out] debug ostream
2711 );
2712 friend ostream& operator<<(ostream& out, const PastIntervals &i);
2713
2714 template <typename F>
2715 void iterate_mayberw_back_to(
2716 bool ec_pool,
2717 epoch_t les,
2718 F &&f) const {
2719 assert(past_intervals);
2720 past_intervals->iterate_mayberw_back_to(ec_pool, les, std::forward<F>(f));
2721 }
2722 void clear() {
2723 assert(past_intervals);
2724 past_intervals->clear();
2725 }
2726
2727 /**
2728 * Should return a value which gives an indication of the amount
2729 * of state contained
2730 */
2731 size_t size() const {
2732 assert(past_intervals);
2733 return past_intervals->size();
2734 }
2735
2736 bool empty() const {
2737 assert(past_intervals);
2738 return past_intervals->empty();
2739 }
2740
2741 void swap(PastIntervals &other) {
2742 ::swap(other.past_intervals, past_intervals);
2743 }
2744
2745 /**
2746 * Return all shards which have been in the acting set back to the
2747 * latest epoch to which we have trimmed except for pg_whoami
2748 */
2749 set<pg_shard_t> get_might_have_unfound(
2750 pg_shard_t pg_whoami,
2751 bool ec_pool) const {
2752 assert(past_intervals);
2753 auto ret = past_intervals->get_all_participants(ec_pool);
2754 ret.erase(pg_whoami);
2755 return ret;
2756 }
2757
2758 /**
2759 * Return all shards which we might want to talk to for peering
2760 */
2761 set<pg_shard_t> get_all_probe(
2762 bool ec_pool) const {
2763 assert(past_intervals);
2764 return past_intervals->get_all_participants(ec_pool);
2765 }
2766
2767 /* Return the set of epochs [start, end) represented by the
2768 * past_interval set.
2769 */
2770 pair<epoch_t, epoch_t> get_bounds() const {
2771 assert(past_intervals);
2772 return past_intervals->get_bounds();
2773 }
2774
2775 enum osd_state_t {
2776 UP,
2777 DOWN,
2778 DNE,
2779 LOST
2780 };
2781 struct PriorSet {
2782 bool ec_pool = false;
2783 set<pg_shard_t> probe; /// current+prior OSDs we need to probe.
2784 set<int> down; /// down osds that would normally be in @a probe and might be interesting.
2785 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
2786
2787 bool pg_down = false; /// some down osds are included in @a cur; the DOWN pg state bit should be set.
2788 unique_ptr<IsPGRecoverablePredicate> pcontdec;
2789
2790 PriorSet() = default;
2791 PriorSet(PriorSet &&) = default;
2792 PriorSet &operator=(PriorSet &&) = default;
2793
2794 PriorSet &operator=(const PriorSet &) = delete;
2795 PriorSet(const PriorSet &) = delete;
2796
2797 bool operator==(const PriorSet &rhs) const {
2798 return (ec_pool == rhs.ec_pool) &&
2799 (probe == rhs.probe) &&
2800 (down == rhs.down) &&
2801 (blocked_by == rhs.blocked_by) &&
2802 (pg_down == rhs.pg_down);
2803 }
2804
2805 bool affected_by_map(
2806 const OSDMap &osdmap,
2807 const DoutPrefixProvider *dpp) const;
2808
2809 // For verifying tests
2810 PriorSet(
2811 bool ec_pool,
2812 set<pg_shard_t> probe,
2813 set<int> down,
2814 map<int, epoch_t> blocked_by,
2815 bool pg_down,
2816 IsPGRecoverablePredicate *pcontdec)
2817 : ec_pool(ec_pool), probe(probe), down(down), blocked_by(blocked_by),
2818 pg_down(pg_down), pcontdec(pcontdec) {}
2819
2820 private:
2821 template <typename F>
2822 PriorSet(
2823 const PastIntervals &past_intervals,
2824 bool ec_pool,
2825 epoch_t last_epoch_started,
2826 IsPGRecoverablePredicate *c,
2827 F f,
2828 const vector<int> &up,
2829 const vector<int> &acting,
2830 const DoutPrefixProvider *dpp);
2831
2832 friend class PastIntervals;
2833 };
2834
2835 void update_type(bool ec_pool, bool compact);
2836 void update_type_from_map(bool ec_pool, const OSDMap &osdmap);
2837
2838 template <typename... Args>
2839 PriorSet get_prior_set(Args&&... args) const {
2840 return PriorSet(*this, std::forward<Args>(args)...);
2841 }
2842 };
2843 WRITE_CLASS_ENCODER(PastIntervals)
2844
2845 ostream& operator<<(ostream& out, const PastIntervals::pg_interval_t& i);
2846 ostream& operator<<(ostream& out, const PastIntervals &i);
2847 ostream& operator<<(ostream& out, const PastIntervals::PriorSet &i);
2848
2849 template <typename F>
2850 PastIntervals::PriorSet::PriorSet(
2851 const PastIntervals &past_intervals,
2852 bool ec_pool,
2853 epoch_t last_epoch_started,
2854 IsPGRecoverablePredicate *c,
2855 F f,
2856 const vector<int> &up,
2857 const vector<int> &acting,
2858 const DoutPrefixProvider *dpp)
2859 : ec_pool(ec_pool), pg_down(false), pcontdec(c)
2860 {
2861 /*
2862 * We have to be careful to gracefully deal with situations like
2863 * so. Say we have a power outage or something that takes out both
2864 * OSDs, but the monitor doesn't mark them down in the same epoch.
2865 * The history may look like
2866 *
2867 * 1: A B
2868 * 2: B
2869 * 3: let's say B dies for good, too (say, from the power spike)
2870 * 4: A
2871 *
2872 * which makes it look like B may have applied updates to the PG
2873 * that we need in order to proceed. This sucks...
2874 *
2875 * To minimize the risk of this happening, we CANNOT go active if
2876 * _any_ OSDs in the prior set are down until we send an MOSDAlive
2877 * to the monitor such that the OSDMap sets osd_up_thru to an epoch.
2878 * Then, we have something like
2879 *
2880 * 1: A B
2881 * 2: B up_thru[B]=0
2882 * 3:
2883 * 4: A
2884 *
2885 * -> we can ignore B, bc it couldn't have gone active (alive_thru
2886 * still 0).
2887 *
2888 * or,
2889 *
2890 * 1: A B
2891 * 2: B up_thru[B]=0
2892 * 3: B up_thru[B]=2
2893 * 4:
2894 * 5: A
2895 *
2896 * -> we must wait for B, bc it was alive through 2, and could have
2897 * written to the pg.
2898 *
2899 * If B is really dead, then an administrator will need to manually
2900 * intervene by marking the OSD as "lost."
2901 */
2902
2903 // Include current acting and up nodes... not because they may
2904 // contain old data (this interval hasn't gone active, obviously),
2905 // but because we want their pg_info to inform choose_acting(), and
2906 // so that we know what they do/do not have explicitly before
2907 // sending them any new info/logs/whatever.
2908 for (unsigned i = 0; i < acting.size(); i++) {
2909 if (acting[i] != 0x7fffffff /* CRUSH_ITEM_NONE, can't import crush.h here */)
2910 probe.insert(pg_shard_t(acting[i], ec_pool ? shard_id_t(i) : shard_id_t::NO_SHARD));
2911 }
2912 // It may be possible to exclude the up nodes, but let's keep them in
2913 // there for now.
2914 for (unsigned i = 0; i < up.size(); i++) {
2915 if (up[i] != 0x7fffffff /* CRUSH_ITEM_NONE, can't import crush.h here */)
2916 probe.insert(pg_shard_t(up[i], ec_pool ? shard_id_t(i) : shard_id_t::NO_SHARD));
2917 }
2918
2919 set<pg_shard_t> all_probe = past_intervals.get_all_probe(ec_pool);
2920 ldpp_dout(dpp, 10) << "build_prior all_probe " << all_probe << dendl;
2921 for (auto &&i: all_probe) {
2922 switch (f(0, i.osd, nullptr)) {
2923 case UP: {
2924 probe.insert(i);
2925 break;
2926 }
2927 case DNE:
2928 case LOST:
2929 case DOWN: {
2930 down.insert(i.osd);
2931 break;
2932 }
2933 }
2934 }
2935
2936 past_intervals.iterate_mayberw_back_to(
2937 ec_pool,
2938 last_epoch_started,
2939 [&](epoch_t start, const set<pg_shard_t> &acting) {
2940 ldpp_dout(dpp, 10) << "build_prior maybe_rw interval:" << start
2941 << ", acting: " << acting << dendl;
2942
2943 // look at candidate osds during this interval. each falls into
2944 // one of three categories: up, down (but potentially
2945 // interesting), or lost (down, but we won't wait for it).
2946 set<pg_shard_t> up_now;
2947 map<int, epoch_t> candidate_blocked_by;
2948 // any candidates down now (that might have useful data)
2949 bool any_down_now = false;
2950
2951 // consider ACTING osds
2952 for (auto &&so: acting) {
2953 epoch_t lost_at = 0;
2954 switch (f(start, so.osd, &lost_at)) {
2955 case UP: {
2956 // include past acting osds if they are up.
2957 up_now.insert(so);
2958 break;
2959 }
2960 case DNE: {
2961 ldpp_dout(dpp, 10) << "build_prior prior osd." << so.osd
2962 << " no longer exists" << dendl;
2963 break;
2964 }
2965 case LOST: {
2966 ldpp_dout(dpp, 10) << "build_prior prior osd." << so.osd
2967 << " is down, but lost_at " << lost_at << dendl;
2968 up_now.insert(so);
2969 break;
2970 }
2971 case DOWN: {
2972 ldpp_dout(dpp, 10) << "build_prior prior osd." << so.osd
2973 << " is down" << dendl;
2974 candidate_blocked_by[so.osd] = lost_at;
2975 any_down_now = true;
2976 break;
2977 }
2978 }
2979 }
2980
2981 // if not enough osds survived this interval, and we may have gone rw,
2982 // then we need to wait for one of those osds to recover to
2983 // ensure that we haven't lost any information.
2984 if (!(*pcontdec)(up_now) && any_down_now) {
2985 // fixme: how do we identify a "clean" shutdown anyway?
2986 ldpp_dout(dpp, 10) << "build_prior possibly went active+rw,"
2987 << " insufficient up; including down osds" << dendl;
2988 assert(!candidate_blocked_by.empty());
2989 pg_down = true;
2990 blocked_by.insert(
2991 candidate_blocked_by.begin(),
2992 candidate_blocked_by.end());
2993 }
2994 });
2995
2996 ldpp_dout(dpp, 10) << "build_prior final: probe " << probe
2997 << " down " << down
2998 << " blocked_by " << blocked_by
2999 << (pg_down ? " pg_down":"")
3000 << dendl;
3001 }
3002
3003 /**
3004 * pg_query_t - used to ask a peer for information about a pg.
3005 *
3006 * note: if version=0, type=LOG, then we just provide our full log.
3007 */
3008 struct pg_query_t {
3009 enum {
3010 INFO = 0,
3011 LOG = 1,
3012 MISSING = 4,
3013 FULLLOG = 5,
3014 };
3015 const char *get_type_name() const {
3016 switch (type) {
3017 case INFO: return "info";
3018 case LOG: return "log";
3019 case MISSING: return "missing";
3020 case FULLLOG: return "fulllog";
3021 default: return "???";
3022 }
3023 }
3024
3025 __s32 type;
3026 eversion_t since;
3027 pg_history_t history;
3028 epoch_t epoch_sent;
3029 shard_id_t to;
3030 shard_id_t from;
3031
3032 pg_query_t() : type(-1), epoch_sent(0), to(shard_id_t::NO_SHARD),
3033 from(shard_id_t::NO_SHARD) {}
3034 pg_query_t(
3035 int t,
3036 shard_id_t to,
3037 shard_id_t from,
3038 const pg_history_t& h,
3039 epoch_t epoch_sent)
3040 : type(t),
3041 history(h),
3042 epoch_sent(epoch_sent),
3043 to(to), from(from) {
3044 assert(t != LOG);
3045 }
3046 pg_query_t(
3047 int t,
3048 shard_id_t to,
3049 shard_id_t from,
3050 eversion_t s,
3051 const pg_history_t& h,
3052 epoch_t epoch_sent)
3053 : type(t), since(s), history(h),
3054 epoch_sent(epoch_sent), to(to), from(from) {
3055 assert(t == LOG);
3056 }
3057
3058 void encode(bufferlist &bl, uint64_t features) const;
3059 void decode(bufferlist::iterator &bl);
3060
3061 void dump(Formatter *f) const;
3062 static void generate_test_instances(list<pg_query_t*>& o);
3063 };
3064 WRITE_CLASS_ENCODER_FEATURES(pg_query_t)
3065
3066 inline ostream& operator<<(ostream& out, const pg_query_t& q) {
3067 out << "query(" << q.get_type_name() << " " << q.since;
3068 if (q.type == pg_query_t::LOG)
3069 out << " " << q.history;
3070 out << ")";
3071 return out;
3072 }
3073
3074 class PGBackend;
3075 class ObjectModDesc {
3076 bool can_local_rollback;
3077 bool rollback_info_completed;
3078
3079 // version required to decode, reflected in encode/decode version
3080 __u8 max_required_version = 1;
3081 public:
3082 class Visitor {
3083 public:
3084 virtual void append(uint64_t old_offset) {}
3085 virtual void setattrs(map<string, boost::optional<bufferlist> > &attrs) {}
3086 virtual void rmobject(version_t old_version) {}
3087 /**
3088 * Used to support the unfound_lost_delete log event: if the stashed
3089 * version exists, we unstash it, otherwise, we do nothing. This way
3090 * each replica rolls back to whatever state it had prior to the attempt
3091 * at mark unfound lost delete
3092 */
3093 virtual void try_rmobject(version_t old_version) {
3094 rmobject(old_version);
3095 }
3096 virtual void create() {}
3097 virtual void update_snaps(const set<snapid_t> &old_snaps) {}
3098 virtual void rollback_extents(
3099 version_t gen,
3100 const vector<pair<uint64_t, uint64_t> > &extents) {}
3101 virtual ~Visitor() {}
3102 };
3103 void visit(Visitor *visitor) const;
3104 mutable bufferlist bl;
3105 enum ModID {
3106 APPEND = 1,
3107 SETATTRS = 2,
3108 DELETE = 3,
3109 CREATE = 4,
3110 UPDATE_SNAPS = 5,
3111 TRY_DELETE = 6,
3112 ROLLBACK_EXTENTS = 7
3113 };
3114 ObjectModDesc() : can_local_rollback(true), rollback_info_completed(false) {}
3115 void claim(ObjectModDesc &other) {
3116 bl.clear();
3117 bl.claim(other.bl);
3118 can_local_rollback = other.can_local_rollback;
3119 rollback_info_completed = other.rollback_info_completed;
3120 }
3121 void claim_append(ObjectModDesc &other) {
3122 if (!can_local_rollback || rollback_info_completed)
3123 return;
3124 if (!other.can_local_rollback) {
3125 mark_unrollbackable();
3126 return;
3127 }
3128 bl.claim_append(other.bl);
3129 rollback_info_completed = other.rollback_info_completed;
3130 }
3131 void swap(ObjectModDesc &other) {
3132 bl.swap(other.bl);
3133
3134 ::swap(other.can_local_rollback, can_local_rollback);
3135 ::swap(other.rollback_info_completed, rollback_info_completed);
3136 ::swap(other.max_required_version, max_required_version);
3137 }
3138 void append_id(ModID id) {
3139 uint8_t _id(id);
3140 ::encode(_id, bl);
3141 }
3142 void append(uint64_t old_size) {
3143 if (!can_local_rollback || rollback_info_completed)
3144 return;
3145 ENCODE_START(1, 1, bl);
3146 append_id(APPEND);
3147 ::encode(old_size, bl);
3148 ENCODE_FINISH(bl);
3149 }
3150 void setattrs(map<string, boost::optional<bufferlist> > &old_attrs) {
3151 if (!can_local_rollback || rollback_info_completed)
3152 return;
3153 ENCODE_START(1, 1, bl);
3154 append_id(SETATTRS);
3155 ::encode(old_attrs, bl);
3156 ENCODE_FINISH(bl);
3157 }
3158 bool rmobject(version_t deletion_version) {
3159 if (!can_local_rollback || rollback_info_completed)
3160 return false;
3161 ENCODE_START(1, 1, bl);
3162 append_id(DELETE);
3163 ::encode(deletion_version, bl);
3164 ENCODE_FINISH(bl);
3165 rollback_info_completed = true;
3166 return true;
3167 }
3168 bool try_rmobject(version_t deletion_version) {
3169 if (!can_local_rollback || rollback_info_completed)
3170 return false;
3171 ENCODE_START(1, 1, bl);
3172 append_id(TRY_DELETE);
3173 ::encode(deletion_version, bl);
3174 ENCODE_FINISH(bl);
3175 rollback_info_completed = true;
3176 return true;
3177 }
3178 void create() {
3179 if (!can_local_rollback || rollback_info_completed)
3180 return;
3181 rollback_info_completed = true;
3182 ENCODE_START(1, 1, bl);
3183 append_id(CREATE);
3184 ENCODE_FINISH(bl);
3185 }
3186 void update_snaps(const set<snapid_t> &old_snaps) {
3187 if (!can_local_rollback || rollback_info_completed)
3188 return;
3189 ENCODE_START(1, 1, bl);
3190 append_id(UPDATE_SNAPS);
3191 ::encode(old_snaps, bl);
3192 ENCODE_FINISH(bl);
3193 }
3194 void rollback_extents(
3195 version_t gen, const vector<pair<uint64_t, uint64_t> > &extents) {
3196 assert(can_local_rollback);
3197 assert(!rollback_info_completed);
3198 if (max_required_version < 2)
3199 max_required_version = 2;
3200 ENCODE_START(2, 2, bl);
3201 append_id(ROLLBACK_EXTENTS);
3202 ::encode(gen, bl);
3203 ::encode(extents, bl);
3204 ENCODE_FINISH(bl);
3205 }
3206
3207 // cannot be rolled back
3208 void mark_unrollbackable() {
3209 can_local_rollback = false;
3210 bl.clear();
3211 }
3212 bool can_rollback() const {
3213 return can_local_rollback;
3214 }
3215 bool empty() const {
3216 return can_local_rollback && (bl.length() == 0);
3217 }
3218
3219 bool requires_kraken() const {
3220 return max_required_version >= 2;
3221 }
3222
3223 /**
3224 * Create fresh copy of bl bytes to avoid keeping large buffers around
3225 * in the case that bl contains ptrs which point into a much larger
3226 * message buffer
3227 */
3228 void trim_bl() {
3229 if (bl.length() > 0)
3230 bl.rebuild();
3231 }
3232 void encode(bufferlist &bl) const;
3233 void decode(bufferlist::iterator &bl);
3234 void dump(Formatter *f) const;
3235 static void generate_test_instances(list<ObjectModDesc*>& o);
3236 };
3237 WRITE_CLASS_ENCODER(ObjectModDesc)
3238
3239
3240 /**
3241 * pg_log_entry_t - single entry/event in pg log
3242 *
3243 */
3244 struct pg_log_entry_t {
3245 enum {
3246 MODIFY = 1, // some unspecified modification (but not *all* modifications)
3247 CLONE = 2, // cloned object from head
3248 DELETE = 3, // deleted object
3249 BACKLOG = 4, // event invented by generate_backlog [deprecated]
3250 LOST_REVERT = 5, // lost new version, revert to an older version.
3251 LOST_DELETE = 6, // lost new version, revert to no object (deleted).
3252 LOST_MARK = 7, // lost new version, now EIO
3253 PROMOTE = 8, // promoted object from another tier
3254 CLEAN = 9, // mark an object clean
3255 ERROR = 10, // write that returned an error
3256 };
3257 static const char *get_op_name(int op) {
3258 switch (op) {
3259 case MODIFY:
3260 return "modify";
3261 case PROMOTE:
3262 return "promote";
3263 case CLONE:
3264 return "clone";
3265 case DELETE:
3266 return "delete";
3267 case BACKLOG:
3268 return "backlog";
3269 case LOST_REVERT:
3270 return "l_revert";
3271 case LOST_DELETE:
3272 return "l_delete";
3273 case LOST_MARK:
3274 return "l_mark";
3275 case CLEAN:
3276 return "clean";
3277 case ERROR:
3278 return "error";
3279 default:
3280 return "unknown";
3281 }
3282 }
3283 const char *get_op_name() const {
3284 return get_op_name(op);
3285 }
3286
3287 // describes state for a locally-rollbackable entry
3288 ObjectModDesc mod_desc;
3289 bufferlist snaps; // only for clone entries
3290 hobject_t soid;
3291 osd_reqid_t reqid; // caller+tid to uniquely identify request
3292 vector<pair<osd_reqid_t, version_t> > extra_reqids;
3293 eversion_t version, prior_version, reverting_to;
3294 version_t user_version; // the user version for this entry
3295 utime_t mtime; // this is the _user_ mtime, mind you
3296 int32_t return_code; // only stored for ERRORs for dup detection
3297
3298 __s32 op;
3299 bool invalid_hash; // only when decoding sobject_t based entries
3300 bool invalid_pool; // only when decoding pool-less hobject based entries
3301
3302 pg_log_entry_t()
3303 : user_version(0), return_code(0), op(0),
3304 invalid_hash(false), invalid_pool(false) {}
3305 pg_log_entry_t(int _op, const hobject_t& _soid,
3306 const eversion_t& v, const eversion_t& pv,
3307 version_t uv,
3308 const osd_reqid_t& rid, const utime_t& mt,
3309 int return_code)
3310 : soid(_soid), reqid(rid), version(v), prior_version(pv), user_version(uv),
3311 mtime(mt), return_code(return_code), op(_op),
3312 invalid_hash(false), invalid_pool(false)
3313 {}
3314
3315 bool is_clone() const { return op == CLONE; }
3316 bool is_modify() const { return op == MODIFY; }
3317 bool is_promote() const { return op == PROMOTE; }
3318 bool is_clean() const { return op == CLEAN; }
3319 bool is_backlog() const { return op == BACKLOG; }
3320 bool is_lost_revert() const { return op == LOST_REVERT; }
3321 bool is_lost_delete() const { return op == LOST_DELETE; }
3322 bool is_lost_mark() const { return op == LOST_MARK; }
3323 bool is_error() const { return op == ERROR; }
3324
3325 bool is_update() const {
3326 return
3327 is_clone() || is_modify() || is_promote() || is_clean() ||
3328 is_backlog() || is_lost_revert() || is_lost_mark();
3329 }
3330 bool is_delete() const {
3331 return op == DELETE || op == LOST_DELETE;
3332 }
3333
3334 bool can_rollback() const {
3335 return mod_desc.can_rollback();
3336 }
3337
3338 void mark_unrollbackable() {
3339 mod_desc.mark_unrollbackable();
3340 }
3341
3342 bool requires_kraken() const {
3343 return mod_desc.requires_kraken();
3344 }
3345
3346 // Errors are only used for dup detection, whereas
3347 // the index by objects is used by recovery, copy_get,
3348 // and other facilities that don't expect or need to
3349 // be aware of error entries.
3350 bool object_is_indexed() const {
3351 return !is_error();
3352 }
3353
3354 bool reqid_is_indexed() const {
3355 return reqid != osd_reqid_t() &&
3356 (op == MODIFY || op == DELETE || op == ERROR);
3357 }
3358
3359 string get_key_name() const;
3360 void encode_with_checksum(bufferlist& bl) const;
3361 void decode_with_checksum(bufferlist::iterator& p);
3362
3363 void encode(bufferlist &bl) const;
3364 void decode(bufferlist::iterator &bl);
3365 void dump(Formatter *f) const;
3366 static void generate_test_instances(list<pg_log_entry_t*>& o);
3367
3368 };
3369 WRITE_CLASS_ENCODER(pg_log_entry_t)
3370
3371 ostream& operator<<(ostream& out, const pg_log_entry_t& e);
3372
3373
3374
3375 /**
3376 * pg_log_t - incremental log of recent pg changes.
3377 *
3378 * serves as a recovery queue for recent changes.
3379 */
3380 struct pg_log_t {
3381 /*
3382 * head - newest entry (update|delete)
3383 * tail - entry previous to oldest (update|delete) for which we have
3384 * complete negative information.
3385 * i.e. we can infer pg contents for any store whose last_update >= tail.
3386 */
3387 eversion_t head; // newest entry
3388 eversion_t tail; // version prior to oldest
3389
3390 protected:
3391 // We can rollback rollback-able entries > can_rollback_to
3392 eversion_t can_rollback_to;
3393
3394 // always <= can_rollback_to, indicates how far stashed rollback
3395 // data can be found
3396 eversion_t rollback_info_trimmed_to;
3397
3398 public:
3399 mempool::osd::list<pg_log_entry_t> log; // the actual log.
3400
3401 pg_log_t() = default;
3402 pg_log_t(const eversion_t &last_update,
3403 const eversion_t &log_tail,
3404 const eversion_t &can_rollback_to,
3405 const eversion_t &rollback_info_trimmed_to,
3406 mempool::osd::list<pg_log_entry_t> &&entries)
3407 : head(last_update), tail(log_tail), can_rollback_to(can_rollback_to),
3408 rollback_info_trimmed_to(rollback_info_trimmed_to),
3409 log(std::move(entries)) {}
3410 pg_log_t(const eversion_t &last_update,
3411 const eversion_t &log_tail,
3412 const eversion_t &can_rollback_to,
3413 const eversion_t &rollback_info_trimmed_to,
3414 const std::list<pg_log_entry_t> &entries)
3415 : head(last_update), tail(log_tail), can_rollback_to(can_rollback_to),
3416 rollback_info_trimmed_to(rollback_info_trimmed_to) {
3417 for (auto &&entry: entries) {
3418 log.push_back(entry);
3419 }
3420 }
3421
3422 void clear() {
3423 eversion_t z;
3424 rollback_info_trimmed_to = can_rollback_to = head = tail = z;
3425 log.clear();
3426 }
3427
3428 eversion_t get_rollback_info_trimmed_to() const {
3429 return rollback_info_trimmed_to;
3430 }
3431 eversion_t get_can_rollback_to() const {
3432 return can_rollback_to;
3433 }
3434
3435
3436 pg_log_t split_out_child(pg_t child_pgid, unsigned split_bits) {
3437 mempool::osd::list<pg_log_entry_t> oldlog, childlog;
3438 oldlog.swap(log);
3439
3440 eversion_t old_tail;
3441 unsigned mask = ~((~0)<<split_bits);
3442 for (auto i = oldlog.begin();
3443 i != oldlog.end();
3444 ) {
3445 if ((i->soid.get_hash() & mask) == child_pgid.m_seed) {
3446 childlog.push_back(*i);
3447 } else {
3448 log.push_back(*i);
3449 }
3450 oldlog.erase(i++);
3451 }
3452
3453 return pg_log_t(
3454 head,
3455 tail,
3456 can_rollback_to,
3457 rollback_info_trimmed_to,
3458 std::move(childlog));
3459 }
3460
3461 mempool::osd::list<pg_log_entry_t> rewind_from_head(eversion_t newhead) {
3462 assert(newhead >= tail);
3463
3464 mempool::osd::list<pg_log_entry_t>::iterator p = log.end();
3465 mempool::osd::list<pg_log_entry_t> divergent;
3466 while (true) {
3467 if (p == log.begin()) {
3468 // yikes, the whole thing is divergent!
3469 ::swap(divergent, log);
3470 break;
3471 }
3472 --p;
3473 if (p->version.version <= newhead.version) {
3474 /*
3475 * look at eversion.version here. we want to avoid a situation like:
3476 * our log: 100'10 (0'0) m 10000004d3a.00000000/head by client4225.1:18529
3477 * new log: 122'10 (0'0) m 10000004d3a.00000000/head by client4225.1:18529
3478 * lower_bound = 100'9
3479 * i.e, same request, different version. If the eversion.version is > the
3480 * lower_bound, we it is divergent.
3481 */
3482 ++p;
3483 divergent.splice(divergent.begin(), log, p, log.end());
3484 break;
3485 }
3486 assert(p->version > newhead);
3487 }
3488 head = newhead;
3489
3490 if (can_rollback_to > newhead)
3491 can_rollback_to = newhead;
3492
3493 if (rollback_info_trimmed_to > newhead)
3494 rollback_info_trimmed_to = newhead;
3495
3496 return divergent;
3497 }
3498
3499 bool empty() const {
3500 return log.empty();
3501 }
3502
3503 bool null() const {
3504 return head.version == 0 && head.epoch == 0;
3505 }
3506
3507 size_t approx_size() const {
3508 return head.version - tail.version;
3509 }
3510
3511 static void filter_log(spg_t import_pgid, const OSDMap &curmap,
3512 const string &hit_set_namespace, const pg_log_t &in,
3513 pg_log_t &out, pg_log_t &reject);
3514
3515 /**
3516 * copy entries from the tail of another pg_log_t
3517 *
3518 * @param other pg_log_t to copy from
3519 * @param from copy entries after this version
3520 */
3521 void copy_after(const pg_log_t &other, eversion_t from);
3522
3523 /**
3524 * copy a range of entries from another pg_log_t
3525 *
3526 * @param other pg_log_t to copy from
3527 * @param from copy entries after this version
3528 * @param to up to and including this version
3529 */
3530 void copy_range(const pg_log_t &other, eversion_t from, eversion_t to);
3531
3532 /**
3533 * copy up to N entries
3534 *
3535 * @param other source log
3536 * @param max max number of entries to copy
3537 */
3538 void copy_up_to(const pg_log_t &other, int max);
3539
3540 ostream& print(ostream& out) const;
3541
3542 void encode(bufferlist &bl) const;
3543 void decode(bufferlist::iterator &bl, int64_t pool = -1);
3544 void dump(Formatter *f) const;
3545 static void generate_test_instances(list<pg_log_t*>& o);
3546 };
3547 WRITE_CLASS_ENCODER(pg_log_t)
3548
3549 inline ostream& operator<<(ostream& out, const pg_log_t& log)
3550 {
3551 out << "log((" << log.tail << "," << log.head << "], crt="
3552 << log.get_can_rollback_to() << ")";
3553 return out;
3554 }
3555
3556
3557 /**
3558 * pg_missing_t - summary of missing objects.
3559 *
3560 * kept in memory, as a supplement to pg_log_t
3561 * also used to pass missing info in messages.
3562 */
3563 struct pg_missing_item {
3564 eversion_t need, have;
3565 pg_missing_item() {}
3566 explicit pg_missing_item(eversion_t n) : need(n) {} // have no old version
3567 pg_missing_item(eversion_t n, eversion_t h) : need(n), have(h) {}
3568
3569 void encode(bufferlist& bl) const {
3570 ::encode(need, bl);
3571 ::encode(have, bl);
3572 }
3573 void decode(bufferlist::iterator& bl) {
3574 ::decode(need, bl);
3575 ::decode(have, bl);
3576 }
3577 void dump(Formatter *f) const {
3578 f->dump_stream("need") << need;
3579 f->dump_stream("have") << have;
3580 }
3581 static void generate_test_instances(list<pg_missing_item*>& o) {
3582 o.push_back(new pg_missing_item);
3583 o.push_back(new pg_missing_item);
3584 o.back()->need = eversion_t(1, 2);
3585 o.back()->have = eversion_t(1, 1);
3586 }
3587 bool operator==(const pg_missing_item &rhs) const {
3588 return need == rhs.need && have == rhs.have;
3589 }
3590 bool operator!=(const pg_missing_item &rhs) const {
3591 return !(*this == rhs);
3592 }
3593 };
3594 WRITE_CLASS_ENCODER(pg_missing_item)
3595 ostream& operator<<(ostream& out, const pg_missing_item &item);
3596
3597 class pg_missing_const_i {
3598 public:
3599 virtual const map<hobject_t, pg_missing_item> &
3600 get_items() const = 0;
3601 virtual const map<version_t, hobject_t> &get_rmissing() const = 0;
3602 virtual unsigned int num_missing() const = 0;
3603 virtual bool have_missing() const = 0;
3604 virtual bool is_missing(const hobject_t& oid, pg_missing_item *out = nullptr) const = 0;
3605 virtual bool is_missing(const hobject_t& oid, eversion_t v) const = 0;
3606 virtual eversion_t have_old(const hobject_t& oid) const = 0;
3607 virtual ~pg_missing_const_i() {}
3608 };
3609
3610
3611 template <bool Track>
3612 class ChangeTracker {
3613 public:
3614 void changed(const hobject_t &obj) {}
3615 template <typename F>
3616 void get_changed(F &&f) const {}
3617 void flush() {}
3618 bool is_clean() const {
3619 return true;
3620 }
3621 };
3622 template <>
3623 class ChangeTracker<true> {
3624 set<hobject_t> _changed;
3625 public:
3626 void changed(const hobject_t &obj) {
3627 _changed.insert(obj);
3628 }
3629 template <typename F>
3630 void get_changed(F &&f) const {
3631 for (auto const &i: _changed) {
3632 f(i);
3633 }
3634 }
3635 void flush() {
3636 _changed.clear();
3637 }
3638 bool is_clean() const {
3639 return _changed.empty();
3640 }
3641 };
3642
3643 template <bool TrackChanges>
3644 class pg_missing_set : public pg_missing_const_i {
3645 using item = pg_missing_item;
3646 map<hobject_t, item> missing; // oid -> (need v, have v)
3647 map<version_t, hobject_t> rmissing; // v -> oid
3648 ChangeTracker<TrackChanges> tracker;
3649
3650 public:
3651 pg_missing_set() = default;
3652
3653 template <typename missing_type>
3654 pg_missing_set(const missing_type &m) {
3655 for (auto &&i: missing)
3656 tracker.changed(i.first);
3657 missing = m.get_items();
3658 rmissing = m.get_rmissing();
3659 for (auto &&i: missing)
3660 tracker.changed(i.first);
3661 }
3662
3663 const map<hobject_t, item> &get_items() const override {
3664 return missing;
3665 }
3666 const map<version_t, hobject_t> &get_rmissing() const override {
3667 return rmissing;
3668 }
3669 unsigned int num_missing() const override {
3670 return missing.size();
3671 }
3672 bool have_missing() const override {
3673 return !missing.empty();
3674 }
3675 bool is_missing(const hobject_t& oid, pg_missing_item *out = nullptr) const override {
3676 auto iter = missing.find(oid);
3677 if (iter == missing.end())
3678 return false;
3679 if (out)
3680 *out = iter->second;
3681 return true;
3682 }
3683 bool is_missing(const hobject_t& oid, eversion_t v) const override {
3684 map<hobject_t, item>::const_iterator m =
3685 missing.find(oid);
3686 if (m == missing.end())
3687 return false;
3688 const item &item(m->second);
3689 if (item.need > v)
3690 return false;
3691 return true;
3692 }
3693 eversion_t have_old(const hobject_t& oid) const override {
3694 map<hobject_t, item>::const_iterator m =
3695 missing.find(oid);
3696 if (m == missing.end())
3697 return eversion_t();
3698 const item &item(m->second);
3699 return item.have;
3700 }
3701
3702 void claim(pg_missing_set& o) {
3703 static_assert(!TrackChanges, "Can't use claim with TrackChanges");
3704 missing.swap(o.missing);
3705 rmissing.swap(o.rmissing);
3706 }
3707
3708 /*
3709 * this needs to be called in log order as we extend the log. it
3710 * assumes missing is accurate up through the previous log entry.
3711 */
3712 void add_next_event(const pg_log_entry_t& e) {
3713 if (e.is_update()) {
3714 map<hobject_t, item>::iterator missing_it;
3715 missing_it = missing.find(e.soid);
3716 bool is_missing_divergent_item = missing_it != missing.end();
3717 if (e.prior_version == eversion_t() || e.is_clone()) {
3718 // new object.
3719 if (is_missing_divergent_item) { // use iterator
3720 rmissing.erase((missing_it->second).need.version);
3721 missing_it->second = item(e.version, eversion_t()); // .have = nil
3722 } else // create new element in missing map
3723 missing[e.soid] = item(e.version, eversion_t()); // .have = nil
3724 } else if (is_missing_divergent_item) {
3725 // already missing (prior).
3726 rmissing.erase((missing_it->second).need.version);
3727 (missing_it->second).need = e.version; // leave .have unchanged.
3728 } else if (e.is_backlog()) {
3729 // May not have prior version
3730 assert(0 == "these don't exist anymore");
3731 } else {
3732 // not missing, we must have prior_version (if any)
3733 assert(!is_missing_divergent_item);
3734 missing[e.soid] = item(e.version, e.prior_version);
3735 }
3736 rmissing[e.version.version] = e.soid;
3737 } else if (e.is_delete()) {
3738 rm(e.soid, e.version);
3739 }
3740
3741 tracker.changed(e.soid);
3742 }
3743
3744 void revise_need(hobject_t oid, eversion_t need) {
3745 if (missing.count(oid)) {
3746 rmissing.erase(missing[oid].need.version);
3747 missing[oid].need = need; // no not adjust .have
3748 } else {
3749 missing[oid] = item(need, eversion_t());
3750 }
3751 rmissing[need.version] = oid;
3752
3753 tracker.changed(oid);
3754 }
3755
3756 void revise_have(hobject_t oid, eversion_t have) {
3757 if (missing.count(oid)) {
3758 tracker.changed(oid);
3759 missing[oid].have = have;
3760 }
3761 }
3762
3763 void add(const hobject_t& oid, eversion_t need, eversion_t have) {
3764 missing[oid] = item(need, have);
3765 rmissing[need.version] = oid;
3766 tracker.changed(oid);
3767 }
3768
3769 void rm(const hobject_t& oid, eversion_t v) {
3770 std::map<hobject_t, item>::iterator p = missing.find(oid);
3771 if (p != missing.end() && p->second.need <= v)
3772 rm(p);
3773 }
3774
3775 void rm(std::map<hobject_t, item>::const_iterator m) {
3776 tracker.changed(m->first);
3777 rmissing.erase(m->second.need.version);
3778 missing.erase(m);
3779 }
3780
3781 void got(const hobject_t& oid, eversion_t v) {
3782 std::map<hobject_t, item>::iterator p = missing.find(oid);
3783 assert(p != missing.end());
3784 assert(p->second.need <= v);
3785 got(p);
3786 }
3787
3788 void got(std::map<hobject_t, item>::const_iterator m) {
3789 tracker.changed(m->first);
3790 rmissing.erase(m->second.need.version);
3791 missing.erase(m);
3792 }
3793
3794 void split_into(
3795 pg_t child_pgid,
3796 unsigned split_bits,
3797 pg_missing_set *omissing) {
3798 unsigned mask = ~((~0)<<split_bits);
3799 for (map<hobject_t, item>::iterator i = missing.begin();
3800 i != missing.end();
3801 ) {
3802 if ((i->first.get_hash() & mask) == child_pgid.m_seed) {
3803 omissing->add(i->first, i->second.need, i->second.have);
3804 rm(i++);
3805 } else {
3806 ++i;
3807 }
3808 }
3809 }
3810
3811 void clear() {
3812 for (auto const &i: missing)
3813 tracker.changed(i.first);
3814 missing.clear();
3815 rmissing.clear();
3816 }
3817
3818 void encode(bufferlist &bl) const {
3819 ENCODE_START(3, 2, bl);
3820 ::encode(missing, bl);
3821 ENCODE_FINISH(bl);
3822 }
3823 void decode(bufferlist::iterator &bl, int64_t pool = -1) {
3824 for (auto const &i: missing)
3825 tracker.changed(i.first);
3826 DECODE_START_LEGACY_COMPAT_LEN(3, 2, 2, bl);
3827 ::decode(missing, bl);
3828 DECODE_FINISH(bl);
3829
3830 if (struct_v < 3) {
3831 // Handle hobject_t upgrade
3832 map<hobject_t, item> tmp;
3833 for (map<hobject_t, item>::iterator i =
3834 missing.begin();
3835 i != missing.end();
3836 ) {
3837 if (!i->first.is_max() && i->first.pool == -1) {
3838 hobject_t to_insert(i->first);
3839 to_insert.pool = pool;
3840 tmp[to_insert] = i->second;
3841 missing.erase(i++);
3842 } else {
3843 ++i;
3844 }
3845 }
3846 missing.insert(tmp.begin(), tmp.end());
3847 }
3848
3849 for (map<hobject_t,item>::iterator it =
3850 missing.begin();
3851 it != missing.end();
3852 ++it)
3853 rmissing[it->second.need.version] = it->first;
3854 for (auto const &i: missing)
3855 tracker.changed(i.first);
3856 }
3857 void dump(Formatter *f) const {
3858 f->open_array_section("missing");
3859 for (map<hobject_t,item>::const_iterator p =
3860 missing.begin(); p != missing.end(); ++p) {
3861 f->open_object_section("item");
3862 f->dump_stream("object") << p->first;
3863 p->second.dump(f);
3864 f->close_section();
3865 }
3866 f->close_section();
3867 }
3868 template <typename F>
3869 void filter_objects(F &&f) {
3870 for (auto i = missing.begin(); i != missing.end();) {
3871 if (f(i->first)) {
3872 rm(i++);
3873 } else {
3874 ++i;
3875 }
3876 }
3877 }
3878 static void generate_test_instances(list<pg_missing_set*>& o) {
3879 o.push_back(new pg_missing_set);
3880 o.push_back(new pg_missing_set);
3881 o.back()->add(
3882 hobject_t(object_t("foo"), "foo", 123, 456, 0, ""),
3883 eversion_t(5, 6), eversion_t(5, 1));
3884 }
3885 template <typename F>
3886 void get_changed(F &&f) const {
3887 tracker.get_changed(f);
3888 }
3889 void flush() {
3890 tracker.flush();
3891 }
3892 bool is_clean() const {
3893 return tracker.is_clean();
3894 }
3895 template <typename missing_t>
3896 bool debug_verify_from_init(
3897 const missing_t &init_missing,
3898 ostream *oss) const {
3899 if (!TrackChanges)
3900 return true;
3901 auto check_missing(init_missing.get_items());
3902 tracker.get_changed([&](const hobject_t &hoid) {
3903 check_missing.erase(hoid);
3904 if (missing.count(hoid)) {
3905 check_missing.insert(*(missing.find(hoid)));
3906 }
3907 });
3908 bool ok = true;
3909 if (check_missing.size() != missing.size()) {
3910 if (oss) {
3911 *oss << "Size mismatch, check: " << check_missing.size()
3912 << ", actual: " << missing.size() << "\n";
3913 }
3914 ok = false;
3915 }
3916 for (auto &i: missing) {
3917 if (!check_missing.count(i.first)) {
3918 if (oss)
3919 *oss << "check_missing missing " << i.first << "\n";
3920 ok = false;
3921 } else if (check_missing[i.first] != i.second) {
3922 if (oss)
3923 *oss << "check_missing missing item mismatch on " << i.first
3924 << ", check: " << check_missing[i.first]
3925 << ", actual: " << i.second << "\n";
3926 ok = false;
3927 }
3928 }
3929 if (oss && !ok) {
3930 *oss << "check_missing: " << check_missing << "\n";
3931 set<hobject_t> changed;
3932 tracker.get_changed([&](const hobject_t &hoid) { changed.insert(hoid); });
3933 *oss << "changed: " << changed << "\n";
3934 }
3935 return ok;
3936 }
3937 };
3938 template <bool TrackChanges>
3939 void encode(
3940 const pg_missing_set<TrackChanges> &c, bufferlist &bl, uint64_t features=0) {
3941 ENCODE_DUMP_PRE();
3942 c.encode(bl);
3943 ENCODE_DUMP_POST(cl);
3944 }
3945 template <bool TrackChanges>
3946 void decode(pg_missing_set<TrackChanges> &c, bufferlist::iterator &p) {
3947 c.decode(p);
3948 }
3949 template <bool TrackChanges>
3950 ostream& operator<<(ostream& out, const pg_missing_set<TrackChanges> &missing)
3951 {
3952 out << "missing(" << missing.num_missing();
3953 //if (missing.num_lost()) out << ", " << missing.num_lost() << " lost";
3954 out << ")";
3955 return out;
3956 }
3957
3958 using pg_missing_t = pg_missing_set<false>;
3959 using pg_missing_tracker_t = pg_missing_set<true>;
3960
3961
3962 /**
3963 * pg list objects response format
3964 *
3965 */
3966 struct pg_nls_response_t {
3967 collection_list_handle_t handle;
3968 list<librados::ListObjectImpl> entries;
3969
3970 void encode(bufferlist& bl) const {
3971 ENCODE_START(1, 1, bl);
3972 ::encode(handle, bl);
3973 __u32 n = (__u32)entries.size();
3974 ::encode(n, bl);
3975 for (list<librados::ListObjectImpl>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
3976 ::encode(i->nspace, bl);
3977 ::encode(i->oid, bl);
3978 ::encode(i->locator, bl);
3979 }
3980 ENCODE_FINISH(bl);
3981 }
3982 void decode(bufferlist::iterator& bl) {
3983 DECODE_START(1, bl);
3984 ::decode(handle, bl);
3985 __u32 n;
3986 ::decode(n, bl);
3987 entries.clear();
3988 while (n--) {
3989 librados::ListObjectImpl i;
3990 ::decode(i.nspace, bl);
3991 ::decode(i.oid, bl);
3992 ::decode(i.locator, bl);
3993 entries.push_back(i);
3994 }
3995 DECODE_FINISH(bl);
3996 }
3997 void dump(Formatter *f) const {
3998 f->dump_stream("handle") << handle;
3999 f->open_array_section("entries");
4000 for (list<librados::ListObjectImpl>::const_iterator p = entries.begin(); p != entries.end(); ++p) {
4001 f->open_object_section("object");
4002 f->dump_string("namespace", p->nspace);
4003 f->dump_string("object", p->oid);
4004 f->dump_string("key", p->locator);
4005 f->close_section();
4006 }
4007 f->close_section();
4008 }
4009 static void generate_test_instances(list<pg_nls_response_t*>& o) {
4010 o.push_back(new pg_nls_response_t);
4011 o.push_back(new pg_nls_response_t);
4012 o.back()->handle = hobject_t(object_t("hi"), "key", 1, 2, -1, "");
4013 o.back()->entries.push_back(librados::ListObjectImpl("", "one", ""));
4014 o.back()->entries.push_back(librados::ListObjectImpl("", "two", "twokey"));
4015 o.back()->entries.push_back(librados::ListObjectImpl("", "three", ""));
4016 o.push_back(new pg_nls_response_t);
4017 o.back()->handle = hobject_t(object_t("hi"), "key", 3, 4, -1, "");
4018 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1one", ""));
4019 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1two", "n1twokey"));
4020 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1three", ""));
4021 o.push_back(new pg_nls_response_t);
4022 o.back()->handle = hobject_t(object_t("hi"), "key", 5, 6, -1, "");
4023 o.back()->entries.push_back(librados::ListObjectImpl("", "one", ""));
4024 o.back()->entries.push_back(librados::ListObjectImpl("", "two", "twokey"));
4025 o.back()->entries.push_back(librados::ListObjectImpl("", "three", ""));
4026 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1one", ""));
4027 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1two", "n1twokey"));
4028 o.back()->entries.push_back(librados::ListObjectImpl("n1", "n1three", ""));
4029 }
4030 };
4031
4032 WRITE_CLASS_ENCODER(pg_nls_response_t)
4033
4034 // For backwards compatibility with older OSD requests
4035 struct pg_ls_response_t {
4036 collection_list_handle_t handle;
4037 list<pair<object_t, string> > entries;
4038
4039 void encode(bufferlist& bl) const {
4040 __u8 v = 1;
4041 ::encode(v, bl);
4042 ::encode(handle, bl);
4043 ::encode(entries, bl);
4044 }
4045 void decode(bufferlist::iterator& bl) {
4046 __u8 v;
4047 ::decode(v, bl);
4048 assert(v == 1);
4049 ::decode(handle, bl);
4050 ::decode(entries, bl);
4051 }
4052 void dump(Formatter *f) const {
4053 f->dump_stream("handle") << handle;
4054 f->open_array_section("entries");
4055 for (list<pair<object_t, string> >::const_iterator p = entries.begin(); p != entries.end(); ++p) {
4056 f->open_object_section("object");
4057 f->dump_stream("object") << p->first;
4058 f->dump_string("key", p->second);
4059 f->close_section();
4060 }
4061 f->close_section();
4062 }
4063 static void generate_test_instances(list<pg_ls_response_t*>& o) {
4064 o.push_back(new pg_ls_response_t);
4065 o.push_back(new pg_ls_response_t);
4066 o.back()->handle = hobject_t(object_t("hi"), "key", 1, 2, -1, "");
4067 o.back()->entries.push_back(make_pair(object_t("one"), string()));
4068 o.back()->entries.push_back(make_pair(object_t("two"), string("twokey")));
4069 }
4070 };
4071
4072 WRITE_CLASS_ENCODER(pg_ls_response_t)
4073
4074 /**
4075 * object_copy_cursor_t
4076 */
4077 struct object_copy_cursor_t {
4078 uint64_t data_offset;
4079 string omap_offset;
4080 bool attr_complete;
4081 bool data_complete;
4082 bool omap_complete;
4083
4084 object_copy_cursor_t()
4085 : data_offset(0),
4086 attr_complete(false),
4087 data_complete(false),
4088 omap_complete(false)
4089 {}
4090
4091 bool is_initial() const {
4092 return !attr_complete && data_offset == 0 && omap_offset.empty();
4093 }
4094 bool is_complete() const {
4095 return attr_complete && data_complete && omap_complete;
4096 }
4097
4098 static void generate_test_instances(list<object_copy_cursor_t*>& o);
4099 void encode(bufferlist& bl) const;
4100 void decode(bufferlist::iterator &bl);
4101 void dump(Formatter *f) const;
4102 };
4103 WRITE_CLASS_ENCODER(object_copy_cursor_t)
4104
4105 /**
4106 * object_copy_data_t
4107 *
4108 * Return data from a copy request. The semantics are a little strange
4109 * as a result of the encoding's heritage.
4110 *
4111 * In particular, the sender unconditionally fills in the cursor (from what
4112 * it receives and sends), the size, and the mtime, but is responsible for
4113 * figuring out whether it should put any data in the attrs, data, or
4114 * omap members (corresponding to xattrs, object data, and the omap entries)
4115 * based on external data (the client includes a max amount to return with
4116 * the copy request). The client then looks into the attrs, data, and/or omap
4117 * based on the contents of the cursor.
4118 */
4119 struct object_copy_data_t {
4120 enum {
4121 FLAG_DATA_DIGEST = 1<<0,
4122 FLAG_OMAP_DIGEST = 1<<1,
4123 };
4124 object_copy_cursor_t cursor;
4125 uint64_t size;
4126 utime_t mtime;
4127 uint32_t data_digest, omap_digest;
4128 uint32_t flags;
4129 map<string, bufferlist> attrs;
4130 bufferlist data;
4131 bufferlist omap_header;
4132 bufferlist omap_data;
4133
4134 /// which snaps we are defined for (if a snap and not the head)
4135 vector<snapid_t> snaps;
4136 ///< latest snap seq for the object (if head)
4137 snapid_t snap_seq;
4138
4139 ///< recent reqids on this object
4140 vector<pair<osd_reqid_t, version_t> > reqids;
4141
4142 uint64_t truncate_seq;
4143 uint64_t truncate_size;
4144
4145 public:
4146 object_copy_data_t() :
4147 size((uint64_t)-1), data_digest(-1),
4148 omap_digest(-1), flags(0),
4149 truncate_seq(0),
4150 truncate_size(0) {}
4151
4152 static void generate_test_instances(list<object_copy_data_t*>& o);
4153 void encode(bufferlist& bl, uint64_t features) const;
4154 void decode(bufferlist::iterator& bl);
4155 void dump(Formatter *f) const;
4156 };
4157 WRITE_CLASS_ENCODER_FEATURES(object_copy_data_t)
4158
4159 /**
4160 * pg creation info
4161 */
4162 struct pg_create_t {
4163 epoch_t created; // epoch pg created
4164 pg_t parent; // split from parent (if != pg_t())
4165 __s32 split_bits;
4166
4167 pg_create_t()
4168 : created(0), split_bits(0) {}
4169 pg_create_t(unsigned c, pg_t p, int s)
4170 : created(c), parent(p), split_bits(s) {}
4171
4172 void encode(bufferlist &bl) const;
4173 void decode(bufferlist::iterator &bl);
4174 void dump(Formatter *f) const;
4175 static void generate_test_instances(list<pg_create_t*>& o);
4176 };
4177 WRITE_CLASS_ENCODER(pg_create_t)
4178
4179 // -----------------------------------------
4180
4181 struct osd_peer_stat_t {
4182 utime_t stamp;
4183
4184 osd_peer_stat_t() { }
4185
4186 void encode(bufferlist &bl) const;
4187 void decode(bufferlist::iterator &bl);
4188 void dump(Formatter *f) const;
4189 static void generate_test_instances(list<osd_peer_stat_t*>& o);
4190 };
4191 WRITE_CLASS_ENCODER(osd_peer_stat_t)
4192
4193 ostream& operator<<(ostream& out, const osd_peer_stat_t &stat);
4194
4195
4196 // -----------------------------------------
4197
4198 class ObjectExtent {
4199 /**
4200 * ObjectExtents are used for specifying IO behavior against RADOS
4201 * objects when one is using the ObjectCacher.
4202 *
4203 * To use this in a real system, *every member* must be filled
4204 * out correctly. In particular, make sure to initialize the
4205 * oloc correctly, as its default values are deliberate poison
4206 * and will cause internal ObjectCacher asserts.
4207 *
4208 * Similarly, your buffer_extents vector *must* specify a total
4209 * size equal to your length. If the buffer_extents inadvertently
4210 * contain less space than the length member specifies, you
4211 * will get unintelligible asserts deep in the ObjectCacher.
4212 *
4213 * If you are trying to do testing and don't care about actual
4214 * RADOS function, the simplest thing to do is to initialize
4215 * the ObjectExtent (truncate_size can be 0), create a single entry
4216 * in buffer_extents matching the length, and set oloc.pool to 0.
4217 */
4218 public:
4219 object_t oid; // object id
4220 uint64_t objectno;
4221 uint64_t offset; // in object
4222 uint64_t length; // in object
4223 uint64_t truncate_size; // in object
4224
4225 object_locator_t oloc; // object locator (pool etc)
4226
4227 vector<pair<uint64_t,uint64_t> > buffer_extents; // off -> len. extents in buffer being mapped (may be fragmented bc of striping!)
4228
4229 ObjectExtent() : objectno(0), offset(0), length(0), truncate_size(0) {}
4230 ObjectExtent(object_t o, uint64_t ono, uint64_t off, uint64_t l, uint64_t ts) :
4231 oid(o), objectno(ono), offset(off), length(l), truncate_size(ts) { }
4232 };
4233
4234 inline ostream& operator<<(ostream& out, const ObjectExtent &ex)
4235 {
4236 return out << "extent("
4237 << ex.oid << " (" << ex.objectno << ") in " << ex.oloc
4238 << " " << ex.offset << "~" << ex.length
4239 << " -> " << ex.buffer_extents
4240 << ")";
4241 }
4242
4243
4244
4245
4246
4247
4248 // ---------------------------------------
4249
4250 class OSDSuperblock {
4251 public:
4252 uuid_d cluster_fsid, osd_fsid;
4253 int32_t whoami; // my role in this fs.
4254 epoch_t current_epoch; // most recent epoch
4255 epoch_t oldest_map, newest_map; // oldest/newest maps we have.
4256 double weight;
4257
4258 CompatSet compat_features;
4259
4260 // last interval over which i mounted and was then active
4261 epoch_t mounted; // last epoch i mounted
4262 epoch_t clean_thru; // epoch i was active and clean thru
4263
4264 OSDSuperblock() :
4265 whoami(-1),
4266 current_epoch(0), oldest_map(0), newest_map(0), weight(0),
4267 mounted(0), clean_thru(0) {
4268 }
4269
4270 void encode(bufferlist &bl) const;
4271 void decode(bufferlist::iterator &bl);
4272 void dump(Formatter *f) const;
4273 static void generate_test_instances(list<OSDSuperblock*>& o);
4274 };
4275 WRITE_CLASS_ENCODER(OSDSuperblock)
4276
4277 inline ostream& operator<<(ostream& out, const OSDSuperblock& sb)
4278 {
4279 return out << "sb(" << sb.cluster_fsid
4280 << " osd." << sb.whoami
4281 << " " << sb.osd_fsid
4282 << " e" << sb.current_epoch
4283 << " [" << sb.oldest_map << "," << sb.newest_map << "]"
4284 << " lci=[" << sb.mounted << "," << sb.clean_thru << "]"
4285 << ")";
4286 }
4287
4288
4289 // -------
4290
4291
4292
4293
4294
4295
4296 /*
4297 * attached to object head. describes most recent snap context, and
4298 * set of existing clones.
4299 */
4300 struct SnapSet {
4301 snapid_t seq;
4302 bool head_exists;
4303 vector<snapid_t> snaps; // descending
4304 vector<snapid_t> clones; // ascending
4305 map<snapid_t, interval_set<uint64_t> > clone_overlap; // overlap w/ next newest
4306 map<snapid_t, uint64_t> clone_size;
4307 map<snapid_t, vector<snapid_t>> clone_snaps; // descending
4308
4309 SnapSet() : seq(0), head_exists(false) {}
4310 explicit SnapSet(bufferlist& bl) {
4311 bufferlist::iterator p = bl.begin();
4312 decode(p);
4313 }
4314
4315 bool is_legacy() const {
4316 return clone_snaps.size() < clones.size() || !head_exists;
4317 }
4318
4319 /// populate SnapSet from a librados::snap_set_t
4320 void from_snap_set(const librados::snap_set_t& ss, bool legacy);
4321
4322 /// get space accounted to clone
4323 uint64_t get_clone_bytes(snapid_t clone) const;
4324
4325 void encode(bufferlist& bl) const;
4326 void decode(bufferlist::iterator& bl);
4327 void dump(Formatter *f) const;
4328 static void generate_test_instances(list<SnapSet*>& o);
4329
4330 SnapContext get_ssc_as_of(snapid_t as_of) const {
4331 SnapContext out;
4332 out.seq = as_of;
4333 for (vector<snapid_t>::const_iterator i = snaps.begin();
4334 i != snaps.end();
4335 ++i) {
4336 if (*i <= as_of)
4337 out.snaps.push_back(*i);
4338 }
4339 return out;
4340 }
4341
4342 // return min element of snaps > after, return max if no such element
4343 snapid_t get_first_snap_after(snapid_t after, snapid_t max) const {
4344 for (vector<snapid_t>::const_reverse_iterator i = snaps.rbegin();
4345 i != snaps.rend();
4346 ++i) {
4347 if (*i > after)
4348 return *i;
4349 }
4350 return max;
4351 }
4352
4353 SnapSet get_filtered(const pg_pool_t &pinfo) const;
4354 void filter(const pg_pool_t &pinfo);
4355 };
4356 WRITE_CLASS_ENCODER(SnapSet)
4357
4358 ostream& operator<<(ostream& out, const SnapSet& cs);
4359
4360
4361
4362 #define OI_ATTR "_"
4363 #define SS_ATTR "snapset"
4364
4365 struct watch_info_t {
4366 uint64_t cookie;
4367 uint32_t timeout_seconds;
4368 entity_addr_t addr;
4369
4370 watch_info_t() : cookie(0), timeout_seconds(0) { }
4371 watch_info_t(uint64_t c, uint32_t t, const entity_addr_t& a) : cookie(c), timeout_seconds(t), addr(a) {}
4372
4373 void encode(bufferlist& bl, uint64_t features) const;
4374 void decode(bufferlist::iterator& bl);
4375 void dump(Formatter *f) const;
4376 static void generate_test_instances(list<watch_info_t*>& o);
4377 };
4378 WRITE_CLASS_ENCODER_FEATURES(watch_info_t)
4379
4380 static inline bool operator==(const watch_info_t& l, const watch_info_t& r) {
4381 return l.cookie == r.cookie && l.timeout_seconds == r.timeout_seconds
4382 && l.addr == r.addr;
4383 }
4384
4385 static inline ostream& operator<<(ostream& out, const watch_info_t& w) {
4386 return out << "watch(cookie " << w.cookie << " " << w.timeout_seconds << "s"
4387 << " " << w.addr << ")";
4388 }
4389
4390 struct notify_info_t {
4391 uint64_t cookie;
4392 uint64_t notify_id;
4393 uint32_t timeout;
4394 bufferlist bl;
4395 };
4396
4397 static inline ostream& operator<<(ostream& out, const notify_info_t& n) {
4398 return out << "notify(cookie " << n.cookie
4399 << " notify" << n.notify_id
4400 << " " << n.timeout << "s)";
4401 }
4402
4403
4404 struct object_info_t {
4405 hobject_t soid;
4406 eversion_t version, prior_version;
4407 version_t user_version;
4408 osd_reqid_t last_reqid;
4409
4410 uint64_t size;
4411 utime_t mtime;
4412 utime_t local_mtime; // local mtime
4413
4414 // note: these are currently encoded into a total 16 bits; see
4415 // encode()/decode() for the weirdness.
4416 typedef enum {
4417 FLAG_LOST = 1<<0,
4418 FLAG_WHITEOUT = 1<<1, // object logically does not exist
4419 FLAG_DIRTY = 1<<2, // object has been modified since last flushed or undirtied
4420 FLAG_OMAP = 1 << 3, // has (or may have) some/any omap data
4421 FLAG_DATA_DIGEST = 1 << 4, // has data crc
4422 FLAG_OMAP_DIGEST = 1 << 5, // has omap crc
4423 FLAG_CACHE_PIN = 1 << 6, // pin the object in cache tier
4424 // ...
4425 FLAG_USES_TMAP = 1<<8, // deprecated; no longer used.
4426 } flag_t;
4427
4428 flag_t flags;
4429
4430 static string get_flag_string(flag_t flags) {
4431 string s;
4432 if (flags & FLAG_LOST)
4433 s += "|lost";
4434 if (flags & FLAG_WHITEOUT)
4435 s += "|whiteout";
4436 if (flags & FLAG_DIRTY)
4437 s += "|dirty";
4438 if (flags & FLAG_USES_TMAP)
4439 s += "|uses_tmap";
4440 if (flags & FLAG_OMAP)
4441 s += "|omap";
4442 if (flags & FLAG_DATA_DIGEST)
4443 s += "|data_digest";
4444 if (flags & FLAG_OMAP_DIGEST)
4445 s += "|omap_digest";
4446 if (flags & FLAG_CACHE_PIN)
4447 s += "|cache_pin";
4448 if (s.length())
4449 return s.substr(1);
4450 return s;
4451 }
4452 string get_flag_string() const {
4453 return get_flag_string(flags);
4454 }
4455
4456 /// [clone] descending. pre-luminous; moved to SnapSet
4457 vector<snapid_t> legacy_snaps;
4458
4459 uint64_t truncate_seq, truncate_size;
4460
4461 map<pair<uint64_t, entity_name_t>, watch_info_t> watchers;
4462
4463 // opportunistic checksums; may or may not be present
4464 __u32 data_digest; ///< data crc32c
4465 __u32 omap_digest; ///< omap crc32c
4466
4467 // alloc hint attribute
4468 uint64_t expected_object_size, expected_write_size;
4469 uint32_t alloc_hint_flags;
4470
4471 void copy_user_bits(const object_info_t& other);
4472
4473 static ps_t legacy_object_locator_to_ps(const object_t &oid,
4474 const object_locator_t &loc);
4475
4476 bool test_flag(flag_t f) const {
4477 return (flags & f) == f;
4478 }
4479 void set_flag(flag_t f) {
4480 flags = (flag_t)(flags | f);
4481 }
4482 void clear_flag(flag_t f) {
4483 flags = (flag_t)(flags & ~f);
4484 }
4485 bool is_lost() const {
4486 return test_flag(FLAG_LOST);
4487 }
4488 bool is_whiteout() const {
4489 return test_flag(FLAG_WHITEOUT);
4490 }
4491 bool is_dirty() const {
4492 return test_flag(FLAG_DIRTY);
4493 }
4494 bool is_omap() const {
4495 return test_flag(FLAG_OMAP);
4496 }
4497 bool is_data_digest() const {
4498 return test_flag(FLAG_DATA_DIGEST);
4499 }
4500 bool is_omap_digest() const {
4501 return test_flag(FLAG_OMAP_DIGEST);
4502 }
4503 bool is_cache_pinned() const {
4504 return test_flag(FLAG_CACHE_PIN);
4505 }
4506
4507 void set_data_digest(__u32 d) {
4508 set_flag(FLAG_DATA_DIGEST);
4509 data_digest = d;
4510 }
4511 void set_omap_digest(__u32 d) {
4512 set_flag(FLAG_OMAP_DIGEST);
4513 omap_digest = d;
4514 }
4515 void clear_data_digest() {
4516 clear_flag(FLAG_DATA_DIGEST);
4517 data_digest = -1;
4518 }
4519 void clear_omap_digest() {
4520 clear_flag(FLAG_OMAP_DIGEST);
4521 omap_digest = -1;
4522 }
4523 void new_object() {
4524 set_data_digest(-1);
4525 set_omap_digest(-1);
4526 }
4527
4528 void encode(bufferlist& bl, uint64_t features) const;
4529 void decode(bufferlist::iterator& bl);
4530 void decode(bufferlist& bl) {
4531 bufferlist::iterator p = bl.begin();
4532 decode(p);
4533 }
4534 void dump(Formatter *f) const;
4535 static void generate_test_instances(list<object_info_t*>& o);
4536
4537 explicit object_info_t()
4538 : user_version(0), size(0), flags((flag_t)0),
4539 truncate_seq(0), truncate_size(0),
4540 data_digest(-1), omap_digest(-1),
4541 expected_object_size(0), expected_write_size(0),
4542 alloc_hint_flags(0)
4543 {}
4544
4545 explicit object_info_t(const hobject_t& s)
4546 : soid(s),
4547 user_version(0), size(0), flags((flag_t)0),
4548 truncate_seq(0), truncate_size(0),
4549 data_digest(-1), omap_digest(-1),
4550 expected_object_size(0), expected_write_size(0),
4551 alloc_hint_flags(0)
4552 {}
4553
4554 explicit object_info_t(bufferlist& bl) {
4555 decode(bl);
4556 }
4557 };
4558 WRITE_CLASS_ENCODER_FEATURES(object_info_t)
4559
4560 ostream& operator<<(ostream& out, const object_info_t& oi);
4561
4562
4563
4564 // Object recovery
4565 struct ObjectRecoveryInfo {
4566 hobject_t soid;
4567 eversion_t version;
4568 uint64_t size;
4569 object_info_t oi;
4570 SnapSet ss; // only populated if soid is_snap()
4571 interval_set<uint64_t> copy_subset;
4572 map<hobject_t, interval_set<uint64_t>> clone_subset;
4573
4574 ObjectRecoveryInfo() : size(0) { }
4575
4576 static void generate_test_instances(list<ObjectRecoveryInfo*>& o);
4577 void encode(bufferlist &bl, uint64_t features) const;
4578 void decode(bufferlist::iterator &bl, int64_t pool = -1);
4579 ostream &print(ostream &out) const;
4580 void dump(Formatter *f) const;
4581 };
4582 WRITE_CLASS_ENCODER_FEATURES(ObjectRecoveryInfo)
4583 ostream& operator<<(ostream& out, const ObjectRecoveryInfo &inf);
4584
4585 struct ObjectRecoveryProgress {
4586 uint64_t data_recovered_to;
4587 string omap_recovered_to;
4588 bool first;
4589 bool data_complete;
4590 bool omap_complete;
4591
4592 ObjectRecoveryProgress()
4593 : data_recovered_to(0),
4594 first(true),
4595 data_complete(false), omap_complete(false) { }
4596
4597 bool is_complete(const ObjectRecoveryInfo& info) const {
4598 return (data_recovered_to >= (
4599 info.copy_subset.empty() ?
4600 0 : info.copy_subset.range_end())) &&
4601 omap_complete;
4602 }
4603
4604 static void generate_test_instances(list<ObjectRecoveryProgress*>& o);
4605 void encode(bufferlist &bl) const;
4606 void decode(bufferlist::iterator &bl);
4607 ostream &print(ostream &out) const;
4608 void dump(Formatter *f) const;
4609 };
4610 WRITE_CLASS_ENCODER(ObjectRecoveryProgress)
4611 ostream& operator<<(ostream& out, const ObjectRecoveryProgress &prog);
4612
4613 struct PushReplyOp {
4614 hobject_t soid;
4615
4616 static void generate_test_instances(list<PushReplyOp*>& o);
4617 void encode(bufferlist &bl) const;
4618 void decode(bufferlist::iterator &bl);
4619 ostream &print(ostream &out) const;
4620 void dump(Formatter *f) const;
4621
4622 uint64_t cost(CephContext *cct) const;
4623 };
4624 WRITE_CLASS_ENCODER(PushReplyOp)
4625 ostream& operator<<(ostream& out, const PushReplyOp &op);
4626
4627 struct PullOp {
4628 hobject_t soid;
4629
4630 ObjectRecoveryInfo recovery_info;
4631 ObjectRecoveryProgress recovery_progress;
4632
4633 static void generate_test_instances(list<PullOp*>& o);
4634 void encode(bufferlist &bl, uint64_t features) const;
4635 void decode(bufferlist::iterator &bl);
4636 ostream &print(ostream &out) const;
4637 void dump(Formatter *f) const;
4638
4639 uint64_t cost(CephContext *cct) const;
4640 };
4641 WRITE_CLASS_ENCODER_FEATURES(PullOp)
4642 ostream& operator<<(ostream& out, const PullOp &op);
4643
4644 struct PushOp {
4645 hobject_t soid;
4646 eversion_t version;
4647 bufferlist data;
4648 interval_set<uint64_t> data_included;
4649 bufferlist omap_header;
4650 map<string, bufferlist> omap_entries;
4651 map<string, bufferlist> attrset;
4652
4653 ObjectRecoveryInfo recovery_info;
4654 ObjectRecoveryProgress before_progress;
4655 ObjectRecoveryProgress after_progress;
4656
4657 static void generate_test_instances(list<PushOp*>& o);
4658 void encode(bufferlist &bl, uint64_t features) const;
4659 void decode(bufferlist::iterator &bl);
4660 ostream &print(ostream &out) const;
4661 void dump(Formatter *f) const;
4662
4663 uint64_t cost(CephContext *cct) const;
4664 };
4665 WRITE_CLASS_ENCODER_FEATURES(PushOp)
4666 ostream& operator<<(ostream& out, const PushOp &op);
4667
4668
4669 /*
4670 * summarize pg contents for purposes of a scrub
4671 */
4672 struct ScrubMap {
4673 struct object {
4674 map<string,bufferptr> attrs;
4675 uint64_t size;
4676 __u32 omap_digest; ///< omap crc32c
4677 __u32 digest; ///< data crc32c
4678 bool negative:1;
4679 bool digest_present:1;
4680 bool omap_digest_present:1;
4681 bool read_error:1;
4682 bool stat_error:1;
4683 bool ec_hash_mismatch:1;
4684 bool ec_size_mismatch:1;
4685
4686 object() :
4687 // Init invalid size so it won't match if we get a stat EIO error
4688 size(-1), omap_digest(0), digest(0),
4689 negative(false), digest_present(false), omap_digest_present(false),
4690 read_error(false), stat_error(false), ec_hash_mismatch(false), ec_size_mismatch(false) {}
4691
4692 void encode(bufferlist& bl) const;
4693 void decode(bufferlist::iterator& bl);
4694 void dump(Formatter *f) const;
4695 static void generate_test_instances(list<object*>& o);
4696 };
4697 WRITE_CLASS_ENCODER(object)
4698
4699 map<hobject_t,object> objects;
4700 eversion_t valid_through;
4701 eversion_t incr_since;
4702
4703 void merge_incr(const ScrubMap &l);
4704 void insert(const ScrubMap &r) {
4705 objects.insert(r.objects.begin(), r.objects.end());
4706 }
4707 void swap(ScrubMap &r) {
4708 ::swap(objects, r.objects);
4709 ::swap(valid_through, r.valid_through);
4710 ::swap(incr_since, r.incr_since);
4711 }
4712
4713 void encode(bufferlist& bl) const;
4714 void decode(bufferlist::iterator& bl, int64_t pool=-1);
4715 void dump(Formatter *f) const;
4716 static void generate_test_instances(list<ScrubMap*>& o);
4717 };
4718 WRITE_CLASS_ENCODER(ScrubMap::object)
4719 WRITE_CLASS_ENCODER(ScrubMap)
4720
4721
4722 struct OSDOp {
4723 ceph_osd_op op;
4724 sobject_t soid;
4725
4726 bufferlist indata, outdata;
4727 int32_t rval;
4728
4729 OSDOp() : rval(0) {
4730 memset(&op, 0, sizeof(ceph_osd_op));
4731 }
4732
4733 /**
4734 * split a bufferlist into constituent indata members of a vector of OSDOps
4735 *
4736 * @param ops [out] vector of OSDOps
4737 * @param in [in] combined data buffer
4738 */
4739 static void split_osd_op_vector_in_data(vector<OSDOp>& ops, bufferlist& in);
4740
4741 /**
4742 * merge indata members of a vector of OSDOp into a single bufferlist
4743 *
4744 * Notably this also encodes certain other OSDOp data into the data
4745 * buffer, including the sobject_t soid.
4746 *
4747 * @param ops [in] vector of OSDOps
4748 * @param out [out] combined data buffer
4749 */
4750 static void merge_osd_op_vector_in_data(vector<OSDOp>& ops, bufferlist& out);
4751
4752 /**
4753 * split a bufferlist into constituent outdata members of a vector of OSDOps
4754 *
4755 * @param ops [out] vector of OSDOps
4756 * @param in [in] combined data buffer
4757 */
4758 static void split_osd_op_vector_out_data(vector<OSDOp>& ops, bufferlist& in);
4759
4760 /**
4761 * merge outdata members of a vector of OSDOps into a single bufferlist
4762 *
4763 * @param ops [in] vector of OSDOps
4764 * @param out [out] combined data buffer
4765 */
4766 static void merge_osd_op_vector_out_data(vector<OSDOp>& ops, bufferlist& out);
4767 };
4768
4769 ostream& operator<<(ostream& out, const OSDOp& op);
4770
4771 struct watch_item_t {
4772 entity_name_t name;
4773 uint64_t cookie;
4774 uint32_t timeout_seconds;
4775 entity_addr_t addr;
4776
4777 watch_item_t() : cookie(0), timeout_seconds(0) { }
4778 watch_item_t(entity_name_t name, uint64_t cookie, uint32_t timeout,
4779 const entity_addr_t& addr)
4780 : name(name), cookie(cookie), timeout_seconds(timeout),
4781 addr(addr) { }
4782
4783 void encode(bufferlist &bl, uint64_t features) const {
4784 ENCODE_START(2, 1, bl);
4785 ::encode(name, bl);
4786 ::encode(cookie, bl);
4787 ::encode(timeout_seconds, bl);
4788 ::encode(addr, bl, features);
4789 ENCODE_FINISH(bl);
4790 }
4791 void decode(bufferlist::iterator &bl) {
4792 DECODE_START(2, bl);
4793 ::decode(name, bl);
4794 ::decode(cookie, bl);
4795 ::decode(timeout_seconds, bl);
4796 if (struct_v >= 2) {
4797 ::decode(addr, bl);
4798 }
4799 DECODE_FINISH(bl);
4800 }
4801 };
4802 WRITE_CLASS_ENCODER_FEATURES(watch_item_t)
4803
4804 struct obj_watch_item_t {
4805 hobject_t obj;
4806 watch_item_t wi;
4807 };
4808
4809 /**
4810 * obj list watch response format
4811 *
4812 */
4813 struct obj_list_watch_response_t {
4814 list<watch_item_t> entries;
4815
4816 void encode(bufferlist& bl, uint64_t features) const {
4817 ENCODE_START(1, 1, bl);
4818 ::encode(entries, bl, features);
4819 ENCODE_FINISH(bl);
4820 }
4821 void decode(bufferlist::iterator& bl) {
4822 DECODE_START(1, bl);
4823 ::decode(entries, bl);
4824 DECODE_FINISH(bl);
4825 }
4826 void dump(Formatter *f) const {
4827 f->open_array_section("entries");
4828 for (list<watch_item_t>::const_iterator p = entries.begin(); p != entries.end(); ++p) {
4829 f->open_object_section("watch");
4830 f->dump_stream("watcher") << p->name;
4831 f->dump_int("cookie", p->cookie);
4832 f->dump_int("timeout", p->timeout_seconds);
4833 f->open_object_section("addr");
4834 p->addr.dump(f);
4835 f->close_section();
4836 f->close_section();
4837 }
4838 f->close_section();
4839 }
4840 static void generate_test_instances(list<obj_list_watch_response_t*>& o) {
4841 entity_addr_t ea;
4842 o.push_back(new obj_list_watch_response_t);
4843 o.push_back(new obj_list_watch_response_t);
4844 ea.set_type(entity_addr_t::TYPE_LEGACY);
4845 ea.set_nonce(1000);
4846 ea.set_family(AF_INET);
4847 ea.set_in4_quad(0, 127);
4848 ea.set_in4_quad(1, 0);
4849 ea.set_in4_quad(2, 0);
4850 ea.set_in4_quad(3, 1);
4851 ea.set_port(1024);
4852 o.back()->entries.push_back(watch_item_t(entity_name_t(entity_name_t::TYPE_CLIENT, 1), 10, 30, ea));
4853 ea.set_nonce(1001);
4854 ea.set_in4_quad(3, 2);
4855 ea.set_port(1025);
4856 o.back()->entries.push_back(watch_item_t(entity_name_t(entity_name_t::TYPE_CLIENT, 2), 20, 60, ea));
4857 }
4858 };
4859 WRITE_CLASS_ENCODER_FEATURES(obj_list_watch_response_t)
4860
4861 struct clone_info {
4862 snapid_t cloneid;
4863 vector<snapid_t> snaps; // ascending
4864 vector< pair<uint64_t,uint64_t> > overlap;
4865 uint64_t size;
4866
4867 clone_info() : cloneid(CEPH_NOSNAP), size(0) {}
4868
4869 void encode(bufferlist& bl) const {
4870 ENCODE_START(1, 1, bl);
4871 ::encode(cloneid, bl);
4872 ::encode(snaps, bl);
4873 ::encode(overlap, bl);
4874 ::encode(size, bl);
4875 ENCODE_FINISH(bl);
4876 }
4877 void decode(bufferlist::iterator& bl) {
4878 DECODE_START(1, bl);
4879 ::decode(cloneid, bl);
4880 ::decode(snaps, bl);
4881 ::decode(overlap, bl);
4882 ::decode(size, bl);
4883 DECODE_FINISH(bl);
4884 }
4885 void dump(Formatter *f) const {
4886 if (cloneid == CEPH_NOSNAP)
4887 f->dump_string("cloneid", "HEAD");
4888 else
4889 f->dump_unsigned("cloneid", cloneid.val);
4890 f->open_array_section("snapshots");
4891 for (vector<snapid_t>::const_iterator p = snaps.begin(); p != snaps.end(); ++p) {
4892 f->open_object_section("snap");
4893 f->dump_unsigned("id", p->val);
4894 f->close_section();
4895 }
4896 f->close_section();
4897 f->open_array_section("overlaps");
4898 for (vector< pair<uint64_t,uint64_t> >::const_iterator q = overlap.begin();
4899 q != overlap.end(); ++q) {
4900 f->open_object_section("overlap");
4901 f->dump_unsigned("offset", q->first);
4902 f->dump_unsigned("length", q->second);
4903 f->close_section();
4904 }
4905 f->close_section();
4906 f->dump_unsigned("size", size);
4907 }
4908 static void generate_test_instances(list<clone_info*>& o) {
4909 o.push_back(new clone_info);
4910 o.push_back(new clone_info);
4911 o.back()->cloneid = 1;
4912 o.back()->snaps.push_back(1);
4913 o.back()->overlap.push_back(pair<uint64_t,uint64_t>(0,4096));
4914 o.back()->overlap.push_back(pair<uint64_t,uint64_t>(8192,4096));
4915 o.back()->size = 16384;
4916 o.push_back(new clone_info);
4917 o.back()->cloneid = CEPH_NOSNAP;
4918 o.back()->size = 32768;
4919 }
4920 };
4921 WRITE_CLASS_ENCODER(clone_info)
4922
4923 /**
4924 * obj list snaps response format
4925 *
4926 */
4927 struct obj_list_snap_response_t {
4928 vector<clone_info> clones; // ascending
4929 snapid_t seq;
4930
4931 void encode(bufferlist& bl) const {
4932 ENCODE_START(2, 1, bl);
4933 ::encode(clones, bl);
4934 ::encode(seq, bl);
4935 ENCODE_FINISH(bl);
4936 }
4937 void decode(bufferlist::iterator& bl) {
4938 DECODE_START(2, bl);
4939 ::decode(clones, bl);
4940 if (struct_v >= 2)
4941 ::decode(seq, bl);
4942 else
4943 seq = CEPH_NOSNAP;
4944 DECODE_FINISH(bl);
4945 }
4946 void dump(Formatter *f) const {
4947 f->open_array_section("clones");
4948 for (vector<clone_info>::const_iterator p = clones.begin(); p != clones.end(); ++p) {
4949 f->open_object_section("clone");
4950 p->dump(f);
4951 f->close_section();
4952 }
4953 f->dump_unsigned("seq", seq);
4954 f->close_section();
4955 }
4956 static void generate_test_instances(list<obj_list_snap_response_t*>& o) {
4957 o.push_back(new obj_list_snap_response_t);
4958 o.push_back(new obj_list_snap_response_t);
4959 clone_info cl;
4960 cl.cloneid = 1;
4961 cl.snaps.push_back(1);
4962 cl.overlap.push_back(pair<uint64_t,uint64_t>(0,4096));
4963 cl.overlap.push_back(pair<uint64_t,uint64_t>(8192,4096));
4964 cl.size = 16384;
4965 o.back()->clones.push_back(cl);
4966 cl.cloneid = CEPH_NOSNAP;
4967 cl.snaps.clear();
4968 cl.overlap.clear();
4969 cl.size = 32768;
4970 o.back()->clones.push_back(cl);
4971 o.back()->seq = 123;
4972 }
4973 };
4974
4975 WRITE_CLASS_ENCODER(obj_list_snap_response_t)
4976
4977 // PromoteCounter
4978
4979 struct PromoteCounter {
4980 std::atomic_ullong attempts{0};
4981 std::atomic_ullong objects{0};
4982 std::atomic_ullong bytes{0};
4983
4984 void attempt() {
4985 attempts++;
4986 }
4987
4988 void finish(uint64_t size) {
4989 objects++;
4990 bytes += size;
4991 }
4992
4993 void sample_and_attenuate(uint64_t *a, uint64_t *o, uint64_t *b) {
4994 *a = attempts;
4995 *o = objects;
4996 *b = bytes;
4997 attempts = *a / 2;
4998 objects = *o / 2;
4999 bytes = *b / 2;
5000 }
5001 };
5002
5003 /** store_statfs_t
5004 * ObjectStore full statfs information
5005 */
5006 struct store_statfs_t
5007 {
5008 uint64_t total = 0; // Total bytes
5009 uint64_t available = 0; // Free bytes available
5010
5011 int64_t allocated = 0; // Bytes allocated by the store
5012 int64_t stored = 0; // Bytes actually stored by the user
5013 int64_t compressed = 0; // Bytes stored after compression
5014 int64_t compressed_allocated = 0; // Bytes allocated for compressed data
5015 int64_t compressed_original = 0; // Bytes that were successfully compressed
5016
5017 void reset() {
5018 *this = store_statfs_t();
5019 }
5020 bool operator ==(const store_statfs_t& other) const;
5021 void dump(Formatter *f) const;
5022 };
5023 ostream &operator<<(ostream &lhs, const store_statfs_t &rhs);
5024
5025 #endif