2009-02-03 13:15:54 -05:00
|
|
|
// server.cpp
|
2008-09-11 10:45:57 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-28 20:28:49 -05:00
|
|
|
*
|
2008-09-11 10:45:57 -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-09-11 10:45:57 -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-09-11 10:45:57 -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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-04-27 15:27:52 -04:00
|
|
|
#include "pch.h"
|
2009-02-03 13:30:28 -05:00
|
|
|
#include "../util/message.h"
|
2008-09-11 10:45:57 -04:00
|
|
|
#include "../util/unittest.h"
|
2008-10-31 19:17:54 -05:00
|
|
|
#include "../client/connpool.h"
|
2009-03-02 13:35:34 -05:00
|
|
|
#include "../util/message_server.h"
|
2009-02-03 17:10:44 -05:00
|
|
|
|
2009-02-05 11:50:27 -05:00
|
|
|
#include "server.h"
|
2009-02-19 12:55:01 -05:00
|
|
|
#include "request.h"
|
2009-02-12 21:03:46 -05:00
|
|
|
#include "config.h"
|
2009-08-31 16:31:50 -04:00
|
|
|
#include "chunk.h"
|
2010-04-22 13:34:53 -04:00
|
|
|
#include "balance.h"
|
2008-09-11 10:45:57 -04:00
|
|
|
|
2009-01-14 17:09:51 -05:00
|
|
|
namespace mongo {
|
2010-03-31 11:57:27 -04:00
|
|
|
|
|
|
|
|
CmdLine cmdLine;
|
2009-01-15 10:17:11 -05:00
|
|
|
Database *database = 0;
|
2010-03-18 12:15:29 -04:00
|
|
|
string mongosCommand;
|
2009-02-03 17:10:44 -05:00
|
|
|
string ourHostname;
|
2009-04-17 17:11:32 -04:00
|
|
|
OID serverID;
|
2009-08-05 17:15:04 -04:00
|
|
|
bool dbexitCalled = false;
|
2010-04-13 15:23:57 -04:00
|
|
|
|
2009-08-05 17:15:04 -04:00
|
|
|
bool inShutdown(){
|
|
|
|
|
return dbexitCalled;
|
|
|
|
|
}
|
2009-04-17 17:11:32 -04:00
|
|
|
|
2009-01-15 10:17:11 -05:00
|
|
|
string getDbContext() {
|
|
|
|
|
return "?";
|
|
|
|
|
}
|
2008-12-04 18:11:25 -05:00
|
|
|
|
2009-09-10 10:36:11 -04:00
|
|
|
bool haveLocalShardingInfo( const string& ns ){
|
|
|
|
|
assert( 0 );
|
2009-09-10 10:41:17 -04:00
|
|
|
return false;
|
2009-09-10 10:36:11 -04:00
|
|
|
}
|
2010-03-20 23:46:19 -04:00
|
|
|
|
2009-02-03 13:15:54 -05:00
|
|
|
void usage( char * argv[] ){
|
|
|
|
|
out() << argv[0] << " usage:\n\n";
|
2010-03-20 23:46:19 -04:00
|
|
|
out() << " -v+ verbose 1: general 2: more 3: per request 4: more\n";
|
2009-01-15 11:26:38 -05:00
|
|
|
out() << " --port <portno>\n";
|
2010-03-18 12:15:29 -04:00
|
|
|
out() << " --configdb <configdbname>,[<configdbname>,<configdbname>]\n";
|
2009-01-15 11:26:38 -05:00
|
|
|
out() << endl;
|
2009-01-15 10:17:11 -05:00
|
|
|
}
|
2009-09-11 16:14:14 -04:00
|
|
|
|
|
|
|
|
class ShardingConnectionHook : public DBConnectionHook {
|
|
|
|
|
public:
|
|
|
|
|
virtual void onCreate( DBClientBase * conn ){
|
|
|
|
|
conn->simpleCommand( "admin" , 0 , "switchtoclienterrors" );
|
|
|
|
|
}
|
2009-09-14 11:33:42 -04:00
|
|
|
virtual void onHandedOut( DBClientBase * conn ){
|
|
|
|
|
ClientInfo::get()->addShard( conn->getServerAddress() );
|
|
|
|
|
}
|
2009-09-11 16:14:14 -04:00
|
|
|
} shardingConnectionHook;
|
2009-03-02 13:35:34 -05:00
|
|
|
|
|
|
|
|
class ShardedMessageHandler : public MessageHandler {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~ShardedMessageHandler(){}
|
|
|
|
|
virtual void process( Message& m , AbstractMessagingPort* p ){
|
2010-04-28 13:19:45 -04:00
|
|
|
assert( p );
|
2009-03-02 13:35:34 -05:00
|
|
|
Request r( m , p );
|
2010-04-21 22:32:26 -04:00
|
|
|
|
2010-04-28 15:39:52 -04:00
|
|
|
LastError * le = lastError.startRequest( m , r.getClientId() );
|
2010-04-28 13:19:45 -04:00
|
|
|
assert( le );
|
2010-04-21 22:32:26 -04:00
|
|
|
|
2009-09-14 14:32:24 -04:00
|
|
|
if ( logLevel > 5 ){
|
|
|
|
|
log(5) << "client id: " << hex << r.getClientId() << "\t" << r.getns() << "\t" << dec << r.op() << endl;
|
|
|
|
|
}
|
2009-02-19 12:55:01 -05:00
|
|
|
try {
|
2009-09-14 11:33:42 -04:00
|
|
|
setClientId( r.getClientId() );
|
2009-02-19 12:55:01 -05:00
|
|
|
r.process();
|
|
|
|
|
}
|
|
|
|
|
catch ( DBException& e ){
|
2010-04-21 22:32:26 -04:00
|
|
|
le->raiseError( e.getCode() , e.what() );
|
2010-04-28 15:39:52 -04:00
|
|
|
|
2009-11-24 13:56:41 -05:00
|
|
|
m.data->id = r.id();
|
2009-02-19 12:55:01 -05:00
|
|
|
log() << "UserException: " << e.what() << endl;
|
|
|
|
|
if ( r.expectResponse() ){
|
2010-04-14 16:15:57 -04:00
|
|
|
BSONObj err = BSON( "$err" << e.what() << "code" << e.getCode() );
|
2009-03-02 13:35:34 -05:00
|
|
|
replyToQuery( QueryResult::ResultFlag_ErrSet, p , m , err );
|
2009-02-19 12:55:01 -05:00
|
|
|
}
|
|
|
|
|
}
|
2009-01-15 10:17:11 -05:00
|
|
|
}
|
|
|
|
|
};
|
2010-01-25 15:19:30 -05:00
|
|
|
|
|
|
|
|
void sighandler(int sig){
|
2010-05-08 11:47:17 -04:00
|
|
|
dbexit(EXIT_CLEAN, (string("received signal ") + BSONObjBuilder::numStr(sig)).c_str());
|
2010-01-25 15:19:30 -05:00
|
|
|
}
|
2009-09-14 14:32:24 -04:00
|
|
|
|
2010-03-18 16:38:27 -04:00
|
|
|
void setupSignals(){
|
|
|
|
|
// needed for cmdLine, btu we do it in init()
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-17 17:11:32 -04:00
|
|
|
void init(){
|
|
|
|
|
serverID.init();
|
2009-11-03 15:10:24 -05:00
|
|
|
setupSIGTRAPforGDB();
|
2010-01-25 15:19:30 -05:00
|
|
|
signal(SIGTERM, sighandler);
|
|
|
|
|
signal(SIGINT, sighandler);
|
2009-04-17 17:11:32 -04:00
|
|
|
}
|
|
|
|
|
|
2009-01-15 10:17:11 -05:00
|
|
|
void start() {
|
2010-04-22 13:34:53 -04:00
|
|
|
balancer.go();
|
|
|
|
|
|
2009-11-02 16:52:16 -05:00
|
|
|
log() << "waiting for connections on port " << cmdLine.port << endl;
|
2009-03-02 13:35:34 -05:00
|
|
|
//DbGridListener l(port);
|
|
|
|
|
//l.listen();
|
|
|
|
|
ShardedMessageHandler handler;
|
2009-11-02 16:52:16 -05:00
|
|
|
MessageServer * server = createServer( cmdLine.port , &handler );
|
2009-03-02 13:35:34 -05:00
|
|
|
server->run();
|
2009-01-15 10:17:11 -05:00
|
|
|
}
|
2008-09-11 16:25:16 -04:00
|
|
|
|
2009-05-14 16:57:57 -04:00
|
|
|
DBClientBase *createDirectClient(){
|
2009-12-28 16:43:43 -05:00
|
|
|
uassert( 10197 , "createDirectClient not implemented for sharding yet" , 0 );
|
2009-05-14 16:57:57 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-18 12:15:29 -04:00
|
|
|
void printShardingVersionInfo(){
|
2010-04-08 10:57:05 -04:00
|
|
|
log() << mongosCommand << " " << mongodVersion() << " starting (--help for usage)" << endl;
|
2010-03-18 12:15:29 -04:00
|
|
|
printGitVersion();
|
|
|
|
|
printSysInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-14 17:09:51 -05:00
|
|
|
} // namespace mongo
|
|
|
|
|
|
|
|
|
|
using namespace mongo;
|
|
|
|
|
|
2010-03-18 12:15:29 -04:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
2008-12-28 20:28:49 -05:00
|
|
|
int main(int argc, char* argv[], char *envp[] ) {
|
2010-03-15 10:02:12 -07:00
|
|
|
static StaticObserver staticObserver;
|
2010-03-18 12:15:29 -04:00
|
|
|
mongosCommand = argv[0];
|
|
|
|
|
|
|
|
|
|
po::options_description options("Sharding options");
|
|
|
|
|
po::options_description hidden("Hidden options");
|
|
|
|
|
po::positional_options_description positional;
|
2009-02-18 10:10:39 -05:00
|
|
|
|
2010-03-18 12:15:29 -04:00
|
|
|
CmdLine::addGlobalOptions( options , hidden );
|
2009-02-03 17:10:44 -05:00
|
|
|
|
2010-03-18 12:15:29 -04:00
|
|
|
options.add_options()
|
|
|
|
|
( "configdb" , po::value<string>() , "1 or 3 comma separated config servers" )
|
|
|
|
|
( "test" , "just run unit tests" )
|
2010-04-21 17:44:07 -04:00
|
|
|
( "upgrade" , "upgrade meta data version" )
|
2010-04-30 02:30:43 -04:00
|
|
|
( "chunkSize" , po::value<int>(), "maximum amount of data per chunk" )
|
2010-03-18 12:15:29 -04:00
|
|
|
;
|
2010-04-21 17:44:07 -04:00
|
|
|
|
2010-03-18 12:15:29 -04:00
|
|
|
|
|
|
|
|
// parse options
|
|
|
|
|
po::variables_map params;
|
|
|
|
|
if ( ! CmdLine::store( argc , argv , options , hidden , positional , params ) )
|
|
|
|
|
return 0;
|
2009-02-03 17:10:44 -05:00
|
|
|
|
2010-03-18 12:15:29 -04:00
|
|
|
if ( params.count( "help" ) ){
|
|
|
|
|
cout << options << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( params.count( "version" ) ){
|
|
|
|
|
printShardingVersionInfo();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-30 02:30:43 -04:00
|
|
|
if ( params.count( "chunkSize" ) ){
|
|
|
|
|
Chunk::MaxChunkSize = params["chunkSize"].as<int>() * 1024 * 1024;
|
|
|
|
|
}
|
2010-03-18 12:15:29 -04:00
|
|
|
|
|
|
|
|
if ( params.count( "test" ) ){
|
|
|
|
|
logLevel = 5;
|
2009-12-14 09:56:24 -05:00
|
|
|
UnitTest::runTests();
|
2009-02-18 10:10:39 -05:00
|
|
|
cout << "tests passed" << endl;
|
|
|
|
|
return 0;
|
2009-02-17 14:41:31 -05:00
|
|
|
}
|
2009-02-18 10:10:39 -05:00
|
|
|
|
2010-03-18 12:15:29 -04:00
|
|
|
if ( ! params.count( "configdb" ) ){
|
|
|
|
|
out() << "error: no args for --configdb" << endl;
|
|
|
|
|
return 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<string> configdbs;
|
|
|
|
|
{
|
|
|
|
|
string s = params["configdb"].as<string>();
|
|
|
|
|
while ( true ){
|
|
|
|
|
size_t idx = s.find( ',' );
|
|
|
|
|
if ( idx == string::npos ){
|
|
|
|
|
configdbs.push_back( s );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
configdbs.push_back( s.substr( 0 , idx ) );
|
|
|
|
|
s = s.substr( idx + 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( configdbs.size() != 1 && configdbs.size() != 3 ){
|
|
|
|
|
out() << "need either 1 or 3 configdbs" << endl;
|
|
|
|
|
return 5;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-11 16:14:14 -04:00
|
|
|
pool.addHook( &shardingConnectionHook );
|
|
|
|
|
|
2009-02-17 14:41:31 -05:00
|
|
|
if ( argc <= 1 ) {
|
|
|
|
|
usage( argv );
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-08 11:41:19 -05:00
|
|
|
bool ok = cmdLine.port != 0 && configdbs.size();
|
|
|
|
|
|
2008-12-28 20:28:49 -05:00
|
|
|
if ( !ok ) {
|
2009-02-03 13:15:54 -05:00
|
|
|
usage( argv );
|
2008-09-11 16:25:16 -04:00
|
|
|
return 1;
|
|
|
|
|
}
|
2010-03-18 12:15:29 -04:00
|
|
|
|
|
|
|
|
printShardingVersionInfo();
|
2009-11-08 11:41:19 -05:00
|
|
|
|
|
|
|
|
if ( ! configServer.init( configdbs ) ){
|
2009-10-16 11:39:59 -04:00
|
|
|
cout << "couldn't connectd to config db" << endl;
|
2009-02-03 17:10:44 -05:00
|
|
|
return 7;
|
|
|
|
|
}
|
2009-09-10 00:39:30 -04:00
|
|
|
|
2010-03-18 12:15:29 -04:00
|
|
|
if ( ! configServer.ok() ){
|
|
|
|
|
cout << "configServer startup check failed" << endl;
|
|
|
|
|
return 8;
|
|
|
|
|
}
|
2009-09-10 00:39:30 -04:00
|
|
|
|
2010-04-21 17:44:07 -04:00
|
|
|
int configError = configServer.checkConfigVersion( params.count( "upgrade" ) );
|
2009-09-10 00:39:30 -04:00
|
|
|
if ( configError ){
|
2010-04-21 17:44:07 -04:00
|
|
|
if ( configError > 0 ){
|
|
|
|
|
cout << "upgrade success!" << endl;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cout << "config server error: " << configError << endl;
|
|
|
|
|
}
|
2009-09-10 00:39:30 -04:00
|
|
|
return configError;
|
|
|
|
|
}
|
2010-02-12 15:27:43 -05:00
|
|
|
configServer.reloadSettings();
|
|
|
|
|
|
2009-04-17 17:11:32 -04:00
|
|
|
init();
|
2008-09-11 16:25:16 -04:00
|
|
|
start();
|
2009-08-05 16:00:27 -04:00
|
|
|
dbexit( EXIT_CLEAN );
|
2008-12-28 20:28:49 -05:00
|
|
|
return 0;
|
2008-09-11 10:45:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef exit
|
2009-08-05 16:00:27 -04:00
|
|
|
void mongo::dbexit( ExitCode rc, const char *why) {
|
2009-08-05 17:15:04 -04:00
|
|
|
dbexitCalled = true;
|
2009-02-03 10:12:01 -05:00
|
|
|
log() << "dbexit: " << why << " rc:" << rc << endl;
|
|
|
|
|
::exit(rc);
|
|
|
|
|
}
|