Skip to content

Commit

Permalink
Microsoft.Extensions.Configuration support (#8764)
Browse files Browse the repository at this point in the history
* WIP - Use IConfiguration to configure silos and clients, including providers

* More wip

* BROKEN WIP

* more

* More providers

* Test fixes

* Add explainer comment

* Fix HeterogeneousTests

* Add missing Redis Client clustering directive

* Remove playground projects
  • Loading branch information
ReubenBond committed Jan 9, 2024
1 parent c597141 commit cab108e
Show file tree
Hide file tree
Showing 89 changed files with 1,988 additions and 202 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<!-- System packages -->
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageVersion Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageVersion Include="System.IO.Hashing" Version="8.0.0" NoWarn="NU5104" />
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
Expand All @@ -28,6 +29,7 @@
<PackageVersion Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
<PackageVersion Include="Microsoft.AspNetCore.Connections.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
Expand Down
@@ -0,0 +1,147 @@
using System;
using Microsoft.Extensions.Configuration;
using Orleans;
using Orleans.Hosting;
using Orleans.Providers;

[assembly: RegisterProvider("DynamoDB", "Clustering", "Silo", typeof(DynamoDBClusteringProviderBuilder))]
[assembly: RegisterProvider("DynamoDB", "Clustering", "Client", typeof(DynamoDBClusteringProviderBuilder))]

namespace Orleans.Hosting;

internal sealed class DynamoDBClusteringProviderBuilder : IProviderBuilder<ISiloBuilder>, IProviderBuilder<IClientBuilder>
{
public void Configure(ISiloBuilder builder, string name, IConfigurationSection configurationSection)
{
builder.UseDynamoDBClustering(options =>
{
var accessKey = configurationSection[nameof(options.AccessKey)];
if (!string.IsNullOrEmpty(accessKey))
{
options.AccessKey = accessKey;
}
var secretKey = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(secretKey))
{
options.SecretKey = secretKey;
}
var region = configurationSection[nameof(options.Service)] ?? configurationSection["Region"];
if (!string.IsNullOrEmpty(region))
{
options.Service = region;
}
var token = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(token))
{
options.Token = token;
}
var profileName = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(profileName))
{
options.ProfileName = profileName;
}
var tableName = configurationSection[nameof(options.TableName)];
if (!string.IsNullOrEmpty(tableName))
{
options.TableName = tableName;
}
if (int.TryParse(configurationSection[nameof(options.ReadCapacityUnits)], out var rcu))
{
options.ReadCapacityUnits = rcu;
}
if (int.TryParse(configurationSection[nameof(options.WriteCapacityUnits)], out var wcu))
{
options.WriteCapacityUnits = wcu;
}
if (bool.TryParse(configurationSection[nameof(options.UseProvisionedThroughput)], out var upt))
{
options.UseProvisionedThroughput = upt;
}
if (bool.TryParse(configurationSection[nameof(options.CreateIfNotExists)], out var cine))
{
options.CreateIfNotExists = cine;
}
if (bool.TryParse(configurationSection[nameof(options.UpdateIfExists)], out var uie))
{
options.UpdateIfExists = uie;
}
});
}

public void Configure(IClientBuilder builder, string name, IConfigurationSection configurationSection)
{
builder.UseDynamoDBClustering(options =>
{
var accessKey = configurationSection[nameof(options.AccessKey)];
if (!string.IsNullOrEmpty(accessKey))
{
options.AccessKey = accessKey;
}
var secretKey = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(secretKey))
{
options.SecretKey = secretKey;
}
var region = configurationSection[nameof(options.Service)] ?? configurationSection["Region"];
if (!string.IsNullOrEmpty(region))
{
options.Service = region;
}
var token = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(token))
{
options.Token = token;
}
var profileName = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(profileName))
{
options.ProfileName = profileName;
}
var tableName = configurationSection[nameof(options.TableName)];
if (!string.IsNullOrEmpty(tableName))
{
options.TableName = tableName;
}
if (int.TryParse(configurationSection[nameof(options.ReadCapacityUnits)], out var rcu))
{
options.ReadCapacityUnits = rcu;
}
if (int.TryParse(configurationSection[nameof(options.WriteCapacityUnits)], out var wcu))
{
options.WriteCapacityUnits = wcu;
}
if (bool.TryParse(configurationSection[nameof(options.UseProvisionedThroughput)], out var upt))
{
options.UseProvisionedThroughput = upt;
}
if (bool.TryParse(configurationSection[nameof(options.CreateIfNotExists)], out var cine))
{
options.CreateIfNotExists = cine;
}
if (bool.TryParse(configurationSection[nameof(options.UpdateIfExists)], out var uie))
{
options.UpdateIfExists = uie;
}
});
}
}
@@ -0,0 +1,107 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;
using Orleans.Providers;
using Orleans.Storage;

