Skip to content

Commit

Permalink
Issue ReactiveX#258: Add webflux support circuitbreaker annotation
Browse files Browse the repository at this point in the history
* Add response predicate to retry sync and async for enhancement ReactiveX#259

*  ReactiveX#258 add the support to the webflux types in the circuit breaker annotation AOP

*  ReactiveX#258 review comments

*  ReactiveX#258 review comments
  • Loading branch information
Romeh authored and RobWin committed Mar 11, 2019
1 parent 2a53946 commit 6b7078f
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 201 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019 Mahmoud Romeh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.resilience4j.circuitbreaker.annotation;

/**
* API type support for circuit breaker annotation
*/
public enum ApiType {

DEFAULT, WEBFLUX
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/
package io.github.resilience4j.circuitbreaker.annotation;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This annotation can be applied to a class or a specific method.
Expand All @@ -29,11 +33,17 @@
@Documented
public @interface CircuitBreaker {

/**
* Name of the circuit breaker.
*
* @return the name of the circuit breaker
*/
String name();
/**
* Name of the circuit breaker.
*
* @return the name of the circuit breaker
*/
String name();

/**
* @return the type of circuit breaker (default or webflux which is reactor circuit breaker)
*/
ApiType type() default ApiType.DEFAULT;


}
1 change: 1 addition & 0 deletions resilience4j-spring-boot2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
testCompile ( libraries.spring_boot2_test )
testCompile ( libraries.micrometer_prometheus )
testCompile ( libraries.spring_boot2_web )
testCompile project(':resilience4j-reactor')
}

compileJava.dependsOn(processResources)
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package io.github.resilience4j.circuitbreaker;

import io.github.resilience4j.circuitbreaker.autoconfigure.CircuitBreakerProperties;
import io.github.resilience4j.circuitbreaker.configure.CircuitBreakerAspect;
import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse;
import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse;
import io.github.resilience4j.service.test.DummyService;
import io.github.resilience4j.service.test.TestApplication;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.time.Duration;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -29,99 +29,155 @@
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.time.Duration;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import io.github.resilience4j.circuitbreaker.autoconfigure.CircuitBreakerProperties;
import io.github.resilience4j.circuitbreaker.configure.CircuitBreakerAspect;
import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse;
import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse;
import io.github.resilience4j.service.test.DummyService;
import io.github.resilience4j.service.test.ReactiveDummyService;
import io.github.resilience4j.service.test.TestApplication;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = TestApplication.class)
classes = TestApplication.class)
public class CircuitBreakerAutoConfigurationTest {

@Autowired
CircuitBreakerRegistry circuitBreakerRegistry;
@Autowired
CircuitBreakerRegistry circuitBreakerRegistry;

@Autowired
CircuitBreakerProperties circuitBreakerProperties;
@Autowired
CircuitBreakerProperties circuitBreakerProperties;

@Autowired
CircuitBreakerAspect circuitBreakerAspect;
@Autowired
CircuitBreakerAspect circuitBreakerAspect;

@Autowired
DummyService dummyService;
@Autowired
DummyService dummyService;

@Autowired
private TestRestTemplate restTemplate;
@Autowired
private TestRestTemplate restTemplate;

/**
* The test verifies that a CircuitBreaker instance is created and configured properly when the DummyService is invoked and
* that the CircuitBreaker records successful and failed calls.
*/
@Test
public void testCircuitBreakerAutoConfiguration() throws IOException {
assertThat(circuitBreakerRegistry).isNotNull();
assertThat(circuitBreakerProperties).isNotNull();
@Autowired
private ReactiveDummyService reactiveDummyService;

try {
dummyService.doSomething(true);
} catch (IOException ex) {
// Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure.
}
// The invocation is recorded by the CircuitBreaker as a success.
dummyService.doSomething(false);
/**
* The test verifies that a CircuitBreaker instance is created and configured properly when the DummyService is invoked and
* that the CircuitBreaker records successful and failed calls.
*/
@Test
public void testCircuitBreakerAutoConfiguration() throws IOException {
assertThat(circuitBreakerRegistry).isNotNull();
assertThat(circuitBreakerProperties).isNotNull();

CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(DummyService.BACKEND);
assertThat(circuitBreaker).isNotNull();

assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(2);
assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
try {
dummyService.doSomething(true);
} catch (IOException ex) {
// Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure.
}
// The invocation is recorded by the CircuitBreaker as a success.
dummyService.doSomething(false);

CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(DummyService.BACKEND);
assertThat(circuitBreaker).isNotNull();

assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(2);
assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);

// expect circuitbreaker is configured as defined in application.yml
assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInClosedState()).isEqualTo(6);
assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInHalfOpenState()).isEqualTo(2);
assertThat(circuitBreaker.getCircuitBreakerConfig().getFailureRateThreshold()).isEqualTo(70f);
assertThat(circuitBreaker.getCircuitBreakerConfig().getWaitDurationInOpenState()).isEqualByComparingTo(Duration.ofSeconds(5L));

