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

244 lines
6.5 KiB
C++
Raw Normal View History

2009-02-03 19:15:54 +01:00
// server.cpp
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01: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
*
* 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
*
* 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"
#include "../util/unittest.h"
2008-11-01 01:17:54 +01:00
#include "../client/connpool.h"
#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"
#include "chunk.h"
2009-01-14 23:09:51 +01:00
namespace mongo {
CmdLine cmdLine;
Database *database = 0;
string mongosCommand;
2009-02-03 23:10:44 +01:00
string ourHostname;
OID serverID;
bool dbexitCalled = false;
bool inShutdown(){
return dbexitCalled;
}
string getDbContext() {
return "?";
}
2008-12-05 00:11:25 +01:00
bool haveLocalShardingInfo( const string& ns ){
assert( 0 );
2009-09-10 16:41:17 +02:00
return false;
}
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";
out() << " --port <portno>\n";
out() << " --configdb <configdbname>,[<configdbname>,<configdbname>]\n";
out() << endl;
}
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() );
}
} shardingConnectionHook;
class ShardedMessageHandler : public MessageHandler {
public:
virtual ~ShardedMessageHandler(){}
virtual void process( Message& m , AbstractMessagingPort* p ){
Request r( m , p );
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() );
replyToQuery( QueryResult::ResultFlag_ErrSet, p , m , err );
2009-02-19 18:55:01 +01:00
}
}
}
};
void sighandler(int sig){
dbexit(EXIT_CLEAN, (string("recieved signal ") + BSONObjBuilder::numStr(sig)).c_str());
}
void setupSignals(){
// needed for cmdLine, btu we do it in init()
}
void init(){
serverID.init();
2009-11-03 21:10:24 +01:00
setupSIGTRAPforGDB();
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);
}
void start() {
2009-11-02 22:52:16 +01:00
log() << "waiting for connections on port " << cmdLine.port << endl;
//DbGridListener l(port);
//l.listen();
ShardedMessageHandler handler;
2009-11-02 22:52:16 +01:00
MessageServer * server = createServer( cmdLine.port , &handler );
server->run();
}
2008-09-11 22:25:16 +02:00
2009-05-14 22:57:57 +02:00
DBClientBase *createDirectClient(){
uassert( 10197 , "createDirectClient not implemented for sharding yet" , 0 );
2009-05-14 22:57:57 +02:00
return 0;
}
void printShardingVersionInfo(){
2010-03-23 03:12:51 +01:00
log() << mongosCommand << " v0.3 (alpha 3) starting (--help for usage)" << endl;
printGitVersion();
printSysInfo();
}
2009-01-14 23:09:51 +01:00
} // namespace mongo
using namespace mongo;
#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[] ) {
static StaticObserver staticObserver;
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
CmdLine::addGlobalOptions( options , hidden );
2009-02-03 23:10:44 +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
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
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;
}
pool.addHook( &shardingConnectionHook );
2009-02-17 20:41:31 +01:00
if ( argc <= 1 ) {
usage( argv );
return 3;
}
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;
}
printShardingVersionInfo();
if ( ! configServer.init( configdbs ) ){
cout << "couldn't connectd to config db" << endl;
2009-02-03 23:10:44 +01:00
return 7;
}
if ( ! configServer.ok() ){
cout << "configServer startup check failed" << endl;
return 8;
}
int configError = configServer.checkConfigVersion();
if ( configError ){
cout << "config server error: " << configError << endl;
return configError;
}
configServer.reloadSettings();
init();
2008-09-11 22:25:16 +02:00
start();
dbexit( EXIT_CLEAN );
2008-12-29 02:28:49 +01:00
return 0;
}
#undef exit
void mongo::dbexit( ExitCode rc, const char *why) {
dbexitCalled = true;
2009-02-03 16:12:01 +01:00
log() << "dbexit: " << why << " rc:" << rc << endl;
::exit(rc);
}