0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/s/commands_admin.cpp

697 lines
26 KiB
C++
Raw Normal View History

// s/commands_admin.cpp
2008-10-24 23:51:28 +02:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
2008-10-24 23:51:28 +02:00
* 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.
2008-12-29 02:28:49 +01:00
*
2008-10-24 23:51:28 +02:00
* 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.
2008-12-29 02:28:49 +01:00
*
2008-10-24 23:51:28 +02:00
* 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/>.
*/
/* TODO
_ concurrency control.
_ limit() works right?
_ KillCursors
later
_ secondary indexes
*/
#include "stdafx.h"
2009-02-03 19:30:28 +01:00
#include "../util/message.h"
2008-10-24 23:51:28 +02:00
#include "../db/dbmessage.h"
2008-11-01 01:17:54 +01:00
#include "../client/connpool.h"
2008-10-24 23:51:28 +02:00
#include "../db/commands.h"
2009-02-19 18:55:01 +01:00
2009-02-13 03:03:46 +01:00
#include "config.h"
#include "chunk.h"
2009-03-30 19:28:49 +02:00
#include "strategy.h"
2008-10-24 23:51:28 +02:00
2009-01-14 23:09:51 +01:00
namespace mongo {
extern string ourHostname;
2009-04-21 21:08:20 +02:00
namespace dbgrid_cmds {
2009-04-21 21:08:20 +02:00
2009-02-06 22:43:00 +01:00
set<string> dbgridCommands;
2009-02-05 22:45:58 +01:00
class GridAdminCmd : public Command {
public:
2009-02-06 22:43:00 +01:00
GridAdminCmd( const char * n ) : Command( n ){
dbgridCommands.insert( n );
}
2009-02-05 22:45:58 +01:00
virtual bool slaveOk(){
return true;
}
2009-02-05 22:45:58 +01:00
virtual bool adminOnly() {
return true;
}
};
2009-04-21 21:08:20 +02:00
2009-02-13 15:28:23 +01:00
// --------------- misc commands ----------------------
2009-02-05 22:45:58 +01:00
class NetStatCmd : public GridAdminCmd {
public:
NetStatCmd() : GridAdminCmd("netstat") { }
2009-09-01 22:23:56 +02:00
virtual void help( stringstream& help ) const {
help << " shows status/reachability of servers in the cluster";
}
2009-02-05 22:45:58 +01:00
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
result.append("configserver", configServer.getPrimary() );
result.append("isdbgrid", 1);
return true;
}
} netstat;
2009-02-13 15:28:23 +01:00
class ListGridCommands : public GridAdminCmd {
public:
ListGridCommands() : GridAdminCmd("gridcommands") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-04-21 21:08:20 +02:00
2009-02-13 15:28:23 +01:00
BSONObjBuilder arr;
int num=0;
for ( set<string>::iterator i = dbgridCommands.begin(); i != dbgridCommands.end(); i++ ){
string s = BSONObjBuilder::numStr( num++ );
arr.append( s.c_str() , *i );
}
2009-04-21 21:08:20 +02:00
2009-02-13 15:28:23 +01:00
result.appendArray( "commands" , arr.done() );
return true;
}
2009-04-21 21:08:20 +02:00
} listGridCommands;
2009-02-13 15:28:23 +01:00
// ------------ database level commands -------------
2009-04-21 21:08:20 +02:00
2009-02-05 22:45:58 +01:00
class ListDatabaseCommand : public GridAdminCmd {
public:
ListDatabaseCommand() : GridAdminCmd("listdatabases") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-04-21 21:08:20 +02:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-03-19 21:23:04 +01:00
auto_ptr<DBClientCursor> cursor = conn->query( "config.databases" , BSONObj() );
2009-02-12 19:47:43 +01:00
2009-04-21 21:08:20 +02:00
BSONObjBuilder list;
2009-02-12 19:47:43 +01:00
int num = 0;
while ( cursor->more() ){
string s = BSONObjBuilder::numStr( num++ );
2009-04-21 21:08:20 +02:00
2009-02-12 19:47:43 +01:00
BSONObj o = cursor->next();
list.append( s.c_str() , o["name"].valuestrsafe() );
}
2009-04-21 21:08:20 +02:00
2009-02-12 19:47:43 +01:00
result.appendArray("databases" , list.obj() );
conn.done();
2009-04-21 21:08:20 +02:00
return true;
2009-02-05 22:45:58 +01:00
}
} gridListDatabase;
2009-02-13 22:18:14 +01:00
class MoveDatabasePrimaryCommand : public GridAdminCmd {
public:
MoveDatabasePrimaryCommand() : GridAdminCmd("moveprimary") { }
virtual void help( stringstream& help ) const {
help << " example: { moveprimary : 'foo' , to : 'localhost:9999' } TODO: locking? ";
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string dbname = cmdObj["moveprimary"].valuestrsafe();
2009-04-21 21:08:20 +02:00
2009-02-13 22:18:14 +01:00
if ( dbname.size() == 0 ){
errmsg = "no db";
return false;
}
if ( dbname == "config" ){
errmsg = "can't move config db";
return false;
}
2009-04-21 21:08:20 +02:00
2009-02-13 22:18:14 +01:00
DBConfig * config = grid.getDBConfig( dbname , false );
if ( ! config ){
errmsg = "can't find db!";
return false;
}
2009-04-21 21:08:20 +02:00
2009-02-13 22:18:14 +01:00
string to = cmdObj["to"].valuestrsafe();
if ( ! to.size() ){
errmsg = "you have to specify where you want to move it";
return false;
}
if ( to == config->getPrimary() ){
errmsg = "thats already the primary";
return false;
}
if ( ! grid.knowAboutShard( to ) ){
2009-02-13 22:18:14 +01:00
errmsg = "that server isn't known to me";
return false;
}
2009-04-21 21:08:20 +02:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-02-13 22:18:14 +01:00
log() << "moving " << dbname << " primary from: " << config->getPrimary() << " to: " << to << endl;
2009-04-21 21:08:20 +02:00
2009-02-13 22:18:14 +01:00
// TODO LOCKING: this is not safe with multiple mongos
2009-04-21 21:08:20 +02:00
2009-02-13 22:18:14 +01:00
ScopedDbConnection toconn( to );
// TODO AARON - we need a clone command which replays operations from clone start to now
// using a seperate smaller oplog
BSONObj cloneRes;
2009-02-17 16:47:28 +01:00
bool worked = toconn->runCommand( dbname.c_str() , BSON( "clone" << config->getPrimary() ) , cloneRes );
2009-02-13 22:18:14 +01:00
toconn.done();
if ( ! worked ){
log() << "clone failed" << cloneRes << endl;
errmsg = "clone failed";
conn.done();
return false;
}
ScopedDbConnection fromconn( config->getPrimary() );
2009-04-21 21:08:20 +02:00
2009-02-13 22:18:14 +01:00
config->setPrimary( to );
2009-02-20 21:07:59 +01:00
config->save( true );
2009-04-21 21:08:20 +02:00
2009-02-13 22:18:14 +01:00
log() << " dropping " << dbname << " from old" << endl;
fromconn->dropDatabase( dbname.c_str() );
fromconn.done();
result << "primary" << to;
conn.done();
return true;
}
} movePrimary;
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
class EnableShardingCmd : public GridAdminCmd {
2009-02-18 19:54:22 +01:00
public:
2009-09-01 18:17:41 +02:00
EnableShardingCmd() : GridAdminCmd( "enablesharding" ){}
2009-02-18 19:54:22 +01:00
virtual void help( stringstream& help ) const {
2009-04-21 21:08:20 +02:00
help
2009-08-30 17:22:58 +02:00
<< "Enable sharding for a db. (Use 'shardcollection' command afterwards.)\n"
<< " { enablesharding : \"<dbname>\" }\n";
2009-02-18 19:54:22 +01:00
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-08-30 17:22:58 +02:00
string dbname = cmdObj["enablesharding"].valuestrsafe();
2009-02-18 19:54:22 +01:00
if ( dbname.size() == 0 ){
errmsg = "no db";
return false;
}
2009-02-13 22:18:14 +01:00
2009-02-18 19:54:22 +01:00
DBConfig * config = grid.getDBConfig( dbname );
2009-09-01 18:17:41 +02:00
if ( config->isShardingEnabled() ){
2009-08-30 17:22:58 +02:00
errmsg = "already enabled";
2009-02-18 19:54:22 +01:00
return false;
}
2009-12-10 16:46:41 +01:00
log() << "enabling sharding on: " << dbname << endl;
2009-02-06 22:43:00 +01:00
2009-09-01 18:17:41 +02:00
config->enableSharding();
2009-02-20 21:07:59 +01:00
config->save( true );
2009-02-19 18:55:01 +01:00
2009-02-18 19:54:22 +01:00
return true;
}
2009-09-01 18:17:41 +02:00
} enableShardingCmd;
2009-02-18 19:54:22 +01:00
// ------------ collection level commands -------------
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
class ShardCollectionCmd : public GridAdminCmd {
2009-02-18 19:54:22 +01:00
public:
2009-09-01 18:17:41 +02:00
ShardCollectionCmd() : GridAdminCmd( "shardcollection" ){}
2009-08-30 17:22:58 +02:00
virtual void help( stringstream& help ) const {
help
2009-09-03 22:48:34 +02:00
<< "Shard a collection. Requires key. Optional unique. Sharding must already be enabled for the database.\n"
2009-08-30 17:22:58 +02:00
<< " { enablesharding : \"<dbname>\" }\n";
}
2009-02-19 18:55:01 +01:00
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-08-30 17:22:58 +02:00
string ns = cmdObj["shardcollection"].valuestrsafe();
2009-02-19 18:55:01 +01:00
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
2009-09-01 18:17:41 +02:00
if ( ! config->isShardingEnabled() ){
2009-08-30 17:22:58 +02:00
errmsg = "sharding not enabled for db";
2009-02-19 18:55:01 +01:00
return false;
}
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
if ( config->isSharded( ns ) ){
2009-02-19 18:55:01 +01:00
errmsg = "already sharded";
return false;
}
2009-04-21 21:08:20 +02:00
2009-02-19 18:55:01 +01:00
BSONObj key = cmdObj.getObjectField( "key" );
if ( key.isEmpty() ){
errmsg = "no shard key";
return false;
2009-11-25 05:09:44 +01:00
} else if (key.nFields() > 1){
errmsg = "compound shard keys not supported yet";
return false;
2009-02-19 18:55:01 +01:00
}
2009-04-21 21:08:20 +02:00
2009-09-01 21:59:19 +02:00
if ( ns.find( ".system." ) != string::npos ){
errmsg = "can't shard system namespaces";
return false;
}
{
ScopedDbConnection conn( config->getPrimary() );
BSONObjBuilder b;
b.append( "ns" , ns );
b.appendBool( "unique" , true );
if ( conn->count( config->getName() + ".system.indexes" , b.obj() ) ){
errmsg = "can't shard collection with unique indexes";
conn.done();
return false;
}
BSONObj res = conn->findOne( config->getName() + ".system.namespaces" , BSON( "name" << ns ) );
if ( res["options"].type() == Object && res["options"].embeddedObject()["capped"].trueValue() ){
errmsg = "can't shard capped collection";
conn.done();
return false;
}
conn.done();
}
2009-12-10 16:46:41 +01:00
log() << "CMD: shardcollection: " << cmdObj << endl;
2009-09-01 21:59:19 +02:00
2009-09-03 22:48:34 +02:00
config->shardCollection( ns , key , cmdObj["unique"].trueValue() );
2009-02-20 21:07:59 +01:00
config->save( true );
2009-02-19 18:55:01 +01:00
2009-09-01 21:59:19 +02:00
result << "collectionsharded" << ns;
2009-02-19 18:55:01 +01:00
return true;
2009-04-21 21:08:20 +02:00
}
2009-09-01 18:17:41 +02:00
} shardCollectionCmd;
2009-04-21 21:08:20 +02:00
class GetShardVersion : public GridAdminCmd {
public:
GetShardVersion() : GridAdminCmd( "getShardVersion" ){}
virtual void help( stringstream& help ) const {
help << " example: { getShardVersion : 'alleyinsider.foo' } ";
}
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj["getShardVersion"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "need to speciy fully namespace";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
if ( ! config->isSharded( ns ) ){
errmsg = "ns not sharded.";
return false;
}
ChunkManager * cm = config->getChunkManager( ns );
if ( ! cm ){
errmsg = "no chunk manager?";
return false;
}
result.appendTimestamp( "version" , cm->getVersion() );
return 1;
}
} getShardVersionCmd;
2009-04-21 21:08:20 +02:00
2009-03-30 20:33:40 +02:00
class SplitCollectionHelper : public GridAdminCmd {
2009-02-18 19:54:22 +01:00
public:
SplitCollectionHelper( const char * name ) : GridAdminCmd( name ) , _name( name ){}
2009-02-18 19:54:22 +01:00
virtual void help( stringstream& help ) const {
2009-04-21 21:08:20 +02:00
help
2009-02-19 23:32:19 +01:00
<< " example: { shard : 'alleyinsider.blog.posts' , find : { ts : 1 } } - split the shard that contains give key \n"
<< " example: { shard : 'alleyinsider.blog.posts' , middle : { ts : 1 } } - split the shard that contains the key with this as the middle \n"
<< " NOTE: this does not move move the chunks, it merely creates a logical seperation \n"
;
2009-02-18 19:54:22 +01:00
}
2009-04-21 21:08:20 +02:00
virtual bool _split( BSONObjBuilder& result , string&errmsg , const string& ns , ChunkManager * manager , Chunk& old , BSONObj middle ) = 0;
2009-04-21 21:08:20 +02:00
2009-02-19 23:32:19 +01:00
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj[_name.c_str()].valuestrsafe();
2009-02-19 23:32:19 +01:00
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
2009-03-30 20:33:40 +02:00
2009-02-19 23:32:19 +01:00
DBConfig * config = grid.getDBConfig( ns );
2009-09-01 18:17:41 +02:00
if ( ! config->isSharded( ns ) ){
2009-02-19 23:32:19 +01:00
errmsg = "ns not sharded. have to shard before can split";
return false;
}
2009-04-21 21:08:20 +02:00
2009-02-19 23:32:19 +01:00
BSONObj find = cmdObj.getObjectField( "find" );
if ( find.isEmpty() ){
find = cmdObj.getObjectField( "middle" );
2009-04-21 21:08:20 +02:00
2009-11-05 22:59:39 +01:00
if ( find.isEmpty() ){
errmsg = "need to specify find or middle";
return false;
}
2009-02-19 23:32:19 +01:00
}
2009-12-10 16:46:41 +01:00
ChunkManager * info = config->getChunkManager( ns );
Chunk& old = info->findChunk( find );
2009-04-21 21:08:20 +02:00
2009-03-30 20:33:40 +02:00
return _split( result , errmsg , ns , info , old , cmdObj.getObjectField( "middle" ) );
}
protected:
string _name;
2009-03-30 20:33:40 +02:00
};
class SplitValueCommand : public SplitCollectionHelper {
public:
SplitValueCommand() : SplitCollectionHelper( "splitvalue" ){}
virtual bool _split( BSONObjBuilder& result , string& errmsg , const string& ns , ChunkManager * manager , Chunk& old , BSONObj middle ){
2009-04-21 21:08:20 +02:00
2009-03-30 20:33:40 +02:00
result << "shardinfo" << old.toString();
2009-04-21 21:08:20 +02:00
2009-03-30 20:33:40 +02:00
result.appendBool( "auto" , middle.isEmpty() );
2009-04-21 21:08:20 +02:00
if ( middle.isEmpty() )
middle = old.pickSplitPoint();
result.append( "middle" , middle );
2009-04-21 21:08:20 +02:00
2009-03-30 20:33:40 +02:00
return true;
2009-04-21 21:08:20 +02:00
}
2009-03-30 20:33:40 +02:00
} splitValueCmd;
class SplitCollection : public SplitCollectionHelper {
public:
2009-04-21 21:08:20 +02:00
SplitCollection() : SplitCollectionHelper( "split" ){}
virtual bool _split( BSONObjBuilder& result , string& errmsg , const string& ns , ChunkManager * manager , Chunk& old , BSONObj middle ){
2009-04-21 21:08:20 +02:00
2009-03-30 20:33:40 +02:00
log() << "splitting: " << ns << " shard: " << old << endl;
2009-04-21 21:08:20 +02:00
2009-03-30 20:33:40 +02:00
if ( middle.isEmpty() )
2009-02-19 23:32:19 +01:00
old.split();
2009-03-30 20:33:40 +02:00
else
old.split( middle );
2009-04-21 21:08:20 +02:00
2009-02-19 23:32:19 +01:00
return true;
2009-04-21 21:08:20 +02:00
}
2009-02-18 19:54:22 +01:00
} splitCollectionCmd;
2009-09-01 18:17:41 +02:00
class MoveChunkCmd : public GridAdminCmd {
public:
2009-09-01 18:17:41 +02:00
MoveChunkCmd() : GridAdminCmd( "movechunk" ){}
virtual void help( stringstream& help ) const {
2009-09-01 18:17:41 +02:00
help << "{ movechunk : 'test.foo' , find : { num : 1 } , to : 'localhost:30001' }";
}
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-09-01 18:17:41 +02:00
string ns = cmdObj["movechunk"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
2009-09-01 18:17:41 +02:00
if ( ! config->isSharded( ns ) ){
errmsg = "ns not sharded. have to shard before can move a chunk";
return false;
}
2009-04-21 21:08:20 +02:00
BSONObj find = cmdObj.getObjectField( "find" );
if ( find.isEmpty() ){
errmsg = "need to specify find. see help";
return false;
}
string to = cmdObj["to"].valuestrsafe();
if ( ! to.size() ){
2009-09-01 18:17:41 +02:00
errmsg = "you have to specify where you want to move the chunk";
return false;
}
2009-12-10 16:46:41 +01:00
log() << "CMD: movechunk: " << cmdObj << endl;
2009-04-21 21:08:20 +02:00
ChunkManager * info = config->getChunkManager( ns );
Chunk& c = info->findChunk( find );
2009-09-01 18:17:41 +02:00
string from = c.getShard();
2009-02-20 19:46:57 +01:00
2009-09-01 18:17:41 +02:00
if ( from == to ){
errmsg = "that chunk is already on that shard";
return false;
}
if ( ! grid.knowAboutShard( to ) ){
2009-09-01 18:17:41 +02:00
errmsg = "that shard isn't known to me";
return false;
}
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
if ( ! c.moveAndCommit( to , errmsg ) )
return false;
2009-04-21 21:08:20 +02:00
return true;
}
2009-09-01 18:17:41 +02:00
} moveChunkCmd;
2009-02-18 19:54:22 +01:00
// ------------ server level commands -------------
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
class ListShardsCmd : public GridAdminCmd {
2009-02-06 22:25:14 +01:00
public:
2009-09-01 18:17:41 +02:00
ListShardsCmd() : GridAdminCmd("listshards") { }
2009-09-01 22:23:56 +02:00
virtual void help( stringstream& help ) const {
help << "list all shards of the system";
}
2009-02-06 22:25:14 +01:00
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-02-09 19:34:40 +01:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-02-05 22:45:58 +01:00
2009-02-06 22:25:14 +01:00
vector<BSONObj> all;
auto_ptr<DBClientCursor> cursor = conn->query( "config.shards" , BSONObj() );
2009-02-06 22:25:14 +01:00
while ( cursor->more() ){
BSONObj o = cursor->next();
all.push_back( o );
}
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
result.append("shards" , all );
2009-02-09 19:34:40 +01:00
conn.done();
2009-02-06 22:25:14 +01:00
return true;
}
2009-09-01 18:17:41 +02:00
} listShardsCmd;
2009-04-21 21:08:20 +02:00
/* a shard is a single mongod server or a replica pair. add it (them) to the cluster as a storage partition. */
class AddShard : public GridAdminCmd {
2009-02-06 22:25:14 +01:00
public:
AddShard() : GridAdminCmd("addshard") { }
2009-09-01 22:23:56 +02:00
virtual void help( stringstream& help ) const {
help << "add a new shard to the system";
}
2009-02-06 22:25:14 +01:00
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-02-09 19:34:40 +01:00
ScopedDbConnection conn( configServer.getPrimary() );
string host = cmdObj["addshard"].valuestrsafe();
if ( host == "localhost" || host.find( "localhost:" ) == 0 ||
host == "127.0.0.1" || host.find( "127.0.0.1:" ) == 0 ){
if ( cmdObj["allowLocal"].type() != Bool ||
! cmdObj["allowLocal"].boolean() ){
errmsg =
"can't use localhost as a shard since all shards need to communicate. "
"allowLocal to override for testing";
return false;
}
}
if ( host.find( ":" ) == string::npos ){
stringstream ss;
ss << host << ":" << CmdLine::ShardServerPort;
host = ss.str();
}
BSONObj shard;
{
BSONObjBuilder b;
b.append( "host" , host );
if ( cmdObj["maxSize"].isNumber() )
b.append( cmdObj["maxSize"] );
shard = b.obj();
}
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
BSONObj old = conn->findOne( "config.shards" , shard );
2009-02-06 22:25:14 +01:00
if ( ! old.isEmpty() ){
result.append( "msg" , "already exists" );
2009-02-09 19:34:40 +01:00
conn.done();
2009-02-06 22:25:14 +01:00
return false;
}
2009-04-21 21:08:20 +02:00
try {
ScopedDbConnection newShardConn( host );
newShardConn->getLastError();
newShardConn.done();
}
catch ( DBException& e ){
errmsg = "couldn't connect to new shard";
result.append( "host" , host );
result.append( "exception" , e.what() );
conn.done();
return false;
}
2009-09-01 18:17:41 +02:00
conn->insert( "config.shards" , shard );
result.append( "added" , shard["host"].valuestrsafe() );
2009-02-09 19:34:40 +01:00
conn.done();
2009-02-06 22:25:14 +01:00
return true;
}
2009-02-08 23:55:33 +01:00
} addServer;
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
class RemoveShardCmd : public GridAdminCmd {
2009-02-08 23:55:33 +01:00
public:
2009-09-01 18:17:41 +02:00
RemoveShardCmd() : GridAdminCmd("removeshard") { }
2009-09-01 22:23:56 +02:00
virtual void help( stringstream& help ) const {
help << "remove a shard to the system.\nshard must be empty or command will return an error.";
}
2009-02-08 23:55:33 +01:00
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-09-01 18:17:41 +02:00
if ( 1 ){
2009-09-01 22:23:56 +02:00
errmsg = "removeshard not yet implemented";
2009-09-01 18:17:41 +02:00
return 0;
}
2009-02-09 19:34:40 +01:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
BSONObj server = BSON( "host" << cmdObj["removeshard"].valuestrsafe() );
conn->remove( "config.shards" , server );
2009-04-21 21:08:20 +02:00
2009-02-09 19:34:40 +01:00
conn.done();
2009-02-08 23:55:33 +01:00
return true;
}
2009-09-01 18:17:41 +02:00
} removeShardCmd;
2009-02-08 23:55:33 +01:00
2009-04-21 21:08:20 +02:00
2009-02-13 15:28:23 +01:00
// --------------- public commands ----------------
2009-04-21 21:08:20 +02:00
class IsDbGridCmd : public Command {
public:
virtual bool slaveOk() {
return true;
}
IsDbGridCmd() : Command("isdbgrid") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
result.append("isdbgrid", 1);
result.append("hostname", ourHostname);
return true;
}
} isdbgrid;
2009-04-21 21:08:20 +02:00
class CmdIsMaster : public Command {
public:
2009-01-19 02:37:17 +01:00
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
2009-09-01 22:23:56 +02:00
virtual void help( stringstream& help ) const {
help << "test if this is master half of a replica pair";
}
CmdIsMaster() : Command("ismaster") { }
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
result.append("ismaster", 1.0 );
result.append("msg", "isdbgrid");
return true;
}
} ismaster;
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
class CmdShardingGetPrevError : public Command {
2009-02-26 21:55:00 +01:00
public:
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
2009-09-01 22:23:56 +02:00
virtual void help( stringstream& help ) const {
help << "get previous error (since last reseterror command)";
}
2009-09-01 18:17:41 +02:00
CmdShardingGetPrevError() : Command("getpreverror") { }
2009-02-26 21:55:00 +01:00
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
2009-09-01 22:23:56 +02:00
errmsg += "getpreverror not supported for sharded environments";
2009-02-26 21:55:00 +01:00
return false;
}
} cmdGetPrevError;
2009-04-21 21:08:20 +02:00
2009-09-01 18:17:41 +02:00
class CmdShardingGetLastError : public Command {
2009-02-26 21:55:00 +01:00
public:
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
2009-09-01 22:23:56 +02:00
virtual void help( stringstream& help ) const {
help << "check for an error on the last command executed";
}
CmdShardingGetLastError() : Command("getlasterror") { }
virtual bool run(const char *nsraw, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
string dbName = nsraw;
dbName = dbName.substr( 0 , dbName.size() - 5 );
DBConfig * conf = grid.getDBConfig( dbName , false );
2009-09-14 17:33:42 +02:00
ClientInfo * client = ClientInfo::get();
set<string> * shards = client->getPrev();
if ( shards->size() == 0 ){
result.appendNull( "err" );
return true;
}
if ( shards->size() == 1 ){
string theShard = *(shards->begin() );
result.append( "theshard" , theShard.c_str() );
ScopedDbConnection conn( theShard );
BSONObj res;
bool ok = conn->runCommand( conf->getName() , cmdObj , res );
result.appendElements( res );
conn.done();
return ok;
}
vector<string> errors;
for ( set<string>::iterator i = shards->begin(); i != shards->end(); i++ ){
string theShard = *i;
ScopedDbConnection conn( theShard );
string temp = conn->getLastError();
if ( temp.size() )
errors.push_back( temp );
conn.done();
}
if ( errors.size() == 0 ){
result.appendNull( "err" );
return true;
}
result.append( "err" , errors[0].c_str() );
BSONObjBuilder all;
for ( unsigned i=0; i<errors.size(); i++ ){
all.append( all.numStr( i ).c_str() , errors[i].c_str() );
}
result.appendArray( "errs" , all.obj() );
return true;
2009-02-26 21:55:00 +01:00
}
} cmdGetLastError;
2009-09-14 17:33:42 +02:00
}
2009-01-14 23:09:51 +01:00
} // namespace mongo