Skip to content

Enable pipe to support back pressure and errors - #1078

Merged
dhensby merged 9 commits into
tediousjs:masterfrom
jeffbski-rga:pipe-supporting-back-pressure
Nov 19, 2020
Merged

dhensby merged 9 commits into
tediousjs:masterfrom
jeffbski-rga:pipe-supporting-back-pressure

Conversation

@jeffbski-rga

Copy link
Copy Markdown
Contributor

pipe method should support Node.js stream back pressure and forward errors to the stream.

What this does:

Modify pipe method to wrap the original request in a Readable stream and pipe it to the incoming stream. Errors are forwarded to the stream as well.

@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

I see the tests are failing since you are running build with MS SQL Server 2008 which doesn't support offset and fetch options. Uggh. Why keeping with something so old (12 years)?

I'll work on changing the tests so it doesn't need it. I only used that feature to take advantage of an existing set of data and only doing a partial load since I didn't know how fast the machine and network would be.

@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

Maybe I can simply switch it to using select top 2000 * from ... I'll make that change and see if it works for the build.

@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

Oh, I see it actually runs with different versions in each test. Interesting.

@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

Weird results in the tests now. node@10 tests pass for both x86 and x64.
node@12 x64 passes, but x86 got an install error eaddr (network failure?)

node 6 & 8 are all failing in the msnodesqlv8 tests with Uncaught TypeError: this._currentRequest.resumeQuery is not a function.

I guess I'll need some help from the maintainers to understand what is going on here. I don't know why resumeQuery would exist for some versions but not all? Are these older msnodesqlv8 objects not having the same interface?

@dhensby

dhensby commented Aug 8, 2020

Copy link
Copy Markdown
Collaborator

Hi @jeffbski-rga thanks so much for the effort on this including the tests - I've been a bit busy the past few days but will endeavour to have a look at this over the weekend.

@dhensby
dhensby changed the base branch from 6 to master August 8, 2020 12:22
@dhensby
dhensby force-pushed the pipe-supporting-back-pressure branch from 87d020c to fa40b9e Compare August 8, 2020 12:23

@dhensby dhensby left a comment

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.

I've retargeted this PR at the master branch because this is a breaking change so can't go into the v6 release line; it will have to form part of v7.

I'll have a go and getting the test suite to pass

Comment thread lib/base/request.js Resolved
Comment thread lib/base/request.js Resolved
Comment thread test/tedious/tedious.js Resolved
@dhensby dhensby closed this Aug 8, 2020
@dhensby dhensby reopened this Aug 8, 2020
@dhensby
dhensby force-pushed the pipe-supporting-back-pressure branch from 39d3065 to d7ad689 Compare August 8, 2020 15:33
@dhensby

dhensby commented Aug 8, 2020

Copy link
Copy Markdown
Collaborator

I'm going to have to close this and open a new PR because AppVeyor can't cope with the branch being changed

@dhensby dhensby closed this Aug 8, 2020
@dhensby dhensby reopened this Aug 8, 2020
@dhensby

dhensby commented Aug 8, 2020

Copy link
Copy Markdown
Collaborator

Ah - no, I think I just haven't removed v6 and v8 tests on master yet...

@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

In my latest commit, I have added a toReadableStream method which just returns the ReadableStream.

@potatozerg

Copy link
Copy Markdown

@dhensby Hi, is there any update? Thanks!

@jeffbski-rga

jeffbski-rga commented Sep 8, 2020 •

Copy link
Copy Markdown
Contributor Author

@potatozerg it seemed to fall off their radar. I hope they will resume getting it in. In the meantime, I needed it for my project so I have published it to npm as @jeffbski-rga/[email protected] from this branch on my repo https://github.com/jeffbski-rga/node-mssql/tree/npm

npm i @jeffbski-rga/[email protected]

It seems to be working great. I am primarily using the toReadableStream method I added but they (toReadableStream and pipe) both share the same code so either should work well.

@potatozerg

Copy link
Copy Markdown

@jeffbski-rga Thanks a lot! I will give it a try when I finish my current task.

