Skip to content

Commit 9f5aa39

Browse files
committed
Handle PR comments and improved tests
Signed-off-by: Roy Chiang <[email protected]>
1 parent eeabb3a commit 9f5aa39

File tree

2 files changed

+75
-57
lines changed

2 files changed

+75
-57
lines changed

pkg/ring/ring.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -342,29 +342,26 @@ func (r *Ring) Get(key uint32, op Operation, bufDescs []InstanceDesc, bufHosts,
342342
continue
343343
}
344344

345-
instance := r.ringDesc.Ingesters[info.InstanceID]
346-
347-
// Check whether the replica set should be extended given we're including
348-
// this instance.
349-
shouldExtendReplicaSet := op.ShouldExtendReplicaSetOnState(instance.State)
350-
if shouldExtendReplicaSet {
351-
n++
352-
}
353-
354345
// Ignore if the instances don't have a zone set.
355346
if r.cfg.ZoneAwarenessEnabled && info.Zone != "" {
356347
if util.StringsContain(distinctZones, info.Zone) {
357348
continue
358349
}
350+
}
359351

360-
// We should only add instance zone if we are not going to extend,
352+
distinctHosts = append(distinctHosts, info.InstanceID)
353+
instance := r.ringDesc.Ingesters[info.InstanceID]
354+
355+
// Check whether the replica set should be extended given we're including
356+
// this instance.
357+
if op.ShouldExtendReplicaSetOnState(instance.State) {
358+
n++
359+
} else if r.cfg.ZoneAwarenessEnabled && info.Zone != "" {
360+
// We should only add the zone if we are not going to extend,
361361
// as we want to extend the instance in the same AZ.
362-
if !shouldExtendReplicaSet {
363-
distinctZones = append(distinctZones, info.Zone)
364-
}
362+
distinctZones = append(distinctZones, info.Zone)
365363
}
366364

367-
distinctHosts = append(distinctHosts, info.InstanceID)
368365
instances = append(instances, instance)
369366
}
370367

pkg/ring/ring_test.go

