All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mp.fault-tolerance.adoc Maven / Gradle / Ivy

There is a newer version: 4.1.1
Show newest version
///////////////////////////////////////////////////////////////////////////////

    Copyright (c) 2021, 2024 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.

///////////////////////////////////////////////////////////////////////////////

= Fault Tolerance Introduction
:description: Fault Tolerance
:keywords: helidon, webserver, fault, tolerance, faulttolerance, mp
:feature-name: MicroProfile Fault Tolerance
:microprofile-bundle: true
:rootdir: {docdir}/..

include::{rootdir}/includes/mp.adoc[]

== Contents

- <>
- <>
- <>
- <>
- <>
- <>
- <>

== Overview

Fault Tolerance is part of the MicroProfile set of specifications. This API defines mostly
annotations that improve application robustness by providing support to conveniently handle
error conditions (faults) that may occur in real-world applications. Examples include
service restarts, network delays, temporal infrastructure instabilities, etc.

include::{rootdir}/includes/dependencies.adoc[]

[source,xml]
----
 
     io.helidon.microprofile
     helidon-microprofile-fault-tolerance
 
----

== API

The MicroProfile Fault Tolerance specification defines a set of annotations to decorate
classes and methods in your application for the purpose of improving its robustness.
Many of these annotations can be applied at the class or method level: if applied at the
class level, they will impact all methods in the class; if applied both at the
class and method level, the latter will take precedence over the former.

The following table provides a brief description of each of these annotations, including
its parameters and default values.

[cols="5,5", role="flex, sm10"]
|===
|Annotation |Description

a|
----
@Retry(
    maxRetries=3,
    delay=0,
    delayUnit=ChronoUnit.MILLIS,
    maxDuration=180000,
    durationUnit=ChronoUnit.MILLIS,
    jitter=200,
    jitterDelayUnit=ChronoUnit.MILLIS,
    retryOn={Exception.class},
    abortOn={}
)
----

|Retries the execution of a method if a failure is encountered. Annotation attributes can be
used to control the number of retries, delay between retries and which exceptions to retry
or abort on.

a|
----
@Timeout(
    value=1000,
    unit=ChronoUnit.MILLIS
)
----
|Defines an upper bound on a method's execution time. Default value is 1 second.

a|
----
@CircuitBreaker(
    failOn={Throwable.class},
    skipOn={},
    delay=5000,
    delayUnit=ChronoUnit.MILLIS,
    requestVolumeThreshold=20,
    failureRation=.50,
    successThreshold=1
)
----

|Defines a policy to avoid repeated execution of logic that is likely to fail. A circuit
breaker can be _closed_, _open_ or _half-open_. In _closed_ state a circuit breaker will
execute logic normally. In _open_ state a circuit breaker will
prevent execution of logic that has been seen to fail. Finally, in _half-open_ state
a circuit breaker will allow _trial_ executions in an attempt to switch its internal
state to _closed_. The other annotation parameters are used to control how these
state transitions are triggered.

a|
----
@Bulkhead(
    value=10,
    waitingTaskQueue=10
)
----
|Defines a policy to limit the number of concurrent executions allowed over some
application logic. A queue is used to park tasks awaiting execution after
the limit has been reached. A queue is only active when invocations are
`@Asynchronous`.

a|
----
@Fallback(
    value=DEFAULT.class,
    fallbackMethod="",
    applyOn={Throwable.class},
    skipOn={}
)
----
|Establishes a handler to be executed upon encountering an invocation failure. A handler
is either a class that implements `FallbackHandler` or just a simple method in the
same class. Additional properties are used to control the conditions under which
these handlers are called.

a|
----
@Asynchronous
----
|Executes an invocation asynchronously without blocking the calling thread. Annotated
method must return `Future` or `CompletionStage`. Typically used to avoid blocking
the calling thread on I/O or on a long-running computation.

|===

== Configuration

Helidon's implementation uses two types of thread pools: normal and scheduled. The default
core size of these executors is 20; however, that can be configured using an `application.yaml`
file as follows:

[source,yaml]
----
executor:
  core-pool-size: 32

scheduled-executor:
  core-pool-size: 32
----

NOTE: There is currently _no support_ to configure these executor properties via a
`microprofile-config.properties` file.

For a complete set of properties available to configure these executors, see
link:{configurable-javadoc-base-url}/io/helidon/common/configurable/ThreadPoolConfig.html[ThreadPoolConfig].
link:{configurable-javadoc-base-url}/io/helidon/common/configurable/ScheduledThreadPoolConfig.html[ScheduledThreadPoolConfig].

== Examples

1. The method  `retryWithFallback` shall be called at most 3 times, first
call plus 2 retries, with a delay of 400 milliseconds between calls. If none of the
calls is successful, the `onFailure` method shall be called as a fallback mechanism.
+
[source,java]
----
include::{sourcedir}/mp/FaultToleranceSnippets.java[tag=snippet_1, indent=0]
----

2. The method `timedCircuitBreaker` defines a _rolling window_ of size 10 and a policy
to open the circuit breaker after 4 or more failures occur in that window, and to transition
back to half-open state after 3 consecutive and successful runs. Additionally, it sets an overall timeout
for the invocation of 1.5 seconds.
+
[source,java]
----
include::{sourcedir}/mp/FaultToleranceSnippets.java[tag=snippet_2, indent=0]
----

3. The method `executeWithQueueAndFallback` defines a bulkhead that will limit the number of
concurrent calls to a maximum of 2; any additional tasks shall be queued up to a maximum of 10.
Finally, if an error occurs the `onFailure` method shall be called as a fallback mechanism.
The `@Asynchronous` annotation is needed to enable queueing of bulkhead tasks.
+
[source,java]
----
include::{sourcedir}/mp/FaultToleranceSnippets.java[tag=snippet_3, indent=0]
----

== Additional Information

For additional information about this API, see the
link:{microprofile-fault-tolerance-javadoc-url}[MicroProfile Fault Tolerance Javadocs].

== Reference

* link:{microprofile-fault-tolerance-spec-url}[MicroProfile Fault Tolerance]




© 2015 - 2024 Weber Informatics LLC | Privacy Policy