diff options
author | Ali Mir <ali.mir@mongodb.com> | 2020-09-23 18:18:26 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-10-05 21:50:35 +0000 |
commit | b1e8378b3a37b4b98d675309fbb43e5e405a782e (patch) | |
tree | 6f3cac131b38f50def684b4a4ba5d2b932f830b2 /src/mongo | |
parent | 7e134d1450c1b55426cbe02a7d6bc1a556ab6eb5 (diff) | |
download | mongo-b1e8378b3a37b4b98d675309fbb43e5e405a782e.tar.gz |
SERVER-51106 Make the isMaster command a derived class of hello
(cherry picked from commit e178007c01565bf59d038f3be1566da036c60397)
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/commands/generic.cpp | 26 | ||||
-rw-r--r-- | src/mongo/db/repl/replication_info.cpp | 42 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_is_master_cmd.cpp | 30 |
3 files changed, 64 insertions, 34 deletions
diff --git a/src/mongo/db/commands/generic.cpp b/src/mongo/db/commands/generic.cpp index 2cc295f993f..786084fe6ac 100644 --- a/src/mongo/db/commands/generic.cpp +++ b/src/mongo/db/commands/generic.cpp @@ -31,7 +31,6 @@ #include "mongo/platform/basic.h" -#include "mongo/base/string_data.h" #include "mongo/bson/util/bson_extract.h" #include "mongo/bson/util/builder.h" #include "mongo/db/commands.h" @@ -52,8 +51,6 @@ using std::string; using std::stringstream; using std::vector; -constexpr auto kIsMasterString = "isMaster"_sd; - class PingCommand : public BasicCommand { public: PingCommand() : BasicCommand("ping") {} @@ -163,22 +160,19 @@ public: const BSONObj& cmdObj, BSONObjBuilder& result) { // Sort the command names before building the result BSON. - std::vector<std::string> commandNames; - const auto commandRegistry = globalCommandRegistry(); - for (const auto command : commandRegistry->allCommands()) { - // Don't show oldnames unless it's "isMaster". The output of the listCommands command - // must include "isMaster," even though it's an alias for the "hello" command, in order - // to preserve backwards compatibility with Ops Manager 4.4. - if (command.first == command.second->getName() || command.first == kIsMasterString) - commandNames.push_back(command.first); + std::vector<Command*> commands; + for (const auto command : globalCommandRegistry()->allCommands()) { + // Don't show oldnames + if (command.first == command.second->getName()) + commands.push_back(command.second); } - std::sort(commandNames.begin(), commandNames.end()); + std::sort(commands.begin(), commands.end(), [](Command* lhs, Command* rhs) { + return (lhs->getName()) < (rhs->getName()); + }); BSONObjBuilder b(result.subobjStart("commands")); - for (const auto& c : commandNames) { - const auto command = commandRegistry->findCommand(c); - auto name = (c == kIsMasterString) ? kIsMasterString : command->getName(); - BSONObjBuilder temp(b.subobjStart(name)); + for (const auto& command : commands) { + BSONObjBuilder temp(b.subobjStart(command->getName())); temp.append("help", command->help()); temp.append("slaveOk", command->secondaryAllowed(opCtx->getServiceContext()) == diff --git a/src/mongo/db/repl/replication_info.cpp b/src/mongo/db/repl/replication_info.cpp index fa701dc8af2..b42f20365b1 100644 --- a/src/mongo/db/repl/replication_info.cpp +++ b/src/mongo/db/repl/replication_info.cpp @@ -76,7 +76,6 @@ namespace repl { namespace { constexpr auto kHelloString = "hello"_sd; -// Aliases for the hello command in order to provide backwards compatibility. constexpr auto kCamelCaseIsMasterString = "isMaster"_sd; constexpr auto kLowerCaseIsMasterString = "ismaster"_sd; @@ -222,9 +221,9 @@ public: } } oplogInfoServerStatus; -class CmdHello final : public BasicCommand { +class CmdHello : public BasicCommand { public: - CmdHello() : BasicCommand(kHelloString, {kCamelCaseIsMasterString, kLowerCaseIsMasterString}) {} + CmdHello() : CmdHello(kHelloString, {}) {} bool requiresAuth() const final { return false; @@ -236,7 +235,7 @@ public: std::string help() const override { return "Check if this server is primary for a replica set\n" - "{ isMaster : 1 }"; + "{ hello : 1 }"; } bool supportsWriteConcern(const BSONObj& cmd) const final { @@ -262,11 +261,6 @@ public: LastError::get(opCtx->getClient()).disable(); } - // Parse the command name, which should be one of the following: hello, isMaster, or - // ismaster. If the command is "hello", we must attach an "isWritablePrimary" response field - // instead of "ismaster" and "secondaryDelaySecs" response field instead of "slaveDelay". - bool useLegacyResponseFields = (cmdObj.firstElementFieldNameStringData() != kHelloString); - transport::Session::TagMask sessionTagsToSet = 0; transport::Session::TagMask sessionTagsToUnset = 0; @@ -374,7 +368,7 @@ public: }); } - appendReplicationInfo(opCtx, result, 0, useLegacyResponseFields); + appendReplicationInfo(opCtx, result, 0, useLegacyResponseFields()); if (serverGlobalParams.clusterRole == ClusterRole::ConfigServer) { const int configServerModeNumber = 2; @@ -418,8 +412,36 @@ public: return true; } + +protected: + CmdHello(const StringData cmdName, const std::initializer_list<StringData>& alias) + : BasicCommand(cmdName, alias) {} + + virtual bool useLegacyResponseFields() { + return false; + } + } cmdhello; +class CmdIsMaster : public CmdHello { +public: + CmdIsMaster() : CmdHello(kCamelCaseIsMasterString, {kLowerCaseIsMasterString}) {} + + std::string help() const override { + return "Check if this server is primary for a replica set\n" + "{ isMaster : 1 }"; + } + +protected: + // Parse the command name, which should be one of the following: hello, isMaster, or + // ismaster. If the command is "hello", we must attach an "isWritablePrimary" response field + // instead of "ismaster" and "secondaryDelaySecs" response field instead of "slaveDelay". + bool useLegacyResponseFields() override { + return true; + } + +} cmdIsMaster; + OpCounterServerStatusSection replOpCounterServerStatusSection("opcountersRepl", &replOpCounters); } // namespace diff --git a/src/mongo/s/commands/cluster_is_master_cmd.cpp b/src/mongo/s/commands/cluster_is_master_cmd.cpp index 490605fbefe..4a7ecc1aa02 100644 --- a/src/mongo/s/commands/cluster_is_master_cmd.cpp +++ b/src/mongo/s/commands/cluster_is_master_cmd.cpp @@ -48,13 +48,12 @@ namespace mongo { namespace { constexpr auto kHelloString = "hello"_sd; -// Aliases for the hello command in order to provide backwards compatibility. constexpr auto kCamelCaseIsMasterString = "isMaster"_sd; constexpr auto kLowerCaseIsMasterString = "ismaster"_sd; class CmdHello : public BasicCommand { public: - CmdHello() : BasicCommand(kHelloString, {kCamelCaseIsMasterString, kLowerCaseIsMasterString}) {} + CmdHello() : CmdHello(kHelloString, {}) {} bool supportsWriteConcern(const BSONObj& cmd) const override { return false; @@ -84,11 +83,6 @@ public: BSONObjBuilder& result) override { CommandHelpers::handleMarkKillOnClientDisconnect(opCtx); - // Parse the command name, which should be one of the following: hello, isMaster, or - // ismaster. If the command is "hello", we must attach an "isWritablePrimary" response field - // instead of "ismaster". - bool useLegacyResponseFields = (cmdObj.firstElementFieldNameStringData() != kHelloString); - auto& clientMetadataIsMasterState = ClientMetadataIsMasterState::get(opCtx->getClient()); bool seenIsMaster = clientMetadataIsMasterState.hasSeenIsMaster(); if (!seenIsMaster) { @@ -118,7 +112,7 @@ public: opCtx->getClient(), std::move(swParseClientMetadata.getValue())); } - if (useLegacyResponseFields) { + if (useLegacyResponseFields()) { result.appendBool("ismaster", true); } else { result.appendBool("isWritablePrimary", true); @@ -151,7 +145,27 @@ public: return true; } +protected: + CmdHello(const StringData cmdName, const std::initializer_list<StringData>& alias) + : BasicCommand(cmdName, alias) {} + + virtual bool useLegacyResponseFields() { + return false; + } + } hello; +class CmdIsMaster : public CmdHello { + +public: + CmdIsMaster() : CmdHello(kCamelCaseIsMasterString, {kLowerCaseIsMasterString}) {} + +protected: + bool useLegacyResponseFields() override { + return true; + } + +} isMaster; + } // namespace } // namespace mongo |