@@ -21,6 +21,7 @@ import (
21
21
"fmt"
22
22
"math/rand"
23
23
"sync"
24
+ "sync/atomic"
24
25
"time"
25
26
26
27
"google.golang.org/protobuf/encoding/prototext"
@@ -185,6 +186,9 @@ type txThrottlerStateImpl struct {
185
186
186
187
healthCheck discovery.LegacyHealthCheck
187
188
topologyWatchers []TopologyWatcherInterface
189
+
190
+ shardMaxLag atomic.Int64
191
+ endChannel chan bool
188
192
}
189
193
190
194
// NewTxThrottler tries to construct a txThrottler from the
@@ -330,8 +334,9 @@ func newTxThrottlerState(topoServer *topo.Server, config *txThrottlerConfig, tar
330
334
return nil , err
331
335
}
332
336
result := & txThrottlerStateImpl {
333
- config : config ,
334
- throttler : t ,
337
+ config : config ,
338
+ throttler : t ,
339
+ endChannel : make (chan bool ),
335
340
}
336
341
result .healthCheck = healthCheckFactory ()
337
342
result .healthCheck .SetListener (result , false /* sendDownEvents */ )
@@ -349,6 +354,9 @@ func newTxThrottlerState(topoServer *topo.Server, config *txThrottlerConfig, tar
349
354
discovery .DefaultTopologyWatcherRefreshInterval ,
350
355
discovery .DefaultTopoReadConcurrency ))
351
356
}
357
+
358
+ go result .updateMaxShardLag ()
359
+
352
360
return result , nil
353
361
}
354
362
@@ -361,18 +369,31 @@ func (ts *txThrottlerStateImpl) throttle() bool {
361
369
ts .throttleMu .Lock ()
362
370
defer ts .throttleMu .Unlock ()
363
371
364
- var maxLag uint32
365
-
366
- for _ , tabletType := range ts .config .tabletTypes {
367
- maxLagPerTabletType := ts .throttler .LastMaxLagNotIgnoredForTabletType (tabletType )
368
- if maxLagPerTabletType > maxLag {
369
- maxLag = maxLagPerTabletType
370
- }
371
- }
372
+ maxLag := ts .shardMaxLag .Load ()
372
373
373
374
return ts .throttler .Throttle (0 /* threadId */ ) > 0 &&
374
- int64 (maxLag ) > ts .config .throttlerConfig .TargetReplicationLagSec
375
+ maxLag > ts .config .throttlerConfig .TargetReplicationLagSec
376
+ }
375
377
378
+ func (ts * txThrottlerStateImpl ) updateMaxShardLag () {
379
+ // We use half of the target lag to ensure we have enough resolution to see changes in lag below that value
380
+ ticker := time .NewTicker (time .Duration (ts .config .throttlerConfig .TargetReplicationLagSec / 2 ) * time .Second )
381
+ for {
382
+ select {
383
+ case _ = <- ticker .C :
384
+ var maxLag uint32
385
+
386
+ for _ , tabletType := range ts .config .tabletTypes {
387
+ maxLagPerTabletType := ts .throttler .LastMaxLagNotIgnoredForTabletType (tabletType )
388
+ if maxLagPerTabletType > maxLag {
389
+ maxLag = maxLagPerTabletType
390
+ }
391
+ }
392
+ ts .shardMaxLag .Store (int64 (maxLag ))
393
+ case _ = <- ts .endChannel :
394
+ break
395
+ }
396
+ }
376
397
}
377
398
378
399
func (ts * txThrottlerStateImpl ) deallocateResources () {
@@ -387,6 +408,7 @@ func (ts *txThrottlerStateImpl) deallocateResources() {
387
408
ts .healthCheck .Close ()
388
409
ts .healthCheck = nil
389
410
411
+ ts .endChannel <- true
390
412
// After ts.healthCheck is closed txThrottlerStateImpl.StatsUpdate() is guaranteed not
391
413
// to be executing, so we can safely close the throttler.
392
414
ts .throttler .Close ()
0 commit comments