@@ -315,12 +315,6 @@ func (i *Lifecycler) getTokens() Tokens {
315
315
return i .tokens
316
316
}
317
317
318
- func (i * Lifecycler ) getStateAndTokens () (InstanceState , Tokens ) {
319
- i .stateMtx .RLock ()
320
- defer i .stateMtx .RUnlock ()
321
- return i .state , i .tokens
322
- }
323
-
324
318
func (i * Lifecycler ) setTokens (tokens Tokens ) {
325
319
i .lifecyclerMetrics .tokensOwned .Set (float64 (len (tokens )))
326
320
@@ -440,7 +434,9 @@ func (i *Lifecycler) loop(ctx context.Context) error {
440
434
if uint64 (i .cfg .HeartbeatPeriod ) > 0 {
441
435
heartbeatTicker := time .NewTicker (i .cfg .HeartbeatPeriod )
442
436
heartbeatTicker .Stop ()
443
- time .AfterFunc (time .Duration (uint64 (mathrand .Int63 ())% uint64 (i .cfg .HeartbeatPeriod )), func () {
437
+ // We are jittering for at least half of the time and max the time of the heartbeat.
438
+ // If we jitter too soon, we can have problems of concurrency with autoJoin leaving the instance on ACTIVE without tokens
439
+ time .AfterFunc (time .Duration (uint64 (i .cfg .HeartbeatPeriod / 2 )+ uint64 (mathrand .Int63 ())% uint64 (i .cfg .HeartbeatPeriod / 2 )), func () {
444
440
i .heartbeat ()
445
441
heartbeatTicker .Reset (i .cfg .HeartbeatPeriod )
446
442
})
@@ -806,7 +802,7 @@ func (i *Lifecycler) updateConsul(ctx context.Context) error {
806
802
ringDesc .AddIngester (i .ID , i .Addr , i .Zone , i .getTokens (), i .GetState (), i .getRegisteredAt ())
807
803
} else {
808
804
instanceDesc .Timestamp = time .Now ().Unix ()
809
- instanceDesc .State , instanceDesc . Tokens = i .getStateAndTokens ()
805
+ instanceDesc .State = i .GetState ()
810
806
instanceDesc .Addr = i .Addr
811
807
instanceDesc .Zone = i .Zone
812
808
instanceDesc .RegisteredTimestamp = i .getRegisteredAt ().Unix ()
0 commit comments