Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Akka.Persistence.MongoDb.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29306.81
# Visual Studio Version 17
VisualStudioVersion = 17.6.33723.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Persistence.MongoDb", "src\Akka.Persistence.MongoDb\Akka.Persistence.MongoDb.csproj", "{E945AABA-2779-41E8-9B43-8898FFD64F22}"
EndProject
Expand All @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{BE1178E1
build.sh = build.sh
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Akka.Persistence.MongoDb.Hosting", "src\Akka.Persistence.MongoDb.Hosting\Akka.Persistence.MongoDb.Hosting.csproj", "{72B8C165-FE00-465F-A2E9-60B4B79F81AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,6 +31,10 @@ Global
{0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Release|Any CPU.Build.0 = Release|Any CPU
{72B8C165-FE00-465F-A2E9-60B4B79F81AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72B8C165-FE00-465F-A2E9-60B4B79F81AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72B8C165-FE00-465F-A2E9-60B4B79F81AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72B8C165-FE00-465F-A2E9-60B4B79F81AF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />

<PropertyGroup>
<TargetFrameworks>$(NetStandardLibVersion)</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka.Persistence.Hosting" Version="$(AkkaVersion)" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the other projects it looked like targeting $(AkkaVersion) was the correct approach.

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Akka.Persistence.MongoDb\Akka.Persistence.MongoDb.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
using System;
using Akka.Actor;
using Akka.Hosting;
using Akka.Persistence.Hosting;

#nullable enable
namespace Akka.Persistence.MongoDb.Hosting;

public static class AkkaPersistenceMongoDbHostingExtensions
{
/// <summary>
/// Adds Akka.Persistence.SqlServer support to this <see cref="ActorSystem"/>.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="connectionString">
/// Connection string used for database access.
/// </param>
/// <param name="mode">
/// <para>
/// Determines which settings should be added by this method call.
/// </para>
/// <i>Default</i>: <see cref="PersistenceMode.Both"/>
/// </param>
/// <param name="autoInitialize">
/// <para>
/// Should the SQL store table be initialized automatically.
/// </para>
/// <i>Default</i>: <c>false</c>
/// </param>
/// <param name="journalBuilder">
/// <para>
/// An <see cref="Action{T}"/> used to configure an <see cref="AkkaPersistenceJournalBuilder"/> instance.
/// </para>
/// <i>Default</i>: <c>null</c>
/// </param>
/// <param name="pluginIdentifier">
/// <para>
/// The configuration identifier for the plugins
/// </para>
/// <i>Default</i>: <c>"sql-server"</c>
/// </param>
/// <param name="isDefaultPlugin">
/// <para>
/// A <c>bool</c> flag to set the plugin as the default persistence plugin for the <see cref="ActorSystem"/>
/// </para>
/// <b>Default</b>: <c>true</c>
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when <see cref="journalBuilder"/> is set and <see cref="mode"/> is set to
/// <see cref="PersistenceMode.SnapshotStore"/>
/// </exception>
public static AkkaConfigurationBuilder WithMongoDbPersistence(
this AkkaConfigurationBuilder builder,
string connectionString,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this method I have only added the connectionString as a parameter that is custom on top of the default. For this plugin specifically are there any others that you would suggest adding?

PersistenceMode mode = PersistenceMode.Both,
bool autoInitialize = true,
Action<AkkaPersistenceJournalBuilder>? journalBuilder = null,
string pluginIdentifier = "mongodb",
bool isDefaultPlugin = true)
{
if (mode == PersistenceMode.SnapshotStore && journalBuilder is { })
throw new Exception(
$"{nameof(journalBuilder)} can only be set when {nameof(mode)} is set to either {PersistenceMode.Both} or {PersistenceMode.Journal}");

var journalOpt = new MongoDbJournalOptions(isDefaultPlugin, pluginIdentifier)
{
ConnectionString = connectionString,
AutoInitialize = autoInitialize,
};

var adapters = new AkkaPersistenceJournalBuilder(journalOpt.Identifier, builder);
journalBuilder?.Invoke(adapters);
journalOpt.Adapters = adapters;

var snapshotOpt = new MongoDbSnapshotOptions(isDefaultPlugin, pluginIdentifier)
{
ConnectionString = connectionString,
AutoInitialize = autoInitialize,
};

return mode switch
{
PersistenceMode.Journal => builder.WithMongoDbPersistence(journalOpt, null),
PersistenceMode.SnapshotStore => builder.WithMongoDbPersistence(null, snapshotOpt),
PersistenceMode.Both => builder.WithMongoDbPersistence(journalOpt, snapshotOpt),
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, "Invalid PersistenceMode defined.")
};
}

/// <summary>
/// Adds Akka.Persistence.MongoDb support to this <see cref="ActorSystem"/>. At least one of the
/// configurator delegate needs to be populated else this method will throw an exception.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="journalOptionConfigurator">
/// <para>
/// An <see cref="Action{T}"/> that modifies an instance of <see cref="MongoDbJournalOptions"/>,
/// used to configure the journal plugin
/// </para>
/// <i>Default</i>: <c>null</c>
/// </param>
/// <param name="snapshotOptionConfigurator">
/// <para>
/// An <see cref="Action{T}"/> that modifies an instance of <see cref="MongoDbSnapshotOptions"/>,
/// used to configure the snapshot store plugin
/// </para>
/// <i>Default</i>: <c>null</c>
/// </param>
/// <param name="isDefaultPlugin">
/// <para>
/// A <c>bool</c> flag to set the plugin as the default persistence plugin for the <see cref="ActorSystem"/>
/// </para>
/// <b>Default</b>: <c>true</c>
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <exception cref="ArgumentException">
/// Thrown when both <paramref name="journalOptionConfigurator"/> and <paramref name="snapshotOptionConfigurator"/> are null.
/// </exception>
public static AkkaConfigurationBuilder WithMongoDbPersistence(
this AkkaConfigurationBuilder builder,
Action<MongoDbJournalOptions>? journalOptionConfigurator = null,
Action<MongoDbSnapshotOptions>? snapshotOptionConfigurator = null,
bool isDefaultPlugin = true)
{
if (journalOptionConfigurator is null && snapshotOptionConfigurator is null)
throw new ArgumentException(
$"{nameof(journalOptionConfigurator)} and {nameof(snapshotOptionConfigurator)} could not both be null");

MongoDbJournalOptions? journalOptions = null;
if (journalOptionConfigurator is { })
{
journalOptions = new MongoDbJournalOptions(isDefaultPlugin);
journalOptionConfigurator(journalOptions);
}

MongoDbSnapshotOptions? snapshotOptions = null;
if (snapshotOptionConfigurator is { })
{
snapshotOptions = new MongoDbSnapshotOptions(isDefaultPlugin);
snapshotOptionConfigurator(snapshotOptions);
}

return builder.WithMongoDbPersistence(journalOptions, snapshotOptions);
}

/// <summary>
/// Adds Akka.Persistence.MongoDb support to this <see cref="ActorSystem"/>. At least one of the options
/// have to be populated else this method will throw an exception.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="journalOptions">
/// <para>
/// An instance of <see cref="MongoDbJournalOptions"/>, used to configure the journal plugin
/// </para>
/// <i>Default</i>: <c>null</c>
/// </param>
/// <param name="snapshotOptions">
/// <para>
/// An instance of <see cref="MongoDbSnapshotOptions"/>, used to configure the snapshot store plugin
/// </para>
/// <i>Default</i>: <c>null</c>
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <exception cref="ArgumentException">
/// Thrown when both <paramref name="journalOptions"/> and <paramref name="snapshotOptions"/> are null.
/// </exception>
public static AkkaConfigurationBuilder WithMongoDbPersistence(
this AkkaConfigurationBuilder builder,
MongoDbJournalOptions? journalOptions = null,
MongoDbSnapshotOptions? snapshotOptions = null)
{
if (journalOptions is null && snapshotOptions is null)
throw new ArgumentException(
$"{nameof(journalOptions)} and {nameof(snapshotOptions)} could not both be null");

return (journalOptions, snapshotOptions) switch
{
(null, null) =>
throw new ArgumentException(
$"{nameof(journalOptions)} and {nameof(snapshotOptions)} could not both be null"),

(_, null) =>
builder
.AddHocon(journalOptions.ToConfig(), HoconAddMode.Prepend)
.AddHocon(journalOptions.DefaultConfig, HoconAddMode.Append)
.AddHocon(MongoDbPersistence.DefaultConfiguration(), HoconAddMode.Append),

(null, _) =>
builder
.AddHocon(snapshotOptions.ToConfig(), HoconAddMode.Prepend)
.AddHocon(snapshotOptions.DefaultConfig, HoconAddMode.Append),

(_, _) =>
builder
.AddHocon(journalOptions.ToConfig(), HoconAddMode.Prepend)
.AddHocon(snapshotOptions.ToConfig(), HoconAddMode.Prepend)
.AddHocon(journalOptions.DefaultConfig, HoconAddMode.Append)
.AddHocon(snapshotOptions.DefaultConfig, HoconAddMode.Append)
.AddHocon(MongoDbPersistence.DefaultConfiguration(), HoconAddMode.Append),
};
}
}
82 changes: 82 additions & 0 deletions src/Akka.Persistence.MongoDb.Hosting/MongoDbJournalOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Text;
using Akka.Configuration;
using Akka.Hosting;
using Akka.Persistence.Hosting;

#nullable enable
namespace Akka.Persistence.MongoDb.Hosting;

public class MongoDbJournalOptions : JournalOptions
{
private static readonly Config Default = MongoDbPersistence.DefaultConfiguration()
.GetConfig(MongoDbJournalSettings.JournalConfigPath);

public MongoDbJournalOptions() : this(true)
{
}

public MongoDbJournalOptions(bool isDefaultPlugin, string identifier = "mongodb") : base(isDefaultPlugin)
{
Identifier = identifier;
AutoInitialize = true;
}

/// <summary>
/// Connection string used to access the MongoDb, also specifies the database.
/// </summary>
public string ConnectionString { get; set; } = "";

/// <summary>
/// Name of the collection for the event journal or snapshots
/// </summary>
public string? Collection { get; set; }

/// <summary>
/// Name of the collection for the event journal metadata
/// </summary>
public string? MetadataCollection { get; set; }

/// <summary>
/// Transaction
/// </summary>
public bool? UseWriteTransaction { get; set; }

/// <summary>
/// When true, enables BSON serialization (which breaks features like Akka.Cluster.Sharding, AtLeastOnceDelivery, and so on.)
/// </summary>
public bool? LegacySerialization { get; set; }

/// <summary>
/// Timeout for individual database operations.
/// </summary>
/// <remarks>
/// Defaults to 10s.
/// </remarks>
public TimeSpan? CallTimeout { get; set; }

public override string Identifier { get; set; }
protected override Config InternalDefaultConfig { get; } = Default;

protected override StringBuilder Build(StringBuilder sb)
{
sb.AppendLine($"connection-string = {ConnectionString.ToHocon()}");

if(Collection is not null)
sb.AppendLine($"collection = {Collection.ToHocon()}");

if(MetadataCollection is not null)
sb.AppendLine($"metadata-collection = {MetadataCollection.ToHocon()}");

if(CallTimeout is not null)
sb.AppendLine($"call-timeout = {CallTimeout.ToHocon()}");

if(UseWriteTransaction is not null)
sb.AppendLine($"use-write-transaction = {UseWriteTransaction.ToHocon()}");

if(LegacySerialization is not null)
sb.AppendLine($"legacy-serialization = {LegacySerialization.ToHocon()}");

return base.Build(sb);
}
}
Loading