// expect circuitbreakers actuator endpoint contains both circuitbreakers
ResponseEntity<CircuitBreakerEndpointResponse> circuitBreakerList = restTemplate.getForEntity("/actuator/circuitbreakers", CircuitBreakerEndpointResponse.class);
assertThat(circuitBreakerList.getBody().getCircuitBreakers()).hasSize(2).containsExactly("backendA", "backendB");

// expect circuitbreaker-event actuator endpoint recorded both events
ResponseEntity<CircuitBreakerEventsEndpointResponse> circuitBreakerEventList = restTemplate.getForEntity("/actuator/circuitbreakerevents", CircuitBreakerEventsEndpointResponse.class);
assertThat(circuitBreakerEventList.getBody().getCircuitBreakerEvents()).hasSize(4);

circuitBreakerEventList = restTemplate.getForEntity("/actuator/circuitbreakerevents/backendA", CircuitBreakerEventsEndpointResponse.class);
assertThat(circuitBreakerEventList.getBody().getCircuitBreakerEvents()).hasSize(2);

// expect no health indicator for backendB, as it is disabled via properties
ResponseEntity<HealthResponse> healthResponse = restTemplate.getForEntity("/actuator/health", HealthResponse.class);
assertThat(healthResponse.getBody().getDetails()).isNotNull();
assertThat(healthResponse.getBody().getDetails().get("backendACircuitBreaker")).isNotNull();
assertThat(healthResponse.getBody().getDetails().get("backendBCircuitBreaker")).isNull();

assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new RecordedException())).isTrue();
assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new IgnoredException())).isFalse();

// Verify that an exception for which recordFailurePredicate returns false and it is not included in
// recordExceptions evaluates to false.
assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new Exception())).isFalse();

// expect aspect configured as defined in application.yml
assertThat(circuitBreakerAspect.getOrder()).isEqualTo(400);
}

/**
* The test verifies that a CircuitBreaker instance is created and configured properly when the DummyService is invoked and
* that the CircuitBreaker records successful and failed calls.
*/
@Test
public void testCircuitBreakerAutoConfigurationReactive() throws IOException {
assertThat(circuitBreakerRegistry).isNotNull();
assertThat(circuitBreakerProperties).isNotNull();

try {
reactiveDummyService.doSomethingFlux(true).subscribe(String::toUpperCase, Throwable::getCause);
} catch (IOException ex) {
// Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure.
}
// The invocation is recorded by the CircuitBreaker as a success.
reactiveDummyService.doSomethingFlux(false).subscribe(String::toUpperCase, Throwable::getCause);

// expect circuitbreaker is configured as defined in application.yml
assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInClosedState()).isEqualTo(6);
assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInHalfOpenState()).isEqualTo(2);
assertThat(circuitBreaker.getCircuitBreakerConfig().getFailureRateThreshold()).isEqualTo(70f);
assertThat(circuitBreaker.getCircuitBreakerConfig().getWaitDurationInOpenState()).isEqualByComparingTo(Duration.ofSeconds(5L));
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(ReactiveDummyService.BACKEND);
assertThat(circuitBreaker).isNotNull();

// expect circuitbreakers actuator endpoint contains both circuitbreakers
ResponseEntity<CircuitBreakerEndpointResponse> circuitBreakerList = restTemplate.getForEntity("/actuator/circuitbreakers", CircuitBreakerEndpointResponse.class);
assertThat(circuitBreakerList.getBody().getCircuitBreakers()).hasSize(2).containsExactly("backendA", "backendB");
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(2);
assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);