@potatozerg

Copy link
Copy Markdown

@jeffbski-rga How should I require(import) it? importing mssql doesn't work anymore. I uninstalled the mssql package and installed yours. Thanks.

@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

@potatozerg sorry didn't see the message till now

It's just a scoped package so you use the scope in front of the name like so.

import SQL from '@jeffbski-rga/mssql';

@potatozerg

Copy link
Copy Markdown

@jeffbski-rga Hi Jeff, I have tried it, but it didn't work. I can provide more info once I get to my office. Basically I have tried all kinds of importing formats that might work, but it keeps popping errors.

@potatozerg

Copy link
Copy Markdown

@jeffbski-rga It actually works! Thanks a lot. But do you know why when I use the toReadableStream() function it just makes my Nodejs backend extremely slow? The original package(mssql) has the same problem too.

@jeffbski-rga

jeffbski-rga commented Sep 17, 2020 •

Copy link
Copy Markdown
Contributor Author

@potatozerg I haven't noticed any problems, seems to run pretty well. I'm using from a lambda. If I had to guess maybe something in your stream processing is not quite right?. I'm using the pump npm library to connect up my readable streams with transform and writable streams since it handles a bunch of edge cases with errors and all. If you want to post a gist URL, I can take a look if there is something obvious. Of course if you have the same problem without using toReadableStream then I don't know, it could still be due to the processing you are doing with the data coming back.

For instance, if the write stream supports the writev method it will be able to be given an arrary of rows at a time to deal with, otherwise you'll be stuck with dealing with one row at a time which depending on what you are doing with the data could be very inefficient. It is an order of magnitude faster using the writev in an oracle writeStream I wrote versus inserting a row at a time.

@potatozerg

Copy link
Copy Markdown

@jeffbski-rga Thanks a lot. I will dig into it when I get the chance. I will keep you updated.

@dhensby dhensby left a comment

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.

OK - I'm happy with this and would like to merge it subject to the changes requested.

Sorry for the delay on this - I've been busy :(

Comment thread README.md Resolved
Comment thread README.md Resolved
Comment thread README.md Resolved
Comment thread README.md Resolved
Comment thread README.md Resolved
@dhensby
dhensby force-pushed the pipe-supporting-back-pressure branch 2 times, most recently from e79e846 to ec19f59 Compare September 18, 2020 13:48
@dhensby

dhensby commented Sep 18, 2020

Copy link
Copy Markdown
Collaborator

OK - I've implemented my requested changes and rebased.

Unfortunately the v8 sql driver tests are failing, which is a pain. I'm not sure why either so I'll need to get my windows machine out to try and debug it

@dhensby

dhensby commented Sep 21, 2020

Copy link
Copy Markdown
Collaborator

Well, this is annoying - I've taken a look on my windows machine and I can't pin-point where this error is coming from.

When I step through the execution slowly the test actually completes without node crashing (but it does fail with too few rows).

What I do notice is that the stream pauses and resumes for every single row, which seems excessive and maybe is the cause of the crash as nodemsqlv8 may have some issues around repeated pause/resumes. Clearly this isn't ideal but as the tedious implementation works we can assume this is more a problem with the driver than our code.

I'm tempted to skip the nodemsqlv8 test for this and come back to it later...

@jeffbski-rga

jeffbski-rga commented Sep 21, 2020 •

Copy link
Copy Markdown
Contributor Author

@dhensby Just to be clear my implementation only pauses the stream when the backpressure from the transform and writable streams indicates it is necessary. This comes back as a false during the push as shown below (which matches the node.js stream guidance).

  origRequest.on('row', (row) => {
      if (!readableStream.push(row)) {
        origRequest.pause()
      }
    })

Now maybe the test is causing this to be false quite often, but we are following the spec here.

@dhensby

dhensby commented Sep 22, 2020

Copy link
Copy Markdown
Collaborator

Yes, I don't think the implementation is wrong, I'm just saying what my observation is when running through the debugger.

I think the best approach for now is to skip the test for the failing driver and get this merged.

@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

