Skip to content

Suggestions on gracefully recovering from a DB crash/restart with poolCluster #1061

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

Closed
adrianblynch opened this issue Apr 22, 2015 · 5 comments
Assignees
Labels

Comments

@adrianblynch
Copy link
Contributor

We're using the following poolCluster.getConnection('*', 'RANDOM', function (err, connection) { ... }. If a request comes into our API when the DB is down we get the POOL_NOEXIST error as expected.

Once the DB is back up, the pool of connections can't be used.

My question is, what are people doing to gracefully get things back up and running again?

A bit more detail about what we have in case it's important to the answer...

var config = require('./config')();
var log = require('./log');
var poolCluster = mysql.createPoolCluster();

config.database.main.forEach(function (serverConfig) {
    poolCluster.add(serverConfig.id, serverConfig);
});

poolCluster.on('remove', function (nodeId) {
    log.error('Removed mysql node: ' + nodeId);
    // TODO: Look for another way as this uses a private method
    if (poolCluster._findNodeIds().length === 0) {
        var errorMsg = 'No remaining mysql servers, exiting';
        log.error(errorMsg);
        throw new Error(errorMsg);
    }
});

exports.conn = {
    query: function (sql, values, cb) {
        var query = mysql.createQuery(sql, values, cb);

        poolCluster.getConnection('*', 'RANDOM', function (err, connection) {
            if (err) {
                query.on('error', function () {
                    log.error('sql error', err);
                });
                query.end(err);
                return;
            }

            query.once('end', function () {
                connection.release();
            });

            connection.query(query);
        });

        return query;
    }
};
@dougwilson
Copy link
Member

Have you played around with the restoreNodeTimeout option in PoolCluster (https://github.com/felixge/node-mysql#poolcluster-option)? It was added in version 2.6.0

@dougwilson dougwilson self-assigned this Apr 22, 2015
@adrianblynch
Copy link
Contributor Author

I have not! But I will!

Will report back with what I find.

Thanks Doug.

@dougwilson
Copy link
Member

Hi @adrianblynch , I haven't really heard back. I'm going to close this issue, but feel free to reply and I can always re-open if necessary :)

@tilleps
Copy link

tilleps commented Aug 17, 2015

Having running into this myself, restoreNodeTimeout appears to have done the trick. However, it would be nice to have a mechanism to manually restore the node, something like:

poolCluster.restore(function(cb) {});

@tilleps
Copy link

tilleps commented Aug 17, 2015

Is there any negative implications to setting restoreNodeTimeout to a really node value like 1(ms)?

dveeden pushed a commit to dveeden/mysql that referenced this issue Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants