Skip to content

Commit 00eff37

Browse files
committed
Replaced custom attempt to implement rolling window for circuit breaker failures with "out of the box" to simplify code.
1 parent 47359d2 commit 00eff37

12 files changed

Lines changed: 344 additions & 216 deletions

docs/config-app.md

Lines changed: 309 additions & 138 deletions
Large diffs are not rendered by default.

src/main/java/org/prebid/server/geolocation/CircuitBreakerSecuredGeoLocationService.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.prebid.server.metric.Metrics;
1111
import org.prebid.server.vertx.CircuitBreaker;
1212

13-
import java.time.Clock;
1413
import java.util.Objects;
1514
import java.util.concurrent.TimeUnit;
1615

@@ -31,13 +30,16 @@ public CircuitBreakerSecuredGeoLocationService(Vertx vertx,
3130
Metrics metrics,
3231
int openingThreshold,
3332
long openingIntervalMs,
34-
long closingIntervalMs,
35-
Clock clock) {
33+
long closingIntervalMs) {
3634

3735
this.geoLocationService = Objects.requireNonNull(geoLocationService);
3836

39-
breaker = new CircuitBreaker("geo_cb", Objects.requireNonNull(vertx),
40-
openingThreshold, openingIntervalMs, closingIntervalMs, Objects.requireNonNull(clock))
37+
breaker = new CircuitBreaker(
38+
"geo_cb",
39+
Objects.requireNonNull(vertx),
40+
openingThreshold,
41+
openingIntervalMs,
42+
closingIntervalMs)
4143
.openHandler(ignored -> circuitOpened())
4244
.halfOpenHandler(ignored -> circuitHalfOpened())
4345
.closeHandler(ignored -> circuitClosed());

src/main/java/org/prebid/server/spring/config/GeoLocationConfiguration.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.io.InputStreamReader;
3131
import java.io.Reader;
3232
import java.nio.charset.StandardCharsets;
33-
import java.time.Clock;
3433
import java.time.ZoneId;
3534
import java.util.ArrayList;
3635
import java.util.List;
@@ -70,13 +69,15 @@ CircuitBreakerSecuredGeoLocationService circuitBreakerSecuredGeoLocationService(
7069
Vertx vertx,
7170
Metrics metrics,
7271
FileSyncerProperties fileSyncerProperties,
73-
@Qualifier("maxMindCircuitBreakerProperties") CircuitBreakerProperties circuitBreakerProperties,
74-
Clock clock) {
75-
76-
return new CircuitBreakerSecuredGeoLocationService(vertx,
77-
createGeoLocationService(fileSyncerProperties, vertx), metrics,
78-
circuitBreakerProperties.getOpeningThreshold(), circuitBreakerProperties.getOpeningIntervalMs(),
79-
circuitBreakerProperties.getClosingIntervalMs(), clock);
72+
@Qualifier("maxMindCircuitBreakerProperties") CircuitBreakerProperties circuitBreakerProperties) {
73+
74+
return new CircuitBreakerSecuredGeoLocationService(
75+
vertx,
76+
createGeoLocationService(fileSyncerProperties, vertx),
77+
metrics,
78+
circuitBreakerProperties.getOpeningThreshold(),
79+
circuitBreakerProperties.getOpeningIntervalMs(),
80+
circuitBreakerProperties.getClosingIntervalMs());
8081
}
8182

8283
private GeoLocationService createGeoLocationService(FileSyncerProperties properties, Vertx vertx) {

src/main/java/org/prebid/server/spring/config/ServiceConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,7 @@ CircuitBreakerSecuredHttpClient circuitBreakerSecuredHttpClient(
660660
Metrics metrics,
661661
HttpClientProperties httpClientProperties,
662662
@Qualifier("httpClientCircuitBreakerProperties")
663-
HttpClientCircuitBreakerProperties circuitBreakerProperties,
664-
Clock clock) {
663+
HttpClientCircuitBreakerProperties circuitBreakerProperties) {
665664

666665
final HttpClient httpClient = createBasicHttpClient(vertx, httpClientProperties);
667666

@@ -672,8 +671,7 @@ CircuitBreakerSecuredHttpClient circuitBreakerSecuredHttpClient(
672671
circuitBreakerProperties.getOpeningThreshold(),
673672
circuitBreakerProperties.getOpeningIntervalMs(),
674673
circuitBreakerProperties.getClosingIntervalMs(),
675-
circuitBreakerProperties.getIdleExpireHours(),
676-
clock);
674+
circuitBreakerProperties.getIdleExpireHours());
677675
}
678676

679677
private static BasicHttpClient createBasicHttpClient(Vertx vertx, HttpClientProperties httpClientProperties) {

src/main/java/org/prebid/server/spring/config/database/DatabaseConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ CircuitBreakerSecuredDatabaseClient circuitBreakerSecuredAsyncDatabaseClient(
167167
metrics,
168168
circuitBreakerProperties.getOpeningThreshold(),
169169
circuitBreakerProperties.getOpeningIntervalMs(),
170-
circuitBreakerProperties.getClosingIntervalMs(),
171-
clock);
170+
circuitBreakerProperties.getClosingIntervalMs());
172171
}
173172

