2008-10-24 23:51:28 +02:00
|
|
|
// griddb.cpp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "../grid/message.h"
|
|
|
|
#include "../util/unittest.h"
|
2008-11-01 01:17:54 +01:00
|
|
|
#include "../client/connpool.h"
|
2008-10-24 23:51:28 +02:00
|
|
|
#include "../db/pdfile.h"
|
|
|
|
#include "../client/model.h"
|
2008-10-29 22:48:03 +01:00
|
|
|
#include "../util/background.h"
|
2008-11-09 23:49:37 +01:00
|
|
|
#include "griddatabase.h"
|
2008-10-24 23:51:28 +02:00
|
|
|
|
|
|
|
static boost::mutex griddb_mutex;
|
2008-11-09 23:49:37 +01:00
|
|
|
GridDatabase gridDatabase;
|
|
|
|
DBClientWithCommands *Model::globalConn = &gridDatabase.conn;
|
2008-10-24 23:51:28 +02:00
|
|
|
string ourHostname;
|
2008-11-13 21:52:20 +01:00
|
|
|
extern string dashDashGridDb;
|
2008-10-24 23:51:28 +02:00
|
|
|
|
2008-11-09 23:49:37 +01:00
|
|
|
GridDatabase::GridDatabase() { }
|
2008-10-24 23:51:28 +02:00
|
|
|
|
2008-11-09 23:49:37 +01:00
|
|
|
void GridDatabase::init() {
|
2008-12-04 20:33:18 +01:00
|
|
|
string hn = getHostName();
|
|
|
|
if( hn.empty() ) {
|
2008-10-24 23:51:28 +02:00
|
|
|
sleepsecs(5);
|
|
|
|
exit(16);
|
|
|
|
}
|
2008-12-04 20:33:18 +01:00
|
|
|
ourHostname = hn;
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
strcpy(buf, hn.c_str());
|
2008-10-24 23:51:28 +02:00
|
|
|
|
2008-11-13 21:52:20 +01:00
|
|
|
if( dashDashGridDb.empty() ) {
|
|
|
|
char *p = strchr(buf, '-');
|
|
|
|
if( p )
|
|
|
|
p = strchr(p+1, '-');
|
|
|
|
if( !p ) {
|
|
|
|
log() << "can't parse server's hostname, expect <city>-<locname>-n<nodenum>, got: " << buf << endl;
|
|
|
|
sleepsecs(5);
|
|
|
|
exit(17);
|
|
|
|
}
|
|
|
|
p[1] = 0;
|
2008-10-24 23:51:28 +02:00
|
|
|
}
|
|
|
|
|
2008-11-13 21:52:20 +01:00
|
|
|
string left, right; // with :port#
|
|
|
|
string hostLeft, hostRight;
|
|
|
|
|
|
|
|
if( dashDashGridDb.empty() ) {
|
|
|
|
stringstream sl, sr;
|
|
|
|
sl << buf << "grid-l";
|
|
|
|
sr << buf << "grid-r";
|
|
|
|
hostLeft = sl.str();
|
|
|
|
hostRight = sr.str();
|
|
|
|
sl << ":" << Port;
|
|
|
|
sr << ":" << Port;
|
|
|
|
left = sl.str();
|
|
|
|
right = sr.str();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stringstream sl, sr;
|
|
|
|
sl << dashDashGridDb;
|
|
|
|
sr << dashDashGridDb;
|
|
|
|
if( !isdigit(dashDashGridDb[0]) ) {
|
|
|
|
sl << "-l";
|
|
|
|
sr << "-r";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* ip address specified, so "-l" / "-r" not meaningful
|
|
|
|
silly though that we put it on both sides -- to be fixed.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
hostLeft = sl.str();
|
|
|
|
hostRight = sr.str();
|
|
|
|
sl << ":" << Port;
|
|
|
|
sr << ":" << Port;
|
|
|
|
left = sl.str();
|
|
|
|
right = sr.str();
|
2008-10-24 23:51:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-13 21:52:20 +01:00
|
|
|
if( !isdigit(left[0]) )
|
2008-10-24 23:51:28 +02:00
|
|
|
/* this loop is not really necessary, we we print out if we can't connect
|
|
|
|
but it gives much prettier error msg this way if the config is totally
|
|
|
|
wrong so worthwhile.
|
|
|
|
*/
|
|
|
|
while( 1 ) {
|
|
|
|
if( hostbyname(hostLeft.c_str()).empty() ) {
|
|
|
|
log() << "can't resolve DNS for " << hostLeft << ", sleeping and then trying again" << endl;
|
|
|
|
sleepsecs(15);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( hostbyname(hostRight.c_str()).empty() ) {
|
|
|
|
log() << "can't resolve DNS for " << hostRight << ", sleeping and then trying again" << endl;
|
|
|
|
sleepsecs(15);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-04 02:42:59 +01:00
|
|
|
Logstream& l = log();
|
|
|
|
(l << "connecting to griddb L:" << left << " R:" << right << "...").flush();
|
2008-10-24 23:51:28 +02:00
|
|
|
|
|
|
|
bool ok = conn.connect(left.c_str(),right.c_str());
|
2008-11-04 02:42:59 +01:00
|
|
|
if( !ok ) {
|
|
|
|
l << '\n';
|
2008-10-24 23:51:28 +02:00
|
|
|
log() << " griddb connect failure at startup (will retry)" << endl;
|
2008-11-04 02:42:59 +01:00
|
|
|
} else {
|
|
|
|
l << "ok" << endl;
|
|
|
|
}
|
2008-10-24 23:51:28 +02:00
|
|
|
}
|