-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathpebble.go
1361 lines (1200 loc) · 39.7 KB
/
pebble.go
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
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package storage
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/storage/fs"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/logtags"
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/bloom"
"github.com/cockroachdb/pebble/vfs"
)
// MVCCKeyCompare compares cockroach keys, including the MVCC timestamps.
func MVCCKeyCompare(a, b []byte) int {
// NB: For performance, this routine manually splits the key into the
// user-key and timestamp components rather than using SplitMVCCKey. Don't
// try this at home kids: use SplitMVCCKey.
aEnd := len(a) - 1
bEnd := len(b) - 1
if aEnd < 0 || bEnd < 0 {
// This should never happen unless there is some sort of corruption of
// the keys. This is a little bizarre, but the behavior exactly matches
// engine/db.cc:DBComparator.
return bytes.Compare(a, b)
}
// Compute the index of the separator between the key and the timestamp.
aSep := aEnd - int(a[aEnd])
bSep := bEnd - int(b[bEnd])
if aSep < 0 || bSep < 0 {
// This should never happen unless there is some sort of corruption of
// the keys. This is a little bizarre, but the behavior exactly matches
// engine/db.cc:DBComparator.
return bytes.Compare(a, b)
}
// Compare the "user key" part of the key.
if c := bytes.Compare(a[:aSep], b[:bSep]); c != 0 {
return c
}
// Compare the timestamp part of the key.
aTS := a[aSep:aEnd]
bTS := b[bSep:bEnd]
if len(aTS) == 0 {
if len(bTS) == 0 {
return 0
}
return -1
} else if len(bTS) == 0 {
return 1
}
return bytes.Compare(bTS, aTS)
}
// MVCCComparer is a pebble.Comparer object that implements MVCC-specific
// comparator settings for use with Pebble.
var MVCCComparer = &pebble.Comparer{
Compare: MVCCKeyCompare,
AbbreviatedKey: func(k []byte) uint64 {
key, _, ok := enginepb.SplitMVCCKey(k)
if !ok {
return 0
}
return pebble.DefaultComparer.AbbreviatedKey(key)
},
FormatKey: func(k []byte) fmt.Formatter {
decoded, err := DecodeMVCCKey(k)
if err != nil {
return mvccKeyFormatter{err: err}
}
return mvccKeyFormatter{key: decoded}
},
Separator: func(dst, a, b []byte) []byte {
aKey, _, ok := enginepb.SplitMVCCKey(a)
if !ok {
return append(dst, a...)
}
bKey, _, ok := enginepb.SplitMVCCKey(b)
if !ok {
return append(dst, a...)
}
// If the keys are the same just return a.
if bytes.Equal(aKey, bKey) {
return append(dst, a...)
}
n := len(dst)
// MVCC key comparison uses bytes.Compare on the roachpb.Key, which is the same semantics as
// pebble.DefaultComparer, so reuse the latter's Separator implementation.
dst = pebble.DefaultComparer.Separator(dst, aKey, bKey)
// Did it pick a separator different than aKey -- if it did not we can't do better than a.
buf := dst[n:]
if bytes.Equal(aKey, buf) {
return append(dst[:n], a...)
}
// The separator is > aKey, so we only need to add the timestamp sentinel.
return append(dst, 0)
},
Successor: func(dst, a []byte) []byte {
aKey, _, ok := enginepb.SplitMVCCKey(a)
if !ok {
return append(dst, a...)
}
n := len(dst)
// MVCC key comparison uses bytes.Compare on the roachpb.Key, which is the same semantics as
// pebble.DefaultComparer, so reuse the latter's Successor implementation.
dst = pebble.DefaultComparer.Successor(dst, aKey)
// Did it pick a successor different than aKey -- if it did not we can't do better than a.
buf := dst[n:]
if bytes.Equal(aKey, buf) {
return append(dst[:n], a...)
}
// The successor is > aKey, so we only need to add the timestamp sentinel.
return append(dst, 0)
},
Split: func(k []byte) int {
key, _, ok := enginepb.SplitMVCCKey(k)
if !ok {
return len(k)
}
// This matches the behavior of libroach/KeyPrefix. RocksDB requires that
// keys generated via a SliceTransform be comparable with normal encoded
// MVCC keys. Encoded MVCC keys have a suffix indicating the number of
// bytes of timestamp data. MVCC keys without a timestamp have a suffix of
// 0. We're careful in EncodeKey to make sure that the user-key always has
// a trailing 0. If there is no timestamp this falls out naturally. If
// there is a timestamp we prepend a 0 to the encoded timestamp data.
return len(key) + 1
},
Name: "cockroach_comparator",
}
// MVCCMerger is a pebble.Merger object that implements the merge operator used
// by Cockroach.
var MVCCMerger = &pebble.Merger{
Name: "cockroach_merge_operator",
Merge: func(_, value []byte) (pebble.ValueMerger, error) {
res := &MVCCValueMerger{}
err := res.MergeNewer(value)
if err != nil {
return nil, err
}
return res, nil
},
}
// pebbleTimeBoundPropCollector implements a property collector for MVCC
// Timestamps. Its behavior matches TimeBoundTblPropCollector in
// table_props.cc.
//
// The handling of timestamps in intents is mildly complicated. Consider:
//
// a@<meta> -> <MVCCMetadata: Timestamp=t2>
// a@t2 -> <value>
// a@t1 -> <value>
//
// The metadata record (a.k.a. the intent) for a key always sorts first. The
// timestamp field always points to the next record. In this case, the meta
// record contains t2 and the next record is t2. Because of this duplication of
// the timestamp both in the intent and in the timestamped record that
// immediately follows it, we only need to unmarshal the MVCCMetadata if it is
// the last key in the sstable.
type pebbleTimeBoundPropCollector struct {
min, max []byte
lastValue []byte
}
func (t *pebbleTimeBoundPropCollector) Add(key pebble.InternalKey, value []byte) error {
_, ts, ok := enginepb.SplitMVCCKey(key.UserKey)
if !ok {
return errors.Errorf("failed to split MVCC key")
}
if len(ts) > 0 {
t.lastValue = t.lastValue[:0]
t.updateBounds(ts)
} else {
t.lastValue = append(t.lastValue[:0], value...)
}
return nil
}
func (t *pebbleTimeBoundPropCollector) Finish(userProps map[string]string) error {
if len(t.lastValue) > 0 {
// The last record in the sstable was an intent. Unmarshal the metadata and
// update the bounds with the timestamp it contains.
meta := &enginepb.MVCCMetadata{}
if err := protoutil.Unmarshal(t.lastValue, meta); err != nil {
// We're unable to parse the MVCCMetadata. Fail open by not setting the
// min/max timestamp properties. This mimics the behavior of
// TimeBoundTblPropCollector.
// TODO(petermattis): Return the error here and in C++, see #43422.
return nil //nolint:returnerrcheck
}
if meta.Txn != nil {
ts := encodeTimestamp(hlc.Timestamp(meta.Timestamp))
t.updateBounds(ts)
}
}
userProps["crdb.ts.min"] = string(t.min)
userProps["crdb.ts.max"] = string(t.max)
return nil
}
func (t *pebbleTimeBoundPropCollector) updateBounds(ts []byte) {
if len(t.min) == 0 || bytes.Compare(ts, t.min) < 0 {
t.min = append(t.min[:0], ts...)
}
if len(t.max) == 0 || bytes.Compare(ts, t.max) > 0 {
t.max = append(t.max[:0], ts...)
}
}
func (t *pebbleTimeBoundPropCollector) Name() string {
// This constant needs to match the one used by the RocksDB version of this
// table property collector. DO NOT CHANGE.
return "TimeBoundTblPropCollectorFactory"
}
var _ pebble.NeedCompacter = &pebbleDeleteRangeCollector{}
// pebbleDeleteRangeCollector marks an sstable for compaction that contains a
// range tombstone.
type pebbleDeleteRangeCollector struct {
numRangeTombstones int
}
func (c *pebbleDeleteRangeCollector) Add(key pebble.InternalKey, value []byte) error {
if key.Kind() == pebble.InternalKeyKindRangeDelete {
c.numRangeTombstones++
}
return nil
}
// NeedCompact implements the pebble.NeedCompacter interface.
func (c *pebbleDeleteRangeCollector) NeedCompact() bool {
// NB: Mark any file containing range deletions as requiring a
// compaction. This ensures that range deletions are quickly compacted out
// of existence.
return c.numRangeTombstones > 0
}
func (*pebbleDeleteRangeCollector) Finish(userProps map[string]string) error {
return nil
}
func (*pebbleDeleteRangeCollector) Name() string {
// This constant needs to match the one used by the RocksDB version of this
// table property collector. DO NOT CHANGE.
return "DeleteRangeTblPropCollectorFactory"
}
// PebbleTablePropertyCollectors is the list of Pebble TablePropertyCollectors.
var PebbleTablePropertyCollectors = []func() pebble.TablePropertyCollector{
func() pebble.TablePropertyCollector { return &pebbleTimeBoundPropCollector{} },
func() pebble.TablePropertyCollector { return &pebbleDeleteRangeCollector{} },
}
// DefaultPebbleOptions returns the default pebble options.
func DefaultPebbleOptions() *pebble.Options {
// In RocksDB, the concurrency setting corresponds to both flushes and
// compactions. In Pebble, there is always a slot for a flush, and
// compactions are counted separately.
maxConcurrentCompactions := rocksdbConcurrency - 1
if maxConcurrentCompactions < 1 {
maxConcurrentCompactions = 1
}
opts := &pebble.Options{
Comparer: MVCCComparer,
L0CompactionThreshold: 2,
L0StopWritesThreshold: 1000,
LBaseMaxBytes: 64 << 20, // 64 MB
Levels: make([]pebble.LevelOptions, 7),
MaxConcurrentCompactions: maxConcurrentCompactions,
MemTableSize: 64 << 20, // 64 MB
MemTableStopWritesThreshold: 4,
Merger: MVCCMerger,
MinFlushRate: 4 << 20, // 4 MB/sec
TablePropertyCollectors: PebbleTablePropertyCollectors,
}
for i := 0; i < len(opts.Levels); i++ {
l := &opts.Levels[i]
l.BlockSize = 32 << 10 // 32 KB
l.IndexBlockSize = 256 << 10 // 256 KB
l.FilterPolicy = bloom.FilterPolicy(10)
l.FilterType = pebble.TableFilter
if i > 0 {
l.TargetFileSize = opts.Levels[i-1].TargetFileSize * 2
}
l.EnsureDefaults()
}
// Do not create bloom filters for the last level (i.e. the largest level
// which contains data in the LSM store). This configuration reduces the size
// of the bloom filters by 10x. This is significant given that bloom filters
// require 1.25 bytes (10 bits) per key which can translate into gigabytes of
// memory given typical key and value sizes. The downside is that bloom
// filters will only be usable on the higher levels, but that seems
// acceptable. We typically see read amplification of 5-6x on clusters
// (i.e. there are 5-6 levels of sstables) which means we'll achieve 80-90%
// of the benefit of having bloom filters on every level for only 10% of the
// memory cost.
opts.Levels[6].FilterPolicy = nil
return opts
}
var pebbleLog *log.SecondaryLogger
// InitPebbleLogger initializes the logger to use for Pebble log messages. If
// not called, WARNING, ERROR, and FATAL logs will be output to the normal
// CockroachDB log. The caller is responsible for ensuring the
// Close() method is eventually called on the new logger.
func InitPebbleLogger(ctx context.Context) *log.SecondaryLogger {
pebbleLog = log.NewSecondaryLogger(ctx, nil, "pebble",
true /* enableGC */, false /* forceSyncWrites */, false /* enableMsgCount */)
return pebbleLog
}
type pebbleLogger struct {
ctx context.Context
depth int
}
func (l pebbleLogger) Infof(format string, args ...interface{}) {
if pebbleLog != nil {
pebbleLog.LogfDepth(l.ctx, l.depth, format, args...)
return
}
// Only log INFO logs to the normal CockroachDB log at --v=3 and above.
if log.V(3) {
log.InfofDepth(l.ctx, l.depth, format, args...)
}
}
func (l pebbleLogger) Fatalf(format string, args ...interface{}) {
log.FatalfDepth(l.ctx, l.depth, format, args...)
}
// PebbleConfig holds all configuration parameters and knobs used in setting up
// a new Pebble instance.
type PebbleConfig struct {
// StorageConfig contains storage configs for all storage engines.
base.StorageConfig
// Pebble specific options.
Opts *pebble.Options
}
// EncryptionStatsHandler provides encryption related stats.
type EncryptionStatsHandler interface {
// Returns a serialized enginepbccl.EncryptionStatus.
GetEncryptionStatus() ([]byte, error)
// Returns a serialized enginepbccl.DataKeysRegistry, scrubbed of key contents.
GetDataKeysRegistry() ([]byte, error)
// Returns the ID of the active data key, or "plain" if none.
GetActiveDataKeyID() (string, error)
// Returns the enum value of the encryption type.
GetActiveStoreKeyType() int32
// Returns the KeyID embedded in the serialized EncryptionSettings.
GetKeyIDFromSettings(settings []byte) (string, error)
}
// Pebble is a wrapper around a Pebble database instance.
type Pebble struct {
db *pebble.DB
closed bool
path string
auxDir string
maxSize int64
attrs roachpb.Attributes
settings *cluster.Settings
statsHandler EncryptionStatsHandler
fileRegistry *PebbleFileRegistry
// Relevant options copied over from pebble.Options.
fs vfs.FS
logger pebble.Logger
}
var _ Engine = &Pebble{}
// NewEncryptedEnvFunc creates an encrypted environment and returns the vfs.FS to use for reading
// and writing data. This should be initialized by calling engineccl.Init() before calling
// NewPebble(). The optionBytes is a binary serialized baseccl.EncryptionOptions, so that non-CCL
// code does not depend on CCL code.
var NewEncryptedEnvFunc func(fs vfs.FS, fr *PebbleFileRegistry, dbDir string, readOnly bool, optionBytes []byte) (vfs.FS, EncryptionStatsHandler, error)
// ResolveEncryptedEnvOptions fills in cfg.Opts.FS with an encrypted vfs if this
// store has encryption-at-rest enabled. Also returns the associated file
// registry and EncryptionStatsHandler.
func ResolveEncryptedEnvOptions(
cfg *PebbleConfig,
) (*PebbleFileRegistry, EncryptionStatsHandler, error) {
fileRegistry := &PebbleFileRegistry{FS: cfg.Opts.FS, DBDir: cfg.Dir, ReadOnly: cfg.Opts.ReadOnly}
if cfg.UseFileRegistry {
if err := fileRegistry.Load(); err != nil {
return nil, nil, err
}
} else {
if err := fileRegistry.checkNoRegistryFile(); err != nil {
return nil, nil, fmt.Errorf("encryption was used on this store before, but no encryption flags " +
"specified. You need a CCL build and must fully specify the --enterprise-encryption flag")
}
fileRegistry = nil
}
var statsHandler EncryptionStatsHandler
if len(cfg.ExtraOptions) > 0 {
// Encryption is enabled.
if !cfg.UseFileRegistry {
return nil, nil, fmt.Errorf("file registry is needed to support encryption")
}
if NewEncryptedEnvFunc == nil {
return nil, nil, fmt.Errorf("encryption is enabled but no function to create the encrypted env")
}
var err error
cfg.Opts.FS, statsHandler, err =
NewEncryptedEnvFunc(cfg.Opts.FS, fileRegistry, cfg.Dir, cfg.Opts.ReadOnly, cfg.ExtraOptions)
if err != nil {
return nil, nil, err
}
}
return fileRegistry, statsHandler, nil
}
// NewPebble creates a new Pebble instance, at the specified path.
func NewPebble(ctx context.Context, cfg PebbleConfig) (*Pebble, error) {
// pebble.Open also calls EnsureDefaults, but only after doing a clone. Call
// EnsureDefaults beforehand so we have a matching cfg here for when we save
// cfg.FS and cfg.ReadOnly later on.
cfg.Opts.EnsureDefaults()
cfg.Opts.ErrorIfNotExists = cfg.MustExist
if settings := cfg.Settings; settings != nil {
cfg.Opts.WALMinSyncInterval = func() time.Duration {
return minWALSyncInterval.Get(&settings.SV)
}
}
auxDir := cfg.Opts.FS.PathJoin(cfg.Dir, base.AuxiliaryDir)
if err := cfg.Opts.FS.MkdirAll(auxDir, 0755); err != nil {
return nil, err
}
fileRegistry, statsHandler, err := ResolveEncryptedEnvOptions(&cfg)
if err != nil {
return nil, err
}
// The context dance here is done so that we have a clean context without
// timeouts that has a copy of the log tags.
logCtx := logtags.WithTags(context.Background(), logtags.FromContext(ctx))
cfg.Opts.Logger = pebbleLogger{
ctx: logCtx,
depth: 1,
}
cfg.Opts.EventListener = pebble.MakeLoggingEventListener(pebbleLogger{
ctx: logCtx,
depth: 2, // skip over the EventListener stack frame
})
db, err := pebble.Open(cfg.StorageConfig.Dir, cfg.Opts)
if err != nil {
return nil, err
}
return &Pebble{
db: db,
path: cfg.Dir,
auxDir: auxDir,
maxSize: cfg.MaxSize,
attrs: cfg.Attrs,
settings: cfg.Settings,
statsHandler: statsHandler,
fileRegistry: fileRegistry,
fs: cfg.Opts.FS,
logger: cfg.Opts.Logger,
}, nil
}
func newTeeInMem(ctx context.Context, attrs roachpb.Attributes, cacheSize int64) *TeeEngine {
// Note that we use the same unmodified directories for both pebble and
// rocksdb. This is to make sure the file paths match up, and that we're
// able to write to both and ingest from both memory filesystems.
pebbleInMem := newPebbleInMem(ctx, attrs, cacheSize)
rocksDBInMem := newRocksDBInMem(attrs, cacheSize)
tee := NewTee(ctx, rocksDBInMem, pebbleInMem)
return tee
}
func newPebbleInMem(ctx context.Context, attrs roachpb.Attributes, cacheSize int64) *Pebble {
opts := DefaultPebbleOptions()
opts.Cache = pebble.NewCache(cacheSize)
defer opts.Cache.Unref()
opts.FS = vfs.NewMem()
db, err := NewPebble(
ctx,
PebbleConfig{
StorageConfig: base.StorageConfig{
Attrs: attrs,
// TODO(bdarnell): The hard-coded 512 MiB is wrong; see
// https://github.com/cockroachdb/cockroach/issues/16750
MaxSize: 512 << 20, /* 512 MiB */
},
Opts: opts,
})
if err != nil {
panic(err)
}
return db
}
func (p *Pebble) String() string {
dir := p.path
if dir == "" {
dir = "<in-mem>"
}
attrs := p.attrs.String()
if attrs == "" {
attrs = "<no-attributes>"
}
return fmt.Sprintf("%s=%s", attrs, dir)
}
// Close implements the Engine interface.
func (p *Pebble) Close() {
if p.closed {
p.logger.Infof("closing unopened pebble instance")
return
}
p.closed = true
_ = p.db.Close()
}
// Closed implements the Engine interface.
func (p *Pebble) Closed() bool {
return p.closed
}
// ExportToSst is part of the engine.Reader interface.
func (p *Pebble) ExportToSst(
startKey, endKey roachpb.Key,
startTS, endTS hlc.Timestamp,
exportAllRevisions bool,
targetSize, maxSize uint64,
io IterOptions,
) ([]byte, roachpb.BulkOpSummary, roachpb.Key, error) {
return pebbleExportToSst(p, startKey, endKey, startTS, endTS, exportAllRevisions, targetSize, maxSize, io)
}
// Get implements the Engine interface.
func (p *Pebble) Get(key MVCCKey) ([]byte, error) {
if len(key.Key) == 0 {
return nil, emptyKeyError()
}
ret, closer, err := p.db.Get(EncodeKey(key))
if closer != nil {
retCopy := make([]byte, len(ret))
copy(retCopy, ret)
ret = retCopy
closer.Close()
}
if errors.Is(err, pebble.ErrNotFound) || len(ret) == 0 {
return nil, nil
}
return ret, err
}
// GetCompactionStats implements the Engine interface.
func (p *Pebble) GetCompactionStats() string {
// NB: The initial blank line matches the formatting used by RocksDB and
// ensures that compaction stats display will not contain the log prefix
// (this method is only used for logging purposes).
return "\n" + p.db.Metrics().String()
}
// GetProto implements the Engine interface.
func (p *Pebble) GetProto(
key MVCCKey, msg protoutil.Message,
) (ok bool, keyBytes, valBytes int64, err error) {
if len(key.Key) == 0 {
return false, 0, 0, emptyKeyError()
}
encodedKey := EncodeKey(key)
val, closer, err := p.db.Get(encodedKey)
if closer != nil {
if msg != nil {
err = protoutil.Unmarshal(val, msg)
}
keyBytes = int64(len(encodedKey))
valBytes = int64(len(val))
closer.Close()
return true, keyBytes, valBytes, err
}
if errors.Is(err, pebble.ErrNotFound) {
return false, 0, 0, nil
}
return false, 0, 0, err
}
// Iterate implements the Engine interface.
func (p *Pebble) Iterate(
start, end roachpb.Key, f func(MVCCKeyValue) (stop bool, err error),
) error {
return iterateOnReader(p, start, end, f)
}
// NewIterator implements the Engine interface.
func (p *Pebble) NewIterator(opts IterOptions) Iterator {
iter := newPebbleIterator(p.db, opts)
if iter == nil {
panic("couldn't create a new iterator")
}
return iter
}
// ApplyBatchRepr implements the Engine interface.
func (p *Pebble) ApplyBatchRepr(repr []byte, sync bool) error {
// batch.SetRepr takes ownership of the underlying slice, so make a copy.
reprCopy := make([]byte, len(repr))
copy(reprCopy, repr)
batch := p.db.NewBatch()
if err := batch.SetRepr(reprCopy); err != nil {
return err
}
opts := pebble.NoSync
if sync {
opts = pebble.Sync
}
return batch.Commit(opts)
}
// Clear implements the Engine interface.
func (p *Pebble) Clear(key MVCCKey) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
return p.db.Delete(EncodeKey(key), pebble.Sync)
}
// SingleClear implements the Engine interface.
func (p *Pebble) SingleClear(key MVCCKey) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
return p.db.SingleDelete(EncodeKey(key), pebble.Sync)
}
// ClearRange implements the Engine interface.
func (p *Pebble) ClearRange(start, end MVCCKey) error {
bufStart := EncodeKey(start)
bufEnd := EncodeKey(end)
return p.db.DeleteRange(bufStart, bufEnd, pebble.Sync)
}
// ClearIterRange implements the Engine interface.
func (p *Pebble) ClearIterRange(iter Iterator, start, end roachpb.Key) error {
// Write all the tombstones in one batch.
batch := p.NewWriteOnlyBatch()
defer batch.Close()
if err := batch.ClearIterRange(iter, start, end); err != nil {
return err
}
return batch.Commit(true)
}
// Merge implements the Engine interface.
func (p *Pebble) Merge(key MVCCKey, value []byte) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
return p.db.Merge(EncodeKey(key), value, pebble.Sync)
}
// Put implements the Engine interface.
func (p *Pebble) Put(key MVCCKey, value []byte) error {
if len(key.Key) == 0 {
return emptyKeyError()
}
return p.db.Set(EncodeKey(key), value, pebble.Sync)
}
// LogData implements the Engine interface.
func (p *Pebble) LogData(data []byte) error {
return p.db.LogData(data, pebble.Sync)
}
// LogLogicalOp implements the Engine interface.
func (p *Pebble) LogLogicalOp(op MVCCLogicalOpType, details MVCCLogicalOpDetails) {
// No-op. Logical logging disabled.
}
// Attrs implements the Engine interface.
func (p *Pebble) Attrs() roachpb.Attributes {
return p.attrs
}
// Capacity implements the Engine interface.
func (p *Pebble) Capacity() (roachpb.StoreCapacity, error) {
return computeCapacity(p.path, p.maxSize)
}
// Flush implements the Engine interface.
func (p *Pebble) Flush() error {
return p.db.Flush()
}
// GetStats implements the Engine interface.
func (p *Pebble) GetStats() (*Stats, error) {
m := p.db.Metrics()
// Aggregate compaction stats across levels.
var ingestedBytes, compactedBytesRead, compactedBytesWritten int64
for _, lm := range m.Levels {
ingestedBytes += int64(lm.BytesIngested)
compactedBytesRead += int64(lm.BytesRead)
compactedBytesWritten += int64(lm.BytesCompacted)
}
return &Stats{
BlockCacheHits: m.BlockCache.Hits,
BlockCacheMisses: m.BlockCache.Misses,
BlockCacheUsage: m.BlockCache.Size,
BlockCachePinnedUsage: 0,
BloomFilterPrefixChecked: m.Filter.Hits + m.Filter.Misses,
BloomFilterPrefixUseful: m.Filter.Hits,
MemtableTotalSize: int64(m.MemTable.Size),
Flushes: m.Flush.Count,
FlushedBytes: int64(m.Levels[0].BytesFlushed),
Compactions: m.Compact.Count,
IngestedBytes: ingestedBytes,
CompactedBytesRead: compactedBytesRead,
CompactedBytesWritten: compactedBytesWritten,
TableReadersMemEstimate: m.TableCache.Size,
PendingCompactionBytesEstimate: int64(m.Compact.EstimatedDebt),
L0FileCount: m.Levels[0].NumFiles,
}, nil
}
// GetEncryptionRegistries implements the Engine interface.
func (p *Pebble) GetEncryptionRegistries() (*EncryptionRegistries, error) {
rv := &EncryptionRegistries{}
var err error
if p.statsHandler != nil {
rv.KeyRegistry, err = p.statsHandler.GetDataKeysRegistry()
if err != nil {
return nil, err
}
}
if p.fileRegistry != nil {
rv.FileRegistry, err = protoutil.Marshal(p.fileRegistry.getRegistryCopy())
if err != nil {
return nil, err
}
}
return rv, nil
}
// GetEnvStats implements the Engine interface.
func (p *Pebble) GetEnvStats() (*EnvStats, error) {
// TODO(sumeer): make the stats complete. There are no bytes stats. The TotalFiles is missing
// files that are not in the registry (from before encryption was enabled).
stats := &EnvStats{}
if p.statsHandler == nil {
return stats, nil
}
stats.EncryptionType = p.statsHandler.GetActiveStoreKeyType()
var err error
stats.EncryptionStatus, err = p.statsHandler.GetEncryptionStatus()
if err != nil {
return nil, err
}
fr := p.fileRegistry.getRegistryCopy()
activeKeyID, err := p.statsHandler.GetActiveDataKeyID()
if err != nil {
return nil, err
}
m := p.db.Metrics()
stats.TotalFiles = 3 /* CURRENT, MANIFEST, OPTIONS */
stats.TotalFiles += uint64(m.WAL.Files + m.Table.ZombieCount + m.WAL.ObsoleteFiles)
stats.TotalBytes = m.WAL.Size + m.Table.ZombieSize
for _, l := range m.Levels {
stats.TotalFiles += uint64(l.NumFiles)
stats.TotalBytes += l.Size
}
sstSizes := make(map[pebble.FileNum]uint64)
for _, ssts := range p.db.SSTables() {
for _, sst := range ssts {
sstSizes[sst.FileNum] = sst.Size
}
}
for filePath, entry := range fr.Files {
keyID, err := p.statsHandler.GetKeyIDFromSettings(entry.EncryptionSettings)
if err != nil {
return nil, err
}
if len(keyID) == 0 {
keyID = "plain"
}
if keyID != activeKeyID {
continue
}
stats.ActiveKeyFiles++
filename := p.fs.PathBase(filePath)
numStr := strings.TrimSuffix(filename, ".sst")
if len(numStr) == len(filename) {
continue // not a sstable
}
u, err := strconv.ParseUint(numStr, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "parsing filename %q", errors.Safe(filename))
}
stats.ActiveKeyBytes += sstSizes[pebble.FileNum(u)]
}
return stats, nil
}
// GetAuxiliaryDir implements the Engine interface.
func (p *Pebble) GetAuxiliaryDir() string {
return p.auxDir
}
// NewBatch implements the Engine interface.
func (p *Pebble) NewBatch() Batch {
return newPebbleBatch(p.db, p.db.NewIndexedBatch())
}
// NewReadOnly implements the Engine interface.
func (p *Pebble) NewReadOnly() ReadWriter {
return &pebbleReadOnly{
parent: p,
}
}
// NewWriteOnlyBatch implements the Engine interface.
func (p *Pebble) NewWriteOnlyBatch() Batch {
return newPebbleBatch(p.db, p.db.NewBatch())
}
// NewSnapshot implements the Engine interface.
func (p *Pebble) NewSnapshot() Reader {
return &pebbleSnapshot{
snapshot: p.db.NewSnapshot(),
}
}
// Type implements the Engine interface.
func (p *Pebble) Type() enginepb.EngineType {
return enginepb.EngineTypePebble
}
// IngestExternalFiles implements the Engine interface.
func (p *Pebble) IngestExternalFiles(ctx context.Context, paths []string) error {
return p.db.Ingest(paths)
}
// PreIngestDelay implements the Engine interface.
func (p *Pebble) PreIngestDelay(ctx context.Context) {
preIngestDelay(ctx, p, p.settings)
}
// ApproximateDiskBytes implements the Engine interface.
func (p *Pebble) ApproximateDiskBytes(from, to roachpb.Key) (uint64, error) {
count, err := p.db.EstimateDiskUsage(from, to)
if err != nil {
return 0, err
}
return count, nil
}
// Compact implements the Engine interface.
func (p *Pebble) Compact() error {
return p.db.Compact(nil, EncodeKey(MVCCKeyMax))
}
// CompactRange implements the Engine interface.
func (p *Pebble) CompactRange(start, end roachpb.Key, forceBottommost bool) error {
bufStart := EncodeKey(MVCCKey{start, hlc.Timestamp{}})
bufEnd := EncodeKey(MVCCKey{end, hlc.Timestamp{}})
return p.db.Compact(bufStart, bufEnd)
}
// InMem returns true if the receiver is an in-memory engine and false
// otherwise.
func (p *Pebble) InMem() bool {
return p.path == ""
}
// ReadFile implements the Engine interface.
func (p *Pebble) ReadFile(filename string) ([]byte, error) {
file, err := p.fs.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
return ioutil.ReadAll(file)
}
// WriteFile writes data to a file in this RocksDB's env.
func (p *Pebble) WriteFile(filename string, data []byte) error {
file, err := p.fs.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, bytes.NewReader(data))
return err
}
// Remove implements the FS interface.
func (p *Pebble) Remove(filename string) error {
return p.fs.Remove(filename)
}
// RemoveAll implements the Engine interface.
func (p *Pebble) RemoveAll(dir string) error {
return p.fs.RemoveAll(dir)
}
// Link implements the FS interface.
func (p *Pebble) Link(oldname, newname string) error {
return p.fs.Link(oldname, newname)
}
var _ fs.FS = &Pebble{}
// Create implements the FS interface.
func (p *Pebble) Create(name string) (fs.File, error) {
// TODO(peter): On RocksDB, the MemEnv allows creating a file when the parent
// directory does not exist. Various tests in the storage package depend on
// this because they are accidentally creating the required directory on the
// actual filesystem instead of in the memory filesystem. See
// diskSideloadedStorage and SSTSnapshotStrategy.
if p.InMem() {
_ = p.fs.MkdirAll(p.fs.PathDir(name), 0755)
}
return p.fs.Create(name)
}
// CreateWithSync implements the FS interface.
func (p *Pebble) CreateWithSync(name string, bytesPerSync int) (fs.File, error) {
// TODO(peter): On RocksDB, the MemEnv allows creating a file when the parent
// directory does not exist. Various tests in the storage package depend on
// this because they are accidentally creating the required directory on the
// actual filesystem instead of in the memory filesystem. See
// diskSideloadedStorage and SSTSnapshotStrategy.
if p.InMem() {
_ = p.fs.MkdirAll(p.fs.PathDir(name), 0755)
}
f, err := p.fs.Create(name)
if err != nil {
return nil, err
}
return vfs.NewSyncingFile(f, vfs.SyncingFileOptions{BytesPerSync: bytesPerSync}), nil
}