Skip to content

Commit cc88aa0

Browse files
authored
Fix resolve token conflict logic under race condition (#7554)
* avoid duplicated tokens when using minimize spread token strategy Signed-off-by: Alex Le <leqiyue@amazon.com> * Revert "avoid duplicated tokens when using minimize spread token strategy" This reverts commit aff3b18. Signed-off-by: Alex Le <leqiyue@amazon.com> * fix resolve token conflict logic under race condition Signed-off-by: Alex Le <leqiyue@amazon.com> * only keep doing conflict check while instance is in joining state Signed-off-by: Alex Le <leqiyue@amazon.com> * update changelog Signed-off-by: Alex Le <leqiyue@amazon.com> * add more test Signed-off-by: Alex Le <leqiyue@amazon.com> * fix lint Signed-off-by: Alex Le <leqiyue@amazon.com> --------- Signed-off-by: Alex Le <leqiyue@amazon.com>
1 parent e0e8fc7 commit cc88aa0

4 files changed

Lines changed: 167 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* [BUGFIX] Security: Limit decompressed gzip output in `ParseProtoReader` and OTLP ingestion path. The decompressed body is now capped by `-distributor.otlp-max-recv-msg-size`. #7515
4141
* [BUGFIX] Tenant Federation: Fix regex resolver clearing known users list when user scan fails. #7534
4242
* [BUGFIX] Ingester: Release the TSDB appender on every early-return path in `Push` (e.g. out-of-order label set) by deferring `Rollback`. Previously such requests leaked TSDB head series references, mmap'd chunks and pending state per request, causing the `cortex_ingester_tsdb_head_active_appenders` gauge to grow unbounded. #7528
43+
* [BUGFIX] Ring: Fix ring token conflict resolution only applied to updated instance and make constantly token conflict check during instance observe period.
4344

4445
## 1.21.0 2026-04-24
4546

‎pkg/ring/merge_test.go‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,89 @@ func mergeLocalCAS(ring1, ring2 *Desc, nowUnixTime int64) (*Desc, *Desc) {
507507
changeRing := change.(*Desc)
508508
return ring1, changeRing
509509
}
510+
511+
func TestMergeResolvesConflictsDuringObservePeriod(t *testing.T) {
512+
now := time.Now().Unix()
513+
514+
// Ring has two ingesters with duplicate tokens. One is in JOINING state
515+
// (observe period), which should trigger conflict resolution even though
516+
// the incoming merge doesn't change tokens.
517+
ring1 := &Desc{
518+
Ingesters: map[string]InstanceDesc{
519+
"ing-A": {Addr: "addr-A", Timestamp: now, State: JOINING, Tokens: []uint32{1, 2, 3, 10, 20}},
520+
"ing-B": {Addr: "addr-B", Timestamp: now, State: JOINING, Tokens: []uint32{1, 2, 3, 30, 40}},
521+
},
522+
}
523+
524+
// ing-B sends a heartbeat (only timestamp changes, no token change)
525+
ring2 := &Desc{
526+
Ingesters: map[string]InstanceDesc{
527+
"ing-B": {Addr: "addr-B", Timestamp: now + 5, State: JOINING, Tokens: []uint32{1, 2, 3, 30, 40}},
528+
},
529+
}
530+
531+
change, err := ring1.Merge(ring2, false)
532+
assert.NoError(t, err)
533+
assert.NotNil(t, change)
534+
535+
// After merge, conflict should be resolved: ing-A wins tokens 1,2,3 (lower name)
536+
assert.Equal(t, []uint32{1, 2, 3, 10, 20}, ring1.Ingesters["ing-A"].Tokens)
537+
assert.Equal(t, []uint32{30, 40}, ring1.Ingesters["ing-B"].Tokens)
538+
}
539+
540+
func TestMergeSkipsConflictCheckWhenAllActive(t *testing.T) {
541+
now := time.Now().Unix()
542+
543+
// Ring has two ingesters with duplicate tokens but both are ACTIVE.
544+
// Without tokensChanged, the conflict check should NOT trigger
545+
// (steady-state optimization).
546+
ring1 := &Desc{
547+
Ingesters: map[string]InstanceDesc{
548+
"ing-A": {Addr: "addr-A", Timestamp: now, State: ACTIVE, Tokens: []uint32{1, 2, 3, 10, 20}},
549+
"ing-B": {Addr: "addr-B", Timestamp: now, State: ACTIVE, Tokens: []uint32{1, 2, 3, 30, 40}},
550+
},
551+
}
552+
553+
// ing-B sends a heartbeat (only timestamp changes)
554+
ring2 := &Desc{
555+
Ingesters: map[string]InstanceDesc{
556+
"ing-B": {Addr: "addr-B", Timestamp: now + 5, State: ACTIVE, Tokens: []uint32{1, 2, 3, 30, 40}},
557+
},
558+
}
559+
560+
change, err := ring1.Merge(ring2, false)
561+
assert.NoError(t, err)
562+
assert.NotNil(t, change)
563+
564+
// Conflict is NOT resolved because no ingester is JOINING and tokens didn't change
565+
assert.Equal(t, []uint32{1, 2, 3, 10, 20}, ring1.Ingesters["ing-A"].Tokens)
566+
assert.Equal(t, []uint32{1, 2, 3, 30, 40}, ring1.Ingesters["ing-B"].Tokens)
567+
}
568+
569+
func TestMergeResolvesConflictsWhenTokensChange(t *testing.T) {
570+
now := time.Now().Unix()
571+
572+
// Ring has two ACTIVE ingesters with duplicate tokens.
573+
// A new merge brings a token change, which triggers conflict resolution.
574+
ring1 := &Desc{
575+
Ingesters: map[string]InstanceDesc{
576+
"ing-A": {Addr: "addr-A", Timestamp: now, State: ACTIVE, Tokens: []uint32{1, 2, 3, 10, 20}},
577+
"ing-B": {Addr: "addr-B", Timestamp: now, State: ACTIVE, Tokens: []uint32{1, 2, 3, 30, 40}},
578+
},
579+
}
580+
581+
// ing-B updates with new tokens (e.g., after regeneration)
582+
ring2 := &Desc{
583+
Ingesters: map[string]InstanceDesc{
584+
"ing-B": {Addr: "addr-B", Timestamp: now + 5, State: ACTIVE, Tokens: []uint32{1, 2, 3, 30, 40, 50}},
585+
},
586+
}
587+
588+
change, err := ring1.Merge(ring2, false)
589+
assert.NoError(t, err)
590+
assert.NotNil(t, change)
591+
592+
// Conflict resolved: ing-A wins tokens 1,2,3
593+
assert.Equal(t, []uint32{1, 2, 3, 10, 20}, ring1.Ingesters["ing-A"].Tokens)
594+
assert.Equal(t, []uint32{30, 40, 50}, ring1.Ingesters["ing-B"].Tokens)
595+
}

‎pkg/ring/model.go‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,17 @@ func (d *Desc) mergeWithTime(mergeable memberlist.Mergeable, localCAS bool, now
289289
}
290290

291291
// resolveConflicts allocates lot of memory, so if we can avoid it, do that.
292-
if tokensChanged && conflictingTokensExist(thisIngesterMap) {
292+
// Check when tokens changed or any ingester is in JOINING state (observe period).
293+
shouldCheckConflicts := tokensChanged
294+
if !shouldCheckConflicts {
295+
for _, ing := range thisIngesterMap {
296+
if ing.State == JOINING {
297+
shouldCheckConflicts = true
298+
break
299+
}
300+
}
301+
}
302+
if shouldCheckConflicts && conflictingTokensExist(thisIngesterMap) {
293303
resolveConflicts(thisIngesterMap)
294304
}
295305

@@ -781,23 +791,40 @@ func (d *Desc) FindDifference(o codec.MultiKey) (any, []string, error) {
781791
} else if oing.Timestamp == ing.Timestamp && ing.State != LEFT && oing.State == LEFT {
782792
// we accept LEFT even if timestamp hasn't changed
783793
toUpdated.Ingesters[name] = oing
784-
if !tokensEqual(ing.Tokens, oing.Tokens) {
785-
tokensChanged = true
786-
}
794+
}
795+
}
796+
}
797+
798+
// Check for token conflicts when tokens changed or when any ingester is still
799+
// in JOINING state (observe period). During the observe period, concurrent joins
800+
// may have produced duplicate tokens that need resolution.
801+
shouldCheckConflicts := tokensChanged
802+
if !shouldCheckConflicts {
803+
for _, ing := range out.Ingesters {
804+
if ing.State == JOINING {
805+
shouldCheckConflicts = true
806+
break
787807
}
788808
}
789809
}
790810

