Skip to content

fix(abstract/connection-manager): acquire call never finishes - #8330

Merged
sushantdhiman merged 10 commits into
masterfrom
fix-pool-issue
Sep 20, 2017
Merged

sushantdhiman merged 10 commits into
masterfrom
fix-pool-issue

Conversation

@sushantdhiman

@sushantdhiman sushantdhiman commented Sep 18, 2017 •

Copy link
Copy Markdown
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

This is basically a hack but pooling library has given us no options. Pool acquire call
now resolves even if there is an error. We simply check if its an error instance or not
and reject accordingly.

Read more here #7884 (comment)

_determineConnection method can be used to check if thing returned by pooling library is
actaully a connection or error

Closes #7884
Closes #7616
Closes #8014
Closes #8133
Closes #8322
Closes #8185
Closes #8098
Closes #8076
Closes #8014

This is basically a hack but pooling library has given us no options. Pool acquire call
now resolves even if there is an error. We simply check if its an error instance or not
and reject accordingly.

Read more here #7884 (comment)

_determineConnection method can be used to check if thing returned by pooling library is
actaully a connection or error
@mention-bot

Copy link
Copy Markdown

@sushantdhiman, thanks for your PR! By analyzing the history of the files in this pull request, we identified @felixfbecker, @swordfish444 and @Greenkeeper to be potential reviewers.

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

This is sort of work in progress, I will ask for final review from @sequelize/core when I am happy with this PR and may be add a few tests

}

return resolve(mayBeConnection);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How do you feel about this? Since you're using bluebird, it reduces a little boilerplate and I think this is equivalent.

_determineConnection(mayBeConnection) {
  if (mayBeConnection instanceof Error) {
    return Promise.resolve(this.pool.destroy(mayBeConnection))
      .catch(/Resource not currently part of this pool/, () => {}) // suppress this error
      .then(() => { throw mayBeConnection; });
  }

  return Promise.resolve(mayBeConnection);
}

@codecov

codecov Bot commented Sep 18, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #8330 into master will decrease coverage by <.01%.
The diff coverage is 81.48%.

@caseyduquettesc

Copy link
Copy Markdown

For a "hack" this certainly looks cleaner to me :)

.then(() => this.dialect.connectionManager.connect(config))
.then(connection => this.sequelize.runHooks('afterConnect', connection, config).return(connection))
.then(connection => resolve(connection))
.catch(e => { resolve(e); });

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.

No need to wrap in new Promise i reckon? Can just return err from catch i think?

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.

👍

this.poolError = e;
});
}),
create: () => this._connect(config),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think converting reject(err) to resolve(err) should be here. This hack is only for pool. _connect shouldn't be hacky.

this._connect(config).catch(err => err);

});
}),
create: () => this._connect(config),
destroy: connection => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggest changing name connection to mayBeConnection

And, I think "connection destroy" shouldn't be logged if error.

if (mayBeConnection instanceof Error) {
  return Promise.resolve(); // avoid "connection destroy" debug log
}
return this._disconnect(mayBeConnection).tap(() => {
  debug('connection destroy');
});

._connect(config.replication.read[nextRead])
.tap(connection => {
connection.queryType = 'read';
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As I mentioned above, I think the hack should be here.

return this
  ._connect(config.replication.read[nextRead])
  .then(connection => { // not to apply to error object, use "then", not "tap"
    connection.queryType = 'read';
  })
  .catch(err => err);

connection.queryType = 'read';
});
},
destroy: connection => this._disconnect(connection),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as above:

