Line data Source code
1 : // Copyright 2012 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 : "io"
10 : "math"
11 : "sync"
12 : "sync/atomic"
13 : "time"
14 :
15 : "github.com/cockroachdb/errors"
16 : "github.com/cockroachdb/pebble/internal/invariants"
17 : "github.com/cockroachdb/pebble/rangekey"
18 : )
19 :
20 : // ErrSnapshotExcised is returned from WaitForFileOnlySnapshot if an excise
21 : // overlapping with one of the EventuallyFileOnlySnapshot's KeyRanges gets
22 : // applied before the transition of that EFOS to a file-only snapshot.
23 : var ErrSnapshotExcised = errors.New("pebble: snapshot excised before conversion to file-only snapshot")
24 :
25 : // Snapshot provides a read-only point-in-time view of the DB state.
26 : type Snapshot struct {
27 : // The db the snapshot was created from.
28 : db *DB
29 : seqNum uint64
30 :
31 : // Set if part of an EventuallyFileOnlySnapshot.
32 : efos *EventuallyFileOnlySnapshot
33 :
34 : // The list the snapshot is linked into.
35 : list *snapshotList
36 :
37 : // The next/prev link for the snapshotList doubly-linked list of snapshots.
38 : prev, next *Snapshot
39 : }
40 :
41 : var _ Reader = (*Snapshot)(nil)
42 :
43 : // Get gets the value for the given key. It returns ErrNotFound if the Snapshot
44 : // does not contain the key.
45 : //
46 : // The caller should not modify the contents of the returned slice, but it is
47 : // safe to modify the contents of the argument after Get returns. The returned
48 : // slice will remain valid until the returned Closer is closed. On success, the
49 : // caller MUST call closer.Close() or a memory leak will occur.
50 2 : func (s *Snapshot) Get(key []byte) ([]byte, io.Closer, error) {
51 2 : if s.db == nil {
52 1 : panic(ErrClosed)
53 : }
54 2 : return s.db.getInternal(key, nil /* batch */, s)
55 : }
56 :
57 : // NewIter returns an iterator that is unpositioned (Iterator.Valid() will
58 : // return false). The iterator can be positioned via a call to SeekGE,
59 : // SeekLT, First or Last.
60 2 : func (s *Snapshot) NewIter(o *IterOptions) (*Iterator, error) {
61 2 : return s.NewIterWithContext(context.Background(), o)
62 2 : }
63 :
64 : // NewIterWithContext is like NewIter, and additionally accepts a context for
65 : // tracing.
66 2 : func (s *Snapshot) NewIterWithContext(ctx context.Context, o *IterOptions) (*Iterator, error) {
67 2 : if s.db == nil {
68 1 : panic(ErrClosed)
69 : }
70 2 : return s.db.newIter(ctx, nil /* batch */, snapshotIterOpts{seqNum: s.seqNum}, o), nil
71 : }
72 :
73 : // ScanInternal scans all internal keys within the specified bounds, truncating
74 : // any rangedels and rangekeys to those bounds. For use when an external user
75 : // needs to be aware of all internal keys that make up a key range.
76 : //
77 : // See comment on db.ScanInternal for the behaviour that can be expected of
78 : // point keys deleted by range dels and keys masked by range keys.
79 : func (s *Snapshot) ScanInternal(
80 : ctx context.Context,
81 : lower, upper []byte,
82 : visitPointKey func(key *InternalKey, value LazyValue, iterInfo IteratorLevel) error,
83 : visitRangeDel func(start, end []byte, seqNum uint64) error,
84 : visitRangeKey func(start, end []byte, keys []rangekey.Key) error,
85 : visitSharedFile func(sst *SharedSSTMeta) error,
86 1 : ) error {
87 1 : if s.db == nil {
88 0 : panic(ErrClosed)
89 : }
90 1 : scanInternalOpts := &scanInternalOptions{
91 1 : visitPointKey: visitPointKey,
92 1 : visitRangeDel: visitRangeDel,
93 1 : visitRangeKey: visitRangeKey,
94 1 : visitSharedFile: visitSharedFile,
95 1 : skipSharedLevels: visitSharedFile != nil,
96 1 : IterOptions: IterOptions{
97 1 : KeyTypes: IterKeyTypePointsAndRanges,
98 1 : LowerBound: lower,
99 1 : UpperBound: upper,
100 1 : },
101 1 : }
102 1 :
103 1 : iter := s.db.newInternalIter(snapshotIterOpts{seqNum: s.seqNum}, scanInternalOpts)
104 1 : defer iter.close()
105 1 :
106 1 : return scanInternalImpl(ctx, lower, upper, iter, scanInternalOpts)
107 : }
108 :
109 : // closeLocked is similar to Close(), except it requires that db.mu be held
110 : // by the caller.
111 2 : func (s *Snapshot) closeLocked() error {
112 2 : s.db.mu.snapshots.remove(s)
113 2 :
114 2 : // If s was the previous earliest snapshot, we might be able to reclaim
115 2 : // disk space by dropping obsolete records that were pinned by s.
116 2 : if e := s.db.mu.snapshots.earliest(); e > s.seqNum {
117 2 : s.db.maybeScheduleCompactionPicker(pickElisionOnly)
118 2 : }
119 2 : s.db = nil
120 2 : return nil
121 : }
122 :
123 : // Close closes the snapshot, releasing its resources. Close must be called.
124 : // Failure to do so will result in a tiny memory leak and a large leak of
125 : // resources on disk due to the entries the snapshot is preventing from being
126 : // deleted.
127 : //
128 : // d.mu must NOT be held by the caller.
129 2 : func (s *Snapshot) Close() error {
130 2 : db := s.db
131 2 : if db == nil {
132 1 : panic(ErrClosed)
133 : }
134 2 : db.mu.Lock()
135 2 : defer db.mu.Unlock()
136 2 : return s.closeLocked()
137 : }
138 :
139 : type snapshotList struct {
140 : root Snapshot
141 : }
142 :
143 2 : func (l *snapshotList) init() {
144 2 : l.root.next = &l.root
145 2 : l.root.prev = &l.root
146 2 : }
147 :
148 2 : func (l *snapshotList) empty() bool {
149 2 : return l.root.next == &l.root
150 2 : }
151 :
152 2 : func (l *snapshotList) count() int {
153 2 : if l.empty() {
154 2 : return 0
155 2 : }
156 1 : var count int
157 1 : for i := l.root.next; i != &l.root; i = i.next {
158 1 : count++
159 1 : }
160 1 : return count
161 : }
162 :
163 2 : func (l *snapshotList) earliest() uint64 {
164 2 : v := uint64(math.MaxUint64)
165 2 : if !l.empty() {
166 2 : v = l.root.next.seqNum
167 2 : }
168 2 : return v
169 : }
170 :
171 2 : func (l *snapshotList) toSlice() []uint64 {
172 2 : if l.empty() {
173 2 : return nil
174 2 : }
175 2 : var results []uint64
176 2 : for i := l.root.next; i != &l.root; i = i.next {
177 2 : results = append(results, i.seqNum)
178 2 : }
179 2 : return results
180 : }
181 :
182 2 : func (l *snapshotList) pushBack(s *Snapshot) {
183 2 : if s.list != nil || s.prev != nil || s.next != nil {
184 0 : panic("pebble: snapshot list is inconsistent")
185 : }
186 2 : s.prev = l.root.prev
187 2 : s.prev.next = s
188 2 : s.next = &l.root
189 2 : s.next.prev = s
190 2 : s.list = l
191 : }
192 :
193 2 : func (l *snapshotList) remove(s *Snapshot) {
194 2 : if s == &l.root {
195 0 : panic("pebble: cannot remove snapshot list root node")
196 : }
197 2 : if s.list != l {
198 0 : panic("pebble: snapshot list is inconsistent")
199 : }
200 2 : s.prev.next = s.next
201 2 : s.next.prev = s.prev
202 2 : s.next = nil // avoid memory leaks
203 2 : s.prev = nil // avoid memory leaks
204 2 : s.list = nil // avoid memory leaks
205 : }
206 :
207 : // EventuallyFileOnlySnapshot (aka EFOS) provides a read-only point-in-time view
208 : // of the database state, similar to Snapshot. An EventuallyFileOnlySnapshot
209 : // induces less write amplification than Snapshot, at the cost of increased space
210 : // amplification. While a Snapshot may increase write amplification across all
211 : // flushes and compactions for the duration of its lifetime, an
212 : // EventuallyFileOnlySnapshot only incurs that cost for flushes/compactions if
213 : // memtables at the time of EFOS instantiation contained keys that the EFOS is
214 : // interested in (i.e. its protectedRanges). In that case, the EFOS prevents
215 : // elision of keys visible to it, similar to a Snapshot, until those memtables
216 : // are flushed, and once that happens, the "EventuallyFileOnlySnapshot"
217 : // transitions to a file-only snapshot state in which it pins zombies sstables
218 : // like an open Iterator would, without pinning any memtables. Callers that can
219 : // tolerate the increased space amplification of pinning zombie sstables until
220 : // the snapshot is closed may prefer EventuallyFileOnlySnapshots for their
221 : // reduced write amplification. Callers that desire the benefits of the file-only
222 : // state that requires no pinning of memtables should call
223 : // `WaitForFileOnlySnapshot()` (and possibly re-mint an EFOS if it returns
224 : // ErrSnapshotExcised) before relying on the EFOS to keep producing iterators
225 : // with zero write-amp and zero pinning of memtables in memory.
226 : //
227 : // EventuallyFileOnlySnapshots interact with the IngestAndExcise operation in
228 : // subtle ways. No new iterators can be created once
229 : // EventuallyFileOnlySnapshot.excised is set to true.
230 : type EventuallyFileOnlySnapshot struct {
231 : mu struct {
232 : // NB: If both this mutex and db.mu are being grabbed, db.mu should be
233 : // grabbed _before_ grabbing this one.
234 : sync.Mutex
235 :
236 : // Either the snap field is set below, or the version is set at any given
237 : // point of time. If a snapshot is referenced, this is not a file-only
238 : // snapshot yet, and if a version is set (and ref'd) this is a file-only
239 : // snapshot.
240 :
241 : // The wrapped regular snapshot, if not a file-only snapshot yet.
242 : snap *Snapshot
243 : // The wrapped version reference, if a file-only snapshot.
244 : vers *version
245 : }
246 :
247 : // Key ranges to watch for an excise on.
248 : protectedRanges []KeyRange
249 : // excised, if true, signals that the above ranges were excised during the
250 : // lifetime of this snapshot.
251 : excised atomic.Bool
252 :
253 : // The db the snapshot was created from.
254 : db *DB
255 : seqNum uint64
256 :
257 : closed chan struct{}
258 : }
259 :
260 : func (d *DB) makeEventuallyFileOnlySnapshot(
261 : keyRanges []KeyRange, internalKeyRanges []internalKeyRange,
262 2 : ) *EventuallyFileOnlySnapshot {
263 2 : isFileOnly := true
264 2 :
265 2 : d.mu.Lock()
266 2 : defer d.mu.Unlock()
267 2 : seqNum := d.mu.versions.visibleSeqNum.Load()
268 2 : // Check if any of the keyRanges overlap with a memtable.
269 2 : for i := range d.mu.mem.queue {
270 2 : mem := d.mu.mem.queue[i]
271 2 : if ingestMemtableOverlaps(d.cmp, mem, internalKeyRanges) {
272 2 : isFileOnly = false
273 2 : break
274 : }
275 : }
276 2 : es := &EventuallyFileOnlySnapshot{
277 2 : db: d,
278 2 : seqNum: seqNum,
279 2 : protectedRanges: keyRanges,
280 2 : closed: make(chan struct{}),
281 2 : }
282 2 : if isFileOnly {
283 2 : es.mu.vers = d.mu.versions.currentVersion()
284 2 : es.mu.vers.Ref()
285 2 : } else {
286 2 : s := &Snapshot{
287 2 : db: d,
288 2 : seqNum: seqNum,
289 2 : }
290 2 : s.efos = es
291 2 : es.mu.snap = s
292 2 : d.mu.snapshots.pushBack(s)
293 2 : }
294 2 : return es
295 : }
296 :
297 : // Transitions this EventuallyFileOnlySnapshot to a file-only snapshot. Requires
298 : // earliestUnflushedSeqNum and vers to correspond to the same Version from the
299 : // current or a past acquisition of db.mu. vers must have been Ref()'d before
300 : // that mutex was released, if it was released.
301 : //
302 : // NB: The caller is expected to check for es.excised before making this
303 : // call.
304 : //
305 : // d.mu must be held when calling this method.
306 2 : func (es *EventuallyFileOnlySnapshot) transitionToFileOnlySnapshot(vers *version) error {
307 2 : es.mu.Lock()
308 2 : select {
309 1 : case <-es.closed:
310 1 : vers.UnrefLocked()
311 1 : es.mu.Unlock()
312 1 : return ErrClosed
313 2 : default:
314 : }
315 2 : if es.mu.snap == nil {
316 0 : es.mu.Unlock()
317 0 : panic("pebble: tried to transition an eventually-file-only-snapshot twice")
318 : }
319 : // The caller has already called Ref() on vers.
320 2 : es.mu.vers = vers
321 2 : // NB: The callers should have already done a check of es.excised.
322 2 : oldSnap := es.mu.snap
323 2 : es.mu.snap = nil
324 2 : es.mu.Unlock()
325 2 : return oldSnap.closeLocked()
326 : }
327 :
328 : // hasTransitioned returns true if this EFOS has transitioned to a file-only
329 : // snapshot.
330 1 : func (es *EventuallyFileOnlySnapshot) hasTransitioned() bool {
331 1 : es.mu.Lock()
332 1 : defer es.mu.Unlock()
333 1 : return es.mu.vers != nil
334 1 : }
335 :
336 : // waitForFlush waits for a flush on any memtables that need to be flushed
337 : // before this EFOS can transition to a file-only snapshot. If this EFOS is
338 : // waiting on a flush of the mutable memtable, it forces a rotation within
339 : // `dur` duration. For immutable memtables, it schedules a flush and waits for
340 : // it to finish.
341 1 : func (es *EventuallyFileOnlySnapshot) waitForFlush(ctx context.Context, dur time.Duration) error {
342 1 : es.db.mu.Lock()
343 1 : defer es.db.mu.Unlock()
344 1 :
345 1 : earliestUnflushedSeqNum := es.db.getEarliestUnflushedSeqNumLocked()
346 1 : for earliestUnflushedSeqNum < es.seqNum {
347 1 : select {
348 0 : case <-es.closed:
349 0 : return ErrClosed
350 0 : case <-ctx.Done():
351 0 : return ctx.Err()
352 1 : default:
353 : }
354 : // Check if the current mutable memtable contains keys less than seqNum.
355 : // If so, rotate it.
356 1 : if es.db.mu.mem.mutable.logSeqNum < es.seqNum && dur.Nanoseconds() > 0 {
357 1 : es.db.maybeScheduleDelayedFlush(es.db.mu.mem.mutable, dur)
358 1 : } else {
359 0 : // Find the last memtable that contains seqNums less than es.seqNum,
360 0 : // and force a flush on it.
361 0 : var mem *flushableEntry
362 0 : for i := range es.db.mu.mem.queue {
363 0 : if es.db.mu.mem.queue[i].logSeqNum < es.seqNum {
364 0 : mem = es.db.mu.mem.queue[i]
365 0 : }
366 : }
367 0 : mem.flushForced = true
368 0 : es.db.maybeScheduleFlush()
369 : }
370 1 : es.db.mu.compact.cond.Wait()
371 1 :
372 1 : earliestUnflushedSeqNum = es.db.getEarliestUnflushedSeqNumLocked()
373 : }
374 1 : if es.excised.Load() {
375 1 : return ErrSnapshotExcised
376 1 : }
377 1 : return nil
378 : }
379 :
380 : // WaitForFileOnlySnapshot blocks the calling goroutine until this snapshot
381 : // has been converted into a file-only snapshot (i.e. all memtables containing
382 : // keys < seqNum are flushed). A duration can be passed in, and if nonzero,
383 : // a delayed flush will be scheduled at that duration if necessary.
384 : //
385 : // Idempotent; can be called multiple times with no side effects.
386 : func (es *EventuallyFileOnlySnapshot) WaitForFileOnlySnapshot(
387 : ctx context.Context, dur time.Duration,
388 1 : ) error {
389 1 : if es.hasTransitioned() {
390 1 : return nil
391 1 : }
392 :
393 1 : if err := es.waitForFlush(ctx, dur); err != nil {
394 1 : return err
395 1 : }
396 :
397 1 : if invariants.Enabled {
398 1 : // Since we aren't returning an error, we _must_ have transitioned to a
399 1 : // file-only snapshot by now.
400 1 : if !es.hasTransitioned() {
401 0 : panic("expected EFOS to have transitioned to file-only snapshot after flush")
402 : }
403 : }
404 1 : return nil
405 : }
406 :
407 : // Close closes the file-only snapshot and releases all referenced resources.
408 : // Not idempotent.
409 2 : func (es *EventuallyFileOnlySnapshot) Close() error {
410 2 : close(es.closed)
411 2 : es.db.mu.Lock()
412 2 : defer es.db.mu.Unlock()
413 2 : es.mu.Lock()
414 2 : defer es.mu.Unlock()
415 2 :
416 2 : if es.mu.snap != nil {
417 2 : if err := es.mu.snap.closeLocked(); err != nil {
418 0 : return err
419 0 : }
420 : }
421 2 : if es.mu.vers != nil {
422 2 : es.mu.vers.UnrefLocked()
423 2 : }
424 2 : return nil
425 : }
426 :
427 : // Get implements the Reader interface.
428 1 : func (es *EventuallyFileOnlySnapshot) Get(key []byte) (value []byte, closer io.Closer, err error) {
429 1 : // TODO(jackson): Use getInternal.
430 1 : iter, err := es.NewIter(nil)
431 1 : if err != nil {
432 0 : return nil, nil, err
433 0 : }
434 1 : var valid bool
435 1 : if es.db.opts.Comparer.Split != nil {
436 1 : valid = iter.SeekPrefixGE(key)
437 1 : } else {
438 0 : valid = iter.SeekGE(key)
439 0 : }
440 1 : if !valid {
441 1 : if err = firstError(iter.Error(), iter.Close()); err != nil {
442 0 : return nil, nil, err
443 0 : }
444 1 : return nil, nil, ErrNotFound
445 : }
446 1 : if !es.db.equal(iter.Key(), key) {
447 1 : return nil, nil, firstError(iter.Close(), ErrNotFound)
448 1 : }
449 1 : return iter.Value(), iter, nil
450 : }
451 :
452 : // NewIter returns an iterator that is unpositioned (Iterator.Valid() will
453 : // return false). The iterator can be positioned via a call to SeekGE,
454 : // SeekLT, First or Last.
455 2 : func (es *EventuallyFileOnlySnapshot) NewIter(o *IterOptions) (*Iterator, error) {
456 2 : return es.NewIterWithContext(context.Background(), o)
457 2 : }
458 :
459 : // NewIterWithContext is like NewIter, and additionally accepts a context for
460 : // tracing.
461 : func (es *EventuallyFileOnlySnapshot) NewIterWithContext(
462 : ctx context.Context, o *IterOptions,
463 2 : ) (*Iterator, error) {
464 2 : select {
465 0 : case <-es.closed:
466 0 : panic(ErrClosed)
467 2 : default:
468 : }
469 :
470 2 : es.mu.Lock()
471 2 : defer es.mu.Unlock()
472 2 : if es.mu.vers != nil {
473 2 : sOpts := snapshotIterOpts{seqNum: es.seqNum, vers: es.mu.vers}
474 2 : return es.db.newIter(ctx, nil /* batch */, sOpts, o), nil
475 2 : }
476 :
477 2 : if es.excised.Load() {
478 1 : return nil, ErrSnapshotExcised
479 1 : }
480 2 : sOpts := snapshotIterOpts{seqNum: es.seqNum}
481 2 : iter := es.db.newIter(ctx, nil /* batch */, sOpts, o)
482 2 :
483 2 : // If excised is true, then keys relevant to the snapshot might not be
484 2 : // present in the readState being used by the iterator. Error out.
485 2 : if es.excised.Load() {
486 0 : iter.Close()
487 0 : return nil, ErrSnapshotExcised
488 0 : }
489 2 : return iter, nil
490 : }
491 :
492 : // ScanInternal scans all internal keys within the specified bounds, truncating
493 : // any rangedels and rangekeys to those bounds. For use when an external user
494 : // needs to be aware of all internal keys that make up a key range.
495 : //
496 : // See comment on db.ScanInternal for the behaviour that can be expected of
497 : // point keys deleted by range dels and keys masked by range keys.
498 : func (es *EventuallyFileOnlySnapshot) ScanInternal(
499 : ctx context.Context,
500 : lower, upper []byte,
501 : visitPointKey func(key *InternalKey, value LazyValue, iterInfo IteratorLevel) error,
502 : visitRangeDel func(start, end []byte, seqNum uint64) error,
503 : visitRangeKey func(start, end []byte, keys []rangekey.Key) error,
504 : visitSharedFile func(sst *SharedSSTMeta) error,
505 1 : ) error {
506 1 : if es.db == nil {
507 0 : panic(ErrClosed)
508 : }
509 1 : if es.excised.Load() {
510 0 : return ErrSnapshotExcised
511 0 : }
512 1 : var sOpts snapshotIterOpts
513 1 : es.mu.Lock()
514 1 : if es.mu.vers != nil {
515 1 : sOpts = snapshotIterOpts{
516 1 : seqNum: es.seqNum,
517 1 : vers: es.mu.vers,
518 1 : }
519 1 : } else {
520 1 : sOpts = snapshotIterOpts{
521 1 : seqNum: es.seqNum,
522 1 : }
523 1 : }
524 1 : es.mu.Unlock()
525 1 : opts := &scanInternalOptions{
526 1 : IterOptions: IterOptions{
527 1 : KeyTypes: IterKeyTypePointsAndRanges,
528 1 : LowerBound: lower,
529 1 : UpperBound: upper,
530 1 : },
531 1 : visitPointKey: visitPointKey,
532 1 : visitRangeDel: visitRangeDel,
533 1 : visitRangeKey: visitRangeKey,
534 1 : visitSharedFile: visitSharedFile,
535 1 : skipSharedLevels: visitSharedFile != nil,
536 1 : }
537 1 : iter := es.db.newInternalIter(sOpts, opts)
538 1 : defer iter.close()
539 1 :
540 1 : // If excised is true, then keys relevant to the snapshot might not be
541 1 : // present in the readState being used by the iterator. Error out.
542 1 : if es.excised.Load() {
543 0 : return ErrSnapshotExcised
544 0 : }
545 :
546 1 : return scanInternalImpl(ctx, lower, upper, iter, opts)
547 : }
|