-
Notifications
You must be signed in to change notification settings - Fork 263
/
DurableOrchestrationContext.cs
635 lines (546 loc) · 25.5 KB
/
DurableOrchestrationContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using DurableTask.Core;
using DurableTask.Core.Exceptions;
using DurableTask.Core.History;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.WebJobs
{
/// <summary>
/// Parameter data for orchestration bindings that can be used to schedule function-based activities.
/// </summary>
public sealed class DurableOrchestrationContext : DurableOrchestrationContextBase
{
private const string DefaultVersion = "";
private const int MaxTimerDurationInDays = 6;
private readonly Dictionary<string, Stack> pendingExternalEvents =
new Dictionary<string, Stack>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Queue<string>> bufferedExternalEvents =
new Dictionary<string, Queue<string>>(StringComparer.OrdinalIgnoreCase);
private readonly DurableTaskExtension config;
private readonly string orchestrationName;
private readonly List<Func<Task>> deferredTasks;
private OrchestrationContext innerContext;
private string serializedInput;
private string serializedOutput;
private string serializedCustomStatus;
private int newGuidCounter = 0;
internal DurableOrchestrationContext(DurableTaskExtension config, string functionName)
{
this.config = config ?? throw new ArgumentNullException(nameof(config));
this.deferredTasks = new List<Func<Task>>();
this.orchestrationName = functionName;
}
/// <inheritdoc />
public override DateTime CurrentUtcDateTime => this.innerContext.CurrentUtcDateTime;
/// <inheritdoc />
public override bool IsReplaying => this.innerContext?.IsReplaying ?? base.IsReplaying;
internal bool ContinuedAsNew { get; private set; }
internal bool PreserveUnprocessedEvents { get; private set; }
internal bool IsOutputSet => this.serializedOutput != null;
internal bool IsCompleted { get; set; }
internal ExceptionDispatchInfo OrchestrationException { get; set; }
internal string HubName => this.config.Options.HubName;
internal string Name => this.orchestrationName;
internal IList<HistoryEvent> History { get; set; }
/// <summary>
/// Returns the orchestrator function input as a raw JSON string value.
/// </summary>
/// <returns>
/// The raw JSON-formatted orchestrator function input.
/// </returns>
internal string GetRawInput()
{
this.ThrowIfInvalidAccess();
return this.serializedInput;
}
/// <summary>
/// Gets the input of the current orchestrator function instance as a <c>JToken</c>.
/// </summary>
/// <returns>
/// The parsed <c>JToken</c> representation of the orchestrator function input.
/// </returns>
internal JToken GetInputAsJson()
{
return this.serializedInput != null ? JToken.Parse(this.serializedInput) : null;
}
/// <inheritdoc />
public override T GetInput<T>()
{
this.ThrowIfInvalidAccess();
// Nulls need special handling because the JSON converter will throw
// if you try to convert a JSON null into a CLR value type.
if (this.serializedInput == null || this.serializedInput == "null")
{
return default(T);
}
return MessagePayloadDataConverter.Default.Deserialize<T>(this.serializedInput);
}
/// <inheritdoc />
public override Guid NewGuid()
{
// The name is a combination of the instance ID, the current orchestrator date/time, and a counter.
string guidNameValue = string.Concat(
this.InstanceId,
"_",
this.innerContext.CurrentUtcDateTime.ToString("o"),
"_",
this.newGuidCounter.ToString());
this.newGuidCounter++;
return GuidManager.CreateDeterministicGuid(GuidManager.UrlNamespaceValue, guidNameValue);
}
internal void SetInput(string rawInput)
{
this.serializedInput = rawInput;
}
internal void SetInnerContext(OrchestrationContext frameworkContext)
{
this.innerContext = frameworkContext;
}
/// <summary>
/// Sets the JSON-serializeable output of the current orchestrator function.
/// </summary>
/// <remarks>
/// If this method is not called explicitly, the return value of the orchestrator function is used as the output.
/// </remarks>
/// <param name="output">The JSON-serializeable value to use as the orchestrator function output.</param>
internal void SetOutput(object output)
{
this.ThrowIfInvalidAccess();
if (this.IsOutputSet)
{
throw new InvalidOperationException("The output has already been set of this orchestration instance.");
}
if (output != null)
{
JToken json = output as JToken;
if (json != null)
{
this.serializedOutput = json.ToString(Formatting.None);
}
else
{
this.serializedOutput = MessagePayloadDataConverter.Default.Serialize(output);
}
}
else
{
this.serializedOutput = null;
}
}
internal string GetSerializedOutput()
{
return this.serializedOutput;
}
/// <inheritdoc />
public override void SetCustomStatus(object customStatusObject)
{
// Limit the custom status payload to 16 KB
const int MaxCustomStatusPayloadSizeInKB = 16;
this.serializedCustomStatus = MessagePayloadDataConverter.Default.Serialize(
customStatusObject,
MaxCustomStatusPayloadSizeInKB);
}
internal string GetSerializedCustomStatus()
{
return this.serializedCustomStatus;
}
/// <inheritdoc />
public override Task<TResult> CallActivityAsync<TResult>(string functionName, object input)
{
return this.CallDurableTaskFunctionAsync<TResult>(functionName, FunctionType.Activity, null, null, input);
}
/// <inheritdoc />
public override Task<TResult> CallActivityWithRetryAsync<TResult>(string functionName, RetryOptions retryOptions, object input)
{
if (retryOptions == null)
{
throw new ArgumentNullException(nameof(retryOptions));
}
return this.CallDurableTaskFunctionAsync<TResult>(functionName, FunctionType.Activity, null, retryOptions, input);
}
/// <inheritdoc />
public override Task<TResult> CallSubOrchestratorAsync<TResult>(string functionName, string instanceId, object input)
{
return this.CallDurableTaskFunctionAsync<TResult>(functionName, FunctionType.Orchestrator, instanceId, null, input);
}
/// <inheritdoc />
public override Task<TResult> CallSubOrchestratorWithRetryAsync<TResult>(string functionName, RetryOptions retryOptions, string instanceId, object input)
{
if (retryOptions == null)
{
throw new ArgumentNullException(nameof(retryOptions));
}
return this.CallDurableTaskFunctionAsync<TResult>(functionName, FunctionType.Orchestrator, instanceId, retryOptions, input);
}
/// <summary>
/// Creates a durable timer that expires at a specified time.
/// </summary>
/// <remarks>
/// All durable timers created using this method must either expire or be cancelled
/// using the <paramref name="cancelToken"/> before the orchestrator function completes.
/// Otherwise the underlying framework will keep the instance alive until the timer expires.
/// </remarks>
/// <typeparam name="T">The type of <paramref name="state"/>.</typeparam>
/// <param name="fireAt">The time at which the timer should expire.</param>
/// <param name="state">Any state to be preserved by the timer.</param>
/// <param name="cancelToken">The <c>CancellationToken</c> to use for cancelling the timer.</param>
/// <returns>A durable task that completes when the durable timer expires.</returns>
public override async Task<T> CreateTimer<T>(DateTime fireAt, T state, CancellationToken cancelToken)
{
this.ThrowIfInvalidAccess();
// This check can be removed once the storage provider supports extended timers.
// https://github.com/Azure/azure-functions-durable-extension/issues/14
if (fireAt.Subtract(this.CurrentUtcDateTime) > TimeSpan.FromDays(MaxTimerDurationInDays))
{
throw new ArgumentException($"Timer durations must not exceed {MaxTimerDurationInDays} days.", nameof(fireAt));
}
Task<T> timerTask = this.innerContext.CreateTimer(fireAt, state, cancelToken);
this.config.TraceHelper.FunctionListening(
this.config.Options.HubName,
this.orchestrationName,
this.InstanceId,
reason: $"CreateTimer:{fireAt:o}",
isReplay: this.innerContext.IsReplaying);
T result = await timerTask;
this.config.TraceHelper.TimerExpired(
this.config.Options.HubName,
this.orchestrationName,
this.InstanceId,
expirationTime: fireAt,
isReplay: this.innerContext.IsReplaying);
return result;
}
/// <inheritdoc />
public override Task<T> WaitForExternalEvent<T>(string name)
{
this.ThrowIfInvalidAccess();
lock (this.pendingExternalEvents)
{
// We use a stack to make it easier for users to abandon external events
// that they no longer care about. The common case is a Task.WhenAny in a loop.
Stack taskCompletionSources;
TaskCompletionSource<T> tcs;
// Set up the stack for listening to external events
if (!this.pendingExternalEvents.TryGetValue(name, out taskCompletionSources))
{
tcs = new TaskCompletionSource<T>();
taskCompletionSources = new Stack();
taskCompletionSources.Push(tcs);
this.pendingExternalEvents[name] = taskCompletionSources;
}
else
{
if (taskCompletionSources.Count > 0 &&
taskCompletionSources.Peek().GetType() != typeof(TaskCompletionSource<T>))
{
throw new ArgumentException("Events with the same name should have the same type argument.");
}
else
{
tcs = new TaskCompletionSource<T>();
taskCompletionSources.Push(tcs);
}
}
// Check the queue to see if any events came in before the orchestrator was listening
if (this.bufferedExternalEvents.TryGetValue(name, out Queue<string> queue))
{
string rawInput = queue.Dequeue();
if (queue.Count == 0)
{
this.bufferedExternalEvents.Remove(name);
}
// We can call raise event right away, since we already have an event's input
this.RaiseEvent(name, rawInput);
}
else
{
this.config.TraceHelper.FunctionListening(
this.config.Options.HubName,
this.orchestrationName,
this.InstanceId,
reason: $"WaitForExternalEvent:{name}",
isReplay: this.innerContext.IsReplaying);
}
return tcs.Task;
}
}
/// <inheritdoc/>
public override Task<T> WaitForExternalEvent<T>(string name, TimeSpan timeout)
{
Action<TaskCompletionSource<T>> timedOutAction = cts =>
cts.TrySetException(new TimeoutException($"Event {name} not received in {timeout}"));
return this.WaitForExternalEvent(name, timeout, timedOutAction);
}
/// <inheritdoc/>
public override Task<T> WaitForExternalEvent<T>(string name, TimeSpan timeout, T defaultValue)
{
Action<TaskCompletionSource<T>> timedOutAction = cts => cts.TrySetResult(defaultValue);
return this.WaitForExternalEvent(name, timeout, timedOutAction);
}
private Task<T> WaitForExternalEvent<T>(string name, TimeSpan timeout, Action<TaskCompletionSource<T>> timeoutAction)
{
var tcs = new TaskCompletionSource<T>();
var cts = new CancellationTokenSource();
var timeoutAt = this.CurrentUtcDateTime + timeout;
var timeoutTask = this.CreateTimer(timeoutAt, cts.Token);
var waitForEventTask = this.WaitForExternalEvent<T>(name);
waitForEventTask.ContinueWith(
t =>
{
using (cts)
{
if (t.Exception != null)
{
tcs.TrySetException(t.Exception);
}
else
{
tcs.TrySetResult(t.Result);
}
cts.Cancel();
}
}, TaskContinuationOptions.ExecuteSynchronously);
timeoutTask.ContinueWith(
t =>
{
using (cts)
{
if (t.Exception == null)
{
timeoutAction(tcs);
}
else
{
// t.Exception is an aggregate exception, so grab internal exception
tcs.TrySetException(t.Exception.InnerException);
}
}
}, TaskContinuationOptions.ExecuteSynchronously);
return tcs.Task;
}
/// <inheritdoc />
public override void ContinueAsNew(object input) => this.ContinueAsNew(input, false);
/// <inheritdoc />
public override void ContinueAsNew(object input, bool preserveUnprocessedEvents)
{
this.ThrowIfInvalidAccess();
this.innerContext.ContinueAsNew(input);
this.ContinuedAsNew = true;
this.PreserveUnprocessedEvents = preserveUnprocessedEvents;
}
private async Task<TResult> CallDurableTaskFunctionAsync<TResult>(
string functionName,
FunctionType functionType,
string instanceId,
RetryOptions retryOptions,
object input)
{
this.ThrowIfInvalidAccess();
// TODO: Support for versioning
string version = DefaultVersion;
this.config.ThrowIfFunctionDoesNotExist(functionName, functionType);
Task<TResult> callTask;
switch (functionType)
{
case FunctionType.Activity:
System.Diagnostics.Debug.Assert(instanceId == null, "The instanceId parameter should not be used for activity functions.");
if (retryOptions == null)
{
callTask = this.innerContext.ScheduleTask<TResult>(functionName, version, input);
}
else
{
callTask = this.innerContext.ScheduleWithRetry<TResult>(
functionName,
version,
retryOptions.GetRetryOptions(),
input);
}
break;
case FunctionType.Orchestrator:
// Instance IDs should not be reused when creating sub-orchestrations. This is a best-effort
// check. We cannot easily check the full hierarchy, so we just look at the current orchestration
// and the immediate parent.
if (string.Equals(instanceId, this.InstanceId, StringComparison.OrdinalIgnoreCase) ||
(this.ParentInstanceId != null && string.Equals(instanceId, this.ParentInstanceId, StringComparison.OrdinalIgnoreCase)))
{
throw new ArgumentException("The instance ID of a sub-orchestration must be different than the instance ID of a parent orchestration.");
}
if (retryOptions == null)
{
callTask = this.innerContext.CreateSubOrchestrationInstance<TResult>(
functionName,
version,
instanceId,
input);
}
else
{
callTask = this.innerContext.CreateSubOrchestrationInstanceWithRetry<TResult>(
functionName,
version,
instanceId,
retryOptions.GetRetryOptions(),
input);
}
break;
default:
throw new InvalidOperationException($"Unexpected function type '{functionType}'.");
}
string sourceFunctionId = this.orchestrationName;
this.config.TraceHelper.FunctionScheduled(
this.config.Options.HubName,
functionName,
this.InstanceId,
reason: sourceFunctionId,
functionType: functionType,
isReplay: this.innerContext.IsReplaying);
TResult output;
Exception exception = null;
try
{
output = await callTask;
}
catch (TaskFailedException e)
{
exception = e;
string message = string.Format(
"The {0} function '{1}' failed: \"{2}\". See the function execution logs for additional details.",
functionType.ToString().ToLowerInvariant(),
functionName,
e.InnerException?.Message);
throw new FunctionFailedException(message, e.InnerException);
}
catch (SubOrchestrationFailedException e)
{
exception = e;
string message = string.Format(
"The {0} function '{1}' failed: \"{2}\". See the function execution logs for additional details.",
functionType.ToString().ToLowerInvariant(),
functionName,
e.InnerException?.Message);
throw new FunctionFailedException(message, e.InnerException);
}
catch (Exception e)
{
exception = e;
throw;
}
finally
{
if (exception != null && this.innerContext.IsReplaying)
{
// If this were not a replay, then the activity function trigger would have already
// emitted a FunctionFailed trace with the full exception details.
this.config.TraceHelper.FunctionFailed(
this.config.Options.HubName,
functionName,
this.InstanceId,
reason: $"(replayed {exception.GetType().Name})",
functionType: functionType,
isReplay: true);
}
}
if (this.innerContext.IsReplaying)
{
// If this were not a replay, then the activity function trigger would have already
// emitted a FunctionCompleted trace with the actual output details.
this.config.TraceHelper.FunctionCompleted(
this.config.Options.HubName,
functionName,
this.InstanceId,
output: "(replayed)",
continuedAsNew: false,
functionType: functionType,
isReplay: true);
}
return output;
}
internal void RaiseEvent(string name, string input)
{
lock (this.pendingExternalEvents)
{
Stack taskCompletionSources;
if (this.pendingExternalEvents.TryGetValue(name, out taskCompletionSources))
{
object tcs = taskCompletionSources.Pop();
Type tcsType = tcs.GetType();
Type genericTypeArgument = tcsType.GetGenericArguments()[0];
// If we're going to raise an event we should remove it from the pending collection
// because otherwise WaitForExternalEventAsync() will always find one with this key and run infinitely.
if (taskCompletionSources.Count == 0)
{
this.pendingExternalEvents.Remove(name);
}
object deserializedObject = MessagePayloadDataConverter.Default.Deserialize(input, genericTypeArgument);
MethodInfo trySetResult = tcsType.GetMethod("TrySetResult");
trySetResult.Invoke(tcs, new[] { deserializedObject });
}
else
{
// Add the event to an (in-memory) queue, so we don't drop or lose it
if (!this.bufferedExternalEvents.TryGetValue(name, out Queue<string> bufferedEvents))
{
bufferedEvents = new Queue<string>();
this.bufferedExternalEvents[name] = bufferedEvents;
}
bufferedEvents.Enqueue(input);
this.config.TraceHelper.ExternalEventSaved(
this.HubName,
this.Name,
this.InstanceId,
name,
this.IsReplaying);
}
}
}
internal void RescheduleBufferedExternalEvents()
{
var instance = new OrchestrationInstance { InstanceId = this.InstanceId };
foreach (var pair in this.bufferedExternalEvents)
{
string eventName = pair.Key;
Queue<string> events = pair.Value;
while (events.Count > 0)
{
// Need to round-trip serialization since SendEvent always tries to serialize.
string rawInput = events.Dequeue();
JToken jsonData = JToken.Parse(rawInput);
this.innerContext.SendEvent(instance, eventName, jsonData);
}
}
}
private void ThrowIfInvalidAccess()
{
if (this.innerContext == null)
{
throw new InvalidOperationException("The inner context has not been initialized.");
}
if (!OrchestrationContext.IsOrchestratorThread)
{
throw new InvalidOperationException(
"Multithreaded execution was detected. This can happen if the orchestrator function code awaits on a task that was not created by a DurableOrchestrationContext method. More details can be found in this article https://docs.microsoft.com/en-us/azure/azure-functions/durable-functions-checkpointing-and-replay#orchestrator-code-constraints.");
}
}
internal void AddDeferredTask(Func<Task> function)
{
this.deferredTasks.Add(function);
}
internal async Task RunDeferredTasks()
{
await Task.WhenAll(this.deferredTasks.Select(x => x()));
this.deferredTasks.Clear();
}
}
}