Skip to content

Commit

Permalink
add better config properties
Browse files Browse the repository at this point in the history
The old config properties, specified by MP Fault Tolerance, are still
supported, but the new ones are preferred. The transformation is:

- `<classname>/<methodname>/<annotation>/<member>` moves to
  `smallrye.faulttolerance."<classname>/<methodname>".<annotation>.<member>`
- `<classname>/<annotation>/<member>` moves to
  `smallrye.faulttolerance."<classname>".<annotation>.<member>`
- `<annotation>/<member>` moves to
  `smallrye.faulttolerance.global.<annotation>.<member>`

All the `<annotation>` and `<member>` parts are changed from camel case
(`BeforeRetry`, `methodName`) to kebab case (`before-retry`, `method-name`).
Two annotation members are special cased to improve consistency:

- `Retry/durationUnit` moves to `retry.max-duration-unit`
- `Retry/jitterDelayUnit` moves to `retry.jitter-unit`

Further:

- `MP_Fault_Tolerance_NonFallback_Enabled` moves to `smallrye.faulttolerance.enabled`
- `MP_Fault_Tolerance_Metrics_Enabled` moves to `smallrye.faulttolerance.metrics.enabled`
  • Loading branch information
Ladicek committed Nov 27, 2024
1 parent c3f421c commit 2ff4fa4
Show file tree
Hide file tree
Showing 49 changed files with 1,288 additions and 125 deletions.
3 changes: 3 additions & 0 deletions doc/modules/ROOT/pages/howto/begin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ For example:

[source,properties]
----
smallrye.faulttolerance."com.example.MyService/hello".retry.max-retries=5 <1>
# alternatively, a specification-defined property can be used
com.example.MyService/hello/Retry/maxRetries=5 <1>
----
<1> Even though the `@Retry` annotation says that `maxRetries` should be 10, this configuration takes precedence.
Expand Down
2 changes: 1 addition & 1 deletion doc/modules/ROOT/pages/integration/metrics.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ This bean is used to emit the actual metrics.

=== Noop

If the "noop" provider is used, metrics are completely disabled and the `MP_Fault_Tolerance_Metrics_Enabled` configuration property is ignored at runtime.
If the "noop" provider is used, metrics are completely disabled.

No metrics API and implementation is required in this case.
50 changes: 49 additions & 1 deletion doc/modules/ROOT/pages/reference/config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ To change the `maxRetries` property using MicroProfile Config, set:

[source,properties]
----
smallrye.faulttolerance."com.example.MyService/hello".retry.max-retries=5
# alternatively, a specification-defined property can be used
com.example.MyService/hello/Retry/maxRetries=5
----

Expand Down Expand Up @@ -58,6 +61,9 @@ The MicroProfile Config configuration property doesn't include the method name i

[source,properties]
----
smallrye.faulttolerance."com.example.MyService".retry.max-retries=5
# alternatively, a specification-defined property can be used
com.example.MyService/Retry/maxRetries=5
----

Expand All @@ -76,6 +82,9 @@ For example:

[source,properties]
----
smallrye.faulttolerance.global.retry.max-retries=5
# alternatively, a specification-defined property can be used
Retry/maxRetries=5
----

Expand Down Expand Up @@ -109,6 +118,11 @@ The following configuration:

[source,properties]
----
smallrye.faulttolerance.global.retry.max-retries=30
smallrye.faulttolerance."com.example.MyService".retry.max-retries=15
smallrye.faulttolerance."com.example.MyService/complexHello".retry.max-retries=20
# alternatively, specification-defined properties can be used
Retry/maxRetries=30
com.example.MyService/Retry/maxRetries=15
com.example.MyService/complexHello/Retry/maxRetries=20
Expand All @@ -126,32 +140,66 @@ It is possible to disable certain fault tolerance annotation on some method, if

[source,properties]
----
smallrye.faulttolerance."com.example.MyService/hello".retry.enabled=false
# alternatively, a specification-defined property can be used
com.example.MyService/hello/Retry/enabled=false
----

Or on some class, if the annotation is present on the class:

[source,properties]
----
smallrye.faulttolerance."com.example.MyService".retry.enabled=5
# alternatively, a specification-defined property can be used
com.example.MyService/Retry/enabled=false
----

Or globally:

[source,properties]
----
smallrye.faulttolerance.global.retry.enabled=5
# alternatively, a specification-defined property can be used
Retry/enabled=false
----

It is also possible to disable all fault tolerance completely:

[source,properties]
----
smallrye.faulttolerance.enabled=false
# alternatively, a specification-defined property can be used
MP_Fault_Tolerance_NonFallback_Enabled=false
----

This will leave only fallbacks enabled, all other annotations will be disabled.

== {smallrye-fault-tolerance} Configuration Properties

As demonstrated in the examples above, {smallrye-fault-tolerance} provides its own configuration properties, in addition to the specification-defined properties.
The specification-defined properties can of course be used, but the {smallrye-fault-tolerance} configuration properties have higher priority.

The mapping is relatively straightforward:

- `<classname>/<methodname>/<annotation>/<member>` moves to `smallrye.faulttolerance."<classname>/<methodname>".<annotation>.<member>`
- `<classname>/<annotation>/<member>` moves to `smallrye.faulttolerance."<classname>".<annotation>.<member>`
- `<annotation>/<member>` moves to `smallrye.faulttolerance.global.<annotation>.<member>`

All the `<annotation>` and `<member>` parts are changed from camel case (`BeforeRetry`, `methodName`) to kebab case (`before-retry`, `method-name`).
Two annotation members are special cased to improve consistency:

