summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2016-07-27 12:15:10 -0400
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2016-07-30 09:51:13 -0400
commit1af99e48949f36754e2b877f1057bdaba0b83aea (patch)
tree84e8f859b1544dfbf40860c80e1b2febf8b0c5bd
parentbde529a3eb87a984e8bc7b78cad98e13d0e04a6f (diff)
downloadmongo-1af99e48949f36754e2b877f1057bdaba0b83aea.tar.gz
SERVER-25050 Cleanup Strategy and Shard class
No functional changes, just cleanup the Strategy class and rename runBatchWriteCommand to runBatchWriteCommandOnConfig to better indicate its intent.
-rw-r--r--jstests/core/pseudocommand_db.js1
-rw-r--r--src/mongo/db/commands.h1
-rw-r--r--src/mongo/s/catalog/replset/sharding_catalog_client_impl.cpp10
-rw-r--r--src/mongo/s/client/shard.cpp7
-rw-r--r--src/mongo/s/client/shard.h10
-rw-r--r--src/mongo/s/commands/strategy.cpp133
-rw-r--r--src/mongo/s/commands/strategy.h3
-rw-r--r--src/mongo/s/s_only.cpp31
8 files changed, 92 insertions, 104 deletions
diff --git a/jstests/core/pseudocommand_db.js b/jstests/core/pseudocommand_db.js
index 3b7a82cd44d..bbe523f1a5b 100644
--- a/jstests/core/pseudocommand_db.js
+++ b/jstests/core/pseudocommand_db.js
@@ -5,5 +5,4 @@
// test that we can run the 'inprog' pseudocommand on any database.
assert.commandWorked(testDB.$cmd.sys.inprog.findOne());
}
-
})();
diff --git a/src/mongo/db/commands.h b/src/mongo/db/commands.h
index 252bb639a30..ef808084454 100644
--- a/src/mongo/db/commands.h
+++ b/src/mongo/db/commands.h
@@ -329,7 +329,6 @@ public:
// of Client. This will happen as part of SERVER-18292
static void execCommandClientBasic(OperationContext* txn,
Command* c,
- Client& client,
int queryOptions,
const char* ns,
BSONObj& cmdObj,
diff --git a/src/mongo/s/catalog/replset/sharding_catalog_client_impl.cpp b/src/mongo/s/catalog/replset/sharding_catalog_client_impl.cpp
index 59649d30e47..c59178d6d22 100644
--- a/src/mongo/s/catalog/replset/sharding_catalog_client_impl.cpp
+++ b/src/mongo/s/catalog/replset/sharding_catalog_client_impl.cpp
@@ -1316,8 +1316,8 @@ void ShardingCatalogClientImpl::writeConfigServerDirect(OperationContext* txn,
}
auto configShard = Grid::get(txn)->shardRegistry()->getConfigShard();
- *batchResponse =
- configShard->runBatchWriteCommand(txn, batchRequest, Shard::RetryPolicy::kNotIdempotent);
+ *batchResponse = configShard->runBatchWriteCommandOnConfig(
+ txn, batchRequest, Shard::RetryPolicy::kNotIdempotent);
}
Status ShardingCatalogClientImpl::insertConfigDocument(OperationContext* txn,
@@ -1340,7 +1340,7 @@ Status ShardingCatalogClientImpl::insertConfigDocument(OperationContext* txn,
auto configShard = Grid::get(txn)->shardRegistry()->getConfigShard();
for (int retry = 1; retry <= kMaxWriteRetry; retry++) {
auto response =
- configShard->runBatchWriteCommand(txn, request, Shard::RetryPolicy::kNoRetry);
+ configShard->runBatchWriteCommandOnConfig(txn, request, Shard::RetryPolicy::kNoRetry);
Status status = response.toStatus();
@@ -1422,7 +1422,7 @@ StatusWith<bool> ShardingCatalogClientImpl::updateConfigDocument(
auto configShard = Grid::get(txn)->shardRegistry()->getConfigShard();
auto response =
- configShard->runBatchWriteCommand(txn, request, Shard::RetryPolicy::kIdempotent);
+ configShard->runBatchWriteCommandOnConfig(txn, request, Shard::RetryPolicy::kIdempotent);
Status status = response.toStatus();
if (!status.isOK()) {
@@ -1454,7 +1454,7 @@ Status ShardingCatalogClientImpl::removeConfigDocuments(OperationContext* txn,
auto configShard = Grid::get(txn)->shardRegistry()->getConfigShard();
auto response =
- configShard->runBatchWriteCommand(txn, request, Shard::RetryPolicy::kIdempotent);
+ configShard->runBatchWriteCommandOnConfig(txn, request, Shard::RetryPolicy::kIdempotent);
return response.toStatus();
}
diff --git a/src/mongo/s/client/shard.cpp b/src/mongo/s/client/shard.cpp
index 2855aa26842..68721257b5b 100644
--- a/src/mongo/s/client/shard.cpp
+++ b/src/mongo/s/client/shard.cpp
@@ -120,9 +120,10 @@ StatusWith<Shard::CommandResponse> Shard::runCommand(OperationContext* txn,
MONGO_UNREACHABLE;
}
-BatchedCommandResponse Shard::runBatchWriteCommand(OperationContext* txn,
- const BatchedCommandRequest& batchRequest,
- RetryPolicy retryPolicy) {
+BatchedCommandResponse Shard::runBatchWriteCommandOnConfig(
+ OperationContext* txn, const BatchedCommandRequest& batchRequest, RetryPolicy retryPolicy) {
+ invariant(isConfig());
+
const std::string dbname = batchRequest.getNS().db().toString();
invariant(batchRequest.sizeWriteOps() == 1);
diff --git a/src/mongo/s/client/shard.h b/src/mongo/s/client/shard.h
index af4eb97fcd0..191935611e2 100644
--- a/src/mongo/s/client/shard.h
+++ b/src/mongo/s/client/shard.h
@@ -148,12 +148,12 @@ public:
RetryPolicy retryPolicy);
/**
- * Executes the specified batch write command on this shard's primary and retries on the
- * specified set of errors using the specified retry policy.
+ * Expects a single-entry batch wrtie command and runs it on the config server's primary using
+ * the specified retry policy.
*/
- BatchedCommandResponse runBatchWriteCommand(OperationContext* txn,
- const BatchedCommandRequest& batchRequest,
- RetryPolicy retryPolicy);
+ BatchedCommandResponse runBatchWriteCommandOnConfig(OperationContext* txn,
+ const BatchedCommandRequest& batchRequest,
+ RetryPolicy retryPolicy);
/**
* Warning: This method exhausts the cursor and pulls all data into memory.
diff --git a/src/mongo/s/commands/strategy.cpp b/src/mongo/s/commands/strategy.cpp
index d121c36346d..06040d20f60 100644
--- a/src/mongo/s/commands/strategy.cpp
+++ b/src/mongo/s/commands/strategy.cpp
@@ -108,7 +108,45 @@ void runAgainstRegistered(OperationContext* txn,
return;
}
- Command::execCommandClientBasic(txn, c, cc(), queryOptions, ns, jsobj, anObjBuilder);
+ Command::execCommandClientBasic(txn, c, queryOptions, ns, jsobj, anObjBuilder);
+}
+
+/**
+ * This code handles the $cmd.sys.inprog queries.
+ */
+bool handleSpecialNamespaces(OperationContext* txn, Request& request, const QueryMessage& q) {
+ const char* ns = strstr(request.getns(), ".$cmd.sys.");
+ if (!ns)
+ return false;
+
+ ns += 10;
+
+ BSONObjBuilder reply;
+
+ const auto upgradeToRealCommand = [txn, &q, &reply](StringData commandName) {
+ BSONObjBuilder cmdBob;
+ cmdBob.append(commandName, 1);
+ cmdBob.appendElements(q.query); // fields are validated by Commands
+ auto interposedCmd = cmdBob.done();
+
+ // Rewrite upgraded pseudoCommands to run on the 'admin' database.
+ const NamespaceString interposedNss("admin", "$cmd");
+ runAgainstRegistered(txn, interposedNss.ns().c_str(), interposedCmd, reply, q.queryOptions);
+ };
+
+ if (strcmp(ns, "inprog") == 0) {
+ upgradeToRealCommand("currentOp");
+ } else if (strcmp(ns, "killop") == 0) {
+ upgradeToRealCommand("killOp");
+ } else if (strcmp(ns, "unlock") == 0) {
+ reply.append("err", "can't do unlock through mongos");
+ } else {
+ warning() << "unknown sys command [" << ns << "]";
+ return false;
+ }
+
+ replyToQuery(0, request.session(), request.m(), reply.done());
+ return true;
}
} // namespace
@@ -224,7 +262,7 @@ void Strategy::queryOp(OperationContext* txn, Request& request) {
}
void Strategy::clientCommandOp(OperationContext* txn, Request& request) {
- QueryMessage q(request.d());
+ const QueryMessage q(request.d());
LOG(3) << "command: " << q.ns << " " << redact(q.query) << " ntoreturn: " << q.ntoreturn
<< " options: " << q.queryOptions;
@@ -235,44 +273,40 @@ void Strategy::clientCommandOp(OperationContext* txn, Request& request) {
" " + q.query.toString());
}
- NamespaceString nss(request.getns());
- // Regular queries are handled in strategy_shard.cpp
- verify(nss.isCommand() || nss.isSpecialCommand());
+ const NamespaceString nss(request.getns());
+ invariant(nss.isCommand() || nss.isSpecialCommand());
if (handleSpecialNamespaces(txn, request, q))
return;
+ BSONObj cmdObj = q.query;
+
+ {
+ BSONElement e = cmdObj.firstElement();
+ if (e.type() == Object && (e.fieldName()[0] == '$' ? str::equals("query", e.fieldName() + 1)
+ : str::equals("query", e.fieldName()))) {
+ // Extract the embedded query object.
+ if (cmdObj.hasField(Query::ReadPrefField.name())) {
+ // The command has a read preference setting. We don't want to lose this information
+ // so we copy this to a new field called $queryOptions.$readPreference
+ BSONObjBuilder finalCmdObjBuilder;
+ finalCmdObjBuilder.appendElements(e.embeddedObject());
+
+ BSONObjBuilder queryOptionsBuilder(finalCmdObjBuilder.subobjStart("$queryOptions"));
+ queryOptionsBuilder.append(cmdObj[Query::ReadPrefField.name()]);
+ queryOptionsBuilder.done();
+
+ cmdObj = finalCmdObjBuilder.obj();
+ } else {
+ cmdObj = e.embeddedObject();
+ }
+ }
+ }
+
int loops = 5;
while (true) {
try {
- BSONObj cmdObj = q.query;
- {
- BSONElement e = cmdObj.firstElement();
- if (e.type() == Object &&
- (e.fieldName()[0] == '$' ? str::equals("query", e.fieldName() + 1)
- : str::equals("query", e.fieldName()))) {
- // Extract the embedded query object.
-
- if (cmdObj.hasField(Query::ReadPrefField.name())) {
- // The command has a read preference setting. We don't want
- // to lose this information so we copy this to a new field
- // called $queryOptions.$readPreference
- BSONObjBuilder finalCmdObjBuilder;
- finalCmdObjBuilder.appendElements(e.embeddedObject());
-
- BSONObjBuilder queryOptionsBuilder(
- finalCmdObjBuilder.subobjStart("$queryOptions"));
- queryOptionsBuilder.append(cmdObj[Query::ReadPrefField.name()]);
- queryOptionsBuilder.done();
-
- cmdObj = finalCmdObjBuilder.obj();
- } else {
- cmdObj = e.embeddedObject();
- }
- }
- }
-
OpQueryReplyBuilder reply;
{
BSONObjBuilder builder(reply.bufBuilderForResults());
@@ -307,41 +341,6 @@ void Strategy::clientCommandOp(OperationContext* txn, Request& request) {
}
}
-// TODO: remove after MongoDB 3.2
-bool Strategy::handleSpecialNamespaces(OperationContext* txn, Request& request, QueryMessage& q) {
- const char* ns = strstr(request.getns(), ".$cmd.sys.");
- if (!ns)
- return false;
- ns += 10;
-
- BSONObjBuilder reply;
-
- const auto upgradeToRealCommand = [txn, &q, &reply](StringData commandName) {
- BSONObjBuilder cmdBob;
- cmdBob.append(commandName, 1);
- cmdBob.appendElements(q.query); // fields are validated by Commands
- auto interposedCmd = cmdBob.done();
- // Rewrite upgraded pseudoCommands to run on the 'admin' database.
- NamespaceString interposedNss("admin", "$cmd");
- runAgainstRegistered(txn, interposedNss.ns().c_str(), interposedCmd, reply, q.queryOptions);
- };
-
- if (strcmp(ns, "inprog") == 0) {
- upgradeToRealCommand("currentOp");
- } else if (strcmp(ns, "killop") == 0) {
- upgradeToRealCommand("killOp");
- } else if (strcmp(ns, "unlock") == 0) {
- reply.append("err", "can't do unlock through mongos");
- } else {
- warning() << "unknown sys command [" << ns << "]";
- return false;
- }
-
- BSONObj x = reply.done();
- replyToQuery(0, request.session(), request.m(), x);
- return true;
-}
-
void Strategy::commandOp(OperationContext* txn,
const string& db,
const BSONObj& command,
diff --git a/src/mongo/s/commands/strategy.h b/src/mongo/s/commands/strategy.h
index f45031e2e0f..91031dee935 100644
--- a/src/mongo/s/commands/strategy.h
+++ b/src/mongo/s/commands/strategy.h
@@ -102,9 +102,6 @@ public:
* DEPRECATED: should not be used by new code.
*/
static void clientCommandOp(OperationContext* txn, Request& request);
-
-protected:
- static bool handleSpecialNamespaces(OperationContext* txn, Request& request, QueryMessage& q);
};
} // namespace mongo
diff --git a/src/mongo/s/s_only.cpp b/src/mongo/s/s_only.cpp
index 56d4238f3bc..d29eb7de7a4 100644
--- a/src/mongo/s/s_only.cpp
+++ b/src/mongo/s/s_only.cpp
@@ -1,6 +1,5 @@
-// s_only.cpp
-
-/* Copyright 2009 10gen Inc.
+/**
+ * Copyright (C) 2009-2016 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
@@ -18,13 +17,13 @@
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
- * must comply with the GNU Affero General Public License in all respects
- * for all of the code used other than as permitted herein. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you do not
- * wish to do so, delete this exception statement from your version. If you
- * delete this exception statement from all source files in the program,
- * then also delete it in the license file.
+ * must comply with the GNU Affero General Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kSharding
@@ -83,20 +82,14 @@ void Command::execCommand(OperationContext* txn,
std::string db = request.getDatabase().rawData();
BSONObjBuilder result;
- execCommandClientBasic(txn,
- command,
- *txn->getClient(),
- queryFlags,
- request.getDatabase().rawData(),
- cmdObj,
- result);
+ execCommandClientBasic(
+ txn, command, queryFlags, request.getDatabase().rawData(), cmdObj, result);
replyBuilder->setCommandReply(result.done()).setMetadata(rpc::makeEmptyMetadata());
}
void Command::execCommandClientBasic(OperationContext* txn,
Command* c,
- Client& client,
int queryOptions,
const char* ns,
BSONObj& cmdObj,
@@ -112,7 +105,7 @@ void Command::execCommandClientBasic(OperationContext* txn,
return;
}
- Status status = _checkAuthorization(c, &client, dbname, cmdObj);
+ Status status = _checkAuthorization(c, txn->getClient(), dbname, cmdObj);
if (!status.isOK()) {
appendCommandStatus(result, status);
return;