Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert sync over async network handling to async #835

Open
wants to merge 70 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
1b3dca6
test garnet benchmark
msft-paddy14 Oct 8, 2024
bb6f943
Merge branch 'main' of github.com:microsoft/garnet
msft-paddy14 Oct 29, 2024
2929295
Merge branch 'main' of github.com:microsoft/garnet
msft-paddy14 Nov 25, 2024
43c5c13
Revert "test garnet benchmark"
msft-paddy14 Nov 25, 2024
ab602de
add async socketprocessing
msft-paddy14 Nov 26, 2024
16e66f6
resolve conflict
msft-paddy14 Nov 26, 2024
2b7dd00
fix async socket processing
msft-paddy14 Nov 26, 2024
3de873f
remove bad files
msft-paddy14 Nov 27, 2024
771cba6
fix formatting
msft-paddy14 Nov 28, 2024
efbcabe
fix exception handling
msft-paddy14 Nov 28, 2024
b1f689c
Merge branch 'main' into users/padgupta/add_async_socket_processing
vazois Dec 3, 2024
b7fce4b
Merge branch 'main' into users/padgupta/add_async_socket_processing
vazois Dec 3, 2024
d390c39
1. convert Task->ValueTask for netowkr processing
msft-paddy14 Dec 5, 2024
95f8ae3
add condition await
msft-paddy14 Dec 6, 2024
3685fcc
Merge branch 'main' into users/padgupta/add_async_socket_processing
vazois Dec 9, 2024
0f61eee
Merge branch 'main' into users/padgupta/add_async_socket_processing
vazois Dec 9, 2024
1f3efb3
add bdn test for networking
msft-paddy14 Dec 16, 2024
5de4248
add async in test
msft-paddy14 Dec 16, 2024
2a30dd8
Merge branch 'users/padgupta/add_async_socket_processing' of github.c…
msft-paddy14 Dec 16, 2024
28cf2ac
remove visibility changes for testing
msft-paddy14 Dec 16, 2024
5dfa63f
add bdn test for networking
msft-paddy14 Dec 16, 2024
6708f98
add async in test
msft-paddy14 Dec 16, 2024
5982be1
fix tests for generic path
msft-paddy14 Dec 16, 2024
0ea355f
remove visibility changes for testing
msft-paddy14 Dec 16, 2024
3fc28b0
fix tests for generic path
msft-paddy14 Dec 16, 2024
6d21e2e
fix formatting
msft-paddy14 Dec 17, 2024
eb40b2f
fix formatting
msft-paddy14 Dec 17, 2024
5420f57
rewrite network test
msft-paddy14 Dec 17, 2024
d83009d
rewrite network test
msft-paddy14 Dec 17, 2024
f20e20a
set max/min threads
msft-paddy14 Dec 17, 2024
841407f
set max/min threads
msft-paddy14 Dec 17, 2024
7cc2d22
fix formatting
msft-paddy14 Dec 17, 2024
467614c
fix formatting
msft-paddy14 Dec 17, 2024
8f5625a
fix naming violations
msft-paddy14 Dec 17, 2024
3c34c7b
remove dead code
msft-paddy14 Dec 17, 2024
5707567
formatting
msft-paddy14 Dec 17, 2024
b2063a3
Merge branch 'main' into users/padgupta/add_async_socket_processing
vazois Dec 17, 2024
39c5ebe
replace with do while
msft-paddy14 Dec 17, 2024
4f5d3ca
remove networking test in BDN
msft-paddy14 Dec 18, 2024
6cae8b7
address formatting comments
msft-paddy14 Dec 19, 2024
1459249
remove bad include
msft-paddy14 Dec 19, 2024
848fcd6
remove more dead code
msft-paddy14 Dec 19, 2024
70e067c
Merge branch 'main' into users/padgupta/add_async_socket_processing
msft-paddy14 Dec 19, 2024
355a705
merge main
msft-paddy14 Dec 19, 2024
3315f67
use new NetworkRecive method for benchmark
msft-paddy14 Dec 19, 2024
a2b8cc5
Merge branch 'main' into users/padgupta/add_async_socket_processing
vazois Dec 19, 2024
f8f7c14
Adjust for async mdoe
msft-paddy14 Dec 19, 2024
cad1a4e
Change method names for clairty
msft-paddy14 Dec 19, 2024
d8650b9
update slowconsume to suync
msft-paddy14 Dec 20, 2024
5c74474
fix formatting
msft-paddy14 Dec 20, 2024
ff53cd4
Merge branch 'users/padgupta/add_async_socket_processing' of https://…
msft-paddy14 Dec 20, 2024
6d6dc00
remove tls cert copy
msft-paddy14 Dec 20, 2024
9973f9e
fix formatting
msft-paddy14 Dec 20, 2024
83b695f
remove whitespace
msft-paddy14 Dec 20, 2024
6311d97
Merge branch 'main' into users/padgupta/add_async_socket_processing
badrishc Dec 20, 2024
1162747
revert names of tests
badrishc Dec 20, 2024
073488f
resolve metge conflicts
msft-paddy14 Dec 21, 2024
5a3700d
bump version and bdn fixes
msft-paddy14 Dec 21, 2024
da8599c
formatting fixes
msft-paddy14 Dec 21, 2024
7b2d699
Merge branch 'users/padgupta/add_async_socket_processing' of https://…
msft-paddy14 Dec 21, 2024
21f7a86
revert unsafe class changes
msft-paddy14 Dec 21, 2024
e1d2f78
fix comments
badrishc Dec 21, 2024
5b2accf
Merge branch 'main' into users/padgupta/add_async_socket_processing
badrishc Dec 22, 2024
2488fc8
Merge branch 'main' of https://github.com/microsoft/garnet into users…
msft-paddy14 Dec 23, 2024
d09be21
Merge branch 'users/padgupta/add_async_socket_processing' of https://…
msft-paddy14 Dec 23, 2024
363bfb9
segregate TLS and non TLS paths
msft-paddy14 Dec 23, 2024
81503e8
separate based on TLS and no-TLS
badrishc Dec 28, 2024
816bb59
nit
badrishc Dec 28, 2024
ca7fedd
fix bdn
badrishc Dec 28, 2024
16ea215
Merge branch 'main' into users/padgupta/add_async_socket_processing
badrishc Dec 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions libs/common/Networking/NetworkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ async Task AuthenticateAsClientAsync(SslClientAuthenticationOptions sslClientOpt
/// On network receive
/// </summary>
/// <param name="bytesTransferred">Number of bytes transferred</param>
public unsafe void OnNetworkReceive(int bytesTransferred)
public async Task OnNetworkReceiveAsync(int bytesTransferred)
msft-paddy14 marked this conversation as resolved.
Show resolved Hide resolved
{
// Wait for SslStream async processing to complete, if any (e.g., authentication phase)
while (readerStatus == TlsReaderStatus.Active)
expectingData.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult();
await expectingData.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);

// Increment network bytes read
networkBytesRead += bytesTransferred;
Expand All @@ -287,7 +287,10 @@ public unsafe void OnNetworkReceive(int bytesTransferred)
if (sslStream == null)
{
transportReceiveBuffer = networkReceiveBuffer;
transportReceiveBufferPtr = networkReceiveBufferPtr;
unsafe
{
transportReceiveBufferPtr = networkReceiveBufferPtr;
}
transportBytesRead = networkBytesRead;

// We do not have an active read task, so we will process on the network thread
Expand All @@ -298,7 +301,7 @@ public unsafe void OnNetworkReceive(int bytesTransferred)
readerStatus = TlsReaderStatus.Active;
Read();
while (readerStatus == TlsReaderStatus.Active)
expectingData.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult();
await expectingData.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
}
break;
case TlsReaderStatus.Waiting:
Expand All @@ -309,7 +312,7 @@ public unsafe void OnNetworkReceive(int bytesTransferred)
_ = receivedData.Release();

while (readerStatus == TlsReaderStatus.Active)
expectingData.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult();
await expectingData.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
break;
default:
ThrowInvalidOperationException($"Unexpected reader status {readerStatus}");
Expand Down
53 changes: 40 additions & 13 deletions libs/common/Networking/TcpNetworkHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,58 @@ void Dispose(SocketAsyncEventArgs e)
}

