-
Notifications
You must be signed in to change notification settings - Fork 0
feat(java): per-task retry backoff via core RetryPolicy #311
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
78 changes: 78 additions & 0 deletions
78
sdks/java/src/main/java/org/byteveda/taskito/task/RetryPolicy.java
This file contains hidden or 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,78 @@ | ||
| package org.byteveda.taskito.task; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * A task's retry-backoff curve: how long to wait between attempts. | ||
| * | ||
| * <p>The retry <em>budget</em> (how many attempts) is set per enqueue via | ||
| * {@link Task#maxRetries}. The core scheduler owns retry execution, so retries | ||
| * stay durable, survive worker crashes, and behave identically to the other | ||
| * Taskito SDKs — this type only supplies the timing the scheduler applies. | ||
| * | ||
| * <p>Instances are immutable. | ||
| */ | ||
| public final class RetryPolicy { | ||
| private final Duration baseDelay; | ||
| private final Duration maxDelay; | ||
| private final List<Duration> customDelays; | ||
|
|
||
| private RetryPolicy(Duration baseDelay, Duration maxDelay, List<Duration> customDelays) { | ||
| this.baseDelay = baseDelay; | ||
| this.maxDelay = maxDelay; | ||
| this.customDelays = customDelays; | ||
| } | ||
|
|
||
| /** | ||
| * Exponential backoff: retry N waits about {@code base · 2^N}, capped at | ||
| * {@code max}, plus a random jitter of up to {@code base} (spreads retries so | ||
| * a batch of failures doesn't retry in lockstep). | ||
| */ | ||
| public static RetryPolicy exponential(Duration base, Duration max) { | ||
| return new RetryPolicy(nonNegative(base, "base"), nonNegative(max, "max"), Collections.emptyList()); | ||
| } | ||
|
|
||
| /** | ||
| * Explicit per-attempt delays, applied exactly (no jitter): retry N waits | ||
| * {@code delays[N]}. The list is authoritative for the retries it covers, so | ||
| * supply at least as many delays as the task's {@code maxRetries} — once the | ||
| * list is exhausted any further retries fire immediately. | ||
| */ | ||
| public static RetryPolicy delays(Duration... delays) { | ||
| if (delays.length == 0) { | ||
| throw new IllegalArgumentException("at least one delay is required"); | ||
| } | ||
| List<Duration> copy = new ArrayList<>(delays.length); | ||
| for (Duration delay : delays) { | ||
| copy.add(nonNegative(delay, "delay")); | ||
| } | ||
| return new RetryPolicy(null, null, Collections.unmodifiableList(copy)); | ||
| } | ||
|
|
||
| /** Exponential base delay, or {@code null} when using {@link #delays}. */ | ||
| public Duration baseDelay() { | ||
| return baseDelay; | ||
| } | ||
|
|
||
| /** Backoff cap, or {@code null} when using {@link #delays}. */ | ||
| public Duration maxDelay() { | ||
| return maxDelay; | ||
| } | ||
|
|
||
| /** Explicit per-attempt delays, or an empty list when using exponential backoff. */ | ||
| public List<Duration> customDelays() { | ||
| return customDelays; | ||
| } | ||
|
|
||
| private static Duration nonNegative(Duration value, String what) { | ||
| Objects.requireNonNull(value, what + " must not be null"); | ||
| if (value.isNegative()) { | ||
| throw new IllegalArgumentException(what + " must not be negative"); | ||
| } | ||
| return value; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
66 changes: 66 additions & 0 deletions
66
sdks/java/src/test/java/org/byteveda/taskito/RetryPolicyTest.java
This file contains hidden or 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,66 @@ | ||
| package org.byteveda.taskito; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Path; | ||
| import java.time.Duration; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import org.byteveda.taskito.events.EventName; | ||
| import org.byteveda.taskito.task.RetryPolicy; | ||
| import org.byteveda.taskito.task.Task; | ||
| import org.byteveda.taskito.worker.Worker; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| /** | ||
| * A handler that fails twice then succeeds must be retried by the core scheduler | ||
| * — proving the per-task {@link RetryPolicy} is wired through to the native retry | ||
| * engine (RETRY outcomes fire; no Java-side re-enqueue emulation). | ||
| */ | ||
| class RetryPolicyTest { | ||
|
|
||
| @Test | ||
| @Timeout(30) | ||
| void failingTaskIsRetriedUntilItSucceeds(@TempDir Path dir) throws Exception { | ||
| Task<String> flaky = Task.of("flaky", String.class) | ||
| .maxRetries(3) | ||
| .retryPolicy(RetryPolicy.delays(Duration.ofMillis(10), Duration.ofMillis(10))); | ||
|
|
||
| try (Queue queue = Taskito.builder() | ||
| .backend("sqlite") | ||
| .url(dir.resolve("t.db").toString()) | ||
| .open()) { | ||
| String id = queue.enqueue(flaky, "go"); | ||
|
|
||
| AtomicInteger attempts = new AtomicInteger(); | ||
| AtomicInteger retries = new AtomicInteger(); | ||
| CountDownLatch done = new CountDownLatch(1); | ||
|
|
||
| try (Worker worker = queue.worker() | ||
| .handle(flaky, (String payload) -> { | ||
| if (attempts.incrementAndGet() < 3) { | ||
| throw new IllegalStateException("transient failure"); | ||
| } | ||
| return 42; | ||
| }) | ||
| .on(EventName.RETRY, event -> retries.incrementAndGet()) | ||
| .on(EventName.SUCCESS, event -> done.countDown()) | ||
| .start()) { | ||
| assertTrue(done.await(25, TimeUnit.SECONDS), "task should eventually succeed"); | ||
|
|
||
| assertEquals(3, attempts.get(), "should run three times (two failures, one success)"); | ||
| assertEquals(2, retries.get(), "core should emit a RETRY outcome per failure"); | ||
|
|
||
| Optional<byte[]> result = queue.getResult(id); | ||
| assertTrue(result.isPresent()); | ||
| assertEquals("42", new String(result.get(), StandardCharsets.UTF_8)); | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.