Skip to content

Commit 8be7a44

Browse files
authored
Avoid concurrency between heartbeat and autoJoin (#5438)
Signed-off-by: Daniel Deluiggi <[email protected]>
1 parent f4e9af3 commit 8be7a44

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

pkg/ring/lifecycler.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,6 @@ func (i *Lifecycler) getTokens() Tokens {
315315
return i.tokens
316316
}
317317

318-
func (i *Lifecycler) getStateAndTokens() (InstanceState, Tokens) {
319-
i.stateMtx.RLock()
320-
defer i.stateMtx.RUnlock()
321-
return i.state, i.tokens
322-
}
323-
324318
func (i *Lifecycler) setTokens(tokens Tokens) {
325319
i.lifecyclerMetrics.tokensOwned.Set(float64(len(tokens)))
326320

@@ -440,7 +434,9 @@ func (i *Lifecycler) loop(ctx context.Context) error {
440434
if uint64(i.cfg.HeartbeatPeriod) > 0 {
441435
heartbeatTicker := time.NewTicker(i.cfg.HeartbeatPeriod)
442436
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() {
444440
i.heartbeat()
445441
heartbeatTicker.Reset(i.cfg.HeartbeatPeriod)
446442
})
@@ -806,7 +802,7 @@ func (i *Lifecycler) updateConsul(ctx context.Context) error {
806802
ringDesc.AddIngester(i.ID, i.Addr, i.Zone, i.getTokens(), i.GetState(), i.getRegisteredAt())
807803
} else {
808804
instanceDesc.Timestamp = time.Now().Unix()
809-
instanceDesc.State, instanceDesc.Tokens = i.getStateAndTokens()
805+
instanceDesc.State = i.GetState()
810806
instanceDesc.Addr = i.Addr
811807
instanceDesc.Zone = i.Zone
812808
instanceDesc.RegisteredTimestamp = i.getRegisteredAt().Unix()

pkg/ring/ring_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,7 @@ func startLifecycler(t *testing.T, cfg Config, heartbeat time.Duration, lifecycl
26322632
func TestShuffleShardWithCaching(t *testing.T) {
26332633
inmem, closer := consul.NewInMemoryClientWithConfig(GetCodec(), consul.Config{
26342634
MaxCasRetries: 20,
2635-
CasRetryDelay: 500 * time.Millisecond,
2635+
CasRetryDelay: 100 * time.Millisecond,
26362636
}, log.NewNopLogger(), nil)
26372637
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
26382638

@@ -2661,8 +2661,6 @@ func TestShuffleShardWithCaching(t *testing.T) {
26612661
lcs = append(lcs, lc)
26622662
}
26632663

2664-
time.Sleep(5 * time.Second)
2665-
26662664
// Wait until all instances in the ring are ACTIVE.
26672665
test.Poll(t, 5*time.Second, numLifecyclers, func() interface{} {
26682666
active := 0

0 commit comments

Comments
 (0)