66package changefeedccl
77
88import (
9+ "cmp"
910 "context"
11+ "maps"
1012 "slices"
1113 "strings"
1214 "sync/atomic"
@@ -25,6 +27,7 @@ import (
2527 "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
2628 "github.com/cockroachdb/cockroach/pkg/util/cidr"
2729 "github.com/cockroachdb/cockroach/pkg/util/hlc"
30+ "github.com/cockroachdb/cockroach/pkg/util/iterutil"
2831 "github.com/cockroachdb/cockroach/pkg/util/log/logcrash"
2932 "github.com/cockroachdb/cockroach/pkg/util/metric"
3033 "github.com/cockroachdb/cockroach/pkg/util/metric/aggmetric"
@@ -188,11 +191,11 @@ type sliMetrics struct {
188191
189192 mu struct {
190193 syncutil.Mutex
191- id int64
192- resolved map [int64 ]hlc.Timestamp
193- checkpoint map [int64 ]hlc.Timestamp
194- fastestSpan map [int64 ]hlc. Timestamp
195- fastestTable map [int64 ]hlc. Timestamp
194+ id int64
195+ resolved map [int64 ]hlc.Timestamp
196+ checkpoint map [int64 ]hlc.Timestamp
197+ spanSkew map [int64 ]int64
198+ tableSkew map [int64 ]int64
196199 }
197200 NetMetrics * cidr.NetMetrics
198201
@@ -206,8 +209,8 @@ func (m *sliMetrics) closeId(id int64) {
206209 defer m .mu .Unlock ()
207210 delete (m .mu .checkpoint , id )
208211 delete (m .mu .resolved , id )
209- delete (m .mu .fastestSpan , id )
210- delete (m .mu .fastestTable , id )
212+ delete (m .mu .spanSkew , id )
213+ delete (m .mu .tableSkew , id )
211214}
212215
213216// setResolved writes a resolved timestamp entry for the given id.
@@ -228,15 +231,15 @@ func (m *sliMetrics) setCheckpoint(id int64, ts hlc.Timestamp) {
228231 }
229232}
230233
231- // setFastestTS saves the fastest span/table timestamps for a given id .
232- func (m * sliMetrics ) setFastestTS (id int64 , spanTS hlc. Timestamp , tableTS hlc. Timestamp ) {
234+ // setProgressSkew saves the span skew /table skew for a given ID .
235+ func (m * sliMetrics ) setProgressSkew (id int64 , spanSkew int64 , tableSkew int64 ) {
233236 m .mu .Lock ()
234237 defer m .mu .Unlock ()
235- if _ , ok := m .mu .fastestSpan [id ]; ok {
236- m .mu .fastestSpan [id ] = spanTS
238+ if _ , ok := m .mu .spanSkew [id ]; ok {
239+ m .mu .spanSkew [id ] = spanSkew
237240 }
238- if _ , ok := m .mu .fastestTable [id ]; ok {
239- m .mu .fastestTable [id ] = tableTS
241+ if _ , ok := m .mu .tableSkew [id ]; ok {
242+ m .mu .tableSkew [id ] = tableSkew
240243 }
241244}
242245
@@ -249,8 +252,8 @@ func (m *sliMetrics) claimId() int64 {
249252 // ignored until a nonzero timestamp is written.
250253 m .mu .checkpoint [id ] = hlc.Timestamp {}
251254 m .mu .resolved [id ] = hlc.Timestamp {}
252- m .mu .fastestSpan [id ] = hlc. Timestamp {}
253- m .mu .fastestTable [id ] = hlc. Timestamp {}
255+ m .mu .spanSkew [id ] = 0
256+ m .mu .tableSkew [id ] = 0
254257 m .mu .id ++
255258 return id
256259}
@@ -1296,8 +1299,8 @@ func (a *AggMetrics) getOrCreateScope(scope string) (*sliMetrics, error) {
12961299 }
12971300 sm .mu .resolved = make (map [int64 ]hlc.Timestamp )
12981301 sm .mu .checkpoint = make (map [int64 ]hlc.Timestamp )
1299- sm .mu .fastestSpan = make (map [int64 ]hlc. Timestamp )
1300- sm .mu .fastestTable = make (map [int64 ]hlc. Timestamp )
1302+ sm .mu .spanSkew = make (map [int64 ]int64 )
1303+ sm .mu .tableSkew = make (map [int64 ]int64 )
13011304 sm .mu .id = 1 // start the first id at 1 so we can detect intiialization
13021305
13031306 minTimestampGetter := func (m map [int64 ]hlc.Timestamp ) func () int64 {
@@ -1328,34 +1331,21 @@ func (a *AggMetrics) getOrCreateScope(scope string) (*sliMetrics, error) {
13281331 }
13291332 }
13301333
1331- maxTimestampSkewGetter := func (
1332- base map [int64 ]hlc.Timestamp , ahead map [int64 ]hlc.Timestamp ,
1333- ) func () int64 {
1334+ maxTimestampSkewGetter := func (m map [int64 ]int64 ) func () int64 {
13341335 return func () int64 {
13351336 sm .mu .Lock ()
13361337 defer sm .mu .Unlock ()
1337- var maxSkew int64
1338- for id , b := range base {
1339- a := ahead [id ]
1340- if a .IsEmpty () || b .IsEmpty () {
1341- continue
1342- }
1343- skew := a .WallTime - b .WallTime
1344- if skew > maxSkew {
1345- maxSkew = skew
1346- }
1347- }
1348- return maxSkew
1338+ return iterutil .MaxFunc (maps .Values (m ), cmp .Compare )
13491339 }
13501340 }
13511341
13521342 sm .AggregatorProgress = a .AggregatorProgress .AddFunctionalChild (minTimestampGetter (sm .mu .resolved ), scope )
13531343 sm .CheckpointProgress = a .CheckpointProgress .AddFunctionalChild (minTimestampGetter (sm .mu .checkpoint ), scope )
13541344 sm .MaxBehindNanos = a .MaxBehindNanos .AddFunctionalChild (maxBehindNanosGetter (sm .mu .resolved ), scope )
13551345 sm .SpanProgressSkew = a .SpanProgressSkew .AddFunctionalChild (
1356- maxTimestampSkewGetter (sm .mu .checkpoint , sm . mu . fastestSpan ), scope )
1346+ maxTimestampSkewGetter (sm .mu .spanSkew ), scope )
13571347 sm .TableProgressSkew = a .TableProgressSkew .AddFunctionalChild (
1358- maxTimestampSkewGetter (sm .mu .checkpoint , sm . mu . fastestTable ), scope )
1348+ maxTimestampSkewGetter (sm .mu .tableSkew ), scope )
13591349
13601350 a .mu .sliMetrics [scope ] = sm
13611351 return sm , nil
@@ -1364,7 +1354,7 @@ func (a *AggMetrics) getOrCreateScope(scope string) (*sliMetrics, error) {
13641354// getLaggingRangesCallback returns a function which can be called to update the
13651355// lagging ranges metric. It should be called with the current number of lagging
13661356// ranges.
1367- func (s * sliMetrics ) getLaggingRangesCallback () func (lagging int64 , total int64 ) {
1357+ func (m * sliMetrics ) getLaggingRangesCallback () func (lagging int64 , total int64 ) {
13681358 // Because this gauge is shared between changefeeds in the same metrics scope,
13691359 // we must instead modify it using `Inc` and `Dec` (as opposed to `Update`) to
13701360 // ensure values written by others are not overwritten. The code below is used
@@ -1388,10 +1378,10 @@ func (s *sliMetrics) getLaggingRangesCallback() func(lagging int64, total int64)
13881378 last .Lock ()
13891379 defer last .Unlock ()
13901380
1391- s .LaggingRanges .Dec (last .lagging - lagging )
1381+ m .LaggingRanges .Dec (last .lagging - lagging )
13921382 last .lagging = lagging
13931383
1394- s .TotalRanges .Dec (last .total - total )
1384+ m .TotalRanges .Dec (last .total - total )
13951385 last .total = total
13961386 }
13971387}
0 commit comments