]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/filestore/DBObjectMap.h
f60ae1753bae6add5420b146305765256778ec0e
[ceph.git] / ceph / src / os / filestore / DBObjectMap.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 #ifndef DBOBJECTMAP_DB_H
3 #define DBOBJECTMAP_DB_H
4
5 #include "include/buffer_fwd.h"
6 #include <set>
7 #include <map>
8 #include <string>
9
10 #include <vector>
11 #include "include/memory.h"
12 #include <boost/scoped_ptr.hpp>
13
14 #include "os/ObjectMap.h"
15 #include "kv/KeyValueDB.h"
16 #include "osd/osd_types.h"
17 #include "common/Mutex.h"
18 #include "common/Cond.h"
19 #include "common/simple_cache.hpp"
20 #include <boost/optional/optional_io.hpp>
21
22 #include "SequencerPosition.h"
23
24 /**
25 * DBObjectMap: Implements ObjectMap in terms of KeyValueDB
26 *
27 * Prefix space structure:
28 *
29 * @see complete_prefix
30 * @see user_prefix
31 * @see sys_prefix
32 *
33 * - HOBJECT_TO_SEQ: Contains leaf mapping from ghobject_t->header.seq and
34 * corresponding omap header
35 * - SYS_PREFIX: GLOBAL_STATE_KEY - contains next seq number
36 * @see State
37 * @see write_state
38 * @see init
39 * @see generate_new_header
40 * - USER_PREFIX + header_key(header->seq) + USER_PREFIX
41 * : key->value for header->seq
42 * - USER_PREFIX + header_key(header->seq) + COMPLETE_PREFIX: see below
43 * - USER_PREFIX + header_key(header->seq) + XATTR_PREFIX: xattrs
44 * - USER_PREFIX + header_key(header->seq) + SYS_PREFIX
45 * : USER_HEADER_KEY - omap header for header->seq
46 * : HEADER_KEY - encoding of header for header->seq
47 *
48 * For each node (represented by a header), we
49 * store three mappings: the key mapping, the complete mapping, and the parent.
50 * The complete mapping (COMPLETE_PREFIX space) is key->key. Each x->y entry in
51 * this mapping indicates that the key mapping contains all entries on [x,y).
52 * Note, max string is represented by "", so ""->"" indicates that the parent
53 * is unnecessary (@see rm_keys). When looking up a key not contained in the
54 * the complete set, we have to check the parent if we don't find it in the
55 * key set. During rm_keys, we copy keys from the parent and update the
56 * complete set to reflect the change @see rm_keys.
57 */
58 class DBObjectMap : public ObjectMap {
59 public:
60 boost::scoped_ptr<KeyValueDB> db;
61
62 /**
63 * Serializes access to next_seq as well as the in_use set
64 */
65 Mutex header_lock;
66 Cond header_cond;
67 Cond map_header_cond;
68
69 /**
70 * Set of headers currently in use
71 */
72 set<uint64_t> in_use;
73 set<ghobject_t> map_header_in_use;
74
75 /**
76 * Takes the map_header_in_use entry in constructor, releases in
77 * destructor
78 */
79 class MapHeaderLock {
80 DBObjectMap *db;
81 boost::optional<ghobject_t> locked;
82
83 MapHeaderLock(const MapHeaderLock &);
84 MapHeaderLock &operator=(const MapHeaderLock &);
85 public:
86 explicit MapHeaderLock(DBObjectMap *db) : db(db) {}
87 MapHeaderLock(DBObjectMap *db, const ghobject_t &oid) : db(db), locked(oid) {
88 Mutex::Locker l(db->header_lock);
89 while (db->map_header_in_use.count(*locked))
90 db->map_header_cond.Wait(db->header_lock);
91 db->map_header_in_use.insert(*locked);
92 }
93
94 const ghobject_t &get_locked() const {
95 assert(locked);
96 return *locked;
97 }
98
99 void swap(MapHeaderLock &o) {
100 assert(db == o.db);
101
102 // centos6's boost optional doesn't seem to have swap :(
103 boost::optional<ghobject_t> _locked = o.locked;
104 o.locked = locked;
105 locked = _locked;
106 }
107
108 ~MapHeaderLock() {
109 if (locked) {
110 Mutex::Locker l(db->header_lock);
111 assert(db->map_header_in_use.count(*locked));
112 db->map_header_cond.Signal();
113 db->map_header_in_use.erase(*locked);
114 }
115 }
116 };
117
118 DBObjectMap(CephContext* cct, KeyValueDB *db)
119 : ObjectMap(cct), db(db), header_lock("DBOBjectMap"),
120 cache_lock("DBObjectMap::CacheLock"),
121 caches(cct->_conf->filestore_omap_header_cache_size)
122 {}
123
124 int set_keys(
125 const ghobject_t &oid,
126 const map<string, bufferlist> &set,
127 const SequencerPosition *spos=0
128 ) override;
129
130 int set_header(
131 const ghobject_t &oid,
132 const bufferlist &bl,
133 const SequencerPosition *spos=0
134 ) override;
135
136 int get_header(
137 const ghobject_t &oid,
138 bufferlist *bl
139 ) override;
140
141 int clear(
142 const ghobject_t &oid,
143 const SequencerPosition *spos=0
144 ) override;
145
146 int clear_keys_header(
147 const ghobject_t &oid,
148 const SequencerPosition *spos=0
149 ) override;
150
151 int rm_keys(
152 const ghobject_t &oid,
153 const set<string> &to_clear,
154 const SequencerPosition *spos=0
155 ) override;
156
157 int get(
158 const ghobject_t &oid,
159 bufferlist *header,
160 map<string, bufferlist> *out
161 ) override;
162
163 int get_keys(
164 const ghobject_t &oid,
165 set<string> *keys
166 ) override;
167
168 int get_values(
169 const ghobject_t &oid,
170 const set<string> &keys,
171 map<string, bufferlist> *out
172 ) override;
173
174 int check_keys(
175 const ghobject_t &oid,
176 const set<string> &keys,
177 set<string> *out
178 ) override;
179
180 int get_xattrs(
181 const ghobject_t &oid,
182 const set<string> &to_get,
183 map<string, bufferlist> *out
184 ) override;
185
186 int get_all_xattrs(
187 const ghobject_t &oid,
188 set<string> *out
189 ) override;
190
191 int set_xattrs(
192 const ghobject_t &oid,
193 const map<string, bufferlist> &to_set,
194 const SequencerPosition *spos=0
195 ) override;
196
197 int remove_xattrs(
198 const ghobject_t &oid,
199 const set<string> &to_remove,
200 const SequencerPosition *spos=0
201 ) override;
202
203 int clone(
204 const ghobject_t &oid,
205 const ghobject_t &target,
206 const SequencerPosition *spos=0
207 ) override;
208
209 int rename(
210 const ghobject_t &from,
211 const ghobject_t &to,
212 const SequencerPosition *spos=0
213 );
214
215 int legacy_clone(
216 const ghobject_t &oid,
217 const ghobject_t &target,
218 const SequencerPosition *spos=0
219 );
220
221 /// Read initial state from backing store
222 int init(bool upgrade = false);
223
224 /// Upgrade store to current version
225 int upgrade_to_v2();
226
227 /// Consistency check, debug, there must be no parallel writes
228 int check(std::ostream &out, bool repair = false) override;
229
230 /// Ensure that all previous operations are durable
231 int sync(const ghobject_t *oid=0, const SequencerPosition *spos=0) override;
232
233 /// Util, get all objects, there must be no other concurrent access
234 int list_objects(vector<ghobject_t> *objs ///< [out] objects
235 );
236
237 struct _Header;
238 // Util, get all object headers, there must be no other concurrent access
239 int list_object_headers(vector<_Header> *out ///< [out] headers
240 );
241
242 ObjectMapIterator get_iterator(const ghobject_t &oid) override;
243
244 static const string USER_PREFIX;
245 static const string XATTR_PREFIX;
246 static const string SYS_PREFIX;
247 static const string COMPLETE_PREFIX;
248 static const string HEADER_KEY;
249 static const string USER_HEADER_KEY;
250 static const string GLOBAL_STATE_KEY;
251 static const string HOBJECT_TO_SEQ;
252
253 /// Legacy
254 static const string LEAF_PREFIX;
255 static const string REVERSE_LEAF_PREFIX;
256
257 /// persistent state for store @see generate_header
258 struct State {
259 __u8 v;
260 uint64_t seq;
261 State() : v(0), seq(1) {}
262 explicit State(uint64_t seq) : v(0), seq(seq) {}
263
264 void encode(bufferlist &bl) const {
265 ENCODE_START(2, 1, bl);
266 ::encode(v, bl);
267 ::encode(seq, bl);
268 ENCODE_FINISH(bl);
269 }
270
271 void decode(bufferlist::iterator &bl) {
272 DECODE_START(2, bl);
273 if (struct_v >= 2)
274 ::decode(v, bl);
275 else
276 v = 0;
277 ::decode(seq, bl);
278 DECODE_FINISH(bl);
279 }
280
281 void dump(Formatter *f) const {
282 f->dump_unsigned("seq", seq);
283 }
284
285 static void generate_test_instances(list<State*> &o) {
286 o.push_back(new State(0));
287 o.push_back(new State(20));
288 }
289 } state;
290
291 struct _Header {
292 uint64_t seq;
293 uint64_t parent;
294 uint64_t num_children;
295
296 ghobject_t oid;
297
298 SequencerPosition spos;
299
300 void encode(bufferlist &bl) const {
301 coll_t unused;
302 ENCODE_START(2, 1, bl);
303 ::encode(seq, bl);
304 ::encode(parent, bl);
305 ::encode(num_children, bl);
306 ::encode(unused, bl);
307 ::encode(oid, bl);
308 ::encode(spos, bl);
309 ENCODE_FINISH(bl);
310 }
311
312 void decode(bufferlist::iterator &bl) {
313 coll_t unused;
314 DECODE_START(2, bl);
315 ::decode(seq, bl);
316 ::decode(parent, bl);
317 ::decode(num_children, bl);
318 ::decode(unused, bl);
319 ::decode(oid, bl);
320 if (struct_v >= 2)
321 ::decode(spos, bl);
322 DECODE_FINISH(bl);
323 }
324
325 void dump(Formatter *f) const {
326 f->dump_unsigned("seq", seq);
327 f->dump_unsigned("parent", parent);
328 f->dump_unsigned("num_children", num_children);
329 f->dump_stream("oid") << oid;
330 }
331
332 static void generate_test_instances(list<_Header*> &o) {
333 o.push_back(new _Header);
334 o.push_back(new _Header);
335 o.back()->parent = 20;
336 o.back()->seq = 30;
337 }
338
339 _Header() : seq(0), parent(0), num_children(1) {}
340 };
341
342 /// String munging (public for testing)
343 static string ghobject_key(const ghobject_t &oid);
344 static string ghobject_key_v0(coll_t c, const ghobject_t &oid);
345 static int is_buggy_ghobject_key_v1(CephContext* cct,
346 const string &in);
347 private:
348 /// Implicit lock on Header->seq
349 typedef ceph::shared_ptr<_Header> Header;
350 Mutex cache_lock;
351 SimpleLRU<ghobject_t, _Header> caches;
352
353 string map_header_key(const ghobject_t &oid);
354 string header_key(uint64_t seq);
355 string complete_prefix(Header header);
356 string user_prefix(Header header);
357 string sys_prefix(Header header);
358 string xattr_prefix(Header header);
359 string sys_parent_prefix(_Header header);
360 string sys_parent_prefix(Header header) {
361 return sys_parent_prefix(*header);
362 }
363
364 class EmptyIteratorImpl : public ObjectMapIteratorImpl {
365 public:
366 int seek_to_first() override { return 0; }
367 int seek_to_last() { return 0; }
368 int upper_bound(const string &after) override { return 0; }
369 int lower_bound(const string &to) override { return 0; }
370 bool valid() override { return false; }
371 int next(bool validate=true) override { ceph_abort(); return 0; }
372 string key() override { ceph_abort(); return ""; }
373 bufferlist value() override { ceph_abort(); return bufferlist(); }
374 int status() override { return 0; }
375 };
376
377
378 /// Iterator
379 class DBObjectMapIteratorImpl : public ObjectMapIteratorImpl {
380 public:
381 DBObjectMap *map;
382
383 /// NOTE: implicit lock hlock->get_locked() when returned out of the class
384 MapHeaderLock hlock;
385 /// NOTE: implicit lock on header->seq AND for all ancestors
386 Header header;
387
388 /// parent_iter == NULL iff no parent
389 ceph::shared_ptr<DBObjectMapIteratorImpl> parent_iter;
390 KeyValueDB::Iterator key_iter;
391 KeyValueDB::Iterator complete_iter;
392
393 /// cur_iter points to currently valid iterator
394 ceph::shared_ptr<ObjectMapIteratorImpl> cur_iter;
395 int r;
396
397 /// init() called, key_iter, complete_iter, parent_iter filled in
398 bool ready;
399 /// past end
400 bool invalid;
401
402 DBObjectMapIteratorImpl(DBObjectMap *map, Header header) :
403 map(map), hlock(map), header(header), r(0), ready(false), invalid(true) {}
404 int seek_to_first() override;
405 int seek_to_last();
406 int upper_bound(const string &after) override;
407 int lower_bound(const string &to) override;
408 bool valid() override;
409 int next(bool validate=true) override;
410 string key() override;
411 bufferlist value() override;
412 int status() override;
413
414 bool on_parent() {
415 return cur_iter == parent_iter;
416 }
417
418 /// skips to next valid parent entry
419 int next_parent();
420
421 /// first parent() >= to
422 int lower_bound_parent(const string &to);
423
424 /**
425 * Tests whether to_test is in complete region
426 *
427 * postcondition: complete_iter will be max s.t. complete_iter->value > to_test
428 */
429 int in_complete_region(const string &to_test, ///< [in] key to test
430 string *begin, ///< [out] beginning of region
431 string *end ///< [out] end of region
432 ); ///< @returns true if to_test is in the complete region, else false
433
434 private:
435 int init();
436 bool valid_parent();
437 int adjust();
438 };
439
440 typedef ceph::shared_ptr<DBObjectMapIteratorImpl> DBObjectMapIterator;
441 DBObjectMapIterator _get_iterator(Header header) {
442 return std::make_shared<DBObjectMapIteratorImpl>(this, header);
443 }
444
445 /// sys
446
447 /// Removes node corresponding to header
448 void clear_header(Header header, KeyValueDB::Transaction t);
449
450 /// Set node containing input to new contents
451 void set_header(Header input, KeyValueDB::Transaction t);
452
453 /// Remove leaf node corresponding to oid in c
454 void remove_map_header(
455 const MapHeaderLock &l,
456 const ghobject_t &oid,
457 Header header,
458 KeyValueDB::Transaction t);
459
460 /// Set leaf node for c and oid to the value of header
461 void set_map_header(
462 const MapHeaderLock &l,
463 const ghobject_t &oid, _Header header,
464 KeyValueDB::Transaction t);
465
466 /// Set leaf node for c and oid to the value of header
467 bool check_spos(const ghobject_t &oid,
468 Header header,
469 const SequencerPosition *spos);
470
471 /// Lookup or create header for c oid
472 Header lookup_create_map_header(
473 const MapHeaderLock &l,
474 const ghobject_t &oid,
475 KeyValueDB::Transaction t);
476
477 /**
478 * Generate new header for c oid with new seq number
479 *
480 * Has the side effect of syncronously saving the new DBObjectMap state
481 */
482 Header _generate_new_header(const ghobject_t &oid, Header parent);
483 Header generate_new_header(const ghobject_t &oid, Header parent) {
484 Mutex::Locker l(header_lock);
485 return _generate_new_header(oid, parent);
486 }
487
488 /// Lookup leaf header for c oid
489 Header _lookup_map_header(
490 const MapHeaderLock &l,
491 const ghobject_t &oid);
492 Header lookup_map_header(
493 const MapHeaderLock &l2,
494 const ghobject_t &oid) {
495 Mutex::Locker l(header_lock);
496 return _lookup_map_header(l2, oid);
497 }
498
499 /// Lookup header node for input
500 Header lookup_parent(Header input);
501
502
503 /// Helpers
504 int _get_header(Header header, bufferlist *bl);
505
506 /// Scan keys in header into out_keys and out_values (if nonnull)
507 int scan(Header header,
508 const set<string> &in_keys,
509 set<string> *out_keys,
510 map<string, bufferlist> *out_values);
511
512 /// Remove header and all related prefixes
513 int _clear(Header header,
514 KeyValueDB::Transaction t);
515
516 /* Scan complete region bumping *begin to the beginning of any
517 * containing region and adding all complete region keys between
518 * the updated begin and end to the complete_keys_to_remove set */
519 int merge_new_complete(DBObjectMapIterator &iter,
520 string *begin,
521 const string &end,
522 set<string> *complete_keys_to_remove);
523
524 /// Writes out State (mainly next_seq)
525 int write_state(KeyValueDB::Transaction _t =
526 KeyValueDB::Transaction());
527
528 /// Copies header entry from parent @see rm_keys
529 int copy_up_header(Header header,
530 KeyValueDB::Transaction t);
531
532 /// Sets header @see set_header
533 void _set_header(Header header, const bufferlist &bl,
534 KeyValueDB::Transaction t);
535
536 /**
537 * Removes header seq lock and possibly object lock
538 * once Header is out of scope
539 * @see lookup_parent
540 * @see generate_new_header
541 */
542 class RemoveOnDelete {
543 public:
544 DBObjectMap *db;
545 explicit RemoveOnDelete(DBObjectMap *db) :
546 db(db) {}
547 void operator() (_Header *header) {
548 Mutex::Locker l(db->header_lock);
549 assert(db->in_use.count(header->seq));
550 db->in_use.erase(header->seq);
551 db->header_cond.Signal();
552 delete header;
553 }
554 };
555 friend class RemoveOnDelete;
556 };
557 WRITE_CLASS_ENCODER(DBObjectMap::_Header)
558 WRITE_CLASS_ENCODER(DBObjectMap::State)
559
560 ostream& operator<<(ostream& out, const DBObjectMap::_Header& h);
561
562 #endif