Line data Source code
1 : // Copyright 2018 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 rowblk
6 :
7 : import (
8 : "bytes"
9 : "context"
10 : "fmt"
11 : "os"
12 : "sync"
13 :
14 : "github.com/cockroachdb/pebble/internal/base"
15 : "github.com/cockroachdb/pebble/internal/invariants"
16 : "github.com/cockroachdb/pebble/internal/keyspan"
17 : "github.com/cockroachdb/pebble/internal/rangedel"
18 : "github.com/cockroachdb/pebble/internal/rangekey"
19 : "github.com/cockroachdb/pebble/internal/treeprinter"
20 : "github.com/cockroachdb/pebble/sstable/block"
21 : )
22 :
23 : // fragmentIter wraps an Iter, implementing the keyspan.FragmentIterator
24 : // interface. It's used for reading range deletion and range key blocks.
25 : //
26 : // Range deletions and range keys are fragmented before they're persisted to the
27 : // block. Overlapping fragments have identical bounds. The fragmentIter gathers
28 : // all the fragments with identical bounds within a block and returns a single
29 : // keyspan.Span describing all the keys defined over the span.
30 : //
31 : // # Memory lifetime
32 : //
33 : // A Span returned by fragmentIter is only guaranteed to be stable until the
34 : // next fragmentIter iteration positioning method. A Span's Keys slice may be
35 : // reused, so the user must not assume it's stable.
36 : //
37 : // Blocks holding range deletions and range keys are configured to use a restart
38 : // interval of 1. This provides key stability. The caller may treat the various
39 : // byte slices (start, end, suffix, value) as stable for the lifetime of the
40 : // iterator.
41 : type fragmentIter struct {
42 : suffixCmp base.CompareRangeSuffixes
43 : blockIter Iter
44 : keyBuf [2]keyspan.Key
45 : span keyspan.Span
46 : dir int8
47 :
48 : // fileNum is used for logging/debugging.
49 : fileNum base.DiskFileNum
50 :
51 : syntheticPrefixAndSuffix block.SyntheticPrefixAndSuffix
52 : // startKeyBuf is a buffer that is reused to store the start key of the span
53 : // when a synthetic prefix is used.
54 : startKeyBuf []byte
55 : // endKeyBuf is a buffer that is reused to generate the end key of the span
56 : // when a synthetic prefix is set. It always starts with syntheticPrefix.
57 : endKeyBuf []byte
58 :
59 : closeCheck invariants.CloseChecker
60 : }
61 :
62 : var _ keyspan.FragmentIterator = (*fragmentIter)(nil)
63 :
64 : var fragmentBlockIterPool = sync.Pool{
65 2 : New: func() interface{} {
66 2 : i := &fragmentIter{}
67 2 : if invariants.UseFinalizers {
68 2 : invariants.SetFinalizer(i, checkFragmentBlockIterator)
69 2 : }
70 2 : return i
71 : },
72 : }
73 :
74 : // NewFragmentIter returns a new keyspan iterator that iterates over a block's
75 : // spans.
76 : func NewFragmentIter(
77 : fileNum base.DiskFileNum,
78 : comparer *base.Comparer,
79 : blockHandle block.BufferHandle,
80 : transforms block.FragmentIterTransforms,
81 2 : ) (keyspan.FragmentIterator, error) {
82 2 : i := fragmentBlockIterPool.Get().(*fragmentIter)
83 2 :
84 2 : i.suffixCmp = comparer.CompareRangeSuffixes
85 2 : // Use the i.keyBuf array to back the Keys slice to prevent an allocation
86 2 : // when the spans contain few keys.
87 2 : i.span.Keys = i.keyBuf[:0]
88 2 : i.fileNum = fileNum
89 2 : i.syntheticPrefixAndSuffix = transforms.SyntheticPrefixAndSuffix
90 2 : if transforms.HasSyntheticPrefix() {
91 2 : i.endKeyBuf = append(i.endKeyBuf[:0], transforms.SyntheticPrefix()...)
92 2 : }
93 2 : i.closeCheck = invariants.CloseChecker{}
94 2 :
95 2 : if err := i.blockIter.InitHandle(comparer, blockHandle, block.IterTransforms{
96 2 : SyntheticSeqNum: transforms.SyntheticSeqNum,
97 2 : // We let the blockIter prepend the prefix to span start keys; the fragment
98 2 : // iterator will prepend it for end keys. We could do everything in the
99 2 : // fragment iterator, but we'd have to duplicate the logic for adjusting the
100 2 : // seek key for SeekGE/SeekLT.
101 2 : SyntheticPrefixAndSuffix: transforms.SyntheticPrefixAndSuffix.RemoveSuffix(),
102 2 : // It's okay for HideObsoletePoints to be false here, even for shared
103 2 : // ingested sstables. This is because rangedels do not apply to points in
104 2 : // the same sstable at the same sequence number anyway, so exposing obsolete
105 2 : // rangedels is harmless.
106 2 : HideObsoletePoints: false,
107 2 : }); err != nil {
108 0 : i.Close()
109 0 : return nil, err
110 0 : }
111 2 : return i, nil
112 : }
113 :
114 : // initSpan initializes the span with a single fragment.
115 : //
116 : // Note that the span start and end keys and range key contents are aliased to
117 : // the key or value when we don't have a synthetic prefix. This is ok because
118 : // the range del/key block doesn't use prefix compression, so the key/value will
119 : // be pointing directly into the buffer data.
120 2 : func (i *fragmentIter) initSpan(ik base.InternalKey, internalValue []byte) error {
121 2 : if ik.Kind() == base.InternalKeyKindRangeDelete {
122 2 : i.span = rangedel.Decode(ik, internalValue, i.span.Keys[:0])
123 2 : } else {
124 2 : var err error
125 2 : i.span, err = rangekey.Decode(ik, internalValue, i.span.Keys[:0])
126 2 : if err != nil {
127 0 : return err
128 0 : }
129 : }
130 : // When synthetic prefix is used in the blockIter, the keys cannot be used
131 : // across multiple blockIter operations; we have to make a copy in this case.
132 2 : if i.syntheticPrefixAndSuffix.HasPrefix() || invariants.Sometimes(10) {
133 2 : i.startKeyBuf = append(i.startKeyBuf[:0], i.span.Start...)
134 2 : i.span.Start = i.startKeyBuf
135 2 : }
136 2 : return nil
137 : }
138 :
139 : // addToSpan adds a fragment to the existing span. The fragment must be for the
140 : // same start/end keys.
141 : func (i *fragmentIter) addToSpan(
142 : cmp base.Compare, ik base.InternalKey, internalValue []byte,
143 2 : ) error {
144 2 : var err error
145 2 : if ik.Kind() == base.InternalKeyKindRangeDelete {
146 2 : err = rangedel.DecodeIntoSpan(cmp, ik, internalValue, &i.span)
147 2 : } else {
148 2 : err = rangekey.DecodeIntoSpan(cmp, ik, internalValue, &i.span)
149 2 : }
150 2 : return err
151 : }
152 :
153 : // applySpanTransforms applies changes to the span that we decoded, if
154 : // appropriate.
155 2 : func (i *fragmentIter) applySpanTransforms() error {
156 2 : if i.syntheticPrefixAndSuffix.HasPrefix() || invariants.Sometimes(10) {
157 2 : syntheticPrefix := i.syntheticPrefixAndSuffix.Prefix()
158 2 : // We have to make a copy of the start key because it will not stay valid
159 2 : // across multiple blockIter operations.
160 2 : i.startKeyBuf = append(i.startKeyBuf[:0], i.span.Start...)
161 2 : i.span.Start = i.startKeyBuf
162 2 : if invariants.Enabled && !bytes.Equal(syntheticPrefix, i.endKeyBuf[:len(syntheticPrefix)]) {
163 0 : panic("pebble: invariant violation: synthetic prefix mismatch")
164 : }
165 2 : i.endKeyBuf = append(i.endKeyBuf[:len(syntheticPrefix)], i.span.End...)
166 2 : i.span.End = i.endKeyBuf
167 : }
168 :
169 2 : if i.syntheticPrefixAndSuffix.HasSuffix() {
170 2 : syntheticSuffix := i.syntheticPrefixAndSuffix.Suffix()
171 2 : for keyIdx := range i.span.Keys {
172 2 : k := &i.span.Keys[keyIdx]
173 2 :
174 2 : switch k.Kind() {
175 2 : case base.InternalKeyKindRangeKeySet:
176 2 : if len(k.Suffix) > 0 {
177 2 : if invariants.Enabled && i.suffixCmp(syntheticSuffix, k.Suffix) >= 0 {
178 1 : return base.AssertionFailedf("synthetic suffix %q >= RangeKeySet suffix %q",
179 1 : syntheticSuffix, k.Suffix)
180 1 : }
181 2 : k.Suffix = syntheticSuffix
182 : }
183 1 : case base.InternalKeyKindRangeKeyDelete:
184 : // Nothing to do.
185 1 : default:
186 1 : return base.AssertionFailedf("synthetic suffix not supported with key kind %s", k.Kind())
187 : }
188 : }
189 : }
190 2 : return nil
191 : }
192 :
193 : // gatherForward gathers internal keys with identical bounds. Keys defined over
194 : // spans of the keyspace are fragmented such that any overlapping key spans have
195 : // identical bounds. When these spans are persisted to a range deletion or range
196 : // key block, they may be persisted as multiple internal keys in order to encode
197 : // multiple sequence numbers or key kinds.
198 : //
199 : // gatherForward iterates forward, re-combining the fragmented internal keys to
200 : // reconstruct a keyspan.Span that holds all the keys defined over the span.
201 2 : func (i *fragmentIter) gatherForward(kv *base.InternalKV) (*keyspan.Span, error) {
202 2 : i.span = keyspan.Span{}
203 2 : if kv == nil || !i.blockIter.Valid() {
204 2 : return nil, nil
205 2 : }
206 : // Use the i.keyBuf array to back the Keys slice to prevent an allocation
207 : // when a span contains few keys.
208 2 : i.span.Keys = i.keyBuf[:0]
209 2 :
210 2 : // Decode the span's end key and individual keys from the value.
211 2 : if err := i.initSpan(kv.K, kv.InPlaceValue()); err != nil {
212 0 : return nil, err
213 0 : }
214 :
215 : // There might exist additional internal keys with identical bounds encoded
216 : // within the block. Iterate forward, accumulating all the keys with
217 : // identical bounds to s.
218 :
219 : // Overlapping fragments are required to have exactly equal start and
220 : // end bounds.
221 2 : for kv = i.blockIter.Next(); kv != nil && i.blockIter.cmp(kv.K.UserKey, i.span.Start) == 0; kv = i.blockIter.Next() {
222 2 : if err := i.addToSpan(i.blockIter.cmp, kv.K, kv.InPlaceValue()); err != nil {
223 0 : return nil, err
224 0 : }
225 : }
226 2 : if err := i.applySpanTransforms(); err != nil {
227 0 : return nil, err
228 0 : }
229 :
230 : // Apply a consistent ordering.
231 2 : keyspan.SortKeysByTrailer(i.span.Keys)
232 2 :
233 2 : // i.blockIter is positioned over the first internal key for the next span.
234 2 : return &i.span, nil
235 : }
236 :
237 : // gatherBackward gathers internal keys with identical bounds. Keys defined over
238 : // spans of the keyspace are fragmented such that any overlapping key spans have
239 : // identical bounds. When these spans are persisted to a range deletion or range
240 : // key block, they may be persisted as multiple internal keys in order to encode
241 : // multiple sequence numbers or key kinds.
242 : //
243 : // gatherBackward iterates backwards, re-combining the fragmented internal keys
244 : // to reconstruct a keyspan.Span that holds all the keys defined over the span.
245 2 : func (i *fragmentIter) gatherBackward(kv *base.InternalKV) (*keyspan.Span, error) {
246 2 : i.span = keyspan.Span{}
247 2 : if kv == nil || !i.blockIter.Valid() {
248 2 : return nil, nil
249 2 : }
250 :
251 : // Decode the span's end key and individual keys from the value.
252 2 : if err := i.initSpan(kv.K, kv.InPlaceValue()); err != nil {
253 0 : return nil, err
254 0 : }
255 :
256 : // There might exist additional internal keys with identical bounds encoded
257 : // within the block. Iterate backward, accumulating all the keys with
258 : // identical bounds to s.
259 : //
260 : // Overlapping fragments are required to have exactly equal start and
261 : // end bounds.
262 2 : for kv = i.blockIter.Prev(); kv != nil && i.blockIter.cmp(kv.K.UserKey, i.span.Start) == 0; kv = i.blockIter.Prev() {
263 2 : if err := i.addToSpan(i.blockIter.cmp, kv.K, kv.InPlaceValue()); err != nil {
264 0 : return nil, err
265 0 : }
266 : }
267 : // i.blockIter is positioned over the last internal key for the previous
268 : // span.
269 :
270 : // Apply a consistent ordering.
271 2 : keyspan.SortKeysByTrailer(i.span.Keys)
272 2 :
273 2 : i.applySpanTransforms()
274 2 : return &i.span, nil
275 : }
276 :
277 : // SetContext is part of the FragmentIterator interface.
278 0 : func (i *fragmentIter) SetContext(ctx context.Context) {}
279 :
280 : // Close implements (keyspan.FragmentIterator).Close.
281 2 : func (i *fragmentIter) Close() {
282 2 : i.blockIter.Close()
283 2 : i.closeCheck.Close()
284 2 :
285 2 : if invariants.Sometimes(25) {
286 2 : // In invariants mode, sometimes don't add the object to the pool so that we
287 2 : // can check for double closes that take longer than the object stays in the
288 2 : // pool.
289 2 : return
290 2 : }
291 2 : i.span = keyspan.Span{}
292 2 : i.dir = 0
293 2 : i.fileNum = 0
294 2 : i.syntheticPrefixAndSuffix = block.SyntheticPrefixAndSuffix{}
295 2 : i.startKeyBuf = i.startKeyBuf[:0]
296 2 : i.endKeyBuf = i.endKeyBuf[:0]
297 2 : fragmentBlockIterPool.Put(i)
298 : }
299 :
300 : // First implements (keyspan.FragmentIterator).First
301 2 : func (i *fragmentIter) First() (*keyspan.Span, error) {
302 2 : i.dir = +1
303 2 : return i.gatherForward(i.blockIter.First())
304 2 : }
305 :
306 : // Last implements (keyspan.FragmentIterator).Last.
307 2 : func (i *fragmentIter) Last() (*keyspan.Span, error) {
308 2 : i.dir = -1
309 2 : return i.gatherBackward(i.blockIter.Last())
310 2 : }
311 :
312 : // Next implements (keyspan.FragmentIterator).Next.
313 2 : func (i *fragmentIter) Next() (*keyspan.Span, error) {
314 2 : switch {
315 2 : case i.dir == -1 && !i.span.Valid():
316 2 : // Switching directions.
317 2 : //
318 2 : // i.blockIter is exhausted, before the first key. Move onto the first.
319 2 : i.blockIter.First()
320 2 : i.dir = +1
321 2 : case i.dir == -1 && i.span.Valid():
322 2 : // Switching directions.
323 2 : //
324 2 : // i.blockIter is currently positioned over the last internal key for
325 2 : // the previous span. Next it once to move to the first internal key
326 2 : // that makes up the current span, and gatherForwaad to land on the
327 2 : // first internal key making up the next span.
328 2 : //
329 2 : // In the diagram below, if the last span returned to the user during
330 2 : // reverse iteration was [b,c), i.blockIter is currently positioned at
331 2 : // [a,b). The block iter must be positioned over [d,e) to gather the
332 2 : // next span's fragments.
333 2 : //
334 2 : // ... [a,b) [b,c) [b,c) [b,c) [d,e) ...
335 2 : // ^ ^
336 2 : // i.blockIter want
337 2 : if x, err := i.gatherForward(i.blockIter.Next()); err != nil {
338 0 : return nil, err
339 2 : } else if invariants.Enabled && !x.Valid() {
340 0 : panic("pebble: invariant violation: next entry unexpectedly invalid")
341 : }
342 2 : i.dir = +1
343 : }
344 : // We know that this blockIter has in-place values.
345 2 : return i.gatherForward(i.blockIter.KV())
346 : }
347 :
348 : // Prev implements (keyspan.FragmentIterator).Prev.
349 2 : func (i *fragmentIter) Prev() (*keyspan.Span, error) {
350 2 : switch {
351 2 : case i.dir == +1 && !i.span.Valid():
352 2 : // Switching directions.
353 2 : //
354 2 : // i.blockIter is exhausted, after the last key. Move onto the last.
355 2 : i.blockIter.Last()
356 2 : i.dir = -1
357 2 : case i.dir == +1 && i.span.Valid():
358 2 : // Switching directions.
359 2 : //
360 2 : // i.blockIter is currently positioned over the first internal key for
361 2 : // the next span. Prev it once to move to the last internal key that
362 2 : // makes up the current span, and gatherBackward to land on the last
363 2 : // internal key making up the previous span.
364 2 : //
365 2 : // In the diagram below, if the last span returned to the user during
366 2 : // forward iteration was [b,c), i.blockIter is currently positioned at
367 2 : // [d,e). The block iter must be positioned over [a,b) to gather the
368 2 : // previous span's fragments.
369 2 : //
370 2 : // ... [a,b) [b,c) [b,c) [b,c) [d,e) ...
371 2 : // ^ ^
372 2 : // want i.blockIter
373 2 : if x, err := i.gatherBackward(i.blockIter.Prev()); err != nil {
374 0 : return nil, err
375 2 : } else if invariants.Enabled && !x.Valid() {
376 0 : panic("pebble: invariant violation: previous entry unexpectedly invalid")
377 : }
378 2 : i.dir = -1
379 : }
380 : // We know that this blockIter has in-place values.
381 2 : return i.gatherBackward(i.blockIter.KV())
382 : }
383 :
384 : // SeekGE implements (keyspan.FragmentIterator).SeekGE.
385 2 : func (i *fragmentIter) SeekGE(k []byte) (*keyspan.Span, error) {
386 2 : if s, err := i.SeekLT(k); err != nil {
387 0 : return nil, err
388 2 : } else if s != nil && i.blockIter.cmp(k, s.End) < 0 {
389 2 : return s, nil
390 2 : }
391 : // TODO(jackson): If the above i.SeekLT(k) discovers a span but the span
392 : // doesn't meet the k < s.End comparison, then there's no need for the
393 : // SeekLT to gatherBackward.
394 2 : return i.Next()
395 : }
396 :
397 : // SeekLT implements (keyspan.FragmentIterator).SeekLT.
398 2 : func (i *fragmentIter) SeekLT(k []byte) (*keyspan.Span, error) {
399 2 : i.dir = -1
400 2 : return i.gatherBackward(i.blockIter.SeekLT(k, base.SeekLTFlagsNone))
401 2 : }
402 :
403 : // String implements fmt.Stringer.
404 0 : func (i *fragmentIter) String() string {
405 0 : return "fragment-block-iter"
406 0 : }
407 :
408 : // WrapChildren implements FragmentIterator.
409 0 : func (i *fragmentIter) WrapChildren(wrap keyspan.WrapFn) {}
410 :
411 : // DebugTree is part of the FragmentIterator interface.
412 0 : func (i *fragmentIter) DebugTree(tp treeprinter.Node) {
413 0 : tp.Childf("%T(%p) fileNum=%s", i, i, i.fileNum)
414 0 : }
415 :
416 2 : func checkFragmentBlockIterator(obj interface{}) {
417 2 : i := obj.(*fragmentIter)
418 2 : if h := i.blockIter.Handle(); h.Valid() {
419 0 : fmt.Fprintf(os.Stderr, "fragmentBlockIter.blockIter.handle is not nil: %#v\n", h)
420 0 : os.Exit(1)
421 0 : }
422 : }
|