Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(pool): ensure that lsid is sent in get requests to mongos
Browse files Browse the repository at this point in the history
There is a strange case, when topology is mongos and readPreference
is not primary, where we nest our find queries in a "$query" attribute.
This was having the effect of not sending the lsid on find requests,
which caused further errors when getMores attempted to send an lsid.

Fixes NODE-1420
  • Loading branch information
daprahamian committed Apr 20, 2018
1 parent 468d1ff commit ae820f6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/connection/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,11 @@ Pool.prototype.write = function(commands, options, cb) {
// decorate the commands with session-specific details
commands.forEach(command => {
if (command instanceof Query) {
Object.assign(command.query, sessionOptions);
if (command.query.$query) {
Object.assign(command.query.$query, sessionOptions);
} else {
Object.assign(command.query, sessionOptions);
}
} else {
Object.assign(command, sessionOptions);
}
Expand Down
66 changes: 66 additions & 0 deletions test/tests/unit/mongos/sessions_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ var Mongos = require('../../../../lib/topologies/mongos'),
mock = require('mongodb-mock-server'),
genClusterTime = require('../common').genClusterTime;

const sessions = require('../../../../lib/sessions');
const ServerSessionPool = sessions.ServerSessionPool;
const ClientSession = sessions.ClientSession;
const ReadPreference = require('../../../../lib/topologies/read_preference');

const test = {};
describe('Sessions (Mongos)', function() {
afterEach(() => mock.cleanup());
Expand Down Expand Up @@ -161,4 +166,65 @@ describe('Sessions (Mongos)', function() {
mongos.connect();
}
});

it(
'should ensure that lsid is received within the query object of a find request when read preference is not primary',
{
metadata: { requires: { topology: 'single' } },
test: function(done) {
const clusterTime = genClusterTime(Date.now());
test.server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster) {
request.reply(
Object.assign({}, mock.DEFAULT_ISMASTER_36, {
msg: 'isdbgrid',
$clusterTime: clusterTime
})
);
} else if (doc.$query) {
try {
expect(doc.$readPreference).to.deep.equal({ mode: 'primaryPreferred' });
expect(doc)
.to.haveOwnProperty('$query')
.to.haveOwnProperty('lsid')
.that.is.an('object');
done();
} catch (e) {
done(e);
}
} else {
done('YOU HAVE FAILED. WE WILL FIND ANOTHER WAY. RELEASING CONTROL');
}
});

const mongos = new Mongos([test.server.address()], {
connectionTimeout: 30000,
socketTimeout: 30000,
haInterval: 500,
size: 1
});

mongos.on('error', done);
mongos.once('connect', () => {
const namespace = 'testdb.testcollection';
const findCommand = {
find: namespace
};
const pool = new ServerSessionPool(mongos);
const session = new ClientSession(mongos, pool);
const readPreference = new ReadPreference('primaryPreferred');

const cursor = mongos.cursor('testdb.testcollection', findCommand, {
session,
readPreference
});

cursor.next(() => {});
});

mongos.connect();
}
}
);
});

0 comments on commit ae820f6

Please sign in to comment.