Skip to content

Commit bbf2de7

Browse files
authored
Move workflow update polling inside of interceptor (#2159)
1 parent 6b39e44 commit bbf2de7

File tree

9 files changed

+115
-91
lines changed

9 files changed

+115
-91
lines changed

.github/workflows/features.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ jobs:
77
with:
88
java-repo-path: ${{github.event.pull_request.head.repo.full_name}}
99
version: ${{github.event.pull_request.head.ref}}
10-
version-is-repo-ref: true
10+
version-is-repo-ref: true
11+
features-repo-ref: java-update-iceptor-change

temporal-opentracing/src/main/java/io/temporal/opentracing/internal/OpenTracingWorkflowClientCallsInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.opentracing.Scope;
2424
import io.opentracing.Span;
2525
import io.opentracing.Tracer;
26+
import io.temporal.client.UpdateHandle;
2627
import io.temporal.common.interceptors.WorkflowClientCallsInterceptor;
2728
import io.temporal.common.interceptors.WorkflowClientCallsInterceptorBase;
2829
import io.temporal.opentracing.OpenTracingOptions;
@@ -119,7 +120,7 @@ public <R> QueryOutput<R> query(QueryInput<R> input) {
119120
}
120121

121122
@Override
122-
public <R> StartUpdateOutput<R> startUpdate(StartUpdateInput<R> input) {
123+
public <R> UpdateHandle<R> startUpdate(StartUpdateInput<R> input) {
123124
Span workflowStartUpdateSpan =
124125
contextAccessor.writeSpanContextToHeader(
125126
() ->

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

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.temporal.common.interceptors.Header;
3232
import io.temporal.common.interceptors.WorkflowClientCallsInterceptor;
3333
import io.temporal.failure.CanceledFailure;
34+
import io.temporal.internal.client.LazyUpdateHandleImpl;
3435
import io.temporal.serviceclient.CheckedExceptionWrapper;
3536
import io.temporal.serviceclient.StatusUtils;
3637
import java.lang.reflect.Type;
@@ -331,42 +332,20 @@ public <R> UpdateHandle<R> startUpdate(UpdateOptions<R> options, Object... args)
331332
options.validate();
332333
WorkflowExecution targetExecution = execution.get();
333334
try {
334-
WorkflowClientCallsInterceptor.StartUpdateOutput<R> result =
335-
workflowClientInvoker.startUpdate(
336-
new WorkflowClientCallsInterceptor.StartUpdateInput<>(
337-
targetExecution,
338-
options.getUpdateName(),
339-
Header.empty(),
340-
options.getUpdateId(),
341-
args,
342-
options.getResultClass(),
343-
options.getResultType(),
344-
options.getFirstExecutionRunId(),
345-
WaitPolicy.newBuilder()
346-
.setLifecycleStage(options.getWaitForStage().getProto())
347-
.build()));
348-
349-
if (result.hasResult()) {
350-
return new CompletedUpdateHandleImpl<>(
351-
result.getReference().getUpdateId(),
352-
result.getReference().getWorkflowExecution(),
353-
result.getResult());
354-
} else {
355-
LazyUpdateHandleImpl<R> handle =
356-
new LazyUpdateHandleImpl<>(
357-
workflowClientInvoker,
358-
workflowType.orElse(null),
359-
options.getUpdateName(),
360-
result.getReference().getUpdateId(),
361-
result.getReference().getWorkflowExecution(),
362-
options.getResultClass(),
363-
options.getResultType());
364-
if (options.getWaitForStage() == WorkflowUpdateStage.COMPLETED) {
365-
// Don't return the handle until completed, since that's what's been asked for
366-
handle.waitCompleted();
367-
}
368-
return handle;
369-
}
335+
return workflowClientInvoker.startUpdate(
336+
new WorkflowClientCallsInterceptor.StartUpdateInput<>(
337+
targetExecution,
338+
workflowType,
339+
options.getUpdateName(),
340+
Header.empty(),
341+
options.getUpdateId(),
342+
args,
343+
options.getResultClass(),
344+
options.getResultType(),
345+
options.getFirstExecutionRunId(),
346+
WaitPolicy.newBuilder()
347+
.setLifecycleStage(options.getWaitForStage().getProto())
348+
.build()));
370349
} catch (Exception e) {
371350
Throwable throwable = throwAsWorkflowFailureException(e, targetExecution);
372351
throw new WorkflowServiceException(targetExecution, workflowType.orElse(null), throwable);

temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowClientCallsInterceptor.java

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import io.temporal.api.common.v1.WorkflowExecution;
2424
import io.temporal.api.enums.v1.WorkflowExecutionStatus;
25-
import io.temporal.api.update.v1.UpdateRef;
2625
import io.temporal.api.update.v1.WaitPolicy;
26+
import io.temporal.client.UpdateHandle;
2727
import io.temporal.client.WorkflowOptions;
2828
import io.temporal.common.Experimental;
2929
import java.lang.reflect.Type;
@@ -78,7 +78,7 @@ public interface WorkflowClientCallsInterceptor {
7878
<R> QueryOutput<R> query(QueryInput<R> input);
7979

8080
@Experimental
81-
<R> StartUpdateOutput<R> startUpdate(StartUpdateInput<R> input);
81+
<R> UpdateHandle<R> startUpdate(StartUpdateInput<R> input);
8282

8383
@Experimental
8484
<R> PollWorkflowUpdateOutput<R> pollWorkflowUpdate(PollWorkflowUpdateInput<R> input);
@@ -383,6 +383,7 @@ public WorkflowExecution getWorkflowExecution() {
383383
@Experimental
384384
final class StartUpdateInput<R> {
385385
private final WorkflowExecution workflowExecution;
386+
private final Optional<String> workflowType;
386387
private final String updateName;
387388
private final Header header;
388389
private final Object[] arguments;
@@ -394,6 +395,7 @@ final class StartUpdateInput<R> {
394395

395396
public StartUpdateInput(
396397
WorkflowExecution workflowExecution,
398+
Optional<String> workflowType,
397399
String updateName,
398400
Header header,
399401
String updateId,
@@ -403,6 +405,7 @@ public StartUpdateInput(
403405
String firstExecutionRunId,
404406
WaitPolicy waitPolicy) {
405407
this.workflowExecution = workflowExecution;
408+
this.workflowType = workflowType;
406409
this.header = header;
407410
this.updateId = updateId;
408411
this.updateName = updateName;
@@ -417,6 +420,10 @@ public WorkflowExecution getWorkflowExecution() {
417420
return workflowExecution;
418421
}
419422

423+
public Optional<String> getWorkflowType() {
424+
return workflowType;
425+
}
426+
420427
public String getUpdateName() {
421428
return updateName;
422429
}
@@ -450,44 +457,6 @@ public WaitPolicy getWaitPolicy() {
450457
}
451458
}
452459

453-
@Experimental
454-
final class UpdateOutput<R> {
455-
private final R result;
456-
457-
public UpdateOutput(R result) {
458-
this.result = result;
459-
}
460-
461-
public R getResult() {
462-
return result;
463-
}
464-
}
465-
466-
@Experimental
467-
final class StartUpdateOutput<R> {
468-
private final UpdateRef reference;
469-
private final R result;
470-
private final boolean hasResult;
471-
472-
public StartUpdateOutput(UpdateRef reference, boolean hasResult, R result) {
473-
this.reference = reference;
474-
this.result = result;
475-
this.hasResult = hasResult;
476-
}
477-
478-
public UpdateRef getReference() {
479-
return reference;
480-
}
481-
482-
public boolean hasResult() {
483-
return hasResult;
484-
}
485-
486-
public R getResult() {
487-
return result;
488-
}
489-
}
490-
491460
@Experimental
492461
final class PollWorkflowUpdateInput<R> {
493462
private final WorkflowExecution workflowExecution;

temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowClientCallsInterceptorBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package io.temporal.common.interceptors;
2222

23+
import io.temporal.client.UpdateHandle;
2324
import java.util.concurrent.TimeoutException;
2425

2526
/** Convenience base class for {@link WorkflowClientCallsInterceptor} implementations. */
@@ -62,7 +63,7 @@ public <R> QueryOutput<R> query(QueryInput<R> input) {
6263
}
6364

6465
@Override
65-
public <R> StartUpdateOutput<R> startUpdate(StartUpdateInput<R> input) {
66+
public <R> UpdateHandle<R> startUpdate(StartUpdateInput<R> input) {
6667
return next.startUpdate(input);
6768
}
6869

temporal-sdk/src/main/java/io/temporal/client/CompletedUpdateHandleImpl.java renamed to temporal-sdk/src/main/java/io/temporal/internal/client/CompletedUpdateHandleImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
* limitations under the License.
1919
*/
2020

21-
package io.temporal.client;
21+
package io.temporal.internal.client;
2222

2323
import io.temporal.api.common.v1.WorkflowExecution;
24+
import io.temporal.client.UpdateHandle;
2425
import io.temporal.common.Experimental;
2526
import java.util.concurrent.CompletableFuture;
2627
import java.util.concurrent.TimeUnit;
2728

2829
@Experimental
29-
final class CompletedUpdateHandleImpl<T> implements UpdateHandle<T> {
30+
public final class CompletedUpdateHandleImpl<T> implements UpdateHandle<T> {
3031

3132
private final String id;
3233
private final WorkflowExecution execution;
3334
private final T result;
3435

35-
CompletedUpdateHandleImpl(String id, WorkflowExecution execution, T result) {
36+
public CompletedUpdateHandleImpl(String id, WorkflowExecution execution, T result) {
3637
this.id = id;
3738
this.execution = execution;
3839
this.result = result;

temporal-sdk/src/main/java/io/temporal/client/LazyUpdateHandleImpl.java renamed to temporal-sdk/src/main/java/io/temporal/internal/client/LazyUpdateHandleImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
* limitations under the License.
1919
*/
2020

21-
package io.temporal.client;
21+
package io.temporal.internal.client;
2222

2323
import io.grpc.Status;
2424
import io.grpc.StatusRuntimeException;
2525
import io.temporal.api.common.v1.WorkflowExecution;
26+
import io.temporal.client.UpdateHandle;
27+
import io.temporal.client.WorkflowException;
28+
import io.temporal.client.WorkflowServiceException;
2629
import io.temporal.common.Experimental;
2730
import io.temporal.common.interceptors.WorkflowClientCallsInterceptor;
2831
import io.temporal.serviceclient.CheckedExceptionWrapper;
@@ -33,7 +36,7 @@
3336
import java.util.concurrent.TimeoutException;
3437

3538
@Experimental
36-
final class LazyUpdateHandleImpl<T> implements UpdateHandle<T> {
39+
public final class LazyUpdateHandleImpl<T> implements UpdateHandle<T> {
3740

3841
private final WorkflowClientCallsInterceptor workflowClientInvoker;
3942
private final String workflowType;
@@ -44,7 +47,7 @@ final class LazyUpdateHandleImpl<T> implements UpdateHandle<T> {
4447
private final Type resultType;
4548
private WorkflowClientCallsInterceptor.PollWorkflowUpdateOutput<T> waitCompletedPollCall;
4649

47-
LazyUpdateHandleImpl(
50+
public LazyUpdateHandleImpl(
4851
WorkflowClientCallsInterceptor workflowClientInvoker,
4952
String workflowType,
5053
String updateName,

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
import io.temporal.api.query.v1.WorkflowQuery;
3232
import io.temporal.api.update.v1.*;
3333
import io.temporal.api.workflowservice.v1.*;
34-
import io.temporal.client.WorkflowClientOptions;
35-
import io.temporal.client.WorkflowUpdateException;
34+
import io.temporal.client.*;
3635
import io.temporal.common.converter.DataConverter;
3736
import io.temporal.common.interceptors.WorkflowClientCallsInterceptor;
3837
import io.temporal.internal.client.external.GenericWorkflowClient;
@@ -298,7 +297,7 @@ public <R> QueryOutput<R> query(QueryInput<R> input) {
298297
}
299298

300299
@Override
301-
public <R> StartUpdateOutput<R> startUpdate(StartUpdateInput<R> input) {
300+
public <R> UpdateHandle<R> startUpdate(StartUpdateInput<R> input) {
302301
DataConverter dataConverterWithWorkflowContext =
303302
clientOptions
304303
.getDataConverter()
@@ -337,10 +336,11 @@ public <R> StartUpdateOutput<R> startUpdate(StartUpdateInput<R> input) {
337336
// Re-attempt the update until it is at least accepted, or passes the lifecycle stage specified
338337
// by the user.
339338
UpdateWorkflowExecutionResponse result;
339+
UpdateWorkflowExecutionLifecycleStage waitForStage = input.getWaitPolicy().getLifecycleStage();
340340
do {
341341
Deadline pollTimeoutDeadline = Deadline.after(POLL_UPDATE_TIMEOUT_S, TimeUnit.SECONDS);
342342
result = genericClient.update(updateRequest, pollTimeoutDeadline);
343-
} while (result.getStage().getNumber() < input.getWaitPolicy().getLifecycleStage().getNumber()
343+
} while (result.getStage().getNumber() < waitForStage.getNumber()
344344
&& result.getStage().getNumber()
345345
< UpdateWorkflowExecutionLifecycleStage
346346
.UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_ACCEPTED
@@ -356,7 +356,10 @@ public <R> StartUpdateOutput<R> startUpdate(StartUpdateInput<R> input) {
356356
input.getResultClass(),
357357
input.getResultType(),
358358
dataConverterWithWorkflowContext);
359-
return new StartUpdateOutput<R>(result.getUpdateRef(), true, resultValue);
359+
return new CompletedUpdateHandleImpl<>(
360+
result.getUpdateRef().getUpdateId(),
361+
result.getUpdateRef().getWorkflowExecution(),
362+
resultValue);
360363
case FAILURE:
361364
throw new WorkflowUpdateException(
362365
result.getUpdateRef().getWorkflowExecution(),
@@ -370,7 +373,20 @@ public <R> StartUpdateOutput<R> startUpdate(StartUpdateInput<R> input) {
370373
+ result.getOutcome().getValueCase());
371374
}
372375
} else {
373-
return new StartUpdateOutput<R>(result.getUpdateRef(), false, null);
376+
LazyUpdateHandleImpl<R> handle =
377+
new LazyUpdateHandleImpl<>(
378+
this,
379+
input.getWorkflowType().orElse(null),
380+
input.getUpdateName(),
381+
result.getUpdateRef().getUpdateId(),
382+
result.getUpdateRef().getWorkflowExecution(),
383+
input.getResultClass(),
384+
input.getResultType());
385+
if (waitForStage == WorkflowUpdateStage.COMPLETED.getProto()) {
386+
// Don't return the handle until completed, since that's what's been asked for
387+
handle.waitCompleted();
388+
}
389+
return handle;
374390
}
375391
}
376392

0 commit comments

Comments
 (0)