+64-43
Original file line numberDiff line numberDiff line change
@@ -135,56 +135,77 @@ func TestAddIngesterReplacesExistingTokens(t *testing.T) {
135135
func TestRing_Get_ZoneAwarenessWithIngesterLeaving(t *testing.T) {
136136
const testCount = 10000
137137

138-
r := NewDesc()
139-
instances := map[string]InstanceDesc{
140-
"instance-1": {Addr: "127.0.0.1", Zone: "zone-a", Tokens: GenerateTokens(128, nil), State: ACTIVE},
141-
"instance-2": {Addr: "127.0.0.2", Zone: "zone-a", Tokens: GenerateTokens(128, nil), State: ACTIVE},
142-
"instance-3": {Addr: "127.0.0.3", Zone: "zone-b", Tokens: GenerateTokens(128, nil), State: ACTIVE},
143-
"instance-4": {Addr: "127.0.0.4", Zone: "zone-b", Tokens: GenerateTokens(128, nil), State: ACTIVE},
144-
"instance-5": {Addr: "127.0.0.5", Zone: "zone-c", Tokens: GenerateTokens(128, nil), State: LEAVING},
145-
"instance-6": {Addr: "127.0.0.6", Zone: "zone-c", Tokens: GenerateTokens(128, nil), State: ACTIVE},
146-
}
147-
var prevTokens []uint32
148-
for id, instance := range instances {
149-
ingTokens := GenerateTokens(128, prevTokens)
150-
r.AddIngester(id, instance.Addr, instance.Zone, ingTokens, instance.State, time.Now())
151-
prevTokens = append(prevTokens, ingTokens...)
152-
}
153-
instancesList := make([]InstanceDesc, 0, len(r.GetIngesters()))
154-
for _, v := range r.GetIngesters() {
155-
instancesList = append(instancesList, v)
156-
}
157-
158-
ring := Ring{
159-
cfg: Config{
160-
HeartbeatTimeout: time.Hour,
161-
ReplicationFactor: 3,
162-
ZoneAwarenessEnabled: true,
138+
tests := map[string]struct {
139+
replicationFactor int
140+
expectedInstances int
141+
expectedZones int
142+
}{
143+
"should succeed if there are enough instances per zone on RF = 3": {
144+
replicationFactor: 3,
145+
expectedInstances: 3,
146+
expectedZones: 3,
147+
},
148+
"should succeed if there are enough instances per zone on RF = 2": {
149+
replicationFactor: 2,
150+
expectedInstances: 2,
151+
expectedZones: 2,
163152
},
164-
ringDesc: r,
165-
ringTokens: r.GetTokens(),
166-
ringTokensByZone: r.getTokensByZone(),
167-
ringInstanceByToken: r.getTokensInfo(),
168-
ringZones: getZones(r.getTokensByZone()),
169-
strategy: NewDefaultReplicationStrategy(),
170153
}
171154

172-
_, bufHosts, bufZones := MakeBuffersForGet()
155+
for testName, testData := range tests {
156+
t.Run(testName, func(t *testing.T) {
157+
r := NewDesc()
158+
instances := map[string]InstanceDesc{
159+
"instance-1": {Addr: "127.0.0.1", Zone: "zone-a", State: ACTIVE},
160+
"instance-2": {Addr: "127.0.0.2", Zone: "zone-a", State: ACTIVE},
161+
"instance-3": {Addr: "127.0.0.3", Zone: "zone-b", State: ACTIVE},
162+
"instance-4": {Addr: "127.0.0.4", Zone: "zone-b", State: ACTIVE},
163+
"instance-5": {Addr: "127.0.0.5", Zone: "zone-c", State: LEAVING},
164+
"instance-6": {Addr: "127.0.0.6", Zone: "zone-c", State: ACTIVE},
165+
}
166+
var prevTokens []uint32
167+
for id, instance := range instances {
168+
ingTokens := GenerateTokens(128, prevTokens)
169+
r.AddIngester(id, instance.Addr, instance.Zone, ingTokens, instance.State, time.Now())
170+
prevTokens = append(prevTokens, ingTokens...)
171+
}
172+
instancesList := make([]InstanceDesc, 0, len(r.GetIngesters()))
173+
for _, v := range r.GetIngesters() {
174+
instancesList = append(instancesList, v)
175+
}
176+
177+
ring := Ring{
178+
cfg: Config{
179+
HeartbeatTimeout: time.Hour,
180+
ReplicationFactor: testData.replicationFactor,
181+
ZoneAwarenessEnabled: true,
182+
},
183+
ringDesc: r,
184+
ringTokens: r.GetTokens(),
185+
ringTokensByZone: r.getTokensByZone(),
186+
ringInstanceByToken: r.getTokensInfo(),
187+
ringZones: getZones(r.getTokensByZone()),
188+
strategy: NewDefaultReplicationStrategy(),
189+
}
173190

174-
// Use the GenerateTokens to get an array of random uint32 values.
175-
testValues := GenerateTokens(testCount, nil)
191+
_, bufHosts, bufZones := MakeBuffersForGet()
176192

177-
for i := 0; i < testCount; i++ {
178-
set, err := ring.Get(testValues[i], Write, instancesList, bufHosts, bufZones)
179-
require.NoError(t, err)
193+
// Use the GenerateTokens to get an array of random uint32 values.
194+
testValues := GenerateTokens(testCount, nil)
180195

181-
distinctZones := map[string]int{}
182-
for _, instance := range set.Instances {
183-
distinctZones[instance.Zone]++
184-
}
196+
for i := 0; i < testCount; i++ {
197+
set, err := ring.Get(testValues[i], Write, instancesList, bufHosts, bufZones)
198+
require.NoError(t, err)
199+
200+
distinctZones := map[string]int{}
201+
for _, instance := range set.Instances {
202+
distinctZones[instance.Zone]++
203+
}
185204

186-
assert.Equal(t, len(set.Instances), 3)
187-
assert.Equal(t, len(distinctZones), 3)
205+
assert.Len(t, set.Instances, testData.expectedInstances)
206+
assert.Len(t, distinctZones, testData.expectedZones)
207+
}
208+
})
188209
}
189210
}
190211

0 commit comments

Comments
 (0)