Skip to content

Commit

Permalink
Align custom checks for ingestion and free space with main instance p…
Browse files Browse the repository at this point in the history
…lus get rid of double validation (things are still messy but better than before)
  • Loading branch information
danielmarbach committed Mar 21, 2024
1 parent d136277 commit d43dd72
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 65 deletions.
@@ -1,6 +1,7 @@
namespace ServiceControl.Audit.Persistence.RavenDB.CustomChecks
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,7 +14,7 @@ class CheckFreeDiskSpace : CustomCheck
public CheckFreeDiskSpace(DatabaseConfiguration databaseConfiguration) : base("ServiceControl.Audit database", "Storage space", TimeSpan.FromMinutes(5))
{
dataPath = databaseConfiguration.ServerConfiguration.DbPath;
percentageThreshold = databaseConfiguration.MinimumStorageLeftRequiredForIngestion / 100m;
percentageThreshold = databaseConfiguration.DataSpaceRemainingThreshold / 100m;
Logger.Debug($"Check ServiceControl data drive space remaining custom check starting. Threshold {percentageThreshold:P0}");
}

Expand Down Expand Up @@ -42,9 +43,42 @@ public override Task<CheckResult> PerformCheck(CancellationToken cancellationTok
: 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 dataPath;
readonly decimal percentageThreshold;

public const int DataSpaceRemainingThresholdDefault = 20;
static readonly ILog Logger = LogManager.GetLogger(typeof(CheckFreeDiskSpace));
}
}
@@ -1,6 +1,7 @@
namespace ServiceControl.Audit.Persistence.RavenDB.CustomChecks
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -49,6 +50,40 @@ public override Task<CheckResult> PerformCheck(CancellationToken cancellationTok
return CheckResult.Failed(message);
}

public static int Parse(IDictionary<string, string> settings)
{
if (!settings.TryGetValue(RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey, out var thresholdValue))
{
thresholdValue = $"{MinimumStorageLeftRequiredForIngestionDefault}";
}

string message;
if (!int.TryParse(thresholdValue, out var threshold))
{
message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} must be an integer.";
Logger.Fatal(message);
throw new Exception(message);
}

if (threshold < 0)
{
message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} is invalid, minimum value is 0.";
Logger.Fatal(message);
throw new Exception(message);
}

if (threshold > 100)
{
message = $"{RavenPersistenceConfiguration.MinimumStorageLeftRequiredForIngestionKey} is invalid, maximum value is 100.";
Logger.Fatal(message);
throw new Exception(message);
}

return threshold;
}

public const int MinimumStorageLeftRequiredForIngestionDefault = 20;

static readonly Task<CheckResult> SuccessResult = Task.FromResult(CheckResult.Pass);
static readonly ILog Logger = LogManager.GetLogger(typeof(CheckMinimumStorageRequiredForIngestion));
}
Expand Down
Expand Up @@ -9,6 +9,7 @@ public class DatabaseConfiguration(
bool enableFullTextSearch,
TimeSpan auditRetentionPeriod,
int maxBodySizeToStore,
int dataSpaceRemainingThreshold,
int minimumStorageLeftRequiredForIngestion,
ServerConfiguration serverConfiguration)
{
Expand All @@ -26,6 +27,8 @@ public class DatabaseConfiguration(

public int MaxBodySizeToStore { get; } = maxBodySizeToStore;

public int DataSpaceRemainingThreshold { get; } = dataSpaceRemainingThreshold;

public int MinimumStorageLeftRequiredForIngestion { get; internal set; } = minimumStorageLeftRequiredForIngestion; //Setting for ATT only
}
}
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using CustomChecks;
using NServiceBus.Logging;

public class RavenPersistenceConfiguration : IPersistenceConfiguration
Expand All @@ -16,6 +17,7 @@ public class RavenPersistenceConfiguration : IPersistenceConfiguration
public const string LogPathKey = "LogPath";
public const string RavenDbLogLevelKey = "RavenDBLogLevel";
public const string MinimumStorageLeftRequiredForIngestionKey = "MinimumStorageLeftRequiredForIngestion";
public const string DataSpaceRemainingThresholdKey = "DataSpaceRemainingThreshold";

public IEnumerable<string> ConfigurationKeys => new[]{
DatabaseNameKey,
Expand All @@ -25,6 +27,7 @@ public class RavenPersistenceConfiguration : IPersistenceConfiguration
ExpirationProcessTimerInSecondsKey,
LogPathKey,
RavenDbLogLevelKey,
DataSpaceRemainingThresholdKey,
MinimumStorageLeftRequiredForIngestionKey
};

