Browse Source

Switch initialDelay to Duration. (#768)

pull/732/merge
Olga Maciaszek-Sharma 5 years ago committed by GitHub
parent
commit
b430529ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      docs/src/main/asciidoc/_configprops.adoc
  2. 6
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerProperties.java
  3. 3
      spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplier.java
  4. 47
      spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java

2
docs/src/main/asciidoc/_configprops.adoc

@ -31,7 +31,7 @@ @@ -31,7 +31,7 @@
|spring.cloud.loadbalancer.cache.capacity | 256 | Initial cache capacity expressed as int.
|spring.cloud.loadbalancer.cache.enabled | true | Enables Spring Cloud LoadBalancer caching mechanism.
|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 <code>StringToDurationConverter</code>. @see <a href= "https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/StringToDurationConverter.java">StringToDurationConverter.java</a>
|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

6
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerProperties.java

@ -49,7 +49,7 @@ public class LoadBalancerProperties { @@ -49,7 +49,7 @@ public class LoadBalancerProperties {
/**
* 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 class LoadBalancerProperties { @@ -58,11 +58,11 @@ public class LoadBalancerProperties {
private Map<String, String> path = new LinkedCaseInsensitiveMap<>();
public int getInitialDelay() {
public Duration getInitialDelay() {
return initialDelay;
}
public void setInitialDelay(int initialDelay) {
public void setInitialDelay(Duration initialDelay) {
this.initialDelay = initialDelay;
}

3
spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplier.java

@ -16,7 +16,6 @@ @@ -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 class HealthCheckServiceInstanceListSupplier @@ -69,7 +68,7 @@ public class HealthCheckServiceInstanceListSupplier
"/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);

47
spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/core/HealthCheckServiceInstanceListSupplierTests.java

@ -79,7 +79,7 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -79,7 +79,7 @@ class HealthCheckServiceInstanceListSupplierTests {
}
@AfterEach
void tearDown() throws Exception {
void tearDown() {
if (listSupplier != null) {
listSupplier.destroy();
listSupplier = null;
@ -132,7 +132,7 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -132,7 +132,7 @@ class HealthCheckServiceInstanceListSupplierTests {
@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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -160,8 +160,7 @@ class HealthCheckServiceInstanceListSupplierTests {
};
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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -169,7 +168,7 @@ class HealthCheckServiceInstanceListSupplierTests {
@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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -196,8 +195,7 @@ class HealthCheckServiceInstanceListSupplierTests {
};
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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -206,7 +204,7 @@ class HealthCheckServiceInstanceListSupplierTests {
@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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -234,8 +232,7 @@ class HealthCheckServiceInstanceListSupplierTests {
};
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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -243,7 +240,7 @@ class HealthCheckServiceInstanceListSupplierTests {
@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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -269,15 +266,14 @@ class HealthCheckServiceInstanceListSupplierTests {
};
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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -298,8 +294,7 @@ class HealthCheckServiceInstanceListSupplierTests {
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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -307,7 +302,7 @@ class HealthCheckServiceInstanceListSupplierTests {
@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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -336,8 +331,7 @@ class HealthCheckServiceInstanceListSupplierTests {
};
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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -347,7 +341,7 @@ class HealthCheckServiceInstanceListSupplierTests {
@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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -372,8 +366,7 @@ class HealthCheckServiceInstanceListSupplierTests {
};
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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -384,7 +377,7 @@ class HealthCheckServiceInstanceListSupplierTests {
@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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -409,8 +402,7 @@ class HealthCheckServiceInstanceListSupplierTests {
};
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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -423,7 +415,7 @@ class HealthCheckServiceInstanceListSupplierTests {
@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 @@ class HealthCheckServiceInstanceListSupplierTests { @@ -454,8 +446,7 @@ class HealthCheckServiceInstanceListSupplierTests {
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);

Loading…
Cancel
Save