Skip to content

Commit

Permalink
Fix disposal caused Null ref
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdaankhalidmsft committed Oct 24, 2024
1 parent 3a7c4ba commit 4f055bf
Showing 1 changed file with 27 additions and 8 deletions.
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

0 comments on commit 4f055bf

Please sign in to comment.