diff --git a/pkg/kv/kvserver/batcheval/cmd_export.go b/pkg/kv/kvserver/batcheval/cmd_export.go index fa5d1a567b7f..8a4c491c76dd 100644 --- a/pkg/kv/kvserver/batcheval/cmd_export.go +++ b/pkg/kv/kvserver/batcheval/cmd_export.go @@ -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 @@ -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 diff --git a/pkg/storage/mvcc.go b/pkg/storage/mvcc.go index 56fe725f10eb..7bc9be49620b 100644 --- a/pkg/storage/mvcc.go +++ b/pkg/storage/mvcc.go @@ -5766,8 +5766,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, @@ -5787,16 +5812,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. @@ -5935,7 +5958,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 } } @@ -6046,11 +6069,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) } } @@ -6108,23 +6131,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 }