-
Notifications
You must be signed in to change notification settings - Fork 36
Adding Hosting Extensions for Akka.Persistence.MongoDB #331
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cc6a606
Initial start implementing hosting support
mhbuck 4eb0e7d
Get the tests started. A couple still need to be done
mhbuck a45b91b
Bit more clean up
mhbuck cff5a02
Final Tests
mhbuck ec4ae98
Whitespace clean up
mhbuck 8ef8efd
Addition of a readme
mhbuck 469a32b
Adding NetFrameworkTestVersion
mhbuck fdd3deb
Changes due to not being able to use using declarations
mhbuck 8cd7e00
Merge branch 'dev' into hosting
mhbuck 6ffaaf1
Clean up whitespace
mhbuck 655d3f7
Explicitly set the default
mhbuck 38be7df
Move tests into current test project
mhbuck 7c6a94e
FluentAssertions version in Common props
mhbuck 90beb71
Make sure that Persistence.Query default hocon configurations are loaded
gsoedharmo-conga 472d991
Switch to optional addition to prevent user config clobbering problem
gsoedharmo-conga f32eabe
Improve documentation
gsoedharmo-conga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Akka.Persistence.MongoDb.Hosting/Akka.Persistence.MongoDb.Hosting.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Akka.Persistence.MongoDb\Akka.Persistence.MongoDb.csproj" /> | ||
| </ItemGroup> | ||
| </Project> | ||
215 changes: 215 additions & 0 deletions
215
src/Akka.Persistence.MongoDb.Hosting/AkkaPersistenceMongoDbHostingExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this method I have only added the |
||
| 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
82
src/Akka.Persistence.MongoDb.Hosting/MongoDbJournalOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.