-
Notifications
You must be signed in to change notification settings - Fork 165
Add support for activity reset #2546
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
Quinn-With-Two-Ns
merged 3 commits into
temporalio:master
from
Quinn-With-Two-Ns:issue-2488
Jul 9, 2025
Merged
Changes from all commits
Commits
Show all changes
3 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
2 changes: 2 additions & 0 deletions
2
temporal-sdk/src/main/java/io/temporal/client/ActivityPausedException.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
20 changes: 20 additions & 0 deletions
20
temporal-sdk/src/main/java/io/temporal/client/ActivityResetException.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,20 @@ | ||
package io.temporal.client; | ||
|
||
import io.temporal.activity.ActivityInfo; | ||
import io.temporal.common.Experimental; | ||
|
||
/*** | ||
* Indicates that the activity attempt was reset by the user. | ||
* | ||
* <p>Catching this exception directly is discouraged and catching the parent class {@link ActivityCompletionException} is recommended instead.<br> | ||
*/ | ||
@Experimental | ||
public final class ActivityResetException extends ActivityCompletionException { | ||
public ActivityResetException(ActivityInfo info) { | ||
super(info); | ||
} | ||
|
||
public ActivityResetException() { | ||
super(); | ||
} | ||
} |
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
113 changes: 113 additions & 0 deletions
113
temporal-sdk/src/test/java/io/temporal/activity/ActivityResetTest.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,113 @@ | ||
package io.temporal.activity; | ||
|
||
import static org.junit.Assume.assumeTrue; | ||
|
||
import io.temporal.api.common.v1.WorkflowExecution; | ||
import io.temporal.api.workflow.v1.PendingActivityInfo; | ||
import io.temporal.api.workflowservice.v1.ResetActivityRequest; | ||
import io.temporal.client.ActivityResetException; | ||
import io.temporal.client.WorkflowStub; | ||
import io.temporal.common.converter.GlobalDataConverter; | ||
import io.temporal.testing.internal.SDKTestOptions; | ||
import io.temporal.testing.internal.SDKTestWorkflowRule; | ||
import io.temporal.workflow.Async; | ||
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.AtomicInteger; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class ActivityResetTest { | ||
|
||
@Rule | ||
public SDKTestWorkflowRule testWorkflowRule = | ||
SDKTestWorkflowRule.newBuilder() | ||
.setWorkflowTypes(TestWorkflowImpl.class) | ||
.setActivityImplementations(new HeartBeatingActivityImpl()) | ||
.build(); | ||
|
||
@Test | ||
public void activityReset() { | ||
assumeTrue( | ||
"Test Server doesn't support activity pause", SDKTestWorkflowRule.useExternalService); | ||
|
||
TestWorkflows.TestWorkflowReturnString workflow = | ||
testWorkflowRule.newWorkflowStub(TestWorkflows.TestWorkflowReturnString.class); | ||
Assert.assertEquals("I am stopped after reset", workflow.execute()); | ||
Assert.assertEquals( | ||
1, | ||
WorkflowStub.fromTyped(workflow) | ||
.describe() | ||
.getRawDescription() | ||
.getPendingActivitiesCount()); | ||
PendingActivityInfo activityInfo = | ||
WorkflowStub.fromTyped(workflow).describe().getRawDescription().getPendingActivities(0); | ||
Assert.assertEquals( | ||
"1", | ||
GlobalDataConverter.get() | ||
.fromPayload( | ||
activityInfo.getHeartbeatDetails().getPayloads(0), String.class, String.class)); | ||
} | ||
|
||
public static class TestWorkflowImpl implements TestWorkflows.TestWorkflowReturnString { | ||
|
||
private final TestActivities.TestActivity1 activities = | ||
Workflow.newActivityStub( | ||
TestActivities.TestActivity1.class, | ||
SDKTestOptions.newActivityOptions20sScheduleToClose()); | ||
|
||
@Override | ||
public String execute() { | ||
Async.function(activities::execute, ""); | ||
Workflow.sleep(Duration.ofSeconds(1)); | ||
return activities.execute("CompleteOnPause"); | ||
} | ||
} | ||
|
||
public static class HeartBeatingActivityImpl implements TestActivities.TestActivity1 { | ||
private final AtomicInteger resetCounter = new AtomicInteger(0); | ||
|
||
@Override | ||
public String execute(String arg) { | ||
ActivityInfo info = Activity.getExecutionContext().getInfo(); | ||
// Have the activity pause itself | ||
Activity.getExecutionContext() | ||
.getWorkflowClient() | ||
.getWorkflowServiceStubs() | ||
.blockingStub() | ||
.resetActivity( | ||
ResetActivityRequest.newBuilder() | ||
.setNamespace(info.getNamespace()) | ||
.setExecution( | ||
WorkflowExecution.newBuilder() | ||
.setWorkflowId(info.getWorkflowId()) | ||
.setRunId(info.getRunId()) | ||
.build()) | ||
.setId(info.getActivityId()) | ||
.build()); | ||
while (true) { | ||
try { | ||
Thread.sleep(1000); | ||
// Check if the activity has been reset, and the activity info shows we are on the 1st | ||
// attempt. | ||
if (resetCounter.get() >= 1 | ||
&& Activity.getExecutionContext().getInfo().getAttempt() == 1) { | ||
return "I am stopped after reset"; | ||
} | ||
// Heartbeat and verify that the correct exception is thrown | ||
Activity.getExecutionContext().heartbeat("1"); | ||
} catch (ActivityResetException pe) { | ||
// Counter is incremented to track the number of resets | ||
resetCounter.addAndGet(1); | ||
// This will fail the attempt, and the activity will be retried. | ||
throw pe; | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.