From b03709546eccdb701a0a660b3b1a2356f62d17c0 Mon Sep 17 00:00:00 2001 From: Michael Butler Date: Mon, 22 Apr 2024 07:49:23 -0400 Subject: [PATCH] backupccl: copy spans before SubtractSpans calls Previously, backup assumed arguments to roachpb.SubtractSpans could be used, but the first arg is modified in place. This patch adds a wrapper to roachpb.SubtractSpans to copy the input spans before use. Informs #122672 Informs #122734 Informs #122754 Informs #122764 Release note: none --- pkg/ccl/backupccl/backup_job.go | 6 +++--- pkg/roachpb/merge_spans.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/ccl/backupccl/backup_job.go b/pkg/ccl/backupccl/backup_job.go index 4c0462da388a..b212d01d5a17 100644 --- a/pkg/ccl/backupccl/backup_job.go +++ b/pkg/ccl/backupccl/backup_job.go @@ -191,8 +191,8 @@ func backup( } // Subtract out any completed spans. - spans := roachpb.SubtractSpans(backupManifest.Spans, completedSpans) - introducedSpans := roachpb.SubtractSpans(backupManifest.IntroducedSpans, completedIntroducedSpans) + spans := roachpb.SubtractSpansWithCopy(backupManifest.Spans, completedSpans) + introducedSpans := roachpb.SubtractSpansWithCopy(backupManifest.IntroducedSpans, completedIntroducedSpans) pkIDs := make(map[uint64]bool) for i := range backupManifest.Descriptors { @@ -1649,7 +1649,7 @@ func createBackupManifest( } } - newSpans = roachpb.SubtractSpans(spans, prevBackups[len(prevBackups)-1].Spans) + newSpans = roachpb.SubtractSpansWithCopy(spans, prevBackups[len(prevBackups)-1].Spans) } // if CompleteDbs is lost by a 1.x node, FormatDescriptorTrackingVersion diff --git a/pkg/roachpb/merge_spans.go b/pkg/roachpb/merge_spans.go index 1e0444b05af7..ebb386bedf37 100644 --- a/pkg/roachpb/merge_spans.go +++ b/pkg/roachpb/merge_spans.go @@ -101,6 +101,16 @@ func MergeSpans(spans *[]Span) ([]Span, bool) { return r, distinct } +// SubtractSpansWithCopy is the same thing as Subtract spans, but copies the +// todo span first so it can be used after this function call. +func SubtractSpansWithCopy(todo, done Spans) Spans { + newTodo := make(Spans, 0, len(todo)) + for i := range todo { + newTodo = append(newTodo, todo[i].Clone()) + } + return SubtractSpans(newTodo, done) +} + // SubtractSpans subtracts the subspans covered by a set of non-overlapping // spans from another set of non-overlapping spans. //