Line data Source code
1 : // Copyright 2020 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 : "io"
11 : "sync/atomic"
12 : "time"
13 :
14 : "github.com/cockroachdb/errors"
15 : "github.com/cockroachdb/pebble/internal/base"
16 : "github.com/cockroachdb/pebble/internal/invariants"
17 : "github.com/cockroachdb/pebble/internal/keyspan"
18 : "github.com/cockroachdb/pebble/internal/keyspan/keyspanimpl"
19 : "github.com/cockroachdb/pebble/internal/manifest"
20 : )
21 :
22 : // flushable defines the interface for immutable memtables.
23 : type flushable interface {
24 : newIter(o *IterOptions) internalIterator
25 : newFlushIter(o *IterOptions) internalIterator
26 : newRangeDelIter(o *IterOptions) keyspan.FragmentIterator
27 : newRangeKeyIter(o *IterOptions) keyspan.FragmentIterator
28 : containsRangeKeys() bool
29 : // inuseBytes returns the number of inuse bytes by the flushable.
30 : inuseBytes() uint64
31 : // totalBytes returns the total number of bytes allocated by the flushable.
32 : totalBytes() uint64
33 : // readyForFlush returns true when the flushable is ready for flushing. See
34 : // memTable.readyForFlush for one implementation which needs to check whether
35 : // there are any outstanding write references.
36 : readyForFlush() bool
37 : // computePossibleOverlaps determines whether the flushable's keys overlap
38 : // with the bounds of any of the provided bounded items. If an item overlaps
39 : // or might overlap but it's not possible to determine overlap cheaply,
40 : // computePossibleOverlaps invokes the provided function with the object
41 : // that might overlap. computePossibleOverlaps must not perform any I/O and
42 : // implementations should invoke the provided function for items that would
43 : // require I/O to determine overlap.
44 : computePossibleOverlaps(overlaps func(bounded) shouldContinue, bounded ...bounded)
45 : }
46 :
47 : type shouldContinue bool
48 :
49 : const (
50 : continueIteration shouldContinue = true
51 : stopIteration = false
52 : )
53 :
54 : type bounded interface {
55 : UserKeyBounds() base.UserKeyBounds
56 : }
57 :
58 : var _ bounded = (*fileMetadata)(nil)
59 : var _ bounded = KeyRange{}
60 :
61 1 : func sliceAsBounded[B bounded](s []B) []bounded {
62 1 : ret := make([]bounded, len(s))
63 1 : for i := 0; i < len(s); i++ {
64 1 : ret[i] = s[i]
65 1 : }
66 1 : return ret
67 : }
68 :
69 : // flushableEntry wraps a flushable and adds additional metadata and
70 : // functionality that is common to all flushables.
71 : type flushableEntry struct {
72 : flushable
73 : // Channel which is closed when the flushable has been flushed.
74 : flushed chan struct{}
75 : // flushForced indicates whether a flush was forced on this memtable (either
76 : // manual, or due to ingestion). Protected by DB.mu.
77 : flushForced bool
78 : // delayedFlushForcedAt indicates whether a timer has been set to force a
79 : // flush on this memtable at some point in the future. Protected by DB.mu.
80 : // Holds the timestamp of when the flush will be issued.
81 : delayedFlushForcedAt time.Time
82 : // logNum corresponds to the WAL that contains the records present in the
83 : // receiver.
84 : logNum base.DiskFileNum
85 : // logSize is the size in bytes of the associated WAL. Protected by DB.mu.
86 : logSize uint64
87 : // The current logSeqNum at the time the memtable was created. This is
88 : // guaranteed to be less than or equal to any seqnum stored in the memtable.
89 : logSeqNum uint64
90 : // readerRefs tracks the read references on the flushable. The two sources of
91 : // reader references are DB.mu.mem.queue and readState.memtables. The memory
92 : // reserved by the flushable in the cache is released when the reader refs
93 : // drop to zero. If the flushable is referencing sstables, then the file
94 : // refount is also decreased once the reader refs drops to 0. If the
95 : // flushable is a memTable, when the reader refs drops to zero, the writer
96 : // refs will already be zero because the memtable will have been flushed and
97 : // that only occurs once the writer refs drops to zero.
98 : readerRefs atomic.Int32
99 : // Closure to invoke to release memory accounting.
100 : releaseMemAccounting func()
101 : // unrefFiles, if not nil, should be invoked to decrease the ref count of
102 : // files which are backing the flushable.
103 : unrefFiles func() []*fileBacking
104 : // deleteFnLocked should be called if the caller is holding DB.mu.
105 : deleteFnLocked func(obsolete []*fileBacking)
106 : // deleteFn should be called if the caller is not holding DB.mu.
107 : deleteFn func(obsolete []*fileBacking)
108 : }
109 :
110 1 : func (e *flushableEntry) readerRef() {
111 1 : switch v := e.readerRefs.Add(1); {
112 0 : case v <= 1:
113 0 : panic(fmt.Sprintf("pebble: inconsistent reference count: %d", v))
114 : }
115 : }
116 :
117 : // db.mu must not be held when this is called.
118 1 : func (e *flushableEntry) readerUnref(deleteFiles bool) {
119 1 : e.readerUnrefHelper(deleteFiles, e.deleteFn)
120 1 : }
121 :
122 : // db.mu must be held when this is called.
123 1 : func (e *flushableEntry) readerUnrefLocked(deleteFiles bool) {
124 1 : e.readerUnrefHelper(deleteFiles, e.deleteFnLocked)
125 1 : }
126 :
127 : func (e *flushableEntry) readerUnrefHelper(
128 : deleteFiles bool, deleteFn func(obsolete []*fileBacking),
129 1 : ) {
130 1 : switch v := e.readerRefs.Add(-1); {
131 0 : case v < 0:
132 0 : panic(fmt.Sprintf("pebble: inconsistent reference count: %d", v))
133 1 : case v == 0:
134 1 : if e.releaseMemAccounting == nil {
135 0 : panic("pebble: memtable reservation already released")
136 : }
137 1 : e.releaseMemAccounting()
138 1 : e.releaseMemAccounting = nil
139 1 : if e.unrefFiles != nil {
140 1 : obsolete := e.unrefFiles()
141 1 : e.unrefFiles = nil
142 1 : if deleteFiles {
143 1 : deleteFn(obsolete)
144 1 : }
145 : }
146 : }
147 : }
148 :
149 : type flushableList []*flushableEntry
150 :
151 : // ingestedFlushable is the implementation of the flushable interface for the
152 : // ingesting sstables which are added to the flushable list.
153 : type ingestedFlushable struct {
154 : // files are non-overlapping and ordered (according to their bounds).
155 : files []physicalMeta
156 : comparer *Comparer
157 : newIters tableNewIters
158 : newRangeKeyIters keyspanimpl.TableNewSpanIter
159 :
160 : // Since the level slice is immutable, we construct and set it once. It
161 : // should be safe to read from slice in future reads.
162 : slice manifest.LevelSlice
163 : // hasRangeKeys is set on ingestedFlushable construction.
164 : hasRangeKeys bool
165 : // exciseSpan is populated if an excise operation should be performed during
166 : // flush.
167 : exciseSpan KeyRange
168 : }
169 :
170 : func newIngestedFlushable(
171 : files []*fileMetadata,
172 : comparer *Comparer,
173 : newIters tableNewIters,
174 : newRangeKeyIters keyspanimpl.TableNewSpanIter,
175 : exciseSpan KeyRange,
176 1 : ) *ingestedFlushable {
177 1 : if invariants.Enabled {
178 1 : for i := 1; i < len(files); i++ {
179 1 : prev := files[i-1].UserKeyBounds()
180 1 : this := files[i].UserKeyBounds()
181 1 : if prev.End.IsUpperBoundFor(comparer.Compare, this.Start) {
182 0 : panic(errors.AssertionFailedf("ingested flushable files overlap: %s %s", prev, this))
183 : }
184 : }
185 : }
186 1 : var physicalFiles []physicalMeta
187 1 : var hasRangeKeys bool
188 1 : for _, f := range files {
189 1 : if f.HasRangeKeys {
190 1 : hasRangeKeys = true
191 1 : }
192 1 : physicalFiles = append(physicalFiles, f.PhysicalMeta())
193 : }
194 :
195 1 : ret := &ingestedFlushable{
196 1 : files: physicalFiles,
197 1 : comparer: comparer,
198 1 : newIters: newIters,
199 1 : newRangeKeyIters: newRangeKeyIters,
200 1 : // slice is immutable and can be set once and used many times.
201 1 : slice: manifest.NewLevelSliceKeySorted(comparer.Compare, files),
202 1 : hasRangeKeys: hasRangeKeys,
203 1 : exciseSpan: exciseSpan,
204 1 : }
205 1 :
206 1 : return ret
207 : }
208 :
209 : // TODO(sumeer): ingestedFlushable iters also need to plumb context for
210 : // tracing.
211 :
212 : // newIter is part of the flushable interface.
213 1 : func (s *ingestedFlushable) newIter(o *IterOptions) internalIterator {
214 1 : var opts IterOptions
215 1 : if o != nil {
216 1 : opts = *o
217 1 : }
218 : // TODO(bananabrick): The manifest.Level in newLevelIter is only used for
219 : // logging. Update the manifest.Level encoding to account for levels which
220 : // aren't truly levels in the lsm. Right now, the encoding only supports
221 : // L0 sublevels, and the rest of the levels in the lsm.
222 1 : return newLevelIter(
223 1 : context.Background(), opts, s.comparer, s.newIters, s.slice.Iter(), manifest.Level(0),
224 1 : internalIterOpts{},
225 1 : )
226 : }
227 :
228 : // newFlushIter is part of the flushable interface.
229 0 : func (s *ingestedFlushable) newFlushIter(*IterOptions) internalIterator {
230 0 : // newFlushIter is only used for writing memtables to disk as sstables.
231 0 : // Since ingested sstables are already present on disk, they don't need to
232 0 : // make use of a flush iter.
233 0 : panic("pebble: not implemented")
234 : }
235 :
236 : func (s *ingestedFlushable) constructRangeDelIter(
237 : file *manifest.FileMetadata, _ keyspan.SpanIterOptions,
238 1 : ) (keyspan.FragmentIterator, error) {
239 1 : iters, err := s.newIters(context.Background(), file, nil, internalIterOpts{}, iterRangeDeletions)
240 1 : if err != nil {
241 1 : return nil, err
242 1 : }
243 1 : return iters.RangeDeletion(), nil
244 : }
245 :
246 : // newRangeDelIter is part of the flushable interface.
247 : // TODO(bananabrick): Using a level iter instead of a keyspan level iter to
248 : // surface range deletes is more efficient.
249 : //
250 : // TODO(sumeer): *IterOptions are being ignored, so the index block load for
251 : // the point iterator in constructRangeDeIter is not tracked.
252 1 : func (s *ingestedFlushable) newRangeDelIter(_ *IterOptions) keyspan.FragmentIterator {
253 1 : return keyspanimpl.NewLevelIter(
254 1 : keyspan.SpanIterOptions{}, s.comparer.Compare,
255 1 : s.constructRangeDelIter, s.slice.Iter(), manifest.Level(0),
256 1 : manifest.KeyTypePoint,
257 1 : )
258 1 : }
259 :
260 : // newRangeKeyIter is part of the flushable interface.
261 1 : func (s *ingestedFlushable) newRangeKeyIter(o *IterOptions) keyspan.FragmentIterator {
262 1 : if !s.containsRangeKeys() {
263 1 : return nil
264 1 : }
265 :
266 1 : return keyspanimpl.NewLevelIter(
267 1 : keyspan.SpanIterOptions{}, s.comparer.Compare, s.newRangeKeyIters,
268 1 : s.slice.Iter(), manifest.Level(0), manifest.KeyTypeRange,
269 1 : )
270 : }
271 :
272 : // containsRangeKeys is part of the flushable interface.
273 1 : func (s *ingestedFlushable) containsRangeKeys() bool {
274 1 : return s.hasRangeKeys
275 1 : }
276 :
277 : // inuseBytes is part of the flushable interface.
278 0 : func (s *ingestedFlushable) inuseBytes() uint64 {
279 0 : // inuseBytes is only used when memtables are flushed to disk as sstables.
280 0 : panic("pebble: not implemented")
281 : }
282 :
283 : // totalBytes is part of the flushable interface.
284 1 : func (s *ingestedFlushable) totalBytes() uint64 {
285 1 : // We don't allocate additional bytes for the ingestedFlushable.
286 1 : return 0
287 1 : }
288 :
289 : // readyForFlush is part of the flushable interface.
290 1 : func (s *ingestedFlushable) readyForFlush() bool {
291 1 : // ingestedFlushable should always be ready to flush. However, note that
292 1 : // memtables before the ingested sstables in the memtable queue must be
293 1 : // flushed before an ingestedFlushable can be flushed. This is because the
294 1 : // ingested sstables need an updated view of the Version to
295 1 : // determine where to place the files in the lsm.
296 1 : return true
297 1 : }
298 :
299 : // computePossibleOverlaps is part of the flushable interface.
300 : func (s *ingestedFlushable) computePossibleOverlaps(
301 : fn func(bounded) shouldContinue, bounded ...bounded,
302 1 : ) {
303 1 : for _, b := range bounded {
304 1 : if s.anyFileOverlaps(b.UserKeyBounds()) {
305 1 : // Some file overlaps in key boundaries. The file doesn't necessarily
306 1 : // contain any keys within the key range, but we would need to perform I/O
307 1 : // to know for sure. The flushable interface dictates that we're not
308 1 : // permitted to perform I/O here, so err towards assuming overlap.
309 1 : if !fn(b) {
310 1 : return
311 1 : }
312 : }
313 : }
314 : }
315 :
316 : // anyFileBoundsOverlap returns true if there is at least a file in s.files with
317 : // bounds that overlap the given bounds.
318 1 : func (s *ingestedFlushable) anyFileOverlaps(bounds base.UserKeyBounds) bool {
319 1 : // Note that s.files are non-overlapping and sorted.
320 1 : for _, f := range s.files {
321 1 : fileBounds := f.UserKeyBounds()
322 1 : if !fileBounds.End.IsUpperBoundFor(s.comparer.Compare, bounds.Start) {
323 1 : // The file ends before the bounds start. Go to the next file.
324 1 : continue
325 : }
326 1 : if !bounds.End.IsUpperBoundFor(s.comparer.Compare, fileBounds.Start) {
327 1 : // The file starts after the bounds end. There is no overlap, and
328 1 : // further files will not overlap either (the files are sorted).
329 1 : return false
330 1 : }
331 : // There is overlap. Note that UserKeyBounds.Overlaps() performs exactly the
332 : // checks above.
333 1 : return true
334 : }
335 1 : return false
336 : }
337 :
338 : // computePossibleOverlapsGenericImpl is an implementation of the flushable
339 : // interface's computePossibleOverlaps function for flushable implementations
340 : // with only in-memory state that do not have special requirements and should
341 : // read through the ordinary flushable iterators.
342 : //
343 : // This function must only be used with implementations that are infallible (eg,
344 : // memtable iterators) and will panic if an error is encountered.
345 : func computePossibleOverlapsGenericImpl[F flushable](
346 : f F, cmp Compare, fn func(bounded) shouldContinue, bounded []bounded,
347 1 : ) {
348 1 : iter := f.newIter(nil)
349 1 : rangeDelIter := f.newRangeDelIter(nil)
350 1 : rkeyIter := f.newRangeKeyIter(nil)
351 1 : for _, b := range bounded {
352 1 : overlap, err := determineOverlapAllIters(cmp, b.UserKeyBounds(), iter, rangeDelIter, rkeyIter)
353 1 : if invariants.Enabled && err != nil {
354 0 : panic(errors.AssertionFailedf("expected iterator to be infallible: %v", err))
355 : }
356 1 : if overlap {
357 1 : if !fn(b) {
358 1 : break
359 : }
360 : }
361 : }
362 :
363 1 : for _, c := range [3]io.Closer{iter, rangeDelIter, rkeyIter} {
364 1 : if c != nil {
365 1 : if err := c.Close(); err != nil {
366 0 : // This implementation must be used in circumstances where
367 0 : // reading through the iterator is infallible.
368 0 : panic(err)
369 : }
370 : }
371 : }
372 : }
373 :
374 : // determineOverlapAllIters checks for overlap in a point iterator, range
375 : // deletion iterator and range key iterator.
376 : func determineOverlapAllIters(
377 : cmp base.Compare,
378 : bounds base.UserKeyBounds,
379 : pointIter base.InternalIterator,
380 : rangeDelIter, rangeKeyIter keyspan.FragmentIterator,
381 1 : ) (bool, error) {
382 1 : if pointIter != nil {
383 1 : if pointOverlap, err := determineOverlapPointIterator(cmp, bounds, pointIter); pointOverlap || err != nil {
384 1 : return pointOverlap, err
385 1 : }
386 : }
387 1 : if rangeDelIter != nil {
388 1 : if rangeDelOverlap, err := determineOverlapKeyspanIterator(cmp, bounds, rangeDelIter); rangeDelOverlap || err != nil {
389 1 : return rangeDelOverlap, err
390 1 : }
391 : }
392 1 : if rangeKeyIter != nil {
393 1 : return determineOverlapKeyspanIterator(cmp, bounds, rangeKeyIter)
394 1 : }
395 1 : return false, nil
396 : }
|