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

533 lines
19 KiB
C++
Raw Normal View History

2008-10-24 23:51:28 +02:00
// dbgrid/request.cpp
/**
* 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"
2009-02-19 18:55:01 +01:00
#include "shard.h"
2008-10-24 23:51:28 +02:00
2009-01-14 23:09:51 +01:00
namespace mongo {
extern string ourHostname;
2009-02-05 22:45:58 +01:00
namespace dbgrid_cmds {
2009-02-05 22:45:58 +01: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-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") { }
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){
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 );
}
result.appendArray( "commands" , arr.done() );
result.append("ok" , 1 );
return true;
}
} listGridCommands;
// ------------ database level commands -------------
2009-02-06 22:25:14 +01: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-02-12 19:47:43 +01:00
ScopedDbConnection conn( configServer.getPrimary() );
auto_ptr<DBClientCursor> cursor = conn->query( "config.databases" , emptyObj );
BSONObjBuilder list;
int num = 0;
while ( cursor->more() ){
string s = BSONObjBuilder::numStr( num++ );
BSONObj o = cursor->next();
list.append( s.c_str() , o["name"].valuestrsafe() );
}
result.appendArray("databases" , list.obj() );
conn.done();
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-02-18 19:54:22 +01: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;
}
DBConfig * config = grid.getDBConfig( dbname , false );
if ( ! config ){
errmsg = "can't find db!";
return false;
}
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.knowAboutServer( to ) ){
2009-02-13 22:18:14 +01:00
errmsg = "that server isn't known to me";
return false;
}
ScopedDbConnection conn( configServer.getPrimary() );
2009-02-13 22:18:14 +01:00
log() << "moving " << dbname << " primary from: " << config->getPrimary() << " to: " << to << endl;
// TODO LOCKING: this is not safe with multiple mongos
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() );
config->setPrimary( to );
2009-02-20 21:07:59 +01:00
config->save( true );
2009-02-13 22:18:14 +01:00
log() << " dropping " << dbname << " from old" << endl;
fromconn->dropDatabase( dbname.c_str() );
fromconn.done();
result << "ok" << 1;
result << "primary" << to;
conn.done();
return true;
}
} movePrimary;
2009-02-19 18:55:01 +01:00
2009-02-18 19:54:22 +01:00
class PartitionCmd : public GridAdminCmd {
public:
PartitionCmd() : GridAdminCmd( "partition" ){}
virtual void help( stringstream& help ) const {
help
<< "turns on partitioning for a db. have to do this before sharding, etc.. will work.\n"
<< " { partition : \"alleyinsider\" }\n";
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string dbname = cmdObj["partition"].valuestrsafe();
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 );
if ( config->isPartitioned() ){
errmsg = "already partitioned";
return false;
}
2009-02-06 22:43:00 +01:00
2009-02-18 19:54:22 +01:00
config->turnOnPartitioning();
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
result << "ok" << 1;
return true;
}
} partitionCmd;
// ------------ collection level commands -------------
class ShardCmd : public GridAdminCmd {
public:
ShardCmd() : GridAdminCmd( "shard" ){}
2009-02-19 18:55:01 +01:00
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj["shard"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
if ( ! config->isPartitioned() ){
errmsg = "db not partitioned ";
return false;
}
if ( config->sharded( ns ) ){
errmsg = "already sharded";
return false;
}
BSONObj key = cmdObj.getObjectField( "key" );
if ( key.isEmpty() ){
errmsg = "no shard key";
return false;
}
config->turnOnSharding( ns , key );
2009-02-20 21:07:59 +01:00
config->save( true );
2009-02-19 18:55:01 +01:00
result << "ok" << 1;
return true;
2009-02-18 19:54:22 +01:00
}
} shardCmd;
class SplitCollection : public GridAdminCmd {
public:
SplitCollection() : GridAdminCmd( "split" ){}
virtual void help( stringstream& help ) const {
2009-02-19 23:32:19 +01:00
help
<< " 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-02-19 23:32:19 +01:00
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj["split"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
if ( ! config->sharded( ns ) ){
errmsg = "ns not sharded. have to shard before can split";
return false;
}
BSONObj find = cmdObj.getObjectField( "find" );
bool middle = false;
if ( find.isEmpty() ){
find = cmdObj.getObjectField( "middle" );
middle = true;
}
if ( find.isEmpty() ){
errmsg = "need to specify find or middle";
return false;
}
ShardManager * info = config->getShardManager( ns );
2009-02-19 23:32:19 +01:00
Shard& old = info->findShard( find );
log() << "splitting: " << ns << " on: " << find << endl;
if ( middle )
old.split( cmdObj.getObjectField( "middle" ) );
else
old.split();
info->save();
2009-02-19 23:32:19 +01:00
result << "ok" << 1;
return true;
2009-02-18 19:54:22 +01:00
}
} splitCollectionCmd;
class MoveShard : public GridAdminCmd {
public:
MoveShard() : GridAdminCmd( "moveshard" ){}
virtual void help( stringstream& help ) const {
help << "{ moveshard : 'test.foo' , find : { num : 1 } , to : 'localhost:30001' }";
}
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj["moveshard"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
if ( ! config->sharded( ns ) ){
errmsg = "ns not sharded. have to split before can move a shard";
return false;
}
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() ){
errmsg = "you have to specify where you want to move it";
return false;
}
ShardManager * info = config->getShardManager( ns );
Shard& s = info->findShard( find );
2009-02-20 19:46:57 +01:00
string from = s.getServer();
if ( s.getServer() == to ){
errmsg = "that shard is already on that server";
return false;
}
if ( ! grid.knowAboutServer( to ) ){
errmsg = "that server isn't known to me";
return false;
}
log() << "ns: " << ns << " moving shard: " << s << " to: " << to << endl;
// copyCollection
ScopedDbConnection toconn( to );
BSONObj cloneRes;
2009-02-20 19:46:57 +01:00
BSONObj filter;
{
BSONObjBuilder b;
s.getFilter( b );
filter = b.obj();
}
bool worked = toconn->runCommand( config->getName().c_str() ,
BSON( "cloneCollection" << ns <<
2009-02-20 19:46:57 +01:00
"from" << from <<
"query" << filter
) ,
cloneRes
);
toconn.done();
if ( ! worked ){
errmsg = (string)"cloneCollection failed: " + cloneRes.toString();
return false;
}
// update config db
2009-02-20 19:46:57 +01:00
s.setServer( to );
info->save();
2009-02-20 19:46:57 +01:00
// delete old data
2009-02-20 19:46:57 +01:00
ScopedDbConnection fromconn( from );
fromconn->remove( ns.c_str() , filter );
string removeerror = fromconn->getLastError();
fromconn.done();
if ( removeerror.size() ){
errmsg = (string)"error removing old data:" + removeerror;
return false;
}
result << "ok" << 1;
return true;
}
} moveShardCmd;
2009-02-18 19:54:22 +01:00
// ------------ server level commands -------------
2009-02-06 22:25:14 +01:00
class ListServers : public GridAdminCmd {
public:
ListServers() : GridAdminCmd("listservers") { }
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;
2009-02-09 19:34:40 +01:00
auto_ptr<DBClientCursor> cursor = conn->query( "config.servers" , emptyObj );
2009-02-06 22:25:14 +01:00
while ( cursor->more() ){
BSONObj o = cursor->next();
all.push_back( o );
}
result.append("servers" , all );
result.append("ok" , 1 );
2009-02-09 19:34:40 +01:00
conn.done();
2009-02-06 22:25:14 +01:00
return true;
}
} listServers;
class AddServer : public GridAdminCmd {
public:
AddServer() : GridAdminCmd("addserver") { }
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-06 22:25:14 +01:00
BSONObj server = BSON( "host" << cmdObj["addserver"].valuestrsafe() );
2009-02-09 19:34:40 +01:00
BSONObj old = conn->findOne( "config.servers" , server );
2009-02-06 22:25:14 +01:00
if ( ! old.isEmpty() ){
result.append( "ok" , 0.0 );
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-02-09 19:34:40 +01:00
conn->insert( "config.servers" , server );
2009-02-06 22:25:14 +01:00
result.append( "ok", 1 );
result.append( "added" , server["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-02-06 22:25:14 +01:00
2009-02-08 23:55:33 +01:00
class RemoveServer : public GridAdminCmd {
public:
RemoveServer() : GridAdminCmd("removeserver") { }
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-08 23:55:33 +01:00
BSONObj server = BSON( "host" << cmdObj["removeserver"].valuestrsafe() );
2009-02-09 19:34:40 +01:00
conn->remove( "config.servers" , server );
2009-02-08 23:55:33 +01:00
2009-02-09 19:34:40 +01:00
conn.done();
2009-02-08 23:55:33 +01:00
return true;
}
} removeServer;
2009-02-06 22:25:14 +01:00
2009-02-13 15:28:23 +01:00
// --------------- public commands ----------------
2009-02-26 21:55:00 +01: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-02-26 21:55:00 +01:00
class CmdIsMaster : public Command {
public:
2009-01-19 02:37:17 +01:00
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
CmdIsMaster() : Command("ismaster") { }
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
result.append("ismaster", 0.0);
result.append("msg", "isdbgrid");
return true;
}
} ismaster;
2009-02-26 21:55:00 +01:00
class CmdShardGetPrevError : public Command {
public:
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
CmdShardGetPrevError() : Command("getpreverror") { }
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
errmsg += "getpreverror not supported on mongos";
result << "ok" << 0;
return false;
}
} cmdGetPrevError;
class CmdShardGetLastError : public Command {
public:
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() {
return true;
}
CmdShardGetLastError() : Command("getplasterror") { }
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
errmsg += "getlasterror not working yet";
result << "ok" << 0;
return false;
}
} cmdGetLastError;
}
2009-01-14 23:09:51 +01:00
} // namespace mongo