Files
mongo/s/server.cpp

430 lines
12 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"
#include "../util/net/message.h"
#include "../util/unittest.h"
2008-10-31 19:17:54 -05:00
#include "../client/connpool.h"
#include "../util/net/message_server.h"
#include "../util/stringutils.h"
#include "../util/version.h"
#include "../util/ramlog.h"
#include "../util/signal_handlers.h"
#include "../util/admin_access.h"
#include "../util/concurrency/task.h"
#include "../db/dbwebserver.h"
2011-06-13 14:53:33 -07:00
#include "../scripting/engine.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"
2010-12-27 16:27:32 -05:00
#include "client.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"
#include "grid.h"
2010-08-02 14:50:12 -04:00
#include "cursors.h"
#include "shard_version.h"
2009-01-14 17:09:51 -05:00
namespace mongo {
2011-01-04 00:40:41 -05:00
CmdLine cmdLine;
Database *database = 0;
string mongosCommand;
bool dbexitCalled = false;
static bool scriptingEnabled = true;
2011-01-04 00:40:41 -05:00
bool inShutdown() {
return dbexitCalled;
}
2011-01-04 00:40:41 -05:00
string getDbContext() {
return "?";
}
2008-12-04 18:11:25 -05:00
2011-01-04 00:40:41 -05:00
bool haveLocalShardingInfo( const string& ns ) {
assert( 0 );
2009-09-10 10:41:17 -04:00
return false;
}
2011-01-04 00:40:41 -05:00
void usage( char * argv[] ) {
2009-02-03 13:15:54 -05:00
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;
}
2011-06-22 14:22:09 -04:00
void ShardingConnectionHook::onHandedOut( DBClientBase * conn ) {
ClientInfo::get()->addShard( conn->getServerAddress() );
}
2011-01-04 00:40:41 -05:00
class ShardedMessageHandler : public MessageHandler {
public:
2011-01-04 00:40:41 -05:00
virtual ~ShardedMessageHandler() {}
2010-05-28 17:07:18 -04:00
virtual void connected( AbstractMessagingPort* p ) {
2011-06-22 14:22:09 -04:00
ClientInfo *c = ClientInfo::get();
massert(15849, "client info not defined", c);
c->getAuthenticationInfo()->isLocalHost = p->remote().isLocalHost();
}
virtual void process( Message& m , AbstractMessagingPort* p , LastError * le) {
2010-04-28 13:19:45 -04:00
assert( p );
Request r( m , p );
assert( le );
lastError.startRequest( m , le );
2011-01-04 00:40:41 -05:00
2009-02-19 12:55:01 -05:00
try {
r.init();
2009-02-19 12:55:01 -05:00
r.process();
}
2011-01-04 00:40:41 -05:00
catch ( AssertionException & e ) {
log( e.isUserAssertion() ? 1 : 0 ) << "AssertionException while processing op type : " << m.operation() << " to : " << r.getns() << causedBy(e) << endl;
2010-12-29 00:52:03 -05:00
le->raiseError( e.getCode() , e.what() );
2011-01-04 00:40:41 -05:00
2010-12-29 00:52:03 -05:00
m.header()->id = r.id();
2011-01-04 00:40:41 -05:00
if ( r.expectResponse() ) {
2010-12-29 00:52:03 -05:00
BSONObj err = BSON( "$err" << e.what() << "code" << e.getCode() );
replyToQuery( ResultFlag_ErrSet, p , m , err );
}
}
2011-01-04 00:40:41 -05:00
catch ( DBException& e ) {
log() << "DBException in process: " << e.what() << endl;
2011-01-04 00:40:41 -05:00
le->raiseError( e.getCode() , e.what() );
2011-01-04 00:40:41 -05:00
2010-05-12 15:26:00 -07:00
m.header()->id = r.id();
2011-01-04 00:40:41 -05:00
if ( r.expectResponse() ) {
2010-04-14 16:15:57 -04:00
BSONObj err = BSON( "$err" << e.what() << "code" << e.getCode() );
2010-07-18 13:34:16 -04:00
replyToQuery( ResultFlag_ErrSet, p , m , err );
2009-02-19 12:55:01 -05:00
}
}
}
2010-05-28 17:07:18 -04:00
2011-01-04 00:40:41 -05:00
virtual void disconnected( AbstractMessagingPort* p ) {
// all things are thread local
2010-05-28 17:07:18 -04:00
}
};
2011-01-04 00:40:41 -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());
}
2011-01-04 00:40:41 -05:00
// this gets called when new fails to allocate memory
void my_new_handler() {
rawOut( "out of memory, printing stack and exiting:" );
printStackTrace();
::exit(EXIT_ABRUPT);
}
2011-01-04 00:40:41 -05:00
void setupSignals( bool inFork ) {
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);
2010-08-17 12:25:39 -04:00
#if defined(SIGQUIT)
signal( SIGQUIT , printStackAndExit );
2010-08-17 12:25:39 -04:00
#endif
signal( SIGSEGV , printStackAndExit );
signal( SIGABRT , printStackAndExit );
signal( SIGFPE , printStackAndExit );
2010-08-17 12:25:39 -04:00
#if defined(SIGBUS)
signal( SIGBUS , printStackAndExit );
2010-08-17 12:25:39 -04:00
#endif
set_new_handler( my_new_handler );
}
2011-01-04 00:40:41 -05:00
void init() {
serverID.init();
2009-11-03 15:10:24 -05:00
setupSIGTRAPforGDB();
setupCoreSignals();
setupSignals( false );
Logstream::get().addGlobalTee( new RamLog("global") );
}
2011-01-04 00:40:41 -05:00
void start( const MessageServer::Options& opts ) {
2010-09-17 14:35:37 -04:00
setThreadName( "mongosMain" );
installChunkShardVersioning();
2010-04-22 13:34:53 -04:00
balancer.go();
2010-08-02 14:50:12 -04:00
cursorCache.startTimeoutThread();
PeriodicTask::theRunner->go();
2010-04-22 13:34:53 -04:00
ShardedMessageHandler handler;
MessageServer * server = createServer( opts , &handler );
2010-08-02 14:50:12 -04:00
server->setAsTimeTracker();
server->run();
}
2008-09-11 16:25:16 -04:00
2011-01-04 00:40:41 -05:00
DBClientBase *createDirectClient() {
uassert( 10197 , "createDirectClient not implemented for sharding yet" , 0 );
2009-05-14 16:57:57 -04:00
return 0;
}
2011-10-27 15:05:36 -04:00
void printShardingVersionInfo(bool out) {
if (out) {
cout << mongosCommand << " " << mongodVersion() << " starting (--help for usage)" << endl;
cout << "git version: " << gitVersion() << endl;
cout << "build sys info: " << sysInfo() << endl;
} else {
log() << mongosCommand << " " << mongodVersion() << " starting (--help for usage)" << endl;
printGitVersion();
printSysInfo();
}
}
void cloudCmdLineParamIs(string cmd);
2009-01-14 17:09:51 -05:00
} // namespace mongo
using namespace mongo;
#include <boost/program_options.hpp>
namespace po = boost::program_options;
2011-02-09 00:02:42 -05:00
int _main(int argc, char* argv[]) {
static StaticObserver staticObserver;
mongosCommand = argv[0];
po::options_description options("General options");
po::options_description sharding_options("Sharding options");
po::options_description hidden("Hidden options");
po::positional_options_description positional;
2011-01-04 00:40:41 -05:00
CmdLine::addGlobalOptions( options , hidden );
2011-01-04 00:40:41 -05:00
sharding_options.add_options()
2011-01-04 00:40:41 -05:00
( "configdb" , po::value<string>() , "1 or 3 comma separated config servers" )
( "test" , "just run unit tests" )
( "upgrade" , "upgrade meta data version" )
( "chunkSize" , po::value<int>(), "maximum amount of data per chunk" )
( "ipv6", "enable IPv6 support (disabled by default)" )
( "jsonp","allow JSONP access via http (has security implications)" )
("noscripting", "disable scripting engine")
2011-01-04 00:40:41 -05:00
;
options.add(sharding_options);
// parse options
po::variables_map params;
if ( ! CmdLine::store( argc , argv , options , hidden , positional , params ) )
return 0;
2011-01-04 00:40:41 -05:00
// The default value may vary depending on compile options, but for mongos
// we want durability to be disabled.
cmdLine.dur = false;
2011-01-04 00:40:41 -05:00
if ( params.count( "help" ) ) {
cout << options << endl;
return 0;
}
2011-01-04 00:40:41 -05:00
if ( params.count( "version" ) ) {
2011-10-27 15:05:36 -04:00
printShardingVersionInfo(true);
return 0;
}
2011-01-04 00:40:41 -05:00
if ( params.count( "chunkSize" ) ) {
2011-10-13 14:45:38 -04:00
int csize = params["chunkSize"].as<int>();
// validate chunksize before proceeding
if ( csize == 0 ) {
out() << "error: need a non-zero chunksize" << endl;
return 11;
}
Chunk::MaxChunkSize = csize * 1024 * 1024;
2010-04-30 02:30:43 -04:00
}
2011-01-04 00:40:41 -05:00
if ( params.count( "ipv6" ) ) {
2010-07-19 12:17:03 -04:00
enableIPv6();
}
2011-01-04 00:40:41 -05:00
if ( params.count( "jsonp" ) ) {
cmdLine.jsonp = true;
}
2011-01-04 00:40:41 -05: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
}
2011-01-04 00:40:41 -05:00
if (params.count("noscripting")) {
scriptingEnabled = false;
}
2011-01-04 00:40:41 -05:00
if ( ! params.count( "configdb" ) ) {
out() << "error: no args for --configdb" << endl;
return 4;
}
if( params.count("cloud") ) {
string s = params["cloud"].as<string>();
cloudCmdLineParamIs(s);
}
vector<string> configdbs;
splitStringDelim( params["configdb"].as<string>() , &configdbs , ',' );
2011-01-04 00:40:41 -05:00
if ( configdbs.size() != 1 && configdbs.size() != 3 ) {
out() << "need either 1 or 3 configdbs" << endl;
return 5;
}
2011-05-26 12:03:34 +02:00
// we either have a setting where all processes are in localhost or none are
2011-01-04 00:40:41 -05:00
for ( vector<string>::const_iterator it = configdbs.begin() ; it != configdbs.end() ; ++it ) {
try {
HostAndPort configAddr( *it ); // will throw if address format is invalid
2011-01-04 00:40:41 -05:00
if ( it == configdbs.begin() ) {
grid.setAllowLocalHost( configAddr.isLocalHost() );
}
2011-01-04 00:40:41 -05:00
if ( configAddr.isLocalHost() != grid.allowLocalHost() ) {
out() << "cannot mix localhost and ip addresses in configdbs" << endl;
return 10;
}
2011-01-04 00:40:41 -05:00
}
catch ( DBException& e) {
out() << "configdb: " << e.what() << endl;
return 9;
}
}
// set some global state
2011-01-04 00:40:41 -05:00
pool.addHook( new ShardingConnectionHook( false ) );
pool.setName( "mongos connectionpool" );
shardConnectionPool.addHook( new ShardingConnectionHook( true ) );
shardConnectionPool.setName( "mongos shardconnection connectionpool" );
DBClientConnection::setLazyKillCursor( false );
2010-12-20 12:52:31 -05:00
ReplicaSetMonitor::setConfigChangeHook( boost::bind( &ConfigServer::replicaSetChange , &configServer , _1 ) );
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;
}
2011-01-04 00:40:41 -05:00
2011-10-27 15:05:36 -04:00
printShardingVersionInfo(false);
2011-01-04 00:40:41 -05:00
if ( ! configServer.init( configdbs ) ) {
2010-07-04 11:30:59 -04:00
cout << "couldn't resolve config db address" << endl;
2009-02-03 17:10:44 -05:00
return 7;
}
2011-01-04 00:40:41 -05:00
if ( ! configServer.ok( true ) ) {
2011-10-28 09:32:58 -04:00
cout << "configServer connection startup check failed" << endl;
return 8;
}
2011-01-04 00:40:41 -05:00
{
class CheckConfigServers : public task::Task {
virtual string name() const { return "CheckConfigServers"; }
virtual void doWork() { configServer.ok(true); }
};
static CheckConfigServers checkConfigServers;
task::repeat(&checkConfigServers, 60*1000);
}
int configError = configServer.checkConfigVersion( params.count( "upgrade" ) );
2011-01-04 00:40:41 -05:00
if ( configError ) {
if ( configError > 0 ) {
cout << "upgrade success!" << endl;
}
else {
cout << "config server error: " << configError << endl;
}
return configError;
}
configServer.reloadSettings();
init();
2011-10-25 12:37:07 -04:00
#ifndef _WIN32
CmdLine::launchOk();
2011-10-25 12:37:07 -04:00
#endif
boost::thread web( boost::bind(&webServerThread, new NoAdminAccess() /* takes ownership */) );
2011-01-04 00:40:41 -05:00
if ( scriptingEnabled ) {
ScriptEngine::setup();
// globalScriptEngine->setCheckInterruptCallback( jsInterruptCallback );
// globalScriptEngine->setGetInterruptSpecCallback( jsGetInterruptSpecCallback );
}
MessageServer::Options opts;
opts.port = cmdLine.port;
2010-07-27 09:59:34 -04:00
opts.ipList = cmdLine.bind_ip;
start(opts);
dbexit( EXIT_CLEAN );
2008-12-28 20:28:49 -05:00
return 0;
}
2011-02-09 00:02:42 -05:00
int main(int argc, char* argv[]) {
try {
2011-06-17 14:57:16 -04:00
doPreServerStatupInits();
2011-02-09 00:02:42 -05:00
return _main(argc, argv);
}
catch(DBException& e) {
cout << "uncaught exception in mongos main:" << endl;
cout << e.toString() << endl;
}
catch(std::exception& e) {
cout << "uncaught exception in mongos main:" << endl;
cout << e.what() << endl;
}
catch(...) {
cout << "uncaught exception in mongos main" << endl;
}
return 20;
}
#undef exit
2011-04-05 02:24:16 -04:00
void mongo::exitCleanly( ExitCode code ) {
// TODO: do we need to add anything?
mongo::dbexit( code );
}
void mongo::dbexit( ExitCode rc, const char *why, bool tryToGetLock ) {
dbexitCalled = true;
2011-01-04 00:40:41 -05:00
log() << "dbexit: " << why
<< " rc:" << rc
<< " " << ( why ? why : "" )
<< endl;
2009-02-03 10:12:01 -05:00
::exit(rc);
}