-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathScriptInvocationContextExtensions.cs
175 lines (153 loc) · 7.25 KB
/
ScriptInvocationContextExtensions.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
// 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.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Grpc.Extensions;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.WebJobs.Script.Grpc
{
internal static class ScriptInvocationContextExtensions
{
public static async Task<InvocationRequest> ToRpcInvocationRequest(this ScriptInvocationContext context, ILogger logger, GrpcCapabilities capabilities, bool isSharedMemoryDataTransferEnabled, ISharedMemoryManager sharedMemoryManager)
{
bool excludeHttpTriggerMetadata = !string.IsNullOrEmpty(capabilities.GetCapabilityState(RpcWorkerConstants.RpcHttpTriggerMetadataRemoved));
var invocationRequest = new InvocationRequest
{
FunctionId = context.FunctionMetadata.GetFunctionId(),
InvocationId = context.ExecutionContext.InvocationId.ToString(),
TraceContext = GetRpcTraceContext(context.Traceparent, context.Tracestate, context.Attributes, logger),
};
var rpcValueCache = new Dictionary<object, TypedData>();
Dictionary<object, RpcSharedMemory> sharedMemValueCache = null;
StringBuilder logBuilder = null;
bool usedSharedMemory = false;
if (isSharedMemoryDataTransferEnabled)
{
sharedMemValueCache = new Dictionary<object, RpcSharedMemory>();
logBuilder = new StringBuilder();
}
foreach (var input in context.Inputs)
{
RpcSharedMemory sharedMemValue = null;
ParameterBinding parameterBinding = null;
if (isSharedMemoryDataTransferEnabled)
{
// Try to transfer this data over shared memory instead of RPC
if (input.val == null || !sharedMemValueCache.TryGetValue(input.val, out sharedMemValue))
{
sharedMemValue = await input.val.ToRpcSharedMemoryAsync(logger, invocationRequest.InvocationId, sharedMemoryManager);
if (input.val != null)
{
sharedMemValueCache.Add(input.val, sharedMemValue);
}
}
}
if (sharedMemValue != null)
{
// Data was successfully transferred over shared memory; create a ParameterBinding accordingly
parameterBinding = new ParameterBinding
{
Name = input.name,
RpcSharedMemory = sharedMemValue
};
usedSharedMemory = true;
logBuilder.AppendFormat("{0}:{1},", input.name, sharedMemValue.Count);
}
else
{
// Data was not transferred over shared memory (either disabled, type not supported or some error); resort to RPC
TypedData rpcValue = null;
if (input.val == null || !rpcValueCache.TryGetValue(input.val, out rpcValue))
{
rpcValue = await input.val.ToRpc(logger, capabilities);
if (input.val != null)
{
rpcValueCache.Add(input.val, rpcValue);
}
}
parameterBinding = new ParameterBinding
{
Name = input.name,
Data = rpcValue
};
}
invocationRequest.InputData.Add(parameterBinding);
}
foreach (var pair in context.BindingData)
{
if (ShouldSkipBindingData(pair, context, excludeHttpTriggerMetadata))
{
continue;
}
if (!rpcValueCache.TryGetValue(pair.Value, out TypedData rpcValue))
{
rpcValue = await pair.Value.ToRpc(logger, capabilities);
rpcValueCache.Add(pair.Value, rpcValue);
}
invocationRequest.TriggerMetadata.Add(pair.Key, rpcValue);
}
if (usedSharedMemory)
{
logger.LogDebug("Shared memory usage for request of invocation Id: {Id} is {SharedMemoryUsage}", invocationRequest.InvocationId, logBuilder.ToString());
}
return invocationRequest;
}
/// <summary>
/// Determine whether we can omit the specified binding data for performance.
/// </summary>
private static bool ShouldSkipBindingData(KeyValuePair<string, object> bindingData, ScriptInvocationContext context, bool excludeHttpTriggerMetadata)
{
if (bindingData.Value == null)
{
return true;
}
// if this is an http request and the worker declares that it handles exclusion
// of req/$request binding data members
if (excludeHttpTriggerMetadata && bindingData.Value is HttpRequest)
{
return true;
}
if (bindingData.Key.Equals("sys", StringComparison.OrdinalIgnoreCase) &&
bindingData.Value.GetType().Name.Equals("SystemBindingData", StringComparison.OrdinalIgnoreCase))
{
// The system binding data isn't RPC friendly. It's designed for in memory use in the binding
// pipeline (e.g. sys.RandGuid, etc.)
return true;
}
return false;
}
internal static RpcTraceContext GetRpcTraceContext(string activityId, string traceStateString, IEnumerable<KeyValuePair<string, string>> tags, ILogger logger)
{
RpcTraceContext traceContext = new RpcTraceContext
{
TraceParent = activityId ?? string.Empty,
TraceState = traceStateString ?? string.Empty,
};
foreach (KeyValuePair<string, string> tag in tags ?? Enumerable.Empty<KeyValuePair<string, string>>())
{
if (string.IsNullOrEmpty(tag.Value))
{
logger?.LogDebug($"Excluding {tag.Key} from being added to TraceContext.Attributes since it's value is null/empty");
}
else
{
if (traceContext.Attributes.ContainsKey(tag.Key))
{
logger?.LogWarning($"Overwriting '{tag.Key}' with existing value '{traceContext.Attributes[tag.Key]}' with '{tag.Value}'");
}
traceContext.Attributes[tag.Key] = tag.Value;
}
}
return traceContext;
}
}
}