Skip to content

Commit 4cf0850

Browse files
committed
exempt WIP
1 parent 45d293d commit 4cf0850

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

db.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,8 +1114,8 @@ func (d *DB) newIter(
11141114
dbi.batch.batch = batch
11151115
dbi.batch.batchSeqNum = batch.nextSeqNum()
11161116
}
1117-
if !dbi.batchOnlyIter && d.iterTracker != nil {
1118-
dbi.tracker = d.iterTracker
1117+
dbi.tracker = d.iterTracker
1118+
if !dbi.batchOnlyIter && d.iterTracker != nil && !dbi.opts.ExemptFromTracking {
11191119
dbi.trackerHandle = d.iterTracker.Start()
11201120
}
11211121
return finishInitializingIter(ctx, buf)

internal/inflight/in_flight.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type Tracker struct {
7272
// A zero Handle is not valid.
7373
type Handle uint64
7474

75+
func (h Handle) IsValid() bool {
76+
return h != 0
77+
}
78+
7579
// ReportFn is called (typically periodically) with a formatted report that
7680
// describes entries older than some threshold. An empty string report is never
7781
// delivered by the polling tracker.

iterator.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ type Iterator struct {
210210
// short-lived (since they pin memtables and sstables), (b) plumbing a
211211
// context into every method is very painful, (c) they do not (yet) respect
212212
// context cancellation and are only used for tracing.
213-
ctx context.Context
213+
ctx context.Context
214+
// TODO(radu): group fields that are always set from `db` fields (tracker,
215+
// merge, comparer, fc) into a single struct we can point to.
214216
tracker *inflight.Tracker
215217
trackerHandle inflight.Handle
216218
opts IterOptions
@@ -2365,8 +2367,9 @@ const maxKeyBufCacheSize = 4 << 10 // 4 KB
23652367
// It is not valid to call any method, including Close, after the iterator
23662368
// has been closed.
23672369
func (i *Iterator) Close() error {
2368-
if i.tracker != nil {
2370+
if i.trackerHandle.IsValid() {
23692371
i.tracker.Stop(i.trackerHandle)
2372+
i.trackerHandle = 0
23702373
}
23712374
// Close the child iterator before releasing the readState because when the
23722375
// readState is released sstables referenced by the readState may be deleted
@@ -2900,9 +2903,9 @@ func (i *Iterator) CloneWithContext(ctx context.Context, opts CloneOptions) (*It
29002903
newIters: i.newIters,
29012904
newIterRangeKey: i.newIterRangeKey,
29022905
seqNum: i.seqNum,
2906+
tracker: i.tracker,
29032907
}
2904-
if i.tracker != nil {
2905-
dbi.tracker = i.tracker
2908+
if i.tracker != nil && !dbi.opts.ExemptFromTracking {
29062909
dbi.trackerHandle = i.tracker.Start()
29072910
}
29082911
if i.batch != nil {

metamorphic/ops.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,9 +1181,6 @@ const debugIterators = false
11811181
func (o *newIterOp) run(t *Test, h historyRecorder) {
11821182
r := t.getReader(o.readerID)
11831183
opts := iterOptions(t.testOpts.KeyFormat, o.iterOpts)
1184-
if debugIterators {
1185-
opts.DebugRangeKeyStack = true
1186-
}
11871184

11881185
var i *pebble.Iterator
11891186
for {
@@ -1261,9 +1258,14 @@ type newIterUsingCloneOp struct {
12611258
func (o *newIterUsingCloneOp) run(t *Test, h historyRecorder) {
12621259
iter := t.getIter(o.existingIterID)
12631260
cloneOpts := pebble.CloneOptions{
1264-
IterOptions: iterOptions(t.testOpts.KeyFormat, o.iterOpts),
12651261
RefreshBatchView: o.refreshBatch,
12661262
}
1263+
// We treat the zero options as an indication that we want to keep the options
1264+
// of the original iterator. This way, we don't depend on iterOptions() and
1265+
// whether it returns nil or not.
1266+
if !o.iterOpts.IsZero() {
1267+
cloneOpts.IterOptions = iterOptions(t.testOpts.KeyFormat, o.iterOpts)
1268+
}
12671269
i, err := iter.iter.Clone(cloneOpts)
12681270
if err != nil {
12691271
panic(err)
@@ -1377,7 +1379,10 @@ func (o *iterSetOptionsOp) formattedString(kf KeyFormat) string {
13771379
}
13781380

13791381
func iterOptions(kf KeyFormat, o iterOpts) *pebble.IterOptions {
1380-
if o.IsZero() && !debugIterators {
1382+
// Sometimes exempt from tracking.
1383+
exemptFromTracking := rand.IntN(4) == 0
1384+
if o.IsZero() && !debugIterators && !exemptFromTracking && rand.IntN(2) == 0 {
1385+
// Sometimes return nil if we are using the default options.
13811386
return nil
13821387
}
13831388
var lower, upper []byte
@@ -1395,6 +1400,7 @@ func iterOptions(kf KeyFormat, o iterOpts) *pebble.IterOptions {
13951400
Suffix: o.maskSuffix,
13961401
},
13971402
UseL6Filters: o.useL6Filters,
1403+
ExemptFromTracking: exemptFromTracking,
13981404
DebugRangeKeyStack: debugIterators,
13991405
}
14001406
if opts.RangeKeyMasking.Suffix != nil {

options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ type IterOptions struct {
207207
// changed by calling SetOptions.
208208
Category block.Category
209209

210+
// ExemptFromTracking indicates that we should not track the lifetime of the
211+
// iterator (used to log information about long-lived iterators). Useful for
212+
// hot paths where we know the iterator will be short-lived.
213+
ExemptFromTracking bool
214+
215+
// DebugRangeKeyStack enables additional logging of the range key stack
216+
// iterator, via keyspan.InjectLogging. Only used for debugging.
210217
DebugRangeKeyStack bool
211218

212219
// Internal options.

0 commit comments

Comments
 (0)