Files
mongo/s/commands_admin.cpp

703 lines
26 KiB
C++
Raw Normal View History

// s/commands_admin.cpp
2008-10-24 17:51:28 -04:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -05:00
*
2008-10-24 17:51:28 -04: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-28 20:28:49 -05:00
*
2008-10-24 17:51:28 -04: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-28 20:28:49 -05:00
*
2008-10-24 17:51:28 -04: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 13:30:28 -05:00
#include "../util/message.h"
2008-10-24 17:51:28 -04:00
#include "../db/dbmessage.h"
2008-10-31 19:17:54 -05:00
#include "../client/connpool.h"
2008-10-24 17:51:28 -04:00
#include "../db/commands.h"
2009-02-19 12:55:01 -05:00
2009-02-12 21:03:46 -05:00
#include "config.h"
#include "chunk.h"
2009-03-30 13:28:49 -04:00
#include "strategy.h"
2008-10-24 17:51:28 -04:00
2009-01-14 17:09:51 -05:00
namespace mongo {
extern string ourHostname;
2009-04-21 15:08:20 -04:00
namespace dbgrid_cmds {
2009-04-21 15:08:20 -04:00
2009-02-06 16:43:00 -05:00
set<string> dbgridCommands;
2009-02-05 16:45:58 -05:00
class GridAdminCmd : public Command {
public:
2009-02-06 16:43:00 -05:00
GridAdminCmd( const char * n ) : Command( n ){
dbgridCommands.insert( n );
}
2009-02-05 16:45:58 -05:00
virtual bool slaveOk(){
return true;
}
2009-02-05 16:45:58 -05:00
virtual bool adminOnly() {
return true;
}
2010-02-26 14:38:51 -05:00
// all grid commands are designed not to lock
virtual LockType locktype(){ return NONE; }
2009-02-05 16:45:58 -05:00
};
2009-04-21 15:08:20 -04:00
2009-02-13 09:28:23 -05:00
// --------------- misc commands ----------------------
2009-02-05 16:45:58 -05:00
class NetStatCmd : public GridAdminCmd {
public:
NetStatCmd() : GridAdminCmd("netstat") { }
2009-09-01 16:23:56 -04:00
virtual void help( stringstream& help ) const {
help << " shows status/reachability of servers in the cluster";
}
2009-02-05 16:45:58 -05: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 09:28:23 -05:00
class ListGridCommands : public GridAdminCmd {
public:
ListGridCommands() : GridAdminCmd("gridcommands") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-04-21 15:08:20 -04:00
2009-02-13 09:28:23 -05: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 15:08:20 -04:00
2009-02-13 09:28:23 -05:00
result.appendArray( "commands" , arr.done() );
return true;
}
2009-04-21 15:08:20 -04:00
} listGridCommands;
2009-02-13 09:28:23 -05:00
// ------------ database level commands -------------
2009-04-21 15:08:20 -04:00
2009-02-05 16:45:58 -05:00
class ListDatabaseCommand : public GridAdminCmd {
public:
ListDatabaseCommand() : GridAdminCmd("listdatabases") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-04-21 15:08:20 -04:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-03-19 16:23:04 -04:00
auto_ptr<DBClientCursor> cursor = conn->query( "config.databases" , BSONObj() );
2009-02-12 13:47:43 -05:00
2009-04-21 15:08:20 -04:00
BSONObjBuilder list;
2009-02-12 13:47:43 -05:00
int num = 0;
while ( cursor->more() ){
string s = BSONObjBuilder::numStr( num++ );
2009-04-21 15:08:20 -04:00
2009-02-12 13:47:43 -05:00
BSONObj o = cursor->next();
list.append( s.c_str() , o["name"].valuestrsafe() );
}
2009-04-21 15:08:20 -04:00
2009-02-12 13:47:43 -05:00
result.appendArray("databases" , list.obj() );
conn.done();
2009-04-21 15:08:20 -04:00
return true;
2009-02-05 16:45:58 -05:00
}
} gridListDatabase;
2009-02-13 16:18:14 -05: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 15:08:20 -04:00
2009-02-13 16:18:14 -05:00
if ( dbname.size() == 0 ){
errmsg = "no db";
return false;
}
if ( dbname == "config" ){
errmsg = "can't move config db";
return false;
}
2009-04-21 15:08:20 -04:00
2009-02-13 16:18:14 -05:00
DBConfig * config = grid.getDBConfig( dbname , false );
if ( ! config ){
errmsg = "can't find db!";
return false;
}
2009-04-21 15:08:20 -04:00
2009-02-13 16:18:14 -05: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 16:18:14 -05:00
errmsg = "that server isn't known to me";
return false;
}
2009-04-21 15:08:20 -04:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-02-13 16:18:14 -05:00
log() << "moving " << dbname << " primary from: " << config->getPrimary() << " to: " << to << endl;
2009-04-21 15:08:20 -04:00
2009-02-13 16:18:14 -05:00
// TODO LOCKING: this is not safe with multiple mongos
2009-04-21 15:08:20 -04:00
2009-02-13 16:18:14 -05: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 10:47:28 -05:00
bool worked = toconn->runCommand( dbname.c_str() , BSON( "clone" << config->getPrimary() ) , cloneRes );
2009-02-13 16:18:14 -05:00
toconn.done();
if ( ! worked ){
log() << "clone failed" << cloneRes << endl;
errmsg = "clone failed";
conn.done();
return false;
}
ScopedDbConnection fromconn( config->getPrimary() );
2009-04-21 15:08:20 -04:00
2009-02-13 16:18:14 -05:00
config->setPrimary( to );
2009-02-20 15:07:59 -05:00
config->save( true );
2009-04-21 15:08:20 -04:00
2009-02-13 16:18:14 -05: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 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
class EnableShardingCmd : public GridAdminCmd {
2009-02-18 13:54:22 -05:00
public:
2009-09-01 12:17:41 -04:00
EnableShardingCmd() : GridAdminCmd( "enablesharding" ){}
2009-02-18 13:54:22 -05:00
virtual void help( stringstream& help ) const {
2009-04-21 15:08:20 -04:00
help
2009-08-30 11:22:58 -04:00
<< "Enable sharding for a db. (Use 'shardcollection' command afterwards.)\n"
<< " { enablesharding : \"<dbname>\" }\n";
2009-02-18 13:54:22 -05:00
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-08-30 11:22:58 -04:00
string dbname = cmdObj["enablesharding"].valuestrsafe();
2009-02-18 13:54:22 -05:00
if ( dbname.size() == 0 ){
errmsg = "no db";
return false;
}
2009-02-13 16:18:14 -05:00
2009-02-18 13:54:22 -05:00
DBConfig * config = grid.getDBConfig( dbname );
2009-09-01 12:17:41 -04:00
if ( config->isShardingEnabled() ){
2009-08-30 11:22:58 -04:00
errmsg = "already enabled";
2009-02-18 13:54:22 -05:00
return false;
}
2009-12-10 10:46:41 -05:00
log() << "enabling sharding on: " << dbname << endl;
2009-02-06 16:43:00 -05:00
2009-09-01 12:17:41 -04:00
config->enableSharding();
2009-02-20 15:07:59 -05:00
config->save( true );
2009-02-19 12:55:01 -05:00
2009-02-18 13:54:22 -05:00
return true;
}
2009-09-01 12:17:41 -04:00
} enableShardingCmd;
2009-02-18 13:54:22 -05:00
// ------------ collection level commands -------------
2009-04-21 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
class ShardCollectionCmd : public GridAdminCmd {
2009-02-18 13:54:22 -05:00
public:
2009-09-01 12:17:41 -04:00
ShardCollectionCmd() : GridAdminCmd( "shardcollection" ){}
2009-08-30 11:22:58 -04:00
virtual void help( stringstream& help ) const {
help
2009-09-03 16:48:34 -04:00
<< "Shard a collection. Requires key. Optional unique. Sharding must already be enabled for the database.\n"
2009-08-30 11:22:58 -04:00
<< " { enablesharding : \"<dbname>\" }\n";
}
2009-02-19 12:55:01 -05:00
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-08-30 11:22:58 -04:00
string ns = cmdObj["shardcollection"].valuestrsafe();
2009-02-19 12:55:01 -05:00
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
2009-09-01 12:17:41 -04:00
if ( ! config->isShardingEnabled() ){
2009-08-30 11:22:58 -04:00
errmsg = "sharding not enabled for db";
2009-02-19 12:55:01 -05:00
return false;
}
2009-04-21 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
if ( config->isSharded( ns ) ){
2009-02-19 12:55:01 -05:00
errmsg = "already sharded";
return false;
}
2009-04-21 15:08:20 -04:00
2009-02-19 12:55:01 -05:00
BSONObj key = cmdObj.getObjectField( "key" );
if ( key.isEmpty() ){
errmsg = "no shard key";
return false;
2009-11-24 23:09:44 -05:00
} else if (key.nFields() > 1){
errmsg = "compound shard keys not supported yet";
return false;
2009-02-19 12:55:01 -05:00
}
2009-04-21 15:08:20 -04:00
2009-09-01 15:59:19 -04: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 10:46:41 -05:00
log() << "CMD: shardcollection: " << cmdObj << endl;
2009-09-01 15:59:19 -04:00
2009-09-03 16:48:34 -04:00
config->shardCollection( ns , key , cmdObj["unique"].trueValue() );
2009-02-20 15:07:59 -05:00
config->save( true );
2009-02-19 12:55:01 -05:00
2009-09-01 15:59:19 -04:00
result << "collectionsharded" << ns;
2009-02-19 12:55:01 -05:00
return true;
2009-04-21 15:08:20 -04:00
}
2009-09-01 12:17:41 -04:00
} shardCollectionCmd;
2009-04-21 15:08:20 -04: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 15:08:20 -04:00
2009-03-30 14:33:40 -04:00
class SplitCollectionHelper : public GridAdminCmd {
2009-02-18 13:54:22 -05:00
public:
SplitCollectionHelper( const char * name ) : GridAdminCmd( name ) , _name( name ){}
2009-02-18 13:54:22 -05:00
virtual void help( stringstream& help ) const {
2009-04-21 15:08:20 -04:00
help
2009-02-19 17:32:19 -05: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 13:54:22 -05:00
}
2009-04-21 15:08:20 -04:00
virtual bool _split( BSONObjBuilder& result , string&errmsg , const string& ns , ChunkManager * manager , Chunk& old , BSONObj middle ) = 0;
2009-04-21 15:08:20 -04:00
2009-02-19 17:32:19 -05:00
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj[_name.c_str()].valuestrsafe();
2009-02-19 17:32:19 -05:00
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
2009-03-30 14:33:40 -04:00
2009-02-19 17:32:19 -05:00
DBConfig * config = grid.getDBConfig( ns );
2009-09-01 12:17:41 -04:00
if ( ! config->isSharded( ns ) ){
2009-02-19 17:32:19 -05:00
errmsg = "ns not sharded. have to shard before can split";
return false;
}
2009-04-21 15:08:20 -04:00
2009-02-19 17:32:19 -05:00
BSONObj find = cmdObj.getObjectField( "find" );
if ( find.isEmpty() ){
find = cmdObj.getObjectField( "middle" );
2009-04-21 15:08:20 -04:00
2009-11-05 16:59:39 -05:00
if ( find.isEmpty() ){
errmsg = "need to specify find or middle";
return false;
}
2009-02-19 17:32:19 -05:00
}
2009-12-10 10:46:41 -05:00
ChunkManager * info = config->getChunkManager( ns );
Chunk& old = info->findChunk( find );
2009-04-21 15:08:20 -04:00
2009-03-30 14:33:40 -04:00
return _split( result , errmsg , ns , info , old , cmdObj.getObjectField( "middle" ) );
}
protected:
string _name;
2009-03-30 14:33:40 -04: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 15:08:20 -04:00
2009-03-30 14:33:40 -04:00
result << "shardinfo" << old.toString();
2009-04-21 15:08:20 -04:00
2009-03-30 14:33:40 -04:00
result.appendBool( "auto" , middle.isEmpty() );
2009-04-21 15:08:20 -04:00
if ( middle.isEmpty() )
middle = old.pickSplitPoint();
result.append( "middle" , middle );
2009-04-21 15:08:20 -04:00
2009-03-30 14:33:40 -04:00
return true;
2009-04-21 15:08:20 -04:00
}
2009-03-30 14:33:40 -04:00
} splitValueCmd;
class SplitCollection : public SplitCollectionHelper {
public:
2009-04-21 15:08:20 -04:00
SplitCollection() : SplitCollectionHelper( "split" ){}
virtual bool _split( BSONObjBuilder& result , string& errmsg , const string& ns , ChunkManager * manager , Chunk& old , BSONObj middle ){
2009-04-21 15:08:20 -04:00
2009-03-30 14:33:40 -04:00
log() << "splitting: " << ns << " shard: " << old << endl;
2009-04-21 15:08:20 -04:00
2009-03-30 14:33:40 -04:00
if ( middle.isEmpty() )
2009-02-19 17:32:19 -05:00
old.split();
2009-03-30 14:33:40 -04:00
else
old.split( middle );
2009-04-21 15:08:20 -04:00
2009-02-19 17:32:19 -05:00
return true;
2009-04-21 15:08:20 -04:00
}
2009-02-18 13:54:22 -05:00
} splitCollectionCmd;
2009-09-01 12:17:41 -04:00
class MoveChunkCmd : public GridAdminCmd {
public:
2009-09-01 12:17:41 -04:00
MoveChunkCmd() : GridAdminCmd( "movechunk" ){}
virtual void help( stringstream& help ) const {
2009-09-01 12:17:41 -04: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 12:17:41 -04:00
string ns = cmdObj["movechunk"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
2009-09-01 12:17:41 -04:00
if ( ! config->isSharded( ns ) ){
errmsg = "ns not sharded. have to shard before can move a chunk";
return false;
}
2009-04-21 15:08:20 -04: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 12:17:41 -04:00
errmsg = "you have to specify where you want to move the chunk";
return false;
}
2009-12-10 10:46:41 -05:00
log() << "CMD: movechunk: " << cmdObj << endl;
2009-04-21 15:08:20 -04:00
ChunkManager * info = config->getChunkManager( ns );
Chunk& c = info->findChunk( find );
2009-09-01 12:17:41 -04:00
string from = c.getShard();
2009-02-20 13:46:57 -05:00
2009-09-01 12:17:41 -04:00
if ( from == to ){
errmsg = "that chunk is already on that shard";
return false;
}
if ( ! grid.knowAboutShard( to ) ){
2009-09-01 12:17:41 -04:00
errmsg = "that shard isn't known to me";
return false;
}
2009-04-21 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
if ( ! c.moveAndCommit( to , errmsg ) )
return false;
2009-04-21 15:08:20 -04:00
return true;
}
2009-09-01 12:17:41 -04:00
} moveChunkCmd;
2009-02-18 13:54:22 -05:00
// ------------ server level commands -------------
2009-04-21 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
class ListShardsCmd : public GridAdminCmd {
2009-02-06 16:25:14 -05:00
public:
2009-09-01 12:17:41 -04:00
ListShardsCmd() : GridAdminCmd("listshards") { }
2009-09-01 16:23:56 -04:00
virtual void help( stringstream& help ) const {
help << "list all shards of the system";
}
2009-02-06 16:25:14 -05:00
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-02-09 13:34:40 -05:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-02-05 16:45:58 -05:00
2009-02-06 16:25:14 -05:00
vector<BSONObj> all;
auto_ptr<DBClientCursor> cursor = conn->query( "config.shards" , BSONObj() );
2009-02-06 16:25:14 -05:00
while ( cursor->more() ){
BSONObj o = cursor->next();
all.push_back( o );
}
2009-04-21 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
result.append("shards" , all );
2009-02-09 13:34:40 -05:00
conn.done();
2009-02-06 16:25:14 -05:00
return true;
}
2009-09-01 12:17:41 -04:00
} listShardsCmd;
2009-04-21 15:08:20 -04: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 16:25:14 -05:00
public:
AddShard() : GridAdminCmd("addshard") { }
2009-09-01 16:23:56 -04:00
virtual void help( stringstream& help ) const {
help << "add a new shard to the system";
}
2009-02-06 16:25:14 -05:00
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-02-09 13:34:40 -05: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 ){
2010-01-26 13:49:55 -05:00
if ( ! cmdObj["allowLocal"].trueValue() ){
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 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
BSONObj old = conn->findOne( "config.shards" , shard );
2009-02-06 16:25:14 -05:00
if ( ! old.isEmpty() ){
result.append( "msg" , "already exists" );
2009-02-09 13:34:40 -05:00
conn.done();
2009-02-06 16:25:14 -05:00
return false;
}
2009-04-21 15:08:20 -04: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 12:17:41 -04:00
conn->insert( "config.shards" , shard );
result.append( "added" , shard["host"].valuestrsafe() );
2009-02-09 13:34:40 -05:00
conn.done();
2009-02-06 16:25:14 -05:00
return true;
}
2009-02-08 17:55:33 -05:00
} addServer;
2009-04-21 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
class RemoveShardCmd : public GridAdminCmd {
2009-02-08 17:55:33 -05:00
public:
2009-09-01 12:17:41 -04:00
RemoveShardCmd() : GridAdminCmd("removeshard") { }
2009-09-01 16:23:56 -04: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 17:55:33 -05:00
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
2009-09-01 12:17:41 -04:00
if ( 1 ){
2009-09-01 16:23:56 -04:00
errmsg = "removeshard not yet implemented";
2009-09-01 12:17:41 -04:00
return 0;
}
2009-02-09 13:34:40 -05:00
ScopedDbConnection conn( configServer.getPrimary() );
2009-04-21 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
BSONObj server = BSON( "host" << cmdObj["removeshard"].valuestrsafe() );
conn->remove( "config.shards" , server );
2009-04-21 15:08:20 -04:00
2009-02-09 13:34:40 -05:00
conn.done();
2009-02-08 17:55:33 -05:00
return true;
}
2009-09-01 12:17:41 -04:00
} removeShardCmd;
2009-02-08 17:55:33 -05:00
2009-04-21 15:08:20 -04:00
2009-02-13 09:28:23 -05:00
// --------------- public commands ----------------
2009-04-21 15:08:20 -04:00
class IsDbGridCmd : public Command {
public:
2010-02-26 14:38:51 -05:00
virtual LockType locktype(){ return NONE; }
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 15:08:20 -04:00
class CmdIsMaster : public Command {
public:
2010-02-26 14:38:51 -05:00
virtual LockType locktype(){ return NONE; }
2009-01-18 20:37:17 -05:00
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
2009-09-01 16:23:56 -04: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 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
class CmdShardingGetPrevError : public Command {
2009-02-26 15:55:00 -05:00
public:
2010-02-26 14:38:51 -05:00
virtual LockType locktype(){ return NONE; }
2009-02-26 15:55:00 -05:00
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
2009-09-01 16:23:56 -04:00
virtual void help( stringstream& help ) const {
help << "get previous error (since last reseterror command)";
}
2009-09-01 12:17:41 -04:00
CmdShardingGetPrevError() : Command("getpreverror") { }
2009-02-26 15:55:00 -05:00
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
2009-09-01 16:23:56 -04:00
errmsg += "getpreverror not supported for sharded environments";
2009-02-26 15:55:00 -05:00
return false;
}
} cmdGetPrevError;
2009-04-21 15:08:20 -04:00
2009-09-01 12:17:41 -04:00
class CmdShardingGetLastError : public Command {
2009-02-26 15:55:00 -05:00
public:
2010-02-26 14:38:51 -05:00
virtual LockType locktype(){ return NONE; }
2009-02-26 15:55:00 -05:00
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
2009-09-01 16:23:56 -04: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 11:33:42 -04: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 15:55:00 -05:00
}
} cmdGetLastError;
2009-09-14 11:33:42 -04:00
}
2009-01-14 17:09:51 -05:00
} // namespace mongo