mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
SERVER-31293 Don't consider readPreference 'primary' as equivalent to 'slaveOk' in OP_QUERY find path
This commit is contained in:
parent
2260912ecb
commit
a9c8f03bb7
54
jstests/replsets/slaveok_read_pref.js
Normal file
54
jstests/replsets/slaveok_read_pref.js
Normal file
@ -0,0 +1,54 @@
|
||||
// Test that slaveOk is implicitly allowed for queries on a secondary with a read preference other
|
||||
// than 'primary', and that queries which do have 'primary' read preference fail.
|
||||
(function() {
|
||||
"use strict";
|
||||
|
||||
const readPrefs =
|
||||
[undefined, "primary", "secondary", "primaryPreferred", "secondaryPreferred", "nearest"];
|
||||
|
||||
const rst = new ReplSetTest({nodes: 3});
|
||||
rst.startSet();
|
||||
|
||||
const nodes = rst.nodeList();
|
||||
rst.initiate({
|
||||
_id: jsTestName(),
|
||||
members: [
|
||||
{_id: 0, host: nodes[0]},
|
||||
{_id: 1, host: nodes[1], priority: 0},
|
||||
{_id: 2, host: nodes[2], arbiterOnly: true}
|
||||
]
|
||||
});
|
||||
|
||||
const priDB = rst.getPrimary().getDB(jsTestName());
|
||||
assert(priDB.dropDatabase());
|
||||
|
||||
assert.commandWorked(priDB.test.insert({a: 1}, {writeConcern: {w: "majority"}}));
|
||||
|
||||
const secDB = rst.getSecondary().getDB(jsTestName());
|
||||
|
||||
for (let readMode of["commands", "legacy"]) {
|
||||
for (let readPref of readPrefs) {
|
||||
for (let slaveOk of[true, false]) {
|
||||
const testType = {readMode: readMode, readPref: readPref, slaveOk: slaveOk};
|
||||
|
||||
secDB.getMongo().forceReadMode(readMode);
|
||||
secDB.getMongo().setSlaveOk(slaveOk);
|
||||
|
||||
const cursor =
|
||||
(readPref ? secDB.test.find().readPref(readPref) : secDB.test.find());
|
||||
|
||||
if (readPref === "primary" || (!readPref && !slaveOk)) {
|
||||
// Attempting to run the query throws an error of type NotMasterNoSlaveOk.
|
||||
const slaveOkErr = assert.throws(() => cursor.itcount(), [], testType);
|
||||
assert.commandFailedWithCode(slaveOkErr, ErrorCodes.NotMasterNoSlaveOk);
|
||||
} else {
|
||||
// Succeeds for all non-primary readPrefs, and for no readPref iff slaveOk.
|
||||
const docCount = assert.doesNotThrow(() => cursor.itcount(), [], testType);
|
||||
assert.eq(docCount, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rst.stopSet();
|
||||
})();
|
@ -562,9 +562,12 @@ std::string runQuery(OperationContext* opCtx,
|
||||
{
|
||||
const QueryRequest& qr = cq->getQueryRequest();
|
||||
|
||||
// uassert if we are not on a primary, and not a secondary with SlaveOk query parameter set.
|
||||
// TODO(SERVER-31293): Don't set slaveOk for reads with a read pref of "primary".
|
||||
const bool slaveOK = qr.isSlaveOk() || qr.hasReadPref();
|
||||
// Allow the query to run on secondaries if the read preference permits it. If no read
|
||||
// preference was specified, allow the query to run iff slaveOk has been set.
|
||||
const bool slaveOK = qr.hasReadPref()
|
||||
? uassertStatusOK(ReadPreferenceSetting::fromContainingBSON(q.query))
|
||||
.canRunOnSecondary()
|
||||
: qr.isSlaveOk();
|
||||
uassertStatusOK(
|
||||
repl::ReplicationCoordinator::get(opCtx)->checkCanServeReadsFor(opCtx, nss, slaveOK));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user