Java Code Examples for io.github.resilience4j.circuitbreaker.CircuitBreaker#State
The following examples show how to use
io.github.resilience4j.circuitbreaker.CircuitBreaker#State .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Resilience4jTest.java From spring-cloud-formula with Apache License 2.0 | 6 votes |
@Test public void testCircuitBreakerWithOpen() { Supplier<Void> supplier = () -> { this.demoLogic(80); return null; }; for (int i = 0; i < 1000; i++) { try { Void result = circuitBreaker.executeSupplier(supplier); System.out.println(result); } catch (Exception e) { // do nothing } } CircuitBreaker.State state = circuitBreaker.getState(); assertEquals(state, CircuitBreaker.State.OPEN); }
Example 2
Source File: CircuitBreakersHealthIndicatorTest.java From resilience4j with Apache License 2.0 | 5 votes |
private void assertState(CircuitBreaker.State givenState, Status expectedStatus, Map<String, Object> details) { then(details.get(givenState.name())).isInstanceOf(Health.class); Health health = (Health) details.get(givenState.name()); then(health.getStatus()).isEqualTo(expectedStatus); then(health.getDetails()) .contains( entry("state", givenState) ); }
Example 3
Source File: CircuitBreakersHealthIndicatorTest.java From resilience4j with Apache License 2.0 | 5 votes |
private void setCircuitBreakerWhen(CircuitBreaker.State givenState, CircuitBreaker circuitBreaker, CircuitBreakerConfig config, CircuitBreaker.Metrics metrics, io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties, CircuitBreakerConfigurationProperties circuitBreakerProperties, boolean allowHealthIndicatorToFail) { when(circuitBreaker.getName()).thenReturn(givenState.name()); when(circuitBreaker.getState()).thenReturn(givenState); when(circuitBreaker.getCircuitBreakerConfig()).thenReturn(config); when(circuitBreaker.getMetrics()).thenReturn(metrics); when(circuitBreakerProperties.findCircuitBreakerProperties(givenState.name())) .thenReturn(Optional.of(instanceProperties)); when(instanceProperties.getRegisterHealthIndicator()).thenReturn(true); when(instanceProperties.getAllowHealthIndicatorToFail()).thenReturn(allowHealthIndicatorToFail); }
Example 4
Source File: CircuitBreakersHealthIndicatorTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void healthIndicatorMaxImpactCanBeOverridden() { CircuitBreaker openCircuitBreaker = mock(CircuitBreaker.class); CircuitBreaker halfOpenCircuitBreaker = mock(CircuitBreaker.class); CircuitBreaker closeCircuitBreaker = mock(CircuitBreaker.class); Map<CircuitBreaker.State, CircuitBreaker> expectedStateToCircuitBreaker = new HashMap<>(); expectedStateToCircuitBreaker.put(OPEN, openCircuitBreaker); expectedStateToCircuitBreaker.put(HALF_OPEN, halfOpenCircuitBreaker); expectedStateToCircuitBreaker.put(CLOSED, closeCircuitBreaker); CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties = mock(CircuitBreakerConfigurationProperties.InstanceProperties.class); CircuitBreakerConfigurationProperties circuitBreakerProperties = mock(CircuitBreakerConfigurationProperties.class); // given CircuitBreakerRegistry registry = mock(CircuitBreakerRegistry.class); CircuitBreakerConfig config = mock(CircuitBreakerConfig.class); CircuitBreaker.Metrics metrics = mock(CircuitBreaker.Metrics.class); // when when(registry.getAllCircuitBreakers()).thenReturn(Array.ofAll(expectedStateToCircuitBreaker.values())); boolean allowHealthIndicatorToFail = false; // do not allow health indicator to fail expectedStateToCircuitBreaker.forEach( (state, circuitBreaker) -> setCircuitBreakerWhen(state, circuitBreaker, config, metrics, instanceProperties, circuitBreakerProperties, allowHealthIndicatorToFail)); CircuitBreakersHealthIndicator healthIndicator = new CircuitBreakersHealthIndicator(registry, circuitBreakerProperties, new SimpleStatusAggregator()); // then Health health = healthIndicator.health(); then(health.getStatus()).isEqualTo(Status.UP); then(health.getDetails()).containsKeys(OPEN.name(), HALF_OPEN.name(), CLOSED.name()); assertState(OPEN, new Status("CIRCUIT_OPEN"), health.getDetails()); assertState(HALF_OPEN, new Status("CIRCUIT_HALF_OPEN"), health.getDetails()); assertState(CLOSED, Status.UP, health.getDetails()); }
Example 5
Source File: CircuitBreakersHealthIndicatorTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void testHealthStatus() { CircuitBreaker openCircuitBreaker = mock(CircuitBreaker.class); CircuitBreaker halfOpenCircuitBreaker = mock(CircuitBreaker.class); CircuitBreaker closeCircuitBreaker = mock(CircuitBreaker.class); Map<CircuitBreaker.State, CircuitBreaker> expectedStateToCircuitBreaker = new HashMap<>(); expectedStateToCircuitBreaker.put(OPEN, openCircuitBreaker); expectedStateToCircuitBreaker.put(HALF_OPEN, halfOpenCircuitBreaker); expectedStateToCircuitBreaker.put(CLOSED, closeCircuitBreaker); CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties = mock(CircuitBreakerConfigurationProperties.InstanceProperties.class); CircuitBreakerConfigurationProperties circuitBreakerProperties = mock( CircuitBreakerConfigurationProperties.class); // given CircuitBreakerRegistry registry = mock(CircuitBreakerRegistry.class); CircuitBreakerConfig config = mock(CircuitBreakerConfig.class); CircuitBreaker.Metrics metrics = mock(CircuitBreaker.Metrics.class); // when when(registry.getAllCircuitBreakers()).thenReturn(Array.ofAll(expectedStateToCircuitBreaker.values())); boolean allowHealthIndicatorToFail = true; expectedStateToCircuitBreaker.forEach( (state, circuitBreaker) -> setCircuitBreakerWhen(state, circuitBreaker, config, metrics, instanceProperties, circuitBreakerProperties, allowHealthIndicatorToFail)); CircuitBreakersHealthIndicator healthIndicator = new CircuitBreakersHealthIndicator(registry, circuitBreakerProperties, new SimpleStatusAggregator()); // then Health health = healthIndicator.health(); then(health.getStatus()).isEqualTo(Status.DOWN); then(health.getDetails()).containsKeys(OPEN.name(), HALF_OPEN.name(), CLOSED.name()); assertState(OPEN, Status.DOWN, health.getDetails()); assertState(HALF_OPEN, new Status("CIRCUIT_HALF_OPEN"), health.getDetails()); assertState(CLOSED, Status.UP, health.getDetails()); }
Example 6
Source File: CircuitBreakerStateMachineTest.java From resilience4j with Apache License 2.0 | 4 votes |
@Test public void allCircuitBreakerStatesAllowTransitionToMetricsOnlyMode() { for (final CircuitBreaker.State state : CircuitBreaker.State.values()) { CircuitBreaker.StateTransition.transitionBetween(circuitBreaker.getName(), state, CircuitBreaker.State.METRICS_ONLY); } }
Example 7
Source File: CircuitBreakerStateDTO.java From resilience4j with Apache License 2.0 | 4 votes |
public void setCurrentState(CircuitBreaker.State currentState) { this.currentState = currentState; }
Example 8
Source File: CircuitBreakerStateDTO.java From resilience4j with Apache License 2.0 | 4 votes |
public CircuitBreaker.State getCurrentState() { return currentState; }
Example 9
Source File: CircuitBreakerStateDTO.java From resilience4j with Apache License 2.0 | 4 votes |
public CircuitBreakerStateDTO(String circuitBreakerName, CircuitBreaker.State currentState, CircuitBreakerMetricsDTO metrics) { this.circuitBreakerName = circuitBreakerName; this.currentState = currentState; this.metrics = metrics; }
Example 10
Source File: AbstractCircuitBreakerMetrics.java From resilience4j with Apache License 2.0 | 4 votes |
private void registerMetrics( MeterRegistry meterRegistry, CircuitBreaker circuitBreaker, List<Tag> customTags) { // Remove previous meters before register removeMetrics(meterRegistry, circuitBreaker.getName()); Set<Meter.Id> idSet = new HashSet<>(); final CircuitBreaker.State[] states = CircuitBreaker.State.values(); for (CircuitBreaker.State state : states) { idSet.add(Gauge.builder(names.getStateMetricName(), circuitBreaker, cb -> cb.getState() == state ? 1 : 0) .description("The states of the circuit breaker") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(KIND_STATE, state.name().toLowerCase()) .tags(customTags) .register(meterRegistry).getId()); } idSet.add(Gauge.builder(names.getBufferedCallsMetricName(), circuitBreaker, cb -> cb.getMetrics().getNumberOfFailedCalls()) .description("The number of buffered failed calls stored in the ring buffer") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(TagNames.KIND, KIND_FAILED) .tags(customTags) .register(meterRegistry).getId()); idSet.add(Gauge.builder(names.getBufferedCallsMetricName(), circuitBreaker, cb -> cb.getMetrics().getNumberOfSuccessfulCalls()) .description("The number of buffered successful calls stored in the ring buffer") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(TagNames.KIND, KIND_SUCCESSFUL) .tags(customTags) .register(meterRegistry).getId()); idSet.add(Gauge.builder(names.getSlowCallsMetricName(), circuitBreaker, cb -> cb.getMetrics().getNumberOfSlowSuccessfulCalls()) .description("The number of slow successful which were slower than a certain threshold") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(TagNames.KIND, KIND_SUCCESSFUL) .tags(customTags) .register(meterRegistry).getId()); idSet.add(Gauge.builder(names.getSlowCallsMetricName(), circuitBreaker, cb -> cb.getMetrics().getNumberOfSlowFailedCalls()) .description( "The number of slow failed calls which were slower than a certain threshold") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(TagNames.KIND, KIND_FAILED) .tags(customTags) .register(meterRegistry).getId()); idSet.add(Gauge.builder(names.getFailureRateMetricName(), circuitBreaker, cb -> cb.getMetrics().getFailureRate()) .description("The failure rate of the circuit breaker") .tag(TagNames.NAME, circuitBreaker.getName()) .tags(customTags) .register(meterRegistry).getId()); idSet.add(Gauge.builder(names.getSlowCallRateMetricName(), circuitBreaker, cb -> cb.getMetrics().getSlowCallRate()) .description("The slow call of the circuit breaker") .tag(TagNames.NAME, circuitBreaker.getName()) .tags(customTags) .register(meterRegistry).getId()); Timer successfulCalls = Timer.builder(names.getCallsMetricName()) .description("Total number of successful calls") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(TagNames.KIND, KIND_SUCCESSFUL) .tags(customTags) .register(meterRegistry); Timer failedCalls = Timer.builder(names.getCallsMetricName()) .description("Total number of failed calls") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(TagNames.KIND, KIND_FAILED) .tags(customTags) .register(meterRegistry); Timer ignoredFailedCalls = Timer.builder(names.getCallsMetricName()) .description("Total number of calls which failed but the exception was ignored") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(TagNames.KIND, KIND_IGNORED) .tags(customTags) .register(meterRegistry); Counter notPermittedCalls = Counter.builder(names.getNotPermittedCallsMetricName()) .description("Total number of not permitted calls") .tag(TagNames.NAME, circuitBreaker.getName()) .tag(TagNames.KIND, KIND_NOT_PERMITTED) .tags(customTags) .register(meterRegistry); idSet.add(successfulCalls.getId()); idSet.add(failedCalls.getId()); idSet.add(ignoredFailedCalls.getId()); idSet.add(notPermittedCalls.getId()); circuitBreaker.getEventPublisher() .onIgnoredError(event -> ignoredFailedCalls.record(event.getElapsedDuration())) .onCallNotPermitted(event -> notPermittedCalls.increment()) .onSuccess(event -> successfulCalls.record(event.getElapsedDuration())) .onError(event -> failedCalls.record(event.getElapsedDuration())); meterIdMap.put(circuitBreaker.getName(), idSet); }
Example 11
Source File: CircuitBreakerStateMachineTest.java From resilience4j with Apache License 2.0 | 4 votes |
@Test public void allCircuitBreakerStatesAllowTransitionToItsOwnState() { for (final CircuitBreaker.State state : CircuitBreaker.State.values()) { CircuitBreaker.StateTransition.transitionBetween(circuitBreaker.getName(), state, state); } }
Example 12
Source File: CircuitBreakerStateMachine.java From resilience4j with Apache License 2.0 | 4 votes |
/** * Get the state of the CircuitBreaker */ @Override public CircuitBreaker.State getState() { return CircuitBreaker.State.HALF_OPEN; }
Example 13
Source File: CircuitBreakerStateMachine.java From resilience4j with Apache License 2.0 | 4 votes |
/** * Get the state of the CircuitBreaker */ @Override public CircuitBreaker.State getState() { return CircuitBreaker.State.FORCED_OPEN; }
Example 14
Source File: CircuitBreakerStateMachine.java From resilience4j with Apache License 2.0 | 4 votes |
/** * Get the state of the CircuitBreaker */ @Override public CircuitBreaker.State getState() { return CircuitBreaker.State.METRICS_ONLY; }
Example 15
Source File: CircuitBreakerStateMachine.java From resilience4j with Apache License 2.0 | 4 votes |
/** * Get the state of the CircuitBreaker */ @Override public CircuitBreaker.State getState() { return CircuitBreaker.State.DISABLED; }
Example 16
Source File: CircuitBreakerStateMachine.java From resilience4j with Apache License 2.0 | 4 votes |
/** * Get the state of the CircuitBreaker */ @Override public CircuitBreaker.State getState() { return CircuitBreaker.State.OPEN; }
Example 17
Source File: CircuitBreakerStateMachine.java From resilience4j with Apache License 2.0 | 4 votes |
/** * Get the state of the CircuitBreaker */ @Override public CircuitBreaker.State getState() { return CircuitBreaker.State.CLOSED; }
Example 18
Source File: AbstractCircuitBreakerMetrics.java From resilience4j with Apache License 2.0 | 4 votes |
protected List<MetricFamilySamples> collectGaugeSamples(List<CircuitBreaker> circuitBreakers) { GaugeMetricFamily stateFamily = new GaugeMetricFamily( names.getStateMetricName(), "The state of the circuit breaker:", NAME_AND_STATE ); GaugeMetricFamily bufferedCallsFamily = new GaugeMetricFamily( names.getBufferedCallsMetricName(), "The number of buffered calls", LabelNames.NAME_AND_KIND ); GaugeMetricFamily slowCallsFamily = new GaugeMetricFamily( names.getSlowCallsMetricName(), "The number of slow calls", LabelNames.NAME_AND_KIND ); GaugeMetricFamily failureRateFamily = new GaugeMetricFamily( names.getFailureRateMetricName(), "The failure rate", LabelNames.NAME ); GaugeMetricFamily slowCallRateFamily = new GaugeMetricFamily( names.getSlowCallRateMetricName(), "The slow call rate", LabelNames.NAME ); for (CircuitBreaker circuitBreaker : circuitBreakers) { final CircuitBreaker.State[] states = CircuitBreaker.State.values(); for (CircuitBreaker.State state : states) { stateFamily.addMetric(asList(circuitBreaker.getName(), state.name().toLowerCase()), circuitBreaker.getState() == state ? 1 : 0); } List<String> nameLabel = Collections.singletonList(circuitBreaker.getName()); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); bufferedCallsFamily.addMetric(asList(circuitBreaker.getName(), KIND_SUCCESSFUL), metrics.getNumberOfSuccessfulCalls()); bufferedCallsFamily.addMetric(asList(circuitBreaker.getName(), KIND_FAILED), metrics.getNumberOfFailedCalls()); slowCallsFamily.addMetric(asList(circuitBreaker.getName(), KIND_SUCCESSFUL), metrics.getNumberOfSlowSuccessfulCalls()); slowCallsFamily.addMetric(asList(circuitBreaker.getName(), KIND_FAILED), metrics.getNumberOfSlowFailedCalls()); failureRateFamily.addMetric(nameLabel, metrics.getFailureRate()); slowCallRateFamily.addMetric(nameLabel, metrics.getSlowCallRate()); } return asList(stateFamily, bufferedCallsFamily, slowCallsFamily, failureRateFamily, slowCallRateFamily); }
Example 19
Source File: AbstractCircuitBreakerTest.java From resilience4j-spring-boot2-demo with Apache License 2.0 | 4 votes |
protected void checkHealthStatus(String circuitBreakerName, CircuitBreaker.State state) { CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(circuitBreakerName); assertThat(circuitBreaker.getState()).isEqualTo(state); }
Example 20
Source File: CircuitBreakerStateMachine.java From resilience4j with Apache License 2.0 | votes |
CircuitBreaker.State getState();