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

Function TestRing_ShuffleShard_Caching

ring/ring_test.go:4075–4207  ·  view source on GitHub ↗

This test checks if shuffle-sharded ring can be reused, and whether it receives updates from "main" ring.

(t *testing.T)

Source from the content-addressed store, hash-verified

4073// This test checks if shuffle-sharded ring can be reused, and whether it receives
4074// updates from "main" ring.
4075func TestRing_ShuffleShard_Caching(t *testing.T) {
4076 t.Parallel()
4077
4078 inmem, closer := consul.NewInMemoryClientWithConfig(GetCodec(), consul.Config{
4079 MaxCasRetries: 20,
4080 CasRetryDelay: 500 * time.Millisecond,
4081 }, log.NewNopLogger(), nil)
4082 t.Cleanup(func() { assert.NoError(t, closer.Close()) })
4083
4084 cfg := Config{
4085 KVStore: kv.Config{Mock: inmem},
4086 HeartbeatTimeout: 1 * time.Minute,
4087 ReplicationFactor: 3,
4088 ZoneAwarenessEnabled: true,
4089 }
4090
4091 ring, err := New(cfg, "test", "test", log.NewNopLogger(), nil)
4092 require.NoError(t, err)
4093 require.NoError(t, services.StartAndAwaitRunning(context.Background(), ring))
4094 t.Cleanup(func() {
4095 _ = services.StopAndAwaitTerminated(context.Background(), ring)
4096 })
4097
4098 // We will stop <number of zones> instances later, to see that subring is recomputed.
4099 const zones = 3
4100 const numLifecyclers = zones * 3 // 3 instances in each zone.
4101
4102 lcs := []*Lifecycler(nil)
4103 for i := 0; i < numLifecyclers; i++ {
4104 lc := startLifecycler(t, cfg, 500*time.Millisecond, i, zones)
4105 lcs = append(lcs, lc)
4106 }
4107
4108 // Wait until all instances in the ring are ACTIVE.
4109 test.Poll(t, 5*time.Second, numLifecyclers, func() interface{} {
4110 active := 0
4111 rs, _ := ring.GetReplicationSetForOperation(Read)
4112 for _, ing := range rs.Instances {
4113 if ing.State == ACTIVE {
4114 active++
4115 }
4116 }
4117 return active
4118 })
4119
4120 // Use shardSize = zones, to get one instance from each zone.
4121 const shardSize = zones
4122 const user = "user"
4123
4124 // This subring should be cached, and reused.
4125 subring := ring.ShuffleShard(user, shardSize)
4126
4127 // Do 100 iterations over two seconds. Make sure we get the same subring.
4128 const iters = 100
4129 sleep := (2 * time.Second) / iters
4130 for i := 0; i < iters; i++ {
4131 newSubring := ring.ShuffleShard(user, shardSize)
4132 require.Same(t, subring, newSubring, "cached subring reused")

Callers

nothing calls this directly

Calls 15

StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
PollFunction · 0.92
GetCodecFunction · 0.85
startLifecyclerFunction · 0.85
readOnlyInstanceCountMethod · 0.80
NewFunction · 0.70
CloseMethod · 0.65
ShuffleShardMethod · 0.65
InstancesCountMethod · 0.65

Tested by

no test coverage detected