Search in sources :

Example 1 with CircuitBreakerConfig

use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.

the class StateTransitionMetricsTest method setUp.

@Before
public void setUp() throws Exception {
    CircuitBreakerConfig config = CircuitBreakerConfig.custom().waitDurationInOpenState(Duration.ofSeconds(1)).failureRateThreshold(50).ringBufferSizeInHalfOpenState(3).ringBufferSizeInClosedState(10).build();
    circuitBreaker = CircuitBreakerRegistry.ofDefaults().circuitBreaker("test", config);
    metricRegistry.registerAll(CircuitBreakerMetrics.ofCircuitBreaker(circuitBreaker));
}
Also used : CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Before(org.junit.Before)

Example 2 with CircuitBreakerConfig

use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.

the class CircularEventConsumerTest method shouldBufferAllEvents.

@Test
public void shouldBufferAllEvents() {
    // Given
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().ringBufferSizeInClosedState(3).recordFailure(throwable -> API.Match(throwable).of(Case($(instanceOf(WebServiceException.class)), true), Case($(), false))).build();
    CircuitBreaker circuitBreaker = CircuitBreaker.of("testName", circuitBreakerConfig);
    CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(10);
    circuitBreaker.getEventPublisher().onEvent(ringBuffer);
    assertThat(ringBuffer.getBufferedEvents()).isEmpty();
    // When
    circuitBreaker.onSuccess(0);
    circuitBreaker.onError(0, new WebServiceException("Bla"));
    circuitBreaker.onError(0, new IOException("Bla"));
    circuitBreaker.onError(0, new WebServiceException("Bla"));
    // Then
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(2);
    circuitBreaker.reset();
    CircuitBreaker.Metrics resetMetrics = circuitBreaker.getMetrics();
    assertThat(resetMetrics.getNumberOfBufferedCalls()).isEqualTo(0);
    assertThat(resetMetrics.getNumberOfFailedCalls()).isEqualTo(0);
    // Should store 3 events, because circuit emits 2 error events and one state transition event
    assertThat(ringBuffer.getBufferedEvents()).hasSize(7);
    assertThat(ringBuffer.getBufferedEvents()).extracting("eventType").containsExactly(Type.SUCCESS, Type.ERROR, Type.IGNORED_ERROR, Type.ERROR, Type.STATE_TRANSITION, Type.STATE_TRANSITION, Type.RESET);
// ringBuffer.getBufferedEvents().forEach(event -> LOG.info(event.toString()));
}
Also used : CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) WebServiceException(javax.xml.ws.WebServiceException) Predicates.instanceOf(io.vavr.Predicates.instanceOf) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Type(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type) Test(org.junit.Test) IOException(java.io.IOException) Case(io.vavr.API.Case) API(io.vavr.API) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) API.$(io.vavr.API.$) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) WebServiceException(javax.xml.ws.WebServiceException) IOException(java.io.IOException) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Example 3 with CircuitBreakerConfig

use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.

the class CircuitBreakerHealthIndicator method addDetails.

private Health.Builder addDetails(Health.Builder builder, CircuitBreaker circuitBreaker) {
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    CircuitBreakerConfig config = circuitBreaker.getCircuitBreakerConfig();
    builder.withDetail(FAILURE_RATE, metrics.getFailureRate() + "%").withDetail(FAILURE_RATE_THRESHOLD, config.getFailureRateThreshold() + "%").withDetail(MAX_BUFFERED_CALLS, metrics.getMaxNumberOfBufferedCalls()).withDetail(BUFFERED_CALLS, metrics.getNumberOfBufferedCalls()).withDetail(FAILED_CALLS, metrics.getNumberOfFailedCalls()).withDetail(NOT_PERMITTED, metrics.getNumberOfNotPermittedCalls());
    return builder;
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)

Example 4 with CircuitBreakerConfig

use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project java-chassis by ServiceComb.

the class CircuitBreakerHandler method getCircuitBreaker.

