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

Microsoft.Extensions.Configuration support for Cosmos Clustering Provider #9204

Draft
wants to merge 3 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
17 changes: 0 additions & 17 deletions src/Azure/Orleans.Clustering.Cosmos/CosmosClusteringOptions.cs

This file was deleted.

115 changes: 115 additions & 0 deletions src/Azure/Orleans.Clustering.Cosmos/CosmosClusteringProviderBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Orleans;
using Orleans.Clustering.Cosmos;
using Orleans.Hosting;
using Orleans.Providers;

[assembly: RegisterProvider("Cosmos", "Clustering", "Silo", typeof(CosmosClusteringProviderBuilder))]
[assembly: RegisterProvider("Cosmos", "Clustering", "Client", typeof(CosmosClusteringProviderBuilder))]

namespace Orleans.Hosting;

internal sealed class CosmosClusteringProviderBuilder : IProviderBuilder<ISiloBuilder>, IProviderBuilder<IClientBuilder>
{
public void Configure(ISiloBuilder builder, string name, IConfigurationSection configurationSection) =>
builder.UseCosmosClustering(optionsBuilder =>
optionsBuilder.Configure<IServiceProvider>((options, services) =>
{
var databaseName = configurationSection[nameof(options.DatabaseName)];
if (!string.IsNullOrEmpty(databaseName))
{
options.DatabaseName = databaseName;
}
var containerName = configurationSection[nameof(options.ContainerName)];
if (!string.IsNullOrEmpty(containerName))
{
options.ContainerName = containerName;
}
if (bool.TryParse(configurationSection[nameof(options.IsResourceCreationEnabled)], out var irce))
{
options.IsResourceCreationEnabled = irce;
}
if(int.TryParse(configurationSection[nameof(options.DatabaseThroughput)], out var dt))
{
options.DatabaseThroughput = dt;
}
if(bool.TryParse(configurationSection[nameof(options.CleanResourcesOnInitialization)], out var croi))
{
options.CleanResourcesOnInitialization = croi;
}

var serviceKey = configurationSection["ServiceKey"];
if(!string.IsNullOrEmpty(serviceKey))
{
options.ConfigureCosmosClient(sp=>
new ValueTask<CosmosClient>(sp.GetRequiredKeyedService<CosmosClient>(serviceKey)));
}
else
{
var connectionName = configurationSection["ConnectionName"];
var connectionString = configurationSection["ConnectionString"];
if (!string.IsNullOrEmpty(connectionName) && string.IsNullOrEmpty(connectionString))
{
var rootConfiguration = services.GetRequiredService<IConfiguration>();
connectionString = rootConfiguration.GetConnectionString(connectionName);
}

if (!string.IsNullOrEmpty(connectionString))
{
options.ConfigureCosmosClient(connectionString);
}
}
}));

public void Configure(IClientBuilder builder, string name, IConfigurationSection configurationSection) =>
builder.UseCosmosGatewayListProvider(optionsBuilder =>
optionsBuilder.Configure<IServiceProvider>((options, services) =>
{
var databaseName = configurationSection[nameof(options.DatabaseName)];
if (!string.IsNullOrEmpty(databaseName))
{
options.DatabaseName = databaseName;
}
var containerName = configurationSection[nameof(options.ContainerName)];
if (!string.IsNullOrEmpty(containerName))
{
options.ContainerName = containerName;
}
if (bool.TryParse(configurationSection[nameof(options.IsResourceCreationEnabled)], out var irce))
{
options.IsResourceCreationEnabled = irce;
}
if (int.TryParse(configurationSection[nameof(options.DatabaseThroughput)], out var dt))
{
options.DatabaseThroughput = dt;
}
if (bool.TryParse(configurationSection[nameof(options.CleanResourcesOnInitialization)], out var croi))
{
options.CleanResourcesOnInitialization = croi;
}

var serviceKey = configurationSection["ServiceKey"];
if (!string.IsNullOrEmpty(serviceKey))
{
options.ConfigureCosmosClient(sp =>
new ValueTask<CosmosClient>(sp.GetRequiredKeyedService<CosmosClient>(serviceKey)));
}
else
{
// Construct a connection multiplexer from a connection string.
var connectionName = configurationSection["ConnectionName"];
var connectionString = configurationSection["ConnectionString"];
if (!string.IsNullOrEmpty(connectionName) && string.IsNullOrEmpty(connectionString))
{
var rootConfiguration = services.GetRequiredService<IConfiguration>();
connectionString = rootConfiguration.GetConnectionString(connectionName);
}

if (!string.IsNullOrEmpty(connectionString))
{
options.ConfigureCosmosClient(connectionString);
}
}
}));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Orleans.Clustering.Cosmos;

/// <summary>
/// Options for configuring Azure Cosmos DB clustering.
/// </summary>
public class CosmosClusteringOptions : CosmosOptions
{
private const string ORLEANS_CLUSTER_CONTAINER = "OrleansCluster";

/// <summary>
/// Initializes a new <see cref="CosmosClusteringOptions"/> instance.
/// </summary>
public CosmosClusteringOptions()
{
ContainerName = ORLEANS_CLUSTER_CONTAINER;
}
}

/// <summary>
/// Configuration validator for <see cref="CosmosClusteringOptions"/>.
/// </summary>
public class CosmosClusteringOptionsValidator : CosmosOptionsValidator<CosmosClusteringOptions>
{
/// <summary>
/// Initializes a new instance of the <see cref="CosmosClusteringOptionsValidator"/> class.
/// </summary>
/// <param name="options">The option to be validated.</param>
/// <param name="name">The option name to be validated.</param>
public CosmosClusteringOptionsValidator(CosmosClusteringOptions options, string name) : base(options, name)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Tester.AzureUtils")]
Loading