Skip to content

Commit 84b015e

Browse files
nathan-miller23Nathan Miller
andauthored
Propagate PhysicalBridge backlog to ServerCounters (#2996)
* propagate PhysicalBridge backlog to ServerCounters * style fix --------- Co-authored-by: Nathan Miller <nathanmiller@microsoft.com>
1 parent d80bcbf commit 84b015e

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/StackExchange.Redis/PhysicalBridge.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ internal void GetCounters(ConnectionCounters counters)
287287
counters.SocketCount = Interlocked.Read(ref socketCount);
288288
counters.WriterCount = Interlocked.CompareExchange(ref activeWriters, 0, 0);
289289
counters.NonPreferredEndpointCount = Interlocked.Read(ref nonPreferredEndpointCount);
290+
counters.PendingUnsentItems = Volatile.Read(ref _backlogCurrentEnqueued);
290291
physical?.GetCounters(counters);
291292
}
292293

tests/StackExchange.Redis.Tests/BacklogTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,76 @@ static Task<TimeSpan> PingAsync(ServerEndPoint server, CommandFlags flags = Comm
398398
ClearAmbientFailures();
399399
}
400400
}
401+
402+
[Fact]
403+
public async Task TotalOutstandingIncludesBacklogQueue()
404+
{
405+
try
406+
{
407+
var options = new ConfigurationOptions()
408+
{
409+
BacklogPolicy = BacklogPolicy.Default,
410+
AbortOnConnectFail = false,
411+
ConnectTimeout = 1000,
412+
ConnectRetry = 2,
413+
SyncTimeout = 10000,
414+
KeepAlive = 10000,
415+
AsyncTimeout = 5000,
416+
AllowAdmin = true,
417+
SocketManager = SocketManager.ThreadPool,
418+
};
419+
options.EndPoints.Add(TestConfig.Current.PrimaryServerAndPort);
420+
421+
using var conn = await ConnectionMultiplexer.ConnectAsync(options, Writer);
422+
var db = conn.GetDatabase();
423+
Log("Test: Initial (connected) ping");
424+
await db.PingAsync();
425+
426+
var server = conn.GetServerSnapshot()[0];
427+
428+
// Verify TotalOutstanding is 0 when connected and idle
429+
Log("Test: asserting connected counters");
430+
var connectedServerCounters = server.GetCounters();
431+
var connectedConnCounters = conn.GetCounters();
432+
Assert.Equal(0, connectedServerCounters.Interactive.TotalOutstanding);
433+
Assert.Equal(0, connectedConnCounters.TotalOutstanding);
434+
435+
Log("Test: Simulating failure");
436+
conn.AllowConnect = false;
437+
server.SimulateConnectionFailure(SimulatedFailureType.All);
438+
439+
// Queue up some commands
440+
Log("Test: Disconnected pings");
441+
_ = db.PingAsync();
442+
_ = db.PingAsync();
443+
var lastPing = db.PingAsync();
444+
445+
Log("Test: asserting disconnected counters");
446+
var disconnectedServerCounters = server.GetCounters();
447+
var disconnectedConnCounters = conn.GetCounters();
448+
Assert.True(disconnectedServerCounters.Interactive.PendingUnsentItems >= 3, $"Expected PendingUnsentItems >= 3, got {disconnectedServerCounters.Interactive.PendingUnsentItems}");
449+
Assert.True(disconnectedConnCounters.TotalOutstanding >= 3, $"Expected TotalOutstanding >= 3, got {disconnectedServerCounters.Interactive.TotalOutstanding}");
450+
451+
Log("Test: Awaiting reconnect");
452+
conn.AllowConnect = true;
453+
await UntilConditionAsync(TimeSpan.FromSeconds(3), () => conn.IsConnected).ForAwait();
454+
455+
Log("Test: Awaiting lastPing");
456+
await lastPing;
457+
458+
Log("Test: Checking reconnected");
459+
Assert.True(conn.IsConnected);
460+
461+
Log("Test: asserting reconnected counters");
462+
var reconnectedServerCounters = server.GetCounters();
463+
var reconnectedConnCounters = conn.GetCounters();
464+
Assert.Equal(0, reconnectedServerCounters.Interactive.PendingUnsentItems);
465+
Assert.Equal(0, reconnectedConnCounters.TotalOutstanding);
466+
Log("Test: Done");
467+
}
468+
finally
469+
{
470+
ClearAmbientFailures();
471+
}
472+
}
401473
}

0 commit comments

Comments
 (0)