174173
private static BasicDatabaseClient createBasicDatabaseClient(Pool pool,

src/main/java/org/prebid/server/vertx/CircuitBreaker.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
import io.vertx.core.Handler;
66
import io.vertx.core.Vertx;
77

8-
import java.time.Clock;
98
import java.util.Objects;
109
import java.util.function.Supplier;
1110

12-
/**
13-
* Wrapper over Vert.x {@link io.vertx.circuitbreaker.CircuitBreaker} with functionality
14-
* to reset failure counter to adjust open-circuit time frame.
15-
*/
1611
public class CircuitBreaker {
1712

1813
private final io.vertx.circuitbreaker.CircuitBreaker breaker;
@@ -21,44 +16,32 @@ public CircuitBreaker(String name,
2116
Vertx vertx,
2217
int openingThreshold,
2318
long openingIntervalMs,
24-
long closingIntervalMs,
25-
Clock clock) {
19+
long closingIntervalMs) {
2620

2721
breaker = io.vertx.circuitbreaker.CircuitBreaker.create(
2822
Objects.requireNonNull(name),
2923
Objects.requireNonNull(vertx),
3024
new CircuitBreakerOptions()
3125
.setNotificationPeriod(0)
3226
.setMaxFailures(openingThreshold)
27+
.setFailuresRollingWindow(openingIntervalMs)
3328
.setResetTimeout(closingIntervalMs));
3429
}
3530

36-
/**
37-
* Executes the given operation with the circuit breaker control.
38-
*/
3931
public <T> Future<T> execute(Supplier<Future<T>> command) {
4032
return breaker.execute(command);
4133
}
4234

43-
/**
44-
* Sets a {@link Handler} invoked when the circuit breaker state switches to open.
45-
*/
4635
public CircuitBreaker openHandler(Handler<Void> handler) {
4736
breaker.openHandler(handler);
4837
return this;
4938
}
5039

51-
/**
52-
* Sets a {@link Handler} invoked when the circuit breaker state switches to half-open.
53-
*/
5440
public CircuitBreaker halfOpenHandler(Handler<Void> handler) {
5541
breaker.halfOpenHandler(handler);
5642
return this;
5743
}
5844

59-
/**
60-
* Sets a {@link Handler} invoked when the circuit breaker state switches to close.
61-
*/
6245
public CircuitBreaker closeHandler(Handler<Void> handler) {
6346
breaker.closeHandler(handler);
6447
return this;

src/main/java/org/prebid/server/vertx/database/CircuitBreakerSecuredDatabaseClient.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.prebid.server.metric.Metrics;
1212
import org.prebid.server.vertx.CircuitBreaker;
1313

14-
import java.time.Clock;
1514
import java.util.List;
1615
import java.util.Objects;
1716
import java.util.concurrent.TimeUnit;
@@ -34,8 +33,7 @@ public CircuitBreakerSecuredDatabaseClient(Vertx vertx,
3433
Metrics metrics,
3534
int openingThreshold,
3635
long openingIntervalMs,
37-
long closingIntervalMs,
38-
Clock clock) {
36+
long closingIntervalMs) {
3937

4038
this.databaseClient = Objects.requireNonNull(databaseClient);
4139

@@ -44,8 +42,7 @@ public CircuitBreakerSecuredDatabaseClient(Vertx vertx,
4442
Objects.requireNonNull(vertx),
4543
openingThreshold,
4644
openingIntervalMs,
47-
closingIntervalMs,
48-
Objects.requireNonNull(clock))
45+
closingIntervalMs)
4946
.openHandler(ignored -> circuitOpened())
5047
.halfOpenHandler(ignored -> circuitHalfOpened())
5148
.closeHandler(ignored -> circuitClosed());

src/main/java/org/prebid/server/vertx/httpclient/CircuitBreakerSecuredHttpClient.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import java.net.MalformedURLException;
1717
import java.net.URL;
18-
import java.time.Clock;
1918
import java.util.Map;
2019
import java.util.Objects;
2120
import java.util.concurrent.TimeUnit;
@@ -41,13 +40,12 @@ public CircuitBreakerSecuredHttpClient(Vertx vertx,
4140
int openingThreshold,
4241
long openingIntervalMs,
4342
long closingIntervalMs,
44-
int idleExpireHours,
45-
Clock clock) {
43+
int idleExpireHours) {
4644

4745
this.httpClient = Objects.requireNonNull(httpClient);
4846

4947
circuitBreakerCreator = name -> createCircuitBreaker(
50-
name, vertx, openingThreshold, openingIntervalMs, closingIntervalMs, clock, metrics);
48+
name, vertx, openingThreshold, openingIntervalMs, closingIntervalMs, metrics);
5149

5250
circuitBreakerByName = Caffeine.newBuilder()
5351
.expireAfterAccess(idleExpireHours, TimeUnit.HOURS)
@@ -88,16 +86,14 @@ private CircuitBreaker createCircuitBreaker(String name,
8886
int openingThreshold,
8987
long openingIntervalMs,
9088
long closingIntervalMs,
91-
Clock clock,
9289
Metrics metrics) {
9390

9491
final CircuitBreaker circuitBreaker = new CircuitBreaker(
9592
"http_cb_" + name,
9693
Objects.requireNonNull(vertx),
9794
openingThreshold,
9895
openingIntervalMs,
99-
closingIntervalMs,
100-
Objects.requireNonNull(clock))
96+
closingIntervalMs)
10197
.openHandler(ignored -> circuitOpened(name))
10298
.halfOpenHandler(ignored -> circuitHalfOpened(name))
10399
.closeHandler(ignored -> circuitClosed(name));

src/test/java/org/prebid/server/geolocation/CircuitBreakerSecuredGeoLocationServiceTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
import org.prebid.server.geolocation.model.GeoInfo;
1717
import org.prebid.server.metric.Metrics;
1818

19-
import java.time.Clock;
20-
import java.time.Instant;
21-
import java.time.ZoneId;
2219
import java.util.concurrent.TimeUnit;
2320
import java.util.function.BooleanSupplier;
2421

@@ -34,7 +31,6 @@ public class CircuitBreakerSecuredGeoLocationServiceTest {
3431

3532
private Vertx vertx;
3633

37-
private Clock clock;
3834
@Mock
3935
private GeoLocationService wrappedGeoLocationService;
4036
@Mock
@@ -45,9 +41,8 @@ public class CircuitBreakerSecuredGeoLocationServiceTest {
4541
@BeforeEach
4642
public void setUp() {
4743
vertx = Vertx.vertx();
48-
clock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
4944
geoLocationService = new CircuitBreakerSecuredGeoLocationService(vertx, wrappedGeoLocationService, metrics, 1,
50-
100L, 200L, clock);
45+
1000L, 200L);
5146
}
5247

5348
@AfterEach
@@ -145,7 +140,7 @@ public void lookupShouldSucceedsIfCircuitIsHalfOpenedAndWrappedGeoLocationSuccee
145140
public void lookupShouldFailsWithOriginalExceptionIfOpeningIntervalExceeds() {
146141
// given
147142
geoLocationService = new CircuitBreakerSecuredGeoLocationService(vertx, wrappedGeoLocationService, metrics, 2,
148-
100L, 200L, clock);
143+
100L, 200L);
149144

150145
givenWrappedGeoLocationReturning(
151146
Future.failedFuture(new RuntimeException("exception1")),

src/test/java/org/prebid/server/vertx/CircuitBreakerTest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.prebid.server.vertx;
22

33
import io.vertx.core.Future;
4-
import io.vertx.core.Handler;
54
import io.vertx.core.Promise;
65
import io.vertx.core.Vertx;
76
import io.vertx.junit5.VertxExtension;
@@ -12,9 +11,6 @@
1211
import org.junit.jupiter.api.extension.ExtendWith;
1312
import org.mockito.junit.jupiter.MockitoExtension;
1413

15-
import java.time.Clock;
16-
import java.time.Instant;
17-
import java.time.ZoneId;
1814
import java.util.concurrent.TimeUnit;
1915
import java.util.function.Supplier;
2016

@@ -26,15 +22,12 @@ public class CircuitBreakerTest {
2622

2723
private Vertx vertx;
2824

29-
private Clock clock;
30-
3125
private CircuitBreaker circuitBreaker;
3226

3327
@BeforeEach
3428
public void setUp() {
3529
vertx = Vertx.vertx();
36-
clock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
37-
circuitBreaker = new CircuitBreaker("name", vertx, 1, 100L, 200L, clock);
30+
circuitBreaker = new CircuitBreaker("name", vertx, 1, 1000L, 200L);
3831
}
3932

4033
@AfterEach
@@ -118,7 +111,7 @@ public void executeShouldSucceedsIfCircuitIsHalfOpenedAndOperationSucceeds() {
118111
@Test
119112
public void executeShouldFailsWithOriginalExceptionIfOpeningIntervalExceeds() {
120113
// given
121-
circuitBreaker = new CircuitBreaker("name", vertx, 2, 100L, 200L, clock);
114+
circuitBreaker = new CircuitBreaker("name", vertx, 2, 100L, 200L);
122115

123116
// when
124117
final Future<?> future1 = executeWithFail("exception1");

0 commit comments

Comments
 (0)