diff options
author | Amirsaman Memaripour <amirsaman.memaripour@10gen.com> | 2020-01-23 17:14:41 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2020-01-23 17:14:41 +0000 |
commit | 3cd3a6da3fe0b6f022b721094bda0b97c3527d23 (patch) | |
tree | 05a14ef581b1250f81a5cc33c567edc0f4d074f1 /src/mongo/db/commands.cpp | |
parent | 85f4d7a449d5bb6eb5668ea53f727f591cc820ad (diff) | |
download | mongo-3cd3a6da3fe0b6f022b721094bda0b97c3527d23.tar.gz |
SERVER-45202 Improve command alias infrastructure
Diffstat (limited to 'src/mongo/db/commands.cpp')
-rw-r--r-- | src/mongo/db/commands.cpp | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/src/mongo/db/commands.cpp b/src/mongo/db/commands.cpp index b2c5fb809f8..8fc57f0746a 100644 --- a/src/mongo/db/commands.cpp +++ b/src/mongo/db/commands.cpp @@ -500,10 +500,11 @@ const OperationContext::Decoration<boost::optional<BSONArray>> errorLabelsOverri OperationContext::declareDecoration<boost::optional<BSONArray>>(); bool CommandHelpers::shouldActivateFailCommandFailPoint(const BSONObj& data, - StringData cmdName, - Client* client, - const NamespaceString& nss) { - if (cmdName == "configureFailPoint"_sd) // Banned even if in failCommands. + const CommandInvocation* invocation, + Client* client) { + const Command* cmd = invocation->definition(); + const NamespaceString& nss = invocation->ns(); + if (cmd->getName() == "configureFailPoint"_sd) // Banned even if in failCommands. return false; if (data.hasField("threadName") && @@ -528,7 +529,7 @@ bool CommandHelpers::shouldActivateFailCommandFailPoint(const BSONObj& data, } for (auto&& failCommand : data.getObjectField("failCommands")) { - if (failCommand.type() == String && failCommand.valueStringData() == cmdName) { + if (failCommand.type() == String && cmd->hasAlias(failCommand.valueStringData())) { return true; } } @@ -537,11 +538,11 @@ bool CommandHelpers::shouldActivateFailCommandFailPoint(const BSONObj& data, } void CommandHelpers::evaluateFailCommandFailPoint(OperationContext* opCtx, - StringData commandName, - const NamespaceString& nss) { + const CommandInvocation* invocation) { bool closeConnection; bool hasErrorCode; long long errorCode; + const Command* cmd = invocation->definition(); failCommand.executeIf( [&](const BSONObj& data) { if (data.hasField(kErrorLabelsFieldName) && @@ -555,13 +556,13 @@ void CommandHelpers::evaluateFailCommandFailPoint(OperationContext* opCtx, if (closeConnection) { opCtx->getClient()->session()->end(); - log() << "Failing command '" << commandName + log() << "Failing command '" << cmd->getName() << "' via 'failCommand' failpoint. Action: closing connection."; uasserted(50985, "Failing command due to 'failCommand' failpoint"); } if (hasErrorCode) { - log() << "Failing command '" << commandName + log() << "Failing command '" << cmd->getName() << "' via 'failCommand' failpoint. Action: returning error code " << errorCode << "."; uasserted(ErrorCodes::Error(errorCode), @@ -574,7 +575,7 @@ void CommandHelpers::evaluateFailCommandFailPoint(OperationContext* opCtx, closeConnection; hasErrorCode = data.hasField("errorCode") && bsonExtractIntegerField(data, "errorCode", &errorCode).isOK(); - return shouldActivateFailCommandFailPoint(data, commandName, opCtx->getClient(), nss) && + return shouldActivateFailCommandFailPoint(data, invocation, opCtx->getClient()) && (closeConnection || hasErrorCode); }); } @@ -718,11 +719,16 @@ std::unique_ptr<CommandInvocation> BasicCommandWithReplyBuilderInterface::parse( return std::make_unique<Invocation>(opCtx, request, this); } -Command::Command(StringData name, StringData oldName) +Command::Command(StringData name, std::vector<StringData> aliases) : _name(name.toString()), + _aliases(std::move(aliases)), _commandsExecutedMetric("commands." + _name + ".total", &_commandsExecuted), _commandsFailedMetric("commands." + _name + ".failed", &_commandsFailed) { - globalCommandRegistry()->registerCommand(this, name, oldName); + globalCommandRegistry()->registerCommand(this, _name, _aliases); +} + +bool Command::hasAlias(const StringData& alias) const { + return globalCommandRegistry()->findCommand(alias) == this; } Status BasicCommandWithReplyBuilderInterface::explain(OperationContext* opCtx, @@ -772,8 +778,12 @@ bool ErrmsgCommandDeprecated::run(OperationContext* opCtx, ////////////////////////////////////////////////////////////// // CommandRegistry -void CommandRegistry::registerCommand(Command* command, StringData name, StringData oldName) { - for (StringData key : {name, oldName}) { +void CommandRegistry::registerCommand(Command* command, + StringData name, + std::vector<StringData> aliases) { + aliases.push_back(name); + + for (auto key : aliases) { if (key.empty()) { continue; } |