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

Clarify UseDevelopmentClustering and UseLocalhostClustering #4438

Merged
merged 5 commits into from Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Orleans.Core/Configuration/Options/ClusterOptions.cs
Expand Up @@ -12,6 +12,11 @@ public class ClusterOptions
/// </summary>
internal const string DevelopmentClusterId = "dev";

/// <summary>
/// Default service id for development clusters.
/// </summary>
internal const string DevelopmentServiceId = "dev";

/// <summary>
/// Gets or sets the cluster identity. This used to be called DeploymentId before Orleans 2.0 name.
/// </summary>
Expand Down
39 changes: 29 additions & 10 deletions src/Orleans.Core/Core/ClientBuilderExtensions.cs
Expand Up @@ -165,30 +165,49 @@ public static IClientBuilder ConfigureLogging(this IClientBuilder builder, Actio
/// </summary>
/// <param name="builder"></param>
/// <param name="gatewayPort">The local silo's gateway port.</param>
/// <param name="clusterId">Cluster ID to use</param>
/// <param name="serviceId">The service id.</param>
/// <param name="clusterId">The cluster id.</param>
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<ClusterOptions>(options =>
{
if (!string.IsNullOrWhiteSpace(clusterId)) options.ClusterId = clusterId;
});
return builder.UseLocalhostClustering(new [] {gatewayPort}, serviceId, clusterId);
}

/// <summary>
/// Configures the client to connect to a silo on the localhost.
/// </summary>
/// <param name="builder"></param>
/// <param name="gatewayPorts">The local silo gateway port.</param>
public static IClientBuilder UseLocalhostClustering(this IClientBuilder builder, params int[] gatewayPorts)
/// <param name="gatewayPorts">The local silo gateway ports.</param>
/// <param name="serviceId">The service id.</param>
/// <param name="clusterId">The cluster id.</param>
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<ClusterOptions>(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<ClusterOptions>(options =>
{
if (string.IsNullOrWhiteSpace(options.ClusterId)) options.ClusterId = ClusterOptions.DevelopmentClusterId;
if (string.IsNullOrWhiteSpace(options.ServiceId)) options.ServiceId = ClusterOptions.DevelopmentServiceId;
});
}
else
{
services.Configure<ClusterOptions>(options =>
{
options.ServiceId = serviceId;
options.ClusterId = clusterId;
});
}
});
}

Expand Down
Expand Up @@ -70,8 +70,7 @@ public void Configure(object configuration, ISiloHostBuilder builder)
{
options.PrimarySiloEndpoint = config.SeedNodes?.FirstOrDefault();
}
},
config.ClusterId);
});
}
}
}
Expand Down
49 changes: 27 additions & 22 deletions src/Orleans.Runtime/Hosting/CoreHostingExtensions.cs
Expand Up @@ -39,21 +39,23 @@ public static ISiloHostBuilder ConfigureDefaults(this ISiloHostBuilder builder)
}

/// <summary>
/// Configures a localhost silo for development and testing.
/// Configures the silo to use development-only clustering and listen on localhost.
/// </summary>
/// <param name="builder">The silo builder.</param>
/// <param name="siloPort">The silo port.</param>
/// <param name="gatewayPort">The gateway port.</param>
/// <param name="primarySiloEndpoint">
/// The endpoint of the primary silo, or <see langword="null"/> to use this silo as the primary.
/// </param>
/// <param name="clusterId">Cluster ID</param>
/// <param name="serviceId">The service id.</param>
/// <param name="clusterId">The cluster id.</param>
/// <returns>The silo builder.</returns>
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<EndpointOptions>(options =>
Expand All @@ -64,8 +66,27 @@ public static ISiloHostBuilder ConfigureDefaults(this ISiloHostBuilder builder)
});

builder.UseDevelopmentClustering(primarySiloEndpoint ?? new IPEndPoint(IPAddress.Loopback, siloPort));
builder.Configure<ClusterOptions>(options => options.ClusterId = clusterId);
builder.Configure<ClusterMembershipOptions>(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<ClusterOptions>(options =>
{
if (string.IsNullOrWhiteSpace(options.ClusterId)) options.ClusterId = ClusterOptions.DevelopmentClusterId;
if (string.IsNullOrWhiteSpace(options.ServiceId)) options.ServiceId = ClusterOptions.DevelopmentServiceId;
});
}
else
{
services.Configure<ClusterOptions>(options =>
{
options.ServiceId = serviceId;
options.ClusterId = clusterId;
});
}
});

return builder;
}
Expand All @@ -88,33 +109,17 @@ public static ISiloHostBuilder UseDevelopmentClustering(this ISiloHostBuilder bu
/// </summary>
public static ISiloHostBuilder UseDevelopmentClustering(
this ISiloHostBuilder builder,
Action<DevelopmentClusterMembershipOptions> configureOptions,
string clusterId = ClusterOptions.DevelopmentClusterId)
Action<DevelopmentClusterMembershipOptions> configureOptions)
{
return builder
.Configure<ClusterOptions>(options => options.ClusterId = clusterId)
.ConfigureServices(
services =>
{
if (configureOptions != null)
{
services.Configure(configureOptions);
}

services.ConfigureFormatter<DevelopmentClusterMembershipOptions>();
services
.AddSingleton<GrainBasedMembershipTable>()
.AddFromExisting<IMembershipTable, GrainBasedMembershipTable>();
});
return builder.UseDevelopmentClustering(options => options.Configure(configureOptions));
}

/// <summary>
/// Configures the silo to use development-only clustering.
/// </summary>
public static ISiloHostBuilder UseDevelopmentClustering(
this ISiloHostBuilder builder,
Action<OptionsBuilder<DevelopmentClusterMembershipOptions>> configureOptions,
string clusterId = ClusterOptions.DevelopmentClusterId)
Action<OptionsBuilder<DevelopmentClusterMembershipOptions>> configureOptions)
{
return builder.ConfigureServices(
services =>
Expand Down
2 changes: 1 addition & 1 deletion test/Tester/LocalhostSiloTests.cs
Expand Up @@ -62,7 +62,7 @@ public async Task LocalhostClusterTest()
.Build();

var client = new ClientBuilder()
.UseLocalhostClustering(30001, 30002)
.UseLocalhostClustering(new[] {30001, 30002})
.Build();

try
Expand Down