summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAli Mir <ali.mir@mongodb.com>2020-09-23 18:18:26 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-10-05 21:45:34 +0000
commit02ea85d348d4f03459fc2661f76992fff4ce48a7 (patch)
tree2a3f834ca4c01561c61111b0e77194f68014784f
parent92154af191d67409ab3cf3e0147e7299c63e1216 (diff)
downloadmongo-r4.0.21-rc0.tar.gz
SERVER-51106 Make the isMaster command a derived class of hellor4.0.21-rc0
(cherry picked from commit e178007c01565bf59d038f3be1566da036c60397)
-rw-r--r--src/mongo/db/commands/generic.cpp26
-rw-r--r--src/mongo/db/repl/replication_info.cpp40
-rw-r--r--src/mongo/s/commands/cluster_is_master_cmd.cpp30
3 files changed, 63 insertions, 33 deletions
diff --git a/src/mongo/db/commands/generic.cpp b/src/mongo/db/commands/generic.cpp
index 6b8e392f06b..b155d8f3b78 100644
--- a/src/mongo/db/commands/generic.cpp
+++ b/src/mongo/db/commands/generic.cpp
@@ -32,7 +32,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"
@@ -53,8 +52,6 @@ using std::string;
using std::stringstream;
using std::vector;
-constexpr auto kIsMasterString = "isMaster"_sd;
-
class PingCommand : public BasicCommand {
public:
PingCommand() : BasicCommand("ping") {}
@@ -156,22 +153,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 de095d7e48d..745a33659e5 100644
--- a/src/mongo/db/repl/replication_info.cpp
+++ b/src/mongo/db/repl/replication_info.cpp
@@ -76,7 +76,6 @@ namespace {
MONGO_FAIL_POINT_DEFINE(impersonateFullyUpgradedFutureVersion);
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;
@@ -221,7 +220,7 @@ public:
class CmdHello : public BasicCommand {
public:
- CmdHello() : BasicCommand(kHelloString, {kCamelCaseIsMasterString, kLowerCaseIsMasterString}) {}
+ CmdHello() : CmdHello(kHelloString, {}) {}
bool requiresAuth() const override {
return false;
@@ -231,7 +230,7 @@ public:
}
std::string help() const override {
return "Check if this server is primary for a replica set\n"
- "{ isMaster : 1 }";
+ "{ hello : 1 }";
}
virtual bool supportsWriteConcern(const BSONObj& cmd) const override {
return false;
@@ -250,11 +249,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.firstElementFieldName() != kHelloString);
-
transport::Session::TagMask sessionTagsToSet = 0;
transport::Session::TagMask sessionTagsToUnset = 0;
@@ -363,7 +357,7 @@ public:
});
}
- appendReplicationInfo(opCtx, result, 0, useLegacyResponseFields);
+ appendReplicationInfo(opCtx, result, 0, useLegacyResponseFields());
if (serverGlobalParams.clusterRole == ClusterRole::ConfigServer) {
const int configServerModeNumber = 2;
@@ -409,8 +403,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 fa5257b1fd5..4e67f31ee95 100644
--- a/src/mongo/s/commands/cluster_is_master_cmd.cpp
+++ b/src/mongo/s/commands/cluster_is_master_cmd.cpp
@@ -50,14 +50,13 @@ 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;
@@ -85,11 +84,6 @@ public:
const std::string& dbname,
const BSONObj& cmdObj,
BSONObjBuilder& result) override {
- // 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.firstElementFieldName() != kHelloString);
-
auto& clientMetadataIsMasterState = ClientMetadataIsMasterState::get(opCtx->getClient());
bool seenIsMaster = clientMetadataIsMasterState.hasSeenIsMaster();
if (!seenIsMaster) {
@@ -119,7 +113,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