- `Retry/durationUnit` moves to `retry.max-duration-unit`, because the value property is called `maxDuration` (`max-duration`)
- `Retry/jitterDelayUnit` moves to `retry.jitter-unit`, because the value property is called `jitter`

Further:

- `MP_Fault_Tolerance_NonFallback_Enabled` moves to `smallrye.faulttolerance.enabled`
- `MP_Fault_Tolerance_Metrics_Enabled` moves to `smallrye.faulttolerance.metrics.enabled`

== Links

This is described in detail in the {microprofile-fault-tolerance-url}#configuration[{microprofile-fault-tolerance} specification].
Configuration is described in detail in the {microprofile-fault-tolerance-url}#configuration[{microprofile-fault-tolerance} specification].
7 changes: 6 additions & 1 deletion doc/modules/ROOT/pages/reference/metrics.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ It is possible to completely disable fault tolerance metrics using MicroProfile

[source,properties]
----
smallrye.faulttolerance.metrics.enabled=false
# alternatively, a specification-defined property can be used
MP_Fault_Tolerance_Metrics_Enabled=false
----

Expand All @@ -104,4 +107,6 @@ smallrye.faulttolerance.opentelemetry.disabled=true
smallrye.faulttolerance.micrometer.disabled=true
----

This applies when only one metric provider is present as well as when multiple metric providers are present.
Note that setting `smallrye.faulttolerance.*.disabled` to `false` does not mean the provider is enabled unconditionally.
When that provider is not discovered or selected by the integrator, it cannot be enabled in any way.
These properties are only meant for disabling an otherwise enabled metrics provider; not the other way around.
8 changes: 4 additions & 4 deletions doc/modules/ROOT/pages/reference/programmatic-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ If you don't set a configuration property, it will default to the same value the
****
There's one exception to the "no external configuration" rule.
The CDI implementation looks for the well-known `MP_Fault_Tolerance_NonFallback_Enabled` configuration property in MicroProfile Config.
The standalone implementation looks for a system property with the same name, or obtains the value from custom configuration (as xref:integration/programmatic-api.adoc[described] in the integration section).
The CDI implementation looks for the `smallrye.faulttolerance.enabled` / `MP_Fault_Tolerance_NonFallback_Enabled` configuration properties in MicroProfile Config.
The standalone implementation looks for system properties with the same name, or obtains the value from custom configuration (as xref:integration/programmatic-api.adoc[described] in the integration section).
If such property exists and is set to `false`, only the fallback and thread offload fault tolerance strategies will be applied.
If at least one of these properties exists and is set to `false`, only the fallback and thread offload fault tolerance strategies will be applied.
Everything else will be ignored.
Note that this is somewhat different to the declarative, annotation-based API, where only fallback is retained and the `@Asynchronous` strategy is skipped as well.
Expand Down Expand Up @@ -241,7 +241,7 @@ All event listeners registered like this must run quickly and must not throw exc

== Configuration

As mentioned above, with the single exception of `MP_Fault_Tolerance_NonFallback_Enabled`, there is no support for external configuration of fault tolerance strategies.
As mentioned above, except of `smallrye.faulttolerance.enabled` / `MP_Fault_Tolerance_NonFallback_Enabled`, there is no support for external configuration of fault tolerance strategies.
This may change in the future, though possibly only in the CDI implementation.

== Metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@
public @interface AutoConfig {
/**
* Whether the annotation values can be overridden by MP Config.
* <p>
* Usually {@code true}, but there may be annotations for which that is not desirable.
*/
boolean configurable() default true;

/**
* Whether the annotation values can be overridden by SmallRye Fault Tolerance-specific MP Config
* properties, in addition to the specification-defined MP Config properties.
* <p>
* Usually {@code true}, but there may be annotations for which that is not desirable.
*/
boolean newConfigAllowed() default true;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.smallrye.faulttolerance.autoconfig;

import java.lang.annotation.Annotation;
import java.util.Optional;

import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.faulttolerance.Fallback;

public interface Config {
/**
Expand Down Expand Up @@ -38,38 +34,4 @@ public interface Config {
* are guaranteed to not touch MP Config.
*/
void materialize();

// ---

static <A extends Annotation> boolean isEnabled(Class<A> annotationType, MethodDescriptor method) {
// TODO converting strings to boolean here is inconsistent,
// but it's how SmallRye Fault Tolerance has always done it

org.eclipse.microprofile.config.Config config = ConfigProvider.getConfig();

Optional<String> onMethod = config.getOptionalValue(method.declaringClass.getName() +
"/" + method.name + "/" + annotationType.getSimpleName() + "/enabled", String.class);
if (onMethod.isPresent()) {
return Boolean.parseBoolean(onMethod.get());
}

Optional<String> onClass = config.getOptionalValue(method.declaringClass.getName() +
"/" + annotationType.getSimpleName() + "/enabled", String.class);
if (onClass.isPresent()) {
return Boolean.parseBoolean(onClass.get());
}

Optional<String> onGlobal = config.getOptionalValue(annotationType.getSimpleName()
+ "/enabled", String.class);
if (onGlobal.isPresent()) {
return Boolean.parseBoolean(onGlobal.get());
}

if (Fallback.class.equals(annotationType)) {
return true;
}

return config.getOptionalValue("MP_Fault_Tolerance_NonFallback_Enabled", Boolean.class)
.orElse(true);
}
}
5 changes: 5 additions & 0 deletions implementation/autoconfig/processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-common</artifactId>
<version>${version.smallrye-config}</version>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit 2ff4fa4

Please sign in to comment.