From 4b726ab63ee9432da95cd26abe2087da4d46b21b Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Fri, 15 Sep 2023 01:17:33 +0700 Subject: [PATCH 1/2] Fix missing default query configuration --- Akka.Persistence.Sql.sln | 9 +++ .../Query/JournalSequenceActor.cs | 2 +- .../Query/SqlReadJournal.cs | 5 +- .../Query/SqlReadJournalProvider.cs | 16 +---- src/Directory.Packages.props | 1 + .../HoconConfiguration.csproj | 24 ++++++++ src/Examples/HoconConfiguration/Program.cs | 58 +++++++++++++++++++ .../TestPersistenceActor.cs | 50 ++++++++++++++++ src/Examples/HoconConfiguration/db/test.db | 0 9 files changed, 147 insertions(+), 18 deletions(-) create mode 100644 src/Examples/HoconConfiguration/HoconConfiguration.csproj create mode 100644 src/Examples/HoconConfiguration/Program.cs create mode 100644 src/Examples/HoconConfiguration/TestPersistenceActor.cs create mode 100644 src/Examples/HoconConfiguration/db/test.db diff --git a/Akka.Persistence.Sql.sln b/Akka.Persistence.Sql.sln index 0c5becfd..b1e43013 100644 --- a/Akka.Persistence.Sql.sln +++ b/Akka.Persistence.Sql.sln @@ -80,6 +80,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Persistence.Sql.Hostin EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Persistence.Sql.Benchmarks", "src\Akka.Persistence.Sql.Benchmarks\Akka.Persistence.Sql.Benchmarks.csproj", "{1BE5B28E-0AB1-44BC-A5E8-5D352B42ACCF}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{42F8143A-03CA-41DA-B24D-33193ABB252C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HoconConfiguration", "src\Examples\HoconConfiguration\HoconConfiguration.csproj", "{043A8917-5931-40FC-A093-21BB0AB56875}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -130,6 +134,10 @@ Global {1BE5B28E-0AB1-44BC-A5E8-5D352B42ACCF}.Debug|Any CPU.Build.0 = Debug|Any CPU {1BE5B28E-0AB1-44BC-A5E8-5D352B42ACCF}.Release|Any CPU.ActiveCfg = Release|Any CPU {1BE5B28E-0AB1-44BC-A5E8-5D352B42ACCF}.Release|Any CPU.Build.0 = Release|Any CPU + {043A8917-5931-40FC-A093-21BB0AB56875}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {043A8917-5931-40FC-A093-21BB0AB56875}.Debug|Any CPU.Build.0 = Debug|Any CPU + {043A8917-5931-40FC-A093-21BB0AB56875}.Release|Any CPU.ActiveCfg = Release|Any CPU + {043A8917-5931-40FC-A093-21BB0AB56875}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -144,6 +152,7 @@ Global {74AFECF0-CC70-47C9-9393-2AB952B23BBD} = {C4AC49FF-9ECF-4D38-A201-1105BBF7E628} {4ACEF3FD-F5B4-4C07-B85A-AD4D6AB68F4D} = {C59D2898-1994-4C40-B152-1798060051ED} {1BE5B28E-0AB1-44BC-A5E8-5D352B42ACCF} = {3E392F73-EB03-4148-BC60-A1F734486190} + {043A8917-5931-40FC-A093-21BB0AB56875} = {42F8143A-03CA-41DA-B24D-33193ABB252C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B99E6BB8-642A-4A68-86DF-69567CBA700A} diff --git a/src/Akka.Persistence.Sql/Query/JournalSequenceActor.cs b/src/Akka.Persistence.Sql/Query/JournalSequenceActor.cs index 6dbcbcd3..950dc02f 100644 --- a/src/Akka.Persistence.Sql/Query/JournalSequenceActor.cs +++ b/src/Akka.Persistence.Sql/Query/JournalSequenceActor.cs @@ -205,7 +205,7 @@ protected override void PreStart() { if (t.IsFaulted) { - _log.Info(t.Exception, "Failed to recover fast, using event-by-event recovery instead"); + _log.Debug($"Failed to recover fast, using event-by-event recovery instead. Message: [{t.Exception?.Message ?? "nothing"}]"); } else if (t.IsCompleted) { diff --git a/src/Akka.Persistence.Sql/Query/SqlReadJournal.cs b/src/Akka.Persistence.Sql/Query/SqlReadJournal.cs index 31adbfa0..12d0e56a 100644 --- a/src/Akka.Persistence.Sql/Query/SqlReadJournal.cs +++ b/src/Akka.Persistence.Sql/Query/SqlReadJournal.cs @@ -50,8 +50,7 @@ public class SqlReadJournal : public SqlReadJournal( ExtendedActorSystem system, - Configuration.Config config, - string configPath) + Configuration.Config config) { var writePluginId = config.GetString("write-plugin"); @@ -68,7 +67,7 @@ public SqlReadJournal( _mat = Materializer.CreateSystemMaterializer( context: system, settings: ActorMaterializerSettings.Create(system), - namePrefix: $"l2db-query-mat{configPath}"); + namePrefix: $"l2db-query-mat-{Guid.NewGuid():N}"); _readJournalDao = new ByteArrayReadJournalDao( scheduler: system.Scheduler.Advanced, diff --git a/src/Akka.Persistence.Sql/Query/SqlReadJournalProvider.cs b/src/Akka.Persistence.Sql/Query/SqlReadJournalProvider.cs index 1216ae3e..61502634 100644 --- a/src/Akka.Persistence.Sql/Query/SqlReadJournalProvider.cs +++ b/src/Akka.Persistence.Sql/Query/SqlReadJournalProvider.cs @@ -12,7 +12,6 @@ namespace Akka.Persistence.Sql.Query public class SqlReadJournalProvider : IReadJournalProvider { private readonly Configuration.Config _config; - private readonly string _configPath; private readonly ExtendedActorSystem _system; public SqlReadJournalProvider( @@ -20,21 +19,10 @@ public SqlReadJournalProvider( Configuration.Config config) { _system = system; - _config = config; - _configPath = "sql"; - } - - public SqlReadJournalProvider( - ExtendedActorSystem system, - Configuration.Config config, - string configPath) - { - _system = system; - _config = config; - _configPath = configPath; + _config = config.WithFallback(SqlPersistence.DefaultQueryConfiguration); } public IReadJournal GetReadJournal() - => new SqlReadJournal(_system, _config, _configPath); + => new SqlReadJournal(_system, _config); } } diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 13a610d4..89758bb2 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -16,6 +16,7 @@ + diff --git a/src/Examples/HoconConfiguration/HoconConfiguration.csproj b/src/Examples/HoconConfiguration/HoconConfiguration.csproj new file mode 100644 index 00000000..0b58cd9c --- /dev/null +++ b/src/Examples/HoconConfiguration/HoconConfiguration.csproj @@ -0,0 +1,24 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + + Always + + + + diff --git a/src/Examples/HoconConfiguration/Program.cs b/src/Examples/HoconConfiguration/Program.cs new file mode 100644 index 00000000..a88c2cac --- /dev/null +++ b/src/Examples/HoconConfiguration/Program.cs @@ -0,0 +1,58 @@ +// See https://aka.ms/new-console-template for more information + +using Akka.Actor; +using Akka.Persistence; +using Akka.Persistence.Query; +using Akka.Persistence.Sql.Query; +using Akka.Streams; +using Akka.Streams.Dsl; +using HoconConfiguration; + +const string config = + """ + akka.persistence { + journal { + plugin = "akka.persistence.journal.sql" + sql { + class = "Akka.Persistence.Sql.Journal.SqlWriteJournal, Akka.Persistence.Sql" + connection-string = "DataSource=db/test.db;" + provider-name = "SQLite.MS" + } + } + query.journal.sql { + class = "Akka.Persistence.Sql.Query.SqlReadJournalProvider, Akka.Persistence.Sql" + connection-string = "DataSource=db/test.db;" + provider-name = "SQLite.MS" + } + snapshot-store { + plugin = "akka.persistence.snapshot-store.sql" + sql { + class = "Akka.Persistence.Sql.Snapshot.SqlSnapshotStore, Akka.Persistence.Sql" + connection-string = "DataSource=db/test.db;" + provider-name = "SQLite.MS" + } + } + } + """; + +var sys = ActorSystem.Create("my-system", config); +var actor = sys.ActorOf(TestPersistenceActor.Props("test")); +var reader = sys.ReadJournalFor("akka.persistence.query.journal.sql"); + +foreach (var i in Enumerable.Range(0, 200)) +{ + var chr = (char)(65 + i % 26); + actor.Tell(new string(new []{chr})); + await Task.Delay(TimeSpan.FromSeconds(0.05)); +} + +_ = reader.CurrentAllEvents(Offset.NoOffset()) + .Select(e => + { + Console.WriteLine($"New event: {e.Event}"); + return e; + }).RunWith(Sink.Ignore(), sys.Materializer()); + +Console.ReadKey(); + +await sys.Terminate(); \ No newline at end of file diff --git a/src/Examples/HoconConfiguration/TestPersistenceActor.cs b/src/Examples/HoconConfiguration/TestPersistenceActor.cs new file mode 100644 index 00000000..1edffe9c --- /dev/null +++ b/src/Examples/HoconConfiguration/TestPersistenceActor.cs @@ -0,0 +1,50 @@ +// ----------------------------------------------------------------------- +// +// Copyright (C) 2013-2023 .NET Foundation +// +// ----------------------------------------------------------------------- + +using Akka.Actor; +using Akka.Event; +using Akka.Persistence; + +namespace HoconConfiguration +{ + public class TestPersistenceActor: ReceivePersistentActor + { + public static Props Props(string id) => Akka.Actor.Props.Create(() => new TestPersistenceActor(id)); + + public override string PersistenceId { get; } + + private int _counter; + private string _state = string.Empty; + + public TestPersistenceActor(string persistenceId) + { + PersistenceId = persistenceId; + var log = Context.GetLogger(); + + Recover(s => _state = (string)s.Snapshot); + Recover(s => _state += s); + + Command( + msg => + { + Persist(msg, + s => + { + _state += s; + _counter++; + if(_counter % 25 == 0) + SaveSnapshot(_state); + log.Info($"Persisted message: {s}"); + }); + }); + Command( + _ => + { + log.Info($"Snapshot persisted. State: {_state}"); + }); + } + } +} diff --git a/src/Examples/HoconConfiguration/db/test.db b/src/Examples/HoconConfiguration/db/test.db new file mode 100644 index 00000000..e69de29b From 07b115e460cad1e2e6fd38d204e73a7173cfa66e Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Fri, 15 Sep 2023 01:22:53 +0700 Subject: [PATCH 2/2] Make example project non-packable and exclude from build script --- build.fsx | 3 +++ src/Examples/HoconConfiguration/HoconConfiguration.csproj | 1 + 2 files changed, 4 insertions(+) diff --git a/build.fsx b/build.fsx index 18393d67..042610ba 100644 --- a/build.fsx +++ b/build.fsx @@ -89,8 +89,10 @@ Target "RunTests" (fun _ -> | true -> !! "./src/**/*.Tests.csproj" -- "./src/**/*.Benchmark.*.csproj" -- "./src/**/*.Data.Compatibility.Tests.csproj" // All of the data docker images are Linux only + -- "./src/Examples/**/*.csproj" // skip example projects | _ -> !! "./src/**/*.Tests.csproj" -- "./src/**/*.Benchmark.*.csproj" + -- "./src/Examples/**/*.csproj" // skip example projects ++ "./src/**/*.DockerTests.csproj" // if you need to filter specs for Linux vs. Windows, do it here let runSingleProject project = @@ -144,6 +146,7 @@ Target "CreateNuget" (fun _ -> -- "src/**/*Tests.csproj" // Don't publish unit tests -- "src/**/*Tests*.csproj" -- "src/**/*.Benchmark.*.csproj" + -- "./src/Examples/**/*.csproj" // skip example projects let runSingleProject project = DotNetCli.Pack diff --git a/src/Examples/HoconConfiguration/HoconConfiguration.csproj b/src/Examples/HoconConfiguration/HoconConfiguration.csproj index 0b58cd9c..227466e6 100644 --- a/src/Examples/HoconConfiguration/HoconConfiguration.csproj +++ b/src/Examples/HoconConfiguration/HoconConfiguration.csproj @@ -5,6 +5,7 @@ net6.0 enable enable + false