Skip to content

Commit

Permalink
Generate update ID at call time if not set (#2319)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns authored Nov 15, 2024
1 parent 4cee4e0 commit 2a68883
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.google.common.base.Objects;
import java.lang.reflect.Type;
import java.util.UUID;
import javax.annotation.Nullable;

public final class UpdateOptions<T> {
public static <T> UpdateOptions.Builder<T> newBuilder() {
Expand Down Expand Up @@ -73,7 +73,7 @@ public String getUpdateName() {
return updateName;
}

public String getUpdateId() {
public @Nullable String getUpdateId() {
return updateId;
}

Expand Down Expand Up @@ -238,10 +238,6 @@ public Builder<T> setResultType(Type resultType) {

/** Builds StartUpdateOptions with default values. */
public UpdateOptions<T> build() {
if (updateId == null || updateId.isEmpty()) {
updateId = UUID.randomUUID().toString();
}

return new UpdateOptions<T>(
updateName,
updateId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,17 @@ public <R> WorkflowUpdateHandle<R> startUpdate(UpdateOptions<R> options, Object.
options.validate();
WorkflowExecution targetExecution = execution.get();
try {
String updateId =
Strings.isNullOrEmpty(options.getUpdateId())
? UUID.randomUUID().toString()
: options.getUpdateId();
return workflowClientInvoker.startUpdate(
new WorkflowClientCallsInterceptor.StartUpdateInput<>(
targetExecution,
workflowType,
options.getUpdateName(),
Header.empty(),
options.getUpdateId(),
updateId,
args,
options.getResultClass(),
options.getResultType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static io.temporal.internal.common.HeaderUtils.intoPayloadMap;
import static io.temporal.internal.common.WorkflowExecutionUtils.makeUserMetaData;

import com.google.common.base.Strings;
import io.grpc.Deadline;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
Expand Down Expand Up @@ -189,13 +190,17 @@ public <R> WorkflowUpdateWithStartOutput<R> updateWithStart(
UpdateWithStartWorkflowOperation<R> updateOperation = input.getUpdateOperation();
UpdateOptions<?> updateOptions = updateOperation.getOptions();
updateOptions.validate();
String updateId =
Strings.isNullOrEmpty(updateOptions.getUpdateId())
? UUID.randomUUID().toString()
: updateOptions.getUpdateId();
StartUpdateInput<?> updateInput =
new StartUpdateInput<>(
WorkflowExecution.newBuilder().setWorkflowId(startInput.getWorkflowId()).build(),
Optional.of(startInput.getWorkflowType()),
updateOptions.getUpdateName(),
Header.empty(),
updateOptions.getUpdateId(),
updateId,
updateOperation.getUpdateArgs(),
updateOptions.getResultClass(),
updateOptions.getResultType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,38 @@ public void updateWorkflowDuplicateId() throws ExecutionException, InterruptedEx
assertEquals("some-value", handle.getResultAsync().get());
}

@Test
public void updateWorkflowReuseOptions() throws ExecutionException, InterruptedException {
WorkflowClient workflowClient = testWorkflowRule.getWorkflowClient();
String workflowType = TestWorkflows.WorkflowWithUpdate.class.getSimpleName();
WorkflowStub workflowStub =
workflowClient.newUntypedWorkflowStub(
workflowType,
SDKTestOptions.newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue()));

WorkflowExecution execution = workflowStub.start();
SDKTestWorkflowRule.waitForOKQuery(workflowStub);

UpdateOptions updateOptions =
UpdateOptions.newBuilder(String.class)
.setUpdateName("update")
.setFirstExecutionRunId(execution.getRunId())
.setWaitForStage(WorkflowUpdateStage.ACCEPTED)
.build();
assertEquals(
"some-value",
workflowStub.startUpdate(updateOptions, 0, "some-value").getResultAsync().get());
testWorkflowRule.waitForTheEndOfWFT(execution.getWorkflowId());
// Try to send another update request with the same update options
assertEquals(
"some-other-value",
workflowStub.startUpdate(updateOptions, 0, "some-other-value").getResultAsync().get());

// Complete the workflow
workflowStub.update("complete", void.class);
assertEquals("complete", workflowStub.getResult(String.class));
}

@Test
public void updateWithStart() throws ExecutionException, InterruptedException {
String workflowId = UUID.randomUUID().toString();
Expand Down

0 comments on commit 2a68883

Please sign in to comment.