/
BlueStore.h
1997 lines (1721 loc) · 56.7 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 <boost/dynamic_bitset.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_compress_lat,
l_bluestore_decompress_lat,
l_bluestore_csum_lat,
l_bluestore_compress_success_count,
l_bluestore_compress_rejected_count,
l_bluestore_write_pad_bytes,
l_bluestore_wal_write_ops,
l_bluestore_wal_write_bytes,
l_bluestore_write_penalty_read_ops,
l_bluestore_allocated,
l_bluestore_stored,
l_bluestore_compressed,
l_bluestore_compressed_allocated,
l_bluestore_compressed_original,
l_bluestore_onodes,
l_bluestore_onode_hits,
l_bluestore_onode_misses,
l_bluestore_extents,
l_bluestore_blobs,
l_bluestore_buffers,
l_bluestore_buffer_bytes,
l_bluestore_buffer_hit_bytes,
l_bluestore_buffer_miss_bytes,
l_bluestore_write_big,
l_bluestore_write_big_bytes,
l_bluestore_write_big_blobs,
l_bluestore_write_small,
l_bluestore_write_small_bytes,
l_bluestore_write_small_unused,
l_bluestore_write_small_wal,
l_bluestore_write_small_pre_read,
l_bluestore_write_small_new,
l_bluestore_txc,
l_bluestore_onode_reshard,
l_bluestore_gc_bytes,
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;
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;
uint32_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, uint32_t o, uint32_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, uint32_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;
}
uint32_t end() const {
return offset + length;
}
void truncate(uint32_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;
// we use a bare intrusive list here instead of std::map because
// it uses less memory and we expect this to be very small (very
// few IOs in flight to the same Blob at the same time).
state_list_t writing; ///< writing buffers, sorted by seq, ascending
BufferSpace(Cache *c) : cache(c) {
if (cache) {
cache->add_blob();
}
}
~BufferSpace() {
assert(buffer_map.empty());
assert(writing.empty());
if (cache) {
cache->rm_blob();
}
}
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::recursive_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::recursive_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::recursive_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::recursive_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 SharedBlobSet;
/// in-memory shared blob state (incl cached buffers)
struct SharedBlob : public boost::intrusive::unordered_set_base_hook<> {
std::atomic_int nref = {0}; ///< reference count
// these are defined/set if the shared_blob is 'loaded'
bool loaded = false; ///< whether shared_blob_t is loaded
bluestore_shared_blob_t shared_blob; ///< the actual shared state
// these are defined/set if the blob is marked 'shared'
uint64_t sbid = 0; ///< shared blob id
string key; ///< key in kv store
SharedBlobSet *parent_set = 0; ///< containing SharedBlobSet
BufferSpace bc; ///< buffer cache
SharedBlob(uint64_t i, const string& k, Cache *c);
~SharedBlob();
friend void intrusive_ptr_add_ref(SharedBlob *b) { b->get(); }
friend void intrusive_ptr_release(SharedBlob *b) { b->put(); }
friend ostream& operator<<(ostream& out, const SharedBlob& sb);
void get() {
++nref;
}
void put();
friend bool operator==(const SharedBlob &l, const SharedBlob &r) {
return l.sbid == r.sbid;
}
friend std::size_t hash_value(const SharedBlob &e) {
rjhash<uint32_t> h;
return h(e.sbid);
}
};
typedef boost::intrusive_ptr<SharedBlob> SharedBlobRef;
/// a lookup table of SharedBlobs
struct SharedBlobSet {
typedef boost::intrusive::unordered_set<SharedBlob>::bucket_type bucket_type;
typedef boost::intrusive::unordered_set<SharedBlob>::bucket_traits bucket_traits;
std::mutex lock; ///< protect lookup, insertion, removal
int num_buckets;
vector<bucket_type> buckets;
boost::intrusive::unordered_set<SharedBlob> uset;
SharedBlob dummy; ///< for lookups
explicit SharedBlobSet(unsigned n)
: num_buckets(n),
buckets(n),
uset(bucket_traits(buckets.data(), num_buckets)),
dummy(0, string(), nullptr) {
assert(n > 0);
}
SharedBlobRef lookup(uint64_t sbid) {
std::lock_guard<std::mutex> l(lock);
dummy.sbid = sbid;
auto p = uset.find(dummy);
if (p == uset.end()) {
return nullptr;
}
return &*p;
}
void add(SharedBlob *sb) {
std::lock_guard<std::mutex> l(lock);
uset.insert(*sb);
sb->parent_set = this;
}
bool remove(SharedBlob *sb) {
std::lock_guard<std::mutex> l(lock);
if (sb->nref == 0) {
assert(sb->parent_set == this);
uset.erase(*sb);
return true;
}
return false;
}
bool empty() {
std::lock_guard<std::mutex> l(lock);
return uset.empty();
}
};
/// in-memory blob metadata and associated cached buffers (if any)
struct Blob : public boost::intrusive::set_base_hook<> {
std::atomic_int nref = {0}; ///< reference count
int16_t id = -1; ///< id, for spanning blobs only, >= 0
int16_t last_encoded_id = -1; ///< (ephemeral) used during encoding only
SharedBlobRef shared_blob; ///< shared blob state (if any)
/// refs from this shard. ephemeral if id<0, persisted if spanning.
bluestore_extent_ref_map_t ref_map;
private:
mutable bluestore_blob_t blob; ///< decoded blob metadata
mutable bool dirty = true; ///< true if blob is newer than blob_bl
mutable bufferlist blob_bl; ///< cached encoded blob
public:
Blob() {}
~Blob() {
}
friend void intrusive_ptr_add_ref(Blob *b) { b->get(); }
friend void intrusive_ptr_release(Blob *b) { b->put(); }
friend ostream& operator<<(ostream& out, const Blob &b);
// 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;
}
bool is_spanning() const {
return id >= 0;
}
void dup(Blob& o) {
o.shared_blob = shared_blob;
o.blob = blob;
o.dirty = dirty;
o.blob_bl = blob_bl;
}
const bluestore_blob_t& get_blob() const {
return blob;
}
bluestore_blob_t& dirty_blob() {
if (!dirty) {
dirty = true;
blob_bl.clear();
}
return blob;
}
size_t get_encoded_length() const {
return blob_bl.length();
}
bool is_dirty() const {
return dirty;
}
bool is_unreferenced(uint64_t offset, uint64_t length) const {
return !ref_map.intersects(offset, length);
}
/// discard buffers for unallocated regions
void discard_unallocated();
/// get logical references
void get_ref(uint64_t offset, uint64_t length);
/// put logical references, and get back any released extents
bool put_ref(uint64_t offset, uint64_t length, uint64_t min_alloc_size,
vector<bluestore_pextent_t> *r);
void get() {
++nref;
}
void put() {
if (--nref == 0)
delete this;
}
void encode(bufferlist& bl) const {
if (dirty) {
// manage blob_bl memory carefully
blob_bl.clear();
blob_bl.reserve(blob.estimate_encoded_size());
::encode(blob, blob_bl);
dirty = false;
} else {
assert(blob_bl.length());
}
bl.append(blob_bl);
}
void decode(bufferlist::iterator& p) {
bufferlist::iterator s = p;
::decode(blob, p);
s.copy(p.get_off() - s.get_off(), blob_bl);
dirty = false;
}
};
typedef boost::intrusive_ptr<Blob> BlobRef;
typedef boost::intrusive::set<Blob> blob_map_t;
/// a logical extent, pointing to (some portion of) a blob
struct Extent : public boost::intrusive::set_base_hook<> {
uint32_t logical_offset = 0; ///< logical offset
uint32_t blob_offset = 0; ///< blob offset
uint32_t length = 0; ///< length
uint8_t blob_depth; /// blob overlapping count
BlobRef blob; ///< the blob with our data
/// ctor for lookup only
explicit Extent(uint32_t lo) : logical_offset(lo) { }
/// ctor for delayed intitialization (see decode_some())
explicit Extent() {
}
/// ctor for general usage
Extent(uint32_t lo, uint32_t o, uint32_t l, uint8_t bd, BlobRef& b)
: logical_offset(lo), blob_offset(o), length(l), blob_depth(bd) {
assign_blob(b);
}
~Extent() {
if (blob) {
blob->shared_blob->bc.cache->rm_extent();
}
}
void assign_blob(const BlobRef& b) {
assert(!blob);
blob = b;
blob->shared_blob->bc.cache->add_extent();
}
// comparators for intrusive_set
friend bool operator<(const Extent &a, const Extent &b) {
return a.logical_offset < b.logical_offset;
}
friend bool operator>(const Extent &a, const Extent &b) {
return a.logical_offset > b.logical_offset;
}
friend bool operator==(const Extent &a, const Extent &b) {
return a.logical_offset == b.logical_offset;
}
uint32_t blob_end() {
return logical_offset + blob->get_blob().get_logical_length() -
blob_offset;
}
bool blob_escapes_range(uint32_t o, uint32_t l) {
uint32_t bstart = logical_offset - blob_offset;
return (bstart < o ||
bstart + blob->get_blob().get_logical_length() > o + l);
}
};
typedef boost::intrusive::set<Extent> extent_map_t;
friend ostream& operator<<(ostream& out, const Extent& e);
struct Collection;
struct Onode;
/// a sharded extent map, mapping offsets to lextents to blobs
struct ExtentMap {
Onode *onode;
extent_map_t extent_map; ///< map of Extents to Blobs
blob_map_t spanning_blob_map; ///< blobs that span shards
struct Shard {
string key; ///< kv key
uint32_t offset; ///< starting logical offset
bluestore_onode_t::shard_info *shard_info;
bool loaded = false; ///< true if shard is loaded
bool dirty = false; ///< true if shard is dirty and needs reencoding
};
vector<Shard> shards; ///< shards
bool inline_dirty = false;
bufferlist inline_bl; ///< cached encoded map, if unsharded; empty=>dirty
ExtentMap(Onode *o);
~ExtentMap() {
spanning_blob_map.clear_and_dispose([&](Blob *b) { b->put(); });
extent_map.clear_and_dispose([&](Extent *e) { delete e; });
}
bool encode_some(uint32_t offset, uint32_t length, bufferlist& bl,
unsigned *pn);
void decode_some(bufferlist& bl);
void encode_spanning_blobs(bufferlist& bl);
void decode_spanning_blobs(Collection *c, bufferlist::iterator& p);
BlobRef get_spanning_blob(int id);
bool update(Onode *on, KeyValueDB::Transaction t, bool force);
void reshard(Onode *on);
/// initialize Shards from the onode
void init_shards(Onode *on, bool loaded, bool dirty);
/// return index of shard containing offset
/// or -1 if not found
int seek_shard(uint32_t offset) {
size_t end = shards.size();
size_t mid, left = 0;
size_t right = end; // one passed the right end
while (left < right) {
mid = left + (right - left) / 2;
if (offset >= shards[mid].offset) {
size_t next = mid + 1;
if (next >= end || offset < shards[next].offset)
return mid;
//continue to search forwards
left = next;
} else {
//continue to search backwards
right = mid;
}
}
return -1; // not found
}
/// ensure that a range of the map is loaded
void fault_range(KeyValueDB *db,
uint32_t offset, uint32_t length);
/// ensure a range of the map is marked dirty
void dirty_range(KeyValueDB::Transaction t,
uint32_t offset, uint32_t length);
extent_map_t::iterator find(uint64_t offset);
/// find a lextent that includes offset
extent_map_t::iterator find_lextent(uint64_t offset);
/// seek to the first lextent including or after offset
extent_map_t::iterator seek_lextent(uint64_t offset);
/// add a new Extent
void add(uint32_t lo, uint32_t o, uint32_t l, uint8_t bd, BlobRef& b) {
extent_map.insert(*new Extent(lo, o, l, bd, b));
}
/// remove (and delete) an Extent
void rm(extent_map_t::iterator p) {
Extent *e = &*p;
extent_map.erase(p);
delete e;
}
bool has_any_lextents(uint64_t offset, uint64_t length);
/// consolidate adjacent lextents in extent_map
int compress_extent_map(uint64_t offset, uint64_t length);
/// punch a logical hole. add lextents to deref to target list.
void punch_hole(uint64_t offset, uint64_t length,
extent_map_t *old_extents);
/// put new lextent into lextent_map overwriting existing ones if
/// any and update references accordingly
Extent *set_lextent(uint64_t logical_offset,
uint64_t offset, uint64_t length, uint8_t blob_depth,
BlobRef b, extent_map_t *old_extents);
};
struct OnodeSpace;
/// an in-memory object
struct Onode {
std::atomic_int nref; ///< reference count
Collection *c;
ghobject_t oid;
string key; ///< key under PREFIX_OBJ where we are stored
OnodeSpace *space; ///< containing OnodeSpace
boost::intrusive::list_member_hook<> lru_item;
bluestore_onode_t onode; ///< metadata stored as value in kv store
bool exists; ///< true if object logically exists
ExtentMap extent_map;
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, Collection *c, const ghobject_t& o, const string& k)
: nref(0),
c(c),
oid(o),
key(k),
space(s),
exists(false),
extent_map(this) {
}
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 {
PerfCounters *logger;
std::recursive_mutex lock; ///< protect lru and other structures
std::atomic<uint64_t> num_extents = {0};
std::atomic<uint64_t> num_blobs = {0};
static Cache *create(string type, PerfCounters *logger);
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;
void add_extent() {
++num_extents;
}
void rm_extent() {
--num_extents;
}
void add_blob() {
++num_blobs;
}
void rm_blob() {
--num_blobs;
}
virtual void trim(uint64_t onode_max, uint64_t buffer_max) = 0;
virtual void add_stats(uint64_t *onodes, uint64_t *extents,
uint64_t *blobs,
uint64_t *buffers,
uint64_t *bytes) = 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;
void add_stats(uint64_t *onodes, uint64_t *extents,
uint64_t *blobs,
uint64_t *buffers,
uint64_t *bytes) override {
std::lock_guard<std::recursive_mutex> l(lock);
*onodes += onode_lru.size();
*extents += num_extents;
*blobs += num_blobs;
*buffers += buffer_lru.size();
*bytes += buffer_size;
}
#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
BUFFER_TYPE_MAX
};
uint64_t buffer_list_bytes[BUFFER_TYPE_MAX] = {0}; ///< bytes per type
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;
void add_stats(uint64_t *onodes, uint64_t *extents,
uint64_t *blobs,
uint64_t *buffers,
uint64_t *bytes) override {
std::lock_guard<std::recursive_mutex> l(lock);
*onodes += onode_lru.size();
*extents += num_extents;
*blobs += num_blobs;
*buffers += buffer_hot.size() + buffer_warm_in.size();
*bytes += buffer_bytes;
}
#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();
}
OnodeRef 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,
const string& new_okey);
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;
SharedBlobSet shared_blob_set; ///< open SharedBlobs
// cache onodes on a per-collection basis to avoid lock
// contention.
OnodeSpace onode_map;
OnodeRef get_onode(const ghobject_t& oid, bool create);
// the terminology is confusing here, sorry!
//
// blob_t shared_blob_t
// !shared unused -> open
// shared !loaded -> open + shared
// shared loaded -> open + shared + loaded
//
// i.e.,
// open = SharedBlob is instantiated
// shared = blob_t shared flag is set; SharedBlob is hashed.
// loaded = SharedBlob::shared_blob_t is loaded from kv store
void open_shared_blob(BlobRef b);
void load_shared_blob(SharedBlobRef sb);
void make_blob_shared(BlobRef b);
BlobRef new_blob() {
BlobRef b = new Blob;
b->shared_blob = new SharedBlob(0, string(), cache);
return b;
}
const coll_t &get_cid() override {