Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const RegisterSlave = require('./register_slave.js');
const BinlogDump = require('./binlog_dump.js');
const ChangeUser = require('./change_user.js');
const Quit = require('./quit.js');
const ResetConnection = require('./reset_connection.js');

module.exports = {
ClientHandshake,
Expand All @@ -23,5 +24,6 @@ module.exports = {
RegisterSlave,
BinlogDump,
ChangeUser,
Quit
Quit,
ResetConnection
};
26 changes: 26 additions & 0 deletions lib/commands/reset_connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const Command = require('./command');
const Packets = require('../packets/index.js');

class ResetConnection extends Command {
constructor(callback) {
super();
this.onResult = callback;
}

start(packet, connection) {
const req = new Packets.ResetConnection();
connection.writePacket(req.toPacket());
return ResetConnection.prototype.resetConnectionResponse;
}

resetConnectionResponse() {
if (this.onResult) {
process.nextTick(this.onResult.bind(this));
}
return null;
}
}

module.exports = ResetConnection;
4 changes: 4 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ class Connection extends EventEmitter {
return this.addCommand(new Commands.Ping(cb));
}

reset(cb) {
Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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/

Copy link
Owner

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

  1. 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();
    }
  })
}
  1. replace line 66 in connection.js with this._initialiseStatementCache({ disableDispose: false });
  1. in ResetConnectionCommand.resetConnectionResponse(packet, connection) add call connection._initialiseStatementCache({ disableDispose: true }) at the beginning before calling onResult()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah... got it

Copy link
Contributor Author

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

Copy link
Owner

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, ...

Copy link
Owner

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 = () => {};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gotcha

return this.addCommand(new Commands.ResetConnection(cb));
}

_registerSlave(opts, cb) {
return this.addCommand(new Commands.RegisterSlave(opts, cb));
}
Expand Down
1 change: 1 addition & 0 deletions lib/constants/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ module.exports = {
STMT_FETCH: 0x1c,
DAEMON: 0x1d, // deprecated
BINLOG_DUMP_GTID: 0x1e,
RESET_CONNECTION: 0x1f, // introduced in 5.7.3
UNKNOWN: 0xff // bad!
};
4 changes: 3 additions & 1 deletion lib/packets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const RegisterSlave = require('./register_slave');
const ResultSetHeader = require('./resultset_header');
const SSLRequest = require('./ssl_request');
const TextRow = require('./text_row');
const ResetConnection = require('./reset_connection');

const ctorMap = {
AuthNextFactor,
Expand All @@ -50,7 +51,8 @@ const ctorMap = {
RegisterSlave,
ResultSetHeader,
SSLRequest,
TextRow
TextRow,
ResetConnection
};
Object.entries(ctorMap).forEach(([name, ctor]) => {
module.exports[name] = ctor;
Expand Down
18 changes: 18 additions & 0 deletions lib/packets/reset_connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const Packet = require('../packets/packet');
const CommandCodes = require('../constants/commands');

class ResetConnection {
constructor() {
}

toPacket() {
const packet = new Packet(0, Buffer.allocUnsafe(5), 0, 5);
packet.offset = 4;
packet.writeInt8(CommandCodes.RESET_CONNECTION);
return packet;
}
}

module.exports = ResetConnection;
9 changes: 9 additions & 0 deletions promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ class PromiseConnection extends EventEmitter {
});
}

reset() {
const c = this.connection;
const localErr = new Error();
return new this.Promise((resolve, reject) => {
const done = makeDoneCb(resolve, reject, localErr);
c.reset(done);
});
}

connect() {
const c = this.connection;
const localErr = new Error();
Expand Down