From 36feb4b91bc99fc9e6b0451bd7a82f0ab7a56815 Mon Sep 17 00:00:00 2001 From: Rhys Bevilaqua Date: Thu, 19 Nov 2026 12:41:14 +0800 Subject: [PATCH 1/8] Add pre-flight check for incomplete batch operations --- .../ravendb-to-sql-migration-instructions.md | 15 ++ .../ravendb-to-sql-migration-overview.md | 7 +- .../ravendb-to-sql-migration-system-design.md | 5 +- .../DataMigration/RavenMigrationSource.cs | 2 +- .../UnfinishedOperationsCheck.cs | 46 ++++++ .../UnfinishedOperationsCheckTests.cs | 150 ++++++++++++++++++ 6 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 src/ServiceControl.Persistence.RavenDB/DataMigration/UnfinishedOperationsCheck.cs create mode 100644 src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnfinishedOperationsCheckTests.cs diff --git a/docs/migration/ravendb-to-sql-migration-instructions.md b/docs/migration/ravendb-to-sql-migration-instructions.md index 27e456c9c0..3fcab78b59 100644 --- a/docs/migration/ravendb-to-sql-migration-instructions.md +++ b/docs/migration/ravendb-to-sql-migration-instructions.md @@ -72,6 +72,7 @@ Get the data ready: - Resolve or archive as many unresolved failed messages as you can. They never age out, and ServiceControl stays closed until they are all copied. **A large backlog makes the outage long**. The copy reads archived and resolved messages only after ServiceControl opens. The exception is a RavenDB index that is broken or leaves out some failed messages: then the copy reads every failed message during the outage, logs a warning, and a large archive makes the outage longer too. - Run `--import-failed-errors` while the instance is still on RavenDB. A failed error import is a message that ServiceControl took off the error queue but failed to store. The migration copies these either way, but imported ones arrive as ordinary failed messages. +- Let retry, archive and unarchive operations finish on RavenDB before stopping ServiceControl. Migration refuses persisted batches, the retry forwarding marker, and archive or unarchive operation documents. Stopping ServiceControl leaves unfinished work persisted. Keep these settings as they are: @@ -205,6 +206,20 @@ No configuration change is needed to finish. Once every category is Done or Aban ServiceControl does not start, and its log names the check that failed. Nothing is copied. Correct the cause, then run `--start-data-migration` again. The dry run runs the same checks, so you can use it to test your correction. +### RavenDB holds unfinished operations + +The refusal names the unfinished operation families and an example document id from each. The migration does not copy active operation state. Completed retry history and outstanding retry claims do not cause this refusal. + +1. Set `ServiceControl/PersistenceType` back to RavenDB. +2. Start ServiceControl. +3. Request the same group action again if an archive or unarchive operation does not resume. +4. Wait until the retry, archive and unarchive operations finish. +5. Stop ServiceControl. +6. Set `ServiceControl/PersistenceType` back to the SQL target. +7. Run `--start-data-migration` again. + +If the refusal persists, contact support rather than deleting operation documents. A batch document can remain without its operation document. + ### A category is Failed A category becomes Failed when it cannot copy rows, or when an error stops it. The rows that it already copied stay in SQL. A restart does not change a Failed category. You must run one of two commands. diff --git a/docs/migration/ravendb-to-sql-migration-overview.md b/docs/migration/ravendb-to-sql-migration-overview.md index 36c4d99b4d..80822ca732 100644 --- a/docs/migration/ravendb-to-sql-migration-overview.md +++ b/docs/migration/ravendb-to-sql-migration-overview.md @@ -165,8 +165,7 @@ There are eighteen *categories*: sixteen required and two optional. **Never copied, because nothing needs them:** - RavenDB index definitions. -- In-flight work collections, which are empty when nothing is running: `RetryBatches`, `RetryBatchNowForwardings`, `ArchiveOperations` and `UnarchiveOperations`. -- `ArchiveBatches` and `UnarchiveBatches`, which exist only because of how RavenDB works. +- In-flight retry, archive and unarchive work: `RetryBatches`, `RetryBatchNowForwardings`, `ArchiveOperations`, `UnarchiveOperations`, `ArchiveBatches` and `UnarchiveBatches`. Migration refuses while any of these documents remain. Stopping ServiceControl does not finish persisted work; restart on RavenDB, let the operations finish, stop it, and retry migration. - The `ConnectedApplications` document, written only by versions 6.0 and 6.1. Nothing has read it since 6.2. - Broker and audit service version details, which refill on the next throughput collection. - Failed message edit locks, which stop one failed message being edited twice. See [Open decisions](#values-that-change-or-merge-in-sql). @@ -263,7 +262,9 @@ In this order, cheapest first: If every required category is already Done or Abandoned, the offline command can succeed without opening the source. Normal startup does not need the source; only the online copier opens it. -Checks stop at the first refusal, which names the check. +9. The source holds no persisted retry batches, forwarding marker, archive operations or batches, or unarchive operations or batches. The check names an example document from each unfinished family. Completed retry history and outstanding retry claims do not cause this refusal. + +Checks stop at the first refusal, which names the check. If persisted operations remain, start ServiceControl on RavenDB again, let them finish, stop it, and rerun `--start-data-migration`. ```mermaid flowchart LR diff --git a/docs/migration/ravendb-to-sql-migration-system-design.md b/docs/migration/ravendb-to-sql-migration-system-design.md index d066366a1b..d86725c210 100644 --- a/docs/migration/ravendb-to-sql-migration-system-design.md +++ b/docs/migration/ravendb-to-sql-migration-system-design.md @@ -106,7 +106,7 @@ sequenceDiagram Checks->>Target: Open Start->>Start: IMigrationCheckpointStore.ReadAll Start->>Source: Open - Start->>Checks: source.ContributedChecks(), empty for RavenDB + Start->>Checks: source.ContributedChecks(), UnfinishedOperationsCheck Start->>Engine: RunRequiredCategories under ClosedWindowProgress Engine-->>Start: one checkpoint per category Start->>Start: ReportWhatTheCopyLeftBehind @@ -140,7 +140,7 @@ Each position in the sequence is deliberate: > [!IMPORTANT] > Readers must stream by id prefix, load by id, or query a static index. `RefuseWrite` blocks writes but passes query POSTs (`/queries`, `/multi_get`, `/streams/queries`), and a dynamic query creates an `Auto/` index. Only the embedded server sets `DisableAutoIndexes`. 7. **Source outage tolerance.** A failed source open is tolerated only when every required category already `IsFinished`. The host logs it, `MigrationStartup.RecordSourceOutage` writes the exception (inner exception when present) as `LastError` on each selected optional category still copying, creating `NotStarted` rows where absent, the command skips the copy and succeeds. The operator can then start ServiceControl; its online copier handles any later source outage independently. In every other case the host refuses and writes nothing; if a required category never started, the refusal says the only exits are pointing back at RavenDB or starting over. -8. **`source.ContributedChecks()`** runs once the source is open, because such a check needs a session. `RavenMigrationSource` returns none, and a refusal here is never tolerated. +8. **`source.ContributedChecks()`** runs once the source is open, because such a check needs a session. `RavenMigrationSource` contributes `UnfinishedOperationsCheck`, which streams one document from each of `RetryBatches/`, `ArchiveOperations/` and `UnarchiveOperations/`. These prefixes cover retry batches and the forwarding marker, plus archive and unarchive operation documents and their batches. Any match refuses the run before checkpoint initialization or copying, names an example document from each unfinished family, and instructs the operator to restart on RavenDB and let the work finish. Completed retry history and retry claims use different prefixes and do not cause this refusal. A refusal here is never tolerated. 9. **Checkpoint initialization.** The command seeds `NotStarted` rows for required and selected optional categories before copying. `MigrationStartup.RunRequiredCategories` also ensures required rows exist, then runs each required category under its own linked `CancellationTokenSource`, awaited with `WaitAsync` on that token. 10. **`ClosedWindowProgress`** polls the checkpoint store every 30 seconds and logs progress. When the running category has committed nothing for 30 minutes, measured from the later of `LastProgressAt` and its run start, it cancels that category's token. `WaitAsync` returns even if the call underneath ignores cancellation, the category settles `Halted` with the stall in `LastError`, and the next category runs. The abandoned call keeps running until the host refuses and the process exits. A host shutdown leaves the row `InProgress`. 11. **`CopyOrExplainWhyItStopped`** maps `MigrationCheckpointConflictException` to a message about a second instance writing the same checkpoints. @@ -154,6 +154,7 @@ Each position in the sequence is deliberate: - It runs the selected, supported optional categories in registry order, `EventLog` then `ArchivedAndResolvedFailedMessages`, skipping rows that are `IsFinished` or `IsFailed`. The offline command has already seeded the rows that identify the online work. - It creates and opens its own source only when a supported category is still copying. On failure it calls `RecordSourceOutage` and exits for this start. It does not depend on in-process state from `--start-data-migration`, which ran in another process. +- It reruns source-contributed checks before copying. The RavenDB unfinished-operation check should still pass because the source is read-only and new operations run on SQL. A refusal stops the online copy for that start, not ServiceControl. - An exception leaves the category `InProgress` with `LastError`, untouched until the next start, so a transient failure never forces an abandon. Windowing: `MigrationEngineOptions.WindowFor` supplies the window and the reader applies it through `IWindowedMigrationCategoryReader`. The engine writes `StartedWindowSeconds` once, on the first `InProgress` upsert. `CopiesFrom` is `StartedAt` minus the currently configured window, so window stability across restarts depends entirely on `OptionalCategoryWindowsAreUnchangedCheck`. Verify counts from `StartedAt` minus `StartedWindowSeconds` instead, which is the same window while the category copies, because nothing stops the setting changing once it has finished. diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs index a77fe39612..03a3f1fa89 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs @@ -45,7 +45,7 @@ sealed class RavenMigrationSource(RavenReadOnlySourceLifecycle lifecycle) : IMig public Task Open(CancellationToken cancellationToken = default) => lifecycle.Open(cancellationToken); - public IReadOnlyList ContributedChecks() => []; + public IReadOnlyList ContributedChecks() => [new UnfinishedOperationsCheck(lifecycle)]; public async Task Describe(CancellationToken cancellationToken = default) { diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/UnfinishedOperationsCheck.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/UnfinishedOperationsCheck.cs new file mode 100644 index 0000000000..8a6b67138e --- /dev/null +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/UnfinishedOperationsCheck.cs @@ -0,0 +1,46 @@ +#nullable enable + +namespace ServiceControl.Persistence.RavenDB.DataMigration; + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; +using ServiceControl.Persistence.DataMigration; + +// Archive and unarchive operations share an id prefix with their batches. Completed retry history is a separate document and does not count. +sealed class UnfinishedOperationsCheck(RavenReadOnlySourceLifecycle lifecycle) : IMigrationStartupCheck +{ + public string Name => "RavenDB holds no unfinished retry, archive or unarchive operations"; + + public async Task Run(CancellationToken cancellationToken = default) + { + var unfinished = new List(); + + using var session = lifecycle.OpenSession(lifecycle.Settings.DatabaseName); + + foreach (var (description, prefix) in Prefixes) + { + await using var documents = await session.Advanced.StreamAsync(prefix, pageSize: 1, token: cancellationToken); + + if (await documents.MoveNextAsync()) + { + unfinished.Add($"{description} (for example '{documents.Current.Id}')"); + } + } + + if (unfinished.Count > 0) + { + throw new InvalidOperationException( + $"The RavenDB database '{lifecycle.Settings.DatabaseName}' holds unfinished operations: {string.Join("; ", unfinished)}. The migration does not copy them. Start ServiceControl on RavenDB again, wait until its retry, archive and unarchive operations have finished, stop it, and run --start-data-migration again. If an archive or unarchive operation does not resume, request the same group action again on RavenDB. Stopping ServiceControl does not finish them."); + } + } + + static readonly (string Description, string Prefix)[] Prefixes = + [ + ("retry batches and the forwarding marker", "RetryBatches/"), + ("archive operations and their batches", "ArchiveOperations/"), + ("unarchive operations and their batches", "UnarchiveOperations/") + ]; +} diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnfinishedOperationsCheckTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnfinishedOperationsCheckTests.cs new file mode 100644 index 0000000000..261fe40d9d --- /dev/null +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnfinishedOperationsCheckTests.cs @@ -0,0 +1,150 @@ +namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; + +using System; +using System.Linq; +using System.Threading.Tasks; +using NUnit.Framework; +using ServiceControl.Persistence.RavenDB; +using ServiceControl.Persistence.RavenDB.Recoverability; +using ServiceControl.Recoverability; + +class UnfinishedOperationsCheckTests : RavenMigrationSourceTestBase +{ + [Test] + public async Task The_source_contributes_the_check() + { + await using var source = await OpenMigrationSource(); + + Assert.That(source.ContributedChecks().Select(check => check.Name), Has.Member("RavenDB holds no unfinished retry, archive or unarchive operations")); + } + + [Test] + public async Task Passes_when_no_operation_is_persisted() + { + await using var source = await OpenMigrationSource(); + + Assert.DoesNotThrowAsync(() => RunChecks(source)); + } + + [Test] + public async Task Passes_when_only_completed_retry_history_is_persisted() + { + await RetryHistoryStore.RecordRetryOperationCompleted("group-1", RetryType.FailureGroup, DateTime.UtcNow, DateTime.UtcNow, "Sales.Orders", "Exception Type", false, 1, DateTime.UtcNow, 10); + await CompleteDatabaseOperation(); + + await using var source = await OpenMigrationSource(); + + Assert.DoesNotThrowAsync(() => RunChecks(source)); + } + + [Test] + public async Task Passes_when_only_a_retry_claim_is_persisted() + { + var messageId = Guid.NewGuid().ToString(); + var claimId = RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(messageId); + using (var session = DocumentStore.OpenAsyncSession(new Raven.Client.Documents.Session.SessionOptions { Database = ((RavenPersisterSettings)PersistenceSettings).DatabaseName })) + { + await session.StoreAsync(new FailedMessageRetry + { + Id = claimId, + FailedMessageId = messageId, + RetryBatchId = RetryDocumentDataStore.MakeDocumentId("completed-batch") + }, claimId); + await session.SaveChangesAsync(); + } + + await using var source = await OpenMigrationSource(); + + Assert.DoesNotThrowAsync(() => RunChecks(source)); + } + + [TestCase("retry batch")] + [TestCase("forwarding marker")] + [TestCase("archive operation")] + [TestCase("archive batch")] + [TestCase("unarchive operation")] + [TestCase("unarchive batch")] + public async Task Refuses_when_an_unfinished_operation_is_persisted_and_says_how_to_finish_it(string unfinished) + { + var documentId = await Seed(unfinished); + await using var source = await OpenMigrationSource(); + + var refusal = Assert.ThrowsAsync(() => RunChecks(source)); + + using (Assert.EnterMultipleScope()) + { + Assert.That(refusal.Message, Does.Contain(documentId), "the operator is told which document is unfinished"); + Assert.That(refusal.Message, Does.Contain("Start ServiceControl on RavenDB again")); + Assert.That(refusal.Message, Does.Contain("--start-data-migration again")); + Assert.That(refusal.Message, Does.Contain("request the same group action again on RavenDB")); + } + } + + [Test] + public async Task Names_every_kind_of_unfinished_operation() + { + await Seed("retry batch"); + await Seed("archive operation"); + await Seed("unarchive operation"); + await using var source = await OpenMigrationSource(); + + var refusal = Assert.ThrowsAsync(() => RunChecks(source)); + + using (Assert.EnterMultipleScope()) + { + Assert.That(refusal.Message, Does.Contain("retry batches")); + Assert.That(refusal.Message, Does.Contain("archive operations")); + Assert.That(refusal.Message, Does.Contain("unarchive operations")); + } + } + + static async Task RunChecks(Persistence.DataMigration.IMigrationSource source) + { + foreach (var check in source.ContributedChecks()) + { + await check.Run(); + } + } + + async Task Seed(string unfinished) + { + using var session = DocumentStore.OpenAsyncSession(new Raven.Client.Documents.Session.SessionOptions { Database = ((RavenPersisterSettings)PersistenceSettings).DatabaseName }); + + string id; + + switch (unfinished) + { + case "retry batch": + id = RetryDocumentDataStore.MakeDocumentId(Guid.NewGuid().ToString()); + await session.StoreAsync(new RetryBatch { Id = id, Status = RetryBatchStatus.Forwarding }, id); + break; + case "forwarding marker": + id = RetryDocumentDataStore.NowForwardingDocumentId; + await session.StoreAsync(new RetryBatchNowForwarding { RetryBatchId = RetryDocumentDataStore.MakeDocumentId("batch-1") }, id); + break; + case "archive operation": + id = ArchiveOperation.MakeId("group-1", ArchiveType.FailureGroup); + await session.StoreAsync(new ArchiveOperation { Id = id, RequestId = "group-1", ArchiveType = ArchiveType.FailureGroup }, id); + break; + case "archive batch": + id = ArchiveBatch.MakeId("group-1", ArchiveType.FailureGroup, 1); + await session.StoreAsync(new ArchiveBatch { Id = id }, id); + break; + case "unarchive operation": + id = UnarchiveOperation.MakeId("group-1", ArchiveType.FailureGroup); + await session.StoreAsync(new UnarchiveOperation { Id = id, RequestId = "group-1", ArchiveType = ArchiveType.FailureGroup }, id); + break; + case "unarchive batch": + id = UnarchiveBatch.MakeId("group-1", ArchiveType.FailureGroup, 1); + await session.StoreAsync(new UnarchiveBatch { Id = id }, id); + break; + default: + throw new ArgumentOutOfRangeException(nameof(unfinished), unfinished, null); + } + + await session.SaveChangesAsync(); + await CompleteDatabaseOperation(); + + return id; + } +} From 27264058cb4fcb1eb67af9b58380497aa6b8f011 Mon Sep 17 00:00:00 2001 From: Rhys Bevilaqua Date: Thu, 19 Nov 2026 15:44:20 +0800 Subject: [PATCH 2/8] Split error migration into two stages to background migrate the build of the messages --- .../ravendb-to-sql-migration-instructions.md | 8 +- .../ravendb-to-sql-migration-overview.md | 36 ++- .../ravendb-to-sql-migration-system-design.md | 47 +++- .../FailedMessageBodyMigrationTests.cs | 14 +- .../GroupCommentsMigrationTests.cs | 1 + .../When_a_batch_exceeds_one_statement.cs | 4 +- .../When_the_copy_merges_or_changes_rows.cs | 3 +- ...unresolved_failed_messages_are_migrated.cs | 76 ++++--- .../DataMigration/EFCoreMigrationTarget.cs | 1 + .../Writers/FailedMessagesWriter.cs | 40 ++-- .../Writers/GroupCommentsWriter.cs | 4 +- .../BodyStorage/S3BodyStoragePersistence.cs | 15 +- .../DataMigration/FailedMessageStream.cs | 63 +++++- .../DataMigration/RavenMigrationSource.cs | 3 +- .../Readers/FailedMessageStatusReader.cs | 192 ++++++++++++++++ .../Readers/FailedMessagesReader.cs | 179 ++------------- .../Readers/UnresolvedFailedMessagesReader.cs | 19 ++ .../FailedMessageBodySourceTests.cs | 12 + .../RavenMigrationSourceTests.cs | 4 +- .../RetryIssuedFailedMessageSourceTests.cs | 192 ++++++++++++++++ .../UnresolvedFailedMessageSourceTests.cs | 61 +++-- .../FailedMessagesMigrationTargetTests.cs | 209 +++++++++++++++++- .../GroupCommentsMigrationTargetTests.cs | 2 +- .../Migration/MigrationTargetCoverageTests.cs | 2 + .../S3BodyStorageConditionalWriteTests.cs | 201 +++++++++++++++++ .../DataMigration/MigrationCategoryIds.cs | 8 + .../MigrationCategoryRegistry.cs | 17 +- .../DataMigration/MigrationEngineOptions.cs | 7 +- ...ategory_runs_in_a_fixed_order.approved.txt | 5 +- ...ional_categories_are_selected.approved.txt | 4 +- .../MigrationBeforeTheHostOpensTests.cs | 11 + .../MigrationCategoryRegistryTests.cs | 32 ++- .../Migration/MigrationEngineOptionsTests.cs | 15 +- 33 files changed, 1167 insertions(+), 320 deletions(-) create mode 100644 src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs create mode 100644 src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/UnresolvedFailedMessagesReader.cs create mode 100644 src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs create mode 100644 src/ServiceControl.Persistence.Tests/EFCore/S3BodyStorageConditionalWriteTests.cs diff --git a/docs/migration/ravendb-to-sql-migration-instructions.md b/docs/migration/ravendb-to-sql-migration-instructions.md index 3fcab78b59..1b162a864b 100644 --- a/docs/migration/ravendb-to-sql-migration-instructions.md +++ b/docs/migration/ravendb-to-sql-migration-instructions.md @@ -1,7 +1,7 @@ # Migration Engine: Instructions > [!NOTE] -> Production copying remains blocked until the complete migration ships. `--migration-source-report` works; `--start-data-migration` reports that copying is unavailable. +> Production copying remains blocked until the complete migration ships. `--migration-source-report` works; `--start-data-migration` reports that copying is unavailable. The release blockers for the failed-message split are listed under [Unresolved release blockers](ravendb-to-sql-migration-overview.md#unresolved-release-blockers) in the overview. This page tells you how to move the data of a ServiceControl error instance from RavenDB to SQL Server or PostgreSQL. The ServiceControl executable provides the offline command and the online copier, so you do not need a separate tool. [The migration overview](ravendb-to-sql-migration-overview.md) explains how the migration works and why. @@ -9,7 +9,7 @@ The migration does not move the audit instance or the monitoring instance. The a ## How the migration runs -Run `--start-data-migration` with ServiceControl stopped to copy the data it needs to open. **This part is your outage**. After the command succeeds, start ServiceControl to copy the event log and the archived and resolved failed messages in the background. **The migration never writes to RavenDB**. +Run `--start-data-migration` with ServiceControl stopped to copy the data it needs to open. **This part is your outage**. After the command succeeds, start ServiceControl to copy the unresolved failed messages, the event log and the archived and resolved failed messages in the background. **The migration never writes to RavenDB**. > [!IMPORTANT] > Until ServiceControl opens, you can go back to RavenDB and lose nothing. After it opens, you cannot go back. @@ -70,7 +70,7 @@ Make sure that your setup is supported: Get the data ready: -- Resolve or archive as many unresolved failed messages as you can. They never age out, and ServiceControl stays closed until they are all copied. **A large backlog makes the outage long**. The copy reads archived and resolved messages only after ServiceControl opens. The exception is a RavenDB index that is broken or leaves out some failed messages: then the copy reads every failed message during the outage, logs a warning, and a large archive makes the outage longer too. +- Let retries finish where you can. ServiceControl stays closed until every retry-issued message and every message that holds a retry claim is copied, and a retry-issued message never ages out. Unresolved messages without a claim copy after ServiceControl opens, so a large unresolved backlog does not lengthen the outage, but it does lengthen the background copy. The copy reads archived and resolved messages only after ServiceControl opens. The exception is a RavenDB index that is broken or leaves out some failed messages: then the copy reads every failed message during the outage, logs a warning, and a large archive makes the outage longer too. - Run `--import-failed-errors` while the instance is still on RavenDB. A failed error import is a message that ServiceControl took off the error queue but failed to store. The migration copies these either way, but imported ones arrive as ordinary failed messages. - Let retry, archive and unarchive operations finish on RavenDB before stopping ServiceControl. Migration refuses persisted batches, the retry forwarding marker, and archive or unarchive operation documents. Stopping ServiceControl leaves unfinished work persisted. @@ -147,6 +147,8 @@ If you want less optional data, set a shorter window for the optional categories By default, the migration copies all the optional data that RavenDB still holds (current retension period setting). A window longer than the retention period copies nothing extra. After a category starts copying, you cannot change its window. To stop it early, abandon it. +The unresolved failed messages have no window and no setting turns them off. They copy once ServiceControl opens, however old they are. + ## Step 4: Run the dry run The dry run reads RavenDB and tells you what the migration will do, including an estimate of how long ServiceControl will be closed. It does not write anything. Run it with the `--migration-dry-run` argument, in the same way as the source report. [The overview](ravendb-to-sql-migration-overview.md#the-dry-run) lists what it reports. diff --git a/docs/migration/ravendb-to-sql-migration-overview.md b/docs/migration/ravendb-to-sql-migration-overview.md index 80822ca732..d4417fe863 100644 --- a/docs/migration/ravendb-to-sql-migration-overview.md +++ b/docs/migration/ravendb-to-sql-migration-overview.md @@ -1,7 +1,7 @@ # Migration Engine: Design Overview > [!NOTE] -> This build does not carry the whole migration yet. This page describes it as it will be when it ships. +> This build does not carry the whole migration yet. This page describes it as it will be when it ships. The failed-message split described below has unresolved release blockers, listed under [Unresolved release blockers](#unresolved-release-blockers). Each one must be resolved or explicitly accepted before the migration ships. [The instructions](ravendb-to-sql-migration-instructions.md) describe how an operator runs the migration, and the [system design](ravendb-to-sql-migration-system-design.md) provides the system architecture. See the [glossary](#glossary) for terms used throughout. @@ -18,10 +18,10 @@ - **Retention does most of the work.** Error retention is 5 to 45 days and event retention defaults to 14 days, so most of RavenDB ages out on its own within weeks. - **What the instance needs to open is copied while it is closed.** This is the *required* data. Most of it is small. -- **Everything else is copied in the background while it runs.** This is the *optional* data: the event log and archived and resolved messages. +- **Everything else is copied in the background while it runs.** This is the *optional* data: the unresolved failed messages, the event log and archived and resolved messages. - **Anything not copied ages out of RavenDB**, and the customer deletes the old database when ready. -- **The main thing that makes the outage long is a big backlog of unresolved failures.** They are required, rarely age out (see the [6.18 exception](#rows-removed-after-the-copy)), and can be months old. Keep them low by resolving and archiving. The *dry run* tells you where you stand. -- **A big archive does not make the outage longer, as long as RavenDB has indexed every failed message.** The copy finds unresolved failures through a RavenDB index, and waits for it to catch up first. If the index is broken or incomplete, the copy reads every failed message instead and logs a warning. Then a big archive does make the outage longer. +- **The failed messages copied during the outage are every retry-issued message, whether or not its retry claim remains, and every other failed message that still holds a retry claim.** This does not cover every delayed retry confirmation, see the [unresolved release blockers](#unresolved-release-blockers). Unresolved messages that hold no claim copy in the background, however old they are, so a big unresolved backlog does not lengthen the outage. +- **A big archive does not make the outage longer, as long as RavenDB has indexed every failed message.** The copy finds retry-issued messages through a RavenDB index, and waits for it to catch up first. If the index is broken or incomplete, the copy reads every failed message instead and logs a warning. Then a big archive does make the outage longer. ```mermaid flowchart LR @@ -139,11 +139,11 @@ The copy goes from RavenDB, through the ServiceControl host, to SQL, because doc ### What data moves -There are eighteen *categories*: sixteen required and two optional. +There are nineteen *categories*: sixteen required and three optional. | Category | Kind | Why | | --- | --- | --- | -| Unresolved and retry-issued failed messages, with bodies | Required | The point of the instance. Issuing a retry removes the expiry, so retry-issued messages never age out. Leave one behind and the retry confirmation finds no row to resolve, so the message stays missing even though the retry succeeded | +| Retry-issued failed messages, and failed messages that hold a retry claim, with bodies | Required | Issuing a retry removes the expiry, so retry-issued messages never age out. Leave one behind and the retry confirmation finds no row to resolve, so the message stays missing even though the retry succeeded. A message of any other status that still holds a retry claim copies with it, with its groups, body and claim. The category keeps the checkpoint id `UnresolvedAndRetryIssuedFailedMessages` from the layout that copied every unresolved message here, see [Progress and checkpoints](#progress-and-checkpoints) | | Message redirects | Required | | | Known endpoints, with the monitored flag | Required | The flag is part of the endpoint row | | Endpoint settings | Required | Copied after known endpoints | @@ -158,7 +158,8 @@ There are eighteen *categories*: sixteen required and two optional. | Integration events still waiting to be sent | Required | Each is an event a subscriber has not received yet. There are only any if the old instance fell behind or could not reach the broker. They are sent once ServiceControl opens, later than they would have been, except a `MessageFailed` event for a message archived or resolved before the move (see [Rows removed after the copy](#rows-removed-after-the-copy)). RavenDB sends them in no particular order anyway, so no ordering is lost | | Custom checks, with their last status | Required | Not every check reports again. An endpoint that is down never does, and a check with no repeat interval reports only when its endpoint starts. Leaving one out could hide a known failure until that endpoint restarts. Results of the six checks the RavenDB persister runs about its own database, matched on id and category, are left out, as harmless skips | | Failed error imports, with bodies | Required | Messages taken off the error queue but not ingested. They exist nowhere else and never expire. After the move the "Error Message Ingestion" custom check keeps flagging them and `--import-failed-errors` imports them into SQL. Importing them on RavenDB first is better still | -| Group comments | Required | Copied after unresolved failed messages, so a note like "do not retry" is there when ServiceControl opens. Blank comments are not copied. Some comments are removed after ServiceControl opens, see [Open decisions](#rows-removed-after-the-copy) | +| Group comments | Required | Copied after the required failed messages and never waiting for an optional category. Blank comments are not copied. A comment on a group whose messages have not reached SQL yet is removed shortly after ServiceControl opens, which is an [unresolved release blocker](#unresolved-release-blockers) | +| Unresolved failed messages, with bodies | Optional | Copied once ServiceControl has opened, whatever the age of the message, because an operator is waiting on each one. It has no window and no setting turns it off. A message that already holds a retry claim copied with the required category, so it counts as already present. An existing SQL row always wins, see [Writing to SQL](#writing-to-sql) | | Event log | Optional | The history behind the ServicePulse activity feed. On a busy instance it holds a row for every failure, retry and submission | | Archived and resolved failed messages, with bodies | Optional | The biggest category by far, and most of the copying time | @@ -173,11 +174,11 @@ There are eighteen *categories*: sixteen required and two optional. **Written new by the copy rather than copied:** -- Retry claims in `FailedMessageRetries`. A claim ties a message to the retry that sent it. The copy writes one for each retry-issued message it inserts that RavenDB still held a claim for, so a retry by id sends or holds back a message just as it did before the move. +- Retry claims in `FailedMessageRetries`. A claim ties a message to the retry that sent it. The copy writes one for each message it inserts that RavenDB still held a claim for, whatever the status of the message, so a retry by id sends or holds back a message just as it did before the move. ### Optional data windows -- **Each optional category copies only rows inside its *window*:** events raised within it, or messages archived or resolved within it. +- **Each windowed optional category copies only rows inside its *window*:** events raised within it, or messages archived or resolved within it. The unresolved failed messages have no window. The migration always selects that category and copies every unresolved message however old. - **The window defaults to the instance's own retention period**: `ServiceControl/EventRetentionPeriod` for the event log, `ServiceControl/ErrorRetentionPeriod` for archived and resolved messages. So by default everything RavenDB still holds is copied. - **`0` turns a category off before it starts.** A longer window than the retention period copies nothing extra, because older rows would only be deleted by SQL's clean-up. - **The window counts back from when the category first started copying**, which the checkpoint records, so a restart copies the same window rather than one that has slid forward. @@ -290,6 +291,8 @@ flowchart LR ### Writing to SQL - **A failed message is written as one row**, with its status unchanged. Its older attempts are dropped, see [Open decisions](#values-that-change-or-merge-in-sql). +- **An existing SQL row always wins.** The copy never updates a row and counts an existing row as already present, not as a skip. When ingestion created the row first, its attempt count and first failure time can omit earlier RavenDB failures, and it can hold an older payload than RavenDB's newest attempt. These outcomes are accepted. +- **Bodies are immutable and written only if absent.** The body store keeps one body per `UniqueMessageId`, and each backend refuses to replace one that exists: the file system through an exclusive move, Azure Blob and S3 through a conditional upload. The first writer wins, so a body the copy stores just before ingestion does is the body that stays. That is safe only because every attempt of one message carries the same body bytes, which ingestion already assumes. An S3-compatible store that ignores `If-None-Match` falls back to a check followed by an unconditional upload, which can replace a live body. - **A failed message keeps its failure groups and its last-modified time**, so filtered lists show the same messages as before. A failure group with no id is the exception, see [Open decisions](#values-that-change-or-merge-in-sql). - **`UniqueMessageId` keeps its value** but becomes a `uniqueidentifier`. It is the primary key, the ServicePulse URL, the retry key and the body key. - **`StatusChangedAt` is rebuilt.** For archived and resolved messages it is `@expires` minus the current `ServiceControl/ErrorRetentionPeriod` (wrong if that changed, see [Open decisions](#values-that-change-or-merge-in-sql)). For unresolved and retry-issued messages it is the newest attempt's time, because the column is `NOT NULL`. @@ -350,7 +353,7 @@ A category is *Failed* when something went wrong. What it copied stays committed - **The other categories still run**, so one fix-and-retry covers them all. A category that depends on a Failed one waits. - **A Failed required category** keeps ServiceControl closed until it is retried to Done or abandoned. That is deliberate: opening the host is the point of no return. -- **A Failed optional category** leaves the instance running with that slice missing, and stops the migration being finished. +- **A Failed optional category** leaves the instance running with that slice missing, and stops the migration being finished. For the unresolved failed messages that slice is the unresolved backlog, which then exists only in RavenDB. Some losses leave a category Done without any check noticing. They are listed under [Open decisions](#what-the-checks-do-not-catch). @@ -495,10 +498,19 @@ Each case below changes, merges or drops data, or lets a loss pass without faili The dry run counts the subscription and endpoint settings merges before anything moves. +### Unresolved release blockers + +Moving the unresolved messages that hold no retry claim online opens the cases below. The operator chose to continue with the split and defer each decision. None of them is an accepted outcome, and each one blocks the release. The migration stays blocked from copying in production until each is resolved or explicitly accepted, because `MigrationIsReleasedCheck` still refuses every copy. This split changes no retry confirmation handling and adds no runtime restriction. + +- **A retry confirmation can be consumed before its message arrives.** A re-failure can leave a message `Unresolved` after its retry claim is removed, and a confirmation for it can still be queued when ServiceControl stops. The offline cohort leaves that message for the online copy. If SQL consumes the confirmation first, the conditional update matches no row and succeeds, the claim delete matches nothing, and the confirmation is gone. The online copy then inserts RavenDB's message as unresolved, though a retry of it succeeded. Retrying the migration cannot recover the confirmation. A blank or unreadable success time makes this worse, because the confirmation then counts as newer than any failure. The plan records the interleaving, and `Known_release_blocker_a_retry_confirmation_for_a_message_the_unresolved_copy_has_not_inserted_is_consumed_and_the_message_arrives_unresolved` pins today's behavior without handling it. +- **Group comments on groups the online copy has not reached are removed.** See [Rows removed after the copy](#rows-removed-after-the-copy). The partial cleanup of comments for groups that are only partly copied is deferred as well. +- **A SQL row can be resurrected as unresolved.** Ingestion creates the row, an operator archives or resolves it in SQL, and the retention sweep deletes it before the online copy reaches the message. The copy then finds no row and inserts RavenDB's unresolved message. The insert-only rule protects only a row that still exists. +- **Online operations on partly copied data are not restricted.** Group retry, archive and unarchive act on the messages copied so far, a queued edit-and-retry command is discarded when its original message has not arrived, and a pending `MessageFailed` event is deleted when its message has not arrived. The deferred investigation must decide whether to restrict, defer or accept each, and how they behave if the unresolved category is abandoned. + ### Rows removed after the copy -- **A group comment on a group whose messages are all archived or resolved is removed soon after ServiceControl opens**, because that group has no failed messages in SQL until the background copy brings them. The same happens when the group's messages are gone from RavenDB or were skipped by the copy. The copy names each such comment, in a warning starting `Changed GroupComment/ in category GroupComments on the way into SQL: The group '' has no copied failed message`, which also quotes the whole comment. -- **A `MessageFailed` event still waiting to be sent for a message archived or resolved before the move is never sent.** ServiceControl looks the message up when it sends the event. Archived and resolved messages arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. +- **A group comment on a group that has no failed message in SQL is removed soon after ServiceControl opens**, because the retention sweep deletes a comment whose group has no row. The group has none when its messages are unresolved without a retry claim and the background copy has not reached them, when they are archived or resolved, when they are gone from RavenDB, or when the copy skipped them. The sweep first runs about a minute after ServiceControl opens and then hourly, so this is deterministic for such groups and not a rare race. The copy names each such comment, in a warning starting `Changed GroupComment/ in category GroupComments on the way into SQL: The group '' has no copied failed message`, which also quotes the whole comment. The comment loss is an [unresolved release blocker](#unresolved-release-blockers), not an accepted outcome. +- **A `MessageFailed` event still waiting to be sent for a message archived or resolved before the move is never sent.** ServiceControl looks the message up when it sends the event. Archived and resolved messages arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. The same lookup can fail for an unresolved message the background copy has not reached yet, see [Unresolved release blockers](#unresolved-release-blockers). - **Unresolved messages from version 6.18 or earlier can expire during the copy.** On those versions, a message that failed again after being archived or resolved kept its expiry. One deleted before the copy reads it is not copied and not counted. No log line names these messages yet. ### What the checks do not catch diff --git a/docs/migration/ravendb-to-sql-migration-system-design.md b/docs/migration/ravendb-to-sql-migration-system-design.md index d86725c210..4b5f08994f 100644 --- a/docs/migration/ravendb-to-sql-migration-system-design.md +++ b/docs/migration/ravendb-to-sql-migration-system-design.md @@ -66,7 +66,7 @@ The RavenDB persister registers no `IMigrationTarget`, `IMigrationTargetReadines ## What runs on every SQL instance - **Registrations.** `BasePersistence.RegisterDataStores`, reached through each provider's `AddPersistence`, registers `IMigrationCheckpointStore` as `EFMigrationCheckpointStore`, `IMigrationTargetReadiness` as `EFCoreMigrationTargetReadiness` and `IMigrationTarget` as `EFCoreMigrationTarget`, all singletons. -- **`MigrationBeforeTheHostOpens`.** `RunCommand` registers a lifecycle service that checks required checkpoints in `StartingAsync`, before any hosted service starts or Kestrel binds. It delegates to `FinishedCopyBeforeAnIngestionNodeOpens`, treating unknown ids as required. An unfinished required row refuses startup and names `--start-data-migration`. The service never copies data. With no checkpoint store registered (RavenDB), it returns immediately. +- **`MigrationBeforeTheHostOpens`.** `RunCommand` registers a lifecycle service that checks required checkpoints in `StartingAsync`, before any hosted service starts or Kestrel binds. It delegates to `FinishedCopyBeforeAnIngestionNodeOpens`, treating unknown ids as required and refusing checkpoints from an earlier failed-message layout. An unfinished required row refuses startup and names `--start-data-migration`. The service never copies data. With no checkpoint store registered (RavenDB), it returns immediately. - **`FinishedCopyBeforeAnIngestionNodeOpens`.** Registered by `ErrorIngestionOnlyCommand.BuildHost` on every ingestion-only host and by `ImportFailedErrorsCommand.BuildHost` on every SQL import. In `StartingAsync` it reads every checkpoint row, drops the rows the registry classifies as optional, and refuses unless every remaining row `IsFinished`. An id unknown to this build counts as required. The worker never copies: a single host owns the copy, because a second copier would race the offline command on the checkpoint version. - **`RecordHostOpenedOnTarget` on every writer.** The main host, ingestion workers, and SQL imports register `RecordHostOpenedOnTarget` and stamp `Migration/HostOpenedOnTarget` when checkpoint rows exist. Ingestion ends the free abort, so rollback advice must never offer a free abort after a worker has written. @@ -77,13 +77,36 @@ The ingestion gate sees categories the copy never reached because `MigrationStar ## The required copy -Unresolved and retry-issued failed messages copy while the host is closed because the copy cannot merge with live writes. `FailedMessagesWriterBase` inserts only messages the target lacks and never updates one it holds. From the moment the host opens, ingestion writes to the same rows: +Retry-issued failed messages, and every other failed message that holds a retry claim, copy while the host is closed. Unresolved messages without a claim copy after the host opens, as the optional category `UnresolvedFailedMessages`. The required category's id is `UnresolvedAndRetryIssuedFailedMessages`. `FailedMessagesWriterBase` inserts only messages the target lacks and never updates one it holds, so an existing SQL row always wins. From the moment the host opens, ingestion writes to the same rows: -- **A retry confirmation finds no row.** `ResolveRetriedMessages` updates only rows already in SQL, and `FailedMessageBatchWriter.ResolveRetried` deletes the retry claim either way. The copy then inserts the message as still failing, though its retry succeeded. -- **A message that fails again loses its history.** Ingestion upserts a row holding only the new attempt. The copy then finds the id present and drops the RavenDB row, with its earlier attempts, first failure time and retry-issued status. -- **Group actions see part of the list.** Retrying or archiving a group in ServicePulse acts only on the messages copied so far. +- **A retry confirmation finds no row.** `ResolveRetriedMessages` updates only rows already in SQL, and `FailedMessageBatchWriter.ResolveRetried` deletes the retry claim either way. A message the copy has not inserted yet would arrive as still failing, though its retry succeeded. The offline cohort is the retry-issued messages and the messages holding a claim. It does not cover every delayed confirmation, in particular a delayed confirmation for an unresolved message whose claim a re-failure removed. That case is an [unresolved release blocker](ravendb-to-sql-migration-overview.md#unresolved-release-blockers), and no confirmation handling changed. +- **A message that fails again keeps only what ingestion stored.** Ingestion upserts a row holding the new attempt. The online copy finds the id present, counts it as already present and drops the RavenDB row. SQL then holds an attempt count and first failure time that omit earlier RavenDB attempts, and possibly an older payload. These outcomes are accepted, and the copy does not merge failure history. +- **Group actions see part of the list.** Retrying or archiving a group in ServicePulse acts only on the messages copied so far. Restricting this is deferred. -Archived and resolved messages are safe to copy after the host opens. If one of them fails again, the live unresolved row should win, and an insert-only copy gives exactly that. Copying unresolved messages in the background would need a writer that merges by attempt time, and retry confirmations held until their row arrives. Both change live ingestion. +Archived and resolved messages are safe to copy after the host opens. If one of them fails again, the live unresolved row should win, and an insert-only copy gives exactly that. + +### The offline cohort + +`FailedMessagesReader` derives from `FailedMessageStatusReader`, which `UnresolvedFailedMessagesReader` also derives from. The base class streams the messages with the category's statuses in id order, through a query on `FailedMessageViewIndex` after waiting for the index, or through `RavenDocumentStream.ByPrefix` when the index cannot be trusted. Both ways use the document id as the cursor. + +The offline reader reads in two phases: + +1. The retry-issued messages come from the base class. Each batch goes through `FailedMessageStream.ResolveRetryClaims`, which sets `RetryClaimHeld` for every retry-issued row. +2. `FailedMessageStream.ClaimHolders` streams the `FailedMessageRetries/` documents in id order through `RavenDocumentStream.ByPrefix`, and for each batch of claims reads the messages they name that are not retry-issued, whatever their status. Memory stays bounded by the batch size, however many claims are outstanding. It queries the collection by `id()`, which builds no index. Each row carries `RetryClaimHeld` as true. A claim whose message is gone makes no row. + +The cursor of the second phase is `claim-holder:` followed by the id of the last claim document walked. A cursor without the prefix, which is the end of the first phase, runs the first phase after it and then starts the second phase from the beginning. A prefixed cursor skips the first phase, so the document-existence check of the first phase does not see it. + +`FailedMessagesWriterBase` inserts a retry claim for every inserted row whose `RetryClaimHeld` is true. A retry-issued row with no value fails the batch, and any other status with no value means no claim. Each writer supplies its own `Count`: the offline writer counts retry-issued rows and rows with a claim, and the online writer counts unresolved rows. + +### The online unresolved copy + +`UnresolvedFailedMessagesReader` reads the `Unresolved` status through the same base class and attaches no claim state. `MigrationEngineOptions.FromSettings` always selects the category, and the engine runs it as optional: it throttles between batches, and an exception leaves it copying with `LastError` until the next start. The reader and writer need no window, because an unresolved message is copied however old it is. + +`RavenMigrationSource.ReadBody` reads the body attachment for both failed-message categories. `GroupComments` keeps `MustFollow` on the required category and never depends on the online category, so a required category never waits for work that cannot run until the host opens. + +### Body writes + +Every `IBodyStoragePersistence` backend writes a body only if none exists for the id: the file system moves a temporary file without overwrite, Azure Blob uploads with `IfNoneMatch`, and S3 checks for the object and then uploads with `IfNoneMatch = "*"`, treating a 412 precondition failure as an existing body. A migration write racing live ingestion therefore never replaces a stored body, and the first writer wins. That relies on one `UniqueMessageId` always carrying the same bytes. An S3-compatible store that ignores the header keeps the check-then-upload race. For developer smoke tests, Debug builds honor `SERVICECONTROL_MIGRATION_ALLOW_UNRELEASED=true` in both `--start-data-migration` and normal startup. Release builds ignore this override. @@ -141,7 +164,7 @@ Each position in the sequence is deliberate: > Readers must stream by id prefix, load by id, or query a static index. `RefuseWrite` blocks writes but passes query POSTs (`/queries`, `/multi_get`, `/streams/queries`), and a dynamic query creates an `Auto/` index. Only the embedded server sets `DisableAutoIndexes`. 7. **Source outage tolerance.** A failed source open is tolerated only when every required category already `IsFinished`. The host logs it, `MigrationStartup.RecordSourceOutage` writes the exception (inner exception when present) as `LastError` on each selected optional category still copying, creating `NotStarted` rows where absent, the command skips the copy and succeeds. The operator can then start ServiceControl; its online copier handles any later source outage independently. In every other case the host refuses and writes nothing; if a required category never started, the refusal says the only exits are pointing back at RavenDB or starting over. 8. **`source.ContributedChecks()`** runs once the source is open, because such a check needs a session. `RavenMigrationSource` contributes `UnfinishedOperationsCheck`, which streams one document from each of `RetryBatches/`, `ArchiveOperations/` and `UnarchiveOperations/`. These prefixes cover retry batches and the forwarding marker, plus archive and unarchive operation documents and their batches. Any match refuses the run before checkpoint initialization or copying, names an example document from each unfinished family, and instructs the operator to restart on RavenDB and let the work finish. Completed retry history and retry claims use different prefixes and do not cause this refusal. A refusal here is never tolerated. -9. **Checkpoint initialization.** The command seeds `NotStarted` rows for required and selected optional categories before copying. `MigrationStartup.RunRequiredCategories` also ensures required rows exist, then runs each required category under its own linked `CancellationTokenSource`, awaited with `WaitAsync` on that token. +9. **Checkpoint initialization.** `RunRequiredCopy` first refuses checkpoints from an earlier failed-message layout. The command then seeds `NotStarted` rows for the selected optional categories and after them the required categories, before copying. `MigrationStartup.RunRequiredCategories` also ensures required rows exist, then runs each required category under its own linked `CancellationTokenSource`, awaited with `WaitAsync` on that token. 10. **`ClosedWindowProgress`** polls the checkpoint store every 30 seconds and logs progress. When the running category has committed nothing for 30 minutes, measured from the later of `LastProgressAt` and its run start, it cancels that category's token. `WaitAsync` returns even if the call underneath ignores cancellation, the category settles `Halted` with the stall in `LastError`, and the next category runs. The abandoned call keeps running until the host refuses and the process exits. A host shutdown leaves the row `InProgress`. 11. **`CopyOrExplainWhyItStopped`** maps `MigrationCheckpointConflictException` to a message about a second instance writing the same checkpoints. 12. **`ReportWhatTheCopyLeftBehind`** logs skips per category. It offers `--migration-retry` only for an `IsFailed` row, and reports harmless skips on a `Complete` row with the explanation `HarmlessSkipExplanations` holds for each harmless reason. A test fails unless that map covers exactly the `IsBenign` reasons. @@ -152,12 +175,14 @@ Each position in the sequence is deliberate: `OptionalCategoryCopier` is a `BackgroundService` registered by `RunCommand` only on the main instance. It waits for `ApplicationStarted`, then reads checkpoints. With no unfinished optional rows, it exits without creating a source or opening a target. -- It runs the selected, supported optional categories in registry order, `EventLog` then `ArchivedAndResolvedFailedMessages`, skipping rows that are `IsFinished` or `IsFailed`. The offline command has already seeded the rows that identify the online work. +- It runs the selected, supported optional categories in registry order, `UnresolvedFailedMessages`, `EventLog` then `ArchivedAndResolvedFailedMessages`, skipping rows that are `IsFinished` or `IsFailed`. The offline command has already seeded the rows that identify the online work. - It creates and opens its own source only when a supported category is still copying. On failure it calls `RecordSourceOutage` and exits for this start. It does not depend on in-process state from `--start-data-migration`, which ran in another process. - It reruns source-contributed checks before copying. The RavenDB unfinished-operation check should still pass because the source is read-only and new operations run on SQL. A refusal stops the online copy for that start, not ServiceControl. - An exception leaves the category `InProgress` with `LastError`, untouched until the next start, so a transient failure never forces an abandon. -Windowing: `MigrationEngineOptions.WindowFor` supplies the window and the reader applies it through `IWindowedMigrationCategoryReader`. The engine writes `StartedWindowSeconds` once, on the first `InProgress` upsert. `CopiesFrom` is `StartedAt` minus the currently configured window, so window stability across restarts depends entirely on `OptionalCategoryWindowsAreUnchangedCheck`. Verify counts from `StartedAt` minus `StartedWindowSeconds` instead, which is the same window while the category copies, because nothing stops the setting changing once it has finished. +`UnresolvedFailedMessages` has no window. `MigrationEngineOptions.FromSettings` selects it even when both windows are zero. + +Windowing for the other two: `MigrationEngineOptions.WindowFor` supplies the window and the reader applies it through `IWindowedMigrationCategoryReader`. The engine writes `StartedWindowSeconds` once, on the first `InProgress` upsert. `CopiesFrom` is `StartedAt` minus the currently configured window, so window stability across restarts depends entirely on `OptionalCategoryWindowsAreUnchangedCheck`. Verify counts from `StartedAt` minus `StartedWindowSeconds` instead, which is the same window while the category copies, because nothing stops the setting changing once it has finished. > [!IMPORTANT] > Do not remove or reorder `OptionalCategoryWindowsAreUnchangedCheck`. The stored `StartedWindowSeconds` column does not protect the window by itself. The online startup path must validate unchanged windows too, because `--start-data-migration` no longer runs during normal startup. @@ -175,7 +200,7 @@ Windowing: `MigrationEngineOptions.WindowFor` supplies the window and the reader | Ordering | `MigrationCategory.MustFollow`, from `MigrationCategoryRegistry.All` | Data, not code. A follower whose predecessor is not `IsFinished` is upserted `Blocked` with `LastError` naming it | | Start or resume | Engine: `NotStarted`, `Blocked`, or any row with `LastError` moves to `InProgress`, cursor kept | `Halted` never resumes by itself; only `--migration-retry` resets it | | Batch size | `EFCoreMigrationTarget.BatchSizeFor` via the category's `IMigrationCategoryWriter` | Writers use `RowsPerStatementFor` from the dialect's parameter budget; `StoreKeyedInsert`-only writers use a fixed 500 | -| Read after cursor | `IMigrationCategoryReader`, usually via `RavenDocumentStream.ByPrefix`; `FailedMessagesReader` streams a query on `FailedMessageViewIndex` ordered by `id()`, after waiting for the index to be up to date, and falls back to `ByPrefix` when the index is errored, disabled or holds fewer failed messages with a copied status than the collection does; the two licensing endpoint readers use `ThroughputDocuments.Endpoints`; single-document readers load by id | No-tracking session. A cursor whose document no longer exists is refused, because RavenDB would otherwise resume after the missing id and skip rows. The cursor advances on every document seen. Both unresolved paths resume on the same document id cursor | +| Read after cursor | `IMigrationCategoryReader`, usually via `RavenDocumentStream.ByPrefix`; `FailedMessageStatusReader` streams a query on `FailedMessageViewIndex` for its statuses ordered by `id()`, after waiting for the index to be up to date, and falls back to `ByPrefix` when the index is errored, disabled or holds fewer failed messages with a copied status than the collection does, and `FailedMessagesReader` then reads the claim holders, see [the offline cohort](#the-offline-cohort); the two licensing endpoint readers use `ThroughputDocuments.Endpoints`; single-document readers load by id | No-tracking session. A cursor whose document no longer exists is refused, because RavenDB would otherwise resume after the missing id and skip rows. The cursor advances on every document seen. Both paths resume on the same document id cursor | | Fetch bodies | `MigrationBodyLoader` over `IMigrationSource.ReadBody` | Rows with a null body only, 8 concurrent, `MigrationEngine.MaxBodyReadAttempts` (3) with `MigrationEngineOptions.BodyRetryBackoff` (200 ms). `IsDefect` exceptions such as `NotSupportedException` are not retried and reach the batch catch. A missing attachment is skipped as `BodyMissing` after one attempt, a permanent fault no retry fixes, since ingestion writes an attachment for every failed message. Body skips stay off the checkpoint until the write commits | | Map documents to rows | `IMigrationCategoryWriter.Prepare`, returning `PreparedBatch` | Only the writer knows `NOT NULL` columns, key caps and what the product deletes anyway. Rows are inserted later inside the target transaction. Body-carrying writers write external bodies here, before the transaction, only for messages the target does not hold. A crash leaves orphan bodies, see [open decisions](ravendb-to-sql-migration-overview.md#what-the-checks-do-not-catch) | | Insert and checkpoint | `EFCoreMigrationTarget.Write`: `AccountForEveryRow`, then in one transaction under the execution strategy `PreparedBatch.Insert`, `AlreadyPresentIn`, `MigrationCheckpoint.Extend`, `MigrationCheckpointExtensions.UpsertCheckpoint` | Most writers use the dialect's `InsertMissing` built on `MigrationInsert`. Store-keyed rows use `StoreKeyedInsert`, because `MigrationInsert` rejects store-generated keys. Throughput days use `AddLicensingEndpointThroughput`. Each attempt clears the change tracker. Skips, merges and changes are logged after commit. `MigrationWriteResult.Merged` and `Changed` carry the batch's counts of each, which the engine adds up per category for the current run in `MigrationEngine.MergedAndChangedThisRun`; the checkpoint stores neither. See [progress and checkpoints](ravendb-to-sql-migration-overview.md#progress-and-checkpoints) | @@ -183,7 +208,7 @@ Windowing: `MigrationEngineOptions.WindowFor` supplies the window and the reader | Early stop | `HaltThreshold.Exceeded` on per-run counters | Fault skips (reasons not `IsBenign`, plus body skips) must exceed both `Migration/HaltThresholdPercent` and `Migration/HaltThresholdMinimum`. Counters are not persisted, so the threshold is per run | | Settle | `MigrationEngine.Settle`, after `IMigrationTarget.Count` at the end of the source | Saves the category's SQL row count on the row it settles, as `TargetCountAtSettle`. A halt saves without it, because the target may be what failed, and `--migration-abandon` reads it later. Logs before saving, because the checkpoint store shares the target database and a failed save would hide the cause | -Failed message body placement lives in `FailedMessagesWriter`, shared by `ArchivedAndResolvedFailedMessagesWriter`. `MessageBodyClassifier.Classify` decides placement and `FailedMessageDocumentTranslator.SetBody` fills the columns: text up to `MaxBodySizeToStore` (100 KB default) inline; longer text inline prefix plus external copy; binary, invalid UTF-8 or NUL-containing text external only; empty bodies not stored. External writes go through `IBodyStoragePersistence.WriteBody`, which owns compression and the filesystem, Azure Blob or S3 backend, and complete before the transaction opens. `FailedErrorImportsWriter` places a failed error import's body by the SQL import store's own rule, inline up to `MaxBodySizeToStore` and otherwise external only, under `FailedErrorImportEntity.ExternalBodyId`, also before the transaction opens. +Failed message body placement lives in `FailedMessagesWriterBase`, shared by `FailedMessagesWriter` and `UnresolvedFailedMessagesWriter`, and by the archived and resolved writer when it exists. `MessageBodyClassifier.Classify` decides placement and `FailedMessageDocumentTranslator.SetBody` fills the columns: text up to `MaxBodySizeToStore` (100 KB default) inline; longer text inline prefix plus external copy; binary, invalid UTF-8 or NUL-containing text external only; empty bodies not stored. External writes go through `IBodyStoragePersistence.WriteBody`, which owns compression and the filesystem, Azure Blob or S3 backend, and complete before the transaction opens. `FailedErrorImportsWriter` places a failed error import's body by the SQL import store's own rule, inline up to `MaxBodySizeToStore` and otherwise external only, under `FailedErrorImportEntity.ExternalBodyId`, also before the transaction opens. Invariants enforced across class boundaries: diff --git a/src/ServiceControl.Migration.AcceptanceTests/FailedMessageBodyMigrationTests.cs b/src/ServiceControl.Migration.AcceptanceTests/FailedMessageBodyMigrationTests.cs index b0ed4e2e08..ed509297d7 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/FailedMessageBodyMigrationTests.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/FailedMessageBodyMigrationTests.cs @@ -21,7 +21,7 @@ public async Task A_small_text_body_stays_inline_and_is_never_written_externally var content = Encoding.UTF8.GetBytes("1"); await SeedSourceWithBody(failure.ToFailedMessage(), content, "text/xml"); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var row = await GetFailedMessage(failure.UniqueMessageId); var fetched = await BodyStorage.TryFetch(failure.UniqueMessageId.ToString()); @@ -47,7 +47,7 @@ public async Task A_large_text_body_leaves_a_search_prefix_inline_and_writes_the var content = Encoding.UTF8.GetBytes(text); await SeedSourceWithBody(failure.ToFailedMessage(), content, "application/json"); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var row = await GetFailedMessage(failure.UniqueMessageId); @@ -81,7 +81,7 @@ public async Task A_binary_body_goes_external_with_nothing_left_inline() var content = new byte[] { 0x08, 0x00, 0x96, 0x01, 0xFF }; await SeedSourceWithBody(failure.ToFailedMessage(), content, "application/x-protobuf"); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var row = await GetFailedMessage(failure.UniqueMessageId); var written = RecordedBodies.Written.Single(); @@ -112,9 +112,9 @@ public async Task A_message_with_no_body_attachment_is_left_out_entirely() await SeedSourceWithBody(readable.ToFailedMessage(), Encoding.UTF8.GetBytes("1"), "text/xml"); await SeedSource(($"FailedMessages/{missing.UniqueMessageId}", missing.ToFailedMessage())); - var result = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var result = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); - var checkpoint = await ReadCheckpoint(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var checkpoint = await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages); using (Assert.EnterMultipleScope()) { @@ -132,7 +132,7 @@ public async Task A_zero_length_body_is_copied_rather_than_treated_as_missing() var failure = new IngestedFailure(); await SeedSourceWithBody(failure.ToFailedMessage(), [], "text/plain"); - var result = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var result = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var row = await GetFailedMessage(failure.UniqueMessageId); @@ -158,7 +158,7 @@ await SeedSourceWithBodies( (inline.ToFailedMessage(), inlineContent, "text/xml"), (external.ToFailedMessage(), externalContent, "application/x-protobuf")); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); Assert.That(await Fetch(inline.UniqueMessageId), Is.EqualTo(inlineContent)); Assert.That(await Fetch(external.UniqueMessageId), Is.EqualTo(externalContent), "the body was stored under an id TryFetch does not resolve"); diff --git a/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs b/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs index 0d5d7751b2..64ae3590af 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs @@ -30,6 +30,7 @@ await SeedSourceGroupComments( (spaces.Groups.Single().Id, " "), ("group-with-no-copied-message", "Raised with the shipping team")); await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var result = await CopyCategory(MigrationCategoryIds.GroupComments); var comments = (await GroupsStore.GetUnresolvedGroupsByClassifier(kept.Groups.Single().Type, null, new PagingInfo())).ToDictionary(view => view.Id, view => view.Comment); diff --git a/src/ServiceControl.Migration.AcceptanceTests/When_a_batch_exceeds_one_statement.cs b/src/ServiceControl.Migration.AcceptanceTests/When_a_batch_exceeds_one_statement.cs index c3d218938d..d185a03f5d 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/When_a_batch_exceeds_one_statement.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/When_a_batch_exceeds_one_statement.cs @@ -14,13 +14,13 @@ class When_a_batch_exceeds_one_statement : MigrationAcceptanceTest [Test] public async Task Copies_more_messages_than_fit_in_one_statement() { - var category = MigrationCategoryRegistry.All.Single(entry => entry.Id == MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var category = MigrationCategoryRegistry.All.Single(entry => entry.Id == MigrationCategoryIds.UnresolvedFailedMessages); var batchSize = await Target.BatchSizeFor(category); var failures = Enumerable.Range(0, (batchSize * 2) + 1).Select(_ => new IngestedFailure()).ToArray(); await SeedSourceWithBodies([.. failures.Select(failure => (failure.ToFailedMessage(), failure.Body, failure.ContentType))]); - var result = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var result = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); Assert.That(result.Copied, Is.EqualTo(failures.Length)); diff --git a/src/ServiceControl.Migration.AcceptanceTests/When_the_copy_merges_or_changes_rows.cs b/src/ServiceControl.Migration.AcceptanceTests/When_the_copy_merges_or_changes_rows.cs index a553e8cc93..1caa4e7e05 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/When_the_copy_merges_or_changes_rows.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/When_the_copy_merges_or_changes_rows.cs @@ -8,6 +8,7 @@ namespace ServiceControl.Migration.AcceptanceTests; using Microsoft.Extensions.Logging.Abstractions; using NUnit.Framework; using ServiceControl.Hosting.Commands; +using ServiceControl.MessageFailures; using ServiceControl.Persistence.DataMigration; using ServiceControl.Persistence.Tests; @@ -19,7 +20,7 @@ class When_the_copy_merges_or_changes_rows : MigrationAcceptanceTest public async Task The_category_summary_counts_changed_and_merged_rows_apart_from_the_rest() { var retried = new IngestedFailure { MessageType = new string('m', 600) }; - await SeedSourceWithBody(retried.ToFailedMessage(numberOfAttempts: 2), retried.Body, retried.ContentType); + await SeedSourceWithBody(retried.ToFailedMessage(FailedMessageStatus.RetryIssued, numberOfAttempts: 2), retried.Body, retried.ContentType); await SeedSourceSubscriptions( new SubscriptionRow("MyMessages.OrderPlaced", new Version(1, 0, 0), "Billing@BILLING01", "Billing"), new SubscriptionRow("MyMessages.OrderPlaced", new Version(2, 0, 0), "Billing@BILLING01", "Billing")); diff --git a/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs b/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs index 25c51dc849..406fee6b5e 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs @@ -28,7 +28,7 @@ public async Task Fills_every_column_the_newest_attempt_supplies() var failure = new IngestedFailure { FailingEndpointAddress = "Sales@MACHINE" }; await SeedFailure(failure); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var row = await GetFailedMessage(failure.UniqueMessageId); @@ -58,7 +58,7 @@ public async Task Collapses_attempt_history_to_the_newest_attempt() var failure = new IngestedFailure(); await SeedFailure(failure, numberOfAttempts: 5); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var row = await GetFailedMessage(failure.UniqueMessageId); @@ -77,7 +77,7 @@ public async Task A_resumed_copy_leaves_the_rows_the_first_run_wrote() var failure = new IngestedFailure(); await SeedFailure(failure); - var first = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var first = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); await QueryTarget(async db => { @@ -87,9 +87,9 @@ await QueryTarget(async db => return existing; }); - await DeleteCheckpoint(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await DeleteCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages); - var second = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var second = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var row = await GetFailedMessage(failure.UniqueMessageId); @@ -105,14 +105,14 @@ await QueryTarget(async db => } [Test] - public async Task Counts_only_the_statuses_this_category_owns() + public async Task Counts_only_the_rows_each_category_owns() { var unresolved = new IngestedFailure(); var retryIssued = new IngestedFailure(); await SeedFailure(unresolved); await SeedFailure(retryIssued, FailedMessageStatus.RetryIssued); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var offline = await CopyCategory(OfflineCategoryId); // Written straight into the target, because this build has no category that copies archived messages. await QueryTarget(async db => @@ -128,12 +128,22 @@ await QueryTarget(async db => return await db.SaveChangesAsync(); }); - var category = MigrationCategoryRegistry.All.Single(entry => entry.Id == MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var offlineCategory = MigrationCategoryRegistry.All.Single(entry => entry.Id == OfflineCategoryId); using (Assert.EnterMultipleScope()) { - Assert.That(await Target.Count(category), Is.EqualTo(2), "the category's count takes in the archive's rows unless it honours the category's statuses"); - Assert.That(await QueryTarget(db => db.FailedMessages.CountAsync()), Is.EqualTo(3), "the filter is doing the work, not an empty table"); + Assert.That(offline.Copied, Is.EqualTo(1), "an unresolved message without a claim waits for the copy that runs with ServiceControl open"); + Assert.That(await Target.Count(offlineCategory), Is.EqualTo(1), "the category's count takes in the archive's rows unless it honours the category's cohort"); + Assert.That(await QueryTarget(db => db.FailedMessages.CountAsync()), Is.EqualTo(2), "the filter is doing the work, not an empty table"); + } + + var online = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); + var onlineCategory = MigrationCategoryRegistry.All.Single(entry => entry.Id == MigrationCategoryIds.UnresolvedFailedMessages); + + using (Assert.EnterMultipleScope()) + { + Assert.That(online.Copied, Is.EqualTo(1)); + Assert.That(await Target.Count(onlineCategory), Is.EqualTo(1)); } } @@ -143,7 +153,7 @@ public async Task A_copied_message_keeps_its_failure_groups() var failure = new IngestedFailure(); await SeedFailure(failure); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var groups = await QueryTarget(db => db.FailedMessageGroups .AsNoTracking() @@ -169,9 +179,10 @@ public async Task A_retry_by_id_after_the_copy_stages_the_unresolved_message_and await SeedFailure(retryIssued, FailedMessageStatus.RetryIssued); await SeedSourceRetryClaim(retryIssued.UniqueMessageIdString); - var copy = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var offline = await CopyCategory(OfflineCategoryId); + var online = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); - Assert.That(copy.Copied, Is.EqualTo(2), "the test proves nothing unless both messages reached the target"); + Assert.That(offline.Copied + online.Copied, Is.EqualTo(2), "the test proves nothing unless both messages reached the target"); string[] ids = [unresolved.UniqueMessageIdString, retryIssued.UniqueMessageIdString]; var batchId = await RetryBatchStore.CreateBatch("migration-test-session", "retry-by-id", RetryType.MultipleMessages, ids, null, DateTime.UtcNow); @@ -190,7 +201,7 @@ public async Task A_retry_issued_message_resent_from_pending_retries_after_the_c await SeedFailure(retryIssued, FailedMessageStatus.RetryIssued); await SeedSourceRetryClaim(retryIssued.UniqueMessageIdString); - var copy = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var copy = await CopyCategory(OfflineCategoryId); using (Assert.EnterMultipleScope()) { @@ -217,12 +228,12 @@ public async Task A_message_already_in_the_target_gets_no_group_rows_from_the_co var failure = new IngestedFailure(); await SeedFailure(failure); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); await QueryTarget(db => db.FailedMessageGroups.Where(group => group.FailedMessageUniqueId == failure.UniqueMessageId).ExecuteDeleteAsync()); - await DeleteCheckpoint(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await DeleteCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages); - var second = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var second = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var groups = await QueryTarget(db => db.FailedMessageGroups.AsNoTracking().CountAsync(group => group.FailedMessageUniqueId == failure.UniqueMessageId)); @@ -240,6 +251,7 @@ public async Task Answers_for_a_copied_message_on_the_URL_it_had_on_RavenDB() await SeedFailure(failure); await MigrateThenRunHost(); + await WaitUntil(async () => (await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages))?.State.IsFinished() == true, "the unresolved copy finished"); var response = await HttpClient.GetAsync($"api/errors/last/{failure.UniqueMessageIdString}"); @@ -259,18 +271,20 @@ public async Task Answers_for_a_copied_message_on_the_URL_it_had_on_RavenDB() } [Test] - public async Task The_required_copy_completes_the_category_before_the_host_opens() + public async Task The_required_copy_completes_the_category_before_the_host_opens_and_leaves_the_unresolved_messages_to_the_online_copy() { - var failures = Enumerable.Range(0, 3).Select(_ => new IngestedFailure()).ToArray(); + var retryIssued = Enumerable.Range(0, 3).Select(_ => new IngestedFailure()).ToArray(); - foreach (var failure in failures) + foreach (var failure in retryIssued) { - await SeedFailure(failure); + await SeedFailure(failure, FailedMessageStatus.RetryIssued); } + await SeedFailure(new IngestedFailure()); + await MigrateThenRunHost(); - var checkpoint = await ReadCheckpoint("UnresolvedAndRetryIssuedFailedMessages"); + var checkpoint = await ReadCheckpoint(OfflineCategoryId); using (Assert.EnterMultipleScope()) { @@ -279,21 +293,25 @@ public async Task The_required_copy_completes_the_category_before_the_host_opens Assert.That(checkpoint.SkippedCount, Is.Zero); Assert.That(checkpoint.SourceTotal, Is.Null, "the engine no longer counts the source before copying"); } + + await WaitUntil(async () => (await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages))?.State.IsFinished() == true, "the unresolved copy finished"); + + Assert.That((await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages)).CopiedCount, Is.EqualTo(1)); } [Test] public async Task Records_each_skip_under_the_reason_that_caused_it() { var good = new IngestedFailure(); - await SeedFailure(good); + await SeedFailure(good, FailedMessageStatus.RetryIssued); var unparseable = new IngestedFailure(); - var unparseableDocument = unparseable.ToFailedMessage(); + var unparseableDocument = unparseable.ToFailedMessage(FailedMessageStatus.RetryIssued); unparseableDocument.UniqueMessageId = "not-a-guid"; await SeedSourceWithBody(unparseableDocument, unparseable.Body, unparseable.ContentType); var attemptless = new IngestedFailure(); - var attemptlessDocument = attemptless.ToFailedMessage(); + var attemptlessDocument = attemptless.ToFailedMessage(FailedMessageStatus.RetryIssued); attemptlessDocument.ProcessingAttempts.Clear(); await SeedSourceWithBody(attemptlessDocument, attemptless.Body, attemptless.ContentType); @@ -301,7 +319,7 @@ public async Task Records_each_skip_under_the_reason_that_caused_it() var refused = Assert.ThrowsAsync(async () => await MigrateCommand.Run(Settings, AllowingAnUnreleasedMigration(), cancellation.Token)); - var checkpoint = await ReadCheckpoint("UnresolvedAndRetryIssuedFailedMessages"); + var checkpoint = await ReadCheckpoint(OfflineCategoryId); using (Assert.EnterMultipleScope()) { @@ -338,7 +356,7 @@ await SeedSourceWithBodies( (badTimeSentDocument, badTimeSent.Body, badTimeSent.ContentType), (badHostIdDocument, badHostId.Body, badHostId.ContentType)); - var result = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var result = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); using (Assert.EnterMultipleScope()) { @@ -364,7 +382,7 @@ public async Task A_failure_stored_without_its_time_is_skipped_when_newest_and_l await DeleteStoredTimeOfFailure(newestHasNoTime, attempt: 0); await DeleteStoredTimeOfFailure(olderHasNoTime, attempt: 0); - var result = await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var result = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var copied = await GetFailedMessage(olderHasNoTime.UniqueMessageId); @@ -385,6 +403,8 @@ Task DeleteStoredTimeOfFailure(IngestedFailure failure, int attempt) => null, new PatchRequest { Script = $"delete this.ProcessingAttempts[{attempt}].FailureDetails.TimeOfFailure;" })); + const string OfflineCategoryId = MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; + // Seeded with its body, because the copy leaves out a message whose body the source does not hold. Task SeedFailure(IngestedFailure failure, FailedMessageStatus status = FailedMessageStatus.Unresolved, int numberOfAttempts = 1) => SeedSourceWithBody(failure.ToFailedMessage(status, numberOfAttempts), failure.Body, failure.ContentType); diff --git a/src/ServiceControl.Persistence.EFCore/DataMigration/EFCoreMigrationTarget.cs b/src/ServiceControl.Persistence.EFCore/DataMigration/EFCoreMigrationTarget.cs index 6d8297c073..d79c6eb124 100644 --- a/src/ServiceControl.Persistence.EFCore/DataMigration/EFCoreMigrationTarget.cs +++ b/src/ServiceControl.Persistence.EFCore/DataMigration/EFCoreMigrationTarget.cs @@ -27,6 +27,7 @@ sealed class EFCoreMigrationTarget( new KnownEndpointsWriter(migrationDialect), new EndpointSettingsWriter(migrationDialect), new FailedMessagesWriter(migrationDialect, storagePersistence, settings), + new UnresolvedFailedMessagesWriter(migrationDialect, storagePersistence, settings), new LicensingEndpointsWriter(migrationDialect), new LicensingThroughputWriter(migrationDialect), new LicensingReportMasksWriter(migrationDialect), diff --git a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs index a1f596b00c..070312530e 100644 --- a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs +++ b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs @@ -9,11 +9,11 @@ namespace ServiceControl.Persistence.EFCore.DataMigration.Writers; using ServiceControl.Persistence.DataMigration; /// -/// Writes failed messages, their failure group rows and a retry claim for each retry-issued message the source still -/// held one for, inserting only messages the target does not already hold and never updating one it does. The claims +/// Writes failed messages, their failure group rows and a retry claim for each message the source still held one for, +/// inserting only messages the target does not already hold and never updating one it does. The claims /// mirror the source, so a retry by id behaves as it did before the move. A retry-issued row with no bool RetryClaimHeld -/// value fails the batch. Each category that copies failed messages derives from this and names its category id and the -/// statuses it copies. +/// value fails the batch. Each category that copies failed messages derives from this and names its category id and +/// which target rows it counts. /// abstract class FailedMessagesWriterBase(IMigrationSqlDialect migrationDialect, IBodyStoragePersistence storagePersistence, EFPersisterSettings persisterSettings) : MigrationCategoryWriter { @@ -21,17 +21,14 @@ abstract class FailedMessagesWriterBase(IMigrationSqlDialect migrationDialect, I public static readonly Guid MigratedRetryClaimBatchId = new("5c1b7a0e-2f4d-4e8a-9b36-7d0c4f1e8a52"); /// - /// The statuses this category copies. counts only rows with one of these statuses, because - /// both failed message categories share one table. + /// The rows of the shared failed message table that counts for this category. /// - protected abstract IReadOnlyCollection Statuses { get; } + protected abstract IQueryable Copied(ServiceControlDbContext dbContext); public override int BatchSize(ServiceControlDbContext dbContext) => migrationDialect.RowsPerStatementFor(dbContext); public override Task Count(ServiceControlDbContext dbContext, CancellationToken cancellationToken = default) => - dbContext.FailedMessages - .Where(message => Statuses.Contains(message.Status)) - .LongCountAsync(cancellationToken); + Copied(dbContext).LongCountAsync(cancellationToken); protected override async Task PrepareDocuments(ServiceControlDbContext dbContext, IReadOnlyList<(MigrationRow Row, FailedMessage Document)> documents, CancellationToken cancellationToken = default) { @@ -74,7 +71,7 @@ protected override async Task PrepareDocuments(ServiceControlDbCo externalBodies.Add((row.SourceId, translation.Row, body)); } - if (translation.Row.Status == FailedMessageStatus.RetryIssued && SourceHeldRetryClaim(row)) + if (SourceHeldRetryClaim(row, translation.Row.Status)) { heldClaims.Add(translation.Row.UniqueMessageId); } @@ -129,10 +126,11 @@ .. translation.Groups ]; // Throws rather than guess, because a wrong guess either blocks a retry by id or lets one send the message twice. - static bool SourceHeldRetryClaim(MigrationRow row) => + static bool SourceHeldRetryClaim(MigrationRow row, FailedMessageStatus status) => row.Metadata.GetValueOrDefault(MigrationMetadataKeys.RetryClaimHeld) switch { bool held => held, + null when status != FailedMessageStatus.RetryIssued => false, null => throw new InvalidOperationException($"Row '{row.SourceId}' is retry-issued but arrived without a {MigrationMetadataKeys.RetryClaimHeld} value, which the source resolves for every retry-issued failed message"), var other => throw new InvalidOperationException($"Row '{row.SourceId}' carries a {other.GetType().FullName} as its {MigrationMetadataKeys.RetryClaimHeld} value, where the source sets a {typeof(bool).FullName}") }; @@ -188,12 +186,26 @@ await migrationDialect.InsertMissing(dbContext, [.. inserted } /// -/// Writes the unresolved and retry-issued failed messages. +/// Writes the retry-issued failed messages and the other messages that hold a retry claim, whatever their status. /// sealed class FailedMessagesWriter(IMigrationSqlDialect migrationDialect, IBodyStoragePersistence storagePersistence, EFPersisterSettings persisterSettings) : FailedMessagesWriterBase(migrationDialect, storagePersistence, persisterSettings) { public override string CategoryId => MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; - protected override IReadOnlyCollection Statuses => MigrationCategoryRegistry.UnresolvedAndRetryIssuedStatuses; + protected override IQueryable Copied(ServiceControlDbContext dbContext) => + dbContext.FailedMessages.Where(message => message.Status == FailedMessageStatus.RetryIssued + || dbContext.FailedMessageRetries.Any(claim => claim.UniqueMessageId == message.UniqueMessageId)); +} + +/// +/// Writes the unresolved failed messages. +/// +sealed class UnresolvedFailedMessagesWriter(IMigrationSqlDialect migrationDialect, IBodyStoragePersistence storagePersistence, EFPersisterSettings persisterSettings) + : FailedMessagesWriterBase(migrationDialect, storagePersistence, persisterSettings) +{ + public override string CategoryId => MigrationCategoryIds.UnresolvedFailedMessages; + + protected override IQueryable Copied(ServiceControlDbContext dbContext) => + dbContext.FailedMessages.Where(message => message.Status == FailedMessageStatus.Unresolved); } diff --git a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/GroupCommentsWriter.cs b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/GroupCommentsWriter.cs index 3179f62c16..39a8c4788e 100644 --- a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/GroupCommentsWriter.cs +++ b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/GroupCommentsWriter.cs @@ -59,7 +59,7 @@ protected override async Task PrepareDocuments(ServiceControlDbCo }; } - // Group comments copy after the unresolved failed messages, so the groups the target holds now are the groups it holds when ServiceControl opens. + // Group comments copy after the required failed messages. The unresolved messages without a retry claim arrive after ServiceControl opens, so a group that only they belong to is missing here. async Task> CommentsTheSweepWillDelete(ServiceControlDbContext dbContext, IReadOnlyList<(string SourceId, GroupCommentEntity Row)> inserted, CancellationToken cancellationToken) { string[] groupIds = [.. inserted.Select(comment => comment.Row.GroupId).Distinct(StringComparer.Ordinal)]; @@ -73,7 +73,7 @@ protected override async Task PrepareDocuments(ServiceControlDbCo return [.. inserted .Where(comment => !held.Contains(comment.Row.GroupId)) .Select(comment => (comment.SourceId, $"The group '{comment.Row.GroupId}' has no copied failed message, so the retention sweep removes this comment shortly after ServiceControl opens. " + - "Its messages are archived or resolved, are gone from the source, or were skipped by the copy. " + + "Its messages have not been copied yet, are archived or resolved, are gone from the source, or were skipped by the copy. " + $"The comment was: '{comment.Row.Comment}'"))]; } } diff --git a/src/ServiceControl.Persistence.EFCore/Implementation/BodyStorage/S3BodyStoragePersistence.cs b/src/ServiceControl.Persistence.EFCore/Implementation/BodyStorage/S3BodyStoragePersistence.cs index f5194dab85..1172f2cdb5 100644 --- a/src/ServiceControl.Persistence.EFCore/Implementation/BodyStorage/S3BodyStoragePersistence.cs +++ b/src/ServiceControl.Persistence.EFCore/Implementation/BodyStorage/S3BodyStoragePersistence.cs @@ -29,7 +29,7 @@ public async Task WriteBody(string bodyId, ReadOnlyMemory body, string con { var key = Key(bodyId); - // Bodies are immutable, so an existing object is already correct. + // Bodies are immutable, so an existing object is already correct. This skips the upload; the conditional put below decides a race. if (await Exists(key, cancellationToken)) { return; @@ -43,13 +43,22 @@ public async Task WriteBody(string bodyId, ReadOnlyMemory body, string con BucketName = bucketName, Key = key, InputStream = new ReadOnlyStream(payload), - ContentType = contentType + ContentType = contentType, + // A writer that stored this body after the check above wins, and a live body is never replaced. + IfNoneMatch = "*" }; request.Metadata.Add("format-version", FormatVersion); request.Metadata.Add("body-size", body.Length.ToString()); request.Metadata.Add("is-compressed", (compressed is not null).ToString()); - await client.PutObjectAsync(request, cancellationToken); + try + { + await client.PutObjectAsync(request, cancellationToken); + } + catch (AmazonS3Exception ex) when (ex.StatusCode == HttpStatusCode.PreconditionFailed) + { + // An object for this immutable body already exists. + } } public async Task ReadBody(string bodyId, CancellationToken cancellationToken = default) diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/FailedMessageStream.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/FailedMessageStream.cs index 3f32f8227f..7678e631e7 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/FailedMessageStream.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/FailedMessageStream.cs @@ -5,6 +5,7 @@ namespace ServiceControl.Persistence.RavenDB.DataMigration; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Raven.Client; @@ -20,7 +21,7 @@ class FailedMessageStream(RavenReadOnlySourceLifecycle lifecycle) { /// /// Builds the function that turns each streamed failed message into a row. The function returns null for a message whose - /// status is not in , and throws for a status that neither + /// status is not in , and throws for a status that no /// failed message category copies. /// public Func, MigrationRow?> Project(IReadOnlyCollection statuses) => @@ -28,9 +29,9 @@ class FailedMessageStream(RavenReadOnlySourceLifecycle lifecycle) { var status = result.Document.Status; - if (!MigrationCategoryRegistry.UnresolvedAndRetryIssuedStatuses.Contains(status) && !MigrationCategoryRegistry.ArchivedAndResolvedStatuses.Contains(status)) + if (!CopiedStatuses.Contains(status)) { - throw new InvalidOperationException($"The failed message document '{result.Id}' has the status value {(int)status}, which neither failed message category copies, so it would be left behind without a skip to show for it."); + throw new InvalidOperationException($"The failed message document '{result.Id}' has the status value {(int)status}, which no failed message category copies, so it would be left behind without a skip to show for it."); } if (!statuses.Contains(status)) @@ -50,6 +51,62 @@ class FailedMessageStream(RavenReadOnlySourceLifecycle lifecycle) }); }; + /// + /// Every status some failed message category copies. + /// + public static readonly FailedMessageStatus[] CopiedStatuses = + [.. MigrationCategoryRegistry.RetryIssuedStatuses, .. MigrationCategoryRegistry.UnresolvedStatuses, .. MigrationCategoryRegistry.ArchivedAndResolvedStatuses]; + + /// + /// The cursor of a batch from , which follows the id of the last retry claim document it walked. It cannot be a + /// message id, so a resume tells the retry-issued messages apart from the claim holders that come after them. + /// + public const string ClaimHolderCursorPrefix = "claim-holder:"; + + /// + /// Reads the messages that hold a retry claim and are not retry-issued, whatever their status, after the claim document id in + /// , each row carrying as true. The claim documents + /// stream in id order through , so memory stays bounded by the batch size. + /// A claim whose message is gone makes no row. Every batch's cursor is and the last claim document id it walked. + /// + public async IAsyncEnumerable ClaimHolders(string categoryId, string? resumeAfter, int batchSize, [EnumeratorCancellation] CancellationToken cancellationToken = default) + { + var claimPrefix = RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(""); + var project = Project(CopiedStatuses); + + // The claim document stands in as the row so the stream can batch it; the message it names replaces it below. + await foreach (var claims in RavenDocumentStream.ByPrefix( + lifecycle, categoryId, lifecycle.Settings.DatabaseName, claimPrefix, resumeAfter, batchSize, RavenDocumentStream.MaxDocumentsWalkedPerBatch, + result => new MigrationRow(result.Id, result.Document, new Dictionary()), cancellationToken)) + { + string[] messageIds = [.. claims.Rows.Select(claim => FailedMessageIdGenerator.MakeDocumentId(claim.SourceId[claimPrefix.Length..]))]; + var found = new Dictionary(StringComparer.OrdinalIgnoreCase); + + if (messageIds.Length > 0) + { + using var session = lifecycle.OpenSession(lifecycle.Settings.DatabaseName); + + // Queried by id on the collection, which builds no index. A load would not do, because a session that does not track cannot hand back the document's metadata. + var query = session.Advanced.AsyncDocumentQuery(collectionName: FailedMessageIdGenerator.CollectionName).WhereIn("id()", messageIds.Cast()); + await using var results = await session.Advanced.StreamAsync(query, cancellationToken); + + // The metadata is valid only while the stream is on its result, so each result becomes a row as it arrives. + while (await results.MoveNextAsync()) + { + // A retry-issued message is already in the batches before these. + if (results.Current.Document.Status != FailedMessageStatus.RetryIssued) + { + var row = project(results.Current)!; + found[results.Current.Id] = row with { Metadata = new Dictionary(row.Metadata) { [MigrationMetadataKeys.RetryClaimHeld] = true } }; + } + } + } + + // A claim can outlive its message, which then has no row. + yield return new MigrationBatch([.. messageIds.Where(found.ContainsKey).Select(id => found[id])], ClaimHolderCursorPrefix + claims.Cursor); + } + } + /// /// Returns the batch with every retry-issued row carrying , true when /// RavenDB still holds a retry claim for the message. It looks up every claim in the batch with one request, and returns a diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs index 03a3f1fa89..78e1b08b40 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs @@ -28,6 +28,7 @@ sealed class RavenMigrationSource(RavenReadOnlySourceLifecycle lifecycle) : IMig new KnownEndpointsReader(lifecycle), new EndpointSettingsReader(lifecycle), new FailedMessagesReader(lifecycle), + new UnresolvedFailedMessagesReader(lifecycle), new LicensingEndpointsReader(lifecycle), new LicensingThroughputReader(lifecycle), new LicensingReportMasksReader(lifecycle), @@ -103,7 +104,7 @@ public IAsyncEnumerable Read( public async Task ReadBody(MigrationCategory category, string sourceId, CancellationToken cancellationToken = default) { // Another category may keep its body inside the document, where a body attachment read would find nothing and skip every row as BodyMissing. - if (category.Id != MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages) + if (category.Id is not (MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages or MigrationCategoryIds.UnresolvedFailedMessages)) { throw new NotSupportedException($"The migration source does not read bodies as attachments for the '{category.Id}' category."); } diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs new file mode 100644 index 0000000000..a96980273c --- /dev/null +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs @@ -0,0 +1,192 @@ +#nullable enable + +namespace ServiceControl.Persistence.RavenDB.DataMigration.Readers; + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Raven.Client.Documents.Commands; +using Raven.Client.Documents.Indexes; +using Raven.Client.Documents.Operations; +using Raven.Client.Documents.Operations.Indexes; +using Raven.Client.Documents.Session; +using Raven.Client.Exceptions; +using ServiceControl.Infrastructure; +using ServiceControl.MessageFailures; +using ServiceControl.MessageFailures.Api; +using ServiceControl.Persistence.DataMigration; + +/// +/// Reads the failed messages with one of a category's statuses. The archived and resolved messages in the same collection can +/// outnumber the others many times, so it queries for just these statuses instead of reading them all. +/// +abstract class FailedMessageStatusReader(RavenReadOnlySourceLifecycle lifecycle, IReadOnlyCollection statuses) : MigrationCategoryReader(lifecycle) +{ + /// + /// Waits for to catch up, then streams the messages with the category's statuses in id + /// order, starting after . When the index is missing, errored or disabled, or does not hold + /// every failed message with a status some category copies, it logs a warning and reads the whole collection instead. + /// Both ways use the document id as the cursor, so a copy can switch from one to the other between runs. + /// Throws when the source holds no document with the id in , + /// or when a failed message has a status no category copies. + /// + protected async IAsyncEnumerable ReadStatuses(string? resumeAfter, int batchSize, [EnumeratorCancellation] CancellationToken cancellationToken = default) + { + var databaseName = Lifecycle.Settings.DatabaseName; + var project = new FailedMessageStream(Lifecycle).Project(statuses); + + var batches = await IndexHoldsEveryCopiedFailedMessage(databaseName, cancellationToken) + ? ThroughIndex(databaseName, resumeAfter, batchSize, project, cancellationToken) + : Documents($"{FailedMessageIdGenerator.CollectionName}/", resumeAfter, batchSize, project, cancellationToken); + + await foreach (var batch in batches) + { + yield return batch; + } + } + + async IAsyncEnumerable ThroughIndex( + string databaseName, + string? resumeAfter, + int batchSize, + Func, MigrationRow?> project, + [EnumeratorCancellation] CancellationToken cancellationToken) + { + using var session = Lifecycle.OpenSession(databaseName); + + await RavenDocumentStream.RefuseMissingCursor(session, CategoryId, databaseName, resumeAfter, cancellationToken); + + var query = Query(session).WhereIn(nameof(FailedMessage.Status), StatusValues(statuses)); + + if (resumeAfter is not null) + { + query = query.AndAlso().WhereGreaterThan(DocumentId, resumeAfter); + } + + await using var enumerator = await session.Advanced.StreamAsync(query.OrderBy(DocumentId).OfType(), cancellationToken); + + await foreach (var batch in RavenDocumentStream.Batches(enumerator, resumeAfter, batchSize, RavenDocumentStream.MaxDocumentsWalkedPerBatch, project)) + { + yield return batch; + } + } + + // A full read passes a document RavenDB could not index to the target to skip, and refuses a status no category copies, + // but a query on the index sees neither, so either kind of document sends the copy back to a full read. + async Task IndexHoldsEveryCopiedFailedMessage(string databaseName, CancellationToken cancellationToken) + { + using var session = Lifecycle.OpenSession(databaseName); + + while (true) + { + var index = await WaitForIndex(databaseName, cancellationToken); + + if (index is null || index.State is IndexState.Error or IndexState.Disabled) + { + logger.LogWarning("The RavenDB index {IndexName} in {DatabaseName} is {IndexState}, so copying {CategoryId} reads every failed message instead, archived and resolved ones included, which makes the copy slower", IndexName, databaseName, index?.State.ToString() ?? "missing", CategoryId); + return false; + } + + // Expiration keeps deleting archived and resolved messages during the copy, so the collection is counted before and + // after the index count, because a deletion in between could hide a document the index left out. A stale index count + // means something changed after the wait, so it waits and counts again. + var storedBefore = await StoredFailedMessages(databaseName, cancellationToken); + int indexed; + QueryStatistics statistics; + try + { + indexed = await Query(session).Statistics(out statistics).WhereIn(nameof(FailedMessage.Status), StatusValues(FailedMessageStream.CopiedStatuses)).CountAsync(cancellationToken); + } + catch (RavenException) + { + // RavenDB can mark the index errored after the wait saw it working, and then refuses the query. The next pass falls back to a full read. + if ((await WaitForIndex(databaseName, cancellationToken))?.State is IndexState.Error or IndexState.Disabled) + { + continue; + } + + throw; + } + + var storedAfter = await StoredFailedMessages(databaseName, cancellationToken); + + if (statistics.IsStale) + { + continue; + } + + if (indexed == storedBefore && indexed == storedAfter) + { + return true; + } + + logger.LogWarning("The RavenDB database {DatabaseName} held {StoredBefore} and then {StoredAfter} failed messages while its index {IndexName} held {Indexed} with a status a category copies, so copying {CategoryId} reads every failed message instead, archived and resolved ones included, which makes the copy slower. The counts differ when RavenDB could not index a document, which the index errors in RavenDB Studio name, when a document has a status no category copies, or when expiration deleted documents during the count", databaseName, storedBefore, storedAfter, IndexName, indexed, CategoryId); + return false; + } + } + + async Task StoredFailedMessages(string databaseName, CancellationToken cancellationToken) => + (await Lifecycle.DocumentStore.Maintenance.ForDatabase(databaseName).SendAsync(new GetCollectionStatisticsOperation(), cancellationToken)) + .Collections.GetValueOrDefault(FailedMessageIdGenerator.CollectionName); + + // A query on an index that is behind leaves out what it has not indexed yet, which here means unresolved messages nothing + // would copy. It waits rather than halts, because halting would need an operator to retry for a wait that is normally seconds. + async Task WaitForIndex(string databaseName, CancellationToken cancellationToken) + { + var waiting = Stopwatch.StartNew(); + var nextLog = TimeSpan.Zero; + + while (true) + { + var statistics = await Lifecycle.DocumentStore.Maintenance.ForDatabase(databaseName).SendAsync(new GetStatisticsOperation(), cancellationToken); + var index = statistics.Indexes.SingleOrDefault(candidate => candidate.Name == IndexName); + + // A missing, errored or disabled index never catches up, so waiting for it would keep ServiceControl closed for good. + if (index is null || !index.IsStale || index.State is IndexState.Error or IndexState.Disabled) + { + return index; + } + + if (waiting.Elapsed >= nextLog) + { + await LogWait(databaseName, waiting.Elapsed, cancellationToken); + nextLog += LogEvery; + } + + await Task.Delay(PollEvery, cancellationToken); + } + } + + // A paused index stays stale with its state still Normal, so without this the log reads exactly like an index that is merely slow. + async Task LogWait(string databaseName, TimeSpan waited, CancellationToken cancellationToken) + { + var indexing = await Lifecycle.DocumentStore.Maintenance.ForDatabase(databaseName).SendAsync(new GetIndexingStatusOperation(), cancellationToken); + var paused = indexing.Status == IndexRunningStatus.Paused + || indexing.Indexes.Any(index => index.Name == IndexName && index.Status == IndexRunningStatus.Paused); + + if (paused) + { + logger.LogWarning("Copying {CategoryId} is waiting for the RavenDB index {IndexName} in {DatabaseName}, but indexing is paused, so the copy waits until it is resumed in RavenDB Studio. Waited {Waited} so far", CategoryId, IndexName, databaseName, waited); + return; + } + + logger.LogInformation("Waiting for the RavenDB index {IndexName} in {DatabaseName} to catch up before copying {CategoryId}, waited {Waited} so far", IndexName, databaseName, CategoryId, waited); + } + + static IAsyncDocumentQuery Query(IAsyncDocumentSession session) => + session.Advanced.AsyncDocumentQuery(); + + static object[] StatusValues(IEnumerable statuses) => statuses.Select(status => (object)(int)status).ToArray(); + + static readonly string IndexName = new FailedMessageViewIndex().IndexName; + static readonly TimeSpan PollEvery = TimeSpan.FromSeconds(1); + static readonly TimeSpan LogEvery = TimeSpan.FromSeconds(30); + static readonly ILogger logger = LoggerUtil.CreateStaticLogger(); + + const string DocumentId = "id()"; +} diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs index 1d32916de9..0a668e6b5d 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs @@ -4,194 +4,41 @@ namespace ServiceControl.Persistence.RavenDB.DataMigration.Readers; using System; using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; using System.Runtime.CompilerServices; using System.Threading; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using Raven.Client.Documents.Commands; -using Raven.Client.Documents.Indexes; -using Raven.Client.Documents.Operations; -using Raven.Client.Documents.Operations.Indexes; -using Raven.Client.Documents.Session; -using Raven.Client.Exceptions; -using ServiceControl.Infrastructure; -using ServiceControl.MessageFailures; -using ServiceControl.MessageFailures.Api; using ServiceControl.Persistence.DataMigration; /// -/// Reads the failed messages that are still waiting on somebody: unresolved, or with a retry issued. This category is -/// copied while ServiceControl is closed, and the archived and resolved messages in the same collection can outnumber it -/// many times, so it queries for just these statuses instead of reading them all. +/// Reads the retry-issued messages and the messages that hold a retry claim, which are copied while ServiceControl is closed: every +/// retry-issued message, whether or not its retry claim remains, and then every other message that still holds a retry claim, +/// whatever its status. The unresolved messages without a claim are 's. /// -sealed class FailedMessagesReader(RavenReadOnlySourceLifecycle lifecycle) : MigrationCategoryReader(lifecycle) +sealed class FailedMessagesReader(RavenReadOnlySourceLifecycle lifecycle) : FailedMessageStatusReader(lifecycle, MigrationCategoryRegistry.RetryIssuedStatuses) { public override string CategoryId => MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; /// - /// Waits for to catch up, then streams the unresolved and retry issued messages in id - /// order, starting after . When the index is missing, errored or disabled, or does not hold - /// every failed message with a status some category copies, it logs a warning and reads the whole collection instead. - /// Both ways use the document id as the cursor, so a copy can switch from one to the other between runs. - /// Throws when the source holds no document with the id in , - /// or when a failed message has a status no category copies. + /// Streams the retry-issued messages through , then the claim holders, + /// whose cursors start with . A resume from such a cursor skips the first part. /// public override async IAsyncEnumerable Read(string? resumeAfter, int batchSize, [EnumeratorCancellation] CancellationToken cancellationToken = default) { - var databaseName = Lifecycle.Settings.DatabaseName; var stream = new FailedMessageStream(Lifecycle); - var project = stream.Project(MigrationCategoryRegistry.UnresolvedAndRetryIssuedStatuses); + var inClaimHolders = resumeAfter?.StartsWith(FailedMessageStream.ClaimHolderCursorPrefix, StringComparison.Ordinal) == true; - var batches = await IndexHoldsEveryCopiedFailedMessage(databaseName, cancellationToken) - ? ThroughIndex(databaseName, resumeAfter, batchSize, project, cancellationToken) - : Documents($"{FailedMessageIdGenerator.CollectionName}/", resumeAfter, batchSize, project, cancellationToken); - - await foreach (var batch in batches) + if (!inClaimHolders) { - yield return await stream.ResolveRetryClaims(batch, cancellationToken); - } - } - - async IAsyncEnumerable ThroughIndex( - string databaseName, - string? resumeAfter, - int batchSize, - Func, MigrationRow?> project, - [EnumeratorCancellation] CancellationToken cancellationToken) - { - using var session = Lifecycle.OpenSession(databaseName); - - await RavenDocumentStream.RefuseMissingCursor(session, CategoryId, databaseName, resumeAfter, cancellationToken); - - var query = Query(session).WhereIn(nameof(FailedMessage.Status), StatusValues(MigrationCategoryRegistry.UnresolvedAndRetryIssuedStatuses)); - - if (resumeAfter is not null) - { - query = query.AndAlso().WhereGreaterThan(DocumentId, resumeAfter); - } - - await using var enumerator = await session.Advanced.StreamAsync(query.OrderBy(DocumentId).OfType(), cancellationToken); - - await foreach (var batch in RavenDocumentStream.Batches(enumerator, resumeAfter, batchSize, RavenDocumentStream.MaxDocumentsWalkedPerBatch, project)) - { - yield return batch; - } - } - - // A full read passes a document RavenDB could not index to the target to skip, and refuses a status no category copies, - // but a query on the index sees neither, so either kind of document sends the copy back to a full read. - async Task IndexHoldsEveryCopiedFailedMessage(string databaseName, CancellationToken cancellationToken) - { - using var session = Lifecycle.OpenSession(databaseName); - - while (true) - { - var index = await WaitForIndex(databaseName, cancellationToken); - - if (index is null || index.State is IndexState.Error or IndexState.Disabled) - { - logger.LogWarning("The RavenDB index {IndexName} in {DatabaseName} is {IndexState}, so copying {CategoryId} reads every failed message instead, archived and resolved ones included, which keeps ServiceControl closed for longer", IndexName, databaseName, index?.State.ToString() ?? "missing", CategoryId); - return false; - } - - // Expiration keeps deleting archived and resolved messages during the copy, so the collection is counted before and - // after the index count, because a deletion in between could hide a document the index left out. A stale index count - // means something changed after the wait, so it waits and counts again. - var storedBefore = await StoredFailedMessages(databaseName, cancellationToken); - int indexed; - QueryStatistics statistics; - try + await foreach (var batch in ReadStatuses(resumeAfter, batchSize, cancellationToken)) { - indexed = await Query(session).Statistics(out statistics).WhereIn(nameof(FailedMessage.Status), StatusValues(CopiedStatuses)).CountAsync(cancellationToken); + yield return await stream.ResolveRetryClaims(batch, cancellationToken); } - catch (RavenException) - { - // RavenDB can mark the index errored after the wait saw it working, and then refuses the query. The next pass falls back to a full read. - if ((await WaitForIndex(databaseName, cancellationToken))?.State is IndexState.Error or IndexState.Disabled) - { - continue; - } - - throw; - } - - var storedAfter = await StoredFailedMessages(databaseName, cancellationToken); - - if (statistics.IsStale) - { - continue; - } - - if (indexed == storedBefore && indexed == storedAfter) - { - return true; - } - - logger.LogWarning("The RavenDB database {DatabaseName} held {StoredBefore} and then {StoredAfter} failed messages while its index {IndexName} held {Indexed} with a status a category copies, so copying {CategoryId} reads every failed message instead, archived and resolved ones included, which keeps ServiceControl closed for longer. The counts differ when RavenDB could not index a document, which the index errors in RavenDB Studio name, when a document has a status no category copies, or when expiration deleted documents during the count", databaseName, storedBefore, storedAfter, IndexName, indexed, CategoryId); - return false; - } - } - - async Task StoredFailedMessages(string databaseName, CancellationToken cancellationToken) => - (await Lifecycle.DocumentStore.Maintenance.ForDatabase(databaseName).SendAsync(new GetCollectionStatisticsOperation(), cancellationToken)) - .Collections.GetValueOrDefault(FailedMessageIdGenerator.CollectionName); - - // A query on an index that is behind leaves out what it has not indexed yet, which here means unresolved messages nothing - // would copy. It waits rather than halts, because halting would need an operator to retry for a wait that is normally seconds. - async Task WaitForIndex(string databaseName, CancellationToken cancellationToken) - { - var waiting = Stopwatch.StartNew(); - var nextLog = TimeSpan.Zero; - - while (true) - { - var statistics = await Lifecycle.DocumentStore.Maintenance.ForDatabase(databaseName).SendAsync(new GetStatisticsOperation(), cancellationToken); - var index = statistics.Indexes.SingleOrDefault(candidate => candidate.Name == IndexName); - - // A missing, errored or disabled index never catches up, so waiting for it would keep ServiceControl closed for good. - if (index is null || !index.IsStale || index.State is IndexState.Error or IndexState.Disabled) - { - return index; - } - - if (waiting.Elapsed >= nextLog) - { - await LogWait(databaseName, waiting.Elapsed, cancellationToken); - nextLog += LogEvery; - } - - await Task.Delay(PollEvery, cancellationToken); } - } - // A paused index stays stale with its state still Normal, so without this the log reads exactly like an index that is merely slow. - async Task LogWait(string databaseName, TimeSpan waited, CancellationToken cancellationToken) - { - var indexing = await Lifecycle.DocumentStore.Maintenance.ForDatabase(databaseName).SendAsync(new GetIndexingStatusOperation(), cancellationToken); - var paused = indexing.Status == IndexRunningStatus.Paused - || indexing.Indexes.Any(index => index.Name == IndexName && index.Status == IndexRunningStatus.Paused); + var claimHoldersAfter = inClaimHolders ? resumeAfter![FailedMessageStream.ClaimHolderCursorPrefix.Length..] : null; - if (paused) + await foreach (var batch in stream.ClaimHolders(CategoryId, claimHoldersAfter, batchSize, cancellationToken)) { - logger.LogWarning("Copying {CategoryId} is waiting for the RavenDB index {IndexName} in {DatabaseName}, but indexing is paused, so ServiceControl stays closed until it is resumed in RavenDB Studio. Waited {Waited} so far", CategoryId, IndexName, databaseName, waited); - return; + yield return batch; } - - logger.LogInformation("Waiting for the RavenDB index {IndexName} in {DatabaseName} to catch up before copying {CategoryId}, waited {Waited} so far", IndexName, databaseName, CategoryId, waited); } - - static IAsyncDocumentQuery Query(IAsyncDocumentSession session) => - session.Advanced.AsyncDocumentQuery(); - - static object[] StatusValues(IEnumerable statuses) => statuses.Select(status => (object)(int)status).ToArray(); - - static readonly FailedMessageStatus[] CopiedStatuses = [.. MigrationCategoryRegistry.UnresolvedAndRetryIssuedStatuses, .. MigrationCategoryRegistry.ArchivedAndResolvedStatuses]; - static readonly string IndexName = new FailedMessageViewIndex().IndexName; - static readonly TimeSpan PollEvery = TimeSpan.FromSeconds(1); - static readonly TimeSpan LogEvery = TimeSpan.FromSeconds(30); - static readonly ILogger logger = LoggerUtil.CreateStaticLogger(); - - const string DocumentId = "id()"; } diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/UnresolvedFailedMessagesReader.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/UnresolvedFailedMessagesReader.cs new file mode 100644 index 0000000000..3e43214cdb --- /dev/null +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/UnresolvedFailedMessagesReader.cs @@ -0,0 +1,19 @@ +#nullable enable + +namespace ServiceControl.Persistence.RavenDB.DataMigration.Readers; + +using System.Collections.Generic; +using System.Threading; +using ServiceControl.Persistence.DataMigration; + +/// +/// Reads every unresolved failed message, whatever its age, for the copy that runs while ServiceControl is open. A message that +/// holds a retry claim was copied with the required category and is already present, which the copy counts as such. +/// +sealed class UnresolvedFailedMessagesReader(RavenReadOnlySourceLifecycle lifecycle) : FailedMessageStatusReader(lifecycle, MigrationCategoryRegistry.UnresolvedStatuses) +{ + public override string CategoryId => MigrationCategoryIds.UnresolvedFailedMessages; + + public override IAsyncEnumerable Read(string? resumeAfter, int batchSize, CancellationToken cancellationToken = default) => + ReadStatuses(resumeAfter, batchSize, cancellationToken); +} diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/FailedMessageBodySourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/FailedMessageBodySourceTests.cs index beaee964fe..4478dd1ba9 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/FailedMessageBodySourceTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/FailedMessageBodySourceTests.cs @@ -32,6 +32,18 @@ public async Task Reads_the_attachment_and_its_own_content_type() } } + [Test] + public async Task Reads_the_attachment_for_the_unresolved_failed_messages_too() + { + var content = Encoding.UTF8.GetBytes("2"); + var documentId = await SeedWithAttachment(content, "application/json"); + + await using var source = await OpenMigrationSource(); + var body = await source.ReadBody(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedFailedMessages), documentId); + + Assert.That(body?.Content.ToArray(), Is.EqualTo(content)); + } + [Test] public async Task Reads_a_zero_length_attachment_as_an_empty_body() { diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTests.cs index 21502d2d23..7b5f4784f3 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTests.cs @@ -11,6 +11,7 @@ namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; using Raven.Client.Documents.Operations.Indexes; using ServiceControl.Contracts.CustomChecks; using ServiceControl.ExternalIntegrations; +using ServiceControl.MessageFailures; using ServiceControl.Infrastructure.RavenDB.Subscriptions; using ServiceControl.Notifications; using ServiceControl.Operations; @@ -136,7 +137,8 @@ public async Task Reading_every_category_creates_no_index() { [MigrationCategoryIds.EndpointSettings] = tests => tests.SeedEndpointSettings(2), [MigrationCategoryIds.KnownEndpoints] = tests => tests.MonitoringDataStore.CreateIfNotExists(new EndpointDetails { Name = "Sales.Orders", HostId = Guid.NewGuid(), Host = "HOST01" }), - [MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages] = tests => tests.SeedFailedMessage(new IngestedFailure().ToFailedMessage()), + [MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages] = tests => tests.SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)), + [MigrationCategoryIds.UnresolvedFailedMessages] = tests => tests.SeedFailedMessage(new IngestedFailure().ToFailedMessage()), [MigrationCategoryIds.LicensingEndpoints] = tests => tests.SeedThroughputDocument("Sales.Orders/Broker", new EndpointDocument(new EndpointIdentifier("Sales.Orders", ThroughputSource.Broker))), [MigrationCategoryIds.LicensingThroughput] = tests => tests.SeedThroughputDocument("Sales.Orders/Broker", new EndpointDocument(new EndpointIdentifier("Sales.Orders", ThroughputSource.Broker))), [MigrationCategoryIds.LicensingReportMasks] = tests => tests.SeedThroughputDocument(RavenLicensingDataStore.ReportMasksDocumentId, new ReportConfigurationDocument { MaskedStrings = ["contoso"] }), diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs new file mode 100644 index 0000000000..d7c5c276b7 --- /dev/null +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs @@ -0,0 +1,192 @@ +namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using NUnit.Framework; +using Raven.Client.Documents.Operations.Indexes; +using ServiceControl.MessageFailures; +using ServiceControl.MessageFailures.Api; +using ServiceControl.Persistence.DataMigration; +using ServiceControl.Persistence.RavenDB; + +[TestFixture] +class RetryIssuedFailedMessageSourceTests : RavenMigrationSourceTestBase +{ + static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + + [Test] + public async Task Reads_every_retry_issued_message_whether_or_not_its_claim_remains() + { + var claimed = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + var released = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + await SeedClaim(claimed); + await CompleteDatabaseOperation(); + + var rows = (await ReadAll()).ToDictionary(row => row.SourceId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(rows.Keys, Is.EquivalentTo(new[] { claimed.Id, released.Id })); + Assert.That(rows[claimed.Id].Metadata[MigrationMetadataKeys.RetryClaimHeld], Is.True); + Assert.That(rows[released.Id].Metadata[MigrationMetadataKeys.RetryClaimHeld], Is.False); + } + } + + [Test] + public async Task Reads_a_message_that_holds_a_claim_whatever_its_status_and_no_message_without_one() + { + var unresolved = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + var archived = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + var resolved = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Resolved)); + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Resolved)); + await SeedClaim(unresolved); + await SeedClaim(archived); + await SeedClaim(resolved); + await CompleteDatabaseOperation(); + + var rows = await ReadAll(); + + using (Assert.EnterMultipleScope()) + { + Assert.That(rows.Select(row => row.SourceId), Is.EquivalentTo(new[] { unresolved.Id, archived.Id, resolved.Id })); + Assert.That(rows.Select(row => row.Metadata[MigrationMetadataKeys.RetryClaimHeld]), Is.All.True, "the claim is copied for a holder of any status, so a retry by id stays blocked"); + Assert.That(rows.Select(row => ((FailedMessage)row.Document).Status), Is.EquivalentTo(new[] { FailedMessageStatus.Unresolved, FailedMessageStatus.Archived, FailedMessageStatus.Resolved })); + } + } + + [Test] + public async Task Reads_an_unresolved_message_without_a_claim_in_neither_category_phase() + { + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + await CompleteDatabaseOperation(); + + Assert.That(await ReadAll(), Is.Empty, "the unresolved copy that runs with the host open owns it"); + } + + [Test] + public async Task Reads_a_retry_issued_message_with_a_claim_once() + { + var message = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + await SeedClaim(message); + await CompleteDatabaseOperation(); + + Assert.That((await ReadAll()).Select(row => row.SourceId), Is.EqualTo(new[] { message.Id })); + } + + [Test] + public async Task Skips_a_claim_whose_message_is_gone() + { + var gone = new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived); + var present = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + await SeedClaim(present); + await SeedClaim(FailedMessageIdGenerator.MakeDocumentId(gone.UniqueMessageId), gone.UniqueMessageId); + await CompleteDatabaseOperation(); + + Assert.That((await ReadAll()).Select(row => row.SourceId), Is.EqualTo(new[] { present.Id })); + } + + [Test] + public async Task The_claim_holders_follow_the_retry_issued_messages_and_their_cursor_resumes_in_that_phase() + { + var holders = new List(); + var retryIssued = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + + for (var count = 0; count < 5; count++) + { + var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(count % 2 == 0 ? FailedMessageStatus.Archived : FailedMessageStatus.Unresolved)); + await SeedClaim(holder); + holders.Add(holder); + } + + await CompleteDatabaseOperation(); + var holderIds = holders.Select(holder => holder.Id).OrderBy(id => id, StringComparer.OrdinalIgnoreCase).ToArray(); + + await using var source = await OpenMigrationSource(); + var batches = await CollectBatches(source, Category, batchSize: 2); + + Assert.That(batches.Select(batch => batch.Cursor).Skip(1), Has.All.StartWith("claim-holder:")); + + var resumed = await CollectBatches(source, Category, resumeAfter: batches[1].Cursor, batchSize: 2); + + using (Assert.EnterMultipleScope()) + { + Assert.That(batches.SelectMany(batch => batch.Rows).Select(row => row.SourceId), Is.EqualTo(new[] { retryIssued.Id }.Concat(holderIds)), "retry-issued first, then every claim holder once, in id order"); + Assert.That(resumed.SelectMany(batch => batch.Rows).Select(row => row.SourceId), Is.EqualTo(batches.Skip(2).SelectMany(batch => batch.Rows).Select(row => row.SourceId)), "a claim-holder cursor resumes after its message and does not look for it in the retry-issued phase"); + } + } + + [Test] + public async Task A_cursor_from_the_earlier_layout_still_copies_the_claim_holders_before_it() + { + var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + await SeedClaim(holder); + var later = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + await CompleteDatabaseOperation(); + var cursorPastTheHolder = new[] { holder.Id, later.Id }.Max(StringComparer.Ordinal); + + await using var source = await OpenMigrationSource(); + var rows = (await CollectBatches(source, Category, resumeAfter: cursorPastTheHolder)).SelectMany(batch => batch.Rows).ToList(); + + Assert.That(rows.Select(row => row.SourceId), Is.EqualTo(new[] { holder.Id }), "the earlier layout never copied an archived claim holder, so the claim phase starts from the beginning whatever the plain cursor is"); + } + + [Test] + public async Task Reads_the_claim_holders_when_the_index_is_missing() + { + await DocumentStore.Maintenance.SendAsync(new DeleteIndexOperation(new FailedMessageViewIndex().IndexName), TestContext.CurrentContext.CancellationToken); + var retryIssued = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Resolved)); + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + await SeedClaim(holder); + + var rows = await ReadAll().WaitAsync(TimeSpan.FromSeconds(30), TestContext.CurrentContext.CancellationToken); + + Assert.That(rows.Select(row => row.SourceId), Is.EquivalentTo(new[] { retryIssued.Id, holder.Id })); + } + + [Test] + public async Task Counts_the_rows_the_copy_reads() + { + var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + await SeedClaim(holder); + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + await CompleteDatabaseOperation(); + + await using var source = await OpenMigrationSource(); + + Assert.That(await source.Count(Category), Is.EqualTo(2)); + } + + [Test] + public async Task Carries_the_last_modified_of_a_claim_holder() + { + var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + await SeedClaim(holder); + await CompleteDatabaseOperation(); + + var row = (await ReadAll()).Single(); + + Assert.That(row.Metadata[MigrationMetadataKeys.LastModified], Is.Not.Null, "the target needs the time the status changed, and a loaded document carries it in its metadata like a streamed one"); + } + + async Task SeedClaim(FailedMessage message) => await SeedClaim(message.Id, message.UniqueMessageId); + + async Task SeedClaim(string failedMessageId, string uniqueMessageId) + { + using var session = DocumentStore.OpenAsyncSession(); + await session.StoreAsync(new FailedMessageRetry { FailedMessageId = failedMessageId, RetryBatchId = "RetryBatches/forwarded" }, RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(uniqueMessageId)); + await session.SaveChangesAsync(); + } + + async Task> ReadAll() + { + await using var source = await OpenMigrationSource(); + + return (await CollectBatches(source, Category)).SelectMany(batch => batch.Rows).ToList(); + } +} diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnresolvedFailedMessageSourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnresolvedFailedMessageSourceTests.cs index 9faec057e1..dd55778870 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnresolvedFailedMessageSourceTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnresolvedFailedMessageSourceTests.cs @@ -20,10 +20,10 @@ namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; class UnresolvedFailedMessageSourceTests : RavenMigrationSourceTestBase { [Test] - public async Task Reads_both_unresolved_statuses_and_neither_of_the_others() + public async Task Reads_the_unresolved_status_and_none_of_the_others() { var unresolved = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); - var retryIssued = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Resolved)); await CompleteDatabaseOperation(); @@ -32,7 +32,19 @@ public async Task Reads_both_unresolved_statuses_and_neither_of_the_others() Assert.That( rows.Select(row => ((FailedMessage)row.Document).UniqueMessageId), - Is.EquivalentTo(new[] { unresolved.UniqueMessageId, retryIssued.UniqueMessageId })); + Is.EquivalentTo(new[] { unresolved.UniqueMessageId })); + } + + [Test] + public async Task Reads_an_unresolved_message_however_old_it_is() + { + var failure = new IngestedFailure(); + var message = failure.ToFailedMessage(FailedMessageStatus.Unresolved); + message.ProcessingAttempts.ForEach(attempt => attempt.AttemptedAt = DateTime.UtcNow.AddDays(-4000)); + var stored = await SeedFailedMessage(message); + await CompleteDatabaseOperation(); + + Assert.That((await ReadAll()).Select(row => row.SourceId), Is.EqualTo(new[] { stored.Id }), "the category has no window, because an operator is waiting on an unresolved message however old it is"); } [Test] @@ -101,29 +113,19 @@ public async Task Sends_only_the_migration_metadata_keys_across_the_seam() } [Test] - public async Task A_retry_issued_message_says_whether_the_source_still_holds_its_retry_claim() + public async Task An_unresolved_message_carries_no_claim_state_even_when_the_source_holds_a_claim_for_it() { - var claimed = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); - var released = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); var unresolved = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); using (var session = DocumentStore.OpenAsyncSession()) { - await session.StoreAsync(new FailedMessageRetry { FailedMessageId = claimed.Id, RetryBatchId = "RetryBatches/forwarded" }, RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(claimed.UniqueMessageId)); await session.StoreAsync(new FailedMessageRetry { FailedMessageId = unresolved.Id, RetryBatchId = "RetryBatches/staging" }, RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(unresolved.UniqueMessageId)); await session.SaveChangesAsync(); } await CompleteDatabaseOperation(); - var rows = (await ReadAll()).ToDictionary(row => ((FailedMessage)row.Document).UniqueMessageId); - - using (Assert.EnterMultipleScope()) - { - Assert.That(rows[claimed.UniqueMessageId].Metadata[MigrationMetadataKeys.RetryClaimHeld], Is.True); - Assert.That(rows[released.UniqueMessageId].Metadata[MigrationMetadataKeys.RetryClaimHeld], Is.False); - Assert.That(rows[unresolved.UniqueMessageId].Metadata, Does.Not.ContainKey(MigrationMetadataKeys.RetryClaimHeld), "only a retry-issued message gets a claim in SQL, even when the source holds one for another"); - } + Assert.That((await ReadAll()).Single().Metadata, Does.Not.ContainKey(MigrationMetadataKeys.RetryClaimHeld), "the claim holder is copied with the required category, and this copy finds it already present"); } [Test] @@ -182,9 +184,9 @@ public async Task Resuming_after_a_cursor_reads_only_the_unresolved_messages_aft using (Assert.EnterMultipleScope()) { - Assert.That(first.Rows.Select(row => row.SourceId), Is.EqualTo(new[] { ids[0], ids[2] })); - Assert.That(first.Cursor, Is.EqualTo(ids[2])); - Assert.That(resumed.SelectMany(batch => batch.Rows).Select(row => row.SourceId), Is.EqualTo(new[] { ids[3], ids[5] }), "a resume neither repeats a row nor skips one"); + Assert.That(first.Rows.Select(row => row.SourceId), Is.EqualTo(new[] { ids[0], ids[3] })); + Assert.That(first.Cursor, Is.EqualTo(ids[3])); + Assert.That(resumed.SelectMany(batch => batch.Rows).Select(row => row.SourceId), Is.EqualTo(new[] { ids[5] }), "a resume neither repeats a row nor skips one"); } } @@ -248,7 +250,7 @@ public async Task Reads_through_a_Corax_index_as_through_a_Lucene_one() await using var source = await OpenMigrationSource(); var batches = await CollectBatches(source, Category); - Assert.That(batches.Select(batch => (batch.Cursor, batch.Rows.Select(row => row.SourceId))), Is.EqualTo(new[] { (ids[2], new[] { ids[0], ids[2] }) }), "a full read would walk on to the resolved message and save its cursor past it"); + Assert.That(batches.Select(batch => (batch.Cursor, batch.Rows.Select(row => row.SourceId))), Is.EqualTo(new[] { (ids[0], new[] { ids[0] }) }), "a full read would walk on to the resolved message and save its cursor past it"); } [Test] @@ -257,7 +259,7 @@ public async Task Resumes_through_a_Corax_index_with_no_row_repeated_or_skipped( await UseCorax(); var statuses = Enumerable.Range(0, 60).Select(position => (FailedMessageStatus)((position % 4) + 1)).ToArray(); var ids = await SeedInIdOrder(statuses); - var expected = ids.Where((_, position) => statuses[position] is FailedMessageStatus.Unresolved or FailedMessageStatus.RetryIssued).ToList(); + var expected = ids.Where((_, position) => statuses[position] == FailedMessageStatus.Unresolved).ToList(); await using var source = await OpenMigrationSource(); var read = new List(); @@ -285,21 +287,12 @@ public async Task Reads_the_whole_collection_when_the_index_is_missing() { await DocumentStore.Maintenance.SendAsync(new DeleteIndexOperation(IndexName), TestContext.CurrentContext.CancellationToken); var stored = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); - var claimed = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); - - using (var session = DocumentStore.OpenAsyncSession()) - { - await session.StoreAsync(new FailedMessageRetry { FailedMessageId = claimed.Id, RetryBatchId = "RetryBatches/forwarded" }, RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(claimed.UniqueMessageId)); - await session.SaveChangesAsync(); - } + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); var rows = await ReadAll().WaitAsync(TimeSpan.FromSeconds(30), TestContext.CurrentContext.CancellationToken); - using (Assert.EnterMultipleScope()) - { - Assert.That(rows.Select(row => row.SourceId), Is.EquivalentTo(new[] { stored.Id, claimed.Id }), "the copy cannot build the index on a source it only reads, so reading everything is the only way through"); - Assert.That(rows.Single(row => row.SourceId == claimed.Id).Metadata[MigrationMetadataKeys.RetryClaimHeld], Is.True, "the whole-collection read must resolve claims too, or the writer stops on the first retry-issued row"); - } + Assert.That(rows.Select(row => row.SourceId), Is.EqualTo(new[] { stored.Id }), "the copy cannot build the index on a source it only reads, so reading everything is the only way through"); } [Test] @@ -919,7 +912,7 @@ async Task> StreamWithWalkLimit(int batchSize, int maxDocum await foreach (var batch in RavenDocumentStream.ByPrefix( lifecycle, Category.Id, settings.DatabaseName, $"{FailedMessageIdGenerator.CollectionName}/", null, batchSize, maxDocumentsWalkedPerBatch, - new FailedMessageStream(lifecycle).Project(MigrationCategoryRegistry.UnresolvedAndRetryIssuedStatuses), TestContext.CurrentContext.CancellationToken)) + new FailedMessageStream(lifecycle).Project(MigrationCategoryRegistry.UnresolvedStatuses), TestContext.CurrentContext.CancellationToken)) { batches.Add(batch); } @@ -927,7 +920,7 @@ async Task> StreamWithWalkLimit(int batchSize, int maxDocum return batches; } - static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedFailedMessages); static readonly string IndexName = new FailedMessageViewIndex().IndexName; // The parsers read headers through IReadOnlyDictionary, so re-implementing its lookup makes every header read fail. diff --git a/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs index 814f5565a3..62ce59904d 100644 --- a/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs +++ b/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs @@ -10,8 +10,11 @@ namespace ServiceControl.Persistence.Tests; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using NServiceBus.Extensibility; +using NServiceBus.Transport; using NUnit.Framework; using ServiceControl.MessageFailures; +using ServiceControl.Operations; using ServiceControl.Persistence.DataMigration; using ServiceControl.Persistence.EFCore.Abstractions; using ServiceControl.Persistence.EFCore.DataMigration; @@ -37,6 +40,8 @@ class FailedMessagesMigrationTargetTests : PersistenceTestBase static readonly MigrationBody SmallTextBody = new(Encoding.UTF8.GetBytes("1"), "text/xml"); + static readonly MigrationCategory UnresolvedCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.UnresolvedFailedMessages); + static readonly MigrationCategory FailedMessagesCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); [Test] @@ -480,9 +485,13 @@ public async Task A_changed_message_the_target_already_holds_is_not_reported_aga } [Test] - public async Task Count_takes_in_only_unresolved_and_retry_issued_messages() + public async Task Count_takes_in_retry_issued_messages_and_claim_holders_of_any_status() { - await Target.Write(FailedMessagesCategory, BatchOf(new IngestedFailure().ToFailedMessage(), new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)), Checkpoint()); + var holder = new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived); + await Target.Write(FailedMessagesCategory, BatchOf( + RowOf(new IngestedFailure().ToFailedMessage(), SmallTextBody), + RowOf(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued), SmallTextBody), + WithClaimHeld(RowOf(holder, SmallTextBody), true)), Checkpoint()); await Query(db => { db.FailedMessages.AddRange( @@ -493,12 +502,198 @@ await Query(db => using (Assert.EnterMultipleScope()) { - Assert.That(await Target.Count(FailedMessagesCategory), Is.EqualTo(2), "the archived and resolved rows belong to another category"); - Assert.That(await Query(db => db.FailedMessages.CountAsync()), Is.EqualTo(4), "the filter is doing the work, not an empty table"); + Assert.That(await Target.Count(FailedMessagesCategory), Is.EqualTo(2), "an unresolved message without a claim and the archived and resolved rows without one belong to other copies"); + Assert.That(await Query(db => db.FailedMessages.CountAsync()), Is.EqualTo(5), "the filter is doing the work, not an empty table"); + } + } + + [Test] + public async Task Count_of_the_unresolved_copy_takes_in_only_unresolved_messages() + { + await Target.Write(UnresolvedCategory, BatchOf(new IngestedFailure().ToFailedMessage(), new IngestedFailure().ToFailedMessage()), Checkpoint(MigrationCategoryIds.UnresolvedFailedMessages)); + await Query(db => + { + db.FailedMessages.AddRange( + new FailedMessageEntity { UniqueMessageId = Guid.NewGuid(), Status = FailedMessageStatus.RetryIssued, HeadersJson = "{}", FailingEndpointAddress = "error" }, + new FailedMessageEntity { UniqueMessageId = Guid.NewGuid(), Status = FailedMessageStatus.Archived, HeadersJson = "{}", FailingEndpointAddress = "error" }); + return db.SaveChangesAsync(); + }); + + Assert.That(await Target.Count(UnresolvedCategory), Is.EqualTo(2)); + } + + [Test] + public async Task A_claim_holder_of_any_status_gets_a_retry_claim_and_a_message_without_one_does_not() + { + var unresolved = new IngestedFailure(); + var archived = new IngestedFailure(); + var resolved = new IngestedFailure(); + var withoutClaim = new IngestedFailure(); + + var result = await Target.Write(FailedMessagesCategory, BatchOf( + WithClaimHeld(RowOf(unresolved.ToFailedMessage(), SmallTextBody), true), + WithClaimHeld(RowOf(archived.ToFailedMessage(FailedMessageStatus.Archived), SmallTextBody), true), + WithClaimHeld(RowOf(resolved.ToFailedMessage(FailedMessageStatus.Resolved), SmallTextBody), true), + WithClaimHeld(RowOf(withoutClaim.ToFailedMessage(FailedMessageStatus.Archived), SmallTextBody), false)), Checkpoint()); + + var rows = await Query(db => db.FailedMessages.AsNoTracking().ToDictionaryAsync(message => message.UniqueMessageId, message => message.Status)); + var claimed = await Query(db => db.FailedMessageRetries.AsNoTracking().Select(claim => claim.UniqueMessageId).ToListAsync()); + + using (Assert.EnterMultipleScope()) + { + Assert.That(result.Copied, Is.EqualTo(4)); + Assert.That(rows[archived.UniqueMessageId], Is.EqualTo(FailedMessageStatus.Archived), "the row keeps the status it had in the source"); + Assert.That(rows[resolved.UniqueMessageId], Is.EqualTo(FailedMessageStatus.Resolved)); + Assert.That(claimed, Is.EquivalentTo(new[] { unresolved.UniqueMessageId, archived.UniqueMessageId, resolved.UniqueMessageId }), "a retry by id stays blocked for every holder, whatever its status"); + } + } + + [Test] + public async Task A_claim_holder_already_in_the_target_gets_no_retry_claim_from_the_copy() + { + var present = new IngestedFailure(); + await Query(db => + { + db.FailedMessages.Add(new FailedMessageEntity { UniqueMessageId = present.UniqueMessageId, Status = FailedMessageStatus.Unresolved, HeadersJson = "{}", FailingEndpointAddress = "error" }); + return db.SaveChangesAsync(); + }); + + var result = await Target.Write(FailedMessagesCategory, BatchOf(WithClaimHeld(RowOf(present.ToFailedMessage(), SmallTextBody), true)), Checkpoint()); + + using (Assert.EnterMultipleScope()) + { + Assert.That(result.AlreadyPresent, Is.EqualTo(1)); + Assert.That(await Query(db => db.FailedMessageRetries.CountAsync()), Is.Zero); + } + } + + [Test] + public async Task The_unresolved_copy_inserts_unresolved_messages_with_their_groups_and_bodies_and_no_claim() + { + var failure = new IngestedFailure { ContentType = "application/octet-stream" }; + var body = new MigrationBody(new byte[] { 0x08, 0x00, 0x96, 0x01 }, "application/octet-stream"); + + var result = await Target.Write(UnresolvedCategory, BatchOf(RowOf(failure.ToFailedMessage(), body)), Checkpoint(MigrationCategoryIds.UnresolvedFailedMessages)); + + using (Assert.EnterMultipleScope()) + { + Assert.That(result.Copied, Is.EqualTo(1)); + Assert.That(await Query(db => db.FailedMessageGroups.CountAsync(group => group.FailedMessageUniqueId == failure.UniqueMessageId)), Is.EqualTo(failure.Groups.Count)); + Assert.That(await BodyStored(failure.UniqueMessageIdString), Is.True); + Assert.That(await Query(db => db.FailedMessageRetries.CountAsync()), Is.Zero); + } + } + + [Test] + public async Task A_row_ingestion_wrote_first_wins_over_the_unresolved_copy_with_its_attempt_count_payload_and_body() + { + var failure = new IngestedFailure { ContentType = "application/octet-stream" }; + var liveBody = new byte[] { 0x01, 0x02 }; + await Query(db => + { + db.FailedMessages.Add(new FailedMessageEntity + { + UniqueMessageId = failure.UniqueMessageId, + Status = FailedMessageStatus.Unresolved, + ExceptionMessage = "Written by ingestion", + NumberOfProcessingAttempts = 1, + HeadersJson = "{}", + FailingEndpointAddress = "error", + BodyStoredExternally = true + }); + return db.SaveChangesAsync(); + }); + await BodyStore.WriteBody(failure.UniqueMessageIdString, liveBody, "application/octet-stream"); + + var result = await Target.Write(UnresolvedCategory, BatchOf(RowOf(failure.ToFailedMessage(numberOfAttempts: 3), new MigrationBody(new byte[] { 0x09, 0x09, 0x09 }, "application/octet-stream"))), Checkpoint(MigrationCategoryIds.UnresolvedFailedMessages)); + + var row = await Query(db => db.FailedMessages.AsNoTracking().SingleAsync(message => message.UniqueMessageId == failure.UniqueMessageId)); + var stored = await BodyStore.ReadBody(failure.UniqueMessageIdString); + using var memory = new MemoryStream(); + await stored.Stream.CopyToAsync(memory); + await stored.Stream.DisposeAsync(); + + using (Assert.EnterMultipleScope()) + { + Assert.That(result.Copied, Is.Zero); + Assert.That(result.AlreadyPresent, Is.EqualTo(1), "an existing SQL row counts as already present, not as a skip"); + Assert.That(result.Skipped, Is.Zero); + Assert.That(row.ExceptionMessage, Is.EqualTo("Written by ingestion"), "SQL wins, so its payload stays even when the source holds a newer attempt"); + Assert.That(row.NumberOfProcessingAttempts, Is.EqualTo(1), "SQL wins, so its attempt count omits the earlier RavenDB attempts"); + Assert.That(memory.ToArray(), Is.EqualTo(liveBody), "the migration never replaces a body a live row already holds"); + } + } + + [Test] + public async Task A_body_stored_after_the_present_check_and_before_the_body_write_is_not_replaced() + { + // The present check and the body write are not atomic, so a body that ingestion stores between them has to survive the write. + var failure = new IngestedFailure { ContentType = "application/octet-stream" }; + var liveBody = new byte[] { 0x01, 0x02 }; + await BodyStore.WriteBody(failure.UniqueMessageIdString, liveBody, "application/octet-stream"); + + await Target.Write(UnresolvedCategory, BatchOf(RowOf(failure.ToFailedMessage(), new MigrationBody(new byte[] { 0x09, 0x09, 0x09 }, "application/octet-stream"))), Checkpoint(MigrationCategoryIds.UnresolvedFailedMessages)); + + var stored = await BodyStore.ReadBody(failure.UniqueMessageIdString); + using var memory = new MemoryStream(); + await stored.Stream.CopyToAsync(memory); + await stored.Stream.DisposeAsync(); + + Assert.That(memory.ToArray(), Is.EqualTo(liveBody)); + } + + // Known release blocker, not accepted behavior: docs/migration/failed-message-migration-split-plan.md describes the interleaving. + // The test pins today's behavior and changes no confirmation handling, so it has to be rewritten when the blocker is resolved. + [Test] + public async Task Known_release_blocker_a_retry_confirmation_for_a_message_the_unresolved_copy_has_not_inserted_is_consumed_and_the_message_arrives_unresolved() + { + var failure = new IngestedFailure(); + var confirmation = new MessageContext( + Guid.NewGuid().ToString(), + new Dictionary + { + ["ServiceControl.Retry.Successful"] = string.Empty, + ["ServiceControl.Retry.UniqueMessageId"] = failure.UniqueMessageIdString + }, + ReadOnlyMemory.Empty, + new TransportTransaction(), + "receiveAddress", + new ContextBag()); + var unitOfWork = await IngestionUnitOfWorkFactory.StartNew(); + + await new RetryConfirmationProcessor(new FakeDomainEvents()).Process([confirmation], unitOfWork); + + Assert.DoesNotThrowAsync(() => unitOfWork.Complete(TestContext.CurrentContext.CancellationToken), "a confirmation for a missing row succeeds today, which is how it is consumed"); + Assert.That(await Query(db => db.FailedMessages.CountAsync()), Is.Zero, "the confirmation records nothing for a row that is absent"); + + await Target.Write(UnresolvedCategory, BatchOf(failure.ToFailedMessage()), Checkpoint(MigrationCategoryIds.UnresolvedFailedMessages)); + + var row = await Query(db => db.FailedMessages.AsNoTracking().SingleAsync(message => message.UniqueMessageId == failure.UniqueMessageId)); + Assert.That(row.Status, Is.EqualTo(FailedMessageStatus.Unresolved), "the consumed confirmation no longer resolves the message"); + } + + [Test] + public void The_unresolved_copy_follows_the_required_copy_checkpoint_layout() + { + Assert.That(UnresolvedCategory.Kind, Is.EqualTo(MigrationCategoryKind.Optional)); + } + + [Test] + public async Task Writes_a_message_ingestion_has_not_stored_even_when_the_target_holds_a_claim_for_another() + { + var other = new IngestedFailure(); + var failure = new IngestedFailure(); + await Target.Write(FailedMessagesCategory, BatchOf(RowOf(other.ToFailedMessage(FailedMessageStatus.RetryIssued), SmallTextBody)), Checkpoint()); + + var result = await Target.Write(UnresolvedCategory, BatchOf(failure.ToFailedMessage()), Checkpoint(MigrationCategoryIds.UnresolvedFailedMessages)); + + using (Assert.EnterMultipleScope()) + { + Assert.That(result.Copied, Is.EqualTo(1)); + Assert.That(await Query(db => db.FailedMessageRetries.CountAsync()), Is.EqualTo(1), "the unresolved copy neither adds nor removes a claim"); } } - // PostgreSQL takes a fixed number of rows per statement whatever the entity, so only SQL Server can catch the wrong entity here. [Test] public async Task A_batch_is_one_statement_of_failed_message_rows() { @@ -706,6 +901,6 @@ sealed class UnwritableBodyStore : IBodyStoragePersistence FailedMessagesWriter Writer() => new(ServiceProvider.GetRequiredService(), BodyStore, ServiceProvider.GetRequiredService()); - static MigrationCheckpoint Checkpoint() => - new(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages, MigrationCategoryState.InProgress, null, 0, 0, null, null, null, null, null, null); + static MigrationCheckpoint Checkpoint(string categoryId = MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages) => + new(categoryId, MigrationCategoryState.InProgress, null, 0, 0, null, null, null, null, null, null); } diff --git a/src/ServiceControl.Persistence.Tests/EFCore/Migration/GroupCommentsMigrationTargetTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/Migration/GroupCommentsMigrationTargetTests.cs index 9f50b13692..d932cbc893 100644 --- a/src/ServiceControl.Persistence.Tests/EFCore/Migration/GroupCommentsMigrationTargetTests.cs +++ b/src/ServiceControl.Persistence.Tests/EFCore/Migration/GroupCommentsMigrationTargetTests.cs @@ -99,7 +99,7 @@ public async Task A_comment_on_a_group_with_no_copied_message_is_reported_as_a_c { ("GroupComment/group-of-archived-messages", "The group 'group-of-archived-messages' has no copied failed message, so the retention sweep removes this comment shortly after ServiceControl opens. " + - "Its messages are archived or resolved, are gone from the source, or were skipped by the copy. " + + "Its messages have not been copied yet, are archived or resolved, are gone from the source, or were skipped by the copy. " + $"The comment was: '{longComment}'") }), "a comment on a group with a copied message stays, so only the other is named, with the whole comment because the sweep deletes it"); Assert.That((first.Copied, first.Changed), Is.EqualTo((2, 1))); diff --git a/src/ServiceControl.Persistence.Tests/EFCore/Migration/MigrationTargetCoverageTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/Migration/MigrationTargetCoverageTests.cs index 3e942eace6..ae2043d82f 100644 --- a/src/ServiceControl.Persistence.Tests/EFCore/Migration/MigrationTargetCoverageTests.cs +++ b/src/ServiceControl.Persistence.Tests/EFCore/Migration/MigrationTargetCoverageTests.cs @@ -95,6 +95,7 @@ public async Task Every_writer_whose_rows_a_re_read_cannot_recognise_deletes_the [MigrationCategoryIds.KnownEndpoints] = nameof(KnownEndpointsMigrationTargetTests.A_known_endpoint_with_a_value_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), [MigrationCategoryIds.EndpointSettings] = nameof(EndpointSettingsMigrationTargetTests.A_setting_whose_name_is_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), [MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages] = nameof(FailedMessagesMigrationTargetTests.A_message_or_group_value_longer_than_its_column_skips_the_message_whole_and_the_rest_copy), + [MigrationCategoryIds.UnresolvedFailedMessages] = nameof(FailedMessagesMigrationTargetTests.A_message_or_group_value_longer_than_its_column_skips_the_message_whole_and_the_rest_copy), [MigrationCategoryIds.LicensingEndpoints] = nameof(LicensingEndpointsMigrationTargetTests.A_licensing_endpoint_with_a_value_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), [MigrationCategoryIds.LicensingThroughput] = nameof(LicensingThroughputMigrationTargetTests.A_day_whose_endpoint_name_is_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), [MigrationCategoryIds.MessageRedirects] = nameof(MessageRedirectsMigrationTargetTests.A_redirect_with_an_address_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), @@ -113,6 +114,7 @@ public async Task Every_writer_whose_rows_a_re_read_cannot_recognise_deletes_the [typeof(KnownEndpointsWriter)] = [typeof(KnownEndpointEntity)], [typeof(EndpointSettingsWriter)] = [typeof(EndpointSettingsEntity)], [typeof(FailedMessagesWriter)] = [typeof(FailedMessageEntity), typeof(FailedMessageGroupEntity)], + [typeof(UnresolvedFailedMessagesWriter)] = [typeof(FailedMessageEntity), typeof(FailedMessageGroupEntity)], [typeof(LicensingEndpointsWriter)] = [typeof(LicensingEndpointEntity)], [typeof(LicensingThroughputWriter)] = [typeof(LicensingEndpointThroughputEntity)], [typeof(LicensingReportMasksWriter)] = [typeof(SettingEntity)], diff --git a/src/ServiceControl.Persistence.Tests/EFCore/S3BodyStorageConditionalWriteTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/S3BodyStorageConditionalWriteTests.cs new file mode 100644 index 0000000000..51306bb304 --- /dev/null +++ b/src/ServiceControl.Persistence.Tests/EFCore/S3BodyStorageConditionalWriteTests.cs @@ -0,0 +1,201 @@ +namespace ServiceControl.Persistence.Tests; + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Threading; +using System.Threading.Tasks; +using Amazon.S3; +using NUnit.Framework; +using ServiceControl.Persistence.EFCore.Abstractions; +using ServiceControl.Persistence.EFCore.Implementation.BodyStorage; + +// Runs the real S3 client against a local listener, so it needs neither Docker nor LocalStack. +[TestFixture] +class S3BodyStorageConditionalWriteTests +{ + FakeS3Endpoint endpoint; + + [SetUp] + public void StartEndpoint() => endpoint = new FakeS3Endpoint(); + + [TearDown] + public void StopEndpoint() => endpoint.Dispose(); + + S3BodyStoragePersistence Store() => new(new S3BodyStorageSettings + { + MinCompressionSize = 64, + ServiceUrl = endpoint.Url, + Region = "us-east-1", + Credentials = new S3StaticCredentials { AccessKeyId = "test", SecretAccessKey = "test" }, + BucketName = "bodies" + }); + + [Test] + public async Task The_upload_creates_the_object_only_if_none_exists_and_keeps_its_content_type_and_metadata() + { + await Store().WriteBody("body-1", new byte[] { 1, 2, 3 }, "text/plain"); + + var put = endpoint.Requests.Single(request => request.Method == "PUT"); + + using (Assert.EnterMultipleScope()) + { + Assert.That(put.Headers["If-None-Match"], Is.EqualTo("*"), "without the condition a body stored after the existence check is replaced"); + Assert.That(put.Headers["Content-Type"], Is.EqualTo("text/plain")); + Assert.That(put.Headers["x-amz-meta-body-size"], Is.EqualTo("3")); + Assert.That(put.Headers["x-amz-meta-is-compressed"], Is.EqualTo("False")); + } + } + + [Test] + public async Task A_compressed_upload_is_conditional_too() + { + await Store().WriteBody("body-2", new byte[10_000], "application/octet-stream"); + + var put = endpoint.Requests.Single(request => request.Method == "PUT"); + + using (Assert.EnterMultipleScope()) + { + Assert.That(put.Headers["If-None-Match"], Is.EqualTo("*")); + Assert.That(put.Headers["x-amz-meta-is-compressed"], Is.EqualTo("True")); + Assert.That(put.Headers["x-amz-meta-body-size"], Is.EqualTo("10000")); + } + } + + [Test] + public async Task An_existing_object_is_not_uploaded_over() + { + endpoint.ObjectExists = true; + + await Store().WriteBody("body-3", new byte[] { 1 }, "text/plain"); + + Assert.That(endpoint.Requests.Select(request => request.Method), Is.EqualTo(new[] { "HEAD" })); + } + + [Test] + public void An_object_stored_after_the_existence_check_is_left_alone_without_an_error() + { + endpoint.PutStatus = HttpStatusCode.PreconditionFailed; + + Assert.DoesNotThrowAsync(() => Store().WriteBody("body-4", new byte[] { 1 }, "text/plain")); + Assert.That(endpoint.Requests.Count(request => request.Method == "PUT"), Is.EqualTo(1)); + } + + [Test] + public void Any_other_storage_failure_still_fails_the_write() + { + endpoint.PutStatus = HttpStatusCode.Forbidden; + + var exception = Assert.ThrowsAsync(() => Store().WriteBody("body-5", new byte[] { 1 }, "text/plain")); + + Assert.That(exception.StatusCode, Is.EqualTo(HttpStatusCode.Forbidden)); + } + + [Test] + public void A_cancelled_write_is_cancelled() + { + using var cancelled = new CancellationTokenSource(); + cancelled.Cancel(); + + Assert.CatchAsync(() => Store().WriteBody("body-6", new byte[] { 1 }, "text/plain", cancelled.Token)); + } + + sealed class FakeS3Endpoint : IDisposable + { + readonly HttpListener listener = new(); + readonly object gate = new(); + readonly List requests = []; + + public FakeS3Endpoint() + { + // The listener needs a free port, which it cannot report before it starts, so probe for one. + for (var attempt = 0; ; attempt++) + { + var port = Random.Shared.Next(20000, 60000); + var candidate = new HttpListener(); + candidate.Prefixes.Add($"http://localhost:{port}/"); + + try + { + candidate.Start(); + listener = candidate; + Url = $"http://localhost:{port}"; + break; + } + catch (HttpListenerException) when (attempt < 20) + { + candidate.Close(); + } + } + + _ = Serve(); + } + + public string Url { get; } + public bool ObjectExists { get; set; } + public HttpStatusCode PutStatus { get; set; } = HttpStatusCode.OK; + + public IReadOnlyList Requests + { + get + { + lock (gate) + { + return [.. requests]; + } + } + } + + async Task Serve() + { + while (listener.IsListening) + { + HttpListenerContext context; + + try + { + context = await listener.GetContextAsync(); + } + catch (Exception ex) when (ex is HttpListenerException or ObjectDisposedException or InvalidOperationException) + { + return; + } + + var headers = context.Request.Headers.AllKeys.ToDictionary(key => key, key => context.Request.Headers[key], StringComparer.OrdinalIgnoreCase); + + lock (gate) + { + requests.Add(new RecordedRequest(context.Request.HttpMethod, headers)); + } + + // The body is not needed, but the client waits for the server to read it. + await context.Request.InputStream.CopyToAsync(Stream.Null); + + var status = context.Request.HttpMethod switch + { + "HEAD" => ObjectExists ? HttpStatusCode.OK : HttpStatusCode.NotFound, + "PUT" => PutStatus, + _ => HttpStatusCode.NotImplemented + }; + + context.Response.StatusCode = (int)status; + + if (context.Request.HttpMethod == "PUT" && status != HttpStatusCode.OK) + { + var code = status == HttpStatusCode.PreconditionFailed ? "PreconditionFailed" : "AccessDenied"; + var error = System.Text.Encoding.UTF8.GetBytes($"{code}{code}1"); + context.Response.ContentType = "application/xml"; + await context.Response.OutputStream.WriteAsync(error); + } + + context.Response.Close(); + } + } + + public void Dispose() => listener.Close(); + } + + sealed record RecordedRequest(string Method, Dictionary Headers); +} diff --git a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryIds.cs b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryIds.cs index 6670fcfbcb..fe22801861 100644 --- a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryIds.cs +++ b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryIds.cs @@ -17,7 +17,15 @@ public static class MigrationCategoryIds public const string LicensingThroughput = nameof(LicensingThroughput); public const string LicensingReportMasks = nameof(LicensingReportMasks); public const string LicensedEndpointDetails = nameof(LicensedEndpointDetails); + + /// + /// The retry-issued messages and the messages that hold a retry claim, copied before the host opens: every retry-issued message and + /// every other message that still holds a retry claim. + /// public const string UnresolvedAndRetryIssuedFailedMessages = nameof(UnresolvedAndRetryIssuedFailedMessages); + + /// The unresolved failed messages, copied once the host is open, whatever their age. + public const string UnresolvedFailedMessages = nameof(UnresolvedFailedMessages); public const string PendingIntegrationEvents = nameof(PendingIntegrationEvents); public const string EventLog = nameof(EventLog); diff --git a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs index e3db874daf..4f3e0492d8 100644 --- a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs +++ b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs @@ -12,10 +12,16 @@ namespace ServiceControl.Persistence.DataMigration; public static class MigrationCategoryRegistry { /// - /// The failed message statuses that are copied before the host opens. A message in one of these is waiting on - /// somebody, so an operator who cannot see it after the cutover has lost work. + /// The failed message status that is copied before the host opens, whether or not a retry claim is held. A retry + /// confirmation that arrives after the cutover resolves a message only if its row is already there. /// - public static readonly IReadOnlyList UnresolvedAndRetryIssuedStatuses = [FailedMessageStatus.Unresolved, FailedMessageStatus.RetryIssued]; + public static readonly IReadOnlyList RetryIssuedStatuses = [FailedMessageStatus.RetryIssued]; + + /// + /// The failed message status that is copied in the background without a window, because an operator is waiting on + /// these however old they are. + /// + public static readonly IReadOnlyList UnresolvedStatuses = [FailedMessageStatus.Unresolved]; /// /// The failed message statuses that are copied in the background. These are history, so the instance is usable @@ -44,8 +50,9 @@ public static class MigrationCategoryRegistry new(MigrationCategoryIds.GroupComments, MigrationCategoryKind.Required, CarriesBodies: false, Order: 16, MustFollow: MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages), // Optional, copied in the background once the host is open. - new(MigrationCategoryIds.EventLog, MigrationCategoryKind.Optional, CarriesBodies: false, Order: 1), - new(MigrationCategoryIds.ArchivedAndResolvedFailedMessages, MigrationCategoryKind.Optional, CarriesBodies: true, Order: 2), + new(MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryKind.Optional, CarriesBodies: true, Order: 1), + new(MigrationCategoryIds.EventLog, MigrationCategoryKind.Optional, CarriesBodies: false, Order: 2), + new(MigrationCategoryIds.ArchivedAndResolvedFailedMessages, MigrationCategoryKind.Optional, CarriesBodies: true, Order: 3), ]; public static MigrationCategory? Find(string id) => All.FirstOrDefault(c => c.Id == id); diff --git a/src/ServiceControl.Persistence/DataMigration/MigrationEngineOptions.cs b/src/ServiceControl.Persistence/DataMigration/MigrationEngineOptions.cs index 8dee699d9f..5651af3531 100644 --- a/src/ServiceControl.Persistence/DataMigration/MigrationEngineOptions.cs +++ b/src/ServiceControl.Persistence/DataMigration/MigrationEngineOptions.cs @@ -11,7 +11,7 @@ namespace ServiceControl.Persistence.DataMigration; /// How long to wait between batches of an optional category, which is what keeps the background copy off the instance's back. Zero waits not at all. /// The share of skipped rows, as a percentage, that stops a category. /// The number of skipped rows that has to be passed before the percentage counts, so a handful of bad rows in a small category is not a halt. -/// The optional categories to copy. An optional category outside this list is never copied. +/// The optional categories to copy. An optional category outside this list is never copied. The unresolved failed messages are always in it. public sealed record MigrationEngineOptions( TimeSpan ThrottlePause, int HaltThresholdPercent, @@ -37,7 +37,7 @@ public sealed record MigrationEngineOptions( /// /// Reads the options from the instance's settings. Anything not set takes the default from /// , except the two windows, which default to the retention periods passed in. - /// An optional category is selected when its window is greater than zero. + /// A windowed optional category is selected when its window is greater than zero, and the unresolved failed messages are always selected. /// /// The settings root the migration keys are read under. /// The instance's event retention period, used as the event log window when none is set. @@ -51,7 +51,8 @@ public static MigrationEngineOptions FromSettings(SettingsRootNamespace settings var eventLogWindow = ReadWindow(settingsRootNamespace, MigrationSettings.EventLogWindowKey, eventRetentionPeriod); var archivedAndResolvedWindow = ReadWindow(settingsRootNamespace, MigrationSettings.ArchivedAndResolvedFailedMessagesWindowKey, errorRetentionPeriod); - var selectedIds = new List(); + // Not windowed: an unresolved message is waiting on somebody however old it is. + var selectedIds = new List { MigrationCategoryIds.UnresolvedFailedMessages }; if (eventLogWindow > TimeSpan.Zero) { diff --git a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt index 6c99c6b3e0..563a77e86d 100644 --- a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt +++ b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt @@ -14,5 +14,6 @@ Required 13: PendingIntegrationEvents Required 14: CustomChecks Required 15: FailedErrorImports, with bodies Required 16: GroupComments, after UnresolvedAndRetryIssuedFailedMessages -Optional 1: EventLog -Optional 2: ArchivedAndResolvedFailedMessages, with bodies \ No newline at end of file +Optional 1: UnresolvedFailedMessages, with bodies +Optional 2: EventLog +Optional 3: ArchivedAndResolvedFailedMessages, with bodies \ No newline at end of file diff --git a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Only_configured_optional_categories_are_selected.approved.txt b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Only_configured_optional_categories_are_selected.approved.txt index d0b5240bad..3338eba16c 100644 --- a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Only_configured_optional_categories_are_selected.approved.txt +++ b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Only_configured_optional_categories_are_selected.approved.txt @@ -1,4 +1,4 @@ Configured: EventLog Runs as: -Optional 1: EventLog -Never copied: ArchivedAndResolvedFailedMessages \ No newline at end of file +Optional 2: EventLog +Never copied: UnresolvedFailedMessages, ArchivedAndResolvedFailedMessages \ No newline at end of file diff --git a/src/ServiceControl.UnitTests/Migration/MigrationBeforeTheHostOpensTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationBeforeTheHostOpensTests.cs index 9e86ba2723..6566769991 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationBeforeTheHostOpensTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationBeforeTheHostOpensTests.cs @@ -64,6 +64,17 @@ public async Task Optional_work_does_not_hold_the_host_closed(MigrationCategoryS await new MigrationBeforeTheHostOpens(services).StartingAsync(); } + [Test] + public async Task The_required_failed_messages_with_the_unresolved_failed_messages_row_open_the_host_while_that_row_still_copies() + { + var store = new InMemoryMigrationCheckpointStore(); + await store.Upsert(Checkpoint(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages, MigrationCategoryState.Complete)); + await store.Upsert(Checkpoint(MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryState.InProgress)); + using var services = Services(store); + + await new MigrationBeforeTheHostOpens(services).StartingAsync(); + } + static ServiceProvider Services(IMigrationCheckpointStore store) => new ServiceCollection().AddSingleton(store).BuildServiceProvider(); diff --git a/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs index 214b13a2d2..cf786152dc 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs @@ -12,12 +12,12 @@ namespace ServiceControl.UnitTests.Migration; class MigrationCategoryRegistryTests { [Test] - public void Contains_all_eighteen_categories_with_unique_ids() + public void Contains_all_nineteen_categories_with_unique_ids() { using (Assert.EnterMultipleScope()) { - Assert.That(MigrationCategoryRegistry.All, Has.Count.EqualTo(18)); - Assert.That(MigrationCategoryRegistry.All.Select(c => c.Id).Distinct().Count(), Is.EqualTo(18)); + Assert.That(MigrationCategoryRegistry.All, Has.Count.EqualTo(19)); + Assert.That(MigrationCategoryRegistry.All.Select(c => c.Id).Distinct().Count(), Is.EqualTo(19)); } } @@ -51,7 +51,7 @@ public void Optional_categories_are_ordered_exactly_as_the_contract_lists_them() Assert.That(optionalIds, Is.EqualTo(new[] { - "EventLog", "ArchivedAndResolvedFailedMessages" + "UnresolvedFailedMessages", "EventLog", "ArchivedAndResolvedFailedMessages" })); } @@ -76,23 +76,37 @@ public void Three_categories_declare_a_MustFollow() } [Test] - public void The_two_failed_message_categories_and_failed_imports_carry_bodies() + public void The_three_failed_message_categories_and_failed_imports_carry_bodies() { var carriesBodies = MigrationCategoryRegistry.All.Where(c => c.CarriesBodies).Select(c => c.Id).ToArray(); - Assert.That(carriesBodies, Is.EquivalentTo(new[] { "UnresolvedAndRetryIssuedFailedMessages", "ArchivedAndResolvedFailedMessages", "FailedErrorImports" })); + Assert.That(carriesBodies, Is.EquivalentTo(new[] { "UnresolvedAndRetryIssuedFailedMessages", "UnresolvedFailedMessages", "ArchivedAndResolvedFailedMessages", "FailedErrorImports" })); } [Test] - public void The_two_failed_message_categories_split_every_status_between_them() + public void The_unresolved_failed_messages_are_copied_after_the_host_opens_and_the_group_comments_do_not_wait_for_them() { - var split = MigrationCategoryRegistry.UnresolvedAndRetryIssuedStatuses.Concat(MigrationCategoryRegistry.ArchivedAndResolvedStatuses).ToArray(); + using (Assert.EnterMultipleScope()) + { + Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedFailedMessages)?.Kind, Is.EqualTo(MigrationCategoryKind.Optional)); + Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.GroupComments)?.MustFollow, Is.EqualTo(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)); + Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)?.Kind, Is.EqualTo(MigrationCategoryKind.Required)); + } + } + + [Test] + public void The_failed_message_statuses_are_split_between_the_retry_issued_unresolved_and_archived_and_resolved_copies() + { + var split = MigrationCategoryRegistry.RetryIssuedStatuses + .Concat(MigrationCategoryRegistry.UnresolvedStatuses) + .Concat(MigrationCategoryRegistry.ArchivedAndResolvedStatuses) + .ToArray(); using (Assert.EnterMultipleScope()) { Assert.That(split, Is.Unique); Assert.That(split, Is.EquivalentTo(Enum.GetValues()), - "a status neither category claims is a failed message no migration copies"); + "a status no category claims is a failed message no migration copies"); } } diff --git a/src/ServiceControl.UnitTests/Migration/MigrationEngineOptionsTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationEngineOptionsTests.cs index 71e62dac7b..34ee9c7d63 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationEngineOptionsTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationEngineOptionsTests.cs @@ -35,7 +35,7 @@ public void Defaults_match_the_contract_when_nothing_is_configured() Assert.That(options.ThrottlePause, Is.EqualTo(TimeSpan.FromMilliseconds(100))); Assert.That(options.HaltThresholdPercent, Is.EqualTo(5)); Assert.That(options.HaltThresholdMinimum, Is.EqualTo(100)); - Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.EventLog, MigrationCategoryIds.ArchivedAndResolvedFailedMessages }), "both optional categories are copied unless turned off"); + Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.EventLog, MigrationCategoryIds.ArchivedAndResolvedFailedMessages }), "both windowed optional categories are copied unless turned off, and the unresolved failed messages always are"); Assert.That(options.EventLogWindow, Is.EqualTo(EventRetention), "the event log window defaults to the event retention period"); Assert.That(options.ArchivedAndResolvedFailedMessagesWindow, Is.EqualTo(ErrorRetention), "the archived and resolved window defaults to the error retention period"); Assert.That(options.BodyRetryBackoff, Is.EqualTo(TimeSpan.FromMilliseconds(200))); @@ -60,7 +60,7 @@ public void Reads_configured_values_from_environment_variables() Assert.That(options.HaltThresholdMinimum, Is.EqualTo(50)); Assert.That(options.EventLogWindow, Is.EqualTo(TimeSpan.FromDays(2))); Assert.That(options.ArchivedAndResolvedFailedMessagesWindow, Is.EqualTo(TimeSpan.FromDays(5.5))); - Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.EventLog, MigrationCategoryIds.ArchivedAndResolvedFailedMessages })); + Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.EventLog, MigrationCategoryIds.ArchivedAndResolvedFailedMessages })); } } @@ -72,7 +72,16 @@ public void A_zero_window_turns_its_category_off(string variable, string stillSe var options = Read(); - Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { stillSelected })); + Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, stillSelected })); + } + + [Test] + public void The_unresolved_failed_messages_are_selected_when_both_windows_are_zero() + { + Environment.SetEnvironmentVariable("SERVICECONTROL_MIGRATION_EVENTLOGWINDOW", "0"); + Environment.SetEnvironmentVariable("SERVICECONTROL_MIGRATION_ARCHIVEDANDRESOLVEDFAILEDMESSAGESWINDOW", "0"); + + Assert.That(Read().SelectedOptionalCategoryIds, Is.EqualTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages }), "unresolved messages have no window to turn off"); } [TestCase("a week")] From 9a63050165a5513f586eeb201faf69d2b7233ae1 Mon Sep 17 00:00:00 2001 From: Rhys Bevilaqua Date: Fri, 20 Nov 2026 11:33:01 +0800 Subject: [PATCH 3/8] Tidy up docs --- .../ravendb-to-sql-migration-instructions.md | 2 +- .../ravendb-to-sql-migration-overview.md | 26 ++++------- .../ravendb-to-sql-migration-system-design.md | 46 ++++--------------- 3 files changed, 20 insertions(+), 54 deletions(-) diff --git a/docs/migration/ravendb-to-sql-migration-instructions.md b/docs/migration/ravendb-to-sql-migration-instructions.md index 1b162a864b..43cf0163e6 100644 --- a/docs/migration/ravendb-to-sql-migration-instructions.md +++ b/docs/migration/ravendb-to-sql-migration-instructions.md @@ -1,7 +1,7 @@ # Migration Engine: Instructions > [!NOTE] -> Production copying remains blocked until the complete migration ships. `--migration-source-report` works; `--start-data-migration` reports that copying is unavailable. The release blockers for the failed-message split are listed under [Unresolved release blockers](ravendb-to-sql-migration-overview.md#unresolved-release-blockers) in the overview. +> Production copying remains blocked until the complete migration ships. `--migration-source-report` works; `--start-data-migration` reports that copying is unavailable. This page tells you how to move the data of a ServiceControl error instance from RavenDB to SQL Server or PostgreSQL. The ServiceControl executable provides the offline command and the online copier, so you do not need a separate tool. [The migration overview](ravendb-to-sql-migration-overview.md) explains how the migration works and why. diff --git a/docs/migration/ravendb-to-sql-migration-overview.md b/docs/migration/ravendb-to-sql-migration-overview.md index d4417fe863..3f6130d09b 100644 --- a/docs/migration/ravendb-to-sql-migration-overview.md +++ b/docs/migration/ravendb-to-sql-migration-overview.md @@ -1,7 +1,7 @@ # Migration Engine: Design Overview > [!NOTE] -> This build does not carry the whole migration yet. This page describes it as it will be when it ships. The failed-message split described below has unresolved release blockers, listed under [Unresolved release blockers](#unresolved-release-blockers). Each one must be resolved or explicitly accepted before the migration ships. +> This build does not carry the whole migration yet. This page describes it as it will be when it ships. `MigrationIsReleasedCheck` refuses production copies until it does. [The instructions](ravendb-to-sql-migration-instructions.md) describe how an operator runs the migration, and the [system design](ravendb-to-sql-migration-system-design.md) provides the system architecture. See the [glossary](#glossary) for terms used throughout. @@ -20,7 +20,7 @@ - **What the instance needs to open is copied while it is closed.** This is the *required* data. Most of it is small. - **Everything else is copied in the background while it runs.** This is the *optional* data: the unresolved failed messages, the event log and archived and resolved messages. - **Anything not copied ages out of RavenDB**, and the customer deletes the old database when ready. -- **The failed messages copied during the outage are every retry-issued message, whether or not its retry claim remains, and every other failed message that still holds a retry claim.** This does not cover every delayed retry confirmation, see the [unresolved release blockers](#unresolved-release-blockers). Unresolved messages that hold no claim copy in the background, however old they are, so a big unresolved backlog does not lengthen the outage. +- **The failed messages copied during the outage are every retry-issued message, whether or not its retry claim remains, and every other failed message that still holds a retry claim.** Unresolved messages that hold no claim copy in the background, however old they are, so a big unresolved backlog does not lengthen the outage. - **A big archive does not make the outage longer, as long as RavenDB has indexed every failed message.** The copy finds retry-issued messages through a RavenDB index, and waits for it to catch up first. If the index is broken or incomplete, the copy reads every failed message instead and logs a warning. Then a big archive does make the outage longer. ```mermaid @@ -143,7 +143,7 @@ There are nineteen *categories*: sixteen required and three optional. | Category | Kind | Why | | --- | --- | --- | -| Retry-issued failed messages, and failed messages that hold a retry claim, with bodies | Required | Issuing a retry removes the expiry, so retry-issued messages never age out. Leave one behind and the retry confirmation finds no row to resolve, so the message stays missing even though the retry succeeded. A message of any other status that still holds a retry claim copies with it, with its groups, body and claim. The category keeps the checkpoint id `UnresolvedAndRetryIssuedFailedMessages` from the layout that copied every unresolved message here, see [Progress and checkpoints](#progress-and-checkpoints) | +| Retry-issued failed messages, and failed messages that hold a retry claim, with bodies | Required | Issuing a retry removes the expiry, so retry-issued messages never age out. Leave one behind and the retry confirmation finds no row to resolve, so the message stays missing even though the retry succeeded. A message of any other status that still holds a retry claim copies with it, with its groups, body and claim. The category keeps the checkpoint id `UnresolvedAndRetryIssuedFailedMessages` from the layout that copied every unresolved message here, see [Progress and checkpoints](#progress-and-checkpoints). A delayed retry confirmation for an unresolved message whose claim a re-failure removed is not covered, an accepted edge case, see [Writing to SQL](#writing-to-sql) | | Message redirects | Required | | | Known endpoints, with the monitored flag | Required | The flag is part of the endpoint row | | Endpoint settings | Required | Copied after known endpoints | @@ -158,8 +158,8 @@ There are nineteen *categories*: sixteen required and three optional. | Integration events still waiting to be sent | Required | Each is an event a subscriber has not received yet. There are only any if the old instance fell behind or could not reach the broker. They are sent once ServiceControl opens, later than they would have been, except a `MessageFailed` event for a message archived or resolved before the move (see [Rows removed after the copy](#rows-removed-after-the-copy)). RavenDB sends them in no particular order anyway, so no ordering is lost | | Custom checks, with their last status | Required | Not every check reports again. An endpoint that is down never does, and a check with no repeat interval reports only when its endpoint starts. Leaving one out could hide a known failure until that endpoint restarts. Results of the six checks the RavenDB persister runs about its own database, matched on id and category, are left out, as harmless skips | | Failed error imports, with bodies | Required | Messages taken off the error queue but not ingested. They exist nowhere else and never expire. After the move the "Error Message Ingestion" custom check keeps flagging them and `--import-failed-errors` imports them into SQL. Importing them on RavenDB first is better still | -| Group comments | Required | Copied after the required failed messages and never waiting for an optional category. Blank comments are not copied. A comment on a group whose messages have not reached SQL yet is removed shortly after ServiceControl opens, which is an [unresolved release blocker](#unresolved-release-blockers) | -| Unresolved failed messages, with bodies | Optional | Copied once ServiceControl has opened, whatever the age of the message, because an operator is waiting on each one. It has no window and no setting turns it off. A message that already holds a retry claim copied with the required category, so it counts as already present. An existing SQL row always wins, see [Writing to SQL](#writing-to-sql) | +| Group comments | Required | Copied after the required failed messages and never waiting for an optional category. Blank comments are not copied. A comment on a group whose messages have not reached SQL yet is removed shortly after ServiceControl opens, an accepted edge case, see [Rows removed after the copy](#rows-removed-after-the-copy) | +| Unresolved failed messages, with bodies | Optional | Copied once ServiceControl has opened, whatever the age of the message, because an operator is waiting on each one. It has no window and no setting turns it off. A message that already holds a retry claim copied with the required category, so it counts as already present. An existing SQL row always wins, see [Writing to SQL](#writing-to-sql). Group retry, archive and unarchive act only on the messages copied so far, an accepted edge case | | Event log | Optional | The history behind the ServicePulse activity feed. On a busy instance it holds a row for every failure, retry and submission | | Archived and resolved failed messages, with bodies | Optional | The biggest category by far, and most of the copying time | @@ -292,6 +292,7 @@ flowchart LR - **A failed message is written as one row**, with its status unchanged. Its older attempts are dropped, see [Open decisions](#values-that-change-or-merge-in-sql). - **An existing SQL row always wins.** The copy never updates a row and counts an existing row as already present, not as a skip. When ingestion created the row first, its attempt count and first failure time can omit earlier RavenDB failures, and it can hold an older payload than RavenDB's newest attempt. These outcomes are accepted. +- **The copy accepts unlikely races with live operations.** A delayed retry confirmation can be consumed before an unresolved message whose claim a re-failure removed arrives, leaving it unresolved. A message archived or resolved in SQL and then deleted by retention before the copy reaches it can arrive again as unresolved. A queued edit-and-retry is discarded if its original message has not arrived. - **Bodies are immutable and written only if absent.** The body store keeps one body per `UniqueMessageId`, and each backend refuses to replace one that exists: the file system through an exclusive move, Azure Blob and S3 through a conditional upload. The first writer wins, so a body the copy stores just before ingestion does is the body that stays. That is safe only because every attempt of one message carries the same body bytes, which ingestion already assumes. An S3-compatible store that ignores `If-None-Match` falls back to a check followed by an unconditional upload, which can replace a live body. - **A failed message keeps its failure groups and its last-modified time**, so filtered lists show the same messages as before. A failure group with no id is the exception, see [Open decisions](#values-that-change-or-merge-in-sql). - **`UniqueMessageId` keeps its value** but becomes a `uniqueidentifier`. It is the primary key, the ServicePulse URL, the retry key and the body key. @@ -498,19 +499,10 @@ Each case below changes, merges or drops data, or lets a loss pass without faili The dry run counts the subscription and endpoint settings merges before anything moves. -### Unresolved release blockers - -Moving the unresolved messages that hold no retry claim online opens the cases below. The operator chose to continue with the split and defer each decision. None of them is an accepted outcome, and each one blocks the release. The migration stays blocked from copying in production until each is resolved or explicitly accepted, because `MigrationIsReleasedCheck` still refuses every copy. This split changes no retry confirmation handling and adds no runtime restriction. - -- **A retry confirmation can be consumed before its message arrives.** A re-failure can leave a message `Unresolved` after its retry claim is removed, and a confirmation for it can still be queued when ServiceControl stops. The offline cohort leaves that message for the online copy. If SQL consumes the confirmation first, the conditional update matches no row and succeeds, the claim delete matches nothing, and the confirmation is gone. The online copy then inserts RavenDB's message as unresolved, though a retry of it succeeded. Retrying the migration cannot recover the confirmation. A blank or unreadable success time makes this worse, because the confirmation then counts as newer than any failure. The plan records the interleaving, and `Known_release_blocker_a_retry_confirmation_for_a_message_the_unresolved_copy_has_not_inserted_is_consumed_and_the_message_arrives_unresolved` pins today's behavior without handling it. -- **Group comments on groups the online copy has not reached are removed.** See [Rows removed after the copy](#rows-removed-after-the-copy). The partial cleanup of comments for groups that are only partly copied is deferred as well. -- **A SQL row can be resurrected as unresolved.** Ingestion creates the row, an operator archives or resolves it in SQL, and the retention sweep deletes it before the online copy reaches the message. The copy then finds no row and inserts RavenDB's unresolved message. The insert-only rule protects only a row that still exists. -- **Online operations on partly copied data are not restricted.** Group retry, archive and unarchive act on the messages copied so far, a queued edit-and-retry command is discarded when its original message has not arrived, and a pending `MessageFailed` event is deleted when its message has not arrived. The deferred investigation must decide whether to restrict, defer or accept each, and how they behave if the unresolved category is abandoned. - ### Rows removed after the copy -- **A group comment on a group that has no failed message in SQL is removed soon after ServiceControl opens**, because the retention sweep deletes a comment whose group has no row. The group has none when its messages are unresolved without a retry claim and the background copy has not reached them, when they are archived or resolved, when they are gone from RavenDB, or when the copy skipped them. The sweep first runs about a minute after ServiceControl opens and then hourly, so this is deterministic for such groups and not a rare race. The copy names each such comment, in a warning starting `Changed GroupComment/ in category GroupComments on the way into SQL: The group '' has no copied failed message`, which also quotes the whole comment. The comment loss is an [unresolved release blocker](#unresolved-release-blockers), not an accepted outcome. -- **A `MessageFailed` event still waiting to be sent for a message archived or resolved before the move is never sent.** ServiceControl looks the message up when it sends the event. Archived and resolved messages arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. The same lookup can fail for an unresolved message the background copy has not reached yet, see [Unresolved release blockers](#unresolved-release-blockers). +- **A group comment on a group that has no failed message in SQL is removed soon after ServiceControl opens**, because the retention sweep deletes a comment whose group has no row. The group has none when its messages are unresolved without a retry claim and the background copy has not reached them, when they are archived or resolved, when they are gone from RavenDB, or when the copy skipped them. The copy names each such comment, in a warning starting `Changed GroupComment/ in category GroupComments on the way into SQL: The group '' has no copied failed message`, which also quotes the whole comment. This loss is accepted for groups the background copy has not reached yet. +- **A `MessageFailed` event still waiting to be sent for a message archived or resolved before the move is never sent.** ServiceControl looks the message up when it sends the event. Archived and resolved messages arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. The same lookup can fail for an unresolved message the background copy has not reached yet, an accepted edge case. - **Unresolved messages from version 6.18 or earlier can expire during the copy.** On those versions, a message that failed again after being archived or resolved kept its expiry. One deleted before the copy reads it is not copied and not counted. No log line names these messages yet. ### What the checks do not catch @@ -531,7 +523,7 @@ Moving the unresolved messages that hold no retry claim online opens the cases b - **Category:** one kind of data copied as a unit, such as known endpoints or the event log. - **Required category:** one ServiceControl needs the moment it opens, so it copies while ServiceControl is closed. -- **Optional category:** one copied in the background after ServiceControl opens. It can be shortened or turned off before it starts. +- **Optional category:** one copied in the background after ServiceControl opens. An optional category other than the unresolved failed messages can be shortened or turned off before it starts. - **Window:** how far back an optional category copies. - **Closed period:** the time ServiceControl is not serving traffic because the required copy is running. - **Checkpoint:** the row in SQL recording one category's progress, saved in the same transaction as the rows it describes. diff --git a/docs/migration/ravendb-to-sql-migration-system-design.md b/docs/migration/ravendb-to-sql-migration-system-design.md index 4b5f08994f..e1fee92666 100644 --- a/docs/migration/ravendb-to-sql-migration-system-design.md +++ b/docs/migration/ravendb-to-sql-migration-system-design.md @@ -77,37 +77,6 @@ The ingestion gate sees categories the copy never reached because `MigrationStar ## The required copy -Retry-issued failed messages, and every other failed message that holds a retry claim, copy while the host is closed. Unresolved messages without a claim copy after the host opens, as the optional category `UnresolvedFailedMessages`. The required category's id is `UnresolvedAndRetryIssuedFailedMessages`. `FailedMessagesWriterBase` inserts only messages the target lacks and never updates one it holds, so an existing SQL row always wins. From the moment the host opens, ingestion writes to the same rows: - -- **A retry confirmation finds no row.** `ResolveRetriedMessages` updates only rows already in SQL, and `FailedMessageBatchWriter.ResolveRetried` deletes the retry claim either way. A message the copy has not inserted yet would arrive as still failing, though its retry succeeded. The offline cohort is the retry-issued messages and the messages holding a claim. It does not cover every delayed confirmation, in particular a delayed confirmation for an unresolved message whose claim a re-failure removed. That case is an [unresolved release blocker](ravendb-to-sql-migration-overview.md#unresolved-release-blockers), and no confirmation handling changed. -- **A message that fails again keeps only what ingestion stored.** Ingestion upserts a row holding the new attempt. The online copy finds the id present, counts it as already present and drops the RavenDB row. SQL then holds an attempt count and first failure time that omit earlier RavenDB attempts, and possibly an older payload. These outcomes are accepted, and the copy does not merge failure history. -- **Group actions see part of the list.** Retrying or archiving a group in ServicePulse acts only on the messages copied so far. Restricting this is deferred. - -Archived and resolved messages are safe to copy after the host opens. If one of them fails again, the live unresolved row should win, and an insert-only copy gives exactly that. - -### The offline cohort - -`FailedMessagesReader` derives from `FailedMessageStatusReader`, which `UnresolvedFailedMessagesReader` also derives from. The base class streams the messages with the category's statuses in id order, through a query on `FailedMessageViewIndex` after waiting for the index, or through `RavenDocumentStream.ByPrefix` when the index cannot be trusted. Both ways use the document id as the cursor. - -The offline reader reads in two phases: - -1. The retry-issued messages come from the base class. Each batch goes through `FailedMessageStream.ResolveRetryClaims`, which sets `RetryClaimHeld` for every retry-issued row. -2. `FailedMessageStream.ClaimHolders` streams the `FailedMessageRetries/` documents in id order through `RavenDocumentStream.ByPrefix`, and for each batch of claims reads the messages they name that are not retry-issued, whatever their status. Memory stays bounded by the batch size, however many claims are outstanding. It queries the collection by `id()`, which builds no index. Each row carries `RetryClaimHeld` as true. A claim whose message is gone makes no row. - -The cursor of the second phase is `claim-holder:` followed by the id of the last claim document walked. A cursor without the prefix, which is the end of the first phase, runs the first phase after it and then starts the second phase from the beginning. A prefixed cursor skips the first phase, so the document-existence check of the first phase does not see it. - -`FailedMessagesWriterBase` inserts a retry claim for every inserted row whose `RetryClaimHeld` is true. A retry-issued row with no value fails the batch, and any other status with no value means no claim. Each writer supplies its own `Count`: the offline writer counts retry-issued rows and rows with a claim, and the online writer counts unresolved rows. - -### The online unresolved copy - -`UnresolvedFailedMessagesReader` reads the `Unresolved` status through the same base class and attaches no claim state. `MigrationEngineOptions.FromSettings` always selects the category, and the engine runs it as optional: it throttles between batches, and an exception leaves it copying with `LastError` until the next start. The reader and writer need no window, because an unresolved message is copied however old it is. - -`RavenMigrationSource.ReadBody` reads the body attachment for both failed-message categories. `GroupComments` keeps `MustFollow` on the required category and never depends on the online category, so a required category never waits for work that cannot run until the host opens. - -### Body writes - -Every `IBodyStoragePersistence` backend writes a body only if none exists for the id: the file system moves a temporary file without overwrite, Azure Blob uploads with `IfNoneMatch`, and S3 checks for the object and then uploads with `IfNoneMatch = "*"`, treating a 412 precondition failure as an existing body. A migration write racing live ingestion therefore never replaces a stored body, and the first writer wins. That relies on one `UniqueMessageId` always carrying the same bytes. An S3-compatible store that ignores the header keeps the check-then-upload race. - For developer smoke tests, Debug builds honor `SERVICECONTROL_MIGRATION_ALLOW_UNRELEASED=true` in both `--start-data-migration` and normal startup. Release builds ignore this override. ```mermaid @@ -200,7 +169,7 @@ Windowing for the other two: `MigrationEngineOptions.WindowFor` supplies the win | Ordering | `MigrationCategory.MustFollow`, from `MigrationCategoryRegistry.All` | Data, not code. A follower whose predecessor is not `IsFinished` is upserted `Blocked` with `LastError` naming it | | Start or resume | Engine: `NotStarted`, `Blocked`, or any row with `LastError` moves to `InProgress`, cursor kept | `Halted` never resumes by itself; only `--migration-retry` resets it | | Batch size | `EFCoreMigrationTarget.BatchSizeFor` via the category's `IMigrationCategoryWriter` | Writers use `RowsPerStatementFor` from the dialect's parameter budget; `StoreKeyedInsert`-only writers use a fixed 500 | -| Read after cursor | `IMigrationCategoryReader`, usually via `RavenDocumentStream.ByPrefix`; `FailedMessageStatusReader` streams a query on `FailedMessageViewIndex` for its statuses ordered by `id()`, after waiting for the index to be up to date, and falls back to `ByPrefix` when the index is errored, disabled or holds fewer failed messages with a copied status than the collection does, and `FailedMessagesReader` then reads the claim holders, see [the offline cohort](#the-offline-cohort); the two licensing endpoint readers use `ThroughputDocuments.Endpoints`; single-document readers load by id | No-tracking session. A cursor whose document no longer exists is refused, because RavenDB would otherwise resume after the missing id and skip rows. The cursor advances on every document seen. Both paths resume on the same document id cursor | +| Read after cursor | `IMigrationCategoryReader`, usually via `RavenDocumentStream.ByPrefix`; `FailedMessageStatusReader` streams a query on `FailedMessageViewIndex` for its statuses ordered by `id()`, after waiting for the index to be up to date, and falls back to `ByPrefix` when the index is errored, disabled or holds fewer failed messages with a copied status than the collection does, and `FailedMessagesReader` then reads the claim holders, see [Readers and writers](#readers-and-writers); the two licensing endpoint readers use `ThroughputDocuments.Endpoints`; single-document readers load by id | No-tracking session. A cursor whose document no longer exists is refused, because RavenDB would otherwise resume after the missing id and skip rows. The cursor advances on every document seen. Both paths resume on the same document id cursor | | Fetch bodies | `MigrationBodyLoader` over `IMigrationSource.ReadBody` | Rows with a null body only, 8 concurrent, `MigrationEngine.MaxBodyReadAttempts` (3) with `MigrationEngineOptions.BodyRetryBackoff` (200 ms). `IsDefect` exceptions such as `NotSupportedException` are not retried and reach the batch catch. A missing attachment is skipped as `BodyMissing` after one attempt, a permanent fault no retry fixes, since ingestion writes an attachment for every failed message. Body skips stay off the checkpoint until the write commits | | Map documents to rows | `IMigrationCategoryWriter.Prepare`, returning `PreparedBatch` | Only the writer knows `NOT NULL` columns, key caps and what the product deletes anyway. Rows are inserted later inside the target transaction. Body-carrying writers write external bodies here, before the transaction, only for messages the target does not hold. A crash leaves orphan bodies, see [open decisions](ravendb-to-sql-migration-overview.md#what-the-checks-do-not-catch) | | Insert and checkpoint | `EFCoreMigrationTarget.Write`: `AccountForEveryRow`, then in one transaction under the execution strategy `PreparedBatch.Insert`, `AlreadyPresentIn`, `MigrationCheckpoint.Extend`, `MigrationCheckpointExtensions.UpsertCheckpoint` | Most writers use the dialect's `InsertMissing` built on `MigrationInsert`. Store-keyed rows use `StoreKeyedInsert`, because `MigrationInsert` rejects store-generated keys. Throughput days use `AddLicensingEndpointThroughput`. Each attempt clears the change tracker. Skips, merges and changes are logged after commit. `MigrationWriteResult.Merged` and `Changed` carry the batch's counts of each, which the engine adds up per category for the current run in `MigrationEngine.MergedAndChangedThisRun`; the checkpoint stores neither. See [progress and checkpoints](ravendb-to-sql-migration-overview.md#progress-and-checkpoints) | @@ -208,8 +177,12 @@ Windowing for the other two: `MigrationEngineOptions.WindowFor` supplies the win | Early stop | `HaltThreshold.Exceeded` on per-run counters | Fault skips (reasons not `IsBenign`, plus body skips) must exceed both `Migration/HaltThresholdPercent` and `Migration/HaltThresholdMinimum`. Counters are not persisted, so the threshold is per run | | Settle | `MigrationEngine.Settle`, after `IMigrationTarget.Count` at the end of the source | Saves the category's SQL row count on the row it settles, as `TargetCountAtSettle`. A halt saves without it, because the target may be what failed, and `--migration-abandon` reads it later. Logs before saving, because the checkpoint store shares the target database and a failed save would hide the cause | +`FailedMessagesWriterBase` inserts only messages the target lacks, so an existing SQL row always wins. Live ingestion can therefore replace RavenDB's earlier failure history with the attempt it stores. The overview describes the [accepted edge cases](ravendb-to-sql-migration-overview.md#writing-to-sql). + Failed message body placement lives in `FailedMessagesWriterBase`, shared by `FailedMessagesWriter` and `UnresolvedFailedMessagesWriter`, and by the archived and resolved writer when it exists. `MessageBodyClassifier.Classify` decides placement and `FailedMessageDocumentTranslator.SetBody` fills the columns: text up to `MaxBodySizeToStore` (100 KB default) inline; longer text inline prefix plus external copy; binary, invalid UTF-8 or NUL-containing text external only; empty bodies not stored. External writes go through `IBodyStoragePersistence.WriteBody`, which owns compression and the filesystem, Azure Blob or S3 backend, and complete before the transaction opens. `FailedErrorImportsWriter` places a failed error import's body by the SQL import store's own rule, inline up to `MaxBodySizeToStore` and otherwise external only, under `FailedErrorImportEntity.ExternalBodyId`, also before the transaction opens. +Every `IBodyStoragePersistence` backend writes a body only if none exists for the id: the file system moves a temporary file without overwrite, Azure Blob uploads with `IfNoneMatch`, and S3 uploads with `IfNoneMatch = "*"`, treating a 412 precondition failure as an existing body. The first writer wins, which relies on one `UniqueMessageId` always carrying the same bytes. An S3-compatible store that ignores the header keeps the check-then-upload race. + Invariants enforced across class boundaries: - `MigrationCheckpoint.Extend` throws unless the per-`MigrationSkipReason` counts sum exactly to the skipped count. `EFCoreMigrationTarget.AccountForEveryRow` throws unless prepared rows plus skips equal the batch size, catching rows dropped during preparation. `AlreadyPresentIn` throws when a writer over-reports. Every row is copied, skipped or already present. @@ -309,13 +282,14 @@ Every reader derives from `MigrationCategoryReader` and every writer | 9 | `LicensingThroughput` | Required | `LicensingThroughputReader`, `LicensingThroughputWriter` | Throughput database. Data link to `LicensingEndpoints`. The reader gives one batch per endpoint from `ThroughputDocuments.Endpoints` with a batch size of one, and ignores the requested batch size, because one endpoint can hold a year or more of days. Each batch's cursor is its endpoint's id. The writer keeps the last row of a day repeated for one source endpoint, compared ordinally. `AddLicensingEndpointThroughput` splits the batch into statements and adds each day's count to the stored one, summing endpoints whose names the key column treats as the same, in one batch or across batches. A re-read is not idempotent, so a resume relies on each batch committing with its checkpoint, and a retry deletes the copied rows first | | 10 | `LicensingReportMasks` | Required | `LicensingReportMasksReader`, `LicensingReportMasksWriter` | Throughput database. One `Settings` row via `SettingRowMapper` | | 11 | `LicensedEndpointDetails` | Required | `LicensedEndpointDetailsReader`, `LicensedEndpointDetailsWriter` | Throughput database. One `Settings` row via `SettingRowMapper` | -| 12 | `UnresolvedAndRetryIssuedFailedMessages` | Required | `FailedMessagesReader`, `FailedMessagesWriter` | Carries bodies. `FailedMessageStream` projection, `FailedMessageDocumentTranslator`. `FailedMessageStream.ResolveRetryClaims` looks up each batch's RavenDB retry claims in one request and gives every retry-issued row the `RetryClaimHeld` metadata key. The writer writes a claim only for a retry-issued message it inserts whose flag is true, so the claims mirror RavenDB's, and fails the batch when the flag is missing or not a bool | +| 12 | `UnresolvedAndRetryIssuedFailedMessages` | Required | `FailedMessagesReader`, `FailedMessagesWriter` | Carries bodies. `FailedMessageStream` projection, `FailedMessageDocumentTranslator`. `FailedMessagesReader` and `UnresolvedFailedMessagesReader` both derive from `FailedMessageStatusReader`, which streams the category's statuses in id order through `FailedMessageViewIndex` after waiting for the index, or through `RavenDocumentStream.ByPrefix` when the index cannot be trusted, with the document id as the cursor either way. `FailedMessagesReader` reads in two phases. Phase one is the retry-issued messages, where `FailedMessageStream.ResolveRetryClaims` looks up each batch's claims in one request and sets `RetryClaimHeld` on every row. Phase two is `FailedMessageStream.ClaimHolders`, which streams the `FailedMessageRetries/` documents by id prefix and, per batch of claims, queries the non-retry-issued messages they name by `id()`, with `RetryClaimHeld` true and no index built. A claim whose message is gone makes no row, and memory stays bounded by the batch size. The phase-two cursor is `claim-holder:` and the last claim id. A cursor without the prefix, which is the end of phase one, runs phase one after it and then starts phase two. A prefixed cursor skips phase one and its document-existence check. The writer, through `FailedMessagesWriterBase`, inserts a claim for every inserted row whose flag is true, so the claims mirror RavenDB's. A retry-issued row with no flag fails the batch, and any other status with no flag means no claim. The writer counts retry-issued rows and rows with a claim | | 13 | `PendingIntegrationEvents` | Required | `PendingIntegrationEventsReader`, `PendingIntegrationEventsWriter` | Store-keyed via `StoreKeyedInsert`. Deletes its rows on retry | | 14 | `CustomChecks` | Required | `CustomChecksReader`, `CustomChecksWriter` | The RavenDB reader flags a check whose id and category match one of the RavenDB persister's own six checks with the `SourceStorageCheck` metadata key, and the writer skips a flagged row as `SourceStorageCheck` without knowing any id. A RavenDB audit instance's dirty memory result reports under another category, so it is copied | | 15 | `FailedErrorImports` | Required | `FailedErrorImportsReader`, `FailedErrorImportsWriter` | Carries bodies inline in `Read`, never calls `ReadBody` | -| 16 | `GroupComments` | Required | `GroupCommentsReader`, `GroupCommentsWriter` | Ordered after `UnresolvedAndRetryIssuedFailedMessages`, not a data link. Skips blanks as `BlankGroupComment`. Reports group ids that differ only in trailing spaces as merges on SQL Server. Reports each copied comment on a group with no copied failed message as a change, because the retention sweep deletes it | -| 1 | `EventLog` | Optional | `EventLogReader`, `EventLogWriter` | Windowed, store-keyed, abandon-only. Skips as `PastRetention` | -| 2 | `ArchivedAndResolvedFailedMessages` | Optional | `ArchivedAndResolvedFailedMessagesReader`, `ArchivedAndResolvedFailedMessagesWriter` | Windowed, carries bodies, shares `FailedMessagesWriter` preparation. Skips as `PastRetention` | +| 16 | `GroupComments` | Required | `GroupCommentsReader`, `GroupCommentsWriter` | Ordered after `UnresolvedAndRetryIssuedFailedMessages`, not a data link, and never depends on an optional category, so a required category never waits for work that cannot run until the host opens. Skips blanks as `BlankGroupComment`. Reports group ids that differ only in trailing spaces as merges on SQL Server. Reports each copied comment on a group with no copied failed message as a change, because the retention sweep deletes it. For a group the background copy has not reached yet, that loss is an accepted edge case | +| 1 | `UnresolvedFailedMessages` | Optional | `UnresolvedFailedMessagesReader`, `UnresolvedFailedMessagesWriter` | Carries bodies, no window, and attaches no claim state. `MigrationEngineOptions.FromSettings` always selects it, and the engine runs it as optional: it throttles between batches, and an exception leaves it copying with `LastError` until the next start. `FailedMessagesWriterBase` inserts only messages the target lacks. The writer counts unresolved rows. `RavenMigrationSource.ReadBody` reads the body attachment for both failed-message categories | +| 2 | `EventLog` | Optional | `EventLogReader`, `EventLogWriter` | Windowed, store-keyed, abandon-only. Skips as `PastRetention` | +| 3 | `ArchivedAndResolvedFailedMessages` | Optional | `ArchivedAndResolvedFailedMessagesReader`, `ArchivedAndResolvedFailedMessagesWriter` | Windowed, carries bodies, shares `FailedMessagesWriter` preparation. Skips as `PastRetention` | ## Progress reporting From 47ebe60e97541ea1eef114482ab926826d831677 Mon Sep 17 00:00:00 2001 From: Rhys Bevilaqua Date: Mon, 23 Nov 2026 13:01:44 +0800 Subject: [PATCH 4/8] Move group comments to after failed messages --- .../ravendb-to-sql-migration-instructions.md | 4 +-- .../ravendb-to-sql-migration-overview.md | 14 +++++------ .../ravendb-to-sql-migration-system-design.md | 14 +++++------ .../GroupCommentsMigrationTests.cs | 25 +++++++++++++++++++ .../MigrationAcceptanceTest.cs | 20 +++++++++++++++ .../Writers/GroupCommentsWriter.cs | 11 ++++---- .../GroupCommentsMigrationTargetTests.cs | 8 +++--- .../MigrationCategoryRegistry.cs | 6 ++--- .../DataMigration/MigrationEngineOptions.cs | 9 ++++--- ...ategory_runs_in_a_fixed_order.approved.txt | 6 ++--- ...ional_categories_are_selected.approved.txt | 4 +-- .../MigrationCategoryRegistryTests.cs | 11 ++++---- .../Migration/MigrationEngineOptionsTests.cs | 10 ++++---- .../Migration/MigrationEngineOrderingTests.cs | 11 ++++---- 14 files changed, 99 insertions(+), 54 deletions(-) diff --git a/docs/migration/ravendb-to-sql-migration-instructions.md b/docs/migration/ravendb-to-sql-migration-instructions.md index 43cf0163e6..a5521ae450 100644 --- a/docs/migration/ravendb-to-sql-migration-instructions.md +++ b/docs/migration/ravendb-to-sql-migration-instructions.md @@ -9,7 +9,7 @@ The migration does not move the audit instance or the monitoring instance. The a ## How the migration runs -Run `--start-data-migration` with ServiceControl stopped to copy the data it needs to open. **This part is your outage**. After the command succeeds, start ServiceControl to copy the unresolved failed messages, the event log and the archived and resolved failed messages in the background. **The migration never writes to RavenDB**. +Run `--start-data-migration` with ServiceControl stopped to copy the data it needs to open. **This part is your outage**. After the command succeeds, start ServiceControl to copy the unresolved failed messages, the group comments, the event log and the archived and resolved failed messages in the background. **The migration never writes to RavenDB**. > [!IMPORTANT] > Until ServiceControl opens, you can go back to RavenDB and lose nothing. After it opens, you cannot go back. @@ -147,7 +147,7 @@ If you want less optional data, set a shorter window for the optional categories By default, the migration copies all the optional data that RavenDB still holds (current retension period setting). A window longer than the retention period copies nothing extra. After a category starts copying, you cannot change its window. To stop it early, abandon it. -The unresolved failed messages have no window and no setting turns them off. They copy once ServiceControl opens, however old they are. +The unresolved failed messages and the group comments have no window and no setting turns them off. They copy once ServiceControl opens, however old the unresolved messages are. Group comments appear in ServicePulse as their batches commit, after the unresolved failed messages category is Done or Abandoned. ## Step 4: Run the dry run diff --git a/docs/migration/ravendb-to-sql-migration-overview.md b/docs/migration/ravendb-to-sql-migration-overview.md index 3f6130d09b..cb71aa6dce 100644 --- a/docs/migration/ravendb-to-sql-migration-overview.md +++ b/docs/migration/ravendb-to-sql-migration-overview.md @@ -18,7 +18,7 @@ - **Retention does most of the work.** Error retention is 5 to 45 days and event retention defaults to 14 days, so most of RavenDB ages out on its own within weeks. - **What the instance needs to open is copied while it is closed.** This is the *required* data. Most of it is small. -- **Everything else is copied in the background while it runs.** This is the *optional* data: the unresolved failed messages, the event log and archived and resolved messages. +- **Everything else is copied in the background while it runs.** This is the *optional* data: the unresolved failed messages, the group comments, the event log and archived and resolved messages. - **Anything not copied ages out of RavenDB**, and the customer deletes the old database when ready. - **The failed messages copied during the outage are every retry-issued message, whether or not its retry claim remains, and every other failed message that still holds a retry claim.** Unresolved messages that hold no claim copy in the background, however old they are, so a big unresolved backlog does not lengthen the outage. - **A big archive does not make the outage longer, as long as RavenDB has indexed every failed message.** The copy finds retry-issued messages through a RavenDB index, and waits for it to catch up first. If the index is broken or incomplete, the copy reads every failed message instead and logs a warning. Then a big archive does make the outage longer. @@ -139,7 +139,7 @@ The copy goes from RavenDB, through the ServiceControl host, to SQL, because doc ### What data moves -There are nineteen *categories*: sixteen required and three optional. +There are nineteen *categories*: fifteen required and four optional. | Category | Kind | Why | | --- | --- | --- | @@ -158,8 +158,8 @@ There are nineteen *categories*: sixteen required and three optional. | Integration events still waiting to be sent | Required | Each is an event a subscriber has not received yet. There are only any if the old instance fell behind or could not reach the broker. They are sent once ServiceControl opens, later than they would have been, except a `MessageFailed` event for a message archived or resolved before the move (see [Rows removed after the copy](#rows-removed-after-the-copy)). RavenDB sends them in no particular order anyway, so no ordering is lost | | Custom checks, with their last status | Required | Not every check reports again. An endpoint that is down never does, and a check with no repeat interval reports only when its endpoint starts. Leaving one out could hide a known failure until that endpoint restarts. Results of the six checks the RavenDB persister runs about its own database, matched on id and category, are left out, as harmless skips | | Failed error imports, with bodies | Required | Messages taken off the error queue but not ingested. They exist nowhere else and never expire. After the move the "Error Message Ingestion" custom check keeps flagging them and `--import-failed-errors` imports them into SQL. Importing them on RavenDB first is better still | -| Group comments | Required | Copied after the required failed messages and never waiting for an optional category. Blank comments are not copied. A comment on a group whose messages have not reached SQL yet is removed shortly after ServiceControl opens, an accepted edge case, see [Rows removed after the copy](#rows-removed-after-the-copy) | | Unresolved failed messages, with bodies | Optional | Copied once ServiceControl has opened, whatever the age of the message, because an operator is waiting on each one. It has no window and no setting turns it off. A message that already holds a retry claim copied with the required category, so it counts as already present. An existing SQL row always wins, see [Writing to SQL](#writing-to-sql). Group retry, archive and unarchive act only on the messages copied so far, an accepted edge case | +| Group comments | Optional | Copied in the background right after the unresolved failed messages, before the event log and the archived and resolved messages. It has no window and no setting turns it off. A comment is missing from SQL until this copy reaches it. Blank comments are not copied. The next retention sweep removes a copied comment on a group with no failed message in SQL, see [Rows removed after the copy](#rows-removed-after-the-copy) | | Event log | Optional | The history behind the ServicePulse activity feed. On a busy instance it holds a row for every failure, retry and submission | | Archived and resolved failed messages, with bodies | Optional | The biggest category by far, and most of the copying time | @@ -178,7 +178,7 @@ There are nineteen *categories*: sixteen required and three optional. ### Optional data windows -- **Each windowed optional category copies only rows inside its *window*:** events raised within it, or messages archived or resolved within it. The unresolved failed messages have no window. The migration always selects that category and copies every unresolved message however old. +- **Each windowed optional category copies only rows inside its *window*:** events raised within it, or messages archived or resolved within it. The unresolved failed messages and the group comments have no window. The migration always selects both categories and copies every unresolved message however old. - **The window defaults to the instance's own retention period**: `ServiceControl/EventRetentionPeriod` for the event log, `ServiceControl/ErrorRetentionPeriod` for archived and resolved messages. So by default everything RavenDB still holds is copied. - **`0` turns a category off before it starts.** A longer window than the retention period copies nothing extra, because older rows would only be deleted by SQL's clean-up. - **The window counts back from when the category first started copying**, which the checkpoint records, so a restart copies the same window rather than one that has slid forward. @@ -384,7 +384,7 @@ Stop ServiceControl before either command. In a container, run it as a one-off ` - **A required category can be abandoned once it has started.** One that never ran cannot, so nothing required is given up blind. If RavenDB is gone first, point back or start over. - **An optional category can be abandoned at any time.** That is how to stop a big optional copy short, or get out when RavenDB is gone. -- **Abandoning cascades** to throughput history (from licensing endpoint records) and endpoint settings (from known endpoints), and the command names each. Group comments are not abandoned with unresolved failed messages. +- **Abandoning cascades** to throughput history (from licensing endpoint records) and endpoint settings (from known endpoints), and the command names each. Group comments are not abandoned with unresolved failed messages, but they stay Copying until the unresolved failed messages are Done or Abandoned. - **A category that failed only on [skips no retry can fix](#skipped-rows) should be abandoned, not retried.** If it stopped early at the threshold, raise the threshold and retry first, because abandoning gives up every unread row too. > [!IMPORTANT] @@ -501,7 +501,7 @@ The dry run counts the subscription and endpoint settings merges before anything ### Rows removed after the copy -- **A group comment on a group that has no failed message in SQL is removed soon after ServiceControl opens**, because the retention sweep deletes a comment whose group has no row. The group has none when its messages are unresolved without a retry claim and the background copy has not reached them, when they are archived or resolved, when they are gone from RavenDB, or when the copy skipped them. The copy names each such comment, in a warning starting `Changed GroupComment/ in category GroupComments on the way into SQL: The group '' has no copied failed message`, which also quotes the whole comment. This loss is accepted for groups the background copy has not reached yet. +- **The next retention sweep removes a copied group comment whose group has no failed message in SQL**, because the sweep deletes a comment whose group has no row. The group has none when its messages are archived or resolved, when they are gone from RavenDB, or when the copy skipped them. Comments wait until the unresolved failed messages category is Done or Abandoned, so waiting for that copy alone does not cause comment loss. Abandoning it can leave groups without messages in SQL. Nothing keeps a comment on a group with only archived or resolved messages, even when those messages copy later. The copy names each such comment, in a warning starting `Changed GroupComment/ in category GroupComments on the way into SQL: The group '' has no copied failed message`, which also quotes the whole comment. - **A `MessageFailed` event still waiting to be sent for a message archived or resolved before the move is never sent.** ServiceControl looks the message up when it sends the event. Archived and resolved messages arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. The same lookup can fail for an unresolved message the background copy has not reached yet, an accepted edge case. - **Unresolved messages from version 6.18 or earlier can expire during the copy.** On those versions, a message that failed again after being archived or resolved kept its expiry. One deleted before the copy reads it is not copied and not counted. No log line names these messages yet. @@ -523,7 +523,7 @@ The dry run counts the subscription and endpoint settings merges before anything - **Category:** one kind of data copied as a unit, such as known endpoints or the event log. - **Required category:** one ServiceControl needs the moment it opens, so it copies while ServiceControl is closed. -- **Optional category:** one copied in the background after ServiceControl opens. An optional category other than the unresolved failed messages can be shortened or turned off before it starts. +- **Optional category:** one copied in the background after ServiceControl opens. An optional category other than the unresolved failed messages and the group comments can be shortened or turned off before it starts. - **Window:** how far back an optional category copies. - **Closed period:** the time ServiceControl is not serving traffic because the required copy is running. - **Checkpoint:** the row in SQL recording one category's progress, saved in the same transaction as the rows it describes. diff --git a/docs/migration/ravendb-to-sql-migration-system-design.md b/docs/migration/ravendb-to-sql-migration-system-design.md index e1fee92666..b70560f080 100644 --- a/docs/migration/ravendb-to-sql-migration-system-design.md +++ b/docs/migration/ravendb-to-sql-migration-system-design.md @@ -144,12 +144,12 @@ Each position in the sequence is deliberate: `OptionalCategoryCopier` is a `BackgroundService` registered by `RunCommand` only on the main instance. It waits for `ApplicationStarted`, then reads checkpoints. With no unfinished optional rows, it exits without creating a source or opening a target. -- It runs the selected, supported optional categories in registry order, `UnresolvedFailedMessages`, `EventLog` then `ArchivedAndResolvedFailedMessages`, skipping rows that are `IsFinished` or `IsFailed`. The offline command has already seeded the rows that identify the online work. +- It runs the selected, supported optional categories in registry order, `UnresolvedFailedMessages`, `GroupComments`, `EventLog` then `ArchivedAndResolvedFailedMessages`, skipping rows that are `IsFinished` or `IsFailed`. The offline command has already seeded the rows that identify the online work. - It creates and opens its own source only when a supported category is still copying. On failure it calls `RecordSourceOutage` and exits for this start. It does not depend on in-process state from `--start-data-migration`, which ran in another process. - It reruns source-contributed checks before copying. The RavenDB unfinished-operation check should still pass because the source is read-only and new operations run on SQL. A refusal stops the online copy for that start, not ServiceControl. - An exception leaves the category `InProgress` with `LastError`, untouched until the next start, so a transient failure never forces an abandon. -`UnresolvedFailedMessages` has no window. `MigrationEngineOptions.FromSettings` selects it even when both windows are zero. +`UnresolvedFailedMessages` and `GroupComments` have no window. `MigrationEngineOptions.FromSettings` selects both even when both windows are zero, so the offline command seeds their checkpoint rows and the online copier runs them. Windowing for the other two: `MigrationEngineOptions.WindowFor` supplies the window and the reader applies it through `IWindowedMigrationCategoryReader`. The engine writes `StartedWindowSeconds` once, on the first `InProgress` upsert. `CopiesFrom` is `StartedAt` minus the currently configured window, so window stability across restarts depends entirely on `OptionalCategoryWindowsAreUnchangedCheck`. Verify counts from `StartedAt` minus `StartedWindowSeconds` instead, which is the same window while the category copies, because nothing stops the setting changing once it has finished. @@ -188,7 +188,7 @@ Invariants enforced across class boundaries: - `MigrationCheckpoint.Extend` throws unless the per-`MigrationSkipReason` counts sum exactly to the skipped count. `EFCoreMigrationTarget.AccountForEveryRow` throws unless prepared rows plus skips equal the batch size, catching rows dropped during preparation. `AlreadyPresentIn` throws when a writer over-reports. Every row is copied, skipped or already present. - `MigrationSkipReasonExtensions.IsBenign` is fixed: `PastRetention`, `BlankGroupComment`, `SourceStorageCheck`. Everything else is a fault, `Unknown` included. - `PreparedBatch.Merges` reports rows whose key collides with another under the target's collation. Writers with a text key detect them with `KeyMerges.In` or `KeyMerges.Split`, which compare with `IMigrationSqlDialect.KeyComparer` against the batch and the existing table. `FailedErrorImportsWriter`, whose key is a GUID, reports a second import of the same message in the batch, and one the table holds with another failure time. `EFCoreMigrationTarget` logs one warning per merge naming both keys, the source row kept, or the stored row, and the source row dropped. The dropped row counts as already present, and also in `MigrationWriteResult.Merged`, so the category summary `MigrationStartup.ReportWhatTheCopyLeftBehind` logs shows merges apart from the rest of already present. Every writer whose key can fold two source rows into one reports them here, or the summary understates merges. -- `PreparedBatch.Changes` reports rows the insert adds whose stored value differs from the source's on purpose: a failed message's cut message type, dropped or blank failure groups, and failure groups one key folds together; a failed error import's missing exception or body stored empty; and a group comment on a group with no copied failed message, which the retention sweep deletes. A failed message keeping only its newest processing attempt is not a change, because every row does that by design. Writers report only rows the target does not already hold, so a re-read reports nothing twice. `EFCoreMigrationTarget` logs one warning per change after the commit, and `MigrationWriteResult.Changed` counts them. A process that stops between the commit and the log loses that batch's lines for good. `MigrationStartup.ReportWhatTheCopyLeftBehind` warns, naming both counts, when a category's merged count exceeds its already-present count, because the key comparer then disagrees with the database. `PreparedBatch.RenumberedRows` names rows the database gives new ids, and the target logs it once per copy of a category, on the batch whose checkpoint has copied nothing yet. +- `PreparedBatch.Changes` reports rows the insert adds whose stored value differs from the source's on purpose: a failed message's cut message type, dropped or blank failure groups, and failure groups one key folds together; a failed error import's missing exception or body stored empty; and a group comment on a group with no copied failed message. A failed message keeping only its newest processing attempt is not a change, because every row does that by design. Writers report only rows the target does not already hold, so a re-read reports nothing twice. `EFCoreMigrationTarget` logs one warning per change after the commit, and `MigrationWriteResult.Changed` counts them. A process that stops between the commit and the log loses that batch's lines for good. `MigrationStartup.ReportWhatTheCopyLeftBehind` warns, naming both counts, when a category's merged count exceeds its already-present count, because the key comparer then disagrees with the database. `PreparedBatch.RenumberedRows` names rows the database gives new ids, and the target logs it once per copy of a category, on the batch whose checkpoint has copied nothing yet. - `TargetCountAtSettle` is read with `IMigrationTarget.Count` just before the save that settles a category at the end of the source, or that abandons it, and is never updated afterwards. For a required category it is exactly what the copy left in SQL: the host is closed, `TargetHoldsNoServiceControlDataCheck` started the target empty, and a second copier would move the row's version so the settle save is refused. That is why the count needs no shared transaction with the save. For an optional category it also includes what ServiceControl wrote while the copy ran. ## Category states @@ -248,7 +248,7 @@ Every migration command except the source report builds a host with `AddPersiste All three are required categories, and `CanBeRetried` takes a required category only when it is Failed, which keeps the host closed, so the host has not opened and the target started empty. `ResetForRetry` also checks this itself: inside the reset transaction it reads the `Migration/HostOpenedOnTarget` setting, and when it is set it refuses a reset that would delete, naming the category, and deletes and saves nothing. A reset that deletes nothing, such as an optional category's, is not refused. `EventLog` is also store-keyed but copies after the host opens, when its rows are indistinguishable from live ones, so it is abandon-only. -`--migration-abandon` first re-saves the named row unchanged, when it exists, to bump its version and fence off a concurrent copy. It then abandons dependants along `MigrationCategory.MustFollowIsDataLink` (`EndpointSettings` after `KnownEndpoints`, `LicensingThroughput` after `LicensingEndpoints`), deepest first, skipping `Complete` and `Abandoned` dependants and inserting `Abandoned` rows for dependants without one, and saves the named row last. `GroupComments` follows the unresolved failed messages for ordering only and is not cascaded. An optional category with no row gets a new `Abandoned` row. Every row it saves `Abandoned` carries `TargetCountAtSettle`, read just before the save, as a settle does. No domain event is raised, because ServiceControl is stopped. +`--migration-abandon` first re-saves the named row unchanged, when it exists, to bump its version and fence off a concurrent copy. It then abandons dependants along `MigrationCategory.MustFollowIsDataLink` (`EndpointSettings` after `KnownEndpoints`, `LicensingThroughput` after `LicensingEndpoints`), deepest first, skipping `Complete` and `Abandoned` dependants and inserting `Abandoned` rows for dependants without one, and saves the named row last. `GroupComments` follows `UnresolvedFailedMessages` for ordering only and is not cascaded. An optional category with no row gets a new `Abandoned` row. Every row it saves `Abandoned` carries `TargetCountAtSettle`, read just before the save, as a settle does. No domain event is raised, because ServiceControl is stopped. Both operator commands rely on `MigrationCheckpointConflictException` to detect a race with a running copy. @@ -286,10 +286,10 @@ Every reader derives from `MigrationCategoryReader` and every writer | 13 | `PendingIntegrationEvents` | Required | `PendingIntegrationEventsReader`, `PendingIntegrationEventsWriter` | Store-keyed via `StoreKeyedInsert`. Deletes its rows on retry | | 14 | `CustomChecks` | Required | `CustomChecksReader`, `CustomChecksWriter` | The RavenDB reader flags a check whose id and category match one of the RavenDB persister's own six checks with the `SourceStorageCheck` metadata key, and the writer skips a flagged row as `SourceStorageCheck` without knowing any id. A RavenDB audit instance's dirty memory result reports under another category, so it is copied | | 15 | `FailedErrorImports` | Required | `FailedErrorImportsReader`, `FailedErrorImportsWriter` | Carries bodies inline in `Read`, never calls `ReadBody` | -| 16 | `GroupComments` | Required | `GroupCommentsReader`, `GroupCommentsWriter` | Ordered after `UnresolvedAndRetryIssuedFailedMessages`, not a data link, and never depends on an optional category, so a required category never waits for work that cannot run until the host opens. Skips blanks as `BlankGroupComment`. Reports group ids that differ only in trailing spaces as merges on SQL Server. Reports each copied comment on a group with no copied failed message as a change, because the retention sweep deletes it. For a group the background copy has not reached yet, that loss is an accepted edge case | | 1 | `UnresolvedFailedMessages` | Optional | `UnresolvedFailedMessagesReader`, `UnresolvedFailedMessagesWriter` | Carries bodies, no window, and attaches no claim state. `MigrationEngineOptions.FromSettings` always selects it, and the engine runs it as optional: it throttles between batches, and an exception leaves it copying with `LastError` until the next start. `FailedMessagesWriterBase` inserts only messages the target lacks. The writer counts unresolved rows. `RavenMigrationSource.ReadBody` reads the body attachment for both failed-message categories | -| 2 | `EventLog` | Optional | `EventLogReader`, `EventLogWriter` | Windowed, store-keyed, abandon-only. Skips as `PastRetention` | -| 3 | `ArchivedAndResolvedFailedMessages` | Optional | `ArchivedAndResolvedFailedMessagesReader`, `ArchivedAndResolvedFailedMessagesWriter` | Windowed, carries bodies, shares `FailedMessagesWriter` preparation. Skips as `PastRetention` | +| 2 | `GroupComments` | Optional | `GroupCommentsReader`, `GroupCommentsWriter` | Unwindowed and always selected, with `MustFollow` set to `UnresolvedFailedMessages`, not a data link, so the retention sweep does not find a comment before the unresolved messages of its group. The engine blocks it until that category is `IsFinished`. A comment is absent from SQL until this copy reaches it. Skips blanks as `BlankGroupComment`. Reports group ids that differ only in trailing spaces as merges on SQL Server. Reports each copied comment on a group with no copied failed message as a change. Abandoning the unresolved failed messages copy can leave such groups. Nothing protects a comment on a group with only archived or resolved messages | +| 3 | `EventLog` | Optional | `EventLogReader`, `EventLogWriter` | Windowed, store-keyed, abandon-only. Skips as `PastRetention` | +| 4 | `ArchivedAndResolvedFailedMessages` | Optional | `ArchivedAndResolvedFailedMessagesReader`, `ArchivedAndResolvedFailedMessagesWriter` | Windowed, carries bodies, shares `FailedMessagesWriter` preparation. Skips as `PastRetention` | ## Progress reporting diff --git a/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs b/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs index 64ae3590af..2570006af7 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs @@ -4,6 +4,7 @@ namespace ServiceControl.Migration.AcceptanceTests; using System.Linq; using System.Threading.Tasks; using NUnit.Framework; +using ServiceControl.Hosting.Commands; using ServiceControl.MessageFailures; using ServiceControl.Persistence.DataMigration; using ServiceControl.Persistence.Infrastructure; @@ -46,6 +47,30 @@ await SeedSourceGroupComments( } } + [Test] + public async Task A_comment_on_an_unresolved_group_survives_a_retention_sweep_between_the_offline_and_online_copies() + { + var unresolved = new IngestedFailure(); + var groupId = unresolved.Groups.Single().Id; + await SeedSourceWithBodies((unresolved.ToFailedMessage(), unresolved.Body, unresolved.ContentType)); + await SeedSourceGroupComments((groupId, "Waiting on the payments team")); + MigrationSettings(); + + await MigrateCommand.Run(Settings, AllowingAnUnreleasedMigration()); + + // The sweep that runs once ServiceControl opens, before the background copy has reached the unresolved message. + await RunRetentionSweep(); + await RunHostUntilTheApiAnswers(); + await WaitUntil( + async () => (await ReadCheckpoint(MigrationCategoryIds.GroupComments))?.State.IsFinished() == true, + "the group comments copy finished", + TimeSpan.FromMinutes(1)); + await RunRetentionSweep(); + + var comments = (await GroupsStore.GetUnresolvedGroupsByClassifier(unresolved.Groups.Single().Type, null, new PagingInfo())).ToDictionary(view => view.Id, view => view.Comment); + Assert.That(comments[groupId], Is.EqualTo("Waiting on the payments team")); + } + // The prefix is retyped because this project cannot reference the RavenDB persister that declares it. Task SeedSourceGroupComments(params (string GroupId, string Comment)[] comments) => SeedSource([.. comments.Select(comment => ($"GroupComment/{comment.GroupId}", (object)new GroupComment { Id = $"GroupComment/{comment.GroupId}", Comment = comment.Comment }))]); diff --git a/src/ServiceControl.Migration.AcceptanceTests/MigrationAcceptanceTest.cs b/src/ServiceControl.Migration.AcceptanceTests/MigrationAcceptanceTest.cs index 35590023e8..e820f441ba 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/MigrationAcceptanceTest.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/MigrationAcceptanceTest.cs @@ -117,9 +117,11 @@ public async Task SetUp() var targetServiceCollection = new ServiceCollection(); targetServiceCollection.AddLogging(); + targetServiceCollection.AddMetrics(); targetServiceCollection.AddPersistence(Settings); targetServiceCollection.AddSingleton(RecordedBodies); targetServiceCollection.AddSingleton(new NServiceBus.CriticalError((_, _) => Task.CompletedTask)); + targetServiceCollection.AddSingleton(); targetServices = targetServiceCollection.BuildServiceProvider(); Target = targetServices.GetRequiredService(); @@ -588,6 +590,24 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except protected ExternalIntegrationRequestsDataStore ExternalIntegrationStore => targetServices.GetRequiredService(); protected ICustomChecksDataStore CustomChecks => targetServices.GetRequiredService(); protected IFailedErrorImportDataStore FailedImportStore => targetServices.GetRequiredService(); + // Resolved from the target services, whose hosted sweeper never starts, so a test decides when a sweep runs. + protected async Task RunRetentionSweep() + { + var sweeper = targetServices.GetRequiredService(); + var attempt = sweeper.TryStartManualSweep(null, null); + Assert.That(attempt.Outcome, Is.EqualTo(RetentionSweepStatus.Started)); + await WaitUntil(() => Task.FromResult(!sweeper.GetStatus().IsRunning), "the retention sweep finished", TimeSpan.FromMinutes(1)); + Assert.That(sweeper.GetStatus().LastOutcome, Is.EqualTo(RetentionSweepOutcome.Succeeded)); + } + + sealed class UnstartedApplicationLifetime : IHostApplicationLifetime + { + public CancellationToken ApplicationStarted => CancellationToken.None; + public CancellationToken ApplicationStopping => CancellationToken.None; + public CancellationToken ApplicationStopped => CancellationToken.None; + public void StopApplication() { } + } + protected IGroupsDataStore GroupsStore => targetServices.GetRequiredService(); // Built by hand, because its DI constructor needs NServiceBus settings the target services do not have. diff --git a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/GroupCommentsWriter.cs b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/GroupCommentsWriter.cs index 39a8c4788e..db811e4a2d 100644 --- a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/GroupCommentsWriter.cs +++ b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/GroupCommentsWriter.cs @@ -11,7 +11,7 @@ namespace ServiceControl.Persistence.EFCore.DataMigration.Writers; /// Copies the group comments, one row per comment keyed on its group id, and never replaces a comment the target already holds. /// A null or blank comment is skipped as BlankGroupComment, because the product keeps no row for one. /// A comment whose group id the key column treats as another's is reported as a merge. -/// A copied comment on a group the target holds no failed message in is reported as a change, because the retention sweep deletes it once ServiceControl opens. +/// A copied comment whose group has no failed message in the target is reported as a change. /// sealed class GroupCommentsWriter(IMigrationSqlDialect migrationDialect) : MigrationCategoryWriter { @@ -55,12 +55,11 @@ protected override async Task PrepareDocuments(ServiceControlDbCo skips, merges) { - Changes = await CommentsTheSweepWillDelete(dbContext, inserted, cancellationToken) + Changes = await CommentsWithoutCopiedMessages(dbContext, inserted, cancellationToken) }; } - // Group comments copy after the required failed messages. The unresolved messages without a retry claim arrive after ServiceControl opens, so a group that only they belong to is missing here. - async Task> CommentsTheSweepWillDelete(ServiceControlDbContext dbContext, IReadOnlyList<(string SourceId, GroupCommentEntity Row)> inserted, CancellationToken cancellationToken) + async Task> CommentsWithoutCopiedMessages(ServiceControlDbContext dbContext, IReadOnlyList<(string SourceId, GroupCommentEntity Row)> inserted, CancellationToken cancellationToken) { string[] groupIds = [.. inserted.Select(comment => comment.Row.GroupId).Distinct(StringComparer.Ordinal)]; var held = (await dbContext.FailedMessageGroups @@ -72,8 +71,8 @@ protected override async Task PrepareDocuments(ServiceControlDbCo return [.. inserted .Where(comment => !held.Contains(comment.Row.GroupId)) - .Select(comment => (comment.SourceId, $"The group '{comment.Row.GroupId}' has no copied failed message, so the retention sweep removes this comment shortly after ServiceControl opens. " + - "Its messages have not been copied yet, are archived or resolved, are gone from the source, or were skipped by the copy. " + + .Select(comment => (comment.SourceId, $"The group '{comment.Row.GroupId}' has no copied failed message. " + + "Its messages may be archived or resolved, gone from the source, skipped by the copy, or not reached before the unresolved failed messages copy was abandoned. " + $"The comment was: '{comment.Row.Comment}'"))]; } } diff --git a/src/ServiceControl.Persistence.Tests/EFCore/Migration/GroupCommentsMigrationTargetTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/Migration/GroupCommentsMigrationTargetTests.cs index d932cbc893..84d449fc71 100644 --- a/src/ServiceControl.Persistence.Tests/EFCore/Migration/GroupCommentsMigrationTargetTests.cs +++ b/src/ServiceControl.Persistence.Tests/EFCore/Migration/GroupCommentsMigrationTargetTests.cs @@ -81,7 +81,7 @@ public async Task A_comment_whose_group_id_the_key_treats_as_an_earlier_ones_is_ } [Test] - public async Task A_comment_on_a_group_with_no_copied_message_is_reported_as_a_change_the_sweep_will_delete() + public async Task A_comment_on_a_group_with_no_copied_message_is_reported_as_a_change() { var failure = new IngestedFailure(); var group = failure.Groups.Single(); @@ -98,10 +98,10 @@ public async Task A_comment_on_a_group_with_no_copied_message_is_reported_as_a_c Assert.That(changes, Is.EqualTo(new[] { ("GroupComment/group-of-archived-messages", - "The group 'group-of-archived-messages' has no copied failed message, so the retention sweep removes this comment shortly after ServiceControl opens. " + - "Its messages have not been copied yet, are archived or resolved, are gone from the source, or were skipped by the copy. " + + "The group 'group-of-archived-messages' has no copied failed message. " + + "Its messages may be archived or resolved, gone from the source, skipped by the copy, or not reached before the unresolved failed messages copy was abandoned. " + $"The comment was: '{longComment}'") - }), "a comment on a group with a copied message stays, so only the other is named, with the whole comment because the sweep deletes it"); + }), "only the comment whose group has no copied message is reported"); Assert.That((first.Copied, first.Changed), Is.EqualTo((2, 1))); Assert.That(again.Changed, Is.Zero, "a re-read finds the comment already present, and this write copied nothing"); } diff --git a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs index 4f3e0492d8..f44deec4b1 100644 --- a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs +++ b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs @@ -47,12 +47,12 @@ public static class MigrationCategoryRegistry new(MigrationCategoryIds.PendingIntegrationEvents, MigrationCategoryKind.Required, CarriesBodies: false, Order: 13), new(MigrationCategoryIds.CustomChecks, MigrationCategoryKind.Required, CarriesBodies: false, Order: 14), new(MigrationCategoryIds.FailedErrorImports, MigrationCategoryKind.Required, CarriesBodies: true, Order: 15), - new(MigrationCategoryIds.GroupComments, MigrationCategoryKind.Required, CarriesBodies: false, Order: 16, MustFollow: MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages), // Optional, copied in the background once the host is open. new(MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryKind.Optional, CarriesBodies: true, Order: 1), - new(MigrationCategoryIds.EventLog, MigrationCategoryKind.Optional, CarriesBodies: false, Order: 2), - new(MigrationCategoryIds.ArchivedAndResolvedFailedMessages, MigrationCategoryKind.Optional, CarriesBodies: true, Order: 3), + new(MigrationCategoryIds.GroupComments, MigrationCategoryKind.Optional, CarriesBodies: false, Order: 2, MustFollow: MigrationCategoryIds.UnresolvedFailedMessages), + new(MigrationCategoryIds.EventLog, MigrationCategoryKind.Optional, CarriesBodies: false, Order: 3), + new(MigrationCategoryIds.ArchivedAndResolvedFailedMessages, MigrationCategoryKind.Optional, CarriesBodies: true, Order: 4), ]; public static MigrationCategory? Find(string id) => All.FirstOrDefault(c => c.Id == id); diff --git a/src/ServiceControl.Persistence/DataMigration/MigrationEngineOptions.cs b/src/ServiceControl.Persistence/DataMigration/MigrationEngineOptions.cs index 5651af3531..d2d33f4086 100644 --- a/src/ServiceControl.Persistence/DataMigration/MigrationEngineOptions.cs +++ b/src/ServiceControl.Persistence/DataMigration/MigrationEngineOptions.cs @@ -11,7 +11,7 @@ namespace ServiceControl.Persistence.DataMigration; /// How long to wait between batches of an optional category, which is what keeps the background copy off the instance's back. Zero waits not at all. /// The share of skipped rows, as a percentage, that stops a category. /// The number of skipped rows that has to be passed before the percentage counts, so a handful of bad rows in a small category is not a halt. -/// The optional categories to copy. An optional category outside this list is never copied. The unresolved failed messages are always in it. +/// The optional categories to copy. An optional category outside this list is never copied. The unresolved failed messages and the group comments are always in it. public sealed record MigrationEngineOptions( TimeSpan ThrottlePause, int HaltThresholdPercent, @@ -37,7 +37,7 @@ public sealed record MigrationEngineOptions( /// /// Reads the options from the instance's settings. Anything not set takes the default from /// , except the two windows, which default to the retention periods passed in. - /// A windowed optional category is selected when its window is greater than zero, and the unresolved failed messages are always selected. + /// A windowed optional category is selected when its window is greater than zero, and the unresolved failed messages and the group comments are always selected. /// /// The settings root the migration keys are read under. /// The instance's event retention period, used as the event log window when none is set. @@ -51,8 +51,9 @@ public static MigrationEngineOptions FromSettings(SettingsRootNamespace settings var eventLogWindow = ReadWindow(settingsRootNamespace, MigrationSettings.EventLogWindowKey, eventRetentionPeriod); var archivedAndResolvedWindow = ReadWindow(settingsRootNamespace, MigrationSettings.ArchivedAndResolvedFailedMessagesWindowKey, errorRetentionPeriod); - // Not windowed: an unresolved message is waiting on somebody however old it is. - var selectedIds = new List { MigrationCategoryIds.UnresolvedFailedMessages }; + // Not windowed: an unresolved message is waiting on somebody however old it is. The comments follow it so + // the retention sweep does not delete a comment whose group's messages have not arrived. + var selectedIds = new List { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.GroupComments }; if (eventLogWindow > TimeSpan.Zero) { diff --git a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt index 563a77e86d..38cd699c24 100644 --- a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt +++ b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt @@ -13,7 +13,7 @@ Required 12: UnresolvedAndRetryIssuedFailedMessages, with bodies Required 13: PendingIntegrationEvents Required 14: CustomChecks Required 15: FailedErrorImports, with bodies -Required 16: GroupComments, after UnresolvedAndRetryIssuedFailedMessages Optional 1: UnresolvedFailedMessages, with bodies -Optional 2: EventLog -Optional 3: ArchivedAndResolvedFailedMessages, with bodies \ No newline at end of file +Optional 2: GroupComments, after UnresolvedFailedMessages +Optional 3: EventLog +Optional 4: ArchivedAndResolvedFailedMessages, with bodies \ No newline at end of file diff --git a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Only_configured_optional_categories_are_selected.approved.txt b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Only_configured_optional_categories_are_selected.approved.txt index 3338eba16c..9f23b51323 100644 --- a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Only_configured_optional_categories_are_selected.approved.txt +++ b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Only_configured_optional_categories_are_selected.approved.txt @@ -1,4 +1,4 @@ Configured: EventLog Runs as: -Optional 2: EventLog -Never copied: UnresolvedFailedMessages, ArchivedAndResolvedFailedMessages \ No newline at end of file +Optional 3: EventLog +Never copied: UnresolvedFailedMessages, GroupComments, ArchivedAndResolvedFailedMessages \ No newline at end of file diff --git a/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs index cf786152dc..bb83981b93 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs @@ -36,7 +36,7 @@ public void Required_categories_are_ordered_exactly_as_the_contract_lists_them() "NotificationSettings", "TrialEndDate", "RetryOperations", "LicensingEndpoints", "LicensingThroughput", "LicensingReportMasks", "LicensedEndpointDetails", "UnresolvedAndRetryIssuedFailedMessages", - "PendingIntegrationEvents", "CustomChecks", "FailedErrorImports", "GroupComments" + "PendingIntegrationEvents", "CustomChecks", "FailedErrorImports" })); } @@ -51,7 +51,7 @@ public void Optional_categories_are_ordered_exactly_as_the_contract_lists_them() Assert.That(optionalIds, Is.EqualTo(new[] { - "UnresolvedFailedMessages", "EventLog", "ArchivedAndResolvedFailedMessages" + "UnresolvedFailedMessages", "GroupComments", "EventLog", "ArchivedAndResolvedFailedMessages" })); } @@ -71,7 +71,7 @@ public void Three_categories_declare_a_MustFollow() ["EndpointSettings"] = "KnownEndpoints", // A group comment whose failed messages have not arrived reads as an orphan to the // retention sweeper, which deletes it. - ["GroupComments"] = "UnresolvedAndRetryIssuedFailedMessages" + ["GroupComments"] = "UnresolvedFailedMessages" })); } @@ -84,12 +84,13 @@ public void The_three_failed_message_categories_and_failed_imports_carry_bodies( } [Test] - public void The_unresolved_failed_messages_are_copied_after_the_host_opens_and_the_group_comments_do_not_wait_for_them() + public void The_group_comments_are_copied_after_the_host_opens_and_after_the_unresolved_failed_messages() { using (Assert.EnterMultipleScope()) { Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedFailedMessages)?.Kind, Is.EqualTo(MigrationCategoryKind.Optional)); - Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.GroupComments)?.MustFollow, Is.EqualTo(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)); + Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.GroupComments)?.Kind, Is.EqualTo(MigrationCategoryKind.Optional)); + Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.GroupComments)?.MustFollow, Is.EqualTo(MigrationCategoryIds.UnresolvedFailedMessages)); Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)?.Kind, Is.EqualTo(MigrationCategoryKind.Required)); } } diff --git a/src/ServiceControl.UnitTests/Migration/MigrationEngineOptionsTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationEngineOptionsTests.cs index 34ee9c7d63..ac98f0bfed 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationEngineOptionsTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationEngineOptionsTests.cs @@ -35,7 +35,7 @@ public void Defaults_match_the_contract_when_nothing_is_configured() Assert.That(options.ThrottlePause, Is.EqualTo(TimeSpan.FromMilliseconds(100))); Assert.That(options.HaltThresholdPercent, Is.EqualTo(5)); Assert.That(options.HaltThresholdMinimum, Is.EqualTo(100)); - Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.EventLog, MigrationCategoryIds.ArchivedAndResolvedFailedMessages }), "both windowed optional categories are copied unless turned off, and the unresolved failed messages always are"); + Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.GroupComments, MigrationCategoryIds.EventLog, MigrationCategoryIds.ArchivedAndResolvedFailedMessages }), "both windowed optional categories are copied unless turned off, and the unresolved failed messages and the group comments always are"); Assert.That(options.EventLogWindow, Is.EqualTo(EventRetention), "the event log window defaults to the event retention period"); Assert.That(options.ArchivedAndResolvedFailedMessagesWindow, Is.EqualTo(ErrorRetention), "the archived and resolved window defaults to the error retention period"); Assert.That(options.BodyRetryBackoff, Is.EqualTo(TimeSpan.FromMilliseconds(200))); @@ -60,7 +60,7 @@ public void Reads_configured_values_from_environment_variables() Assert.That(options.HaltThresholdMinimum, Is.EqualTo(50)); Assert.That(options.EventLogWindow, Is.EqualTo(TimeSpan.FromDays(2))); Assert.That(options.ArchivedAndResolvedFailedMessagesWindow, Is.EqualTo(TimeSpan.FromDays(5.5))); - Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.EventLog, MigrationCategoryIds.ArchivedAndResolvedFailedMessages })); + Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.GroupComments, MigrationCategoryIds.EventLog, MigrationCategoryIds.ArchivedAndResolvedFailedMessages })); } } @@ -72,16 +72,16 @@ public void A_zero_window_turns_its_category_off(string variable, string stillSe var options = Read(); - Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, stillSelected })); + Assert.That(options.SelectedOptionalCategoryIds, Is.EquivalentTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.GroupComments, stillSelected })); } [Test] - public void The_unresolved_failed_messages_are_selected_when_both_windows_are_zero() + public void The_unresolved_failed_messages_and_the_group_comments_are_selected_when_both_windows_are_zero() { Environment.SetEnvironmentVariable("SERVICECONTROL_MIGRATION_EVENTLOGWINDOW", "0"); Environment.SetEnvironmentVariable("SERVICECONTROL_MIGRATION_ARCHIVEDANDRESOLVEDFAILEDMESSAGESWINDOW", "0"); - Assert.That(Read().SelectedOptionalCategoryIds, Is.EqualTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages }), "unresolved messages have no window to turn off"); + Assert.That(Read().SelectedOptionalCategoryIds, Is.EqualTo(new[] { MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryIds.GroupComments }), "neither has a window to turn off"); } [TestCase("a week")] diff --git a/src/ServiceControl.UnitTests/Migration/MigrationEngineOrderingTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationEngineOrderingTests.cs index 056805e938..db27fa84d7 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationEngineOrderingTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationEngineOrderingTests.cs @@ -74,7 +74,7 @@ public async Task GroupComments_does_not_start_before_the_unresolved_failed_mess var source = new InMemoryMigrationSource(); source.Seed(comments.Id, Row("GroupComment/g-1")); var checkpointStore = new InMemoryMigrationCheckpointStore(); - await checkpointStore.Upsert(new MigrationCheckpoint("UnresolvedAndRetryIssuedFailedMessages", predecessorState, "m-500", 500, 0, null, null, DateTime.UtcNow, DateTime.UtcNow, null, null)); + await checkpointStore.Upsert(new MigrationCheckpoint("UnresolvedFailedMessages", predecessorState, "m-500", 500, 0, null, null, DateTime.UtcNow, DateTime.UtcNow, null, null)); var target = new InMemoryMigrationTarget(checkpointStore); var engine = BuildEngine(source, checkpointStore, target); @@ -83,7 +83,7 @@ public async Task GroupComments_does_not_start_before_the_unresolved_failed_mess using (Assert.EnterMultipleScope()) { Assert.That(checkpoint.State, Is.EqualTo(MigrationCategoryState.Blocked)); - Assert.That(checkpoint.LastError, Is.EqualTo($"Blocked: GroupComments must follow UnresolvedAndRetryIssuedFailedMessages, which is {predecessorState}")); + Assert.That(checkpoint.LastError, Is.EqualTo($"Blocked: GroupComments must follow UnresolvedFailedMessages, which is {predecessorState}")); Assert.That(target.WrittenRows(comments.Id), Is.Empty); } } @@ -91,9 +91,8 @@ public async Task GroupComments_does_not_start_before_the_unresolved_failed_mess [Test] public async Task A_blocked_category_runs_once_the_category_it_follows_settles() { - // A block nothing can clear would strand group comments and keep the host closed for good. var comments = MigrationCategoryRegistry.Find("GroupComments")!; - var unresolved = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var unresolved = MigrationCategoryRegistry.Find("UnresolvedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(comments.Id, Row("GroupComment/g-1")); var checkpointStore = new InMemoryMigrationCheckpointStore(); @@ -141,7 +140,7 @@ public async Task A_predecessor_that_has_a_row_but_has_not_run_is_named_by_the_s var source = new InMemoryMigrationSource(); source.Seed(comments.Id, Row("GroupComment/g-1")); var checkpointStore = new InMemoryMigrationCheckpointStore(); - await checkpointStore.Upsert(new MigrationCheckpoint("UnresolvedAndRetryIssuedFailedMessages", predecessorState, null, 0, 0, null, null, null, null, null, null)); + await checkpointStore.Upsert(new MigrationCheckpoint("UnresolvedFailedMessages", predecessorState, null, 0, 0, null, null, null, null, null, null)); var target = new InMemoryMigrationTarget(checkpointStore); var checkpoint = await BuildEngine(source, checkpointStore, target).RunCategoryAsync(comments); @@ -149,7 +148,7 @@ public async Task A_predecessor_that_has_a_row_but_has_not_run_is_named_by_the_s using (Assert.EnterMultipleScope()) { Assert.That(checkpoint.State, Is.EqualTo(MigrationCategoryState.Blocked)); - Assert.That(checkpoint.LastError, Is.EqualTo($"Blocked: GroupComments must follow UnresolvedAndRetryIssuedFailedMessages, which is {predecessorState}")); + Assert.That(checkpoint.LastError, Is.EqualTo($"Blocked: GroupComments must follow UnresolvedFailedMessages, which is {predecessorState}")); } } From bde7f5538b965f639bb1e5c471c5e713e728db8a Mon Sep 17 00:00:00 2001 From: Rhys Bevilaqua Date: Mon, 23 Nov 2026 15:13:01 +0800 Subject: [PATCH 5/8] Scope unfinished-operation checks to offline migration --- .../ravendb-to-sql-migration-instructions.md | 6 +- .../ravendb-to-sql-migration-overview.md | 4 +- .../ravendb-to-sql-migration-system-design.md | 6 +- .../GroupCommentsMigrationTests.cs | 1 + ..._the_source_holds_unfinished_operations.cs | 62 +++++++++++++++++++ .../DataMigration/RavenMigrationSource.cs | 3 +- .../UnfinishedOperationsCheck.cs | 2 +- .../UnfinishedOperationsCheckTests.cs | 18 +++++- .../DataMigration/IMigrationSource.cs | 6 +- .../Fakes/InMemoryMigrationSource.cs | 2 +- .../Migration/MigrationStartup.cs | 4 +- .../Migration/OptionalCategoryCopier.cs | 2 +- 12 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 src/ServiceControl.Migration.AcceptanceTests/When_the_source_holds_unfinished_operations.cs diff --git a/docs/migration/ravendb-to-sql-migration-instructions.md b/docs/migration/ravendb-to-sql-migration-instructions.md index a5521ae450..f2414e8e77 100644 --- a/docs/migration/ravendb-to-sql-migration-instructions.md +++ b/docs/migration/ravendb-to-sql-migration-instructions.md @@ -72,7 +72,7 @@ Get the data ready: - Let retries finish where you can. ServiceControl stays closed until every retry-issued message and every message that holds a retry claim is copied, and a retry-issued message never ages out. Unresolved messages without a claim copy after ServiceControl opens, so a large unresolved backlog does not lengthen the outage, but it does lengthen the background copy. The copy reads archived and resolved messages only after ServiceControl opens. The exception is a RavenDB index that is broken or leaves out some failed messages: then the copy reads every failed message during the outage, logs a warning, and a large archive makes the outage longer too. - Run `--import-failed-errors` while the instance is still on RavenDB. A failed error import is a message that ServiceControl took off the error queue but failed to store. The migration copies these either way, but imported ones arrive as ordinary failed messages. -- Let retry, archive and unarchive operations finish on RavenDB before stopping ServiceControl. Migration refuses persisted batches, the retry forwarding marker, and archive or unarchive operation documents. Stopping ServiceControl leaves unfinished work persisted. +- Let retry, archive and unarchive operations finish on RavenDB before stopping ServiceControl. The offline command refuses persisted batches, the retry forwarding marker, and archive or unarchive operation documents. Stopping ServiceControl leaves unfinished work persisted. Keep these settings as they are: @@ -210,11 +210,11 @@ ServiceControl does not start, and its log names the check that failed. Nothing ### RavenDB holds unfinished operations -The refusal names the unfinished operation families and an example document id from each. The migration does not copy active operation state. Completed retry history and outstanding retry claims do not cause this refusal. +The offline command makes this refusal before it copies anything. The refusal names the unfinished operation families and an example document id from each. The migration does not copy active operation state. Completed retry history and outstanding retry claims do not cause this refusal. 1. Set `ServiceControl/PersistenceType` back to RavenDB. 2. Start ServiceControl. -3. Request the same group action again if an archive or unarchive operation does not resume. +3. Let retry batches resume when ServiceControl starts. An archive or unarchive operation resumes only when you request the same group action again. If that is no longer possible, for example because no failed messages remain in the group, contact Particular support. Do not delete the leftover documents yourself. 4. Wait until the retry, archive and unarchive operations finish. 5. Stop ServiceControl. 6. Set `ServiceControl/PersistenceType` back to the SQL target. diff --git a/docs/migration/ravendb-to-sql-migration-overview.md b/docs/migration/ravendb-to-sql-migration-overview.md index cb71aa6dce..f11ed47478 100644 --- a/docs/migration/ravendb-to-sql-migration-overview.md +++ b/docs/migration/ravendb-to-sql-migration-overview.md @@ -166,7 +166,7 @@ There are nineteen *categories*: fifteen required and four optional. **Never copied, because nothing needs them:** - RavenDB index definitions. -- In-flight retry, archive and unarchive work: `RetryBatches`, `RetryBatchNowForwardings`, `ArchiveOperations`, `UnarchiveOperations`, `ArchiveBatches` and `UnarchiveBatches`. Migration refuses while any of these documents remain. Stopping ServiceControl does not finish persisted work; restart on RavenDB, let the operations finish, stop it, and retry migration. +- In-flight retry, archive and unarchive work: `RetryBatches`, `RetryBatchNowForwardings`, `ArchiveOperations`, `UnarchiveOperations`, `ArchiveBatches` and `UnarchiveBatches`. The offline command refuses while any of these documents remain. Stopping ServiceControl does not finish persisted work; restart on RavenDB, let the operations finish, stop it, and retry migration. Retry batches resume when ServiceControl starts. An archive or unarchive operation resumes only when the same group action is requested again; if that is impossible, contact Particular support. - The `ConnectedApplications` document, written only by versions 6.0 and 6.1. Nothing has read it since 6.2. - Broker and audit service version details, which refill on the next throughput collection. - Failed message edit locks, which stop one failed message being edited twice. See [Open decisions](#values-that-change-or-merge-in-sql). @@ -265,7 +265,7 @@ In this order, cheapest first: 9. The source holds no persisted retry batches, forwarding marker, archive operations or batches, or unarchive operations or batches. The check names an example document from each unfinished family. Completed retry history and outstanding retry claims do not cause this refusal. -Checks stop at the first refusal, which names the check. If persisted operations remain, start ServiceControl on RavenDB again, let them finish, stop it, and rerun `--start-data-migration`. +Checks stop at the first refusal, which names the check. Check 9 runs only offline. If persisted operations remain, start ServiceControl on RavenDB again, let them finish, stop it, and rerun `--start-data-migration`. ```mermaid flowchart LR diff --git a/docs/migration/ravendb-to-sql-migration-system-design.md b/docs/migration/ravendb-to-sql-migration-system-design.md index b70560f080..9d371bc434 100644 --- a/docs/migration/ravendb-to-sql-migration-system-design.md +++ b/docs/migration/ravendb-to-sql-migration-system-design.md @@ -98,7 +98,7 @@ sequenceDiagram Checks->>Target: Open Start->>Start: IMigrationCheckpointStore.ReadAll Start->>Source: Open - Start->>Checks: source.ContributedChecks(), UnfinishedOperationsCheck + Start->>Checks: source.ContributedChecks(Required), UnfinishedOperationsCheck Start->>Engine: RunRequiredCategories under ClosedWindowProgress Engine-->>Start: one checkpoint per category Start->>Start: ReportWhatTheCopyLeftBehind @@ -132,7 +132,7 @@ Each position in the sequence is deliberate: > [!IMPORTANT] > Readers must stream by id prefix, load by id, or query a static index. `RefuseWrite` blocks writes but passes query POSTs (`/queries`, `/multi_get`, `/streams/queries`), and a dynamic query creates an `Auto/` index. Only the embedded server sets `DisableAutoIndexes`. 7. **Source outage tolerance.** A failed source open is tolerated only when every required category already `IsFinished`. The host logs it, `MigrationStartup.RecordSourceOutage` writes the exception (inner exception when present) as `LastError` on each selected optional category still copying, creating `NotStarted` rows where absent, the command skips the copy and succeeds. The operator can then start ServiceControl; its online copier handles any later source outage independently. In every other case the host refuses and writes nothing; if a required category never started, the refusal says the only exits are pointing back at RavenDB or starting over. -8. **`source.ContributedChecks()`** runs once the source is open, because such a check needs a session. `RavenMigrationSource` contributes `UnfinishedOperationsCheck`, which streams one document from each of `RetryBatches/`, `ArchiveOperations/` and `UnarchiveOperations/`. These prefixes cover retry batches and the forwarding marker, plus archive and unarchive operation documents and their batches. Any match refuses the run before checkpoint initialization or copying, names an example document from each unfinished family, and instructs the operator to restart on RavenDB and let the work finish. Completed retry history and retry claims use different prefixes and do not cause this refusal. A refusal here is never tolerated. +8. **`source.ContributedChecks(MigrationCategoryKind.Required)`** runs once the source is open, because such a check needs a session. The argument names the copy the checks guard. The offline command asks for `Required`, and `OptionalCategoryCopier` asks for `Optional`, after ServiceControl has opened on SQL, when no check may advise going back to the old database. `RavenMigrationSource` contributes, for `Required` only, `UnfinishedOperationsCheck`, which streams one document from each of `RetryBatches/`, `ArchiveOperations/` and `UnarchiveOperations/`. These prefixes cover retry batches and the forwarding marker, plus archive and unarchive operation documents and their batches. Any match refuses the run before checkpoint initialization or copying, names an example document from each unfinished family, and instructs the operator to restart on RavenDB and let the work finish. Retry batches resume when ServiceControl starts. An archive or unarchive operation resumes only when the same group action is requested again, and the message directs the operator to Particular support when that is impossible. It never suggests deleting documents. Completed retry history and retry claims use different prefixes and do not cause this refusal. A refusal here is never tolerated. 9. **Checkpoint initialization.** `RunRequiredCopy` first refuses checkpoints from an earlier failed-message layout. The command then seeds `NotStarted` rows for the selected optional categories and after them the required categories, before copying. `MigrationStartup.RunRequiredCategories` also ensures required rows exist, then runs each required category under its own linked `CancellationTokenSource`, awaited with `WaitAsync` on that token. 10. **`ClosedWindowProgress`** polls the checkpoint store every 30 seconds and logs progress. When the running category has committed nothing for 30 minutes, measured from the later of `LastProgressAt` and its run start, it cancels that category's token. `WaitAsync` returns even if the call underneath ignores cancellation, the category settles `Halted` with the stall in `LastError`, and the next category runs. The abandoned call keeps running until the host refuses and the process exits. A host shutdown leaves the row `InProgress`. 11. **`CopyOrExplainWhyItStopped`** maps `MigrationCheckpointConflictException` to a message about a second instance writing the same checkpoints. @@ -146,7 +146,7 @@ Each position in the sequence is deliberate: - It runs the selected, supported optional categories in registry order, `UnresolvedFailedMessages`, `GroupComments`, `EventLog` then `ArchivedAndResolvedFailedMessages`, skipping rows that are `IsFinished` or `IsFailed`. The offline command has already seeded the rows that identify the online work. - It creates and opens its own source only when a supported category is still copying. On failure it calls `RecordSourceOutage` and exits for this start. It does not depend on in-process state from `--start-data-migration`, which ran in another process. -- It reruns source-contributed checks before copying. The RavenDB unfinished-operation check should still pass because the source is read-only and new operations run on SQL. A refusal stops the online copy for that start, not ServiceControl. +- It runs the checks the source contributes for `Optional` before copying. The RavenDB source contributes none, so the unfinished-operation refusal is made only offline, where going back to RavenDB is still possible. A refusal stops the online copy for that start, not ServiceControl. - An exception leaves the category `InProgress` with `LastError`, untouched until the next start, so a transient failure never forces an abandon. `UnresolvedFailedMessages` and `GroupComments` have no window. `MigrationEngineOptions.FromSettings` selects both even when both windows are zero, so the offline command seeds their checkpoint rows and the online copier runs them. diff --git a/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs b/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs index 2570006af7..12c8aa3721 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs @@ -1,5 +1,6 @@ namespace ServiceControl.Migration.AcceptanceTests; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; diff --git a/src/ServiceControl.Migration.AcceptanceTests/When_the_source_holds_unfinished_operations.cs b/src/ServiceControl.Migration.AcceptanceTests/When_the_source_holds_unfinished_operations.cs new file mode 100644 index 0000000000..a97df08553 --- /dev/null +++ b/src/ServiceControl.Migration.AcceptanceTests/When_the_source_holds_unfinished_operations.cs @@ -0,0 +1,62 @@ +namespace ServiceControl.Migration.AcceptanceTests; + +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using NUnit.Framework; +using ServiceControl.Hosting.Commands; +using ServiceControl.Persistence.DataMigration; +using ServiceControl.Persistence.Tests; + +[TestFixture] +[NonParallelizable] +class When_the_source_holds_unfinished_operations : MigrationAcceptanceTest +{ + [Test] + public async Task The_offline_command_refuses_before_any_category_is_copied() + { + await SeedSourceKnownEndpoints("Sales"); + await SeedSourceEndpointSettings(("Sales", true)); + var batchId = $"RetryBatches/{Guid.NewGuid()}"; + await SeedSource((batchId, new { Status = 2 })); + using var cancellation = new CancellationTokenSource(TimeSpan.FromMinutes(3)); + + var refusal = Assert.ThrowsAsync(async () => await MigrateCommand.Run(Settings, AllowingAnUnreleasedMigration(), cancellation.Token)); + + using (Assert.EnterMultipleScope()) + { + Assert.That(refusal.Message, Does.Contain(batchId).And.Contain("Start ServiceControl on RavenDB again")); + Assert.That(await QueryTarget(dbContext => dbContext.EndpointSettings.CountAsync()), Is.Zero, "a required row was copied before the refusal"); + Assert.That(await QueryTarget(dbContext => dbContext.MigrationCheckpoints.CountAsync()), Is.Zero, "a refusal initializes no checkpoint"); + } + } + + // An operation found once ServiceControl runs on SQL is not a reason to advise going back to RavenDB or to hold the copy. + [Test] + public async Task The_online_copy_ignores_an_operation_left_in_the_source_after_the_offline_copy() + { + MigrationSettings(); + var failure = new IngestedFailure(); + await SeedSourceWithBody(failure.ToFailedMessage(), failure.Body, failure.ContentType); + await MigrateCommand.Run(Settings, AllowingAnUnreleasedMigration()); + await SeedSource(($"RetryBatches/{Guid.NewGuid()}", new { Status = 2 })); + + await RunHostUntilTheApiAnswers(); + await WaitUntil( + async () => (await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages))?.State.IsFinished() == true, + "the unresolved copy finished", + TimeSpan.FromMinutes(1)); + + var checkpoint = await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages); + var response = await HttpClient.GetAsync($"api/errors/last/{failure.UniqueMessageIdString}"); + + using (Assert.EnterMultipleScope()) + { + Assert.That(checkpoint.State, Is.EqualTo(MigrationCategoryState.Complete)); + Assert.That(checkpoint.CopiedCount, Is.EqualTo(1)); + Assert.That(checkpoint.LastError, Is.Null); + Assert.That(response.IsSuccessStatusCode, Is.True, "the copied message is not reachable"); + } + } +} diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs index 78e1b08b40..938d399693 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs @@ -46,7 +46,8 @@ sealed class RavenMigrationSource(RavenReadOnlySourceLifecycle lifecycle) : IMig public Task Open(CancellationToken cancellationToken = default) => lifecycle.Open(cancellationToken); - public IReadOnlyList ContributedChecks() => [new UnfinishedOperationsCheck(lifecycle)]; + public IReadOnlyList ContributedChecks(MigrationCategoryKind copying) => + copying == MigrationCategoryKind.Required ? [new UnfinishedOperationsCheck(lifecycle)] : []; public async Task Describe(CancellationToken cancellationToken = default) { diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/UnfinishedOperationsCheck.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/UnfinishedOperationsCheck.cs index 8a6b67138e..6a86655e09 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/UnfinishedOperationsCheck.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/UnfinishedOperationsCheck.cs @@ -33,7 +33,7 @@ public async Task Run(CancellationToken cancellationToken = default) if (unfinished.Count > 0) { throw new InvalidOperationException( - $"The RavenDB database '{lifecycle.Settings.DatabaseName}' holds unfinished operations: {string.Join("; ", unfinished)}. The migration does not copy them. Start ServiceControl on RavenDB again, wait until its retry, archive and unarchive operations have finished, stop it, and run --start-data-migration again. If an archive or unarchive operation does not resume, request the same group action again on RavenDB. Stopping ServiceControl does not finish them."); + $"The RavenDB database '{lifecycle.Settings.DatabaseName}' holds unfinished operations: {string.Join("; ", unfinished)}. The migration does not copy them. Start ServiceControl on RavenDB again, wait until its retry, archive and unarchive operations have finished, stop it, and run --start-data-migration again. Retry batches resume when ServiceControl starts. An archive or unarchive operation resumes only when the same group action is requested again on RavenDB. If that is no longer possible, for example because no failed messages remain in the group, contact Particular support. Stopping ServiceControl does not finish them."); } } diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnfinishedOperationsCheckTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnfinishedOperationsCheckTests.cs index 261fe40d9d..7a636f5190 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnfinishedOperationsCheckTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/UnfinishedOperationsCheckTests.cs @@ -4,6 +4,7 @@ namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; using System.Linq; using System.Threading.Tasks; using NUnit.Framework; +using ServiceControl.Persistence.DataMigration; using ServiceControl.Persistence.RavenDB; using ServiceControl.Persistence.RavenDB.Recoverability; using ServiceControl.Recoverability; @@ -15,7 +16,16 @@ public async Task The_source_contributes_the_check() { await using var source = await OpenMigrationSource(); - Assert.That(source.ContributedChecks().Select(check => check.Name), Has.Member("RavenDB holds no unfinished retry, archive or unarchive operations")); + Assert.That(source.ContributedChecks(MigrationCategoryKind.Required).Select(check => check.Name), Has.Member("RavenDB holds no unfinished retry, archive or unarchive operations")); + } + + [Test] + public async Task The_source_contributes_no_check_to_the_online_copy() + { + await Seed("retry batch"); + await using var source = await OpenMigrationSource(); + + Assert.That(source.ContributedChecks(MigrationCategoryKind.Optional), Is.Empty, "a refusal after ServiceControl opened on SQL would advise going back to RavenDB"); } [Test] @@ -76,7 +86,9 @@ public async Task Refuses_when_an_unfinished_operation_is_persisted_and_says_how Assert.That(refusal.Message, Does.Contain(documentId), "the operator is told which document is unfinished"); Assert.That(refusal.Message, Does.Contain("Start ServiceControl on RavenDB again")); Assert.That(refusal.Message, Does.Contain("--start-data-migration again")); - Assert.That(refusal.Message, Does.Contain("request the same group action again on RavenDB")); + Assert.That(refusal.Message, Does.Contain("requested again on RavenDB")); + Assert.That(refusal.Message, Does.Contain("contact Particular support")); + Assert.That(refusal.Message, Does.Not.Contain("delete")); } } @@ -100,7 +112,7 @@ public async Task Names_every_kind_of_unfinished_operation() static async Task RunChecks(Persistence.DataMigration.IMigrationSource source) { - foreach (var check in source.ContributedChecks()) + foreach (var check in source.ContributedChecks(MigrationCategoryKind.Required)) { await check.Run(); } diff --git a/src/ServiceControl.Persistence/DataMigration/IMigrationSource.cs b/src/ServiceControl.Persistence/DataMigration/IMigrationSource.cs index d6c40216b4..f81123b94e 100644 --- a/src/ServiceControl.Persistence/DataMigration/IMigrationSource.cs +++ b/src/ServiceControl.Persistence/DataMigration/IMigrationSource.cs @@ -18,9 +18,11 @@ public interface IMigrationSource : IAsyncDisposable Task Open(CancellationToken cancellationToken = default); /// - /// The checks this source wants run before the copy starts, in the order they must run. Call it after Open. + /// The checks this source wants run before it is read for , in the order they must run. Call it after Open. + /// Required checks guard the offline command; optional checks guard the background copy after ServiceControl opens on the target. + /// A check whose refusal advises going back to the old database must not contribute to the optional copy. /// - IReadOnlyList ContributedChecks(); + IReadOnlyList ContributedChecks(MigrationCategoryKind copying); /// /// What the source report and the dry run print about the source. diff --git a/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationSource.cs b/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationSource.cs index 0d3bbfceb6..b05ed2f0a5 100644 --- a/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationSource.cs +++ b/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationSource.cs @@ -75,7 +75,7 @@ public async Task BodyReadsInFlight(int count, CancellationToken cancellationTok public Task Open(CancellationToken cancellationToken = default) => Task.CompletedTask; - public IReadOnlyList ContributedChecks() => []; + public IReadOnlyList ContributedChecks(MigrationCategoryKind copying) => []; public Task Describe(CancellationToken cancellationToken = default) => Task.FromResult(Description); diff --git a/src/ServiceControl/Migration/MigrationStartup.cs b/src/ServiceControl/Migration/MigrationStartup.cs index 9c0626bd7e..ade637ee89 100644 --- a/src/ServiceControl/Migration/MigrationStartup.cs +++ b/src/ServiceControl/Migration/MigrationStartup.cs @@ -95,7 +95,7 @@ await MigrationStartupCheckRunner.Run( throw new Exception($"{exception.Message} {NoWayOutWithoutTheSource(settings)}", exception.InnerException); } - await MigrationStartupCheckRunner.Run(source.ContributedChecks(), cancellationToken); + await MigrationStartupCheckRunner.Run(source.ContributedChecks(MigrationCategoryKind.Required), cancellationToken); logger.LogInformation( "Offline migration: copying {CopyCount} required categories with ServiceControl stopped ({DeferredCount} not yet implemented: {Deferred})", @@ -133,7 +133,7 @@ public static async Task RunChecksAndOpen(IServiceProvid await MigrationStartupCheckRunner.Run([SourceOpens(source)], cancellationToken); - await MigrationStartupCheckRunner.Run(source.ContributedChecks(), cancellationToken); + await MigrationStartupCheckRunner.Run(source.ContributedChecks(MigrationCategoryKind.Required), cancellationToken); return options; } diff --git a/src/ServiceControl/Migration/OptionalCategoryCopier.cs b/src/ServiceControl/Migration/OptionalCategoryCopier.cs index f7e43480b0..e33d25cf0d 100644 --- a/src/ServiceControl/Migration/OptionalCategoryCopier.cs +++ b/src/ServiceControl/Migration/OptionalCategoryCopier.cs @@ -82,7 +82,7 @@ async Task Copy(CancellationToken cancellationToken) return; } - await MigrationStartupCheckRunner.Run(source.ContributedChecks(), cancellationToken); + await MigrationStartupCheckRunner.Run(source.ContributedChecks(MigrationCategoryKind.Optional), cancellationToken); await engine.RunCategories(categories, cancellationToken); } } From 78af173d95aa24a42ca9f8dc1bf690a06367648f Mon Sep 17 00:00:00 2001 From: Rhys Bevilaqua Date: Tue, 24 Nov 2026 11:37:15 +0800 Subject: [PATCH 6/8] Bring across recent errors in the required window --- .../ravendb-to-sql-migration-instructions.md | 6 +- .../ravendb-to-sql-migration-overview.md | 24 +- .../ravendb-to-sql-migration-system-design.md | 8 +- .../MigrationAcceptanceTest.cs | 2 + .../RecentErrorsMigrationTests.cs | 200 +++++++++++++++ ..._the_source_holds_unfinished_operations.cs | 2 +- ...unresolved_failed_messages_are_migrated.cs | 20 +- .../Writers/FailedMessagesWriter.cs | 8 +- .../DataMigration/FailedMessageStream.cs | 39 +-- .../Readers/FailedMessageStatusReader.cs | 85 ++++++- .../Readers/FailedMessagesReader.cs | 19 +- .../RavenMigrationSourceTestBase.cs | 40 +++ .../DataMigration/RecentErrorsSourceTests.cs | 232 ++++++++++++++++++ .../RetryIssuedFailedMessageSourceTests.cs | 64 ++--- .../FailedMessagesMigrationTargetTests.cs | 6 +- 15 files changed, 662 insertions(+), 93 deletions(-) create mode 100644 src/ServiceControl.Migration.AcceptanceTests/RecentErrorsMigrationTests.cs create mode 100644 src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RecentErrorsSourceTests.cs diff --git a/docs/migration/ravendb-to-sql-migration-instructions.md b/docs/migration/ravendb-to-sql-migration-instructions.md index f2414e8e77..8aedcf0fab 100644 --- a/docs/migration/ravendb-to-sql-migration-instructions.md +++ b/docs/migration/ravendb-to-sql-migration-instructions.md @@ -9,7 +9,7 @@ The migration does not move the audit instance or the monitoring instance. The a ## How the migration runs -Run `--start-data-migration` with ServiceControl stopped to copy the data it needs to open. **This part is your outage**. After the command succeeds, start ServiceControl to copy the unresolved failed messages, the group comments, the event log and the archived and resolved failed messages in the background. **The migration never writes to RavenDB**. +Run `--start-data-migration` with ServiceControl stopped to copy the data it needs to open, including the failed messages RavenDB last modified in the hour before its newest unresolved message. **This part is your outage**. After the command succeeds, start ServiceControl to copy the unresolved failed messages, the group comments, the event log and the archived and resolved failed messages in the background. **The migration never writes to RavenDB**. > [!IMPORTANT] > Until ServiceControl opens, you can go back to RavenDB and lose nothing. After it opens, you cannot go back. @@ -70,7 +70,7 @@ Make sure that your setup is supported: Get the data ready: -- Let retries finish where you can. ServiceControl stays closed until every retry-issued message and every message that holds a retry claim is copied, and a retry-issued message never ages out. Unresolved messages without a claim copy after ServiceControl opens, so a large unresolved backlog does not lengthen the outage, but it does lengthen the background copy. The copy reads archived and resolved messages only after ServiceControl opens. The exception is a RavenDB index that is broken or leaves out some failed messages: then the copy reads every failed message during the outage, logs a warning, and a large archive makes the outage longer too. +- Let retries finish where you can. ServiceControl stays closed until every retry-issued message, every message that holds a retry claim and every failed message of any status that RavenDB last modified in the hour before the newest unresolved message is copied, and a retry-issued message never ages out. A busy instance that fails many messages in an hour has a longer outage for it, and a retry, archive or resolve in that hour counts too. Older unresolved messages without a claim copy after ServiceControl opens, so a large unresolved backlog does not lengthen the outage, but it does lengthen the background copy. The hour is counted back from the newest unresolved message in RavenDB, which does not change while ServiceControl is stopped, so a restart or a retry of the category copies the same messages. With no unresolved message only the retry-issued messages and claim holders copy. Archived and resolved messages outside the recent set that hold no claim copy only after ServiceControl opens. The exception is a RavenDB index that is broken or leaves out some failed messages: then the copy reads every failed message during the outage, logs a warning, and a large archive makes the outage longer too. - Run `--import-failed-errors` while the instance is still on RavenDB. A failed error import is a message that ServiceControl took off the error queue but failed to store. The migration copies these either way, but imported ones arrive as ordinary failed messages. - Let retry, archive and unarchive operations finish on RavenDB before stopping ServiceControl. The offline command refuses persisted batches, the retry forwarding marker, and archive or unarchive operation documents. Stopping ServiceControl leaves unfinished work persisted. @@ -147,7 +147,7 @@ If you want less optional data, set a shorter window for the optional categories By default, the migration copies all the optional data that RavenDB still holds (current retension period setting). A window longer than the retention period copies nothing extra. After a category starts copying, you cannot change its window. To stop it early, abandon it. -The unresolved failed messages and the group comments have no window and no setting turns them off. They copy once ServiceControl opens, however old the unresolved messages are. Group comments appear in ServicePulse as their batches commit, after the unresolved failed messages category is Done or Abandoned. +The unresolved failed messages and the group comments have no window and no setting turns them off. They copy once ServiceControl opens, however old the unresolved messages are. The recent hour of failed messages copies during the outage and has no setting either. A `MessageFailed` event that was waiting when you stopped ServiceControl is sent only if its message is in that hour, is retry-issued or holds a retry claim, so give the old instance time to send its pending events before the outage. Group comments appear in ServicePulse as their batches commit, after the unresolved failed messages category is Done or Abandoned. ## Step 4: Run the dry run diff --git a/docs/migration/ravendb-to-sql-migration-overview.md b/docs/migration/ravendb-to-sql-migration-overview.md index f11ed47478..7bf8bb8dc0 100644 --- a/docs/migration/ravendb-to-sql-migration-overview.md +++ b/docs/migration/ravendb-to-sql-migration-overview.md @@ -20,8 +20,8 @@ - **What the instance needs to open is copied while it is closed.** This is the *required* data. Most of it is small. - **Everything else is copied in the background while it runs.** This is the *optional* data: the unresolved failed messages, the group comments, the event log and archived and resolved messages. - **Anything not copied ages out of RavenDB**, and the customer deletes the old database when ready. -- **The failed messages copied during the outage are every retry-issued message, whether or not its retry claim remains, and every other failed message that still holds a retry claim.** Unresolved messages that hold no claim copy in the background, however old they are, so a big unresolved backlog does not lengthen the outage. -- **A big archive does not make the outage longer, as long as RavenDB has indexed every failed message.** The copy finds retry-issued messages through a RavenDB index, and waits for it to catch up first. If the index is broken or incomplete, the copy reads every failed message instead and logs a warning. Then a big archive does make the outage longer. +- **The failed messages copied during the outage are the recent errors, every retry-issued message and every message that holds a retry claim.** The recent errors are the failed messages RavenDB last modified in the hour before the newest unresolved message, in any status, see [Recent errors](#recent-errors). With no unresolved message there are none. A retry-issued message copies whatever its age, whether or not its retry claim remains, and so does any other message that still holds a claim. Older unresolved messages that hold no claim copy in the background, so a big unresolved backlog does not lengthen the outage. A busy instance can fail many messages in one hour, and every one of them lengthens the outage. +- **A big archive does not make the outage longer, as long as RavenDB has indexed every failed message.** The copy finds the retry-issued and recent messages through a RavenDB index, and waits for it to catch up first. If the index is broken or incomplete, the copy reads every failed message instead and logs a warning. Then a big archive does make the outage longer. ```mermaid flowchart LR @@ -143,7 +143,7 @@ There are nineteen *categories*: fifteen required and four optional. | Category | Kind | Why | | --- | --- | --- | -| Retry-issued failed messages, and failed messages that hold a retry claim, with bodies | Required | Issuing a retry removes the expiry, so retry-issued messages never age out. Leave one behind and the retry confirmation finds no row to resolve, so the message stays missing even though the retry succeeded. A message of any other status that still holds a retry claim copies with it, with its groups, body and claim. The category keeps the checkpoint id `UnresolvedAndRetryIssuedFailedMessages` from the layout that copied every unresolved message here, see [Progress and checkpoints](#progress-and-checkpoints). A delayed retry confirmation for an unresolved message whose claim a re-failure removed is not covered, an accepted edge case, see [Writing to SQL](#writing-to-sql) | +| Recent errors, retry-issued failed messages, and failed messages that hold a retry claim, with bodies | Required | The recent errors, in every status, are in SQL when ServiceControl opens, so the events and requests that name the newest messages find them, see [Recent errors](#recent-errors). Issuing a retry removes the expiry, so retry-issued messages never age out. Leave one behind and the retry confirmation finds no row to resolve, so the message stays missing even though the retry succeeded. A message of any other status and age that still holds a retry claim copies with it, with its groups, body and claim. The category keeps the checkpoint id `UnresolvedAndRetryIssuedFailedMessages` from the layout that copied every unresolved message here, see [Progress and checkpoints](#progress-and-checkpoints). A delayed retry confirmation for an unresolved message whose claim a re-failure removed is not covered, an accepted edge case, see [Writing to SQL](#writing-to-sql) | | Message redirects | Required | | | Known endpoints, with the monitored flag | Required | The flag is part of the endpoint row | | Endpoint settings | Required | Copied after known endpoints | @@ -155,10 +155,10 @@ There are nineteen *categories*: fifteen required and four optional. | Licensing report masks | Required | | | Uploaded licensed endpoint details file | Required | Nothing recomputes it. Losing it means downloading it again from the licence portal and uploading it again | | Subscriptions | Required | | -| Integration events still waiting to be sent | Required | Each is an event a subscriber has not received yet. There are only any if the old instance fell behind or could not reach the broker. They are sent once ServiceControl opens, later than they would have been, except a `MessageFailed` event for a message archived or resolved before the move (see [Rows removed after the copy](#rows-removed-after-the-copy)). RavenDB sends them in no particular order anyway, so no ordering is lost | +| Integration events still waiting to be sent | Required | Each is an event a subscriber has not received yet. There are only any if the old instance fell behind or could not reach the broker. They are sent once ServiceControl opens, later than they would have been, except a `MessageFailed` event for an older message that is not retry-issued, holds no claim and is archived, resolved or not yet copied (see [Rows removed after the copy](#rows-removed-after-the-copy)). RavenDB sends them in no particular order anyway, so no ordering is lost | | Custom checks, with their last status | Required | Not every check reports again. An endpoint that is down never does, and a check with no repeat interval reports only when its endpoint starts. Leaving one out could hide a known failure until that endpoint restarts. Results of the six checks the RavenDB persister runs about its own database, matched on id and category, are left out, as harmless skips | | Failed error imports, with bodies | Required | Messages taken off the error queue but not ingested. They exist nowhere else and never expire. After the move the "Error Message Ingestion" custom check keeps flagging them and `--import-failed-errors` imports them into SQL. Importing them on RavenDB first is better still | -| Unresolved failed messages, with bodies | Optional | Copied once ServiceControl has opened, whatever the age of the message, because an operator is waiting on each one. It has no window and no setting turns it off. A message that already holds a retry claim copied with the required category, so it counts as already present. An existing SQL row always wins, see [Writing to SQL](#writing-to-sql). Group retry, archive and unarchive act only on the messages copied so far, an accepted edge case | +| Unresolved failed messages, with bodies | Optional | Copied once ServiceControl has opened, whatever the age of the message, because an operator is waiting on each one. It has no window and no setting turns it off. A message the required category already copied, because it holds a retry claim or is one of the recent errors, counts as already present, and the copy leaves its row, groups, body and claim as they are. An existing SQL row always wins, see [Writing to SQL](#writing-to-sql). Group retry, archive and unarchive act only on the messages copied so far, an accepted edge case | | Group comments | Optional | Copied in the background right after the unresolved failed messages, before the event log and the archived and resolved messages. It has no window and no setting turns it off. A comment is missing from SQL until this copy reaches it. Blank comments are not copied. The next retention sweep removes a copied comment on a group with no failed message in SQL, see [Rows removed after the copy](#rows-removed-after-the-copy) | | Event log | Optional | The history behind the ServicePulse activity feed. On a busy instance it holds a row for every failure, retry and submission | | Archived and resolved failed messages, with bodies | Optional | The biggest category by far, and most of the copying time | @@ -176,6 +176,16 @@ There are nineteen *categories*: fifteen required and four optional. - Retry claims in `FailedMessageRetries`. A claim ties a message to the retry that sent it. The copy writes one for each message it inserts that RavenDB still held a claim for, whatever the status of the message, so a retry by id sends or holds back a message just as it did before the move. +### Recent errors + +- **The required failed message category copies every failed message RavenDB last modified at or after the newest unresolved message's last-modified time minus one hour, in every status,** with its bodies and groups. There is no upper bound. The hour is fixed and no setting changes it. It is not a measure of when a message failed: any write to the document, such as a retry, an archive or an unarchive, moves its last-modified time. A message modified exactly at the cutoff is recent. +- **The anchor is the newest unresolved message's RavenDB last-modified time,** found by the source reader itself. Every ServiceControl instance is stopped while the source is copied, so nothing ingests or changes a message and every read, restart, resume and retry finds the same anchor. Nothing is stored for it and no clock is read. With no unresolved message the recent hour is not copied, and the retry-issued messages and claim holders still are, whatever their age. +- **A message is read and counted once,** whether it is recent, retry-issued or holds a retry claim, and its claim copies with it. Reading through the index or through the full read gives the same set. `--migration-verify` and the dry run are not yet commands in this build. The target count of this category is the whole failed message table, which is the copied set only while ServiceControl is stopped and nothing else has written to it. After ServiceControl opens it includes live and optional rows, so it is not a category count then. +- **A pending `MessageFailed` event is protected only when its message is a recent error, a retry-issued message or a claim holder.** There is no separate check of the events. An event for any other older message is not protected, see [Rows removed after the copy](#rows-removed-after-the-copy). +- **The optional unresolved copy inserts only the rows that are missing.** It does not update a recent row or its claim, and it counts the row as already present. + +The reader, cursors and counts are described in the [system design](ravendb-to-sql-migration-system-design.md#readers-and-writers). + ### Optional data windows - **Each windowed optional category copies only rows inside its *window*:** events raised within it, or messages archived or resolved within it. The unresolved failed messages and the group comments have no window. The migration always selects both categories and copies every unresolved message however old. @@ -292,7 +302,7 @@ flowchart LR - **A failed message is written as one row**, with its status unchanged. Its older attempts are dropped, see [Open decisions](#values-that-change-or-merge-in-sql). - **An existing SQL row always wins.** The copy never updates a row and counts an existing row as already present, not as a skip. When ingestion created the row first, its attempt count and first failure time can omit earlier RavenDB failures, and it can hold an older payload than RavenDB's newest attempt. These outcomes are accepted. -- **The copy accepts unlikely races with live operations.** A delayed retry confirmation can be consumed before an unresolved message whose claim a re-failure removed arrives, leaving it unresolved. A message archived or resolved in SQL and then deleted by retention before the copy reaches it can arrive again as unresolved. A queued edit-and-retry is discarded if its original message has not arrived. +- **The copy accepts unlikely races with live operations.** A delayed retry confirmation can be consumed before an unresolved message whose claim a re-failure removed arrives, leaving it unresolved, unless the message is among the recent errors. A message archived or resolved in SQL and then deleted by retention before the copy reaches it can arrive again as unresolved. A queued edit-and-retry is discarded if its original message has not arrived. - **Bodies are immutable and written only if absent.** The body store keeps one body per `UniqueMessageId`, and each backend refuses to replace one that exists: the file system through an exclusive move, Azure Blob and S3 through a conditional upload. The first writer wins, so a body the copy stores just before ingestion does is the body that stays. That is safe only because every attempt of one message carries the same body bytes, which ingestion already assumes. An S3-compatible store that ignores `If-None-Match` falls back to a check followed by an unconditional upload, which can replace a live body. - **A failed message keeps its failure groups and its last-modified time**, so filtered lists show the same messages as before. A failure group with no id is the exception, see [Open decisions](#values-that-change-or-merge-in-sql). - **`UniqueMessageId` keeps its value** but becomes a `uniqueidentifier`. It is the primary key, the ServicePulse URL, the retry key and the body key. @@ -502,7 +512,7 @@ The dry run counts the subscription and endpoint settings merges before anything ### Rows removed after the copy - **The next retention sweep removes a copied group comment whose group has no failed message in SQL**, because the sweep deletes a comment whose group has no row. The group has none when its messages are archived or resolved, when they are gone from RavenDB, or when the copy skipped them. Comments wait until the unresolved failed messages category is Done or Abandoned, so waiting for that copy alone does not cause comment loss. Abandoning it can leave groups without messages in SQL. Nothing keeps a comment on a group with only archived or resolved messages, even when those messages copy later. The copy names each such comment, in a warning starting `Changed GroupComment/ in category GroupComments on the way into SQL: The group '' has no copied failed message`, which also quotes the whole comment. -- **A `MessageFailed` event still waiting to be sent for a message archived or resolved before the move is never sent.** ServiceControl looks the message up when it sends the event. Archived and resolved messages arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. The same lookup can fail for an unresolved message the background copy has not reached yet, an accepted edge case. +- **A `MessageFailed` event still waiting to be sent for a message outside the recent errors, retry-issued messages and claim holders is lost when the message is archived, resolved or not yet copied.** ServiceControl looks the message up when it sends the event. Archived and resolved messages older than the recent hour arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. The same lookup can fail for an older unresolved message the background copy has not reached yet. The required copy covers the events for messages that changed in the hour before the anchor, are retry-issued or hold a claim, and nothing else covers the rest, an accepted gap. - **Unresolved messages from version 6.18 or earlier can expire during the copy.** On those versions, a message that failed again after being archived or resolved kept its expiry. One deleted before the copy reads it is not copied and not counted. No log line names these messages yet. ### What the checks do not catch diff --git a/docs/migration/ravendb-to-sql-migration-system-design.md b/docs/migration/ravendb-to-sql-migration-system-design.md index 9d371bc434..803798a9a8 100644 --- a/docs/migration/ravendb-to-sql-migration-system-design.md +++ b/docs/migration/ravendb-to-sql-migration-system-design.md @@ -133,7 +133,7 @@ Each position in the sequence is deliberate: > Readers must stream by id prefix, load by id, or query a static index. `RefuseWrite` blocks writes but passes query POSTs (`/queries`, `/multi_get`, `/streams/queries`), and a dynamic query creates an `Auto/` index. Only the embedded server sets `DisableAutoIndexes`. 7. **Source outage tolerance.** A failed source open is tolerated only when every required category already `IsFinished`. The host logs it, `MigrationStartup.RecordSourceOutage` writes the exception (inner exception when present) as `LastError` on each selected optional category still copying, creating `NotStarted` rows where absent, the command skips the copy and succeeds. The operator can then start ServiceControl; its online copier handles any later source outage independently. In every other case the host refuses and writes nothing; if a required category never started, the refusal says the only exits are pointing back at RavenDB or starting over. 8. **`source.ContributedChecks(MigrationCategoryKind.Required)`** runs once the source is open, because such a check needs a session. The argument names the copy the checks guard. The offline command asks for `Required`, and `OptionalCategoryCopier` asks for `Optional`, after ServiceControl has opened on SQL, when no check may advise going back to the old database. `RavenMigrationSource` contributes, for `Required` only, `UnfinishedOperationsCheck`, which streams one document from each of `RetryBatches/`, `ArchiveOperations/` and `UnarchiveOperations/`. These prefixes cover retry batches and the forwarding marker, plus archive and unarchive operation documents and their batches. Any match refuses the run before checkpoint initialization or copying, names an example document from each unfinished family, and instructs the operator to restart on RavenDB and let the work finish. Retry batches resume when ServiceControl starts. An archive or unarchive operation resumes only when the same group action is requested again, and the message directs the operator to Particular support when that is impossible. It never suggests deleting documents. Completed retry history and retry claims use different prefixes and do not cause this refusal. A refusal here is never tolerated. -9. **Checkpoint initialization.** `RunRequiredCopy` first refuses checkpoints from an earlier failed-message layout. The command then seeds `NotStarted` rows for the selected optional categories and after them the required categories, before copying. `MigrationStartup.RunRequiredCategories` also ensures required rows exist, then runs each required category under its own linked `CancellationTokenSource`, awaited with `WaitAsync` on that token. +9. **Checkpoint initialization.** `RunRequiredCopy` first refuses checkpoints from an earlier failed-message layout. The command then seeds `NotStarted` rows for the required categories and after them the selected optional ones, before copying. `MigrationStartup.RunRequiredCategories` also ensures required rows exist, then runs each required category under its own linked `CancellationTokenSource`, awaited with `WaitAsync` on that token. 10. **`ClosedWindowProgress`** polls the checkpoint store every 30 seconds and logs progress. When the running category has committed nothing for 30 minutes, measured from the later of `LastProgressAt` and its run start, it cancels that category's token. `WaitAsync` returns even if the call underneath ignores cancellation, the category settles `Halted` with the stall in `LastError`, and the next category runs. The abandoned call keeps running until the host refuses and the process exits. A host shutdown leaves the row `InProgress`. 11. **`CopyOrExplainWhyItStopped`** maps `MigrationCheckpointConflictException` to a message about a second instance writing the same checkpoints. 12. **`ReportWhatTheCopyLeftBehind`** logs skips per category. It offers `--migration-retry` only for an `IsFailed` row, and reports harmless skips on a `Complete` row with the explanation `HarmlessSkipExplanations` holds for each harmless reason. A test fails unless that map covers exactly the `IsBenign` reasons. @@ -151,6 +151,8 @@ Each position in the sequence is deliberate: `UnresolvedFailedMessages` and `GroupComments` have no window. `MigrationEngineOptions.FromSettings` selects both even when both windows are zero, so the offline command seeds their checkpoint rows and the online copier runs them. +The failed message category `UnresolvedAndRetryIssuedFailedMessages` copies recent errors with no window setting and no checkpoint column. `FailedMessagesReader` finds the cutoff itself on every `Read`: the newest unresolved message's `@last-modified` less one hour. Every ServiceControl instance is stopped while the source is copied, so the newest unresolved message is stable and `Read`, `Count`, a resume, a restart and a retry reset all derive the same cutoff. `IMigrationSource` and `IMigrationCategoryReader` carry no cutoff and the source never reads the target. + Windowing for the other two: `MigrationEngineOptions.WindowFor` supplies the window and the reader applies it through `IWindowedMigrationCategoryReader`. The engine writes `StartedWindowSeconds` once, on the first `InProgress` upsert. `CopiesFrom` is `StartedAt` minus the currently configured window, so window stability across restarts depends entirely on `OptionalCategoryWindowsAreUnchangedCheck`. Verify counts from `StartedAt` minus `StartedWindowSeconds` instead, which is the same window while the category copies, because nothing stops the setting changing once it has finished. > [!IMPORTANT] @@ -169,7 +171,7 @@ Windowing for the other two: `MigrationEngineOptions.WindowFor` supplies the win | Ordering | `MigrationCategory.MustFollow`, from `MigrationCategoryRegistry.All` | Data, not code. A follower whose predecessor is not `IsFinished` is upserted `Blocked` with `LastError` naming it | | Start or resume | Engine: `NotStarted`, `Blocked`, or any row with `LastError` moves to `InProgress`, cursor kept | `Halted` never resumes by itself; only `--migration-retry` resets it | | Batch size | `EFCoreMigrationTarget.BatchSizeFor` via the category's `IMigrationCategoryWriter` | Writers use `RowsPerStatementFor` from the dialect's parameter budget; `StoreKeyedInsert`-only writers use a fixed 500 | -| Read after cursor | `IMigrationCategoryReader`, usually via `RavenDocumentStream.ByPrefix`; `FailedMessageStatusReader` streams a query on `FailedMessageViewIndex` for its statuses ordered by `id()`, after waiting for the index to be up to date, and falls back to `ByPrefix` when the index is errored, disabled or holds fewer failed messages with a copied status than the collection does, and `FailedMessagesReader` then reads the claim holders, see [Readers and writers](#readers-and-writers); the two licensing endpoint readers use `ThroughputDocuments.Endpoints`; single-document readers load by id | No-tracking session. A cursor whose document no longer exists is refused, because RavenDB would otherwise resume after the missing id and skip rows. The cursor advances on every document seen. Both paths resume on the same document id cursor | +| Read after cursor | `IMigrationCategoryReader`, usually via `RavenDocumentStream.ByPrefix`; `FailedMessageStatusReader` streams a query on `FailedMessageViewIndex` for its statuses ordered by `id()`, after waiting for the index to be up to date, and falls back to `ByPrefix` when the index is errored, disabled or holds fewer failed messages with a copied status than the collection does, and `FailedMessagesReader` then reads the claim holders that the status query did not return, see [Readers and writers](#readers-and-writers); the two licensing endpoint readers use `ThroughputDocuments.Endpoints`; single-document readers load by id | No-tracking session. A cursor whose document no longer exists is refused, because RavenDB would otherwise resume after the missing id and skip rows. The cursor advances on every document seen. Both paths resume on the same document id cursor | | Fetch bodies | `MigrationBodyLoader` over `IMigrationSource.ReadBody` | Rows with a null body only, 8 concurrent, `MigrationEngine.MaxBodyReadAttempts` (3) with `MigrationEngineOptions.BodyRetryBackoff` (200 ms). `IsDefect` exceptions such as `NotSupportedException` are not retried and reach the batch catch. A missing attachment is skipped as `BodyMissing` after one attempt, a permanent fault no retry fixes, since ingestion writes an attachment for every failed message. Body skips stay off the checkpoint until the write commits | | Map documents to rows | `IMigrationCategoryWriter.Prepare`, returning `PreparedBatch` | Only the writer knows `NOT NULL` columns, key caps and what the product deletes anyway. Rows are inserted later inside the target transaction. Body-carrying writers write external bodies here, before the transaction, only for messages the target does not hold. A crash leaves orphan bodies, see [open decisions](ravendb-to-sql-migration-overview.md#what-the-checks-do-not-catch) | | Insert and checkpoint | `EFCoreMigrationTarget.Write`: `AccountForEveryRow`, then in one transaction under the execution strategy `PreparedBatch.Insert`, `AlreadyPresentIn`, `MigrationCheckpoint.Extend`, `MigrationCheckpointExtensions.UpsertCheckpoint` | Most writers use the dialect's `InsertMissing` built on `MigrationInsert`. Store-keyed rows use `StoreKeyedInsert`, because `MigrationInsert` rejects store-generated keys. Throughput days use `AddLicensingEndpointThroughput`. Each attempt clears the change tracker. Skips, merges and changes are logged after commit. `MigrationWriteResult.Merged` and `Changed` carry the batch's counts of each, which the engine adds up per category for the current run in `MigrationEngine.MergedAndChangedThisRun`; the checkpoint stores neither. See [progress and checkpoints](ravendb-to-sql-migration-overview.md#progress-and-checkpoints) | @@ -282,7 +284,7 @@ Every reader derives from `MigrationCategoryReader` and every writer | 9 | `LicensingThroughput` | Required | `LicensingThroughputReader`, `LicensingThroughputWriter` | Throughput database. Data link to `LicensingEndpoints`. The reader gives one batch per endpoint from `ThroughputDocuments.Endpoints` with a batch size of one, and ignores the requested batch size, because one endpoint can hold a year or more of days. Each batch's cursor is its endpoint's id. The writer keeps the last row of a day repeated for one source endpoint, compared ordinally. `AddLicensingEndpointThroughput` splits the batch into statements and adds each day's count to the stored one, summing endpoints whose names the key column treats as the same, in one batch or across batches. A re-read is not idempotent, so a resume relies on each batch committing with its checkpoint, and a retry deletes the copied rows first | | 10 | `LicensingReportMasks` | Required | `LicensingReportMasksReader`, `LicensingReportMasksWriter` | Throughput database. One `Settings` row via `SettingRowMapper` | | 11 | `LicensedEndpointDetails` | Required | `LicensedEndpointDetailsReader`, `LicensedEndpointDetailsWriter` | Throughput database. One `Settings` row via `SettingRowMapper` | -| 12 | `UnresolvedAndRetryIssuedFailedMessages` | Required | `FailedMessagesReader`, `FailedMessagesWriter` | Carries bodies. `FailedMessageStream` projection, `FailedMessageDocumentTranslator`. `FailedMessagesReader` and `UnresolvedFailedMessagesReader` both derive from `FailedMessageStatusReader`, which streams the category's statuses in id order through `FailedMessageViewIndex` after waiting for the index, or through `RavenDocumentStream.ByPrefix` when the index cannot be trusted, with the document id as the cursor either way. `FailedMessagesReader` reads in two phases. Phase one is the retry-issued messages, where `FailedMessageStream.ResolveRetryClaims` looks up each batch's claims in one request and sets `RetryClaimHeld` on every row. Phase two is `FailedMessageStream.ClaimHolders`, which streams the `FailedMessageRetries/` documents by id prefix and, per batch of claims, queries the non-retry-issued messages they name by `id()`, with `RetryClaimHeld` true and no index built. A claim whose message is gone makes no row, and memory stays bounded by the batch size. The phase-two cursor is `claim-holder:` and the last claim id. A cursor without the prefix, which is the end of phase one, runs phase one after it and then starts phase two. A prefixed cursor skips phase one and its document-existence check. The writer, through `FailedMessagesWriterBase`, inserts a claim for every inserted row whose flag is true, so the claims mirror RavenDB's. A retry-issued row with no flag fails the batch, and any other status with no flag means no claim. The writer counts retry-issued rows and rows with a claim | +| 12 | `UnresolvedAndRetryIssuedFailedMessages` | Required | `FailedMessagesReader`, `FailedMessagesWriter` | Carries bodies. `FailedMessageStream` projection, `FailedMessageDocumentTranslator`. `FailedMessagesReader` and `UnresolvedFailedMessagesReader` both derive from `FailedMessageStatusReader`, which streams the category's statuses in id order through `FailedMessageViewIndex` after waiting for the index, or through `RavenDocumentStream.ByPrefix` when the index cannot be trusted, with the document id as the cursor either way. `FailedMessagesReader` reads in two phases. First it asks `FailedMessageStatusReader.CanQueryIndex` which way to read, then `LatestUnresolvedLastModified` for the anchor: through the index it is one query for the unresolved message with the highest `LastModified`, and otherwise one pass over the `FailedMessages/` documents that keeps only the newest time. It then reads in the same way and finds nothing recent when there is no unresolved message. Phase one is `ReadStatuses` with the retry-issued status or a last-modified time at or after the cutoff, in any status. The index query is `Status in (RetryIssued) OR LastModified >= cutoff ticks` ordered by `id()`. The full read applies the same predicate in `FailedMessageStream.IsSelected` to the document's `@last-modified`, and a document without one is selected so that it fails by name in the writer. `FailedMessageStream.ResolveRetryClaims` looks up each batch's claims in one request and sets `RetryClaimHeld` on every row. Phase two is `FailedMessageStream.ClaimHolders`, which streams the `FailedMessageRetries/` documents by id prefix and, per batch of claims, queries the messages they name by `id()`, with `RetryClaimHeld` true and no index built. It leaves out every message `IsSelected` takes, because phase one read it, so no message is read or counted twice and the claim a row carries is the one phase one resolved. A claim whose message is gone makes no row, and memory stays bounded by the batch size. The phase-two cursor is `claim-holder:` and the last claim id. A cursor without the prefix, which is the end of phase one, runs phase one after it and then starts phase two. A prefixed cursor skips phase one and its document-existence check. The writer, through `FailedMessagesWriterBase`, inserts a claim for every inserted row whose flag is true, so the claims mirror RavenDB's. A retry-issued row with no flag fails the batch, and any other status with no flag means no claim. The writer counts every row of the failed message table for this category, which equals the rows it copied only while the host is closed and the target started empty. The optional `UnresolvedFailedMessagesWriter` shares `FailedMessagesWriterBase`, so it too inserts only missing rows and leaves a recent row, its groups and its claim as the required copy wrote them | | 13 | `PendingIntegrationEvents` | Required | `PendingIntegrationEventsReader`, `PendingIntegrationEventsWriter` | Store-keyed via `StoreKeyedInsert`. Deletes its rows on retry | | 14 | `CustomChecks` | Required | `CustomChecksReader`, `CustomChecksWriter` | The RavenDB reader flags a check whose id and category match one of the RavenDB persister's own six checks with the `SourceStorageCheck` metadata key, and the writer skips a flagged row as `SourceStorageCheck` without knowing any id. A RavenDB audit instance's dirty memory result reports under another category, so it is copied | | 15 | `FailedErrorImports` | Required | `FailedErrorImportsReader`, `FailedErrorImportsWriter` | Carries bodies inline in `Read`, never calls `ReadBody` | diff --git a/src/ServiceControl.Migration.AcceptanceTests/MigrationAcceptanceTest.cs b/src/ServiceControl.Migration.AcceptanceTests/MigrationAcceptanceTest.cs index e820f441ba..a73cc000c8 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/MigrationAcceptanceTest.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/MigrationAcceptanceTest.cs @@ -685,6 +685,8 @@ protected Task FindFailedMessage(Guid uniqueMessageId) => protected EFPersisterSettings EFSettings => targetServices.GetRequiredService(); + protected IFailedMessageQueryDataStore FailedMessageQueryStore => targetServices.GetRequiredService(); + protected IBodyStorage BodyStorage => targetServices.GetRequiredService(); protected Task Ingest(params IngestedFailure[] failures) => diff --git a/src/ServiceControl.Migration.AcceptanceTests/RecentErrorsMigrationTests.cs b/src/ServiceControl.Migration.AcceptanceTests/RecentErrorsMigrationTests.cs new file mode 100644 index 0000000000..94d52f6f90 --- /dev/null +++ b/src/ServiceControl.Migration.AcceptanceTests/RecentErrorsMigrationTests.cs @@ -0,0 +1,200 @@ +namespace ServiceControl.Migration.AcceptanceTests; + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using NUnit.Framework; +using ServiceControl.ExternalIntegrations; +using ServiceControl.Hosting.Commands; +using ServiceControl.MessageFailures; +using ServiceControl.Operations.BodyStorage; +using ServiceControl.Persistence; +using ServiceControl.Persistence.DataMigration; +using ServiceControl.Persistence.Tests; +using ServiceControl.Recoverability.ExternalIntegration; + +/// +/// The required failed message category copies the errors of the hour before the newest unresolved message, in every status, +/// besides the retry-issued messages and the claim holders of any age, so the host opens with them in place. The source is +/// stopped while it is copied, so the newest unresolved message is the same on every read and nothing is persisted for it. +/// +[TestFixture] +[NonParallelizable] +class RecentErrorsMigrationTests : MigrationAcceptanceTest +{ + const string RequiredCategoryId = MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; + + [Test] + public async Task Copies_the_recent_errors_of_every_status_once_with_their_bodies_groups_and_claims() + { + var retryIssuedClaimed = await Seed(FailedMessageStatus.RetryIssued, claimed: true); + var retryIssuedReleased = await Seed(FailedMessageStatus.RetryIssued); + var archivedClaimed = await Seed(FailedMessageStatus.Archived, claimed: true); + var resolvedClaimed = await Seed(FailedMessageStatus.Resolved, claimed: true); + var archivedPlain = await Seed(FailedMessageStatus.Archived); + var resolvedPlain = await Seed(FailedMessageStatus.Resolved); + var unresolvedClaimed = await Seed(FailedMessageStatus.Unresolved, claimed: true); + var newestUnresolved = await Seed(FailedMessageStatus.Unresolved); + + var copy = await CopyCategory(RequiredCategoryId); + + Seeded[] copiedIds = [retryIssuedClaimed, retryIssuedReleased, archivedClaimed, resolvedClaimed, archivedPlain, resolvedPlain, unresolvedClaimed, newestUnresolved]; + Seeded[] claimedIds = [retryIssuedClaimed, archivedClaimed, resolvedClaimed, unresolvedClaimed]; + var rows = await QueryTarget(db => db.FailedMessages.AsNoTracking().ToDictionaryAsync(row => row.UniqueMessageId)); + var claims = await QueryTarget(db => db.FailedMessageRetries.AsNoTracking().Select(claim => claim.UniqueMessageId).ToListAsync()); + var groups = await QueryTarget(db => db.FailedMessageGroups.AsNoTracking().Select(group => group.FailedMessageUniqueId).ToListAsync()); + var category = MigrationCategoryRegistry.Find(RequiredCategoryId)!; + await using var source = await OpenSource(); + + using (Assert.EnterMultipleScope()) + { + Assert.That(copy.Copied, Is.EqualTo(copiedIds.Length), "a message that is retry-issued, holds a claim and is recent is still one row"); + Assert.That(rows.Keys, Is.EquivalentTo(copiedIds.Select(failure => failure.UniqueMessageId))); + Assert.That(copiedIds.Select(failure => rows[failure.UniqueMessageId].Status), Is.EqualTo(copiedIds.Select(failure => failure.Status))); + Assert.That(claims, Is.EquivalentTo(claimedIds.Select(failure => failure.UniqueMessageId)), "every claim the source held is copied as one claim and no other message gets one"); + Assert.That(groups, Is.EquivalentTo(copiedIds.Select(failure => failure.UniqueMessageId)), "every copied message keeps its failure group"); + Assert.That(await Target.Count(category), Is.EqualTo(copiedIds.Length)); + Assert.That(await source.Count(category), Is.EqualTo(copiedIds.Length), "the source counts the set the target holds"); + } + + foreach (var failure in copiedIds) + { + await AssertBody(failure); + } + + var optional = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); + + using (Assert.EnterMultipleScope()) + { + Assert.That(optional.Copied, Is.Zero, "both unresolved messages are recent errors"); + Assert.That(optional.AlreadyPresent, Is.EqualTo(2)); + Assert.That(await QueryTarget(db => db.FailedMessages.CountAsync()), Is.EqualTo(copiedIds.Length)); + Assert.That(await QueryTarget(db => db.FailedMessageRetries.CountAsync()), Is.EqualTo(claimedIds.Length), "the optional copy adds no claim"); + } + } + + [Test] + public async Task Copies_only_the_retry_issued_messages_and_claim_holders_when_the_source_has_no_unresolved_message() + { + var retryIssued = await Seed(FailedMessageStatus.RetryIssued); + var archivedClaimed = await Seed(FailedMessageStatus.Archived, claimed: true); + var archivedPlain = await Seed(FailedMessageStatus.Archived); + var resolvedPlain = await Seed(FailedMessageStatus.Resolved); + + var copy = await CopyCategory(RequiredCategoryId); + + var copied = await QueryTarget(db => db.FailedMessages.Select(row => row.UniqueMessageId).ToListAsync()); + + using (Assert.EnterMultipleScope()) + { + Assert.That(copy.Copied, Is.EqualTo(2)); + Assert.That(copied, Is.EquivalentTo(new[] { retryIssued.UniqueMessageId, archivedClaimed.UniqueMessageId }), "with no unresolved message there is no anchor, so the recent hour is not copied"); + Assert.That(copied, Does.Not.Contain(archivedPlain.UniqueMessageId).And.Not.Contain(resolvedPlain.UniqueMessageId)); + } + } + + [Test] + public async Task A_retry_reset_and_a_second_copy_read_the_same_set_because_the_source_decides_it() + { + await Seed(FailedMessageStatus.Archived); + await Seed(FailedMessageStatus.Unresolved); + await Seed(FailedMessageStatus.Resolved); + + var first = await CopyCategory(RequiredCategoryId); + await Target.ResetForRetry(RetryResetMigrationTests.ResetForRetry(await ReadCheckpoint(RequiredCategoryId))); + var second = await CopyCategory(RequiredCategoryId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(first.Copied, Is.EqualTo(3)); + Assert.That(second.AlreadyPresent, Is.EqualTo(3), "the second read selected the same three messages"); + Assert.That(second.Copied, Is.Zero); + } + } + + [Test] + public async Task The_offline_command_copies_the_recent_message_before_the_host_opens() + { + await SeedSourceKnownEndpoints("Sales"); + var failure = await Seed(FailedMessageStatus.Unresolved); + using var cancellation = new CancellationTokenSource(TimeSpan.FromMinutes(3)); + + await MigrateCommand.Run(Settings, AllowingAnUnreleasedMigration(), cancellation.Token); + + var failedMessages = await ReadCheckpoint(RequiredCategoryId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(failedMessages.CopiedCount, Is.EqualTo(1), "the newest unresolved message is copied before the host opens"); + Assert.That(await FindFailedMessage(failure.UniqueMessageId), Is.Not.Null); + } + } + + [Test] + public async Task A_pending_MessageFailed_event_for_a_recent_message_dispatches_with_the_message_already_copied() + { + var failure = await Seed(FailedMessageStatus.Unresolved); + await SeedSource(($"ExternalIntegrationDispatchRequests/{Guid.NewGuid()}", new ExternalIntegrationDispatchRequest { DispatchContext = new MessageFailedPublisher.DispatchContext { FailedMessageId = failure.UniqueMessageId } })); + + await CopyCategory(RequiredCategoryId); + await CopyCategory(MigrationCategoryIds.PendingIntegrationEvents); + + object[] dispatched = []; + ExternalIntegrationStore.Subscribe((contexts, _) => + { + dispatched = contexts; + return Task.CompletedTask; + }); + await ExternalIntegrationStore.DispatchNow(); + + var publisher = new MessageFailedPublisher(FailedMessageQueryStore); + var events = (await publisher.PublishEventsForOwnContexts(dispatched)).ToArray(); + + using (Assert.EnterMultipleScope()) + { + Assert.That(((MessageFailedPublisher.DispatchContext)dispatched.Single()).FailedMessageId, Is.EqualTo(failure.UniqueMessageId)); + Assert.That(events, Has.Length.EqualTo(1), "a request whose message is missing from the target dispatches nothing and is deleted"); + } + + await AssertBody(failure); + } + + async Task AssertBody(Seeded failure) + { + var body = await BodyStorage.TryFetch(failure.UniqueMessageId.ToString()); + + Assert.That(body.State, Is.EqualTo(MessageBodyState.Available), $"the body of {failure.Status} message {failure.UniqueMessageIdString}"); + Assert.That(body.Content.ContentType, Is.EqualTo(failure.ContentType)); + Assert.That(await ReadAll(body.Content.Stream), Is.EqualTo(failure.Body)); + } + + // Seeded with a body, a failure group and, when claimed, the retry claim RavenDB would hold. + async Task Seed(FailedMessageStatus status, bool claimed = false) + { + var failure = new IngestedFailure(); + await SeedSourceWithBody(failure.ToFailedMessage(status), failure.Body, failure.ContentType); + + if (claimed) + { + await SeedSourceRetryClaim(failure.UniqueMessageIdString); + } + + return new Seeded(failure.UniqueMessageId, failure.UniqueMessageIdString, status, failure.Body, failure.ContentType); + } + + record Seeded(Guid UniqueMessageId, string UniqueMessageIdString, FailedMessageStatus Status, byte[] Body, string ContentType); + + static async Task ReadAll(Stream stream) + { + await using (stream) + { + using var buffer = new MemoryStream(); + await stream.CopyToAsync(buffer); + return buffer.ToArray(); + } + } +} diff --git a/src/ServiceControl.Migration.AcceptanceTests/When_the_source_holds_unfinished_operations.cs b/src/ServiceControl.Migration.AcceptanceTests/When_the_source_holds_unfinished_operations.cs index a97df08553..e3bc52bcd4 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/When_the_source_holds_unfinished_operations.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/When_the_source_holds_unfinished_operations.cs @@ -54,7 +54,7 @@ await WaitUntil( using (Assert.EnterMultipleScope()) { Assert.That(checkpoint.State, Is.EqualTo(MigrationCategoryState.Complete)); - Assert.That(checkpoint.CopiedCount, Is.EqualTo(1)); + Assert.That(checkpoint.AlreadyPresentCount, Is.EqualTo(1), "the message is the newest unresolved one, so the offline copy took it"); Assert.That(checkpoint.LastError, Is.Null); Assert.That(response.IsSuccessStatusCode, Is.True, "the copied message is not reachable"); } diff --git a/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs b/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs index 406fee6b5e..704174c3f7 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs @@ -105,7 +105,7 @@ await QueryTarget(async db => } [Test] - public async Task Counts_only_the_rows_each_category_owns() + public async Task Counts_the_whole_table_for_the_required_copy_and_only_the_unresolved_rows_for_the_unresolved_copy() { var unresolved = new IngestedFailure(); var retryIssued = new IngestedFailure(); @@ -114,7 +114,6 @@ public async Task Counts_only_the_rows_each_category_owns() var offline = await CopyCategory(OfflineCategoryId); - // Written straight into the target, because this build has no category that copies archived messages. await QueryTarget(async db => { db.FailedMessages.Add(new FailedMessageEntity @@ -132,9 +131,9 @@ await QueryTarget(async db => using (Assert.EnterMultipleScope()) { - Assert.That(offline.Copied, Is.EqualTo(1), "an unresolved message without a claim waits for the copy that runs with ServiceControl open"); - Assert.That(await Target.Count(offlineCategory), Is.EqualTo(1), "the category's count takes in the archive's rows unless it honours the category's cohort"); - Assert.That(await QueryTarget(db => db.FailedMessages.CountAsync()), Is.EqualTo(2), "the filter is doing the work, not an empty table"); + Assert.That(offline.Copied, Is.EqualTo(2), "the unresolved message is the newest one, so it is a recent error and copies with the retry-issued message"); + Assert.That(await Target.Count(offlineCategory), Is.EqualTo(3), "the required count includes the extra archived row written directly to the shared table"); + Assert.That(await QueryTarget(db => db.FailedMessages.CountAsync()), Is.EqualTo(3)); } var online = await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); @@ -142,7 +141,8 @@ await QueryTarget(async db => using (Assert.EnterMultipleScope()) { - Assert.That(online.Copied, Is.EqualTo(1)); + Assert.That(online.Copied, Is.Zero); + Assert.That(online.AlreadyPresent, Is.EqualTo(1), "the required copy took the recent unresolved message"); Assert.That(await Target.Count(onlineCategory), Is.EqualTo(1)); } } @@ -271,7 +271,7 @@ public async Task Answers_for_a_copied_message_on_the_URL_it_had_on_RavenDB() } [Test] - public async Task The_required_copy_completes_the_category_before_the_host_opens_and_leaves_the_unresolved_messages_to_the_online_copy() + public async Task The_required_copy_completes_the_category_before_the_host_opens_with_the_recent_unresolved_message() { var retryIssued = Enumerable.Range(0, 3).Select(_ => new IngestedFailure()).ToArray(); @@ -289,14 +289,16 @@ public async Task The_required_copy_completes_the_category_before_the_host_opens using (Assert.EnterMultipleScope()) { Assert.That(checkpoint.State, Is.EqualTo(MigrationCategoryState.Complete)); - Assert.That(checkpoint.CopiedCount, Is.EqualTo(3)); + Assert.That(checkpoint.CopiedCount, Is.EqualTo(4), "the three retry-issued messages and the unresolved message, which is the newest and so recent"); Assert.That(checkpoint.SkippedCount, Is.Zero); Assert.That(checkpoint.SourceTotal, Is.Null, "the engine no longer counts the source before copying"); } await WaitUntil(async () => (await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages))?.State.IsFinished() == true, "the unresolved copy finished"); - Assert.That((await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages)).CopiedCount, Is.EqualTo(1)); + var online = await ReadCheckpoint(MigrationCategoryIds.UnresolvedFailedMessages); + + Assert.That((online.CopiedCount, online.AlreadyPresentCount), Is.EqualTo((0, 1)), "the unresolved copy finds the message the required copy took"); } [Test] diff --git a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs index 070312530e..ac4e85e1a1 100644 --- a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs +++ b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs @@ -186,16 +186,16 @@ await migrationDialect.InsertMissing(dbContext, [.. inserted } /// -/// Writes the retry-issued failed messages and the other messages that hold a retry claim, whatever their status. +/// Writes the retry-issued failed messages, the recent errors and the other messages that hold a retry claim, whatever their status. +/// Its count is every row of the failed message table, which is the rows this category copied only while ServiceControl is stopped +/// and nothing else has written to the table. Once ServiceControl has opened, live ingestion and the optional copies add rows to it. /// sealed class FailedMessagesWriter(IMigrationSqlDialect migrationDialect, IBodyStoragePersistence storagePersistence, EFPersisterSettings persisterSettings) : FailedMessagesWriterBase(migrationDialect, storagePersistence, persisterSettings) { public override string CategoryId => MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; - protected override IQueryable Copied(ServiceControlDbContext dbContext) => - dbContext.FailedMessages.Where(message => message.Status == FailedMessageStatus.RetryIssued - || dbContext.FailedMessageRetries.Any(claim => claim.UniqueMessageId == message.UniqueMessageId)); + protected override IQueryable Copied(ServiceControlDbContext dbContext) => dbContext.FailedMessages; } /// diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/FailedMessageStream.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/FailedMessageStream.cs index 7678e631e7..c2d2ef6527 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/FailedMessageStream.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/FailedMessageStream.cs @@ -20,11 +20,11 @@ namespace ServiceControl.Persistence.RavenDB.DataMigration; class FailedMessageStream(RavenReadOnlySourceLifecycle lifecycle) { /// - /// Builds the function that turns each streamed failed message into a row. The function returns null for a message whose - /// status is not in , and throws for a status that no + /// Builds the function that turns each streamed failed message into a row. The function returns null for a message that + /// rejects, and throws for a status that no /// failed message category copies. /// - public Func, MigrationRow?> Project(IReadOnlyCollection statuses) => + public Func, MigrationRow?> Project(IReadOnlyCollection statuses, DateTime? recentErrorsFrom = null) => result => { var status = result.Document.Status; @@ -34,7 +34,7 @@ class FailedMessageStream(RavenReadOnlySourceLifecycle lifecycle) throw new InvalidOperationException($"The failed message document '{result.Id}' has the status value {(int)status}, which no failed message category copies, so it would be left behind without a skip to show for it."); } - if (!statuses.Contains(status)) + if (!IsSelected(result, statuses, recentErrorsFrom)) { return null; } @@ -51,6 +51,14 @@ class FailedMessageStream(RavenReadOnlySourceLifecycle lifecycle) }); }; + /// + /// Whether a message has one of , or was last modified at or after + /// whatever its status. A message with no last-modified time counts as recent, so it reaches the writer and fails there by name instead of going missing. + /// + public static bool IsSelected(StreamResult result, IReadOnlyCollection statuses, DateTime? recentErrorsFrom) => + statuses.Contains(result.Document.Status) + || (recentErrorsFrom is { } from && (ReadMetadataDate(result, Constants.Documents.Metadata.LastModified) is not { } modified || modified >= from)); + /// /// Every status some failed message category copies. /// @@ -64,12 +72,13 @@ class FailedMessageStream(RavenReadOnlySourceLifecycle lifecycle) public const string ClaimHolderCursorPrefix = "claim-holder:"; /// - /// Reads the messages that hold a retry claim and are not retry-issued, whatever their status, after the claim document id in + /// Reads the messages that hold a retry claim and that the status phase did not read, which are neither retry-issued nor recent as + /// says, whatever their status, after the claim document id in /// , each row carrying as true. The claim documents /// stream in id order through , so memory stays bounded by the batch size. /// A claim whose message is gone makes no row. Every batch's cursor is and the last claim document id it walked. /// - public async IAsyncEnumerable ClaimHolders(string categoryId, string? resumeAfter, int batchSize, [EnumeratorCancellation] CancellationToken cancellationToken = default) + public async IAsyncEnumerable ClaimHolders(string categoryId, string? resumeAfter, int batchSize, DateTime? recentErrorsFrom, [EnumeratorCancellation] CancellationToken cancellationToken = default) { var claimPrefix = RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(""); var project = Project(CopiedStatuses); @@ -93,8 +102,8 @@ public async IAsyncEnumerable ClaimHolders(string categoryId, st // The metadata is valid only while the stream is on its result, so each result becomes a row as it arrives. while (await results.MoveNextAsync()) { - // A retry-issued message is already in the batches before these. - if (results.Current.Document.Status != FailedMessageStatus.RetryIssued) + // A retry-issued or recent message is already in the batches before these, with its claim resolved. + if (!IsSelected(results.Current, MigrationCategoryRegistry.RetryIssuedStatuses, recentErrorsFrom)) { var row = project(results.Current)!; found[results.Current.Id] = row with { Metadata = new Dictionary(row.Metadata) { [MigrationMetadataKeys.RetryClaimHeld] = true } }; @@ -108,13 +117,13 @@ public async IAsyncEnumerable ClaimHolders(string categoryId, st } /// - /// Returns the batch with every retry-issued row carrying , true when - /// RavenDB still holds a retry claim for the message. It looks up every claim in the batch with one request, and returns a - /// batch with no retry-issued row as it is. + /// Returns the batch with every row carrying , true when + /// RavenDB still holds a retry claim for the message. It looks up every claim in the batch with one request, and returns an + /// empty batch as it is. /// public async Task ResolveRetryClaims(MigrationBatch batch, CancellationToken cancellationToken = default) { - string[] claimIds = [.. batch.Rows.Where(IsRetryIssued).Select(ClaimId)]; + string[] claimIds = [.. batch.Rows.Select(ClaimId)]; if (claimIds.Length == 0) { @@ -128,14 +137,10 @@ public async Task ResolveRetryClaims(MigrationBatch batch, Cance return batch with { - Rows = [.. batch.Rows.Select(row => IsRetryIssued(row) - ? row with { Metadata = new Dictionary(row.Metadata) { [MigrationMetadataKeys.RetryClaimHeld] = held.Contains(ClaimId(row)) } } - : row)] + Rows = [.. batch.Rows.Select(row => row with { Metadata = new Dictionary(row.Metadata) { [MigrationMetadataKeys.RetryClaimHeld] = held.Contains(ClaimId(row)) } })] }; } - static bool IsRetryIssued(MigrationRow row) => ((FailedMessage)row.Document).Status == FailedMessageStatus.RetryIssued; - static string ClaimId(MigrationRow row) => RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(((FailedMessage)row.Document).UniqueMessageId); // The translator checks TimeSent, SendingEndpoint and ReceivingEndpoint by exact type, but RavenDB can hand them back as text or untyped objects. diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs index a96980273c..92d691e8c8 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs @@ -10,10 +10,12 @@ namespace ServiceControl.Persistence.RavenDB.DataMigration.Readers; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Raven.Client; using Raven.Client.Documents.Commands; using Raven.Client.Documents.Indexes; using Raven.Client.Documents.Operations; using Raven.Client.Documents.Operations.Indexes; +using Raven.Client.Documents.Queries; using Raven.Client.Documents.Session; using Raven.Client.Exceptions; using ServiceControl.Infrastructure; @@ -22,38 +24,89 @@ namespace ServiceControl.Persistence.RavenDB.DataMigration.Readers; using ServiceControl.Persistence.DataMigration; /// -/// Reads the failed messages with one of a category's statuses. The archived and resolved messages in the same collection can +/// Reads the failed messages with one of a category's statuses, and optionally every message last modified at or after a cutoff. The archived and resolved messages in the same collection can /// outnumber the others many times, so it queries for just these statuses instead of reading them all. /// abstract class FailedMessageStatusReader(RavenReadOnlySourceLifecycle lifecycle, IReadOnlyCollection statuses) : MigrationCategoryReader(lifecycle) { /// - /// Waits for to catch up, then streams the messages with the category's statuses in id - /// order, starting after . When the index is missing, errored or disabled, or does not hold - /// every failed message with a status some category copies, it logs a warning and reads the whole collection instead. - /// Both ways use the document id as the cursor, so a copy can switch from one to the other between runs. + /// Streams the messages with the category's statuses in id order, starting after , through + /// once says which way to read. + /// + protected async IAsyncEnumerable ReadStatuses(string? resumeAfter, int batchSize, [EnumeratorCancellation] CancellationToken cancellationToken = default) + { + await foreach (var batch in ReadStatuses(await CanQueryIndex(cancellationToken), resumeAfter, batchSize, null, cancellationToken)) + { + yield return batch; + } + } + + /// + /// Streams the messages with the category's statuses, or last modified at or after when it is + /// not null, in id order, starting after , through the index or the whole collection as + /// says. Both ways use the document id as the cursor, so a copy can switch from one to the other between runs. /// Throws when the source holds no document with the id in , /// or when a failed message has a status no category copies. /// - protected async IAsyncEnumerable ReadStatuses(string? resumeAfter, int batchSize, [EnumeratorCancellation] CancellationToken cancellationToken = default) + protected IAsyncEnumerable ReadStatuses(bool throughIndex, string? resumeAfter, int batchSize, DateTime? recentErrorsFrom, CancellationToken cancellationToken = default) { var databaseName = Lifecycle.Settings.DatabaseName; - var project = new FailedMessageStream(Lifecycle).Project(statuses); + var project = new FailedMessageStream(Lifecycle).Project(statuses, recentErrorsFrom); - var batches = await IndexHoldsEveryCopiedFailedMessage(databaseName, cancellationToken) - ? ThroughIndex(databaseName, resumeAfter, batchSize, project, cancellationToken) + return throughIndex + ? ThroughIndex(databaseName, resumeAfter, batchSize, recentErrorsFrom, project, cancellationToken) : Documents($"{FailedMessageIdGenerator.CollectionName}/", resumeAfter, batchSize, project, cancellationToken); + } + + /// + /// Waits for to catch up, and says whether it can be queried. When the index is missing, + /// errored or disabled, or does not hold every failed message with a status some category copies, it logs a warning and says no, + /// and the whole collection is read instead. + /// + protected Task CanQueryIndex(CancellationToken cancellationToken = default) => + IndexHoldsEveryCopiedFailedMessage(Lifecycle.Settings.DatabaseName, cancellationToken); - await foreach (var batch in batches) + /// + /// The RavenDB last-modified time of the unresolved message modified most recently, or null when there is none. The source is + /// stopped while it is read, so every call returns the same time. Through the index it is one query. Otherwise it walks every + /// failed message once, holding only the newest time. + /// + protected async Task LatestUnresolvedLastModified(bool throughIndex, CancellationToken cancellationToken = default) + { + using var session = Lifecycle.OpenSession(Lifecycle.Settings.DatabaseName); + + if (throughIndex) { - yield return batch; + var query = Query(session) + .WhereEquals(nameof(FailedMessage.Status), (int)FailedMessageStatus.Unresolved) + .OrderByDescending(nameof(FailedMessageViewIndex.SortAndFilterOptions.LastModified), OrderingType.Long) + .Take(1); + await using var newest = await session.Advanced.StreamAsync(query.OfType(), cancellationToken); + + return await newest.MoveNextAsync() ? FailedMessageStream.ReadMetadataDate(newest.Current, Constants.Documents.Metadata.LastModified) : null; } + + DateTime? latest = null; + await using var all = await session.Advanced.StreamAsync($"{FailedMessageIdGenerator.CollectionName}/", token: cancellationToken); + + while (await all.MoveNextAsync()) + { + if (all.Current.Document.Status == FailedMessageStatus.Unresolved + && FailedMessageStream.ReadMetadataDate(all.Current, Constants.Documents.Metadata.LastModified) is { } modified + && (latest is null || modified > latest)) + { + latest = modified; + } + } + + return latest; } async IAsyncEnumerable ThroughIndex( string databaseName, string? resumeAfter, int batchSize, + DateTime? recentErrorsFrom, Func, MigrationRow?> project, [EnumeratorCancellation] CancellationToken cancellationToken) { @@ -61,7 +114,15 @@ async IAsyncEnumerable ThroughIndex( await RavenDocumentStream.RefuseMissingCursor(session, CategoryId, databaseName, resumeAfter, cancellationToken); - var query = Query(session).WhereIn(nameof(FailedMessage.Status), StatusValues(statuses)); + var query = Query(session).OpenSubclause().WhereIn(nameof(FailedMessage.Status), StatusValues(statuses)); + + if (recentErrorsFrom is { } from) + { + // The same comparison as FailedMessageStream.IsSelected, on the ticks the index holds. + query = query.OrElse().WhereGreaterThanOrEqual(nameof(FailedMessageViewIndex.SortAndFilterOptions.LastModified), from.Ticks); + } + + query = query.CloseSubclause(); if (resumeAfter is not null) { diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs index 0a668e6b5d..63df82ff3c 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs @@ -9,26 +9,33 @@ namespace ServiceControl.Persistence.RavenDB.DataMigration.Readers; using ServiceControl.Persistence.DataMigration; /// -/// Reads the retry-issued messages and the messages that hold a retry claim, which are copied while ServiceControl is closed: every -/// retry-issued message, whether or not its retry claim remains, and then every other message that still holds a retry claim, -/// whatever its status. The unresolved messages without a claim are 's. +/// Reads what is copied while ServiceControl is closed: every retry-issued message, every message that holds a retry claim whatever +/// its status and age, and the recent errors, which are the messages of any status last modified at or after the newest unresolved +/// message's last-modified time less one hour. With no unresolved message there are no recent errors. +/// Every ServiceControl instance is stopped while a source is copied, so the newest unresolved message does not change between reads. +/// A message is read once, and every row says whether RavenDB still holds its retry claim. +/// The older unresolved messages without a claim are 's. /// sealed class FailedMessagesReader(RavenReadOnlySourceLifecycle lifecycle) : FailedMessageStatusReader(lifecycle, MigrationCategoryRegistry.RetryIssuedStatuses) { + static readonly TimeSpan RecentErrorsWindow = TimeSpan.FromHours(1); + public override string CategoryId => MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; /// - /// Streams the retry-issued messages through , then the claim holders, + /// Streams the retry-issued and recent messages through , then the claim holders, /// whose cursors start with . A resume from such a cursor skips the first part. /// public override async IAsyncEnumerable Read(string? resumeAfter, int batchSize, [EnumeratorCancellation] CancellationToken cancellationToken = default) { var stream = new FailedMessageStream(Lifecycle); + var throughIndex = await CanQueryIndex(cancellationToken); + var recentErrorsFrom = await LatestUnresolvedLastModified(throughIndex, cancellationToken) - RecentErrorsWindow; var inClaimHolders = resumeAfter?.StartsWith(FailedMessageStream.ClaimHolderCursorPrefix, StringComparison.Ordinal) == true; if (!inClaimHolders) { - await foreach (var batch in ReadStatuses(resumeAfter, batchSize, cancellationToken)) + await foreach (var batch in ReadStatuses(throughIndex, resumeAfter, batchSize, recentErrorsFrom, cancellationToken)) { yield return await stream.ResolveRetryClaims(batch, cancellationToken); } @@ -36,7 +43,7 @@ public override async IAsyncEnumerable Read(string? resumeAfter, var claimHoldersAfter = inClaimHolders ? resumeAfter![FailedMessageStream.ClaimHolderCursorPrefix.Length..] : null; - await foreach (var batch in stream.ClaimHolders(CategoryId, claimHoldersAfter, batchSize, cancellationToken)) + await foreach (var batch in stream.ClaimHolders(CategoryId, claimHoldersAfter, batchSize, recentErrorsFrom, cancellationToken)) { yield return batch; } diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTestBase.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTestBase.cs index f940d6f8ba..8172f07b50 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTestBase.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTestBase.cs @@ -2,8 +2,15 @@ namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; using System; using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using NUnit.Framework; +using Raven.Client.Documents.Smuggler; +using ServiceControl.MessageFailures; using ServiceControl.Configuration; using ServiceControl.Persistence.DataMigration; using ServiceControl.Persistence.RavenDB; @@ -55,6 +62,39 @@ protected async Task OpenMigrationSource() return source; } + // RavenDB assigns last-modified from its own clock, but a smuggler import keeps the one carried in the document. + protected async Task SeedFailedMessage(FailedMessage failedMessage, DateTime lastModified) + { + await SeedFailedMessage(failedMessage); + + JObject document; + using (var session = DocumentStore.OpenAsyncSession()) + { + var stored = await session.LoadAsync(failedMessage.Id); + document = JObject.FromObject(stored); + var metadata = session.Advanced.GetMetadataFor(stored); + document["@metadata"] = new JObject + { + ["@id"] = failedMessage.Id, + ["@collection"] = (string)metadata["@collection"], + ["Raven-Clr-Type"] = (string)metadata["Raven-Clr-Type"], + ["@last-modified"] = lastModified.ToUniversalTime() + }; + } + + using var dump = new MemoryStream(); + using (var gzip = new GZipStream(dump, CompressionMode.Compress, leaveOpen: true)) + { + gzip.Write(Encoding.UTF8.GetBytes(new JObject { ["Docs"] = new JArray(document) }.ToString(Formatting.None))); + } + + dump.Position = 0; + var import = await DocumentStore.Smuggler.ImportAsync(new DatabaseSmugglerImportOptions(), dump, TestContext.CurrentContext.CancellationToken); + await import.WaitForCompletionAsync(); + + return failedMessage; + } + protected static async Task> CollectBatches(IMigrationSource source, MigrationCategory category, string resumeAfter = null, int batchSize = 100) { var batches = new List(); diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RecentErrorsSourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RecentErrorsSourceTests.cs new file mode 100644 index 0000000000..a8d80fa7be --- /dev/null +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RecentErrorsSourceTests.cs @@ -0,0 +1,232 @@ +namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using NUnit.Framework; +using Raven.Client.Documents.Operations.Indexes; +using ServiceControl.MessageFailures; +using ServiceControl.MessageFailures.Api; +using ServiceControl.Persistence.DataMigration; +using ServiceControl.Persistence.RavenDB; + +/// +/// The required failed message category reads the retry-issued messages, the claim holders and the recent errors, which are the +/// messages of any status last modified at or after the newest unresolved message's last-modified time less a window. +/// Each test runs through the index and through the full-collection read, which must read the same rows. +/// +[TestFixture] +class RecentErrorsSourceTests : RavenMigrationSourceTestBase +{ + static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + static readonly FailedMessageStatus[] AllStatuses = [FailedMessageStatus.Unresolved, FailedMessageStatus.Archived, FailedMessageStatus.Resolved, FailedMessageStatus.RetryIssued]; + + [Test] + public async Task Reads_every_status_modified_at_or_after_the_cutoff_and_only_the_retry_issued_before_it([Values] bool throughIndex) + { + await UseReadPath(throughIndex); + var newestUnresolved = await Seed(FailedMessageStatus.Unresolved); + await CompleteDatabaseOperation(); + var anchor = await LastModified(newestUnresolved); + var cutoff = anchor - TimeSpan.FromHours(1); + + var before = new List(); + foreach (var status in AllStatuses) + { + before.Add(await Seed(status, cutoff - TimeSpan.FromHours(1))); + } + + var tickBefore = await Seed(FailedMessageStatus.Archived, cutoff - TimeSpan.FromTicks(1)); + var boundary = await Seed(FailedMessageStatus.Archived, cutoff); + var between = new List(); + var afterAnchor = new List(); + foreach (var status in AllStatuses) + { + between.Add(await Seed(status, anchor - TimeSpan.FromMinutes(30))); + + // A later unresolved message would move the anchor. + if (status != FailedMessageStatus.Unresolved) + { + afterAnchor.Add(await Seed(status, anchor + TimeSpan.FromMinutes(30))); + } + } + + await CompleteDatabaseOperation(); + + var oldRetryIssued = before.Single(message => message.Status == FailedMessageStatus.RetryIssued); + var expected = between.Concat(afterAnchor).Concat([boundary, newestUnresolved, oldRetryIssued]).Select(message => message.Id); + + var rows = await ReadAll(); + + Assert.That(rows.Select(row => row.SourceId), Is.EquivalentTo(expected), "a message modified at the cutoff or later is recent whatever its status, with no upper bound, and one a tick before it is read only when retry-issued"); + Assert.That(rows.Select(row => row.SourceId), Does.Not.Contain(tickBefore.Id)); + } + + [Test] + public async Task Reads_no_recent_errors_when_there_is_no_unresolved_message([Values] bool throughIndex) + { + await UseReadPath(throughIndex); + var retryIssued = await Seed(FailedMessageStatus.RetryIssued); + var claimedArchived = await Seed(FailedMessageStatus.Archived); + var claimedResolved = await Seed(FailedMessageStatus.Resolved); + await Seed(FailedMessageStatus.Archived); + await Seed(FailedMessageStatus.Resolved); + await SeedClaim(claimedArchived); + await SeedClaim(claimedResolved); + await CompleteDatabaseOperation(); + + var rows = await ReadAll(); + + Assert.That(rows.Select(row => row.SourceId), Is.EquivalentTo(new[] { retryIssued.Id, claimedArchived.Id, claimedResolved.Id }), "every retry-issued message and claim holder is read, and the recent hour is not expanded without an anchor"); + } + + [Test] + public async Task Reads_each_message_once_with_the_claim_it_holds([Values] bool throughIndex) + { + await UseReadPath(throughIndex); + var oldRetryIssuedClaimed = await Seed(FailedMessageStatus.RetryIssued, TwoHoursAgo); + var oldRetryIssuedReleased = await Seed(FailedMessageStatus.RetryIssued, TwoHoursAgo); + var oldUnresolvedClaimed = await Seed(FailedMessageStatus.Unresolved, TwoHoursAgo); + var oldArchivedClaimed = await Seed(FailedMessageStatus.Archived, TwoHoursAgo); + var oldResolvedClaimed = await Seed(FailedMessageStatus.Resolved, TwoHoursAgo); + var oldArchivedPlain = await Seed(FailedMessageStatus.Archived, TwoHoursAgo); + var oldUnresolvedPlain = await Seed(FailedMessageStatus.Unresolved, TwoHoursAgo); + var cutoffMessage = await Seed(FailedMessageStatus.Resolved); + var recentRetryIssuedClaimed = await Seed(FailedMessageStatus.RetryIssued); + var recentUnresolvedClaimed = await Seed(FailedMessageStatus.Unresolved); + var recentArchivedClaimed = await Seed(FailedMessageStatus.Archived); + var recentResolvedPlain = await Seed(FailedMessageStatus.Resolved); + var newestUnresolved = await Seed(FailedMessageStatus.Unresolved); + + foreach (var claimed in new[] { oldRetryIssuedClaimed, oldUnresolvedClaimed, oldArchivedClaimed, oldResolvedClaimed, recentRetryIssuedClaimed, recentUnresolvedClaimed, recentArchivedClaimed }) + { + await SeedClaim(claimed); + } + + await CompleteDatabaseOperation(); + + var rows = await ReadAll(); + var claimHeld = rows.ToDictionary(row => row.SourceId, row => (bool)row.Metadata[MigrationMetadataKeys.RetryClaimHeld]); + + using (Assert.EnterMultipleScope()) + { + Assert.That(rows, Has.Count.EqualTo(rows.Select(row => row.SourceId).Distinct().Count()), "an overlap between the status phase and the claim phase would read a message twice"); + Assert.That(claimHeld.Keys, Is.EquivalentTo(new[] + { + oldRetryIssuedClaimed, oldRetryIssuedReleased, oldUnresolvedClaimed, oldArchivedClaimed, oldResolvedClaimed, + cutoffMessage, recentRetryIssuedClaimed, recentUnresolvedClaimed, recentArchivedClaimed, recentResolvedPlain, newestUnresolved + }.Select(message => message.Id))); + Assert.That(claimHeld.Where(pair => pair.Value).Select(pair => pair.Key), Is.EquivalentTo(new[] + { + oldRetryIssuedClaimed, oldUnresolvedClaimed, oldArchivedClaimed, oldResolvedClaimed, recentRetryIssuedClaimed, recentUnresolvedClaimed, recentArchivedClaimed + }.Select(message => message.Id)), "every message that holds a claim says so, whatever its status, age or phase, and the others say they hold none"); + Assert.That(claimHeld.ContainsKey(oldArchivedPlain.Id) || claimHeld.ContainsKey(oldUnresolvedPlain.Id), Is.False, "an older message without a claim waits for the optional copy"); + Assert.That(await CountThrough(), Is.EqualTo(rows.Count), "the count reads the same set the copy does"); + } + } + + [Test] + public async Task A_resume_from_any_batch_cursor_reads_the_rest_without_a_gap_or_a_repeat([Values] bool throughIndex) + { + await UseReadPath(throughIndex); + var seeded = new List(); + for (var count = 0; count < 4; count++) + { + seeded.Add(await Seed(AllStatuses[count % AllStatuses.Length], TwoHoursAgo)); + } + + var cutoffMessage = await Seed(FailedMessageStatus.Unresolved); + for (var count = 0; count < 9; count++) + { + var message = await Seed(AllStatuses[count % AllStatuses.Length]); + if (count % 3 == 0) + { + await SeedClaim(message); + } + } + + var newestUnresolved = await Seed(FailedMessageStatus.Unresolved); + + foreach (var oldMessage in seeded.Where(message => message.Status != FailedMessageStatus.RetryIssued)) + { + await SeedClaim(oldMessage); + } + + await CompleteDatabaseOperation(); + + await using var source = await OpenMigrationSource(); + var batches = await CollectBatches(source, Category, batchSize: 2); + var all = batches.SelectMany(batch => batch.Rows).Select(row => row.SourceId).ToArray(); + + Assert.That(batches.Select(batch => batch.Cursor), Has.Some.StartWith("claim-holder:"), "the set must cross the phase boundary for the test to prove anything"); + Assert.That(all, Is.Unique); + + for (var resumeAt = 0; resumeAt < batches.Count; resumeAt++) + { + var resumed = await CollectBatches(source, Category, resumeAfter: batches[resumeAt].Cursor, batchSize: 2); + + Assert.That(resumed.SelectMany(batch => batch.Rows).Select(row => row.SourceId), Is.EqualTo(batches.Skip(resumeAt + 1).SelectMany(batch => batch.Rows).Select(row => row.SourceId)), $"resuming after batch {resumeAt}"); + } + } + + [Test] + public async Task A_second_source_reads_the_same_rows_because_the_anchor_comes_from_the_source([Values] bool throughIndex) + { + await UseReadPath(throughIndex); + await Seed(FailedMessageStatus.Archived); + await Seed(FailedMessageStatus.Unresolved); + await Seed(FailedMessageStatus.Resolved); + await Seed(FailedMessageStatus.Unresolved); + await CompleteDatabaseOperation(); + + var first = (await ReadAll()).Select(row => row.SourceId).ToArray(); + var restarted = (await ReadAll()).Select(row => row.SourceId).ToArray(); + + Assert.That(first, Has.Length.EqualTo(4)); + Assert.That(restarted, Is.EqualTo(first), "a restart or a retry derives the same cutoff, with nothing persisted"); + } + + async Task UseReadPath(bool throughIndex) + { + if (!throughIndex) + { + await DocumentStore.Maintenance.SendAsync(new DeleteIndexOperation(new FailedMessageViewIndex().IndexName), TestContext.CurrentContext.CancellationToken); + } + } + + async Task Seed(FailedMessageStatus status) => await SeedFailedMessage(new IngestedFailure().ToFailedMessage(status)); + + async Task Seed(FailedMessageStatus status, DateTime lastModified) => await SeedFailedMessage(new IngestedFailure().ToFailedMessage(status), lastModified); + + static DateTime TwoHoursAgo => DateTime.UtcNow.AddHours(-2); + + async Task LastModified(FailedMessage message) + { + using var session = DocumentStore.OpenAsyncSession(); + var loaded = await session.LoadAsync(message.Id); + + return DateTime.SpecifyKind(session.Advanced.GetLastModifiedFor(loaded).Value, DateTimeKind.Utc); + } + + async Task SeedClaim(FailedMessage message) + { + using var session = DocumentStore.OpenAsyncSession(); + await session.StoreAsync(new FailedMessageRetry { FailedMessageId = message.Id, RetryBatchId = "RetryBatches/forwarded" }, RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(message.UniqueMessageId)); + await session.SaveChangesAsync(); + } + + async Task> ReadAll() + { + await using var source = await OpenMigrationSource(); + + return (await CollectBatches(source, Category)).SelectMany(batch => batch.Rows).ToList(); + } + + async Task CountThrough() + { + await using var source = await OpenMigrationSource(); + + return await source.Count(Category); + } +} diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs index d7c5c276b7..8f0c202906 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs @@ -16,11 +16,19 @@ class RetryIssuedFailedMessageSourceTests : RavenMigrationSourceTestBase { static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + FailedMessage newestUnresolved; + + // Every message a test seeds is two hours older than this one, so none is recent and the status and claim rules stand alone. + [SetUp] + public async Task SeedNewestUnresolved() => newestUnresolved = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + + Task SeedOld(FailedMessageStatus status) => SeedFailedMessage(new IngestedFailure().ToFailedMessage(status), DateTime.UtcNow.AddHours(-2)); + [Test] public async Task Reads_every_retry_issued_message_whether_or_not_its_claim_remains() { - var claimed = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); - var released = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + var claimed = await SeedOld(FailedMessageStatus.RetryIssued); + var released = await SeedOld(FailedMessageStatus.RetryIssued); await SeedClaim(claimed); await CompleteDatabaseOperation(); @@ -37,12 +45,12 @@ public async Task Reads_every_retry_issued_message_whether_or_not_its_claim_rema [Test] public async Task Reads_a_message_that_holds_a_claim_whatever_its_status_and_no_message_without_one() { - var unresolved = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); - var archived = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); - var resolved = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Resolved)); - await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); - await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); - await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Resolved)); + var unresolved = await SeedOld(FailedMessageStatus.Unresolved); + var archived = await SeedOld(FailedMessageStatus.Archived); + var resolved = await SeedOld(FailedMessageStatus.Resolved); + await SeedOld(FailedMessageStatus.Unresolved); + await SeedOld(FailedMessageStatus.Archived); + await SeedOld(FailedMessageStatus.Resolved); await SeedClaim(unresolved); await SeedClaim(archived); await SeedClaim(resolved); @@ -61,7 +69,7 @@ public async Task Reads_a_message_that_holds_a_claim_whatever_its_status_and_no_ [Test] public async Task Reads_an_unresolved_message_without_a_claim_in_neither_category_phase() { - await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + await SeedOld(FailedMessageStatus.Unresolved); await CompleteDatabaseOperation(); Assert.That(await ReadAll(), Is.Empty, "the unresolved copy that runs with the host open owns it"); @@ -70,7 +78,7 @@ public async Task Reads_an_unresolved_message_without_a_claim_in_neither_categor [Test] public async Task Reads_a_retry_issued_message_with_a_claim_once() { - var message = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + var message = await SeedOld(FailedMessageStatus.RetryIssued); await SeedClaim(message); await CompleteDatabaseOperation(); @@ -81,7 +89,7 @@ public async Task Reads_a_retry_issued_message_with_a_claim_once() public async Task Skips_a_claim_whose_message_is_gone() { var gone = new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived); - var present = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + var present = await SeedOld(FailedMessageStatus.Archived); await SeedClaim(present); await SeedClaim(FailedMessageIdGenerator.MakeDocumentId(gone.UniqueMessageId), gone.UniqueMessageId); await CompleteDatabaseOperation(); @@ -93,11 +101,11 @@ public async Task Skips_a_claim_whose_message_is_gone() public async Task The_claim_holders_follow_the_retry_issued_messages_and_their_cursor_resumes_in_that_phase() { var holders = new List(); - var retryIssued = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); + var retryIssued = await SeedOld(FailedMessageStatus.RetryIssued); for (var count = 0; count < 5; count++) { - var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(count % 2 == 0 ? FailedMessageStatus.Archived : FailedMessageStatus.Unresolved)); + var holder = await SeedOld(count % 2 == 0 ? FailedMessageStatus.Archived : FailedMessageStatus.Unresolved); await SeedClaim(holder); holders.Add(holder); } @@ -114,33 +122,33 @@ public async Task The_claim_holders_follow_the_retry_issued_messages_and_their_c using (Assert.EnterMultipleScope()) { - Assert.That(batches.SelectMany(batch => batch.Rows).Select(row => row.SourceId), Is.EqualTo(new[] { retryIssued.Id }.Concat(holderIds)), "retry-issued first, then every claim holder once, in id order"); + Assert.That(batches.SelectMany(batch => batch.Rows).Select(row => row.SourceId).Where(id => id != newestUnresolved.Id), Is.EqualTo(new[] { retryIssued.Id }.Concat(holderIds)), "retry-issued first, then every claim holder once, in id order"); Assert.That(resumed.SelectMany(batch => batch.Rows).Select(row => row.SourceId), Is.EqualTo(batches.Skip(2).SelectMany(batch => batch.Rows).Select(row => row.SourceId)), "a claim-holder cursor resumes after its message and does not look for it in the retry-issued phase"); } } [Test] - public async Task A_cursor_from_the_earlier_layout_still_copies_the_claim_holders_before_it() + public async Task A_status_phase_cursor_still_copies_older_claim_holders_before_it() { - var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + var holder = await SeedOld(FailedMessageStatus.Archived); await SeedClaim(holder); - var later = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + var later = await SeedOld(FailedMessageStatus.Unresolved); await CompleteDatabaseOperation(); var cursorPastTheHolder = new[] { holder.Id, later.Id }.Max(StringComparer.Ordinal); await using var source = await OpenMigrationSource(); - var rows = (await CollectBatches(source, Category, resumeAfter: cursorPastTheHolder)).SelectMany(batch => batch.Rows).ToList(); + var rows = (await CollectBatches(source, Category, resumeAfter: cursorPastTheHolder)).SelectMany(batch => batch.Rows).Where(row => row.SourceId != newestUnresolved.Id).ToList(); - Assert.That(rows.Select(row => row.SourceId), Is.EqualTo(new[] { holder.Id }), "the earlier layout never copied an archived claim holder, so the claim phase starts from the beginning whatever the plain cursor is"); + Assert.That(rows.Select(row => row.SourceId), Is.EqualTo(new[] { holder.Id }), "a claim holder outside the recent set is read in the claim phase even when its message id precedes the status cursor"); } [Test] public async Task Reads_the_claim_holders_when_the_index_is_missing() { await DocumentStore.Maintenance.SendAsync(new DeleteIndexOperation(new FailedMessageViewIndex().IndexName), TestContext.CurrentContext.CancellationToken); - var retryIssued = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); - var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Resolved)); - await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + var retryIssued = await SeedOld(FailedMessageStatus.RetryIssued); + var holder = await SeedOld(FailedMessageStatus.Resolved); + await SeedOld(FailedMessageStatus.Unresolved); await SeedClaim(holder); var rows = await ReadAll().WaitAsync(TimeSpan.FromSeconds(30), TestContext.CurrentContext.CancellationToken); @@ -151,21 +159,21 @@ public async Task Reads_the_claim_holders_when_the_index_is_missing() [Test] public async Task Counts_the_rows_the_copy_reads() { - var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + var holder = await SeedOld(FailedMessageStatus.Archived); await SeedClaim(holder); - await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)); - await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Unresolved)); + await SeedOld(FailedMessageStatus.RetryIssued); + await SeedOld(FailedMessageStatus.Unresolved); await CompleteDatabaseOperation(); await using var source = await OpenMigrationSource(); - Assert.That(await source.Count(Category), Is.EqualTo(2)); + Assert.That(await source.Count(Category), Is.EqualTo(3), "the holder, the retry-issued message and the newest unresolved message"); } [Test] public async Task Carries_the_last_modified_of_a_claim_holder() { - var holder = await SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived)); + var holder = await SeedOld(FailedMessageStatus.Archived); await SeedClaim(holder); await CompleteDatabaseOperation(); @@ -187,6 +195,6 @@ async Task> ReadAll() { await using var source = await OpenMigrationSource(); - return (await CollectBatches(source, Category)).SelectMany(batch => batch.Rows).ToList(); + return (await CollectBatches(source, Category)).SelectMany(batch => batch.Rows).Where(row => row.SourceId != newestUnresolved.Id).ToList(); } } diff --git a/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs index 62ce59904d..1e30bb6818 100644 --- a/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs +++ b/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs @@ -485,7 +485,7 @@ public async Task A_changed_message_the_target_already_holds_is_not_reported_aga } [Test] - public async Task Count_takes_in_retry_issued_messages_and_claim_holders_of_any_status() + public async Task Count_of_the_required_copy_takes_in_every_failed_message_row_whatever_its_status_or_claim() { var holder = new IngestedFailure().ToFailedMessage(FailedMessageStatus.Archived); await Target.Write(FailedMessagesCategory, BatchOf( @@ -502,8 +502,8 @@ await Query(db => using (Assert.EnterMultipleScope()) { - Assert.That(await Target.Count(FailedMessagesCategory), Is.EqualTo(2), "an unresolved message without a claim and the archived and resolved rows without one belong to other copies"); - Assert.That(await Query(db => db.FailedMessages.CountAsync()), Is.EqualTo(5), "the filter is doing the work, not an empty table"); + Assert.That(await Target.Count(FailedMessagesCategory), Is.EqualTo(5), "while ServiceControl is stopped the table holds only what the required copy wrote, recent errors included, so the count is the whole table"); + Assert.That(await Query(db => db.FailedMessages.CountAsync()), Is.EqualTo(5)); } } From fa6bd8c02d5426d7e1a0ca79fee30c9fdba79a9a Mon Sep 17 00:00:00 2001 From: Rhys Bevilaqua Date: Tue, 24 Nov 2026 12:02:47 +0800 Subject: [PATCH 7/8] Clarify documents and comments --- docs/migration/ravendb-to-sql-migration-instructions.md | 6 +++--- docs/migration/ravendb-to-sql-migration-overview.md | 6 +++--- docs/migration/ravendb-to-sql-migration-system-design.md | 2 +- .../DataMigration/Readers/FailedMessageStatusReader.cs | 6 +++--- .../DataMigration/Readers/FailedMessagesReader.cs | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/migration/ravendb-to-sql-migration-instructions.md b/docs/migration/ravendb-to-sql-migration-instructions.md index 8aedcf0fab..ee2a4830b1 100644 --- a/docs/migration/ravendb-to-sql-migration-instructions.md +++ b/docs/migration/ravendb-to-sql-migration-instructions.md @@ -9,7 +9,7 @@ The migration does not move the audit instance or the monitoring instance. The a ## How the migration runs -Run `--start-data-migration` with ServiceControl stopped to copy the data it needs to open, including the failed messages RavenDB last modified in the hour before its newest unresolved message. **This part is your outage**. After the command succeeds, start ServiceControl to copy the unresolved failed messages, the group comments, the event log and the archived and resolved failed messages in the background. **The migration never writes to RavenDB**. +Run `--start-data-migration` with ServiceControl stopped to copy the data it needs to open, including every failed message RavenDB last modified at or after one hour before its newest unresolved message's last-modified time. There is no upper bound. **This part is your outage**. After the command succeeds, start ServiceControl to copy the unresolved failed messages, the group comments, the event log and the archived and resolved failed messages in the background. **The migration never writes to RavenDB**. > [!IMPORTANT] > Until ServiceControl opens, you can go back to RavenDB and lose nothing. After it opens, you cannot go back. @@ -70,7 +70,7 @@ Make sure that your setup is supported: Get the data ready: -- Let retries finish where you can. ServiceControl stays closed until every retry-issued message, every message that holds a retry claim and every failed message of any status that RavenDB last modified in the hour before the newest unresolved message is copied, and a retry-issued message never ages out. A busy instance that fails many messages in an hour has a longer outage for it, and a retry, archive or resolve in that hour counts too. Older unresolved messages without a claim copy after ServiceControl opens, so a large unresolved backlog does not lengthen the outage, but it does lengthen the background copy. The hour is counted back from the newest unresolved message in RavenDB, which does not change while ServiceControl is stopped, so a restart or a retry of the category copies the same messages. With no unresolved message only the retry-issued messages and claim holders copy. Archived and resolved messages outside the recent set that hold no claim copy only after ServiceControl opens. The exception is a RavenDB index that is broken or leaves out some failed messages: then the copy reads every failed message during the outage, logs a warning, and a large archive makes the outage longer too. +- Let retries finish where you can. ServiceControl stays closed until every retry-issued message, every message that holds a retry claim and every failed message of any status that RavenDB last modified at or after one hour before the newest unresolved message's last-modified time is copied. A retry-issued message never ages out. There is no upper bound on the recent set. A retry, archive or resolve after the newest unresolved message's last-modified time counts too and can lengthen the outage. Older unresolved messages without a claim copy after ServiceControl opens, so a large unresolved backlog does not lengthen the outage, but it does lengthen the background copy. The cutoff is the newest unresolved message's last-modified time minus one hour. It stays the same across restarts and retries unless RavenDB expiration removes that message. Expiration can move the cutoff earlier or leave no unresolved message. With no unresolved message only the retry-issued messages and claim holders copy. Archived and resolved messages outside the recent set that hold no claim copy only after ServiceControl opens. The exception is a RavenDB index that is broken or leaves out some failed messages: then the copy reads every failed message during the outage, logs a warning, and a large archive makes the outage longer too. - Run `--import-failed-errors` while the instance is still on RavenDB. A failed error import is a message that ServiceControl took off the error queue but failed to store. The migration copies these either way, but imported ones arrive as ordinary failed messages. - Let retry, archive and unarchive operations finish on RavenDB before stopping ServiceControl. The offline command refuses persisted batches, the retry forwarding marker, and archive or unarchive operation documents. Stopping ServiceControl leaves unfinished work persisted. @@ -147,7 +147,7 @@ If you want less optional data, set a shorter window for the optional categories By default, the migration copies all the optional data that RavenDB still holds (current retension period setting). A window longer than the retention period copies nothing extra. After a category starts copying, you cannot change its window. To stop it early, abandon it. -The unresolved failed messages and the group comments have no window and no setting turns them off. They copy once ServiceControl opens, however old the unresolved messages are. The recent hour of failed messages copies during the outage and has no setting either. A `MessageFailed` event that was waiting when you stopped ServiceControl is sent only if its message is in that hour, is retry-issued or holds a retry claim, so give the old instance time to send its pending events before the outage. Group comments appear in ServicePulse as their batches commit, after the unresolved failed messages category is Done or Abandoned. +The unresolved failed messages and the group comments have no window and no setting turns them off. They copy once ServiceControl opens, however old the unresolved messages are. The recent errors copy during the outage and have no window setting either. This set includes every failed message last modified at or after one hour before the newest unresolved message's last-modified time, with no upper bound. With no unresolved message, the recent set is empty. A `MessageFailed` event that was waiting when you stopped ServiceControl is sent only if its message is in that set, is retry-issued or holds a retry claim, so give the old instance time to send its pending events before the outage. Group comments appear in ServicePulse as their batches commit, after the unresolved failed messages category is Done or Abandoned. ## Step 4: Run the dry run diff --git a/docs/migration/ravendb-to-sql-migration-overview.md b/docs/migration/ravendb-to-sql-migration-overview.md index 7bf8bb8dc0..853d450826 100644 --- a/docs/migration/ravendb-to-sql-migration-overview.md +++ b/docs/migration/ravendb-to-sql-migration-overview.md @@ -20,7 +20,7 @@ - **What the instance needs to open is copied while it is closed.** This is the *required* data. Most of it is small. - **Everything else is copied in the background while it runs.** This is the *optional* data: the unresolved failed messages, the group comments, the event log and archived and resolved messages. - **Anything not copied ages out of RavenDB**, and the customer deletes the old database when ready. -- **The failed messages copied during the outage are the recent errors, every retry-issued message and every message that holds a retry claim.** The recent errors are the failed messages RavenDB last modified in the hour before the newest unresolved message, in any status, see [Recent errors](#recent-errors). With no unresolved message there are none. A retry-issued message copies whatever its age, whether or not its retry claim remains, and so does any other message that still holds a claim. Older unresolved messages that hold no claim copy in the background, so a big unresolved backlog does not lengthen the outage. A busy instance can fail many messages in one hour, and every one of them lengthens the outage. +- **The failed messages copied during the outage are the recent errors, every retry-issued message and every message that holds a retry claim.** The recent errors are the failed messages RavenDB last modified at or after one hour before the newest unresolved message's last-modified time, in any status. There is no upper bound, see [Recent errors](#recent-errors). With no unresolved message there are none. A retry-issued message copies whatever its age, whether or not its retry claim remains, and so does any other message that still holds a claim. Older unresolved messages that hold no claim copy in the background, so a big unresolved backlog does not lengthen the outage. Every message in the recent set lengthens the outage, including messages changed after the newest unresolved message's last-modified time. - **A big archive does not make the outage longer, as long as RavenDB has indexed every failed message.** The copy finds the retry-issued and recent messages through a RavenDB index, and waits for it to catch up first. If the index is broken or incomplete, the copy reads every failed message instead and logs a warning. Then a big archive does make the outage longer. ```mermaid @@ -179,7 +179,7 @@ There are nineteen *categories*: fifteen required and four optional. ### Recent errors - **The required failed message category copies every failed message RavenDB last modified at or after the newest unresolved message's last-modified time minus one hour, in every status,** with its bodies and groups. There is no upper bound. The hour is fixed and no setting changes it. It is not a measure of when a message failed: any write to the document, such as a retry, an archive or an unarchive, moves its last-modified time. A message modified exactly at the cutoff is recent. -- **The anchor is the newest unresolved message's RavenDB last-modified time,** found by the source reader itself. Every ServiceControl instance is stopped while the source is copied, so nothing ingests or changes a message and every read, restart, resume and retry finds the same anchor. Nothing is stored for it and no clock is read. With no unresolved message the recent hour is not copied, and the retry-issued messages and claim holders still are, whatever their age. +- **The anchor is the newest unresolved message's RavenDB last-modified time,** found by the source reader itself. Every ServiceControl instance is stopped while the source is copied, so nothing ingests or changes a message. Reads, restarts, resumes and retries find the same anchor unless RavenDB expiration removes the newest unresolved message. Expiration can move the cutoff earlier or leave no unresolved message. A resumed read does not revisit message ids before its cursor, so its copied set can differ from a fresh read or count. Nothing is stored for the anchor and no clock is read. With no unresolved message the recent set is empty, and the retry-issued messages and claim holders still are, whatever their age. - **A message is read and counted once,** whether it is recent, retry-issued or holds a retry claim, and its claim copies with it. Reading through the index or through the full read gives the same set. `--migration-verify` and the dry run are not yet commands in this build. The target count of this category is the whole failed message table, which is the copied set only while ServiceControl is stopped and nothing else has written to it. After ServiceControl opens it includes live and optional rows, so it is not a category count then. - **A pending `MessageFailed` event is protected only when its message is a recent error, a retry-issued message or a claim holder.** There is no separate check of the events. An event for any other older message is not protected, see [Rows removed after the copy](#rows-removed-after-the-copy). - **The optional unresolved copy inserts only the rows that are missing.** It does not update a recent row or its claim, and it counts the row as already present. @@ -512,7 +512,7 @@ The dry run counts the subscription and endpoint settings merges before anything ### Rows removed after the copy - **The next retention sweep removes a copied group comment whose group has no failed message in SQL**, because the sweep deletes a comment whose group has no row. The group has none when its messages are archived or resolved, when they are gone from RavenDB, or when the copy skipped them. Comments wait until the unresolved failed messages category is Done or Abandoned, so waiting for that copy alone does not cause comment loss. Abandoning it can leave groups without messages in SQL. Nothing keeps a comment on a group with only archived or resolved messages, even when those messages copy later. The copy names each such comment, in a warning starting `Changed GroupComment/ in category GroupComments on the way into SQL: The group '' has no copied failed message`, which also quotes the whole comment. -- **A `MessageFailed` event still waiting to be sent for a message outside the recent errors, retry-issued messages and claim holders is lost when the message is archived, resolved or not yet copied.** ServiceControl looks the message up when it sends the event. Archived and resolved messages older than the recent hour arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. The same lookup can fail for an older unresolved message the background copy has not reached yet. The required copy covers the events for messages that changed in the hour before the anchor, are retry-issued or hold a claim, and nothing else covers the rest, an accepted gap. +- **A `MessageFailed` event still waiting to be sent for a message outside the recent errors, retry-issued messages and claim holders is lost when the message is archived, resolved or not yet copied.** ServiceControl looks the message up when it sends the event. Archived and resolved messages last modified before the cutoff arrive only with the background copy, so the lookup finds nothing, and the event is deleted with no log line. RavenDB would have sent it. The same lookup can fail for an older unresolved message the background copy has not reached yet. The required copy covers the events for messages last modified at or after one hour before the anchor, with no upper bound, or messages that are retry-issued or hold a claim, and nothing else covers the rest, an accepted gap. - **Unresolved messages from version 6.18 or earlier can expire during the copy.** On those versions, a message that failed again after being archived or resolved kept its expiry. One deleted before the copy reads it is not copied and not counted. No log line names these messages yet. ### What the checks do not catch diff --git a/docs/migration/ravendb-to-sql-migration-system-design.md b/docs/migration/ravendb-to-sql-migration-system-design.md index 803798a9a8..7a6574f61e 100644 --- a/docs/migration/ravendb-to-sql-migration-system-design.md +++ b/docs/migration/ravendb-to-sql-migration-system-design.md @@ -151,7 +151,7 @@ Each position in the sequence is deliberate: `UnresolvedFailedMessages` and `GroupComments` have no window. `MigrationEngineOptions.FromSettings` selects both even when both windows are zero, so the offline command seeds their checkpoint rows and the online copier runs them. -The failed message category `UnresolvedAndRetryIssuedFailedMessages` copies recent errors with no window setting and no checkpoint column. `FailedMessagesReader` finds the cutoff itself on every `Read`: the newest unresolved message's `@last-modified` less one hour. Every ServiceControl instance is stopped while the source is copied, so the newest unresolved message is stable and `Read`, `Count`, a resume, a restart and a retry reset all derive the same cutoff. `IMigrationSource` and `IMigrationCategoryReader` carry no cutoff and the source never reads the target. +The failed message category `UnresolvedAndRetryIssuedFailedMessages` copies recent errors with no window setting and no checkpoint column. `FailedMessagesReader` finds the cutoff itself on every `Read`: the newest unresolved message's `@last-modified` less one hour. It selects messages last modified at or after that cutoff, with no upper bound. Every ServiceControl instance is stopped while the source is copied, so `Read`, `Count`, a resume, a restart and a retry reset derive the same cutoff unless RavenDB expiration removes the newest unresolved message. Expiration can move the cutoff earlier or leave no unresolved message. A resume keeps its cursor and does not revisit earlier message ids, so its copied set can differ from a fresh read or count. `IMigrationSource` and `IMigrationCategoryReader` carry no cutoff and the source never reads the target. Windowing for the other two: `MigrationEngineOptions.WindowFor` supplies the window and the reader applies it through `IWindowedMigrationCategoryReader`. The engine writes `StartedWindowSeconds` once, on the first `InProgress` upsert. `CopiesFrom` is `StartedAt` minus the currently configured window, so window stability across restarts depends entirely on `OptionalCategoryWindowsAreUnchangedCheck`. Verify counts from `StartedAt` minus `StartedWindowSeconds` instead, which is the same window while the category copies, because nothing stops the setting changing once it has finished. diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs index 92d691e8c8..f14d8a0618 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessageStatusReader.cs @@ -67,9 +67,9 @@ protected Task CanQueryIndex(CancellationToken cancellationToken = default IndexHoldsEveryCopiedFailedMessage(Lifecycle.Settings.DatabaseName, cancellationToken); /// - /// The RavenDB last-modified time of the unresolved message modified most recently, or null when there is none. The source is - /// stopped while it is read, so every call returns the same time. Through the index it is one query. Otherwise it walks every - /// failed message once, holding only the newest time. + /// The RavenDB last-modified time of the unresolved message modified most recently, or null when there is none. ServiceControl is + /// stopped during the copy, but RavenDB expiration can remove that message between calls. Through the index it is one query. + /// Otherwise it walks every failed message once, holding only the newest time. /// protected async Task LatestUnresolvedLastModified(bool throughIndex, CancellationToken cancellationToken = default) { diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs index 63df82ff3c..623521d835 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs @@ -12,7 +12,7 @@ namespace ServiceControl.Persistence.RavenDB.DataMigration.Readers; /// Reads what is copied while ServiceControl is closed: every retry-issued message, every message that holds a retry claim whatever /// its status and age, and the recent errors, which are the messages of any status last modified at or after the newest unresolved /// message's last-modified time less one hour. With no unresolved message there are no recent errors. -/// Every ServiceControl instance is stopped while a source is copied, so the newest unresolved message does not change between reads. +/// ServiceControl is stopped during the copy, but RavenDB expiration can remove the newest unresolved message and change the cutoff between reads. /// A message is read once, and every row says whether RavenDB still holds its retry claim. /// The older unresolved messages without a claim are 's. /// From 8ada453e00a8eada58bece621f4d135f9e1d90a2 Mon Sep 17 00:00:00 2001 From: Rhys Bevilaqua Date: Tue, 24 Nov 2026 15:12:01 +0800 Subject: [PATCH 8/8] Rename the mandatory error migration group to be more in line with what it is --- .../ravendb-to-sql-migration-overview.md | 2 +- .../ravendb-to-sql-migration-system-design.md | 4 ++-- .../GroupCommentsMigrationTests.cs | 2 +- .../RecentErrorsMigrationTests.cs | 2 +- .../When_the_copy_merges_or_changes_rows.cs | 2 +- ...en_unresolved_failed_messages_are_migrated.cs | 4 ++-- .../Writers/FailedMessagesWriter.cs | 2 +- .../DataMigration/RavenMigrationSource.cs | 2 +- .../Readers/FailedMessagesReader.cs | 2 +- .../FailedMessageBodySourceTests.cs | 2 +- .../DataMigration/RavenMigrationSourceTests.cs | 2 +- .../DataMigration/RecentErrorsSourceTests.cs | 2 +- .../RetryIssuedFailedMessageSourceTests.cs | 2 +- .../MigrationGroupInsertSeekTests.cs | 4 ++-- .../MigrationPlanReuseTests.cs | 6 +++--- .../FailedMessageMigrationCoverageTests.cs | 2 +- .../FailedMessagesMigrationTargetTests.cs | 12 ++++++------ .../Migration/MigrationTargetCoverageTests.cs | 4 ++-- .../DataMigration/MigrationCategoryIds.cs | 5 ++--- .../DataMigration/MigrationCategoryRegistry.cs | 2 +- ...y_category_runs_in_a_fixed_order.approved.txt | 2 +- .../Fakes/InMemoryMigrationSourceTests.cs | 4 ++-- .../Fakes/InMemoryMigrationTargetTests.cs | 4 ++-- .../MigrationBeforeTheHostOpensTests.cs | 2 +- .../Migration/MigrationBodyLoaderTests.cs | 2 +- .../Migration/MigrationCategoryRegistryTests.cs | 6 +++--- .../Migration/MigrationEngineBodyRetryTests.cs | 16 ++++++++-------- .../Migration/MigrationEngineCollisionTests.cs | 2 +- .../Migration/MigrationEngineFailurePathTests.cs | 2 +- .../Migration/MigrationEngineSkipReasonTests.cs | 6 +++--- .../Migration/RequiredCopyGateTests.cs | 10 +++++----- 31 files changed, 60 insertions(+), 61 deletions(-) diff --git a/docs/migration/ravendb-to-sql-migration-overview.md b/docs/migration/ravendb-to-sql-migration-overview.md index 853d450826..386a07003b 100644 --- a/docs/migration/ravendb-to-sql-migration-overview.md +++ b/docs/migration/ravendb-to-sql-migration-overview.md @@ -143,7 +143,7 @@ There are nineteen *categories*: fifteen required and four optional. | Category | Kind | Why | | --- | --- | --- | -| Recent errors, retry-issued failed messages, and failed messages that hold a retry claim, with bodies | Required | The recent errors, in every status, are in SQL when ServiceControl opens, so the events and requests that name the newest messages find them, see [Recent errors](#recent-errors). Issuing a retry removes the expiry, so retry-issued messages never age out. Leave one behind and the retry confirmation finds no row to resolve, so the message stays missing even though the retry succeeded. A message of any other status and age that still holds a retry claim copies with it, with its groups, body and claim. The category keeps the checkpoint id `UnresolvedAndRetryIssuedFailedMessages` from the layout that copied every unresolved message here, see [Progress and checkpoints](#progress-and-checkpoints). A delayed retry confirmation for an unresolved message whose claim a re-failure removed is not covered, an accepted edge case, see [Writing to SQL](#writing-to-sql) | +| Recent errors, retry-issued failed messages, and failed messages that hold a retry claim, with bodies | Required | The recent errors, in every status, are in SQL when ServiceControl opens, so the events and requests that name the newest messages find them, see [Recent errors](#recent-errors). Issuing a retry removes the expiry, so retry-issued messages never age out. Leave one behind and the retry confirmation finds no row to resolve, so the message stays missing even though the retry succeeded. A message of any other status and age that still holds a retry claim copies with it, with its groups, body and claim. The category id is `RecentAndRetriedFailedMessages`, see [Progress and checkpoints](#progress-and-checkpoints). A delayed retry confirmation for an unresolved message whose claim a re-failure removed is not covered, an accepted edge case, see [Writing to SQL](#writing-to-sql) | | Message redirects | Required | | | Known endpoints, with the monitored flag | Required | The flag is part of the endpoint row | | Endpoint settings | Required | Copied after known endpoints | diff --git a/docs/migration/ravendb-to-sql-migration-system-design.md b/docs/migration/ravendb-to-sql-migration-system-design.md index 7a6574f61e..bab5096a1e 100644 --- a/docs/migration/ravendb-to-sql-migration-system-design.md +++ b/docs/migration/ravendb-to-sql-migration-system-design.md @@ -151,7 +151,7 @@ Each position in the sequence is deliberate: `UnresolvedFailedMessages` and `GroupComments` have no window. `MigrationEngineOptions.FromSettings` selects both even when both windows are zero, so the offline command seeds their checkpoint rows and the online copier runs them. -The failed message category `UnresolvedAndRetryIssuedFailedMessages` copies recent errors with no window setting and no checkpoint column. `FailedMessagesReader` finds the cutoff itself on every `Read`: the newest unresolved message's `@last-modified` less one hour. It selects messages last modified at or after that cutoff, with no upper bound. Every ServiceControl instance is stopped while the source is copied, so `Read`, `Count`, a resume, a restart and a retry reset derive the same cutoff unless RavenDB expiration removes the newest unresolved message. Expiration can move the cutoff earlier or leave no unresolved message. A resume keeps its cursor and does not revisit earlier message ids, so its copied set can differ from a fresh read or count. `IMigrationSource` and `IMigrationCategoryReader` carry no cutoff and the source never reads the target. +The failed message category `RecentAndRetriedFailedMessages` copies recent errors with no window setting and no checkpoint column. `FailedMessagesReader` finds the cutoff itself on every `Read`: the newest unresolved message's `@last-modified` less one hour. It selects messages last modified at or after that cutoff, with no upper bound. Every ServiceControl instance is stopped while the source is copied, so `Read`, `Count`, a resume, a restart and a retry reset derive the same cutoff unless RavenDB expiration removes the newest unresolved message. Expiration can move the cutoff earlier or leave no unresolved message. A resume keeps its cursor and does not revisit earlier message ids, so its copied set can differ from a fresh read or count. `IMigrationSource` and `IMigrationCategoryReader` carry no cutoff and the source never reads the target. Windowing for the other two: `MigrationEngineOptions.WindowFor` supplies the window and the reader applies it through `IWindowedMigrationCategoryReader`. The engine writes `StartedWindowSeconds` once, on the first `InProgress` upsert. `CopiesFrom` is `StartedAt` minus the currently configured window, so window stability across restarts depends entirely on `OptionalCategoryWindowsAreUnchangedCheck`. Verify counts from `StartedAt` minus `StartedWindowSeconds` instead, which is the same window while the category copies, because nothing stops the setting changing once it has finished. @@ -284,7 +284,7 @@ Every reader derives from `MigrationCategoryReader` and every writer | 9 | `LicensingThroughput` | Required | `LicensingThroughputReader`, `LicensingThroughputWriter` | Throughput database. Data link to `LicensingEndpoints`. The reader gives one batch per endpoint from `ThroughputDocuments.Endpoints` with a batch size of one, and ignores the requested batch size, because one endpoint can hold a year or more of days. Each batch's cursor is its endpoint's id. The writer keeps the last row of a day repeated for one source endpoint, compared ordinally. `AddLicensingEndpointThroughput` splits the batch into statements and adds each day's count to the stored one, summing endpoints whose names the key column treats as the same, in one batch or across batches. A re-read is not idempotent, so a resume relies on each batch committing with its checkpoint, and a retry deletes the copied rows first | | 10 | `LicensingReportMasks` | Required | `LicensingReportMasksReader`, `LicensingReportMasksWriter` | Throughput database. One `Settings` row via `SettingRowMapper` | | 11 | `LicensedEndpointDetails` | Required | `LicensedEndpointDetailsReader`, `LicensedEndpointDetailsWriter` | Throughput database. One `Settings` row via `SettingRowMapper` | -| 12 | `UnresolvedAndRetryIssuedFailedMessages` | Required | `FailedMessagesReader`, `FailedMessagesWriter` | Carries bodies. `FailedMessageStream` projection, `FailedMessageDocumentTranslator`. `FailedMessagesReader` and `UnresolvedFailedMessagesReader` both derive from `FailedMessageStatusReader`, which streams the category's statuses in id order through `FailedMessageViewIndex` after waiting for the index, or through `RavenDocumentStream.ByPrefix` when the index cannot be trusted, with the document id as the cursor either way. `FailedMessagesReader` reads in two phases. First it asks `FailedMessageStatusReader.CanQueryIndex` which way to read, then `LatestUnresolvedLastModified` for the anchor: through the index it is one query for the unresolved message with the highest `LastModified`, and otherwise one pass over the `FailedMessages/` documents that keeps only the newest time. It then reads in the same way and finds nothing recent when there is no unresolved message. Phase one is `ReadStatuses` with the retry-issued status or a last-modified time at or after the cutoff, in any status. The index query is `Status in (RetryIssued) OR LastModified >= cutoff ticks` ordered by `id()`. The full read applies the same predicate in `FailedMessageStream.IsSelected` to the document's `@last-modified`, and a document without one is selected so that it fails by name in the writer. `FailedMessageStream.ResolveRetryClaims` looks up each batch's claims in one request and sets `RetryClaimHeld` on every row. Phase two is `FailedMessageStream.ClaimHolders`, which streams the `FailedMessageRetries/` documents by id prefix and, per batch of claims, queries the messages they name by `id()`, with `RetryClaimHeld` true and no index built. It leaves out every message `IsSelected` takes, because phase one read it, so no message is read or counted twice and the claim a row carries is the one phase one resolved. A claim whose message is gone makes no row, and memory stays bounded by the batch size. The phase-two cursor is `claim-holder:` and the last claim id. A cursor without the prefix, which is the end of phase one, runs phase one after it and then starts phase two. A prefixed cursor skips phase one and its document-existence check. The writer, through `FailedMessagesWriterBase`, inserts a claim for every inserted row whose flag is true, so the claims mirror RavenDB's. A retry-issued row with no flag fails the batch, and any other status with no flag means no claim. The writer counts every row of the failed message table for this category, which equals the rows it copied only while the host is closed and the target started empty. The optional `UnresolvedFailedMessagesWriter` shares `FailedMessagesWriterBase`, so it too inserts only missing rows and leaves a recent row, its groups and its claim as the required copy wrote them | +| 12 | `RecentAndRetriedFailedMessages` | Required | `FailedMessagesReader`, `FailedMessagesWriter` | Carries bodies. `FailedMessageStream` projection, `FailedMessageDocumentTranslator`. `FailedMessagesReader` and `UnresolvedFailedMessagesReader` both derive from `FailedMessageStatusReader`, which streams the category's statuses in id order through `FailedMessageViewIndex` after waiting for the index, or through `RavenDocumentStream.ByPrefix` when the index cannot be trusted, with the document id as the cursor either way. `FailedMessagesReader` reads in two phases. First it asks `FailedMessageStatusReader.CanQueryIndex` which way to read, then `LatestUnresolvedLastModified` for the anchor: through the index it is one query for the unresolved message with the highest `LastModified`, and otherwise one pass over the `FailedMessages/` documents that keeps only the newest time. It then reads in the same way and finds nothing recent when there is no unresolved message. Phase one is `ReadStatuses` with the retry-issued status or a last-modified time at or after the cutoff, in any status. The index query is `Status in (RetryIssued) OR LastModified >= cutoff ticks` ordered by `id()`. The full read applies the same predicate in `FailedMessageStream.IsSelected` to the document's `@last-modified`, and a document without one is selected so that it fails by name in the writer. `FailedMessageStream.ResolveRetryClaims` looks up each batch's claims in one request and sets `RetryClaimHeld` on every row. Phase two is `FailedMessageStream.ClaimHolders`, which streams the `FailedMessageRetries/` documents by id prefix and, per batch of claims, queries the messages they name by `id()`, with `RetryClaimHeld` true and no index built. It leaves out every message `IsSelected` takes, because phase one read it, so no message is read or counted twice and the claim a row carries is the one phase one resolved. A claim whose message is gone makes no row, and memory stays bounded by the batch size. The phase-two cursor is `claim-holder:` and the last claim id. A cursor without the prefix, which is the end of phase one, runs phase one after it and then starts phase two. A prefixed cursor skips phase one and its document-existence check. The writer, through `FailedMessagesWriterBase`, inserts a claim for every inserted row whose flag is true, so the claims mirror RavenDB's. A retry-issued row with no flag fails the batch, and any other status with no flag means no claim. The writer counts every row of the failed message table for this category, which equals the rows it copied only while the host is closed and the target started empty. The optional `UnresolvedFailedMessagesWriter` shares `FailedMessagesWriterBase`, so it too inserts only missing rows and leaves a recent row, its groups and its claim as the required copy wrote them | | 13 | `PendingIntegrationEvents` | Required | `PendingIntegrationEventsReader`, `PendingIntegrationEventsWriter` | Store-keyed via `StoreKeyedInsert`. Deletes its rows on retry | | 14 | `CustomChecks` | Required | `CustomChecksReader`, `CustomChecksWriter` | The RavenDB reader flags a check whose id and category match one of the RavenDB persister's own six checks with the `SourceStorageCheck` metadata key, and the writer skips a flagged row as `SourceStorageCheck` without knowing any id. A RavenDB audit instance's dirty memory result reports under another category, so it is copied | | 15 | `FailedErrorImports` | Required | `FailedErrorImportsReader`, `FailedErrorImportsWriter` | Carries bodies inline in `Read`, never calls `ReadBody` | diff --git a/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs b/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs index 12c8aa3721..eefb0e1485 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/GroupCommentsMigrationTests.cs @@ -31,7 +31,7 @@ await SeedSourceGroupComments( (neverWritten.Groups.Single().Id, null), (spaces.Groups.Single().Id, " "), ("group-with-no-copied-message", "Raised with the shipping team")); - await CopyCategory(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + await CopyCategory(MigrationCategoryIds.RecentAndRetriedFailedMessages); await CopyCategory(MigrationCategoryIds.UnresolvedFailedMessages); var result = await CopyCategory(MigrationCategoryIds.GroupComments); diff --git a/src/ServiceControl.Migration.AcceptanceTests/RecentErrorsMigrationTests.cs b/src/ServiceControl.Migration.AcceptanceTests/RecentErrorsMigrationTests.cs index 94d52f6f90..edbb46b882 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/RecentErrorsMigrationTests.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/RecentErrorsMigrationTests.cs @@ -26,7 +26,7 @@ namespace ServiceControl.Migration.AcceptanceTests; [NonParallelizable] class RecentErrorsMigrationTests : MigrationAcceptanceTest { - const string RequiredCategoryId = MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; + const string RequiredCategoryId = MigrationCategoryIds.RecentAndRetriedFailedMessages; [Test] public async Task Copies_the_recent_errors_of_every_status_once_with_their_bodies_groups_and_claims() diff --git a/src/ServiceControl.Migration.AcceptanceTests/When_the_copy_merges_or_changes_rows.cs b/src/ServiceControl.Migration.AcceptanceTests/When_the_copy_merges_or_changes_rows.cs index 1caa4e7e05..f16dc11d20 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/When_the_copy_merges_or_changes_rows.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/When_the_copy_merges_or_changes_rows.cs @@ -30,7 +30,7 @@ await SeedSourceSubscriptions( using (Assert.EnterMultipleScope()) { - Assert.That(summaries.Lines, Has.Exactly(1).EqualTo((LogLevel.Warning, $"{MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages}: 1 copied (1 changed), 0 already present, 0 merged, nothing skipped")), + Assert.That(summaries.Lines, Has.Exactly(1).EqualTo((LogLevel.Warning, $"{MigrationCategoryIds.RecentAndRetriedFailedMessages}: 1 copied (1 changed), 0 already present, 0 merged, nothing skipped")), "the message type was cut, which the summary has to count even though nothing was skipped, and the dropped older attempt is not a change"); Assert.That(summaries.Lines, Has.Exactly(1).EqualTo((LogLevel.Warning, $"{MigrationCategoryIds.Subscriptions}: 1 copied (0 changed), 0 already present, 1 merged, nothing skipped")), "the second version is counted as already present by the checkpoint, and the summary has to show it as a merge instead"); diff --git a/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs b/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs index 704174c3f7..c7118a25f4 100644 --- a/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs +++ b/src/ServiceControl.Migration.AcceptanceTests/When_unresolved_failed_messages_are_migrated.cs @@ -325,7 +325,7 @@ public async Task Records_each_skip_under_the_reason_that_caused_it() using (Assert.EnterMultipleScope()) { - Assert.That(refused.Message, Does.Contain("UnresolvedAndRetryIssuedFailedMessages").And.Contain("--migration-retry").And.Contain("--migration-abandon"), "a fault skip leaves a required category Failed, so the host stays closed and names both commands"); + Assert.That(refused.Message, Does.Contain("RecentAndRetriedFailedMessages").And.Contain("--migration-retry").And.Contain("--migration-abandon"), "a fault skip leaves a required category Failed, so the host stays closed and names both commands"); Assert.That(checkpoint.State, Is.EqualTo(MigrationCategoryState.CompleteWithErrors)); Assert.That(checkpoint.CopiedCount, Is.EqualTo(1)); Assert.That(checkpoint.SkippedCount, Is.EqualTo(2)); @@ -405,7 +405,7 @@ Task DeleteStoredTimeOfFailure(IngestedFailure failure, int attempt) => null, new PatchRequest { Script = $"delete this.ProcessingAttempts[{attempt}].FailureDetails.TimeOfFailure;" })); - const string OfflineCategoryId = MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; + const string OfflineCategoryId = MigrationCategoryIds.RecentAndRetriedFailedMessages; // Seeded with its body, because the copy leaves out a message whose body the source does not hold. Task SeedFailure(IngestedFailure failure, FailedMessageStatus status = FailedMessageStatus.Unresolved, int numberOfAttempts = 1) => diff --git a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs index ac4e85e1a1..b79d5446d1 100644 --- a/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs +++ b/src/ServiceControl.Persistence.EFCore/DataMigration/Writers/FailedMessagesWriter.cs @@ -193,7 +193,7 @@ await migrationDialect.InsertMissing(dbContext, [.. inserted sealed class FailedMessagesWriter(IMigrationSqlDialect migrationDialect, IBodyStoragePersistence storagePersistence, EFPersisterSettings persisterSettings) : FailedMessagesWriterBase(migrationDialect, storagePersistence, persisterSettings) { - public override string CategoryId => MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; + public override string CategoryId => MigrationCategoryIds.RecentAndRetriedFailedMessages; protected override IQueryable Copied(ServiceControlDbContext dbContext) => dbContext.FailedMessages; } diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs index 938d399693..9181cb19bb 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/RavenMigrationSource.cs @@ -105,7 +105,7 @@ public IAsyncEnumerable Read( public async Task ReadBody(MigrationCategory category, string sourceId, CancellationToken cancellationToken = default) { // Another category may keep its body inside the document, where a body attachment read would find nothing and skip every row as BodyMissing. - if (category.Id is not (MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages or MigrationCategoryIds.UnresolvedFailedMessages)) + if (category.Id is not (MigrationCategoryIds.RecentAndRetriedFailedMessages or MigrationCategoryIds.UnresolvedFailedMessages)) { throw new NotSupportedException($"The migration source does not read bodies as attachments for the '{category.Id}' category."); } diff --git a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs index 623521d835..25116141e5 100644 --- a/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs +++ b/src/ServiceControl.Persistence.RavenDB/DataMigration/Readers/FailedMessagesReader.cs @@ -20,7 +20,7 @@ sealed class FailedMessagesReader(RavenReadOnlySourceLifecycle lifecycle) : Fail { static readonly TimeSpan RecentErrorsWindow = TimeSpan.FromHours(1); - public override string CategoryId => MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages; + public override string CategoryId => MigrationCategoryIds.RecentAndRetriedFailedMessages; /// /// Streams the retry-issued and recent messages through , then the claim holders, diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/FailedMessageBodySourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/FailedMessageBodySourceTests.cs index 4478dd1ba9..ee80750870 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/FailedMessageBodySourceTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/FailedMessageBodySourceTests.cs @@ -12,7 +12,7 @@ namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; class FailedMessageBodySourceTests : RavenMigrationSourceTestBase { static readonly MigrationCategory FailedMessages = - MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.RecentAndRetriedFailedMessages); [Test] public async Task Reads_the_attachment_and_its_own_content_type() diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTests.cs index 7b5f4784f3..8321da62e7 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RavenMigrationSourceTests.cs @@ -137,7 +137,7 @@ public async Task Reading_every_category_creates_no_index() { [MigrationCategoryIds.EndpointSettings] = tests => tests.SeedEndpointSettings(2), [MigrationCategoryIds.KnownEndpoints] = tests => tests.MonitoringDataStore.CreateIfNotExists(new EndpointDetails { Name = "Sales.Orders", HostId = Guid.NewGuid(), Host = "HOST01" }), - [MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages] = tests => tests.SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)), + [MigrationCategoryIds.RecentAndRetriedFailedMessages] = tests => tests.SeedFailedMessage(new IngestedFailure().ToFailedMessage(FailedMessageStatus.RetryIssued)), [MigrationCategoryIds.UnresolvedFailedMessages] = tests => tests.SeedFailedMessage(new IngestedFailure().ToFailedMessage()), [MigrationCategoryIds.LicensingEndpoints] = tests => tests.SeedThroughputDocument("Sales.Orders/Broker", new EndpointDocument(new EndpointIdentifier("Sales.Orders", ThroughputSource.Broker))), [MigrationCategoryIds.LicensingThroughput] = tests => tests.SeedThroughputDocument("Sales.Orders/Broker", new EndpointDocument(new EndpointIdentifier("Sales.Orders", ThroughputSource.Broker))), diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RecentErrorsSourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RecentErrorsSourceTests.cs index a8d80fa7be..1d0f240566 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RecentErrorsSourceTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RecentErrorsSourceTests.cs @@ -19,7 +19,7 @@ namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; [TestFixture] class RecentErrorsSourceTests : RavenMigrationSourceTestBase { - static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages); static readonly FailedMessageStatus[] AllStatuses = [FailedMessageStatus.Unresolved, FailedMessageStatus.Archived, FailedMessageStatus.Resolved, FailedMessageStatus.RetryIssued]; [Test] diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs index 8f0c202906..3ca0434848 100644 --- a/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs +++ b/src/ServiceControl.Persistence.Tests.RavenDB/DataMigration/RetryIssuedFailedMessageSourceTests.cs @@ -14,7 +14,7 @@ namespace ServiceControl.Persistence.Tests.RavenDB.DataMigration; [TestFixture] class RetryIssuedFailedMessageSourceTests : RavenMigrationSourceTestBase { - static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages); FailedMessage newestUnresolved; diff --git a/src/ServiceControl.Persistence.Tests.SqlServer/MigrationGroupInsertSeekTests.cs b/src/ServiceControl.Persistence.Tests.SqlServer/MigrationGroupInsertSeekTests.cs index f1914f23b1..19de168ce3 100644 --- a/src/ServiceControl.Persistence.Tests.SqlServer/MigrationGroupInsertSeekTests.cs +++ b/src/ServiceControl.Persistence.Tests.SqlServer/MigrationGroupInsertSeekTests.cs @@ -16,7 +16,7 @@ namespace ServiceControl.Persistence.Tests; class MigrationGroupInsertSeekTests : PersistenceTestBase { - static readonly MigrationCategory FailedMessagesCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + static readonly MigrationCategory FailedMessagesCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.RecentAndRetriedFailedMessages); IMigrationTarget Target => ServiceProvider.GetRequiredService(); @@ -40,7 +40,7 @@ public async Task The_group_insert_seeks_existing_rows_by_key_while_the_group_ta }, new MigrationBody("1"u8.ToArray(), "text/xml"))) .ToList(); - var result = await Target.Write(FailedMessagesCategory, new MigrationBatch(rows, rows[^1].SourceId), new MigrationCheckpoint(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages, MigrationCategoryState.InProgress, null, 0, 0, null, null, null, null, null, null)); + var result = await Target.Write(FailedMessagesCategory, new MigrationBatch(rows, rows[^1].SourceId), new MigrationCheckpoint(MigrationCategoryIds.RecentAndRetriedFailedMessages, MigrationCategoryState.InProgress, null, 0, 0, null, null, null, null, null, null)); using var scope = ServiceProvider.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); diff --git a/src/ServiceControl.Persistence.Tests.SqlServer/MigrationPlanReuseTests.cs b/src/ServiceControl.Persistence.Tests.SqlServer/MigrationPlanReuseTests.cs index f6135cc924..9a792f8cb0 100644 --- a/src/ServiceControl.Persistence.Tests.SqlServer/MigrationPlanReuseTests.cs +++ b/src/ServiceControl.Persistence.Tests.SqlServer/MigrationPlanReuseTests.cs @@ -16,7 +16,7 @@ namespace ServiceControl.Persistence.Tests; class MigrationPlanReuseTests : PersistenceTestBase { - static readonly MigrationCategory FailedMessagesCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + static readonly MigrationCategory FailedMessagesCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.RecentAndRetriedFailedMessages); IMigrationTarget Target => ServiceProvider.GetRequiredService(); @@ -32,7 +32,7 @@ public async Task Batches_whose_unbounded_text_sits_either_side_of_4000_characte longHeaders.Headers["Padding"] = new string('h', 5_000); await Target.Write(FailedMessagesCategory, BatchOf(shortHeaders.ToFailedMessage()), Checkpoint()); - var saved = await ServiceProvider.GetRequiredService().Read(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var saved = await ServiceProvider.GetRequiredService().Read(MigrationCategoryIds.RecentAndRetriedFailedMessages); var result = await Target.Write(FailedMessagesCategory, BatchOf(longHeaders.ToFailedMessage()), saved); using var scope = ServiceProvider.CreateScope(); @@ -74,5 +74,5 @@ static MigrationBatch BatchOf(FailedMessage document) } static MigrationCheckpoint Checkpoint() => - new(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages, MigrationCategoryState.InProgress, null, 0, 0, null, null, null, null, null, null); + new(MigrationCategoryIds.RecentAndRetriedFailedMessages, MigrationCategoryState.InProgress, null, 0, 0, null, null, null, null, null, null); } diff --git a/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessageMigrationCoverageTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessageMigrationCoverageTests.cs index b273ece28a..07a610cc04 100644 --- a/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessageMigrationCoverageTests.cs +++ b/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessageMigrationCoverageTests.cs @@ -44,7 +44,7 @@ public void Every_failure_group_column_is_set_from_a_fully_populated_document() public async Task Every_retry_claim_column_is_set_for_a_copied_retry_issued_message() { var target = ServiceProvider.GetRequiredService(); - var category = MigrationCategoryRegistry.All.Single(entry => entry.Id == MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var category = MigrationCategoryRegistry.All.Single(entry => entry.Id == MigrationCategoryIds.RecentAndRetriedFailedMessages); var row = FullyPopulatedRow(); var source = row with { diff --git a/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs index 1e30bb6818..29c0bd05bd 100644 --- a/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs +++ b/src/ServiceControl.Persistence.Tests/EFCore/Migration/FailedMessagesMigrationTargetTests.cs @@ -42,7 +42,7 @@ class FailedMessagesMigrationTargetTests : PersistenceTestBase static readonly MigrationCategory UnresolvedCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.UnresolvedFailedMessages); - static readonly MigrationCategory FailedMessagesCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + static readonly MigrationCategory FailedMessagesCategory = MigrationCategoryRegistry.All.Single(category => category.Id == MigrationCategoryIds.RecentAndRetriedFailedMessages); [Test] public async Task A_copied_message_arrives_with_its_failure_group_rows() @@ -155,7 +155,7 @@ public async Task Copied_counts_the_messages_inserted_not_the_messages_handed_in var present = new IngestedFailure(); var absent = new IngestedFailure(); await Target.Write(FailedMessagesCategory, BatchOf(present.ToFailedMessage()), Checkpoint()); - var saved = await CheckpointStore.Read(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages); + var saved = await CheckpointStore.Read(MigrationCategoryIds.RecentAndRetriedFailedMessages); var result = await Target.Write(FailedMessagesCategory, BatchOf(present.ToFailedMessage(), absent.ToFailedMessage()), saved); @@ -803,7 +803,7 @@ public async Task A_body_that_cannot_be_written_fails_the_write_before_any_row_o "a storage SDK's own error does not name the body, so without the row and body id the operator cannot tell which message halted the category"); Assert.That(exception.InnerException, Is.TypeOf().And.Message.EqualTo(UnwritableBodyStore.Failure)); Assert.That(await Query(db => db.FailedMessages.CountAsync()), Is.Zero, "a row committed without its body would name a body that was never stored"); - Assert.That(await CheckpointStore.Read(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages), Is.Null, "a checkpoint past the batch would stop a retry from reading it again"); + Assert.That(await CheckpointStore.Read(MigrationCategoryIds.RecentAndRetriedFailedMessages), Is.Null, "a checkpoint past the batch would stop a retry from reading it again"); } } @@ -818,7 +818,7 @@ public void A_document_that_cannot_be_prepared_fails_the_write_naming_the_row_an using (Assert.EnterMultipleScope()) { - Assert.That(exception.Message, Does.Contain($"'FailedMessages/{document.UniqueMessageId}'").And.Contain(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages).And.Contain(nameof(InvalidCastException)), + Assert.That(exception.Message, Does.Contain($"'FailedMessages/{document.UniqueMessageId}'").And.Contain(MigrationCategoryIds.RecentAndRetriedFailedMessages).And.Contain(nameof(InvalidCastException)), "the halt's LastError is built from this message, and without the row the operator cannot tell which document to look at"); Assert.That(exception.InnerException, Is.TypeOf()); } @@ -831,7 +831,7 @@ public void A_row_that_reaches_the_writer_without_a_body_fails_the_write_naming_ var exception = Assert.ThrowsAsync(() => Target.Write(FailedMessagesCategory, BatchOf(RowOf(document, body: null)), Checkpoint())); - Assert.That(exception.Message, Does.Contain($"'FailedMessages/{document.UniqueMessageId}'").And.Contain(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)); + Assert.That(exception.Message, Does.Contain($"'FailedMessages/{document.UniqueMessageId}'").And.Contain(MigrationCategoryIds.RecentAndRetriedFailedMessages)); } // Disposes the stream, because the file store holds the body file open until it is. @@ -901,6 +901,6 @@ sealed class UnwritableBodyStore : IBodyStoragePersistence FailedMessagesWriter Writer() => new(ServiceProvider.GetRequiredService(), BodyStore, ServiceProvider.GetRequiredService()); - static MigrationCheckpoint Checkpoint(string categoryId = MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages) => + static MigrationCheckpoint Checkpoint(string categoryId = MigrationCategoryIds.RecentAndRetriedFailedMessages) => new(categoryId, MigrationCategoryState.InProgress, null, 0, 0, null, null, null, null, null, null); } diff --git a/src/ServiceControl.Persistence.Tests/EFCore/Migration/MigrationTargetCoverageTests.cs b/src/ServiceControl.Persistence.Tests/EFCore/Migration/MigrationTargetCoverageTests.cs index ae2043d82f..8f569fb9ae 100644 --- a/src/ServiceControl.Persistence.Tests/EFCore/Migration/MigrationTargetCoverageTests.cs +++ b/src/ServiceControl.Persistence.Tests/EFCore/Migration/MigrationTargetCoverageTests.cs @@ -22,7 +22,7 @@ public async Task Every_category_the_target_supports_has_a_batch_size_and_a_coun { var target = ServiceProvider.GetRequiredService(); - Assert.That(target.SupportedCategoryIds, Is.SupersetOf(new[] { MigrationCategoryIds.KnownEndpoints, MigrationCategoryIds.EndpointSettings, MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages }), "the test proves nothing if the target supports no category"); + Assert.That(target.SupportedCategoryIds, Is.SupersetOf(new[] { MigrationCategoryIds.KnownEndpoints, MigrationCategoryIds.EndpointSettings, MigrationCategoryIds.RecentAndRetriedFailedMessages }), "the test proves nothing if the target supports no category"); foreach (var id in target.SupportedCategoryIds) { @@ -94,7 +94,7 @@ public async Task Every_writer_whose_rows_a_re_read_cannot_recognise_deletes_the { [MigrationCategoryIds.KnownEndpoints] = nameof(KnownEndpointsMigrationTargetTests.A_known_endpoint_with_a_value_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), [MigrationCategoryIds.EndpointSettings] = nameof(EndpointSettingsMigrationTargetTests.A_setting_whose_name_is_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), - [MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages] = nameof(FailedMessagesMigrationTargetTests.A_message_or_group_value_longer_than_its_column_skips_the_message_whole_and_the_rest_copy), + [MigrationCategoryIds.RecentAndRetriedFailedMessages] = nameof(FailedMessagesMigrationTargetTests.A_message_or_group_value_longer_than_its_column_skips_the_message_whole_and_the_rest_copy), [MigrationCategoryIds.UnresolvedFailedMessages] = nameof(FailedMessagesMigrationTargetTests.A_message_or_group_value_longer_than_its_column_skips_the_message_whole_and_the_rest_copy), [MigrationCategoryIds.LicensingEndpoints] = nameof(LicensingEndpointsMigrationTargetTests.A_licensing_endpoint_with_a_value_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), [MigrationCategoryIds.LicensingThroughput] = nameof(LicensingThroughputMigrationTargetTests.A_day_whose_endpoint_name_is_longer_than_its_column_is_skipped_as_value_too_long_and_the_rest_copy), diff --git a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryIds.cs b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryIds.cs index fe22801861..6b1faa9ad7 100644 --- a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryIds.cs +++ b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryIds.cs @@ -19,10 +19,9 @@ public static class MigrationCategoryIds public const string LicensedEndpointDetails = nameof(LicensedEndpointDetails); /// - /// The retry-issued messages and the messages that hold a retry claim, copied before the host opens: every retry-issued message and - /// every other message that still holds a retry claim. + /// Recent errors of any status, plus retry-issued messages and retry-claim holders of any age, copied before the host opens. /// - public const string UnresolvedAndRetryIssuedFailedMessages = nameof(UnresolvedAndRetryIssuedFailedMessages); + public const string RecentAndRetriedFailedMessages = nameof(RecentAndRetriedFailedMessages); /// The unresolved failed messages, copied once the host is open, whatever their age. public const string UnresolvedFailedMessages = nameof(UnresolvedFailedMessages); diff --git a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs index f44deec4b1..5f6bc7b8a5 100644 --- a/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs +++ b/src/ServiceControl.Persistence/DataMigration/MigrationCategoryRegistry.cs @@ -43,7 +43,7 @@ public static class MigrationCategoryRegistry new(MigrationCategoryIds.LicensingThroughput, MigrationCategoryKind.Required, CarriesBodies: false, Order: 9, MustFollow: MigrationCategoryIds.LicensingEndpoints), new(MigrationCategoryIds.LicensingReportMasks, MigrationCategoryKind.Required, CarriesBodies: false, Order: 10), new(MigrationCategoryIds.LicensedEndpointDetails, MigrationCategoryKind.Required, CarriesBodies: false, Order: 11), - new(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages, MigrationCategoryKind.Required, CarriesBodies: true, Order: 12), + new(MigrationCategoryIds.RecentAndRetriedFailedMessages, MigrationCategoryKind.Required, CarriesBodies: true, Order: 12), new(MigrationCategoryIds.PendingIntegrationEvents, MigrationCategoryKind.Required, CarriesBodies: false, Order: 13), new(MigrationCategoryIds.CustomChecks, MigrationCategoryKind.Required, CarriesBodies: false, Order: 14), new(MigrationCategoryIds.FailedErrorImports, MigrationCategoryKind.Required, CarriesBodies: true, Order: 15), diff --git a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt index 38cd699c24..52fea146d4 100644 --- a/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt +++ b/src/ServiceControl.UnitTests/ApprovalFiles/MigrationEngineCategorySelectionTests.Every_category_runs_in_a_fixed_order.approved.txt @@ -9,7 +9,7 @@ Required 8: LicensingEndpoints Required 9: LicensingThroughput, after LicensingEndpoints Required 10: LicensingReportMasks Required 11: LicensedEndpointDetails -Required 12: UnresolvedAndRetryIssuedFailedMessages, with bodies +Required 12: RecentAndRetriedFailedMessages, with bodies Required 13: PendingIntegrationEvents Required 14: CustomChecks Required 15: FailedErrorImports, with bodies diff --git a/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationSourceTests.cs b/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationSourceTests.cs index 23492909d4..cd3e9c6c8f 100644 --- a/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationSourceTests.cs +++ b/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationSourceTests.cs @@ -70,8 +70,8 @@ public async Task ReadBody_fails_the_configured_number_of_times_then_returns_the source.SetBody("msg-1", finalBody); source.FailBodyReads("msg-1", times: 1, new TimeoutException("transient")); - Assert.ThrowsAsync(() => source.ReadBody(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)!, "msg-1")); - var secondAttempt = await source.ReadBody(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)!, "msg-1"); + Assert.ThrowsAsync(() => source.ReadBody(MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages)!, "msg-1")); + var secondAttempt = await source.ReadBody(MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages)!, "msg-1"); using (Assert.EnterMultipleScope()) { diff --git a/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationTargetTests.cs b/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationTargetTests.cs index ae52e815dc..033e0efc6b 100644 --- a/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationTargetTests.cs +++ b/src/ServiceControl.UnitTests/Migration/Fakes/InMemoryMigrationTargetTests.cs @@ -42,7 +42,7 @@ public async Task The_committed_checkpoint_carries_the_real_split_rather_than_th // Three rows in, one copied: a checkpoint saying three would survive a crash as three. var checkpointStore = new InMemoryMigrationCheckpointStore(); var target = new InMemoryMigrationTarget(checkpointStore); - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; target.SeedExistingKey("already-present"); target.RejectKey("rejected", MigrationSkipReason.BodyUnreadable); var batch = new MigrationBatch([Row("already-present"), Row("rejected"), Row("new-row")], Cursor: "new-row"); @@ -68,7 +68,7 @@ public async Task Count_answers_for_one_category_rather_than_one_table() { var checkpointStore = new InMemoryMigrationCheckpointStore(); var target = new InMemoryMigrationTarget(checkpointStore); - var required = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var required = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var archive = MigrationCategoryRegistry.Find("ArchivedAndResolvedFailedMessages")!; await target.Write(required, new MigrationBatch([Row("a"), Row("b")], "b"), EmptyCheckpoint(required.Id)); diff --git a/src/ServiceControl.UnitTests/Migration/MigrationBeforeTheHostOpensTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationBeforeTheHostOpensTests.cs index 6566769991..b147728fde 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationBeforeTheHostOpensTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationBeforeTheHostOpensTests.cs @@ -68,7 +68,7 @@ public async Task Optional_work_does_not_hold_the_host_closed(MigrationCategoryS public async Task The_required_failed_messages_with_the_unresolved_failed_messages_row_open_the_host_while_that_row_still_copies() { var store = new InMemoryMigrationCheckpointStore(); - await store.Upsert(Checkpoint(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages, MigrationCategoryState.Complete)); + await store.Upsert(Checkpoint(MigrationCategoryIds.RecentAndRetriedFailedMessages, MigrationCategoryState.Complete)); await store.Upsert(Checkpoint(MigrationCategoryIds.UnresolvedFailedMessages, MigrationCategoryState.InProgress)); using var services = Services(store); diff --git a/src/ServiceControl.UnitTests/Migration/MigrationBodyLoaderTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationBodyLoaderTests.cs index 201bb885c8..38bba329bc 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationBodyLoaderTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationBodyLoaderTests.cs @@ -17,7 +17,7 @@ namespace ServiceControl.UnitTests.Migration; [TestFixture] class MigrationBodyLoaderTests { - static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)!; + static readonly MigrationCategory Category = MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages)!; static readonly MigrationBody Body = new(Encoding.UTF8.GetBytes("1"), "text/xml"); [Test] diff --git a/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs index bb83981b93..4ec646b98d 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationCategoryRegistryTests.cs @@ -35,7 +35,7 @@ public void Required_categories_are_ordered_exactly_as_the_contract_lists_them() "KnownEndpoints", "EndpointSettings", "MessageRedirects", "Subscriptions", "NotificationSettings", "TrialEndDate", "RetryOperations", "LicensingEndpoints", "LicensingThroughput", "LicensingReportMasks", - "LicensedEndpointDetails", "UnresolvedAndRetryIssuedFailedMessages", + "LicensedEndpointDetails", "RecentAndRetriedFailedMessages", "PendingIntegrationEvents", "CustomChecks", "FailedErrorImports" })); } @@ -80,7 +80,7 @@ public void The_three_failed_message_categories_and_failed_imports_carry_bodies( { var carriesBodies = MigrationCategoryRegistry.All.Where(c => c.CarriesBodies).Select(c => c.Id).ToArray(); - Assert.That(carriesBodies, Is.EquivalentTo(new[] { "UnresolvedAndRetryIssuedFailedMessages", "UnresolvedFailedMessages", "ArchivedAndResolvedFailedMessages", "FailedErrorImports" })); + Assert.That(carriesBodies, Is.EquivalentTo(new[] { "RecentAndRetriedFailedMessages", "UnresolvedFailedMessages", "ArchivedAndResolvedFailedMessages", "FailedErrorImports" })); } [Test] @@ -91,7 +91,7 @@ public void The_group_comments_are_copied_after_the_host_opens_and_after_the_unr Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedFailedMessages)?.Kind, Is.EqualTo(MigrationCategoryKind.Optional)); Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.GroupComments)?.Kind, Is.EqualTo(MigrationCategoryKind.Optional)); Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.GroupComments)?.MustFollow, Is.EqualTo(MigrationCategoryIds.UnresolvedFailedMessages)); - Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)?.Kind, Is.EqualTo(MigrationCategoryKind.Required)); + Assert.That(MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages)?.Kind, Is.EqualTo(MigrationCategoryKind.Required)); } } diff --git a/src/ServiceControl.UnitTests/Migration/MigrationEngineBodyRetryTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationEngineBodyRetryTests.cs index 103d4a2043..ea67b61224 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationEngineBodyRetryTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationEngineBodyRetryTests.cs @@ -20,7 +20,7 @@ class MigrationEngineBodyRetryTests [Test] public async Task A_body_that_fails_until_the_last_attempt_then_succeeds_is_written_with_its_body_attached() { - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1")); var body = new MigrationBody(new byte[] { 1 }, "text/plain"); @@ -59,7 +59,7 @@ public async Task A_body_that_fails_until_the_last_attempt_then_succeeds_is_writ [TestCaseSource(nameof(Defects))] public async Task A_body_read_that_fails_as_a_defect_halts_the_category_on_the_first_attempt_without_skipping_the_message(Exception defect) { - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1")); source.FailBodyReads("msg-1", MigrationEngine.MaxBodyReadAttempts, defect); @@ -85,7 +85,7 @@ public async Task A_shutdown_during_a_body_read_stops_the_run_instead_of_skippin { // Retrying a shutdown to the attempt limit and then recording the message as permanently // unreadable is the one path here that silently loses a customer's failed message. - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1")); using var stopping = new CancellationTokenSource(); @@ -112,7 +112,7 @@ public async Task The_configured_backoff_is_waited_between_body_read_attempts() { // Without the wait, three attempts against a body store that is briefly down all fail inside a // millisecond and the message is skipped for an outage it would have survived. - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1"), Row("msg-2")); // Every attempt on msg-1 fails, so the guard against a wait after the final one is the only thing @@ -152,7 +152,7 @@ public async Task The_skip_warning_for_an_unreadable_body_carries_the_last_attem { // The first failure of a retry run is usually a transient connect error. The last one says what the // body store was doing when the message was given up on, and it is all the operator gets. - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1")); var failures = Enumerable.Range(1, MigrationEngine.MaxBodyReadAttempts) @@ -179,7 +179,7 @@ public async Task The_skip_warning_for_an_unreadable_body_carries_the_last_attem [Test] public async Task A_body_the_source_already_attached_is_written_without_reading_it_again() { - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); var attached = new MigrationBody(new byte[] { 7 }, "text/plain"); source.Seed(category.Id, Row("msg-1") with { Body = attached }); @@ -201,7 +201,7 @@ public async Task A_body_the_source_already_attached_is_written_without_reading_ [Test] public async Task Exhausted_attempts_skip_the_whole_message_and_count_toward_the_halt_threshold() { - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1"), Row("msg-2")); source.FailBodyReads("msg-1", MigrationEngine.MaxBodyReadAttempts, new TimeoutException("down")); @@ -225,7 +225,7 @@ public async Task Exhausted_attempts_skip_the_whole_message_and_count_toward_the [Test] public async Task A_body_store_outage_across_many_messages_halts_the_category() { - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); var ids = Enumerable.Range(1, 1_000).Select(i => $"msg-{i}").ToArray(); source.Seed(category.Id, [.. ids.Select(Row)]); diff --git a/src/ServiceControl.UnitTests/Migration/MigrationEngineCollisionTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationEngineCollisionTests.cs index 01dd62c96f..9a0a4c77be 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationEngineCollisionTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationEngineCollisionTests.cs @@ -20,7 +20,7 @@ class MigrationEngineCollisionTests [Test] public async Task Rows_already_present_in_the_target_are_counted_as_already_present_across_batches_not_skipped_or_overwritten() { - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1", "from-source"), Row("msg-2", "from-source"), Row("msg-3", "from-source"), Row("msg-4", "from-source")); var checkpointStore = new InMemoryMigrationCheckpointStore(); diff --git a/src/ServiceControl.UnitTests/Migration/MigrationEngineFailurePathTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationEngineFailurePathTests.cs index 39ce6653c2..c6281e10b7 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationEngineFailurePathTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationEngineFailurePathTests.cs @@ -122,7 +122,7 @@ await checkpointStore.Upsert(untouched with [Test] public async Task Body_skips_in_a_batch_whose_write_fails_are_counted_once_across_the_restart() { - var category = MigrationCategoryRegistry.Find("UnresolvedAndRetryIssuedFailedMessages")!; + var category = MigrationCategoryRegistry.Find("RecentAndRetriedFailedMessages")!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1"), Row("msg-2")); // msg-1's body is unreadable on both runs: every attempt fails on each. diff --git a/src/ServiceControl.UnitTests/Migration/MigrationEngineSkipReasonTests.cs b/src/ServiceControl.UnitTests/Migration/MigrationEngineSkipReasonTests.cs index 7726a173f1..0a86736d39 100644 --- a/src/ServiceControl.UnitTests/Migration/MigrationEngineSkipReasonTests.cs +++ b/src/ServiceControl.UnitTests/Migration/MigrationEngineSkipReasonTests.cs @@ -45,7 +45,7 @@ public async Task Reasons_the_target_reports_add_up_across_batches_on_the_checkp [Test] public async Task A_message_whose_body_is_never_read_is_counted_under_BodyUnreadable() { - var category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)!; + var category = MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages)!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1"), Row("msg-2") with { Body = new MigrationBody(new byte[] { 1 }, "application/octet-stream") }); source.FailBodyReads("msg-1", MigrationEngine.MaxBodyReadAttempts, new TimeoutException("down")); @@ -62,7 +62,7 @@ public async Task A_message_whose_body_is_never_read_is_counted_under_BodyUnread [Test] public async Task A_message_the_source_holds_no_body_for_is_counted_under_BodyMissing() { - var category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)!; + var category = MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages)!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1"), Row("msg-2")); source.SetBody("msg-2", new MigrationBody(new byte[] { 2 }, "text/plain")); @@ -90,7 +90,7 @@ public async Task A_batch_that_loses_rows_two_different_ways_records_both_reason { // The breakdown is what tells a customer what they lost and why. A merge that overwrote instead of // summing would leave the total right and the reasons wrong, and verification would still balance. - var category = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)!; + var category = MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages)!; var source = new InMemoryMigrationSource(); source.Seed(category.Id, Row("msg-1"), Row("msg-2") with { Body = new MigrationBody(new byte[] { 1 }, "application/octet-stream") }, Row("msg-3") with { Body = new MigrationBody(new byte[] { 1 }, "application/octet-stream") }); // One the engine skips itself because the body will not read, one the target refuses. diff --git a/src/ServiceControl.UnitTests/Migration/RequiredCopyGateTests.cs b/src/ServiceControl.UnitTests/Migration/RequiredCopyGateTests.cs index 0bd83978d7..3c9d2e03a4 100644 --- a/src/ServiceControl.UnitTests/Migration/RequiredCopyGateTests.cs +++ b/src/ServiceControl.UnitTests/Migration/RequiredCopyGateTests.cs @@ -122,7 +122,7 @@ public void The_refusal_reads_to_its_end_as_a_sentence_and_says_how_to_get_back( [Test] public void Every_failed_required_category_is_named_with_its_skips_and_both_commands() { - var withErrors = Checkpoint(MigrationCategoryState.CompleteWithErrors, MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages) with + var withErrors = Checkpoint(MigrationCategoryState.CompleteWithErrors, MigrationCategoryIds.RecentAndRetriedFailedMessages) with { CopiedCount = 40, SkippedCount = 6, @@ -136,18 +136,18 @@ public void Every_failed_required_category_is_named_with_its_skips_and_both_comm var halted = Checkpoint(MigrationCategoryState.Halted, MigrationCategoryIds.MessageRedirects) with { LastError = "TimeoutException at cursor r-7: the target did not answer" }; var exception = Assert.Throws(() => MigrationStartup.RefuseIfAnyCategoryDidNotComplete( - [MessageRedirects, UnresolvedAndRetryIssuedFailedMessages], [halted, withErrors], NewSettings())); + [MessageRedirects, RecentAndRetriedFailedMessages], [halted, withErrors], NewSettings())); using (Assert.EnterMultipleScope()) { - foreach (var categoryId in new[] { MigrationCategoryIds.MessageRedirects, MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages }) + foreach (var categoryId in new[] { MigrationCategoryIds.MessageRedirects, MigrationCategoryIds.RecentAndRetriedFailedMessages }) { Assert.That(exception.Message, Does.Contain($"--migration-retry {categoryId} once the cause is fixed").And.Contain($"--migration-abandon {categoryId} to keep what was copied"), $"one start gives every required category its go, so the refusal has to name every Failed one, {categoryId} included"); } Assert.That(exception.Message, Does.Contain($"{MigrationCategoryIds.MessageRedirects} is Failed (Halted)").And.Contain("the target did not answer")); - Assert.That(exception.Message, Does.Contain($"{MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages} is Failed (CompleteWithErrors) after copying 40 and skipping 6")); + Assert.That(exception.Message, Does.Contain($"{MigrationCategoryIds.RecentAndRetriedFailedMessages} is Failed (CompleteWithErrors) after copying 40 and skipping 6")); Assert.That(exception.Message, Does.Contain("RequiredValueMissing 2, no retry can fix"), "a row no retry can store is a reason to abandon, and the operator has to be told which"); Assert.That(exception.Message, Does.Contain("BodyUnreadable 3").And.Not.Contain("BodyUnreadable 3, no retry can fix"), "an unreadable body may read on the next try"); Assert.That(exception.Message, Does.Contain("BodyMissing 1, no retry can fix"), "a body the source does not hold is missing again on every retry"); @@ -543,7 +543,7 @@ static MigrationCheckpoint Settled(MigrationCategoryState state, long copied, lo static readonly MigrationCategory KnownEndpoints = MigrationCategoryRegistry.Find(MigrationCategoryIds.KnownEndpoints)!; static readonly MigrationCategory MessageRedirects = MigrationCategoryRegistry.Find(MigrationCategoryIds.MessageRedirects)!; static readonly MigrationCategory CustomChecks = MigrationCategoryRegistry.Find(MigrationCategoryIds.CustomChecks)!; - static readonly MigrationCategory UnresolvedAndRetryIssuedFailedMessages = MigrationCategoryRegistry.Find(MigrationCategoryIds.UnresolvedAndRetryIssuedFailedMessages)!; + static readonly MigrationCategory RecentAndRetriedFailedMessages = MigrationCategoryRegistry.Find(MigrationCategoryIds.RecentAndRetriedFailedMessages)!; const string UnknownCategoryId = "SomeCategoryFromANewerBuild";