MCPcopy
hub / github.com/grafana/dskit / testCheckingOfKeyOwnership

Function testCheckingOfKeyOwnership

ring/token_range_test.go:192–268  ·  view source on GitHub ↗
(t *testing.T, randomizeInstanceStates bool)

Source from the content-addressed store, hash-verified

190}
191
192func testCheckingOfKeyOwnership(t *testing.T, randomizeInstanceStates bool) {
193 const instancesPerZone = 20
194 const numZones = 3
195 const numTokens = 512
196 const replicationFactor = numZones // This is the only config supported by GetTokenRangesForInstance right now.
197
198 seed := time.Now().UnixNano()
199 t.Log("token generator seed:", seed)
200 gen := NewRandomTokenGeneratorWithSeed(seed)
201
202 stateRand := rand.New(rand.NewSource(seed))
203
204 // Generate users with different number of tokens
205 userTokens := map[string][]uint32{}
206 shardSizes := map[string]int{}
207 for _, cnt := range []int{1000, 5000, 10000, 25000, 50000, 100000} {
208 uid := fmt.Sprintf("%dk", cnt/1000)
209 userTokens[uid] = gen.GenerateTokens(cnt, nil)
210
211 shardSize := cnt / 7500
212 shardSize = (shardSize / numZones) * numZones // round down to numZones
213 if shardSize < numZones {
214 shardSize = numZones
215 }
216 shardSizes[uid] = shardSize
217 }
218
219 // Generate ring
220 ringDesc := &Desc{Ingesters: generateRingInstances(gen, instancesPerZone*numZones, numZones, numTokens)}
221
222 if randomizeInstanceStates {
223 for ins, ing := range ringDesc.Ingesters {
224 ing.State = InstanceState(stateRand.Int31n(int32(LEFT))) // LEFT is not state that clients can see, so we don't test it.
225 ringDesc.Ingesters[ins] = ing
226 }
227 }
228
229 ring := Ring{
230 cfg: Config{HeartbeatTimeout: time.Hour, ZoneAwarenessEnabled: true, SubringCacheDisabled: false, ReplicationFactor: replicationFactor},
231 ringDesc: ringDesc,
232 ringTokens: ringDesc.GetTokens(),
233 ringTokensByZone: ringDesc.getTokensByZone(),
234 ringInstanceByToken: ringDesc.getTokensInfo(),
235 ringZones: getZones(ringDesc.getTokensByZone()),
236 shuffledSubringCache: map[subringCacheKey]*Ring{},
237 strategy: nopReplicationStrategy{},
238 lastTopologyChange: time.Now(),
239 }
240
241 bufInstances, bufHosts, bufZones := MakeBuffersForGet()
242
243 for uid, tokens := range userTokens {
244 shardSize := shardSizes[uid]
245
246 subRing := ring.ShuffleShard(uid, shardSize)
247 sr := subRing.(*Ring)
248
249 for instanceID := range sr.ringDesc.Ingesters {

Callers 1

Calls 15

GetTokensMethod · 0.95
getTokensByZoneMethod · 0.95
getTokensInfoMethod · 0.95
ShuffleShardMethod · 0.95
generateRingInstancesFunction · 0.85
InstanceStateTypeAlias · 0.85
getZonesFunction · 0.85
MakeBuffersForGetFunction · 0.85
IncludesKeyMethod · 0.80
GenerateTokensMethod · 0.65

Tested by

no test coverage detected