Skip to content

Commit 7dc6813

Browse files
minor tweaks
1 parent 99cb666 commit 7dc6813

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/ServiceControl.Audit.Persistence.RavenDB/CustomChecks/CheckDirtyMemory.cs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,37 @@ class CheckDirtyMemory(DatabaseConfiguration databaseConfiguration) : CustomChec
1313
public override async Task<CheckResult> PerformCheck(CancellationToken cancellationToken = default)
1414
{
1515
var retriever = await GetMemoryRetriever();
16-
var memoryInfo = await retriever.GetMemoryInformation(cancellationToken);
16+
var (isHighDirty, dirtyMemory) = await retriever.GetMemoryInformation(cancellationToken);
1717

18-
if (memoryInfo.IsHighDirty)
18+
if (isHighDirty)
1919
{
20-
var message = $"There is a high level of dirty memory ({memoryInfo.DirtyMemory}kb). Check the ServiceControl " +
20+
var message = $"There is a high level of dirty memory ({dirtyMemory}kb). Check the ServiceControl " +
2121
"troubleshooting guide for guidance on how to mitigate the issue.";
2222
Log.Warn(message);
2323
return CheckResult.Failed(message);
2424
}
2525

26-
lastDirtyMemoryReads.Add(memoryInfo.DirtyMemory);
26+
lastDirtyMemoryReads.Add(dirtyMemory);
2727
if (lastDirtyMemoryReads.Count > 20)
2828
{
2929
//cap the list at 20 which means we're keeping about 1 hour and 40 minutes of data
3030
lastDirtyMemoryReads.RemoveAt(0);
3131
}
3232

33-
if (lastDirtyMemoryReads.Count < 3)
33+
switch (lastDirtyMemoryReads.Count)
3434
{
35-
Log.Debug("Not enough dirty memory data in the series to calculate a trend.");
36-
}
37-
38-
// TODO do we need a threshold below which the check never fails?
39-
// Three means we'll be observing for 15 minutes before calculating the trend
40-
if (lastDirtyMemoryReads.Count >= 3 && AnalyzeTrendUsingRegression(lastDirtyMemoryReads) == TrendDirection.Increasing)
41-
{
42-
var message = $"Dirty memory is increasing. Last available value is {memoryInfo.DirtyMemory}kb. " +
43-
$"Check the ServiceControl troubleshooting guide for guidance on how to mitigate the issue.";
44-
Log.Warn(message);
45-
return CheckResult.Failed(message);
35+
case < 3:
36+
Log.Debug("Not enough dirty memory data in the series to calculate a trend.");
37+
break;
38+
// TODO do we need a threshold below which the check never fails?
39+
// Three means we'll be observing for 15 minutes before calculating the trend
40+
case >= 3 when AnalyzeTrendUsingRegression(lastDirtyMemoryReads) == TrendDirection.Increasing:
41+
{
42+
var message = $"Dirty memory is increasing. Last available value is {dirtyMemory}kb. " +
43+
$"Check the ServiceControl troubleshooting guide for guidance on how to mitigate the issue.";
44+
Log.Warn(message);
45+
return CheckResult.Failed(message);
46+
}
4647
}
4748

4849
return CheckResult.Pass;
@@ -53,7 +54,7 @@ public override async Task<CheckResult> PerformCheck(CancellationToken cancellat
5354

5455
static TrendDirection AnalyzeTrendUsingRegression(List<int> values)
5556
{
56-
if (values == null || values.Count <= 1)
57+
if (values is not { Count: > 1 })
5758
{
5859
throw new ArgumentException("Need at least two values to determine a trend");
5960
}
@@ -84,12 +85,7 @@ static TrendDirection AnalyzeTrendUsingRegression(List<int> values)
8485
return TrendDirection.Flat;
8586
}
8687

87-
if (slope > 0)
88-
{
89-
return TrendDirection.Increasing;
90-
}
91-
92-
return TrendDirection.Decreasing;
88+
return slope > 0 ? TrendDirection.Increasing : TrendDirection.Decreasing;
9389
}
9490

9591
enum TrendDirection

0 commit comments

Comments
 (0)