Skip to content

Commit d1b6d26

Browse files
authored
Calculate current ownership when generating tokens for an ingester that already have tokens (#6062)
Signed-off-by: alanprot <[email protected]>
1 parent b26bc82 commit d1b6d26

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [BUGFIX] Configsdb: Fix endline issue in db password. #5920
2323
* [BUGFIX] Ingester: Fix `user` and `type` labels for the `cortex_ingester_tsdb_head_samples_appended_total` TSDB metric. #5952
2424
* [BUGFIX] Querier: Enforce max query length check for `/api/v1/series` API even though `ignoreMaxQueryLength` is set to true. #6018
25+
* [BUGFIX] Ingester: Fix issue with the minimize token generator where it was not taking in consideration the current ownerhip of an instance when generating extra tokens. #6062
2526

2627
## 1.17.1 2024-05-20
2728

pkg/ring/token_generator.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,15 @@ func (g *MinimizeSpreadTokenGenerator) GenerateTokens(ring *Desc, id, zone strin
9595
}
9696

9797
// Only take in consideration tokens from instances in the same AZ
98-
if i != id && instance.Zone == zone {
99-
instanceTokens = append(instanceTokens, instance.Tokens)
98+
if instance.Zone != zone {
99+
continue
100+
}
101+
102+
instanceTokens = append(instanceTokens, instance.Tokens)
103+
104+
// Do not add the current instance on the tokensPerInstanceWithDistance map as it will be used to create the heap
105+
// to calculate from what instance we should take ownership
106+
if i != id {
100107
tokensPerInstanceWithDistance[i] = &totalTokenPerInstance{id: i, zone: instance.Zone}
101108

102109
if len(instance.Tokens) == 0 {
@@ -117,6 +124,7 @@ func (g *MinimizeSpreadTokenGenerator) GenerateTokens(ring *Desc, id, zone strin
117124
}
118125

119126
zonalTokens := MergeTokens(instanceTokens)
127+
currentInstance := &totalTokenPerInstance{id: id, zone: zone}
120128

121129
// If we don't have tokens to split, lets create the tokens randomly
122130
if len(zonalTokens) == 0 {
@@ -127,14 +135,22 @@ func (g *MinimizeSpreadTokenGenerator) GenerateTokens(ring *Desc, id, zone strin
127135
// This map will be later on used to create the heap in order to take tokens from the ingesters with most distance
128136
for i := 1; i <= len(zonalTokens); i++ {
129137
index := i % len(zonalTokens)
130-
if id, ok := usedTokens[zonalTokens[index]]; ok {
131-
instanceDistance := tokensPerInstanceWithDistance[id]
138+
if tokenInstanceId, ok := usedTokens[zonalTokens[index]]; ok && tokenInstanceId != id {
139+
instanceDistance := tokensPerInstanceWithDistance[tokenInstanceId]
132140
instanceDistance.tokens = append(instanceDistance.tokens, &tokenDistanceEntry{
133141
token: zonalTokens[index],
134142
prev: zonalTokens[i-1],
135143
distance: tokenDistance(zonalTokens[i-1], zonalTokens[index]),
136144
})
137145
instanceDistance.totalDistance += tokenDistance(zonalTokens[i-1], zonalTokens[index])
146+
} else if tokenInstanceId == id {
147+
// If the token is owned by the current instance, lets calculate the current distance
148+
currentInstance.tokens = append(currentInstance.tokens, &tokenDistanceEntry{
149+
token: zonalTokens[index],
150+
prev: zonalTokens[i-1],
151+
distance: tokenDistance(zonalTokens[i-1], zonalTokens[index]),
152+
})
153+
currentInstance.totalDistance += tokenDistance(zonalTokens[i-1], zonalTokens[index])
138154
}
139155
}
140156

@@ -149,7 +165,6 @@ func (g *MinimizeSpreadTokenGenerator) GenerateTokens(ring *Desc, id, zone strin
149165

150166
heap.Init(distancesHeap)
151167

152-
currentInstance := &totalTokenPerInstance{id: id, zone: zone}
153168
expectedOwnership := float64(1) / (float64(len(tokensPerInstanceWithDistance) + 1))
154169
expectedOwnershipDistance := int64(expectedOwnership * maxTokenValue)
155170

pkg/ring/token_generator_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ func TestMinimizeSpreadTokenGenerator(t *testing.T) {
9797
}
9898
require.Equal(t, mTokenGenerator.called, len(zones))
9999

100+
// Test generating tokens for an ingester that already have tokens in the ring
101+
rg := NewRandomTokenGenerator()
102+
rTokens := rg.GenerateTokens(rindDesc, "partial", zones[0], 256, true)
103+
rindDesc.AddIngester("partial", "partial", zones[0], rTokens, ACTIVE, time.Now())
104+
nTokens := minimizeTokenGenerator.GenerateTokens(rindDesc, "partial", zones[0], 256, true)
105+
rindDesc.AddIngester("partial", "partial", zones[0], append(rTokens, nTokens...), ACTIVE, time.Now())
106+
assertDistancePerIngester(t, rindDesc, 0.01)
107+
100108
mTokenGenerator.called = 0
101109
// Should fallback to random generator when more than 1 ingester does not have tokens and force flag is set
102110
rindDesc.AddIngester("pendingIngester-1", "pendingIngester-1", zones[0], []uint32{}, PENDING, time.Now())

0 commit comments

Comments
 (0)