-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CosmosClusteringProviderBuilder for Orleans clustering
Introduce CosmosClusteringProviderBuilder class implementing IProviderBuilder<ISiloBuilder> and IProviderBuilder<IClientBuilder> interfaces. Register the class as a provider for both "Silo" and "Client" roles. Implement Configure methods to set up Cosmos DB clustering options and client configuration. Import necessary namespaces to support the new functionality. This adds support for using the Cosmos DB Clustering Provider from Orleans IConfiguration setup.
- Loading branch information
1 parent
2c1aee2
commit ff3874c
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
src/Azure/Orleans.Clustering.Cosmos/CosmosClusteringProviderBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
})); | ||
} |