connection => {
  if (mayBeConnection instanceof Error) {
    return Promise.resolve();
  }
  return this._disconnect(connection);
}

})
.catch(e => {
this.poolError = e;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as above:

return this
  ._connect(config.replication.write)
  .then(connection => { // not to apply to error object, use "then", not "tap"
    connection.queryType = 'write';
  })
  .catch(err => err);

.then(() => this.dialect.connectionManager.connect(config))
.then(connection => this.sequelize.runHooks('afterConnect', connection, config).return(connection));
.then(connection => this.sequelize.runHooks('afterConnect', connection, config).return(connection))
.catch(error => { return error; });

@amachang amachang Sep 19, 2017 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As I mentioned above, I think the hack shouldn't be here.

Delete a call of catch.

// hacky, _determineConnection could send Error instance for disconnection
if (connection instanceof Error) {
return Promise.resolve();
}

@amachang amachang Sep 19, 2017 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As I mentioned above, I think the hack shouldn't be here.

Delete a statement of if (connection instanceof Error) { ... }.

@amachang

Copy link
Copy Markdown

I suggest a little bit changes, but it works nice!

idle: 10000,
acquire: 10000,
evict: 60000,
evict: 10000,

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.

We need to set evict ~= idle , reason being even if idle time is crossed its the evictor that will remove those idle connections.

More details coopernurse/node-pool#178

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.

Maybe a good idea to mention this in the API docs. (perhaps just a link to that issue)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@sushantdhiman by saying evict ~= idle, do you mean evict ≈ idle ? i think it's fine to set evict to some value a little smaller than idle.

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.

@xianxingg as mentioned in coopernurse/node-pool#178 (comment)

They should be close (or even evict < idle) because only evictor remove idle connection from pool

}

config.pool =_.defaults(config.pool || {}, defaultPoolingConfig, {
this.config.pool =_.defaults(config.pool || {}, defaultPoolingConfig, {

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.

Woosh, user supplied pooling.config was never used

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: space after equals

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While I agree with the change because it's clearer, doesn't this.config === config? And wouldn't that mean that changing config also changes this.config?

@sushantdhiman sushantdhiman Sep 20, 2017 •

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.

const config is deep cloned version of this.config, so any changes made to it will not reflect into this.config

No, you are right actually. Its my mistake, may be because I was working late on this last night 😜

@sushantdhiman sushantdhiman removed the wip label Sep 19, 2017
@sushantdhiman
sushantdhiman requested a review from a team September 19, 2017 16:50

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

Looks like solid work!

idle: 10000,
acquire: 10000,
evict: 60000,
evict: 10000,

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.

Maybe a good idea to mention this in the API docs. (perhaps just a link to that issue)

.then(() => sequelize.Promise.delay(100))
.then(() => {
// This query will be queued just after the `client.end` is executed and before its callback is called
expect(sequelize.connectionManager.pool.size).to.equal(0);

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.

Good catch - Otherwise we're not really sure what this test is testing! :D

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

Good work!

return this._disconnect(connection).tap(() => {
debug('connection destroy');
});
create: () => this._connect(config).catch(err => err ),

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.

space

Comment thread test/config/config.js
database: process.env.SEQ_MSSQL_DB || process.env.SEQ_DB || 'sequelize_test',
username: process.env.SEQ_MSSQL_USER || process.env.SEQ_USER || 'sequelize',
password: process.env.SEQ_MSSQL_PW || process.env.SEQ_PW || 'nEGkLma26gXVHFUAHJxcmsrK',
host: process.env.SEQ_MSSQL_HOST || process.env.SEQ_HOST || 'mssql.sequelizejs.com',

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.

did we host that? If so, we can shut it down then :)

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.

@sushantdhiman
sushantdhiman merged commit 5426912 into master Sep 20, 2017
@sushantdhiman
sushantdhiman deleted the fix-pool-issue branch September 20, 2017 14:54
@mjy78

mjy78 commented Sep 21, 2017

Copy link
Copy Markdown
Contributor

Thanks for putting the time into this fix. I've deployed the update, however under load I'm still seeing the "ResourceRequest timed out" errors even with a max pool size of 50. I then increased pool.acquire to 20000 and thus far the errors appeared to have stopped.

@ruanjiayou

Copy link
Copy Markdown

can we listen this error event?when "ResourceRequest timed out" occured ,we just use "process.exit(0)", then pm2 will restart application?

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

Labels

None yet

Projects

None yet

10 participants