Line data Source code
1 : // Copyright 2022 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2 : // of this source code is governed by a BSD-style license that can be found in
3 : // the LICENSE file.
4 :
5 : package pebble
6 :
7 : import (
8 : "context"
9 : "fmt"
10 : "sort"
11 :
12 : "github.com/cockroachdb/errors"
13 : "github.com/cockroachdb/pebble/internal/base"
14 : "github.com/cockroachdb/pebble/internal/keyspan"
15 : "github.com/cockroachdb/pebble/internal/manifest"
16 : "github.com/cockroachdb/pebble/sstable"
17 : )
18 :
19 : // ExternalIterOption provide an interface to specify open-time options to
20 : // NewExternalIter.
21 : type ExternalIterOption interface {
22 : // iterApply is called on the iterator during opening in order to set internal
23 : // parameters.
24 : iterApply(*Iterator)
25 : // readerOptions returns any reader options added by this iter option.
26 : readerOptions() []sstable.ReaderOption
27 : }
28 :
29 : type externalIterReaderOptions struct {
30 : opts []sstable.ReaderOption
31 : }
32 :
33 0 : func (e *externalIterReaderOptions) iterApply(iterator *Iterator) {
34 0 : // Do nothing.
35 0 : }
36 :
37 0 : func (e *externalIterReaderOptions) readerOptions() []sstable.ReaderOption {
38 0 : return e.opts
39 0 : }
40 :
41 : // ExternalIterReaderOptions returns an ExternalIterOption that specifies
42 : // sstable.ReaderOptions to be applied on sstable readers in NewExternalIter.
43 0 : func ExternalIterReaderOptions(opts ...sstable.ReaderOption) ExternalIterOption {
44 0 : return &externalIterReaderOptions{opts: opts}
45 0 : }
46 :
47 : // NewExternalIter takes an input 2d array of sstable files which may overlap
48 : // across subarrays but not within a subarray (at least as far as points are
49 : // concerned; range keys are allowed to overlap arbitrarily even within a
50 : // subarray), and returns an Iterator over the merged contents of the sstables.
51 : // Input sstables may contain point keys, range keys, range deletions, etc. The
52 : // input files slice must be sorted in reverse chronological ordering. A key in a
53 : // file at a lower index subarray will shadow a key with an identical user key
54 : // contained within a file at a higher index subarray. Each subarray must be
55 : // sorted in internal key order, where lower index files contain keys that sort
56 : // left of files with higher indexes.
57 : //
58 : // Input sstables must only contain keys with the zero sequence number.
59 : //
60 : // Iterators constructed through NewExternalIter do not support all iterator
61 : // options, including block-property and table filters. NewExternalIter errors
62 : // if an incompatible option is set.
63 : func NewExternalIter(
64 : o *Options,
65 : iterOpts *IterOptions,
66 : files [][]sstable.ReadableFile,
67 : extraOpts ...ExternalIterOption,
68 0 : ) (it *Iterator, err error) {
69 0 : return NewExternalIterWithContext(context.Background(), o, iterOpts, files, extraOpts...)
70 0 : }
71 :
72 : // NewExternalIterWithContext is like NewExternalIter, and additionally
73 : // accepts a context for tracing.
74 : func NewExternalIterWithContext(
75 : ctx context.Context,
76 : o *Options,
77 : iterOpts *IterOptions,
78 : files [][]sstable.ReadableFile,
79 : extraOpts ...ExternalIterOption,
80 0 : ) (it *Iterator, err error) {
81 0 : if iterOpts != nil {
82 0 : if err := validateExternalIterOpts(iterOpts); err != nil {
83 0 : return nil, err
84 0 : }
85 : }
86 :
87 0 : var readers [][]*sstable.Reader
88 0 :
89 0 : seqNumOffset := 0
90 0 : var extraReaderOpts []sstable.ReaderOption
91 0 : for i := range extraOpts {
92 0 : extraReaderOpts = append(extraReaderOpts, extraOpts[i].readerOptions()...)
93 0 : }
94 0 : for _, levelFiles := range files {
95 0 : seqNumOffset += len(levelFiles)
96 0 : }
97 0 : for _, levelFiles := range files {
98 0 : seqNumOffset -= len(levelFiles)
99 0 : subReaders, err := openExternalTables(o, levelFiles, seqNumOffset, o.MakeReaderOptions(), extraReaderOpts...)
100 0 : readers = append(readers, subReaders)
101 0 : if err != nil {
102 0 : // Close all the opened readers.
103 0 : for i := range readers {
104 0 : for j := range readers[i] {
105 0 : _ = readers[i][j].Close()
106 0 : }
107 : }
108 0 : return nil, err
109 : }
110 : }
111 :
112 0 : buf := iterAllocPool.Get().(*iterAlloc)
113 0 : dbi := &buf.dbi
114 0 : *dbi = Iterator{
115 0 : ctx: ctx,
116 0 : alloc: buf,
117 0 : merge: o.Merger.Merge,
118 0 : comparer: *o.Comparer,
119 0 : readState: nil,
120 0 : keyBuf: buf.keyBuf,
121 0 : prefixOrFullSeekKey: buf.prefixOrFullSeekKey,
122 0 : boundsBuf: buf.boundsBuf,
123 0 : batch: nil,
124 0 : // Add the readers to the Iterator so that Close closes them, and
125 0 : // SetOptions can re-construct iterators from them.
126 0 : externalReaders: readers,
127 0 : newIters: func(context.Context, *manifest.FileMetadata, *IterOptions,
128 0 : internalIterOpts, iterKinds) (iterSet, error) {
129 0 : // NB: External iterators are currently constructed without any
130 0 : // `levelIters`. newIters should never be called. When we support
131 0 : // organizing multiple non-overlapping files into a single level
132 0 : // (see TODO below), we'll need to adjust this tableNewIters
133 0 : // implementation to open iterators by looking up f in a map
134 0 : // of readers indexed by *fileMetadata.
135 0 : panic("unreachable")
136 : },
137 : seqNum: base.InternalKeySeqNumMax,
138 : }
139 0 : if iterOpts != nil {
140 0 : dbi.opts = *iterOpts
141 0 : dbi.processBounds(iterOpts.LowerBound, iterOpts.UpperBound)
142 0 : }
143 0 : for i := range extraOpts {
144 0 : extraOpts[i].iterApply(dbi)
145 0 : }
146 0 : if err := finishInitializingExternal(ctx, dbi); err != nil {
147 0 : dbi.Close()
148 0 : return nil, err
149 0 : }
150 0 : return dbi, nil
151 : }
152 :
153 0 : func validateExternalIterOpts(iterOpts *IterOptions) error {
154 0 : switch {
155 0 : case iterOpts.TableFilter != nil:
156 0 : return errors.Errorf("pebble: external iterator: TableFilter unsupported")
157 0 : case iterOpts.PointKeyFilters != nil:
158 0 : return errors.Errorf("pebble: external iterator: PointKeyFilters unsupported")
159 0 : case iterOpts.RangeKeyFilters != nil:
160 0 : return errors.Errorf("pebble: external iterator: RangeKeyFilters unsupported")
161 0 : case iterOpts.OnlyReadGuaranteedDurable:
162 0 : return errors.Errorf("pebble: external iterator: OnlyReadGuaranteedDurable unsupported")
163 0 : case iterOpts.UseL6Filters:
164 0 : return errors.Errorf("pebble: external iterator: UseL6Filters unsupported")
165 : }
166 0 : return nil
167 : }
168 :
169 0 : func createExternalPointIter(ctx context.Context, it *Iterator) (topLevelIterator, error) {
170 0 : // TODO(jackson): In some instances we could generate fewer levels by using
171 0 : // L0Sublevels code to organize nonoverlapping files into the same level.
172 0 : // This would allow us to use levelIters and keep a smaller set of data and
173 0 : // files in-memory. However, it would also require us to identify the bounds
174 0 : // of all the files upfront.
175 0 :
176 0 : if !it.opts.pointKeys() {
177 0 : return emptyIter, nil
178 0 : } else if it.pointIter != nil {
179 0 : return it.pointIter, nil
180 0 : }
181 0 : mlevels := it.alloc.mlevels[:0]
182 0 :
183 0 : if len(it.externalReaders) > cap(mlevels) {
184 0 : mlevels = make([]mergingIterLevel, 0, len(it.externalReaders))
185 0 : }
186 : // We set a synthetic sequence number, with lower levels having higer numbers.
187 0 : seqNum := 0
188 0 : for _, readers := range it.externalReaders {
189 0 : seqNum += len(readers)
190 0 : }
191 0 : for _, readers := range it.externalReaders {
192 0 : var combinedIters []internalIterator
193 0 : for _, r := range readers {
194 0 : var (
195 0 : rangeDelIter keyspan.FragmentIterator
196 0 : pointIter internalIterator
197 0 : err error
198 0 : )
199 0 : // We could set hideObsoletePoints=true, since we are reading at
200 0 : // InternalKeySeqNumMax, but we don't bother since these sstables should
201 0 : // not have obsolete points (so the performance optimization is
202 0 : // unnecessary), and we don't want to bother constructing a
203 0 : // BlockPropertiesFilterer that includes obsoleteKeyBlockPropertyFilter.
204 0 : transforms := sstable.IterTransforms{SyntheticSeqNum: sstable.SyntheticSeqNum(seqNum)}
205 0 : seqNum--
206 0 : pointIter, err = r.NewIterWithBlockPropertyFiltersAndContextEtc(
207 0 : ctx, transforms, it.opts.LowerBound, it.opts.UpperBound, nil, /* BlockPropertiesFilterer */
208 0 : false, /* useFilterBlock */
209 0 : &it.stats.InternalStats, it.opts.CategoryAndQoS, nil,
210 0 : sstable.TrivialReaderProvider{Reader: r})
211 0 : if err != nil {
212 0 : return nil, err
213 0 : }
214 0 : rangeDelIter, err = r.NewRawRangeDelIter(transforms)
215 0 : if err != nil {
216 0 : return nil, err
217 0 : }
218 0 : mlevels = append(mlevels, mergingIterLevel{
219 0 : iter: pointIter,
220 0 : rangeDelIter: rangeDelIter,
221 0 : })
222 : }
223 0 : if len(combinedIters) == 1 {
224 0 : mlevels = append(mlevels, mergingIterLevel{
225 0 : iter: combinedIters[0],
226 0 : })
227 0 : } else if len(combinedIters) > 1 {
228 0 : sli := &simpleLevelIter{
229 0 : cmp: it.cmp,
230 0 : iters: combinedIters,
231 0 : }
232 0 : sli.init(it.opts)
233 0 : mlevels = append(mlevels, mergingIterLevel{
234 0 : iter: sli,
235 0 : rangeDelIter: nil,
236 0 : })
237 0 : }
238 : }
239 :
240 0 : it.alloc.merging.init(&it.opts, &it.stats.InternalStats, it.comparer.Compare, it.comparer.Split, mlevels...)
241 0 : it.alloc.merging.snapshot = base.InternalKeySeqNumMax
242 0 : if len(mlevels) <= cap(it.alloc.levelsPositioned) {
243 0 : it.alloc.merging.levelsPositioned = it.alloc.levelsPositioned[:len(mlevels)]
244 0 : }
245 0 : return &it.alloc.merging, nil
246 : }
247 :
248 0 : func finishInitializingExternal(ctx context.Context, it *Iterator) error {
249 0 : pointIter, err := createExternalPointIter(ctx, it)
250 0 : if err != nil {
251 0 : return err
252 0 : }
253 0 : it.pointIter = pointIter
254 0 : it.iter = it.pointIter
255 0 :
256 0 : if it.opts.rangeKeys() {
257 0 : it.rangeKeyMasking.init(it, it.comparer.Compare, it.comparer.Split)
258 0 : var rangeKeyIters []keyspan.FragmentIterator
259 0 : if it.rangeKey == nil {
260 0 : // We could take advantage of the lack of overlaps in range keys within
261 0 : // each slice in it.externalReaders, and generate keyspanimpl.LevelIters
262 0 : // out of those. However, since range keys are expected to be sparse to
263 0 : // begin with, the performance gain might not be significant enough to
264 0 : // warrant it.
265 0 : //
266 0 : // TODO(bilal): Explore adding a simpleRangeKeyLevelIter that does not
267 0 : // operate on FileMetadatas (similar to simpleLevelIter), and implements
268 0 : // this optimization.
269 0 : // We set a synthetic sequence number, with lower levels having higer numbers.
270 0 : seqNum := 0
271 0 : for _, readers := range it.externalReaders {
272 0 : seqNum += len(readers)
273 0 : }
274 0 : for _, readers := range it.externalReaders {
275 0 : for _, r := range readers {
276 0 : transforms := sstable.IterTransforms{SyntheticSeqNum: sstable.SyntheticSeqNum(seqNum)}
277 0 : seqNum--
278 0 : if rki, err := r.NewRawRangeKeyIter(transforms); err != nil {
279 0 : return err
280 0 : } else if rki != nil {
281 0 : rangeKeyIters = append(rangeKeyIters, rki)
282 0 : }
283 : }
284 : }
285 0 : if len(rangeKeyIters) > 0 {
286 0 : it.rangeKey = iterRangeKeyStateAllocPool.Get().(*iteratorRangeKeyState)
287 0 : it.rangeKey.init(it.comparer.Compare, it.comparer.Split, &it.opts)
288 0 : it.rangeKey.rangeKeyIter = it.rangeKey.iterConfig.Init(
289 0 : &it.comparer,
290 0 : base.InternalKeySeqNumMax,
291 0 : it.opts.LowerBound, it.opts.UpperBound,
292 0 : &it.hasPrefix, &it.prefixOrFullSeekKey,
293 0 : false /* internalKeys */, &it.rangeKey.internal,
294 0 : )
295 0 : for i := range rangeKeyIters {
296 0 : it.rangeKey.iterConfig.AddLevel(rangeKeyIters[i])
297 0 : }
298 : }
299 : }
300 0 : if it.rangeKey != nil {
301 0 : it.rangeKey.iiter.Init(&it.comparer, it.iter, it.rangeKey.rangeKeyIter,
302 0 : keyspan.InterleavingIterOpts{
303 0 : Mask: &it.rangeKeyMasking,
304 0 : LowerBound: it.opts.LowerBound,
305 0 : UpperBound: it.opts.UpperBound,
306 0 : })
307 0 : it.iter = &it.rangeKey.iiter
308 0 : }
309 : }
310 0 : return nil
311 : }
312 :
313 : func openExternalTables(
314 : o *Options,
315 : files []sstable.ReadableFile,
316 : seqNumOffset int,
317 : readerOpts sstable.ReaderOptions,
318 : extraReaderOpts ...sstable.ReaderOption,
319 0 : ) (readers []*sstable.Reader, err error) {
320 0 : readers = make([]*sstable.Reader, 0, len(files))
321 0 : for i := range files {
322 0 : readable, err := sstable.NewSimpleReadable(files[i])
323 0 : if err != nil {
324 0 : return readers, err
325 0 : }
326 0 : r, err := sstable.NewReader(readable, readerOpts, extraReaderOpts...)
327 0 : if err != nil {
328 0 : return readers, err
329 0 : }
330 0 : readers = append(readers, r)
331 : }
332 0 : return readers, err
333 : }
334 :
335 : // simpleLevelIter is similar to a levelIter in that it merges the points
336 : // from multiple point iterators that are non-overlapping in the key ranges
337 : // they return. It is only expected to support forward iteration and forward
338 : // regular seeking; reverse iteration and prefix seeking is not supported.
339 : // Intended to be a low-overhead, non-FileMetadata dependent option for
340 : // NewExternalIter. To optimize seeking and forward iteration, it maintains
341 : // two slices of child iterators; one of all iterators, and a subset of it that
342 : // contains just the iterators that contain point keys within the current
343 : // bounds.
344 : //
345 : // Note that this levelIter does not support pausing at file boundaries
346 : // in case of range tombstones in this file that could apply to points outside
347 : // of this file (and outside of this level). This is sufficient for optimizing
348 : // the main use cases of NewExternalIter, however for completeness it would make
349 : // sense to build this pausing functionality in.
350 : type simpleLevelIter struct {
351 : cmp Compare
352 : err error
353 : lowerBound []byte
354 : iters []internalIterator
355 : filtered []internalIterator
356 : firstKeys [][]byte
357 : firstKeysBuf []byte
358 : currentIdx int
359 : }
360 :
361 : var _ internalIterator = &simpleLevelIter{}
362 :
363 : // init initializes this simpleLevelIter.
364 0 : func (s *simpleLevelIter) init(opts IterOptions) {
365 0 : s.currentIdx = 0
366 0 : s.lowerBound = opts.LowerBound
367 0 : s.resetFilteredIters()
368 0 : }
369 :
370 0 : func (s *simpleLevelIter) resetFilteredIters() {
371 0 : s.filtered = s.filtered[:0]
372 0 : s.firstKeys = s.firstKeys[:0]
373 0 : s.firstKeysBuf = s.firstKeysBuf[:0]
374 0 : s.err = nil
375 0 : for i := range s.iters {
376 0 : var iterKey *base.InternalKey
377 0 : if s.lowerBound != nil {
378 0 : iterKey, _ = s.iters[i].SeekGE(s.lowerBound, base.SeekGEFlagsNone)
379 0 : } else {
380 0 : iterKey, _ = s.iters[i].First()
381 0 : }
382 0 : if iterKey != nil {
383 0 : s.filtered = append(s.filtered, s.iters[i])
384 0 : bufStart := len(s.firstKeysBuf)
385 0 : s.firstKeysBuf = append(s.firstKeysBuf, iterKey.UserKey...)
386 0 : s.firstKeys = append(s.firstKeys, s.firstKeysBuf[bufStart:bufStart+len(iterKey.UserKey)])
387 0 : } else if err := s.iters[i].Error(); err != nil {
388 0 : s.err = err
389 0 : }
390 : }
391 : }
392 :
393 : func (s *simpleLevelIter) SeekGE(
394 : key []byte, flags base.SeekGEFlags,
395 0 : ) (*base.InternalKey, base.LazyValue) {
396 0 : if s.err != nil {
397 0 : return nil, base.LazyValue{}
398 0 : }
399 : // Find the first file that is entirely >= key. The file before that could
400 : // contain the key we're looking for.
401 0 : n := sort.Search(len(s.firstKeys), func(i int) bool {
402 0 : return s.cmp(key, s.firstKeys[i]) <= 0
403 0 : })
404 0 : if n > 0 {
405 0 : s.currentIdx = n - 1
406 0 : } else {
407 0 : s.currentIdx = n
408 0 : }
409 0 : if s.currentIdx < len(s.filtered) {
410 0 : if iterKey, val := s.filtered[s.currentIdx].SeekGE(key, flags); iterKey != nil {
411 0 : return iterKey, val
412 0 : }
413 0 : if err := s.filtered[s.currentIdx].Error(); err != nil {
414 0 : s.err = err
415 0 : }
416 0 : s.currentIdx++
417 : }
418 0 : return s.skipEmptyFileForward(key, flags)
419 : }
420 :
421 : func (s *simpleLevelIter) skipEmptyFileForward(
422 : seekKey []byte, flags base.SeekGEFlags,
423 0 : ) (*base.InternalKey, base.LazyValue) {
424 0 : var iterKey *base.InternalKey
425 0 : var val base.LazyValue
426 0 : for s.currentIdx >= 0 && s.currentIdx < len(s.filtered) && s.err == nil {
427 0 : if seekKey != nil {
428 0 : iterKey, val = s.filtered[s.currentIdx].SeekGE(seekKey, flags)
429 0 : } else if s.lowerBound != nil {
430 0 : iterKey, val = s.filtered[s.currentIdx].SeekGE(s.lowerBound, flags)
431 0 : } else {
432 0 : iterKey, val = s.filtered[s.currentIdx].First()
433 0 : }
434 0 : if iterKey != nil {
435 0 : return iterKey, val
436 0 : }
437 0 : if err := s.filtered[s.currentIdx].Error(); err != nil {
438 0 : s.err = err
439 0 : }
440 0 : s.currentIdx++
441 : }
442 0 : return nil, base.LazyValue{}
443 : }
444 :
445 : func (s *simpleLevelIter) SeekPrefixGE(
446 : prefix, key []byte, flags base.SeekGEFlags,
447 0 : ) (*base.InternalKey, base.LazyValue) {
448 0 : panic("unimplemented")
449 : }
450 :
451 : func (s *simpleLevelIter) SeekLT(
452 : key []byte, flags base.SeekLTFlags,
453 0 : ) (*base.InternalKey, base.LazyValue) {
454 0 : panic("unimplemented")
455 : }
456 :
457 0 : func (s *simpleLevelIter) First() (*base.InternalKey, base.LazyValue) {
458 0 : if s.err != nil {
459 0 : return nil, base.LazyValue{}
460 0 : }
461 0 : s.currentIdx = 0
462 0 : return s.skipEmptyFileForward(nil /* seekKey */, base.SeekGEFlagsNone)
463 : }
464 :
465 0 : func (s *simpleLevelIter) Last() (*base.InternalKey, base.LazyValue) {
466 0 : panic("unimplemented")
467 : }
468 :
469 0 : func (s *simpleLevelIter) Next() (*base.InternalKey, base.LazyValue) {
470 0 : if s.err != nil {
471 0 : return nil, base.LazyValue{}
472 0 : }
473 0 : if s.currentIdx < 0 || s.currentIdx >= len(s.filtered) {
474 0 : return nil, base.LazyValue{}
475 0 : }
476 0 : if iterKey, val := s.filtered[s.currentIdx].Next(); iterKey != nil {
477 0 : return iterKey, val
478 0 : }
479 0 : s.currentIdx++
480 0 : return s.skipEmptyFileForward(nil /* seekKey */, base.SeekGEFlagsNone)
481 : }
482 :
483 0 : func (s *simpleLevelIter) NextPrefix(succKey []byte) (*base.InternalKey, base.LazyValue) {
484 0 : if s.err != nil {
485 0 : return nil, base.LazyValue{}
486 0 : }
487 0 : if s.currentIdx < 0 || s.currentIdx >= len(s.filtered) {
488 0 : return nil, base.LazyValue{}
489 0 : }
490 0 : if iterKey, val := s.filtered[s.currentIdx].NextPrefix(succKey); iterKey != nil {
491 0 : return iterKey, val
492 0 : }
493 0 : s.currentIdx++
494 0 : return s.skipEmptyFileForward(succKey /* seekKey */, base.SeekGEFlagsNone)
495 : }
496 :
497 0 : func (s *simpleLevelIter) Prev() (*base.InternalKey, base.LazyValue) {
498 0 : panic("unimplemented")
499 : }
500 :
501 0 : func (s *simpleLevelIter) Error() error {
502 0 : if s.currentIdx >= 0 && s.currentIdx < len(s.filtered) {
503 0 : s.err = firstError(s.err, s.filtered[s.currentIdx].Error())
504 0 : }
505 0 : return s.err
506 : }
507 :
508 0 : func (s *simpleLevelIter) Close() error {
509 0 : var err error
510 0 : for i := range s.iters {
511 0 : err = firstError(err, s.iters[i].Close())
512 0 : }
513 0 : return err
514 : }
515 :
516 0 : func (s *simpleLevelIter) SetBounds(lower, upper []byte) {
517 0 : s.currentIdx = -1
518 0 : s.lowerBound = lower
519 0 : for i := range s.iters {
520 0 : s.iters[i].SetBounds(lower, upper)
521 0 : }
522 0 : s.resetFilteredIters()
523 : }
524 :
525 0 : func (s *simpleLevelIter) SetContext(_ context.Context) {}
526 :
527 0 : func (s *simpleLevelIter) String() string {
528 0 : if s.currentIdx < 0 || s.currentIdx >= len(s.filtered) {
529 0 : return "simpleLevelIter: current=<nil>"
530 0 : }
531 0 : return fmt.Sprintf("simpleLevelIter: current=%s", s.filtered[s.currentIdx])
532 : }
533 :
534 : var _ internalIterator = &simpleLevelIter{}
|