Skip to content

Commit cc1bdfb

Browse files
Bugfix/2897 limit exponential backoff (#2903)
* #2897: WIP Fix exponential backoff * reduce number of places where defaults can be defined * use configured backoff in retry * #2897: javaformat * add Test * add docs * reduce number of places with defaults --------- Co-authored-by: ulrichschulte <ulrich.schulte@codecentric.de>
1 parent c8681eb commit cc1bdfb

File tree

9 files changed

+61
-20
lines changed

9 files changed

+61
-20
lines changed

spring-boot-admin-docs/src/site/asciidoc/server.adoc

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ In addition when the reverse proxy terminates the https connection, it may be ne
2222
| Time interval to check the status of instances.
2323
| 10,000ms
2424

25+
| spring.boot.admin.monitor.status-max-backoff
26+
| The maximal backoff for status check retries (retry after error has exponential backoff, minimum backoff is 1 second).
27+
| 60,000ms
28+
2529
| spring.boot.admin.monitor.status-lifetime
2630
| Lifetime of status. The status won't be updated as long the last status isn't expired.
2731
| 10,000ms
@@ -30,6 +34,10 @@ In addition when the reverse proxy terminates the https connection, it may be ne
3034
| Time interval to check the info of instances.
3135
| 1m
3236

37+
| spring.boot.admin.monitor.info-max-backoff
38+
| The maximal backoff for info check retries (retry after error has exponential backoff, minimum backoff is 1 second).
39+
| 10m
40+
3341
| spring.boot.admin.monitor.info-lifetime
3442
| Lifetime of info. The info won't be updated as long the last info isn't expired.
3543
| 1m

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/config/AdminServerAutoConfiguration.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public StatusUpdater statusUpdater(InstanceRepository instanceRepository,
9595
@Bean(initMethod = "start", destroyMethod = "stop")
9696
@ConditionalOnMissingBean
9797
public StatusUpdateTrigger statusUpdateTrigger(StatusUpdater statusUpdater, Publisher<InstanceEvent> events) {
98-
StatusUpdateTrigger trigger = new StatusUpdateTrigger(statusUpdater, events);
99-
trigger.setInterval(this.adminServerProperties.getMonitor().getStatusInterval());
100-
trigger.setLifetime(this.adminServerProperties.getMonitor().getStatusLifetime());
101-
return trigger;
98+
return new StatusUpdateTrigger(statusUpdater, events,
99+
this.adminServerProperties.getMonitor().getStatusInterval(),
100+
this.adminServerProperties.getMonitor().getStatusLifetime(),
101+
this.adminServerProperties.getMonitor().getStatusMaxBackoff());
102102
}
103103

104104
@Bean
@@ -129,10 +129,9 @@ public InfoUpdater infoUpdater(InstanceRepository instanceRepository,
129129
@Bean(initMethod = "start", destroyMethod = "stop")
130130
@ConditionalOnMissingBean
131131
public InfoUpdateTrigger infoUpdateTrigger(InfoUpdater infoUpdater, Publisher<InstanceEvent> events) {
132-
InfoUpdateTrigger trigger = new InfoUpdateTrigger(infoUpdater, events);
133-
trigger.setInterval(this.adminServerProperties.getMonitor().getInfoInterval());
134-
trigger.setLifetime(this.adminServerProperties.getMonitor().getInfoLifetime());
135-
return trigger;
132+
return new InfoUpdateTrigger(infoUpdater, events, this.adminServerProperties.getMonitor().getInfoInterval(),
133+
this.adminServerProperties.getMonitor().getInfoLifetime(),
134+
this.adminServerProperties.getMonitor().getInfoMaxBackoff());
136135
}
137136

138137
@Bean

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/config/AdminServerProperties.java

+14
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,26 @@ public static class MonitorProperties {
103103
@DurationUnit(ChronoUnit.MILLIS)
104104
private Duration statusLifetime = Duration.ofMillis(10_000L);
105105

106+
/**
107+
* The maximal backoff for status check retries (retry after error has exponential
108+
* backoff, minimum backoff is 1 second).
109+
*/
110+
@DurationUnit(ChronoUnit.MILLIS)
111+
private Duration statusMaxBackoff = Duration.ofMillis(60_000L);
112+
106113
/**
107114
* Time interval to check the info of instances,
108115
*/
109116
@DurationUnit(ChronoUnit.MILLIS)
110117
private Duration infoInterval = Duration.ofMinutes(1L);
111118

119+
/**
120+
* The maximal backoff for info check retries (retry after error has exponential
121+
* backoff, minimum backoff is 1 second).
122+
*/
123+
@DurationUnit(ChronoUnit.MILLIS)
124+
private Duration infoMaxBackoff = Duration.ofMinutes(10);
125+
112126
/**
113127
* Lifetime of info. The info won't be updated as long the last info isn't
114128
* expired.

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/services/InfoUpdateTrigger.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ public class InfoUpdateTrigger extends AbstractEventHandler<InstanceEvent> {
3838

3939
private final IntervalCheck intervalCheck;
4040

41-
public InfoUpdateTrigger(InfoUpdater infoUpdater, Publisher<InstanceEvent> publisher) {
41+
public InfoUpdateTrigger(InfoUpdater infoUpdater, Publisher<InstanceEvent> publisher, Duration updateInterval,
42+
Duration infoLifetime, Duration maxBackoff) {
4243
super(publisher, InstanceEvent.class);
4344
this.infoUpdater = infoUpdater;
44-
this.intervalCheck = new IntervalCheck("info", this::updateInfo, Duration.ofMinutes(5), Duration.ofMinutes(1));
45+
this.intervalCheck = new IntervalCheck("info", this::updateInfo, updateInterval, infoLifetime, maxBackoff);
4546
}
4647

4748
@Override

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/services/IntervalCheck.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class IntervalCheck {
5252

5353
private final Function<InstanceId, Mono<Void>> checkFn;
5454

55+
@Setter
56+
private Duration maxBackoff;
57+
5558
@Getter
5659
@Setter
5760
private Duration interval;
@@ -65,16 +68,13 @@ public class IntervalCheck {
6568
@Nullable
6669
private Scheduler scheduler;
6770

68-
public IntervalCheck(String name, Function<InstanceId, Mono<Void>> checkFn) {
69-
this(name, checkFn, Duration.ofSeconds(10), Duration.ofSeconds(10));
70-
}
71-
7271
public IntervalCheck(String name, Function<InstanceId, Mono<Void>> checkFn, Duration interval,
73-
Duration minRetention) {
72+
Duration minRetention, Duration maxBackoff) {
7473
this.name = name;
7574
this.checkFn = checkFn;
7675
this.interval = interval;
7776
this.minRetention = minRetention;
77+
this.maxBackoff = maxBackoff;
7878
}
7979

8080
public void start() {
@@ -85,6 +85,7 @@ public void start() {
8585
.subscribeOn(this.scheduler)
8686
.concatMap((i) -> this.checkAllInstances())
8787
.retryWhen(Retry.backoff(Long.MAX_VALUE, Duration.ofSeconds(1))
88+
.maxBackoff(maxBackoff)
8889
.doBeforeRetry((s) -> log.warn("Unexpected error in {}-check", this.name, s.failure())))
8990
.subscribe(null, (error) -> log.error("Unexpected error in {}-check", name, error));
9091
}

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/services/StatusUpdateTrigger.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public class StatusUpdateTrigger extends AbstractEventHandler<InstanceEvent> {
3737

3838
private final IntervalCheck intervalCheck;
3939

40-
public StatusUpdateTrigger(StatusUpdater statusUpdater, Publisher<InstanceEvent> publisher) {
40+
public StatusUpdateTrigger(StatusUpdater statusUpdater, Publisher<InstanceEvent> publisher, Duration updateInterval,
41+
Duration statusLifetime, Duration maxBackoff) {
4142
super(publisher, InstanceEvent.class);
4243
this.statusUpdater = statusUpdater;
43-
this.intervalCheck = new IntervalCheck("status", this::updateStatus);
44+
this.intervalCheck = new IntervalCheck("status", this::updateStatus, updateInterval, statusLifetime,
45+
maxBackoff);
4446
}
4547

4648
@Override

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/server/services/InfoUpdateTriggerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public class InfoUpdateTriggerTest {
6060
public void setUp() throws Exception {
6161
when(this.updater.updateInfo(any(InstanceId.class))).thenReturn(Mono.empty());
6262

63-
this.trigger = new InfoUpdateTrigger(this.updater, this.events.flux());
63+
this.trigger = new InfoUpdateTrigger(this.updater, this.events.flux(), Duration.ofMinutes(5), Duration.ofMinutes(1),
64+
Duration.ofMinutes(10));
6465
this.trigger.start();
6566
await().until(this.events::wasSubscribed);
6667
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/server/services/IntervalCheckTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class IntervalCheckTest {
4141
private final Function<InstanceId, Mono<Void>> checkFn = mock(Function.class, (i) -> Mono.empty());
4242

4343
private final IntervalCheck intervalCheck = new IntervalCheck("test", this.checkFn, Duration.ofMillis(10),
44-
Duration.ofMillis(10));
44+
Duration.ofMillis(10), Duration.ofSeconds(1));
4545

4646
@Test
4747
public void should_check_after_being_started() throws InterruptedException {
@@ -81,6 +81,20 @@ public void should_recheck_after_retention_period() throws InterruptedException
8181
verify(this.checkFn, atLeast(2)).apply(INSTANCE_ID);
8282
}
8383

84+
@Test
85+
public void should_not_wait_longer_than_maxBackoff() throws InterruptedException {
86+
this.intervalCheck.setInterval(Duration.ofMillis(10));
87+
this.intervalCheck.setMinRetention(Duration.ofMillis(10));
88+
this.intervalCheck.setMaxBackoff(Duration.ofSeconds(2));
89+
this.intervalCheck.markAsChecked(INSTANCE_ID);
90+
91+
when(this.checkFn.apply(any())).thenReturn(Mono.error(new RuntimeException("Test")));
92+
93+
this.intervalCheck.start();
94+
Thread.sleep(1000 * 10);
95+
verify(this.checkFn, atLeast(7)).apply(INSTANCE_ID);
96+
}
97+
8498
@Test
8599
public void should_check_after_error() throws InterruptedException {
86100
this.intervalCheck.markAsChecked(INSTANCE_ID);

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/server/services/StatusUpdateTriggerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public void setUp() throws Exception {
5858
when(this.updater.updateStatus(any(InstanceId.class))).thenReturn(Mono.empty());
5959
when(this.updater.timeout(any())).thenReturn(this.updater);
6060

61-
this.trigger = new StatusUpdateTrigger(this.updater, this.events.flux());
61+
this.trigger = new StatusUpdateTrigger(this.updater, this.events.flux(), Duration.ofSeconds(10),
62+
Duration.ofSeconds(10), Duration.ofSeconds(60));
6263
this.trigger.start();
6364
await().until(this.events::wasSubscribed);
6465
}

0 commit comments

Comments
 (0)