// expect circuitbreaker-event actuator endpoint recorded both events
ResponseEntity<CircuitBreakerEventsEndpointResponse> circuitBreakerEventList = restTemplate.getForEntity("/actuator/circuitbreakerevents", CircuitBreakerEventsEndpointResponse.class);
assertThat(circuitBreakerEventList.getBody().getCircuitBreakerEvents()).hasSize(2);
// expect circuitbreaker is configured as defined in application.yml
assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInClosedState()).isEqualTo(6);
assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInHalfOpenState()).isEqualTo(2);
assertThat(circuitBreaker.getCircuitBreakerConfig().getFailureRateThreshold()).isEqualTo(70f);
assertThat(circuitBreaker.getCircuitBreakerConfig().getWaitDurationInOpenState()).isEqualByComparingTo(Duration.ofSeconds(5L));

circuitBreakerEventList = restTemplate.getForEntity("/actuator/circuitbreakerevents?name=backendA", CircuitBreakerEventsEndpointResponse.class);
assertThat(circuitBreakerEventList.getBody().getCircuitBreakerEvents()).hasSize(2);
// expect circuitbreakers actuator endpoint contains both circuitbreakers
ResponseEntity<CircuitBreakerEndpointResponse> circuitBreakerList = restTemplate.getForEntity("/actuator/circuitbreakers", CircuitBreakerEndpointResponse.class);
assertThat(circuitBreakerList.getBody().getCircuitBreakers()).hasSize(2).containsExactly("backendA", "backendB");

// expect no health indicator for backendB, as it is disabled via properties
ResponseEntity<HealthResponse> healthResponse = restTemplate.getForEntity("/actuator/health", HealthResponse.class);
assertThat(healthResponse.getBody().getDetails()).isNotNull();
assertThat(healthResponse.getBody().getDetails().get("backendACircuitBreaker")).isNotNull();
assertThat(healthResponse.getBody().getDetails().get("backendBCircuitBreaker")).isNull();
// expect circuitbreaker-event actuator endpoint recorded both events
ResponseEntity<CircuitBreakerEventsEndpointResponse> circuitBreakerEventList = restTemplate.getForEntity("/actuator/circuitbreakerevents", CircuitBreakerEventsEndpointResponse.class);
assertThat(circuitBreakerEventList.getBody().getCircuitBreakerEvents()).hasSize(2);

assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new RecordedException())).isTrue();
assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new IgnoredException())).isFalse();
circuitBreakerEventList = restTemplate.getForEntity("/actuator/circuitbreakerevents/backendB", CircuitBreakerEventsEndpointResponse.class);
assertThat(circuitBreakerEventList.getBody().getCircuitBreakerEvents()).hasSize(2);

// Verify that an exception for which recordFailurePredicate returns false and it is not included in
// recordExceptions evaluates to false.
assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new Exception())).isFalse();
// expect no health indicator for backendB, as it is disabled via properties
ResponseEntity<HealthResponse> healthResponse = restTemplate.getForEntity("/actuator/health", HealthResponse.class);
assertThat(healthResponse.getBody().getDetails()).isNotNull();
assertThat(healthResponse.getBody().getDetails().get("backendACircuitBreaker")).isNotNull();
assertThat(healthResponse.getBody().getDetails().get("backendBCircuitBreaker")).isNull();

// expect aspect configured as defined in application.yml
assertThat(circuitBreakerAspect.getOrder()).isEqualTo(400);
}
// expect aspect configured as defined in application.yml
assertThat(circuitBreakerAspect.getOrder()).isEqualTo(400);
}

private final static class HealthResponse {
private Map<String, Object> details;
private final static class HealthResponse {
private Map<String, Object> details;

public Map<String, Object> getDetails() {
public Map<String, Object> getDetails() {
return details;
}

public void setDetails(Map<String, Object> details) {
public void setDetails(Map<String, Object> details) {
this.details = details;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019 Mahmoud Romeh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.resilience4j.service.test;


import java.io.IOException;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
* reactive web service test using reactor types
*/
public interface ReactiveDummyService {
String BACKEND = "backendB";

Flux<String> doSomethingFlux(boolean throwException) throws IOException;

Mono<String> doSomethingMono(boolean throwException) throws IOException;
}
Loading

0 comments on commit 6b7078f

Please sign in to comment.