summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlake Oler <blake.oler@mongodb.com>2020-01-02 19:23:49 +0000
committerevergreen <evergreen@mongodb.com>2020-01-02 19:23:49 +0000
commit4f81376f5584638da9610c60260ab6e353607e74 (patch)
tree1e501bf403db6176ab9f2f13efa918333e3da27f
parent4019b32248c7ceb767d15be1e8b78874b4f7ccb7 (diff)
downloadmongo-4f81376f5584638da9610c60260ab6e353607e74.tar.gz
SERVER-43310 Always route a sharded cluster's createIndexes call through a replica set's primary node
(cherry picked from commit bbca8dbaf5a32ece935d2a14a8d7f94da41378e3)
-rw-r--r--buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml2
-rw-r--r--jstests/sharding/cluster_create_indexes_always_routes_through_primary.js20
-rw-r--r--src/mongo/db/sessions_collection_config_server.cpp2
-rw-r--r--src/mongo/s/commands/cluster_create_indexes_cmd.cpp2
4 files changed, 24 insertions, 2 deletions
diff --git a/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml b/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml
index 7a8f821bacd..03701533b38 100644
--- a/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml
+++ b/buildscripts/resmokeconfig/suites/sharding_last_stable_mongos_and_mixed_shards.yml
@@ -146,6 +146,8 @@ selector:
- jstests/sharding/enable_sharding_with_primary.js
# Enable when SERVER-44733 is backported
- jstests/sharding/change_streams_update_lookup_shard_metadata_missing.js
+ # Enable if SERVER-43310 is backported to 4.0
+ - jstests/sharding/cluster_create_indexes_always_routes_through_primary.js
executor:
config:
diff --git a/jstests/sharding/cluster_create_indexes_always_routes_through_primary.js b/jstests/sharding/cluster_create_indexes_always_routes_through_primary.js
new file mode 100644
index 00000000000..6c661e0abac
--- /dev/null
+++ b/jstests/sharding/cluster_create_indexes_always_routes_through_primary.js
@@ -0,0 +1,20 @@
+// Ensure that a call to createIndexes in a sharded cluster will route to the primary, even when
+// setSlaveOk() is set to true.
+(function() {
+'use strict';
+
+let st = new ShardingTest({shards: {rs0: {nodes: 2}}});
+const testDBName = jsTestName();
+const collName = 'coll';
+const testDB = st.s.getDB(testDBName);
+
+assert.commandWorked(testDB.adminCommand({enableSharding: testDBName}));
+assert.commandWorked(
+ testDB.adminCommand({shardCollection: testDB[collName].getFullName(), key: {x: 1}}));
+
+st.s.setSlaveOk(true);
+assert.commandWorked(
+ testDB.runCommand({createIndexes: collName, indexes: [{key: {a: 1}, name: "index"}]}));
+
+st.stop();
+})();
diff --git a/src/mongo/db/sessions_collection_config_server.cpp b/src/mongo/db/sessions_collection_config_server.cpp
index f89916aace2..f04bef414ce 100644
--- a/src/mongo/db/sessions_collection_config_server.cpp
+++ b/src/mongo/db/sessions_collection_config_server.cpp
@@ -82,7 +82,7 @@ Status SessionsCollectionConfigServer::_generateIndexesIfNeeded(OperationContext
scatterGatherOnlyVersionIfUnsharded(opCtx,
NamespaceString::kLogicalSessionsNamespace,
SessionsCollection::generateCreateIndexesCmd(),
- ReadPreferenceSetting::get(opCtx),
+ ReadPreferenceSetting(ReadPreference::PrimaryOnly),
Shard::RetryPolicy::kNoRetry);
return Status::OK();
} catch (const DBException& ex) {
diff --git a/src/mongo/s/commands/cluster_create_indexes_cmd.cpp b/src/mongo/s/commands/cluster_create_indexes_cmd.cpp
index 31e0d36625a..c0fad9aa46d 100644
--- a/src/mongo/s/commands/cluster_create_indexes_cmd.cpp
+++ b/src/mongo/s/commands/cluster_create_indexes_cmd.cpp
@@ -77,7 +77,7 @@ public:
opCtx,
nss,
CommandHelpers::filterCommandRequestForPassthrough(cmdObj),
- ReadPreferenceSetting::get(opCtx),
+ ReadPreferenceSetting(ReadPreference::PrimaryOnly),
Shard::RetryPolicy::kNoRetry,
{ErrorCodes::CannotImplicitlyCreateCollection});