Expand Down Expand Up @@ -89,15 +92,8 @@ internal static DatabaseConfiguration GetDatabaseConfiguration(PersistenceSettin
serverConfiguration = new ServerConfiguration(dbPath, serverUrl, logPath, logsMode);
}

if (!settings.PersisterSpecificSettings.TryGetValue(MinimumStorageLeftRequiredForIngestionKey, out var minimumStorageLeftRequiredForIngestionKey))
{
minimumStorageLeftRequiredForIngestionKey = "5";
}

if (!int.TryParse(minimumStorageLeftRequiredForIngestionKey, out var minimumStorageLeftRequiredForIngestion))
{
throw new InvalidOperationException($"{MinimumStorageLeftRequiredForIngestionKey} must be an integer.");
}
var dataSpaceRemainingThreshold = CheckFreeDiskSpace.Parse(settings.PersisterSpecificSettings);
var minimumStorageLeftRequiredForIngestion = CheckMinimumStorageRequiredForIngestion.Parse(settings.PersisterSpecificSettings);

var expirationProcessTimerInSeconds = GetExpirationProcessTimerInSeconds(settings);

Expand All @@ -107,6 +103,7 @@ internal static DatabaseConfiguration GetDatabaseConfiguration(PersistenceSettin
settings.EnableFullTextSearchOnBodies,
settings.AuditRetentionPeriod,
settings.MaxBodySizeToStore,
dataSpaceRemainingThreshold,
minimumStorageLeftRequiredForIngestion,
serverConfiguration);
}
Expand Down
Expand Up @@ -33,7 +33,7 @@ public static async Task<EmbeddedDatabase> GetInstance(CancellationToken cancell
var logsMode = "Operations";
var serverUrl = $"http://localhost:{PortUtility.FindAvailablePort(33334)}";

embeddedDatabase = EmbeddedDatabase.Start(new DatabaseConfiguration("audit", 60, true, TimeSpan.FromMinutes(5), 120000, 5, new ServerConfiguration(dbPath, serverUrl, logPath, logsMode)));
embeddedDatabase = EmbeddedDatabase.Start(new DatabaseConfiguration("audit", 60, true, TimeSpan.FromMinutes(5), 120000, 5, 5, new ServerConfiguration(dbPath, serverUrl, logPath, logsMode)));

//make sure that the database is up
using var documentStore = await embeddedDatabase.Connect(cancellationToken);
Expand Down
Expand Up @@ -20,7 +20,6 @@
"ServiceName": "Particular.ServiceControl.Audit",
"TransportConnectionString": null,
"MaximumConcurrencyLevel": 32,
"DataSpaceRemainingThreshold": 20,
"ServiceControlQueueAddress": "Particular.ServiceControl",
"TimeToRestartAuditIngestionAfterFailure": "00:01:00",
"EnableFullTextSearchOnBodies": true
Expand Down
25 changes: 0 additions & 25 deletions src/ServiceControl.Audit/Infrastructure/Settings/Settings.cs
Expand Up @@ -35,7 +35,6 @@ public Settings(string serviceName, string transportType = null, string persiste
AuditRetentionPeriod = GetAuditRetentionPeriod();
Port = SettingsReader.Read(SettingsRootNamespace, "Port", 44444);
MaximumConcurrencyLevel = SettingsReader.Read(SettingsRootNamespace, "MaximumConcurrencyLevel", 32);
DataSpaceRemainingThreshold = GetDataSpaceRemainingThreshold();
ServiceControlQueueAddress = SettingsReader.Read<string>(SettingsRootNamespace, "ServiceControlQueueAddress");
TimeToRestartAuditIngestionAfterFailure = GetTimeToRestartAuditIngestionAfterFailure();
EnableFullTextSearchOnBodies = SettingsReader.Read(SettingsRootNamespace, "EnableFullTextSearchOnBodies", true);
Expand Down Expand Up @@ -132,8 +131,6 @@ public int MaxBodySizeToStore

public string TransportConnectionString { get; set; }
public int MaximumConcurrencyLevel { get; set; }
public int DataSpaceRemainingThreshold { get; set; }

public string ServiceControlQueueAddress { get; set; }

public TimeSpan TimeToRestartAuditIngestionAfterFailure { get; set; }
Expand Down Expand Up @@ -264,27 +261,6 @@ static string Subscope(string address)
return $"{queue}.log@{machine}";
}

int GetDataSpaceRemainingThreshold()
{
string message;
var threshold = SettingsReader.Read(SettingsRootNamespace, "DataSpaceRemainingThreshold", DataSpaceRemainingThresholdDefault);
if (threshold < 0)
{
message = $"{nameof(DataSpaceRemainingThreshold)} is invalid, minimum value is 0.";
logger.Fatal(message);
throw new Exception(message);
}

if (threshold > 100)
{
message = $"{nameof(DataSpaceRemainingThreshold)} is invalid, maximum value is 100.";
logger.Fatal(message);
throw new Exception(message);
}

return threshold;
}

void TryLoadLicenseFromConfig() => LicenseFileText = SettingsReader.Read<string>(SettingsRootNamespace, "LicenseText");

ILog logger = LogManager.GetLogger(typeof(Settings));
Expand All @@ -293,6 +269,5 @@ int GetDataSpaceRemainingThreshold()
public static readonly SettingsRootNamespace SettingsRootNamespace = new("ServiceControl.Audit");

const int MaxBodySizeToStoreDefault = 102400; //100 kb
const int DataSpaceRemainingThresholdDefault = 20;
}
}
Expand Up @@ -69,7 +69,7 @@ public static void Validate(RavenPersisterSettings settings)
}
}

