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