2015-04-14 18:13:08 -04:00
|
|
|
// Test basic read committed functionality.
|
|
|
|
|
(function() {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
// Set up a set and grab things for later.
|
|
|
|
|
var name = "read_committed";
|
|
|
|
|
var replTest = new ReplSetTest({name: name,
|
|
|
|
|
nodes: 3,
|
2015-10-05 15:32:48 -04:00
|
|
|
nodeOptions: {enableMajorityReadConcern: ''}});
|
2015-04-14 18:13:08 -04:00
|
|
|
var nodes = replTest.nodeList();
|
2015-10-05 15:32:48 -04:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
replTest.startSet();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
var conn = MongoRunner.runMongod();
|
|
|
|
|
if (!conn.getDB('admin').serverStatus().storageEngine.supportsCommittedReads) {
|
|
|
|
|
jsTest.log("skipping test since storage engine doesn't support committed reads");
|
|
|
|
|
MongoRunner.stopMongod(conn);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-14 18:13:08 -04:00
|
|
|
replTest.initiate({"_id": name,
|
|
|
|
|
"members": [
|
|
|
|
|
{ "_id": 0, "host": nodes[0] },
|
2015-08-04 05:06:14 -04:00
|
|
|
{ "_id": 1, "host": nodes[1], priority: 0 },
|
2015-04-14 18:13:08 -04:00
|
|
|
{ "_id": 2, "host": nodes[2], arbiterOnly: true}]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get connections and collection.
|
2015-08-04 05:06:14 -04:00
|
|
|
var primary = replTest.getPrimary();
|
|
|
|
|
var secondary = replTest.liveNodes.slaves[0];
|
|
|
|
|
var secondaryId = replTest.getNodeId(secondary);
|
|
|
|
|
var db = primary.getDB(name);
|
2015-04-14 18:13:08 -04:00
|
|
|
var t = db[name];
|
|
|
|
|
|
|
|
|
|
function doDirtyRead() {
|
2015-07-14 08:58:21 -04:00
|
|
|
var res = t.runCommand('find', {"readConcern": {"level": "local"}});
|
2015-04-14 18:13:08 -04:00
|
|
|
assert.commandWorked(res);
|
|
|
|
|
return new DBCommandCursor(db.getMongo(), res).toArray()[0].state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function doCommittedRead() {
|
2015-07-14 08:58:21 -04:00
|
|
|
var res = t.runCommand('find', {"readConcern": {"level": "majority"}});
|
2015-04-14 18:13:08 -04:00
|
|
|
assert.commandWorked(res);
|
|
|
|
|
return new DBCommandCursor(db.getMongo(), res).toArray()[0].state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do a write, wait for it to replicate, and ensure it is visible.
|
2015-07-10 09:30:22 -04:00
|
|
|
assert.writeOK(t.save({_id: 1, state: 0}, {writeConcern: {w: "majority", wtimeout: 60*1000}}));
|
2015-04-14 18:13:08 -04:00
|
|
|
assert.eq(doDirtyRead(), 0);
|
|
|
|
|
assert.eq(doCommittedRead(), 0);
|
|
|
|
|
|
2015-08-04 05:06:14 -04:00
|
|
|
replTest.stop(secondaryId);
|
2015-04-14 18:13:08 -04:00
|
|
|
|
|
|
|
|
// Do a write and ensure it is only visible to dirty reads
|
|
|
|
|
assert.writeOK(t.save({_id: 1, state: 1}));
|
|
|
|
|
assert.eq(doDirtyRead(), 1);
|
|
|
|
|
assert.eq(doCommittedRead(), 0);
|
|
|
|
|
|
|
|
|
|
// Try the committed read again after sleeping to ensure it doesn't only work for queries
|
|
|
|
|
// immediately after the write.
|
|
|
|
|
sleep(1000);
|
|
|
|
|
assert.eq(doCommittedRead(), 0);
|
|
|
|
|
|
|
|
|
|
// Restart the node and ensure the committed view is updated.
|
2015-08-04 05:06:14 -04:00
|
|
|
replTest.restart(secondaryId);
|
|
|
|
|
db.getLastError("majority", 60 * 1000);
|
2015-04-14 18:13:08 -04:00
|
|
|
assert.eq(doDirtyRead(), 1);
|
|
|
|
|
assert.eq(doCommittedRead(), 1);
|
2015-07-14 08:58:21 -04:00
|
|
|
|
|
|
|
|
|
2015-04-14 18:13:08 -04:00
|
|
|
}());
|