-
-
Notifications
You must be signed in to change notification settings - Fork 627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement CMD_RESET_CONNECTION #1437
base: master
Are you sure you want to change the base?
Conversation
8e3972b
to
3995a72
Compare
3995a72
to
299ed3d
Compare
I think mysqljs/mysql has CMD_RESET_CONNECTION implemented ( not sure if .reset() is exposed in the public api ) and it's added automatically to a pool connections before releasing them back. IMO it's a useful feature, maybe worth discussing compatibility and perf impact if we make it on by default |
I have thought of that too. This will help make pooled connection works just like a typical connection including reset local temp table. |
looks like it's not merged yet - mysqljs/mysql#1469 |
ok changeUser is sent only when connection did change user on itself and its different from what's configured on the pool - https://github.com/mysqljs/mysql/blob/3430c513d6b0ca279fb7c79b210c9301e9315658/lib/Pool.js#L116 to me that should be done on release in the background. Not sure what's mysql2 behaviour in that case ( get connection, change user, release connection ) |
I think that we should add an option to turn it on/off defaulting to on? |
agree. default on is a sensible security / "no surprises" option while allowing explicit "off" to gain a bit of performance. |
I wonder if it's possible to have "auto" mode - if no change to variables / affected rows / user / db don't do reset |
I think in general, reset should take that into account already. It would just add another roundtrip cost. |
hey @testn I might have some time this weekend to clear backlog of PRs - do you need any help with integration tests for this? |
Awesome. I still cannot figure out why it failed in some CI setting. Also, connection.end() is not "async". However, to end() the connection, I would like to make a reset() call which is async network call. What should we do with it? |
I just stumbled upon a bug caused by this. I was wondering "do they reset the connection?", and the answer is apparently "no". |
@danielgindi 100% agree ( though would be great to have some tests to support 1-2% perf hit assumption ). Any preference on the name for this flag? |
As for the naming - this should just be "connectionReset". Other connectors do this too: https://mysqlconnector.net/connection-options/. As for the perf hit - I tested about a year ago with the .net connector, with and without the reset. I believe it's safe to assume it would be the same here. Anyway it's a non-issue, as you never over-optimize at safety's expense. |
@sidorares Note that in some cases, the perf hit may be larger. i.e:
In these cases, it may even be better to always reset the connection when returning to the pool. You can see some discussion here: mysql-net/MySqlConnector#178 |
hm, doing reset on connection release might actually be better for performance ( or rather consumer latency ) as in this case there is a chance that reset is happening while no one is waiting |
@@ -707,6 +707,10 @@ class Connection extends EventEmitter { | |||
return this.addCommand(new Commands.Ping(cb)); | |||
} | |||
|
|||
reset(cb) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this need to perform some additional client cleanup, for example prepared statements need to be discarded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok let me take a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preparedstatement is discarded automatically
https://mariadb.com/kb/en/com_reset_connection/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, server side, but we need to delete statements cache otherwise it might be used incorrectly with execute()
after reset
I think this is what needs to be done
- add private connection helper method
connection._initialiseStatementCache({ disableDispose: boolean })
something along the lines:
_initialiseStatementCache({ disableDispose }) {
if (this._statements) {
if (disableDispose) {
this._statements.dispose = null;
}
this._statements.clear();
}
this._statements = new LRU({
max: this.config.maxPreparedStatements,
dispose: function(key, statement) {
statement.close();
}
})
}
- replace line 66 in connection.js with
this._initialiseStatementCache({ disableDispose: false });
- in
ResetConnectionCommand.resetConnectionResponse(packet, connection)
add callconnection._initialiseStatementCache({ disableDispose: true })
at the beginning before callingonResult()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... got it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought statement can be reused even though the connection is reset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to double check but I'm pretty sure statement id is sequential 1, 2, 3, ... for each prepare()
command ( can't remember if 0 or 1 based ) and after reset it goes again as 1, 2, 3, ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small correction: set dispose to noop instead of null, e.i this._statements.dispose = () => {};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha
@testn I believe that releaseConnection(connection, reset = true) {
let cb;
if (!connection._pool) {
// The connection has been removed from the pool and is no longer good.
if (this._connectionQueue.length) {
cb = this._connectionQueue.shift();
process.nextTick(this.getConnection.bind(this, cb));
}
} else {
if (reset) {
connection.reset(() => {
this.releaseConnection(connection, false);
});
} else {
if (this._connectionQueue.length) {
cb = this._connectionQueue.shift();
process.nextTick(cb.bind(null, null, connection));
} else {
this._freeConnections.push(connection);
this.emit('release', connection);
}
}
}
} |
We've tried this in a production environment, after modifying the |
@testn have you taken a look at this? This is to actually trigger the reset. |
Can you explain a little bit? |
@testn we want the connector to reset a connection in the background before making it available again. Now if there was an error, even in the We can make this flag based, but this should be the default behavior anyway. |
What's left to get this over the finish line? |
Address #1328
still in the draft...
To add integration tests