-
Notifications
You must be signed in to change notification settings - Fork 447
/
RpcWorkerConfigFactory.cs
332 lines (281 loc) · 16.2 KB
/
RpcWorkerConfigFactory.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
// 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.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Workers.Profiles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.WebJobs.Script.Workers.Rpc
{
// Gets fully configured WorkerConfigs from IWorkerProviders
internal class RpcWorkerConfigFactory
{
private readonly IConfiguration _config;
private readonly ILogger _logger;
private readonly ISystemRuntimeInformation _systemRuntimeInformation;
private readonly IWorkerProfileManager _profileManager;
private readonly IMetricsLogger _metricsLogger;
private readonly string _workerRuntime;
private readonly IEnvironment _environment;
private Dictionary<string, RpcWorkerConfig> _workerDescriptionDictionary = new Dictionary<string, RpcWorkerConfig>();
public RpcWorkerConfigFactory(IConfiguration config,
ILogger logger,
ISystemRuntimeInformation systemRuntimeInfo,
IEnvironment environment,
IMetricsLogger metricsLogger,
IWorkerProfileManager workerProfileManager)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_systemRuntimeInformation = systemRuntimeInfo ?? throw new ArgumentNullException(nameof(systemRuntimeInfo));
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_metricsLogger = metricsLogger ?? throw new ArgumentNullException(nameof(metricsLogger));
_profileManager = workerProfileManager ?? throw new ArgumentNullException(nameof(workerProfileManager));
_workerRuntime = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName);
WorkersDirPath = GetDefaultWorkersDirectory(Directory.Exists);
var workersDirectorySection = _config.GetSection($"{RpcWorkerConstants.LanguageWorkersSectionName}:{WorkerConstants.WorkersDirectorySectionName}");
if (!string.IsNullOrEmpty(workersDirectorySection.Value))
{
WorkersDirPath = workersDirectorySection.Value;
}
}
public string WorkersDirPath { get; }
public IList<RpcWorkerConfig> GetConfigs()
{
using (_metricsLogger.LatencyEvent(MetricEventNames.GetConfigs))
{
BuildWorkerProviderDictionary();
return _workerDescriptionDictionary.Values.ToList();
}
}
internal static string GetDefaultWorkersDirectory(Func<string, bool> directoryExists)
{
string assemblyLocalPath = Path.GetDirectoryName(new Uri(typeof(RpcWorkerConfigFactory).Assembly.CodeBase).LocalPath);
string workersDirPath = Path.Combine(assemblyLocalPath, RpcWorkerConstants.DefaultWorkersDirectoryName);
if (!directoryExists(workersDirPath))
{
// Site Extension. Default to parent directory
workersDirPath = Path.Combine(Directory.GetParent(assemblyLocalPath).FullName, RpcWorkerConstants.DefaultWorkersDirectoryName);
}
return workersDirPath;
}
internal void BuildWorkerProviderDictionary()
{
AddProviders();
AddProvidersFromAppSettings();
}
internal void AddProviders()
{
_logger.LogDebug("Workers Directory set to: {WorkersDirPath}", WorkersDirPath);
foreach (var workerDir in Directory.EnumerateDirectories(WorkersDirPath))
{
string workerConfigPath = Path.Combine(workerDir, RpcWorkerConstants.WorkerConfigFileName);
if (File.Exists(workerConfigPath))
{
AddProvider(workerDir);
}
}
}
internal void AddProvidersFromAppSettings()
{
var languagesSection = _config.GetSection($"{RpcWorkerConstants.LanguageWorkersSectionName}");
foreach (var languageSection in languagesSection.GetChildren())
{
var workerDirectorySection = languageSection.GetSection(WorkerConstants.WorkerDirectorySectionName);
if (workerDirectorySection.Value != null)
{
_workerDescriptionDictionary.Remove(languageSection.Key);
AddProvider(workerDirectorySection.Value);
}
}
}
internal void AddProvider(string workerDir)
{
using (_metricsLogger.LatencyEvent(string.Format(MetricEventNames.AddProvider, workerDir)))
{
try
{
string workerConfigPath = Path.Combine(workerDir, RpcWorkerConstants.WorkerConfigFileName);
if (!File.Exists(workerConfigPath))
{
_logger.LogDebug("Did not find worker config file at: {workerConfigPath}", workerConfigPath);
return;
}
_logger.LogDebug("Found worker config: {workerConfigPath}", workerConfigPath);
// Parse worker config file
string json = File.ReadAllText(workerConfigPath);
JObject workerConfig = JObject.Parse(json);
RpcWorkerDescription workerDescription = workerConfig.Property(WorkerConstants.WorkerDescription).Value.ToObject<RpcWorkerDescription>();
workerDescription.WorkerDirectory = workerDir;
//Read the profiles from worker description and load the profile for which the conditions match
JToken profiles = workerConfig.GetValue(WorkerConstants.WorkerDescriptionProfiles);
if (profiles != null)
{
List<WorkerDescriptionProfile> workerDescriptionProfiles = ReadWorkerDescriptionProfiles(profiles);
if (workerDescriptionProfiles.Count > 0)
{
_profileManager.SetWorkerDescriptionProfiles(workerDescriptionProfiles, workerDescription.Language);
_profileManager.LoadWorkerDescriptionFromProfiles(workerDescription, out workerDescription);
}
}
// Check if any app settings are provided for that language
var languageSection = _config.GetSection($"{RpcWorkerConstants.LanguageWorkersSectionName}:{workerDescription.Language}");
workerDescription.Arguments = workerDescription.Arguments ?? new List<string>();
GetWorkerDescriptionFromAppSettings(workerDescription, languageSection);
AddArgumentsFromAppSettings(workerDescription, languageSection);
// Validate workerDescription
workerDescription.ApplyDefaultsAndValidate(Directory.GetCurrentDirectory(), _logger);
if (workerDescription.IsDisabled == true)
{
_logger.LogInformation("Skipping WorkerConfig for stack: {language} since it is disabled.", workerDescription.Language);
return;
}
if (ShouldAddWorkerConfig(workerDescription.Language))
{
workerDescription.FormatWorkerPathIfNeeded(_systemRuntimeInformation, _environment, _logger);
workerDescription.FormatArgumentsIfNeeded(_logger);
workerDescription.ThrowIfFileNotExists(workerDescription.DefaultWorkerPath, nameof(workerDescription.DefaultWorkerPath));
workerDescription.ExpandEnvironmentVariables();
WorkerProcessCountOptions workerProcessCount = GetWorkerProcessCount(workerConfig);
var arguments = new WorkerProcessArguments()
{
ExecutablePath = workerDescription.DefaultExecutablePath,
WorkerPath = workerDescription.DefaultWorkerPath
};
arguments.ExecutableArguments.AddRange(workerDescription.Arguments);
var rpcWorkerConfig = new RpcWorkerConfig()
{
Description = workerDescription,
Arguments = arguments,
CountOptions = workerProcessCount,
};
_workerDescriptionDictionary[workerDescription.Language] = rpcWorkerConfig;
ReadLanguageWorkerFile(arguments.WorkerPath);
_logger.LogDebug("Added WorkerConfig for language: {language}", workerDescription.Language);
}
}
catch (Exception ex) when (!ex.IsFatal())
{
_logger.LogError(ex, "Failed to initialize worker provider for: {workerDir}", workerDir);
}
}
}
private List<WorkerDescriptionProfile> ReadWorkerDescriptionProfiles(JToken profilesJToken)
{
var profiles = profilesJToken.ToObject<IList<WorkerProfileDescriptor>>();
if (profiles == null || profiles.Count <= 0)
{
return new List<WorkerDescriptionProfile>(0);
}
var descriptionProfiles = new List<WorkerDescriptionProfile>(profiles.Count);
try
{
foreach (var profile in profiles)
{
var profileConditions = new List<IWorkerProfileCondition>(profile.Conditions.Count);
foreach (var descriptor in profile.Conditions)
{
if (!_profileManager.TryCreateWorkerProfileCondition(descriptor, out IWorkerProfileCondition condition))
{
// Failed to resolve condition. This profile will be disabled using a mock false condition
_logger.LogInformation("Profile {name} is disabled. Cannot resolve the profile condition {condition}", profile.ProfileName, descriptor.Type);
condition = new FalseCondition();
}
profileConditions.Add(condition);
}
descriptionProfiles.Add(new(profile.ProfileName, profileConditions, profile.Description));
}
}
catch (Exception)
{
throw new FormatException("Failed to parse profiles in worker config.");
}
return descriptionProfiles;
}
internal WorkerProcessCountOptions GetWorkerProcessCount(JObject workerConfig)
{
WorkerProcessCountOptions workerProcessCount = workerConfig.Property(WorkerConstants.ProcessCount)?.Value.ToObject<WorkerProcessCountOptions>();
workerProcessCount = workerProcessCount ?? new WorkerProcessCountOptions();
if (workerProcessCount.SetProcessCountToNumberOfCpuCores)
{
workerProcessCount.ProcessCount = _environment.GetEffectiveCoresCount();
// set Max worker process count to Number of effective cores if MaxProcessCount is less than MinProcessCount
workerProcessCount.MaxProcessCount = workerProcessCount.ProcessCount > workerProcessCount.MaxProcessCount ? workerProcessCount.ProcessCount : workerProcessCount.MaxProcessCount;
}
// Env variable takes precedence over worker.config
string processCountEnvSetting = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerProcessCountSettingName);
if (!string.IsNullOrEmpty(processCountEnvSetting))
{
workerProcessCount.ProcessCount = int.Parse(processCountEnvSetting) > 1 ? int.Parse(processCountEnvSetting) : 1;
}
// Validate
if (workerProcessCount.ProcessCount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(workerProcessCount.ProcessCount), "ProcessCount must be greater than 0.");
}
if (workerProcessCount.ProcessCount > workerProcessCount.MaxProcessCount)
{
throw new ArgumentException($"{nameof(workerProcessCount.ProcessCount)} must not be greater than {nameof(workerProcessCount.MaxProcessCount)}");
}
if (workerProcessCount.ProcessStartupInterval.Ticks < 0)
{
throw new ArgumentOutOfRangeException($"{nameof(workerProcessCount.ProcessStartupInterval)}", "The TimeSpan must not be negative.");
}
return workerProcessCount;
}
private static void GetWorkerDescriptionFromAppSettings(RpcWorkerDescription workerDescription, IConfigurationSection languageSection)
{
var defaultExecutablePathSetting = languageSection.GetSection($"{WorkerConstants.WorkerDescriptionDefaultExecutablePath}");
workerDescription.DefaultExecutablePath = defaultExecutablePathSetting.Value != null ? defaultExecutablePathSetting.Value : workerDescription.DefaultExecutablePath;
var defaultRuntimeVersionAppSetting = languageSection.GetSection($"{WorkerConstants.WorkerDescriptionDefaultRuntimeVersion}");
workerDescription.DefaultRuntimeVersion = defaultRuntimeVersionAppSetting.Value != null ? defaultRuntimeVersionAppSetting.Value : workerDescription.DefaultRuntimeVersion;
}
internal static void AddArgumentsFromAppSettings(RpcWorkerDescription workerDescription, IConfigurationSection languageSection)
{
var argumentsSection = languageSection.GetSection($"{WorkerConstants.WorkerDescriptionArguments}");
if (argumentsSection.Value != null)
{
((List<string>)workerDescription.Arguments).AddRange(Regex.Split(argumentsSection.Value, @"\s+"));
}
}
internal bool ShouldAddWorkerConfig(string workerDescriptionLanguage)
{
if (_environment.IsPlaceholderModeEnabled())
{
return true;
}
if (_environment.IsMultiLanguageRuntimeEnvironment())
{
_logger.LogInformation("Found multi-language runtime environment. Starting WorkerConfig for language: {workerDescriptionLanguage}", workerDescriptionLanguage);
return true;
}
if (!string.IsNullOrEmpty(_workerRuntime))
{
_logger.LogDebug("EnvironmentVariable {functionWorkerRuntimeSettingName}: {workerRuntime}", RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _workerRuntime);
if (_workerRuntime.Equals(workerDescriptionLanguage, StringComparison.OrdinalIgnoreCase))
{
return true;
}
// After specialization only create worker provider for the language set by FUNCTIONS_WORKER_RUNTIME env variable
_logger.LogInformation("{FUNCTIONS_WORKER_RUNTIME} set to {workerRuntime}. Skipping WorkerConfig for language: {workerDescriptionLanguage}", RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _workerRuntime, workerDescriptionLanguage);
return false;
}
return true;
}
private void ReadLanguageWorkerFile(string workerPath)
{
if (_environment.IsPlaceholderModeEnabled()
&& !string.IsNullOrEmpty(_workerRuntime)
&& File.Exists(workerPath))
{
// Read language worker file to avoid disk reads during specialization. This is only to page-in bytes.
File.ReadAllBytes(workerPath);
}
}
}
}