Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/NServiceBus.Storage.MongoDB/Outbox/OutboxInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public async Task Install(string identity, CancellationToken cancellationToken =
var databaseSettings = settings.Get<MongoDatabaseSettings>();
var collectionSettings = settings.Get<MongoCollectionSettings>();
var collectionNamingConvention = settings.Get<Func<Type, string>>(SettingsKeys.CollectionNamingConvention);

if (!settings.TryGet(SettingsKeys.TimeToKeepOutboxDeduplicationData, out TimeSpan timeToKeepOutboxDeduplicationData))
{
timeToKeepOutboxDeduplicationData = DefaultTimeToKeepOutboxDeduplicationData;
}
var timeToKeepOutboxDeduplicationData = settings.GetTimeToKeepOutboxDeduplicationData();

// We have to resolve the client provider here because at the time of the creation of the installer the provider might not be registered yet.
var clientProvider = serviceProvider.GetRequiredService<IMongoClientProvider>();
Expand Down Expand Up @@ -84,7 +80,5 @@ await outboxCollection.Indexes.CreateOneAsync(indexModel, cancellationToken: can
.ConfigureAwait(false);
}

static readonly TimeSpan DefaultTimeToKeepOutboxDeduplicationData = TimeSpan.FromDays(7);

internal const string OutboxCleanupIndexName = "OutboxCleanup";
}
19 changes: 19 additions & 0 deletions src/NServiceBus.Storage.MongoDB/Outbox/OutboxSettingsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace NServiceBus.Storage.MongoDB;

using System;
using Settings;

static class OutboxSettingsExtensions
{
public static TimeSpan GetTimeToKeepOutboxDeduplicationData(this IReadOnlySettings settings)
{
if (!settings.TryGet(SettingsKeys.TimeToKeepOutboxDeduplicationData, out TimeSpan timeToKeepOutboxDeduplicationData))
{
timeToKeepOutboxDeduplicationData = DefaultTimeToKeepOutboxDeduplicationData;
}

return timeToKeepOutboxDeduplicationData;
}

static readonly TimeSpan DefaultTimeToKeepOutboxDeduplicationData = TimeSpan.FromDays(7);
}
14 changes: 11 additions & 3 deletions src/NServiceBus.Storage.MongoDB/Outbox/OutboxStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ protected override void Setup(FeatureConfigurationContext context)

context.Services.AddSingleton<IOutboxStorage>(sp => new OutboxPersister(sp.GetRequiredService<IMongoClientProvider>().Client, databaseName, databaseSettings, collectionNamingConvention, collectionSettings));

RegisterOutboxClassMappings();
var usesDefaultClassMap = RegisterOutboxClassMappings();

context.Settings.AddStartupDiagnosticsSection("NServiceBus.Storage.MongoDB.Outbox", new
{
UsesDefaultClassMap = usesDefaultClassMap,
TimeToKeepDeduplicationData = context.Settings.GetTimeToKeepOutboxDeduplicationData(),
});
}

internal static void RegisterOutboxClassMappings()
internal static bool RegisterOutboxClassMappings()
{
if (BsonClassMap.IsClassMapRegistered(typeof(StorageTransportOperation)))
{
return;
return false;
}

BsonClassMap.RegisterClassMap<StorageTransportOperation>(cm =>
Expand All @@ -57,5 +63,7 @@ internal static void RegisterOutboxClassMappings()
new DictionaryInterfaceImplementerSerializer<Dictionary<string, string>>(
DictionaryRepresentation.ArrayOfDocuments));
});

return true;
}
}
24 changes: 22 additions & 2 deletions src/NServiceBus.Storage.MongoDB/Sagas/SagaStorage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace NServiceBus.Storage.MongoDB;

using System.Collections.Generic;
using Features;
using global::MongoDB.Bson.Serialization;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -26,26 +27,45 @@ protected override void Setup(FeatureConfigurationContext context)
context.Services.AddSingleton<ISagaPersister>(new SagaPersister(versionElementName, memberMapCache));

var sagaMetadataCollection = context.Settings.Get<SagaMetadataCollection>();
RegisterSagaEntityClassMappings(sagaMetadataCollection, memberMapCache);
var classMappings = RegisterSagaEntityClassMappings(sagaMetadataCollection, memberMapCache);

context.Settings.AddStartupDiagnosticsSection("NServiceBus.Storage.MongoDB.Sagas", new
{
VersionElement = versionElementName,
ClassMappings = classMappings
});
}

internal static void RegisterSagaEntityClassMappings(SagaMetadataCollection sagaMetadataCollection, MemberMapCache memberMapCache)
internal readonly struct MappingMetadata(string sagaEntity, bool usesDefaultClassMap)
{
public string SagaEntity { get; } = sagaEntity;
public bool UsesDefaultClassMap { get; } = usesDefaultClassMap;
}

internal static IReadOnlyCollection<MappingMetadata> RegisterSagaEntityClassMappings(SagaMetadataCollection sagaMetadataCollection, MemberMapCache memberMapCache)
{
var sagaEntityToClassMapDiagnostics = new List<MappingMetadata>();
foreach (var sagaMetadata in sagaMetadataCollection)
{
var usesDefaultClassMap = false;
if (!BsonClassMap.IsClassMapRegistered(sagaMetadata.SagaEntityType))
{
var classMap = new BsonClassMap(sagaMetadata.SagaEntityType);
classMap.AutoMap();
classMap.SetIgnoreExtraElements(true);

BsonClassMap.RegisterClassMap(classMap);

usesDefaultClassMap = true;
}

sagaEntityToClassMapDiagnostics.Add(new(sagaMetadata.SagaEntityType.FullName!, usesDefaultClassMap));

if (sagaMetadata.TryGetCorrelationProperty(out var property) && property.Name != "Id")
{
_ = memberMapCache.GetOrAdd(sagaMetadata.SagaEntityType, property);
}
}
return sagaEntityToClassMapDiagnostics;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ protected override void Setup(FeatureConfigurationContext context)
useTransactions = true;
}

context.Settings.AddStartupDiagnosticsSection("NServiceBus.Storage.MongoDB.StorageSession", new
{
UseTransaction = useTransactions
Copy link
Contributor Author

@danielmarbach danielmarbach Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about adding the provider, but that could give false indications when overridden by DI.

});

context.RegisterStartupTask(sp => new VerifyClusterDetails(sp.GetRequiredService<IMongoClientProvider>(), databaseName, useTransactions));

context.Services.AddScoped<ICompletableSynchronizedStorageSession, SynchronizedStorageSession>();
Expand Down
Loading