forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_iter_test.go
787 lines (724 loc) · 22.6 KB
/
level_iter_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
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
// 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"
"strings"
"testing"
"time"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/bloom"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/itertest"
"github.com/cockroachdb/pebble/internal/keyspan"
"github.com/cockroachdb/pebble/internal/manifest"
"github.com/cockroachdb/pebble/internal/rangedel"
"github.com/cockroachdb/pebble/internal/rangekey"
"github.com/cockroachdb/pebble/internal/sstableinternal"
"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"
"golang.org/x/exp/rand"
)
const (
level = 1
)
func TestLevelIter(t *testing.T) {
var iterKVs [][]base.InternalKV
var files manifest.LevelSlice
newIters := func(
_ context.Context, file *manifest.FileMetadata, opts *IterOptions, _ internalIterOpts, _ iterKinds,
) (iterSet, error) {
f := base.NewFakeIter(iterKVs[file.FileNum])
f.SetBounds(opts.GetLowerBound(), opts.GetUpperBound())
return iterSet{point: f}, nil
}
datadriven.RunTest(t, "testdata/level_iter", func(t *testing.T, d *datadriven.TestData) string {
switch d.Cmd {
case "define":
iterKVs = nil
var metas []*fileMetadata
for _, line := range strings.Split(d.Input, "\n") {
var kvs []base.InternalKV
for _, key := range strings.Fields(line) {
j := strings.Index(key, ":")
kvs = append(kvs, base.MakeInternalKV(base.ParseInternalKey(key[:j]), []byte(key[j+1:])))
}
iterKVs = append(iterKVs, kvs)
meta := (&fileMetadata{
FileNum: FileNum(len(metas)),
}).ExtendPointKeyBounds(
DefaultComparer.Compare,
kvs[0].K,
kvs[len(kvs)-1].K,
)
meta.InitPhysicalBacking()
metas = append(metas, meta)
}
files = manifest.NewLevelSliceKeySorted(base.DefaultComparer.Compare, metas)
return ""
case "iter":
var opts IterOptions
for _, arg := range d.CmdArgs {
if len(arg.Vals) != 1 {
return fmt.Sprintf("%s: %s=<value>", d.Cmd, arg.Key)
}
switch arg.Key {
case "lower":
opts.LowerBound = []byte(arg.Vals[0])
case "upper":
opts.UpperBound = []byte(arg.Vals[0])
default:
return fmt.Sprintf("%s: unknown arg: %s", d.Cmd, arg.Key)
}
}
iter := newLevelIter(context.Background(), opts, testkeys.Comparer, newIters, files.Iter(), manifest.Level(level), internalIterOpts{})
defer iter.Close()
// Fake up the range deletion initialization.
iter.initRangeDel(func(rangeDelIter keyspan.FragmentIterator) {
if rangeDelIter != nil {
rangeDelIter.Close()
}
})
iter.disableInvariants = true
return itertest.RunInternalIterCmd(t, d, iter, itertest.Verbose)
case "load":
// The "load" command allows testing the iterator options passed to load
// sstables.
//
// load <key> [lower=<key>] [upper=<key>]
var opts IterOptions
var key string
for _, arg := range d.CmdArgs {
if len(arg.Vals) == 0 {
key = arg.Key
continue
}
if len(arg.Vals) != 1 {
return fmt.Sprintf("%s: %s=<value>", d.Cmd, arg.Key)
}
switch arg.Key {
case "lower":
opts.LowerBound = []byte(arg.Vals[0])
case "upper":
opts.UpperBound = []byte(arg.Vals[0])
default:
return fmt.Sprintf("%s: unknown arg: %s", d.Cmd, arg.Key)
}
}
var tableOpts *IterOptions
newIters2 := func(
ctx context.Context, file *manifest.FileMetadata, opts *IterOptions,
internalOpts internalIterOpts, kinds iterKinds,
) (iterSet, error) {
tableOpts = opts
return newIters(ctx, file, opts, internalOpts, kinds)
}
iter := newLevelIter(context.Background(), opts, testkeys.Comparer, newIters2, files.Iter(), manifest.Level(level), internalIterOpts{})
iter.SeekGE([]byte(key), base.SeekGEFlagsNone)
lower, upper := tableOpts.GetLowerBound(), tableOpts.GetUpperBound()
return fmt.Sprintf("[%s,%s]\n", lower, upper)
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
}
type levelIterTest struct {
cmp base.Comparer
mem vfs.FS
readers []*sstable.Reader
metas []*fileMetadata
itersCreated int
}
func newLevelIterTest() *levelIterTest {
lt := &levelIterTest{
cmp: *DefaultComparer,
mem: vfs.NewMem(),
}
lt.cmp.Split = func(a []byte) int { return len(a) }
return lt
}
func (lt *levelIterTest) newIters(
ctx context.Context,
file *manifest.FileMetadata,
opts *IterOptions,
iio internalIterOpts,
kinds iterKinds,
) (iterSet, error) {
lt.itersCreated++
transforms := file.IterTransforms()
var set iterSet
if kinds.Point() {
iter, err := lt.readers[file.FileNum].NewPointIter(
ctx, transforms,
opts.LowerBound, opts.UpperBound, nil, sstable.AlwaysUseFilterBlock, iio.stats, sstable.CategoryAndQoS{},
nil, sstable.MakeTrivialReaderProvider(lt.readers[file.FileNum]))
if err != nil {
return iterSet{}, errors.CombineErrors(err, set.CloseAll())
}
set.point = iter
}
if kinds.RangeDeletion() {
rangeDelIter, err := lt.readers[file.FileNum].NewRawRangeDelIter(context.Background(), file.FragmentIterTransforms())
if err != nil {
return iterSet{}, errors.CombineErrors(err, set.CloseAll())
}
set.rangeDeletion = rangeDelIter
}
return set, nil
}
func (lt *levelIterTest) runClear() string {
lt.mem = vfs.NewMem()
for _, r := range lt.readers {
r.Close()
}
lt.readers = nil
lt.metas = nil
lt.itersCreated = 0
return ""
}
func (lt *levelIterTest) runBuild(d *datadriven.TestData) string {
fileNum := FileNum(len(lt.readers))
name := fmt.Sprint(fileNum)
f0, err := lt.mem.Create(name, vfs.WriteCategoryUnspecified)
if err != nil {
return err.Error()
}
tableFormat := sstable.TableFormatMinSupported
for _, arg := range d.CmdArgs {
if arg.Key == "format" {
switch arg.Vals[0] {
case "pebblev2":
tableFormat = sstable.TableFormatPebblev2
}
}
}
fp := bloom.FilterPolicy(10)
w := sstable.NewRawWriter(objstorageprovider.NewFileWritable(f0), sstable.WriterOptions{
Comparer: <.cmp,
FilterPolicy: fp,
TableFormat: tableFormat,
})
var tombstones []keyspan.Span
f := keyspan.Fragmenter{
Cmp: lt.cmp.Compare,
Format: lt.cmp.FormatKey,
Emit: func(fragmented keyspan.Span) {
tombstones = append(tombstones, fragmented)
},
}
for _, key := range strings.Split(d.Input, "\n") {
j := strings.Index(key, ":")
ikey := base.ParseInternalKey(key[:j])
value := []byte(key[j+1:])
switch ikey.Kind() {
case InternalKeyKindRangeDelete:
f.Add(rangedel.Decode(ikey, value, nil))
case InternalKeyKindRangeKeySet, InternalKeyKindRangeKeyUnset, InternalKeyKindRangeKeyDelete:
span, err := rangekey.Decode(ikey, value, nil)
if err != nil {
return err.Error()
}
if err := w.EncodeSpan(span); err != nil {
return err.Error()
}
default:
if err := w.Add(ikey, value); err != nil {
return err.Error()
}
}
}
f.Finish()
for _, v := range tombstones {
if err := rangedel.Encode(v, w.Add); err != nil {
return err.Error()
}
}
if err := w.Close(); err != nil {
return err.Error()
}
meta, err := w.Metadata()
if err != nil {
return err.Error()
}
f1, err := lt.mem.Open(name)
if err != nil {
return err.Error()
}
readable, err := sstable.NewSimpleReadable(f1)
if err != nil {
return err.Error()
}
r, err := sstable.NewReader(context.Background(), readable, sstable.ReaderOptions{
Filters: map[string]FilterPolicy{
fp.Name(): fp,
},
})
if err != nil {
return err.Error()
}
lt.readers = append(lt.readers, r)
m := &fileMetadata{FileNum: fileNum}
if meta.HasPointKeys {
m.ExtendPointKeyBounds(lt.cmp.Compare, meta.SmallestPoint, meta.LargestPoint)
}
if meta.HasRangeDelKeys {
m.ExtendPointKeyBounds(lt.cmp.Compare, meta.SmallestRangeDel, meta.LargestRangeDel)
}
if meta.HasRangeKeys {
m.ExtendRangeKeyBounds(lt.cmp.Compare, meta.SmallestRangeKey, meta.LargestRangeKey)
}
m.InitPhysicalBacking()
lt.metas = append(lt.metas, m)
var buf bytes.Buffer
for _, f := range lt.metas {
fmt.Fprintf(&buf, "%d: %s-%s\n", f.FileNum, f.Smallest, f.Largest)
}
return buf.String()
}
func TestLevelIterBoundaries(t *testing.T) {
lt := newLevelIterTest()
defer lt.runClear()
var iter *levelIter
datadriven.RunTest(t, "testdata/level_iter_boundaries", func(t *testing.T, d *datadriven.TestData) string {
switch d.Cmd {
case "clear":
return lt.runClear()
case "build":
return lt.runBuild(d)
case "iter":
// The save and continue parameters allow us to save the iterator
// for later continued use.
save := false
cont := false
for _, arg := range d.CmdArgs {
switch arg.Key {
case "save":
save = true
case "continue":
cont = true
default:
return fmt.Sprintf("%s: unknown arg: %s", d.Cmd, arg.Key)
}
}
if !cont && iter != nil {
return "preceding iter was not closed"
}
if cont && iter == nil {
return "no existing iter"
}
if iter == nil {
slice := manifest.NewLevelSliceKeySorted(lt.cmp.Compare, lt.metas)
iter = newLevelIter(context.Background(), IterOptions{}, testkeys.Comparer, lt.newIters, slice.Iter(), manifest.Level(level), internalIterOpts{})
// Fake up the range deletion initialization.
iter.initRangeDel(func(rangeDelIter keyspan.FragmentIterator) {
if rangeDelIter != nil {
rangeDelIter.Close()
}
})
}
if !save {
defer func() {
iter.Close()
iter = nil
}()
}
return itertest.RunInternalIterCmd(t, d, iter, itertest.Verbose)
case "file-pos":
// Returns the FileNum at which the iterator is positioned.
if iter == nil {
return "nil levelIter"
}
if iter.iterFile == nil {
return "nil iterFile"
}
if iter.iter != nil {
return fmt.Sprintf("file %s [loaded]", iter.iterFile.FileNum)
}
return fmt.Sprintf("file %s [not loaded]", iter.iterFile.FileNum)
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
}
// levelIterTestIter allows a datadriven test to use runInternalIterCmd and
// perform parallel operations on both both a levelIter and rangeDelIter.
type levelIterTestIter struct {
*levelIter
rangeDelIter keyspan.FragmentIterator
// rangeDel is only set on seeks: SeekGE, SeekLT, SeekPrefixGE.
// TODO(jackson): Clean this up when #2863 is resolved.
rangeDel *keyspan.Span
}
func must(err error) {
if err != nil {
panic(err)
}
}
func (i *levelIterTestIter) getRangeDel() *keyspan.Span {
return i.rangeDel
}
func (i *levelIterTestIter) rangeDelSeek(
key []byte, kv *base.InternalKV, dir int,
) *base.InternalKV {
i.rangeDel = nil
if i.rangeDelIter != nil {
var t *keyspan.Span
var err error
if dir < 0 {
t, err = keyspan.SeekLE(i.levelIter.cmp, i.rangeDelIter, key)
} else {
t, err = i.rangeDelIter.SeekGE(key)
}
// TODO(jackson): Clean this up when the InternalIterator interface
// is refactored to return an error return value from all
// positioning methods.
must(err)
if t != nil {
i.rangeDel = new(keyspan.Span)
*i.rangeDel = t.Visible(1000)
}
}
return kv
}
func (i *levelIterTestIter) Close() error {
if i.rangeDelIter != nil {
i.rangeDelIter.Close()
i.rangeDelIter = nil
}
return i.levelIter.Close()
}
func (i *levelIterTestIter) String() string {
return "level-iter-test"
}
func (i *levelIterTestIter) SeekGE(key []byte, flags base.SeekGEFlags) *base.InternalKV {
kv := i.levelIter.SeekGE(key, flags)
return i.rangeDelSeek(key, kv, 1)
}
func (i *levelIterTestIter) SeekPrefixGE(
prefix, key []byte, flags base.SeekGEFlags,
) *base.InternalKV {
kv := i.levelIter.SeekPrefixGE(prefix, key, flags)
return i.rangeDelSeek(key, kv, 1)
}
func (i *levelIterTestIter) SeekLT(key []byte, flags base.SeekLTFlags) *base.InternalKV {
kv := i.levelIter.SeekLT(key, flags)
return i.rangeDelSeek(key, kv, -1)
}
func (i *levelIterTestIter) Next() *base.InternalKV {
i.rangeDel = nil
return i.levelIter.Next()
}
func (i *levelIterTestIter) Prev() *base.InternalKV {
i.rangeDel = nil
return i.levelIter.Prev()
}
func TestLevelIterSeek(t *testing.T) {
lt := newLevelIterTest()
defer lt.runClear()
datadriven.RunTest(t, "testdata/level_iter_seek", func(t *testing.T, d *datadriven.TestData) string {
switch d.Cmd {
case "clear":
return lt.runClear()
case "build":
return lt.runBuild(d)
case "iter":
var stats base.InternalIteratorStats
slice := manifest.NewLevelSliceKeySorted(lt.cmp.Compare, lt.metas)
iter := &levelIterTestIter{levelIter: &levelIter{}}
iter.init(context.Background(), IterOptions{}, testkeys.Comparer, lt.newIters, slice.Iter(),
manifest.Level(level), internalIterOpts{stats: &stats})
defer iter.Close()
iter.initRangeDel(func(rangeDelIter keyspan.FragmentIterator) {
if iter.rangeDelIter != nil {
iter.rangeDelIter.Close()
}
iter.rangeDelIter = rangeDelIter
})
return itertest.RunInternalIterCmd(t, d, iter, itertest.Verbose, itertest.WithSpan(iter.getRangeDel), itertest.WithStats(&stats))
case "iters-created":
return fmt.Sprintf("%d", lt.itersCreated)
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
}
func buildLevelIterTables(
b *testing.B, blockSize, restartInterval, count int,
) ([]*sstable.Reader, manifest.LevelSlice, [][]byte, func()) {
mem := vfs.NewMem()
files := make([]vfs.File, count)
for i := range files {
f, err := mem.Create(fmt.Sprintf("bench%d", i), vfs.WriteCategoryUnspecified)
if err != nil {
b.Fatal(err)
}
files[i] = f
}
writers := make([]*sstable.RawWriter, len(files))
for i := range files {
writers[i] = sstable.NewRawWriter(objstorageprovider.NewFileWritable(files[i]), sstable.WriterOptions{
BlockRestartInterval: restartInterval,
BlockSize: blockSize,
Compression: NoCompression,
})
}
var keys [][]byte
var i int
const targetSize = 2 << 20
for _, w := range writers {
for ; w.EstimatedSize() < targetSize; i++ {
key := []byte(fmt.Sprintf("%08d", i))
keys = append(keys, key)
ikey := base.MakeInternalKey(key, 0, InternalKeyKindSet)
w.Add(ikey, nil)
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
}
c := NewCache(128 << 20 /* 128MB */)
defer c.Unref()
opts := sstable.ReaderOptions{Comparer: DefaultComparer}
opts.SetInternalCacheOpts(sstableinternal.CacheOptions{
Cache: c,
})
readers := make([]*sstable.Reader, len(files))
for i := range files {
f, err := mem.Open(fmt.Sprintf("bench%d", i))
if err != nil {
b.Fatal(err)
}
readable, err := sstable.NewSimpleReadable(f)
if err != nil {
b.Fatal(err)
}
readers[i], err = sstable.NewReader(context.Background(), readable, opts)
if err != nil {
b.Fatal(err)
}
}
cleanup := func() {
for _, r := range readers {
require.NoError(b, r.Close())
}
}
meta := make([]*fileMetadata, len(readers))
for i := range readers {
iter, err := readers[i].NewIter(sstable.NoTransforms, nil /* lower */, nil /* upper */)
require.NoError(b, err)
smallest := iter.First()
meta[i] = &fileMetadata{}
meta[i].FileNum = FileNum(i)
largest := iter.Last()
meta[i].ExtendPointKeyBounds(opts.Comparer.Compare, smallest.K.Clone(), largest.K.Clone())
meta[i].InitPhysicalBacking()
}
slice := manifest.NewLevelSliceKeySorted(base.DefaultComparer.Compare, meta)
return readers, slice, keys, cleanup
}
func BenchmarkLevelIterSeekGE(b *testing.B) {
const blockSize = 32 << 10
for _, restartInterval := range []int{16} {
b.Run(fmt.Sprintf("restart=%d", restartInterval),
func(b *testing.B) {
for _, count := range []int{5} {
b.Run(fmt.Sprintf("count=%d", count),
func(b *testing.B) {
readers, metas, keys, cleanup := buildLevelIterTables(b, blockSize, restartInterval, count)
defer cleanup()
newIters := func(
_ context.Context, file *manifest.FileMetadata, _ *IterOptions, _ internalIterOpts, _ iterKinds,
) (iterSet, error) {
iter, err := readers[file.FileNum].NewIter(sstable.NoTransforms, nil /* lower */, nil /* upper */)
return iterSet{point: iter}, err
}
l := newLevelIter(context.Background(), IterOptions{}, DefaultComparer, newIters, metas.Iter(), manifest.Level(level), internalIterOpts{})
rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.SeekGE(keys[rng.Intn(len(keys))], base.SeekGEFlagsNone)
}
l.Close()
})
}
})
}
}
// A benchmark that simulates the behavior of a levelIter being used as part
// of a mergingIter where narrow bounds are repeatedly set and used to Seek
// and then iterate over the keys within the bounds. This resembles MVCC
// scanning by CockroachDB when doing a lookup/index join with a large number
// of left rows, that are batched and reuse the same iterator, and which can
// have good locality of access. This results in the successive bounds being
// in the same file.
func BenchmarkLevelIterSeqSeekGEWithBounds(b *testing.B) {
const blockSize = 32 << 10
for _, restartInterval := range []int{16} {
b.Run(fmt.Sprintf("restart=%d", restartInterval),
func(b *testing.B) {
for _, count := range []int{5} {
b.Run(fmt.Sprintf("count=%d", count),
func(b *testing.B) {
readers, metas, keys, cleanup :=
buildLevelIterTables(b, blockSize, restartInterval, count)
defer cleanup()
// This newIters is cheaper than in practice since it does not do
// tableCacheShard.findNode.
newIters := func(
_ context.Context, file *manifest.FileMetadata, opts *IterOptions, _ internalIterOpts, _ iterKinds,
) (iterSet, error) {
iter, err := readers[file.FileNum].NewIter(
sstable.NoTransforms,
opts.LowerBound, opts.UpperBound)
return iterSet{point: iter}, err
}
l := newLevelIter(context.Background(), IterOptions{}, DefaultComparer, newIters, metas.Iter(), manifest.Level(level), internalIterOpts{})
// Fake up the range deletion initialization, to resemble the usage
// in a mergingIter.
l.initRangeDel(func(rangeDelIter keyspan.FragmentIterator) {
if rangeDelIter != nil {
rangeDelIter.Close()
}
})
keyCount := len(keys)
b.ResetTimer()
for i := 0; i < b.N; i++ {
pos := i % (keyCount - 1)
l.SetBounds(keys[pos], keys[pos+1])
// SeekGE will return keys[pos].
kv := l.SeekGE(keys[pos], base.SeekGEFlagsNone)
// Next() will get called once and return nil.
for kv != nil {
kv = l.Next()
}
}
l.Close()
})
}
})
}
}
// BenchmarkLevelIterSeqSeekPrefixGE simulates the behavior of a levelIter
// being used as part of a mergingIter where SeekPrefixGE is used to seek in a
// monotonically increasing manner. This resembles key-value lookups done by
// CockroachDB when evaluating Put operations.
func BenchmarkLevelIterSeqSeekPrefixGE(b *testing.B) {
const blockSize = 32 << 10
const restartInterval = 16
readers, metas, keys, cleanup :=
buildLevelIterTables(b, blockSize, restartInterval, 5)
defer cleanup()
// This newIters is cheaper than in practice since it does not do
// tableCacheShard.findNode.
newIters := func(
_ context.Context, file *manifest.FileMetadata, opts *IterOptions, _ internalIterOpts, _ iterKinds,
) (iterSet, error) {
iter, err := readers[file.FileNum].NewIter(
sstable.NoTransforms,
opts.LowerBound, opts.UpperBound)
return iterSet{point: iter}, err
}
for _, skip := range []int{1, 2, 4, 8, 16} {
for _, useNext := range []bool{false, true} {
b.Run(fmt.Sprintf("skip=%d/use-next=%t", skip, useNext),
func(b *testing.B) {
l := newLevelIter(context.Background(), IterOptions{}, testkeys.Comparer, newIters, metas.Iter(), manifest.Level(level), internalIterOpts{})
// Fake up the range deletion initialization, to resemble the usage
// in a mergingIter.
l.initRangeDel(func(rangeDelIter keyspan.FragmentIterator) {
if rangeDelIter != nil {
rangeDelIter.Close()
}
})
keyCount := len(keys)
pos := 0
l.SeekPrefixGE(keys[pos], keys[pos], base.SeekGEFlagsNone)
b.ResetTimer()
for i := 0; i < b.N; i++ {
pos += skip
var flags base.SeekGEFlags
if useNext {
flags = flags.EnableTrySeekUsingNext()
}
if pos >= keyCount {
pos = 0
flags = flags.DisableTrySeekUsingNext()
}
// SeekPrefixGE will return keys[pos].
l.SeekPrefixGE(keys[pos], keys[pos], flags)
}
b.StopTimer()
l.Close()
})
}
}
}
func BenchmarkLevelIterNext(b *testing.B) {
const blockSize = 32 << 10
for _, restartInterval := range []int{16} {
b.Run(fmt.Sprintf("restart=%d", restartInterval),
func(b *testing.B) {
for _, count := range []int{5} {
b.Run(fmt.Sprintf("count=%d", count),
func(b *testing.B) {
readers, metas, _, cleanup := buildLevelIterTables(b, blockSize, restartInterval, count)
defer cleanup()
newIters := func(
_ context.Context, file *manifest.FileMetadata, _ *IterOptions, _ internalIterOpts, _ iterKinds,
) (iterSet, error) {
iter, err := readers[file.FileNum].NewIter(sstable.NoTransforms, nil /* lower */, nil /* upper */)
return iterSet{point: iter}, err
}
l := newLevelIter(context.Background(), IterOptions{}, testkeys.Comparer, newIters, metas.Iter(), manifest.Level(level), internalIterOpts{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
kv := l.Next()
if kv == nil {
kv = l.First()
}
_ = kv
}
l.Close()
})
}
})
}
}
func BenchmarkLevelIterPrev(b *testing.B) {
const blockSize = 32 << 10
for _, restartInterval := range []int{16} {
b.Run(fmt.Sprintf("restart=%d", restartInterval),
func(b *testing.B) {
for _, count := range []int{5} {
b.Run(fmt.Sprintf("count=%d", count),
func(b *testing.B) {
readers, metas, _, cleanup := buildLevelIterTables(b, blockSize, restartInterval, count)
defer cleanup()
newIters := func(
_ context.Context, file *manifest.FileMetadata, _ *IterOptions, _ internalIterOpts, _ iterKinds,
) (iterSet, error) {
iter, err := readers[file.FileNum].NewIter(sstable.NoTransforms, nil /* lower */, nil /* upper */)
return iterSet{point: iter}, err
}
l := newLevelIter(context.Background(), IterOptions{}, DefaultComparer, newIters, metas.Iter(), manifest.Level(level), internalIterOpts{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
kv := l.Prev()
if kv == nil {
kv = l.Last()
}
_ = kv
}
l.Close()
})
}
})
}
}