Skip to content

Commit

Permalink
Support for a name for all those operations that implement FtHandler.…
Browse files Browse the repository at this point in the history
… These names can be used for debugging, error reporting and future config support. Fixed some warnings too. (#2404)

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
  • Loading branch information
spericas authored Oct 1, 2020
1 parent 84b97ef commit ac3ff4f
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public Builder queueLength(int queueLength) {
}

/**
* Name is useful for debugging and in exception handling.
* A name assigned for debugging, error reporting or configuration purposes.
*
* @param name name of this bulkhead
* @param name the name
* @return updated builder instance
*/
public Builder name(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class BulkheadImpl implements Bulkhead {
}
}

@Override
public String name() {
return name;
}

@Override
public <T> Single<T> invoke(Supplier<? extends CompletionStage<T>> supplier) {
return invokeTask(DelayedTask.createSingle(supplier));
Expand Down Expand Up @@ -91,6 +96,7 @@ public long waitingQueueSize() {
}

// this method must be called while NOT holding a permit
@SuppressWarnings("unchecked")
private <R> R invokeTask(DelayedTask<R> task) {
if (inProgress.tryAcquire()) {
LOGGER.finest(() -> name + " invoke immediate: " + task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Builder implements io.helidon.common.Builder<CircuitBreaker> {
// rolling window size to
private int volume = 10;
private LazyValue<? extends ScheduledExecutorService> executor = FaultTolerance.scheduledExecutor();
private String name = "CircuitBreaker-" + System.identityHashCode(this);

private Builder() {
}
Expand Down Expand Up @@ -167,7 +168,8 @@ public Builder volume(int volume) {
* @param classes to consider failures to calculate failure ratio
* @return updated builder instance
*/
public Builder applyOn(Class<? extends Throwable>... classes) {
@SafeVarargs
public final Builder applyOn(Class<? extends Throwable>... classes) {
applyOn.clear();
Arrays.stream(classes)
.forEach(this::addApplyOn);
Expand Down Expand Up @@ -195,7 +197,8 @@ public Builder addApplyOn(Class<? extends Throwable> clazz) {
* @param classes to consider successful
* @return updated builder instance
*/
public Builder skipOn(Class<? extends Throwable>... classes) {
@SafeVarargs
public final Builder skipOn(Class<? extends Throwable>... classes) {
skipOn.clear();
Arrays.stream(classes)
.forEach(this::addSkipOn);
Expand Down Expand Up @@ -228,6 +231,17 @@ public Builder executor(ScheduledExecutorService scheduledExecutor) {
return this;
}

/**
* A name assigned for debugging, error reporting or configuration purposes.
*
* @param name the name
* @return updated builder instance
*/
public Builder name(String name) {
this.name = name;
return this;
}

LazyValue<? extends ScheduledExecutorService> executor() {
return executor;
}
Expand Down Expand Up @@ -255,5 +269,9 @@ int successThreshold() {
int volume() {
return volume;
}

String name() {
return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,20 @@ class CircuitBreakerImpl implements CircuitBreaker {
private final AtomicBoolean halfOpenInProgress = new AtomicBoolean();
private final AtomicReference<ScheduledFuture<Boolean>> schedule = new AtomicReference<>();
private final ErrorChecker errorChecker;
private final String name;

CircuitBreakerImpl(CircuitBreaker.Builder builder) {
this.delayMillis = builder.delay().toMillis();
this.successThreshold = builder.successThreshold();
this.results = new ResultWindow(builder.volume(), builder.errorRatio());
this.executor = builder.executor();
this.errorChecker = ErrorChecker.create(builder.skipOn(), builder.applyOn());
this.name = builder.name();
}

@Override
public String name() {
return name;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public Builder<T> fallbackMulti(Function<Throwable, ? extends Flow.Publisher<T>>
* @param classes classes to fallback on
* @return updated builder instance
*/
public Builder<T> applyOn(Class<? extends Throwable>... classes) {
@SafeVarargs
public final Builder<T> applyOn(Class<? extends Throwable>... classes) {
applyOn.clear();
Arrays.stream(classes)
.forEach(this::addApplyOn);
Expand All @@ -148,7 +149,8 @@ public Builder<T> addApplyOn(Class<? extends Throwable> clazz) {
* @param classes classes not to fallback on
* @return updated builder instance
*/
public Builder<T> skipOn(Class<? extends Throwable>... classes) {
@SafeVarargs
public final Builder<T> skipOn(Class<? extends Throwable>... classes) {
skipOn.clear();
Arrays.stream(classes)
.forEach(this::addSkipOn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,17 @@ public Builder add(FtHandler ft) {

private static class FtHandlerImpl implements FtHandler {
private final List<FtHandler> validFts;
private final String name = "FtHandler-" + System.identityHashCode(this);

private FtHandlerImpl(List<FtHandler> validFts) {
this.validFts = new LinkedList<>(validFts);
}

@Override
public String name() {
return name;
}

@Override
public <T> Multi<T> invokeMulti(Supplier<? extends Flow.Publisher<T>> supplier) {
Supplier<? extends Flow.Publisher<T>> next = supplier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
* </ul>
*/
public interface FtHandler {

/**
* A name assigned to a handler for debugging, error reporting or configuration purposes.
*
* @return a non-null name for this handler
*/
String name();

/**
* Invoke this fault tolerance handler on a supplier of a {@link java.util.concurrent.CompletionStage}, such as a
* {@link io.helidon.common.reactive.Single}.
Expand Down
22 changes: 20 additions & 2 deletions fault-tolerance/src/main/java/io/helidon/faulttolerance/Retry.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Builder implements io.helidon.common.Builder<Retry> {

private Duration overallTimeout = Duration.ofSeconds(1);
private LazyValue<? extends ScheduledExecutorService> scheduledExecutor = FaultTolerance.scheduledExecutor();
private String name = "Retry-" + System.identityHashCode(this);

private Builder() {
}
Expand Down Expand Up @@ -90,7 +91,8 @@ public Builder retryPolicy(RetryPolicy policy) {
* @param classes to consider failures and trigger a retry
* @return updated builder instance
*/
public Builder applyOn(Class<? extends Throwable>... classes) {
@SafeVarargs
public final Builder applyOn(Class<? extends Throwable>... classes) {
applyOn.clear();
Arrays.stream(classes)
.forEach(this::addApplyOn);
Expand Down Expand Up @@ -118,7 +120,8 @@ public Builder addApplyOn(Class<? extends Throwable> clazz) {
* @param classes to skip retries
* @return updated builder instance
*/
public Builder skipOn(Class<? extends Throwable>... classes) {
@SafeVarargs
public final Builder skipOn(Class<? extends Throwable>... classes) {
skipOn.clear();
Arrays.stream(classes)
.forEach(this::addSkipOn);
Expand Down Expand Up @@ -164,6 +167,17 @@ public Builder overallTimeout(Duration overallTimeout) {
return this;
}

/**
* A name assigned for debugging, error reporting or configuration purposes.
*
* @param name the name
* @return updated builder instance
*/
public Builder name(String name) {
this.name = name;
return this;
}

Set<Class<? extends Throwable>> applyOn() {
return applyOn;
}
Expand All @@ -183,6 +197,10 @@ Duration overallTimeout() {
LazyValue<? extends ScheduledExecutorService> scheduledExecutor() {
return scheduledExecutor;
}

String name() {
return name;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ class RetryImpl implements Retry {
private final long maxTimeNanos;
private final Retry.RetryPolicy retryPolicy;
private final AtomicLong retryCounter = new AtomicLong(0L);
private final String name;

RetryImpl(Retry.Builder builder) {
this.scheduledExecutor = builder.scheduledExecutor();
this.errorChecker = ErrorChecker.create(builder.skipOn(), builder.applyOn());
this.maxTimeNanos = builder.overallTimeout().toNanos();
this.retryPolicy = builder.retryPolicy();
this.name = builder.name();
}

@Override
public String name() {
return name;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Builder implements io.helidon.common.Builder<Timeout> {
private Duration timeout = Duration.ofSeconds(10);
private LazyValue<? extends ScheduledExecutorService> executor = FaultTolerance.scheduledExecutor();
private boolean currentThread = false;
private String name = "Timeout-" + System.identityHashCode(this);

private Builder() {
}
Expand Down Expand Up @@ -96,6 +97,17 @@ public Builder executor(ScheduledExecutorService executor) {
return this;
}

/**
* A name assigned for debugging, error reporting or configuration purposes.
*
* @param name the name
* @return updated builder instance
*/
public Builder name(String name) {
this.name = name;
return this;
}

Duration timeout() {
return timeout;
}
Expand All @@ -107,5 +119,9 @@ LazyValue<? extends ScheduledExecutorService> executor() {
boolean currentThread() {
return currentThread;
}

String name() {
return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ class TimeoutImpl implements Timeout {
private final long timeoutMillis;
private final LazyValue<? extends ScheduledExecutorService> executor;
private final boolean currentThread;
private final String name;

TimeoutImpl(Timeout.Builder builder) {
this.timeoutMillis = builder.timeout().toMillis();
this.executor = builder.executor();
this.currentThread = builder.currentThread();
this.name = builder.name();
}

@Override
public String name() {
return name;
}

@Override
Expand Down

0 comments on commit ac3ff4f

Please sign in to comment.