Skip to content

Commit

Permalink
Merge pull request #3 from snovak7/main
Browse files Browse the repository at this point in the history
Feature & Fixes
  • Loading branch information
snovak7 authored Oct 4, 2023
2 parents 4f8f755 + eac49d0 commit af8e58d
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/Clustering/Cassandra/Mapping/MembershipMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Escendit.Orleans.Clustering.Cassandra;
/// <summary>
/// Membership Mapping.
/// </summary>
public class MembershipMapping : Mappings
internal class MembershipMapping : Mappings
{
/// <summary>
/// Initializes a new instance of the <see cref="MembershipMapping"/> class.
Expand Down
2 changes: 1 addition & 1 deletion src/Clustering/Cassandra/Schema/Membership.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Escendit.Orleans.Clustering.Cassandra;
/// <summary>
/// Membership.
/// </summary>
public class Membership
internal class Membership
{
/// <summary>
/// Gets or sets the id.
Expand Down
2 changes: 1 addition & 1 deletion src/Clustering/Cassandra/Schema/Silo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Escendit.Orleans.Clustering.Cassandra;
/// <summary>
/// Silo.
/// </summary>
public class Silo
internal class Silo
{
/// <summary>
/// Gets or sets the address.
Expand Down
2 changes: 1 addition & 1 deletion src/Clustering/Cassandra/Schema/SuspectTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Escendit.Orleans.Clustering.Cassandra;
/// <summary>
/// Suspect Time.
/// </summary>
public class SuspectTime
internal class SuspectTime
{
/// <summary>
/// Gets or sets the address.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public static IServiceCollection AddCassandraGrainStorage(
}

return services
.AddTransient<IConfigurationValidator>(sp =>
new CassandraStorageOptionsValidator(sp.GetOptionsByName<CassandraStorageOptions>(name), name))
.ConfigureNamedOptionForLogging<CassandraStorageOptions>(name)
.ConfigureOptions<DefaultCassandraPostConfigureOptions>()
.AddSingletonNamedService<IGrainStorage>(name, CassandraGrainStorageFactory.Create)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Escendit.Orleans.Persistence.Cassandra.Mapping;
/// <summary>
/// Single Grain Storage Mapping.
/// </summary>
public class SingleGrainStorageMapping : Mappings
internal class SingleGrainStorageMapping : Mappings
{
/// <summary>
/// Initializes a new instance of the <see cref="SingleGrainStorageMapping"/> class.
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions src/Persistence/Cassandra/Schema/SingleGrainStorageTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ namespace Escendit.Orleans.Persistence.Cassandra.Schema;
/// <summary>
/// Single Grain Storage Table.
/// </summary>
public class SingleGrainStorageTable
internal class SingleGrainStorageTable
{
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>The type.</value>
public byte[] Type { get; set; }
public byte[]? Type { get; set; }

/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public byte[] Id { get; set; }
public byte[]? Id { get; set; }

/// <summary>
/// Gets or sets the name.
Expand All @@ -30,7 +30,7 @@ public class SingleGrainStorageTable
/// Gets or sets the state.
/// </summary>
/// <value>The state.</value>
public byte[] State { get; set; }
public byte[]? State { get; set; }

/// <summary>
/// Gets or sets the etag.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Escendit.Orleans.Persistence.Cassandra.Storage;
/// <summary>
/// Cassandra Grain Storage Factory.
/// </summary>
public static class CassandraGrainStorageFactory
internal static class CassandraGrainStorageFactory
{
/// <summary>
/// Create GrainStorage.
Expand All @@ -21,12 +21,12 @@ public static class CassandraGrainStorageFactory
/// <returns>The grain storage.</returns>
public static GrainStorageBase Create(IServiceProvider serviceProvider, string name)
{
var options = serviceProvider.GetOptionsByName<CassandraStorageOptions>(name);
var connectionOptions = serviceProvider.GetOptionsByName<CassandraClientOptions>(name);
return options.Strategy switch
var storageOptions = serviceProvider.GetOptionsByName<CassandraStorageOptions>(name);
var clientOptions = serviceProvider.GetOptionsByName<CassandraClientOptions>(name);
return storageOptions.Strategy switch
{
Strategy.SingleTable => ActivatorUtilities
.CreateInstance<SingleTableGrainStorage>(serviceProvider, name, options, connectionOptions),
.CreateInstance<SingleTableGrainStorage>(serviceProvider, name, storageOptions, clientOptions),
Strategy.TablePerGrain => throw new NotSupportedException(),
null => throw new NotSupportedException(),
_ => throw new ArgumentOutOfRangeException(name),
Expand Down
25 changes: 15 additions & 10 deletions src/Persistence/Cassandra/Storage/GrainStorageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,39 @@ namespace Escendit.Orleans.Persistence.Cassandra.Storage;
using global::Orleans.Runtime;
using global::Orleans.Storage;
using Microsoft.Extensions.Logging;
using Options;

/// <summary>
/// Grain Storage Base.
/// </summary>
public abstract partial class GrainStorageBase : IGrainStorage, ILifecycleParticipant<ISiloLifecycle>, IDisposable
internal abstract partial class GrainStorageBase : IGrainStorage, ILifecycleParticipant<ISiloLifecycle>, IDisposable
{
private readonly string _name;
private readonly ILogger _logger;
private readonly ICluster _cluster;
private readonly CassandraClientOptions _options;
private readonly CassandraClientOptions _clientOptions;
private readonly CassandraStorageOptions _storageOptions;

/// <summary>
/// Initializes a new instance of the <see cref="GrainStorageBase"/> class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="logger">The logger.</param>
/// <param name="cluster">The cluster.</param>
/// <param name="options">The client options.</param>
/// <param name="clientOptions">The client options.</param>
/// <param name="storageOptions">The storage options.</param>
protected GrainStorageBase(
string name,
ILogger logger,
ICluster cluster,
CassandraClientOptions options)
CassandraClientOptions clientOptions,
CassandraStorageOptions storageOptions)
{
_name = name;
_logger = logger;
_cluster = cluster;
_options = options;
_clientOptions = clientOptions;
_storageOptions = storageOptions;
}

/// <summary>
Expand All @@ -65,7 +70,7 @@ protected GrainStorageBase(
public void Participate(ISiloLifecycle observer)
{
LogParticipate(_name);
observer.Subscribe($"{GetType().Name}:{_name}", ServiceLifecycleStage.ApplicationServices, Initialize);
observer.Subscribe($"{GetType().Name}:{_name}", _storageOptions.InitialStage, Initialize);
}

/// <inheritdoc/>
Expand All @@ -82,11 +87,11 @@ public void Dispose()
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
protected virtual async Task Initialize(CancellationToken cancellationToken)
{
LogInitialize(_name, _options.DefaultKeyspace!);
LogInitialize(_name, _clientOptions.DefaultKeyspace!);
Session = await _cluster.ConnectAsync(string.Empty);
Session.CreateKeyspaceIfNotExists(_options.DefaultKeyspace);
Session.ChangeKeyspace(_options.DefaultKeyspace);
LogConnect(_name, _options.DefaultKeyspace!);
Session.CreateKeyspaceIfNotExists(_clientOptions.DefaultKeyspace);
Session.ChangeKeyspace(_clientOptions.DefaultKeyspace);
LogConnect(_name, _clientOptions.DefaultKeyspace!);
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion src/Persistence/Cassandra/Storage/SingleTableGrainStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public SingleTableGrainStorage(
IServiceProvider serviceProvider,
CassandraClientOptions clientOptions,
CassandraStorageOptions storageOptions)
: base(name, logger, serviceProvider.GetRequiredCassandraClient(name), clientOptions)
: base(name, logger, serviceProvider.GetRequiredCassandraClient(name), clientOptions, storageOptions)
{
_storageOptions = storageOptions;
_mappingConfiguration = new MappingConfiguration()
Expand Down Expand Up @@ -147,6 +147,11 @@ await Execute(() =>

private async Task ClearStateInternalAsync<T>(string stateName, GrainId grainId, IGrainState<T> grainState)
{
if (!_storageOptions.DeleteStateOnClear)
{
return;
}

var name = GenerateStateName<T>(stateName, grainId);
var type = GenerateTypeName<T>(stateName, grainId);
var id = GenerateId<T>(stateName, grainId);
Expand Down
19 changes: 18 additions & 1 deletion test/Clustering/Cassandra/Fixtures/SimulatorFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

namespace Escendit.Orleans.Clustering.Cassandra.Tests.Fixtures;

using Escendit.Extensions.Hosting.Cassandra;
using global::Orleans.Configuration;
using global::Orleans.Configuration.Overrides;
using global::Orleans.Runtime;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Options;
using Microsoft.Extensions.Logging;

/// <summary>
/// Simulator Fixture.
Expand All @@ -20,12 +22,27 @@ public sealed class SimulatorFixture
public SimulatorFixture()
{
var services = new ServiceCollection()
.AddLogging()
.AddCassandraClientAsDefault(options =>
{
options
.Endpoints.Add("localhost");
options.DefaultKeyspace = "test";
})
.AddSingleton<IMembershipTable>(sp =>
new CassandraMembershipTable(
"Default",
sp.GetRequiredCassandraClient(),
sp.GetRequiredService<ILogger<CassandraMembershipTable>>(),
new CassandraClientOptions
{
Endpoints =
{
"localhost",
},
DefaultKeyspace = "test",
},
sp.GetProviderClusterOptions("Default").Value))
.Configure<ClusterOptions>(options => options.ClusterId = "default");

services
Expand Down

0 comments on commit af8e58d

Please sign in to comment.