Skip to content

Commit 2a68883

Browse files
Generate update ID at call time if not set (#2319)
1 parent 4cee4e0 commit 2a68883

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

temporal-sdk/src/main/java/io/temporal/client/UpdateOptions.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import com.google.common.base.Objects;
2424
import java.lang.reflect.Type;
25-
import java.util.UUID;
25+
import javax.annotation.Nullable;
2626

2727
public final class UpdateOptions<T> {
2828
public static <T> UpdateOptions.Builder<T> newBuilder() {
@@ -73,7 +73,7 @@ public String getUpdateName() {
7373
return updateName;
7474
}
7575

76-
public String getUpdateId() {
76+
public @Nullable String getUpdateId() {
7777
return updateId;
7878
}
7979

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

239239
/** Builds StartUpdateOptions with default values. */
240240
public UpdateOptions<T> build() {
241-
if (updateId == null || updateId.isEmpty()) {
242-
updateId = UUID.randomUUID().toString();
243-
}
244-
245241
return new UpdateOptions<T>(
246242
updateName,
247243
updateId,

temporal-sdk/src/main/java/io/temporal/client/WorkflowStubImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,17 @@ public <R> WorkflowUpdateHandle<R> startUpdate(UpdateOptions<R> options, Object.
372372
options.validate();
373373
WorkflowExecution targetExecution = execution.get();
374374
try {
375+
String updateId =
376+
Strings.isNullOrEmpty(options.getUpdateId())
377+
? UUID.randomUUID().toString()
378+
: options.getUpdateId();
375379
return workflowClientInvoker.startUpdate(
376380
new WorkflowClientCallsInterceptor.StartUpdateInput<>(
377381
targetExecution,
378382
workflowType,
379383
options.getUpdateName(),
380384
Header.empty(),
381-
options.getUpdateId(),
385+
updateId,
382386
args,
383387
options.getResultClass(),
384388
options.getResultType(),

temporal-sdk/src/main/java/io/temporal/internal/client/RootWorkflowClientInvoker.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static io.temporal.internal.common.HeaderUtils.intoPayloadMap;
2626
import static io.temporal.internal.common.WorkflowExecutionUtils.makeUserMetaData;
2727

28+
import com.google.common.base.Strings;
2829
import io.grpc.Deadline;
2930
import io.grpc.Status;
3031
import io.grpc.StatusRuntimeException;
@@ -189,13 +190,17 @@ public <R> WorkflowUpdateWithStartOutput<R> updateWithStart(
189190
UpdateWithStartWorkflowOperation<R> updateOperation = input.getUpdateOperation();
190191
UpdateOptions<?> updateOptions = updateOperation.getOptions();
191192
updateOptions.validate();
193+
String updateId =
194+
Strings.isNullOrEmpty(updateOptions.getUpdateId())
195+
? UUID.randomUUID().toString()
196+
: updateOptions.getUpdateId();
192197
StartUpdateInput<?> updateInput =
193198
new StartUpdateInput<>(
194199
WorkflowExecution.newBuilder().setWorkflowId(startInput.getWorkflowId()).build(),
195200
Optional.of(startInput.getWorkflowType()),
196201
updateOptions.getUpdateName(),
197202
Header.empty(),
198-
updateOptions.getUpdateId(),
203+
updateId,
199204
updateOperation.getUpdateArgs(),
200205
updateOptions.getResultClass(),
201206
updateOptions.getResultType(),

temporal-sdk/src/test/java/io/temporal/client/functional/UpdateTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,38 @@ public void updateWorkflowDuplicateId() throws ExecutionException, InterruptedEx
169169
assertEquals("some-value", handle.getResultAsync().get());
170170
}
171171

172+
@Test
173+
public void updateWorkflowReuseOptions() throws ExecutionException, InterruptedException {
174+
WorkflowClient workflowClient = testWorkflowRule.getWorkflowClient();
175+
String workflowType = TestWorkflows.WorkflowWithUpdate.class.getSimpleName();
176+
WorkflowStub workflowStub =
177+
workflowClient.newUntypedWorkflowStub(
178+
workflowType,
179+
SDKTestOptions.newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue()));
180+
181+
WorkflowExecution execution = workflowStub.start();
182+
SDKTestWorkflowRule.waitForOKQuery(workflowStub);
183+
184+
UpdateOptions updateOptions =
185+
UpdateOptions.newBuilder(String.class)
186+
.setUpdateName("update")
187+
.setFirstExecutionRunId(execution.getRunId())
188+
.setWaitForStage(WorkflowUpdateStage.ACCEPTED)
189+
.build();
190+
assertEquals(
191+
"some-value",
192+
workflowStub.startUpdate(updateOptions, 0, "some-value").getResultAsync().get());
193+
testWorkflowRule.waitForTheEndOfWFT(execution.getWorkflowId());
194+
// Try to send another update request with the same update options
195+
assertEquals(
196+
"some-other-value",
197+
workflowStub.startUpdate(updateOptions, 0, "some-other-value").getResultAsync().get());
198+
199+
// Complete the workflow
200+
workflowStub.update("complete", void.class);
201+
assertEquals("complete", workflowStub.getResult(String.class));
202+
}
203+
172204
@Test
173205
public void updateWithStart() throws ExecutionException, InterruptedException {
174206
String workflowId = UUID.randomUUID().toString();

0 commit comments

Comments
 (0)