Search in sources :

Example 1 with CircuitBreaker

use of org.eclipse.microprofile.faulttolerance.CircuitBreaker in project Payara by payara.

the class CircuitBreakerInterceptor method circuitBreak.

private Object circuitBreak(InvocationContext invocationContext) throws Exception {
    Object proceededInvocationContext = null;
    FaultToleranceService faultToleranceService = Globals.getDefaultBaseServiceLocator().getService(FaultToleranceService.class);
    CircuitBreaker circuitBreaker = FaultToleranceCdiUtils.getAnnotation(beanManager, CircuitBreaker.class, invocationContext);
    Config config = null;
    try {
        config = ConfigProvider.getConfig();
    } catch (IllegalArgumentException ex) {
        logger.log(Level.INFO, "No config could be found", ex);
    }
    Class<? extends Throwable>[] failOn = circuitBreaker.failOn();
    try {
        String failOnString = ((String) FaultToleranceCdiUtils.getOverrideValue(config, Retry.class, "failOn", invocationContext, String.class).get());
        List<Class> classList = new ArrayList<>();
        // Remove any curly or square brackets from the string, as well as any spaces and ".class"es and loop
        for (String className : failOnString.replaceAll("[\\{\\[ \\]\\}]", "").replaceAll("\\.class", "").split(",")) {
            // Get a class object
            classList.add(Class.forName(className));
        }
        failOn = classList.toArray(failOn);
    } catch (NoSuchElementException nsee) {
        logger.log(Level.FINER, "Could not find element in config", nsee);
    } catch (ClassNotFoundException cnfe) {
        logger.log(Level.INFO, "Could not find class from failOn config, defaulting to annotation. " + "Make sure you give the full canonical class name.", cnfe);
    }
    long delay = (Long) FaultToleranceCdiUtils.getOverrideValue(config, CircuitBreaker.class, "value", invocationContext, Long.class).orElse(circuitBreaker.delay());
    ChronoUnit delayUnit = (ChronoUnit) FaultToleranceCdiUtils.getOverrideValue(config, CircuitBreaker.class, "delayUnit", invocationContext, ChronoUnit.class).orElse(circuitBreaker.delayUnit());
    int requestVolumeThreshold = (Integer) FaultToleranceCdiUtils.getOverrideValue(config, CircuitBreaker.class, "requestVolumeThreshold", invocationContext, Integer.class).orElse(circuitBreaker.requestVolumeThreshold());
    double failureRatio = (Double) FaultToleranceCdiUtils.getOverrideValue(config, CircuitBreaker.class, "failureRatio", invocationContext, Double.class).orElse(circuitBreaker.failureRatio());
    int successThreshold = (Integer) FaultToleranceCdiUtils.getOverrideValue(config, CircuitBreaker.class, "successThreshold", invocationContext, Integer.class).orElse(circuitBreaker.successThreshold());
    long delayMillis = Duration.of(delay, delayUnit).toMillis();
    InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
    CircuitBreakerState circuitBreakerState = faultToleranceService.getCircuitBreakerState(faultToleranceService.getApplicationName(invocationManager, invocationContext), invocationContext.getMethod(), circuitBreaker);
    switch(circuitBreakerState.getCircuitState()) {
        case OPEN:
            logger.log(Level.FINER, "CircuitBreaker is Open, throwing exception");
            // If open, immediately throw an error
            throw new CircuitBreakerOpenException("CircuitBreaker for method " + invocationContext.getMethod().getName() + "is in state OPEN.");
        case CLOSED:
            // If closed, attempt to proceed the invocation context
            try {
                logger.log(Level.FINER, "Proceeding CircuitBreaker context");
                proceededInvocationContext = invocationContext.proceed();
            } catch (Exception ex) {
                logger.log(Level.FINE, "Exception executing CircuitBreaker context");
                // Check if the exception is something that should record a failure
                if (shouldFail(failOn, ex)) {
                    logger.log(Level.FINE, "Caught exception is included in CircuitBreaker failOn, " + "recording failure against CircuitBreaker");
                    // Add a failure result to the queue
                    circuitBreakerState.recordClosedResult(Boolean.FALSE);
                    // Calculate the failure threshold
                    long failureThreshold = Math.round(requestVolumeThreshold * failureRatio);
                    // If we're over the failure threshold, open the circuit
                    if (circuitBreakerState.isOverFailureThreshold(failureThreshold)) {
                        logger.log(Level.FINE, "CircuitBreaker is over failure threshold {0}, opening circuit", failureThreshold);
                        circuitBreakerState.setCircuitState(CircuitBreakerState.CircuitState.OPEN);
                        // Kick off a thread that will half-open the circuit after the specified delay
                        scheduleHalfOpen(delayMillis, circuitBreakerState);
                    }
                }
                // Finally, propagate the error upwards
                throw ex;
            }
            // If everything is bon, just add a success value
            circuitBreakerState.recordClosedResult(Boolean.TRUE);
            break;
        case HALF_OPEN:
            // If half-open, attempt to proceed the invocation context
            try {
                logger.log(Level.FINER, "Proceeding half open CircuitBreaker context");
                proceededInvocationContext = invocationContext.proceed();
            } catch (Exception ex) {
                logger.log(Level.FINE, "Exception executing CircuitBreaker context");
                // Check if the exception is something that should record a failure
                if (shouldFail(failOn, ex)) {
                    logger.log(Level.FINE, "Caught exception is included in CircuitBreaker failOn, " + "reopening half open circuit");
                    // Open the circuit again, and reset the half-open result counter
                    circuitBreakerState.setCircuitState(CircuitBreakerState.CircuitState.OPEN);
                    circuitBreakerState.resetHalfOpenSuccessfulResultCounter();
                    scheduleHalfOpen(delayMillis, circuitBreakerState);
                }
                throw ex;
            }
            // If the invocation context hasn't thrown an error, record a success
            circuitBreakerState.incrementHalfOpenSuccessfulResultCounter();
            logger.log(Level.FINER, "Number of consecutive successful circuitbreaker executions = {0}", circuitBreakerState.getHalfOpenSuccessFulResultCounter());
            // If we've hit the success threshold, close the circuit
            if (circuitBreakerState.getHalfOpenSuccessFulResultCounter() == successThreshold) {
                logger.log(Level.FINE, "Number of consecutive successful CircuitBreaker executions is above " + "threshold {0}, closing circuit", successThreshold);
                circuitBreakerState.setCircuitState(CircuitBreakerState.CircuitState.CLOSED);
                // Reset the counter for when we next need to use it
                circuitBreakerState.resetHalfOpenSuccessfulResultCounter();
                // Reset the rolling results window
                circuitBreakerState.resetResults();
            }
            break;
    }
    return proceededInvocationContext;
}
Also used : CircuitBreaker(org.eclipse.microprofile.faulttolerance.CircuitBreaker) Config(org.eclipse.microprofile.config.Config) CircuitBreakerState(fish.payara.microprofile.faulttolerance.state.CircuitBreakerState) ArrayList(java.util.ArrayList) InvocationManager(org.glassfish.api.invocation.InvocationManager) CircuitBreakerOpenException(org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException) FaultToleranceService(fish.payara.microprofile.faulttolerance.FaultToleranceService) NamingException(javax.naming.NamingException) NoSuchElementException(java.util.NoSuchElementException) CircuitBreakerOpenException(org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException) Retry(org.eclipse.microprofile.faulttolerance.Retry) NoSuchElementException(java.util.NoSuchElementException) ChronoUnit(java.time.temporal.ChronoUnit)

