use of com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration in project oci-java-sdk by oracle.
the class CircuitBreakerConfigurationTest method validateCircuitBreakerConfigurationWithDefaultValues.
@Test
public void validateCircuitBreakerConfigurationWithDefaultValues() {
CircuitBreakerConfiguration config = CircuitBreakerConfiguration.builder().build();
JaxRsCircuitBreakerImpl circuitBreaker = new JaxRsCircuitBreakerImpl(config);
CircuitBreakerConfig internalConfig = circuitBreaker.getInternalCircuitBreakerConfig();
assertEquals(internalConfig.getFailureRateThreshold(), CircuitBreakerConfiguration.DEFAULT_FAILURE_RATE_THRESHOLD, 0.1);
assertEquals(internalConfig.getSlowCallRateThreshold(), CircuitBreakerConfiguration.DEFAULT_SLOW_CALL_RATE_THRESHOLD, 0.1);
assertEquals(internalConfig.getSlowCallDurationThreshold(), Duration.ofMinutes(CircuitBreakerConfiguration.DEFAULT_SLOW_CALL_DURATION_THRESHOLD));
assertEquals(internalConfig.getPermittedNumberOfCallsInHalfOpenState(), CircuitBreakerConfiguration.DEFAULT_PERMITTED_CALLS_IN_HALF_OPEN_STATE);
assertEquals(internalConfig.getSlidingWindowType(), CircuitBreakerConfig.SlidingWindowType.TIME_BASED);
assertEquals(internalConfig.getSlidingWindowSize(), CircuitBreakerConfiguration.DEFAULT_SLIDING_WINDOW_SIZE);
assertEquals(internalConfig.getMinimumNumberOfCalls(), CircuitBreakerConfiguration.DEFAULT_MINIMUM_NUMBER_OF_CALLS);
assertEquals(internalConfig.getWaitDurationInOpenState(), Duration.ofSeconds(CircuitBreakerConfiguration.DEFAULT_WAIT_DURATION_IN_OPEN_STATE));
assertTrue(internalConfig.isAutomaticTransitionFromOpenToHalfOpenEnabled());
Response response503 = mock(Response.class);
Mockito.when(response503.getStatus()).thenReturn(503);
HttpStatusErrorException ex = new HttpStatusErrorException(response503);
Predicate<Throwable> recordExceptionPredicate = internalConfig.getRecordExceptionPredicate();
assertTrue(recordExceptionPredicate.test(ex));
assertTrue(recordExceptionPredicate.test(new ServiceUnavailableException()));
assertTrue(recordExceptionPredicate.test(new InternalServerErrorException()));
assertTrue(recordExceptionPredicate.test(new ProcessingException("test")));
Set<Integer> recordHttpStatuses = circuitBreaker.getRecordHttpStatuses();
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.INTERNAL_SERVER_ERROR));
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.SERVICE_UNAVAILABLE));
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.TOO_MANY_REQUESTS));
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.BAD_GATEWAY));
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.GATEWAY_TIMEOUT));
assertEquals(recordHttpStatuses.size(), 5);
}
use of com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration in project oci-java-sdk by oracle.
the class JaxRsCircuitBreakerImplTest method setup.
@Before
public void setup() throws ExecutionException, InterruptedException {
CircuitBreakerConfiguration config = CircuitBreakerConfiguration.builder().failureRateThreshold(50).permittedNumberOfCallsInHalfOpenState(4).slidingWindowSize(SLIDING_WINDOW_SIZE).minimumNumberOfCalls(MIN_NUM_CALLS).waitDurationInOpenState(Duration.ofSeconds(2)).build();
circuitBreaker = new JaxRsCircuitBreakerImpl(config);
Response response503 = mock(Response.class);
Mockito.when(response503.getStatus()).thenReturn(503);
Mockito.when(response503.getStatusInfo()).thenReturn(Response.Status.BAD_GATEWAY);
invocation503 = mock(Invocation.class);
Mockito.when(invocation503.invoke()).thenReturn(response503);
Mockito.when(responseFuture503.get()).thenReturn(response503);
Response response200 = mock(Response.class);
Mockito.when(response200.getStatus()).thenReturn(200);
Mockito.when(response200.getStatusInfo()).thenReturn(Response.Status.OK);
invocation200 = mock(Invocation.class);
Mockito.when(invocation200.invoke()).thenReturn(response200);
Mockito.when(responseFuture200.get()).thenReturn(response200);
invocationUnavailable = mock(Invocation.class);
Mockito.when(invocationUnavailable.invoke()).thenThrow(new ServiceUnavailableException());
invocationClientError = mock(Invocation.class);
Mockito.when(invocationClientError.invoke()).thenThrow(new ClientErrorException(Response.Status.NOT_ACCEPTABLE));
Mockito.when(responseFuture200.get()).thenReturn(response200);
Mockito.when(responseFuture503.get()).thenReturn(response503);
Mockito.when(responseFutureUnavailable.get()).thenThrow(new ServiceUnavailableException());
Response response409 = mock(Response.class);
Mockito.when(response409.getStatus()).thenReturn(409);
Mockito.when(response409.getStatusInfo()).thenReturn(buildIncorrectStateResponse());
invocation409 = mock(Invocation.class);
Mockito.when(invocation409.invoke()).thenReturn(response409);
Mockito.when(responseFuture409.get()).thenReturn(response409);
}
use of com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration in project oci-java-sdk by oracle.
the class CircuitBreakerConfigurationTest method validateCircuitBreakerConfigurationWithCustomValues.
@Test
public void validateCircuitBreakerConfigurationWithCustomValues() {
CircuitBreakerConfiguration config = CircuitBreakerConfiguration.builder().failureRateThreshold(50).slowCallRateThreshold(90).slowCallDurationThreshold(Duration.ofSeconds(6)).permittedNumberOfCallsInHalfOpenState(2).slidingWindowSize(10).minimumNumberOfCalls(4).waitDurationInOpenState(Duration.ofSeconds(2)).recordHttpStatuses(ImmutableSet.of(CircuitBreakerConfiguration.TOO_MANY_REQUESTS, CircuitBreakerConfiguration.SERVICE_UNAVAILABLE)).recordExceptions(ImmutableList.of(CircuitBreakerConfiguration.SERVICE_UNAVAILABLE_EXCEPTION_CLASS)).build();
JaxRsCircuitBreakerImpl circuitBreaker = new JaxRsCircuitBreakerImpl(config);
CircuitBreakerConfig internalConfig = circuitBreaker.getInternalCircuitBreakerConfig();
assertEquals(internalConfig.getFailureRateThreshold(), 50, 0.1);
assertEquals(internalConfig.getSlowCallRateThreshold(), 90, 0.1);
assertEquals(internalConfig.getSlowCallDurationThreshold(), Duration.ofSeconds(6));
assertEquals(internalConfig.getPermittedNumberOfCallsInHalfOpenState(), 2);
assertEquals(internalConfig.getSlidingWindowType(), CircuitBreakerConfig.SlidingWindowType.TIME_BASED);
assertEquals(internalConfig.getSlidingWindowSize(), 10);
assertEquals(internalConfig.getMinimumNumberOfCalls(), 4);
assertEquals(internalConfig.getWaitDurationInOpenState(), Duration.ofSeconds(2));
assertTrue(internalConfig.isAutomaticTransitionFromOpenToHalfOpenEnabled());
Response response503 = mock(Response.class);
Mockito.when(response503.getStatus()).thenReturn(503);
HttpStatusErrorException ex = new HttpStatusErrorException(response503);
Predicate<Throwable> recordExceptionPredicate = internalConfig.getRecordExceptionPredicate();
assertTrue(recordExceptionPredicate.test(ex));
assertTrue(recordExceptionPredicate.test(new ServiceUnavailableException()));
assertFalse(recordExceptionPredicate.test(new InternalServerErrorException()));
assertFalse(recordExceptionPredicate.test(new ProcessingException("test")));
Set<Integer> recordHttpStatuses = circuitBreaker.getRecordHttpStatuses();
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.SERVICE_UNAVAILABLE));
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.TOO_MANY_REQUESTS));
assertEquals(recordHttpStatuses.size(), 2);
}
use of com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration in project oci-java-sdk by oracle.
the class CircuitBreakerUtils method getUserDefinedCircuitBreaker.
/**
* Gets the user defined CircuitBreaker
* @return the user defined CircuitBreaker
*/
@Deprecated
public static JaxRsCircuitBreaker getUserDefinedCircuitBreaker(ClientConfiguration configuration) {
JaxRsCircuitBreaker circuitBreaker = null;
if (configuration != null) {
if (configuration.getCircuitBreakerConfiguration() != null && configuration.getCircuitBreaker() != null) {
throw new IllegalArgumentException("Invalid CircuitBreaker setting. Please provide either CircuitBreaker configuration or CircuitBreaker and not both");
}
if (configuration.getCircuitBreakerConfiguration() != null) {
circuitBreaker = new JaxRsCircuitBreakerImpl(configuration.getCircuitBreakerConfiguration());
} else if (configuration.getCircuitBreaker() != null)
circuitBreaker = configuration.getCircuitBreaker();
} else {
JaxRsCircuitBreaker userGlobalCircuitBreaker = null;
CircuitBreakerConfiguration globalCircuitBreakerConfiguration = CircuitBreakerUtils.getDefaultCircuitBreakerConfiguration();
if (globalCircuitBreakerConfiguration != null) {
userGlobalCircuitBreaker = new JaxRsCircuitBreakerImpl(globalCircuitBreakerConfiguration);
} else if (isEnvBasedDefaultCircuitBreakerEnabled()) {
userGlobalCircuitBreaker = DEFAULT_CIRCUIT_BREAKER;
}
circuitBreaker = userGlobalCircuitBreaker;
}
LOG.debug("Circuit breaker in use: {}", circuitBreaker);
return circuitBreaker;
}
use of com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration in project oci-java-sdk by oracle.
the class CircuitBreakerUtils method getUserDefinedCircuitBreakerConfiguration.
/**
* Gets the user defined CircuitBreakerConfiguration
* @return the user defined CircuitBreakerConfiguration
*/
public static CircuitBreakerConfiguration getUserDefinedCircuitBreakerConfiguration(ClientConfiguration configuration) {
CircuitBreakerConfiguration circuitBreakerConfiguration = null;
if (configuration != null) {
if (configuration.getCircuitBreakerConfiguration() != null && configuration.getCircuitBreaker() != null) {
throw new IllegalArgumentException("Invalid CircuitBreaker setting. Please provide either CircuitBreaker configuration or CircuitBreaker and not both");
}
if (configuration.getCircuitBreakerConfiguration() != null) {
circuitBreakerConfiguration = configuration.getCircuitBreakerConfiguration();
}
} else if (defaultCircuitBreakerConfiguration != null) {
circuitBreakerConfiguration = defaultCircuitBreakerConfiguration;
} else if (isEnvBasedDefaultCircuitBreakerEnabled()) {
circuitBreakerConfiguration = DEFAULT_CIRCUIT_BREAKER_CONFIGURATION;
} else if (!isEnvBasedDefaultCircuitBreakerEnabled()) {
circuitBreakerConfiguration = getNoCircuitBreakerConfiguration();
}
LOG.debug("Circuit breaker configuration in use: {}", circuitBreakerConfiguration);
return circuitBreakerConfiguration;
}
Aggregations