Skip to content

Commit

Permalink
Add transport status to subchannel picked log (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Aug 26, 2023
1 parent 0104983 commit 3db1683
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void UpdateState(BalancerState state)

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

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

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

public static void PickResultSuccessful(ILogger logger, string subchannelId, BalancerAddress currentAddress)
public static void PickResultSuccessful(ILogger logger, string subchannelId, BalancerAddress currentAddress, TransportStatus transportStatus)
{
_pickResultSuccessful(logger, subchannelId, currentAddress, null);
_pickResultSuccessful(logger, subchannelId, currentAddress, transportStatus, null);
}

public static void PickResultSubchannelNoCurrentAddress(ILogger logger, string subchannelId)
Expand Down
19 changes: 10 additions & 9 deletions src/Grpc.Net.Client/Balancer/Internal/ISubchannelTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ internal interface ISubchannelTransport : IDisposable
{
BalancerAddress? CurrentAddress { get; }
TimeSpan? ConnectTimeout { get; }
TransportStatus TransportStatus { get; }

#if NET5_0_OR_GREATER
ValueTask<Stream> GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken);
#endif

#if !NETSTANDARD2_0 && !NET462
ValueTask<ConnectResult>
#else
Task<ConnectResult>
#endif
TryConnectAsync(ConnectContext context);
ValueTask<ConnectResult> TryConnectAsync(ConnectContext context);

void Disconnect();
}

internal enum TransportStatus
{
NotConnected,
Passive,
InitialSocket,
ActiveStream
}

internal enum ConnectResult
{
Success,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,15 @@ public PassiveSubchannelTransport(Subchannel subchannel)

public BalancerAddress? CurrentAddress => _currentAddress;
public TimeSpan? ConnectTimeout { get; }
public TransportStatus TransportStatus => TransportStatus.Passive;

public void Disconnect()
{
_currentAddress = null;
_subchannel.UpdateConnectivityState(ConnectivityState.Idle, "Disconnected.");
}

public
#if !NETSTANDARD2_0 && !NET462
ValueTask<ConnectResult>
#else
Task<ConnectResult>
#endif
TryConnectAsync(ConnectContext context)
public ValueTask<ConnectResult> TryConnectAsync(ConnectContext context)
{
Debug.Assert(_subchannel._addresses.Count == 1);
Debug.Assert(CurrentAddress == null);
Expand All @@ -68,23 +63,17 @@ public void Disconnect()
_currentAddress = currentAddress;
_subchannel.UpdateConnectivityState(ConnectivityState.Ready, "Passively connected.");

#if !NETSTANDARD2_0 && !NET462
return new ValueTask<ConnectResult>(ConnectResult.Success);
#else
return Task.FromResult(ConnectResult.Success);
#endif
}

public void Dispose()
{
_currentAddress = null;
}

#if NET5_0_OR_GREATER
public ValueTask<Stream> GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
#endif
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ public SocketConnectivitySubchannelTransport(
private object Lock => _subchannel.Lock;
public BalancerAddress? CurrentAddress => _currentAddress;
public TimeSpan? ConnectTimeout { get; }
public TransportStatus TransportStatus
{
get
{
lock (Lock)
{
if (_initialSocket != null)
{
return TransportStatus.InitialSocket;
}
if (_activeStreams.Count > 0)
{
return TransportStatus.ActiveStream;
}
return TransportStatus.NotConnected;
}
}
}

// For testing. Take a copy under lock for thread-safety.
internal IReadOnlyList<ActiveStream> GetActiveStreams()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright notice and license
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
Expand Down Expand Up @@ -40,6 +40,7 @@ internal class TestSubchannelTransport : ISubchannelTransport

public BalancerAddress? CurrentAddress { get; private set; }
public TimeSpan? ConnectTimeout => _factory.ConnectTimeout;
public TransportStatus TransportStatus => TransportStatus.Passive;

public Task TryConnectTask => _connectTcs.Task;

Expand Down

0 comments on commit 3db1683

Please sign in to comment.