See the accompanying Youtube video here
In this walkthrough, we will be migrating an existing simulated Akka.NET cluster from using Akka.Persistence.SqlServer
persistence plugin to Akka.Persistence.Sql
.
For this demo, we will be using one of the code samples provided in the Petabridge Akka.NET code sample repository, specifically the sharding-sqlserver sample code.
All of the changes are explained inside the Migration Guide
- Requirements
- Setup
- Migrating To
Akka.Persistence.Sql
Full Compatibility Mode - Enable WriterUuid Feature
- Upgrade To Tag Table
- Done!
This walkthrough will require some tools to be installed on your computer.
PS C:\> mkdir AkkaTutorial
PS C:\> cd AkkaTutorial
PS C:\AkkaTutorial> git clone https://github.com/petabridge/akkadotnet-code-samples.git
PS C:\AkkaTutorial> cd .\akkadotnet-code-samples\src\clustering\sharding-sqlserver\
PS C:\AkkaTutorial\akkadotnet-code-samples\src\clustering\sharding-sqlserver>
From this point forward, all console commands will be executed from inside this directory path.
This script will start the Microsoft SqlServer Docker container
.\start-dependencies.cmd
-
In two different console windows, run these two projects:
dotnet run --project .\SqlSharding.WebApp\SqlSharding.WebApp.csproj
dotnet run --project .\SqlSharding.Host\SqlSharding.Host.csproj -- seed-db
-
Wait for the seeding process to complete (should take less than 10 seconds)
-
Open
https://localhost:5001
in a browser to make sure that everything is working -
Stop both application by pressing
Ctrl-C
on both console windows.
We will be migrating the SqlSharding.Host
project to use Akka.Persistence.Sql
persistence plugin. Copy the project and register it with the solution file:
Copy-Item -Path .\SqlSharding.Host -Destination .\SqlSharding.Host.Migration -Recurse
Rename-Item -Path .\SqlSharding.Host.Migration\SqlSharding.Host.csproj -NewName SqlSharding.Host.Migration.csproj
dotnet sln add .\SqlSharding.Host.Migration\SqlSharding.Host.Migration.csproj
We will be working with the SqlSharding.Host.Migration
project from now on.
At the end of this step, the content of the SqlSharding.Host.Migration
project should be identical to the SqlSharding.Sql.Host
project
- Remove the package reference to
Akka.Persistence.SqlServer.Hosting
- Add package references to
Akka.Persistence.Sql.Hosting
andMicrosoft.Data.SqlClient
The package reference section should look like this after the modification:
<ItemGroup>
<PackageReference Include="Akka.Persistence.Sql.Hosting" Version="1.5.12-beta1" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="Petabridge.Cmd.Cluster" Version="$(PbmVersion)" />
<PackageReference Include="Petabridge.Cmd.Cluster.Sharding" Version="$(PbmVersion)" />
<PackageReference Include="Petabridge.Cmd.Host" Version="$(PbmVersion)" />
</ItemGroup>
Replace
using Akka.Persistence.SqlServer.Hosting;
with
using Akka.Persistence.Sql.Config;
using Akka.Persistence.Sql.Hosting;
using LinqToDB;
using SqlJournalOptions = Akka.Persistence.Sql.Hosting.SqlJournalOptions;
using SqlSnapshotOptions = Akka.Persistence.Sql.Hosting.SqlSnapshotOptions;
-
Replace
var shardingJournalOptions = new SqlServerJournalOptions( isDefaultPlugin: false, identifier: "sharding") { ConnectionString = connectionString, TableName = "ShardingEventJournal", MetadataTableName = "ShardingMetadata", AutoInitialize = true };
with
var shardingJournalDbOptions = JournalDatabaseOptions.SqlServer; shardingJournalDbOptions.JournalTable!.TableName = "ShardingEventJournal"; shardingJournalDbOptions.MetadataTable!.TableName = "ShardingMetadata"; var shardingJournalOptions = new SqlJournalOptions( isDefaultPlugin: false, identifier: "sharding") { ConnectionString = connectionString, ProviderName = ProviderName.SqlServer2019, DatabaseOptions = shardingJournalDbOptions, TagStorageMode = TagMode.Csv, DeleteCompatibilityMode = true, AutoInitialize = false };
-
Replace
var shardingSnapshotOptions = new SqlServerSnapshotOptions( isDefaultPlugin: false, identifier: "sharding") { ConnectionString = connectionString, TableName = "ShardingSnapshotStore", AutoInitialize = true };
with
var shardingSnapshotDbOptions = SnapshotDatabaseOptions.SqlServer; shardingSnapshotDbOptions.SnapshotTable!.TableName = "ShardingSnapshotStore"; var shardingSnapshotOptions = new SqlSnapshotOptions( isDefaultPlugin: false, identifier: "sharding") { ConnectionString = connectionString, ProviderName = ProviderName.SqlServer2019, DatabaseOptions = shardingSnapshotDbOptions, AutoInitialize = false };
Replace
.WithSqlServerPersistence(
connectionString: connectionString,
journalBuilder: builder =>
{
builder.AddWriteEventAdapter<MessageTagger>("product-tagger", new[] { typeof(IProductEvent) });
})
with
.WithSqlPersistence(
connectionString: connectionString,
providerName: ProviderName.SqlServer2019,
databaseMapping: DatabaseMapping.SqlServer,
tagStorageMode: TagMode.Csv,
deleteCompatibilityMode: true,
useWriterUuidColumn: false,
autoInitialize: false,
journalBuilder: builder =>
{
builder.AddWriteEventAdapter<MessageTagger>("product-tagger", new[] { typeof(IProductEvent) });
})
Replace using Akka.Persistence.Query.Sql;
with using Akka.Persistence.Sql.Query;
Replace using Akka.Persistence.Query.Sql;
with using Akka.Persistence.Sql.Query;
Replace using Akka.Persistence.Query.Sql;
with using Akka.Persistence.Sql.Query;
At this point, the migration project is in Akka.Persistence.Sql
full compatibility mode. All 3 version of the Host
project can co-exist in the same Akka.NET cluster.
-
In three different console windows, run all of the projects:
dotnet run --project .\SqlSharding.WebApp\SqlSharding.WebApp.csproj
dotnet run --project .\SqlSharding.Host\SqlSharding.Host.csproj
dotnet run --project .\SqlSharding.Host.Migration\SqlSharding.Host.Migration.csproj
-
Open
https://localhost:5001
in a browser to make sure that everything is working -
Stop all application by pressing
Ctrl-C
on all console windows.
To go straight to this step, you can directly check out the git branch:
git checkout Migration_01
In a Powershell console, execute:
sqlcmd -S "localhost,1533" -d Akka -U sa -P "yourStrong(!)Password" -Q "ALTER TABLE [dbo].[EventJournal] ADD writer_uuid VARCHAR(128)"
Inside Program.cs
, change the useWriterUuidColumn
argument parameter of the .WithSqlPersistence()
to true
.
.WithSqlPersistence(
connectionString: connectionString,
providerName: ProviderName.SqlServer2019,
databaseMapping: DatabaseMapping.SqlServer,
tagStorageMode: TagMode.Csv,
deleteCompatibilityMode: true,
useWriterUuidColumn: true, // Change this parameter value to true
autoInitialize: false,
journalBuilder: builder =>
{
builder.AddWriteEventAdapter<MessageTagger>("product-tagger", new[] { typeof(IProductEvent) });
})
-
In two different console windows, run the projects:
dotnet run --project .\SqlSharding.WebApp\SqlSharding.WebApp.csproj
dotnet run --project .\SqlSharding.Host.Migration\SqlSharding.Host.Migration.csproj
-
Open
https://localhost:5001
in a browser to make sure that everything is working -
Stop all application by pressing
Ctrl-C
on all console windows.
To go straight to this step, you can directly check out the git branch:
git checkout Migration_02
- Download these SQL script files:
- Copy these SQL script into a folder called
Scripts
- Execute the scripts in order:
sqlcmd -S "localhost,1533" -d Akka -U sa -P "yourStrong(!)Password" -i .\Scripts\1_Migration_Setup.sql sqlcmd -S "localhost,1533" -d Akka -U sa -P "yourStrong(!)Password" -i .\Scripts\2_Migration.sql sqlcmd -S "localhost,1533" -d Akka -U sa -P "yourStrong(!)Password" -i .\Scripts\3_Post_Migration_Cleanup.sql
Inside Program.cs
, change the tagStorageMode
argument parameter of the .WithSqlPersistence()
to TagMode.TagTable
.
The SqlSharding.Host
and SqlSharding.Sql.Host
project is not compatible with SqlSharding.Host.Migration
anymore, you can delete these projects.
dotnet sln remove .\SqlSharding.Host\SqlSharding.Host.csproj
dotnet sln remove .\SqlSharding.Sql.Host\SqlSharding.Sql.Host.csproj
Remove-Item -Recurse -Force .\SqlSharding.Host\
Remove-Item -Recurse -Force .\SqlSharding.Sql.Host\
To confirm that the tag table is working, lets delete the old tag data:
sqlcmd -S "localhost,1533" -d Akka -U sa -P "yourStrong(!)Password" -Q "UPDATE [dbo].[EventJournal] SET Tags = NULL"
-
In two different console windows, run the projects:
dotnet run --project .\SqlSharding.WebApp\SqlSharding.WebApp.csproj
dotnet run --project .\SqlSharding.Host.Migration\SqlSharding.Host.Migration.csproj
-
Open
https://localhost:5001
in a browser to make sure that everything is working -
Stop all application by pressing
Ctrl-C
on all console windows.
To go straight to this step, you can directly check out the git branch:
git checkout Migration_03