forked from dapr/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrpcProtocolHandler.cs
More file actions
324 lines (290 loc) · 13 KB
/
GrpcProtocolHandler.cs
File metadata and controls
324 lines (290 loc) · 13 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
// ------------------------------------------------------------------------
// Copyright 2025 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Dapr.DurableTask.Protobuf;
using Grpc.Core;
using Microsoft.Extensions.Logging;
namespace Dapr.Workflow.Worker.Grpc;
/// <summary>
/// Handles the bidirectional gRPC streaming protocol with the Dapr sidecar.
/// </summary>
internal sealed class GrpcProtocolHandler(TaskHubSidecarService.TaskHubSidecarServiceClient grpcClient, ILoggerFactory loggerFactory, int maxConcurrentWorkItems = 100, int maxConcurrentActivities = 100) : IAsyncDisposable
{
private static readonly TimeSpan ReconnectDelay = TimeSpan.FromSeconds(5);
private readonly CancellationTokenSource _disposalCts = new();
private readonly ILogger<GrpcProtocolHandler> _logger = loggerFactory?.CreateLogger<GrpcProtocolHandler>() ?? throw new ArgumentNullException(nameof(loggerFactory));
private readonly TaskHubSidecarService.TaskHubSidecarServiceClient _grpcClient =
grpcClient ?? throw new ArgumentNullException(nameof(grpcClient));
private readonly int _maxConcurrentWorkItems = maxConcurrentWorkItems > 0 ? maxConcurrentWorkItems : throw new ArgumentOutOfRangeException(nameof(maxConcurrentWorkItems));
private readonly int _maxConcurrentActivities = maxConcurrentActivities > 0 ? maxConcurrentActivities : throw new ArgumentOutOfRangeException(nameof(maxConcurrentActivities));
private AsyncServerStreamingCall<WorkItem>? _streamingCall;
private int _activeWorkItemCount;
/// <summary>
/// Starts the streaming connection with the Dapr sidecar.
/// </summary>
/// <param name="workflowHandler">Handler for workflow work items.</param>
/// <param name="activityHandler">Handler for activity work items.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public async Task StartAsync(
Func<OrchestratorRequest, Task<OrchestratorResponse>> workflowHandler,
Func<ActivityRequest, Task<ActivityResponse>> activityHandler,
CancellationToken cancellationToken)
{
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _disposalCts.Token);
var token = linkedCts.Token;
// Establish the bidirectional stream
var request = new GetWorkItemsRequest
{
MaxConcurrentOrchestrationWorkItems = _maxConcurrentWorkItems,
MaxConcurrentActivityWorkItems = _maxConcurrentActivities
};
while (!token.IsCancellationRequested)
{
try
{
_logger.LogGrpcProtocolHandlerStartStream();
// Establish the server streaming call
_streamingCall = _grpcClient.GetWorkItems(request, cancellationToken: token);
_logger.LogGrpcProtocolHandlerStreamEstablished();
// Process work items from the stream
await ReceiveLoopAsync(_streamingCall.ResponseStream, workflowHandler, activityHandler, token);
// Stream ended gracefully => treat as an interrupted and reconnect unless shutting down
if (!token.IsCancellationRequested)
{
await DelayOrStopAsync(ReconnectDelay, token);
}
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
_logger.LogGrpcProtocolHandlerStreamCanceled();
break;
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled && token.IsCancellationRequested)
{
_logger.LogGrpcProtocolHandlerStreamCanceled();
break;
}
catch (RpcException ex) when (!token.IsCancellationRequested)
{
// Any RpcException while not shutting down -> retry indefinitely (transient or not)
_logger.LogGrpcProtocolHandlerGenericError(ex);
await DelayOrStopAsync(ReconnectDelay, token);
}
catch (Exception ex) when (!token.IsCancellationRequested)
{
// Any other interruption -> retry indefinitely
_logger.LogGrpcProtocolHandlerGenericError(ex);
await DelayOrStopAsync(ReconnectDelay, token);
}
finally
{
_streamingCall?.Dispose();
_streamingCall = null;
}
}
}
private static async Task DelayOrStopAsync(TimeSpan delay, CancellationToken token)
{
try
{
await Task.Delay(delay, token);
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
// Swallow cancellation so StartAsync exits cleanly when the host/test cancels.
}
}
/// <summary>
/// Receives requests from the Dapr sidecar and processes them.
/// </summary>
private async Task ReceiveLoopAsync(
IAsyncStreamReader<WorkItem> workItemsStream,
Func<OrchestratorRequest, Task<OrchestratorResponse>> orchestratorHandler,
Func<ActivityRequest, Task<ActivityResponse>> activityHandler,
CancellationToken cancellationToken)
{
// Track active work items for proper exception handling
var activeWorkItems = new List<Task>();
try
{
await foreach (var workItem in workItemsStream.ReadAllAsync(cancellationToken))
{
// Dispatch based on work item type
var workItemTask = workItem.RequestCase switch
{
WorkItem.RequestOneofCase.OrchestratorRequest => Task.Run(
() => ProcessWorkflowAsync(workItem.OrchestratorRequest, orchestratorHandler, cancellationToken),
cancellationToken),
WorkItem.RequestOneofCase.ActivityRequest => Task.Run(
() => ProcessActivityAsync(workItem.ActivityRequest, activityHandler, cancellationToken),
cancellationToken),
_ => Task.Run(
() => _logger.LogGrpcProtocolHandlerUnknownWorkItemType(workItem.RequestCase),
cancellationToken)
};
activeWorkItems.Add(workItemTask);
// Clean up completed tasks periodically
if (activeWorkItems.Count > _maxConcurrentWorkItems * 2)
{
activeWorkItems.RemoveAll(t => t.IsCompleted);
}
}
_logger.LogGrpcProtocolHandlerReceiveLoopCompleted(activeWorkItems.Count);
// Wait for all active work items to complete
if (activeWorkItems.Count > 0)
{
await Task.WhenAll(activeWorkItems);
}
}
catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested)
{
// Normal shutdown path (host stopping / handler disposing / token canceled)
_logger.LogGrpcProtocolHandlerReceiveLoopCanceled(ex);
}
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled && cancellationToken.IsCancellationRequested)
{
// gRPC surfaces token/dispose cancellation as StatusCode.Cancelled
_logger.LogGrpcProtocolHandlerReceiveLoopCanceled(ex);
}
catch (Exception ex)
{
_logger.LogGrpcProtocolHandlerReceiveLoopError(ex);
throw;
}
}
/// <summary>
/// Processes a workflow request work item.
/// </summary>
private async Task ProcessWorkflowAsync(OrchestratorRequest request,
Func<OrchestratorRequest, Task<OrchestratorResponse>> handler, CancellationToken cancellationToken)
{
var activeCount = Interlocked.Increment(ref _activeWorkItemCount);
try
{
_logger.LogGrpcProtocolHandlerWorkflowProcessorStart(request.InstanceId, activeCount);
var result = await handler(request);
// Send the result back to Dapr
await _grpcClient.CompleteOrchestratorTaskAsync(result, cancellationToken: cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
_logger.LogGrpcProtocolHandlerWorkflowProcessorCanceled(request.InstanceId);
}
catch (Exception ex)
{
try
{
var failureResult = CreateWorkflowFailureResult(request, ex);
await _grpcClient.CompleteOrchestratorTaskAsync(failureResult, cancellationToken: cancellationToken);
}
catch (Exception resultEx)
{
_logger.LogGrpcProtocolHandlerWorkflowProcessorFailedToSendError(resultEx, request.InstanceId);
}
}
finally
{
Interlocked.Decrement(ref _activeWorkItemCount);
}
}
/// <summary>
/// Processes an activity request work item.
/// </summary>
private async Task ProcessActivityAsync(ActivityRequest request,
Func<ActivityRequest, Task<ActivityResponse>> handler, CancellationToken cancellationToken)
{
var activeCount = Interlocked.Increment(ref _activeWorkItemCount);
try
{
_logger.LogGrpcProtocolHandlerActivityProcessorStart(request.OrchestrationInstance.InstanceId, request.Name,
request.TaskId, activeCount);
var result = await handler(request);
// Send the result back to Dapr
await _grpcClient.CompleteActivityTaskAsync(result, cancellationToken: cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
_logger.LogGrpcProtocolHandlerActivityProcessorCanceled(request.Name);
}
catch (Exception ex)
{
_logger.LogGrpcProtocolHandlerActivityProcessorError(ex, request.Name,
request.OrchestrationInstance?.InstanceId);
try
{
var failureResult = CreateActivityFailureResult(request, ex);
await _grpcClient.CompleteActivityTaskAsync(failureResult, cancellationToken: cancellationToken);
}
catch (Exception resultEx)
{
_logger.LogGrpcProtocolHandlerActivityProcessorFailedToSendError(resultEx, request.Name);
}
}
finally
{
Interlocked.Decrement(ref _activeWorkItemCount);
}
}
/// <summary>
/// Creates a failure response for an activity exception.
/// </summary>
private static ActivityResponse CreateActivityFailureResult(ActivityRequest request, Exception ex) =>
new()
{
InstanceId = request.OrchestrationInstance.InstanceId,
FailureDetails = new()
{
ErrorType = ex.GetType().FullName ?? "Exception",
ErrorMessage = ex.Message,
StackTrace = ex.StackTrace
}
};
/// <summary>
/// Creates a failure result for an orchestrator exception.
/// </summary>
private static OrchestratorResponse CreateWorkflowFailureResult(OrchestratorRequest request, Exception ex) =>
new()
{
InstanceId = request.InstanceId,
Actions =
{
new OrchestratorAction
{
CompleteOrchestration = new CompleteOrchestrationAction
{
OrchestrationStatus = OrchestrationStatus.Failed,
FailureDetails = new()
{
ErrorType = ex.GetType().FullName ?? "Exception",
ErrorMessage = ex.Message,
StackTrace = ex.StackTrace
}
}
}
}
};
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
if (_disposalCts.IsCancellationRequested)
return;
_logger.LogGrpcProtocolHandlerDisposing();
await _disposalCts.CancelAsync();
_streamingCall?.Dispose();
_disposalCts.Dispose();
_logger.LogGrpcProtocolHandlerDisposed();
}
}