Merge branch '2.4.x' into 2.5.x

Closes gh-27304
pull/27310/head
Andy Wilkinson 3 years ago
commit 685b78f504

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,11 +38,17 @@ final class RedisHealth {
return builder.up(); return builder.up();
} }
static Builder up(Health.Builder builder, ClusterInfo clusterInfo) { static Builder fromClusterInfo(Health.Builder builder, ClusterInfo clusterInfo) {
builder.withDetail("cluster_size", clusterInfo.getClusterSize()); builder.withDetail("cluster_size", clusterInfo.getClusterSize());
builder.withDetail("slots_up", clusterInfo.getSlotsOk()); builder.withDetail("slots_up", clusterInfo.getSlotsOk());
builder.withDetail("slots_fail", clusterInfo.getSlotsFail()); builder.withDetail("slots_fail", clusterInfo.getSlotsFail());
return builder.up();
if ("fail".equalsIgnoreCase(clusterInfo.getState())) {
return builder.down();
}
else {
return builder.up();
}
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -57,7 +57,7 @@ public class RedisHealthIndicator extends AbstractHealthIndicator {
private void doHealthCheck(Health.Builder builder, RedisConnection connection) { private void doHealthCheck(Health.Builder builder, RedisConnection connection) {
if (connection instanceof RedisClusterConnection) { if (connection instanceof RedisClusterConnection) {
RedisHealth.up(builder, ((RedisClusterConnection) connection).clusterGetClusterInfo()); RedisHealth.fromClusterInfo(builder, ((RedisClusterConnection) connection).clusterGetClusterInfo());
} }
else { else {
RedisHealth.up(builder, connection.info("server")); RedisHealth.up(builder, connection.info("server"));

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -65,7 +65,7 @@ public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicato
private Mono<Health> getHealth(Health.Builder builder, ReactiveRedisConnection connection) { private Mono<Health> getHealth(Health.Builder builder, ReactiveRedisConnection connection) {
if (connection instanceof ReactiveRedisClusterConnection) { if (connection instanceof ReactiveRedisClusterConnection) {
return ((ReactiveRedisClusterConnection) connection).clusterGetClusterInfo() return ((ReactiveRedisClusterConnection) connection).clusterGetClusterInfo()
.map((info) -> up(builder, info)); .map((info) -> fromClusterInfo(builder, info));
} }
return connection.serverCommands().info("server").map((info) -> up(builder, info)); return connection.serverCommands().info("server").map((info) -> up(builder, info));
} }
@ -74,8 +74,8 @@ public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicato
return RedisHealth.up(builder, info).build(); return RedisHealth.up(builder, info).build();
} }
private Health up(Health.Builder builder, ClusterInfo clusterInfo) { private Health fromClusterInfo(Health.Builder builder, ClusterInfo clusterInfo) {
return RedisHealth.up(builder, clusterInfo).build(); return RedisHealth.fromClusterInfo(builder, clusterInfo).build();
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -68,18 +68,57 @@ class RedisHealthIndicatorTests {
assertThat((String) health.getDetails().get("error")).contains("Connection failed"); assertThat((String) health.getDetails().get("error")).contains("Connection failed");
} }
@Test
void healthWhenClusterStateIsAbsentShouldBeUp() {
RedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory(null);
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(redisConnectionFactory);
Health health = healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails().get("cluster_size")).isEqualTo(4L);
assertThat(health.getDetails().get("slots_up")).isEqualTo(4L);
assertThat(health.getDetails().get("slots_fail")).isEqualTo(0L);
verify(redisConnectionFactory, atLeastOnce()).getConnection();
}
@Test
void healthWhenClusterStateIsOkShouldBeUp() {
RedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory("ok");
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(redisConnectionFactory);
Health health = healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails().get("cluster_size")).isEqualTo(4L);
assertThat(health.getDetails().get("slots_up")).isEqualTo(4L);
assertThat(health.getDetails().get("slots_fail")).isEqualTo(0L);
verify(redisConnectionFactory, atLeastOnce()).getConnection();
}
@Test
void healthWhenClusterStateIsFailShouldBeDown() {
RedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory("fail");
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(redisConnectionFactory);
Health health = healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails().get("cluster_size")).isEqualTo(4L);
assertThat(health.getDetails().get("slots_up")).isEqualTo(3L);
assertThat(health.getDetails().get("slots_fail")).isEqualTo(1L);
verify(redisConnectionFactory, atLeastOnce()).getConnection();
}
private RedisHealthIndicator createHealthIndicator(RedisConnection redisConnection) { private RedisHealthIndicator createHealthIndicator(RedisConnection redisConnection) {
RedisConnectionFactory redisConnectionFactory = mock(RedisConnectionFactory.class); RedisConnectionFactory redisConnectionFactory = mock(RedisConnectionFactory.class);
given(redisConnectionFactory.getConnection()).willReturn(redisConnection); given(redisConnectionFactory.getConnection()).willReturn(redisConnection);
return new RedisHealthIndicator(redisConnectionFactory); return new RedisHealthIndicator(redisConnectionFactory);
} }
@Test private RedisConnectionFactory createClusterConnectionFactory(String state) {
void redisClusterIsUp() {
Properties clusterProperties = new Properties(); Properties clusterProperties = new Properties();
if (state != null) {
clusterProperties.setProperty("cluster_state", state);
}
clusterProperties.setProperty("cluster_size", "4"); clusterProperties.setProperty("cluster_size", "4");
clusterProperties.setProperty("cluster_slots_ok", "4"); boolean failure = "fail".equals(state);
clusterProperties.setProperty("cluster_slots_fail", "0"); clusterProperties.setProperty("cluster_slots_ok", failure ? "3" : "4");
clusterProperties.setProperty("cluster_slots_fail", failure ? "1" : "0");
List<RedisClusterNode> redisMasterNodes = Arrays.asList(new RedisClusterNode("127.0.0.1", 7001), List<RedisClusterNode> redisMasterNodes = Arrays.asList(new RedisClusterNode("127.0.0.1", 7001),
new RedisClusterNode("127.0.0.2", 7001)); new RedisClusterNode("127.0.0.2", 7001));
RedisClusterConnection redisConnection = mock(RedisClusterConnection.class); RedisClusterConnection redisConnection = mock(RedisClusterConnection.class);
@ -87,13 +126,7 @@ class RedisHealthIndicatorTests {
given(redisConnection.clusterGetClusterInfo()).willReturn(new ClusterInfo(clusterProperties)); given(redisConnection.clusterGetClusterInfo()).willReturn(new ClusterInfo(clusterProperties));
RedisConnectionFactory redisConnectionFactory = mock(RedisConnectionFactory.class); RedisConnectionFactory redisConnectionFactory = mock(RedisConnectionFactory.class);
given(redisConnectionFactory.getConnection()).willReturn(redisConnection); given(redisConnectionFactory.getConnection()).willReturn(redisConnection);
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(redisConnectionFactory); return redisConnectionFactory;
Health health = healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails().get("cluster_size")).isEqualTo(4L);
assertThat(health.getDetails().get("slots_up")).isEqualTo(4L);
assertThat(health.getDetails().get("slots_fail")).isEqualTo(0L);
verify(redisConnectionFactory, atLeastOnce()).getConnection();
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -67,16 +67,22 @@ class RedisReactiveHealthIndicatorTests {
} }
@Test @Test
void redisClusterIsUp() { void healthWhenClusterStateIsAbsentShouldBeUp() {
Properties clusterProperties = new Properties(); ReactiveRedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory(null);
clusterProperties.setProperty("cluster_size", "4"); RedisReactiveHealthIndicator healthIndicator = new RedisReactiveHealthIndicator(redisConnectionFactory);
clusterProperties.setProperty("cluster_slots_ok", "4"); Mono<Health> health = healthIndicator.health();
clusterProperties.setProperty("cluster_slots_fail", "0"); StepVerifier.create(health).consumeNextWith((h) -> {
ReactiveRedisClusterConnection redisConnection = mock(ReactiveRedisClusterConnection.class); assertThat(h.getStatus()).isEqualTo(Status.UP);
given(redisConnection.closeLater()).willReturn(Mono.empty()); assertThat(h.getDetails().get("cluster_size")).isEqualTo(4L);
given(redisConnection.clusterGetClusterInfo()).willReturn(Mono.just(new ClusterInfo(clusterProperties))); assertThat(h.getDetails().get("slots_up")).isEqualTo(4L);
ReactiveRedisConnectionFactory redisConnectionFactory = mock(ReactiveRedisConnectionFactory.class); assertThat(h.getDetails().get("slots_fail")).isEqualTo(0L);
given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection); }).verifyComplete();
verify(redisConnectionFactory.getReactiveConnection()).closeLater();
}
@Test
void healthWhenClusterStateIsOkShouldBeUp() {
ReactiveRedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory("ok");
RedisReactiveHealthIndicator healthIndicator = new RedisReactiveHealthIndicator(redisConnectionFactory); RedisReactiveHealthIndicator healthIndicator = new RedisReactiveHealthIndicator(redisConnectionFactory);
Mono<Health> health = healthIndicator.health(); Mono<Health> health = healthIndicator.health();
StepVerifier.create(health).consumeNextWith((h) -> { StepVerifier.create(health).consumeNextWith((h) -> {
@ -85,7 +91,20 @@ class RedisReactiveHealthIndicatorTests {
assertThat(h.getDetails().get("slots_up")).isEqualTo(4L); assertThat(h.getDetails().get("slots_up")).isEqualTo(4L);
assertThat(h.getDetails().get("slots_fail")).isEqualTo(0L); assertThat(h.getDetails().get("slots_fail")).isEqualTo(0L);
}).verifyComplete(); }).verifyComplete();
verify(redisConnection).closeLater(); }
@Test
void healthWhenClusterStateIsFailShouldBeDown() {
Properties clusterProperties = new Properties();
clusterProperties.setProperty("cluster_state", "fail");
ReactiveRedisConnectionFactory redisConnectionFactory = createClusterConnectionFactory("fail");
RedisReactiveHealthIndicator healthIndicator = new RedisReactiveHealthIndicator(redisConnectionFactory);
Mono<Health> health = healthIndicator.health();
StepVerifier.create(health).consumeNextWith((h) -> {
assertThat(h.getStatus()).isEqualTo(Status.DOWN);
assertThat(h.getDetails().get("slots_up")).isEqualTo(3L);
assertThat(h.getDetails().get("slots_fail")).isEqualTo(1L);
}).verifyComplete();
} }
@Test @Test
@ -114,11 +133,27 @@ class RedisReactiveHealthIndicatorTests {
private RedisReactiveHealthIndicator createHealthIndicator(ReactiveRedisConnection redisConnection, private RedisReactiveHealthIndicator createHealthIndicator(ReactiveRedisConnection redisConnection,
ReactiveServerCommands serverCommands) { ReactiveServerCommands serverCommands) {
ReactiveRedisConnectionFactory redisConnectionFactory = mock(ReactiveRedisConnectionFactory.class); ReactiveRedisConnectionFactory redisConnectionFactory = mock(ReactiveRedisConnectionFactory.class);
given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection); given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection);
given(redisConnection.serverCommands()).willReturn(serverCommands); given(redisConnection.serverCommands()).willReturn(serverCommands);
return new RedisReactiveHealthIndicator(redisConnectionFactory); return new RedisReactiveHealthIndicator(redisConnectionFactory);
} }
private ReactiveRedisConnectionFactory createClusterConnectionFactory(String state) {
Properties clusterProperties = new Properties();
if (state != null) {
clusterProperties.setProperty("cluster_state", state);
}
clusterProperties.setProperty("cluster_size", "4");
boolean failure = "fail".equals(state);
clusterProperties.setProperty("cluster_slots_ok", failure ? "3" : "4");
clusterProperties.setProperty("cluster_slots_fail", failure ? "1" : "0");
ReactiveRedisClusterConnection redisConnection = mock(ReactiveRedisClusterConnection.class);
given(redisConnection.closeLater()).willReturn(Mono.empty());
given(redisConnection.clusterGetClusterInfo()).willReturn(Mono.just(new ClusterInfo(clusterProperties)));
ReactiveRedisConnectionFactory redisConnectionFactory = mock(ReactiveRedisConnectionFactory.class);
given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection);
return redisConnectionFactory;
}
} }

Loading…
Cancel
Save