From f7e7e994cb14bf349ea1ce9a86ec6012b7330b66 Mon Sep 17 00:00:00 2001 From: Samy Lanka Date: Thu, 2 Apr 2020 18:56:20 -0400 Subject: SERVER-44343 Make 'reIndex' a standalone-only command --- src/mongo/db/commands/drop_indexes.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/mongo/db/commands/drop_indexes.cpp') diff --git a/src/mongo/db/commands/drop_indexes.cpp b/src/mongo/db/commands/drop_indexes.cpp index 1b48d355e5c..38266a3cf48 100644 --- a/src/mongo/db/commands/drop_indexes.cpp +++ b/src/mongo/db/commands/drop_indexes.cpp @@ -100,13 +100,16 @@ public: class CmdReIndex : public ErrmsgCommandDeprecated { public: AllowedOnSecondary secondaryAllowed(ServiceContext*) const override { - return AllowedOnSecondary::kAlways; // can reindex on a secondary + // Even though reIndex is a standalone-only command, this will return that the command is + // allowed on secondaries so that it will fail with a more useful error message to the user + // rather than with a NotMaster error. + return AllowedOnSecondary::kAlways; } virtual bool supportsWriteConcern(const BSONObj& cmd) const override { return false; } std::string help() const override { - return "re-index a collection"; + return "re-index a collection (can only be run on a standalone mongod)"; } virtual void addRequiredPrivileges(const std::string& dbname, const BSONObj& cmdObj, @@ -128,6 +131,15 @@ public: LOGV2(20457, "CMD: reIndex {toReIndexNss}", "toReIndexNss"_attr = toReIndexNss); + if (repl::ReplicationCoordinator::get(opCtx)->getReplicationMode() != + repl::ReplicationCoordinator::modeNone) { + uasserted( + ErrorCodes::IllegalOperation, + str::stream() + << "reIndex is only allowed on a standalone mongod instance. Cannot reIndex '" + << toReIndexNss << "' while replication is active"); + } + AutoGetCollection autoColl(opCtx, toReIndexNss, MODE_X); Collection* collection = autoColl.getCollection(); if (!collection) { -- cgit v1.2.1