diff --git a/CHANGELOG.md b/CHANGELOG.md index 55175feb65..d609880d3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ to docs, or any other relevant information. project-scoped custom roles, and caller rate limits. ### Fixed +- The in-memory test server now reports the scheduled time of activity retries after their backoff. - Autoscaling workflow pollers now prioritize meaningful sticky backlog up to the sticky pool's target and use advertised slot capacity to balance sticky and non-sticky admission. Without an advertised maximum, additional polls of one queue type wait until the other has an active poll. diff --git a/temporal-sdk/src/test/java/io/temporal/activity/ActivityRetryScheduledTimestampTest.java b/temporal-sdk/src/test/java/io/temporal/activity/ActivityRetryScheduledTimestampTest.java new file mode 100644 index 0000000000..5d67c53e77 --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/activity/ActivityRetryScheduledTimestampTest.java @@ -0,0 +1,65 @@ +package io.temporal.activity; + +import static org.junit.Assert.assertTrue; + +import io.temporal.common.RetryOptions; +import io.temporal.failure.ApplicationFailure; +import io.temporal.testing.internal.SDKTestWorkflowRule; +import io.temporal.workflow.Workflow; +import io.temporal.workflow.shared.TestActivities; +import io.temporal.workflow.shared.TestWorkflows; +import java.time.Duration; +import java.util.concurrent.atomic.AtomicLong; +import org.junit.Rule; +import org.junit.Test; + +public class ActivityRetryScheduledTimestampTest { + private static final Duration INITIAL_INTERVAL = Duration.ofSeconds(10); + + private final AtomicLong firstScheduled = new AtomicLong(); + private final AtomicLong retryScheduled = new AtomicLong(); + + @Rule + public SDKTestWorkflowRule testWorkflowRule = + SDKTestWorkflowRule.newBuilder() + .setWorkflowTypes(RetryWorkflow.class) + .setActivityImplementations( + (TestActivities.NoArgsActivity) this::recordScheduledTimestamp) + .build(); + + @Test + public void retryScheduledTimestampIncludesBackoff() { + testWorkflowRule.newWorkflowStub(TestWorkflows.NoArgsWorkflow.class).execute(); + + long elapsed = retryScheduled.get() - firstScheduled.get(); + assertTrue( + "Elapsed time between scheduled attempts: " + elapsed + " ms", + elapsed >= INITIAL_INTERVAL.toMillis()); + } + + private void recordScheduledTimestamp() { + ActivityInfo info = Activity.getExecutionContext().getInfo(); + if (info.getAttempt() == 1) { + firstScheduled.set(info.getCurrentAttemptScheduledTimestamp()); + throw ApplicationFailure.newFailure("Retry this activity", "TestFailure"); + } + retryScheduled.set(info.getCurrentAttemptScheduledTimestamp()); + } + + public static class RetryWorkflow implements TestWorkflows.NoArgsWorkflow { + @Override + public void execute() { + Workflow.newActivityStub( + TestActivities.NoArgsActivity.class, + ActivityOptions.newBuilder() + .setStartToCloseTimeout(Duration.ofSeconds(5)) + .setRetryOptions( + RetryOptions.newBuilder() + .setInitialInterval(INITIAL_INTERVAL) + .setMaximumAttempts(2) + .build()) + .build()) + .execute(); + } + } +} diff --git a/temporal-test-server/src/main/java/io/temporal/internal/testservice/StateMachines.java b/temporal-test-server/src/main/java/io/temporal/internal/testservice/StateMachines.java index 81e24293d0..0d0f2fcbd1 100644 --- a/temporal-test-server/src/main/java/io/temporal/internal/testservice/StateMachines.java +++ b/temporal-test-server/src/main/java/io/temporal/internal/testservice/StateMachines.java @@ -2255,7 +2255,6 @@ private static RetryState attemptActivityRetry( data.retryState = nextAttempt; data.lastAttemptCompleteTime = ctx.currentTime(); task.setAttempt(nextAttempt.getAttempt()); - task.setCurrentAttemptScheduledTime(ctx.currentTime()); }); } else { data.nextBackoffInterval = Durations.ZERO; diff --git a/temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java b/temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java index 812b779035..8466f6402f 100644 --- a/temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java +++ b/temporal-test-server/src/main/java/io/temporal/internal/testservice/TestWorkflowMutableStateImpl.java @@ -2227,7 +2227,11 @@ private void addActivityRetryTimer(RequestContext ctx, StateMachine ctx1.addActivityTask(data.activityTask)); + update( + ctx1 -> { + data.activityTask.getTask().setCurrentAttemptScheduledTime(ctx1.currentTime()); + ctx1.addActivityTask(data.activityTask); + }); } catch (StatusRuntimeException e) { // NOT_FOUND is expected as timers are not removed if (e.getStatus().getCode() != Status.Code.NOT_FOUND) {