Skip to content

Commit 7848cf3

Browse files
committed
Merge pull request #27300 from hoanvh
* gh-27300: Polish "Mark Redis as down when cluster_state is fail" Mark Redis as down when cluster_state is fail Closes gh-27300
2 parents 96e58d8 + 49baacb commit 7848cf3

File tree

5 files changed

+108
-34
lines changed

5 files changed

+108
-34
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisHealth.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,11 +38,17 @@ static Builder up(Health.Builder builder, Properties info) {
3838
return builder.up();
3939
}
4040

41-
static Builder up(Health.Builder builder, ClusterInfo clusterInfo) {
41+
static Builder fromClusterInfo(Health.Builder builder, ClusterInfo clusterInfo) {
4242
builder.withDetail("cluster_size", clusterInfo.getClusterSize());
4343
builder.withDetail("slots_up", clusterInfo.getSlotsOk());
4444
builder.withDetail("slots_fail", clusterInfo.getSlotsFail());
45-
return builder.up();
45+
46+
if ("fail".equalsIgnoreCase(clusterInfo.getState())) {
47+
return builder.down();
48+
}
49+
else {
50+
return builder.up();
51+
}
4652
}
4753

4854
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisHealthIndicator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
5757

5858
private void doHealthCheck(Health.Builder builder, RedisConnection connection) {
5959
if (connection instanceof RedisClusterConnection) {
60-
RedisHealth.up(builder, ((RedisClusterConnection) connection).clusterGetClusterInfo());
60+
RedisHealth.fromClusterInfo(builder, ((RedisClusterConnection) connection).clusterGetClusterInfo());
6161
}
6262
else {
6363
RedisHealth.up(builder, connection.info("server"));

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisReactiveHealthIndicator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ private Mono<Health> doHealthCheck(Health.Builder builder, ReactiveRedisConnecti
6565
private Mono<Health> getHealth(Health.Builder builder, ReactiveRedisConnection connection) {
6666
if (connection instanceof ReactiveRedisClusterConnection) {
6767
return ((ReactiveRedisClusterConnection) connection).clusterGetClusterInfo()
68-
.map((info) -> up(builder, info));
68+
.map((info) -> fromClusterInfo(builder, info));
6969
}
7070
return connection.serverCommands().info("server").map((info) -> up(builder, info));
7171
}
@@ -74,8 +74,8 @@ private Health up(Health.Builder builder, Properties info) {
7474
return RedisHealth.up(builder, info).build();
7575
}
7676

77-
private Health up(Health.Builder builder, ClusterInfo clusterInfo) {
78-
return RedisHealth.up(builder, clusterInfo).build();
77+
private Health fromClusterInfo(Health.Builder builder, ClusterInfo clusterInfo) {
78+
return RedisHealth.fromClusterInfo(builder, clusterInfo).build();
7979
}
8080

8181
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/redis/RedisHealthIndicatorTests.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,32 +68,65 @@ void redisIsDown() {
6868
assertThat((String) health.getDetails().get("error")).contains("Connection failed");
6969
}
7070

71+
@Test
72+
void healthWhenClusterStateIsAbsentShouldBeUp() {
73+
RedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory(null);
74+
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(redisConnectionFactory);
75+
Health health = healthIndicator.health();
76+
assertThat(health.getStatus()).isEqualTo(Status.UP);
77+
assertThat(health.getDetails().get("cluster_size")).isEqualTo(4L);
78+
assertThat(health.getDetails().get("slots_up")).isEqualTo(4L);
79+
assertThat(health.getDetails().get("slots_fail")).isEqualTo(0L);
80+
verify(redisConnectionFactory, atLeastOnce()).getConnection();
81+
}
82+
83+
@Test
84+
void healthWhenClusterStateIsOkShouldBeUp() {
85+
RedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory("ok");
86+
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(redisConnectionFactory);
87+
Health health = healthIndicator.health();
88+
assertThat(health.getStatus()).isEqualTo(Status.UP);
89+
assertThat(health.getDetails().get("cluster_size")).isEqualTo(4L);
90+
assertThat(health.getDetails().get("slots_up")).isEqualTo(4L);
91+
assertThat(health.getDetails().get("slots_fail")).isEqualTo(0L);
92+
verify(redisConnectionFactory, atLeastOnce()).getConnection();
93+
}
94+
95+
@Test
96+
void healthWhenClusterStateIsFailShouldBeDown() {
97+
RedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory("fail");
98+
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(redisConnectionFactory);
99+
Health health = healthIndicator.health();
100+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
101+
assertThat(health.getDetails().get("cluster_size")).isEqualTo(4L);
102+
assertThat(health.getDetails().get("slots_up")).isEqualTo(3L);
103+
assertThat(health.getDetails().get("slots_fail")).isEqualTo(1L);
104+
verify(redisConnectionFactory, atLeastOnce()).getConnection();
105+
}
106+
71107
private RedisHealthIndicator createHealthIndicator(RedisConnection redisConnection) {
72108
RedisConnectionFactory redisConnectionFactory = mock(RedisConnectionFactory.class);
73109
given(redisConnectionFactory.getConnection()).willReturn(redisConnection);
74110
return new RedisHealthIndicator(redisConnectionFactory);
75111
}
76112

77-
@Test
78-
void redisClusterIsUp() {
113+
private RedisConnectionFactory createClusterConnectionFactory(String state) {
79114
Properties clusterProperties = new Properties();
115+
if (state != null) {
116+
clusterProperties.setProperty("cluster_state", state);
117+
}
80118
clusterProperties.setProperty("cluster_size", "4");
81-
clusterProperties.setProperty("cluster_slots_ok", "4");
82-
clusterProperties.setProperty("cluster_slots_fail", "0");
119+
boolean failure = "fail".equals(state);
120+
clusterProperties.setProperty("cluster_slots_ok", failure ? "3" : "4");
121+
clusterProperties.setProperty("cluster_slots_fail", failure ? "1" : "0");
83122
List<RedisClusterNode> redisMasterNodes = Arrays.asList(new RedisClusterNode("127.0.0.1", 7001),
84123
new RedisClusterNode("127.0.0.2", 7001));
85124
RedisClusterConnection redisConnection = mock(RedisClusterConnection.class);
86125
given(redisConnection.clusterGetNodes()).willReturn(redisMasterNodes);
87126
given(redisConnection.clusterGetClusterInfo()).willReturn(new ClusterInfo(clusterProperties));
88127
RedisConnectionFactory redisConnectionFactory = mock(RedisConnectionFactory.class);
89128
given(redisConnectionFactory.getConnection()).willReturn(redisConnection);
90-
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(redisConnectionFactory);
91-
Health health = healthIndicator.health();
92-
assertThat(health.getStatus()).isEqualTo(Status.UP);
93-
assertThat(health.getDetails().get("cluster_size")).isEqualTo(4L);
94-
assertThat(health.getDetails().get("slots_up")).isEqualTo(4L);
95-
assertThat(health.getDetails().get("slots_fail")).isEqualTo(0L);
96-
verify(redisConnectionFactory, atLeastOnce()).getConnection();
129+
return redisConnectionFactory;
97130
}
98131

99132
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/redis/RedisReactiveHealthIndicatorTests.java

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,16 +67,22 @@ void redisIsUp() {
6767
}
6868

6969
@Test
70-
void redisClusterIsUp() {
71-
Properties clusterProperties = new Properties();
72-
clusterProperties.setProperty("cluster_size", "4");
73-
clusterProperties.setProperty("cluster_slots_ok", "4");
74-
clusterProperties.setProperty("cluster_slots_fail", "0");
75-
ReactiveRedisClusterConnection redisConnection = mock(ReactiveRedisClusterConnection.class);
76-
given(redisConnection.closeLater()).willReturn(Mono.empty());
77-
given(redisConnection.clusterGetClusterInfo()).willReturn(Mono.just(new ClusterInfo(clusterProperties)));
78-
ReactiveRedisConnectionFactory redisConnectionFactory = mock(ReactiveRedisConnectionFactory.class);
79-
given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection);
70+
void healthWhenClusterStateIsAbsentShouldBeUp() {
71+
ReactiveRedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory(null);
72+
RedisReactiveHealthIndicator healthIndicator = new RedisReactiveHealthIndicator(redisConnectionFactory);
73+
Mono<Health> health = healthIndicator.health();
74+
StepVerifier.create(health).consumeNextWith((h) -> {
75+
assertThat(h.getStatus()).isEqualTo(Status.UP);
76+
assertThat(h.getDetails().get("cluster_size")).isEqualTo(4L);
77+
assertThat(h.getDetails().get("slots_up")).isEqualTo(4L);
78+
assertThat(h.getDetails().get("slots_fail")).isEqualTo(0L);
79+
}).verifyComplete();
80+
verify(redisConnectionFactory.getReactiveConnection()).closeLater();
81+
}
82+
83+
@Test
84+
void healthWhenClusterStateIsOkShouldBeUp() {
85+
ReactiveRedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory("ok");
8086
RedisReactiveHealthIndicator healthIndicator = new RedisReactiveHealthIndicator(redisConnectionFactory);
8187
Mono<Health> health = healthIndicator.health();
8288
StepVerifier.create(health).consumeNextWith((h) -> {
@@ -85,7 +91,20 @@ void redisClusterIsUp() {
8591
assertThat(h.getDetails().get("slots_up")).isEqualTo(4L);
8692
assertThat(h.getDetails().get("slots_fail")).isEqualTo(0L);
8793
}).verifyComplete();
88-
verify(redisConnection).closeLater();
94+
}
95+
96+
@Test
97+
void healthWhenClusterStateIsFailShouldBeDown() {
98+
Properties clusterProperties = new Properties();
99+
clusterProperties.setProperty("cluster_state", "fail");
100+
ReactiveRedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory("fail");
101+
RedisReactiveHealthIndicator healthIndicator = new RedisReactiveHealthIndicator(redisConnectionFactory);
102+
Mono<Health> health = healthIndicator.health();
103+
StepVerifier.create(health).consumeNextWith((h) -> {
104+
assertThat(h.getStatus()).isEqualTo(Status.DOWN);
105+
assertThat(h.getDetails().get("slots_up")).isEqualTo(3L);
106+
assertThat(h.getDetails().get("slots_fail")).isEqualTo(1L);
107+
}).verifyComplete();
89108
}
90109

91110
@Test
@@ -114,11 +133,27 @@ void redisConnectionIsDown() {
114133

115134
private RedisReactiveHealthIndicator createHealthIndicator(ReactiveRedisConnection redisConnection,
116135
ReactiveServerCommands serverCommands) {
117-
118136
ReactiveRedisConnectionFactory redisConnectionFactory = mock(ReactiveRedisConnectionFactory.class);
119137
given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection);
120138
given(redisConnection.serverCommands()).willReturn(serverCommands);
121139
return new RedisReactiveHealthIndicator(redisConnectionFactory);
122140
}
123141

142+
private ReactiveRedisConnectionFactory createClusterConnectionFactory(String state) {
143+
Properties clusterProperties = new Properties();
144+
if (state != null) {
145+
clusterProperties.setProperty("cluster_state", state);
146+
}
147+
clusterProperties.setProperty("cluster_size", "4");
148+
boolean failure = "fail".equals(state);
149+
clusterProperties.setProperty("cluster_slots_ok", failure ? "3" : "4");
150+
clusterProperties.setProperty("cluster_slots_fail", failure ? "1" : "0");
151+
ReactiveRedisClusterConnection redisConnection = mock(ReactiveRedisClusterConnection.class);
152+
given(redisConnection.closeLater()).willReturn(Mono.empty());
153+
given(redisConnection.clusterGetClusterInfo()).willReturn(Mono.just(new ClusterInfo(clusterProperties)));
154+
ReactiveRedisConnectionFactory redisConnectionFactory = mock(ReactiveRedisConnectionFactory.class);
155+
given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection);
156+
return redisConnectionFactory;
157+
}
158+
124159
}

0 commit comments

Comments
 (0)