Skip to content

Commit 2556418

Browse files
pstibranygouthamve
authored andcommitted
Explicit healty states (#1890)
* Be explicit about which states are considered healthy. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com> * When reporting, all states are OK and we only check timestamp. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com> * Update IsHealthy test to cover reporting. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com> * Use switch, it's bit nicer. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com> * Added parenthesis around state conditions. Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
1 parent 9bc49ce commit 2556418

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

pkg/ring/model.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,20 @@ func (d *Desc) TokensFor(id string) (tokens, other Tokens) {
182182

183183
// IsHealthy checks whether the ingester appears to be alive and heartbeating
184184
func (i *IngesterDesc) IsHealthy(op Operation, heartbeatTimeout time.Duration) bool {
185-
if op == Write && i.State != ACTIVE {
186-
return false
187-
} else if op == Read && i.State == JOINING {
188-
return false
185+
healthy := false
186+
187+
switch op {
188+
case Write:
189+
healthy = (i.State == ACTIVE)
190+
191+
case Read:
192+
healthy = (i.State == ACTIVE) || (i.State == LEAVING) || (i.State == PENDING)
193+
194+
case Reporting:
195+
healthy = true
189196
}
190-
return time.Now().Sub(time.Unix(i.Timestamp, 0)) <= heartbeatTimeout
197+
198+
return healthy && time.Now().Sub(time.Unix(i.Timestamp, 0)) <= heartbeatTimeout
191199
}
192200

193201
// Merge merges other ring into this one. Returns sub-ring that represents the change,

pkg/ring/model_test.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,39 @@ func TestIngesterDesc_IsHealthy(t *testing.T) {
1111
t.Parallel()
1212

1313
tests := map[string]struct {
14-
ingester *IngesterDesc
15-
timeout time.Duration
16-
writeExpected bool
17-
readExpected bool
14+
ingester *IngesterDesc
15+
timeout time.Duration
16+
writeExpected bool
17+
readExpected bool
18+
reportExpected bool
1819
}{
1920
"ALIVE ingester with last keepalive newer than timeout": {
20-
ingester: &IngesterDesc{State: ACTIVE, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
21-
timeout: time.Minute,
22-
writeExpected: true,
23-
readExpected: true,
21+
ingester: &IngesterDesc{State: ACTIVE, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
22+
timeout: time.Minute,
23+
writeExpected: true,
24+
readExpected: true,
25+
reportExpected: true,
2426
},
2527
"ALIVE ingester with last keepalive older than timeout": {
26-
ingester: &IngesterDesc{State: ACTIVE, Timestamp: time.Now().Add(-90 * time.Second).Unix()},
27-
timeout: time.Minute,
28-
writeExpected: false,
29-
readExpected: false,
28+
ingester: &IngesterDesc{State: ACTIVE, Timestamp: time.Now().Add(-90 * time.Second).Unix()},
29+
timeout: time.Minute,
30+
writeExpected: false,
31+
readExpected: false,
32+
reportExpected: false,
3033
},
3134
"JOINING ingester with last keepalive newer than timeout": {
32-
ingester: &IngesterDesc{State: JOINING, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
33-
timeout: time.Minute,
34-
writeExpected: false,
35-
readExpected: false,
35+
ingester: &IngesterDesc{State: JOINING, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
36+
timeout: time.Minute,
37+
writeExpected: false,
38+
readExpected: false,
39+
reportExpected: true,
3640
},
3741
"LEAVING ingester with last keepalive newer than timeout": {
38-
ingester: &IngesterDesc{State: LEAVING, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
39-
timeout: time.Minute,
40-
writeExpected: false,
41-
readExpected: true,
42+
ingester: &IngesterDesc{State: LEAVING, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
43+
timeout: time.Minute,
44+
writeExpected: false,
45+
readExpected: true,
46+
reportExpected: true,
4247
},
4348
}
4449

@@ -51,6 +56,9 @@ func TestIngesterDesc_IsHealthy(t *testing.T) {
5156

5257
actual = testData.ingester.IsHealthy(Read, testData.timeout)
5358
assert.Equal(t, testData.readExpected, actual)
59+
60+
actual = testData.ingester.IsHealthy(Reporting, testData.timeout)
61+
assert.Equal(t, testData.reportExpected, actual)
5462
})
5563
}
5664
}

0 commit comments

Comments
 (0)