Skip to content

Commit 1bdd852

Browse files
committed
address steven comments
1 parent d4de1d1 commit 1bdd852

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

pkg/kv/kvpb/api.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,3 +2563,17 @@ func validateExclusionTimestampForBatch(ts hlc.Timestamp, h Header) error {
25632563
}
25642564
return nil
25652565
}
2566+
2567+
func (r *AddSSTableRequest) Validate(bh Header) error {
2568+
if r.ComputeStatsDiff {
2569+
if r.DisallowConflicts || r.DisallowShadowingBelow.IsSet() {
2570+
return errors.New(
2571+
"invalid AddSSTableRequest: ComputeStatsDiff cannot be used with DisallowConflicts or DisallowShadowingBelow")
2572+
}
2573+
if r.MVCCStats != nil {
2574+
return errors.New(
2575+
"invalid AddSSTableRequest: ComputeStatsDiff cannot be used with precomputed MVCCStats")
2576+
}
2577+
}
2578+
return nil
2579+
}

pkg/kv/kvpb/api.proto

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,16 +2114,23 @@ message AddSSTableRequest {
21142114
util.hlc.Timestamp ignore_keys_above_timestamp = 12 [(gogoproto.nullable) = false];
21152115

21162116
// ComputeStatsDiff causes the server to compute the effect this
2117-
// sst will have on the range's mvcc stats, even in the presence of
2117+
// SST will have on the range's MVCC stats, even in the presence of
21182118
// overlapping keys. This flag cannot be passed with the MVCCStats,
21192119
// DisallowShadowingBelow, or DisallowShadowing fields.
21202120
//
21212121
// This flag assumes that any key in the sst that is shadowed by a key in the
21222122
// engine is also a duplicate. As an example, accurate stats will be computed
21232123
// here:
2124-
// - sst: a3,a2 and eng: a2,a1
2125-
// but not for, as a1 is not a duplicate:
2126-
// - sst: a1 and eng: a2
2124+
// - sst: a@3,a@2 and eng: a@2,a@1
2125+
//
2126+
// but not for, as a@1 is not a duplicate:
2127+
// - sst: a@1 and eng: a@2
2128+
//
2129+
// In the ladder case, we silently create inaccurate stats currently. A TODO
2130+
// in storage.ComputeSSTStatsDiff is to detect this case and increment
2131+
// ContainsEstimates. At least for PCR, the first client of this flag, we
2132+
// expect the edge case to be quite rare, and would only occur if we're
2133+
// ingesting data older than the ingesting range's GC TTL.
21272134
bool compute_stats_diff = 13 [(gogoproto.customname) = "ComputeStatsDiff"];
21282135

21292136
reserved 10, 11;

pkg/kv/kvserver/batcheval/cmd_add_sstable.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ func EvalAddSSTable(
130130
) (result.Result, error) {
131131
args := cArgs.Args.(*kvpb.AddSSTableRequest)
132132
h := cArgs.Header
133+
134+
if err := args.Validate(h); err != nil {
135+
return result.Result{}, err
136+
}
137+
133138
ms := cArgs.Stats
134139
start, end := storage.MVCCKey{Key: args.Key}, storage.MVCCKey{Key: args.EndKey}
135140
sst := args.Data
@@ -290,34 +295,26 @@ func EvalAddSSTable(
290295
if checkConflicts {
291296
stats.Add(checkConflictsStatsDelta)
292297
}
293-
if !(checkConflicts || args.ComputeStatsDiff) {
294-
// If CheckSSTConflicts or ComputeStatsDiff are not used to compute stats,
295-
// then the stats do not account for overlapping keys in the engine, so we
296-
// have to assume they are estimates.
297-
stats.ContainsEstimates++
298-
}
299298
if args.ComputeStatsDiff {
300-
if checkConflicts {
301-
return result.Result{}, errors.New(
302-
"AddSSTableRequest.ComputeStatsDiff cannot be used with DisallowConflicts or DisallowShadowingBelow")
303-
}
304-
if args.MVCCStats != nil {
305-
return result.Result{}, errors.New(
306-
"AddSSTableRequest.ComputeStatsDiff cannot be used with precomputed MVCCStats")
307-
}
308-
statsDiff, err := computeSSTStatsDiffWrapper(ctx, sst, readWriter, h.Timestamp.WallTime, start, end)
299+
statsDiff, err := computeSSTStatsDiffWithFallback(ctx, sst, readWriter, h.Timestamp.WallTime, start, end)
309300
if err != nil {
310301
return result.Result{}, errors.Wrap(err, "computing SST stats diff")
311302
}
312303
stats.Add(statsDiff)
313304
} else if args.MVCCStats != nil {
314305
stats.Add(*args.MVCCStats)
306+
if !checkConflicts {
307+
stats.ContainsEstimates++
308+
}
315309
} else {
316310
sstStats, err := computeSSTStats(ctx, sst, h.Timestamp.WallTime)
317311
if err != nil {
318312
return result.Result{}, errors.Wrap(err, "computing SST stats")
319313
}
320314
stats.Add(sstStats)
315+
if !checkConflicts {
316+
stats.ContainsEstimates++
317+
}
321318
}
322319
ms.Add(stats)
323320

@@ -468,7 +465,7 @@ func EvalAddSSTable(
468465
}, nil
469466
}
470467

471-
func computeSSTStatsDiffWrapper(
468+
func computeSSTStatsDiffWithFallback(
472469
ctx context.Context,
473470
sst []byte,
474471
readWriter storage.ReadWriter,
@@ -519,7 +516,7 @@ func computeSSTStats(ctx context.Context, sst []byte, nowNanos int64) (enginepb.
519516
}
520517

521518
// checkSSTSpanBounds verifies that the keys in the sstable are within the
522-
// span specified by the request header.
519+
// span specified by [start, end].
523520
func checkSSTSpanBounds(ctx context.Context, sst []byte, start, end storage.MVCCKey) error {
524521
sstIter, err := storage.NewMemSSTIterator(sst, true /* verify */, storage.IterOptions{
525522
KeyTypes: storage.IterKeyTypePointsAndRanges,

0 commit comments

Comments
 (0)