Skip to content

Commit ebb528e

Browse files
authored
Merge pull request #5926 from Particular/split-audit-store
Split up saga and message audit stores
2 parents 6cb9452 + 613be00 commit ebb528e

20 files changed

Lines changed: 78 additions & 39 deletions

File tree

‎src/ServiceControl.Audit.Persistence.InMemory/InMemoryAuditDataStore.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using ServiceControl.Audit.Infrastructure;
1212
using ServiceControl.SagaAudit;
1313

14-
class InMemoryAuditDataStore : IAuditDataStore
14+
class InMemoryAuditDataStore : IAuditMessagesViewDataStore, ISagaHistoryDataStore
1515
{
1616
IBodyStorage bodyStorage;
1717
public List<FailedAuditImport> failedAuditImports;

‎src/ServiceControl.Audit.Persistence.InMemory/InMemoryPersistence.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public void AddPersistence(IServiceCollection services)
1010
{
1111
services.AddSingleton(persistenceSettings);
1212
services.AddSingleton<InMemoryAuditDataStore>();
13-
services.AddSingleton<IAuditDataStore>(sp => sp.GetRequiredService<InMemoryAuditDataStore>());
13+
services.AddSingleton<IAuditMessagesViewDataStore>(sp => sp.GetRequiredService<InMemoryAuditDataStore>());
14+
services.AddSingleton<ISagaHistoryDataStore>(sp => sp.GetRequiredService<InMemoryAuditDataStore>());
1415
services.AddSingleton<IBodyStorage, InMemoryAttachmentsBodyStorage>();
1516
services.AddSingleton<IFailedAuditStorage, InMemoryFailedAuditStorage>();
1617
services.AddSingleton<IAuditIngestionUnitOfWorkFactory, InMemoryAuditIngestionUnitOfWorkFactory>();

‎src/ServiceControl.Audit.Persistence.RavenDB/RavenAuditDataStore.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ServiceControl.Audit.Persistence.RavenDB
1616
using Transformers;
1717

1818
class RavenAuditDataStore(IRavenSessionProvider sessionProvider, DatabaseConfiguration databaseConfiguration)
19-
: IAuditDataStore
19+
: IAuditMessagesViewDataStore, ISagaHistoryDataStore
2020
{
2121
public async Task<QueryResult<SagaHistory>> QuerySagaHistoryById(Guid input, CancellationToken cancellationToken = default)
2222
{

‎src/ServiceControl.Audit.Persistence.RavenDB/RavenPersistence.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public void AddPersistence(IServiceCollection services)
2525
endpointConfiguration.AddCustomCheck<CheckRavenDBSearchEngine>();
2626
}
2727

28-
services.AddSingleton<IAuditDataStore, RavenAuditDataStore>();
28+
services.AddSingleton<RavenAuditDataStore>();
29+
services.AddSingleton<IAuditMessagesViewDataStore>(sp => sp.GetRequiredService<RavenAuditDataStore>());
30+
services.AddSingleton<ISagaHistoryDataStore>(sp => sp.GetRequiredService<RavenAuditDataStore>());
2931
services.AddSingleton<IAuditIngestionUnitOfWorkFactory, RavenAuditIngestionUnitOfWorkFactory>();
3032
services.AddSingleton<IFailedAuditStorage, RavenFailedAuditStorage>();
3133
services.AddSingleton<MinimumRequiredStorageState>();

‎src/ServiceControl.Audit.Persistence.Tests.RavenDB/EmbeddedLifecycleTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override async Task Setup()
3737
[Test]
3838
public async Task Verify_embedded_database()
3939
{
40-
await DataStore.GetMessages(false, new PagingInfo(), new SortInfo("Id", "asc"), cancellationToken: TestContext.CurrentContext.CancellationToken);
40+
await MessagesViewStore.GetMessages(false, new PagingInfo(), new SortInfo("Id", "asc"), cancellationToken: TestContext.CurrentContext.CancellationToken);
4141

4242
using (Assert.EnterMultipleScope())
4343
{

‎src/ServiceControl.Audit.Persistence.Tests.RavenDB/PersistenceTestsConfiguration.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
class PersistenceTestsConfiguration
1818
{
19-
public IAuditDataStore AuditDataStore { get; private set; }
19+
public IAuditMessagesViewDataStore MessagesViewStore { get; private set; }
20+
21+
public ISagaHistoryDataStore SagaHistoryStore { get; private set; }
2022

2123
public IFailedAuditStorage FailedAuditStorage { get; private set; }
2224

@@ -78,7 +80,8 @@ public async Task Configure(Action<PersistenceSettings> setSettings)
7880
host = hostBuilder.Build();
7981
await host.StartAsync();
8082

81-
AuditDataStore = host.Services.GetRequiredService<IAuditDataStore>();
83+
MessagesViewStore = host.Services.GetRequiredService<IAuditMessagesViewDataStore>();
84+
SagaHistoryStore = host.Services.GetRequiredService<ISagaHistoryDataStore>();
8285
FailedAuditStorage = host.Services.GetRequiredService<IFailedAuditStorage>();
8386

8487
var documentStoreProvider = host.Services.GetRequiredService<IRavenDocumentStoreProvider>();

‎src/ServiceControl.Audit.Persistence.Tests.RavenDB/ProcessedMessageIdTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task Retains_existing_document_and_body_ids()
3333
var expectedId = $"ProcessedMessages-{started.UtcDateTime.Ticks}-{message.GetProcessingId()}";
3434
Assert.That(message.Id, Is.EqualTo(expectedId));
3535
Assert.That(message.MessageMetadata["BodyUrl"], Is.EqualTo($"/messages/{expectedId}/body"));
36-
var body = await DataStore.GetMessageBody(expectedId);
36+
var body = await MessagesViewStore.GetMessageBody(expectedId);
3737
Assert.That(body.HasContent, Is.True);
3838
body.StreamContent.Dispose();
3939
}

‎src/ServiceControl.Audit.Persistence.Tests.RavenDB/RetentionTests.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task AuditMessageRetention()
4343
Assert.That(queryResultAfterExpiration.Results, Is.Empty);
4444

4545
Task<QueryResult<IList<MessagesView>>> QueryMessages(CancellationToken cancellationToken) =>
46-
DataStore.QueryMessages("MyMessageId", new PagingInfo(), new SortInfo("Id", "asc"), cancellationToken: cancellationToken);
46+
MessagesViewStore.QueryMessages("MyMessageId", new PagingInfo(), new SortInfo("Id", "asc"), cancellationToken: cancellationToken);
4747
}
4848

4949
[Test]
@@ -70,7 +70,7 @@ await IngestSagaAudits(
7070
Assert.That(queryResultAfterExpiration.Results, Is.Null);
7171

7272
Task<QueryResult<SagaHistory>> QuerySagaHistory(CancellationToken cancellationToken) =>
73-
DataStore.QuerySagaHistoryById(sagaId, cancellationToken);
73+
SagaHistoryStore.QuerySagaHistoryById(sagaId, cancellationToken);
7474
}
7575

7676
// The retention window starts at ingestion, so building the indexes of the fresh database must

‎src/ServiceControl.Audit.Persistence.Tests/AuditCountingTests.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public async Task ShouldCountAuditedMessages()
4646

4747
await IngestProcessedMessagesAudits(messages);
4848

49-
var endpointA = (await DataStore.QueryAuditCounts("EndpointA", TestContext.CurrentContext.CancellationToken)).Results;
50-
var endpointB = (await DataStore.QueryAuditCounts("EndpointB", TestContext.CurrentContext.CancellationToken)).Results;
51-
var sysMsgEndpoint = (await DataStore.QueryAuditCounts("SystemEndpoint", TestContext.CurrentContext.CancellationToken)).Results;
49+
var endpointA = (await MessagesViewStore.QueryAuditCounts("EndpointA", TestContext.CurrentContext.CancellationToken)).Results;
50+
var endpointB = (await MessagesViewStore.QueryAuditCounts("EndpointB", TestContext.CurrentContext.CancellationToken)).Results;
51+
var sysMsgEndpoint = (await MessagesViewStore.QueryAuditCounts("SystemEndpoint", TestContext.CurrentContext.CancellationToken)).Results;
5252

5353
Assert.That(sysMsgEndpoint, Is.Empty);
5454

@@ -83,7 +83,7 @@ public async Task Should_return_zero_throughput_entry_when_SendOnly()
8383
await IngestProcessedMessagesAudits(messages);
8484

8585
// Act
86-
var result = (await DataStore.QueryAuditCounts(sendOnlyEndpoint, TestContext.CurrentContext.CancellationToken)).Results;
86+
var result = (await MessagesViewStore.QueryAuditCounts(sendOnlyEndpoint, TestContext.CurrentContext.CancellationToken)).Results;
8787

8888
// Assert
8989
Assert.That(result, Is.Not.Empty, "Expected non-empty result for endpoint that only sent messages");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ServiceControl.Audit.Persistence.Tests
2+
{
3+
using Microsoft.Extensions.DependencyInjection;
4+
using NUnit.Framework;
5+
6+
[TestFixture]
7+
class AuditDataStoreRegistrationTests : PersistenceTestFixture
8+
{
9+
[Test]
10+
public void Should_resolve_all_capabilities_from_one_store_instance()
11+
{
12+
var messagesViewStore = ServiceProvider.GetRequiredService<IAuditMessagesViewDataStore>();
13+
var sagaHistoryStore = ServiceProvider.GetRequiredService<ISagaHistoryDataStore>();
14+
15+
Assert.That(sagaHistoryStore, Is.SameAs(messagesViewStore));
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)