Skip to content

Make timeouts aggregated #1041

Closed
Closed

Description

@roji

Following discussion in #689 (comment), it seems to make sense to make our CommandTimeout behave lie SqlClient's (see docs).

This means that all socket read time is aggregated for a single ADO.NET API call (e.g. Read()). Currently we simply set the socket's ReceiveTimeout to the CommandTimeout, which means that if multiple socket reads happen within the same Read(), the timeout is reset each time. It seems to make sense to aggregate the CommandTimeout across all of Read()'s network reads instead.

Comments from anyone?

Activity

  1. self-assigned this
    on Apr 22, 2016
  2. added this to the milestone on Apr 22, 2016
  3. roji commented on Apr 28, 2016

    @roji
    MemberAuthor

    Note: check the situation with the connection timeout as well, are we doing it right?

  4. modified the milestones: 3.2, on May 5, 2016
  5. modified the milestone: 3.2 on Sep 24, 2016
  6. realityexists commented on Oct 31, 2016

    @realityexists

    Should CommandTimeout be the time per read or the time for all reads? I can see a few scenarios:

    1. The user just wants an overall timeout for reading all results. Obviously the SqlClient behaviour is simpler here, but the user could easily implement an overall timeout themselves - just start a Stopwatch and check the time elapsed after each Read().
    2. The user want a timeout for the first result, but then is happy to take as long as they need to reading the results. An overall timeout doesn't work for this, so the current Npgsql behaviour is better.
    3. The user wants a timeout for the first result and a different (usually much shorter) timeout for each subsequent result. Current Npgsql behaviour is better for this, too, as long as it doesn't cache the value of CommandTimeout, so user code could change it after ExecuteReader has returned (and possibly even before each Read).

    Compatibility with SqlClient is good, but backwards compatibility with Npgsql 3.1 is better. Combined with the current behaviour being more flexible, I'd say leave it like this.

  7. roji commented on Oct 31, 2016

    @roji
    MemberAuthor

    The user just wants an overall timeout for reading all results. Obviously the SqlClient behaviour is simpler here, but the user could easily implement an overall timeout themselves - just start a Stopwatch and check the time elapsed after each Read().

    If by "reading all results" you mean all result rows of a command, then this isn't what SqlClient does (or what this issue was proposing to do) - SqlClient applies timeouts to a single ADO.NET method invocation (e.g. Read()), and only aggregates I/O time spent performing on that invocation for the purpose of timeouts.

    As you say, if the user wants a client-side timeout for reading all results they can implement it themselves.

    The user want a timeout for the first result, but then is happy to take as long as they need to reading the results. An overall timeout doesn't work for this, so the current Npgsql behaviour is better.

    I definitely don't view this as a very valid/interesting scenario - one of the key points of timeouts is to avoid network issues which would block your program forever. I understand where you're coming from - you're thinking about long-running (or deadlocked) queries rather than network issues, but the latter must be addressed.

    One more point - it's not necessarily correct to assume that PostgreSQL takes a long time upfront processing your query, and then spits results back quickly. AFAIK depending on exactly what you're doing it may return the first result very quickly but take long afterwards.

    The user wants a timeout for the first result and a different (usually much shorter) timeout for each subsequent result. Current Npgsql behaviour is better for this, too, as long as it doesn't cache the value of CommandTimeout, so user code could change it after ExecuteReader has returned (and possibly even before each Read).

    Even if this issue is implemented and we start aggregating I/O time, this would still be possible as aggregation only happens within a single ADO.NET method call (as I said above). So you would be free to play around with CommandTimeout between calls.

    To summarize, this issue actually doesn't change behavior in a very significant way, since it only aggregates within a single method call.

  8. realityexists commented on Oct 31, 2016

    @realityexists

    Ah, OK, ignore the above comment then - I misunderstood how it works.

  9. added this to the Backlog milestone on Jun 8, 2018
  10. baal2000 commented on Nov 25, 2018

    @baal2000
    Contributor

    +1 for a standard, provider-agnostic behavior.

  11. roji commented on Nov 25, 2018

    @roji
    MemberAuthor

    Note that as I wrote in #2239, there's nothing standard about aggregating socket read time - it just happens to be the SqlClient behavior (i.e. not documented on DbCommand.CommandTimeout). I'm not sure what other providers do.

    In addition, it isn't very clear whether it really is the desirable behavior for all scenarios. Both behaviors have their pros and cons.

  12. changed the title [-]Make frontend timeouts aggregated[/-] [+]Make timeouts aggregated[/+] on Oct 1, 2020
  13. 16 remaining items

  14. modified the milestones: Backlog, 5.0.0 on Oct 3, 2020
  15. vonzshik commented on Oct 3, 2020

    @vonzshik
    Contributor

    I personally prefer our current implementation, as it's actually more flexible. If you don't have any problems with the network, you won't see any difference. If you do have them, we've also got you here.

  16. roji commented on Oct 3, 2020

    @roji
    MemberAuthor

    If you don't have any problems with the network, you won't see any difference. If you do have them, we've also got you here.

    You're right that if there are network problems, there's no difference.

    But if there aren't network problems, I do think there's the question of "trickling" queries, where results come in little by little. Does an average user want to cancel such a query after X seconds, even though data is still coming? We have no data here to answer the question, but my gut tells me the average user would not want to cancel it. And if they do, as @Brar pointed out they can always manage a cancellation token themselves for the duration of the operation.

  17. vonzshik commented on Oct 3, 2020

    @vonzshik
    Contributor

    But if there aren't network problems, I do think there's the question of "trickling" queries, where results come in little by little.

    Isn't that the definition of the issues with the network? AFAIK, this isn't PG's behavior.

    You're right that if there are network problems, there's no difference.

    I'm talking about the case, when a query takes too long to complete on a PG side. We will still be stuck, waiting for a response from the PG, and eventually fail with a timeout.

  18. roji commented on Oct 3, 2020

    @roji
    MemberAuthor

    Depending on exactly what the query does, I'm pretty sure it's possible to receive a single row, then wait, then receive another row, then wait, etc. - with perfect network conditions. See Robert Haas's answer here.

    I think this is the only relevant scenario for discussion on whether to aggregate or not.

  19. vonzshik commented on Oct 3, 2020

    @vonzshik
    Contributor

    Depending on exactly what the query does, I'm pretty sure it's possible to receive a single row, then wait, then receive another row, then wait, etc. - with perfect network conditions. See Robert Haas's answer here.

    Wouldn't it happen during DataReader.Read?

  20. roji commented on Oct 3, 2020

    @roji
    MemberAuthor

    Yes, that's a very good point. That probably means that within a single Read (or any other single operation), it's not important whether we aggregate or not. The distinction would have been important had we applied CommandTimeout to the entire command, but we don't do that.

    I'd like to wait for @bgrainger's input on this - am interested if things are somehow different MySQL. But otherwise it seems fine to close this.

  21. bgrainger commented on Oct 3, 2020

    @bgrainger
    Contributor

    CommandTimeout could mean "total time spent reading", or it could mean "time spent reading without receiving any data". My personal feeling/assumption is that most/all users really need the 2nd (see #1041 (comment)), but implementing the second may also be fine (most users are unlikely to care about the difference).

    "the 2nd ... the second may also" is confusing; not sure which one you meant.

    That probably means that within a single Read (or any other single operation), it's not important whether we aggregate or not. The distinction would have been important had we applied CommandTimeout to the entire command, but we don't do that.

    I agree with this.

    It's hard for me to speak in broad generalities since there are so many varieties of MySQL Server (MySQL, MariaDB, Amazon RDS, Aurora, etc.; v5.6, v5.7, v8.0) but what I've observed is that MySQL tends to take a long time to compute the first row in a result set (this is time in ExecuteReader) then constantly streams rows back. There may be multiple calls to Socket.Receive to read the packets that make up the resultset header, but during Read, assuming the rows are small enough, I would expect each one to be retrieved with one call to Receive.

    Unfortunately I don't have any hard data, but I suspect that it generally doesn't matter whether timeouts are aggregated for a single call to ExecuteReader or Read. Perhaps there are pathological cases where Socket.Receive returns just one byte every time, and it's necessary to use aggregated timeouts to prevent extremely long operations. Maybe the SqlClient team chose that approach out of an abundance of caution?

  22. bgrainger commented on Oct 3, 2020

    @bgrainger
    Contributor

    MySqlConnector does implement the aggregate timeout approach but IIRC it's more out of worry that that might be necessary in extreme situations, rather than knowing of particular cases where it's necessary for correctness.

  23. roji commented on Oct 3, 2020

    @roji
    MemberAuthor

    @bgrainger thanks for your input.

    what I've observed is that MySQL tends to take a long time to compute the first row in a result set (this is time in ExecuteReader) then constantly streams rows back.

    On the PG side, we do know that rows may have long delays in between them (#1041 (comment)). However, as @vonzshik correctly pointed out, aggregated timeouts only matter if we think that are going to be delays within data for a single row, which really does seem very unlikely.

    I'm going to close this issue as not needed, until we get some evidence that says otherwise.

  24. removed this from the 5.0.0 milestone on Oct 3, 2020
  25. removed their assignment
    on Oct 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions