Reset Workflow in Java SDK

I am trying to restart a workflow using below code.

ResetWorkflowExecutionRequest rwr =
ResetWorkflowExecutionRequest.newBuilder()
.setNamespace(workflowConfiguration.getNamespace())
.setWorkflowExecution(execution)
.setWorkflowTaskFinishEventId(4)
.setReason(reasonStr)
.build();

I have few questions.

  1. Can I restart any status workflow (Complete, timedOut, Failed, Cancelled)?
  2. I would like to start from beginning using sdk, is there a way or work around?
  1. Yes
  2. You have to read history to find out the first WorkflowTaskFinishEventId. Here is an issue to get this improved.

Thanks Maxim. I am reading history events but don’t see any method to get first WorkflowTaskFinishEventId. I am assuming while iterating the history, the first WorkflowTaskCompleted is " first WorkflowTaskFinishEventId". Is it correct understanding? How to get “id” ?. I noticed two methods getStartedEventId & getScheduledEventId. Which is the correct one?

he.getWorkflowTaskCompletedEventAttributes().getStartedEventId();
he.getWorkflowTaskCompletedEventAttributes().getScheduledEventId();

You are correct. It is the first WorkflowTask[…] event. You want the eventId:

he.getEventId()

as the id. Here is the tctl implementation of the reset logic.

Thanks Maxim. That’s helpful. The code captures one more case for getFirstWorkflowTaskEventID(). If EVENT_TYPE_WORKFLOW_TASK_COMPLETED doesn’t exist, can workflow be restarted EVENT_TYPE_WORKFLOW_TASK_SCHEDULED +1 ?

if e.GetEventType() == enumspb.EVENT_TYPE_WORKFLOW_TASK_SCHEDULED {
if workflowTaskEventID == 0 {
workflowTaskEventID = e.GetEventId() + 1
}

I think it covers use case when a workflow was terminated or timed out when a workflow task was only scheduled.

Thanks Maxim.

When trying to restart the workflow, getting response as run_id: “11c31eb2-d9fc-49b9-b84a-c32443bfd74a”. But in temporal UI that new runId is in failed state, result states “No retry policy set”.

ResetWorkflowExecutionResponse rwres = service.blockingStub().resetWorkflowExecution(rwr);

I have two questions:

  1. Which method of ResetWorkflowExecutionResponse provides error details?
  2. Ways to reset a workflow from SDK if retry policy not set?

What version of Temporal are you running? I’ve never heard about the retry policy requirement.
Could you post the history of the failed workflow?

Thanks Maxim. Yes, you are correct reset works without retry policy. There was something not correct in my code.

  1. How does “identity” get captured. I noticed that its different with in one workflow at different workflow tasks.
  2. Does identity get used to restart the workflow? I mean if identity is not network, then reset will timeout?
  3. Please point me to code repo for java sdk ResetWorkflowExecution ?
  1. How does “identity” get captured. I noticed that its different with in one workflow at different workflow tasks.

Identity is set through WorkflowClientOptions.identity. Its default value is the name of RuntimeMxBean. If a worker fails or workflow gets out of cache it can be loaded by a different worker instance. Thus you see different worker identities for the same workflow execution.

  1. Does identity get used to restart the workflow? I mean if identity is not network, then reset will timeout?

Identity is purely for human consumption. It is not used anywhere by the workflow or server code.

    1. Please point me to code repo for java sdk ResetWorkflowExecution ?

The code is automatically generated from Temporal gRPC definitions. Here is the module that does this.

Hi
I am using local Activities and need to reset the workflow from failed state, is it supported in java sdk?
Also when I try to reset ,I could do it just once.
can’t we restart a workflow multiple times?

Ran into the same thing locally. Consecutive resets of the same workflow seem to reuse the same workflow execution that was created for the very first reset. Checking with server team to see if we should file an issue (bug).

Update. Can’t reproduce this consistently. Can you on your end?

yes @tihomir , I can reproduce it consistently

Can we restart a workflow from failed activities using localActivities?

Thanks for the info. Can you share your SDK and Server versions please?

temporal - temporalio/auto-setup:1.12.1
sdk - io.temporal:temporal-sdk:1.3.1

Do you mean from a local activity in another workflow?
I don’t think local activities are best choice for this. They work only for short activities that do not exceed the workflow task timeout. ResetWorkflowExecutionRequest is client api and could take a long time to possibly complete.

say Workflow X has 3 local activities : A,B,C
and it failed at B , now I want to restart workflow X again from activity B.

Also , I tried this with Activities ,activity B started but didn’t start from beginning of activity

This doesn’t work with the sequence of local activities as they are all executed as a single workflow task. The whole sequence is reexecuted in this case.

Why are you using workflow reset to deal with activity failures? I would recommend using normal activities and retrying them for a long time to deal with underlying failures. This way no manual intervention is needed once the failure is fixed.