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 1 : New: func() interface{} {
66 1 : i := &fragmentIter{}
67 1 : if invariants.UseFinalizers {
68 1 : invariants.SetFinalizer(i, checkFragmentBlockIterator)
69 1 : }
70 1 : 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 1 : ) (keyspan.FragmentIterator, error) {
82 1 : i := fragmentBlockIterPool.Get().(*fragmentIter)
83 1 :
84 1 : i.suffixCmp = comparer.CompareRangeSuffixes
85 1 : // Use the i.keyBuf array to back the Keys slice to prevent an allocation
86 1 : // when the spans contain few keys.
87 1 : i.span.Keys = i.keyBuf[:0]
88 1 : i.fileNum = fileNum
89 1 : i.syntheticPrefixAndSuffix = transforms.SyntheticPrefixAndSuffix
90 1 : if transforms.HasSyntheticPrefix() {
91 1 : i.endKeyBuf = append(i.endKeyBuf[:0], transforms.SyntheticPrefix()...)
92 1 : }
93 1 : i.closeCheck = invariants.CloseChecker{}
94 1 :
95 1 : if err := i.blockIter.InitHandle(comparer, blockHandle, block.IterTransforms{
96 1 : SyntheticSeqNum: transforms.SyntheticSeqNum,
97 1 : // We let the blockIter prepend the prefix to span start keys; the fragment
98 1 : // iterator will prepend it for end keys. We could do everything in the
99 1 : // fragment iterator, but we'd have to duplicate the logic for adjusting the
100 1 : // seek key for SeekGE/SeekLT.
101 1 : SyntheticPrefixAndSuffix: transforms.SyntheticPrefixAndSuffix.RemoveSuffix(),
102 1 : // It's okay for HideObsoletePoints to be false here, even for shared
103 1 : // ingested sstables. This is because rangedels do not apply to points in
104 1 : // the same sstable at the same sequence number anyway, so exposing obsolete
105 1 : // rangedels is harmless.
106 1 : HideObsoletePoints: false,
107 1 : }); err != nil {
108 0 : i.Close()
109 0 : return nil, err
110 0 : }
111 1 : 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 1 : func (i *fragmentIter) initSpan(ik base.InternalKey, internalValue []byte) error {
121 1 : if ik.Kind() == base.InternalKeyKindRangeDelete {
122 1 : i.span = rangedel.Decode(ik, internalValue, i.span.Keys[:0])
123 1 : } else {
124 1 : var err error
125 1 : i.span, err = rangekey.Decode(ik, internalValue, i.span.Keys[:0])
126 1 : 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 1 : if i.syntheticPrefixAndSuffix.HasPrefix() || invariants.Sometimes(10) {
133 1 : i.startKeyBuf = append(i.startKeyBuf[:0], i.span.Start...)
134 1 : i.span.Start = i.startKeyBuf
135 1 : }
136 1 : 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 1 : ) error {
144 1 : var err error
145 1 : if ik.Kind() == base.InternalKeyKindRangeDelete {
146 1 : err = rangedel.DecodeIntoSpan(cmp, ik, internalValue, &i.span)
147 1 : } else {
148 1 : err = rangekey.DecodeIntoSpan(cmp, ik, internalValue, &i.span)
149 1 : }
150 1 : return err
151 : }
152 :
153 : // applySpanTransforms applies changes to the span that we decoded, if
154 : // appropriate.
155 1 : func (i *fragmentIter) applySpanTransforms() error {
156 1 : if i.syntheticPrefixAndSuffix.HasPrefix() || invariants.Sometimes(10) {
157 1 : syntheticPrefix := i.syntheticPrefixAndSuffix.Prefix()
158 1 : // We have to make a copy of the start key because it will not stay valid
159 1 : // across multiple blockIter operations.
160 1 : i.startKeyBuf = append(i.startKeyBuf[:0], i.span.Start...)
161 1 : i.span.Start = i.startKeyBuf
162 1 : if invariants.Enabled && !bytes.Equal(syntheticPrefix, i.endKeyBuf[:len(syntheticPrefix)]) {
163 0 : panic("pebble: invariant violation: synthetic prefix mismatch")
164 : }
165 1 : i.endKeyBuf = append(i.endKeyBuf[:len(syntheticPrefix)], i.span.End...)
166 1 : i.span.End = i.endKeyBuf
167 : }
168 :
169 1 : if i.syntheticPrefixAndSuffix.HasSuffix() {
170 1 : syntheticSuffix := i.syntheticPrefixAndSuffix.Suffix()
171 1 : for keyIdx := range i.span.Keys {
172 1 : k := &i.span.Keys[keyIdx]
173 1 :
174 1 : switch k.Kind() {
175 1 : case base.InternalKeyKindRangeKeySet:
176 1 : if len(k.Suffix) > 0 {
177 0 : if invariants.Enabled && i.suffixCmp(syntheticSuffix, k.Suffix) >= 0 {
178 0 : return base.AssertionFailedf("synthetic suffix %q >= RangeKeySet suffix %q",
179 0 : syntheticSuffix, k.Suffix)
180 0 : }
181 0 : k.Suffix = syntheticSuffix
182 : }
183 0 : case base.InternalKeyKindRangeKeyDelete:
184 : // Nothing to do.
185 0 : default:
186 0 : return base.AssertionFailedf("synthetic suffix not supported with key kind %s", k.Kind())
187 : }
188 : }
189 : }
190 1 : 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 1 : func (i *fragmentIter) gatherForward(kv *base.InternalKV) (*keyspan.Span, error) {
202 1 : i.span = keyspan.Span{}
203 1 : if kv == nil || !i.blockIter.Valid() {
204 1 : return nil, nil
205 1 : }
206 : // Use the i.keyBuf array to back the Keys slice to prevent an allocation
207 : // when a span contains few keys.
208 1 : i.span.Keys = i.keyBuf[:0]
209 1 :
210 1 : // Decode the span's end key and individual keys from the value.
211 1 : 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 1 : for kv = i.blockIter.Next(); kv != nil && i.blockIter.cmp(kv.K.UserKey, i.span.Start) == 0; kv = i.blockIter.Next() {
222 1 : if err := i.addToSpan(i.blockIter.cmp, kv.K, kv.InPlaceValue()); err != nil {
223 0 : return nil, err
224 0 : }
225 : }
226 1 : if err := i.applySpanTransforms(); err != nil {
227 0 : return nil, err
228 0 : }
229 :
230 : // Apply a consistent ordering.
231 1 : keyspan.SortKeysByTrailer(i.span.Keys)
232 1 :
233 1 : // i.blockIter is positioned over the first internal key for the next span.
234 1 : 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 1 : func (i *fragmentIter) gatherBackward(kv *base.InternalKV) (*keyspan.Span, error) {
246 1 : i.span = keyspan.Span{}
247 1 : if kv == nil || !i.blockIter.Valid() {
248 1 : return nil, nil
249 1 : }
250 :
251 : // Decode the span's end key and individual keys from the value.
252 1 : 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 1 : for kv = i.blockIter.Prev(); kv != nil && i.blockIter.cmp(kv.K.UserKey, i.span.Start) == 0; kv = i.blockIter.Prev() {
263 1 : 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 1 : keyspan.SortKeysByTrailer(i.span.Keys)
272 1 :
273 1 : i.applySpanTransforms()
274 1 : 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 1 : func (i *fragmentIter) Close() {
282 1 : i.blockIter.Close()
283 1 : i.closeCheck.Close()
284 1 :
285 1 : if invariants.Sometimes(25) {
286 1 : // In invariants mode, sometimes don't add the object to the pool so that we
287 1 : // can check for double closes that take longer than the object stays in the
288 1 : // pool.
289 1 : return
290 1 : }
291 1 : i.span = keyspan.Span{}
292 1 : i.dir = 0
293 1 : i.fileNum = 0
294 1 : i.syntheticPrefixAndSuffix = block.SyntheticPrefixAndSuffix{}
295 1 : i.startKeyBuf = i.startKeyBuf[:0]
296 1 : i.endKeyBuf = i.endKeyBuf[:0]
297 1 : fragmentBlockIterPool.Put(i)
298 : }
299 :
300 : // First implements (keyspan.FragmentIterator).First
301 1 : func (i *fragmentIter) First() (*keyspan.Span, error) {
302 1 : i.dir = +1
303 1 : return i.gatherForward(i.blockIter.First())
304 1 : }
305 :
306 : // Last implements (keyspan.FragmentIterator).Last.
307 1 : func (i *fragmentIter) Last() (*keyspan.Span, error) {
308 1 : i.dir = -1
309 1 : return i.gatherBackward(i.blockIter.Last())
310 1 : }
311 :
312 : // Next implements (keyspan.FragmentIterator).Next.
313 1 : func (i *fragmentIter) Next() (*keyspan.Span, error) {
314 1 : switch {
315 1 : case i.dir == -1 && !i.span.Valid():
316 1 : // Switching directions.
317 1 : //
318 1 : // i.blockIter is exhausted, before the first key. Move onto the first.
319 1 : i.blockIter.First()
320 1 : i.dir = +1
321 1 : case i.dir == -1 && i.span.Valid():
322 1 : // Switching directions.
323 1 : //
324 1 : // i.blockIter is currently positioned over the last internal key for
325 1 : // the previous span. Next it once to move to the first internal key
326 1 : // that makes up the current span, and gatherForwaad to land on the
327 1 : // first internal key making up the next span.
328 1 : //
329 1 : // In the diagram below, if the last span returned to the user during
330 1 : // reverse iteration was [b,c), i.blockIter is currently positioned at
331 1 : // [a,b). The block iter must be positioned over [d,e) to gather the
332 1 : // next span's fragments.
333 1 : //
334 1 : // ... [a,b) [b,c) [b,c) [b,c) [d,e) ...
335 1 : // ^ ^
336 1 : // i.blockIter want
337 1 : if x, err := i.gatherForward(i.blockIter.Next()); err != nil {
338 0 : return nil, err
339 1 : } else if invariants.Enabled && !x.Valid() {
340 0 : panic("pebble: invariant violation: next entry unexpectedly invalid")
341 : }
342 1 : i.dir = +1
343 : }
344 : // We know that this blockIter has in-place values.
345 1 : return i.gatherForward(i.blockIter.KV())
346 : }
347 :
348 : // Prev implements (keyspan.FragmentIterator).Prev.
349 1 : func (i *fragmentIter) Prev() (*keyspan.Span, error) {
350 1 : switch {
351 1 : case i.dir == +1 && !i.span.Valid():
352 1 : // Switching directions.
353 1 : //
354 1 : // i.blockIter is exhausted, after the last key. Move onto the last.
355 1 : i.blockIter.Last()
356 1 : i.dir = -1
357 1 : case i.dir == +1 && i.span.Valid():
358 1 : // Switching directions.
359 1 : //
360 1 : // i.blockIter is currently positioned over the first internal key for
361 1 : // the next span. Prev it once to move to the last internal key that
362 1 : // makes up the current span, and gatherBackward to land on the last
363 1 : // internal key making up the previous span.
364 1 : //
365 1 : // In the diagram below, if the last span returned to the user during
366 1 : // forward iteration was [b,c), i.blockIter is currently positioned at
367 1 : // [d,e). The block iter must be positioned over [a,b) to gather the
368 1 : // previous span's fragments.
369 1 : //
370 1 : // ... [a,b) [b,c) [b,c) [b,c) [d,e) ...
371 1 : // ^ ^
372 1 : // want i.blockIter
373 1 : if x, err := i.gatherBackward(i.blockIter.Prev()); err != nil {
374 0 : return nil, err
375 1 : } else if invariants.Enabled && !x.Valid() {
376 0 : panic("pebble: invariant violation: previous entry unexpectedly invalid")
377 : }
378 1 : i.dir = -1
379 : }
380 : // We know that this blockIter has in-place values.
381 1 : return i.gatherBackward(i.blockIter.KV())
382 : }
383 :
384 : // SeekGE implements (keyspan.FragmentIterator).SeekGE.
385 1 : func (i *fragmentIter) SeekGE(k []byte) (*keyspan.Span, error) {
386 1 : if s, err := i.SeekLT(k); err != nil {
387 0 : return nil, err
388 1 : } else if s != nil && i.blockIter.cmp(k, s.End) < 0 {
389 1 : return s, nil
390 1 : }
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 1 : return i.Next()
395 : }
396 :
397 : // SeekLT implements (keyspan.FragmentIterator).SeekLT.
398 1 : func (i *fragmentIter) SeekLT(k []byte) (*keyspan.Span, error) {
399 1 : i.dir = -1
400 1 : return i.gatherBackward(i.blockIter.SeekLT(k, base.SeekLTFlagsNone))
401 1 : }
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 1 : func checkFragmentBlockIterator(obj interface{}) {
417 1 : i := obj.(*fragmentIter)
418 1 : 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 : }
|