Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't return update handles until desired stage reached #2066

Merged
3 changes: 2 additions & 1 deletion .github/workflows/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ on: [push, pull_request]

jobs:
features-test:
uses: temporalio/features/.github/workflows/java.yaml@main
uses: temporalio/features/.github/workflows/java.yaml@java-breaking-update
with:
java-repo-path: ${{github.event.pull_request.head.repo.full_name}}
version: ${{github.event.pull_request.head.ref}}
version-is-repo-ref: true
features-repo-ref: java-breaking-update
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class LazyUpdateHandleImpl<T> implements UpdateHandle<T> {
private final WorkflowExecution execution;
private final Class<T> resultClass;
private final Type resultType;
private WorkflowClientCallsInterceptor.PollWorkflowUpdateOutput<T> waitCompletedPollCall;

LazyUpdateHandleImpl(
WorkflowClientCallsInterceptor workflowClientInvoker,
Expand Down Expand Up @@ -72,12 +73,23 @@ public String getId() {

@Override
public CompletableFuture<T> getResultAsync(long timeout, TimeUnit unit) {
WorkflowClientCallsInterceptor.PollWorkflowUpdateOutput output =
workflowClientInvoker.pollWorkflowUpdate(
new WorkflowClientCallsInterceptor.PollWorkflowUpdateInput<>(
execution, updateName, id, resultClass, resultType, timeout, unit));

return output
WorkflowClientCallsInterceptor.PollWorkflowUpdateOutput<T> pollCall = null;
boolean setFromWaitCompleted = false;

// If waitCompleted was called, use the result from that call.
synchronized (this) {
if (waitCompletedPollCall != null) {
pollCall = waitCompletedPollCall;
waitCompletedPollCall = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setFromWaitCompleted is never changed to true. I think the intention was to do that here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this looks like a bug

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Java StartUpdate code path is different from the other SDKs in a few subtle ways as well i'll try to align it with other SDKs as well

}
}

if (!setFromWaitCompleted) {
pollCall = pollUntilComplete(timeout, unit);
}

return pollCall
.getResult()
.exceptionally(
failure -> {
Expand Down Expand Up @@ -109,4 +121,17 @@ public CompletableFuture<T> getResultAsync(long timeout, TimeUnit unit) {
public CompletableFuture<T> getResultAsync() {
return this.getResultAsync(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}

// Can be called immediately after initialization to wait for the update to be completed, but
// still have the result be returned by getResultAsync.
void waitCompleted() {
waitCompletedPollCall = pollUntilComplete(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}

private WorkflowClientCallsInterceptor.PollWorkflowUpdateOutput<T> pollUntilComplete(
long timeout, TimeUnit unit) {
return workflowClientInvoker.pollWorkflowUpdate(
new WorkflowClientCallsInterceptor.PollWorkflowUpdateInput<>(
execution, updateName, id, resultClass, resultType, timeout, unit));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public static UpdateOptions getDefaultInstance() {
private final String updateName;
private final String updateId;
private final String firstExecutionRunId;
private final UpdateWaitPolicy waitPolicy;
private final WorkflowUpdateStage waitPolicy;
private final Class<T> resultClass;
private final Type resultType;

private UpdateOptions(
String updateName,
String updateId,
String firstExecutionRunId,
UpdateWaitPolicy waitPolicy,
WorkflowUpdateStage waitPolicy,
Class<T> resultClass,
Type resultType) {
this.updateName = updateName;
Expand All @@ -81,7 +81,7 @@ public String getFirstExecutionRunId() {
return firstExecutionRunId;
}

public UpdateWaitPolicy getWaitPolicy() {
public WorkflowUpdateStage getWaitPolicy() {
return waitPolicy;
}

Expand Down Expand Up @@ -152,7 +152,7 @@ public static final class Builder<T> {
private String updateName;
private String updateId;
private String firstExecutionRunId;
private UpdateWaitPolicy waitPolicy;
private WorkflowUpdateStage waitPolicy;
private Class<T> resultClass;
private Type resultType;

Expand Down Expand Up @@ -208,7 +208,7 @@ public Builder<T> setFirstExecutionRunId(String firstExecutionRunId) {
* <li><b>Completed</b> Wait for the update to be completed by the workflow.
* </ul>
*/
public Builder<T> setWaitPolicy(UpdateWaitPolicy waitPolicy) {
public Builder<T> setWaitPolicy(WorkflowUpdateStage waitPolicy) {
this.waitPolicy = waitPolicy;
return this;
}
Expand Down Expand Up @@ -239,7 +239,7 @@ public UpdateOptions<T> build() {
updateName,
updateId,
firstExecutionRunId == null ? "" : firstExecutionRunId,
waitPolicy == null ? UpdateWaitPolicy.ACCEPTED : waitPolicy,
waitPolicy == null ? WorkflowUpdateStage.ACCEPTED : waitPolicy,
resultClass,
resultType == null ? resultClass : resultType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ static <T> WorkflowStub fromTyped(T typed) {

/**
* Asynchronously update a workflow execution by invoking its update handler and returning a
* handle to the update request.
* handle to the update request. If {@link WorkflowUpdateStage#COMPLETED} is specified, in the
* options, the handle will not be returned until the update is completed.
*
* @param options options that will be used to configure and start a new update request.
* @param args update method arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public <R> R update(String updateName, Class<R> resultClass, Object... args) {
UpdateOptions<R> options =
UpdateOptions.<R>newBuilder()
.setUpdateName(updateName)
.setWaitPolicy(UpdateWaitPolicy.COMPLETED)
.setWaitPolicy(WorkflowUpdateStage.COMPLETED)
.setResultClass(resultClass)
.build();
return startUpdate(options, args).getResultAsync().get();
Expand All @@ -316,7 +316,7 @@ public <R> UpdateHandle<R> startUpdate(String updateName, Class<R> resultClass,
UpdateOptions<R> options =
UpdateOptions.<R>newBuilder()
.setUpdateName(updateName)
.setWaitPolicy(UpdateWaitPolicy.ACCEPTED)
.setWaitPolicy(WorkflowUpdateStage.ACCEPTED)
.setResultClass(resultClass)
.setResultType(resultClass)
.build();
Expand Down Expand Up @@ -351,14 +351,20 @@ public <R> UpdateHandle<R> startUpdate(UpdateOptions<R> options, Object... args)
result.getReference().getWorkflowExecution(),
result.getResult());
} else {
return new LazyUpdateHandleImpl<>(
workflowClientInvoker,
workflowType.orElse(null),
options.getUpdateName(),
result.getReference().getUpdateId(),
result.getReference().getWorkflowExecution(),
options.getResultClass(),
options.getResultType());
LazyUpdateHandleImpl<R> handle =
new LazyUpdateHandleImpl<>(
workflowClientInvoker,
workflowType.orElse(null),
options.getUpdateName(),
result.getReference().getUpdateId(),
result.getReference().getWorkflowExecution(),
options.getResultClass(),
options.getResultType());
if (options.getWaitPolicy() == WorkflowUpdateStage.COMPLETED) {
// Don't return the handle until completed, since that's what's been asked for
handle.waitCompleted();
}
return handle;
}
} catch (Exception e) {
Throwable throwable = throwAsWorkflowFailureException(e, targetExecution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@

import io.temporal.api.enums.v1.UpdateWorkflowExecutionLifecycleStage;

public enum UpdateWaitPolicy {
/** Update request waits for the update to be accepted by the workflow */
public enum WorkflowUpdateStage {
/**
* Update request waits for the update to be until the update request has been admitted by the
* server - it may be the case that due to a considerations like load or resource limits that an
* update is made to wait before the server will indicate that it has been received and will be
* processed. This value does not wait for any sort of acknowledgement from a worker.
*/
ADMITTED(
UpdateWorkflowExecutionLifecycleStage.UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_ADMITTED),

/**
* Update request waits for the update to be accepted (and validated, if there is a validator) by
* the workflow
*/
ACCEPTED(
UpdateWorkflowExecutionLifecycleStage.UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_ACCEPTED),

Expand All @@ -33,7 +45,7 @@ public enum UpdateWaitPolicy {

private final UpdateWorkflowExecutionLifecycleStage policy;

UpdateWaitPolicy(UpdateWorkflowExecutionLifecycleStage policy) {
WorkflowUpdateStage(UpdateWorkflowExecutionLifecycleStage policy) {
this.policy = policy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,18 @@ public <R> StartUpdateOutput<R> startUpdate(StartUpdateInput<R> input) {
.setFirstExecutionRunId(input.getFirstExecutionRunId())
.setRequest(request)
.build();
Deadline pollTimeoutDeadline = Deadline.after(POLL_UPDATE_TIMEOUT_S, TimeUnit.SECONDS);
UpdateWorkflowExecutionResponse result =
genericClient.update(updateRequest, pollTimeoutDeadline);

// Re-attempt the update until it is at least accepted, or passes the lifecycle stage specified
// by the user.
UpdateWorkflowExecutionResponse result;
do {
Deadline pollTimeoutDeadline = Deadline.after(POLL_UPDATE_TIMEOUT_S, TimeUnit.SECONDS);
result = genericClient.update(updateRequest, pollTimeoutDeadline);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make sure down below in pollWorkflowUpdateHelper that you remove the logic that retries on gRPC deadline exceeded error? That should no longer occur, we should just be bubbling all errors out

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, isn't the Python one doing that when it passes retry=True to the service client? Or, if that doesn't retry timeouts, then where is that happening? Because https://github.com/temporalio/sdk-python/blob/1a2acd59634a3b1d694937b8a8433c0014247370/temporalio/client.py#L4303 says it will, but there's no explicit handling of timeouts here: https://github.com/temporalio/sdk-python/blob/1a2acd59634a3b1d694937b8a8433c0014247370/temporalio/client.py#L4359

Copy link
Member Author

@Sushisource Sushisource May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easy to change Java to not do this and just default to max timeout for getResult calls, but, not sure that's the right thing to do.

(I committed it so we can see what I mean - works fine, but, seems like maybe not right? At minimum what python is saying the doc vs. what it does is either inconsistent, or the loop is not needed, or not the same as what I've just done here)

Copy link
Member

@cretz cretz May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I need to update that Python doc to remove that last sentence (I fixed logic but forgot about docs). We are no longer using timeout/exceptions to drive the loop.

Just need to remove the idea that deadline exceeded means something special in the start/poll loop. Let all RPC exceptions bubble out as they always would and change the code to only care about the successful result instead of the whenComplete today that cares about either result or failure (not sure what the combinator is for success-only).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's done now. All it's doing is just interpreting the failure code into the right exception type which makes sense to me.

} while (result.getStage().getNumber() < input.getWaitPolicy().getLifecycleStage().getNumber()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we set a default for input.getWaitPolicy()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per @drewhoskins-temporal's latest requirements, we want wait-for-stage to be a required field for start. Also, we should call it "wait-for-stage" IMO to match Python and future SDKs (or if we don't like that term, we should call it something else and be consistent across SDKs with what it is called).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the latest requirements for start were to, if the wait stage is COMPLETED, after ACCEPTED you switched to polling for response before returning from the start call. Can you confirm at least from the user perspective that occurs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Sorry I missed that

&& result.getStage().getNumber()
< UpdateWorkflowExecutionLifecycleStage
.UPDATE_WORKFLOW_EXECUTION_LIFECYCLE_STAGE_ACCEPTED
.getNumber());

if (result.hasOutcome()) {
switch (result.getOutcome().getValueCase()) {
Expand Down Expand Up @@ -399,20 +408,18 @@ public <R> PollWorkflowUpdateOutput<R> pollWorkflowUpdate(PollWorkflowUpdateInpu

Deadline pollTimeoutDeadline = Deadline.after(input.getTimeout(), input.getTimeoutUnit());
pollWorkflowUpdateHelper(future, pollUpdateRequest, pollTimeoutDeadline);
return new PollWorkflowUpdateOutput(
return new PollWorkflowUpdateOutput<>(
future.thenApply(
(result) -> {
if (result.hasOutcome()) {
switch (result.getOutcome().getValueCase()) {
case SUCCESS:
Optional<Payloads> updateResult = Optional.of(result.getOutcome().getSuccess());
R resultValue =
convertResultPayloads(
updateResult,
input.getResultClass(),
input.getResultType(),
dataConverterWithWorkflowContext);
return resultValue;
return convertResultPayloads(
updateResult,
input.getResultClass(),
input.getResultType(),
dataConverterWithWorkflowContext);
case FAILURE:
throw new WorkflowUpdateException(
input.getWorkflowExecution(),
Expand All @@ -434,31 +441,26 @@ private void pollWorkflowUpdateHelper(
CompletableFuture<PollWorkflowExecutionUpdateResponse> resultCF,
PollWorkflowExecutionUpdateRequest request,
Deadline deadline) {

Deadline pollTimeoutDeadline =
Deadline.after(POLL_UPDATE_TIMEOUT_S, TimeUnit.SECONDS).minimum(deadline);
genericClient
.pollUpdateAsync(request, pollTimeoutDeadline)
.pollUpdateAsync(request, deadline)
.whenComplete(
(r, e) -> {
if (e == null && !r.hasOutcome()) {
pollWorkflowUpdateHelper(resultCF, request, deadline);
return;
}
if ((e instanceof StatusRuntimeException
&& ((StatusRuntimeException) e).getStatus().getCode()
== Status.Code.DEADLINE_EXCEEDED)
|| pollTimeoutDeadline.isExpired()
|| (e == null && !r.hasOutcome())) {
// if the request has timed out, stop retrying
if (!deadline.isExpired()) {
pollWorkflowUpdateHelper(resultCF, request, deadline);
} else {
resultCF.completeExceptionally(
new TimeoutException(
"WorkflowId="
+ request.getUpdateRef().getWorkflowExecution().getWorkflowId()
+ ", runId="
+ request.getUpdateRef().getWorkflowExecution().getRunId()
+ ", updateId="
+ request.getUpdateRef().getUpdateId()));
}
|| deadline.isExpired()) {
resultCF.completeExceptionally(
new TimeoutException(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this may be changing shortly with #2069

"WorkflowId="
+ request.getUpdateRef().getWorkflowExecution().getWorkflowId()
+ ", runId="
+ request.getUpdateRef().getWorkflowExecution().getRunId()
+ ", updateId="
+ request.getUpdateRef().getUpdateId()));
} else if (e != null) {
resultCF.completeExceptionally(e);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,6 @@ public interface QueryableWorkflow {
void mySignal(String value);
}

@WorkflowInterface
public interface SimpleWorkflowWithUpdate {
Comment on lines -180 to -181
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unused


@WorkflowMethod
String execute();

@UpdateMethod
String update(String value);
}

@WorkflowInterface
public interface WorkflowWithUpdate {

Expand Down
Loading
Loading