Search in sources :

Example 1 with CircuitBreakerConfiguration

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);
}
Also used : Response(javax.ws.rs.core.Response) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) CircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 2 with CircuitBreakerConfiguration

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);
}
Also used : Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) ClientErrorException(javax.ws.rs.ClientErrorException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) CircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration) Before(org.junit.Before)

Example 3 with CircuitBreakerConfiguration

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);
}
Also used : Response(javax.ws.rs.core.Response) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) CircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 4 with CircuitBreakerConfiguration

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;
}
Also used : JaxRsCircuitBreaker(com.oracle.bmc.circuitbreaker.JaxRsCircuitBreaker) JaxRsCircuitBreakerImpl(com.oracle.bmc.circuitbreaker.internal.JaxRsCircuitBreakerImpl) NoCircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.NoCircuitBreakerConfiguration) CircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration)

Example 5 with CircuitBreakerConfiguration

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;
}
Also used : NoCircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.NoCircuitBreakerConfiguration) CircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration)

Aggregations

CircuitBreakerConfiguration (com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration)6 ServiceUnavailableException (javax.ws.rs.ServiceUnavailableException)3 Response (javax.ws.rs.core.Response)3 JaxRsCircuitBreaker (com.oracle.bmc.circuitbreaker.JaxRsCircuitBreaker)2 NoCircuitBreakerConfiguration (com.oracle.bmc.circuitbreaker.NoCircuitBreakerConfiguration)2 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 ProcessingException (javax.ws.rs.ProcessingException)2 Test (org.junit.Test)2 ClientConfiguration (com.oracle.bmc.ClientConfiguration)1 JaxRsCircuitBreakerImpl (com.oracle.bmc.circuitbreaker.internal.JaxRsCircuitBreakerImpl)1 IdentityClient (com.oracle.bmc.identity.IdentityClient)1 ListRegionsResponse (com.oracle.bmc.identity.responses.ListRegionsResponse)1 ObjectStorage (com.oracle.bmc.objectstorage.ObjectStorage)1 ObjectStorageClient (com.oracle.bmc.objectstorage.ObjectStorageClient)1 BucketSummary (com.oracle.bmc.objectstorage.model.BucketSummary)1 ListBucketsRequest (com.oracle.bmc.objectstorage.requests.ListBucketsRequest)1 GetNamespaceResponse (com.oracle.bmc.objectstorage.responses.GetNamespaceResponse)1 ListBucketsResponse (com.oracle.bmc.objectstorage.responses.ListBucketsResponse)1 ClientErrorException (javax.ws.rs.ClientErrorException)1