-
Notifications
You must be signed in to change notification settings - Fork 50
Add custom check for failing retention #5903
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
427979d
Add custom check for failing retention
rbev b8bd8e8
Better naming for custom check
rbev f8041bc
Reset dev config
rbev c41e48b
Add targeted interface for faking so custom check doesn't need a real…
rbev e460141
Move the retention custom check state out of the sweeper
rbev fc8012f
Add link to troubleshooting page, keep the check silent until it fail…
rbev 4eaa546
Check consecutive failures in custom check instead of baking it into …
rbev 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
60 changes: 60 additions & 0 deletions
60
src/ServiceControl.Persistence.EFCore/Infrastructure/RetentionSweepCustomCheck.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,60 @@ | ||
| namespace ServiceControl.Persistence.EFCore.Infrastructure; | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using ServiceControl.Persistence.EFCore.Infrastructure.Metrics; | ||
| using NServiceBus.CustomChecks; | ||
|
|
||
| // The standard custom-check state-transition pipeline deduplicates repeated results. | ||
| class RetentionSweepCustomCheck(RetentionSweepCustomCheck.State state) | ||
| : CustomCheck("ServiceControl Retention", "ServiceControl Health", TimeSpan.FromMinutes(1)) | ||
| { | ||
| public override Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default) | ||
| { | ||
| var failures = state.GetFailures(); | ||
| var failureSummary = string.Join("; ", failures.Select(failure => $"{failure.Key}: {failure.Value}")); | ||
|
|
||
| return Task.FromResult(state.ConsecutiveFailedSweeps < 3 | ||
| ? CheckResult.Pass | ||
| : CheckResult.Failed($"Retention processing has failures. Last failure per entity: {failureSummary}. See https://docs.particular.net/servicecontrol/troubleshooting for guidance on resolving the issue.")); | ||
| } | ||
|
|
||
| internal class State | ||
| { | ||
| readonly Dictionary<RetentionEntity, string> failures = []; | ||
|
|
||
| public int ConsecutiveFailedSweeps { get; private set; } | ||
|
|
||
| public void Clear(RetentionEntity entity) | ||
| { | ||
| lock (failures) | ||
| { | ||
| failures.Remove(entity); | ||
| } | ||
| } | ||
|
|
||
| public void ReportError(RetentionEntity entity, string reason) | ||
| { | ||
| lock (failures) | ||
| { | ||
| failures[entity] = reason; | ||
| } | ||
| } | ||
|
|
||
| public void SweepComplete() | ||
| { | ||
| lock (failures) | ||
| { | ||
| ConsecutiveFailedSweeps = failures.Count == 0 ? 0 : ConsecutiveFailedSweeps + 1; | ||
| } | ||
| } | ||
|
|
||
| internal KeyValuePair<RetentionEntity, string>[] GetFailures() | ||
| { | ||
| lock (failures) | ||
| { | ||
| return failures.ToArray(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
1 change: 1 addition & 0 deletions
1
...rsistence.Tests.PostgreSql/ApprovalFiles/CustomCheckTests.VerifyCustomChecks.approved.txt
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| ServiceControl Health: ServiceControl Retention | ||
| Storage space: ServiceControl body storage |
1 change: 1 addition & 0 deletions
1
...ersistence.Tests.SqlServer/ApprovalFiles/CustomCheckTests.VerifyCustomChecks.approved.txt
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| ServiceControl Health: ServiceControl Retention | ||
| Storage space: ServiceControl body storage |
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
150 changes: 150 additions & 0 deletions
150
src/ServiceControl.Persistence.Tests/EFCore/RetentionSweepCustomCheckTests.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,150 @@ | ||
| namespace ServiceControl.Persistence.Tests; | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using NServiceBus.CustomChecks; | ||
| using NUnit.Framework; | ||
| using ServiceControl.Contracts.CustomChecks; | ||
| using ServiceControl.CustomChecks; | ||
| using ServiceControl.Infrastructure.DomainEvents; | ||
| using ServiceControl.Operations; | ||
| using ServiceControl.Persistence.EFCore.Infrastructure; | ||
| using ServiceControl.Persistence.EFCore.Infrastructure.Metrics; | ||
|
|
||
| class RetentionSweepCustomCheckTests : PersistenceTestBase | ||
| { | ||
| RetentionSweeper Sweeper => ServiceProvider.GetRequiredService<RetentionSweeper>(); | ||
| RetentionSweepCustomCheck.State State => ServiceProvider.GetRequiredService<RetentionSweepCustomCheck.State>(); | ||
|
|
||
| RetentionSweepCustomCheck Check => | ||
| ServiceProvider.GetServices<ICustomCheck>().OfType<RetentionSweepCustomCheck>().Single(); | ||
|
|
||
| [Test] | ||
| public async Task Check_initially_passes() | ||
| { | ||
| var result = await Check.PerformCheck(); | ||
| Assert.That(result, Is.EqualTo(CheckResult.Pass)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Check_remains_passing_until_three_consecutive_failed_sweeps() | ||
| { | ||
| State.ReportError(RetentionEntity.FailedMessages, "db timeout"); | ||
|
|
||
| State.SweepComplete(); | ||
| Assert.That(await Check.PerformCheck(), Is.EqualTo(CheckResult.Pass)); | ||
| State.SweepComplete(); | ||
| Assert.That(await Check.PerformCheck(), Is.EqualTo(CheckResult.Pass)); | ||
| State.SweepComplete(); | ||
|
|
||
| var result = await Check.PerformCheck(); | ||
| using (Assert.EnterMultipleScope()) | ||
| { | ||
| Assert.That(result.HasFailed, Is.True); | ||
| Assert.That(result.FailureReason, Does.Contain("FailedMessages")); | ||
| Assert.That(result.FailureReason, Does.Contain("db timeout")); | ||
| Assert.That(result.FailureReason, Does.Contain("https://docs.particular.net/servicecontrol/troubleshooting")); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Successful_sweep_resets_the_consecutive_failure_count() | ||
| { | ||
| State.ReportError(RetentionEntity.FailedMessages, "db timeout"); | ||
| CompleteThreeSweeps(); | ||
|
|
||
| await Sweeper.SweepNow(); | ||
| State.ReportError(RetentionEntity.FailedMessages, "another timeout"); | ||
| State.SweepComplete(); | ||
|
|
||
| var result = await Check.PerformCheck(); | ||
| Assert.That(result, Is.EqualTo(CheckResult.Pass)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Check_does_not_expire_with_time() | ||
| { | ||
| State.ReportError(RetentionEntity.FailedMessages, "db timeout"); | ||
| CompleteThreeSweeps(); | ||
| AdvanceClock(TimeSpan.FromHours(2)); | ||
|
|
||
| var result = await Check.PerformCheck(); | ||
| Assert.That(result.HasFailed, Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Multiple_failing_entities_are_represented() | ||
| { | ||
| State.ReportError(RetentionEntity.FailedMessages, "body delete failed"); | ||
| State.ReportError(RetentionEntity.EventLog, "batch delete timeout"); | ||
| CompleteThreeSweeps(); | ||
|
|
||
| var result = await Check.PerformCheck(); | ||
|
|
||
| using (Assert.EnterMultipleScope()) | ||
| { | ||
| Assert.That(result.HasFailed, Is.True); | ||
| Assert.That(result.FailureReason, Does.Contain("FailedMessages")); | ||
| Assert.That(result.FailureReason, Does.Contain("EventLog")); | ||
| Assert.That(result.FailureReason, Does.Contain("body delete failed")); | ||
| Assert.That(result.FailureReason, Does.Contain("batch delete timeout")); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Clearing_one_retention_type_keeps_other_failures() | ||
| { | ||
| State.ReportError(RetentionEntity.FailedMessages, "body delete failed"); | ||
| State.ReportError(RetentionEntity.EventLog, "batch delete timeout"); | ||
| CompleteThreeSweeps(); | ||
|
|
||
| State.Clear(RetentionEntity.FailedMessages); | ||
|
|
||
| var result = await Check.PerformCheck(); | ||
| using (Assert.EnterMultipleScope()) | ||
| { | ||
| Assert.That(result.FailureReason, Does.Not.Contain("FailedMessages")); | ||
| Assert.That(result.FailureReason, Does.Contain("EventLog")); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Repeated_failed_results_raise_one_state_change_event() | ||
| { | ||
| State.ReportError(RetentionEntity.FailedMessages, "db timeout"); | ||
| CompleteThreeSweeps(); | ||
|
|
||
| var result = await Check.PerformCheck(); | ||
| var domainEvents = (FakeDomainEvents)ServiceProvider.GetRequiredService<IDomainEvents>(); | ||
| var processor = new CustomCheckResultProcessor(domainEvents, CustomChecks, NullLogger<CustomCheckResultProcessor>.Instance); | ||
| var detail = new CustomCheckDetail | ||
| { | ||
| Category = "ServiceControl Health", | ||
| CustomCheckId = "ServiceControl Retention", | ||
| HasFailed = result.HasFailed, | ||
| FailureReason = result.FailureReason, | ||
| ReportedAt = Now, | ||
| OriginatingEndpoint = new EndpointDetails | ||
| { | ||
| Host = "localhost", | ||
| HostId = Guid.NewGuid(), | ||
| Name = "ServiceControl" | ||
| } | ||
| }; | ||
|
|
||
| await processor.ProcessResult(detail); | ||
| await processor.ProcessResult(detail); | ||
|
|
||
| Assert.That(domainEvents.RaisedEvents.OfType<CustomCheckFailed>().Count(), Is.EqualTo(1)); | ||
| } | ||
|
|
||
| void CompleteThreeSweeps() | ||
| { | ||
| State.SweepComplete(); | ||
| State.SweepComplete(); | ||
| State.SweepComplete(); | ||
| } | ||
| } |
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
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 may need to adjust ServicePulse to render these links correctly