| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Linq.Expressions; |
| | | 4 | | |
| | | 5 | | namespace AsyncResponse; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Runtime <see cref="IDurableFlowContext"/> bound to one execution of one flow run. Owns the |
| | | 9 | | /// checkpointed-flow mechanics so flow code doesn't have to: step guards, result memoization, the |
| | | 10 | | /// pending-correlation-id breadcrumb, fresh-start vs re-attach, and the durable resume/failure |
| | | 11 | | /// callbacks that point back at the flow executor. |
| | | 12 | | /// <para> |
| | | 13 | | /// Not thread-safe by design: a flow body runs sequentially, and <c>until</c> predicates run on |
| | | 14 | | /// the channel's dispatch path only while the flow itself is parked awaiting that same step. |
| | | 15 | | /// </para> |
| | | 16 | | /// </summary> |
| | | 17 | | internal sealed class DurableFlowContext : IDurableFlowContext |
| | | 18 | | { |
| | | 19 | | private readonly FlowState _state; |
| | | 20 | | private readonly IFlowStateStore _store; |
| | | 21 | | private readonly IAsyncResponseBuilder _builder; |
| | | 22 | | private readonly AsyncResponseContextPropagation _propagation; |
| | | 23 | | private readonly DurableFlowOptions _options; |
| | | 24 | | private readonly IAsyncResponseSubscriber _subscriber; |
| | | 25 | | private readonly IRecoverableAsyncResponseSubscriber? _recoverableSubscriber; |
| | | 26 | | private readonly ILogger _logger; |
| | | 27 | | private readonly FlowExecutionLease _lease; |
| | | 28 | | private bool _suspended; |
| | | 29 | | private bool _progressDirty; |
| | | 30 | | private DateTime _lastPersistenceUtc; |
| | | 31 | | |
| | | 32 | | /// <summary>Creates the context for one execution of the given run.</summary> |
| | 3 | 33 | | public DurableFlowContext( |
| | 3 | 34 | | FlowState state, |
| | 3 | 35 | | IFlowStateStore store, |
| | 3 | 36 | | IAsyncResponseBuilder builder, |
| | 3 | 37 | | AsyncResponseContextPropagation propagation, |
| | 3 | 38 | | DurableFlowOptions options, |
| | 3 | 39 | | IAsyncResponseSubscriber subscriber, |
| | 3 | 40 | | IRecoverableAsyncResponseSubscriber? recoverableSubscriber, |
| | 3 | 41 | | ILogger logger, |
| | 3 | 42 | | FlowExecutionLease lease) |
| | | 43 | | { |
| | 3 | 44 | | _state = state; |
| | 3 | 45 | | _store = store; |
| | 3 | 46 | | _builder = builder; |
| | 3 | 47 | | _propagation = propagation; |
| | 3 | 48 | | _options = options; |
| | 3 | 49 | | _subscriber = subscriber; |
| | 3 | 50 | | _recoverableSubscriber = recoverableSubscriber; |
| | 3 | 51 | | _logger = logger; |
| | 3 | 52 | | _lease = lease; |
| | 3 | 53 | | } |
| | | 54 | | |
| | 3 | 55 | | internal bool IsSuspended => _suspended; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 3 | 58 | | public string FlowId => _state.FlowId!; |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public async Task StepAsync(string name, Func<Task> step, CancellationToken cancellationToken = default) |
| | | 62 | | { |
| | 3 | 63 | | ThrowIfSuspended(); |
| | 3 | 64 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 3 | 65 | | ArgumentNullException.ThrowIfNull(step); |
| | | 66 | | |
| | 3 | 67 | | var checkpoint = GetStep(name); |
| | 3 | 68 | | if (checkpoint.Completed) |
| | 2 | 69 | | return; |
| | | 70 | | |
| | 3 | 71 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 3 | 72 | | await step().ConfigureAwait(false); |
| | 3 | 73 | | _lease.ThrowIfLost(); |
| | 3 | 74 | | await CompleteStepAsync(name, checkpoint, resultJson: null, cancellationToken).ConfigureAwait(false); |
| | 3 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public async Task<TResult> StepAsync<TResult>(string name, Func<Task<TResult>> step, CancellationToken cancellationT |
| | | 79 | | { |
| | 3 | 80 | | ThrowIfSuspended(); |
| | 3 | 81 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 3 | 82 | | ArgumentNullException.ThrowIfNull(step); |
| | | 83 | | |
| | 3 | 84 | | var checkpoint = GetStep(name); |
| | 3 | 85 | | if (checkpoint.Completed) |
| | 2 | 86 | | return DeserializeResult<TResult>(checkpoint.ResultJson); |
| | | 87 | | |
| | 3 | 88 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 3 | 89 | | var result = await step().ConfigureAwait(false); |
| | 3 | 90 | | _lease.ThrowIfLost(); |
| | 3 | 91 | | await CompleteStepAsync(name, checkpoint, AsyncResponseJson.Serialize(result), cancellationToken).ConfigureAwait |
| | 3 | 92 | | return result; |
| | 3 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <inheritdoc /> |
| | | 96 | | public Task<TResponse> AwaitStepAsync<TResponse>( |
| | | 97 | | string name, |
| | | 98 | | Func<string, Task> trigger, |
| | | 99 | | TimeSpan? timeout = null, |
| | | 100 | | CancellationToken cancellationToken = default) where TResponse : IAsyncResponsePayload |
| | 2 | 101 | | => AwaitStepCoreAsync<TResponse>(name, trigger, until: null, timeout, cancellationToken); |
| | | 102 | | |
| | | 103 | | /// <inheritdoc /> |
| | | 104 | | public Task<TResponse> AwaitStepAsync<TResponse>( |
| | | 105 | | string name, |
| | | 106 | | Func<string, Task> trigger, |
| | | 107 | | Func<TResponse, bool> until, |
| | | 108 | | TimeSpan? timeout = null, |
| | | 109 | | CancellationToken cancellationToken = default) where TResponse : IAsyncResponsePayload |
| | | 110 | | { |
| | 3 | 111 | | ArgumentNullException.ThrowIfNull(until); |
| | 3 | 112 | | return AwaitStepCoreAsync<TResponse>(name, trigger, payload => new ValueTask<bool>(until(payload)), timeout, can |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc /> |
| | | 116 | | public Task<TResponse> AwaitStepAsync<TResponse>( |
| | | 117 | | string name, |
| | | 118 | | Func<string, Task> trigger, |
| | | 119 | | Func<TResponse, Task<bool>> until, |
| | | 120 | | TimeSpan? timeout = null, |
| | | 121 | | CancellationToken cancellationToken = default) where TResponse : IAsyncResponsePayload |
| | | 122 | | { |
| | 3 | 123 | | ArgumentNullException.ThrowIfNull(until); |
| | 3 | 124 | | return AwaitStepCoreAsync<TResponse>(name, trigger, payload => new ValueTask<bool>(until(payload)), timeout, can |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <inheritdoc /> |
| | | 128 | | public Task ReportProgressAsync(string message, CancellationToken cancellationToken = default) |
| | | 129 | | { |
| | 3 | 130 | | ThrowIfSuspended(); |
| | 3 | 131 | | _state.LastMessage = message; |
| | 3 | 132 | | var now = DateTime.UtcNow; |
| | 3 | 133 | | if (_options.ProgressPersistenceInterval <= TimeSpan.Zero |
| | 3 | 134 | | || now - _lastPersistenceUtc >= _options.ProgressPersistenceInterval) |
| | 2 | 135 | | return SaveAsync(cancellationToken); |
| | | 136 | | |
| | 3 | 137 | | _progressDirty = true; |
| | 3 | 138 | | return Task.CompletedTask; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <inheritdoc /> |
| | | 142 | | public TValue? GetValue<TValue>(string key) |
| | | 143 | | { |
| | 2 | 144 | | ThrowIfSuspended(); |
| | 2 | 145 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | 2 | 146 | | return _state.Values is not null && _state.Values.TryGetValue(key, out var json) |
| | 2 | 147 | | ? JsonSafety.SafeDeserialize<TValue>(json) |
| | 2 | 148 | | : default; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <inheritdoc /> |
| | | 152 | | public Task SetValueAsync<TValue>(string key, TValue value, CancellationToken cancellationToken = default) |
| | | 153 | | { |
| | 3 | 154 | | ThrowIfSuspended(); |
| | 3 | 155 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | 3 | 156 | | var values = _state.Values ??= new Dictionary<string, string>(StringComparer.Ordinal); |
| | 3 | 157 | | values[key] = AsyncResponseJson.Serialize(value); |
| | 3 | 158 | | return SaveAsync(cancellationToken); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <inheritdoc /> |
| | | 162 | | public async Task<FlowState> AwaitChildFlowAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicCo |
| | | 163 | | string name, |
| | | 164 | | TInput input, |
| | | 165 | | string? flowId = null, |
| | | 166 | | bool failOnChildFailure = true, |
| | | 167 | | CancellationToken cancellationToken = default) |
| | | 168 | | where TFlow : class, IDurableFlow<TInput> |
| | | 169 | | { |
| | 2 | 170 | | ThrowIfSuspended(); |
| | 2 | 171 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 2 | 172 | | ArgumentNullException.ThrowIfNull(input); |
| | 2 | 173 | | if (flowId is not null) |
| | 2 | 174 | | ArgumentException.ThrowIfNullOrWhiteSpace(flowId); |
| | | 175 | | |
| | 2 | 176 | | var checkpoint = GetStep(name); |
| | 2 | 177 | | var requestedChildFlowId = flowId ?? $"{FlowId}:{name}"; |
| | 2 | 178 | | var breadcrumb = checkpoint.ChildFlowId; |
| | 2 | 179 | | if (breadcrumb is not null && !string.Equals(breadcrumb, requestedChildFlowId, StringComparison.Ordinal)) |
| | | 180 | | { |
| | 2 | 181 | | throw new DurableFlowFailedException( |
| | 2 | 182 | | $"Step '{name}' of flow '{FlowId}' is already bound to child flow id '{breadcrumb}', " + |
| | 2 | 183 | | $"but this execution requested '{requestedChildFlowId}'. A durable step must keep the same child id on e |
| | | 184 | | } |
| | | 185 | | |
| | 2 | 186 | | var childFlowId = breadcrumb ?? requestedChildFlowId; |
| | 2 | 187 | | var inputJson = AsyncResponseJson.Serialize(input); |
| | 2 | 188 | | if (checkpoint.Completed) |
| | | 189 | | { |
| | 2 | 190 | | var completedChild = DeserializeResult<FlowState>(checkpoint.ResultJson) |
| | 2 | 191 | | ?? throw new DurableFlowFailedException( |
| | 2 | 192 | | $"Completed child step '{name}' of flow '{FlowId}' has no child-state snapshot."); |
| | 2 | 193 | | ThrowIfChildMismatched<TFlow, TInput>(completedChild, childFlowId, name, inputJson); |
| | 0 | 194 | | ThrowIfChildFailed(completedChild, failOnChildFailure); |
| | 0 | 195 | | return completedChild; |
| | | 196 | | } |
| | | 197 | | |
| | 2 | 198 | | var child = await _store.LoadAsync(childFlowId, cancellationToken).ConfigureAwait(false); |
| | 2 | 199 | | if (child is null) |
| | | 200 | | { |
| | 2 | 201 | | if (breadcrumb is not null) |
| | | 202 | | { |
| | | 203 | | // The breadcrumb is persisted only after the child state exists, so a missing child |
| | | 204 | | // here means its ledger expired (StateExpiry) or was deleted while this parent was |
| | | 205 | | // suspended. Its outcome is unknowable; re-running it blind would re-execute side |
| | | 206 | | // effects of a possibly-completed run. Fail deterministically instead. |
| | 2 | 207 | | throw new DurableFlowFailedException( |
| | 2 | 208 | | $"Child flow '{childFlowId}' has no state (expired or deleted) while parent flow '{FlowId}' was wait |
| | 2 | 209 | | "Its outcome is unknown, so it is not re-run automatically. Size DurableFlowOptions.StateExpiry beyo |
| | 2 | 210 | | "or start a new parent run to re-execute the work."); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | // Create the child BEFORE persisting the breadcrumb: "breadcrumb exists" must always |
| | | 214 | | // imply "child state existed", which keeps the expired-child check above sound. A crash |
| | | 215 | | // between the two writes is safe — the child id is deterministic, so the re-delivered |
| | | 216 | | // parent execution loads this child instead of re-creating it. |
| | 2 | 217 | | child = CreateChildState<TFlow, TInput>(childFlowId, name, inputJson); |
| | 2 | 218 | | if (await FlowStateConcurrency.TryCreateAsync( |
| | 2 | 219 | | _store, |
| | 2 | 220 | | childFlowId, |
| | 2 | 221 | | child, |
| | 2 | 222 | | _options.StateExpiry, |
| | 2 | 223 | | cancellationToken).ConfigureAwait(false)) |
| | | 224 | | { |
| | 2 | 225 | | _logger.LogDebug("Flow {FlowId} started child flow {ChildFlowId} for step '{Step}'.", FlowId, childFlowI |
| | | 226 | | } |
| | | 227 | | else |
| | | 228 | | { |
| | 0 | 229 | | child = await _store.LoadAsync(childFlowId, cancellationToken).ConfigureAwait(false) |
| | 0 | 230 | | ?? throw new InvalidOperationException($"Child flow '{childFlowId}' was created concurrently but cou |
| | 0 | 231 | | ThrowIfChildMismatched<TFlow, TInput>(child, childFlowId, name, inputJson); |
| | | 232 | | } |
| | | 233 | | } |
| | | 234 | | else |
| | | 235 | | { |
| | 2 | 236 | | ThrowIfChildMismatched<TFlow, TInput>(child, childFlowId, name, inputJson); |
| | | 237 | | } |
| | | 238 | | |
| | 2 | 239 | | if (breadcrumb is null) |
| | | 240 | | { |
| | 2 | 241 | | checkpoint.ChildFlowId = childFlowId; |
| | 2 | 242 | | checkpoint.Faulted = false; |
| | 2 | 243 | | checkpoint.Message = $"Waiting for child flow '{childFlowId}'."; |
| | 2 | 244 | | await SaveAsync(cancellationToken).ConfigureAwait(false); |
| | | 245 | | } |
| | | 246 | | |
| | 2 | 247 | | switch (child.Status) |
| | | 248 | | { |
| | | 249 | | case FlowRunStatus.Succeeded: |
| | 2 | 250 | | await CompleteStepAsync(name, checkpoint, FlowStateJson.SerializeSnapshot(child), cancellationToken).Con |
| | 2 | 251 | | return child; |
| | | 252 | | |
| | | 253 | | case FlowRunStatus.Failed: |
| | 2 | 254 | | checkpoint.Message = child.LastMessage; |
| | 2 | 255 | | await CompleteStepAsync(name, checkpoint, FlowStateJson.SerializeSnapshot(child), cancellationToken, fau |
| | 2 | 256 | | ThrowIfChildFailed(child, failOnChildFailure); |
| | 2 | 257 | | return child; |
| | | 258 | | |
| | | 259 | | default: |
| | 2 | 260 | | await SuspendForChildAsync(childFlowId, cancellationToken).ConfigureAwait(false); |
| | 0 | 261 | | throw new InvalidOperationException("Unreachable."); |
| | | 262 | | } |
| | 2 | 263 | | } |
| | | 264 | | |
| | | 265 | | private async Task<TResponse> AwaitStepCoreAsync<TResponse>( |
| | | 266 | | string name, |
| | | 267 | | Func<string, Task> trigger, |
| | | 268 | | Func<TResponse, ValueTask<bool>>? until, |
| | | 269 | | TimeSpan? timeout, |
| | | 270 | | CancellationToken cancellationToken) where TResponse : IAsyncResponsePayload |
| | | 271 | | { |
| | 3 | 272 | | ThrowIfSuspended(); |
| | 3 | 273 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 3 | 274 | | ArgumentNullException.ThrowIfNull(trigger); |
| | | 275 | | |
| | 3 | 276 | | var checkpoint = GetStep(name); |
| | 3 | 277 | | if (checkpoint.Completed) |
| | 2 | 278 | | return DeserializeResult<TResponse>(checkpoint.ResultJson); |
| | | 279 | | |
| | | 280 | | // Re-attach when a previous execution already triggered this step and died waiting; start |
| | | 281 | | // fresh when there is no breadcrumb or the last attempt faulted (steps are idempotent). |
| | 3 | 282 | | var reattach = checkpoint.PendingCorrelationId is not null && !checkpoint.Faulted; |
| | 3 | 283 | | var correlationId = reattach |
| | 3 | 284 | | ? checkpoint.PendingCorrelationId! |
| | 3 | 285 | | : AsyncResponseContext.GenerateCorrelationId(); |
| | 3 | 286 | | var stepTimeout = timeout ?? _options.DefaultStepTimeout; |
| | | 287 | | |
| | 3 | 288 | | var waiter = await CreateWaiterAsync(correlationId, until, stepTimeout, name).ConfigureAwait(false); |
| | 3 | 289 | | var triggerCompleted = reattach; |
| | | 290 | | try |
| | | 291 | | { |
| | 3 | 292 | | if (!reattach) |
| | | 293 | | { |
| | | 294 | | // Persist the breadcrumb AFTER the registration exists and BEFORE the send: |
| | | 295 | | // "breadcrumb persisted" therefore implies "someone is listening", so a crash on |
| | | 296 | | // either side of the send re-attaches (or times out and restarts the idempotent |
| | | 297 | | // step) — never a lost run, never a double-send. |
| | 3 | 298 | | checkpoint.PendingCorrelationId = correlationId; |
| | 3 | 299 | | checkpoint.Faulted = false; |
| | 3 | 300 | | checkpoint.Message = null; |
| | 3 | 301 | | await SaveAsync(cancellationToken).ConfigureAwait(false); |
| | | 302 | | |
| | 3 | 303 | | await trigger(correlationId).ConfigureAwait(false); |
| | 3 | 304 | | triggerCompleted = true; |
| | | 305 | | } |
| | 2 | 306 | | else if (_logger.IsEnabled(LogLevel.Debug)) |
| | | 307 | | { |
| | 0 | 308 | | _logger.LogDebug( |
| | 0 | 309 | | "Flow {FlowId} step '{Step}' re-attaching to in-flight correlationId {CorrelationId}.", |
| | 0 | 310 | | FlowId, name, correlationId); |
| | | 311 | | } |
| | | 312 | | |
| | 3 | 313 | | var response = await WaitForResponseAsync(waiter.ResponseTask, cancellationToken).ConfigureAwait(false); |
| | | 314 | | |
| | 3 | 315 | | checkpoint.PendingCorrelationId = null; |
| | | 316 | | // Deliberately NOT the caller's token: once the response is claimed from the channel |
| | | 317 | | // it exists nowhere else, so the completion checkpoint must not be interruptible — a |
| | | 318 | | // cancellation here used to leave `pending` set with the response already consumed, |
| | | 319 | | // and the redelivered execution re-attached to a correlation id nothing could answer. |
| | 3 | 320 | | await CompleteStepAsync(name, checkpoint, AsyncResponseJson.Serialize(response), CancellationToken.None).Con |
| | 3 | 321 | | return response; |
| | | 322 | | } |
| | 2 | 323 | | catch (OperationCanceledException ex) when (triggerCompleted) |
| | | 324 | | { |
| | | 325 | | // A lost lease surfaces as cancellation of the linked wait; convert it with the wait |
| | | 326 | | // failure attached so the takeover signal does not discard the real cause. |
| | 2 | 327 | | _lease.ThrowIfLost(ex); |
| | | 328 | | |
| | | 329 | | // SETTLE the handoff before deciding. A point-in-time IsCompletedSuccessfully check |
| | | 330 | | // raced the channel's dispatch: the response could win the task a moment after the |
| | | 331 | | // check, leaving a consumed response behind a still-pending ledger. Disposing the |
| | | 332 | | // waiter cancels its response task unless something already completed it (the channel |
| | | 333 | | // contract since the dispose-cancels fix), so after this await the task is TERMINAL |
| | | 334 | | // and the decision below is the race's single authoritative outcome. The finally's |
| | | 335 | | // second dispose is a no-op behind the subscription's cleanup latch. |
| | 2 | 336 | | await waiter.DisposeAsync().ConfigureAwait(false); |
| | | 337 | | |
| | 2 | 338 | | if (waiter.ResponseTask.IsCompletedSuccessfully) |
| | | 339 | | { |
| | | 340 | | // Delivery won the settlement: the channel claimed and acked that message — it |
| | | 341 | | // exists nowhere else, and re-attaching to its consumed correlation id would park |
| | | 342 | | // the run until the step timeout. The checkpoint therefore wins over the |
| | | 343 | | // cancellation: persist the received payload and return it; the caller's token |
| | | 344 | | // gets its say again at the next step boundary. |
| | 2 | 345 | | var received = waiter.ResponseTask.Result; |
| | 2 | 346 | | checkpoint.PendingCorrelationId = null; |
| | 2 | 347 | | await CompleteStepAsync(name, checkpoint, AsyncResponseJson.Serialize(received), CancellationToken.None) |
| | 2 | 348 | | return received; |
| | | 349 | | } |
| | | 350 | | |
| | 2 | 351 | | if (waiter.ResponseTask.IsFaulted) |
| | | 352 | | { |
| | | 353 | | // The wait FAULTED — a throwing Until predicate (possibly between the catch |
| | | 354 | | // filter and the settlement), or the disposal drain abandoning a wedged delivery |
| | | 355 | | // as AsyncResponseIndeterminateDeliveryException. Either way the message may be |
| | | 356 | | // consumed: restart the idempotent step fresh, exactly like the general fault |
| | | 357 | | // path below. The checkpoint records the fault's own message (not the |
| | | 358 | | // cancellation's) so the ledger says WHY the step restarts. |
| | 2 | 359 | | var fault = waiter.ResponseTask.Exception?.GetBaseException(); |
| | 2 | 360 | | checkpoint.Faulted = true; |
| | 2 | 361 | | checkpoint.Message = fault?.Message ?? ex.Message; |
| | 2 | 362 | | await SaveAsync(CancellationToken.None, cause: fault ?? ex).ConfigureAwait(false); |
| | 2 | 363 | | throw; |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | // Cancellation won the settlement (the task is now canceled; nothing was delivered). |
| | | 367 | | // WAIT-SIDE cancellation is infrastructure, not a step verdict: the channel cancels |
| | | 368 | | // in-flight waiters when it is disposed at host shutdown, and the caller's token |
| | | 369 | | // means "stop this execution", not "the step failed" — the remote operation is still |
| | | 370 | | // in flight. The persisted breadcrumb must survive untouched so the redelivered |
| | | 371 | | // execution RE-ATTACHES to the same correlation id; marking the checkpoint faulted |
| | | 372 | | // here turned every graceful shutdown mid-await into a fresh-correlation restart that |
| | | 373 | | // re-sent the remote request. (A response that never arrives still faults via the |
| | | 374 | | // step timeout.) |
| | | 375 | | // |
| | | 376 | | // The filter keeps this branch away from TRIGGER-thrown cancellation (an HttpClient |
| | | 377 | | // timeout surfaces as TaskCanceledException): the request may never have left the |
| | | 378 | | // process, so that case falls through to the fault path below and restarts fresh. |
| | 2 | 379 | | throw; |
| | 0 | 380 | | } |
| | 2 | 381 | | catch (Exception ex) |
| | | 382 | | { |
| | | 383 | | // Timeout, trigger failure (including trigger-thrown cancellation), or a faulted |
| | | 384 | | // wait: record it so the next execution restarts this step fresh instead of |
| | | 385 | | // re-attaching to a dead correlation id. The original failure rides along as `cause` |
| | | 386 | | // so a rejected save cannot displace it. |
| | 2 | 387 | | checkpoint.Faulted = true; |
| | 2 | 388 | | checkpoint.Message = ex.Message; |
| | 2 | 389 | | await SaveAsync(CancellationToken.None, cause: ex).ConfigureAwait(false); |
| | 2 | 390 | | throw; |
| | | 391 | | } |
| | | 392 | | finally |
| | | 393 | | { |
| | 3 | 394 | | await waiter.DisposeAsync().ConfigureAwait(false); |
| | | 395 | | } |
| | 3 | 396 | | } |
| | | 397 | | |
| | | 398 | | private async Task<IAsyncResponseWaiter<TResponse>> CreateWaiterAsync<TResponse>( |
| | | 399 | | string correlationId, |
| | | 400 | | Func<TResponse, ValueTask<bool>>? until, |
| | | 401 | | TimeSpan? timeout, |
| | | 402 | | string stepName) where TResponse : IAsyncResponsePayload |
| | | 403 | | { |
| | 3 | 404 | | if (_recoverableSubscriber is not null) |
| | | 405 | | { |
| | | 406 | | // The durable safety net: a response landing while no process is executing this flow |
| | | 407 | | // checkpoints the terminal payload and re-enqueues the run, or terminally fails it — |
| | | 408 | | // the same at-least-once, idempotency-required contract as hand-registered callbacks. |
| | 3 | 409 | | var flowId = FlowId; |
| | 3 | 410 | | Expression<Func<IDurableFlowExecutor, Task>> resume = executor => executor.RecoverAsync( |
| | 3 | 411 | | flowId, |
| | 3 | 412 | | Placeholder.Payload<TResponse>()!, |
| | 3 | 413 | | Placeholder.CorrelationId()); |
| | 3 | 414 | | Expression<Func<IDurableFlowExecutor, Task>> failure = executor => executor.FailAsync(flowId, Placeholder.Ex |
| | | 415 | | |
| | 3 | 416 | | return await _recoverableSubscriber.CreateRecoverableResponseWaiter( |
| | 3 | 417 | | correlationId, |
| | 3 | 418 | | CallbackExpressionConverter.ToReflectionCall(resume), |
| | 3 | 419 | | CallbackExpressionConverter.ToReflectionCall(failure), |
| | 3 | 420 | | until, |
| | 3 | 421 | | timeout).ConfigureAwait(false); |
| | | 422 | | } |
| | | 423 | | |
| | 2 | 424 | | _logger.LogDebug( |
| | 2 | 425 | | "Flow {FlowId} step '{Step}': the configured channel exposes no recoverable subscriber; lost-subscriber reco |
| | 2 | 426 | | FlowId, stepName); |
| | | 427 | | |
| | 2 | 428 | | return await _subscriber.CreateResponseWaiter(correlationId, until, timeout).ConfigureAwait(false); |
| | 3 | 429 | | } |
| | | 430 | | |
| | | 431 | | private FlowState CreateChildState<TFlow, TInput>(string flowId, string parentStepName, string inputJson) |
| | | 432 | | { |
| | 2 | 433 | | var now = DateTime.UtcNow; |
| | 2 | 434 | | return new FlowState |
| | 2 | 435 | | { |
| | 2 | 436 | | FlowId = flowId, |
| | 2 | 437 | | FlowTypeName = typeof(TFlow).FullName, |
| | 2 | 438 | | InputTypeName = typeof(TInput).FullName, |
| | 2 | 439 | | InputJson = inputJson, |
| | 2 | 440 | | Status = FlowRunStatus.Running, |
| | 2 | 441 | | LastMessage = $"Child flow started by {FlowId}.", |
| | 2 | 442 | | CreatedAtUtc = now, |
| | 2 | 443 | | UpdatedAtUtc = now, |
| | 2 | 444 | | ParentFlowId = FlowId, |
| | 2 | 445 | | ParentStepName = parentStepName, |
| | 2 | 446 | | Context = _propagation.Capture() |
| | 2 | 447 | | }; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | private Task EnqueueChildAsync(string childFlowId) |
| | | 451 | | { |
| | 2 | 452 | | var id = childFlowId; |
| | 2 | 453 | | return _builder.EnqueueWorkerAsync<IDurableFlowExecutor>(executor => executor.ExecuteAsync(id)); |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | private async Task SuspendForChildAsync(string childFlowId, CancellationToken cancellationToken) |
| | | 457 | | { |
| | | 458 | | // Persist the suspension BEFORE the child becomes runnable: once the child is enqueued it |
| | | 459 | | // can complete and re-execute this parent on another worker at any moment, and a save after |
| | | 460 | | // that point would clobber the re-execution's newer checkpoints with this stale snapshot. |
| | | 461 | | // The executor therefore does NOT save again on the suspension path. |
| | 2 | 462 | | _suspended = true; |
| | 2 | 463 | | _state.LastMessage = $"Flow {FlowId} suspended waiting for child flow {childFlowId}."; |
| | 2 | 464 | | await SaveAsync(cancellationToken).ConfigureAwait(false); |
| | 2 | 465 | | await EnqueueChildAsync(childFlowId).ConfigureAwait(false); |
| | 2 | 466 | | throw new DurableFlowSuspendedException(_state.LastMessage); |
| | | 467 | | } |
| | | 468 | | |
| | | 469 | | private void ThrowIfSuspended() |
| | | 470 | | { |
| | 3 | 471 | | _lease.ThrowIfLost(); |
| | 3 | 472 | | if (_suspended) |
| | 0 | 473 | | throw new DurableFlowSuspendedException(_state.LastMessage ?? $"Flow {FlowId} is suspended."); |
| | 3 | 474 | | } |
| | | 475 | | |
| | | 476 | | private static void ThrowIfChildFailed(FlowState child, bool failOnChildFailure) |
| | | 477 | | { |
| | 2 | 478 | | if (failOnChildFailure && child.Status == FlowRunStatus.Failed) |
| | 2 | 479 | | throw new DurableFlowFailedException($"Child flow '{child.FlowId}' failed: {child.LastMessage ?? "no message |
| | 2 | 480 | | } |
| | | 481 | | |
| | | 482 | | private void ThrowIfChildMismatched<TFlow, TInput>( |
| | | 483 | | FlowState child, |
| | | 484 | | string childFlowId, |
| | | 485 | | string stepName, |
| | | 486 | | string requestedInputJson) |
| | | 487 | | { |
| | | 488 | | // A child id is owned by exactly one parent: the notification that resumes a suspended |
| | | 489 | | // parent follows the child's single ParentFlowId, so a second parent awaiting the same id |
| | | 490 | | // would suspend and never wake. Reject collisions loudly instead of parking forever. |
| | 2 | 491 | | if (!string.Equals(child.ParentFlowId, FlowId, StringComparison.Ordinal)) |
| | | 492 | | { |
| | 2 | 493 | | var owner = child.ParentFlowId is null ? "a run not started by AwaitChildFlowAsync" : $"parent flow '{child. |
| | 2 | 494 | | throw new DurableFlowFailedException( |
| | 2 | 495 | | $"Step '{stepName}' of flow '{FlowId}' awaits child flow id '{childFlowId}', but that id belongs to {own |
| | 2 | 496 | | "Child flow ids are exclusive to the parent that started them — pass a flowId that is unique per parent |
| | 2 | 497 | | "(the default '{parentFlowId}:{stepName}' id is always safe)."); |
| | | 498 | | } |
| | | 499 | | |
| | 2 | 500 | | if (!string.Equals(child.FlowId, childFlowId, StringComparison.Ordinal) |
| | 2 | 501 | | || !string.Equals(child.ParentStepName, stepName, StringComparison.Ordinal)) |
| | | 502 | | { |
| | 2 | 503 | | throw new DurableFlowFailedException( |
| | 2 | 504 | | $"Child flow id '{childFlowId}' is bound to a different child step than '{stepName}' of parent flow '{Fl |
| | 2 | 505 | | "A child id is exclusive to one parent step."); |
| | | 506 | | } |
| | | 507 | | |
| | 2 | 508 | | if (!string.Equals(child.FlowTypeName, typeof(TFlow).FullName, StringComparison.Ordinal)) |
| | | 509 | | { |
| | 2 | 510 | | throw new DurableFlowFailedException( |
| | 2 | 511 | | $"Step '{stepName}' of flow '{FlowId}' awaits child flow id '{childFlowId}' as {typeof(TFlow).FullName}, |
| | 2 | 512 | | $"but the persisted run is {child.FlowTypeName}. The flowId collides with a different flow — use a uniqu |
| | | 513 | | } |
| | | 514 | | |
| | 2 | 515 | | if (!string.Equals(child.InputTypeName, typeof(TInput).FullName, StringComparison.Ordinal) |
| | 2 | 516 | | || !FlowStateJson.JsonEquivalent(child.InputJson, requestedInputJson)) |
| | | 517 | | { |
| | 2 | 518 | | throw new DurableFlowFailedException( |
| | 2 | 519 | | $"Step '{stepName}' of flow '{FlowId}' requested child flow id '{childFlowId}' with a different input " |
| | 2 | 520 | | "type or value than the persisted child. Replays must use semantically identical child input."); |
| | | 521 | | } |
| | 2 | 522 | | } |
| | | 523 | | |
| | | 524 | | private FlowStepState GetStep(string name) |
| | | 525 | | { |
| | 3 | 526 | | var steps = _state.Steps ??= new Dictionary<string, FlowStepState>(StringComparer.Ordinal); |
| | 3 | 527 | | if (!steps.TryGetValue(name, out var step)) |
| | | 528 | | { |
| | 3 | 529 | | step = new FlowStepState(); |
| | 3 | 530 | | steps[name] = step; |
| | | 531 | | } |
| | | 532 | | |
| | 3 | 533 | | return step; |
| | | 534 | | } |
| | | 535 | | |
| | | 536 | | private async Task CompleteStepAsync(string name, FlowStepState step, string? resultJson, CancellationToken cancella |
| | | 537 | | { |
| | 3 | 538 | | step.Completed = true; |
| | 3 | 539 | | step.ResultJson = resultJson; |
| | 3 | 540 | | step.PendingCorrelationId = null; |
| | | 541 | | // A memoized failed child keeps Faulted = true so operators can spot the failure on the |
| | | 542 | | // step itself instead of digging through ResultJson. |
| | 3 | 543 | | step.Faulted = faulted; |
| | 3 | 544 | | step.CompletedAtUtc = DateTime.UtcNow; |
| | 3 | 545 | | _state.LastMessage = faulted ? $"Step '{name}' completed (child flow failed)." : $"Step '{name}' completed."; |
| | 3 | 546 | | await SaveAsync(cancellationToken).ConfigureAwait(false); |
| | | 547 | | |
| | 3 | 548 | | if (_logger.IsEnabled(LogLevel.Debug)) |
| | 0 | 549 | | _logger.LogDebug("Flow {FlowId} step '{Step}' completed.", FlowId, name); |
| | 3 | 550 | | } |
| | | 551 | | |
| | | 552 | | internal Task FlushProgressAsync() |
| | 3 | 553 | | => _progressDirty ? SaveAsync(CancellationToken.None) : Task.CompletedTask; |
| | | 554 | | |
| | | 555 | | private async Task SaveAsync(CancellationToken cancellationToken, Exception? cause = null) |
| | | 556 | | { |
| | 3 | 557 | | _state.UpdatedAtUtc = DateTime.UtcNow; |
| | 3 | 558 | | await _lease.SaveAsync(_state, _options.StateExpiry, cancellationToken, cause).ConfigureAwait(false); |
| | | 559 | | |
| | 3 | 560 | | _progressDirty = false; |
| | 3 | 561 | | _lastPersistenceUtc = DateTime.UtcNow; |
| | 3 | 562 | | } |
| | | 563 | | |
| | | 564 | | private async Task<TResponse> WaitForResponseAsync<TResponse>(Task<TResponse> responseTask, CancellationToken cancel |
| | | 565 | | { |
| | 3 | 566 | | if (!cancellationToken.CanBeCanceled) |
| | 3 | 567 | | return await responseTask.WaitAsync(_lease.LostToken).ConfigureAwait(false); |
| | | 568 | | |
| | 2 | 569 | | using var linked = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _lease.LostToken); |
| | 2 | 570 | | return await responseTask.WaitAsync(linked.Token).ConfigureAwait(false); |
| | 3 | 571 | | } |
| | | 572 | | |
| | | 573 | | private static TResult DeserializeResult<TResult>(string? resultJson) |
| | 2 | 574 | | => resultJson is null ? default! : JsonSafety.SafeDeserialize<TResult>(resultJson)!; |
| | | 575 | | } |