Skip to content

Commit 3db1683

Browse files
authored
Add transport status to subchannel picked log (#2261)
1 parent 0104983 commit 3db1683

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public void UpdateState(BalancerState state)
316316

317317
if (address != null)
318318
{
319-
ConnectionManagerLog.PickResultSuccessful(Logger, subchannel.Id, address);
319+
ConnectionManagerLog.PickResultSuccessful(Logger, subchannel.Id, address, subchannel.Transport.TransportStatus);
320320
return (subchannel, address, result.SubchannelCallTracker);
321321
}
322322
else
@@ -478,8 +478,8 @@ internal static class ConnectionManagerLog
478478
private static readonly Action<ILogger, Exception?> _pickStarted =
479479
LoggerMessage.Define(LogLevel.Trace, new EventId(5, "PickStarted"), "Pick started.");
480480

481-
private static readonly Action<ILogger, string, BalancerAddress, Exception?> _pickResultSuccessful =
482-
LoggerMessage.Define<string, BalancerAddress>(LogLevel.Debug, new EventId(6, "PickResultSuccessful"), "Successfully picked subchannel id '{SubchannelId}' with address {CurrentAddress}.");
481+
private static readonly Action<ILogger, string, BalancerAddress, TransportStatus, Exception?> _pickResultSuccessful =
482+
LoggerMessage.Define<string, BalancerAddress, TransportStatus>(LogLevel.Debug, new EventId(6, "PickResultSuccessful"), "Successfully picked subchannel id '{SubchannelId}' with address {CurrentAddress}. Transport status: {TransportStatus}");
483483

484484
private static readonly Action<ILogger, string, Exception?> _pickResultSubchannelNoCurrentAddress =
485485
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(7, "PickResultSubchannelNoCurrentAddress"), "Picked subchannel id '{SubchannelId}' doesn't have a current address.");
@@ -528,9 +528,9 @@ public static void PickStarted(ILogger logger)
528528
_pickStarted(logger, null);
529529
}
530530

531-
public static void PickResultSuccessful(ILogger logger, string subchannelId, BalancerAddress currentAddress)
531+
public static void PickResultSuccessful(ILogger logger, string subchannelId, BalancerAddress currentAddress, TransportStatus transportStatus)
532532
{
533-
_pickResultSuccessful(logger, subchannelId, currentAddress, null);
533+
_pickResultSuccessful(logger, subchannelId, currentAddress, transportStatus, null);
534534
}
535535

536536
public static void PickResultSubchannelNoCurrentAddress(ILogger logger, string subchannelId)

src/Grpc.Net.Client/Balancer/Internal/ISubchannelTransport.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,22 @@ internal interface ISubchannelTransport : IDisposable
3030
{
3131
BalancerAddress? CurrentAddress { get; }
3232
TimeSpan? ConnectTimeout { get; }
33+
TransportStatus TransportStatus { get; }
3334

34-
#if NET5_0_OR_GREATER
3535
ValueTask<Stream> GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken);
36-
#endif
37-
38-
#if !NETSTANDARD2_0 && !NET462
39-
ValueTask<ConnectResult>
40-
#else
41-
Task<ConnectResult>
42-
#endif
43-
TryConnectAsync(ConnectContext context);
36+
ValueTask<ConnectResult> TryConnectAsync(ConnectContext context);
4437

4538
void Disconnect();
4639
}
4740

41+
internal enum TransportStatus
42+
{
43+
NotConnected,
44+
Passive,
45+
InitialSocket,
46+
ActiveStream
47+
}
48+
4849
internal enum ConnectResult
4950
{
5051
Success,

src/Grpc.Net.Client/Balancer/Internal/PassiveSubchannelTransport.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,15 @@ public PassiveSubchannelTransport(Subchannel subchannel)
4444

4545
public BalancerAddress? CurrentAddress => _currentAddress;
4646
public TimeSpan? ConnectTimeout { get; }
47+
public TransportStatus TransportStatus => TransportStatus.Passive;
4748

4849
public void Disconnect()
4950
{
5051
_currentAddress = null;
5152
_subchannel.UpdateConnectivityState(ConnectivityState.Idle, "Disconnected.");
5253
}
5354

54-
public
55-
#if !NETSTANDARD2_0 && !NET462
56-
ValueTask<ConnectResult>
57-
#else
58-
Task<ConnectResult>
59-
#endif
60-
TryConnectAsync(ConnectContext context)
55+
public ValueTask<ConnectResult> TryConnectAsync(ConnectContext context)
6156
{
6257
Debug.Assert(_subchannel._addresses.Count == 1);
6358
Debug.Assert(CurrentAddress == null);
@@ -68,23 +63,17 @@ public void Disconnect()
6863
_currentAddress = currentAddress;
6964
_subchannel.UpdateConnectivityState(ConnectivityState.Ready, "Passively connected.");
7065

71-
#if !NETSTANDARD2_0 && !NET462
7266
return new ValueTask<ConnectResult>(ConnectResult.Success);
73-
#else
74-
return Task.FromResult(ConnectResult.Success);
75-
#endif
7667
}
7768

7869
public void Dispose()
7970
{
8071
_currentAddress = null;
8172
}
8273

83-
#if NET5_0_OR_GREATER
8474
public ValueTask<Stream> GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken)
8575
{
8676
throw new NotSupportedException();
8777
}
88-
#endif
8978
}
9079
#endif

src/Grpc.Net.Client/Balancer/Internal/SocketConnectivitySubchannelTransport.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ public SocketConnectivitySubchannelTransport(
8282
private object Lock => _subchannel.Lock;
8383
public BalancerAddress? CurrentAddress => _currentAddress;
8484
public TimeSpan? ConnectTimeout { get; }
85+
public TransportStatus TransportStatus
86+
{
87+
get
88+
{
89+
lock (Lock)
90+
{
91+
if (_initialSocket != null)
92+
{
93+
return TransportStatus.InitialSocket;
94+
}
95+
if (_activeStreams.Count > 0)
96+
{
97+
return TransportStatus.ActiveStream;
98+
}
99+
return TransportStatus.NotConnected;
100+
}
101+
}
102+
}
85103

86104
// For testing. Take a copy under lock for thread-safety.
87105
internal IReadOnlyList<ActiveStream> GetActiveStreams()

test/Grpc.Net.Client.Tests/Infrastructure/Balancer/TestSubChannelTransport.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -40,6 +40,7 @@ internal class TestSubchannelTransport : ISubchannelTransport
4040

4141
public BalancerAddress? CurrentAddress { get; private set; }
4242
public TimeSpan? ConnectTimeout => _factory.ConnectTimeout;
43+
public TransportStatus TransportStatus => TransportStatus.Passive;
4344

4445
public Task TryConnectTask => _connectTcs.Task;
4546

0 commit comments

Comments
 (0)