Files
mongo/s/server.cpp

203 lines
5.3 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/>.
*/
#include "stdafx.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"
2009-01-14 17:09:51 -05:00
namespace mongo {
2009-11-02 16:52:16 -05:00
Database *database = 0;
2009-02-03 17:10:44 -05:00
string ourHostname;
OID serverID;
bool dbexitCalled = false;
2009-11-02 16:52:16 -05:00
CmdLine cmdLine;
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;
}
2009-02-03 13:15:54 -05:00
void usage( char * argv[] ){
out() << argv[0] << " usage:\n\n";
2009-02-05 11:50:27 -05:00
out() << " -v+ verbose\n";
out() << " --port <portno>\n";
2009-02-05 11:50:27 -05:00
out() << " --configdb <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 ){
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 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 ){
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() ){
BSONObj err = BSON( "$err" << e.what() );
replyToQuery( QueryResult::ResultFlag_ErrSet, p , m , err );
2009-02-19 12:55:01 -05:00
}
}
}
};
void init(){
serverID.init();
2009-11-03 15:10:24 -05:00
setupSIGTRAPforGDB();
}
void start() {
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( "createDirectClient not implemented for sharding yet" , 0 );
return 0;
}
2009-01-14 17:09:51 -05:00
} // namespace mongo
using namespace mongo;
2008-12-28 20:28:49 -05:00
int main(int argc, char* argv[], char *envp[] ) {
2009-02-18 10:10:39 -05:00
bool justTests = false;
2009-02-05 11:50:27 -05:00
vector<string> configdbs;
2009-02-03 17:10:44 -05:00
2008-09-11 16:25:16 -04:00
for (int i = 1; i < argc; i++) {
2008-12-28 20:28:49 -05:00
if ( argv[i] == 0 ) continue;
2008-09-11 16:25:16 -04:00
string s = argv[i];
2008-12-28 20:28:49 -05:00
if ( s == "--port" ) {
2009-11-02 16:52:16 -05:00
cmdLine.port = atoi(argv[++i]);
2008-09-11 16:25:16 -04:00
}
2009-02-05 11:50:27 -05:00
else if ( s == "--configdb" ) {
2009-02-18 10:10:39 -05:00
2009-02-03 17:10:44 -05:00
while ( ++i < argc )
2009-02-05 11:50:27 -05:00
configdbs.push_back(argv[i]);
2009-02-03 17:10:44 -05:00
2009-02-05 11:50:27 -05:00
if ( configdbs.size() == 0 ) {
out() << "error: no args for --configdb\n";
return 4;
}
2009-02-03 17:10:44 -05:00
2009-02-05 11:50:27 -05:00
if ( configdbs.size() > 2 ) {
out() << "error: --configdb does not support more than 2 parameters yet\n";
return 5;
}
2008-11-13 15:52:20 -05:00
}
2009-02-05 11:50:27 -05:00
else if ( s.find( "-v" ) == 0 ){
logLevel = s.size() - 1;
}
2009-02-18 10:10:39 -05:00
else if ( s == "--test" ) {
justTests = true;
logLevel = 5;
}
2008-12-28 20:28:49 -05:00
else {
2009-02-03 13:15:54 -05:00
usage( argv );
2008-09-29 18:00:53 -04:00
return 3;
}
2008-09-11 16:25:16 -04:00
}
2009-02-03 17:10:44 -05:00
2009-02-18 10:10:39 -05:00
UnitTest::runTests();
if ( justTests ){
cout << "tests passed" << endl;
return 0;
2009-02-17 14:41:31 -05:00
}
2009-02-18 10:10:39 -05:00
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;
}
2009-12-03 15:03:20 -05:00
log() << argv[0] << " v0.2.6 (alpha 2f) starting (--help for usage)" << endl;
2009-03-17 10:22:26 -04:00
printGitVersion();
2009-03-18 14:15:52 -04:00
printSysInfo();
if ( ! configServer.init( configdbs ) ){
cout << "couldn't connectd to config db" << endl;
2009-02-03 17:10:44 -05:00
return 7;
}
2009-02-03 17:10:44 -05:00
assert( configServer.ok() );
int configError = configServer.checkConfigVersion();
if ( configError ){
cout << "config server error: " << configError << endl;
return configError;
}
2009-02-03 17:10:44 -05:00
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);
}