forked from Ved-vampir/ceph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlueStore.cc
19629 lines (17873 loc) · 577 KB
/
BlueStore.cc
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.
*
*/
#include <bit>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <algorithm>
#include <boost/container/flat_set.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real.hpp>
#include "include/cpp-btree/btree_set.h"
#include "BlueStore.h"
#include "bluestore_common.h"
#include "simple_bitmap.h"
#include "os/kv.h"
#include "include/compat.h"
#include "include/intarith.h"
#include "include/stringify.h"
#include "include/str_map.h"
#include "include/util.h"
#include "common/errno.h"
#include "common/safe_io.h"
#include "common/PriorityCache.h"
#include "common/url_escape.h"
#include "Allocator.h"
#include "FreelistManager.h"
#include "BlueFS.h"
#include "BlueRocksEnv.h"
#include "auth/Crypto.h"
#include "common/EventTrace.h"
#include "perfglue/heap_profiler.h"
#include "common/blkdev.h"
#include "common/numa.h"
#include "common/pretty_binary.h"
#include "kv/KeyValueHistogram.h"
#ifdef HAVE_LIBZBD
#include "ZonedAllocator.h"
#include "ZonedFreelistManager.h"
#endif
#if defined(WITH_LTTNG)
#define TRACEPOINT_DEFINE
#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
#include "tracing/bluestore.h"
#undef TRACEPOINT_PROBE_DYNAMIC_LINKAGE
#undef TRACEPOINT_DEFINE
#else
#define tracepoint(...)
#endif
#define dout_context cct
#define dout_subsys ceph_subsys_bluestore
using bid_t = decltype(BlueStore::Blob::id);
// bluestore_cache_onode
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Onode, bluestore_onode,
bluestore_cache_onode);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Buffer, bluestore_buffer,
bluestore_cache_buffer);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Extent, bluestore_extent,
bluestore_extent);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Blob, bluestore_blob,
bluestore_blob);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::SharedBlob, bluestore_shared_blob,
bluestore_shared_blob);
// bluestore_txc
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::TransContext, bluestore_transcontext,
bluestore_txc);
using std::byte;
using std::deque;
using std::min;
using std::make_pair;
using std::numeric_limits;
using std::pair;
using std::less;
using std::list;
using std::make_unique;
using std::map;
using std::max;
using std::ostream;
using std::ostringstream;
using std::set;
using std::string;
using std::stringstream;
using std::unique_ptr;
using std::vector;
using ceph::bufferlist;
using ceph::bufferptr;
using ceph::coarse_mono_clock;
using ceph::decode;
using ceph::encode;
using ceph::Formatter;
using ceph::JSONFormatter;
using ceph::make_timespan;
using ceph::mono_clock;
using ceph::mono_time;
using ceph::timespan_str;
// kv store prefixes
const string PREFIX_SUPER = "S"; // field -> value
const string PREFIX_STAT = "T"; // field -> value(int64 array)
const string PREFIX_COLL = "C"; // collection name -> cnode_t
const string PREFIX_OBJ = "O"; // object name -> onode_t
const string PREFIX_OMAP = "M"; // u64 + keyname -> value
const string PREFIX_PGMETA_OMAP = "P"; // u64 + keyname -> value(for meta coll)
const string PREFIX_PERPOOL_OMAP = "m"; // s64 + u64 + keyname -> value
const string PREFIX_PERPG_OMAP = "p"; // u64(pool) + u32(hash) + u64(id) + keyname -> value
const string PREFIX_DEFERRED = "L"; // id -> deferred_transaction_t
const string PREFIX_ALLOC = "B"; // u64 offset -> u64 length (freelist)
const string PREFIX_ALLOC_BITMAP = "b";// (see BitmapFreelistManager)
const string PREFIX_SHARED_BLOB = "X"; // u64 SB id -> shared_blob_t
#ifdef HAVE_LIBZBD
const string PREFIX_ZONED_FM_META = "Z"; // (see ZonedFreelistManager)
const string PREFIX_ZONED_FM_INFO = "z"; // (see ZonedFreelistManager)
const string PREFIX_ZONED_CL_INFO = "G"; // (per-zone cleaner metadata)
#endif
const string BLUESTORE_GLOBAL_STATFS_KEY = "bluestore_statfs";
// write a label in the first block. always use this size. note that
// bluefs makes a matching assumption about the location of its
// superblock (always the second block of the device).
#define BDEV_LABEL_BLOCK_SIZE 4096
// reserve: label (4k) + bluefs super (4k), which means we start at 8k.
#define SUPER_RESERVED 8192
#define OBJECT_MAX_SIZE 0xffffffff // 32 bits
/*
* extent map blob encoding
*
* we use the low bits of the blobid field to indicate some common scenarios
* and spanning vs local ids. See ExtentMap::{encode,decode}_some().
*/
#define BLOBID_FLAG_CONTIGUOUS 0x1 // this extent starts at end of previous
#define BLOBID_FLAG_ZEROOFFSET 0x2 // blob_offset is 0
#define BLOBID_FLAG_SAMELENGTH 0x4 // length matches previous extent
#define BLOBID_FLAG_SPANNING 0x8 // has spanning blob id
#define BLOBID_SHIFT_BITS 4
/*
* object name key structure
*
* encoded u8: shard + 2^7 (so that it sorts properly)
* encoded u64: poolid + 2^63 (so that it sorts properly)
* encoded u32: hash (bit reversed)
*
* escaped string: namespace
*
* escaped string: key or object name
* 1 char: '<', '=', or '>'. if =, then object key == object name, and
* we are done. otherwise, we are followed by the object name.
* escaped string: object name (unless '=' above)
*
* encoded u64: snap
* encoded u64: generation
* 'o'
*/
#define ONODE_KEY_SUFFIX 'o'
/*
* extent shard key
*
* object prefix key
* u32
* 'x'
*/
#define EXTENT_SHARD_KEY_SUFFIX 'x'
/*
* string encoding in the key
*
* The key string needs to lexicographically sort the same way that
* ghobject_t does. We do this by escaping anything <= to '#' with #
* plus a 2 digit hex string, and anything >= '~' with ~ plus the two
* hex digits.
*
* We use ! as a terminator for strings; this works because it is < #
* and will get escaped if it is present in the string.
*
* NOTE: There is a bug in this implementation: due to implicit
* character type conversion in comparison it may produce unexpected
* ordering. Unfortunately fixing the bug would mean invalidating the
* keys in existing deployments. Instead we do additional sorting
* where it is needed.
*/
template<typename S>
static void append_escaped(const string &in, S *out)
{
char hexbyte[in.length() * 3 + 1];
char* ptr = &hexbyte[0];
for (string::const_iterator i = in.begin(); i != in.end(); ++i) {
if (*i <= '#') { // bug: unexpected result for *i > 0x7f
*ptr++ = '#';
*ptr++ = "0123456789abcdef"[(*i >> 4) & 0x0f];
*ptr++ = "0123456789abcdef"[*i & 0x0f];
} else if (*i >= '~') { // bug: unexpected result for *i > 0x7f
*ptr++ = '~';
*ptr++ = "0123456789abcdef"[(*i >> 4) & 0x0f];
*ptr++ = "0123456789abcdef"[*i & 0x0f];
} else {
*ptr++ = *i;
}
}
*ptr++ = '!';
out->append(hexbyte, ptr - &hexbyte[0]);
}
inline unsigned h2i(char c)
{
if ((c >= '0') && (c <= '9')) {
return c - 0x30;
} else if ((c >= 'a') && (c <= 'f')) {
return c - 'a' + 10;
} else if ((c >= 'A') && (c <= 'F')) {
return c - 'A' + 10;
} else {
return 256; // make it always larger than 255
}
}
static int decode_escaped(const char *p, string *out)
{
char buff[256];
char* ptr = &buff[0];
char* max = &buff[252];
const char *orig_p = p;
while (*p && *p != '!') {
if (*p == '#' || *p == '~') {
unsigned hex = 0;
p++;
hex = h2i(*p++) << 4;
if (hex > 255) {
return -EINVAL;
}
hex |= h2i(*p++);
if (hex > 255) {
return -EINVAL;
}
*ptr++ = hex;
} else {
*ptr++ = *p++;
}
if (ptr > max) {
out->append(buff, ptr-buff);
ptr = &buff[0];
}
}
if (ptr != buff) {
out->append(buff, ptr-buff);
}
return p - orig_p;
}
template<typename T>
static void _key_encode_shard(shard_id_t shard, T *key)
{
key->push_back((char)((uint8_t)shard.id + (uint8_t)0x80));
}
static const char *_key_decode_shard(const char *key, shard_id_t *pshard)
{
pshard->id = (uint8_t)*key - (uint8_t)0x80;
return key + 1;
}
static void get_coll_range(const coll_t& cid, int bits,
ghobject_t *temp_start, ghobject_t *temp_end,
ghobject_t *start, ghobject_t *end, bool legacy)
{
spg_t pgid;
constexpr uint32_t MAX_HASH = std::numeric_limits<uint32_t>::max();
// use different nspaces due to we use different schemes when encoding
// keys for listing objects
const std::string_view MAX_NSPACE = legacy ? "\x7f" : "\xff";
if (cid.is_pg(&pgid)) {
start->shard_id = pgid.shard;
*temp_start = *start;
start->hobj.pool = pgid.pool();
temp_start->hobj.pool = -2ll - pgid.pool();
*end = *start;
*temp_end = *temp_start;
uint32_t reverse_hash = hobject_t::_reverse_bits(pgid.ps());
start->hobj.set_bitwise_key_u32(reverse_hash);
temp_start->hobj.set_bitwise_key_u32(reverse_hash);
uint64_t end_hash = reverse_hash + (1ull << (32 - bits));
if (end_hash > MAX_HASH) {
// make sure end hobj is even greater than the maximum possible hobj
end->hobj.set_bitwise_key_u32(MAX_HASH);
temp_end->hobj.set_bitwise_key_u32(MAX_HASH);
end->hobj.nspace = MAX_NSPACE;
} else {
end->hobj.set_bitwise_key_u32(end_hash);
temp_end->hobj.set_bitwise_key_u32(end_hash);
}
} else {
start->shard_id = shard_id_t::NO_SHARD;
start->hobj.pool = -1ull;
*end = *start;
start->hobj.set_bitwise_key_u32(0);
end->hobj.set_bitwise_key_u32(MAX_HASH);
end->hobj.nspace = MAX_NSPACE;
// no separate temp section
*temp_start = *end;
*temp_end = *end;
}
start->generation = 0;
end->generation = 0;
temp_start->generation = 0;
temp_end->generation = 0;
}
static void get_shared_blob_key(uint64_t sbid, string *key)
{
key->clear();
_key_encode_u64(sbid, key);
}
static int get_key_shared_blob(const string& key, uint64_t *sbid)
{
const char *p = key.c_str();
if (key.length() < sizeof(uint64_t))
return -1;
_key_decode_u64(p, sbid);
return 0;
}
template<typename S>
static void _key_encode_prefix(const ghobject_t& oid, S *key)
{
_key_encode_shard(oid.shard_id, key);
_key_encode_u64(oid.hobj.pool + 0x8000000000000000ull, key);
_key_encode_u32(oid.hobj.get_bitwise_key_u32(), key);
}
static const char *_key_decode_prefix(const char *p, ghobject_t *oid)
{
p = _key_decode_shard(p, &oid->shard_id);
uint64_t pool;
p = _key_decode_u64(p, &pool);
oid->hobj.pool = pool - 0x8000000000000000ull;
unsigned hash;
p = _key_decode_u32(p, &hash);
oid->hobj.set_bitwise_key_u32(hash);
return p;
}
#define ENCODED_KEY_PREFIX_LEN (1 + 8 + 4)
static int _get_key_object(const char *p, ghobject_t *oid)
{
int r;
p = _key_decode_prefix(p, oid);
r = decode_escaped(p, &oid->hobj.nspace);
if (r < 0)
return -2;
p += r + 1;
string k;
r = decode_escaped(p, &k);
if (r < 0)
return -3;
p += r + 1;
if (*p == '=') {
// no key
++p;
oid->hobj.oid.name = k;
} else if (*p == '<' || *p == '>') {
// key + name
++p;
r = decode_escaped(p, &oid->hobj.oid.name);
if (r < 0)
return -5;
p += r + 1;
oid->hobj.set_key(k);
} else {
// malformed
return -6;
}
p = _key_decode_u64(p, &oid->hobj.snap.val);
p = _key_decode_u64(p, &oid->generation);
if (*p != ONODE_KEY_SUFFIX) {
return -7;
}
p++;
if (*p) {
// if we get something other than a null terminator here,
// something goes wrong.
return -8;
}
return 0;
}
template<typename S>
static int get_key_object(const S& key, ghobject_t *oid)
{
if (key.length() < ENCODED_KEY_PREFIX_LEN)
return -1;
if (key.length() == ENCODED_KEY_PREFIX_LEN)
return -2;
const char *p = key.c_str();
return _get_key_object(p, oid);
}
template<typename S>
static void _get_object_key(const ghobject_t& oid, S *key)
{
size_t max_len = ENCODED_KEY_PREFIX_LEN +
(oid.hobj.nspace.length() * 3 + 1) +
(oid.hobj.get_key().length() * 3 + 1) +
1 + // for '<', '=', or '>'
(oid.hobj.oid.name.length() * 3 + 1) +
8 + 8 + 1;
key->reserve(max_len);
_key_encode_prefix(oid, key);
append_escaped(oid.hobj.nspace, key);
if (oid.hobj.get_key().length()) {
// is a key... could be < = or >.
append_escaped(oid.hobj.get_key(), key);
// (ASCII chars < = and > sort in that order, yay)
int r = oid.hobj.get_key().compare(oid.hobj.oid.name);
if (r) {
key->append(r > 0 ? ">" : "<");
append_escaped(oid.hobj.oid.name, key);
} else {
// same as no key
key->append("=");
}
} else {
// no key
append_escaped(oid.hobj.oid.name, key);
key->append("=");
}
_key_encode_u64(oid.hobj.snap, key);
_key_encode_u64(oid.generation, key);
key->push_back(ONODE_KEY_SUFFIX);
}
template<typename S>
static void get_object_key(CephContext *cct, const ghobject_t& oid, S *key)
{
key->clear();
_get_object_key(oid, key);
// sanity check
if (true) {
ghobject_t t;
int r = get_key_object(*key, &t);
if (r || t != oid) {
derr << " r " << r << dendl;
derr << "key " << pretty_binary_string(*key) << dendl;
derr << "oid " << oid << dendl;
derr << " t " << t << dendl;
ceph_assert(r == 0 && t == oid);
}
}
}
// extent shard keys are the onode key, plus a u32, plus 'x'. the trailing
// char lets us quickly test whether it is a shard key without decoding any
// of the prefix bytes.
template<typename S>
static void get_extent_shard_key(const S& onode_key, uint32_t offset,
string *key)
{
key->clear();
key->reserve(onode_key.length() + 4 + 1);
key->append(onode_key.c_str(), onode_key.size());
_key_encode_u32(offset, key);
key->push_back(EXTENT_SHARD_KEY_SUFFIX);
}
static void rewrite_extent_shard_key(uint32_t offset, string *key)
{
ceph_assert(key->size() > sizeof(uint32_t) + 1);
ceph_assert(*key->rbegin() == EXTENT_SHARD_KEY_SUFFIX);
_key_encode_u32(offset, key->size() - sizeof(uint32_t) - 1, key);
}
template<typename S>
static void generate_extent_shard_key_and_apply(
const S& onode_key,
uint32_t offset,
string *key,
std::function<void(const string& final_key)> apply)
{
if (key->empty()) { // make full key
ceph_assert(!onode_key.empty());
get_extent_shard_key(onode_key, offset, key);
} else {
rewrite_extent_shard_key(offset, key);
}
apply(*key);
}
int get_key_extent_shard(const string& key, string *onode_key, uint32_t *offset)
{
ceph_assert(key.size() > sizeof(uint32_t) + 1);
ceph_assert(*key.rbegin() == EXTENT_SHARD_KEY_SUFFIX);
int okey_len = key.size() - sizeof(uint32_t) - 1;
*onode_key = key.substr(0, okey_len);
const char *p = key.data() + okey_len;
_key_decode_u32(p, offset);
return 0;
}
static bool is_extent_shard_key(const string& key)
{
return *key.rbegin() == EXTENT_SHARD_KEY_SUFFIX;
}
static void get_deferred_key(uint64_t seq, string *out)
{
_key_encode_u64(seq, out);
}
static void get_pool_stat_key(int64_t pool_id, string *key)
{
key->clear();
_key_encode_u64(pool_id, key);
}
static int get_key_pool_stat(const string& key, uint64_t* pool_id)
{
const char *p = key.c_str();
if (key.length() < sizeof(uint64_t))
return -1;
_key_decode_u64(p, pool_id);
return 0;
}
#ifdef HAVE_LIBZBD
static void get_zone_offset_object_key(
uint32_t zone,
uint64_t offset,
ghobject_t oid,
std::string *key)
{
key->clear();
_key_encode_u32(zone, key);
_key_encode_u64(offset, key);
_get_object_key(oid, key);
}
static int get_key_zone_offset_object(
const string& key,
uint32_t *zone,
uint64_t *offset,
ghobject_t *oid)
{
const char *p = key.c_str();
if (key.length() < sizeof(uint64_t) + sizeof(uint32_t) + ENCODED_KEY_PREFIX_LEN + 1)
return -1;
p = _key_decode_u32(p, zone);
p = _key_decode_u64(p, offset);
int r = _get_key_object(p, oid);
if (r < 0) {
return r;
}
return 0;
}
#endif
template <int LogLevelV>
void _dump_extent_map(CephContext *cct, const BlueStore::ExtentMap &em)
{
uint64_t pos = 0;
for (auto& s : em.shards) {
dout(LogLevelV) << __func__ << " shard " << *s.shard_info
<< (s.loaded ? " (loaded)" : "")
<< (s.dirty ? " (dirty)" : "")
<< dendl;
}
for (auto& e : em.extent_map) {
dout(LogLevelV) << __func__ << " " << e << dendl;
ceph_assert(e.logical_offset >= pos);
pos = e.logical_offset + e.length;
const bluestore_blob_t& blob = e.blob->get_blob();
if (blob.has_csum()) {
vector<uint64_t> v;
unsigned n = blob.get_csum_count();
for (unsigned i = 0; i < n; ++i)
v.push_back(blob.get_csum_item(i));
dout(LogLevelV) << __func__ << " csum: " << std::hex << v << std::dec
<< dendl;
}
std::lock_guard l(e.blob->shared_blob->get_cache()->lock);
for (auto& i : e.blob->shared_blob->bc.buffer_map) {
dout(LogLevelV) << __func__ << " 0x" << std::hex << i.first
<< "~" << i.second->length << std::dec
<< " " << *i.second << dendl;
}
}
}
template <int LogLevelV>
void _dump_onode(CephContext *cct, const BlueStore::Onode& o)
{
if (!cct->_conf->subsys.should_gather<ceph_subsys_bluestore, LogLevelV>())
return;
dout(LogLevelV) << __func__ << " " << &o << " " << o.oid
<< " nid " << o.onode.nid
<< " size 0x" << std::hex << o.onode.size
<< " (" << std::dec << o.onode.size << ")"
<< " expected_object_size " << o.onode.expected_object_size
<< " expected_write_size " << o.onode.expected_write_size
<< " in " << o.onode.extent_map_shards.size() << " shards"
<< ", " << o.extent_map.spanning_blob_map.size()
<< " spanning blobs"
<< dendl;
for (auto& [zone, offset] : o.onode.zone_offset_refs) {
dout(LogLevelV) << __func__ << " zone ref 0x" << std::hex << zone
<< " offset 0x" << offset << std::dec << dendl;
}
for (auto p = o.onode.attrs.begin();
p != o.onode.attrs.end();
++p) {
dout(LogLevelV) << __func__ << " attr " << p->first
<< " len " << p->second.length() << dendl;
}
_dump_extent_map<LogLevelV>(cct, o.extent_map);
}
template <int LogLevelV>
void _dump_transaction(CephContext *cct, ObjectStore::Transaction *t)
{
dout(LogLevelV) << __func__ << " transaction dump:\n";
JSONFormatter f(true);
f.open_object_section("transaction");
t->dump(&f);
f.close_section();
f.flush(*_dout);
*_dout << dendl;
}
// Buffer
ostream& operator<<(ostream& out, const BlueStore::Buffer& b)
{
out << "buffer(" << &b << " space " << b.space << " 0x" << std::hex
<< b.offset << "~" << b.length << std::dec
<< " " << BlueStore::Buffer::get_state_name(b.state);
if (b.flags)
out << " " << BlueStore::Buffer::get_flag_name(b.flags);
return out << ")";
}
namespace {
/*
* Due to a bug in key string encoding (see a comment for append_escaped)
* the KeyValueDB iterator does not lexicographically sort the same
* way that ghobject_t does: objects with the same hash may have wrong order.
*
* This is the iterator wrapper that fixes the keys order.
*/
class CollectionListIterator {
public:
CollectionListIterator(const KeyValueDB::Iterator &it)
: m_it(it) {
}
virtual ~CollectionListIterator() {
}
virtual bool valid() const = 0;
virtual const ghobject_t &oid() const = 0;
virtual void lower_bound(const ghobject_t &oid) = 0;
virtual void upper_bound(const ghobject_t &oid) = 0;
virtual void next() = 0;
virtual int cmp(const ghobject_t &oid) const = 0;
bool is_ge(const ghobject_t &oid) const {
return cmp(oid) >= 0;
}
bool is_lt(const ghobject_t &oid) const {
return cmp(oid) < 0;
}
protected:
KeyValueDB::Iterator m_it;
};
class SimpleCollectionListIterator : public CollectionListIterator {
public:
SimpleCollectionListIterator(CephContext *cct, const KeyValueDB::Iterator &it)
: CollectionListIterator(it), m_cct(cct) {
}
bool valid() const override {
return m_it->valid();
}
const ghobject_t &oid() const override {
ceph_assert(valid());
return m_oid;
}
void lower_bound(const ghobject_t &oid) override {
string key;
get_object_key(m_cct, oid, &key);
m_it->lower_bound(key);
get_oid();
}
void upper_bound(const ghobject_t &oid) override {
string key;
get_object_key(m_cct, oid, &key);
m_it->upper_bound(key);
get_oid();
}
void next() override {
ceph_assert(valid());
m_it->next();
get_oid();
}
int cmp(const ghobject_t &oid) const override {
ceph_assert(valid());
string key;
get_object_key(m_cct, oid, &key);
return m_it->key().compare(key);
}
private:
CephContext *m_cct;
ghobject_t m_oid;
void get_oid() {
m_oid = ghobject_t();
while (m_it->valid() && is_extent_shard_key(m_it->key())) {
m_it->next();
}
if (!valid()) {
return;
}
int r = get_key_object(m_it->key(), &m_oid);
ceph_assert(r == 0);
}
};
class SortedCollectionListIterator : public CollectionListIterator {
public:
SortedCollectionListIterator(const KeyValueDB::Iterator &it)
: CollectionListIterator(it), m_chunk_iter(m_chunk.end()) {
}
bool valid() const override {
return m_chunk_iter != m_chunk.end();
}
const ghobject_t &oid() const override {
ceph_assert(valid());
return m_chunk_iter->first;
}
void lower_bound(const ghobject_t &oid) override {
std::string key;
_key_encode_prefix(oid, &key);
m_it->lower_bound(key);
m_chunk_iter = m_chunk.end();
if (!get_next_chunk()) {
return;
}
if (this->oid().shard_id != oid.shard_id ||
this->oid().hobj.pool != oid.hobj.pool ||
this->oid().hobj.get_bitwise_key_u32() != oid.hobj.get_bitwise_key_u32()) {
return;
}
m_chunk_iter = m_chunk.lower_bound(oid);
if (m_chunk_iter == m_chunk.end()) {
get_next_chunk();
}
}
void upper_bound(const ghobject_t &oid) override {
lower_bound(oid);
if (valid() && this->oid() == oid) {
next();
}
}
void next() override {
ceph_assert(valid());
m_chunk_iter++;
if (m_chunk_iter == m_chunk.end()) {
get_next_chunk();
}
}
int cmp(const ghobject_t &oid) const override {
ceph_assert(valid());
if (this->oid() < oid) {
return -1;
}
if (this->oid() > oid) {
return 1;
}
return 0;
}
private:
std::map<ghobject_t, std::string> m_chunk;
std::map<ghobject_t, std::string>::iterator m_chunk_iter;
bool get_next_chunk() {
while (m_it->valid() && is_extent_shard_key(m_it->key())) {
m_it->next();
}
if (!m_it->valid()) {
return false;
}
ghobject_t oid;
int r = get_key_object(m_it->key(), &oid);
ceph_assert(r == 0);
m_chunk.clear();
while (true) {
m_chunk.insert({oid, m_it->key()});
do {
m_it->next();
} while (m_it->valid() && is_extent_shard_key(m_it->key()));
if (!m_it->valid()) {
break;
}
ghobject_t next;
r = get_key_object(m_it->key(), &next);
ceph_assert(r == 0);
if (next.shard_id != oid.shard_id ||
next.hobj.pool != oid.hobj.pool ||
next.hobj.get_bitwise_key_u32() != oid.hobj.get_bitwise_key_u32()) {
break;
}
oid = next;
}
m_chunk_iter = m_chunk.begin();
return true;
}
};
} // anonymous namespace
// Garbage Collector
void BlueStore::GarbageCollector::process_protrusive_extents(
const BlueStore::ExtentMap& extent_map,
uint64_t start_offset,
uint64_t end_offset,
uint64_t start_touch_offset,
uint64_t end_touch_offset,
uint64_t min_alloc_size)
{
ceph_assert(start_offset <= start_touch_offset && end_offset>= end_touch_offset);
uint64_t lookup_start_offset = p2align(start_offset, min_alloc_size);
uint64_t lookup_end_offset = round_up_to(end_offset, min_alloc_size);
dout(30) << __func__ << " (hex): [" << std::hex
<< lookup_start_offset << ", " << lookup_end_offset
<< ")" << std::dec << dendl;
for (auto it = extent_map.seek_lextent(lookup_start_offset);
it != extent_map.extent_map.end() &&
it->logical_offset < lookup_end_offset;
++it) {
uint64_t alloc_unit_start = it->logical_offset / min_alloc_size;
uint64_t alloc_unit_end = (it->logical_end() - 1) / min_alloc_size;
dout(30) << __func__ << " " << *it
<< "alloc_units: " << alloc_unit_start << ".." << alloc_unit_end
<< dendl;
Blob* b = it->blob.get();
if (it->logical_offset >=start_touch_offset &&
it->logical_end() <= end_touch_offset) {
// Process extents within the range affected by
// the current write request.
// Need to take into account if existing extents
// can be merged with them (uncompressed case)
if (!b->get_blob().is_compressed()) {
if (blob_info_counted && used_alloc_unit == alloc_unit_start) {
--blob_info_counted->expected_allocations; // don't need to allocate
// new AU for compressed
// data since another
// collocated uncompressed
// blob already exists
dout(30) << __func__ << " --expected:"
<< alloc_unit_start << dendl;
}
used_alloc_unit = alloc_unit_end;
blob_info_counted = nullptr;
}
} else if (b->get_blob().is_compressed()) {
// additionally we take compressed blobs that were not impacted
// by the write into account too
BlobInfo& bi =
affected_blobs.emplace(
b, BlobInfo(b->get_referenced_bytes())).first->second;
int adjust =
(used_alloc_unit && used_alloc_unit == alloc_unit_start) ? 0 : 1;
bi.expected_allocations += alloc_unit_end - alloc_unit_start + adjust;
dout(30) << __func__ << " expected_allocations="
<< bi.expected_allocations << " end_au:"
<< alloc_unit_end << dendl;
blob_info_counted = &bi;
used_alloc_unit = alloc_unit_end;
ceph_assert(it->length <= bi.referenced_bytes);
bi.referenced_bytes -= it->length;
dout(30) << __func__ << " affected_blob:" << *b
<< " unref 0x" << std::hex << it->length
<< " referenced = 0x" << bi.referenced_bytes
<< std::dec << dendl;
// NOTE: we can't move specific blob to resulting GC list here
// when reference counter == 0 since subsequent extents might
// decrement its expected_allocation.
// Hence need to enumerate all the extents first.
if (!bi.collect_candidate) {
bi.first_lextent = it;
bi.collect_candidate = true;
}
bi.last_lextent = it;
} else {
if (blob_info_counted && used_alloc_unit == alloc_unit_start) {
// don't need to allocate new AU for compressed data since another
// collocated uncompressed blob already exists