void RecvEventArg_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success || serverHook.Disposed)
{
// No more things to receive
Dispose(e);
return;
}
var networkBufferProcessingTask = OnNetworkReceiveAsync(e.BytesTransferred);
if (networkBufferProcessingTask.IsCompletedSuccessfully)
msft-paddy14 marked this conversation as resolved.
Show resolved Hide resolved
{
NetworkBufferProcessed(networkBufferProcessingTask, sender, e);
return;
}
networkBufferProcessingTask.ContinueWith((res) =>
badrishc marked this conversation as resolved.
Show resolved Hide resolved
{
NetworkBufferProcessed(res, sender, e);
});
}

void NetworkBufferProcessed(Task result, object sender, SocketAsyncEventArgs e)
msft-paddy14 marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
do
if (result.IsCompletedSuccessfully)
{
if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success || serverHook.Disposed)
e.SetBuffer(networkReceiveBuffer, networkBytesRead, networkReceiveBuffer.Length - networkBytesRead);
if (!e.AcceptSocket.ReceiveAsync(e))
{
// No more things to receive
Dispose(e);
break;
RecvEventArg_Completed(sender, e);
}
OnNetworkReceive(e.BytesTransferred);
e.SetBuffer(networkReceiveBuffer, networkBytesRead, networkReceiveBuffer.Length - networkBytesRead);
} while (!e.AcceptSocket.ReceiveAsync(e));
}
msft-paddy14 marked this conversation as resolved.
Show resolved Hide resolved
else if (result.IsFaulted)
msft-paddy14 marked this conversation as resolved.
Show resolved Hide resolved
{
var ex = result.Exception.InnerException;
HandleReceiveFailure(ex, e);
}
}
catch (Exception ex)
{
if (ex is ObjectDisposedException ex2 && ex2.ObjectName == "System.Net.Sockets.Socket")
logger?.LogTrace("Accept socket was disposed at RecvEventArg_Completed");
else
logger?.LogError(ex, "An error occurred at RecvEventArg_Completed");
Dispose(e);
HandleReceiveFailure(ex, e);
}
}

void HandleReceiveFailure(Exception ex, SocketAsyncEventArgs e)
{
if (ex is ObjectDisposedException ex2 && ex2.ObjectName == "System.Net.Sockets.Socket")
logger?.LogTrace("Accept socket was disposed at RecvEventArg_Completed");
else
logger?.LogError(ex, "An error occurred at RecvEventArg_Completed");
Dispose(e);
}

unsafe void AllocateNetworkReceiveBuffer()
{
networkReceiveBufferEntry = networkPool.Get(networkBufferSettings.initialReceiveBufferSize);
Expand Down
Loading