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

Refactor func metadata to allow multiple sources #2834

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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;
Expand All @@ -23,56 +23,72 @@ public static string Emit(GeneratorExecutionContext context, IReadOnlyList<Gener
{
string functionMetadataInfo = AddFunctionMetadataInfo(funcMetadata, context.CancellationToken);

// Note: we still emit `IFunctionMetadataProvider` contract to avoid a breaking change, but it is no longer used for this extension point.
return $$"""
// <auto-generated/>
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace {{FunctionsUtil.GetNamespaceForGeneratedCode(context)}}
{
/// <summary>
/// Custom <see cref="IFunctionMetadataProvider"/> implementation that returns function metadata definitions for the current worker."/>
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class GeneratedFunctionMetadataProvider : IFunctionMetadataProvider
{
/// <inheritdoc/>
public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string directory)
{
var metadataList = new List<IFunctionMetadata>();
{{functionMetadataInfo}}
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

/// <summary>
/// Extension methods to enable registration of the custom <see cref="IFunctionMetadataProvider"/> implementation generated for the current worker.
/// </summary>
public static class WorkerHostBuilderFunctionMetadataProviderExtension
{
///<summary>
/// Adds the GeneratedFunctionMetadataProvider to the service collection.
/// During initialization, the worker will return generated function metadata instead of relying on the Azure Functions host for function indexing.
///</summary>
public static IHostBuilder ConfigureGeneratedFunctionMetadataProvider(this IHostBuilder builder)
{
builder.ConfigureServices(s =>
{
s.AddSingleton<IFunctionMetadataProvider, GeneratedFunctionMetadataProvider>();
});
return builder;
}
}{{GetAutoConfigureStartupClass(includeAutoRegistrationCode)}}
}
""";
// <auto-generated/>

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using IHostBuilder = global::Microsoft.Extensions.Hosting.IHostBuilder;
using WorkerOptions = global::Microsoft.Azure.Functions.Worker.WorkerOptions;
using IFunctionMetadataSource = global::Microsoft.Azure.Functions.Worker.Core.FunctionMetadata.IFunctionMetadataSource;
using IFunctionMetadata = global::Microsoft.Azure.Functions.Worker.Core.FunctionMetadata.IFunctionMetadata;
using DefaultFunctionMetadata = global::Microsoft.Azure.Functions.Worker.Core.FunctionMetadata.DefaultFunctionMetadata;

namespace {{FunctionsUtil.GetNamespaceForGeneratedCode(context)}}
{
/// <summary>
/// Custom <see cref="IFunctionMetadataSource"/> implementation that returns function metadata definitions for the current worker.
/// </summary>
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public class GeneratedFunctionMetadataProvider : IFunctionMetadataProvider, IFunctionMetadataSource
{
/// <summary>
/// Initializes a new instance of the <see cref="GeneratedFunctionMetadataProvider"/> class.
/// </summary>
public GeneratedFunctionMetadataProvider()
{
var metadataList = new global::System.Collections.Generic.List<IFunctionMetadata>();
{{functionMetadataInfo}}
Metadata = metadataList;
}

/// <inheritdoc/>
public global::System.Collections.Generic.IReadOnlyList<IFunctionMetadata> Metadata { get; }

/// <inheritdoc/>
[global::System.Runtime.ObsoleteAttribute("IFunctionMetadataProvider.GetFunctionMetadataAsync is no longer used for function indexing with this class. Use IFunctionMetadataSource.Metadata instead.")]
public global::System.Threading.Tasks.Task<global::System.Collections.Immutable.ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string directory)
{
return global::System.Threading.Tasks.Task.FromResult(global::System.Collections.Immutable.ImmutableArray.CreateRange(Metadata));
}
}

/// <summary>
/// Extension methods to enable registration of the custom <see cref="IFunctionMetadataSource"/> implementation generated for the current worker.
/// </summary>
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public static class WorkerHostBuilderFunctionMetadataProviderExtension
{
///<summary>
/// Adds the GeneratedFunctionMetadataProvider to the service collection.
/// During initialization, the worker will return generated function metadata instead of relying on the Azure Functions host for function indexing.
///</summary>
public static IHostBuilder ConfigureGeneratedFunctionMetadataProvider(this IHostBuilder builder)
{
builder.ConfigureServices(s =>
{
s.TryAddEnumerable(global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Singleton<IFunctionMetadataSource>(new GeneratedFunctionMetadataProvider()));
s.Configure<WorkerOptions>(options => options.Internal.DisableDefaultFunctionMetadata = true);
});
return builder;
}
}
{{GetAutoConfigureStartupClass(includeAutoRegistrationCode)}}
}

""";
}

private static string GetAutoConfigureStartupClass(bool includeAutoRegistrationCode)
Expand All @@ -81,22 +97,23 @@ private static string GetAutoConfigureStartupClass(bool includeAutoRegistrationC
{
string result = $$"""

/// <summary>
/// Auto startup class to register the custom <see cref="IFunctionMetadataProvider"/> implementation generated for the current worker.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public class FunctionMetadataProviderAutoStartup : global::Microsoft.Azure.Functions.Worker.IAutoConfigureStartup
{
/// <summary>
/// Configures the <see cref="IHostBuilder"/> to use the custom <see cref="IFunctionMetadataProvider"/> implementation generated for the current worker.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostBuilder"/> instance to use for service registration.</param>
public void Configure(IHostBuilder hostBuilder)
{
hostBuilder.ConfigureGeneratedFunctionMetadataProvider();
}
}
""";
/// <summary>
/// Auto startup class to register the custom <see cref="IFunctionMetadataSource"/> implementation generated for the current worker.
/// </summary>
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public class FunctionMetadataProviderAutoStartup : global::Microsoft.Azure.Functions.Worker.IAutoConfigureStartup
{
/// <summary>
/// Configures the <see cref="IHostBuilder"/> to use the custom <see cref="IFunctionMetadataSource"/> implementation generated for the current worker.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostBuilder"/> instance to use for service registration.</param>
public void Configure(IHostBuilder hostBuilder)
{
hostBuilder.ConfigureGeneratedFunctionMetadataProvider();
}
}
""";

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Microsoft.Azure.Functions.Worker.Core.FunctionMetadata
/// </summary>
public interface IFunctionMetadataProvider
{

/// <summary>
/// Gets all function metadata that this provider knows about asynchronously
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions src/DotNetWorker.Core/FunctionMetadata/IFunctionsMetadataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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;

namespace Microsoft.Azure.Functions.Worker.Core.FunctionMetadata
{
/// <summary>
/// A source for providing metadata.
/// </summary>
public interface IFunctionMetadataSource
{
/// <summary>
/// Gets the function metadata.
/// </summary>
IReadOnlyList<IFunctionMetadata> Metadata { get; }
}
}
18 changes: 18 additions & 0 deletions src/DotNetWorker.Core/Hosting/WorkerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Text.Json;
using Azure.Core.Serialization;
Expand All @@ -24,6 +25,12 @@ public class WorkerOptions
/// </summary>
public InputConverterCollection InputConverters { get; } = new InputConverterCollection();

/// <summary>
/// Gets the internal options.
/// </summary>
[Obsolete("This property is for internal use only and not expected to be called by user code.")]
public InternalOptions Internal { get; } = new InternalOptions();

/// <summary>
/// Gets the optional worker capabilities.
/// </summary>
Expand Down Expand Up @@ -81,5 +88,16 @@ private void SetBoolCapability(string name, bool value)
Capabilities.Remove(name);
}
}

