From 90bc665a2169b1abc2bdf11db3c4df98360fef34 Mon Sep 17 00:00:00 2001 From: Yevgeniy Miretskiy Date: Fri, 8 Sep 2023 10:06:36 -0400 Subject: [PATCH] changefeedccl: Fix data race in lagging spans metric Fix a race bug in lagging spans metric. Fixes: #110235 Release note: None --- pkg/ccl/changefeedccl/changefeed_test.go | 2 ++ pkg/ccl/changefeedccl/metrics.go | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/ccl/changefeedccl/changefeed_test.go b/pkg/ccl/changefeedccl/changefeed_test.go index f486987c1400..bed759932ede 100644 --- a/pkg/ccl/changefeedccl/changefeed_test.go +++ b/pkg/ccl/changefeedccl/changefeed_test.go @@ -1736,6 +1736,8 @@ func TestNoBackfillAfterNonTargetColumnDrop(t *testing.T) { func TestChangefeedColumnDropsWithFamilyAndNonFamilyTargets(t *testing.T) { defer leaktest.AfterTest(t)() + defer log.Scope(t).Close(t) + testFn := func(t *testing.T, s TestServer, f cdctest.TestFeedFactory) { sqlDB := sqlutils.MakeSQLRunner(s.DB) diff --git a/pkg/ccl/changefeedccl/metrics.go b/pkg/ccl/changefeedccl/metrics.go index a2676eaeb2a6..789b27ae63f0 100644 --- a/pkg/ccl/changefeedccl/metrics.go +++ b/pkg/ccl/changefeedccl/metrics.go @@ -804,10 +804,15 @@ func (s *sliMetrics) getLaggingRangesCallback() func(int64) { // If 3 ranges catch up, last=10,i=7: X.Dec(10 - 7) = X.Dec(3) // If 4 ranges fall behind, last=7,i=11: X.Dec(7 - 11) = X.Inc(4) // If 1 lagging range is deleted, last=7,i=10: X.Dec(11-10) = X.Dec(1) - var last int64 + last := struct { + syncutil.Mutex + v int64 + }{} return func(i int64) { - s.LaggingRanges.Dec(last - i) - last = i + last.Lock() + defer last.Unlock() + s.LaggingRanges.Dec(last.v - i) + last.v = i } }