-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add a retry on storage interface
- Loading branch information
1 parent
0b0e8de
commit 8673acc
Showing
11 changed files
with
271 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
core/src/main/java/io/kestra/core/annotations/Retryable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.kestra.core.annotations; | ||
|
||
import io.micronaut.aop.Around; | ||
import io.micronaut.context.annotation.AliasFor; | ||
import io.micronaut.context.annotation.Type; | ||
import io.micronaut.retry.annotation.DefaultRetryPredicate; | ||
import io.micronaut.retry.annotation.RetryPredicate; | ||
import io.micronaut.retry.intercept.OverrideRetryInterceptor; | ||
|
||
import java.lang.annotation.*; | ||
|
||
import javax.validation.constraints.Digits; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Inherited | ||
@Documented | ||
@Retention(RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.ANNOTATION_TYPE}) | ||
@Around | ||
@Type(OverrideRetryInterceptor.class) | ||
public @interface Retryable { | ||
int MAX_INTEGRAL_DIGITS = 4; | ||
|
||
/** | ||
* @return The exception types to include (defaults to all) | ||
*/ | ||
Class<? extends Throwable>[] value() default {}; | ||
|
||
/** | ||
* @return The exception types to include (defaults to all) | ||
*/ | ||
@AliasFor(member = "value") | ||
Class<? extends Throwable>[] includes() default {}; | ||
|
||
/** | ||
* @return The exception types to exclude (defaults to none) | ||
*/ | ||
Class<? extends Throwable>[] excludes() default {}; | ||
|
||
/** | ||
* @return The maximum number of retry attempts | ||
*/ | ||
@Digits(integer = MAX_INTEGRAL_DIGITS, fraction = 0) | ||
String attempts() default "${kestra.retries.attempts:5}"; | ||
|
||
/** | ||
* @return The delay between retry attempts | ||
*/ | ||
String delay() default "${kestra.retries.delay:1s}"; | ||
|
||
/** | ||
* @return The maximum overall delay | ||
*/ | ||
String maxDelay() default "${kestra.retries.max-delay:}"; | ||
|
||
/** | ||
* @return The multiplier to use to calculate the delay | ||
*/ | ||
@Digits(integer = 2, fraction = 2) | ||
String multiplier() default "${kestra.retries.multiplier:2.0}"; | ||
|
||
/** | ||
* @return The retry predicate class to use instead of {@link io.micronaut.retry.annotation.Retryable#includes} and {@link io.micronaut.retry.annotation.Retryable#excludes} | ||
* (defaults to none) | ||
*/ | ||
Class<? extends RetryPredicate> predicate() default DefaultRetryPredicate.class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
core/src/main/java/io/micronaut/retry/intercept/OverrideRetryInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.micronaut.retry.intercept; | ||
|
||
import io.kestra.core.annotations.Retryable; | ||
import io.micronaut.aop.InterceptPhase; | ||
import io.micronaut.aop.InterceptedMethod; | ||
import io.micronaut.aop.MethodInterceptor; | ||
import io.micronaut.aop.MethodInvocationContext; | ||
import io.micronaut.context.event.ApplicationEventPublisher; | ||
import io.micronaut.core.annotation.AnnotationValue; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.convert.value.MutableConvertibleValues; | ||
import io.micronaut.retry.RetryState; | ||
import io.micronaut.retry.annotation.DefaultRetryPredicate; | ||
import io.micronaut.retry.event.RetryEvent; | ||
import jakarta.inject.Singleton; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Replace {@link DefaultRetryInterceptor} only to catch Throwable | ||
*/ | ||
@Singleton | ||
public class OverrideRetryInterceptor implements MethodInterceptor<Object, Object> { | ||
private static final Logger LOG = LoggerFactory.getLogger(OverrideRetryInterceptor.class); | ||
private final ApplicationEventPublisher<RetryEvent> eventPublisher; | ||
|
||
public OverrideRetryInterceptor(ApplicationEventPublisher<RetryEvent> eventPublisher) { | ||
this.eventPublisher = eventPublisher; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return InterceptPhase.RETRY.getPosition(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Object intercept(MethodInvocationContext<Object, Object> context) { | ||
Optional<AnnotationValue<Retryable>> opt = context.findAnnotation(Retryable.class); | ||
if (opt.isEmpty()) { | ||
return context.proceed(); | ||
} | ||
|
||
AnnotationValue<Retryable> retry = opt.get(); | ||
|
||
MutableRetryState retryState = new SimpleRetry( | ||
retry.get("attempts", Integer.class).orElse(5), | ||
retry.get("multiplier", Double.class).orElse(2D), | ||
retry.get("delay", Duration.class).orElse(Duration.ofSeconds(1)), | ||
retry.get("maxDelay", Duration.class).orElse(null), | ||
new DefaultRetryPredicate(resolveIncludes(retry, "includes"), resolveIncludes(retry, "excludes")) | ||
); | ||
|
||
MutableConvertibleValues<Object> attrs = context.getAttributes(); | ||
attrs.put(RetryState.class.getName(), retry); | ||
|
||
InterceptedMethod interceptedMethod = InterceptedMethod.of(context); | ||
try { | ||
retryState.open(); | ||
|
||
Object result = retrySync(context, retryState, interceptedMethod); | ||
switch (interceptedMethod.resultType()) { | ||
case SYNCHRONOUS: | ||
retryState.close(null); | ||
return result; | ||
default: | ||
return interceptedMethod.unsupported(); | ||
} | ||
} catch (Exception e) { | ||
return interceptedMethod.handleException(e); | ||
} | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
private static List<Class<? extends Throwable>> resolveIncludes(AnnotationValue<Retryable> retry, String includes) { | ||
Class<?>[] values = retry.classValues(includes); | ||
return (List) Collections.unmodifiableList(Arrays.asList(values)); | ||
} | ||
|
||
private Object retrySync(MethodInvocationContext<Object, Object> context, MutableRetryState retryState, InterceptedMethod interceptedMethod) { | ||
boolean firstCall = true; | ||
while (true) { | ||
try { | ||
if (firstCall) { | ||
firstCall = false; | ||
return interceptedMethod.interceptResult(); | ||
} | ||
return interceptedMethod.interceptResult(this); | ||
} catch (Throwable e) { | ||
if (!retryState.canRetry(e)) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Cannot retry anymore. Rethrowing original exception for method: {}", context); | ||
} | ||
retryState.close(e); | ||
throw e; | ||
} else { | ||
long delayMillis = retryState.nextDelay(); | ||
try { | ||
if (eventPublisher != null) { | ||
try { | ||
eventPublisher.publishEvent(new RetryEvent(context, retryState, e)); | ||
} catch (Exception e1) { | ||
LOG.error("Error occurred publishing RetryEvent: " + e1.getMessage(), e1); | ||
} | ||
} | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Retrying execution for method [{}] after delay of {}ms for exception: {}", context, delayMillis, e.getMessage()); | ||
} | ||
Thread.sleep(delayMillis); | ||
} catch (InterruptedException e1) { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core/src/test/java/io/micronaut/retry/intercept/OverrideRetryInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.micronaut.retry.intercept; | ||
|
||
import io.kestra.core.annotations.Retryable; | ||
import io.micronaut.retry.event.RetryEvent; | ||
import io.micronaut.runtime.event.annotation.EventListener; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.channels.AlreadyBoundException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
@MicronautTest | ||
class OverrideRetryInterceptorTest { | ||
@Inject | ||
RetryEvents retryEvents; | ||
|
||
@Inject | ||
TestRetry retry; | ||
|
||
@Test | ||
void test() { | ||
assertThrows(AlreadyBoundException.class, retry::failedMethod); | ||
|
||
assertThat(retryEvents.count, is(5)); | ||
} | ||
|
||
@Singleton | ||
public static class TestRetry { | ||
@Retryable(delay = "1s", multiplier = "2.0") | ||
public String failedMethod() { | ||
throw new AlreadyBoundException(); | ||
} | ||
} | ||
|
||
@Singleton | ||
@Slf4j | ||
public static class RetryEvents { | ||
public int count = 0; | ||
@EventListener | ||
void onRetry(final RetryEvent event) { | ||
this.count++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,6 +242,10 @@ table { | |
border-radius: 0; | ||
} | ||
} | ||
|
||
td { | ||
word-break: break-all; | ||
} | ||
} | ||
|
||
.table { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters