diff options
author | Ali Mir <ali.mir@mongodb.com> | 2020-08-11 11:34:10 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-09-02 19:29:04 +0000 |
commit | 91563e0f58c8e3e2e3473283a5c14a3abc4bc25c (patch) | |
tree | afc96c75e5b171c9e0f778139d24e56d1c9aa070 | |
parent | 4e0bb24f64db02c8dde9967c9571e08fb16bcec3 (diff) | |
download | mongo-91563e0f58c8e3e2e3473283a5c14a3abc4bc25c.tar.gz |
SERVER-49990 Alias setSlaveOk() and getSlaveOk() shell helpers
(cherry picked from commit d6fe50035aff8026937dff9d8544ff213ad05152)
-rw-r--r-- | jstests/aggregation/bugs/server18198.js | 2 | ||||
-rw-r--r-- | jstests/noPassthroughWithMongod/create_indexes_shell_helper.js | 2 | ||||
-rw-r--r-- | jstests/noPassthroughWithMongod/default_read_pref.js | 2 | ||||
-rw-r--r-- | jstests/replsets/drain.js | 4 | ||||
-rw-r--r-- | jstests/replsets/secondaryOk_slaveOk_aliases.js | 46 | ||||
-rw-r--r-- | src/mongo/shell/collection.js | 26 | ||||
-rw-r--r-- | src/mongo/shell/db.js | 24 | ||||
-rw-r--r-- | src/mongo/shell/mongo.js | 18 | ||||
-rw-r--r-- | src/mongo/shell/replsettest.js | 4 | ||||
-rw-r--r-- | src/mongo/shell/utils.js | 15 |
10 files changed, 116 insertions, 27 deletions
diff --git a/jstests/aggregation/bugs/server18198.js b/jstests/aggregation/bugs/server18198.js index a182195a864..879028d119e 100644 --- a/jstests/aggregation/bugs/server18198.js +++ b/jstests/aggregation/bugs/server18198.js @@ -11,7 +11,7 @@ var commandsRan = []; // hook in our patched mongo var mockMongo = { - getSlaveOk: function() { + getSecondaryOk: function() { return true; }, runCommand: function(db, cmd, opts) { diff --git a/jstests/noPassthroughWithMongod/create_indexes_shell_helper.js b/jstests/noPassthroughWithMongod/create_indexes_shell_helper.js index 6d9937139ae..61a5e2dfa44 100644 --- a/jstests/noPassthroughWithMongod/create_indexes_shell_helper.js +++ b/jstests/noPassthroughWithMongod/create_indexes_shell_helper.js @@ -15,7 +15,7 @@ writeMode: function() { return this._writeMode; }, - getSlaveOk: function() { + getSecondaryOk: function() { return true; }, runCommand: function(db, cmd, opts) { diff --git a/jstests/noPassthroughWithMongod/default_read_pref.js b/jstests/noPassthroughWithMongod/default_read_pref.js index e5daba20d8a..6d158cfb4ee 100644 --- a/jstests/noPassthroughWithMongod/default_read_pref.js +++ b/jstests/noPassthroughWithMongod/default_read_pref.js @@ -8,7 +8,7 @@ try { var commandsRan = []; db._mongo = { - getSlaveOk: function() { + getSecondaryOk: function() { return false; }, getReadPrefMode: function() { diff --git a/jstests/replsets/drain.js b/jstests/replsets/drain.js index 41e8d475f83..d00348454b6 100644 --- a/jstests/replsets/drain.js +++ b/jstests/replsets/drain.js @@ -66,7 +66,7 @@ assert(!secondary.getDB("admin").runCommand({"isMaster": 1}).ismaster); // Ensure new primary is not yet readable without slaveOk bit. - secondary.slaveOk = false; + secondary.setSecondaryOk(false); jsTestLog('New primary should not be readable yet, without slaveOk bit'); var res = secondary.getDB("foo").runCommand({find: "foo"}); assert.commandFailed(res); @@ -74,7 +74,7 @@ res.code, "find failed with unexpected error code: " + tojson(res)); // Nor should it be readable with the slaveOk bit. - secondary.slaveOk = true; + secondary.setSecondaryOk(); assert.commandWorked(secondary.getDB("foo").runCommand({find: "foo"})); assert.commandFailedWithCode( diff --git a/jstests/replsets/secondaryOk_slaveOk_aliases.js b/jstests/replsets/secondaryOk_slaveOk_aliases.js new file mode 100644 index 00000000000..ffb9bbbe7a3 --- /dev/null +++ b/jstests/replsets/secondaryOk_slaveOk_aliases.js @@ -0,0 +1,46 @@ +// Tests that member functions setSecondaryOk()/getSecondaryOk() and their +// aliases, setSlaveOk()/getSlaveOk(), produce the same results. + +(function() { + "use strict"; + const dbName = "test"; + const collName = "coll"; + const rst = new ReplSetTest({nodes: 2}); + rst.startSet(); + rst.initiate(); + const primary = rst.getPrimary(); + const secondary = rst.getSecondary(); + + assert.writeOK(primary.getDB(dbName)[collName].insert({x: 1})); + rst.awaitReplication(); + + // secondaryOk is initially set to true in awaitReplication, so reads on secondaries should + // succeed. + assert.eq(secondary.getDB(dbName).getMongo().getSecondaryOk(), true); + assert.eq(secondary.getDB(dbName).getSecondaryOk(), true); + assert.commandWorked(secondary.getDB(dbName).runCommand({find: collName}), + "find command failed with an unexpected error"); + + // Set secondaryOk to false, disallowing reads on secondaries. + secondary.getDB(dbName).getMongo().setSecondaryOk(false); + assert.eq(secondary.getDB(dbName).getMongo().getSecondaryOk(), false); + assert.commandFailedWithCode(secondary.getDB(dbName).runCommand({find: collName}), + ErrorCodes.NotMasterNoSlaveOk, + "find did not fail with the correct error code"); + + // setSlaveOk() is deprecated and aliased to setSecondaryOk(), but ensure + // it still works for backwards compatibility. + secondary.getDB(dbName).getMongo().setSlaveOk(); + assert.eq(secondary.getDB(dbName).getMongo().getSlaveOk(), true); + assert.eq(secondary.getDB(dbName).getSlaveOk(), true); + assert.commandWorked(secondary.getDB(dbName).runCommand({find: collName}), + "find command failed with an unexpected error"); + + // Set slaveOk to false, disallowing reads on secondaries. + secondary.getDB(dbName).getMongo().setSlaveOk(false); + assert.eq(secondary.getDB(dbName).getMongo().getSlaveOk(), false); + assert.commandFailedWithCode(secondary.getDB(dbName).runCommand({find: collName}), + ErrorCodes.NotMasterNoSlaveOk, + "find did not fail with the correct error code"); + rst.stopSet(); +})(); diff --git a/src/mongo/shell/collection.js b/src/mongo/shell/collection.js index 452a2361f30..00143600595 100644 --- a/src/mongo/shell/collection.js +++ b/src/mongo/shell/collection.js @@ -1509,20 +1509,32 @@ DBCollection.prototype.getSplitKeysForChunks = function(chunkSize) { }; DBCollection.prototype.setSlaveOk = function(value) { - if (value == undefined) - value = true; - this._slaveOk = value; + print( + "WARNING: setSlaveOk() is deprecated and may be removed in the next major release. Please use setSecondaryOk() instead."); + this.setSecondaryOk(value); }; DBCollection.prototype.getSlaveOk = function() { - if (this._slaveOk != undefined) - return this._slaveOk; - return this._db.getSlaveOk(); + print( + "WARNING: getSlaveOk() is deprecated and may be removed in the next major release. Please use getSecondaryOk() instead."); + return this.getSecondaryOk(); +}; + +DBCollection.prototype.setSecondaryOk = function(value) { + if (value === undefined) + value = true; + this._secondaryOk = value; +}; + +DBCollection.prototype.getSecondaryOk = function() { + if (this._secondaryOk !== undefined) + return this._secondaryOk; + return this._db.getSecondaryOk(); }; DBCollection.prototype.getQueryOptions = function() { // inherit this method from DB but use apply so - // that slaveOk will be set if is overridden on this DBCollection + // that secondaryOk will be set if is overridden on this DBCollection return this._db.getQueryOptions.apply(this, arguments); }; diff --git a/src/mongo/shell/db.js b/src/mongo/shell/db.js index f417f40060d..dcd23b26b25 100644 --- a/src/mongo/shell/db.js +++ b/src/mongo/shell/db.js @@ -1331,20 +1331,32 @@ var DB; }; DB.prototype.setSlaveOk = function(value) { + print( + "WARNING: setSlaveOk() is deprecated and may be removed in the next major release. Please use setSecondaryOk() instead."); + this.setSecondaryOk(value); + }; + + DB.prototype.getSlaveOk = function() { + print( + "WARNING: getSlaveOk() is deprecated and may be removed in the next major release. Please use getSecondaryOk() instead."); + return this.getSecondaryOk(); + }; + + DB.prototype.setSecondaryOk = function(value) { if (value == undefined) value = true; - this._slaveOk = value; + this._secondaryOk = value; }; - DB.prototype.getSlaveOk = function() { - if (this._slaveOk != undefined) - return this._slaveOk; - return this._mongo.getSlaveOk(); + DB.prototype.getSecondaryOk = function() { + if (this._secondaryOk != undefined) + return this._secondaryOk; + return this._mongo.getSecondaryOk(); }; DB.prototype.getQueryOptions = function() { var options = 0; - if (this.getSlaveOk()) + if (this.getSecondaryOk()) options |= 4; return options; }; diff --git a/src/mongo/shell/mongo.js b/src/mongo/shell/mongo.js index ecd09f6bab3..bd79191af76 100644 --- a/src/mongo/shell/mongo.js +++ b/src/mongo/shell/mongo.js @@ -33,13 +33,25 @@ if (typeof mongoInject == "function") { } Mongo.prototype.setSlaveOk = function(value) { + print( + "WARNING: setSlaveOk() is deprecated and may be removed in the next major release. Please use setSecondaryOk() instead."); + this.setSecondaryOk(value); +}; + +Mongo.prototype.getSlaveOk = function() { + print( + "WARNING: getSlaveOk() is deprecated and may be removed in the next major release. Please use getSecondaryOk() instead."); + return this.getSecondaryOk(); +}; + +Mongo.prototype.setSecondaryOk = function(value) { if (value == undefined) value = true; - this.slaveOk = value; + this.secondaryOk = value; }; -Mongo.prototype.getSlaveOk = function() { - return this.slaveOk || false; +Mongo.prototype.getSecondaryOk = function() { + return this.secondaryOk || false; }; Mongo.prototype.getDB = function(name) { diff --git a/src/mongo/shell/replsettest.js b/src/mongo/shell/replsettest.js index 52573614e29..c089df69472 100644 --- a/src/mongo/shell/replsettest.js +++ b/src/mongo/shell/replsettest.js @@ -125,7 +125,7 @@ var ReplSetTest = function(opts) { var twoPrimaries = false; self.nodes.forEach(function(node) { try { - node.setSlaveOk(); + node.setSecondaryOk(); var n = node.getDB('admin').runCommand({ismaster: 1}); if (n.ismaster == true) { if (self.liveNodes.master) { @@ -1239,7 +1239,7 @@ var ReplSetTest = function(opts) { print("ReplSetTest awaitReplication: checking secondary #" + secondaryCount + ": " + slaveName); - slave.getDB("admin").getMongo().setSlaveOk(); + slave.getDB("admin").getMongo().setSecondaryOk(); var slaveOpTime; if (secondaryOpTimeType == ReplSetTest.OpTimeType.LAST_DURABLE) { diff --git a/src/mongo/shell/utils.js b/src/mongo/shell/utils.js index 4b5e59b9156..a7e991ad048 100644 --- a/src/mongo/shell/utils.js +++ b/src/mongo/shell/utils.js @@ -1296,7 +1296,7 @@ rs.help = function() { "\trs.freeze(secs) make a node ineligible to become primary for the time specified"); print( "\trs.remove(hostportstr) remove a host from the replica set (disconnects)"); - print("\trs.slaveOk() allow queries on secondary nodes"); + print("\trs.secondaryOk() allow queries on secondary nodes"); print(); print("\trs.printReplicationInfo() check oplog size and time range"); print( @@ -1307,8 +1307,15 @@ rs.help = function() { print("\tan error, even if the command succeeds."); }; rs.slaveOk = function(value) { - return db.getMongo().setSlaveOk(value); + print( + "WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead."); + return db.getMongo().setSecondaryOk(value); +}; + +rs.secondaryOk = function(value) { + return db.getMongo().setSecondaryOk(value); }; + rs.status = function() { return db._adminCommand("replSetGetStatus"); }; @@ -1436,7 +1443,7 @@ rs.debug = {}; rs.debug.nullLastOpWritten = function(primary, secondary) { var p = connect(primary + "/local"); var s = connect(secondary + "/local"); - s.getMongo().setSlaveOk(); + s.getMongo().setSecondaryOk(); var secondToLast = s.oplog.rs.find().sort({$natural: -1}).limit(1).next(); var last = p.runCommand({ @@ -1461,7 +1468,7 @@ rs.debug.getLastOpWritten = function(server) { if (server) { s = connect(server + "/local"); } - s.getMongo().setSlaveOk(); + s.getMongo().setSecondaryOk(); return s.oplog.rs.find().sort({$natural: -1}).limit(1).next(); }; |