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

[QUIC] Rewrite Open(Uni|Bidi)rectionalStream to leverage async stream opening #67302

Closed
CarnaViire opened this issue Mar 29, 2022 · 3 comments · Fixed by #67859
Closed

[QUIC] Rewrite Open(Uni|Bidi)rectionalStream to leverage async stream opening #67302

CarnaViire opened this issue Mar 29, 2022 · 3 comments · Fixed by #67859
Assignees
Labels
area-System.Net.Quic bug disabled-test The test is disabled in source code against the issue tenet-performance Performance related issue
Milestone

Comments

@CarnaViire
Copy link
Member

Our current API for opening a QuicStream is synchronous and will throw if stream limit is reached. Waiting for available streams was intended to be a loop of checking for available stream count:

while (true)
{
lock (SyncObj)
{
if (_connection == null)
{
break;
}
if (_connection.GetRemoteAvailableBidirectionalStreamCount() > 0)
{
quicStream = _connection.OpenBidirectionalStream();
requestStream = new Http3RequestStream(request, this, quicStream);
_activeRequests.Add(quicStream, requestStream);
break;
}
waitTask = _connection.WaitForAvailableBidirectionalStreamsAsync(cancellationToken);

This proved to be highly inefficient - adding this loop around the opening eats up 80% of perf even when there's no actual need to wait.

MsQuic has it's own waiting logic implemented, and opening a stream can be asynchronous. As soon as stream is available, START_COMPLETE event will come to the stream. This can be done by passing ASYNC flag instead of FAIL_BLOCKED to StreamStart.

We need to:

  • Change Open(Uni|Bidi)rectionalStream to Open(Uni|Bidi)rectionalStreamAsync, that will create the stream and then wait for the stream to be started
  • We would need additional API on QuicStream; most possibly add "StartAsync" that will contain StreamStart call moved from .ctor and then wait on TCS for the event
  • Remove wait for available streams loop from H3 (the code linked above)

Some thoughts:

  • If cancellation occurs, if stream is already created but not started, it needs to be closed and disposed.
  • Need to consider all places where TCS on opening a stream should be set to canceled (connection close, dispose etc)
  • How dispose should work if stream is not started? Consider race conditions
  • Do we need state checks (throwing on Send if stream is not started yet? etc)

Note: since msquic 2.0 I believe there will be no ASYNC flag anymore because async behavior is the default. (NONE flag)
Note 2: this is only for outbound streams. Inbound streams are already started when accepted.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net.Quic untriaged New issue has not been triaged by the area owner labels Mar 29, 2022
@ghost
Copy link

ghost commented Mar 29, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Our current API for opening a QuicStream is synchronous and will throw if stream limit is reached. Waiting for available streams was intended to be a loop of checking for available stream count:

while (true)
{
lock (SyncObj)
{
if (_connection == null)
{
break;
}
if (_connection.GetRemoteAvailableBidirectionalStreamCount() > 0)
{
quicStream = _connection.OpenBidirectionalStream();
requestStream = new Http3RequestStream(request, this, quicStream);
_activeRequests.Add(quicStream, requestStream);
break;
}
waitTask = _connection.WaitForAvailableBidirectionalStreamsAsync(cancellationToken);

This proved to be highly inefficient - adding this loop around the opening eats up 80% of perf even when there's no actual need to wait.

MsQuic has it's own waiting logic implemented, and opening a stream can be asynchronous. As soon as stream is available, START_COMPLETE event will come to the stream. This can be done by passing ASYNC flag instead of FAIL_BLOCKED to StreamStart.

We need to:

  • Change Open(Uni|Bidi)rectionalStream to Open(Uni|Bidi)rectionalStreamAsync, that will create the stream and then wait for the stream to be started
  • We would need additional API on QuicStream; most possibly add "StartAsync" that will contain StreamStart call moved from .ctor and then wait on TCS for the event
  • Remove wait for available streams loop from H3 (the code linked above)

Some thoughts:

  • If cancellation occurs, if stream is already created but not started, it needs to be closed and disposed.
  • Need to consider all places where TCS on opening a stream should be set to canceled (connection close, dispose etc)
  • How dispose should work if stream is not started? Consider race conditions
  • Do we need state checks (throwing on Send if stream is not started yet? etc)

Note: since msquic 2.0 I believe there will be no ASYNC flag anymore because async behavior is the default. (NONE flag)
Note 2: this is only for outbound streams. Inbound streams are already started when accepted.

Author: CarnaViire
Assignees: -
Labels:

untriaged, area-System.Net.Quic

Milestone: -

@CarnaViire CarnaViire added this to the 7.0.0 milestone Mar 29, 2022
@CarnaViire CarnaViire removed the untriaged New issue has not been triaged by the area owner label Mar 29, 2022
@ManickaP
Copy link
Member

I'm disabling 2 tests against this issue, because Open...directionalStream doesn't throw at the moment (the result gets returned in the callback).

@ManickaP ManickaP added bug disabled-test The test is disabled in source code against the issue tenet-performance Performance related issue labels Mar 31, 2022
@rzikm rzikm self-assigned this Apr 8, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 11, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Apr 27, 2022
@CarnaViire
Copy link
Member Author

@JamesNK @Tratcher

We've just merged an API change for quic stream opening ( Open*directionalStream -> await Open*directionalStreamAsync )
I don't know if you've heard about the change before, if not, sorry for not letting you know sooner.

I believe you would need changes in opening control streams in Kestrel (I guess here https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Transport.Quic/src/Internal/QuicConnectionContext.cs#L192-L207). The change should be super easy, but let us know if you need our help

@ghost ghost locked as resolved and limited conversation to collaborators May 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Quic bug disabled-test The test is disabled in source code against the issue tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants