-
Notifications
You must be signed in to change notification settings - Fork 46
/
CheckMinimumStorageRequiredForIngestion.cs
81 lines (66 loc) · 3.31 KB
/
CheckMinimumStorageRequiredForIngestion.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace ServiceControl.Operations
{
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus.CustomChecks;
using NServiceBus.Logging;
using Persistence.RavenDB;
using ServiceControl.Persistence;
class CheckMinimumStorageRequiredForIngestion(
MinimumRequiredStorageState stateHolder,
RavenPersisterSettings settings)
: CustomCheck("Message Ingestion Process", "ServiceControl Health", TimeSpan.FromSeconds(5))
{
public override Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default)
{
percentageThreshold = settings.MinimumStorageLeftRequiredForIngestion / 100m;
if (dataPathRoot == null)
{
stateHolder.CanIngestMore = true;
return SuccessResult;
}
Logger.Debug($"Check ServiceControl data drive space starting. Threshold {percentageThreshold:P0}");
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}");
}
if (percentRemaining > percentageThreshold)
{
stateHolder.CanIngestMore = true;
return SuccessResult;
}
var message = $"Error 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 {RavenBootstrapper.MinimumStorageLeftRequiredForIngestionKey} configuration setting.";
Logger.Warn(message);
stateHolder.CanIngestMore = false;
return CheckResult.Failed(message);
}
public static void Validate(RavenPersisterSettings settings)
{
int threshold = settings.MinimumStorageLeftRequiredForIngestion;
string message;
if (threshold < 0)
{
message = $"{RavenBootstrapper.MinimumStorageLeftRequiredForIngestionKey} is invalid, minimum value is 0.";
Logger.Fatal(message);
throw new Exception(message);
}
if (threshold > 100)
{
message = $"{RavenBootstrapper.MinimumStorageLeftRequiredForIngestionKey} is invalid, maximum value is 100.";
Logger.Fatal(message);
throw new Exception(message);
}
}
public const int MinimumStorageLeftRequiredForIngestionDefault = 20;
readonly string dataPathRoot = Path.GetPathRoot(settings.DatabasePath);
decimal percentageThreshold;
static readonly Task<CheckResult> SuccessResult = Task.FromResult(CheckResult.Pass);
static readonly ILog Logger = LogManager.GetLogger(typeof(CheckMinimumStorageRequiredForIngestion));
}
}