Files
mongo/s/server.cpp

264 lines
7.2 KiB
C++
Raw Normal View History

2009-02-03 13:15:54 -05:00
// server.cpp
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -05: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
*
* 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
*
* 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"
#include "../util/unittest.h"
2008-10-31 19:17:54 -05:00
#include "../client/connpool.h"
#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"
#include "chunk.h"
2010-04-22 13:34:53 -04:00
#include "balance.h"
2009-01-14 17:09:51 -05:00
namespace mongo {
CmdLine cmdLine;
Database *database = 0;
string mongosCommand;
2009-02-03 17:10:44 -05:00
string ourHostname;
OID serverID;
bool dbexitCalled = false;
bool inShutdown(){
return dbexitCalled;
}
string getDbContext() {
return "?";
}
2008-12-04 18:11:25 -05:00
bool haveLocalShardingInfo( const string& ns ){
assert( 0 );
2009-09-10 10:41:17 -04:00
return false;
}
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";
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 11:33:42 -04: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 ){
2010-04-28 13:19:45 -04:00
assert( p );
Request r( m , p );
LastError * le = lastError.startRequest( m , r.getClientId() );
2010-04-28 13:19:45 -04:00
assert( le );
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 ){
le->raiseError( e.getCode() , e.what() );
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() );
replyToQuery( QueryResult::ResultFlag_ErrSet, p , m , err );
2009-02-19 12:55:01 -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());
}
void setupSignals(){
// needed for cmdLine, btu we do it in init()
}
void init(){
serverID.init();
2009-11-03 15:10:24 -05:00
setupSIGTRAPforGDB();
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);
}
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;
//DbGridListener l(port);
//l.listen();
ShardedMessageHandler handler;
2009-11-02 16:52:16 -05:00
MessageServer * server = createServer( cmdLine.port , &handler );
server->run();
}
2008-09-11 16:25:16 -04:00
2009-05-14 16:57:57 -04:00
DBClientBase *createDirectClient(){
uassert( 10197 , "createDirectClient not implemented for sharding yet" , 0 );
2009-05-14 16:57:57 -04:00
return 0;
}
void printShardingVersionInfo(){
2010-04-08 10:57:05 -04:00
log() << mongosCommand << " " << mongodVersion() << " starting (--help for usage)" << endl;
printGitVersion();
printSysInfo();
}
2009-01-14 17:09:51 -05:00
} // namespace mongo
using namespace mongo;
#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[] ) {
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 10:10:39 -05:00
CmdLine::addGlobalOptions( options , hidden );
2009-02-03 17:10:44 -05:00
options.add_options()
( "configdb" , po::value<string>() , "1 or 3 comma separated config servers" )
( "test" , "just run unit tests" )
( "upgrade" , "upgrade meta data version" )
2010-04-30 02:30:43 -04:00
( "chunkSize" , po::value<int>(), "maximum amount of data per chunk" )
;
// 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
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;
}
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
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 14:41:31 -05:00
if ( argc <= 1 ) {
usage( argv );
return 3;
}
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;
}
printShardingVersionInfo();
if ( ! configServer.init( configdbs ) ){
cout << "couldn't connectd to config db" << endl;
2009-02-03 17:10:44 -05:00
return 7;
}
if ( ! configServer.ok() ){
cout << "configServer startup check failed" << endl;
return 8;
}
int configError = configServer.checkConfigVersion( params.count( "upgrade" ) );
if ( configError ){
if ( configError > 0 ){
cout << "upgrade success!" << endl;
}
else {
cout << "config server error: " << configError << endl;
}
return configError;
}
configServer.reloadSettings();
init();
2008-09-11 16:25:16 -04:00
start();
dbexit( EXIT_CLEAN );
2008-12-28 20:28:49 -05:00
return 0;
}
#undef exit
void mongo::dbexit( ExitCode rc, const char *why) {
dbexitCalled = true;
2009-02-03 10:12:01 -05:00
log() << "dbexit: " << why << " rc:" << rc << endl;
::exit(rc);
}