forked from ceph/ceph
/
BlueStore.h
1668 lines (1436 loc) · 45.8 KB
/
BlueStore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2014 Red Hat
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#ifndef CEPH_OSD_BLUESTORE_H
#define CEPH_OSD_BLUESTORE_H
#include "acconfig.h"
#include <unistd.h>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/unordered_set.hpp>
#include <boost/intrusive/set.hpp>
#include <boost/functional/hash.hpp>
#include "include/assert.h"
#include "include/unordered_map.h"
#include "include/memory.h"
#include "common/Finisher.h"
#include "compressor/Compressor.h"
#include "os/ObjectStore.h"
#include "bluestore_types.h"
#include "BlockDevice.h"
class Allocator;
class FreelistManager;
class BlueFS;
//#define DEBUG_CACHE
enum {
l_bluestore_first = 732430,
l_bluestore_state_prepare_lat,
l_bluestore_state_aio_wait_lat,
l_bluestore_state_io_done_lat,
l_bluestore_state_kv_queued_lat,
l_bluestore_state_kv_committing_lat,
l_bluestore_state_kv_done_lat,
l_bluestore_state_wal_queued_lat,
l_bluestore_state_wal_applying_lat,
l_bluestore_state_wal_aio_wait_lat,
l_bluestore_state_wal_cleanup_lat,
l_bluestore_state_finishing_lat,
l_bluestore_state_done_lat,
l_bluestore_write_pad_bytes,
l_bluestore_wal_write_ops,
l_bluestore_wal_write_bytes,
l_bluestore_write_penalty_read_ops,
l_bluestore_last
};
class BlueStore : public ObjectStore,
public md_config_obs_t {
// -----------------------------------------------------
// types
public:
// config observer
virtual const char** get_tracked_conf_keys() const override;
virtual void handle_conf_change(const struct md_config_t *conf,
const std::set<std::string> &changed) override;
void _set_csum();
void _set_compression();
class TransContext;
class Blob;
// --------------------------------------------------------
// intermediate data structures used while reading
struct region_t {
uint64_t logical_offset;
uint64_t blob_xoffset; //region offset within the blob
uint64_t length;
region_t(uint64_t offset, uint64_t b_offs, uint64_t len)
: logical_offset(offset),
blob_xoffset(b_offs),
length(len) {}
region_t(const region_t& from)
: logical_offset(from.logical_offset),
blob_xoffset(from.blob_xoffset),
length(from.length) {}
friend ostream& operator<<(ostream& out, const region_t& r) {
return out << "0x" << std::hex << r.logical_offset << ":"
<< r.blob_xoffset << "~" << r.length << std::dec;
}
};
typedef list<region_t> regions2read_t;
typedef map<Blob*, regions2read_t> blobs2read_t;
typedef map<uint64_t, bufferlist> ready_regions_t;
struct BufferSpace;
/// cached buffer
struct Buffer {
enum {
STATE_EMPTY, ///< empty buffer -- used for cache history
STATE_CLEAN, ///< clean data that is up to date
STATE_WRITING, ///< data that is being written (io not yet complete)
};
static const char *get_state_name(int s) {
switch (s) {
case STATE_EMPTY: return "empty";
case STATE_CLEAN: return "clean";
case STATE_WRITING: return "writing";
default: return "???";
}
}
enum {
FLAG_NOCACHE = 1, ///< trim when done WRITING (do not become CLEAN)
// NOTE: fix operator<< when you define a second flag
};
static const char *get_flag_name(int s) {
switch (s) {
case FLAG_NOCACHE: return "nocache";
default: return "???";
}
}
BufferSpace *space;
uint16_t state; ///< STATE_*
uint16_t cache_private = 0; ///< opaque (to us) value used by Cache impl
uint32_t flags; ///< FLAG_*
uint64_t seq;
uint64_t offset, length;
bufferlist data;
boost::intrusive::list_member_hook<> lru_item;
boost::intrusive::list_member_hook<> state_item;
Buffer(BufferSpace *space, unsigned s, uint64_t q, uint64_t o, uint64_t l,
unsigned f = 0)
: space(space), state(s), flags(f), seq(q), offset(o), length(l) {}
Buffer(BufferSpace *space, unsigned s, uint64_t q, uint64_t o, bufferlist& b,
unsigned f = 0)
: space(space), state(s), flags(f), seq(q), offset(o),
length(b.length()), data(b) {}
bool is_empty() const {
return state == STATE_EMPTY;
}
bool is_clean() const {
return state == STATE_CLEAN;
}
bool is_writing() const {
return state == STATE_WRITING;
}
uint64_t end() const {
return offset + length;
}
void truncate(uint64_t newlen) {
assert(newlen < length);
if (data.length()) {
bufferlist t;
t.substr_of(data, 0, newlen);
data.claim(t);
}
length = newlen;
}
void dump(Formatter *f) const {
f->dump_string("state", get_state_name(state));
f->dump_unsigned("seq", seq);
f->dump_unsigned("offset", offset);
f->dump_unsigned("length", length);
f->dump_unsigned("data_length", data.length());
}
};
struct Cache;
/// map logical extent range (object) onto buffers
struct BufferSpace {
typedef boost::intrusive::list<
Buffer,
boost::intrusive::member_hook<
Buffer,
boost::intrusive::list_member_hook<>,
&Buffer::state_item> > state_list_t;
map<uint64_t,std::unique_ptr<Buffer>> buffer_map;
Cache *cache;
state_list_t writing;
BufferSpace(Cache *c) : cache(c) {}
~BufferSpace() {
assert(buffer_map.empty());
assert(writing.empty());
}
void _add_buffer(Buffer *b, int level, Buffer *near) {
cache->_audit("_add_buffer start");
buffer_map[b->offset].reset(b);
if (b->is_writing()) {
writing.push_back(*b);
} else {
cache->_add_buffer(b, level, near);
}
cache->_audit("_add_buffer end");
}
void _rm_buffer(Buffer *b) {
_rm_buffer(buffer_map.find(b->offset));
}
void _rm_buffer(map<uint64_t,std::unique_ptr<Buffer>>::iterator p) {
cache->_audit("_rm_buffer start");
if (p->second->is_writing()) {
writing.erase(writing.iterator_to(*p->second));
} else {
cache->_rm_buffer(p->second.get());
}
buffer_map.erase(p);
cache->_audit("_rm_buffer end");
}
map<uint64_t,std::unique_ptr<Buffer>>::iterator _data_lower_bound(
uint64_t offset) {
auto i = buffer_map.lower_bound(offset);
if (i != buffer_map.begin()) {
--i;
if (i->first + i->second->length <= offset)
++i;
}
return i;
}
bool empty() const {
return buffer_map.empty();
}
// must be called under protection of the Cache lock
void _clear();
// return value is the highest cache_private of a trimmed buffer, or 0.
int discard(uint64_t offset, uint64_t length) {
std::lock_guard<std::mutex> l(cache->lock);
return _discard(offset, length);
}
int _discard(uint64_t offset, uint64_t length);
void write(uint64_t seq, uint64_t offset, bufferlist& bl, unsigned flags) {
std::lock_guard<std::mutex> l(cache->lock);
Buffer *b = new Buffer(this, Buffer::STATE_WRITING, seq, offset, bl,
flags);
b->cache_private = _discard(offset, bl.length());
_add_buffer(b, (flags & Buffer::FLAG_NOCACHE) ? 0 : 1, nullptr);
}
void finish_write(uint64_t seq);
void did_read(uint64_t offset, bufferlist& bl) {
std::lock_guard<std::mutex> l(cache->lock);
Buffer *b = new Buffer(this, Buffer::STATE_CLEAN, 0, offset, bl);
b->cache_private = _discard(offset, bl.length());
_add_buffer(b, 1, nullptr);
}
void read(uint64_t offset, uint64_t length,
BlueStore::ready_regions_t& res,
interval_set<uint64_t>& res_intervals);
void truncate(uint64_t offset) {
discard(offset, (uint64_t)-1 - offset);
}
void dump(Formatter *f) const {
std::lock_guard<std::mutex> l(cache->lock);
f->open_array_section("buffers");
for (auto& i : buffer_map) {
f->open_object_section("buffer");
assert(i.first == i.second->offset);
i.second->dump(f);
f->close_section();
}
f->close_section();
}
};
struct BnodeSet;
/// in-memory blob metadata and associated cached buffers (if any)
struct Blob : public boost::intrusive::set_base_hook<> {
int64_t id = 0; ///< id
bluestore_blob_t blob; ///< blob metadata
BufferSpace bc; ///< buffer cache
Blob(int64_t i, Cache *c) : id(i), bc(c) {}
~Blob() {
assert(bc.empty());
}
// comparators for intrusive_set
friend bool operator<(const Blob &a, const Blob &b) {
return a.id < b.id;
}
friend bool operator>(const Blob &a, const Blob &b) {
return a.id > b.id;
}
friend bool operator==(const Blob &a, const Blob &b) {
return a.id == b.id;
}
friend ostream& operator<<(ostream& out, const Blob &b) {
return out << b.id << ":" << b.blob;
}
/// discard buffers for unallocated regions
void discard_unallocated();
};
/// a map of blobs, indexed by int64_t
struct BlobMap {
typedef boost::intrusive::set<Blob> blob_map_t;
blob_map_t blob_map;
void encode(bufferlist& bl) const;
void decode(bufferlist::iterator& p, Cache *c);
bool empty() const {
return blob_map.empty();
}
Blob *get(int64_t id) {
Blob dummy(id, nullptr);
auto p = blob_map.find(dummy);
if (p != blob_map.end()) {
return &*p;
}
return nullptr;
}
Blob *new_blob(Cache *c) {
int64_t id = get_new_id();
Blob *b = new Blob(id, c);
blob_map.insert(*b);
return b;
}
void claim(Blob *b) {
assert(b->id == 0);
b->id = get_new_id();
blob_map.insert(*b);
}
void erase(Blob *b) {
blob_map.erase(*b);
b->id = 0;
}
int64_t get_new_id() {
return blob_map.empty() ? 1 : blob_map.rbegin()->id + 1;
}
// must be called under protection of the Cache lock
void _clear() {
while (!blob_map.empty()) {
Blob *b = &*blob_map.begin();
b->bc._clear();
erase(b);
delete b;
}
}
friend ostream& operator<<(ostream& out, const BlobMap& m) {
out << '{';
for (auto p = m.blob_map.begin(); p != m.blob_map.end(); ++p) {
if (p != m.blob_map.begin()) {
out << ',';
}
out << p->id << '=' << p->blob;
}
return out << '}';
}
};
/// an in-memory extent-map, shared by a group of objects (w/ same hash value)
struct Bnode : public boost::intrusive::unordered_set_base_hook<> {
std::atomic_int nref; ///< reference count
uint32_t hash;
string key; ///< key under PREFIX_OBJ where we are stored
BnodeSet *bnode_set; ///< reference to the containing set
BlobMap blob_map;
Bnode(uint32_t h, const string& k, BnodeSet *s)
: nref(0),
hash(h),
key(k),
bnode_set(s) {}
void get() {
++nref;
}
void put();
friend void intrusive_ptr_add_ref(Bnode *e) { e->get(); }
friend void intrusive_ptr_release(Bnode *e) { e->put(); }
friend bool operator==(const Bnode &l, const Bnode &r) {
return l.hash == r.hash;
}
friend std::size_t hash_value(const Bnode &e) {
return e.hash;
}
};
typedef boost::intrusive_ptr<Bnode> BnodeRef;
/// hash of Bnodes, by (object) hash value
struct BnodeSet {
typedef boost::intrusive::unordered_set<Bnode>::bucket_type bucket_type;
typedef boost::intrusive::unordered_set<Bnode>::bucket_traits bucket_traits;
std::mutex lock;
unsigned num_buckets;
vector<bucket_type> buckets;
boost::intrusive::unordered_set<Bnode> uset;
Bnode dummy; ///< dummy entry used for lookups. protected by lock.
explicit BnodeSet(unsigned n)
: num_buckets(n),
buckets(n),
uset(bucket_traits(buckets.data(), num_buckets)),
dummy(0, string(), NULL) {
assert(n > 0);
}
~BnodeSet() {
assert(uset.empty());
}
BnodeRef get(uint32_t hash);
void add(Bnode *b) {
std::lock_guard<std::mutex> l(lock);
uset.insert(*b);
}
bool remove(Bnode *b) {
std::lock_guard<std::mutex> l(lock);
if (b->nref == 0) {
uset.erase(*b);
return true;
}
return false;
}
};
struct OnodeSpace;
/// an in-memory object
struct Onode {
std::atomic_int nref; ///< reference count
ghobject_t oid;
string key; ///< key under PREFIX_OBJ where we are stored
OnodeSpace *space; ///< containing OnodeSpace
boost::intrusive::list_member_hook<> lru_item;
BnodeRef bnode; ///< ref to Bnode [optional]
bluestore_onode_t onode; ///< metadata stored as value in kv store
bool exists;
BlobMap blob_map; ///< local blobs (this onode onode)
std::mutex flush_lock; ///< protect flush_txns
std::condition_variable flush_cond; ///< wait here for unapplied txns
set<TransContext*> flush_txns; ///< committing or wal txns
Onode(OnodeSpace *s, const ghobject_t& o, const string& k)
: nref(0),
oid(o),
key(k),
space(s),
exists(false) {
}
Blob *get_blob(int64_t id) {
if (id < 0) {
assert(bnode);
return bnode->blob_map.get(-id);
}
return blob_map.get(id);
}
void flush();
void get() {
++nref;
}
void put() {
if (--nref == 0)
delete this;
}
};
typedef boost::intrusive_ptr<Onode> OnodeRef;
/// a cache (shard) of onodes and buffers
struct Cache {
std::mutex lock; ///< protect lru and other structures
static Cache *create(string type);
virtual ~Cache() {}
virtual void _add_onode(OnodeRef& o, int level) = 0;
virtual void _rm_onode(OnodeRef& o) = 0;
virtual void _touch_onode(OnodeRef& o) = 0;
virtual void _add_buffer(Buffer *b, int level, Buffer *near) = 0;
virtual void _rm_buffer(Buffer *b) = 0;
virtual void _adjust_buffer_size(Buffer *b, int64_t delta) = 0;
virtual void _touch_buffer(Buffer *b) = 0;
virtual void trim(uint64_t onode_max, uint64_t buffer_max) = 0;
#ifdef DEBUG_CACHE
virtual void _audit(const char *s) = 0;
#else
void _audit(const char *s) { /* no-op */ }
#endif
};
/// simple LRU cache for onodes and buffers
struct LRUCache : public Cache {
private:
typedef boost::intrusive::list<
Onode,
boost::intrusive::member_hook<
Onode,
boost::intrusive::list_member_hook<>,
&Onode::lru_item> > onode_lru_list_t;
typedef boost::intrusive::list<
Buffer,
boost::intrusive::member_hook<
Buffer,
boost::intrusive::list_member_hook<>,
&Buffer::lru_item> > buffer_lru_list_t;
onode_lru_list_t onode_lru;
buffer_lru_list_t buffer_lru;
uint64_t buffer_size = 0;
public:
void _add_onode(OnodeRef& o, int level) override {
if (level > 0)
onode_lru.push_front(*o);
else
onode_lru.push_back(*o);
}
void _rm_onode(OnodeRef& o) override {
auto q = onode_lru.iterator_to(*o);
onode_lru.erase(q);
}
void _touch_onode(OnodeRef& o) override;
void _add_buffer(Buffer *b, int level, Buffer *near) override {
if (near) {
auto q = buffer_lru.iterator_to(*near);
buffer_lru.insert(q, *b);
} else if (level > 0) {
buffer_lru.push_front(*b);
} else {
buffer_lru.push_back(*b);
}
buffer_size += b->length;
}
void _rm_buffer(Buffer *b) override {
assert(buffer_size >= b->length);
buffer_size -= b->length;
auto q = buffer_lru.iterator_to(*b);
buffer_lru.erase(q);
}
void _adjust_buffer_size(Buffer *b, int64_t delta) override {
assert((int64_t)buffer_size + delta >= 0);
buffer_size += delta;
}
void _touch_buffer(Buffer *b) override {
auto p = buffer_lru.iterator_to(*b);
buffer_lru.erase(p);
buffer_lru.push_front(*b);
_audit("_touch_buffer end");
}
void trim(uint64_t onode_max, uint64_t buffer_max) override;
#ifdef DEBUG_CACHE
void _audit(const char *s) override;
#endif
};
// 2Q cache for buffers, LRU for onodes
struct TwoQCache : public Cache {
private:
// stick with LRU for onodes for now (fixme?)
typedef boost::intrusive::list<
Onode,
boost::intrusive::member_hook<
Onode,
boost::intrusive::list_member_hook<>,
&Onode::lru_item> > onode_lru_list_t;
typedef boost::intrusive::list<
Buffer,
boost::intrusive::member_hook<
Buffer,
boost::intrusive::list_member_hook<>,
&Buffer::lru_item> > buffer_list_t;
onode_lru_list_t onode_lru;
buffer_list_t buffer_hot; //< "Am" hot buffers
buffer_list_t buffer_warm_in; //< "A1in" newly warm buffers
buffer_list_t buffer_warm_out; //< "A1out" empty buffers we've evicted
uint64_t buffer_bytes = 0; //< bytes
enum {
BUFFER_NEW = 0,
BUFFER_WARM_IN, ///< in buffer_warm_in
BUFFER_WARM_OUT, ///< in buffer_warm_out
BUFFER_HOT, ///< in buffer_hot
};
public:
void _add_onode(OnodeRef& o, int level) override {
if (level > 0)
onode_lru.push_front(*o);
else
onode_lru.push_back(*o);
}
void _rm_onode(OnodeRef& o) override {
auto q = onode_lru.iterator_to(*o);
onode_lru.erase(q);
}
void _touch_onode(OnodeRef& o) override;
void _add_buffer(Buffer *b, int level, Buffer *near) override;
void _rm_buffer(Buffer *b) override;
void _adjust_buffer_size(Buffer *b, int64_t delta) override;
void _touch_buffer(Buffer *b) override {
switch (b->cache_private) {
case BUFFER_WARM_IN:
// do nothing (somewhat counter-intuitively!)
break;
case BUFFER_WARM_OUT:
// move from warm_out to hot LRU
assert(0 == "this happens via discard hint");
break;
case BUFFER_HOT:
// move to front of hot LRU
buffer_hot.erase(buffer_hot.iterator_to(*b));
buffer_hot.push_front(*b);
break;
}
_audit("_touch_buffer end");
}
void trim(uint64_t onode_max, uint64_t buffer_max) override;
#ifdef DEBUG_CACHE
void _audit(const char *s) override;
#endif
};
struct OnodeSpace {
Cache *cache;
ceph::unordered_map<ghobject_t,OnodeRef> onode_map; ///< forward lookups
OnodeSpace(Cache *c) : cache(c) {}
~OnodeSpace() {
clear();
}
void add(const ghobject_t& oid, OnodeRef o);
OnodeRef lookup(const ghobject_t& o);
void rename(OnodeRef& o, const ghobject_t& old_oid,
const ghobject_t& new_oid);
void clear();
/// return true if f true for any item
bool map_any(std::function<bool(OnodeRef)> f);
};
struct Cache;
struct Collection : public CollectionImpl {
BlueStore *store;
Cache *cache; ///< our cache shard
coll_t cid;
bluestore_cnode_t cnode;
RWLock lock;
bool exists;
BnodeSet bnode_set; ///< open Bnodes
// cache onodes on a per-collection basis to avoid lock
// contention.
OnodeSpace onode_map;
OnodeRef get_onode(const ghobject_t& oid, bool create);
BnodeRef get_bnode(uint32_t hash);
Blob *get_blob(OnodeRef& o, int64_t blob) {
if (blob < 0) {
if (!o->bnode) {
o->bnode = get_bnode(o->oid.hobj.get_hash());
}
return o->bnode->blob_map.get(-blob);
}
return o->blob_map.get(blob);
}
const coll_t &get_cid() override {
return cid;
}
bool contains(const ghobject_t& oid) {
if (cid.is_meta())
return oid.hobj.pool == -1;
spg_t spgid;
if (cid.is_pg(&spgid))
return
spgid.pgid.contains(cnode.bits, oid) &&
oid.shard_id == spgid.shard;
return false;
}
Collection(BlueStore *ns, Cache *ca, coll_t c);
};
typedef boost::intrusive_ptr<Collection> CollectionRef;
class OmapIteratorImpl : public ObjectMap::ObjectMapIteratorImpl {
CollectionRef c;
OnodeRef o;
KeyValueDB::Iterator it;
string head, tail;
public:
OmapIteratorImpl(CollectionRef c, OnodeRef o, KeyValueDB::Iterator it);
int seek_to_first();
int upper_bound(const string &after);
int lower_bound(const string &to);
bool valid();
int next(bool validate=true);
string key();
bufferlist value();
int status() {
return 0;
}
};
class OpSequencer;
typedef boost::intrusive_ptr<OpSequencer> OpSequencerRef;
struct TransContext {
typedef enum {
STATE_PREPARE,
STATE_AIO_WAIT,
STATE_IO_DONE,
STATE_KV_QUEUED,
STATE_KV_COMMITTING,
STATE_KV_DONE,
STATE_WAL_QUEUED,
STATE_WAL_APPLYING,
STATE_WAL_AIO_WAIT,
STATE_WAL_CLEANUP, // remove wal kv record
STATE_WAL_DONE,
STATE_FINISHING,
STATE_DONE,
} state_t;
state_t state;
const char *get_state_name() {
switch (state) {
case STATE_PREPARE: return "prepare";
case STATE_AIO_WAIT: return "aio_wait";
case STATE_IO_DONE: return "io_done";
case STATE_KV_QUEUED: return "kv_queued";
case STATE_KV_COMMITTING: return "kv_committing";
case STATE_KV_DONE: return "kv_done";
case STATE_WAL_QUEUED: return "wal_queued";
case STATE_WAL_APPLYING: return "wal_applying";
case STATE_WAL_AIO_WAIT: return "wal_aio_wait";
case STATE_WAL_CLEANUP: return "wal_cleanup";
case STATE_WAL_DONE: return "wal_done";
case STATE_FINISHING: return "finishing";
case STATE_DONE: return "done";
}
return "???";
}
void log_state_latency(PerfCounters *logger, int state) {
utime_t lat, now = ceph_clock_now(g_ceph_context);
lat = now - start;
logger->tinc(state, lat);
start = now;
}
OpSequencerRef osr;
boost::intrusive::list_member_hook<> sequencer_item;
uint64_t ops, bytes;
set<OnodeRef> onodes; ///< these onodes need to be updated/written
set<BnodeRef> bnodes; ///< these bnodes need to be updated/written
KeyValueDB::Transaction t; ///< then we will commit this
Context *oncommit; ///< signal on commit
Context *onreadable; ///< signal on readable
Context *onreadable_sync; ///< signal on readable
list<Context*> oncommits; ///< more commit completions
list<CollectionRef> removed_collections; ///< colls we removed
boost::intrusive::list_member_hook<> wal_queue_item;
bluestore_wal_transaction_t *wal_txn; ///< wal transaction (if any)
vector<OnodeRef> wal_op_onodes;
interval_set<uint64_t> allocated, released;
struct volatile_statfs{
enum {
STATFS_ALLOCATED = 0,
STATFS_STORED,
STATFS_COMPRESSED_ORIGINAL,
STATFS_COMPRESSED,
STATFS_COMPRESSED_ALLOCATED,
STATFS_LAST
};
int64_t values[STATFS_LAST];
volatile_statfs() {
memset(this, 0, sizeof(volatile_statfs));
}
void reset() {
*this = volatile_statfs();
}
int64_t& allocated() {
return values[STATFS_ALLOCATED];
}
int64_t& stored() {
return values[STATFS_STORED];
}
int64_t& compressed_original() {
return values[STATFS_COMPRESSED_ORIGINAL];
}
int64_t& compressed() {
return values[STATFS_COMPRESSED];
}
int64_t& compressed_allocated() {
return values[STATFS_COMPRESSED_ALLOCATED];
}
bool is_empty() {
return values[STATFS_ALLOCATED] == 0 &&
values[STATFS_STORED] == 0 &&
values[STATFS_COMPRESSED] == 0 &&
values[STATFS_COMPRESSED_ORIGINAL] == 0 &&
values[STATFS_COMPRESSED_ALLOCATED] == 0;
}
void decode(bufferlist::iterator& it) {
for (size_t i = 0; i < STATFS_LAST; i++) {
::decode(values[i], it);
}
}
void encode(bufferlist& bl) {
for (size_t i = 0; i < STATFS_LAST; i++) {
//::encode(ceph_le64(values[i]), bl);
::encode(values[i], bl);
}
}
} statfs_delta;
IOContext ioc;
CollectionRef first_collection; ///< first referenced collection
uint64_t seq = 0;
utime_t start;
struct DeferredCsum {
OnodeRef onode;
int64_t blob;
uint64_t b_off;
bufferlist data;
DeferredCsum(OnodeRef& o, int64_t b, uint64_t bo, bufferlist& bl)
: onode(o), blob(b), b_off(bo), data(bl) {}
};
list<DeferredCsum> deferred_csum;
explicit TransContext(OpSequencer *o)
: state(STATE_PREPARE),
osr(o),
ops(0),
bytes(0),
oncommit(NULL),
onreadable(NULL),
onreadable_sync(NULL),
wal_txn(NULL),
ioc(this),
start(ceph_clock_now(g_ceph_context)) {
//cout << "txc new " << this << std::endl;
}
~TransContext() {
delete wal_txn;
//cout << "txc del " << this << std::endl;
}
void write_onode(OnodeRef &o) {
onodes.insert(o);
}
void write_bnode(BnodeRef &e) {
bnodes.insert(e);
}
void add_deferred_csum(OnodeRef& o, int64_t b, uint64_t bo, bufferlist& bl) {
deferred_csum.emplace_back(TransContext::DeferredCsum(o, b, bo, bl));
}
};
class OpSequencer : public Sequencer_impl {
public:
std::mutex qlock;
std::condition_variable qcond;
typedef boost::intrusive::list<
TransContext,
boost::intrusive::member_hook<
TransContext,
boost::intrusive::list_member_hook<>,
&TransContext::sequencer_item> > q_list_t;
q_list_t q; ///< transactions
typedef boost::intrusive::list<
TransContext,
boost::intrusive::member_hook<
TransContext,
boost::intrusive::list_member_hook<>,
&TransContext::wal_queue_item> > wal_queue_t;
wal_queue_t wal_q; ///< transactions
boost::intrusive::list_member_hook<> wal_osr_queue_item;
Sequencer *parent;
std::mutex wal_apply_mutex;
uint64_t last_seq = 0;
OpSequencer()
//set the qlock to PTHREAD_MUTEX_RECURSIVE mode
: parent(NULL) {
}
~OpSequencer() {
assert(q.empty());
}
void queue_new(TransContext *txc) {
std::lock_guard<std::mutex> l(qlock);
txc->seq = ++last_seq;
q.push_back(*txc);
}
void flush() {
std::unique_lock<std::mutex> l(qlock);
while (!q.empty())
qcond.wait(l);
}
bool flush_commit(Context *c) {
std::lock_guard<std::mutex> l(qlock);
if (q.empty()) {
return true;
}
TransContext *txc = &q.back();
if (txc->state >= TransContext::STATE_KV_DONE) {
return true;
}
assert(txc->state < TransContext::STATE_KV_DONE);
txc->oncommits.push_back(c);
return false;
}
/// if there is a wal on @seq, wait for it to apply
void wait_for_wal_on_seq(uint64_t seq) {