Example 2 with CircuitBreaker

use of org.eclipse.microprofile.faulttolerance.CircuitBreaker in project Payara by payara.

the class FaultToleranceStressTest method occasionallyFailingService_Method.

/**
 * The method under tests fails every 3rd call whereby in a window of 4 there can be 2 failed calls opening the
 * circuit. As delay is just recorded but not enforced there is only a minimal chance another caller does an attempt
 * while the circuit is open but occasionally this happens.
 *
 * Every 5th call returns a failed Future that should be handed to the caller as is.
 *
 * Every 3rd call fails causing a retry. Together with open circuits this might even cause fallback handler to be
 * used which will also fail the result as it only rethrows the error.
 */
@Asynchronous
@Fallback(FaultToleranceStressTest.class)
@Retry(maxRetries = 1)
@Bulkhead(value = 2, waitingTaskQueue = 2)
@CircuitBreaker(successThreshold = 2, delay = 0, requestVolumeThreshold = 4)
public Future<String> occasionallyFailingService_Method() throws IOException {
    int called = methodInvocationCount.incrementAndGet();
    if (called % 3 == 0) {
        // this causes a retry
        methodFailedInvocationCount.incrementAndGet();
        throw new IOException("Failed");
    }
    if (called % 5 == 0) {
        // this does not cause a retry, its simply a Future that completes with a failure
        CompletableFuture<String> failedValue = new CompletableFuture<>();
        failedValue.completeExceptionally(new IOException("Failed"));
        return failedValue;
    }
    methodSuccessfulInvocationCount.incrementAndGet();
    return CompletableFuture.completedFuture("Success");
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Asynchronous(org.eclipse.microprofile.faulttolerance.Asynchronous) CircuitBreaker(org.eclipse.microprofile.faulttolerance.CircuitBreaker) Bulkhead(org.eclipse.microprofile.faulttolerance.Bulkhead) Fallback(org.eclipse.microprofile.faulttolerance.Fallback) Retry(org.eclipse.microprofile.faulttolerance.Retry)

Aggregations

CircuitBreaker (org.eclipse.microprofile.faulttolerance.CircuitBreaker)2 Retry (org.eclipse.microprofile.faulttolerance.Retry)2 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)1 CircuitBreakerState (fish.payara.microprofile.faulttolerance.state.CircuitBreakerState)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 NamingException (javax.naming.NamingException)1 Config (org.eclipse.microprofile.config.Config)1 Asynchronous (org.eclipse.microprofile.faulttolerance.Asynchronous)1 Bulkhead (org.eclipse.microprofile.faulttolerance.Bulkhead)1 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)1 CircuitBreakerOpenException (org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException)1 InvocationManager (org.glassfish.api.invocation.InvocationManager)1