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

Function TestLifecycler_ChangeReadOnlyState

ring/lifecycler_test.go:1007–1069  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1005}
1006
1007func TestLifecycler_ChangeReadOnlyState(t *testing.T) {
1008 t.Parallel()
1009
1010 ringStore, closer := consul.NewInMemoryClient(GetCodec(), log.NewNopLogger(), nil)
1011 t.Cleanup(func() { assert.NoError(t, closer.Close()) })
1012
1013 var ringConfig Config
1014 flagext.DefaultValues(&ringConfig)
1015 ringConfig.KVStore.Mock = ringStore
1016
1017 ctx := context.Background()
1018
1019 createLifecyclerFn := func(id string, reg prometheus.Registerer) *Lifecycler {
1020 // Add the first ingester to the ring
1021 lifecyclerConfig1 := testLifecyclerConfig(ringConfig, id)
1022 lifecyclerConfig1.HeartbeatPeriod = 100 * time.Millisecond
1023 lifecyclerConfig1.JoinAfter = 100 * time.Millisecond
1024
1025 lifecycler, err := NewLifecycler(lifecyclerConfig1, &nopFlushTransferer{}, "ingester", ringKey, true, log.NewNopLogger(), reg)
1026 require.NoError(t, err)
1027 assert.Equal(t, 0, lifecycler.HealthyInstancesCount())
1028 require.NoError(t, services.StartAndAwaitRunning(ctx, lifecycler))
1029 t.Cleanup(func() {
1030 assert.NoError(t, services.StopAndAwaitTerminated(ctx, lifecycler))
1031 })
1032 return lifecycler
1033 }
1034
1035 reg := prometheus.NewRegistry()
1036 lifecycler1 := createLifecyclerFn("ing1", reg)
1037 lifecycler2 := createLifecyclerFn("ing2", nil)
1038
1039 require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
1040 # HELP lifecycler_read_only Set to 1 if this lifecycler's instance entry is in read-only state.
1041 # TYPE lifecycler_read_only gauge
1042 lifecycler_read_only{name="ingester"} 0
1043 `), "lifecycler_read_only"))
1044
1045 // Assert the ingester has joined both rings
1046 test.Poll(t, time.Second, true, func() interface{} {
1047 return lifecycler1.HealthyInstancesCount() == 2 && lifecycler2.HealthyInstancesCount() == 2
1048 })
1049
1050 ro, ts := lifecycler1.GetReadOnlyState()
1051 require.False(t, ro)
1052 require.Zero(t, ts)
1053 require.NoError(t, lifecycler1.ChangeReadOnlyState(ctx, true))
1054
1055 ro, ts = lifecycler1.GetReadOnlyState()
1056 require.True(t, ro)
1057 require.NotZero(t, ts)
1058
1059 require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
1060 # HELP lifecycler_read_only Set to 1 if this lifecycler's instance entry is in read-only state.
1061 # TYPE lifecycler_read_only gauge
1062 lifecycler_read_only{name="ingester"} 1
1063 `), "lifecycler_read_only"))
1064

Callers

nothing calls this directly

Calls 14

HealthyInstancesCountMethod · 0.95
NewInMemoryClientFunction · 0.92
DefaultValuesFunction · 0.92
StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
PollFunction · 0.92
GetCodecFunction · 0.85
testLifecyclerConfigFunction · 0.85
NewLifecyclerFunction · 0.85
CloseMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected