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

NODEJS-681: ControlConnection Concurrent Read and Write on .host and .connection #430

Open
wants to merge 1 commit 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
14 changes: 9 additions & 5 deletions lib/control-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ class ControlConnection extends events.EventEmitter {
await this._registerToConnectionEvents();
} catch (err) {
this.log('error', 'ControlConnection failed to retrieve topology and keyspaces information', err);
this._triedHosts[this.connection.endpoint] = err;
if (this.connection != null) {
this._triedHosts[this.connection.endpoint] = err;
}

if (err.isSocketError && this.host) {
this.host.removeFromPool(this.connection);
Expand All @@ -501,10 +503,12 @@ class ControlConnection extends events.EventEmitter {
}

this._reconnectionSchedule = this._reconnectionPolicy.newSchedule();
this._setHealthListeners(this.host, this.connection);
this.emit('newConnection', null, this.connection, this.host);

this.log('info', `ControlConnection connected to ${this.connection.endpointFriendlyName} and up to date`);
if (this.host != null && this.connection != null) {
this._setHealthListeners(this.host, this.connection);
this.emit('newConnection', null, this.connection, this.host);

this.log('info', `ControlConnection connected to ${this.connection.endpointFriendlyName} and up to date`);
} // else, someone else started a _refresh or _initializeConnection, ignoring the current result
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/integration/short/control-connection-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ describe('ControlConnection', function () {
assert.strictEqual(cc.hosts.length, 1);
});

it('should not break when refreshing concurrently', async () => {
const cc = newInstance();
cc.options.policies.loadBalancing = new policies.loadBalancing.RoundRobinPolicy();
disposeAfter(cc);

await cc.init();
await new Promise(r => cc.options.policies.loadBalancing.init(null, cc.hosts, r));

/** @type {Array<Promise>} */
const refreshPromises = [];
// randomly emit 200 times cc._refresh
for (let i = 0; i < 200; i++) {
refreshPromises.push(cc._refresh());
// interval of random number
await helper.delayAsync(Math.floor(Math.random() * 200));
}
Promise.all(refreshPromises);
assert.ok(cc.host);
assert.ok(cc.connection);
});

it('should reconnect when host used goes down', async () => {
const options = clientOptions.extend(
utils.extend({ pooling: helper.getPoolingOptions(1, 1, 500) }, helper.baseOptions));
Expand Down
4 changes: 2 additions & 2 deletions test/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1441,10 +1441,10 @@ helper.ads._spawnAndWait = function(processName, params, cb) {
cb = utils.noop;
};

// If process hasn't completed in 10 seconds.
// If process hasn't completed in 20 seconds.
timeout = setTimeout(function() {
callbackOnce(new Error("Timed out while waiting for " + processName + " to complete."));
}, 10000);
}, 20000);

const p = spawn(processName, params, {
env: Object.assign({ KRB5_CONFIG: this.getKrb5ConfigPath()}, process.env)
Expand Down