Skip to content

Commit c927b02

Browse files
kevin-montroseunsafePtrbadrishc
authored
fix: use Stopwatch for INFO durations (#1771) (#1779)
Co-authored-by: Nikolay Zdravkov <unsafeptr@gmail.com> Co-authored-by: Badrish Chandramouli <badrishc@microsoft.com>
1 parent b0eea3d commit c927b02

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

libs/cluster/Server/Replication/ReplicationManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ internal sealed partial class ReplicationManager : IDisposable
3434
readonly ILogger logger;
3535
bool _disposed;
3636

37-
private long primary_sync_last_time;
37+
private long primary_sync_last_timestamp;
3838

39-
internal long LastPrimarySyncSeconds => IsRecovering ? (DateTime.UtcNow.Ticks - primary_sync_last_time) / TimeSpan.TicksPerSecond : 0;
39+
internal long LastPrimarySyncSeconds => IsRecovering ? (long)Stopwatch.GetElapsedTime(primary_sync_last_timestamp).TotalSeconds : 0;
4040

41-
internal void UpdateLastPrimarySyncTime() => this.primary_sync_last_time = DateTime.UtcNow.Ticks;
41+
internal void UpdateLastPrimarySyncTime() => this.primary_sync_last_timestamp = Stopwatch.GetTimestamp();
4242

4343
private SingleWriterMultiReaderLock recoverLock;
4444
private SingleWriterMultiReaderLock recoveryStateChangeLock;

libs/server/Metrics/Info/GarnetInfoMetrics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class GarnetInfoMetrics
4848

4949
private void PopulateServerInfo(StoreWrapper storeWrapper)
5050
{
51-
var uptime = TimeSpan.FromTicks(DateTimeOffset.UtcNow.Ticks - storeWrapper.startupTime);
51+
var uptime = Stopwatch.GetElapsedTime(storeWrapper.startupTimestamp);
5252
serverInfo =
5353
[
5454
new("garnet_version", storeWrapper.version),

libs/server/StoreWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed class StoreWrapper
2828
internal readonly string version;
2929
internal readonly string redisProtocolVersion;
3030
readonly IGarnetServer[] servers;
31-
internal readonly long startupTime;
31+
internal readonly long startupTimestamp;
3232

3333
/// <summary>
3434
/// Default database (DB 0)
@@ -205,7 +205,7 @@ public StoreWrapper(
205205
this.version = version;
206206
this.redisProtocolVersion = redisProtocolVersion;
207207
this.servers = servers;
208-
this.startupTime = DateTimeOffset.UtcNow.Ticks;
208+
this.startupTimestamp = Stopwatch.GetTimestamp();
209209
this.serverOptions = serverOptions;
210210
this.subscribeBroker = subscribeBroker;
211211
this.customCommandManager = customCommandManager;

test/Garnet.test/RespInfoTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ public void ResetStatsTest()
7575
ClassicAssert.AreEqual("total_found:1", totalFound, "Expected total_found to be one after sending one successful request");
7676
}
7777

78+
[Test]
79+
public void UptimeIncreasesAcrossInfoCalls()
80+
{
81+
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
82+
var db = redis.GetDatabase(0);
83+
84+
static long ParseUptime(string info) =>
85+
long.Parse(info.Split("\r\n").First(x => x.StartsWith("uptime_in_seconds:")).Split(':')[1]);
86+
87+
var first = ParseUptime(db.Execute("INFO", "SERVER").ToString());
88+
ClassicAssert.GreaterOrEqual(first, 0);
89+
90+
Thread.Sleep(TimeSpan.FromSeconds(1.1));
91+
92+
var second = ParseUptime(db.Execute("INFO", "SERVER").ToString());
93+
ClassicAssert.Greater(second, first, "uptime_in_seconds should increase between INFO calls");
94+
}
95+
7896
[Test]
7997
[TestCase("ALL")]
8098
[TestCase("DEFAULT")]

0 commit comments

Comments
 (0)