From cd066a784162ec65fafff3a5749c97b78478c337 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Wed, 27 May 2020 19:56:10 +0200 Subject: [PATCH] Switch initialDelay to Duration. --- docs/src/main/asciidoc/_configprops.adoc | 2 +- .../reactive/LoadBalancerProperties.java | 6 +-- ...ealthCheckServiceInstanceListSupplier.java | 3 +- ...CheckServiceInstanceListSupplierTests.java | 47 ++++++++----------- 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/docs/src/main/asciidoc/_configprops.adoc b/docs/src/main/asciidoc/_configprops.adoc index 2c7d150e9..c34411ae8 100644 --- a/docs/src/main/asciidoc/_configprops.adoc +++ b/docs/src/main/asciidoc/_configprops.adoc @@ -30,7 +30,7 @@ |spring.cloud.loadbalancer.cache.caffeine.spec | | The spec to use to create caches. See CaffeineSpec for more details on the spec format. |spring.cloud.loadbalancer.cache.capacity | 256 | Initial cache capacity expressed as int. |spring.cloud.loadbalancer.cache.ttl | 35s | Time To Live - time counted from writing of the record, after which cache entries are expired, expressed as a {@link Duration}. The property {@link String} has to be in keeping with the appropriate syntax as specified in Spring Boot StringToDurationConverter. @see StringToDurationConverter.java -|spring.cloud.loadbalancer.health-check.initial-delay | 0 | Initial delay value for the HealthCheck scheduler. +|spring.cloud.loadbalancer.health-check.initial-delay | null | Initial delay value for the HealthCheck scheduler. |spring.cloud.loadbalancer.health-check.interval | 25s | Interval for rerunning the HealthCheck scheduler. |spring.cloud.loadbalancer.health-check.path | null | null |spring.cloud.loadbalancer.retry.enabled | true | null diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerProperties.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerProperties.java index 6c6ea92ba..d3f830d87 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerProperties.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerProperties.java @@ -49,7 +49,7 @@ public static class HealthCheck { /** * Initial delay value for the HealthCheck scheduler. */ - private int initialDelay = 0; + private Duration initialDelay = Duration.ZERO; /** * Interval for rerunning the HealthCheck scheduler. @@ -58,11 +58,11 @@ public static class HealthCheck { private Map path = new LinkedCaseInsensitiveMap<>(); - public int getInitialDelay() { + public Duration getInitialDelay() { return initialDelay; } - public void setInitialDelay(int initialDelay) { + public void setInitialDelay(Duration initialDelay) { this.initialDelay = initialDelay; } diff --git a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplier.java b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplier.java index 85ad174b9..a310ef05a 100644 --- a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplier.java +++ b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplier.java @@ -16,7 +16,6 @@ package org.springframework.cloud.loadbalancer.core; -import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -69,7 +68,7 @@ public HealthCheckServiceInstanceListSupplier(ServiceInstanceListSupplier delega "/actuator/health"); this.webClient = webClient; aliveInstancesReplay = Flux.defer(delegate) - .delaySubscription(Duration.ofMillis(healthCheck.getInitialDelay())) + .delaySubscription(healthCheck.getInitialDelay()) .switchMap(serviceInstances -> healthCheckFlux(serviceInstances).map( alive -> Collections.unmodifiableList(new ArrayList<>(alive)))) .replay(1).refCount(1); diff --git a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java index b04a68280..55262dc9c 100644 --- a/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java +++ b/spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java @@ -79,7 +79,7 @@ void setUp() { } @AfterEach - void tearDown() throws Exception { + void tearDown() { if (listSupplier != null) { listSupplier.destroy(); listSupplier = null; @@ -132,7 +132,7 @@ void shouldReturnFalseIfEndpointNotFound() { @Test void shouldReturnOnlyAliveService() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); @@ -160,8 +160,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { }; return listSupplier.get(); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNext(Lists.list(serviceInstance1)) .expectNoEvent(healthCheck.getInterval()).thenCancel() .verify(VERIFY_TIMEOUT); @@ -169,7 +168,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { @Test void shouldEmitOnEachAliveServiceInBatch() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2", @@ -196,8 +195,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { }; return listSupplier.get(); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNext(Lists.list(serviceInstance1)) .expectNext(Lists.list(serviceInstance1, serviceInstance2)) .expectNoEvent(healthCheck.getInterval()).thenCancel() @@ -206,7 +204,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { @Test void shouldNotFailIfIsAliveReturnsError() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2", @@ -234,8 +232,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { }; return listSupplier.get(); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNext(Lists.list(serviceInstance1)) .expectNoEvent(healthCheck.getInterval()).thenCancel() .verify(VERIFY_TIMEOUT); @@ -243,7 +240,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { @Test void shouldEmitAllInstancesIfAllIsAliveChecksFailed() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2", @@ -269,15 +266,14 @@ protected Mono isAlive(ServiceInstance serviceInstance) { }; return listSupplier.get(); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNext(Lists.list()).expectNoEvent(healthCheck.getInterval()) .thenCancel().verify(VERIFY_TIMEOUT); } @Test void shouldMakeInitialDaleyAfterPropertiesSet() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); @@ -298,8 +294,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { listSupplier.afterPropertiesSet(); return listSupplier.get(); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNext(Lists.list(serviceInstance1)) .expectNoEvent(healthCheck.getInterval()).thenCancel() .verify(VERIFY_TIMEOUT); @@ -307,7 +302,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { @Test void shouldRepeatIsAliveChecksIndefinitely() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2", @@ -336,8 +331,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { }; return listSupplier.get(); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNext(Lists.list()).expectNoEvent(healthCheck.getInterval()) .expectNext(Lists.list(serviceInstance1)) .expectNoEvent(healthCheck.getInterval()) @@ -347,7 +341,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { @Test void shouldTimeoutIsAliveCheck() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); @@ -372,8 +366,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { }; return listSupplier.get(); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNoEvent(healthCheck.getInterval()).expectNext(Lists.list()) .expectNoEvent(healthCheck.getInterval()) .expectNext(Lists.list(serviceInstance1)) @@ -384,7 +377,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { @Test void shouldUpdateInstances() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); ServiceInstance serviceInstance2 = new DefaultServiceInstance("ignored-service-2", @@ -409,8 +402,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { }; return listSupplier.get(); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNext(Lists.list(serviceInstance1)) .thenAwait(healthCheck.getInterval().dividedBy(2)) .expectNext(Lists.list(serviceInstance1)) @@ -423,7 +415,7 @@ protected Mono isAlive(ServiceInstance serviceInstance) { @Test void shouldCacheResultIfAfterPropertiesSetInvoked() { - healthCheck.setInitialDelay(1000); + healthCheck.setInitialDelay(Duration.ofSeconds(1)); ServiceInstance serviceInstance1 = new DefaultServiceInstance("ignored-service-1", SERVICE_ID, "127.0.0.1", port, false); @@ -454,8 +446,7 @@ protected Flux> healthCheckFlux( listSupplier.afterPropertiesSet(); return listSupplier.get().take(1).concatWith(listSupplier.get().take(1)); - }).expectSubscription() - .expectNoEvent(Duration.ofMillis(healthCheck.getInitialDelay())) + }).expectSubscription().expectNoEvent(healthCheck.getInitialDelay()) .expectNext(Lists.list(serviceInstance1)) .expectNext(Lists.list(serviceInstance1)).thenCancel() .verify(VERIFY_TIMEOUT);