Skip to content

Commit

Permalink
batcheval: refactor MVCCExportToSST to accept a storage.Writer
Browse files Browse the repository at this point in the history
This is a refactor only change that pulls out the
logic in `MVCCExportToSST` into `mvccExportToWriter` that
accepts a `storage.Writer` interface. This will allow us
to pass in a `FingerprintWriter` in a future commit.

Informs: cockroachdb#89336

Release note: None
  • Loading branch information
adityamaru committed Nov 10, 2022
1 parent 53e75b0 commit b49ba6c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
9 changes: 0 additions & 9 deletions pkg/kv/kvserver/batcheval/cmd_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
"github.com/gogo/protobuf/types"
)

// SSTTargetSizeSetting is the cluster setting name for the
Expand Down Expand Up @@ -105,14 +104,6 @@ func evalExport(
ctx, evalExportSpan := tracing.ChildSpan(ctx, "evalExport")
defer evalExportSpan.Finish()

var evalExportTrace types.StringValue
if cArgs.EvalCtx.NodeID() == h.GatewayNodeID {
evalExportTrace.Value = fmt.Sprintf("evaluating Export on gateway node %d", cArgs.EvalCtx.NodeID())
} else {
evalExportTrace.Value = fmt.Sprintf("evaluating Export on remote node %d", cArgs.EvalCtx.NodeID())
}
evalExportSpan.RecordStructured(&evalExportTrace)

// Table's marked to be excluded from backup are expected to be configured
// with a short GC TTL. Additionally, backup excludes such table's from being
// protected from GC when writing ProtectedTimestamp records. The
Expand Down
61 changes: 37 additions & 24 deletions pkg/storage/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5767,8 +5767,33 @@ func MVCCIsSpanEmpty(
}

// MVCCExportToSST exports changes to the keyrange [StartKey, EndKey) over the
// interval (StartTS, EndTS] as a Pebble SST. See MVCCExportOptions for options.
// StartTS may be zero.
// interval (StartTS, EndTS] as a Pebble SST. See mvccExportToWriter for more
// details.
func MVCCExportToSST(
ctx context.Context, cs *cluster.Settings, reader Reader, opts MVCCExportOptions, dest io.Writer,
) (roachpb.BulkOpSummary, MVCCKey, error) {
ctx, span := tracing.ChildSpan(ctx, "storage.MVCCExportToSST")
defer span.Finish()
sstWriter := MakeBackupSSTWriter(ctx, cs, dest)
defer sstWriter.Close()

summary, resumeKey, err := mvccExportToWriter(ctx, reader, opts, &sstWriter)
if err != nil {
return roachpb.BulkOpSummary{}, MVCCKey{}, err
}

if summary.DataSize == 0 {
// If no records were added to the sstable, skip completing it and return a
// nil slice – the export code will discard it anyway (based on 0 DataSize).
return roachpb.BulkOpSummary{}, MVCCKey{}, nil
}

return summary, resumeKey, sstWriter.Finish()
}

// mvccExportToWriter exports changes to the keyrange [StartKey, EndKey) over
// the interval (StartTS, EndTS] to the passed in writer. See MVCCExportOptions
// for options. StartTS may be zero.
//
// This comes in two principal flavors: all revisions or latest revision only.
// In all-revisions mode, exports everything matching the span and time bounds,
Expand All @@ -5788,16 +5813,14 @@ func MVCCIsSpanEmpty(
// intents outside are ignored.
//
// Returns an export summary and a resume key that allows resuming the export if
// it reached a limit. Data is written to dest as it is collected. If an error
// is returned then dest contents are undefined.
func MVCCExportToSST(
ctx context.Context, cs *cluster.Settings, reader Reader, opts MVCCExportOptions, dest io.Writer,
// it reached a limit. Data is written to the writer as it is collected. If an
// error is returned then the writer's contents are undefined. It is the
// responsibility of the caller to Finish() / Close() the passed in writer.
func mvccExportToWriter(
ctx context.Context, reader Reader, opts MVCCExportOptions, writer Writer,
) (roachpb.BulkOpSummary, MVCCKey, error) {
var span *tracing.Span
ctx, span = tracing.ChildSpan(ctx, "storage.MVCCExportToSST")
ctx, span := tracing.ChildSpan(ctx, "storage.mvccExportToWriter")
defer span.Finish()
sstWriter := MakeBackupSSTWriter(ctx, cs, dest)
defer sstWriter.Close()

// If we're not exporting all revisions then we can mask point keys below any
// MVCC range tombstones, since we don't care about them.
Expand Down Expand Up @@ -5936,7 +5959,7 @@ func MVCCExportToSST(
}
// Export only the inner roachpb.Value, not the MVCCValue header.
rawValue := mvccValue.Value.RawBytes
if err := sstWriter.PutRawMVCCRangeKey(rangeKeys.AsRangeKey(v), rawValue); err != nil {
if err := writer.PutRawMVCCRangeKey(rangeKeys.AsRangeKey(v), rawValue); err != nil {
return roachpb.BulkOpSummary{}, MVCCKey{}, err
}
}
Expand Down Expand Up @@ -6047,11 +6070,11 @@ func MVCCExportToSST(
if unsafeKey.Timestamp.IsEmpty() {
// This should never be an intent since the incremental iterator returns
// an error when encountering intents.
if err := sstWriter.PutUnversioned(unsafeKey.Key, unsafeValue); err != nil {
if err := writer.PutUnversioned(unsafeKey.Key, unsafeValue); err != nil {
return roachpb.BulkOpSummary{}, MVCCKey{}, errors.Wrapf(err, "adding key %s", unsafeKey)
}
} else {
if err := sstWriter.PutRawMVCC(unsafeKey, unsafeValue); err != nil {
if err := writer.PutRawMVCC(unsafeKey, unsafeValue); err != nil {
return roachpb.BulkOpSummary{}, MVCCKey{}, errors.Wrapf(err, "adding key %s", unsafeKey)
}
}
Expand Down Expand Up @@ -6109,23 +6132,13 @@ func MVCCExportToSST(
}
// Export only the inner roachpb.Value, not the MVCCValue header.
rawValue := mvccValue.Value.RawBytes
if err := sstWriter.PutRawMVCCRangeKey(rangeKeys.AsRangeKey(v), rawValue); err != nil {
if err := writer.PutRawMVCCRangeKey(rangeKeys.AsRangeKey(v), rawValue); err != nil {
return roachpb.BulkOpSummary{}, MVCCKey{}, err
}
}
rows.BulkOpSummary.DataSize += rangeKeysSize
}

if rows.BulkOpSummary.DataSize == 0 {
// If no records were added to the sstable, skip completing it and return a
// nil slice – the export code will discard it anyway (based on 0 DataSize).
return roachpb.BulkOpSummary{}, MVCCKey{}, nil
}

if err := sstWriter.Finish(); err != nil {
return roachpb.BulkOpSummary{}, MVCCKey{}, err
}

return rows.BulkOpSummary, resumeKey, nil
}

Expand Down

0 comments on commit b49ba6c

Please sign in to comment.