Skip to content

Commit

Permalink
separate based on TLS and no-TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
badrishc committed Dec 28, 2024
1 parent 363bfb9 commit 81503e8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
10 changes: 8 additions & 2 deletions benchmark/BDN.benchmark/Embedded/EmbeddedNetworkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ namespace Embedded.server
{
internal class EmbeddedNetworkHandler : NetworkHandler<GarnetServerEmbedded, EmbeddedNetworkSender>
{
readonly bool useTLS;

public EmbeddedNetworkHandler(GarnetServerEmbedded serverHook, EmbeddedNetworkSender networkSender, NetworkBufferSettings networkBufferSettings, LimitedFixedBufferPool networkPool, bool useTLS, IMessageConsumer messageConsumer = null, ILogger logger = null) : base(serverHook, networkSender, networkBufferSettings, networkPool, useTLS, messageConsumer, logger)
{
this.useTLS = useTLS;
}

public override string RemoteEndpointName => throw new NotImplementedException();
Expand All @@ -25,12 +28,15 @@ public override void Dispose()

public override bool TryClose() => throw new NotImplementedException();

public void Send(Request request)
public async ValueTask Send(Request request)
{
networkReceiveBuffer = request.buffer;
unsafe { networkReceiveBufferPtr = request.bufferPtr; }

OnNetworkReceive(request.buffer.Length);
if (useTLS)
await OnNetworkReceiveWithTLSAsync(request.buffer.Length);
else
OnNetworkReceiveWithoutTLS(request.buffer.Length);

Debug.Assert(networkBytesRead == 0);
Debug.Assert(networkReadHead == 0);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/BDN.benchmark/Network/BasicOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public override void GlobalSetup()
}

[Benchmark]
public void InlinePing()
public async ValueTask InlinePing()
{
Send(ping);
await Send(ping);
}
}
}
2 changes: 1 addition & 1 deletion benchmark/BDN.benchmark/Network/NetworkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public virtual void GlobalCleanup()
server.Dispose();
}

protected void Send(Request request) => networkHandler.Send(request);
protected ValueTask Send(Request request) => networkHandler.Send(request);

protected unsafe void SetupOperation(ref Request request, ReadOnlySpan<byte> operation, int batchSize = batchSize)
{
Expand Down
34 changes: 16 additions & 18 deletions libs/common/Networking/NetworkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,38 +267,25 @@ async Task AuthenticateAsClientAsync(SslClientAuthenticationOptions sslClientOpt
}
}

public void OnNetworkReceive(int bytesTransferred)
public unsafe void OnNetworkReceiveWithoutTLS(int bytesTransferred)
{
networkBytesRead += bytesTransferred;
transportReceiveBuffer = networkReceiveBuffer;
unsafe
{
transportReceiveBufferPtr = networkReceiveBufferPtr;
}
transportReceiveBufferPtr = networkReceiveBufferPtr;
transportBytesRead = networkBytesRead;

// We do not have an active read task, so we will process on the network thread
// Process non-TLS code on the synchronous thread
Process();

EndTransformNetworkToTransport();
UpdateNetworkBuffers();
}

private void UpdateNetworkBuffers()
{
// Shift network buffer after processing is done
if (networkReadHead > 0)
ShiftNetworkReceiveBuffer();

// Double network buffer if out of space after processing is complete
if (networkBytesRead == networkReceiveBuffer.Length)
DoubleNetworkReceiveBuffer();
}

/// <summary>
/// On network receive
/// </summary>
/// <param name="bytesTransferred">Number of bytes transferred</param>
public async ValueTask OnNetworkReceiveWithTLS(int bytesTransferred)
public async ValueTask OnNetworkReceiveWithTLSAsync(int bytesTransferred)
{
// Wait for SslStream async processing to complete, if any (e.g., authentication phase)
while (readerStatus == TlsReaderStatus.Active)
Expand Down Expand Up @@ -334,6 +321,17 @@ public async ValueTask OnNetworkReceiveWithTLS(int bytesTransferred)
UpdateNetworkBuffers();
}

