Skip to content

Refactor cancellation and timeout one more time :) - #3318

Merged
roji merged 1 commit into
npgsql:mainfrom
roji:CancellationRefactor
Nov 11, 2020
Merged

roji merged 1 commit into
npgsql:mainfrom
roji:CancellationRefactor

Conversation

@roji

@roji roji commented Nov 10, 2020

Copy link
Copy Markdown
Member

OK, I think I finally have something nice working. In a nutshell:

  • There's a single NpgsqlConnector.StartCancellableOperation. You call it, give it the user's token and tell it if you want PG cancellation or not. It returns a disposable which you dispose at the end of the operation. That's it.
  • The token is registered to call PerformUserCancellation, which will either do PG cancellation or go directly to hard cancellation (his same method is called by the sync NpgsqlCommand.Cancel). This also takes care of non-PG cancellation, so no more need to flow the user's cancellation token down the stack. This makes PG and non-PG cancellations work very similarly, obviates having to pass the tokens around all the time, and will simplify ResettableCancellationTokenSource (since it will only ever have one token).
  • It's tempting to think of merging the new cancellable operation mechanism and the existing user action mechanism. But an entire command is a user action, encompassing multiple cancellable operations (e.g. ExecuteReader, NextResult, Read...). Maybe something specific for simple operations can be done, where the user action has the same scope as the cancellable operation.

This a draft - @vonzshik I wanted some early feedback from you. If this is a good direction, here's the stuff still to be done by me:

  • Dispose cancellation registrations asynchronously as appropriate
  • Clean up ResettableCancellationTokenSource now that it doesn't have to deal with a second token
  • Remove cancellation tokens from the type handler hierarchy etc.
  • Finish up replication
  • COPY is messy, but this isn't really related to this PR (to be cleaned up later)

@roji
roji requested a review from vonzshik November 10, 2020 13:43
@roji

roji commented Nov 10, 2020

Copy link
Copy Markdown
Member Author

One thing I just realized, is that this new system will reset cancellation between operations inside a single command execution. So Instead of the previous ResetCancellation which was called in ExecuteReader, and then wasn't called again in NextResult/Read, we now do the equivalent of ResetCancellation on every call.

I'm not sure this has any actual bad consequences - in theory if a PG cancellation is sent during ExecuteReader, we'll forget that fact when we do NextResult and may send it again. But is that really problematic in practice?

This also connects to the notes above on user action. We can have another overload of StartCancellableOperation (or whatever) which is subordinate, i.e. only registers the callback but doesn't otherwise reset the state.

@roji

roji commented Nov 10, 2020

Copy link
Copy Markdown
Member Author

(I also don't dispose the registration returned from StartCancellableOperation in command execution... will clean all that up once I get some first feedback from @vonzshik)

@vonzshik

Copy link
Copy Markdown
Contributor

I'm not sure this has any actual bad consequences - in theory if a PG cancellation is sent during ExecuteReader, we'll forget that fact when we do NextResult and may send it again. But is that really problematic in practice?

You're calling it for every Read. It's very possible to call a read hundreds of times, before we've read the cancellation (because the results are already in the buffer). I do not think DDOSing the PG with hundreds of the cancellation requests is a good thing.

@vonzshik vonzshik 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.

Except that part with the cancellation reset on the reader, I think it's great!

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
@roji

roji commented Nov 10, 2020

Copy link
Copy Markdown
Member Author

Except that part with the cancellation reset on the reader, I think it's great!

Great, thanks @vonzshik! I'll keep hacking at this then, and request another review when it's done.

@YohDeadfall

Copy link
Copy Markdown
Contributor

Don't forget to edit the commit name on squash.

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
@roji

roji commented Nov 11, 2020

Copy link
Copy Markdown
Member Author

@vonzshik @YohDeadfall just pushed a lot of work to this PR - I think it should be close to OK. If looks big because it removes all the cancellation tokens from the type handlers etc., but actual changes are less huge.

  • There's now a concept of a cancellable operation, and a nested cancellable operation. User actions start a cancellable operation, so most code doesn't need to separately set up cancellation - this makes things very easy in the simple case. Command execution (and COPY) start the user action, but then start nested cancellable operations for each call inside.
  • I moved the up-front IsCancellationRequested check into these methods as well, so one less thing to worry about everywhere.
  • All CancellationTokenRegistrations are disposed synchronously for now - the situation isn't ideal but I think it's OK for now. We can continue tracking in Improve connector's cancellation request #3277
  • This doesn't touch writing in any way - we continue to pass the token down the stack as before.

I tried to do more or less self-contained commits, so you may want to review them one by one - but some stuff leaked between the commits (we'll squash everything anyway).

@roji

roji commented Nov 11, 2020

Copy link
Copy Markdown
Member Author

(need to rebase to make the CI errors go away, but I will wait for you to review etc.)

@vonzshik vonzshik 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.

Pretty cool, added some notes

Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
@roji

roji commented Nov 11, 2020

Copy link
Copy Markdown
Member Author

@vonzshik am going to squash and rebase this, so start seeing how the build does in CI

@roji
roji force-pushed the CancellationRefactor branch 5 times, most recently from 71897ee to 013681e Compare November 11, 2020 21:33
* The cancellation is no longer passed down the stack to the I/O layer;
  in all cases cancellation is done by registering a callback on the
  token. This unifies the flow for PG cancellation and non-PG
  cancellation.
* Introduce notion of cancellable operations, which can contain nested
  cancellable operations. User actions implicitly represent a
  cancellable operation, making simple, one-operation cases very easy.
* Make the up-front IsCancellationRequested check when starting a
  cancellable operation. This reduces the ceremony around each and
  every operation.
* Various other refactoring to make the code simpler.

Part of npgsql#3166
@roji
roji force-pushed the CancellationRefactor branch from 013681e to b0e59c9 Compare November 11, 2020 21:40
@roji
roji merged commit 1002913 into npgsql:main Nov 11, 2020
@roji
roji deleted the CancellationRefactor branch November 11, 2020 21:49
var columnStream = new NpgsqlReadBuffer.ColumnStream(buf);

// Cancellation is handled at the replication level - we don't want every ReadAsync
var columnStream = new NpgsqlReadBuffer.ColumnStream(connector, startCancellableOperations: false);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@vonzshik FYI this is the one possible exception to the rule about user actions with tokens not having nested operations... The entire replication is a user action with a cancellation token, but if the user is doing physical replication (not logical), they get a Stream back which they can read from (sequentially); so ReadAsync on that may accept cancellation tokens.

This is the only case I'm aware of in Npgsql where it may make sense to have two linked cancellation tokens when reading. As this is a really really niche edge case, I've simply hacked it to ignore the token passed to the stream - you can only cancel via the token passed when starting replication.

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.

3 participants