Skip to content

First implementation of frontend timeouts for async methods - #2911

Merged
roji merged 35 commits into
npgsql:devfrom
vonzshik:async-frontend-timeout
Aug 15, 2020
Merged

roji merged 35 commits into
npgsql:devfrom
vonzshik:async-frontend-timeout

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

Took a stab at #607.

While it's relatively simple fix (on the first glance), I may have missed quite a few corner cases.
What I'm definitely not sure is an exception I throw (took inspiration from NpgsqlConnector.ConnectAsync).
Also, maybe timeout exceptions for sync and async Read should be the same?

@wes-novack

wes-novack commented Jun 22, 2020 •

Copy link
Copy Markdown

Thank you @vonzshik !

@roji will this PR or a similar proposed fix be evaluated sometime soon? My team is also feeling the pain of #607

@roji

roji commented Jun 23, 2020

Copy link
Copy Markdown
Member

@vonzshik @wes-novack apologies for not reviewing this earlier, am under a lot of load at the moment. I'll do my best to set aside some time soon for this.

@zlintz

zlintz commented Jun 24, 2020

Copy link
Copy Markdown

@roji Thank you for the update, just wanted to also express the desire for this and gratitude for the time you spend on this. Looking forward to seeing what happens here

@vonzshik

Copy link
Copy Markdown
Contributor Author

@roji Thank you 😃

@YohDeadfall YohDeadfall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please rebase on the top of dev branch and resolve a conflict.

Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
@YohDeadfall YohDeadfall linked an issue Jun 24, 2020 that may be closed by this pull request
@vonzshik
vonzshik force-pushed the async-frontend-timeout branch from 9c61833 to 9f77ec5 Compare June 24, 2020 10:47
@vonzshik
vonzshik requested a review from roji as a code owner June 24, 2020 10:47
@vonzshik

vonzshik commented Jun 24, 2020 •

Copy link
Copy Markdown
Contributor Author

Looked at failing test for Issue2257, and found at least 2 problems:

  1. Timeouts while creating table. As we are now respecting CommandTimeout for async queries, fix should be simple - just add timeout when creating command, like:
using (var cmd = conn.CreateCommand())
{
   cmd.CommandTimeout = 60;
   //....
}
  1. NpgsqlConnection.BeginBinaryImport does not respect CommandTimeout from connection. After some digging, I found that after setting NpgsqlCommand.CommandTimeout and executing any query, it changes UserTimeout in NpgsqlConnector and does not set it back. As NpgsqlConnection.BeginBinaryImport does not use NpgsqlCommand nor changes UserTimeout itself, test is failing again (as we are expecting timeout exception). Ideas?

Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

