Files
mongo/dbgrid/server.cpp

174 lines
4.4 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"
2009-02-03 17:10:44 -05:00
2009-02-05 11:50:27 -05:00
#include "server.h"
#include "configserver.h"
2008-10-19 17:46:53 -05:00
#include "gridconfig.h"
2009-01-14 17:09:51 -05:00
namespace mongo {
int port = 27017;
const char *curNs = "";
Database *database = 0;
2009-02-03 17:10:44 -05:00
DBClientWithCommands* Model::globalConn;
string ourHostname;
string getDbContext() {
return "?";
}
2008-12-04 18:11:25 -05:00
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() << " --infer infer configdbname by replacing \"-n<n>\"\n";
out() << " in our hostname with \"-grid\".\n";
out() << endl;
}
2008-12-28 20:28:49 -05:00
MessagingPort *grab = 0;
2008-09-12 15:00:20 -04:00
void _dbGridConnThread() {
MessagingPort& dbMsgPort = *grab;
grab = 0;
Message m;
while ( 1 ) {
m.reset();
2008-09-12 15:00:20 -04:00
if ( !dbMsgPort.recv(m) ) {
log() << "end connection " << dbMsgPort.farEnd.toString() << endl;
dbMsgPort.shutdown();
break;
}
2008-12-28 20:28:49 -05:00
processRequest(m, dbMsgPort);
}
2008-09-12 15:00:20 -04:00
2008-12-28 20:28:49 -05:00
}
2008-09-11 16:25:16 -04:00
void dbGridConnThread() {
MessagingPort *p = grab;
2008-12-28 20:28:49 -05:00
try {
_dbGridConnThread();
} catch ( ... ) {
problem() << "uncaught exception in dbgridconnthread, closing connection" << endl;
delete p;
}
2008-12-28 20:28:49 -05: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 16:25:16 -04:00
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-03 17:10:44 -05:00
2008-12-28 20:28:49 -05:00
if ( argc <= 1 ) {
2009-02-03 13:15:54 -05:00
usage( argv );
2008-12-05 12:10:03 -05:00
return 3;
}
2009-02-03 17:10:44 -05:00
bool infer = 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" ) {
2008-09-11 16:25:16 -04:00
port = atoi(argv[++i]);
}
2009-01-14 17:17:24 -05:00
else if ( s == "--infer" ) {
2009-02-03 17:10:44 -05:00
infer = true;
}
2009-02-05 11:50:27 -05:00
else if ( s == "--configdb" ) {
2009-02-03 17:10:44 -05:00
assert( ! infer );
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;
}
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
2008-09-11 16:25:16 -04:00
bool ok = port != 0;
2009-02-03 17:10:44 -05:00
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-02-03 13:15:54 -05:00
log() << argv[0] << " starting (--help for usage)" << endl;
2008-12-28 20:28:49 -05:00
UnitTest::runTests();
2009-02-03 17:10:44 -05:00
2009-02-05 11:50:27 -05:00
if ( ! configServer.init( configdbs , infer ) ){
2009-02-03 17:10:44 -05:00
cerr << "couldn't connectd to config db" << endl;
return 7;
}
assert( configServer.ok() );
Model::globalConn = 0;
2009-02-03 17:10:44 -05:00
2008-09-11 16:25:16 -04:00
start();
2008-12-28 20:28:49 -05:00
dbexit(0);
return 0;
}
#undef exit
2009-02-03 10:12:01 -05:00
void mongo::dbexit(int rc, const char *why) {
log() << "dbexit: " << why << " rc:" << rc << endl;
::exit(rc);
}