Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ to docs, or any other relevant information.
project-scoped custom roles, and caller rate limits.

### Fixed
- `WorkflowStub.getResult` now returns details passed to `terminate` on
`TerminatedFailure.getDetails()`. The details are retrieved and decoded with the workflow data
converter, so a payload codec or external payload storage still yields the original values.
- Autoscaling workflow pollers now prioritize meaningful sticky backlog up to the sticky pool's
target and use advertised slot capacity to balance sticky and non-sticky admission. Without an
advertised maximum, additional polls of one queue type wait until the other has an active poll.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ <R> CompletableFuture<R> getResultAsync(
* chance to perform cleanup.
*
* @param reason optional reason for the termination request
* @param details additional details about the termination reason
* @param details additional details about the termination request
* @throws WorkflowNotFoundException if the workflow execution doesn't exist or is already
* completed
* @throws WorkflowServiceException for all other failures including networking and service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
package io.temporal.failure;

import io.temporal.common.converter.DataConverter;
import io.temporal.common.converter.EncodedValues;
import io.temporal.common.converter.Values;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** <b>This exception is expected to be thrown only by the Temporal framework code.</b> */
public final class TerminatedFailure extends TemporalFailure {
private final Values details;

public TerminatedFailure(String message, @Nullable Throwable cause) {
this(message, new EncodedValues(), cause);
}

public TerminatedFailure(String message, Throwable cause) {
public TerminatedFailure(String message, @Nonnull Values details, @Nullable Throwable cause) {
super(message, message, cause);
this.details = Objects.requireNonNull(details);
}

/**
* Details passed to {@link io.temporal.client.WorkflowStub#terminate(String, Object...)}.
*
* <p>Empty when the workflow was terminated without details. The values are decoded with the
* workflow data converter, including a payload codec and external payload storage.
*/
public Values getDetails() {
return details;
}

@Override
public void setDataConverter(DataConverter converter) {
((EncodedValues) details).setDataConverter(converter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.grpc.StatusRuntimeException;
import io.temporal.api.common.v1.*;
import io.temporal.api.common.v1.Payloads;
import io.temporal.api.enums.v1.EventType;
import io.temporal.api.enums.v1.QueryRejectCondition;
import io.temporal.api.enums.v1.UpdateWorkflowExecutionLifecycleStage;
import io.temporal.api.enums.v1.WorkflowExecutionStatus;
Expand Down Expand Up @@ -515,15 +516,19 @@ public <R> GetResultAsyncOutput<R> getResultAsync(GetResultInput<R> input) {
private <R> R convertClosedWorkflowResult(
GetResultInput<R> input, GetWorkflowExecutionHistoryResponse closed) {
WorkflowExecution execution = input.getWorkflowExecution();
DataConverter dataConverter =
outcomeConverter(
closed.hasPropagatedNexusSerializationContext()
? closed.getPropagatedNexusSerializationContext()
: null,
execution,
workflowConverter(execution));
// The long poll helpers return a response whose single event is the close event.
HistoryEvent closeEvent = closed.getHistory().getEvents(0);
// terminate() encodes details with the workflow serialization context, even when a Nexus
// handler started the workflow and the server propagated a different context for the result.
DataConverter dataConverter =
closeEvent.getEventType() == EventType.EVENT_TYPE_WORKFLOW_EXECUTION_TERMINATED
? workflowConverter(execution)
: outcomeConverter(
closed.hasPropagatedNexusSerializationContext()
? closed.getPropagatedNexusSerializationContext()
: null,
execution,
workflowConverter(execution));
Optional<Payloads> resultValue =
WorkflowExecutionUtils.getResultFromCloseEvent(
execution, input.getWorkflowType(), closeEvent, dataConverter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ public static Optional<Payloads> getResultFromCloseEvent(
case EVENT_TYPE_WORKFLOW_EXECUTION_TERMINATED:
WorkflowExecutionTerminatedEventAttributes terminated =
closeEvent.getWorkflowExecutionTerminatedEventAttributes();
Optional<Payloads> terminationDetails =
terminated.hasDetails() ? Optional.of(terminated.getDetails()) : Optional.empty();
throw new WorkflowFailedException(
workflowExecution,
workflowType.orElse(null),
closeEvent.getEventType(),
-1,
RetryState.RETRY_STATE_NON_RETRYABLE_FAILURE,
new TerminatedFailure(terminated.getReason(), null));
new TerminatedFailure(
terminated.getReason(),
new EncodedValues(terminationDetails, dataConverter),
null));
case EVENT_TYPE_WORKFLOW_EXECUTION_TIMED_OUT:
WorkflowExecutionTimedOutEventAttributes timedOut =
closeEvent.getWorkflowExecutionTimedOutEventAttributes();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package io.temporal.workflow;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowClientOptions;
import io.temporal.client.WorkflowFailedException;
import io.temporal.client.WorkflowOptions;
import io.temporal.client.WorkflowStub;
import io.temporal.common.converter.CodecDataConverter;
import io.temporal.common.converter.CodecDataConverterTest;
import io.temporal.common.converter.DataConverter;
import io.temporal.common.converter.DefaultDataConverter;
import io.temporal.failure.TerminatedFailure;
import io.temporal.internal.payload.storage.TestStorageDriver;
import io.temporal.payload.storage.ExternalStorage;
import io.temporal.testing.CloudTestExclusion.NeedsCloudAdaptation;
import io.temporal.testing.CloudTestExclusionNote;
import io.temporal.testing.internal.SDKTestWorkflowRule;
import java.util.Collections;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

/**
* Termination details are encoded with the workflow data converter. Reading them back has to use
* that same converter, including a payload codec and external payload storage.
*/
@CloudTestExclusionNote(
"Cloud envconfig replaces the custom data converter and external storage used by this test.")
@Category(NeedsCloudAdaptation.class)
public class TerminatedWorkflowDetailsTest {

private static final int THRESHOLD = 64;

private static final String REASON = "shutdown";

/** Longer than the storage threshold so the encoded detail is offloaded. */
private static final String DETAIL = detail();

private static final TestStorageDriver driver = TestStorageDriver.named("terminate-details");

private static final DataConverter dataConverter =
new CodecDataConverter(
DefaultDataConverter.newDefaultInstance(),
Collections.singletonList(new CodecDataConverterTest.PrefixPayloadCodec()),
true);

@Rule
public SDKTestWorkflowRule testWorkflowRule =
SDKTestWorkflowRule.newBuilder()
.setWorkflowTypes(WaitingWorkflowImpl.class)
.setWorkflowClientOptions(
WorkflowClientOptions.newBuilder()
.setDataConverter(dataConverter)
.setExternalStorage(
ExternalStorage.newBuilder()
.setDriver(driver)
.setPayloadSizeThreshold(THRESHOLD)
.build())
.build())
.build();

@Before
public void resetDriver() {
driver.reset();
}

@Test
public void getResultExposesTerminationDetails() {
WorkflowStub stub = start();
stub.terminate(REASON, DETAIL);

WorkflowFailedException exception =
assertThrows(WorkflowFailedException.class, () -> stub.getResult(String.class));
assertTrue(exception.getCause() instanceof TerminatedFailure);
TerminatedFailure failure = (TerminatedFailure) exception.getCause();

assertEquals(REASON, failure.getOriginalMessage());
assertEquals(DETAIL, failure.getDetails().get(0, String.class));
assertTrue("the detail must be stored, not left inline", driver.stores.get() > 0);
assertTrue("getResult must retrieve the stored detail", driver.retrieves.get() > 0);
assertTrue("the stored payload must be the termination detail", driver.stored(DETAIL));
}

@Test
public void getResultHasNoDetailsWhenTerminateOmitsThem() {
WorkflowStub stub = start();
stub.terminate(REASON);

WorkflowFailedException exception =
assertThrows(WorkflowFailedException.class, () -> stub.getResult(String.class));
assertTrue(exception.getCause() instanceof TerminatedFailure);
TerminatedFailure failure = (TerminatedFailure) exception.getCause();

assertEquals(REASON, failure.getOriginalMessage());
assertEquals(0, failure.getDetails().getSize());
}

private WorkflowStub start() {
WaitingWorkflow workflow =
testWorkflowRule
.getWorkflowClient()
.newWorkflowStub(
WaitingWorkflow.class,
WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.getTaskQueue()).build());
WorkflowClient.start(workflow::execute);
return WorkflowStub.fromTyped(workflow);
}

private static String detail() {
StringBuilder detail = new StringBuilder("ticket-42-");
for (int i = 0; i < 180; i++) {
detail.append('x');
}
return detail.toString();
}

@WorkflowInterface
public interface WaitingWorkflow {
@WorkflowMethod
String execute();
}

public static class WaitingWorkflowImpl implements WaitingWorkflow {
@Override
public String execute() {
Workflow.await(() -> false);
return "done";
}
}
}