forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_del_test.go
651 lines (589 loc) · 17.5 KB
/
range_del_test.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
// Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package pebble
import (
"bytes"
"context"
"fmt"
"math/rand/v2"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/testkeys"
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
"github.com/cockroachdb/pebble/sstable"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
func TestRangeDel(t *testing.T) {
var d *DB
defer func() {
if d != nil {
require.NoError(t, closeAllSnapshots(d))
require.NoError(t, d.Close())
}
}()
opts := &Options{}
opts.DisableAutomaticCompactions = true
datadriven.RunTest(t, "testdata/range_del", func(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "define":
if d != nil {
if err := closeAllSnapshots(d); err != nil {
return err.Error()
}
if err := d.Close(); err != nil {
return err.Error()
}
}
var err error
if d, err = runDBDefineCmd(td, opts); err != nil {
return err.Error()
}
d.mu.Lock()
// Disable the "dynamic base level" code for this test.
d.mu.versions.picker.forceBaseLevel1()
s := fmt.Sprintf("mem: %d\n%s", len(d.mu.mem.queue), d.mu.versions.currentVersion().String())
d.mu.Unlock()
return s
case "wait-pending-table-stats":
return runTableStatsCmd(td, d)
case "compact":
if err := runCompactCmd(td, d); err != nil {
return err.Error()
}
d.mu.Lock()
// Disable the "dynamic base level" code for this test.
d.mu.versions.picker.forceBaseLevel1()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
case "get":
return runGetCmd(t, td, d)
case "iter":
snap := Snapshot{
db: d,
seqNum: base.SeqNumMax,
}
if td.HasArg("seq") {
var n uint64
td.ScanArgs(t, "seq", &n)
snap.seqNum = base.SeqNum(n)
}
iter, _ := snap.NewIter(nil)
return runIterCmd(td, iter, true)
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestFlushDelay(t *testing.T) {
opts := &Options{
FS: vfs.NewMem(),
Comparer: testkeys.Comparer,
FlushDelayDeleteRange: 10 * time.Millisecond,
FlushDelayRangeKey: 10 * time.Millisecond,
FormatMajorVersion: internalFormatNewest,
Logger: testLogger{t: t},
}
d, err := Open("", opts)
require.NoError(t, err)
// Ensure that all the various means of writing a rangedel or range key
// trigger their respective flush delays.
cases := []func(){
func() {
require.NoError(t, d.DeleteRange([]byte("a"), []byte("z"), nil))
},
func() {
b := d.NewBatch()
require.NoError(t, b.DeleteRange([]byte("a"), []byte("z"), nil))
require.NoError(t, b.Commit(nil))
},
func() {
b := d.NewBatch()
op := b.DeleteRangeDeferred(1, 1)
op.Key[0] = 'a'
op.Value[0] = 'z'
op.Finish()
require.NoError(t, b.Commit(nil))
},
func() {
b := d.NewBatch()
b2 := d.NewBatch()
require.NoError(t, b.DeleteRange([]byte("a"), []byte("z"), nil))
require.NoError(t, b2.SetRepr(b.Repr()))
require.NoError(t, b2.Commit(nil))
require.NoError(t, b.Close())
},
func() {
b := d.NewBatch()
b2 := d.NewBatch()
require.NoError(t, b.DeleteRange([]byte("a"), []byte("z"), nil))
require.NoError(t, b2.Apply(b, nil))
require.NoError(t, b2.Commit(nil))
require.NoError(t, b.Close())
},
func() {
require.NoError(t, d.RangeKeySet([]byte("a"), []byte("z"), nil, nil, nil))
},
func() {
require.NoError(t, d.RangeKeyUnset([]byte("a"), []byte("z"), nil, nil))
},
func() {
require.NoError(t, d.RangeKeyDelete([]byte("a"), []byte("z"), nil))
},
func() {
b := d.NewBatch()
require.NoError(t, b.RangeKeySet([]byte("a"), []byte("z"), nil, nil, nil))
require.NoError(t, b.Commit(nil))
},
func() {
b := d.NewBatch()
require.NoError(t, b.RangeKeyUnset([]byte("a"), []byte("z"), nil, nil))
require.NoError(t, b.Commit(nil))
},
func() {
b := d.NewBatch()
require.NoError(t, b.RangeKeyDelete([]byte("a"), []byte("z"), nil))
require.NoError(t, b.Commit(nil))
},
func() {
b := d.NewBatch()
b2 := d.NewBatch()
require.NoError(t, b.RangeKeySet([]byte("a"), []byte("z"), nil, nil, nil))
require.NoError(t, b2.SetRepr(b.Repr()))
require.NoError(t, b2.Commit(nil))
require.NoError(t, b.Close())
},
func() {
b := d.NewBatch()
b2 := d.NewBatch()
require.NoError(t, b.RangeKeySet([]byte("a"), []byte("z"), nil, nil, nil))
require.NoError(t, b2.Apply(b, nil))
require.NoError(t, b2.Commit(nil))
require.NoError(t, b.Close())
},
}
for _, f := range cases {
d.mu.Lock()
flushed := d.mu.mem.queue[len(d.mu.mem.queue)-1].flushed
d.mu.Unlock()
f()
<-flushed
}
require.NoError(t, d.Close())
}
func TestFlushDelayStress(t *testing.T) {
rng := rand.New(rand.NewPCG(0, uint64(time.Now().UnixNano())))
opts := &Options{
FS: vfs.NewMem(),
Comparer: testkeys.Comparer,
FlushDelayDeleteRange: time.Duration(rng.IntN(10)+1) * time.Millisecond,
FlushDelayRangeKey: time.Duration(rng.IntN(10)+1) * time.Millisecond,
FormatMajorVersion: internalFormatNewest,
MemTableSize: 8192,
Logger: testLogger{t: t},
}
const runs = 100
for run := 0; run < runs; run++ {
d, err := Open("", opts)
require.NoError(t, err)
now := time.Now().UnixNano()
writers := runtime.GOMAXPROCS(0)
var wg sync.WaitGroup
wg.Add(writers)
for i := 0; i < writers; i++ {
rng := rand.New(rand.NewPCG(0, uint64(now)+uint64(i)))
go func() {
const ops = 100
defer wg.Done()
var k1, k2 [32]byte
for j := 0; j < ops; j++ {
switch rng.IntN(3) {
case 0:
randStr(k1[:], rng)
randStr(k2[:], rng)
require.NoError(t, d.DeleteRange(k1[:], k2[:], nil))
case 1:
randStr(k1[:], rng)
randStr(k2[:], rng)
require.NoError(t, d.RangeKeySet(k1[:], k2[:], []byte("@2"), nil, nil))
case 2:
randStr(k1[:], rng)
randStr(k2[:], rng)
require.NoError(t, d.Set(k1[:], k2[:], nil))
default:
panic("unreachable")
}
}
}()
}
wg.Wait()
time.Sleep(time.Duration(rng.IntN(10)+1) * time.Millisecond)
require.NoError(t, d.Close())
}
}
// Verify that range tombstones at higher levels do not unintentionally delete
// newer keys at lower levels. This test sets up one such scenario. The base
// problem is that range tombstones are not truncated to sstable boundaries on
// disk, only in memory.
func TestRangeDelCompactionTruncation(t *testing.T) {
runTest := func(formatVersion FormatMajorVersion) {
// Use a small target file size so that there is a single key per sstable.
d, err := Open("", &Options{
FS: vfs.NewMem(),
Levels: []LevelOptions{
{TargetFileSize: 100},
{TargetFileSize: 100},
{TargetFileSize: 1},
},
DebugCheck: DebugCheckLevels,
FormatMajorVersion: formatVersion,
})
require.NoError(t, err)
defer d.Close()
d.mu.Lock()
d.mu.versions.dynamicBaseLevel = false
d.mu.Unlock()
lsm := func() string {
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
}
expectLSM := func(expected string) {
t.Helper()
expected = strings.TrimSpace(expected)
actual := strings.TrimSpace(lsm())
if expected != actual {
t.Fatalf("expected\n%s\nbut found\n%s", expected, actual)
}
}
require.NoError(t, d.Set([]byte("a"), bytes.Repeat([]byte("b"), 100), nil))
snap1 := d.NewSnapshot()
defer snap1.Close()
// Flush so that each version of "a" ends up in its own L0 table. If we
// allowed both versions in the same L0 table, compaction could trivially
// move the single L0 table to L1.
require.NoError(t, d.Flush())
require.NoError(t, d.Set([]byte("b"), bytes.Repeat([]byte("c"), 100), nil))
snap2 := d.NewSnapshot()
defer snap2.Close()
require.NoError(t, d.DeleteRange([]byte("a"), []byte("d"), nil))
// Compact to produce the L1 tables.
require.NoError(t, d.Compact([]byte("c"), []byte("c\x00"), false))
expectLSM(`
L1:
000008:[a#12,RANGEDEL-b#inf,RANGEDEL]
000009:[b#12,RANGEDEL-d#inf,RANGEDEL]
`)
// Compact again to move one of the tables to L2.
require.NoError(t, d.Compact([]byte("c"), []byte("c\x00"), false))
expectLSM(`
L1:
000008:[a#12,RANGEDEL-b#inf,RANGEDEL]
L2:
000009:[b#12,RANGEDEL-d#inf,RANGEDEL]
`)
// Write "b" and "c" to a new table.
require.NoError(t, d.Set([]byte("b"), []byte("d"), nil))
require.NoError(t, d.Set([]byte("c"), []byte("e"), nil))
require.NoError(t, d.Flush())
expectLSM(`
L0.0:
000011:[b#13,SET-c#14,SET]
L1:
000008:[a#12,RANGEDEL-b#inf,RANGEDEL]
L2:
000009:[b#12,RANGEDEL-d#inf,RANGEDEL]
`)
// "b" is still visible at this point as it should be.
if _, closer, err := d.Get([]byte("b")); err != nil {
t.Fatalf("expected success, but found %v", err)
} else {
closer.Close()
}
keys := func() string {
iter, _ := d.NewIter(nil)
defer iter.Close()
var buf bytes.Buffer
var sep string
for iter.First(); iter.Valid(); iter.Next() {
fmt.Fprintf(&buf, "%s%s", sep, iter.Key())
sep = " "
}
return buf.String()
}
if expected, actual := `b c`, keys(); expected != actual {
t.Fatalf("expected %q, but found %q", expected, actual)
}
// Compact the L0 table. This will compact the L0 table into L1 and do to the
// sstable target size settings will create 2 tables in L1. Then L1 table
// containing "c" will be compacted again with the L2 table creating two
// tables in L2. Lastly, the L2 table containing "c" will be compacted
// creating the L3 table.
require.NoError(t, d.Compact([]byte("c"), []byte("c\x00"), false))
expectLSM(`
L1:
000008:[a#12,RANGEDEL-b#inf,RANGEDEL]
L2:
000012:[b#13,SET-c#inf,RANGEDEL]
L3:
000013:[c#14,SET-d#inf,RANGEDEL]
`)
// The L1 table still contains a tombstone from [a,d) which will improperly
// delete the newer version of "b" in L2.
if _, closer, err := d.Get([]byte("b")); err != nil {
t.Errorf("expected success, but found %v", err)
} else {
closer.Close()
}
if expected, actual := `b c`, keys(); expected != actual {
t.Errorf("expected %q, but found %q", expected, actual)
}
}
versions := []FormatMajorVersion{
FormatMinSupported,
FormatNewest,
}
for _, version := range versions {
t.Run(fmt.Sprintf("version-%s", version), func(t *testing.T) {
runTest(version)
})
}
}
// This is an alternate scenario to the one created in
// TestRangeDelCompactionTruncation that would result in the bounds for an
// sstable expanding to overlap its left neighbor if we failed to truncate an
// sstable's boundaries to the compaction input boundaries.
func TestRangeDelCompactionTruncation2(t *testing.T) {
// Use a small target file size so that there is a single key per sstable.
d, err := Open("", &Options{
FS: vfs.NewMem(),
Levels: []LevelOptions{
{TargetFileSize: 200},
{TargetFileSize: 200},
{TargetFileSize: 1},
},
DebugCheck: DebugCheckLevels,
})
require.NoError(t, err)
defer d.Close()
lsm := func() string {
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
}
expectLSM := func(expected string) {
t.Helper()
expected = strings.TrimSpace(expected)
actual := strings.TrimSpace(lsm())
if expected != actual {
t.Fatalf("expected\n%s\nbut found\n%s", expected, actual)
}
}
require.NoError(t, d.Set([]byte("b"), bytes.Repeat([]byte("b"), 100), nil))
snap1 := d.NewSnapshot()
defer snap1.Close()
// Flush so that each version of "b" ends up in its own L0 table. If we
// allowed both versions in the same L0 table, compaction could trivially
// move the single L0 table to L1.
require.NoError(t, d.Flush())
require.NoError(t, d.Set([]byte("b"), bytes.Repeat([]byte("c"), 100), nil))
snap2 := d.NewSnapshot()
defer snap2.Close()
require.NoError(t, d.DeleteRange([]byte("a"), []byte("d"), nil))
// Compact to produce the L1 tables.
require.NoError(t, d.Compact([]byte("b"), []byte("b\x00"), false))
expectLSM(`
L6:
000009:[a#12,RANGEDEL-d#inf,RANGEDEL]
`)
require.NoError(t, d.Set([]byte("c"), bytes.Repeat([]byte("d"), 100), nil))
require.NoError(t, d.Compact([]byte("c"), []byte("c\x00"), false))
expectLSM(`
L6:
000012:[a#12,RANGEDEL-c#inf,RANGEDEL]
000013:[c#13,SET-d#inf,RANGEDEL]
`)
}
// TODO(peter): rewrite this test, TestRangeDelCompactionTruncation, and
// TestRangeDelCompactionTruncation2 as data-driven tests.
func TestRangeDelCompactionTruncation3(t *testing.T) {
// Use a small target file size so that there is a single key per sstable.
d, err := Open("tmp", &Options{
Cleaner: ArchiveCleaner{},
FS: vfs.NewMem(),
Levels: []LevelOptions{
{TargetFileSize: 200},
{TargetFileSize: 200},
{TargetFileSize: 1},
},
DebugCheck: DebugCheckLevels,
})
require.NoError(t, err)
defer d.Close()
d.mu.Lock()
d.mu.versions.dynamicBaseLevel = false
d.mu.Unlock()
lsm := func() string {
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
}
expectLSM := func(expected string) {
t.Helper()
expected = strings.TrimSpace(expected)
actual := strings.TrimSpace(lsm())
if expected != actual {
t.Fatalf("expected\n%s\nbut found\n%s", expected, actual)
}
}
require.NoError(t, d.Set([]byte("b"), bytes.Repeat([]byte("b"), 100), nil))
snap1 := d.NewSnapshot()
defer snap1.Close()
// Flush so that each version of "b" ends up in its own L0 table. If we
// allowed both versions in the same L0 table, compaction could trivially
// move the single L0 table to L1.
require.NoError(t, d.Flush())
require.NoError(t, d.Set([]byte("b"), bytes.Repeat([]byte("c"), 100), nil))
snap2 := d.NewSnapshot()
defer snap2.Close()
require.NoError(t, d.DeleteRange([]byte("a"), []byte("d"), nil))
snap3 := d.NewSnapshot()
defer snap3.Close()
if _, _, err := d.Get([]byte("b")); err != ErrNotFound {
t.Fatalf("expected not found, but found %v", err)
}
// Compact a few times to move the tables down to L3.
for i := 0; i < 3; i++ {
require.NoError(t, d.Compact([]byte("b"), []byte("b\x00"), false))
}
expectLSM(`
L3:
000009:[a#12,RANGEDEL-d#inf,RANGEDEL]
`)
require.NoError(t, d.Set([]byte("c"), bytes.Repeat([]byte("d"), 100), nil))
require.NoError(t, d.Compact([]byte("c"), []byte("c\x00"), false))
expectLSM(`
L3:
000013:[a#12,RANGEDEL-b#inf,RANGEDEL]
000014:[b#12,RANGEDEL-c#inf,RANGEDEL]
L4:
000015:[c#13,SET-d#inf,RANGEDEL]
`)
require.NoError(t, d.Compact([]byte("c"), []byte("c\x00"), false))
expectLSM(`
L3:
000013:[a#12,RANGEDEL-b#inf,RANGEDEL]
000014:[b#12,RANGEDEL-c#inf,RANGEDEL]
L5:
000015:[c#13,SET-d#inf,RANGEDEL]
`)
if _, _, err := d.Get([]byte("b")); err != ErrNotFound {
t.Fatalf("expected not found, but found %v", err)
}
require.NoError(t, d.Compact([]byte("a"), []byte("a\x00"), false))
expectLSM(`
L3:
000014:[b#12,RANGEDEL-c#inf,RANGEDEL]
L4:
000013:[a#12,RANGEDEL-b#inf,RANGEDEL]
L5:
000015:[c#13,SET-d#inf,RANGEDEL]
`)
if v, _, err := d.Get([]byte("b")); err != ErrNotFound {
t.Fatalf("expected not found, but found %v [%s]", err, v)
}
}
func BenchmarkRangeDelIterate(b *testing.B) {
for _, entries := range []int{10, 1000, 100000} {
b.Run(fmt.Sprintf("entries=%d", entries), func(b *testing.B) {
for _, deleted := range []int{entries, entries - 1} {
b.Run(fmt.Sprintf("deleted=%d", deleted), func(b *testing.B) {
for _, snapshotCompact := range []bool{false, true} {
b.Run(fmt.Sprintf("snapshotAndCompact=%t", snapshotCompact), func(b *testing.B) {
benchmarkRangeDelIterate(b, entries, deleted, snapshotCompact)
})
}
})
}
})
}
}
func benchmarkRangeDelIterate(b *testing.B, entries, deleted int, snapshotCompact bool) {
mem := vfs.NewMem()
cache := NewCache(128 << 20) // 128 MB
defer cache.Unref()
d, err := Open("", &Options{
Cache: cache,
FS: mem,
DebugCheck: DebugCheckLevels,
})
if err != nil {
b.Fatal(err)
}
defer d.Close()
makeKey := func(i int) []byte {
return []byte(fmt.Sprintf("%09d", i))
}
// Create an sstable with N entries and ingest it. This is a fast way
// to get a lot of entries into pebble.
f, err := mem.Create("ext", vfs.WriteCategoryUnspecified)
if err != nil {
b.Fatal(err)
}
w := sstable.NewRawWriter(objstorageprovider.NewFileWritable(f), sstable.WriterOptions{
BlockSize: 32 << 10, // 32 KB
})
for i := 0; i < entries; i++ {
key := base.MakeInternalKey(makeKey(i), 0, InternalKeyKindSet)
if err := w.AddWithForceObsolete(key, nil, false /* forceObsolete */); err != nil {
b.Fatal(err)
}
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
if err := d.Ingest(context.Background(), []string{"ext"}); err != nil {
b.Fatal(err)
}
// Some benchmarks test snapshots that force the range tombstone into the
// same level as the covered data.
// See https://github.com/cockroachdb/pebble/issues/1070.
if snapshotCompact {
s := d.NewSnapshot()
defer func() { require.NoError(b, s.Close()) }()
}
// Create a range tombstone that deletes most (or all) of those entries.
from := makeKey(0)
to := makeKey(deleted)
if err := d.DeleteRange(from, to, nil); err != nil {
b.Fatal(err)
}
if snapshotCompact {
require.NoError(b, d.Compact(makeKey(0), makeKey(entries), false))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
iter, _ := d.NewIter(nil)
iter.SeekGE(from)
if deleted < entries {
if !iter.Valid() {
b.Fatal("key not found")
}
} else if iter.Valid() {
b.Fatal("unexpected key found")
}
if err := iter.Close(); err != nil {
b.Fatal(err)
}
}
}