Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address LanguageWorkerOptions null in FunctionMetadataManager #10550

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/WebJobs.Script.WebHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsScriptRoot": "C:\\repos\\test\\func\\out\\bin\\Isolated\\debug"
},
"applicationUrl": "https://localhost:62513;http://localhost:62514"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,17 @@ public async Task<IEnumerable<Type>> GetExtensionsStartupTypesAsync()
bool isPrecompiledFunctionApp = false;

// dotnet app precompiled -> Do not use bundles
var workerConfigs = _languageWorkerOptions.CurrentValue.WorkerConfigs;
ExtensionRequirementsInfo extensionRequirements = GetExtensionRequirementsInfo();
ImmutableArray<FunctionMetadata> functionMetadataCollection = ImmutableArray<FunctionMetadata>.Empty;
if (bundleConfigured)
{
ExtensionBundleDetails bundleDetails = await _extensionBundleManager.GetExtensionBundleDetails();
ValidateBundleRequirements(bundleDetails, extensionRequirements);

functionMetadataCollection = _functionMetadataManager.GetFunctionMetadata(forceRefresh: true, includeCustomProviders: false, workerConfigs: workerConfigs);
functionMetadataCollection = _functionMetadataManager.GetFunctionMetadata(forceRefresh: true, includeCustomProviders: false);
bindingsSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

// Generate a Hashset of all the binding types used in the function app
// Generate a HashSet of all the binding types used in the function app
foreach (var functionMetadata in functionMetadataCollection)
{
foreach (var binding in functionMetadata.Bindings)
Expand All @@ -113,7 +112,7 @@ public async Task<IEnumerable<Type>> GetExtensionsStartupTypesAsync()
if (SystemEnvironment.Instance.IsPlaceholderModeEnabled())
{
// Do not move this.
// Calling this log statement in the placeholder mode to avoid jitting during specializtion
// Calling this log statement in the placeholder mode to avoid jitting during specialization
_logger.ScriptStartNotLoadingExtensionBundle("WARMUP_LOG_ONLY", bundleConfigured, isPrecompiledFunctionApp, isLegacyExtensionBundle, isDotnetIsolatedApp, isLogicApp);
}

Expand Down
64 changes: 41 additions & 23 deletions src/WebJobs.Script/Host/FunctionMetadataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Microsoft.Azure.WebJobs.Script.Configuration;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Diagnostics.Extensions;
using Microsoft.Azure.WebJobs.Script.Workers;
using Microsoft.Azure.WebJobs.Script.Workers.Http;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Extensions.Configuration;
Expand All @@ -23,24 +22,32 @@

namespace Microsoft.Azure.WebJobs.Script
{
public class FunctionMetadataManager : IFunctionMetadataManager
public sealed class FunctionMetadataManager : IFunctionMetadataManager, IDisposable
{
private const string FunctionConfigurationErrorMessage = "Unable to determine the primary function script. Make sure at least one script file is present. Try renaming your entry point script to 'run' or alternatively you can specify the name of the entry point script explicitly by adding a 'scriptFile' property to your function metadata.";
private const string MetadataProviderName = "Custom";
private readonly IServiceProvider _serviceProvider;
private IFunctionMetadataProvider _functionMetadataProvider;
private readonly IFunctionMetadataProvider _functionMetadataProvider;
private readonly IEnvironment _environment;

private LanguageWorkerOptions _languageOptions;
private IDisposable _onChangeSubscription;
private IOptions<ScriptJobHostOptions> _scriptOptions;
private ILogger _logger;
private bool _isHttpWorker;
private IEnvironment _environment;
private bool _servicesReset = false;
private ILogger _logger;
private IOptions<ScriptJobHostOptions> _scriptOptions;
private ImmutableArray<FunctionMetadata> _functionMetadataArray;
private Dictionary<string, ICollection<string>> _functionErrors = new Dictionary<string, ICollection<string>>();
private ConcurrentDictionary<string, FunctionMetadata> _functionMetadataMap = new ConcurrentDictionary<string, FunctionMetadata>(StringComparer.OrdinalIgnoreCase);

public FunctionMetadataManager(IOptions<ScriptJobHostOptions> scriptOptions, IFunctionMetadataProvider functionMetadataProvider,
IOptions<HttpWorkerOptions> httpWorkerOptions, IScriptHostManager scriptHostManager, ILoggerFactory loggerFactory,
IEnvironment environment)
public FunctionMetadataManager(
IOptions<ScriptJobHostOptions> scriptOptions,
IFunctionMetadataProvider functionMetadataProvider,
IOptions<HttpWorkerOptions> httpWorkerOptions,
IScriptHostManager scriptHostManager,
ILoggerFactory loggerFactory,
IEnvironment environment,
IOptionsMonitor<LanguageWorkerOptions> languageOptions)
{
_scriptOptions = scriptOptions;
_serviceProvider = scriptHostManager as IServiceProvider;
Expand All @@ -49,6 +56,8 @@ public FunctionMetadataManager(IOptions<ScriptJobHostOptions> scriptOptions, IFu
_isHttpWorker = httpWorkerOptions?.Value?.Description != null;
_environment = environment;

InitializeLanguageOptions(languageOptions);

// Every time script host is re-initializing, we also need to re-initialize
// services that change with the scope of the script host.
scriptHostManager.ActiveHostChanged += (s, e) =>
Expand All @@ -60,8 +69,10 @@ public FunctionMetadataManager(IOptions<ScriptJobHostOptions> scriptOptions, IFu
};
}

/// <inheritdoc />
public ImmutableDictionary<string, ImmutableArray<string>> Errors { get; private set; }

/// <inheritdoc />
public bool TryGetFunctionMetadata(string functionName, out FunctionMetadata functionMetadata, bool forceRefresh)
{
if (forceRefresh)
Expand All @@ -85,18 +96,32 @@ public bool TryGetFunctionMetadata(string functionName, out FunctionMetadata fun
/// <param name="applyAllowList">Apply functions allow list filter.</param>
/// <param name="includeCustomProviders">Include any metadata provided by IFunctionProvider when loading the metadata.</param>
/// <returns> An Immutable array of FunctionMetadata.</returns>
public ImmutableArray<FunctionMetadata> GetFunctionMetadata(bool forceRefresh, bool applyAllowList = true, bool includeCustomProviders = true, IList<RpcWorkerConfig> workerConfigs = null)
public ImmutableArray<FunctionMetadata> GetFunctionMetadata(bool forceRefresh, bool applyAllowList = true, bool includeCustomProviders = true)
{
if (forceRefresh || _servicesReset || _functionMetadataArray.IsDefaultOrEmpty)
{
_functionMetadataArray = LoadFunctionMetadata(forceRefresh, includeCustomProviders, workerConfigs: workerConfigs);
_functionMetadataArray = LoadFunctionMetadata(forceRefresh, includeCustomProviders);
_logger.FunctionMetadataManagerFunctionsLoaded(ApplyAllowList(_functionMetadataArray).Count());
_servicesReset = false;
}

return applyAllowList ? ApplyAllowList(_functionMetadataArray) : _functionMetadataArray;
}

/// <inheritdoc />
public void Dispose() => _onChangeSubscription.Dispose();

private void InitializeLanguageOptions(IOptionsMonitor<LanguageWorkerOptions> options)
{
_onChangeSubscription?.Dispose();
_languageOptions = options.CurrentValue;
_onChangeSubscription = options.OnChange(o =>
{
_languageOptions = o;
_servicesReset = true;
});
}

private ImmutableArray<FunctionMetadata> ApplyAllowList(ImmutableArray<FunctionMetadata> metadataList)
{
var allowList = _scriptOptions.Value?.Functions;
Expand All @@ -119,25 +144,18 @@ private void InitializeServices()
// Resetting the logger switches the logger scope to Script Host level,
// also making the logs available to Application Insights
_logger = _serviceProvider?.GetService<ILoggerFactory>().CreateLogger(LogCategories.Startup);
_servicesReset = true;
}

/// <summary>
/// This is the worker configuration created in the jobhost scope during placeholder initialization
/// This is used as a fallback incase the config is not passed down from previous method call.
/// </summary>
private IList<RpcWorkerConfig> GetFallbackWorkerConfig()
{
return _serviceProvider.GetService<IOptionsMonitor<LanguageWorkerOptions>>().CurrentValue.WorkerConfigs;
_onChangeSubscription.Dispose();
InitializeLanguageOptions(_serviceProvider.GetService<IOptionsMonitor<LanguageWorkerOptions>>());
_servicesReset = true;
}

/// <summary>
/// Read all functions and populate function metadata.
/// </summary>
internal ImmutableArray<FunctionMetadata> LoadFunctionMetadata(bool forceRefresh = false, bool includeCustomProviders = true, IFunctionInvocationDispatcher dispatcher = null, IList<RpcWorkerConfig> workerConfigs = null)
internal ImmutableArray<FunctionMetadata> LoadFunctionMetadata(bool forceRefresh = false, bool includeCustomProviders = true)
{
workerConfigs ??= GetFallbackWorkerConfig();

var workerConfigs = _languageOptions.WorkerConfigs;
_functionMetadataMap.Clear();

ICollection<string> functionsAllowList = _scriptOptions?.Value?.Functions;
Expand Down
5 changes: 1 addition & 4 deletions src/WebJobs.Script/Host/IFunctionMetadataManager.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Workers;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;

namespace Microsoft.Azure.WebJobs.Script
{
public interface IFunctionMetadataManager
{
ImmutableDictionary<string, ImmutableArray<string>> Errors { get; }

ImmutableArray<FunctionMetadata> GetFunctionMetadata(bool forceRefresh = false, bool applyAllowlist = true, bool includeCustomProviders = true, IList<RpcWorkerConfig> workerConfigs = null);
ImmutableArray<FunctionMetadata> GetFunctionMetadata(bool forceRefresh = false, bool applyAllowlist = true, bool includeCustomProviders = true);

bool TryGetFunctionMetadata(string functionName, out FunctionMetadata functionMetadata, bool forceRefresh = false);
}
Expand Down
8 changes: 3 additions & 5 deletions src/WebJobs.Script/Host/ScriptHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -373,13 +374,10 @@ private void LogHostFunctionErrors()
/// <returns>A metadata collection of functions and proxies configured.</returns>
private IEnumerable<FunctionMetadata> GetFunctionsMetadata()
{
IEnumerable<FunctionMetadata> functionMetadata;

functionMetadata = _functionMetadataManager.GetFunctionMetadata(forceRefresh: false, workerConfigs: _languageWorkerOptions.CurrentValue.WorkerConfigs);

ImmutableArray<FunctionMetadata> functionMetadata = _functionMetadataManager.GetFunctionMetadata(forceRefresh: false);
foreach (var error in _functionMetadataManager.Errors)
{
FunctionErrors.Add(error.Key, error.Value.ToArray());
FunctionErrors.Add(error.Key, error.Value);
}

return functionMetadata;
Expand Down
7 changes: 6 additions & 1 deletion test/WebJobs.Script.Tests.Integration/TestFunctionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.WebJobs.Script.Tests;
using Moq;
using Newtonsoft.Json.Linq;
using IApplicationLifetime = Microsoft.AspNetCore.Hosting.IApplicationLifetime;

Expand Down Expand Up @@ -480,12 +481,16 @@ private FunctionMetadataManager GetMetadataManager(IOptionsMonitor<ScriptApplica
WorkerConfigs = TestHelpers.GetTestWorkerConfigs()
};

var mockOptions = new Mock<IOptionsMonitor<LanguageWorkerOptions>>();
mockOptions.Setup(o => o.CurrentValue).Returns(workerOptions);
mockOptions.Setup(o => o.OnChange(It.IsAny<Action<LanguageWorkerOptions, string>>())).Returns(Mock.Of<IDisposable>());

var managerServiceProvider = manager as IServiceProvider;

var metadataProvider = new HostFunctionMetadataProvider(optionsMonitor, NullLogger<HostFunctionMetadataProvider>.Instance, new TestMetricsLogger(), SystemEnvironment.Instance);
var defaultProvider = new FunctionMetadataProvider(NullLogger<FunctionMetadataProvider>.Instance, null, metadataProvider, new OptionsWrapper<FunctionsHostingConfigOptions>(new FunctionsHostingConfigOptions()), SystemEnvironment.Instance);
var metadataManager = new FunctionMetadataManager(managerServiceProvider.GetService<IOptions<ScriptJobHostOptions>>(), defaultProvider,
managerServiceProvider.GetService<IOptions<HttpWorkerOptions>>(), manager, factory, environment);
managerServiceProvider.GetService<IOptions<HttpWorkerOptions>>(), manager, factory, environment, mockOptions.Object);

return metadataManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Grpc;
using Microsoft.Azure.WebJobs.Script.Workers.Http;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
Expand Down Expand Up @@ -64,7 +60,7 @@ public static FunctionMetadataManager GetFunctionMetadataManager(IOptions<Script
var source = new TestChangeTokenSource<ScriptApplicationHostOptions>();
var changeTokens = new[] { source };
var optionsMonitor = new OptionsMonitor<ScriptApplicationHostOptions>(factory, changeTokens, factory);
return new FunctionMetadataManager(jobHostOptions, functionMetadataProvider, httpOptions, managerMock.Object, loggerFactory, SystemEnvironment.Instance);
return new FunctionMetadataManager(jobHostOptions, functionMetadataProvider, httpOptions, managerMock.Object, loggerFactory, SystemEnvironment.Instance, languageWorkerOptions);
}

public static FunctionMetadataManager GetFunctionMetadataManagerWithDefaultHostConfig(IOptions<ScriptJobHostOptions> jobHostOptions,
Expand Down Expand Up @@ -98,7 +94,7 @@ public static FunctionMetadataManager GetFunctionMetadataManagerWithDefaultHostC
var source = new TestChangeTokenSource<ScriptApplicationHostOptions>();
var changeTokens = new[] { source };
var optionsMonitor = new OptionsMonitor<ScriptApplicationHostOptions>(factory, changeTokens, factory);
return new FunctionMetadataManager(jobHostOptions, functionMetadataProvider, httpOptions, managerMock.Object, loggerFactory, SystemEnvironment.Instance);
return new FunctionMetadataManager(jobHostOptions, functionMetadataProvider, httpOptions, managerMock.Object, loggerFactory, SystemEnvironment.Instance, languageWorkerOptions);
}
}
}
12 changes: 12 additions & 0 deletions test/WebJobs.Script.Tests.Shared/TestHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Workers;
using Microsoft.Azure.WebJobs.Script.Workers.Profiles;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -70,6 +72,7 @@ public static IHostBuilder ConfigureDefaultTestWebScriptHost(this IHostBuilder b
AddMockedSingleton<IFunctionDataCache>(services);
AddMockedSingleton<IAzureBlobStorageProvider>(services);
AddMockedSingleton<IAzureTableStorageProvider>(services);
services.AddSingleton<IWorkerProfileManager, WorkerProfileManager>();
services.AddSingleton<IDiagnosticEventRepository, TestDiagnosticEventRepository>();
services.AddSingleton<IDiagnosticEventRepositoryFactory, TestDiagnosticEventRepositoryFactory>();
services.AddSingleton<ISecretManagerProvider, TestSecretManagerProvider>();
Expand All @@ -79,6 +82,8 @@ public static IHostBuilder ConfigureDefaultTestWebScriptHost(this IHostBuilder b
services.AddLogging();
services.AddFunctionMetadataManager();
services.AddHostMetrics();
services.AddConfiguration();
services.ConfigureOptions<LanguageWorkerOptionsSetup>();

configureRootServices?.Invoke(services);

Expand Down Expand Up @@ -110,6 +115,13 @@ public static IServiceCollection AddMockedSingleton<T>(IServiceCollection servic
return services.AddSingleton<T>(mock.Object);
}

private static IServiceCollection AddConfiguration(this IServiceCollection services)
{
var builder = new ConfigurationBuilder();
services.AddSingleton<IConfiguration>(builder.Build());
return services;
}

private static IServiceCollection AddHostMetrics(this IServiceCollection services)
{
services.AddMetrics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Loggers;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Eventing;
using Microsoft.Azure.WebJobs.Script.Metrics;
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Workers;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -261,7 +259,7 @@ public MockMetadataManager(ICollection<FunctionMetadata> functions)
public ImmutableDictionary<string, ImmutableArray<string>> Errors =>
ImmutableDictionary<string, ImmutableArray<string>>.Empty;

public ImmutableArray<FunctionMetadata> GetFunctionMetadata(bool forceRefresh = false, bool applyAllowlist = true, bool includeCustomProviders = true, IList<RpcWorkerConfig> workerConfigs = null)
public ImmutableArray<FunctionMetadata> GetFunctionMetadata(bool forceRefresh = false, bool applyAllowlist = true, bool includeCustomProviders = true)
{
return _functions.ToImmutableArray();
}
Expand Down
Loading
Loading