void UpdateNetworkBuffers()
{
// Shift network buffer after processing is done
if (networkReadHead > 0)
ShiftNetworkReceiveBuffer();

// Double network buffer if out of space after processing is complete
if (networkBytesRead == networkReceiveBuffer.Length)
DoubleNetworkReceiveBuffer();
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowInvalidOperationException(string message)
=> throw new InvalidOperationException(message);
Expand Down
44 changes: 21 additions & 23 deletions libs/common/Networking/TcpNetworkHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ public TcpNetworkHandlerBase(TServerHook serverHook, TNetworkSender networkSende
/// <inheritdoc />
public override void Start(SslServerAuthenticationOptions tlsOptions = null, string remoteEndpointName = null, CancellationToken token = default)
{
Start();
Start(tlsOptions != null);
base.Start(tlsOptions, remoteEndpointName, token);
}

/// <inheritdoc />
public override async Task StartAsync(SslServerAuthenticationOptions tlsOptions = null, string remoteEndpointName = null, CancellationToken token = default)
{
Start();
Start(tlsOptions != null);
await base.StartAsync(tlsOptions, remoteEndpointName, token).ConfigureAwait(false);
}

/// <inheritdoc />
public override void Start(SslClientAuthenticationOptions tlsOptions, string remoteEndpointName = null, CancellationToken token = default)
{
Start();
Start(tlsOptions != null);
base.Start(tlsOptions, remoteEndpointName, token);
}

/// <inheritdoc />
public override async Task StartAsync(SslClientAuthenticationOptions tlsOptions, string remoteEndpointName = null, CancellationToken token = default)
{
Start();
Start(tlsOptions != null);
await base.StartAsync(tlsOptions, remoteEndpointName, token).ConfigureAwait(false);
}

Expand Down Expand Up @@ -103,17 +103,22 @@ public override bool TryClose()
return true;
}

void Start()
void Start(bool useTLS)
{
var receiveEventArgs = new SocketAsyncEventArgs { AcceptSocket = socket };
receiveEventArgs.SetBuffer(networkReceiveBuffer, 0, networkReceiveBuffer.Length);
receiveEventArgs.Completed += RecvEventArg_Completed;
receiveEventArgs.Completed += useTLS ? RecvEventArgCompletedWithTLS : RecvEventArgCompletedWithoutTLS;

// If the client already have packets, avoid handling it here on the handler so we don't block future accepts.
try
{
if (!socket.ReceiveAsync(receiveEventArgs))
Task.Run(() => RecvEventArg_Completed(null, receiveEventArgs));
{
if (useTLS)
Task.Run(() => RecvEventArgCompletedWithTLS(null, receiveEventArgs));
else
Task.Run(() => RecvEventArgCompletedWithoutTLS(null, receiveEventArgs));
}
}
catch (Exception ex)
{
Expand All @@ -135,20 +140,13 @@ void Dispose(SocketAsyncEventArgs e)
e.Dispose();
}

void RecvEventArg_Completed(object sender, SocketAsyncEventArgs e)
{
// Complete receive event and release thread while we process data async
if (this.useTLS)
{
_ = HandleReceiveAsync(sender, e);
}
else
{
HandleReceiveSync(sender, e);
}
}
void RecvEventArgCompletedWithTLS(object sender, SocketAsyncEventArgs e) =>
_ = HandleReceiveWithTLSAsync(sender, e);

void RecvEventArgCompletedWithoutTLS(object sender, SocketAsyncEventArgs e) =>
HandleReceiveWithoutTLS(sender, e);

private void HandleReceiveSync(object sender, SocketAsyncEventArgs e)
private void HandleReceiveWithoutTLS(object sender, SocketAsyncEventArgs e)
{
try
{
Expand All @@ -160,7 +158,7 @@ private void HandleReceiveSync(object sender, SocketAsyncEventArgs e)
Dispose(e);
break;
}
OnNetworkReceive(e.BytesTransferred);
OnNetworkReceiveWithoutTLS(e.BytesTransferred);
e.SetBuffer(networkReceiveBuffer, networkBytesRead, networkReceiveBuffer.Length - networkBytesRead);
} while (!e.AcceptSocket.ReceiveAsync(e));
}
Expand All @@ -170,7 +168,7 @@ private void HandleReceiveSync(object sender, SocketAsyncEventArgs e)
}
}

