-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathGrpcWorkerChannel.cs
755 lines (663 loc) · 34.3 KB
/
GrpcWorkerChannel.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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Eventing;
using Microsoft.Azure.WebJobs.Script.Grpc.Eventing;
using Microsoft.Azure.WebJobs.Script.Grpc.Extensions;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Microsoft.Azure.WebJobs.Script.ManagedDependencies;
using Microsoft.Azure.WebJobs.Script.Workers;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using static Microsoft.Azure.WebJobs.Script.Grpc.Messages.RpcLog.Types;
using FunctionMetadata = Microsoft.Azure.WebJobs.Script.Description.FunctionMetadata;
using MsgType = Microsoft.Azure.WebJobs.Script.Grpc.Messages.StreamingMessage.ContentOneofCase;
using ParameterBindingType = Microsoft.Azure.WebJobs.Script.Grpc.Messages.ParameterBinding.RpcDataOneofCase;
namespace Microsoft.Azure.WebJobs.Script.Grpc
{
internal class GrpcWorkerChannel : IRpcWorkerChannel, IDisposable
{
private readonly TimeSpan workerInitTimeout = TimeSpan.FromSeconds(30);
private readonly IScriptEventManager _eventManager;
private readonly RpcWorkerConfig _workerConfig;
private readonly string _runtime;
private readonly IEnvironment _environment;
private readonly IOptionsMonitor<ScriptApplicationHostOptions> _applicationHostOptions;
private readonly ISharedMemoryManager _sharedMemoryManager;
private IDisposable _functionLoadRequestResponseEvent;
private bool _disposed;
private bool _disposing;
private WorkerInitResponse _initMessage;
private string _workerId;
private RpcWorkerChannelState _state;
private Queue<string> _processStdErrDataQueue = new Queue<string>(3);
private IDictionary<string, Exception> _functionLoadErrors = new Dictionary<string, Exception>();
private ConcurrentDictionary<string, ScriptInvocationContext> _executingInvocations = new ConcurrentDictionary<string, ScriptInvocationContext>();
private IDictionary<string, BufferBlock<ScriptInvocationContext>> _functionInputBuffers = new ConcurrentDictionary<string, BufferBlock<ScriptInvocationContext>>();
private ConcurrentDictionary<string, TaskCompletionSource<bool>> _workerStatusRequests = new ConcurrentDictionary<string, TaskCompletionSource<bool>>();
private IObservable<InboundGrpcEvent> _inboundWorkerEvents;
private List<IDisposable> _inputLinks = new List<IDisposable>();
private List<IDisposable> _eventSubscriptions = new List<IDisposable>();
private IDisposable _startSubscription;
private IDisposable _startLatencyMetric;
private IEnumerable<FunctionMetadata> _functions;
private GrpcCapabilities _workerCapabilities;
private ILogger _workerChannelLogger;
private IMetricsLogger _metricsLogger;
private IWorkerProcess _rpcWorkerProcess;
private TaskCompletionSource<bool> _reloadTask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
private TaskCompletionSource<bool> _workerInitTask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
private TimeSpan _functionLoadTimeout = TimeSpan.FromMinutes(10);
private bool _isSharedMemoryDataTransferEnabled;
internal GrpcWorkerChannel(
string workerId,
IScriptEventManager eventManager,
RpcWorkerConfig workerConfig,
IWorkerProcess rpcWorkerProcess,
ILogger logger,
IMetricsLogger metricsLogger,
int attemptCount,
IEnvironment environment,
IOptionsMonitor<ScriptApplicationHostOptions> applicationHostOptions,
ISharedMemoryManager sharedMemoryManager)
{
_workerId = workerId;
_eventManager = eventManager;
_workerConfig = workerConfig;
_runtime = workerConfig.Description.Language;
_rpcWorkerProcess = rpcWorkerProcess;
_workerChannelLogger = logger;
_metricsLogger = metricsLogger;
_environment = environment;
_applicationHostOptions = applicationHostOptions;
_sharedMemoryManager = sharedMemoryManager;
_workerCapabilities = new GrpcCapabilities(_workerChannelLogger);
_inboundWorkerEvents = _eventManager.OfType<InboundGrpcEvent>()
.Where(msg => msg.WorkerId == _workerId);
_eventSubscriptions.Add(_inboundWorkerEvents
.Where(msg => msg.IsMessageOfType(MsgType.RpcLog) && !msg.IsLogOfCategory(RpcLogCategory.System))
.Subscribe(Log));
_eventSubscriptions.Add(_inboundWorkerEvents
.Where(msg => msg.IsMessageOfType(MsgType.RpcLog) && msg.IsLogOfCategory(RpcLogCategory.System))
.Subscribe(SystemLog));
_eventSubscriptions.Add(_eventManager.OfType<FileEvent>()
.Where(msg => _workerConfig.Description.Extensions.Contains(Path.GetExtension(msg.FileChangeArguments.FullPath)))
.Throttle(TimeSpan.FromMilliseconds(300)) // debounce
.Subscribe(msg => _eventManager.Publish(new HostRestartEvent())));
_eventSubscriptions.Add(_inboundWorkerEvents.Where(msg => msg.MessageType == MsgType.InvocationResponse)
.Subscribe(async (msg) => await InvokeResponse(msg.Message.InvocationResponse)));
_inboundWorkerEvents.Where(msg => msg.MessageType == MsgType.WorkerStatusResponse)
.Subscribe((msg) => ReceiveWorkerStatusResponse(msg.Message.RequestId, msg.Message.WorkerStatusResponse));
_startLatencyMetric = metricsLogger?.LatencyEvent(string.Format(MetricEventNames.WorkerInitializeLatency, workerConfig.Description.Language, attemptCount));
_state = RpcWorkerChannelState.Default;
}
public string Id => _workerId;
public IDictionary<string, BufferBlock<ScriptInvocationContext>> FunctionInputBuffers => _functionInputBuffers;
internal IWorkerProcess WorkerProcess => _rpcWorkerProcess;
internal RpcWorkerConfig Config => _workerConfig;
public bool IsChannelReadyForInvocations()
{
return !_disposing && !_disposed && _state.HasFlag(RpcWorkerChannelState.InvocationBuffersInitialized | RpcWorkerChannelState.Initialized);
}
public async Task StartWorkerProcessAsync()
{
_startSubscription = _inboundWorkerEvents.Where(msg => msg.MessageType == MsgType.StartStream)
.Timeout(TimeSpan.FromSeconds(WorkerConstants.ProcessStartTimeoutSeconds))
.Take(1)
.Subscribe(SendWorkerInitRequest, HandleWorkerStartStreamError);
_workerChannelLogger.LogDebug("Initiating Worker Process start up");
await _rpcWorkerProcess.StartProcessAsync();
_state = _state | RpcWorkerChannelState.Initializing;
await _workerInitTask.Task;
}
public async Task<WorkerStatus> GetWorkerStatusAsync()
{
var workerStatus = new WorkerStatus();
if (!string.IsNullOrEmpty(_workerCapabilities.GetCapabilityState(RpcWorkerConstants.WorkerStatus)))
{
// get the worker's current status
// this will include the OOP worker's channel latency in the request, which can be used upstream
// to make scale decisions
var message = new StreamingMessage
{
RequestId = Guid.NewGuid().ToString(),
WorkerStatusRequest = new WorkerStatusRequest()
};
var sw = Stopwatch.StartNew();
var tcs = new TaskCompletionSource<bool>();
if (_workerStatusRequests.TryAdd(message.RequestId, tcs))
{
SendStreamingMessage(message);
await tcs.Task;
sw.Stop();
workerStatus.Latency = sw.Elapsed;
_workerChannelLogger.LogDebug($"[HostMonitor] Worker status request took {sw.ElapsedMilliseconds}ms");
}
}
// get the process stats for the worker
var workerProcessStats = _rpcWorkerProcess.GetStats();
workerStatus.ProcessStats = workerProcessStats;
if (workerProcessStats.CpuLoadHistory.Any())
{
string formattedLoadHistory = string.Join(",", workerProcessStats.CpuLoadHistory);
int executingFunctionCount = FunctionInputBuffers.Sum(p => p.Value.Count);
_workerChannelLogger.LogDebug($"[HostMonitor] Worker process stats: EffectiveCores={_environment.GetEffectiveCoresCount()}, ProcessId={_rpcWorkerProcess.Id}, ExecutingFunctions={executingFunctionCount}, CpuLoadHistory=({formattedLoadHistory}), AvgLoad={workerProcessStats.CpuLoadHistory.Average()}, MaxLoad={workerProcessStats.CpuLoadHistory.Max()}");
}
return workerStatus;
}
// send capabilities to worker, wait for WorkerInitResponse
internal void SendWorkerInitRequest(GrpcEvent startEvent)
{
_workerChannelLogger.LogDebug("Worker Process started. Received StartStream message");
_inboundWorkerEvents.Where(msg => msg.MessageType == MsgType.WorkerInitResponse)
.Timeout(workerInitTimeout)
.Take(1)
.Subscribe(WorkerInitResponse, HandleWorkerInitError);
WorkerInitRequest initRequest = GetWorkerInitRequest();
// Run as Functions Host V2 compatible
if (_environment.IsV2CompatibilityMode())
{
_workerChannelLogger.LogDebug("Worker and host running in V2 compatibility mode");
initRequest.Capabilities.Add(RpcWorkerConstants.V2Compatable, "true");
}
SendStreamingMessage(new StreamingMessage
{
WorkerInitRequest = initRequest
});
}
internal WorkerInitRequest GetWorkerInitRequest()
{
return new WorkerInitRequest()
{
HostVersion = ScriptHost.Version,
WorkerDirectory = _workerConfig.Description.WorkerDirectory
};
}
internal void FunctionEnvironmentReloadResponse(FunctionEnvironmentReloadResponse res, IDisposable latencyEvent)
{
_workerChannelLogger.LogDebug("Received FunctionEnvironmentReloadResponse");
if (res.Result.IsFailure(out Exception reloadEnvironmentVariablesException))
{
_workerChannelLogger.LogError(reloadEnvironmentVariablesException, "Failed to reload environment variables");
_reloadTask.SetException(reloadEnvironmentVariablesException);
}
_reloadTask.SetResult(true);
latencyEvent.Dispose();
}
internal void WorkerInitResponse(GrpcEvent initEvent)
{
_startLatencyMetric?.Dispose();
_startLatencyMetric = null;
_workerChannelLogger.LogDebug("Received WorkerInitResponse. Worker process initialized");
_initMessage = initEvent.Message.WorkerInitResponse;
_workerChannelLogger.LogDebug($"Worker capabilities: {_initMessage.Capabilities}");
if (_initMessage.Result.IsFailure(out Exception exc))
{
HandleWorkerInitError(exc);
_workerInitTask.SetResult(false);
return;
}
_state = _state | RpcWorkerChannelState.Initialized;
_workerCapabilities.UpdateCapabilities(_initMessage.Capabilities);
_isSharedMemoryDataTransferEnabled = IsSharedMemoryDataTransferEnabled();
_workerInitTask.SetResult(true);
}
public void SetupFunctionInvocationBuffers(IEnumerable<FunctionMetadata> functions)
{
_functions = functions;
foreach (FunctionMetadata metadata in functions)
{
_workerChannelLogger.LogDebug("Setting up FunctionInvocationBuffer for function:{functionName} with functionId:{id}", metadata.Name, metadata.GetFunctionId());
_functionInputBuffers[metadata.GetFunctionId()] = new BufferBlock<ScriptInvocationContext>();
}
_state = _state | RpcWorkerChannelState.InvocationBuffersInitialized;
}
public void SendFunctionLoadRequests(ManagedDependencyOptions managedDependencyOptions, TimeSpan? functionTimeout)
{
if (_functions != null)
{
if (functionTimeout.HasValue)
{
_functionLoadTimeout = functionTimeout.Value > _functionLoadTimeout ? functionTimeout.Value : _functionLoadTimeout;
_eventSubscriptions.Add(_inboundWorkerEvents.Where(msg => msg.MessageType == MsgType.FunctionLoadResponse)
.Timeout(_functionLoadTimeout)
.Take(_functions.Count())
.Subscribe((msg) => LoadResponse(msg.Message.FunctionLoadResponse), HandleWorkerFunctionLoadError));
}
else
{
_eventSubscriptions.Add(_inboundWorkerEvents.Where(msg => msg.MessageType == MsgType.FunctionLoadResponse)
.Subscribe((msg) => LoadResponse(msg.Message.FunctionLoadResponse), HandleWorkerFunctionLoadError));
}
foreach (FunctionMetadata metadata in _functions.OrderBy(metadata => metadata.IsDisabled()))
{
SendFunctionLoadRequest(metadata, managedDependencyOptions);
}
}
}
public Task SendFunctionEnvironmentReloadRequest()
{
_workerChannelLogger.LogDebug("Sending FunctionEnvironmentReloadRequest");
IDisposable latencyEvent = _metricsLogger.LatencyEvent(MetricEventNames.SpecializationEnvironmentReloadRequestResponse);
_eventSubscriptions
.Add(_inboundWorkerEvents.Where(msg => msg.MessageType == MsgType.FunctionEnvironmentReloadResponse)
.Timeout(workerInitTimeout)
.Take(1)
.Subscribe((msg) => FunctionEnvironmentReloadResponse(msg.Message.FunctionEnvironmentReloadResponse, latencyEvent), HandleWorkerEnvReloadError));
IDictionary processEnv = Environment.GetEnvironmentVariables();
FunctionEnvironmentReloadRequest request = GetFunctionEnvironmentReloadRequest(processEnv);
SendStreamingMessage(new StreamingMessage
{
FunctionEnvironmentReloadRequest = request
});
return _reloadTask.Task;
}
internal FunctionEnvironmentReloadRequest GetFunctionEnvironmentReloadRequest(IDictionary processEnv)
{
FunctionEnvironmentReloadRequest request = new FunctionEnvironmentReloadRequest();
foreach (DictionaryEntry entry in processEnv)
{
// Do not add environment variables with empty or null values (see issue #4488 for context)
if (!string.IsNullOrEmpty(entry.Value?.ToString()))
{
request.EnvironmentVariables.Add(entry.Key.ToString(), entry.Value.ToString());
}
}
request.EnvironmentVariables.Add(WorkerConstants.FunctionsWorkerDirectorySettingName, _workerConfig.Description.WorkerDirectory);
request.FunctionAppDirectory = _applicationHostOptions.CurrentValue.ScriptPath;
return request;
}
internal void SendFunctionLoadRequest(FunctionMetadata metadata, ManagedDependencyOptions managedDependencyOptions)
{
_functionLoadRequestResponseEvent = _metricsLogger.LatencyEvent(MetricEventNames.FunctionLoadRequestResponse);
_workerChannelLogger.LogDebug("Sending FunctionLoadRequest for function:{functionName} with functionId:{id}", metadata.Name, metadata.GetFunctionId());
// send a load request for the registered function
SendStreamingMessage(new StreamingMessage
{
FunctionLoadRequest = GetFunctionLoadRequest(metadata, managedDependencyOptions)
});
}
internal FunctionLoadRequest GetFunctionLoadRequest(FunctionMetadata metadata, ManagedDependencyOptions managedDependencyOptions)
{
FunctionLoadRequest request = new FunctionLoadRequest()
{
FunctionId = metadata.GetFunctionId(),
Metadata = new RpcFunctionMetadata()
{
Name = metadata.Name,
Directory = metadata.FunctionDirectory ?? string.Empty,
EntryPoint = metadata.EntryPoint ?? string.Empty,
ScriptFile = metadata.ScriptFile ?? string.Empty,
IsProxy = metadata.IsProxy()
}
};
if (managedDependencyOptions != null && managedDependencyOptions.Enabled)
{
_workerChannelLogger?.LogDebug($"Adding dependency download request to {_workerConfig.Description.Language} language worker");
request.ManagedDependencyEnabled = managedDependencyOptions.Enabled;
}
foreach (var binding in metadata.Bindings)
{
BindingInfo bindingInfo = binding.ToBindingInfo();
request.Metadata.Bindings.Add(binding.Name, bindingInfo);
}
return request;
}
internal void LoadResponse(FunctionLoadResponse loadResponse)
{
_functionLoadRequestResponseEvent?.Dispose();
_workerChannelLogger.LogDebug("Received FunctionLoadResponse for functionId:{functionId}", loadResponse.FunctionId);
if (loadResponse.Result.IsFailure(out Exception functionLoadEx))
{
if (functionLoadEx == null)
{
_workerChannelLogger?.LogError("Worker failed to function id {functionId}. Function load exception is not set by the worker", loadResponse.FunctionId);
}
else
{
_workerChannelLogger?.LogError(functionLoadEx, "Worker failed to function id {functionId}.", loadResponse.FunctionId);
}
//Cache function load errors to replay error messages on invoking failed functions
_functionLoadErrors[loadResponse.FunctionId] = functionLoadEx;
}
if (loadResponse.IsDependencyDownloaded)
{
_workerChannelLogger?.LogDebug($"Managed dependency successfully downloaded by the {_workerConfig.Description.Language} language worker");
}
// link the invocation inputs to the invoke call
var invokeBlock = new ActionBlock<ScriptInvocationContext>(async ctx => await SendInvocationRequest(ctx));
// associate the invocation input buffer with the function
var disposableLink = _functionInputBuffers[loadResponse.FunctionId].LinkTo(invokeBlock);
_inputLinks.Add(disposableLink);
}
internal async Task SendInvocationRequest(ScriptInvocationContext context)
{
try
{
if (_functionLoadErrors.ContainsKey(context.FunctionMetadata.GetFunctionId()))
{
_workerChannelLogger.LogDebug($"Function {context.FunctionMetadata.Name} failed to load");
context.ResultSource.TrySetException(_functionLoadErrors[context.FunctionMetadata.GetFunctionId()]);
_executingInvocations.TryRemove(context.ExecutionContext.InvocationId.ToString(), out ScriptInvocationContext _);
}
else
{
if (context.CancellationToken.IsCancellationRequested)
{
context.ResultSource.SetCanceled();
return;
}
var invocationRequest = await context.ToRpcInvocationRequest(_workerChannelLogger, _workerCapabilities, _isSharedMemoryDataTransferEnabled, _sharedMemoryManager);
_executingInvocations.TryAdd(invocationRequest.InvocationId, context);
SendStreamingMessage(new StreamingMessage
{
InvocationRequest = invocationRequest
});
}
}
catch (Exception invokeEx)
{
context.ResultSource.TrySetException(invokeEx);
}
}
private async Task<object> GetBindingDataAsync(ParameterBinding binding, string invocationId)
{
switch (binding.RpcDataCase)
{
case ParameterBindingType.RpcSharedMemory:
// Data was transferred by the worker using shared memory
return await binding.RpcSharedMemory.ToObjectAsync(_workerChannelLogger, invocationId, _sharedMemoryManager);
case ParameterBindingType.Data:
// Data was transferred by the worker using RPC
return binding.Data.ToObject();
default:
throw new InvalidOperationException("Unknown ParameterBindingType");
}
}
/// <summary>
/// From the output data produced by the worker, get a list of the shared memory maps that were created for this invocation.
/// </summary>
/// <param name="bindings">List of <see cref="ParameterBinding"/> produced by the worker as output.</param>
/// <returns>List of names of shared memory maps produced by the worker.</returns>
private IList<string> GetOutputMaps(IList<ParameterBinding> bindings)
{
IList<string> outputMaps = new List<string>();
foreach (ParameterBinding binding in bindings)
{
if (binding.RpcSharedMemory != null)
{
outputMaps.Add(binding.RpcSharedMemory.Name);
}
}
return outputMaps;
}
internal async Task InvokeResponse(InvocationResponse invokeResponse)
{
_workerChannelLogger.LogDebug("InvocationResponse received for invocation id: {Id}", invokeResponse.InvocationId);
if (_executingInvocations.TryRemove(invokeResponse.InvocationId, out ScriptInvocationContext context)
&& invokeResponse.Result.IsSuccess(context.ResultSource))
{
try
{
StringBuilder logBuilder = new StringBuilder();
bool usedSharedMemory = false;
foreach (ParameterBinding binding in invokeResponse.OutputData)
{
switch (binding.RpcDataCase)
{
case ParameterBindingType.RpcSharedMemory:
logBuilder.AppendFormat("{0}:{1},", binding.Name, binding.RpcSharedMemory.Count);
usedSharedMemory = true;
break;
default:
break;
}
}
if (usedSharedMemory)
{
_workerChannelLogger.LogDebug("Shared memory usage for response of invocation Id: {Id} is {SharedMemoryUsage}", invokeResponse.InvocationId, logBuilder.ToString());
}
IDictionary<string, object> bindingsDictionary = await invokeResponse.OutputData
.ToDictionaryAsync(binding => binding.Name, binding => GetBindingDataAsync(binding, invokeResponse.InvocationId));
var result = new ScriptInvocationResult()
{
Outputs = bindingsDictionary,
Return = invokeResponse?.ReturnValue?.ToObject()
};
context.ResultSource.SetResult(result);
}
catch (Exception responseEx)
{
context.ResultSource.TrySetException(responseEx);
}
finally
{
// Free memory allocated by the host (for input bindings)
if (!_sharedMemoryManager.TryFreeSharedMemoryMapsForInvocation(invokeResponse.InvocationId))
{
_workerChannelLogger.LogWarning($"Cannot free all shared memory resources for invocation: {invokeResponse.InvocationId}");
}
// List of shared memory maps that were produced by the worker (for output bindings)
IList<string> outputMaps = GetOutputMaps(invokeResponse.OutputData);
if (outputMaps.Count > 0)
{
// If this invocation was using any shared memory maps produced by the worker, close them to free memory
SendCloseSharedMemoryResourcesForInvocationRequest(outputMaps);
}
}
}
}
/// <summary>
/// Request to free memory allocated by the worker (for output bindings)
/// </summary>
/// <param name="outputMaps">List of names of shared memory maps to close from the worker.</param>
internal void SendCloseSharedMemoryResourcesForInvocationRequest(IList<string> outputMaps)
{
CloseSharedMemoryResourcesRequest closeSharedMemoryResourcesRequest = new CloseSharedMemoryResourcesRequest();
closeSharedMemoryResourcesRequest.MapNames.AddRange(outputMaps);
SendStreamingMessage(new StreamingMessage()
{
CloseSharedMemoryResourcesRequest = closeSharedMemoryResourcesRequest
});
}
internal void Log(GrpcEvent msg)
{
var rpcLog = msg.Message.RpcLog;
LogLevel logLevel = (LogLevel)rpcLog.Level;
if (_executingInvocations.TryGetValue(rpcLog.InvocationId, out ScriptInvocationContext context))
{
// Restore the execution context from the original invocation. This allows AsyncLocal state to flow to loggers.
System.Threading.ExecutionContext.Run(context.AsyncExecutionContext, (s) =>
{
if (rpcLog.Exception != null)
{
var exception = new Workers.Rpc.RpcException(rpcLog.Message, rpcLog.Exception.Message, rpcLog.Exception.StackTrace);
context.Logger.Log(logLevel, new EventId(0, rpcLog.EventId), rpcLog.Message, exception, (state, exc) => state);
}
else
{
context.Logger.Log(logLevel, new EventId(0, rpcLog.EventId), rpcLog.Message, null, (state, exc) => state);
}
}, null);
}
}
internal void SystemLog(GrpcEvent msg)
{
RpcLog systemLog = msg.Message.RpcLog;
LogLevel logLevel = (LogLevel)systemLog.Level;
switch (logLevel)
{
case LogLevel.Warning:
_workerChannelLogger.LogWarning(systemLog.Message);
break;
case LogLevel.Information:
_workerChannelLogger.LogInformation(systemLog.Message);
break;
case LogLevel.Error:
{
if (systemLog.Exception != null)
{
Workers.Rpc.RpcException exception = new Workers.Rpc.RpcException(systemLog.Message, systemLog.Exception.Message, systemLog.Exception.StackTrace);
_workerChannelLogger.LogError(exception, systemLog.Message);
}
else
{
_workerChannelLogger.LogError(systemLog.Message);
}
}
break;
default:
_workerChannelLogger.LogInformation(systemLog.Message);
break;
}
}
internal void HandleWorkerStartStreamError(Exception exc)
{
_workerChannelLogger.LogError(exc, "Starting worker process failed");
PublishWorkerErrorEvent(exc);
}
internal void HandleWorkerEnvReloadError(Exception exc)
{
_workerChannelLogger.LogError(exc, "Reloading environment variables failed");
_reloadTask.SetException(exc);
}
internal void HandleWorkerInitError(Exception exc)
{
_workerChannelLogger.LogError(exc, "Initializing worker process failed");
PublishWorkerErrorEvent(exc);
}
internal void HandleWorkerFunctionLoadError(Exception exc)
{
_workerChannelLogger.LogError(exc, "Loading function failed.");
if (_disposing || _disposed)
{
return;
}
_eventManager.Publish(new WorkerErrorEvent(_runtime, Id, exc));
}
private void PublishWorkerErrorEvent(Exception exc)
{
_workerInitTask.SetException(exc);
if (_disposing || _disposed)
{
return;
}
_eventManager.Publish(new WorkerErrorEvent(_runtime, Id, exc));
}
private void SendStreamingMessage(StreamingMessage msg)
{
_eventManager.Publish(new OutboundGrpcEvent(_workerId, msg));
}
internal void ReceiveWorkerStatusResponse(string requestId, WorkerStatusResponse response)
{
if (_workerStatusRequests.TryRemove(requestId, out var workerStatusTask))
{
workerStatusTask.SetResult(true);
}
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_startLatencyMetric?.Dispose();
_startSubscription?.Dispose();
// unlink function inputs
foreach (var link in _inputLinks)
{
link.Dispose();
}
(_rpcWorkerProcess as IDisposable)?.Dispose();
foreach (var sub in _eventSubscriptions)
{
sub.Dispose();
}
}
_disposed = true;
}
}
public void Dispose()
{
_disposing = true;
Dispose(true);
}
public async Task DrainInvocationsAsync()
{
_workerChannelLogger.LogDebug($"Count of in-buffer invocations waiting to be drained out: {_executingInvocations.Count}");
foreach (ScriptInvocationContext currContext in _executingInvocations.Values)
{
await currContext.ResultSource.Task;
}
}
public bool IsExecutingInvocation(string invocationId)
{
return _executingInvocations.ContainsKey(invocationId);
}
public bool TryFailExecutions(Exception workerException)
{
if (workerException == null)
{
return false;
}
foreach (ScriptInvocationContext currContext in _executingInvocations?.Values)
{
string invocationId = currContext?.ExecutionContext?.InvocationId.ToString();
_workerChannelLogger.LogDebug("Worker '{workerId}' encountered a fatal error. Failing invocation id: {Id}", _workerId, invocationId);
currContext?.ResultSource?.TrySetException(workerException);
_executingInvocations.TryRemove(invocationId, out ScriptInvocationContext _);
}
return true;
}
/// <summary>
/// Determine if shared memory transfer is enabled.
/// The following conditions must be met:
/// 1) <see cref="RpcWorkerConstants.FunctionsWorkerSharedMemoryDataTransferEnabledSettingName"/> must be set in environment variable (AppSetting).
/// 2) Worker must have the capability <see cref="RpcWorkerConstants.SharedMemoryDataTransfer"/>.
/// </summary>
/// <returns><see cref="true"/> if shared memory data transfer is enabled, <see cref="false"/> otherwise.</returns>
internal bool IsSharedMemoryDataTransferEnabled()
{
// Check if the environment variable (AppSetting) has this feature enabled
string envVal = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerSharedMemoryDataTransferEnabledSettingName);
if (string.IsNullOrEmpty(envVal))
{
return false;
}
bool envValEnabled = false;
if (bool.TryParse(envVal, out bool boolResult))
{
// Check if value was specified as a bool (true/false)
envValEnabled = boolResult;
}
else if (int.TryParse(envVal, out int intResult) && intResult == 1)
{
// Check if value was specified as an int (1/0)
envValEnabled = true;
}
if (!envValEnabled)
{
return false;
}
// Check if the worker supports this feature
bool capabilityEnabled = !string.IsNullOrEmpty(_workerCapabilities.GetCapabilityState(RpcWorkerConstants.SharedMemoryDataTransfer));
_workerChannelLogger.LogDebug("IsSharedMemoryDataTransferEnabled: {SharedMemoryDataTransferEnabled}", capabilityEnabled);
return capabilityEnabled;
}
}
}