Skip to content

Commit

Permalink
Validate that a ClusterId has been specified
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Mar 12, 2018
1 parent 4480e93 commit ebeb8eb
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Options;
using Orleans.Messaging;
using Orleans.Runtime;

Expand All @@ -19,6 +19,12 @@ internal class ClientClusteringValidator : IConfigurationValidator
+ "\n * Microsoft.Orleans.Clustering.ZooKeeper"
+ "\n * Others, see: https://www.nuget.org/packages?q=Microsoft.Orleans.Clustering.";

internal static readonly string ClusterIdNotConfigured =
$"A cluster id has not been configured. Configure a cluster id by specifying a value for {nameof(ClusterOptions)}.{nameof(ClusterOptions.ClusterId)}." +
" For more information, please see the documentation on:" +
"\n * Client Configuration: http://dotnet.github.io/orleans/Documentation/Deployment-and-Operations/Configuration-Guide/Client-Configuration.html" +
"\n * Server Configuration: http://dotnet.github.io/orleans/Documentation/Deployment-and-Operations/Configuration-Guide/Server-Configuration.html";

private readonly IServiceProvider serviceProvider;

public ClientClusteringValidator(IServiceProvider serviceProvider)
Expand All @@ -28,6 +34,12 @@ public ClientClusteringValidator(IServiceProvider serviceProvider)

public void ValidateConfiguration()
{
var clusterOptions = this.serviceProvider.GetRequiredService<IOptions<ClusterOptions>>();
if (string.IsNullOrWhiteSpace(clusterOptions.Value.ClusterId))
{
throw new OrleansConfigurationException(ClusterIdNotConfigured);
}

var gatewayProvider = this.serviceProvider.GetService<IGatewayListProvider>();
if (gatewayProvider == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.Configuration.Validators;
using Orleans.Runtime.MembershipService;

Expand All @@ -22,6 +23,12 @@ public SiloClusteringValidator(IServiceProvider serviceProvider)
/// <inheritdoc />
public void ValidateConfiguration()
{
var clusterOptions = this.serviceProvider.GetRequiredService<IOptions<ClusterOptions>>();
if (string.IsNullOrWhiteSpace(clusterOptions.Value.ClusterId))
{
throw new OrleansConfigurationException(ClientClusteringValidator.ClusterIdNotConfigured);
}

var clusteringProvider = this.serviceProvider.GetService<IMembershipOracle>();
var clusteringTableProvider = this.serviceProvider.GetService<IMembershipTable>();
var storageBackedWithNoStorage = clusteringProvider is MembershipOracle && clusteringTableProvider == null;
Expand Down
20 changes: 20 additions & 0 deletions test/NonSilo.Tests/ClientBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Orleans;
using Orleans.Configuration;
using Orleans.Messaging;
using Orleans.Runtime;
using Orleans.Runtime.Configuration;
Expand Down Expand Up @@ -37,6 +38,25 @@ public Task InitializeGatewayListProvider()
[TestCategory("ClientBuilder")]
public class ClientBuilderTests
{
/// <summary>
/// Tests that a client cannot be created without specifying a ClusterId.
/// </summary>
[Fact]
public void ClientBuilder_NoClusterIdTest()
{
Assert.Throws<OrleansConfigurationException>(() => new ClientBuilder()
.ConfigureServices(services => services.AddSingleton<IGatewayListProvider, NoOpGatewaylistProvider>())
.Build());

var builder = new ClientBuilder()
.Configure<ClusterOptions>(options => options.ClusterId = "test")
.ConfigureServices(services => services.AddSingleton<IGatewayListProvider, NoOpGatewaylistProvider>());
using (var client = builder.Build())
{
Assert.NotNull(client);
}
}

/// <summary>
/// Tests that a client can be created without specifying configuration.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions test/NonSilo.Tests/SiloHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ public Task<bool> UpdateRow(MembershipEntry entry, string etag, TableVersion tab
[TestCategory("SiloHostBuilder")]
public class SiloHostBuilderTests
{
/// <summary>
/// Tests that a silo cannot be created without specifying a ClusterId.
/// </summary>
[Fact]
public void SiloHostBuilder_NoClusterIdTest()
{
Assert.Throws<OrleansConfigurationException>(() => new SiloHostBuilder()
.Configure<EndpointOptions>(options => options.AdvertisedIPAddress = IPAddress.Loopback)
.ConfigureServices(services => services.AddSingleton<IMembershipTable, NoOpMembershipTable>())
.Build());

var builder = new SiloHostBuilder()
.Configure<EndpointOptions>(options => options.AdvertisedIPAddress = IPAddress.Loopback)
.Configure<ClusterOptions>(options => options.ClusterId = "test")
.ConfigureServices(services => services.AddSingleton<IMembershipTable, NoOpMembershipTable>());
using (var silo = builder.Build())
{
Assert.NotNull(silo);
}
}

/// <summary>
/// Tests that a silo can be created without specifying configuration.
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions test/TestExtensions/SerializationTestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Orleans;
using Orleans.Configuration;
using Orleans.Runtime;
using Orleans.Runtime.Configuration;
using Orleans.Serialization;
Expand All @@ -17,8 +18,10 @@ public SerializationTestEnvironment(ClientConfiguration config = null, Action<IC
{
if (config == null) config = this.DefaultConfig();

var builder = new ClientBuilder().ConfigureDefaults();
builder.UseConfiguration(config);
var builder = new ClientBuilder()
.ConfigureDefaults()
.Configure<ClusterOptions>(options => options.ClusterId = nameof(SerializationTestEnvironment))
.UseConfiguration(config);
configureClientBuilder?.Invoke(builder);
this.Client = builder.Build();
this.RuntimeClient = this.Client.ServiceProvider.GetRequiredService<OutsideRuntimeClient>();
Expand Down
1 change: 1 addition & 0 deletions test/Tester/SiloInitializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void SiloInitializationIsRetryableTest()
try
{
var config = ClusterConfiguration.LocalhostPrimarySilo();
config.Globals.ClusterId = Guid.NewGuid().ToString();
var originalLivenessType = config.Globals.LivenessType;
var originalMembershipAssembly = config.Globals.MembershipTableAssembly;

Expand Down

0 comments on commit ebeb8eb

Please sign in to comment.