-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cap the maximum number of entries in SagaDetailsIndex to 50k * Add test to check that index has a take * Add a unit test for the removal of the legacy saga details index --------- Co-authored-by: Szymon Pobiega <szymon.pobiega@gmail.com>
- Loading branch information
1 parent
a2e7a5f
commit f34afdc
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/ServiceControl.Audit.Persistence.Tests.RavenDB/SagaDetailsIndexTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
namespace ServiceControl.Audit.Persistence.Tests | ||
{ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using Raven.Client.Documents.Indexes; | ||
using Raven.Client.Documents.Operations.Indexes; | ||
using ServiceControl.SagaAudit; | ||
|
||
[TestFixture] | ||
class SagaDetailsIndexTests : PersistenceTestFixture | ||
{ | ||
[Test] | ||
public async Task Deletes_index_that_does_not_have_cap_of_50000() | ||
{ | ||
await configuration.DocumentStore.Maintenance.SendAsync(new DeleteIndexOperation("SagaDetailsIndex")); | ||
|
||
var indexWithout50000capDefinition = new IndexDefinition | ||
{ | ||
Name = "SagaDetailsIndex", | ||
Maps = new System.Collections.Generic.HashSet<string> | ||
{ | ||
@"from doc in docs | ||
select new | ||
{ | ||
doc.SagaId, | ||
Id = doc.SagaId, | ||
doc.SagaType, | ||
Changes = new[] | ||
{ | ||
new | ||
{ | ||
Endpoint = doc.Endpoint, | ||
FinishTime = doc.FinishTime, | ||
InitiatingMessage = doc.InitiatingMessage, | ||
OutgoingMessages = doc.OutgoingMessages, | ||
StartTime = doc.StartTime, | ||
StateAfterChange = doc.StateAfterChange, | ||
Status = doc.Status | ||
} | ||
} | ||
}" | ||
}, | ||
Reduce = @"from result in results | ||
group result by result.SagaId | ||
into g | ||
let first = g.First() | ||
select new | ||
{ | ||
Id = first.SagaId, | ||
SagaId = first.SagaId, | ||
SagaType = first.SagaType, | ||
Changes = g.SelectMany(x => x.Changes) | ||
.OrderByDescending(x => x.FinishTime) | ||
.ToList() | ||
}" | ||
}; | ||
|
||
var putIndexesOp = new PutIndexesOperation(indexWithout50000capDefinition); | ||
|
||
await configuration.DocumentStore.Maintenance.SendAsync(putIndexesOp); | ||
|
||
var sagaDetailsIndexOperation = new GetIndexOperation("SagaDetailsIndex"); | ||
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation); | ||
|
||
Assert.IsNotNull(sagaDetailsIndexDefinition); | ||
|
||
await Persistence.RavenDB.DatabaseSetup.DeleteLegacySagaDetailsIndex(configuration.DocumentStore, CancellationToken.None); | ||
|
||
sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation); | ||
|
||
Assert.IsNull(sagaDetailsIndexDefinition); | ||
} | ||
|
||
[Test] | ||
public async Task Does_not_delete_index_that_does_have_cap_of_50000() | ||
{ | ||
await Persistence.RavenDB.DatabaseSetup.DeleteLegacySagaDetailsIndex(configuration.DocumentStore, CancellationToken.None); | ||
|
||
var sagaDetailsIndexOperation = new GetIndexOperation("SagaDetailsIndex"); | ||
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation); | ||
|
||
Assert.IsNotNull(sagaDetailsIndexDefinition); | ||
} | ||
|
||
[Test] | ||
public async Task Should_only_reduce_the_last_50000_saga_state_changes() | ||
{ | ||
var sagaType = "MySagaType"; | ||
var sagaState = "some-saga-state"; | ||
|
||
await IngestSagaAudits(new SagaSnapshot | ||
{ | ||
SagaId = Guid.NewGuid(), | ||
SagaType = sagaType, | ||
Status = SagaStateChangeStatus.New, | ||
StateAfterChange = sagaState | ||
}); | ||
|
||
await configuration.CompleteDBOperation(); | ||
|
||
using (var session = configuration.DocumentStore.OpenAsyncSession()) | ||
{ | ||
var sagaDetailsIndexOperation = new GetIndexOperation("SagaDetailsIndex"); | ||
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation); | ||
|
||
Assert.IsTrue(sagaDetailsIndexDefinition.Reduce.Contains("Take(50000)"), "The SagaDetails index definition does not contain a .Take(50000) to limit the number of saga state changes that are reduced by the map/reduce"); | ||
} | ||
} | ||
|
||
async Task IngestSagaAudits(params SagaSnapshot[] snapshots) | ||
{ | ||
var unitOfWork = StartAuditUnitOfWork(snapshots.Length); | ||
foreach (var snapshot in snapshots) | ||
{ | ||
await unitOfWork.RecordSagaSnapshot(snapshot); | ||
} | ||
await unitOfWork.DisposeAsync(); | ||
await configuration.CompleteDBOperation(); | ||
} | ||
} | ||
} |