Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fault Tolerance 3.0 Support #2680

Merged
merged 13 commits into from
Jan 22, 2021
2 changes: 1 addition & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<version.lib.micronaut.data>2.2.0</version.lib.micronaut.data>
<version.lib.micronaut.sql>3.3.1</version.lib.micronaut.sql>
<version.lib.microprofile-config>1.4</version.lib.microprofile-config>
<version.lib.microprofile-fault-tolerance-api>2.1.1</version.lib.microprofile-fault-tolerance-api>
<version.lib.microprofile-fault-tolerance-api>3.0</version.lib.microprofile-fault-tolerance-api>
<version.lib.microprofile-graphql>1.0.3</version.lib.microprofile-graphql>
<version.lib.microprofile-health>2.2</version.lib.microprofile-health>
<version.lib.microprofile-jwt>1.1.1</version.lib.microprofile-jwt>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,6 @@
import java.util.concurrent.Flow;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
Expand Down Expand Up @@ -78,9 +77,10 @@ private <T> Single<T> retrySingle(RetryContext<? extends CompletionStage<T>> con

long nanos = System.nanoTime() - context.startedNanos;
if (nanos > maxTimeNanos) {
return Single.error(new TimeoutException("Execution took too long. Already executing: "
+ TimeUnit.NANOSECONDS.toMillis(nanos) + " ms, must timeout after: "
+ TimeUnit.NANOSECONDS.toMillis(maxTimeNanos) + " ms."));
return Single.error(new RetryTimeoutException(context.throwable(),
"Execution took too long. Already executing: "
+ TimeUnit.NANOSECONDS.toMillis(nanos) + " ms, must timeout after: "
+ TimeUnit.NANOSECONDS.toMillis(maxTimeNanos) + " ms."));
}

if (currentCallIndex > 0) {
Expand Down Expand Up @@ -120,9 +120,10 @@ private <T> Multi<T> retryMulti(RetryContext<? extends Flow.Publisher<T>> contex

long nanos = System.nanoTime() - context.startedNanos;
if (nanos > maxTimeNanos) {
return Multi.error(new TimeoutException("Execution took too long. Already executing: "
+ TimeUnit.NANOSECONDS.toMillis(nanos) + " ms, must timeout after: "
+ TimeUnit.NANOSECONDS.toMillis(maxTimeNanos) + " ms."));
return Multi.error(new RetryTimeoutException(context.throwable(),
"Execution took too long. Already executing: "
+ TimeUnit.NANOSECONDS.toMillis(nanos) + " ms, must timeout after: "
+ TimeUnit.NANOSECONDS.toMillis(maxTimeNanos) + " ms."));
}

if (currentCallIndex > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.faulttolerance;

import java.util.concurrent.TimeoutException;

/**
* Subclass of {@link TimeoutException} to discern exceptions thrown by a {@link Retry}
* when its overall timeout is reached versus those thrown by a {@link Timeout}.
*/
public class RetryTimeoutException extends TimeoutException {
private static final long serialVersionUID = 1900926677490550714L;

private final Throwable lastRetryException;

/**
* Constructs a {@code RetryTimeoutException} with the specified detail
* message.
*
* @param throwable last retry exception
* @param message the detail message
*/
public RetryTimeoutException(Throwable throwable, String message) {
super(message);
lastRetryException = throwable;
}

/**
* Last exception thrown in {@code Retry} before the overall timeout reached.
*
* @return last exception thrown
*/
public Throwable lastRetryException() {
return lastRetryException;
}
}

34 changes: 34 additions & 0 deletions microprofile/fault-tolerance/etc/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2021 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

<FindBugsFilter
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://github.com/spotbugs/filter/3.0.0"
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">

<Match>
<!--
- Incorrectly reported on just one enum in FaultToleranceMetrics.java
- https://github.com/spotbugs/spotbugs/issues/740
-->
<Field type="org.eclipse.microprofile.metrics.Tag" />
<Bug pattern="SE_BAD_FIELD" />
</Match>

</FindBugsFilter>
6 changes: 5 additions & 1 deletion microprofile/fault-tolerance/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2018, 2020 Oracle and/or its affiliates.
Copyright (c) 2018, 2021 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,10 @@
Microprofile fault tolerance implementation
</description>

<properties>
<spotbugs.exclude>etc/spotbugs/exclude.xml</spotbugs.exclude>
</properties>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,9 +27,9 @@
import org.eclipse.microprofile.faulttolerance.FallbackHandler;

/**
* Class CommandFallback.
* Implements invocation callback logic.
*/
class CommandFallback {
class FallbackHelper {

private final InvocationContext context;

Expand All @@ -46,7 +46,7 @@ class CommandFallback {
* @param introspector Method introspector.
* @param throwable Throwable that caused execution of fallback
*/
CommandFallback(InvocationContext context, MethodIntrospector introspector, Throwable throwable) {
FallbackHelper(InvocationContext context, MethodIntrospector introspector, Throwable throwable) {
this.context = context;
this.throwable = throwable;

Expand Down Expand Up @@ -104,24 +104,13 @@ public Throwable getFailure() {
result = fallbackMethod.invoke(context.getTarget(), context.getParameters());
}
} catch (Throwable t) {
updateMetrics();

// If InvocationTargetException, then unwrap underlying cause
if (t instanceof InvocationTargetException) {
t = t.getCause();
}
throw t instanceof Exception ? (Exception) t : new RuntimeException(t);
}

updateMetrics();
return result;
}

/**
* Updates fallback metrics.
*/
private void updateMetrics() {
Method method = context.getMethod();
FaultToleranceMetrics.getCounter(method, FaultToleranceMetrics.FALLBACK_CALLS_TOTAL).inc();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -229,38 +229,29 @@ private void registerFaultToleranceMethods(AnnotatedType<?> type) {
}

/**
* Registers metrics for all FT methods and init executors.
* Validates annotations.
*
* @param validation Event information.
*/
void registerMetricsAndInitExecutors(@Observes AfterDeploymentValidation validation) {
void validateAnnotations(@Observes AfterDeploymentValidation validation) {
if (FaultToleranceMetrics.enabled()) {
getRegisteredMethods().stream().forEach(beanMethod -> {
final Method method = beanMethod.method();
final Class<?> beanClass = beanMethod.beanClass();

// Counters for all methods
FaultToleranceMetrics.registerMetrics(method);

// Metrics depending on the annotationSet present
if (MethodAntn.isAnnotationPresent(beanClass, method, Retry.class)) {
FaultToleranceMetrics.registerRetryMetrics(method);
new RetryAntn(beanClass, method).validate();
}
if (MethodAntn.isAnnotationPresent(beanClass, method, CircuitBreaker.class)) {
FaultToleranceMetrics.registerCircuitBreakerMetrics(method);
new CircuitBreakerAntn(beanClass, method).validate();
}
if (MethodAntn.isAnnotationPresent(beanClass, method, Timeout.class)) {
FaultToleranceMetrics.registerTimeoutMetrics(method);
new TimeoutAntn(beanClass, method).validate();
}
if (MethodAntn.isAnnotationPresent(beanClass, method, Bulkhead.class)) {
FaultToleranceMetrics.registerBulkheadMetrics(method);
new BulkheadAntn(beanClass, method).validate();
}
if (MethodAntn.isAnnotationPresent(beanClass, method, Fallback.class)) {
FaultToleranceMetrics.registerFallbackMetrics(method);
new FallbackAntn(beanClass, method).validate();
}
if (MethodAntn.isAnnotationPresent(beanClass, method, Asynchronous.class)) {
Expand All @@ -269,17 +260,17 @@ void registerMetricsAndInitExecutors(@Observes AfterDeploymentValidation validat
});
}

// Initialize executors for MP FT - default size of 16
// Initialize executors for MP FT - default size of 20
io.helidon.config.Config config = io.helidon.config.Config.create();
scheduledThreadPoolSupplier = ScheduledThreadPoolSupplier.builder()
.threadNamePrefix("ft-mp-schedule-")
.corePoolSize(16)
.corePoolSize(20)
.config(config.get("scheduled-executor"))
.build();
FaultTolerance.scheduledExecutor(scheduledThreadPoolSupplier);
threadPoolSupplier = ThreadPoolSupplier.builder()
.threadNamePrefix("ft-mp-")
.corePoolSize(16)
.corePoolSize(20)
.config(config.get("executor"))
.build();
FaultTolerance.executor(threadPoolSupplier);
Expand Down
Loading