From bd55d72f12897026439683c9eac9b9910947aa8a Mon Sep 17 00:00:00 2001 From: Reuben Bond Date: Wed, 25 Apr 2018 05:36:29 +1000 Subject: [PATCH] Clarify UseDevelopmentClustering and UseLocalhostClustering (#4438) * Try to clarify meaning of UseDevelopmentClustering and UseLocalhostClustering, plus include ServiceId in calls * Remove ClusterId/ServiceId from UseDevelopmentClustering/UseLocalhostClustering extensions * Only configure default ClusterId/ServiceId in UseLocalhostClustering * Add ServiceId & ClusterId to UseLocalhostClustering extensions * fix build --- .../Configuration/Options/ClusterOptions.cs | 5 ++ .../Core/ClientBuilderExtensions.cs | 39 +++++++++++---- .../LegacyMembershipConfigurator.cs | 3 +- .../Hosting/CoreHostingExtensions.cs | 49 ++++++++++--------- test/Tester/LocalhostSiloTests.cs | 2 +- 5 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/Orleans.Core/Configuration/Options/ClusterOptions.cs b/src/Orleans.Core/Configuration/Options/ClusterOptions.cs index f832e7a619..d07ed7a128 100644 --- a/src/Orleans.Core/Configuration/Options/ClusterOptions.cs +++ b/src/Orleans.Core/Configuration/Options/ClusterOptions.cs @@ -12,6 +12,11 @@ public class ClusterOptions /// internal const string DevelopmentClusterId = "dev"; + /// + /// Default service id for development clusters. + /// + internal const string DevelopmentServiceId = "dev"; + /// /// Gets or sets the cluster identity. This used to be called DeploymentId before Orleans 2.0 name. /// diff --git a/src/Orleans.Core/Core/ClientBuilderExtensions.cs b/src/Orleans.Core/Core/ClientBuilderExtensions.cs index 7602d9b414..396d6517da 100644 --- a/src/Orleans.Core/Core/ClientBuilderExtensions.cs +++ b/src/Orleans.Core/Core/ClientBuilderExtensions.cs @@ -165,30 +165,49 @@ public static IClientBuilder ConfigureLogging(this IClientBuilder builder, Actio /// /// /// The local silo's gateway port. - /// Cluster ID to use + /// The service id. + /// The cluster id. public static IClientBuilder UseLocalhostClustering( this IClientBuilder builder, int gatewayPort = 30000, + string serviceId = ClusterOptions.DevelopmentServiceId, string clusterId = ClusterOptions.DevelopmentClusterId) { - return builder.UseStaticClustering(new IPEndPoint(IPAddress.Loopback, gatewayPort)) - .Configure(options => - { - if (!string.IsNullOrWhiteSpace(clusterId)) options.ClusterId = clusterId; - }); + return builder.UseLocalhostClustering(new [] {gatewayPort}, serviceId, clusterId); } /// /// Configures the client to connect to a silo on the localhost. /// /// - /// The local silo gateway port. - public static IClientBuilder UseLocalhostClustering(this IClientBuilder builder, params int[] gatewayPorts) + /// The local silo gateway ports. + /// The service id. + /// The cluster id. + public static IClientBuilder UseLocalhostClustering(this IClientBuilder builder, + int[] gatewayPorts, + string serviceId = ClusterOptions.DevelopmentServiceId, + string clusterId = ClusterOptions.DevelopmentClusterId) { return builder.UseStaticClustering(gatewayPorts.Select(p => new IPEndPoint(IPAddress.Loopback, p)).ToArray()) - .Configure(options => + .ConfigureServices(services => { - options.ClusterId = ClusterOptions.DevelopmentClusterId; + // If the caller did not override service id or cluster id, configure default values as a fallback. + if (string.Equals(serviceId, ClusterOptions.DevelopmentServiceId) && string.Equals(clusterId, ClusterOptions.DevelopmentClusterId)) + { + services.PostConfigure(options => + { + if (string.IsNullOrWhiteSpace(options.ClusterId)) options.ClusterId = ClusterOptions.DevelopmentClusterId; + if (string.IsNullOrWhiteSpace(options.ServiceId)) options.ServiceId = ClusterOptions.DevelopmentServiceId; + }); + } + else + { + services.Configure(options => + { + options.ServiceId = serviceId; + options.ClusterId = clusterId; + }); + } }); } diff --git a/src/Orleans.Runtime.Legacy/Configuration/LegacyMembershipConfigurator.cs b/src/Orleans.Runtime.Legacy/Configuration/LegacyMembershipConfigurator.cs index fd194464b8..027f16fd74 100644 --- a/src/Orleans.Runtime.Legacy/Configuration/LegacyMembershipConfigurator.cs +++ b/src/Orleans.Runtime.Legacy/Configuration/LegacyMembershipConfigurator.cs @@ -70,8 +70,7 @@ public void Configure(object configuration, ISiloHostBuilder builder) { options.PrimarySiloEndpoint = config.SeedNodes?.FirstOrDefault(); } - }, - config.ClusterId); + }); } } } diff --git a/src/Orleans.Runtime/Hosting/CoreHostingExtensions.cs b/src/Orleans.Runtime/Hosting/CoreHostingExtensions.cs index 22a0e52a7d..073b7f8d7f 100644 --- a/src/Orleans.Runtime/Hosting/CoreHostingExtensions.cs +++ b/src/Orleans.Runtime/Hosting/CoreHostingExtensions.cs @@ -39,7 +39,7 @@ public static ISiloHostBuilder ConfigureDefaults(this ISiloHostBuilder builder) } /// - /// Configures a localhost silo for development and testing. + /// Configures the silo to use development-only clustering and listen on localhost. /// /// The silo builder. /// The silo port. @@ -47,13 +47,15 @@ public static ISiloHostBuilder ConfigureDefaults(this ISiloHostBuilder builder) /// /// The endpoint of the primary silo, or to use this silo as the primary. /// - /// Cluster ID + /// The service id. + /// The cluster id. /// The silo builder. public static ISiloHostBuilder UseLocalhostClustering( this ISiloHostBuilder builder, int siloPort = EndpointOptions.DEFAULT_SILO_PORT, int gatewayPort = EndpointOptions.DEFAULT_GATEWAY_PORT, IPEndPoint primarySiloEndpoint = null, + string serviceId = ClusterOptions.DevelopmentServiceId, string clusterId = ClusterOptions.DevelopmentClusterId) { builder.Configure(options => @@ -64,8 +66,27 @@ public static ISiloHostBuilder ConfigureDefaults(this ISiloHostBuilder builder) }); builder.UseDevelopmentClustering(primarySiloEndpoint ?? new IPEndPoint(IPAddress.Loopback, siloPort)); - builder.Configure(options => options.ClusterId = clusterId); builder.Configure(options => options.ExpectedClusterSize = 1); + builder.ConfigureServices(services => + { + // If the caller did not override service id or cluster id, configure default values as a fallback. + if (string.Equals(serviceId, ClusterOptions.DevelopmentServiceId) && string.Equals(clusterId, ClusterOptions.DevelopmentClusterId)) + { + services.PostConfigure(options => + { + if (string.IsNullOrWhiteSpace(options.ClusterId)) options.ClusterId = ClusterOptions.DevelopmentClusterId; + if (string.IsNullOrWhiteSpace(options.ServiceId)) options.ServiceId = ClusterOptions.DevelopmentServiceId; + }); + } + else + { + services.Configure(options => + { + options.ServiceId = serviceId; + options.ClusterId = clusterId; + }); + } + }); return builder; } @@ -88,24 +109,9 @@ public static ISiloHostBuilder UseDevelopmentClustering(this ISiloHostBuilder bu /// public static ISiloHostBuilder UseDevelopmentClustering( this ISiloHostBuilder builder, - Action configureOptions, - string clusterId = ClusterOptions.DevelopmentClusterId) + Action configureOptions) { - return builder - .Configure(options => options.ClusterId = clusterId) - .ConfigureServices( - services => - { - if (configureOptions != null) - { - services.Configure(configureOptions); - } - - services.ConfigureFormatter(); - services - .AddSingleton() - .AddFromExisting(); - }); + return builder.UseDevelopmentClustering(options => options.Configure(configureOptions)); } /// @@ -113,8 +119,7 @@ public static ISiloHostBuilder UseDevelopmentClustering(this ISiloHostBuilder bu /// public static ISiloHostBuilder UseDevelopmentClustering( this ISiloHostBuilder builder, - Action> configureOptions, - string clusterId = ClusterOptions.DevelopmentClusterId) + Action> configureOptions) { return builder.ConfigureServices( services => diff --git a/test/Tester/LocalhostSiloTests.cs b/test/Tester/LocalhostSiloTests.cs index fd4d3e7ef2..3e17724a47 100644 --- a/test/Tester/LocalhostSiloTests.cs +++ b/test/Tester/LocalhostSiloTests.cs @@ -62,7 +62,7 @@ public async Task LocalhostClusterTest() .Build(); var client = new ClientBuilder() - .UseLocalhostClustering(30001, 30002) + .UseLocalhostClustering(new[] {30001, 30002}) .Build(); try