Skip to content
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

Fix Server Exit Null Reference Exception #747

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions libs/server/Metrics/Latency/GarnetLatencyMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ void Init()

public void Return()
{
if (metrics == null)
return;

foreach (var cmd in defaultLatencyTypes)
{
metrics[(int)cmd].Return();
Expand All @@ -46,7 +49,8 @@ public void Return()

public void Merge(GarnetLatencyMetricsSession lm)
{
if (lm.metrics == null) return;
// Metrics can be null if we are shutting down the server but there are still remaining resp server session being disposed. Early return to handle graceful exit during server disposal.
if (lm.metrics == null || metrics == null) return;
int ver = lm.PriorVersion; // Use prior version for merge
for (int i = 0; i < metrics.Length; i++)
if (lm.metrics[i].latency[ver].TotalCount > 0)
Expand All @@ -55,14 +59,19 @@ public void Merge(GarnetLatencyMetricsSession lm)

public void Reset(LatencyMetricsType cmd)
{
// Early return to handle graceful exit during server disposal.
if (metrics == null)
return;

int idx = (int)cmd;
metrics[idx].latency.Reset();
}

private List<MetricsItem> GetPercentiles(int idx)
{
if (metrics[idx].latency.TotalCount == 0)
if (metrics == null || metrics[idx].latency.TotalCount == 0)
return new();

var curr = metrics[idx].latency;


Expand Down Expand Up @@ -119,7 +128,9 @@ private List<MetricsItem> GetPercentiles(int idx)
public bool GetRespHistogram(int idx, out string response, LatencyMetricsType eventType)
{
response = "";
if (metrics[idx].latency.TotalCount == 0)

// Early return to handle graceful exit during server disposal.
if (metrics == null || metrics[idx].latency.TotalCount == 0)
return false;

var p = GetPercentiles(idx);
Expand Down Expand Up @@ -150,20 +161,28 @@ public string GetRespHistograms(HashSet<LatencyMetricsType> events)
{
int cmdCount = 0;
string response = "";
foreach (var eventType in events)

if (metrics != null)
{
int idx = (int)eventType;
if (GetRespHistogram(idx, out var cmdHistogram, eventType))
foreach (var eventType in events)
{
response += cmdHistogram;
cmdCount++;
int idx = (int)eventType;
if (GetRespHistogram(idx, out var cmdHistogram, eventType))
{
response += cmdHistogram;
cmdCount++;
}
}
}

return cmdCount == 0 ? "*0\r\n" : $"*{cmdCount * 2}\r\n" + response;
}

public MetricsItem[] GetLatencyMetrics(LatencyMetricsType latencyMetricsType)
{
if (metrics == null)
return [];

int idx = (int)latencyMetricsType;
return GetPercentiles(idx)?.ToArray();
}
Expand Down