summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2015-06-18 16:32:05 -0400
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2015-06-19 12:28:39 -0400
commit79a9091df8f56bed80c9a53c1cf943e76dc4c8d8 (patch)
tree1a6c922a1bacc549cccacf9c1361185a07f94a40 /src/mongo
parent77ed46e9b5dacf6f97ac018cb0e129e9b035bf26 (diff)
downloadmongo-79a9091df8f56bed80c9a53c1cf943e76dc4c8d8.tar.gz
SERVER-18062 Implicitly create database for createIndexes
Also moves some commands out of commands_public.cpp in an effort to get rid of this file.
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/s/commands/SConscript2
-rw-r--r--src/mongo/s/commands/cluster_db_stats_cmd.cpp108
-rw-r--r--src/mongo/s/commands/cluster_repair_database_cmd.cpp51
-rw-r--r--src/mongo/s/commands/commands_public.cpp88
-rw-r--r--src/mongo/s/commands/run_on_all_shards_cmd.cpp20
-rw-r--r--src/mongo/s/commands/run_on_all_shards_cmd.h10
6 files changed, 187 insertions, 92 deletions
diff --git a/src/mongo/s/commands/SConscript b/src/mongo/s/commands/SConscript
index 90d4bf2ea83..efaf42ffc34 100644
--- a/src/mongo/s/commands/SConscript
+++ b/src/mongo/s/commands/SConscript
@@ -21,6 +21,7 @@ env.Library(
'cluster_commands_common.cpp',
'cluster_count_cmd.cpp',
'cluster_current_op.cpp',
+ 'cluster_db_stats_cmd.cpp',
'cluster_drop_database_cmd.cpp',
'cluster_enable_sharding_cmd.cpp',
'cluster_explain_cmd.cpp',
@@ -46,6 +47,7 @@ env.Library(
'cluster_plan_cache_cmd.cpp',
'cluster_profile_cmd.cpp',
'cluster_remove_shard_cmd.cpp',
+ 'cluster_repair_database_cmd.cpp',
'cluster_repl_set_get_status_cmd.cpp',
'cluster_reset_error_cmd.cpp',
'cluster_shard_collection_cmd.cpp',
diff --git a/src/mongo/s/commands/cluster_db_stats_cmd.cpp b/src/mongo/s/commands/cluster_db_stats_cmd.cpp
new file mode 100644
index 00000000000..33f804175aa
--- /dev/null
+++ b/src/mongo/s/commands/cluster_db_stats_cmd.cpp
@@ -0,0 +1,108 @@
+/**
+ * Copyright (C) 2015 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,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * 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.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include <vector>
+
+#include "mongo/s/commands/run_on_all_shards_cmd.h"
+
+namespace mongo {
+namespace {
+
+ using std::vector;
+
+ class DBStatsCmd : public RunOnAllShardsCommand {
+ public:
+ DBStatsCmd() : RunOnAllShardsCommand("dbStats", "dbstats") {}
+ virtual void addRequiredPrivileges(const std::string& dbname,
+ const BSONObj& cmdObj,
+ std::vector<Privilege>* out) {
+ ActionSet actions;
+ actions.addAction(ActionType::dbStats);
+ out->push_back(Privilege(ResourcePattern::forDatabaseName(dbname), actions));
+ }
+
+ virtual void aggregateResults(const vector<ShardAndReply>& results,
+ BSONObjBuilder& output) {
+ long long objects = 0;
+ long long unscaledDataSize = 0;
+ long long dataSize = 0;
+ long long storageSize = 0;
+ long long numExtents = 0;
+ long long indexes = 0;
+ long long indexSize = 0;
+ long long fileSize = 0;
+
+ long long freeListNum = 0;
+ long long freeListSize = 0;
+
+ for (const ShardAndReply& shardAndReply : results) {
+ const BSONObj& b = std::get<1>(shardAndReply);
+
+ objects += b["objects"].numberLong();
+ unscaledDataSize += b["avgObjSize"].numberLong() * b["objects"].numberLong();
+ dataSize += b["dataSize"].numberLong();
+ storageSize += b["storageSize"].numberLong();
+ numExtents += b["numExtents"].numberLong();
+ indexes += b["indexes"].numberLong();
+ indexSize += b["indexSize"].numberLong();
+ fileSize += b["fileSize"].numberLong();
+
+ if (b["extentFreeList"].isABSONObj()) {
+ freeListNum += b["extentFreeList"].Obj()["num"].numberLong();
+ freeListSize += b["extentFreeList"].Obj()["totalSize"].numberLong();
+ }
+ }
+
+ // result.appendNumber( "collections" , ncollections ); //TODO: need to find a good way to get this
+ output.appendNumber("objects", objects);
+
+ // avgObjSize on mongod is not scaled based on the argument to db.stats(), so we use
+ // unscaledDataSize here for consistency. See SERVER-7347.
+ output.append("avgObjSize", objects == 0 ? 0 :
+ double(unscaledDataSize) / double(objects));
+ output.appendNumber("dataSize", dataSize);
+ output.appendNumber("storageSize", storageSize);
+ output.appendNumber("numExtents", numExtents);
+ output.appendNumber("indexes", indexes);
+ output.appendNumber("indexSize", indexSize);
+ output.appendNumber("fileSize", fileSize);
+
+ {
+ BSONObjBuilder extentFreeList(output.subobjStart("extentFreeList"));
+ extentFreeList.appendNumber("num", freeListNum);
+ extentFreeList.appendNumber("totalSize", freeListSize);
+ extentFreeList.done();
+ }
+ }
+
+ } clusterDBStatsCmd;
+
+} // namespace
+} // namespace mongo
diff --git a/src/mongo/s/commands/cluster_repair_database_cmd.cpp b/src/mongo/s/commands/cluster_repair_database_cmd.cpp
new file mode 100644
index 00000000000..33c849ecde6
--- /dev/null
+++ b/src/mongo/s/commands/cluster_repair_database_cmd.cpp
@@ -0,0 +1,51 @@
+/**
+ * Copyright (C) 2015 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,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * 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.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/s/commands/run_on_all_shards_cmd.h"
+
+namespace mongo {
+namespace {
+
+ class ClusterRepairDatabaseCmd : public RunOnAllShardsCommand {
+ public:
+ ClusterRepairDatabaseCmd() : RunOnAllShardsCommand("repairDatabase") {}
+
+ virtual void addRequiredPrivileges(const std::string& dbname,
+ const BSONObj& cmdObj,
+ std::vector<Privilege>* out) {
+ ActionSet actions;
+ actions.addAction(ActionType::repairDatabase);
+ out->push_back(Privilege(ResourcePattern::forDatabaseName(dbname), actions));
+ }
+
+ } clusterRepairDatabaseCmd;
+
+} // namespace
+} // namespace mongo
diff --git a/src/mongo/s/commands/commands_public.cpp b/src/mongo/s/commands/commands_public.cpp
index c86db230532..6f0d57bfdd8 100644
--- a/src/mongo/s/commands/commands_public.cpp
+++ b/src/mongo/s/commands/commands_public.cpp
@@ -32,7 +32,6 @@
#include "mongo/platform/basic.h"
-
#include "mongo/client/connpool.h"
#include "mongo/client/parallel.h"
#include "mongo/db/auth/action_set.h"
@@ -61,7 +60,6 @@
#include "mongo/s/version_manager.h"
#include "mongo/scripting/engine.h"
#include "mongo/util/log.h"
-#include "mongo/util/net/message.h"
#include "mongo/util/timer.h"
namespace mongo {
@@ -133,8 +131,10 @@ namespace mongo {
public:
AllShardsCollectionCommand(const char* n,
const char* oldname = NULL,
- bool useShardConn = false):
- RunOnAllShardsCommand(n, oldname, useShardConn) {
+ bool useShardConn = false,
+ bool implicitCreateDb = false)
+ : RunOnAllShardsCommand(n, oldname, useShardConn, implicitCreateDb) {
+
}
virtual void getShardIds(const string& dbName,
@@ -202,7 +202,8 @@ namespace mongo {
CreateIndexesCmd():
AllShardsCollectionCommand("createIndexes",
NULL, /* oldName */
- true /* use ShardConnection */) {
+ true /* use ShardConnection */,
+ true /* implicit create db */) {
// createIndexes command should use ShardConnection so the getLastError would
// be able to properly enforce the write concern (via the saveGLEStats callback).
}
@@ -370,83 +371,6 @@ namespace mongo {
}
} validateCmd;
- class RepairDatabaseCmd : public RunOnAllShardsCommand {
- public:
- RepairDatabaseCmd() : RunOnAllShardsCommand("repairDatabase") {}
- virtual void addRequiredPrivileges(const std::string& dbname,
- const BSONObj& cmdObj,
- std::vector<Privilege>* out) {
- ActionSet actions;
- actions.addAction(ActionType::repairDatabase);
- out->push_back(Privilege(ResourcePattern::forDatabaseName(dbname), actions));
- }
- } repairDatabaseCmd;
-
- class DBStatsCmd : public RunOnAllShardsCommand {
- public:
- DBStatsCmd() : RunOnAllShardsCommand("dbStats", "dbstats") {}
- virtual void addRequiredPrivileges(const std::string& dbname,
- const BSONObj& cmdObj,
- std::vector<Privilege>* out) {
- ActionSet actions;
- actions.addAction(ActionType::dbStats);
- out->push_back(Privilege(ResourcePattern::forDatabaseName(dbname), actions));
- }
-
- virtual void aggregateResults(const vector<ShardAndReply>& results,
- BSONObjBuilder& output) {
- long long objects = 0;
- long long unscaledDataSize = 0;
- long long dataSize = 0;
- long long storageSize = 0;
- long long numExtents = 0;
- long long indexes = 0;
- long long indexSize = 0;
- long long fileSize = 0;
-
- long long freeListNum = 0;
- long long freeListSize = 0;
-
- for (vector<ShardAndReply>::const_iterator it(results.begin()), end(results.end());
- it != end; ++it) {
- const BSONObj& b = std::get<1>(*it);
- objects += b["objects"].numberLong();
- unscaledDataSize += b["avgObjSize"].numberLong() * b["objects"].numberLong();
- dataSize += b["dataSize"].numberLong();
- storageSize += b["storageSize"].numberLong();
- numExtents += b["numExtents"].numberLong();
- indexes += b["indexes"].numberLong();
- indexSize += b["indexSize"].numberLong();
- fileSize += b["fileSize"].numberLong();
-
- if ( b["extentFreeList"].isABSONObj() ) {
- freeListNum += b["extentFreeList"].Obj()["num"].numberLong();
- freeListSize += b["extentFreeList"].Obj()["totalSize"].numberLong();
- }
- }
-
- //result.appendNumber( "collections" , ncollections ); //TODO: need to find a good way to get this
- output.appendNumber( "objects" , objects );
- /* avgObjSize on mongod is not scaled based on the argument to db.stats(), so we use
- * unscaledDataSize here for consistency. See SERVER-7347. */
- output.append ( "avgObjSize" , objects == 0 ? 0 : double(unscaledDataSize) /
- double(objects) );
- output.appendNumber( "dataSize" , dataSize );
- output.appendNumber( "storageSize" , storageSize);
- output.appendNumber( "numExtents" , numExtents );
- output.appendNumber( "indexes" , indexes );
- output.appendNumber( "indexSize" , indexSize );
- output.appendNumber( "fileSize" , fileSize );
-
- {
- BSONObjBuilder extentFreeList( output.subobjStart( "extentFreeList" ) );
- extentFreeList.appendNumber( "num", freeListNum );
- extentFreeList.appendNumber( "totalSize", freeListSize );
- extentFreeList.done();
- }
- }
- } DBStatsCmdObj;
-
class CreateCmd : public PublicGridCommand {
public:
CreateCmd() : PublicGridCommand( "create" ) {}
diff --git a/src/mongo/s/commands/run_on_all_shards_cmd.cpp b/src/mongo/s/commands/run_on_all_shards_cmd.cpp
index bb879415bdf..68e9629227a 100644
--- a/src/mongo/s/commands/run_on_all_shards_cmd.cpp
+++ b/src/mongo/s/commands/run_on_all_shards_cmd.cpp
@@ -35,7 +35,6 @@
#include <list>
#include <set>
-
#include "mongo/db/jsobj.h"
#include "mongo/client/parallel.h"
#include "mongo/s/client/shard.h"
@@ -47,10 +46,13 @@ namespace mongo {
RunOnAllShardsCommand::RunOnAllShardsCommand(const char* name,
const char* oldName,
- bool useShardConn)
- : Command(name, false, oldName)
- , _useShardConn(useShardConn)
- {}
+ bool useShardConn,
+ bool implicitCreateDb)
+ : Command(name, false, oldName),
+ _useShardConn(useShardConn),
+ _implicitCreateDb(implicitCreateDb) {
+
+ }
void RunOnAllShardsCommand::aggregateResults(const std::vector<ShardAndReply>& results,
BSONObjBuilder& output)
@@ -77,11 +79,15 @@ namespace mongo {
BSONObjBuilder& output) {
LOG(1) << "RunOnAllShardsCommand db: " << dbName << " cmd:" << cmdObj;
+
+ if (_implicitCreateDb) {
+ uassertStatusOK(grid.implicitCreateDb(dbName));
+ }
+
std::vector<ShardId> shardIds;
getShardIds(dbName, cmdObj, shardIds);
- // TODO: Future is deprecated, replace with commandOp()
- std::list< std::shared_ptr<Future::CommandResult> > futures;
+ std::list<std::shared_ptr<Future::CommandResult>> futures;
for (const ShardId& shardId : shardIds) {
const auto shard = grid.shardRegistry()->getShard(shardId);
if (!shard) {
diff --git a/src/mongo/s/commands/run_on_all_shards_cmd.h b/src/mongo/s/commands/run_on_all_shards_cmd.h
index 77c825bd1f6..bc0b6f22084 100644
--- a/src/mongo/s/commands/run_on_all_shards_cmd.h
+++ b/src/mongo/s/commands/run_on_all_shards_cmd.h
@@ -52,10 +52,10 @@ namespace mongo {
*/
class RunOnAllShardsCommand : public Command {
public:
-
RunOnAllShardsCommand(const char* name,
const char* oldName=NULL,
- bool useShardConn=false);
+ bool useShardConn=false,
+ bool implicitCreateDb=false);
bool slaveOk() const override { return true; }
bool adminOnly() const override { return false; }
@@ -87,7 +87,11 @@ namespace mongo {
BSONObjBuilder& output) final;
private:
- bool _useShardConn; // use ShardConnection as opposed to ScopedDbConnection
+ // Use ShardConnection as opposed to ScopedDbConnection
+ const bool _useShardConn;
+
+ // Whether the requested database should be created implicitly
+ const bool _implicitCreateDb;
};
} // namespace mongo