Skip to content

Commit f886278

Browse files
committed
Adding tests
Signed-off-by: alanprot <[email protected]>
1 parent 79273a5 commit f886278

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

docs/blocks-storage/querier.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ querier:
215215
# The approximate amount of time between health checks of an individual
216216
# target.
217217
# CLI flag: -querier.store-gateway-client.interval
218-
[interval: <duration> | default = 1s]
218+
[interval: <duration> | default = 5s]
219219

220220
# The amount of time during which no response from a target means a failed
221221
# health check.

docs/configuration/config-file-reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3110,7 +3110,7 @@ grpc_client_config:
31103110
# The approximate amount of time between health checks of an individual
31113111
# target.
31123112
# CLI flag: -ingester.client.interval
3113-
[interval: <duration> | default = 1s]
3113+
[interval: <duration> | default = 5s]
31143114
31153115
# The amount of time during which no response from a target means a failed
31163116
# health check.
@@ -3844,7 +3844,7 @@ store_gateway_client:
38443844
# The approximate amount of time between health checks of an individual
38453845
# target.
38463846
# CLI flag: -querier.store-gateway-client.interval
3847-
[interval: <duration> | default = 1s]
3847+
[interval: <duration> | default = 5s]
38483848
38493849
# The amount of time during which no response from a target means a failed
38503850
# health check.

pkg/util/grpcclient/health_check.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type HealthCheckConfig struct {
3232

3333
// RegisterFlagsWithPrefix for Config.
3434
func (cfg *HealthCheckConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
35-
f.IntVar(&cfg.UnhealthyThreshold, prefix+".unhealthy-threshold", 3, "The number of consecutive failed health checks required before considering a target unhealthy. 0 means disabled.")
35+
f.IntVar(&cfg.UnhealthyThreshold, prefix+".unhealthy-threshold", 0, "The number of consecutive failed health checks required before considering a target unhealthy. 0 means disabled.")
3636
f.DurationVar(&cfg.Timeout, prefix+".timeout", 1*time.Second, "The amount of time during which no response from a target means a failed health check.")
3737
f.DurationVar(&cfg.Interval, prefix+".interval", 5*time.Second, "The approximate amount of time between health checks of an individual target.")
3838
}
@@ -103,7 +103,7 @@ func (h *HealthCheckInterceptors) registeredInstances() []*healthCheckEntry {
103103
}
104104

105105
func (h *HealthCheckInterceptors) iteration(ctx context.Context) error {
106-
level.Warn(h.logger).Log("msg", "Performing health check", "registeredInstances", len(h.registeredInstances()))
106+
level.Debug(h.logger).Log("msg", "Performing health check", "registeredInstances", len(h.registeredInstances()))
107107
for _, instance := range h.registeredInstances() {
108108
dialOpts, err := instance.clientConfig.Config.DialOption(nil, nil)
109109
if err != nil {
@@ -125,6 +125,7 @@ func (h *HealthCheckInterceptors) iteration(ctx context.Context) error {
125125
if time.Since(instance.lastCheckTime.Load()) < instance.clientConfig.HealthCheckConfig.Interval {
126126
continue
127127
}
128+
128129
instance.lastCheckTime.Store(time.Now())
129130

130131
go func(i *healthCheckEntry) {

pkg/util/grpcclient/health_check_test.go

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
8+
"go.uber.org/atomic"
79
"testing"
810
"time"
911

@@ -13,25 +15,69 @@ import (
1315
"google.golang.org/grpc/health/grpc_health_v1"
1416

1517
utillog "github.com/cortexproject/cortex/pkg/util/log"
18+
"github.com/cortexproject/cortex/pkg/util/services"
1619
cortex_testutil "github.com/cortexproject/cortex/pkg/util/test"
1720
)
1821

1922
type healthClientMock struct {
2023
grpc_health_v1.HealthClient
21-
err error
24+
err atomic.Error
2225
}
2326

2427
func (h *healthClientMock) Check(ctx context.Context, in *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) {
2528
return &grpc_health_v1.HealthCheckResponse{
2629
Status: grpc_health_v1.HealthCheckResponse_SERVING,
27-
}, h.err
30+
}, h.err.Load()
2831
}
2932

30-
func TestNewHealthCheckInterceptors(t *testing.T) {
33+
func TestNewHealthCheckService(t *testing.T) {
3134
i := NewHealthCheckInterceptors(utillog.Logger)
32-
hMock := &healthClientMock{
33-
err: fmt.Errorf("some error"),
35+
hMock := &healthClientMock{}
36+
i.healthClientFactory = func(cc grpc.ClientConnInterface) grpc_health_v1.HealthClient {
37+
return hMock
38+
}
39+
40+
require.NoError(t, services.StartAndAwaitRunning(context.Background(), i))
41+
defer services.StopAndAwaitTerminated(context.Background(), i) //nolint:errcheck
42+
43+
cfg := ConfigWithHealthCheck{
44+
HealthCheckConfig: HealthCheckConfig{
45+
UnhealthyThreshold: 2,
46+
Interval: 0,
47+
Timeout: time.Second,
48+
},
3449
}
50+
51+
client, err := grpc.NewClient("localhost:999", grpc.WithTransportCredentials(insecure.NewCredentials()))
52+
require.NoError(t, err)
53+
54+
ui := i.UnaryHealthCheckInterceptor(&cfg)
55+
require.NoError(t, ui(context.Background(), "", struct{}{}, struct{}{}, client,
56+
func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
57+
return nil
58+
}))
59+
60+
instances := i.registeredInstances()
61+
require.Len(t, instances, 1)
62+
63+
// Generate healthcheck error and wait instance to become unhealthy
64+
hMock.err.Store(errors.New("some error"))
65+
66+
cortex_testutil.Poll(t, 5*time.Second, false, func() interface{} {
67+
return instances[0].isHealthy()
68+
})
69+
70+
// Mark instance back to a healthy state
71+
hMock.err.Store(nil)
72+
cortex_testutil.Poll(t, 5*time.Second, true, func() interface{} {
73+
return instances[0].isHealthy()
74+
})
75+
}
76+
77+
func TestNewHealthCheckInterceptors(t *testing.T) {
78+
i := NewHealthCheckInterceptors(utillog.Logger)
79+
hMock := &healthClientMock{}
80+
hMock.err.Store(fmt.Errorf("some error"))
3581
cfg := ConfigWithHealthCheck{
3682
HealthCheckConfig: HealthCheckConfig{
3783
UnhealthyThreshold: 2,
@@ -61,7 +107,7 @@ func TestNewHealthCheckInterceptors(t *testing.T) {
61107
// first health check
62108
require.NoError(t, i.iteration(context.Background()))
63109

64-
//Should second first call
110+
//Should second call even with error
65111
require.NoError(t, ui(context.Background(), "", struct{}{}, struct{}{}, ccUnhealthy, invoker))
66112

67113
require.Equal(t, invokedMap["localhost:999"], 2)
@@ -77,10 +123,9 @@ func TestNewHealthCheckInterceptors(t *testing.T) {
77123
require.NoError(t, ui(context.Background(), "", struct{}{}, struct{}{}, ccHealthy, invoker))
78124

79125
// Should mark the instance back to healthy
80-
hMock.err = nil
126+
hMock.err.Store(nil)
81127
require.NoError(t, i.iteration(context.Background()))
82128
cortex_testutil.Poll(t, time.Second, true, func() interface{} {
83129
return ui(context.Background(), "", struct{}{}, struct{}{}, ccUnhealthy, invoker) == nil
84130
})
85-
86131
}

0 commit comments

Comments
 (0)