I added the ability to optionally pass in stream options for the readable stream that is to be created when using toReadableStream. This allows one to increase the amount of rows that are buffered by passing in options like {highWaterMark: 123}

const readableStream = request.toReadableStream({ highWaterMark: 100 })

@dhensby

dhensby commented Sep 25, 2020

Copy link
Copy Markdown
Collaborator

Thanks @jeffbski-rga - I don't want this to sit around any longer so I'm going to skip the msnodesqlv8 tests for now.

@TimelordUK do you have any idea what is going on here? Why the tests on the msnodesqlv8 driver might be dying?

@dhensby
dhensby force-pushed the pipe-supporting-back-pressure branch from b18c275 to 5479c08 Compare September 25, 2020 15:15
jeffbski-rga and others added 8 commits September 25, 2020 16:16
pipe method should support Node.js stream back pressure so we wrap the original request in a Readable stream and pipe to incoming stream. Errors are forwarded to the stream.
Since the build server is still running SQL Server 2008, it does not support offset and fetch for limiting the query.

Apparently it does support `select top 2000 * from ...` so I can use that instead.
Wrap a request object in a Node.js Readable stream which supports back pressure.

```javascript
const { pipeline } = require('stream')
const request = new sql.Request()
const readableStream = request.toReadableStream()
pipeline(readableStream, transformStream, writableStream)
request.query('select * from mytable')
```
Allow options to be passed into toReadableStream that will be used when creating the stream.

For instance to increase the highWaterMark, you could:

```js
const { pipeline } = require('stream')
const request = new sql.Request()
const readableStream = request.toReadableStream({highWaterMark: 100})
pipeline(readableStream, transformStream, writableStream)
request.query('select * from mytable')
```
@dhensby
dhensby force-pushed the pipe-supporting-back-pressure branch from 5479c08 to d4081b7 Compare September 25, 2020 15:16
@dhensby

dhensby commented Sep 25, 2020

Copy link
Copy Markdown
Collaborator

I've rebased this as we have just merged in a fix for msnodesqlv8 streams (see #1109) which may be playing a role in these failures... 🤞

@dhensby

dhensby commented Sep 25, 2020

Copy link
Copy Markdown
Collaborator

OK - so installing v1 of the driver gets the tests green (but I note some unhandled Promise rejections from some of the tests - presumably from the tear down after the test).

What this does mean is that the library only currently supports v1 of msnodesqlv8, so we should make that clearer in the docs (or, better, we should upgrade master to work with v2).

@dhensby
dhensby force-pushed the pipe-supporting-back-pressure branch 2 times, most recently from c55f066 to 97e7799 Compare September 25, 2020 16:41
@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

If there is anything I could do to help let me know.

@dhensby

dhensby commented Nov 19, 2020

Copy link
Copy Markdown
Collaborator

Sorry for the delay @jeffbski-rga and thanks for the patience - work has been all over the place meaning my time on this project has taken a back-seat.

So I think we should merge this as is and then try to find time to get v2 support in for msnodesqlv8

@dhensby
dhensby merged commit b852916 into tediousjs:master Nov 19, 2020
@TimelordUK

Copy link
Copy Markdown
Contributor

V2 msnodesqlV8 was a significant rework mostly in cpp to allow Linux support. We can now run on many Linux platforms 64 bit as well as windows.

Very little in way of our testing had to be changed so I am not sure why it’s broken your side. I can help take a look when you have time there should be little api difference but driver itself needed quite a few changes to compile and work on both targets.

@dhensby

dhensby commented Nov 19, 2020

Copy link
Copy Markdown
Collaborator

@TimelordUK yeah - it looks like it's almost entirely compatible, but we are seeing a failure on v2 that we don't get on v1 (I think) - I need to double check all this, I'll open a PR soon

@jeffbski-rga

Copy link
Copy Markdown
Contributor Author

Thanks @dhensby and team for getting this in. I will be able to happily retire my fork once it is deployed to npm.

@jeffbski-rga
jeffbski-rga deleted the pipe-supporting-back-pressure branch November 20, 2020 16:09
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.

4 participants