Skip to content

Rework request cancellation - #846

Merged
arthurschreiber merged 7 commits into
masterfrom
arthur/rework-request-cancellation
Jan 16, 2019
Merged

arthurschreiber merged 7 commits into
masterfrom
arthur/rework-request-cancellation

Conversation

@arthurschreiber

@arthurschreiber arthurschreiber commented Jan 14, 2019 •

Copy link
Copy Markdown
Collaborator

This is an attempt to rework how request cancellation works, to make it more generic (it now works with regular requests and both streaming as well as non-streaming bulk loads) and more conformant to the MS-TDS spec.

This also adds a new .cancel method on the Request and BulkLoad classes, and deprecates the cancel method on the Connection class itself.

The changes in 8c9ce1d warrant a major version bump, as they change the behaviour of requests that are canceled after they were sent to the server. Instead of continuing to forward various events from the token stream parser to the canceled request, all this information will be "discarded", as described in 3.2.5.8 Sent Attention State of the MS-TDS specification:

If the response is structurally valid and it does not acknowledge the Attention as described in section 2.2.1.7, then the TDS client MUST discard any data contained in the response and remain in the "Sent Attention" state.

If the response is structurally valid and it acknowledges the Attention as described in section 2.2.1.7, then the TDS client MUST discard any data contained in the response, indicate the completion of the query to the upper layer together with the cause of the Attention (either an upper-layer cancellation as described in section 3.2.4 or query timeout as described in section 3.2.2), and enter the "Logged In" state.

Fixes: #545
Fixes: #534
Fixes: #566

Comment thread src/connection.js Resolved
Comment thread src/connection.js Outdated
@@ -1672,13 +1718,8 @@ class Connection extends EventEmitter {
const message = 'Requests can only be canceled in the ' + this.STATE.SENT_CLIENT_REQUEST.name + ' state, not the ' + this.state.name + ' state';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When cancel() is called after a timeout, a debug message is still displayed, see #545.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I want to deprecate .cancel on Connection, and thus did not touch this function more than necessary. We could change this function to:

if (!this.request) {
  return false;
}

if (this.request.canceled) {
  return false;
}

this.request.cancel();
return true;

And still deprecate (and later remove) this function? 🤔

Comment thread src/connection.js Resolved
@chdh

chdh commented Jan 14, 2019 •

Copy link
Copy Markdown
Collaborator

I have made some comments, but the fundamental problem is that the program logic in connection.js is much too complicated to really comprehend it. There is such a jungle of events, callbacks, flags, states, etc.

The TDS protocol state machine should be encapsulated in a separate module and isolated from the rest. There should be clear documented APIs for every module. Private and public variables should be distinguished, etc.

@arthurschreiber
arthurschreiber force-pushed the arthur/rework-request-cancellation branch 2 times, most recently from 0597d57 to 2f61cca Compare January 15, 2019 07:50
According to 3.2.5.8 of the MS-TDS specification, all data received for 
a request after sending the attention message MUST be discarded.

BREAKING CHANGE: Canceled requests will no longer receive any data.
This will have the side effect that any data that is received after the 
timeout was triggered will be discarded. This mirrors the way 
user-initiated request cancellation behaves.

BREAKING CHANGE: Timed out requests will no longer receive any data.
@arthurschreiber
arthurschreiber force-pushed the arthur/rework-request-cancellation branch from 2f61cca to 52967ee Compare January 15, 2019 07:56
`BulkRequests` can now be canceled, both when in streaming as well as
non-streaming mode.

In case an error happens in the internal row transformation stream on a
streaming `BulkLoad`, the in-flight request will be canceled and the
error will be propagated to the request callback.
@arthurschreiber
arthurschreiber force-pushed the arthur/rework-request-cancellation branch from 52967ee to c54f1fe Compare January 15, 2019 16:08
This adds a `.cancel` method to both `Request` as well as `BulkLoad`,
which can be called before or during request execution.
This adds the missing cancel timer, as described by the MS-TDS
specification. It controls the maximum time spent waiting for a query
cancellation. In case the timer fires, the connection will be closed,
and the current request's callback will be called with an error.
@arthurschreiber
arthurschreiber force-pushed the arthur/rework-request-cancellation branch from c54f1fe to 74682a4 Compare January 15, 2019 18:10
@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

I have made some comments, but the fundamental problem is that the program logic in connection.js is much too complicated to really comprehend it. There is such a jungle of events, callbacks, flags, states, etc.

The TDS protocol state machine should be encapsulated in a separate module and isolated from the rest. There should be clear documented APIs for every module. Private and public variables should be distinguished, etc.

Yeah, you're totally right. 😞 The way the state machine is currently translated to code is... not quite optimal. I'm trying to move the code in a better direction, through a small set of changes that I have in mind. The first set of changes is over #847. It's nothing revolutionary, but I feel like doing small, iterative changes is going to yield results as soon as possible and will help us move in the right direction quicker.

@arthurschreiber
arthurschreiber merged commit 96e7f9b into master Jan 16, 2019
@arthurschreiber
arthurschreiber deleted the arthur/rework-request-cancellation branch January 16, 2019 09:01
@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

🎉 This PR is included in version 5.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

2 participants