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-08-24 20:59:10 +0000 |
commit | 45924f56e67166a9bacc2f76516a9bc1ae37b53e (patch) | |
tree | 8e7171d774b2cb1109aba2ab0836b450a18f8ef8 | |
parent | 4419a5c2a0d099b40b2155dd4def37802286cb7c (diff) | |
download | mongo-45924f56e67166a9bacc2f76516a9bc1ae37b53e.tar.gz |
SERVER-49990 Alias setSlaveOk() and getSlaveOk() shell helpers
(cherry picked from commit d6fe50035aff8026937dff9d8544ff213ad05152)
-rw-r--r-- | etc/backports_required_for_multiversion_tests.yml | 2 | ||||
-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 | 26 | ||||
-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 |
11 files changed, 119 insertions, 28 deletions
diff --git a/etc/backports_required_for_multiversion_tests.yml b/etc/backports_required_for_multiversion_tests.yml index f535f61fa47..b16fe6aabf9 100644 --- a/etc/backports_required_for_multiversion_tests.yml +++ b/etc/backports_required_for_multiversion_tests.yml @@ -95,6 +95,8 @@ all: test_file: jstests/replsets/get_replication_info_helper.js - ticket: SERVER-39621 test_file: jstests/replsets/step_down_chaining_disabled.js + - ticket: SERVER-49990 + test_file: jstests/replsets/secondaryOk_slaveOk_aliases.js suites: diff --git a/jstests/aggregation/bugs/server18198.js b/jstests/aggregation/bugs/server18198.js index 9aa26451161..ce703b49e9a 100644 --- a/jstests/aggregation/bugs/server18198.js +++ b/jstests/aggregation/bugs/server18198.js @@ -11,7 +11,7 @@ try { 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 61f38ce4b94..1d7b2c6f82a 100644 --- a/jstests/noPassthroughWithMongod/create_indexes_shell_helper.js +++ b/jstests/noPassthroughWithMongod/create_indexes_shell_helper.js @@ -12,7 +12,7 @@ try { writeMode: function() { return "commands"; }, - 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 12e8962a0a7..6ff888c1c8c 100644 --- a/jstests/noPassthroughWithMongod/default_read_pref.js +++ b/jstests/noPassthroughWithMongod/default_read_pref.js @@ -8,7 +8,7 @@ var mongo = db.getMongo(); 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 7a8bdea6a53..b06df8b8d90 100644 --- a/jstests/replsets/drain.js +++ b/jstests/replsets/drain.js @@ -66,7 +66,7 @@ assert.writeError(secondary.getDB("foo").flag.insert({sentinel: 2})); 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 @@ assert.eq(ErrorCodes.NotMasterNoSlaveOk, 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..99a34d682ea --- /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.commandWorked(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 4ff9cc8a014..5cd65907c45 100644 --- a/src/mongo/shell/collection.js +++ b/src/mongo/shell/collection.js @@ -1297,20 +1297,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 29cd3861290..c4d4af129c7 100644 --- a/src/mongo/shell/db.js +++ b/src/mongo/shell/db.js @@ -558,7 +558,7 @@ DB.prototype.help = function() { print("\tdb.getLastErrorObj() - return full status object"); print("\tdb.getLogComponents()"); print("\tdb.getMongo() get the server connection object"); - print("\tdb.getMongo().setSlaveOk() allow queries on a replication slave server"); + print("\tdb.getMongo().setSecondaryOk() allow queries on a replication secondary server"); print("\tdb.getName()"); print("\tdb.getProfilingLevel() - deprecated"); print("\tdb.getProfilingStatus() - returns if profiling is on and slow threshold"); @@ -1246,20 +1246,32 @@ DB.autocomplete = function(obj) { }; 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 db7733a1c79..decbdcaf553 100644 --- a/src/mongo/shell/mongo.js +++ b/src/mongo/shell/mongo.js @@ -28,13 +28,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 ecc14b01547..63be94d757c 100644 --- a/src/mongo/shell/replsettest.js +++ b/src/mongo/shell/replsettest.js @@ -143,7 +143,7 @@ var ReplSetTest = function(opts) { self.nodes.forEach(function(node) { try { if (!jsTestOptions().shouldSkipSettingSlaveOk) - node.setSlaveOk(); + node.setSecondaryOk(); var n = node.getDB('admin').runCommand({ismaster: 1}); self._liveNodes.push(node); // We verify that the node has a valid config by checking if n.me exists. Then, we @@ -1810,7 +1810,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 9b2f9dae9e1..5bf0a089e92 100644 --- a/src/mongo/shell/utils.js +++ b/src/mongo/shell/utils.js @@ -1467,7 +1467,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( @@ -1478,8 +1478,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"); }; @@ -1612,7 +1619,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({ @@ -1637,7 +1644,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(); }; |