public const int MinimumStorageLeftRequiredForIngestionDefault = 5;
public const int MinimumStorageLeftRequiredForIngestionDefault = 20;

readonly string dataPathRoot = Path.GetPathRoot(settings.DatabasePath);

Expand Down
Expand Up @@ -36,6 +36,5 @@
"MaximumConcurrencyLevel": 10,
"RetryHistoryDepth": 10,
"RemoteInstances": [],
"DataSpaceRemainingThreshold": 20,
"DisableHealthChecks": false
}
26 changes: 0 additions & 26 deletions src/ServiceControl/Infrastructure/Settings/Settings.cs
Expand Up @@ -52,7 +52,6 @@ public class Settings
AllowMessageEditing = SettingsReader.Read<bool>(SettingsRootNamespace, "AllowMessageEditing");
NotificationsFilter = SettingsReader.Read<string>(SettingsRootNamespace, "NotificationsFilter");
RemoteInstances = GetRemoteInstances().ToArray();
DataSpaceRemainingThreshold = GetDataSpaceRemainingThreshold();
TimeToRestartErrorIngestionAfterFailure = GetTimeToRestartErrorIngestionAfterFailure();
DisableExternalIntegrationsPublishing = SettingsReader.Read(SettingsRootNamespace, "DisableExternalIntegrationsPublishing", false);
}
Expand Down Expand Up @@ -164,8 +163,6 @@ public TimeSpan HeartbeatGracePeriod

public RemoteInstanceSetting[] RemoteInstances { get; set; }

public int DataSpaceRemainingThreshold { get; set; }

public bool DisableHealthChecks { get; set; }

public ITransportCustomization LoadTransportCustomization()
Expand Down Expand Up @@ -370,27 +367,6 @@ static string Subscope(string address)
return $"{queue}.log@{machine}";
}

int GetDataSpaceRemainingThreshold()
{
string message;
var threshold = SettingsReader.Read(SettingsRootNamespace, "DataSpaceRemainingThreshold", DataSpaceRemainingThresholdDefault);
if (threshold < 0)
{
message = $"{nameof(DataSpaceRemainingThreshold)} is invalid, minimum value is 0.";
logger.Fatal(message);
throw new Exception(message);
}

if (threshold > 100)
{
message = $"{nameof(DataSpaceRemainingThreshold)} is invalid, maximum value is 100.";
logger.Fatal(message);
throw new Exception(message);
}

return threshold;
}

void LoadErrorIngestionSettings()
{
var serviceBusRootNamespace = new SettingsRootNamespace("ServiceBus");
Expand Down Expand Up @@ -422,7 +398,5 @@ void LoadErrorIngestionSettings()
static readonly ILog logger = LogManager.GetLogger(typeof(Settings));
public const string DEFAULT_SERVICE_NAME = "Particular.ServiceControl";
public static readonly SettingsRootNamespace SettingsRootNamespace = new("ServiceControl");

const int DataSpaceRemainingThresholdDefault = 20;
}
}

0 comments on commit d43dd72

Please sign in to comment.