/// <summary>
/// Options for internal configurations. Typically not expected to be set by users.
/// </summary>
public class InternalOptions
{
/// <summary>
/// Gets or sets a value indicating whether to disable providing default function metadata (from functions.metadata).
/// </summary>
public bool DisableDefaultFunctionMetadata { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,40 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata;
using Microsoft.Azure.Functions.Worker.Grpc.Messages;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.Functions.Worker
{
internal class DefaultFunctionMetadataProvider : IFunctionMetadataProvider
{
private const string FileName = "functions.metadata";
private JsonSerializerOptions deserializationOptions;

public DefaultFunctionMetadataProvider()
private readonly IEnumerable<IFunctionMetadataSource> _sources;
private readonly WorkerOptions _options;
private readonly JsonSerializerOptions _deserializationOptions = new() { PropertyNameCaseInsensitive = true };

public DefaultFunctionMetadataProvider(IEnumerable<IFunctionMetadataSource> sources, IOptions<WorkerOptions> options)
{
deserializationOptions = new JsonSerializerOptions();
deserializationOptions.PropertyNameCaseInsensitive = true;
_sources = sources;
_options = options.Value;
}

public virtual async Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string directory)
{
string metadataFile = Path.Combine(directory, FileName);
ImmutableArray<IFunctionMetadata>.Builder builder = ImmutableArray.CreateBuilder<IFunctionMetadata>();
builder.AddRange(await GetDefaultFunctionMetadataAsync(directory));

if (!File.Exists(metadataFile))
foreach (var source in _sources)
{
throw new FileNotFoundException($"Function metadata file not found. File path used:{metadataFile}");
builder.AddRange(source.Metadata);
}

using (var fs = File.OpenRead(metadataFile))
{
// deserialize as json element to preserve raw bindings
var jsonMetadataList = await JsonSerializer.DeserializeAsync<JsonElement>(fs);

var functionMetadataResults = new List<IFunctionMetadata>(jsonMetadataList.GetArrayLength());

foreach (var jsonMetadata in jsonMetadataList.EnumerateArray())
{
var functionMetadata = JsonSerializer.Deserialize<RpcFunctionMetadata>(jsonMetadata.GetRawText(), deserializationOptions);

if (functionMetadata is null)
{
throw new NullReferenceException("Function metadata could not be found.");
}

// hard-coded values that are checked for when the host validates functions
functionMetadata.IsProxy = false;
functionMetadata.Language = "dotnet-isolated";
functionMetadata.FunctionId = Guid.NewGuid().ToString();

var rawBindings = GetRawBindings(jsonMetadata);

foreach (var binding in rawBindings.EnumerateArray())
{
functionMetadata.RawBindings.Add(binding.GetRawText());
}

functionMetadataResults.Add(functionMetadata);
}

return functionMetadataResults.ToImmutableArray();
}
return builder.ToImmutable();
}

internal static JsonElement GetRawBindings(JsonElement jsonMetadata)
Expand All @@ -79,5 +53,49 @@ internal static JsonElement GetRawBindings(JsonElement jsonMetadata)

return bindingsJson;
}

private async Task<IEnumerable<IFunctionMetadata>> GetDefaultFunctionMetadataAsync(string directory)
{
#pragma warning disable CS0618 // Type or member is obsolete
if (_options.Internal.DisableDefaultFunctionMetadata)
{
return Enumerable.Empty<IFunctionMetadata>();
}
#pragma warning restore CS0618 // Type or member is obsolete

string metadataFile = Path.Combine(directory, FileName);
if (!File.Exists(metadataFile))
{
throw new FileNotFoundException($"Function metadata file not found. File path used:{metadataFile}");
}

using var fs = File.OpenRead(metadataFile);
// deserialize as json element to preserve raw bindings
var jsonMetadataList = await JsonSerializer.DeserializeAsync<JsonElement>(fs);
return ParseMetadata(jsonMetadataList);
}

private IEnumerable<IFunctionMetadata> ParseMetadata(JsonElement json)
{
foreach (var jsonMetadata in json.EnumerateArray())
{
var functionMetadata = JsonSerializer.Deserialize<RpcFunctionMetadata>(jsonMetadata.GetRawText(), _deserializationOptions)
?? throw new NullReferenceException("Function metadata could not be found.");

// hard-coded values that are checked for when the host validates functions
functionMetadata.IsProxy = false;
functionMetadata.Language = "dotnet-isolated";
functionMetadata.FunctionId = Guid.NewGuid().ToString();

var rawBindings = GetRawBindings(jsonMetadata);

foreach (var binding in rawBindings.EnumerateArray())
{
functionMetadata.RawBindings.Add(binding.GetRawText());
}

yield return functionMetadata;
}
}
}
}
Loading