From c3b948bb8642241d098224406ea82f79046793ae Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Thu, 16 Jun 2022 12:30:52 -0500 Subject: [PATCH 1/2] Converted SqlSharding sample to use PostgreSql --- infrastructure/postgres/docker-compose.yml | 20 +++++++++++++++++++ .../SqlSharding.Host/Program.cs | 5 +++-- .../SqlSharding.Host/SqlSharding.Host.csproj | 1 + .../appsettings.Development.json | 3 ++- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 infrastructure/postgres/docker-compose.yml diff --git a/infrastructure/postgres/docker-compose.yml b/infrastructure/postgres/docker-compose.yml new file mode 100644 index 0000000..7eb54bd --- /dev/null +++ b/infrastructure/postgres/docker-compose.yml @@ -0,0 +1,20 @@ +# Use postgres/example user/password credentials +version: '3.1' + +services: + + db: + image: postgres + restart: always + environment: + POSTGRES_USER: akka-user + POSTGRES_PASSWORD: example + POSTGRES_DB: Akka + ports: + - 5432:5432 + + adminer: + image: adminer + restart: always + ports: + - 8080:8080 \ No newline at end of file diff --git a/src/clustering/sharding-sqlserver/SqlSharding.Host/Program.cs b/src/clustering/sharding-sqlserver/SqlSharding.Host/Program.cs index df02783..d2566b5 100644 --- a/src/clustering/sharding-sqlserver/SqlSharding.Host/Program.cs +++ b/src/clustering/sharding-sqlserver/SqlSharding.Host/Program.cs @@ -4,6 +4,7 @@ using Akka.Cluster.Hosting; using Akka.Cluster.Sharding; using Akka.Hosting; +using Akka.Persistence.PostgreSql.Hosting; using Akka.Persistence.SqlServer.Hosting; using Akka.Remote.Hosting; using Microsoft.Extensions.Configuration; @@ -21,7 +22,7 @@ .ConfigureServices((context, services) => { // maps to environment variable ConnectionStrings__AkkaSqlConnection - var connectionString = context.Configuration.GetConnectionString("AkkaSqlConnection"); + var connectionString = context.Configuration.GetConnectionString("AkkaPostgresConnection"); var akkaSection = context.Configuration.GetSection("Akka"); @@ -42,7 +43,7 @@ .AddAppSerialization() .WithClustering(new ClusterOptions() { Roles = new[] { ProductActorProps.SingletonActorRole }, SeedNodes = seeds }) - .WithSqlServerPersistence(connectionString) + .WithPostgreSqlPersistence(connectionString, autoInitialize:true) .WithShardRegion("products", s => ProductTotalsActor.GetProps(s), new ProductMessageRouter(), new ShardOptions() diff --git a/src/clustering/sharding-sqlserver/SqlSharding.Host/SqlSharding.Host.csproj b/src/clustering/sharding-sqlserver/SqlSharding.Host/SqlSharding.Host.csproj index 0a4352f..a95241d 100644 --- a/src/clustering/sharding-sqlserver/SqlSharding.Host/SqlSharding.Host.csproj +++ b/src/clustering/sharding-sqlserver/SqlSharding.Host/SqlSharding.Host.csproj @@ -13,6 +13,7 @@ + diff --git a/src/clustering/sharding-sqlserver/SqlSharding.Host/appsettings.Development.json b/src/clustering/sharding-sqlserver/SqlSharding.Host/appsettings.Development.json index d9f9495..88c9d77 100644 --- a/src/clustering/sharding-sqlserver/SqlSharding.Host/appsettings.Development.json +++ b/src/clustering/sharding-sqlserver/SqlSharding.Host/appsettings.Development.json @@ -7,6 +7,7 @@ } }, "ConnectionStrings": { - "AkkaSqlConnection": "Server=localhost,1533; Database=Akka; User Id=sa; Password=yourStrong(!)Password;" + "AkkaSqlConnection": "Server=localhost,1533; Database=Akka; User Id=sa; Password=yourStrong(!)Password;", + "AkkaPostgresConnection": "User ID=akka-user; Password=example; Host=localhost;Database=Akka;" } } \ No newline at end of file From 91f4d96259051abe72f42650af138ff8f7d8a630 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Mon, 3 Oct 2022 15:57:03 -0500 Subject: [PATCH 2/2] all Postgres upgrades --- .../Actors/ProductTotalsActor.cs | 18 ++++++++++++++---- .../SqlSharding.Host/Program.cs | 4 +++- .../Sharding/ProductActorProps.cs | 1 + .../SqlSharding.WebApp/Program.cs | 19 ++++++++++--------- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/clustering/sharding-sqlserver/SqlSharding.Host/Actors/ProductTotalsActor.cs b/src/clustering/sharding-sqlserver/SqlSharding.Host/Actors/ProductTotalsActor.cs index 46bc7c8..cfd0f48 100644 --- a/src/clustering/sharding-sqlserver/SqlSharding.Host/Actors/ProductTotalsActor.cs +++ b/src/clustering/sharding-sqlserver/SqlSharding.Host/Actors/ProductTotalsActor.cs @@ -26,6 +26,11 @@ public static Props GetProps(string persistenceId) public const string TotalsEntityNameConstant = "totals"; private readonly ILoggingAdapter _log = Context.GetLogger(); + + + public override string PersistenceId { get; } + + public ProductState State { get; set; } public ProductTotalsActor(string persistenceId) { @@ -48,7 +53,16 @@ public ProductTotalsActor(string persistenceId) Command(cmd => { var response = State.ProcessCommand(cmd); + + if (!response.Success) + { + Sender.Tell(response); + // bail out + return; + } + var sentResponse = false; + PersistAllAsync(response.ResponseEvents, productEvent => { _log.Info("Processed: {0}", productEvent); @@ -86,8 +100,4 @@ public ProductTotalsActor(string persistenceId) } }); } - - public override string PersistenceId { get; } - - public ProductState State { get; set; } } \ No newline at end of file diff --git a/src/clustering/sharding-sqlserver/SqlSharding.Host/Program.cs b/src/clustering/sharding-sqlserver/SqlSharding.Host/Program.cs index d2566b5..b0845b0 100644 --- a/src/clustering/sharding-sqlserver/SqlSharding.Host/Program.cs +++ b/src/clustering/sharding-sqlserver/SqlSharding.Host/Program.cs @@ -3,6 +3,7 @@ using Akka.Actor; using Akka.Cluster.Hosting; using Akka.Cluster.Sharding; +using Akka.DependencyInjection; using Akka.Hosting; using Akka.Persistence.PostgreSql.Hosting; using Akka.Persistence.SqlServer.Hosting; @@ -55,7 +56,8 @@ { var shardRegion = registry.Get(); - var indexProps = Props.Create(() => new ProductIndexActor(shardRegion)); + var depR = DependencyResolver.For(system); + var indexProps = depR.Props(shardRegion); var singletonProps = system.ProductSingletonProps(indexProps); registry.TryRegister(system.ActorOf(singletonProps, ProductActorProps.SingletonActorName)); diff --git a/src/clustering/sharding-sqlserver/SqlSharding.Shared/Sharding/ProductActorProps.cs b/src/clustering/sharding-sqlserver/SqlSharding.Shared/Sharding/ProductActorProps.cs index 748d200..0938c55 100644 --- a/src/clustering/sharding-sqlserver/SqlSharding.Shared/Sharding/ProductActorProps.cs +++ b/src/clustering/sharding-sqlserver/SqlSharding.Shared/Sharding/ProductActorProps.cs @@ -1,5 +1,6 @@ using Akka.Actor; using Akka.Cluster.Tools.Singleton; +using Akka.DependencyInjection; namespace SqlSharding.Shared.Sharding; diff --git a/src/clustering/sharding-sqlserver/SqlSharding.WebApp/Program.cs b/src/clustering/sharding-sqlserver/SqlSharding.WebApp/Program.cs index 7d850c4..a51cb53 100644 --- a/src/clustering/sharding-sqlserver/SqlSharding.WebApp/Program.cs +++ b/src/clustering/sharding-sqlserver/SqlSharding.WebApp/Program.cs @@ -1,5 +1,6 @@ using Akka.Actor; using Akka.Cluster.Hosting; +using Akka.DependencyInjection; using Akka.Hosting; using Akka.Remote.Hosting; using SqlSharding.Shared.Serialization; @@ -24,15 +25,15 @@ builder.Services.AddAkka("SqlSharding", (configurationBuilder, provider) => { configurationBuilder.WithRemoting(hostName, port) - .AddAppSerialization() - .WithClustering(new ClusterOptions() - { Roles = new[] { "Web" }, SeedNodes = seeds }) - .WithShardRegionProxy("products", ProductActorProps.SingletonActorRole, - new ProductMessageRouter()) - .WithActors((system, registry) => - { - var proxyProps = system.ProductIndexProxyProps(); - registry.TryRegister(system.ActorOf(proxyProps, "product-proxy")); + .AddAppSerialization() + .WithClustering(new ClusterOptions() + { Roles = new[] { "Web" }, SeedNodes = seeds }) + .WithShardRegionProxy("products", ProductActorProps.SingletonActorRole, + new ProductMessageRouter()) + .WithActors((system, registry) => + { + var proxyProps = system.ProductIndexProxyProps(); + registry.TryRegister(system.ActorOf(proxyProps, "product-proxy")); }); });