[assembly: RegisterProvider("DynamoDB", "GrainStorage", "Silo", typeof(DynamoDBGrainStorageProviderBuilder))]

namespace Orleans.Hosting;

internal sealed class DynamoDBGrainStorageProviderBuilder : IProviderBuilder<ISiloBuilder>
{
public void Configure(ISiloBuilder builder, string name, IConfigurationSection configurationSection)
{
builder.AddDynamoDBGrainStorage(
name,
(OptionsBuilder<DynamoDBStorageOptions> optionsBuilder) => optionsBuilder.Configure<IServiceProvider>((options, services) =>
{
var accessKey = configurationSection[nameof(options.AccessKey)];
if (!string.IsNullOrEmpty(accessKey))
{
options.AccessKey = accessKey;
}
var secretKey = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(secretKey))
{
options.SecretKey = secretKey;
}
var region = configurationSection[nameof(options.Service)] ?? configurationSection["Region"];
if (!string.IsNullOrEmpty(region))
{
options.Service = region;
}
var token = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(token))
{
options.Token = token;
}
var profileName = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(profileName))
{
options.ProfileName = profileName;
}
var serviceId = configurationSection[nameof(options.ServiceId)];
if (!string.IsNullOrEmpty(serviceId))
{
options.ServiceId = serviceId;
}
var tableName = configurationSection[nameof(options.TableName)];
if (!string.IsNullOrEmpty(tableName))
{
options.TableName = tableName;
}
if (int.TryParse(configurationSection[nameof(options.ReadCapacityUnits)], out var rcu))
{
options.ReadCapacityUnits = rcu;
}
if (int.TryParse(configurationSection[nameof(options.WriteCapacityUnits)], out var wcu))
{
options.WriteCapacityUnits = wcu;
}
if (bool.TryParse(configurationSection[nameof(options.UseProvisionedThroughput)], out var upt))
{
options.UseProvisionedThroughput = upt;
}
if (bool.TryParse(configurationSection[nameof(options.CreateIfNotExists)], out var cine))
{
options.CreateIfNotExists = cine;
}
if (bool.TryParse(configurationSection[nameof(options.UpdateIfExists)], out var uie))
{
options.UpdateIfExists = uie;
}
if (bool.TryParse(configurationSection[nameof(options.DeleteStateOnClear)], out var dsoc))
{
options.DeleteStateOnClear = dsoc;
}
if (TimeSpan.TryParse(configurationSection[nameof(options.TimeToLive)], out var ttl))
{
options.TimeToLive = ttl;
}
var serializerKey = configurationSection["SerializerKey"];
if (!string.IsNullOrEmpty(serializerKey))
{
options.GrainStorageSerializer = services.GetRequiredKeyedService<IGrainStorageSerializer>(serializerKey);
}
}));
}
}
@@ -0,0 +1,78 @@
using Microsoft.Extensions.Configuration;
using Orleans;
using Orleans.Hosting;
using Orleans.Providers;

[assembly: RegisterProvider("DynamoDB", "Reminders", "Silo", typeof(DynamoDBRemindersProviderBuilder))]

namespace Orleans.Hosting;

internal sealed class DynamoDBRemindersProviderBuilder : IProviderBuilder<ISiloBuilder>
{
public void Configure(ISiloBuilder builder, string name, IConfigurationSection configurationSection)
{
builder.UseDynamoDBReminderService(options =>
{
var accessKey = configurationSection[nameof(options.AccessKey)];
if (!string.IsNullOrEmpty(accessKey))
{
options.AccessKey = accessKey;
}
var secretKey = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(secretKey))
{
options.SecretKey = secretKey;
}
var region = configurationSection[nameof(options.Service)] ?? configurationSection["Region"];
if (!string.IsNullOrEmpty(region))
{
options.Service = region;
}
var token = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(token))
{
options.Token = token;
}
var profileName = configurationSection[nameof(options.SecretKey)];
if (!string.IsNullOrEmpty(profileName))
{
options.ProfileName = profileName;
}
var tableName = configurationSection[nameof(options.TableName)];
if (!string.IsNullOrEmpty(tableName))
{
options.TableName = tableName;
}
if (int.TryParse(configurationSection[nameof(options.ReadCapacityUnits)], out var rcu))
{
options.ReadCapacityUnits = rcu;
}
if (int.TryParse(configurationSection[nameof(options.WriteCapacityUnits)], out var wcu))
{
options.WriteCapacityUnits = wcu;
}
if (bool.TryParse(configurationSection[nameof(options.UseProvisionedThroughput)], out var upt))
{
options.UseProvisionedThroughput = upt;
}
if (bool.TryParse(configurationSection[nameof(options.CreateIfNotExists)], out var cine))
{
options.CreateIfNotExists = cine;
}
if (bool.TryParse(configurationSection[nameof(options.UpdateIfExists)], out var uie))
{
options.UpdateIfExists = uie;
}
});
}
}

0 comments on commit cab108e

Please sign in to comment.