Skip to content
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
61 changes: 42 additions & 19 deletions src/workflow/engine/context/OrchestrationRuntimeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
newSendEventAction,
newEventRaisedEvent,
newFailureDetails,
type RetryTimerOrigin,
} from "../worker/protoHelpers";

export class OrchestrationRuntimeContext extends OrchestrationContext {
Expand Down Expand Up @@ -374,13 +375,18 @@ export class OrchestrationRuntimeContext extends OrchestrationContext {
const routing = this.getRoutingOptions(options);
if (options?.retryPolicy) {
let executionId: string | undefined;
return this.callWithRetry(options.retryPolicy, (id) => {
executionId = this.getTaskExecutionId(id, executionId);
const action = newScheduleTaskAction(id, name, encodedInput, executionId, routing);
this.configurePayloadCall(id, options);
this._pendingActions[action.id] = action;
return executionId;
});
return this.callWithRetry(
options.retryPolicy,
(id) => {
executionId = this.getTaskExecutionId(id, executionId);
const action = newScheduleTaskAction(id, name, encodedInput, executionId, routing);
this.configurePayloadCall(id, options);
this._pendingActions[action.id] = action;
return executionId;
},
// Histories from before execution IDs existed have nothing to correlate on.
() => (executionId ? { case: "activityRetry", taskExecutionId: executionId } : undefined),
);
}

const id = this.nextSequenceNumber();
Expand All @@ -404,17 +410,33 @@ export class OrchestrationRuntimeContext extends OrchestrationContext {
const encodedInput = input != null ? serializeWorkflowValue(input, this.serializer) : undefined;
const routing = this.getRoutingOptions(options);
if (options?.retryPolicy) {
const firstInstanceId = instanceId;
return this.callWithRetry(options.retryPolicy, (id, attempt) => {
const childInstanceId =
attempt === 1 && firstInstanceId
? firstInstanceId
: `${this._instanceId}:${id.toString(16).padStart(4, "0")}`;
const action = newCreateChildWorkflowAction(id, name, childInstanceId, encodedInput, routing);
this.configurePayloadCall(id, options);
this._pendingActions[action.id] = action;
return undefined;
});
const requestedInstanceId = instanceId;
// Every attempt is its own child instance. Attempts after the first, and
// the retry timers, point back at the first attempt's instance ID.
let firstInstanceId: string | undefined;
return this.callWithRetry(
options.retryPolicy,
(id, attempt) => {
const childInstanceId =
attempt === 1 && requestedInstanceId
? requestedInstanceId
: `${this._instanceId}:${id.toString(16).padStart(4, "0")}`;
const retryParentInstanceId = firstInstanceId;
firstInstanceId ??= childInstanceId;
const action = newCreateChildWorkflowAction(
id,
name,
childInstanceId,
encodedInput,
routing,
retryParentInstanceId,
);
this.configurePayloadCall(id, options);
this._pendingActions[action.id] = action;
return undefined;
},
() => (firstInstanceId ? { case: "childWorkflowRetry", instanceId: firstInstanceId } : undefined),
);
}

const id = this.nextSequenceNumber();
Expand Down Expand Up @@ -607,6 +629,7 @@ export class OrchestrationRuntimeContext extends OrchestrationContext {
private callWithRetry<T>(
retryPolicy: RetryPolicy,
scheduleAttempt: (id: number, attempt: number) => string | undefined,
retryTimerOrigin: () => RetryTimerOrigin | undefined,
): Task<T> {
const policy = validateRetryPolicy(retryPolicy);
const retryTask = new CompletableTask<T>();
Expand Down Expand Up @@ -635,7 +658,7 @@ export class OrchestrationRuntimeContext extends OrchestrationContext {

const timerId = this.nextSequenceNumber();
const fireAt = new Date(retryAt);
const timerAction = newCreateTimerAction(timerId, fireAt);
const timerAction = newCreateTimerAction(timerId, fireAt, retryTimerOrigin());
this._pendingActions[timerAction.id] = timerAction;
this._pendingTasks[timerId] = new RetryDelayTask(runAttempt);
},
Expand Down
43 changes: 32 additions & 11 deletions src/workflow/engine/worker/protoHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TaskFailureDetailsSchema,
WorkflowInstanceSchema,
TaskRouterSchema,
RetryParentInstanceInfoSchema,
} from "../../../proto/dapr/proto/durabletask/v1/orchestration_pb";
import type {
HistoryEvent,
Expand All @@ -45,6 +46,8 @@ import {
ExecutionResumedEventSchema,
WorkflowStartedEventSchema,
TimerOriginCreateTimerSchema,
TimerOriginActivityRetrySchema,
TimerOriginChildWorkflowRetrySchema,
} from "../../../proto/dapr/proto/durabletask/v1/history_events_pb";
import type { WorkflowAction } from "../../../proto/dapr/proto/durabletask/v1/orchestrator_actions_pb";
import {
Expand All @@ -57,6 +60,12 @@ import {
SendEventActionSchema,
} from "../../../proto/dapr/proto/durabletask/v1/orchestrator_actions_pb";

// What a retry delay timer is waiting to retry. The runtime persists it on the
// TimerCreated event so history readers can tie the timer to its retry chain.
export type RetryTimerOrigin =
| { case: "activityRetry"; taskExecutionId: string }
| { case: "childWorkflowRetry"; instanceId: string };

type RoutingOptions = Pick<ActivityOptions, "targetAppId" | "propagationScope"> & { sourceAppId?: string };

export function decodePropagatedHistory(history?: HistoryMessage): PropagatedHistoryView | undefined {
Expand Down Expand Up @@ -298,19 +307,27 @@ export function newScheduleTaskAction(
});
}

export function newCreateTimerAction(id: number, fireAt: Date): WorkflowAction {
export function newCreateTimerAction(id: number, fireAt: Date, retryOrigin?: RetryTimerOrigin): WorkflowAction {
const action = create(CreateTimerActionSchema, { fireAt: dateToTimestamp(fireAt) });
switch (retryOrigin?.case) {
case "activityRetry":
action.origin = {
case: "activityRetry",
value: create(TimerOriginActivityRetrySchema, { taskExecutionId: retryOrigin.taskExecutionId }),
};
break;
case "childWorkflowRetry":
action.origin = {
case: "childWorkflowRetry",
value: create(TimerOriginChildWorkflowRetrySchema, { instanceId: retryOrigin.instanceId }),
};
break;
default:
action.origin = { case: "createTimer", value: create(TimerOriginCreateTimerSchema, {}) };
}
return create(WorkflowActionSchema, {
id,
workflowActionType: {
case: "createTimer",
value: create(CreateTimerActionSchema, {
fireAt: dateToTimestamp(fireAt),
origin: {
case: "createTimer",
value: create(TimerOriginCreateTimerSchema, {}),
},
}),
},
workflowActionType: { case: "createTimer", value: action },
});
}

Expand All @@ -320,6 +337,7 @@ export function newCreateChildWorkflowAction(
instanceId: string,
encodedInput?: string,
options?: RoutingOptions,
retryParentInstanceId?: string,
): WorkflowAction {
return create(WorkflowActionSchema, {
id,
Expand All @@ -333,6 +351,9 @@ export function newCreateChildWorkflowAction(
instanceId,
input: encodedInput,
historyPropagationScope: options?.propagationScope || undefined,
retryParentInstanceInfo: retryParentInstanceId
? create(RetryParentInstanceInfoSchema, { instanceID: retryParentInstanceId })
: undefined,
}),
},
});
Expand Down
Loading
Loading