-
Notifications
You must be signed in to change notification settings - Fork 51
Implement manual retention sweep #5845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rbev
wants to merge
3
commits into
master
Choose a base branch
from
allow-manual-retention-sweep
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
src/ServiceControl.AcceptanceTests/WebApi/When_triggering_a_manual_retention_sweep.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| namespace ServiceControl.AcceptanceTests.WebApi; | ||
|
|
||
| using System; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Json; | ||
| using System.Text.Json; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using AcceptanceTesting; | ||
| using NServiceBus.AcceptanceTesting; | ||
| using NUnit.Framework; | ||
| using ServiceControl.Api.Contracts; | ||
|
|
||
| class When_triggering_a_manual_retention_sweep : AcceptanceTest | ||
| { | ||
| [Test] | ||
| public async Task Should_be_available_on_efcore_persisters() | ||
| { | ||
| if (StorageConfiguration.PersistenceType == "RavenDB") | ||
| { | ||
| Assert.Ignore("RavenDB has no sweeper — covered by Should_return_501_on_a_ravendb_backed_instance."); | ||
| return; | ||
| } | ||
|
|
||
| HttpStatusCode started = default; | ||
| HttpStatusCode invalidCutoff = default; | ||
| RetentionSweepStatus completion = null; | ||
|
|
||
| await Define<Context>() | ||
| .Done(async _ => | ||
| { | ||
| // Trigger a sweep with a past UTC cutoff. The delete work runs in the background, | ||
| // so the call returns 202 Accepted immediately. | ||
| using var response = await HttpClient.PostAsJsonAsync( | ||
| "/api/retention/sweep", | ||
| new RetentionSweepRequest { ErrorCutoff = DateTime.UtcNow.AddDays(-30) }, | ||
| SerializerOptions); | ||
|
|
||
| started = response.StatusCode; | ||
|
|
||
| // A future-dated cutoff is rejected with 400. | ||
| using var badRequest = await HttpClient.PostAsJsonAsync( | ||
| "/api/retention/sweep", | ||
| new RetentionSweepRequest { ErrorCutoff = DateTime.UtcNow.AddDays(1) }, | ||
| SerializerOptions); | ||
|
|
||
| invalidCutoff = badRequest.StatusCode; | ||
|
|
||
| // The status endpoint must report the run, and the background sweep must complete. | ||
| completion = await WaitUntilSweepFinishes(); | ||
|
|
||
| return true; | ||
| }) | ||
| .Run(); | ||
|
|
||
| using (Assert.EnterMultipleScope()) | ||
| { | ||
| Assert.That(started, Is.EqualTo(HttpStatusCode.Accepted), "the sweep should start in the background"); | ||
| Assert.That(invalidCutoff, Is.EqualTo(HttpStatusCode.BadRequest), "a future cutoff must be rejected"); | ||
| Assert.That(completion, Is.Not.Null, "the background sweep must complete"); | ||
| Assert.That(completion.IsRunning, Is.False, "the background sweep must complete"); | ||
| Assert.That(completion.LastStartedAt, Is.Not.Null); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_report_background_completion_via_status() | ||
| { | ||
| if (StorageConfiguration.PersistenceType == "RavenDB") | ||
| { | ||
| Assert.Ignore("RavenDB has no sweeper — covered by Should_return_501_on_a_ravendb_backed_instance."); | ||
| return; | ||
| } | ||
|
|
||
| RetentionSweepStatus completion = null; | ||
|
|
||
| await Define<Context>() | ||
| .Done(async _ => | ||
| { | ||
| using var response = await HttpClient.PostAsJsonAsync( | ||
| "/api/retention/sweep", | ||
| new RetentionSweepRequest { ErrorCutoff = DateTime.UtcNow.AddDays(-30) }, | ||
| SerializerOptions); | ||
|
|
||
| completion = await WaitUntilSweepFinishes(); | ||
| return response.StatusCode == HttpStatusCode.Accepted; | ||
| }) | ||
| .Run(); | ||
|
|
||
|
|
||
| using (Assert.EnterMultipleScope()) | ||
| { | ||
| Assert.That(completion, Is.Not.Null, "the background sweep must complete"); | ||
| Assert.That(completion.IsRunning, Is.False); | ||
| Assert.That(completion.LastFinishedAt, Is.Not.Null, "a completed run records its finish time"); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_return_501_on_a_ravendb_backed_instance() | ||
| { | ||
| if (StorageConfiguration.PersistenceType != "RavenDB") | ||
| { | ||
| Assert.Ignore("EFCore persisters support the sweep — covered by the efcore tests."); | ||
| return; | ||
| } | ||
|
|
||
| HttpStatusCode postStatus = default; | ||
| HttpStatusCode getStatus = default; | ||
|
|
||
| await Define<Context>() | ||
| .Done(async _ => | ||
| { | ||
| using var response = await HttpClient.PostAsJsonAsync( | ||
| "/api/retention/sweep", | ||
| new RetentionSweepRequest { ErrorCutoff = DateTime.UtcNow.AddDays(-30) }, | ||
| SerializerOptions); | ||
|
|
||
| postStatus = response.StatusCode; | ||
|
|
||
| using var status = await HttpClient.GetAsync("/api/retention/sweep/status"); | ||
|
|
||
| getStatus = status.StatusCode; | ||
|
|
||
| return true; | ||
| }) | ||
| .Run(); | ||
|
|
||
| using (Assert.EnterMultipleScope()) | ||
| { | ||
| // RavenDB retention is the server-side @expires bundle; there is no cutoff-based sweeper | ||
| // to trigger, so the optional IRetentionSweeper resolution is absent and both verbs | ||
| // return 501 Not Implemented. | ||
| Assert.That(postStatus, Is.EqualTo(HttpStatusCode.NotImplemented), "POST must report not-supported on RavenDB"); | ||
| Assert.That(getStatus, Is.EqualTo(HttpStatusCode.NotImplemented), "GET status must report not-supported on RavenDB"); | ||
| } | ||
| } | ||
|
|
||
| async Task<RetentionSweepStatus> WaitUntilSweepFinishes(TimeSpan? timeout = null) | ||
| { | ||
| var deadline = DateTime.UtcNow + (timeout ?? TimeSpan.FromSeconds(30)); | ||
|
|
||
| while (DateTime.UtcNow < deadline) | ||
| { | ||
| using var response = await HttpClient.GetAsync("/api/retention/sweep/status"); | ||
|
|
||
| if (response.StatusCode == HttpStatusCode.OK) | ||
| { | ||
| var status = await response.Content.ReadFromJsonAsync<RetentionSweepStatus>(SerializerOptions); | ||
|
|
||
| if (status is { IsRunning: false }) | ||
| { | ||
| return status; | ||
| } | ||
| } | ||
|
|
||
| await Task.Delay(TimeSpan.FromMilliseconds(200)); | ||
| } | ||
|
|
||
| throw new Exception("The manual retention sweep did not finish within the timeout."); | ||
| } | ||
|
|
||
| class Context : ScenarioContext; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace ServiceControl.Api.Contracts; | ||
|
|
||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Request body for <c>POST /api/retention/sweep</c>. Both cutoffs are optional; when omitted | ||
| /// the corresponding sub-sweep derives its cutoff from the configured retention period, as the | ||
| /// scheduled hourly sweep does. A bare future-dated cutoff is rejected. | ||
| /// </summary> | ||
| public class RetentionSweepRequest | ||
| { | ||
| /// <summary> | ||
| /// Cutoff applied to the failed-message sweep. <c>null</c> means | ||
| /// <c>now - ErrorRetentionPeriod</c>. | ||
| /// </summary> | ||
| public DateTime? ErrorCutoff { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Cutoff applied to the event-log sweep. <c>null</c> means | ||
| /// <c>now - EventsRetentionPeriod</c>. | ||
| /// </summary> | ||
| public DateTime? EventsCutoff { get; set; } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/ServiceControl.Api/Contracts/RetentionSweepResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| namespace ServiceControl.Api.Contracts; | ||
|
|
||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Response body for <c>POST /api/retention/sweep</c>. The <c>Status</c> field signals the | ||
| /// outcome: <c>started</c> (202), <c>already-running</c> (409), or | ||
| /// <c>not-supported</c> (501). | ||
| /// </summary> | ||
| public class RetentionSweepResponse | ||
| { | ||
| public string Status { get; set; } | ||
|
|
||
| public DateTime? StartedAt { get; set; } | ||
|
|
||
| public DateTime? ErrorCutoff { get; set; } | ||
|
|
||
| public DateTime? EventsCutoff { get; set; } | ||
|
|
||
| /// <summary>A human-readable reason included when the operation is not supported.</summary> | ||
| public string Reason { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| namespace ServiceControl.Api.Contracts; | ||
|
|
||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Response body for <c>GET /api/retention/sweep/status</c>. On a persister with no sweeper | ||
| /// (e.g. RavenDB) the endpoint returns 501 with a <see cref="Reason"/> instead. | ||
| /// </summary> | ||
| public class RetentionSweepStatus | ||
| { | ||
| public bool IsRunning { get; set; } | ||
|
|
||
| public DateTime? LastStartedAt { get; set; } | ||
|
|
||
| public DateTime? LastFinishedAt { get; set; } | ||
|
|
||
| public DateTime? LastErrorCutoff { get; set; } | ||
|
|
||
| public DateTime? LastEventsCutoff { get; set; } | ||
|
|
||
| public string LastError { get; set; } | ||
|
|
||
| /// <summary>Present only on the 501 Not Implemented response.</summary> | ||
| public string Reason { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| namespace ServiceControl.Api; | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Contracts; | ||
|
|
||
| /// <summary> | ||
| /// Manual retention-sweep API. The implementation resolves the persister's sweeper | ||
| /// optionally: when no sweeper is registered (e.g. RavenDB, which uses server-side document | ||
| /// expiration) the operations report that the feature is not supported rather than silently | ||
| /// no-op'ing. | ||
| /// </summary> | ||
| public interface IRetentionApi | ||
| { | ||
| /// <summary> | ||
| /// Starts a manual retention sweep with caller-supplied cutoffs. The delete work runs in | ||
| /// the background on a host-lifetime token; this method returns as soon as the run is | ||
| /// accepted (or refused because one is already running / unsupported). | ||
| /// </summary> | ||
| Task<RetentionSweepResponse> SweepAsync(RetentionSweepRequest request, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Returns a point-in-time snapshot of sweep execution state for polling. | ||
| /// </summary> | ||
| Task<RetentionSweepStatus> GetStatusAsync(CancellationToken cancellationToken = default); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't tend to use the postfix "Async", the return gives it away