Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CheckFreeSpace CustomCheck moved into persister and some aligment (#4026
) * Align MinimumStorageRequiredForIngestion * Move CheckFreeDiskSpace into persistence layer * Align custom checks for ingestion and free space with main instance plus get rid of double validation (things are still messy but better than before) * Keep sane thresholds * Fix custom check tests by resolving them from the container and introduce the same test in the main instance * Change to direct service provider usage instead * Aligh custom checks and make sure they pass for external ravendb * Further small alignment * Formatting * Remove unneeded default setting in app.config * Use database path in exception messages * Register audit custom checks manually in tests only to avoid double registration * Adjust custom check to make it work with ATT hacks * Cleanup and unify implementations --------- Co-authored-by: danielmarbach <danielmarbach@users.noreply.github.com> Co-authored-by: Brandon Ording <bording@gmail.com>
- Loading branch information
1 parent
a946f83
commit 4828aa2
Showing
39 changed files
with
393 additions
and
397 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/ServiceControl.Audit.Persistence.RavenDB/CustomChecks/CheckFreeDiskSpace.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace ServiceControl.Audit.Persistence.RavenDB.CustomChecks | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NServiceBus.CustomChecks; | ||
using NServiceBus.Logging; | ||
using RavenDB; | ||
|
||
class CheckFreeDiskSpace(DatabaseConfiguration databaseConfiguration) : CustomCheck("ServiceControl.Audit database", "Storage space", TimeSpan.FromMinutes(5)) | ||
{ | ||
public override Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default) | ||
{ | ||
if (Logger.IsDebugEnabled) | ||
{ | ||
Logger.Debug($"Check ServiceControl data drive space remaining custom check starting. Threshold {percentageThreshold:P0}"); | ||
} | ||
|
||
if (!databaseConfiguration.ServerConfiguration.UseEmbeddedServer) | ||
{ | ||
return CheckResult.Pass; | ||
} | ||
|
||
if (dataPathRoot is null) | ||
{ | ||
throw new Exception($"Unable to find the root of the data path {databaseConfiguration.ServerConfiguration.DbPath}"); | ||
} | ||
|
||
var dataDriveInfo = new DriveInfo(dataPathRoot); | ||
var availableFreeSpace = (decimal)dataDriveInfo.AvailableFreeSpace; | ||
var totalSpace = (decimal)dataDriveInfo.TotalSize; | ||
|
||
var percentRemaining = (decimal)dataDriveInfo.AvailableFreeSpace / dataDriveInfo.TotalSize; | ||
|
||
if (Logger.IsDebugEnabled) | ||
{ | ||
Logger.Debug($"Free space: {availableFreeSpace:N0}B | Total: {totalSpace:N0}B | Percent remaining {percentRemaining:P1}"); | ||
} | ||
|
||
return percentRemaining > percentageThreshold | ||
? CheckResult.Pass | ||
: CheckResult.Failed($"{percentRemaining:P0} disk space remaining on data drive '{dataDriveInfo.VolumeLabel} ({dataDriveInfo.RootDirectory})' on '{Environment.MachineName}'."); | ||
} | ||
|
||
public static int Parse(IDictionary<string, string> settings) | ||
{ | ||
if (!settings.TryGetValue(RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey, out var thresholdValue)) | ||
{ | ||
thresholdValue = $"{DataSpaceRemainingThresholdDefault}"; | ||
} | ||
|
||
string message; | ||
if (!int.TryParse(thresholdValue, out var threshold)) | ||
{ | ||
message = $"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} must be an integer."; | ||
Logger.Fatal(message); | ||
throw new Exception(message); | ||
} | ||
|
||
if (threshold < 0) | ||
{ | ||
message = $"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} is invalid, minimum value is 0."; | ||
Logger.Fatal(message); | ||
throw new Exception(message); | ||
} | ||
|
||
if (threshold > 100) | ||
{ | ||
message = $"{RavenPersistenceConfiguration.DataSpaceRemainingThresholdKey} is invalid, maximum value is 100."; | ||
Logger.Fatal(message); | ||
throw new Exception(message); | ||
} | ||
|
||
return threshold; | ||
} | ||
|
||
readonly string dataPathRoot = Path.GetPathRoot(databaseConfiguration.ServerConfiguration.DbPath); | ||
readonly decimal percentageThreshold = databaseConfiguration.DataSpaceRemainingThreshold / 100m; | ||
|
||
public const int DataSpaceRemainingThresholdDefault = 20; | ||
static readonly ILog Logger = LogManager.GetLogger(typeof(CheckFreeDiskSpace)); | ||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
...ol.Audit.Persistence.RavenDB/CustomChecks/CheckMinimumStorageRequiredForAuditIngestion.cs
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
...Control.Audit.Persistence.RavenDB/CustomChecks/CheckMinimumStorageRequiredForIngestion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
namespace ServiceControl.Audit.Persistence.RavenDB.CustomChecks | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NServiceBus.CustomChecks; | ||
using NServiceBus.Logging; | ||
using RavenDB; | ||
|
||
class CheckMinimumStorageRequiredForIngestion(MinimumRequiredStorageState stateHolder, DatabaseConfiguration databaseConfiguration) : CustomCheck("Audit Message Ingestion Process", "ServiceControl.Audit Health", TimeSpan.FromSeconds(5)) | ||
{ | ||
public override Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default) | ||
{ | ||
var percentageThreshold = databaseConfiguration.MinimumStorageLeftRequiredForIngestion / 100m; | ||
|
||
if (Logger.IsDebugEnabled) | ||
{ | ||
Logger.Debug($"Check ServiceControl data drive space starting. Threshold {percentageThreshold:P0}"); | ||
} | ||
|
||
// Should be checking UseEmbeddedServer but need to check DbPath instead for the ATT hack to work | ||
if (string.IsNullOrEmpty(databaseConfiguration.ServerConfiguration.DbPath)) | ||
{ | ||
stateHolder.CanIngestMore = true; | ||
return SuccessResult; | ||
} | ||
|
||
var dataPathRoot = Path.GetPathRoot(databaseConfiguration.ServerConfiguration.DbPath) ?? throw new Exception($"Unable to find the root of the data path {databaseConfiguration.ServerConfiguration.DbPath}"); | ||
|
||
var dataDriveInfo = new DriveInfo(dataPathRoot); | ||
var availableFreeSpace = (decimal)dataDriveInfo.AvailableFreeSpace; | ||
var totalSpace = (decimal)dataDriveInfo.TotalSize; | ||
|
||
var percentRemaining = (decimal)dataDriveInfo.AvailableFreeSpace / dataDriveInfo.TotalSize; | ||
|
||
if (Logger.IsDebugEnabled) | ||
{ | ||
Logger.Debug($"Free space: {availableFreeSpace} | Total: {totalSpace} | Percent remaining {percentRemaining:P0}"); | ||
} | ||
|
||
if (percentRemaining > percentageThreshold) | ||
{ | ||
stateHolder.CanIngestMore = true; | ||
return SuccessResult; | ||
} | ||
|
||
var message = $"Audit message ingestion stopped! {percentRemaining:P0} disk space remaining on data drive '{dataDriveInfo.VolumeLabel} ({dataDriveInfo.RootDirectory})' on '{Environment.MachineName}'. This is less than {percentageThreshold}% - the minimal required space configured. The threshold can be set using the {RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} configuration setting."; | ||
Logger.Warn(message); | ||
stateHolder.CanIngestMore = false; | ||
return CheckResult.Failed(message); | ||
} | ||
|
||
public static int Parse(IDictionary<string, string> settings) | ||
{ | ||
if (!settings.TryGetValue(RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey, out var thresholdValue)) | ||
{ | ||
thresholdValue = $"{MinimumStorageLeftRequiredForIngestionDefault}"; | ||
} | ||
|
||
if (!int.TryParse(thresholdValue, out var threshold)) | ||
{ | ||
var message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} must be an integer."; | ||
Logger.Fatal(message); | ||
throw new Exception(message); | ||
} | ||
|
||
if (threshold < 0) | ||
{ | ||
var message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} is invalid, minimum value is 0."; | ||
Logger.Fatal(message); | ||
throw new Exception(message); | ||
} | ||
|
||
if (threshold > 100) | ||
{ | ||
var message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} is invalid, maximum value is 100."; | ||
Logger.Fatal(message); | ||
throw new Exception(message); | ||
} | ||
|
||
return threshold; | ||
} | ||
|
||
public const int MinimumStorageLeftRequiredForIngestionDefault = 5; | ||
static readonly Task<CheckResult> SuccessResult = Task.FromResult(CheckResult.Pass); | ||
static readonly ILog Logger = LogManager.GetLogger(typeof(CheckMinimumStorageRequiredForIngestion)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/ServiceControl.Audit.Persistence.RavenDB/MinimumRequiredStorageState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace ServiceControl.Audit.Persistence.RavenDB | ||
{ | ||
public class MinimumRequiredStorageState | ||
{ | ||
public bool CanIngestMore { get; set; } = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.