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

174 lines
4.4 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"
2009-02-03 23:10:44 +01:00
2009-02-05 17:50:27 +01:00
#include "server.h"
#include "configserver.h"
2008-10-20 00:46:53 +02:00
#include "gridconfig.h"
2009-01-14 23:09:51 +01:00
namespace mongo {
int port = 27017;
const char *curNs = "";
Database *database = 0;
2009-02-03 23:10:44 +01:00
DBClientWithCommands* Model::globalConn;
string ourHostname;
string getDbContext() {
return "?";
}
2008-12-05 00:11:25 +01:00
2009-02-03 19:15:54 +01:00
void usage( char * argv[] ){
out() << argv[0] << " usage:\n\n";
2009-02-05 17:50:27 +01:00
out() << " -v+ verbose\n";
out() << " --port <portno>\n";
2009-02-05 17:50:27 +01:00
out() << " --configdb <configdbname> [<configdbname>...]\n";
out() << " --infer infer configdbname by replacing \"-n<n>\"\n";
out() << " in our hostname with \"-grid\".\n";
out() << endl;
}
2008-12-29 02:28:49 +01:00
MessagingPort *grab = 0;
2008-09-12 21:00:20 +02:00
void _dbGridConnThread() {
MessagingPort& dbMsgPort = *grab;
grab = 0;
Message m;
while ( 1 ) {
m.reset();
2008-09-12 21:00:20 +02:00
if ( !dbMsgPort.recv(m) ) {
log() << "end connection " << dbMsgPort.farEnd.toString() << endl;
dbMsgPort.shutdown();
break;
}
2008-12-29 02:28:49 +01:00
processRequest(m, dbMsgPort);
}
2008-09-12 21:00:20 +02:00
2008-12-29 02:28:49 +01:00
}
2008-09-11 22:25:16 +02:00
void dbGridConnThread() {
MessagingPort *p = grab;
2008-12-29 02:28:49 +01:00
try {
_dbGridConnThread();
} catch ( ... ) {
problem() << "uncaught exception in dbgridconnthread, closing connection" << endl;
delete p;
}
2008-12-29 02:28:49 +01:00
}
class DbGridListener : public Listener {
public:
DbGridListener(int p) : Listener(p) { }
virtual void accepted(MessagingPort *mp) {
assert( grab == 0 );
grab = mp;
boost::thread thr(dbGridConnThread);
while ( grab )
sleepmillis(1);
}
};
void start() {
log() << "waiting for connections on port " << port << "..." << endl;
DbGridListener l(port);
l.listen();
}
2008-09-11 22:25:16 +02:00
2009-01-14 23:09:51 +01:00
} // namespace mongo
using namespace mongo;
2008-12-29 02:28:49 +01:00
int main(int argc, char* argv[], char *envp[] ) {
2009-02-03 23:10:44 +01:00
2008-12-29 02:28:49 +01:00
if ( argc <= 1 ) {
2009-02-03 19:15:54 +01:00
usage( argv );
2008-12-05 18:10:03 +01:00
return 3;
}
2009-02-03 23:10:44 +01:00
bool infer = false;
2009-02-05 17:50:27 +01:00
vector<string> configdbs;
2009-02-03 23:10:44 +01:00
2008-09-11 22:25:16 +02:00
for (int i = 1; i < argc; i++) {
2008-12-29 02:28:49 +01:00
if ( argv[i] == 0 ) continue;
2008-09-11 22:25:16 +02:00
string s = argv[i];
2008-12-29 02:28:49 +01:00
if ( s == "--port" ) {
2008-09-11 22:25:16 +02:00
port = atoi(argv[++i]);
}
2009-01-14 23:17:24 +01:00
else if ( s == "--infer" ) {
2009-02-03 23:10:44 +01:00
infer = true;
}
2009-02-05 17:50:27 +01:00
else if ( s == "--configdb" ) {
2009-02-03 23:10:44 +01:00
assert( ! infer );
while ( ++i < argc )
2009-02-05 17:50:27 +01:00
configdbs.push_back(argv[i]);
2009-02-03 23:10:44 +01:00
2009-02-05 17:50:27 +01:00
if ( configdbs.size() == 0 ) {
out() << "error: no args for --configdb\n";
return 4;
}
2009-02-03 23:10:44 +01:00
2009-02-05 17:50:27 +01:00
if ( configdbs.size() > 2 ) {
out() << "error: --configdb does not support more than 2 parameters yet\n";
return 5;
}
2008-11-13 21:52:20 +01:00
}
2009-02-05 17:50:27 +01:00
else if ( s.find( "-v" ) == 0 ){
logLevel = s.size() - 1;
}
2008-12-29 02:28:49 +01:00
else {
2009-02-03 19:15:54 +01:00
usage( argv );
2008-09-30 00:00:53 +02:00
return 3;
}
2008-09-11 22:25:16 +02:00
}
2009-02-03 23:10:44 +01:00
2008-09-11 22:25:16 +02:00
bool ok = port != 0;
2009-02-03 23:10:44 +01:00
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;
}
2009-02-03 19:15:54 +01:00
log() << argv[0] << " starting (--help for usage)" << endl;
2008-12-29 02:28:49 +01:00
UnitTest::runTests();
2009-02-03 23:10:44 +01:00
2009-02-05 17:50:27 +01:00
if ( ! configServer.init( configdbs , infer ) ){
2009-02-03 23:10:44 +01:00
cerr << "couldn't connectd to config db" << endl;
return 7;
}
assert( configServer.ok() );
Model::globalConn = configServer.conn();
2008-09-11 22:25:16 +02:00
start();
2008-12-29 02:28:49 +01:00
dbexit(0);
return 0;
}
#undef exit
2009-02-03 16:12:01 +01:00
void mongo::dbexit(int rc, const char *why) {
log() << "dbexit: " << why << " rc:" << rc << endl;
::exit(rc);
}