-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathLegacyClusterConfigurationExtensions.cs
98 lines (83 loc) · 4.68 KB
/
LegacyClusterConfigurationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.Runtime;
using Orleans.Runtime.Configuration;
using Orleans.Runtime.MembershipService;
namespace Orleans.Hosting
{
internal static class LegacyClusterConfigurationExtensions
{
public static IServiceCollection AddLegacyClusterConfigurationSupport(this IServiceCollection services, ClusterConfiguration configuration)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (services.Any(service => service.ServiceType == typeof(ClusterConfiguration)))
{
throw new InvalidOperationException("Cannot configure legacy ClusterConfiguration support twice");
}
// these will eventually be removed once our code doesn't depend on the old ClientConfiguration
services.AddSingleton(configuration);
services.TryAddSingleton<SiloInitializationParameters>();
services.TryAddFromExisting<ILocalSiloDetails, SiloInitializationParameters>();
services.TryAddSingleton(sp => sp.GetRequiredService<SiloInitializationParameters>().ClusterConfig);
services.TryAddSingleton(sp => sp.GetRequiredService<SiloInitializationParameters>().ClusterConfig.Globals);
services.TryAddTransient(sp => sp.GetRequiredService<SiloInitializationParameters>().NodeConfig);
services.TryAddSingleton<Factory<NodeConfiguration>>(
sp =>
{
var initializationParams = sp.GetRequiredService<SiloInitializationParameters>();
return () => initializationParams.NodeConfig;
});
services.TryAddFromExisting<IMessagingConfiguration, GlobalConfiguration>();
// Add legacy node configuration, based from the instance returned from the above registered factory
services.AddLegacyNodeConfigurationSupport();
// Translate legacy configuration to new Options
services.Configure<SiloMessagingOptions>(options =>
{
LegacyConfigurationExtensions.CopyCommonMessagingOptions(configuration.Globals, options);
options.SiloSenderQueues = configuration.Globals.SiloSenderQueues;
options.GatewaySenderQueues = configuration.Globals.GatewaySenderQueues;
options.MaxForwardCount = configuration.Globals.MaxForwardCount;
options.ClientDropTimeout = configuration.Globals.ClientDropTimeout;
});
services.Configure<SerializationProviderOptions>(options =>
{
options.SerializationProviders = configuration.Globals.SerializationProviders;
options.FallbackSerializationProvider = configuration.Globals.FallbackSerializationProvider;
});
services.Configure<IOptions<SiloIdentityOptions>, GrainClassOptions>((identityOptions, options) =>
{
var nodeConfig = configuration.GetOrCreateNodeConfigurationForSilo(identityOptions.Value.SiloName);
options.ExcludedGrainTypes.AddRange(nodeConfig.ExcludedGrainTypes);
});
LegacyMembershipConfigurator.ConfigureServices(configuration.Globals, services);
return services;
}
internal static void Configure<TService, TOptions>(this IServiceCollection services, Action<TService, TOptions> configure) where TOptions : class
{
services.AddTransient<IConfigureOptions<TOptions>>(sp => new ServiceBasedConfigurator<TService, TOptions>(sp.GetRequiredService<TService>(), configure));
}
private static IServiceCollection AddLegacyNodeConfigurationSupport(this IServiceCollection services)
{
services.Configure<NodeConfiguration, StatisticsOptions>((configuration, options) =>
{
LegacyConfigurationExtensions.CopyStatisticsOptions(configuration, options);
});
return services;
}
private class ServiceBasedConfigurator<TService, TOptions> : IConfigureOptions<TOptions> where TOptions : class
{
private readonly Action<TService, TOptions> configure;
private readonly TService service;
public ServiceBasedConfigurator(TService service, Action<TService, TOptions> configure)
{
this.service = service;
this.configure = configure;
}
public void Configure(TOptions options) => this.configure(this.service, options);
}
}
}