private CircuitBreaker getCircuitBreaker(CircuitBreakerPolicy policy) {
    LOGGER.info("applying new policy: {}", policy.toString());
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().failureRateThreshold(policy.getFailureRateThreshold()).slowCallRateThreshold(policy.getSlowCallRateThreshold()).waitDurationInOpenState(Duration.parse(policy.getWaitDurationInOpenState())).slowCallDurationThreshold(Duration.parse(policy.getSlowCallDurationThreshold())).permittedNumberOfCallsInHalfOpenState(policy.getPermittedNumberOfCallsInHalfOpenState()).minimumNumberOfCalls(policy.getMinimumNumberOfCalls()).slidingWindowType(policy.getSlidingWindowTypeEnum()).slidingWindowSize(Integer.valueOf(policy.getSlidingWindowSize())).build();
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
    return circuitBreakerRegistry.circuitBreaker(policy.getName(), circuitBreakerConfig);
}
Also used : CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)

Example 5 with CircuitBreakerConfig

use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.

the class CircuitBreakerEventEmitterTest method testEmitter.

@Test
public void testEmitter() throws IOException {
    CircuitBreakerConfig config = CircuitBreakerConfig.custom().ringBufferSizeInClosedState(3).ringBufferSizeInHalfOpenState(2).failureRateThreshold(66).waitDurationInOpenState(Duration.ofSeconds(1)).recordFailure(e -> !(e instanceof IllegalArgumentException)).build();
    CircuitBreaker circuitBreaker = CircuitBreakerRegistry.ofDefaults().circuitBreaker("test", config);
    Runnable run = decorateRunnable(circuitBreaker, () -> System.out.println("."));
    Runnable fail = decorateRunnable(circuitBreaker, () -> {
        throw new ConcurrentModificationException();
    });
    Runnable ignore = decorateRunnable(circuitBreaker, () -> {
        throw new IllegalArgumentException();
    });
    SseEmitter sseEmitter = createSseEmitter(ReactorAdapter.toFlux(circuitBreaker.getEventPublisher()));
    TestHandler handler = new TestHandler();
    sseEmitter.initialize(handler);
    exec(run, 2);
    exec(ignore, 1);
    exec(fail, 3);
    circuitBreaker.reset();
    exec(run, 2);
    circuitBreaker.reset();
    sseEmitter.complete();
    assert handler.isCompleted;
    exec(run, 2);
    List<CircuitBreakerEvent.Type> events = handler.events.stream().map(CircuitBreakerEventDTO::getType).collect(toList());
    then(events).containsExactly(SUCCESS, SUCCESS, IGNORED_ERROR, ERROR, ERROR, STATE_TRANSITION, NOT_PERMITTED, STATE_TRANSITION, RESET, SUCCESS, SUCCESS, RESET);
}
Also used : CircuitBreakerEventEmitter.createSseEmitter(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreaker.decorateRunnable(io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable) MediaType(org.springframework.http.MediaType) Test(org.junit.Test) IOException(java.io.IOException) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) ArrayList(java.util.ArrayList) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Try(io.vavr.control.Try) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) CircuitBreakerEventDTO(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTO) Duration(java.time.Duration) ReactorAdapter(io.github.resilience4j.adapter.ReactorAdapter) Type(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ConcurrentModificationException(java.util.ConcurrentModificationException) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) ConcurrentModificationException(java.util.ConcurrentModificationException) MediaType(org.springframework.http.MediaType) Type(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type) CircuitBreaker.decorateRunnable(io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable) CircuitBreakerEventEmitter.createSseEmitter(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Aggregations

CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)8 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)6 Test (org.junit.Test)4 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)3 CircuitBreakerEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent)2 Type (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type)2 IOException (java.io.IOException)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Before (org.junit.Before)2 ReactorAdapter (io.github.resilience4j.adapter.ReactorAdapter)1 CircuitBreaker.decorateRunnable (io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable)1 CircuitBreakerOnStateTransitionEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent)1 InMemoryCircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry)1 CircuitBreakerEventDTO (io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTO)1 CircuitBreakerEventEmitter.createSseEmitter (io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter)1 CircuitBreakerHealthIndicator (io.github.resilience4j.circuitbreaker.monitoring.health.CircuitBreakerHealthIndicator)1 EventConsumer (io.github.resilience4j.core.EventConsumer)1 API (io.vavr.API)1