Allow refetching instances for healthcheck (#855)
* Allow refetching instances by HealthCheckServiceInstanceListSupplier. * Add docs and javadocs. * Fix docs after review.
This commit is contained in:
committed by
GitHub
parent
7287a3c767
commit
cbedae2e82
@@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import reactor.core.Disposable;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.retry.Repeat;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -64,14 +65,19 @@ public class HealthCheckServiceInstanceListSupplier
|
||||
public HealthCheckServiceInstanceListSupplier(ServiceInstanceListSupplier delegate,
|
||||
LoadBalancerProperties.HealthCheck healthCheck, WebClient webClient) {
|
||||
super(delegate);
|
||||
this.healthCheck = healthCheck;
|
||||
defaultHealthCheckPath = healthCheck.getPath().getOrDefault("default",
|
||||
"/actuator/health");
|
||||
this.webClient = webClient;
|
||||
aliveInstancesReplay = Flux.defer(delegate)
|
||||
.delaySubscription(Duration.ofMillis(healthCheck.getInitialDelay()))
|
||||
this.healthCheck = healthCheck;
|
||||
Repeat<Object> aliveInstancesReplayRepeat = Repeat
|
||||
.onlyIf(repeatContext -> this.healthCheck.getRefetchInstances())
|
||||
.fixedBackoff(healthCheck.getRefetchInstancesInterval());
|
||||
Flux<List<ServiceInstance>> aliveInstancesFlux = Flux.defer(delegate)
|
||||
.switchMap(serviceInstances -> healthCheckFlux(serviceInstances).map(
|
||||
alive -> Collections.unmodifiableList(new ArrayList<>(alive))))
|
||||
.repeatWhen(aliveInstancesReplayRepeat);
|
||||
aliveInstancesReplay = aliveInstancesFlux
|
||||
.delaySubscription(Duration.ofMillis(healthCheck.getInitialDelay()))
|
||||
.replay(1).refCount(1);
|
||||
}
|
||||
|
||||
@@ -86,6 +92,9 @@ public class HealthCheckServiceInstanceListSupplier
|
||||
|
||||
protected Flux<List<ServiceInstance>> healthCheckFlux(
|
||||
List<ServiceInstance> instances) {
|
||||
Repeat<Object> healthCheckFluxRepeat = Repeat
|
||||
.onlyIf(repeatContext -> healthCheck.getRepeatHealthCheck())
|
||||
.fixedBackoff(healthCheck.getInterval());
|
||||
return Flux.defer(() -> {
|
||||
List<Mono<ServiceInstance>> checks = new ArrayList<>(instances.size());
|
||||
for (ServiceInstance instance : instances) {
|
||||
@@ -117,7 +126,7 @@ public class HealthCheckServiceInstanceListSupplier
|
||||
result.add(alive);
|
||||
return result;
|
||||
}).defaultIfEmpty(result);
|
||||
}).repeatWhen(restart -> restart.delayElements(healthCheck.getInterval()));
|
||||
}).repeatWhen(healthCheckFluxRepeat);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.loadbalancer.core;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -47,6 +48,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Tests for {@link HealthCheckServiceInstanceListSupplier}.
|
||||
@@ -79,7 +82,7 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws Exception {
|
||||
void tearDown() {
|
||||
if (listSupplier != null) {
|
||||
listSupplier.destroy();
|
||||
listSupplier = null;
|
||||
@@ -140,14 +143,14 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
SERVICE_ID, "127.0.0.2", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Mockito.when(delegate.get()).thenReturn(
|
||||
Flux.just(Lists.list(serviceInstance1, serviceInstance2)));
|
||||
|
||||
HealthCheckServiceInstanceListSupplier mock = Mockito
|
||||
.mock(HealthCheckServiceInstanceListSupplier.class);
|
||||
HealthCheckServiceInstanceListSupplier mock = mock(
|
||||
HealthCheckServiceInstanceListSupplier.class);
|
||||
Mockito.doReturn(Mono.just(true)).when(mock).isAlive(serviceInstance1);
|
||||
Mockito.doReturn(Mono.just(false)).when(mock).isAlive(serviceInstance2);
|
||||
|
||||
@@ -176,14 +179,14 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
SERVICE_ID, "127.0.0.2", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Mockito.when(delegate.get()).thenReturn(
|
||||
Flux.just(Lists.list(serviceInstance1, serviceInstance2)));
|
||||
|
||||
HealthCheckServiceInstanceListSupplier mock = Mockito
|
||||
.mock(HealthCheckServiceInstanceListSupplier.class);
|
||||
HealthCheckServiceInstanceListSupplier mock = mock(
|
||||
HealthCheckServiceInstanceListSupplier.class);
|
||||
Mockito.doReturn(Mono.just(true)).when(mock).isAlive(serviceInstance1);
|
||||
Mockito.doReturn(Mono.just(true)).when(mock).isAlive(serviceInstance2);
|
||||
|
||||
@@ -213,14 +216,14 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
SERVICE_ID, "127.0.0.2", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Mockito.when(delegate.get()).thenReturn(
|
||||
Flux.just(Lists.list(serviceInstance1, serviceInstance2)));
|
||||
|
||||
HealthCheckServiceInstanceListSupplier mock = Mockito
|
||||
.mock(HealthCheckServiceInstanceListSupplier.class);
|
||||
HealthCheckServiceInstanceListSupplier mock = mock(
|
||||
HealthCheckServiceInstanceListSupplier.class);
|
||||
Mockito.doReturn(Mono.just(true)).when(mock).isAlive(serviceInstance1);
|
||||
Mockito.doReturn(Mono.error(new RuntimeException("boom"))).when(mock)
|
||||
.isAlive(serviceInstance2);
|
||||
@@ -250,8 +253,8 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
SERVICE_ID, "127.0.0.2", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Mockito.when(delegate.get()).thenReturn(
|
||||
Flux.just(Lists.list(serviceInstance1, serviceInstance2)));
|
||||
@@ -282,8 +285,8 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
SERVICE_ID, "127.0.0.1", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Mockito.when(delegate.get())
|
||||
.thenReturn(Flux.just(Lists.list(serviceInstance1)));
|
||||
@@ -314,14 +317,14 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
SERVICE_ID, "127.0.0.2", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Mockito.when(delegate.get()).thenReturn(
|
||||
Flux.just(Lists.list(serviceInstance1, serviceInstance2)));
|
||||
|
||||
HealthCheckServiceInstanceListSupplier mock = Mockito
|
||||
.mock(HealthCheckServiceInstanceListSupplier.class);
|
||||
HealthCheckServiceInstanceListSupplier mock = mock(
|
||||
HealthCheckServiceInstanceListSupplier.class);
|
||||
Mockito.doReturn(Mono.just(false), Mono.just(true)).when(mock)
|
||||
.isAlive(serviceInstance1);
|
||||
Mockito.doReturn(Mono.error(new RuntimeException("boom"))).when(mock)
|
||||
@@ -352,14 +355,14 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
SERVICE_ID, "127.0.0.1", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Mockito.when(delegate.get())
|
||||
.thenReturn(Flux.just(Lists.list(serviceInstance1)));
|
||||
|
||||
HealthCheckServiceInstanceListSupplier mock = Mockito
|
||||
.mock(HealthCheckServiceInstanceListSupplier.class);
|
||||
HealthCheckServiceInstanceListSupplier mock = mock(
|
||||
HealthCheckServiceInstanceListSupplier.class);
|
||||
Mockito.when(mock.isAlive(serviceInstance1)).thenReturn(Mono.never(),
|
||||
Mono.just(true));
|
||||
|
||||
@@ -391,8 +394,8 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
SERVICE_ID, "127.0.0.2", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Flux<List<ServiceInstance>> instances = Flux
|
||||
.just(Lists.list(serviceInstance1))
|
||||
@@ -421,6 +424,39 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
.verify(VERIFY_TIMEOUT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRefetchInstances() {
|
||||
healthCheck.setInitialDelay(1000);
|
||||
healthCheck.setRepeatHealthCheck(false);
|
||||
healthCheck.setRefetchInstancesInterval(Duration.ofSeconds(1));
|
||||
healthCheck.setRefetchInstances(true);
|
||||
ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1",
|
||||
SERVICE_ID, "127.0.0.1", port, false);
|
||||
ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2",
|
||||
SERVICE_ID, "127.0.0.2", port, false);
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
when(delegate.get())
|
||||
.thenReturn(Flux.just(Collections.singletonList(serviceInstance1)))
|
||||
.thenReturn(Flux.just(Collections.singletonList(serviceInstance2)));
|
||||
listSupplier = new HealthCheckServiceInstanceListSupplier(delegate,
|
||||
healthCheck, webClient) {
|
||||
@Override
|
||||
protected Mono<Boolean> isAlive(ServiceInstance serviceInstance) {
|
||||
return Mono.just(true);
|
||||
}
|
||||
};
|
||||
return listSupplier.get();
|
||||
}).expectSubscription()
|
||||
.expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay()))
|
||||
.expectNext(Lists.list(serviceInstance1))
|
||||
.thenAwait(healthCheck.getRefetchInstancesInterval())
|
||||
.expectNext(Lists.list(serviceInstance2)).thenCancel()
|
||||
.verify(VERIFY_TIMEOUT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCacheResultIfAfterPropertiesSetInvoked() {
|
||||
healthCheck.setInitialDelay(1000);
|
||||
@@ -430,8 +466,8 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
AtomicInteger emitCounter = new AtomicInteger();
|
||||
|
||||
StepVerifier.withVirtualTime(() -> {
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(
|
||||
ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.getServiceId()).thenReturn(SERVICE_ID);
|
||||
Mockito.when(delegate.get())
|
||||
.thenReturn(Flux.just(Lists.list(serviceInstance1)));
|
||||
@@ -468,8 +504,7 @@ class HealthCheckServiceInstanceListSupplierTests {
|
||||
|
||||
final AtomicInteger instancesCanceled = new AtomicInteger();
|
||||
final AtomicBoolean subscribed = new AtomicBoolean();
|
||||
ServiceInstanceListSupplier delegate = Mockito
|
||||
.mock(ServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = mock(ServiceInstanceListSupplier.class);
|
||||
Mockito.when(delegate.get())
|
||||
.thenReturn(Flux.<List<ServiceInstance>>never()
|
||||
.doOnSubscribe(subscription -> subscribed.set(true))
|
||||
|
||||
Reference in New Issue
Block a user