-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathGrpcDurableTaskWorker.Processor.cs
More file actions
1117 lines (1008 loc) · 51.5 KB
/
Copy pathGrpcDurableTaskWorker.Processor.cs
File metadata and controls
1117 lines (1008 loc) · 51.5 KB
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics;
using System.Linq;
using System.Text;
using DurableTask.Core;
using DurableTask.Core.Entities;
using DurableTask.Core.Entities.OperationFormat;
using DurableTask.Core.History;
using Google.Protobuf;
using Microsoft.DurableTask.Abstractions;
using Microsoft.DurableTask.Entities;
using Microsoft.DurableTask.Tracing;
using Microsoft.DurableTask.Worker.Grpc.Internal;
using Microsoft.DurableTask.Worker.Shims;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using static Microsoft.DurableTask.Protobuf.TaskHubSidecarService;
using ActivityStatusCode = System.Diagnostics.ActivityStatusCode;
using DTCore = DurableTask.Core;
using P = Microsoft.DurableTask.Protobuf;
namespace Microsoft.DurableTask.Worker.Grpc;
/// <summary>
/// The gRPC Durable Task worker.
/// </summary>
sealed partial class GrpcDurableTaskWorker
{
class Processor
{
static readonly Google.Protobuf.WellKnownTypes.Empty EmptyMessage = new();
readonly GrpcDurableTaskWorker worker;
readonly TaskHubSidecarServiceClient client;
readonly DurableTaskShimFactory shimFactory;
readonly GrpcDurableTaskWorkerOptions.InternalOptions internalOptions;
readonly DTCore.IExceptionPropertiesProvider? exceptionPropertiesProvider;
[Obsolete("Experimental")]
readonly IOrchestrationFilter? orchestrationFilter;
public Processor(GrpcDurableTaskWorker worker, TaskHubSidecarServiceClient client, IOrchestrationFilter? orchestrationFilter = null, IExceptionPropertiesProvider? exceptionPropertiesProvider = null)
{
this.worker = worker;
this.client = client;
this.shimFactory = new DurableTaskShimFactory(this.worker.grpcOptions, this.worker.loggerFactory);
this.internalOptions = this.worker.grpcOptions.Internal;
this.orchestrationFilter = orchestrationFilter;
this.exceptionPropertiesProvider = exceptionPropertiesProvider is not null
? new ExceptionPropertiesProviderAdapter(exceptionPropertiesProvider)
: null;
}
ILogger Logger => this.worker.logger;
public async Task ExecuteAsync(CancellationToken cancellation)
{
while (!cancellation.IsCancellationRequested)
{
try
{
AsyncServerStreamingCall<P.WorkItem> stream = await this.ConnectAsync(cancellation);
await this.ProcessWorkItemsAsync(stream, cancellation);
}
catch (RpcException) when (cancellation.IsCancellationRequested)
{
// Worker is shutting down - let the method exit gracefully
break;
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled)
{
// Sidecar is shutting down - retry
this.Logger.SidecarDisconnected();
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Unavailable)
{
// Sidecar is down - keep retrying
this.Logger.SidecarUnavailable();
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.NotFound)
{
// We retry on a NotFound for several reasons:
// 1. It was the existing behavior through the UnexpectedError path.
// 2. A 404 can be returned for a missing task hub or authentication failure. Authentication takes
// time to propagate so we should retry instead of making the user restart the application.
// 3. In some cases, a task hub can be created separately from the scheduler. If a worker is deployed
// between the scheduler and task hub, it would need to be restarted to function.
this.Logger.TaskHubNotFound();
}
catch (OperationCanceledException) when (cancellation.IsCancellationRequested)
{
// Shutting down, lets exit gracefully.
break;
}
catch (Exception ex)
{
// Unknown failure - retry?
this.Logger.UnexpectedError(ex, string.Empty);
}
try
{
// CONSIDER: Exponential backoff
await Task.Delay(TimeSpan.FromSeconds(5), cancellation);
}
catch (OperationCanceledException) when (cancellation.IsCancellationRequested)
{
// Worker is shutting down - let the method exit gracefully
break;
}
}
}
static string GetActionsListForLogging(IReadOnlyList<P.OrchestratorAction> actions)
{
if (actions.Count == 0)
{
return string.Empty;
}
else if (actions.Count == 1)
{
return actions[0].OrchestratorActionTypeCase.ToString();
}
else
{
// Returns something like "ScheduleTask x5, CreateTimer x1,..."
return string.Join(", ", actions
.GroupBy(a => a.OrchestratorActionTypeCase)
.Select(group => $"{group.Key} x{group.Count()}"));
}
}
static P.TaskFailureDetails? EvaluateOrchestrationVersioning(DurableTaskWorkerOptions.VersioningOptions? versioning, string orchestrationVersion, out bool versionCheckFailed)
{
P.TaskFailureDetails? failureDetails = null;
versionCheckFailed = false;
if (versioning != null)
{
int versionComparison = TaskOrchestrationVersioningUtils.CompareVersions(orchestrationVersion, versioning.Version);
switch (versioning.MatchStrategy)
{
case DurableTaskWorkerOptions.VersionMatchStrategy.None:
// No versioning, breakout.
break;
case DurableTaskWorkerOptions.VersionMatchStrategy.Strict:
// Comparison of 0 indicates equality.
if (versionComparison != 0)
{
failureDetails = new P.TaskFailureDetails
{
ErrorType = "VersionMismatch",
ErrorMessage = $"The orchestration version '{orchestrationVersion}' does not match the worker version '{versioning.Version}'.",
IsNonRetriable = true,
};
}
break;
case DurableTaskWorkerOptions.VersionMatchStrategy.CurrentOrOlder:
// Comparison > 0 indicates the orchestration version is greater than the worker version.
if (versionComparison > 0)
{
failureDetails = new P.TaskFailureDetails
{
ErrorType = "VersionMismatch",
ErrorMessage = $"The orchestration version '{orchestrationVersion}' is greater than the worker version '{versioning.Version}'.",
IsNonRetriable = true,
};
}
break;
default:
// If there is a type of versioning we don't understand, it is better to treat it as a versioning failure.
failureDetails = new P.TaskFailureDetails
{
ErrorType = "VersionError",
ErrorMessage = $"The version match strategy '{orchestrationVersion}' is unknown.",
IsNonRetriable = true,
};
break;
}
versionCheckFailed = failureDetails != null;
}
return failureDetails;
}
async ValueTask<OrchestrationRuntimeState> BuildRuntimeStateAsync(
P.OrchestratorRequest orchestratorRequest,
ProtoUtils.EntityConversionState? entityConversionState,
CancellationToken cancellation)
{
Func<P.HistoryEvent, HistoryEvent> converter = entityConversionState is null
? ProtoUtils.ConvertHistoryEvent
: entityConversionState.ConvertFromProto;
IEnumerable<HistoryEvent> pastEvents = [];
if (orchestratorRequest.RequiresHistoryStreaming)
{
// Stream the remaining events from the remote service
P.StreamInstanceHistoryRequest streamRequest = new()
{
InstanceId = orchestratorRequest.InstanceId,
ExecutionId = orchestratorRequest.ExecutionId,
ForWorkItemProcessing = true,
};
using AsyncServerStreamingCall<P.HistoryChunk> streamResponse =
this.client.StreamInstanceHistory(streamRequest, cancellationToken: cancellation);
await foreach (P.HistoryChunk chunk in streamResponse.ResponseStream.ReadAllAsync(cancellation))
{
pastEvents = pastEvents.Concat(chunk.Events.Select(converter));
}
}
else
{
// The history was already provided in the work item request
pastEvents = orchestratorRequest.PastEvents.Select(converter);
}
IEnumerable<HistoryEvent> newEvents = orchestratorRequest.NewEvents.Select(converter);
// Reconstruct the orchestration state in a way that correctly distinguishes new events from past events
var runtimeState = new OrchestrationRuntimeState(pastEvents.ToList());
foreach (HistoryEvent e in newEvents)
{
// AddEvent() puts events into the NewEvents list.
runtimeState.AddEvent(e);
}
if (runtimeState.ExecutionStartedEvent == null)
{
// TODO: What's the right way to handle this? Callback to the sidecar with a retriable error request?
throw new InvalidOperationException("The provided orchestration history was incomplete");
}
return runtimeState;
}
async Task<AsyncServerStreamingCall<P.WorkItem>> ConnectAsync(CancellationToken cancellation)
{
await this.client!.HelloAsync(EmptyMessage, cancellationToken: cancellation);
this.Logger.EstablishedWorkItemConnection();
DurableTaskWorkerOptions workerOptions = this.worker.workerOptions;
// Get the stream for receiving work-items
return this.client!.GetWorkItems(
new P.GetWorkItemsRequest
{
MaxConcurrentActivityWorkItems =
workerOptions.Concurrency.MaximumConcurrentActivityWorkItems,
MaxConcurrentOrchestrationWorkItems =
workerOptions.Concurrency.MaximumConcurrentOrchestrationWorkItems,
MaxConcurrentEntityWorkItems =
workerOptions.Concurrency.MaximumConcurrentEntityWorkItems,
Capabilities = { this.worker.grpcOptions.Capabilities },
WorkItemFilters = this.worker.workItemFilters?.ToGrpcWorkItemFilters(),
},
cancellationToken: cancellation);
}
async Task ProcessWorkItemsAsync(AsyncServerStreamingCall<P.WorkItem> stream, CancellationToken cancellation)
{
// Create a new token source for timing out and a final token source that keys off of them both.
// The timeout token is used to detect when we are no longer getting any messages, including health checks.
// If this is the case, it signifies the connection has been dropped silently and we need to reconnect.
using var timeoutSource = new CancellationTokenSource();
timeoutSource.CancelAfter(TimeSpan.FromSeconds(60));
using var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellation, timeoutSource.Token);
while (!cancellation.IsCancellationRequested)
{
await foreach (P.WorkItem workItem in stream.ResponseStream.ReadAllAsync(tokenSource.Token))
{
timeoutSource.CancelAfter(TimeSpan.FromSeconds(60));
if (workItem.RequestCase == P.WorkItem.RequestOneofCase.OrchestratorRequest)
{
this.RunBackgroundTask(
workItem,
() => this.OnRunOrchestratorAsync(
workItem.OrchestratorRequest,
workItem.CompletionToken,
cancellation),
cancellation);
}
else if (workItem.RequestCase == P.WorkItem.RequestOneofCase.ActivityRequest)
{
this.RunBackgroundTask(
workItem,
() => this.OnRunActivityAsync(
workItem.ActivityRequest,
workItem.CompletionToken,
cancellation),
cancellation);
}
else if (workItem.RequestCase == P.WorkItem.RequestOneofCase.EntityRequest)
{
this.RunBackgroundTask(
workItem,
() => this.OnRunEntityBatchAsync(workItem.EntityRequest.ToEntityBatchRequest(), cancellation),
cancellation);
}
else if (workItem.RequestCase == P.WorkItem.RequestOneofCase.EntityRequestV2)
{
workItem.EntityRequestV2.ToEntityBatchRequest(
out EntityBatchRequest batchRequest,
out List<P.OperationInfo> operationInfos);
this.RunBackgroundTask(
workItem,
() => this.OnRunEntityBatchAsync(
batchRequest,
cancellation,
workItem.CompletionToken,
operationInfos),
cancellation);
}
else if (workItem.RequestCase == P.WorkItem.RequestOneofCase.HealthPing)
{
// No-op
}
else
{
this.Logger.UnexpectedWorkItemType(workItem.RequestCase.ToString());
}
}
if (tokenSource.IsCancellationRequested || tokenSource.Token.IsCancellationRequested)
{
// The token has cancelled, this means either:
// 1. The broader 'cancellation' was triggered, return here to start a graceful shutdown.
// 2. The timeoutSource was triggered, return here to trigger a reconnect to the backend.
if (!cancellation.IsCancellationRequested)
{
// Since the cancellation came from the timeout, log a warning.
this.Logger.ConnectionTimeout();
}
return;
}
}
}
void RunBackgroundTask(P.WorkItem? workItem, Func<Task> handler, CancellationToken cancellation)
{
// TODO: is Task.Run appropriate here? Should we have finer control over the tasks and their threads?
_ = Task.Run(
async () =>
{
try
{
await handler();
}
catch (OperationCanceledException)
{
// Shutting down - ignore
}
catch (Exception ex)
{
string instanceId =
workItem?.OrchestratorRequest?.InstanceId ??
workItem?.ActivityRequest?.OrchestrationInstance?.InstanceId ??
workItem?.EntityRequest?.InstanceId ??
workItem?.EntityRequestV2?.InstanceId ??
string.Empty;
this.Logger.UnexpectedError(ex, instanceId);
if (workItem?.OrchestratorRequest != null)
{
try
{
this.Logger.AbandoningOrchestratorWorkItem(instanceId, workItem?.CompletionToken ?? string.Empty);
await this.client.AbandonTaskOrchestratorWorkItemAsync(
new P.AbandonOrchestrationTaskRequest
{
CompletionToken = workItem?.CompletionToken,
},
cancellationToken: cancellation);
this.Logger.AbandonedOrchestratorWorkItem(instanceId, workItem?.CompletionToken ?? string.Empty);
}
catch (Exception abandonException)
{
this.Logger.UnexpectedError(abandonException, instanceId);
}
}
else if (workItem?.ActivityRequest != null)
{
try
{
this.Logger.AbandoningActivityWorkItem(
instanceId,
workItem.ActivityRequest.Name,
workItem.ActivityRequest.TaskId,
workItem?.CompletionToken ?? string.Empty);
await this.client.AbandonTaskActivityWorkItemAsync(
new P.AbandonActivityTaskRequest
{
CompletionToken = workItem?.CompletionToken,
},
cancellationToken: cancellation);
this.Logger.AbandonedActivityWorkItem(
instanceId,
workItem.ActivityRequest.Name,
workItem.ActivityRequest.TaskId,
workItem?.CompletionToken ?? string.Empty);
}
catch (Exception abandonException)
{
this.Logger.UnexpectedError(abandonException, instanceId);
}
}
else if (workItem?.EntityRequest != null)
{
try
{
this.Logger.AbandoningEntityWorkItem(
workItem.EntityRequest.InstanceId,
workItem?.CompletionToken ?? string.Empty);
await this.client.AbandonTaskEntityWorkItemAsync(
new P.AbandonEntityTaskRequest
{
CompletionToken = workItem?.CompletionToken,
},
cancellationToken: cancellation);
this.Logger.AbandonedEntityWorkItem(
workItem.EntityRequest.InstanceId,
workItem?.CompletionToken ?? string.Empty);
}
catch (Exception abandonException)
{
this.Logger.UnexpectedError(abandonException, workItem.EntityRequest.InstanceId);
}
}
else if (workItem?.EntityRequestV2 != null)
{
try
{
this.Logger.AbandoningEntityWorkItem(
workItem.EntityRequestV2.InstanceId,
workItem?.CompletionToken ?? string.Empty);
await this.client.AbandonTaskEntityWorkItemAsync(
new P.AbandonEntityTaskRequest
{
CompletionToken = workItem?.CompletionToken,
},
cancellationToken: cancellation);
this.Logger.AbandonedEntityWorkItem(
workItem.EntityRequestV2.InstanceId,
workItem?.CompletionToken ?? string.Empty);
}
catch (Exception abandonException)
{
this.Logger.UnexpectedError(abandonException, workItem.EntityRequestV2.InstanceId);
}
}
}
});
}
async Task OnRunOrchestratorAsync(
P.OrchestratorRequest request,
string completionToken,
CancellationToken cancellationToken)
{
var executionStartedEvent =
request
.NewEvents
.Concat(request.PastEvents)
.Where(e => e.EventTypeCase == P.HistoryEvent.EventTypeOneofCase.ExecutionStarted)
.Select(e => e.ExecutionStarted)
.FirstOrDefault();
Activity? traceActivity = TraceHelper.StartTraceActivityForOrchestrationExecution(
executionStartedEvent,
request.OrchestrationTraceContext);
if (executionStartedEvent is not null)
{
P.HistoryEvent? GetSuborchestrationInstanceCreatedEvent(int eventId)
{
var subOrchestrationEvent =
request
.PastEvents
.Where(x => x.EventTypeCase == P.HistoryEvent.EventTypeOneofCase.SubOrchestrationInstanceCreated)
.FirstOrDefault(x => x.EventId == eventId);
return subOrchestrationEvent;
}
P.HistoryEvent? GetTaskScheduledEvent(int eventId)
{
var taskScheduledEvent =
request
.PastEvents
.Where(x => x.EventTypeCase == P.HistoryEvent.EventTypeOneofCase.TaskScheduled)
.LastOrDefault(x => x.EventId == eventId);
return taskScheduledEvent;
}
P.HistoryEvent? GetEntityOperationCalledEvent(string requestId)
{
return request
.PastEvents
.Where(x => x.EventTypeCase == P.HistoryEvent.EventTypeOneofCase.EntityOperationCalled)
.FirstOrDefault(x => x.EntityOperationCalled.RequestId == requestId);
}
foreach (var newEvent in request.NewEvents)
{
switch (newEvent.EventTypeCase)
{
case P.HistoryEvent.EventTypeOneofCase.SubOrchestrationInstanceCompleted:
{
P.HistoryEvent? subOrchestrationInstanceCreatedEvent =
GetSuborchestrationInstanceCreatedEvent(
newEvent.SubOrchestrationInstanceCompleted.TaskScheduledId);
TraceHelper.EmitTraceActivityForSubOrchestrationCompleted(
request.InstanceId,
subOrchestrationInstanceCreatedEvent,
subOrchestrationInstanceCreatedEvent?.SubOrchestrationInstanceCreated);
break;
}
case P.HistoryEvent.EventTypeOneofCase.SubOrchestrationInstanceFailed:
{
P.HistoryEvent? subOrchestrationInstanceCreatedEvent =
GetSuborchestrationInstanceCreatedEvent(
newEvent.SubOrchestrationInstanceFailed.TaskScheduledId);
TraceHelper.EmitTraceActivityForSubOrchestrationFailed(
request.InstanceId,
subOrchestrationInstanceCreatedEvent,
subOrchestrationInstanceCreatedEvent?.SubOrchestrationInstanceCreated,
newEvent.SubOrchestrationInstanceFailed);
break;
}
case P.HistoryEvent.EventTypeOneofCase.TaskCompleted:
{
P.HistoryEvent? taskScheduledEvent =
GetTaskScheduledEvent(newEvent.TaskCompleted.TaskScheduledId);
TraceHelper.EmitTraceActivityForTaskCompleted(
request.InstanceId,
taskScheduledEvent,
taskScheduledEvent?.TaskScheduled);
break;
}
case P.HistoryEvent.EventTypeOneofCase.TaskFailed:
{
P.HistoryEvent? taskScheduledEvent =
GetTaskScheduledEvent(newEvent.TaskFailed.TaskScheduledId);
TraceHelper.EmitTraceActivityForTaskFailed(
request.InstanceId,
taskScheduledEvent,
taskScheduledEvent?.TaskScheduled,
newEvent.TaskFailed);
break;
}
case P.HistoryEvent.EventTypeOneofCase.TimerFired:
TraceHelper.EmitTraceActivityForTimer(
request.InstanceId,
executionStartedEvent.Name,
newEvent.Timestamp.ToDateTime(),
newEvent.TimerFired);
break;
case P.HistoryEvent.EventTypeOneofCase.EntityOperationCompleted:
{
P.HistoryEvent? entityCalledEvent =
GetEntityOperationCalledEvent(
newEvent.EntityOperationCompleted.RequestId);
TraceHelper.EmitTraceActivityForEntityOperationCompleted(
request.InstanceId,
entityCalledEvent,
entityCalledEvent?.EntityOperationCalled);
break;
}
case P.HistoryEvent.EventTypeOneofCase.EntityOperationFailed:
{
P.HistoryEvent? entityCalledEvent =
GetEntityOperationCalledEvent(
newEvent.EntityOperationFailed.RequestId);
TraceHelper.EmitTraceActivityForEntityOperationFailed(
request.InstanceId,
entityCalledEvent,
entityCalledEvent?.EntityOperationCalled,
newEvent.EntityOperationFailed);
break;
}
}
}
}
OrchestratorExecutionResult? result = null;
P.TaskFailureDetails? failureDetails = null;
TaskName name = new("(unknown)");
ProtoUtils.EntityConversionState? entityConversionState =
this.internalOptions.ConvertOrchestrationEntityEvents
? new(this.internalOptions.InsertEntityUnlocksOnCompletion)
: null;
DurableTaskWorkerOptions.VersioningOptions? versioning = this.worker.workerOptions.Versioning;
bool versionFailure = false;
try
{
OrchestrationRuntimeState runtimeState = await this.BuildRuntimeStateAsync(
request,
entityConversionState,
cancellationToken);
bool filterPassed = true;
if (this.orchestrationFilter != null)
{
filterPassed = await this.orchestrationFilter.IsOrchestrationValidAsync(
new OrchestrationFilterParameters
{
Name = runtimeState.Name,
Tags = runtimeState.Tags != null ? new Dictionary<string, string>(runtimeState.Tags) : null,
},
cancellationToken);
}
if (!filterPassed)
{
this.Logger.AbandoningOrchestrationDueToOrchestrationFilter(request.InstanceId, completionToken);
await this.client.AbandonTaskOrchestratorWorkItemAsync(
new P.AbandonOrchestrationTaskRequest
{
CompletionToken = completionToken,
},
cancellationToken: cancellationToken);
return;
}
// If versioning has been explicitly set, we attempt to follow that pattern. If it is not set, we don't compare versions here.
failureDetails = EvaluateOrchestrationVersioning(versioning, runtimeState.Version, out versionFailure);
// Only continue with the work if the versioning check passed.
if (failureDetails == null)
{
name = new TaskName(runtimeState.Name);
this.Logger.ReceivedOrchestratorRequest(
name,
request.InstanceId,
runtimeState.PastEvents.Count,
runtimeState.NewEvents.Count);
await using AsyncServiceScope scope = this.worker.services.CreateAsyncScope();
if (this.worker.Factory.TryCreateOrchestrator(
name, scope.ServiceProvider, out ITaskOrchestrator? orchestrator))
{
// Both the factory invocation and the ExecuteAsync could involve user code and need to be handled
// as part of try/catch.
ParentOrchestrationInstance? parent = runtimeState.ParentInstance switch
{
ParentInstance p => new(new(p.Name), p.OrchestrationInstance.InstanceId),
_ => null,
};
TaskOrchestration shim = this.shimFactory.CreateOrchestration(name, orchestrator, parent);
TaskOrchestrationExecutor executor = new(
runtimeState,
shim,
BehaviorOnContinueAsNew.Carryover,
request.EntityParameters.ToCore(),
ErrorPropagationMode.UseFailureDetails,
this.exceptionPropertiesProvider);
result = executor.Execute();
}
else
{
failureDetails = new P.TaskFailureDetails
{
ErrorType = "OrchestratorTaskNotFound",
ErrorMessage = $"No orchestrator task named '{name}' was found.",
IsNonRetriable = true,
};
}
}
}
catch (Exception unexpected)
{
// This is not expected: Normally TaskOrchestrationExecutor handles exceptions in user code.
this.Logger.OrchestratorFailed(name, request.InstanceId, unexpected.ToString());
failureDetails = unexpected.ToTaskFailureDetails(this.exceptionPropertiesProvider);
}
P.OrchestratorResponse response;
if (result != null)
{
response = ProtoUtils.ConstructOrchestratorResponse(
request.InstanceId,
request.ExecutionId,
result.CustomStatus,
result.Actions,
completionToken,
entityConversionState,
traceActivity);
}
else if (versioning != null && failureDetails != null && versionFailure)
{
this.Logger.OrchestrationVersionFailure(versioning.FailureStrategy.ToString(), failureDetails.ErrorMessage);
if (versioning.FailureStrategy == DurableTaskWorkerOptions.VersionFailureStrategy.Fail)
{
response = new P.OrchestratorResponse
{
InstanceId = request.InstanceId,
CompletionToken = completionToken,
Actions =
{
new P.OrchestratorAction
{
CompleteOrchestration = new P.CompleteOrchestrationAction
{
OrchestrationStatus = P.OrchestrationStatus.Failed,
FailureDetails = failureDetails,
},
},
},
};
}
else
{
this.Logger.AbandoningOrchestrationDueToVersioning(request.InstanceId, completionToken);
await this.client.AbandonTaskOrchestratorWorkItemAsync(
new P.AbandonOrchestrationTaskRequest
{
CompletionToken = completionToken,
},
cancellationToken: cancellationToken);
return;
}
}
else
{
// This is the case for failures that happened *outside* the orchestrator executor
response = new P.OrchestratorResponse
{
InstanceId = request.InstanceId,
CompletionToken = completionToken,
Actions =
{
new P.OrchestratorAction
{
CompleteOrchestration = new P.CompleteOrchestrationAction
{
OrchestrationStatus = P.OrchestrationStatus.Failed,
FailureDetails = failureDetails,
},
},
},
};
}
var completeOrchestrationAction = response.Actions.FirstOrDefault(
a => a.CompleteOrchestration is not null);
if (completeOrchestrationAction is not null)
{
if (completeOrchestrationAction.CompleteOrchestration.OrchestrationStatus == P.OrchestrationStatus.Failed)
{
traceActivity?.SetStatus(
ActivityStatusCode.Error,
completeOrchestrationAction.CompleteOrchestration.Result);
}
traceActivity?.SetTag(
Schema.Task.Status,
completeOrchestrationAction.CompleteOrchestration.OrchestrationStatus.ToString());
traceActivity?.Dispose();
}
this.Logger.SendingOrchestratorResponse(
name,
response.InstanceId,
response.Actions.Count,
GetActionsListForLogging(response.Actions));
await this.CompleteOrchestratorTaskWithChunkingAsync(
response,
this.worker.grpcOptions.CompleteOrchestrationWorkItemChunkSizeInBytes,
cancellationToken);
}
async Task OnRunActivityAsync(P.ActivityRequest request, string completionToken, CancellationToken cancellation)
{
using Activity? traceActivity = TraceHelper.StartTraceActivityForTaskExecution(request);
OrchestrationInstance instance = request.OrchestrationInstance.ToCore();
string rawInput = request.Input;
int inputSize = rawInput != null ? Encoding.UTF8.GetByteCount(rawInput) : 0;
this.Logger.ReceivedActivityRequest(request.Name, request.TaskId, instance.InstanceId, inputSize);
P.TaskFailureDetails? failureDetails = null;
TaskContext innerContext = new(instance);
innerContext.ExceptionPropertiesProvider = this.exceptionPropertiesProvider;
TaskName name = new(request.Name);
string? output = null;
failureDetails = EvaluateOrchestrationVersioning(this.worker.workerOptions.Versioning, request.Version, out bool versioningFailed);
if (!versioningFailed)
{
try
{
await using AsyncServiceScope scope = this.worker.services.CreateAsyncScope();
if (this.worker.Factory.TryCreateActivity(name, scope.ServiceProvider, out ITaskActivity? activity))
{
// Both the factory invocation and the RunAsync could involve user code and need to be handled as
// part of try/catch.
TaskActivity shim = this.shimFactory.CreateActivity(name, activity);
output = await shim.RunAsync(innerContext, request.Input);
}
else
{
failureDetails = new P.TaskFailureDetails
{
ErrorType = "ActivityTaskNotFound",
ErrorMessage = $"No activity task named '{name}' was found.",
IsNonRetriable = true,
};
}
}
catch (Exception applicationException)
{
failureDetails = applicationException.ToTaskFailureDetails(this.exceptionPropertiesProvider);
}
}
else
{
if (this.worker.workerOptions.Versioning?.FailureStrategy == DurableTaskWorkerOptions.VersionFailureStrategy.Reject)
{
this.Logger.AbandoningActivityWorkItem(instance.InstanceId, request.Name, request.TaskId, completionToken);
await this.client.AbandonTaskActivityWorkItemAsync(
new P.AbandonActivityTaskRequest
{
CompletionToken = completionToken,
},
cancellationToken: cancellation);
}
return;
}
int outputSizeInBytes = 0;
if (failureDetails != null)
{
traceActivity?.SetStatus(ActivityStatusCode.Error, failureDetails.ErrorMessage);
outputSizeInBytes = failureDetails.GetApproximateByteCount();
}
else if (output != null)
{
outputSizeInBytes = Encoding.UTF8.GetByteCount(output);
}
string successOrFailure = failureDetails != null ? "failure" : "success";
this.Logger.SendingActivityResponse(
successOrFailure, name, request.TaskId, instance.InstanceId, outputSizeInBytes);
P.ActivityResponse response = new()
{
InstanceId = instance.InstanceId,
TaskId = request.TaskId,
Result = output,
FailureDetails = failureDetails,
CompletionToken = completionToken,
};
// Stop the trace activity here to avoid including the completion time in the latency calculation
traceActivity?.Stop();
await this.client.CompleteActivityTaskAsync(response, cancellationToken: cancellation);
}
async Task OnRunEntityBatchAsync(
EntityBatchRequest batchRequest,
CancellationToken cancellation,
string? completionToken = null,
List<P.OperationInfo>? operationInfos = null)
{
var coreEntityId = DTCore.Entities.EntityId.FromString(batchRequest.InstanceId!);
EntityId entityId = new(coreEntityId.Name, coreEntityId.Key);
// Start a Server trace span for each entity operation in the batch.
// Each operation may come from a different orchestration with its own trace context,
// so we create individual spans to preserve correct parent-child relationships.
List<Activity> traceActivities = batchRequest.Operations?
.Select(op => TraceHelper.StartTraceActivityForEntityOperation(
entityId.Name,
op.Operation,
batchRequest.InstanceId!,
op.TraceContext?.TraceParent,
op.TraceContext?.TraceState))
.OfType<Activity>()
.ToList()
?? [];
TaskName name = new(entityId.Name);
EntityBatchResult? batchResult;
try
{
await using AsyncServiceScope scope = this.worker.services.CreateAsyncScope();
IDurableTaskFactory2 factory = (IDurableTaskFactory2)this.worker.Factory;
if (factory.TryCreateEntity(name, scope.ServiceProvider, out ITaskEntity? entity))
{
// Both the factory invocation and the RunAsync could involve user code and need to be handled as
// part of try/catch.
TaskEntity shim = this.shimFactory.CreateEntity(name, entity, entityId);
batchResult = await shim.ExecuteOperationBatchAsync(batchRequest);
}
else
{
// we could not find the entity. This is considered an application error,
// so we return a non-retriable error-OperationResult for each operation in the batch.
string errorMessage = $"No entity task named '{name}' was found.";
traceActivities.ForEach(a => a.SetStatus(ActivityStatusCode.Error, errorMessage));
batchResult = new EntityBatchResult()
{
Actions = [], // no actions
EntityState = batchRequest.EntityState, // state is unmodified
Results = Enumerable.Repeat(
new OperationResult()
{
FailureDetails = new FailureDetails(
errorType: "EntityTaskNotFound",
errorMessage: errorMessage,
stackTrace: null,
innerFailure: null,
isNonRetriable: true),
},
batchRequest.Operations!.Count).ToList(),
FailureDetails = null,
};
}
}
catch (Exception frameworkException)
{
// return a result with failure details.
// this will cause the batch to be abandoned and retried
// (possibly after a delay and on a different worker).
batchResult = new EntityBatchResult()
{
FailureDetails = new FailureDetails(frameworkException),
};
traceActivities.ForEach(a => a.SetStatus(ActivityStatusCode.Error, frameworkException.Message));
}
finally
{
traceActivities.ForEach(a => a.Dispose());
}
P.EntityBatchResult response = batchResult.ToEntityBatchResult(
completionToken,
operationInfos?.Take(batchResult.Results?.Count ?? 0));
await this.client.CompleteEntityTaskAsync(response, cancellationToken: cancellation);
}
/// <summary>
/// Completes an orchestration task with automatic chunking if the response exceeds the maximum size.
/// </summary>
/// <param name="response">The orchestrator response to send.</param>
/// <param name="maxChunkBytes">The maximum size in bytes for each chunk.</param>
/// <param name="cancellationToken">The cancellation token.</param>
async Task CompleteOrchestratorTaskWithChunkingAsync(
P.OrchestratorResponse response,
int maxChunkBytes,
CancellationToken cancellationToken)
{
// Validate that no single action exceeds the maximum chunk size
static P.TaskFailureDetails? ValidateActionsSize(IEnumerable<P.OrchestratorAction> actions, int maxChunkBytes)
{
foreach (P.OrchestratorAction action in actions)
{
int actionSize = action.CalculateSize();
if (actionSize > maxChunkBytes)