Skip to content
Draft
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<grpc.version>1.84.0</grpc.version>
<protobuf.version>3.25.5</protobuf.version>
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.19.0/dapr/proto</dapr.proto.baseurl>
<durabletask.proto.baseurl>https://raw.githubusercontent.com/dapr/durabletask-protobuf/v0.6.0/protos</durabletask.proto.baseurl>
<durabletask.proto.baseurl>https://raw.githubusercontent.com/ep0ll/dapr-durabletask-protobuf/203d5c77574523cebdbba7e63890eecf4560a165/protos</durabletask.proto.baseurl>
<dapr.sdk.version>1.20.0-SNAPSHOT</dapr.sdk.version>
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
<maven-dependency-plugin.version>3.8.1</maven-dependency-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,36 @@ public void raiseEvent(String workflowInstanceId, String eventName, Object event
*
* @return a page of workflow instance IDs
*/
/**
* Raises an exception for the oldest matching external-event wait in a workflow instance.
*
* @param workflowInstanceId the ID of the workflow instance that will handle the error
* @param eventName the case-insensitive name of the external event
* @param exception the exception whose structured details are delivered to the workflow
*/
public void raiseError(String workflowInstanceId, String eventName, Exception exception) {
this.innerClient.raiseError(workflowInstanceId, eventName, exception);
}

/**
* Raises an exception for an external-event wait on a workflow instance owned by another app.
*
* <p>Permission to operate on workflows owned by another app is governed by the target app's
* WorkflowAccessPolicy.</p>
*
* @param workflowInstanceId the ID of the workflow instance that will handle the error
* @param eventName the case-insensitive name of the external event
* @param exception the exception whose structured details are delivered to the workflow
* @param appId the ID of the app that owns the target workflow instance, or null for the local app
*/
public void raiseError(
String workflowInstanceId,
String eventName,
Exception exception,
@Nullable String appId) {
this.innerClient.raiseError(workflowInstanceId, eventName, exception, appId);
}

public WorkflowInstancePage listInstanceIds() {
return this.listInstanceIds(null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,36 @@ public void raiseEvent(
* a default instance if no such instance is found. Please refer to method
* {@link OrchestrationMetadata#isInstanceFound()} to check if an instance is found.
*/
/**
* Raises an error for the oldest matching external-event wait in a workflow instance.
*
* <p>The matching {@link WorkflowContext#waitForExternalEvent(String)} task fails with a
* {@link io.dapr.workflows.task.exception.TaskFailedException}. If the workflow has not started waiting
* yet, the failure is buffered and delivered when the matching wait is registered.</p>
*
* @param instanceId the ID of the orchestration instance
* @param eventName the case-insensitive name of the external event
* @param exception the exception whose structured details are delivered to the workflow
*/
public abstract void raiseError(String instanceId, String eventName, Exception exception);

/**
* Raises an error for an external-event wait on a workflow instance owned by another app.
*
* @param instanceId the ID of the orchestration instance
* @param eventName the case-insensitive name of the external event
* @param exception the exception whose structured details are delivered to the workflow
* @param appID the ID of the app that owns the target workflow instance, or null for the local app
*/
public void raiseError(
String instanceId,
String eventName,
Exception exception,
@Nullable String appID) {
requireLocalRouting(appID);
this.raiseError(instanceId, eventName, exception);
}

@Nullable
public abstract OrchestrationMetadata getInstanceMetadata(String instanceId, boolean getInputsAndOutputs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.dapr.durabletask.implementation.protobuf.TaskHubSidecarServiceGrpc;
import io.dapr.workflows.client.WorkflowRuntimeStatus;
import io.dapr.workflows.task.internal.Helpers;
import io.dapr.workflows.task.exception.WorkflowFailureDetails;
import io.dapr.workflows.task.serialization.DataConverter;
import io.dapr.workflows.task.serialization.JacksonDataConverter;
import io.grpc.Channel;
Expand Down Expand Up @@ -325,6 +326,29 @@ public void raiseEvent(String instanceId, String eventName, Object eventPayload,

}

@Override
public void raiseError(String instanceId, String eventName, Exception exception) {
this.raiseError(instanceId, eventName, exception, null);
}

@Override
public void raiseError(String instanceId, String eventName, Exception exception, @Nullable String appID) {
Helpers.throwIfArgumentNull(instanceId, "instanceId");
Helpers.throwIfArgumentNull(eventName, "eventName");
Helpers.throwIfArgumentNull(exception, "exception");

OrchestratorService.RaiseEventRequest.Builder builder = OrchestratorService.RaiseEventRequest.newBuilder()
.setInstanceId(instanceId)
.setName(eventName)
.setFailureDetails(new WorkflowFailureDetails(exception).toProto());

if (hasAppID(appID)) {
builder.setRouter(buildTaskRouter(appID));
}

this.sidecarClient.raiseEvent(builder.build());
}

@Override
public OrchestrationMetadata getInstanceMetadata(String instanceId, boolean getInputsAndOutputs) {
return this.getInstanceMetadata(instanceId, getInputsAndOutputs, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
import io.dapr.workflows.task.Task;

/**
* Exception that gets thrown when awaiting a {@link Task} for an activity or sub-orchestration that fails with an
* unhandled exception.
* Exception thrown when awaiting a workflow task that failed, including an activity, sub-orchestration,
* or external-event wait failed through {@code RaiseError}.
*
* <p>Detailed information associated with a particular task failure can be retrieved
* using the {@link #getErrorDetails()} method.</p>
* using the {@link #getErrorDetails()} method. For externally raised failures, the details include
* the original error type, message, stack trace, and any nested cause supplied by the caller.</p>
*/
public class TaskFailedException extends RuntimeException {
private final WorkflowFailureDetails details;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

/**
* Class that represents the details of a task failure.
Expand Down Expand Up @@ -81,6 +83,7 @@ private static Map<String, String> legacyErrorTypes() {
private final String errorMessage;
private final String stackTrace;
private final boolean isNonRetriable;
@Nullable private final WorkflowFailureDetails innerFailure;

/**
* Creates failure details from their individual parts.
Expand All @@ -98,16 +101,37 @@ public WorkflowFailureDetails(
@Nullable String errorMessage,
@Nullable String errorDetails,
boolean isNonRetriable) {
this(errorType, errorMessage, errorDetails, isNonRetriable, null);
}

public WorkflowFailureDetails(
String errorType,
@Nullable String errorMessage,
@Nullable String errorDetails,
boolean isNonRetriable,
@Nullable WorkflowFailureDetails innerFailure) {
this.errorType = errorType;
this.stackTrace = errorDetails;

// Error message can be null for things like NullPointerException but the gRPC contract doesn't allow null
this.errorMessage = errorMessage != null ? errorMessage : "";
this.isNonRetriable = isNonRetriable;
this.innerFailure = innerFailure;
}

public WorkflowFailureDetails(Exception exception) {
this(exception.getClass().getName(), exception.getMessage(), getFullStackTrace(exception), false);
this(exception, Collections.newSetFromMap(new IdentityHashMap<>()));
}

private WorkflowFailureDetails(Exception exception, Set<Throwable> seen) {
this(
exception.getClass().getName(),
exception.getMessage(),
getFullStackTrace(exception),
false,
exception.getCause() == null || !seen.add(exception)
? null
: new WorkflowFailureDetails(exception.getCause(), seen));
}

/**
Expand All @@ -121,8 +145,9 @@ public WorkflowFailureDetails(Exception exception) {
public WorkflowFailureDetails(TaskFailureDetails proto) {
this(proto.getErrorType(),
proto.getErrorMessage(),
proto.getStackTrace().getValue(),
proto.getIsNonRetriable());
proto.hasStackTrace() ? proto.getStackTrace().getValue() : null,
proto.getIsNonRetriable(),
proto.hasInnerFailure() ? new WorkflowFailureDetails(proto.getInnerFailure()) : null);
}

/**
Expand Down Expand Up @@ -167,6 +192,16 @@ public boolean isNonRetriable() {
return this.isNonRetriable;
}

/**
* Gets the underlying failure that caused this failure, if one was provided.
*
* @return the nested failure details, or null when this is the root failure
*/
@Nullable
public WorkflowFailureDetails getInnerFailure() {
return this.innerFailure;
}

/**
* Returns {@code true} if the task failure was provided by the specified exception type, otherwise {@code false}.
*
Expand All @@ -188,15 +223,16 @@ public boolean isCausedBy(Class<? extends Exception> exceptionClass) {
// Try using reflection to load the failure's class type and see if it's a subtype of the specified
// exception. For example, this should always succeed if exceptionClass is System.Exception.
Class<?> actualExceptionClass = Class.forName(resolvedClassName);
return exceptionClass.isAssignableFrom(actualExceptionClass);
if (exceptionClass.isAssignableFrom(actualExceptionClass)) {
return true;
}
} catch (ClassNotFoundException ex) {
// Can't load the class and thus can't tell if it's related. Say so rather than failing silently:
// an unloadable error type is usually history written by a different application or SDK version,
// and a quiet false here is indistinguishable from a genuine "not caused by".
// The failure may have originated in another application or SDK version.
LOGGER.warn("Cannot determine whether failure type '{}' is a {}: the class is not on the "
+ "classpath, so isCausedBy is answering false.", actualClassName, exceptionClass.getName());
return false;
+ "classpath; checking any nested cause instead.", actualClassName, exceptionClass.getName());
}

return this.innerFailure != null && this.innerFailure.isCausedBy(exceptionClass);
}

/**
Expand Down Expand Up @@ -225,10 +261,14 @@ public static String getFullStackTrace(Throwable e) {
* @return the protobuf failure details.
*/
public TaskFailureDetails toProto() {
return TaskFailureDetails.newBuilder()
TaskFailureDetails.Builder builder = TaskFailureDetails.newBuilder()
.setErrorType(this.getErrorType())
.setErrorMessage(this.getErrorMessage())
.setStackTrace(StringValue.of(this.getStackTrace() != null ? this.getStackTrace() : ""))
.build();
.setIsNonRetriable(this.isNonRetriable());
if (this.innerFailure != null) {
builder.setInnerFailure(this.innerFailure.toProto());
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,16 @@ public <V> Task<V> waitForExternalEvent(String name, Duration timeout, Class<V>
for (HistoryEvents.HistoryEvent e : this.unprocessedEvents) {
HistoryEvents.EventRaisedEvent existing = e.getEventRaised();
if (name.equalsIgnoreCase(existing.getName())) {
String rawEventData = existing.getInput().getValue();
V data = this.dataConverter.deserialize(rawEventData, dataType);
eventTask.complete(data);
if (existing.hasFailureDetails()) {
eventTask.completeExceptionally(new TaskFailedException(
name,
id,
new WorkflowFailureDetails(existing.getFailureDetails())));
} else {
String rawEventData = existing.hasInput() ? existing.getInput().getValue() : null;
V data = this.dataConverter.deserialize(rawEventData, dataType);
eventTask.complete(data);
}
this.unprocessedEvents.remove(e);
return eventTask;
}
Expand Down Expand Up @@ -835,21 +842,31 @@ private void handleTaskFailed(HistoryEvents.HistoryEvent e) {
private void handleEventRaised(HistoryEvents.HistoryEvent e) {
HistoryEvents.EventRaisedEvent eventRaised = e.getEventRaised();
String eventName = eventRaised.getName();
String normalizedEventName = eventName.toLowerCase(Locale.ROOT);

Queue<TaskRecord<?>> outstandingEventQueue = this.outstandingEvents.get(eventName);
Queue<TaskRecord<?>> outstandingEventQueue = this.outstandingEvents.get(normalizedEventName);
if (outstandingEventQueue == null) {
// No code is waiting for this event. Buffer it in case user-code waits for it later.
this.unprocessedEvents.add(e);
return;
}

// Signal the first waiter in the queue with this event payload.
// Signal the first waiter in the queue with this event payload or failure.
TaskRecord<?> matchingTaskRecord = outstandingEventQueue.remove();
if (outstandingEventQueue.isEmpty()) {
this.outstandingEvents.remove(eventName);
this.outstandingEvents.remove(normalizedEventName);
}
String rawResult = eventRaised.getInput().getValue();
CompletableTask task = matchingTaskRecord.getTask();
if (eventRaised.hasFailureDetails()) {
ExternalEventTask<?> externalEventTask = (ExternalEventTask<?>) task;
task.completeExceptionally(new TaskFailedException(
eventName,
externalEventTask.taskId,
new WorkflowFailureDetails(eventRaised.getFailureDetails())));
return;
}

String rawResult = eventRaised.hasInput() ? eventRaised.getInput().getValue() : null;
try {
Object result = this.dataConverter.deserialize(
rawResult,
Expand Down
Loading