Skip to content

Commit ddcd03d

Browse files
author
Thomas Reggi
authored
fix: sets primary read preference for writes
Uses a primary read preference for write / DDL operations, needed for server selection. NODE-2784
1 parent 4e03dfa commit ddcd03d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/operations/command_v2.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ class CommandOperationV2 extends OperationBase {
1717

1818
this.ns = parent.s.namespace.withCollection('$cmd');
1919
const propertyProvider = this.hasAspect(Aspect.NO_INHERIT_OPTIONS) ? undefined : parent;
20-
this.readPreference = ReadPreference.resolve(propertyProvider, this.options);
20+
this.readPreference = this.hasAspect(Aspect.WRITE_OPERATION)
21+
? ReadPreference.primary
22+
: ReadPreference.resolve(propertyProvider, this.options);
2123
this.readConcern = resolveReadConcern(propertyProvider, this.options);
2224
this.writeConcern = resolveWriteConcern(propertyProvider, this.options);
2325
this.explain = false;

test/functional/collection.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
2+
const Topology = require('../../lib/core/sdam/topology').Topology;
23
const setupDatabase = require('./shared').setupDatabase;
34
const chai = require('chai');
45
const expect = chai.expect;
56
const sinonChai = require('sinon-chai');
67
const mock = require('mongodb-mock-server');
8+
const ReadPreference = require('../../lib/core/topologies/read_preference');
79
chai.use(sinonChai);
810

911
describe('Collection', function() {
@@ -1307,4 +1309,42 @@ describe('Collection', function() {
13071309
});
13081310
}
13091311
});
1312+
1313+
context('DDL methods with serverSelection readPreference primary', () => {
1314+
const collectionMethodSet = {
1315+
createIndex: [{ quote: 'text' }]
1316+
};
1317+
1318+
Object.keys(collectionMethodSet).forEach(operation => {
1319+
it(`should ${operation} with serverSelection readPreference primary`, {
1320+
metadata: {
1321+
requires: { topology: 'replicaset' }
1322+
},
1323+
test: function(done) {
1324+
const opArgs = collectionMethodSet[operation];
1325+
const configuration = this.configuration;
1326+
const client = configuration.newClient(configuration.writeConcernMax(), {
1327+
useUnifiedTopology: true,
1328+
readPreference: 'primaryPreferred'
1329+
});
1330+
client.connect((err, client) => {
1331+
expect(err).to.not.exist;
1332+
const db = client.db(configuration.db);
1333+
const collection = db.collection('db-two');
1334+
const TopologySpy = this.sinon.spy(Topology.prototype, 'selectServer');
1335+
const callback = err => {
1336+
expect(err).to.not.exist;
1337+
expect(TopologySpy.called).to.equal(true);
1338+
expect(TopologySpy)
1339+
.nested.property('args[0][0].readPreference.mode')
1340+
.to.equal(ReadPreference.PRIMARY);
1341+
client.close(done);
1342+
};
1343+
opArgs.push(callback);
1344+
collection[operation].apply(collection, opArgs);
1345+
});
1346+
}
1347+
});
1348+
});
1349+
});
13101350
});

0 commit comments

Comments
 (0)