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

Standardized update failure exception #2339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions temporal-sdk/src/main/java/io/temporal/client/WorkflowStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ static <T> WorkflowStub fromTyped(T typed) {
* @param <R> type of the update return value
* @param args update method arguments
* @return update result
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or completed and
* can't be signalled
* @throws WorkflowUpdateException if the update is rejected or failed during it's execution by
* the workflow.
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or is completed.
* @throws WorkflowServiceException for all other failures including networking and service
* availability issues
* availability issues.
*/
@Experimental
<R> R update(String updateName, Class<R> resultClass, Object... args);
Expand All @@ -103,6 +104,9 @@ static <T> WorkflowStub fromTyped(T typed) {
* @param <R> type of the update return value
* @param args update method arguments
* @return update handle that can be used to get the result of the update.
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or completed.
* @throws WorkflowServiceException for all other failures including networking and service
* availability issues.
*/
@Experimental
<R> WorkflowUpdateHandle<R> startUpdate(
Expand All @@ -116,6 +120,9 @@ <R> WorkflowUpdateHandle<R> startUpdate(
* @param options options that will be used to configure and start a new update request.
* @param args update method arguments
* @return update handle that can be used to get the result of the update.
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or completed.
* @throws WorkflowServiceException for all other failures including networking and service
* availability issues.
*/
@Experimental
<R> WorkflowUpdateHandle<R> startUpdate(UpdateOptions<R> options, Object... args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public interface WorkflowUpdateHandle<T> {
* Returns the result of the workflow update.
*
* @return the result of the workflow update
* @throws WorkflowUpdateException if the update was rejected or failed by the workflow.
*/
T getResult();

Expand All @@ -58,6 +59,7 @@ public interface WorkflowUpdateHandle<T> {
* @param timeout maximum time to wait and perform the background long polling
* @param unit unit of timeout
* @throws WorkflowUpdateTimeoutOrCancelledException if the timeout is reached.
* @throws WorkflowUpdateException if the update was rejected or failed by the workflow.
* @return the result of the workflow update
*/
T getResult(long timeout, TimeUnit unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package io.temporal.internal.client;

import io.temporal.api.common.v1.WorkflowExecution;
import io.temporal.client.WorkflowUpdateException;
import io.temporal.client.WorkflowUpdateHandle;
import io.temporal.common.Experimental;
import java.util.concurrent.CompletableFuture;
Expand All @@ -31,12 +32,22 @@ public final class CompletedWorkflowUpdateHandleImpl<T> implements WorkflowUpdat

private final String id;
private final WorkflowExecution execution;
private final WorkflowUpdateException exception;
private final T result;

public CompletedWorkflowUpdateHandleImpl(String id, WorkflowExecution execution, T result) {
this.id = id;
this.execution = execution;
this.result = result;
this.exception = null;
}

public CompletedWorkflowUpdateHandleImpl(
String id, WorkflowExecution execution, WorkflowUpdateException ex) {
this.id = id;
this.execution = execution;
this.exception = ex;
this.result = null;
}

@Override
Expand All @@ -51,21 +62,29 @@ public String getId() {

@Override
public T getResult() {
if (exception != null) {
throw exception;
}
return result;
}

@Override
public T getResult(long timeout, TimeUnit unit) {
return result;
return getResult();
}

@Override
public CompletableFuture<T> getResultAsync() {
if (exception != null) {
CompletableFuture<T> result = new CompletableFuture<>();
result.completeExceptionally(exception);
return result;
}
return CompletableFuture.completedFuture(result);
}

@Override
public CompletableFuture<T> getResultAsync(long timeout, TimeUnit unit) {
return CompletableFuture.completedFuture(result);
return getResultAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,15 @@ private <R> WorkflowUpdateHandle<R> toUpdateHandle(
result.getUpdateRef().getWorkflowExecution(),
resultValue);
case FAILURE:
throw new WorkflowUpdateException(
result.getUpdateRef().getWorkflowExecution(),
return new CompletedWorkflowUpdateHandleImpl<>(
result.getUpdateRef().getUpdateId(),
input.getUpdateName(),
dataConverterWithWorkflowContext.failureToException(
result.getOutcome().getFailure()));
result.getUpdateRef().getWorkflowExecution(),
new WorkflowUpdateException(
result.getUpdateRef().getWorkflowExecution(),
result.getUpdateRef().getUpdateId(),
input.getUpdateName(),
dataConverterWithWorkflowContext.failureToException(
result.getOutcome().getFailure())));
default:
throw new RuntimeException(
"Received unexpected outcome from update request: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void dynamicUpdate() throws ExecutionException, InterruptedException {
WorkflowUpdateException.class,
() ->
stub.startUpdate("reject", WorkflowUpdateStage.COMPLETED, String.class, "update input")
.getResultAsync());
.getResult());

stub.startUpdate("complete", WorkflowUpdateStage.COMPLETED, Void.class).getResultAsync().get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public void testUpdateInfo() throws ExecutionException, InterruptedException {
WorkflowUpdateException.class,
() ->
stub.startUpdate(updateOptionsBuilder.setUpdateId("reject").build(), 0, "")
.getResultAsync());

.getResult());
workflow.complete();
String result =
testWorkflowRule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ public void testUpdateUntyped() throws ExecutionException, InterruptedException
// send a bad update that will be rejected through the sync path
assertThrows(
WorkflowUpdateException.class,
() -> workflowStub.startUpdate("update", ACCEPTED, String.class, 0, "Bad Update"));
() ->
workflowStub
.startUpdate("update", ACCEPTED, String.class, 0, "Bad Update")
.getResult());

workflowStub.update("complete", void.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ public void failWhenUpdatedIsRejected() {
.build();

assertThrows(
WorkflowServiceException.class,
() -> WorkflowClient.updateWithStart(workflow::execute, updateOp));
WorkflowUpdateException.class,
() -> WorkflowClient.updateWithStart(workflow::execute, updateOp).getResult());
}

@Test
Expand Down
Loading