Search in sources :

Example 1 with FaultToleranceMetrics

use of fish.payara.microprofile.faulttolerance.FaultToleranceMetrics in project Payara by payara.

the class BulkheadLifecycleTckTest method createService.

@Override
protected FaultToleranceServiceStub createService() {
    // this test needs to use more advanced state per method as multiple methods are involved
    // therefore the below special setup where we have state per method as in the actual implementation
    final Map<Object, AtomicReference<BlockingQueue<Thread>>> concurrentExecutionByMethodId = new ConcurrentHashMap<>();
    final Map<Object, AtomicInteger> waitingQueuePopulationByMethodId = new ConcurrentHashMap<>();
    registry = new MetricRegistryImpl(Type.BASE);
    return new FaultToleranceServiceStub() {

        @Override
        protected FaultToleranceMethodContext stubMethodContext(StubContext ctx) {
            FaultToleranceMetrics metrics = new MethodFaultToleranceMetrics(registry, FaultToleranceUtils.getCanonicalMethodName(ctx.context));
            return new FaultToleranceMethodContextStub(ctx, state, concurrentExecutionByMethodId.computeIfAbsent(ctx.key, key -> new AtomicReference<>()), waitingQueuePopulationByMethodId.computeIfAbsent(ctx.key, key -> new AtomicInteger())) {

                @Override
                public FaultToleranceMetrics getMetrics() {
                    return metrics;
                }

                @Override
                public Future<?> runDelayed(long delayMillis, Runnable task) throws Exception {
                    return CompletableFuture.completedFuture(null);
                }
            };
        }
    };
}
Also used : Awaitility.await(org.awaitility.Awaitility.await) FaultToleranceMethodContext(fish.payara.microprofile.faulttolerance.FaultToleranceMethodContext) FaultToleranceServiceStub(fish.payara.microprofile.faulttolerance.service.FaultToleranceServiceStub) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MetricRegistryImpl(fish.payara.microprofile.metrics.impl.MetricRegistryImpl) BlockingQueue(java.util.concurrent.BlockingQueue) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) FaultToleranceMethodContextStub(fish.payara.microprofile.faulttolerance.service.FaultToleranceMethodContextStub) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Bulkhead(org.eclipse.microprofile.faulttolerance.Bulkhead) Type(org.eclipse.microprofile.metrics.MetricRegistry.Type) Future(java.util.concurrent.Future) FaultToleranceUtils(fish.payara.microprofile.faulttolerance.service.FaultToleranceUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodFaultToleranceMetrics(fish.payara.microprofile.faulttolerance.service.MethodFaultToleranceMetrics) Map(java.util.Map) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Method(java.lang.reflect.Method) FaultToleranceMetrics(fish.payara.microprofile.faulttolerance.FaultToleranceMetrics) Assert.assertEquals(org.junit.Assert.assertEquals) FaultToleranceServiceStub(fish.payara.microprofile.faulttolerance.service.FaultToleranceServiceStub) MethodFaultToleranceMetrics(fish.payara.microprofile.faulttolerance.service.MethodFaultToleranceMetrics) FaultToleranceMetrics(fish.payara.microprofile.faulttolerance.FaultToleranceMetrics) MetricRegistryImpl(fish.payara.microprofile.metrics.impl.MetricRegistryImpl) MethodFaultToleranceMetrics(fish.payara.microprofile.faulttolerance.service.MethodFaultToleranceMetrics) AtomicReference(java.util.concurrent.atomic.AtomicReference) FaultToleranceMethodContextStub(fish.payara.microprofile.faulttolerance.service.FaultToleranceMethodContextStub) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with FaultToleranceMetrics

use of fish.payara.microprofile.faulttolerance.FaultToleranceMetrics in project Payara by payara.

the class FaultTolerancePolicy method proceed.

/**
 * Wraps {@link InvocationContext#proceed()} with fault tolerance behaviour.
 *
 * Processing has 6 stages:
 * <pre>
 * 1) Asynchronous
 * 2) Fallback
 * 3) Retry
 * 4) Circuit Breaker
 * 5) Timeout
 * 6) Bulkhead
 * </pre>
 * The call chain goes from 1) down to 6) skipping stages that are not requested by this policy.
 *
 * Asynchronous execution branches to new threads in stage 1) and 3) each executed by the
 * {@link FaultToleranceService#runAsynchronous(CompletableFuture, Callable)}.
 *
 * @param context intercepted call context
 * @param ftmContextSupplier the environment used to execute the FT behaviour
 * @return the result of {@link InvocationContext#proceed()} after applying FT behaviour
 * @throws Exception as thrown by the wrapped invocation or a {@link FaultToleranceException}
 */
public Object proceed(InvocationContext context, Supplier<FaultToleranceMethodContext> ftmContextSupplier) throws Exception {
    if (!isPresent) {
        logger.log(Level.FINER, "Fault Tolerance not enabled, proceeding normally.");
        return context.proceed();
    }
    FaultToleranceMethodContext ftmContext = ftmContextSupplier.get();
    FaultToleranceMetrics metrics = ftmContext.getMetrics().boundTo(ftmContext, this);
    try {
        Object res = processAsynchronousStage(ftmContext, metrics);
        if (res instanceof AsyncFuture) {
            AsyncFuture async = (AsyncFuture) res;
            async.whenComplete((value, ex) -> {
                // first evaluate async when the results are in...
                if (isExceptionThrown(async)) {
                    metrics.incrementInvocationsExceptionThrown();
                } else {
                    metrics.incrementInvocationsValueReturned();
                }
            });
        } else {
            metrics.incrementInvocationsValueReturned();
        }
        return res;
    } catch (Exception | Error ex) {
        metrics.incrementInvocationsExceptionThrown();
        throw ex;
    }
}
Also used : FaultToleranceMetrics(fish.payara.microprofile.faulttolerance.FaultToleranceMetrics) AsyncFuture(fish.payara.microprofile.faulttolerance.FaultToleranceMethodContext.AsyncFuture) FaultToleranceMethodContext(fish.payara.microprofile.faulttolerance.FaultToleranceMethodContext) FaultToleranceException(org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceException) TimeoutException(org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException) CircuitBreakerOpenException(org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException) ExecutionException(java.util.concurrent.ExecutionException) BulkheadException(org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException) FaultToleranceDefinitionException(org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException)

Aggregations

FaultToleranceMethodContext (fish.payara.microprofile.faulttolerance.FaultToleranceMethodContext)2 FaultToleranceMetrics (fish.payara.microprofile.faulttolerance.FaultToleranceMetrics)2 ExecutionException (java.util.concurrent.ExecutionException)2 AsyncFuture (fish.payara.microprofile.faulttolerance.FaultToleranceMethodContext.AsyncFuture)1 FaultToleranceMethodContextStub (fish.payara.microprofile.faulttolerance.service.FaultToleranceMethodContextStub)1 FaultToleranceServiceStub (fish.payara.microprofile.faulttolerance.service.FaultToleranceServiceStub)1 FaultToleranceUtils (fish.payara.microprofile.faulttolerance.service.FaultToleranceUtils)1 MethodFaultToleranceMetrics (fish.payara.microprofile.faulttolerance.service.MethodFaultToleranceMetrics)1 MetricRegistryImpl (fish.payara.microprofile.metrics.impl.MetricRegistryImpl)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Awaitility.await (org.awaitility.Awaitility.await)1 Bulkhead (org.eclipse.microprofile.faulttolerance.Bulkhead)1