Ensure that instances don't restart at the same time upon config change
Fixes: #350
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.config.reload;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -114,18 +116,34 @@ public class ConfigReloadAutoConfiguration {
|
||||
switch (properties.getStrategy()) {
|
||||
case RESTART_CONTEXT:
|
||||
return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
|
||||
restarter::restart);
|
||||
() -> {
|
||||
wait(properties);
|
||||
restarter.restart();
|
||||
});
|
||||
case REFRESH:
|
||||
return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
|
||||
refresher::refresh);
|
||||
case SHUTDOWN:
|
||||
return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
|
||||
ctx::close);
|
||||
() -> {
|
||||
wait(properties);
|
||||
ctx.close();
|
||||
});
|
||||
}
|
||||
throw new IllegalStateException("Unsupported configuration update strategy: "
|
||||
+ properties.getStrategy());
|
||||
}
|
||||
|
||||
private static void wait(ConfigReloadProperties properties) {
|
||||
final long waitMillis = ThreadLocalRandom.current()
|
||||
.nextLong(properties.getMaxWaitForRestart().toMillis());
|
||||
try {
|
||||
Thread.sleep(waitMillis);
|
||||
}
|
||||
catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -58,6 +58,16 @@ public class ConfigReloadProperties {
|
||||
*/
|
||||
private Duration period = Duration.ofMillis(15000L);
|
||||
|
||||
/**
|
||||
* If Restart or Shutdown strategies are used, Spring Cloud Kubernetes waits a random
|
||||
* amount of time before restarting. This is done in order to avoid having all
|
||||
* instances of the same application restart at the same time. This property
|
||||
* configures the maximum of amount of wait time from the moment the signal is
|
||||
* received that a restart is needed until the moment the restart is actually
|
||||
* triggered
|
||||
*/
|
||||
private Duration maxWaitForRestart = Duration.ofSeconds(2);
|
||||
|
||||
public ConfigReloadProperties() {
|
||||
}
|
||||
|
||||
@@ -109,6 +119,14 @@ public class ConfigReloadProperties {
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public Duration getMaxWaitForRestart() {
|
||||
return maxWaitForRestart;
|
||||
}
|
||||
|
||||
public void setMaxWaitForRestart(Duration maxWaitForRestart) {
|
||||
this.maxWaitForRestart = maxWaitForRestart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload strategies.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user