From 20f357445e2e97b6339c7e2f6b3fb1902ca5c0b9 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Fri, 6 Dec 2024 03:30:33 +0700 Subject: [PATCH] Bump MongoDb.Driver to 3.0.0 (#395) * Bump MongoDb.Driver to 3.0.0 * Cleanup FullTypeNameObjectSerializer * Fix build system * Fix build script * Fix AzDo Pipeline --- build-system/azure-pipeline.template.yaml | 8 +- build-system/linux-pr-validation.yaml | 8 -- build-system/windows-pr-validation.yaml | 8 -- build.fsx | 85 +++++++++++-------- .../Akka.Persistence.MongoDb.Hosting.csproj | 2 +- .../Akka.Persistence.MongoDb.Tests.csproj | 2 +- .../MongoDbJournalSetupSpec.cs | 2 - .../MongoDbSnapshotStoreSetupSpec.cs | 4 - .../Akka.Persistence.MongoDb.csproj | 3 +- .../FullTypeNameObjectSerializer.cs | 19 +++-- .../Journal/MongoDbJournal.cs | 3 - .../Journal/MongoDbJournalQueries.cs | 1 + .../Snapshot/MongoDbGridFSSnapshotStore.cs | 2 - .../Snapshot/MongoDbSnapshotStore.cs | 17 ++-- src/Directory.Build.props | 6 +- src/Directory.Packages.props | 7 +- 16 files changed, 84 insertions(+), 93 deletions(-) diff --git a/build-system/azure-pipeline.template.yaml b/build-system/azure-pipeline.template.yaml index c328a06..efd90da 100644 --- a/build-system/azure-pipeline.template.yaml +++ b/build-system/azure-pipeline.template.yaml @@ -18,14 +18,14 @@ jobs: submodules: recursive # set to 'true' for a single level of submodules or 'recursive' to get submodules of submodules persistCredentials: true - task: UseDotNet@2 - displayName: 'Use .NET 7 SDK 7' + displayName: 'Use .NET SDK 8' inputs: - version: 7.x + version: 8.x - task: UseDotNet@2 - displayName: 'Use .NET Core Runtime 3' + displayName: 'Use .NET SDK 7' inputs: packageType: runtime - version: 3.x + version: 7.x # Linux or macOS - task: Bash@3 displayName: Linux / OSX Build diff --git a/build-system/linux-pr-validation.yaml b/build-system/linux-pr-validation.yaml index afbd925..9b1efe5 100644 --- a/build-system/linux-pr-validation.yaml +++ b/build-system/linux-pr-validation.yaml @@ -14,14 +14,6 @@ pr: include: [ dev, master ] # branch names which will trigger a build jobs: - - template: azure-pipeline.template.yaml - parameters: - name: 'net_core_tests_linux' - displayName: '.NET Core Unit Tests (Linux)' - vmImage: 'ubuntu-latest' - scriptFileName: ./build.sh - scriptArgs: runTestsNetCore - - template: azure-pipeline.template.yaml parameters: name: 'net_7_tests_linux' diff --git a/build-system/windows-pr-validation.yaml b/build-system/windows-pr-validation.yaml index c21b507..1f92479 100644 --- a/build-system/windows-pr-validation.yaml +++ b/build-system/windows-pr-validation.yaml @@ -22,14 +22,6 @@ jobs: scriptFileName: build.cmd scriptArgs: runTests - - template: azure-pipeline.template.yaml - parameters: - name: 'net_core_tests_windows' - displayName: '.NET Core Unit Tests (Windows)' - vmImage: 'windows-latest' - scriptFileName: build.cmd - scriptArgs: runTestsNetCore - - template: azure-pipeline.template.yaml parameters: name: 'net_7_tests_windows' diff --git a/build.fsx b/build.fsx index 9746400..59271ba 100644 --- a/build.fsx +++ b/build.fsx @@ -45,13 +45,13 @@ let outputTests = __SOURCE_DIRECTORY__ @@ "TestResults" let outputPerfTests = __SOURCE_DIRECTORY__ @@ "PerfResults" let outputBinaries = output @@ "binaries" let outputNuGet = output @@ "nuget" -let outputBinariesNet45 = outputBinaries @@ "net45" -let outputBinariesNetStandard = outputBinaries @@ "netstandard2.0" -let outputBinariesNet = outputBinaries @@ "net5.0" + +// Configuration values for builds +let buildNetFrameworkVersion = "net472" +let buildNetVersion = "netstandard2.1" // Configuration values for tests -let testNetFrameworkVersion = "net471" -let testNetCoreVersion = "netcoreapp3.1" +let testNetFrameworkVersion = "net472" let testNetVersion = "net7.0" Target "Clean" (fun _ -> @@ -61,9 +61,6 @@ Target "Clean" (fun _ -> CleanDir outputTests CleanDir outputPerfTests CleanDir outputNuGet - CleanDir outputBinariesNet45 - CleanDir outputBinariesNetStandard - CleanDir outputBinariesNet CleanDir "docs/_site" CleanDirs !! "./**/bin" @@ -85,6 +82,48 @@ Target "Build" (fun _ -> AdditionalArgs = additionalArgs }) // "Rebuild" ) +// Build target that only builds netstandard2.1 library and net7.0 test projects +// Intended for build platforms that does not support .NET Framework (eg. Linux/MAC) +Target "BuildNet" (fun _ -> + let additionalArgs = if versionSuffix.Length > 0 then [sprintf "/p:VersionSuffix=%s" versionSuffix] else [] + + let projects = + match (isWindows) with + | true -> !! "./src/**/*.csproj" + -- "./src/**/*.Tests.csproj" + -- "./src/examples/**/*.csproj" + | _ -> !! "./src/**/*.csproj" // if you need to filter specs for Linux vs. Windows, do it here + -- "./src/**/*.Tests.csproj" + -- "./src/examples/**/*.csproj" + + let tests = + match (isWindows) with + | true -> !! "./src/**/*.Tests.csproj" + | _ -> !! "./src/**/*.Tests.csproj" // if you need to filter specs for Linux vs. Windows, do it here + + let compileSingleProject project = + DotNetCli.Build + (fun p -> + { p with + Project = project + Configuration = configuration + Framework = buildNetVersion + AdditionalArgs = additionalArgs }) // "Rebuild" + + let compileSingleTest project = + DotNetCli.Build + (fun p -> + { p with + Project = project + Configuration = configuration + Framework = testNetVersion + AdditionalArgs = additionalArgs }) // "Rebuild" + + projects |> Seq.iter (log) + projects |> Seq.iter (compileSingleProject) + tests |> Seq.iter (log) + tests |> Seq.iter (compileSingleTest) +) //-------------------------------------------------------------------------------- // Tests targets @@ -96,7 +135,6 @@ type Runtime = let getTestAssembly runtime project = let assemblyPath = match runtime with - | NetCore -> !! ("src" @@ "**" @@ "bin" @@ "Release" @@ testNetCoreVersion @@ fileNameWithoutExt project + ".dll") | NetFramework -> !! ("src" @@ "**" @@ "bin" @@ "Release" @@ testNetFrameworkVersion @@ fileNameWithoutExt project + ".dll") | Net -> !! ("src" @@ "**" @@ "bin" @@ "Release" @@ testNetVersion @@ fileNameWithoutExt project + ".dll") @@ -147,30 +185,6 @@ Target "RunTests" (fun _ -> projects |> Seq.iter (runSingleProject) ) -Target "RunTestsNetCore" (fun _ -> - let projects = - match (isWindows) with - | true -> !! "./src/**/*.Tests.csproj" - | _ -> !! "./src/**/*.Tests.csproj" // if you need to filter specs for Linux vs. Windows, do it here - - let runSingleProject project = - let arguments = - match (hasTeamCity) with - | true -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory %s -- -parallel none -teamcity" testNetCoreVersion outputTests) - | false -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory %s -- -parallel none" testNetCoreVersion outputTests) - - let result = ExecProcess(fun info -> - info.FileName <- "dotnet" - info.WorkingDirectory <- (Directory.GetParent project).FullName - info.Arguments <- arguments) (TimeSpan.FromMinutes 30.0) - - ResultHandling.failBuildIfXUnitReportedError TestRunnerErrorLevel.Error result - - CreateDir outputTests - projects |> Seq.iter (log) - projects |> Seq.iter (runSingleProject) -) - Target "RunTestsNet" (fun _ -> let projects = match (isWindows) with @@ -380,15 +394,13 @@ Target "BuildRelease" DoNothing Target "All" DoNothing Target "Nuget" DoNothing Target "RunTestsFull" DoNothing -Target "RunTestsNetCoreFull" DoNothing // build dependencies "Clean" ==> "AssemblyInfo" ==> "Build" ==> "BuildRelease" // tests dependencies "Build" ==> "RunTests" -"Build" ==> "RunTestsNetCore" -"Build" ==> "RunTestsNet" +"AssemblyInfo" ==> "BuildNet" ==> "RunTestsNet" "Build" ==> "NBench" // nuget dependencies @@ -401,7 +413,6 @@ Target "RunTestsNetCoreFull" DoNothing // all "BuildRelease" ==> "All" "RunTests" ==> "All" -"RunTestsNetCore" ==> "All" "RunTestsNet" ==> "All" "NBench" ==> "All" "Nuget" ==> "All" diff --git a/src/Akka.Persistence.MongoDb.Hosting/Akka.Persistence.MongoDb.Hosting.csproj b/src/Akka.Persistence.MongoDb.Hosting/Akka.Persistence.MongoDb.Hosting.csproj index 5935f16..4653f95 100644 --- a/src/Akka.Persistence.MongoDb.Hosting/Akka.Persistence.MongoDb.Hosting.csproj +++ b/src/Akka.Persistence.MongoDb.Hosting/Akka.Persistence.MongoDb.Hosting.csproj @@ -1,6 +1,6 @@  - $(NetStandardLibVersion) + $(NetStandardLibVersion);$(NetFrameworkLibVersion) diff --git a/src/Akka.Persistence.MongoDb.Tests/Akka.Persistence.MongoDb.Tests.csproj b/src/Akka.Persistence.MongoDb.Tests/Akka.Persistence.MongoDb.Tests.csproj index 08c9d6f..ee3d384 100644 --- a/src/Akka.Persistence.MongoDb.Tests/Akka.Persistence.MongoDb.Tests.csproj +++ b/src/Akka.Persistence.MongoDb.Tests/Akka.Persistence.MongoDb.Tests.csproj @@ -1,6 +1,6 @@ - $(NetFrameworkTestVersion);$(NetTestVersion);$(NetCoreTestVersion) + $(NetFrameworkTestVersion);$(NetTestVersion) diff --git a/src/Akka.Persistence.MongoDb.Tests/MongoDbJournalSetupSpec.cs b/src/Akka.Persistence.MongoDb.Tests/MongoDbJournalSetupSpec.cs index 8e626d3..23e8b4a 100644 --- a/src/Akka.Persistence.MongoDb.Tests/MongoDbJournalSetupSpec.cs +++ b/src/Akka.Persistence.MongoDb.Tests/MongoDbJournalSetupSpec.cs @@ -31,10 +31,8 @@ public MongoDbJournalSetupSpec( private static ActorSystemSetup CreateBootstrapSetup(DatabaseFixture fixture) { - //Default LinqProvider has been changed to LINQ3.LinqProvider can be changed back to LINQ2 in the following way: var connectionString = new MongoUrl(fixture.ConnectionString); var clientSettings = MongoClientSettings.FromUrl(connectionString); - clientSettings.LinqProvider = LinqProvider.V2; var client = new MongoClient(clientSettings); var databaseName = connectionString.DatabaseName; var settings = client.Settings; diff --git a/src/Akka.Persistence.MongoDb.Tests/MongoDbSnapshotStoreSetupSpec.cs b/src/Akka.Persistence.MongoDb.Tests/MongoDbSnapshotStoreSetupSpec.cs index 3f249d7..b7e50f8 100644 --- a/src/Akka.Persistence.MongoDb.Tests/MongoDbSnapshotStoreSetupSpec.cs +++ b/src/Akka.Persistence.MongoDb.Tests/MongoDbSnapshotStoreSetupSpec.cs @@ -1,10 +1,8 @@ using Akka.Actor; using Akka.Actor.Setup; using Akka.Configuration; -using Akka.Persistence.TCK.Journal; using Akka.Persistence.TCK.Snapshot; using MongoDB.Driver; -using MongoDB.Driver.Linq; using Xunit; using Xunit.Abstractions; @@ -25,10 +23,8 @@ public MongoDbSnapshotStoreSetupSpec( private static ActorSystemSetup CreateBootstrapSetup(DatabaseFixture fixture) { - //Default LinqProvider has been changed to LINQ3.LinqProvider can be changed back to LINQ2 in the following way: var connectionString = new MongoUrl(fixture.ConnectionString); var clientSettings = MongoClientSettings.FromUrl(connectionString); - clientSettings.LinqProvider = LinqProvider.V2; var client = new MongoClient(clientSettings); var databaseName = connectionString.DatabaseName; var settings = client.Settings; diff --git a/src/Akka.Persistence.MongoDb/Akka.Persistence.MongoDb.csproj b/src/Akka.Persistence.MongoDb/Akka.Persistence.MongoDb.csproj index 724cc0e..59fda33 100644 --- a/src/Akka.Persistence.MongoDb/Akka.Persistence.MongoDb.csproj +++ b/src/Akka.Persistence.MongoDb/Akka.Persistence.MongoDb.csproj @@ -1,6 +1,6 @@  - $(NetStandardLibVersion) + $(NetStandardLibVersion);$(NetFrameworkLibVersion) @@ -10,6 +10,5 @@ - \ No newline at end of file diff --git a/src/Akka.Persistence.MongoDb/FullTypeNameObjectSerializer.cs b/src/Akka.Persistence.MongoDb/FullTypeNameObjectSerializer.cs index e07d0bb..42f4187 100644 --- a/src/Akka.Persistence.MongoDb/FullTypeNameObjectSerializer.cs +++ b/src/Akka.Persistence.MongoDb/FullTypeNameObjectSerializer.cs @@ -17,15 +17,20 @@ namespace Akka.Persistence.MongoDb /// /// Represents a serializer for objects. /// - class FullTypeNameObjectSerializer : ObjectSerializer + internal class FullTypeNameObjectSerializer : ClassSerializerBase, IHasDiscriminatorConvention { - protected readonly IDiscriminatorConvention DiscriminatorConvention = FullTypeNameDiscriminatorConvention.Instance; - + private readonly ObjectSerializer _serializer; + /// /// Initializes a new instance of the class. /// - public FullTypeNameObjectSerializer() : base(FullTypeNameDiscriminatorConvention.Instance, AllAllowedTypes) { } + public FullTypeNameObjectSerializer() + { + _serializer = new ObjectSerializer(DiscriminatorConvention, ObjectSerializer.AllAllowedTypes); + } + public IDiscriminatorConvention DiscriminatorConvention => FullTypeNameDiscriminatorConvention.Instance; + /// /// Deserializes a value. /// @@ -38,7 +43,7 @@ public override object Deserialize(BsonDeserializationContext context, BsonDeser RegisterNewTypesToDiscriminator(DiscriminatorConvention.GetActualType(bsonReader, typeof(object))); } - return base.Deserialize(context, args); + return _serializer.Deserialize(context, args); } /// @@ -52,14 +57,14 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati RegisterNewTypesToDiscriminator(value.GetType()); } - base.Serialize(context, args, value); + _serializer.Serialize(context, args, value); } /// /// If the type is not registered, attach it to our discriminator /// /// the type to examine - protected void RegisterNewTypesToDiscriminator(Type actualType) + private void RegisterNewTypesToDiscriminator(Type actualType) { // we've detected a new concrete type that isn't registered in MongoDB's serializer if (actualType != typeof(object) && !actualType.GetTypeInfo().IsInterface && !BsonSerializer.IsTypeDiscriminated(actualType)) diff --git a/src/Akka.Persistence.MongoDb/Journal/MongoDbJournal.cs b/src/Akka.Persistence.MongoDb/Journal/MongoDbJournal.cs index deadb2a..7cc66a3 100644 --- a/src/Akka.Persistence.MongoDb/Journal/MongoDbJournal.cs +++ b/src/Akka.Persistence.MongoDb/Journal/MongoDbJournal.cs @@ -74,11 +74,8 @@ private IMongoDatabase GetMongoDb() return _mongoDatabase_DoNotUseDirectly; } - //Default LinqProvider has been changed to LINQ3.LinqProvider can be changed back to LINQ2 in the following way: var connectionString = new MongoUrl(_settings.ConnectionString); var clientSettings = MongoClientSettings.FromUrl(connectionString); - clientSettings.LinqProvider = LinqProvider.V2; - client = new MongoClient(clientSettings); _mongoDatabase_DoNotUseDirectly = client.GetDatabase(connectionString.DatabaseName); return _mongoDatabase_DoNotUseDirectly; diff --git a/src/Akka.Persistence.MongoDb/Journal/MongoDbJournalQueries.cs b/src/Akka.Persistence.MongoDb/Journal/MongoDbJournalQueries.cs index 71d488f..ddf127d 100644 --- a/src/Akka.Persistence.MongoDb/Journal/MongoDbJournalQueries.cs +++ b/src/Akka.Persistence.MongoDb/Journal/MongoDbJournalQueries.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; diff --git a/src/Akka.Persistence.MongoDb/Snapshot/MongoDbGridFSSnapshotStore.cs b/src/Akka.Persistence.MongoDb/Snapshot/MongoDbGridFSSnapshotStore.cs index 1fc471e..e6667c6 100644 --- a/src/Akka.Persistence.MongoDb/Snapshot/MongoDbGridFSSnapshotStore.cs +++ b/src/Akka.Persistence.MongoDb/Snapshot/MongoDbGridFSSnapshotStore.cs @@ -78,10 +78,8 @@ private IMongoDatabase GetMongoDb() var setupOption = Context.System.Settings.Setup.Get(); if (!setupOption.HasValue || setupOption.Value.SnapshotConnectionSettings == null) { - //Default LinqProvider has been changed to LINQ3.LinqProvider can be changed back to LINQ2 in the following way: var connectionString = new MongoUrl(_settings.ConnectionString); var clientSettings = MongoClientSettings.FromUrl(connectionString); - clientSettings.LinqProvider = LinqProvider.V2; client = new MongoClient(clientSettings); _mongoDatabase_DoNotUseDirectly = client.GetDatabase(connectionString.DatabaseName); return _mongoDatabase_DoNotUseDirectly; diff --git a/src/Akka.Persistence.MongoDb/Snapshot/MongoDbSnapshotStore.cs b/src/Akka.Persistence.MongoDb/Snapshot/MongoDbSnapshotStore.cs index 8ba755e..83b0fe9 100644 --- a/src/Akka.Persistence.MongoDb/Snapshot/MongoDbSnapshotStore.cs +++ b/src/Akka.Persistence.MongoDb/Snapshot/MongoDbSnapshotStore.cs @@ -12,7 +12,6 @@ using Akka.Persistence.Snapshot; using Akka.Util; using MongoDB.Driver; -using MongoDB.Driver.Linq; #nullable enable namespace Akka.Persistence.MongoDb.Snapshot @@ -94,10 +93,8 @@ private IMongoDatabase GetMongoDb() var setupOption = Context.System.Settings.Setup.Get(); if (!setupOption.HasValue || setupOption.Value.SnapshotConnectionSettings == null) { - //Default LinqProvider has been changed to LINQ3.LinqProvider can be changed back to LINQ2 in the following way: var connectionString = new MongoUrl(_settings.ConnectionString); var clientSettings = MongoClientSettings.FromUrl(connectionString); - clientSettings.LinqProvider = LinqProvider.V2; client = new MongoClient(clientSettings); _mongoDatabase_DoNotUseDirectly = client.GetDatabase(connectionString.DatabaseName); return _mongoDatabase_DoNotUseDirectly; @@ -135,7 +132,7 @@ protected override void PostStop() base.PostStop(); } - protected override async Task LoadAsync(string persistenceId, SnapshotSelectionCriteria criteria) + protected override async Task LoadAsync(string persistenceId, SnapshotSelectionCriteria criteria) { using var unitedCts = CreatePerCallCts(); var snapshotCollection = await GetSnapshotCollection(unitedCts.Token); @@ -143,11 +140,12 @@ protected override async Task LoadAsync(string persistenceId, return await MaybeWithTransaction(async (session, token) => { var filter = CreateRangeFilter(persistenceId, criteria); - return await (session is not null ? snapshotCollection.Find(session, filter) : snapshotCollection.Find(filter)) + var entry = await (session is not null ? snapshotCollection.Find(session, filter) : snapshotCollection.Find(filter)) .SortByDescending(x => x.SequenceNr) .Limit(1) - .Project(x => ToSelectedSnapshot(x)) .FirstOrDefaultAsync(token); + + return ToSelectedSnapshot(entry); }, unitedCts.Token); } @@ -272,12 +270,15 @@ private SnapshotEntry ToSnapshotEntry(SnapshotMetadata metadata, object snapshot Snapshot = binary, Timestamp = metadata.Timestamp.Ticks, Manifest = binaryManifest, - SerializerId = serializer?.Identifier + SerializerId = serializer.Identifier }; } - private SelectedSnapshot ToSelectedSnapshot(SnapshotEntry entry) + private SelectedSnapshot? ToSelectedSnapshot(SnapshotEntry? entry) { + if (entry is null) + return null; + if (_settings.LegacySerialization) { return new SelectedSnapshot( diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 43a5239..3fcbfb5 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -15,10 +15,10 @@ latest - netcoreapp3.1 net7.0 - net471 - netstandard2.0 + net472 + netstandard2.1 + net472 diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 4b5ba08..e0ee314 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -1,7 +1,9 @@ true - 2.30.0 + 1.5.31 + 1.5.31.1 + 3.0.0 1.5.32 1.5.32 @@ -12,7 +14,6 @@ - @@ -24,7 +25,7 @@ - +