Skip to content

Commit

Permalink
feat(causal-consistency): support afterClusterTime in readConcern
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Nov 20, 2017
1 parent 052dc02 commit a9097f7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
20 changes: 16 additions & 4 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ Collection.prototype.find = function() {

// Merge in options to command
for (var name in newOptions) {
if (newOptions[name] != null) findCommand[name] = newOptions[name];
if (newOptions[name] != null && name !== 'session') {
findCommand[name] = newOptions[name];
}
}

// Format the fields
Expand Down Expand Up @@ -2403,13 +2405,23 @@ function decorateWithCollation(command, self, options) {
}
}

function decorateWithReadConcern(command, self, options) { // eslint-disable-line
let readConcern = {};
function decorateWithReadConcern(command, self, options) {
let readConcern = Object.assign({}, command.readConcern || {});
if (self.s.readConcern) {
Object.assign(readConcern, self.s.readConcern);
}

Object.assign(command, readConcern);
if (
options.session &&
options.session.supports.causalConsistency &&
options.session.operationTime
) {
Object.assign(readConcern, { afterClusterTime: options.session.operationTime });
}

if (Object.keys(readConcern).length > 0) {
Object.assign(command, { readConcern: readConcern });
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"official"
],
"dependencies": {
"mongodb-core": "mongodb-js/mongodb-core#3.0.0"
"mongodb-core": "mongodb-js/mongodb-core#causally-consistent-reads"
},
"devDependencies": {
"betterbenchmarks": "^0.1.0",
Expand Down
55 changes: 55 additions & 0 deletions test/unit/sessions/collection_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
const MongoClient = require('../../..').MongoClient,
Timestamp = require('bson').Timestamp,
expect = require('chai').expect,
mock = require('../../mock');

const test = {};
describe('Sessions', function() {
describe('Collection', function() {
afterEach(() => mock.cleanup());
beforeEach(() => {
return mock.createServer().then(server => {
test.server = server;
});
});

it('should include `afterClusterTime` in read command with causal consistency', {
metadata: { requires: { topology: 'single' } },

test: function() {
let findCommand;
let insertOperationTime = Timestamp.fromNumber(Date.now());
test.server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster) {
request.reply(mock.DEFAULT_ISMASTER_36);
} else if (doc.insert) {
request.reply({ ok: 1, operationTime: insertOperationTime });
} else if (doc.find) {
findCommand = doc;
request.reply({ ok: 1 });
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});

return MongoClient.connect(`mongodb://${test.server.uri()}/test`).then(client => {
const session = client.startSession({ causalConsistency: true });
const coll = client.db('foo').collection('bar');

return coll
.insert({ a: 42 }, { session: session })
.then(() =>
coll.findOne({}, null, { session: session, readConcern: { level: 'majoroy' } })
)
.then(() => {
expect(findCommand.readConcern).to.have.keys(['level', 'afterClusterTime']);
expect(findCommand.readConcern.afterClusterTime).to.eql(insertOperationTime);
return client.close();
});
});
}
});
});
});

0 comments on commit a9097f7

Please sign in to comment.