Skip to content

Commit

Permalink
Format Uptime Output from INFO SERVER (#225)
Browse files Browse the repository at this point in the history
* fix uptime format

* Gossip.cs code cleanup
  • Loading branch information
vazois committed Apr 2, 2024
1 parent 18df19b commit 31b352c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
46 changes: 24 additions & 22 deletions libs/cluster/Server/Gossip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public List<string> GetBanList()
var expiry = w.Value;
var diff = expiry - DateTimeOffset.UtcNow.Ticks;

string str = $"{nodeId} : {TimeSpan.FromTicks(diff).Seconds}";
var str = $"{nodeId} : {TimeSpan.FromTicks(diff).Seconds}";
banlist.Add(str);
}
return banlist;
Expand All @@ -135,12 +135,14 @@ public List<string> GetBanList()
public MetricsItem[] GetPrimaryLinkStatus(ClusterConfig config)
{
var primaryId = config.GetLocalNodePrimaryId();
var primaryLinkStatus = new MetricsItem[2];
var primaryLinkStatus = new MetricsItem[2]
{
new("master_link_status", "down"),
new("master_last_io_seconds_ago", "0")
};

primaryLinkStatus[0] = new("master_link_status", "down");
primaryLinkStatus[1] = new("master_last_io_seconds_ago", "0");
if (primaryId != null)
clusterConnectionStore.GetConnectionInfo(primaryId, ref primaryLinkStatus);
_ = clusterConnectionStore.GetConnectionInfo(primaryId, ref primaryLinkStatus);
return primaryLinkStatus;
}

Expand Down Expand Up @@ -178,14 +180,14 @@ private void DecrementConfigMerge()
private void TryStartGossipTasks()
{
// Start background task for gossip protocol
for (int i = 2; i <= CurrentConfig.NumWorkers; i++)
for (var i = 2; i <= CurrentConfig.NumWorkers; i++)
{
var (address, port) = CurrentConfig.GetWorkerAddress((ushort)i);
RunMeetTask(address, port);
}

Interlocked.Increment(ref numActiveTasks);
Task.Run(GossipMain);
_ = Interlocked.Increment(ref numActiveTasks);
_ = Task.Run(GossipMain);
}

/// <summary>
Expand Down Expand Up @@ -240,7 +242,7 @@ public void Meet(string address, int port)
var conf = CurrentConfig;
var nodeId = conf.GetWorkerNodeIdFromAddress(address, port);
MemoryResult<byte> resp = default;
bool created = false;
var created = false;

gossipStats.UpdateMeetRequestsRecv();
try
Expand Down Expand Up @@ -268,7 +270,7 @@ public void Meet(string address, int port)

logger?.LogInformation("MEET {nodeId} {address} {port}", nodeId, address, port);
// Merge without a check because node is trusted as meet was issued by admin
TryMerge(other);
_ = TryMerge(other);

gossipStats.UpdateMeetRequestsSucceed();

Expand Down Expand Up @@ -306,10 +308,10 @@ private void DisposeBannedWorkerConnections()
if (!Expired(expiry))
{
// Remove connection for banned worker
clusterConnectionStore.TryRemove(nodeId);
_ = clusterConnectionStore.TryRemove(nodeId);
}
else // Remove worker from ban list
workerBanList.TryRemove(nodeId, out var _);
_ = workerBanList.TryRemove(nodeId, out var _);
}
}

Expand Down Expand Up @@ -375,12 +377,12 @@ public void BroadcastGossipSend()

gossipStats.gossip_timeout_count++;
logger?.LogWarning("GOSSIP to remote node [{nodeId} {address}:{port}] timeout!", currNode.nodeid, currNode.address, currNode.port);
clusterConnectionStore.TryRemove(currNode.nodeid);
_ = clusterConnectionStore.TryRemove(currNode.nodeid);
}
catch (Exception ex)
{
logger?.LogWarning(ex, "GOSSIP to remote node [{nodeId} {address} {port}] failed!", currNode.nodeid, currNode.address, currNode.port);
clusterConnectionStore.TryRemove(currNode.nodeid);
_ = clusterConnectionStore.TryRemove(currNode.nodeid);
gossipStats.gossip_failed_count++;
}
}
Expand All @@ -392,16 +394,16 @@ public void BroadcastGossipSend()
public void GossipSampleSend()
{
var nodeCount = clusterConnectionStore.Count;
int fraction = (int)(Math.Ceiling(nodeCount * (GossipSamplePercent / 100.0f)));
int count = Math.Max(Math.Min(1, nodeCount), fraction);
var fraction = (int)(Math.Ceiling(nodeCount * (GossipSamplePercent / 100.0f)));
var count = Math.Max(Math.Min(1, nodeCount), fraction);

long startTime = DateTimeOffset.UtcNow.Ticks;
var startTime = DateTimeOffset.UtcNow.Ticks;
while (count > 0)
{
long minSend = startTime;
var minSend = startTime;
GarnetServerNode currNode = null;

for (int i = 0; i < 3; i++)
for (var i = 0; i < 3; i++)
{
// Pick the node with earliest send timestamp
if (clusterConnectionStore.GetRandomConnection(out var c) && c.GossipSend < minSend)
Expand All @@ -426,12 +428,12 @@ public void GossipSampleSend()

gossipStats.gossip_timeout_count++;
logger?.LogWarning("GOSSIP to remote node [{nodeId} {address}:{port}] timeout!", currNode.nodeid, currNode.address, currNode.port);
clusterConnectionStore.TryRemove(currNode.nodeid);
_ = clusterConnectionStore.TryRemove(currNode.nodeid);
}
catch (Exception ex)
{
logger?.LogError(ex, "GOSSIP to remote node [{nodeId} {address} {port}] failed!", currNode.nodeid, currNode.address, currNode.port);
clusterConnectionStore.TryRemove(currNode.nodeid);
_ = clusterConnectionStore.TryRemove(currNode.nodeid);
gossipStats.gossip_failed_count++;
}

Expand Down Expand Up @@ -482,7 +484,7 @@ private async void GossipMain()
{
logger?.LogWarning("Error disposing closing gossip connections {msg}", ex.Message);
}
Interlocked.Decrement(ref numActiveTasks);
_ = Interlocked.Decrement(ref numActiveTasks);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions libs/server/Metrics/Info/GarnetInfoMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ private void PopulateServerInfo(StoreWrapper storeWrapper)
new("os", Environment.OSVersion.ToString()),
new("processor_count", Environment.ProcessorCount.ToString()),
new("arch_bits", Environment.Is64BitProcess ? "64" : "32"),
new("uptime_in_seconds", uptime.TotalSeconds.ToString()),
new("uptime_in_days", uptime.TotalDays.ToString()),
new("uptime_in_seconds", uptime.Seconds.ToString()),
new("uptime_in_days", uptime.Days.ToString()),
new("monitor_task", storeWrapper.serverOptions.MetricsSamplingFrequency > 0 ? "enabled" : "disabled"),
new("monitor_freq", storeWrapper.serverOptions.MetricsSamplingFrequency.ToString()),
new("latency_monitor", storeWrapper.serverOptions.LatencyMonitor ? "enabled" : "disabled"),
Expand Down

0 comments on commit 31b352c

Please sign in to comment.