791811
// resolveConflicts allocates a lot of memory, so if we can avoid it, do that.
792-
if tokensChanged && conflictingTokensExist(out.Ingesters) {
812+
if shouldCheckConflicts && conflictingTokensExist(out.Ingesters) {
793813
resolveConflicts(out.Ingesters)
794814

795-
//Recheck if any instance was updated by the resolveConflict
796-
//All ingesters in toUpdated have already passed the timestamp check, so we can skip checking again
815+
// Refresh all entries already in toUpdated (their tokens may have changed).
797816
for name := range toUpdated.Ingesters {
798-
//name must appear in out Ingesters, so we can skip the contains key check
799817
toUpdated.Ingesters[name] = out.Ingesters[name]
800818
}
819+
// Also include any existing ingester whose tokens were changed by resolveConflicts.
820+
for name, oing := range out.Ingesters {
821+
if _, alreadyUpdated := toUpdated.Ingesters[name]; alreadyUpdated {
822+
continue
823+
}
824+
if ing, ok := d.Ingesters[name]; ok && !tokensEqual(ing.Tokens, oing.Tokens) {
825+
toUpdated.Ingesters[name] = oing
826+
}
827+
}
801828
}
802829

803830
return toUpdated, toDelete, nil

‎pkg/ring/model_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
func TestInstanceDesc_IsHealthy_ForIngesterOperations(t *testing.T) {
@@ -746,6 +747,50 @@ func TestDesc_FindDifference(t *testing.T) {
746747
}
747748
}
748749

750+
func TestDesc_FindDifference_ConcurrentConflictResolutionIsDeterministic(t *testing.T) {
751+
// Simulate two ingesters with duplicate tokens both doing CAS at the same time
752+
// during the observe period (JOINING state). Both read the same DDB state (current)
753+
// and produce the same new state (out). FindDifference must produce identical
754+
// toUpdate results so they don't write conflicting resolutions to DDB.
755+
current := &Desc{Ingesters: map[string]InstanceDesc{
756+
"ing-A": {Addr: "addr-A", Tokens: []uint32{1, 2, 3, 10, 20}, Timestamp: 100, State: JOINING},
757+
"ing-B": {Addr: "addr-B", Tokens: []uint32{1, 2, 3, 30, 40}, Timestamp: 100, State: JOINING},
758+
}}
759+
760+
// ing-A does a heartbeat CAS (only timestamp changes)
761+
outA := &Desc{Ingesters: map[string]InstanceDesc{
762+
"ing-A": {Addr: "addr-A", Tokens: []uint32{1, 2, 3, 10, 20}, Timestamp: 110, State: JOINING},
763+
"ing-B": {Addr: "addr-B", Tokens: []uint32{1, 2, 3, 30, 40}, Timestamp: 100, State: JOINING},
764+
}}
765+
766+
// ing-B does a heartbeat CAS (only timestamp changes)
767+
outB := &Desc{Ingesters: map[string]InstanceDesc{
768+
"ing-A": {Addr: "addr-A", Tokens: []uint32{1, 2, 3, 10, 20}, Timestamp: 100, State: JOINING},
769+
"ing-B": {Addr: "addr-B", Tokens: []uint32{1, 2, 3, 30, 40}, Timestamp: 110, State: JOINING},
770+
}}
771+
772+
toUpdateA, _, errA := current.FindDifference(outA)
773+
toUpdateB, _, errB := current.FindDifference(outB)
774+
775+
require.NoError(t, errA)
776+
require.NoError(t, errB)
777+
778+
// Both must resolve the conflict: ing-A wins tokens 1,2,3 (lower name), ing-B loses them.
779+
updatedA := toUpdateA.(*Desc)
780+
updatedB := toUpdateB.(*Desc)
781+
782+
// ing-A's resolution should strip tokens 1,2,3 from ing-B
783+
require.Contains(t, updatedA.Ingesters, "ing-B")
784+
assert.Equal(t, []uint32{30, 40}, updatedA.Ingesters["ing-B"].Tokens)
785+
786+
// ing-B's resolution should also strip tokens 1,2,3 from ing-B
787+
require.Contains(t, updatedB.Ingesters, "ing-B")
788+
assert.Equal(t, []uint32{30, 40}, updatedB.Ingesters["ing-B"].Tokens)
789+
790+
// Both produce the same token assignment for ing-B — deterministic resolution.
791+
assert.Equal(t, updatedA.Ingesters["ing-B"].Tokens, updatedB.Ingesters["ing-B"].Tokens)
792+
}
793+
749794
func Test_resolveConflicts(t *testing.T) {
750795
tests := []struct {
751796
name string

0 commit comments

Comments
 (0)