High-level notes

  • There are many direct calls into NpgsqlReadBuffer.Ensure that don't pass through NpgsqlConnector.ReadMessage (see all the type handlers). These should also get the timeout.
  • To fix the above and to get a cleaner implementation all-around, it should be possible to implement the entire mechanism inside NpgsqlReadBuffer.Ensure, rather than at the upper layer in NpgsqlConnector.ReadMessage. NpgsqlReadBuffer could have a Timeout property set as appropriate, and when Ensure is called, that timeout would be applied appropriately for either sync (by setting Socket.ReceiveTimeout if needed), or async (via the cancellation token).
  • We should reuse the cancellation token source and avoid re-allocating one for each async operation (as long as it hasn't fired). You can see an example of this reuse in the multiplexing write loop.
  • Timeouts are currently applied only on reading, not on writing - that should also be done (should be a very similar implementation)

Some general related notes

  • Just above in NpgsqlConnector.ReadMessageLong, we read prepended responses (these are PG responses for messages unrelated to this query which were prepended to it for efficiency). We have a feature where we allow a different timeout to be defined for those (InternalCommandTimeout instead of UserTimeout), but it seems to be broken: if there are prepended responses, we set ReceiveTimeout to InternalcommandTimeout, but then recursively call ReadMessageLong which will overwrite it with UserTimeout. Opened #3045 to look at that at some point (no need to do it now).
  • The timeout is implemented for each read operation, not to the operation as a whole. That's OK, because this is also how sync frontend timeouts are implemented (see #1041 which is about possibly changing this).
  • The user-provided cancellation token currently triggers server-side cancellation (dotnet/runtime#28596 (comment)), so there's no conflict/interference with implementing the socket timeout via an internal cancellation token as is done in this PR. But we may have to revisit this at some point.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
@vonzshik

Copy link
Copy Markdown
Contributor Author

Thank you for review, @roji

I just pushed a few commits about moving timeout to NpgsqlReadBuffer, could you take a look? I'm still not satisfied with how it looks, so any critique is appreciated.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @vonzshik, this is getting close to mergeable - please take a look at the comments, and also at the remaining comments from the last review. BTW when a review comment has been addressed, it's good to mark it as "resolved" to let us know and help the next review.

Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
@vonzshik

vonzshik commented Jul 2, 2020

Copy link
Copy Markdown
Contributor Author

@roji Thank you again for review. Just pushed commits concerning every requested change, so feel free to review when you have time.

BTW when a review comment has been addressed, it's good to mark it as "resolved" to let us know and help the next review.

I'll keep than in mind.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See also test failures

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved

@Brar Brar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks pretty good to me.
I've got a few minor comments and suggestions but don't see any real problems with this PR.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved
Comment thread test/Npgsql.Tests/CommandTests.cs Resolved
Comment thread test/Npgsql.Tests/CommandTests.cs Resolved
Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved
@Brar

Brar commented Aug 15, 2020

Copy link
Copy Markdown
Member

@roji this really looks ready to me now.
Do you think you can give this a quick look in the next days?
Otherwise, if you don't object, I'll merge it as it is to unblock the replication work I'm planning to do on top of this.

@YohDeadfall YohDeadfall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only think I don't like is the inheritance from IDisposable, but it's not a hill I want to die on.

What I would like to see is this changes in our main branch, so can replicate the logic in new type handling am working on.

@roji

roji commented Aug 15, 2020

Copy link
Copy Markdown
Member

Sorry for taking so long with this - I promise I'll give this a review today or tomorrow for sure. It was already pretty close to good last time I looked, so feel free to base your replication work off of this if you want, in the meantime.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Aside from my minor comments below, this looks ready for merge.

Thanks for your hard work on this @vonzshik and sorry it took so long to get to this point!

Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved

void BeginSend(NpgsqlConnector connector)
{
connector.WriteBuffer.Timeout = TimeSpan.FromSeconds(CommandTimeout);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's worth mentioning that Npgsql didn't previously set a sending timeout (sync or async). It's definitely a good thing to have, but is beyond the scope of purely adding async timeouts.

On an unrelated note, our read/write timeouts currently leak from non-multiplexed to multiplexed operations. This is unrelated to this PR, I've opened #3110 to track.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Outdated
/// <summary>
/// Contains the current value of the Socket's RecieveTimeout, used to determine whether
/// we need to change it when commands are received.
/// It's used only for sync IO (<see cref="Stream.Read(byte[], int, int)"/>)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment doesn't seem right - isn't _currentSocketTimeout is used for async timeouts in Ensure (via the Timeout property?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it'not entirely correct. Rewrote it + renamed _currentSocketTimeout to _currentTimeout.

}

// Resetting cancellation token source, so we can use it again
if (async)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It generally feels like this unified sync/async method branches a lot on async (i.e. lots of stuff specific only to one path but not the other). This may be an indication that it should be refactored into two separate methods (which possibly use some common logic in a separate inlined method).

But let's not hold this PR up on that, we can look at refactoring later.

Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved
Comment thread test/Npgsql.Tests/CommandTests.cs Resolved
@vonzshik

Copy link
Copy Markdown
Contributor Author

Thank you for review @roji, it was fun making this pr 😃 .
Also, thanks to @YohDeadfall and @Brar for great reviews.
Btw, I would like to contribute to this project from time to time, could you please give me a few pointers on how should I proceed?

@roji
roji merged commit 801638e into npgsql:dev Aug 15, 2020
@roji

roji commented Aug 15, 2020

Copy link
Copy Markdown
Member

Thanks again @vonzshik, it's great to see this functionality making it into the next version.

It would be great to have more contributions from you! There's nothing in particular to know, you can look through the issues and see something you find interesting. It's definitely better if you can find something that has the 5.0 milestone on it, though if you really want to work on a backlog item that's OK too. It's usually a good idea to post a comment before starting to work, so that we don't end up working on the same thing, and also so that you pick something that isn't in conflict with ongoing work (or just too complicated). Basically communication is the key :)

@vonzshik
vonzshik deleted the async-frontend-timeout branch August 17, 2020 17:11
@freerider7777

Copy link
Copy Markdown

Did timeouts for async get to some version?

@vonzshik

Copy link
Copy Markdown
Contributor Author

@freerider7777 yep, 5.0

@freerider7777

Copy link
Copy Markdown

Thanks, seems sync version also works much better now - I've tested 4.1.8 (lock the table and simple select from EF Core) - when timeout happens, the query continues to run on Postgres (eating resources!!! db admins don't like this), updating to 5.0.3 - I see the query terminates after timeout! Cool.

@freerider7777

Copy link
Copy Markdown

async methods though do not terminate... Maybe that's because we use .Net core 2.2 for now with old version Npgsql.EntityFrameworkCore.PostgreSQL 2.2.4 + NpgSql
But I've updated the version of NpgSql to 5.0.3 - everything seems to work, sync methods now work as expected

@vonzshik

Copy link
Copy Markdown
Contributor Author

@freerider7777 yeah, I believe, cancellation for the async methods of the NetworkStream was added with .net core 3.0 (or 3.1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frontend timeouts for async methods

7 participants