Skip to content

Commit ef0e676

Browse files
authored
Re-resolve contact points after total disconnect (#378)
Fix for https://datastax-oss.atlassian.net/browse/NODEJS-632
1 parent 7f80bfd commit ef0e676

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# ChangeLog - DataStax Node.js Driver
22

3+
## 4.6.3
4+
5+
2021-05-??
6+
7+
### Bug fixes
8+
9+
- [NODEJS-632] - Re-resolve contact points on reconnect when all nodes are unavailable
10+
311
## 4.6.2
412

513
2021-03-30

lib/control-connection.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,28 @@ class ControlConnection extends events.EventEmitter {
415415
}
416416
}
417417

418+
async _refreshControlConnection(hostIterator) {
419+
420+
if (this.options.sni) {
421+
this.connection = this._borrowAConnection(hostIterator);
422+
}
423+
else {
424+
try { this.connection = this._borrowAConnection(hostIterator); }
425+
catch(err) {
426+
427+
/* NODEJS-632: refresh nodes before getting hosts for reconnect since some hostnames may have
428+
* shifted during the flight. */
429+
this.log("info", "ControlConnection could not reconnect using existing connections. Refreshing contact points and retrying");
430+
this._contactPoints.clear();
431+
this._resolvedContactPoints.clear();
432+
await Promise.all(this.options.contactPoints.map(name => this._parseContactPoint(name)));
433+
const refreshedContactPoints = Array.from(this._contactPoints).join(',');
434+
this.log('info', `Refreshed contact points: ${refreshedContactPoints}`);
435+
await this._initializeConnection();
436+
}
437+
}
438+
}
439+
418440
/**
419441
* Acquires a new connection and refreshes topology and keyspace metadata.
420442
* <p>When it fails obtaining a connection and there aren't any more hosts, it schedules reconnection.</p>
@@ -439,8 +461,7 @@ class ControlConnection extends events.EventEmitter {
439461
hostIterator = await promiseUtils.newQueryPlan(this._profileManager.getDefaultLoadBalancing(), null, null);
440462
}
441463

442-
this.connection = this._borrowAConnection(hostIterator);
443-
464+
await this._refreshControlConnection(hostIterator);
444465
} catch (err) {
445466
// There was a failure obtaining a connection or during metadata retrieval
446467
this.log('error', 'ControlConnection failed to acquire a connection', err);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"test": "./node_modules/.bin/mocha test/unit -R spec -t 5000 --recursive",
4444
"ci_jenkins": "./node_modules/.bin/mocha test/unit test/integration/short --recursive -R mocha-jenkins-reporter --exit",
4545
"ci_appveyor": "./node_modules/.bin/mocha test/unit test/integration/short --recursive -R mocha-appveyor-reporter --exit",
46+
"foo": "./node_modules/.bin/mocha test/integration/short --recursive -R spec --exit -g 'should subscribe to TOPOLOGY_CHANGE'",
4647
"ci_unit_appveyor": "./node_modules/.bin/mocha test/unit --recursive -R mocha-appveyor-reporter --exit",
4748
"server_api": "./node_modules/.bin/mocha test/integration/short -g '@SERVER_API' --recursive --exit",
4849
"eslint": "eslint lib test"

test/integration/short/control-connection-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('ControlConnection', function () {
116116
await cc.init();
117117
await new Promise(r => options.policies.loadBalancing.init(null, cc.hosts, r));
118118

119-
await util.promisify(helper.ccmHelper.bootstrapNode)(3);
119+
await util.promisify(helper.ccmHelper.bootstrapNode)({nodeIndex: 3, dc: 'dc1'});
120120
await util.promisify(helper.ccmHelper.startNode)(3);
121121

122122
// While the host is started, it's not a given that it will have been connected and marked up,

test/unit/control-connection-tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,9 @@ describe('ControlConnection', function () {
422422
yield this.delay;
423423
};
424424

425-
const cc = newInstance({ contactPoints: [ '::1' ], policies: { loadBalancing: lbp, reconnection: rp } },
426-
getContext({ state: state, queryResults: { 'peers': [ {'rpc_address': types.InetAddress.fromString('::2') } ] }}));
425+
const cc = newInstance(
426+
{ contactPoints: [ '::1' ], policies: { loadBalancing: lbp, reconnection: rp } },
427+
getContext({ state: state, queryResults: { 'peers': [ {'rpc_address': types.InetAddress.fromString('::2') } ] }, failBorrow: [-1,1]}));
427428

428429
await cc.init();
429430

0 commit comments

Comments
 (0)