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.