private async ValueTask HandleReceiveAsync(object sender, SocketAsyncEventArgs e)
private async ValueTask HandleReceiveWithTLSAsync(object sender, SocketAsyncEventArgs e)
{
try
{
Expand All @@ -182,7 +180,7 @@ private async ValueTask HandleReceiveAsync(object sender, SocketAsyncEventArgs e
Dispose(e);
break;
}
var receiveTask = OnNetworkReceiveWithTLS(e.BytesTransferred);
var receiveTask = OnNetworkReceiveWithTLSAsync(e.BytesTransferred);
if (!receiveTask.IsCompletedSuccessfully)
{
await receiveTask;
Expand Down

4 comments on commit 81503e8

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Network.BasicOperations (ubuntu-latest net8.0 Release)

Benchmark suite Current: 81503e8 Previous: d445d1e Ratio
BDN.benchmark.Network.BasicOperations.InlinePing(Params: None) 88.81523690053395 ns (± 0.5816015308037333) 50.03771458864212 ns (± 0.46968140226160066) 1.77

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cluster.ClusterMigrate (ubuntu-latest net8.0 Release)

Benchmark suite Current: 81503e8 Previous: d445d1e Ratio
BDN.benchmark.Cluster.ClusterMigrate.Get(Params: None) 37165.91718343099 ns (± 153.28747671802714) 37081.35537109375 ns (± 290.24467683876776) 1.00
BDN.benchmark.Cluster.ClusterMigrate.Set(Params: None) 37570.51879648062 ns (± 114.02789525676748) 38342.09306922326 ns (± 154.17135487812058) 0.98
BDN.benchmark.Cluster.ClusterMigrate.MGet(Params: None) 32826.80829092173 ns (± 57.88904072739177) 32510.78174235026 ns (± 217.33666822406425) 1.01
BDN.benchmark.Cluster.ClusterMigrate.MSet(Params: None) 32516.255192683293 ns (± 46.63985708260848) 30949.043325570914 ns (± 35.076770102430544) 1.05

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operations.BasicOperations (ubuntu-latest net8.0 Release)

Benchmark suite Current: 81503e8 Previous: d445d1e Ratio
BDN.benchmark.Operations.BasicOperations.InlinePing(Params: ACL) 1744.7107398986816 ns (± 13.077998347891867) 1740.690207417806 ns (± 9.48242670070595) 1.00
BDN.benchmark.Operations.BasicOperations.InlinePing(Params: AOF) 1776.4279101053874 ns (± 10.871243754735728) 1799.5884095705474 ns (± 7.274944550142853) 0.99
BDN.benchmark.Operations.BasicOperations.InlinePing(Params: None) 1676.5706795178926 ns (± 6.838103233986684) 1677.3249633789062 ns (± 10.37196513813268) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lua.LuaScripts (ubuntu-latest net8.0 Release)

Benchmark suite Current: 81503e8 Previous: d445d1e Ratio
BDN.benchmark.Lua.LuaScripts.Script1(Params: None) 251.7393718447004 ns (± 0.8638711654993624) 260.2515328847445 ns (± 0.36157883023270954) 0.97
BDN.benchmark.Lua.LuaScripts.Script2(Params: None) 453.6776437025804 ns (± 0.9903050066338545) 489.317353112357 ns (± 0.9334724807962262) 0.93
BDN.benchmark.Lua.LuaScripts.Script3(Params: None) 671.6425935305082 ns (± 2.49037960070585) 672.9916081110637 ns (± 2.317009208498218) 1.00
BDN.benchmark.Lua.LuaScripts.Script4(Params: None) 640.6325051967914 ns (± 0.6087487552767409) 642.3749979654948 ns (± 1.7747241653529557) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.