Files
mongo/db/repl/replset.cpp

218 lines
7.9 KiB
C++
Raw Normal View History

2010-04-13 13:22:42 -04:00
/**
2010-04-14 17:25:03 -04:00
* Copyright (C) 2008 10gen Inc.
2010-04-13 13:22:42 -04: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.
*
* 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/>.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2010-04-13 13:22:42 -04:00
#include "../cmdline.h"
2010-04-18 13:05:57 -04:00
#include "../../util/sock.h"
2010-04-21 16:43:51 -04:00
#include "replset.h"
2010-05-13 17:28:53 -04:00
#include "../client.h"
2010-04-13 13:22:42 -04:00
namespace mongo {
2010-04-21 21:19:37 -04:00
bool replSet = false;
2010-04-13 17:38:15 -04:00
ReplSet *theReplSet = 0;
2010-04-21 17:40:24 -04:00
void ReplSet::fillIsMaster(BSONObjBuilder& b) {
2010-05-07 16:42:55 -04:00
log() << "hellO" << rsLog;
2010-04-21 17:40:24 -04:00
b.append("ismaster", 0);
b.append("ok", false);
b.append("msg", "not yet implemented");
}
2010-04-21 14:41:09 -04:00
/** @param cfgString <setname>/<seedhost1>,<seedhost2> */
2010-04-23 17:35:05 -04:00
/*
2010-04-21 16:43:51 -04:00
ReplSet::ReplSet(string cfgString) : fatal(false) {
2010-04-23 17:35:05 -04:00
}
*/
/** @param cfgString <setname>/<seedhost1>,<seedhost2> */
2010-05-11 15:58:44 -04:00
ReplSet::ReplSet(string cfgString) : _self(0), elect(this), _mgr(this) {
2010-05-07 15:35:16 -04:00
_myState = STARTUP;
2010-05-12 16:03:09 -04:00
_currentPrimary = 0;
2010-04-23 17:35:05 -04:00
2010-04-21 14:41:09 -04:00
const char *p = cfgString.c_str();
const char *slash = strchr(p, '/');
uassert(13093, "bad --replSet config string format is: <setname>/<seedhost1>,<seedhost2>[,...]", slash != 0 && p != slash);
_name = string(p, slash-p);
2010-05-11 15:58:44 -04:00
log() << "replSet startup " << cfgString << rsLog;
2010-04-14 17:25:03 -04:00
2010-05-07 17:49:24 -04:00
set<HostAndPort> seedSet;
2010-04-18 13:05:57 -04:00
vector<HostAndPort> *seeds = new vector<HostAndPort>;
2010-04-21 14:41:09 -04:00
p = slash + 1;
2010-04-14 17:25:03 -04:00
while( 1 ) {
2010-04-21 14:41:09 -04:00
const char *comma = strchr(p, ',');
if( comma == 0 ) comma = strchr(p,0);
uassert(13094, "bad --replSet config string", p != comma);
{
HostAndPort m;
try {
2010-05-14 13:07:24 -04:00
m = HostAndPort(p, comma-p);
2010-04-21 14:41:09 -04:00
}
catch(...) {
uassert(13114, "bad --replSet seed hostname", false);
}
2010-05-07 17:49:24 -04:00
uassert(13096, "bad --replSet config string - dups?", seedSet.count(m) == 0 );
seedSet.insert(m);
2010-04-21 14:41:09 -04:00
uassert(13101, "can't use localhost in replset host list", !m.isLocalHost());
if( m.isSelf() )
2010-05-07 16:42:55 -04:00
log() << "replSet ignoring seed " << m.toString() << " (=self)" << rsLog;
2010-04-21 14:41:09 -04:00
else
seeds->push_back(m);
if( *comma == 0 )
break;
p = comma + 1;
2010-04-14 17:25:03 -04:00
}
}
2010-04-14 20:50:15 -04:00
2010-04-15 19:12:28 -04:00
_seeds = seeds;
2010-04-21 16:43:51 -04:00
//for( vector<HostAndPort>::iterator i = seeds->begin(); i != seeds->end(); i++ )
// addMemberIfMissing(*i);
2010-05-11 15:58:44 -04:00
log() << "replSet load config from various servers..." << rsLog;
2010-04-21 16:43:51 -04:00
loadConfig();
2010-05-07 17:49:24 -04:00
for( Member *m = head(); m; m = m->next() )
seedSet.erase(m->_h);
for( set<HostAndPort>::iterator i = seedSet.begin(); i != seedSet.end(); i++ ) {
2010-05-11 15:58:44 -04:00
log() << "replSet warning: command line seed " << i->toString() << " is not present in the current repl set config" << rsLog;
2010-05-07 17:49:24 -04:00
}
2010-04-14 17:25:03 -04:00
}
2010-04-22 16:17:18 -04:00
ReplSet::StartupStatus ReplSet::startupStatus = PRESTART;
2010-04-21 17:40:24 -04:00
string ReplSet::startupStatusMsg;
2010-05-13 17:18:17 -04:00
void ReplSet::setFrom(ReplSetConfig& c, bool save) {
_cfg = new ReplSetConfig(c);
2010-05-07 12:37:09 -04:00
assert( _cfg->ok() );
assert( _name.empty() || _name == _cfg->_id );
_name = _cfg->_id;
2010-05-03 13:33:49 -04:00
assert( !_name.empty() );
2010-05-07 12:37:09 -04:00
assert( _members.head() == 0 );
2010-05-05 14:00:25 -04:00
int me=0;
2010-05-07 12:37:09 -04:00
for( vector<ReplSetConfig::MemberCfg>::iterator i = _cfg->members.begin(); i != _cfg->members.end(); i++ ) {
2010-05-05 14:57:49 -04:00
const ReplSetConfig::MemberCfg& m = *i;
2010-05-05 14:00:25 -04:00
if( m.h.isSelf() ) {
me++;
2010-05-05 14:57:49 -04:00
assert( _self == 0 );
2010-05-07 12:37:09 -04:00
_self = new Member(m.h, m._id, &m);
2010-05-05 14:00:25 -04:00
} else {
2010-05-07 12:37:09 -04:00
Member *mi = new Member(m.h, m._id, &m);
2010-05-05 14:00:25 -04:00
_members.push(mi);
}
2010-05-03 13:33:49 -04:00
}
2010-05-05 14:00:25 -04:00
assert( me == 1 );
2010-05-13 17:18:17 -04:00
if( save ) {
_cfg->save();
}
2010-05-03 13:33:49 -04:00
}
void ReplSet::finishLoadingConfig(vector<ReplSetConfig>& cfgs) {
int v = -1;
ReplSetConfig *highest = 0;
2010-05-13 17:18:17 -04:00
int myVersion = -2000;
int n = 0;
2010-05-03 13:33:49 -04:00
for( vector<ReplSetConfig>::iterator i = cfgs.begin(); i != cfgs.end(); i++ ) {
ReplSetConfig& cfg = *i;
2010-05-13 17:18:17 -04:00
if( ++n == 1 ) myVersion = cfg.version;
2010-05-03 13:33:49 -04:00
if( cfg.ok() && cfg.version > v ) {
highest = &cfg;
v = cfg.version;
}
}
assert( highest );
2010-05-13 17:18:17 -04:00
setFrom(*highest, highest->version > myVersion && highest->version >= 0);
2010-05-03 13:33:49 -04:00
}
2010-04-21 16:43:51 -04:00
void ReplSet::loadConfig() {
2010-04-21 17:40:24 -04:00
while( 1 ) {
2010-04-22 16:17:18 -04:00
startupStatus = LOADINGCONFIG;
2010-05-09 15:16:14 -04:00
startupStatusMsg = "loading " + rsConfigNs + " config (LOADINGCONFIG)";
2010-04-21 17:40:24 -04:00
try {
vector<ReplSetConfig> configs;
configs.push_back( ReplSetConfig(HostAndPort::me()) );
2010-04-22 16:17:18 -04:00
for( vector<HostAndPort>::const_iterator i = _seeds->begin(); i != _seeds->end(); i++ ) {
2010-04-21 17:40:24 -04:00
configs.push_back( ReplSetConfig(*i) );
2010-04-22 16:17:18 -04:00
}
2010-04-21 17:40:24 -04:00
int nok = 0;
2010-04-22 16:17:18 -04:00
int nempty = 0;
2010-04-21 17:40:24 -04:00
for( vector<ReplSetConfig>::iterator i = configs.begin(); i != configs.end(); i++ ) {
if( i->ok() )
nok++;
2010-04-22 16:17:18 -04:00
if( i->empty() )
nempty++;
2010-04-21 17:40:24 -04:00
}
2010-04-22 16:17:18 -04:00
if( nok == 0 ) {
2010-04-23 18:51:51 -04:00
if( nempty == (int) configs.size() ) {
2010-04-22 16:17:18 -04:00
startupStatus = EMPTYCONFIG;
2010-05-13 17:18:17 -04:00
startupStatusMsg = "can't get " + rsConfigNs + " config from self or any seed (EMPTYCONFIG)";
2010-05-09 15:16:14 -04:00
log() << "replSet can't get " << rsConfigNs << " config from self or any seed (EMPTYCONFIG)" << rsLog;
2010-05-08 14:12:24 -04:00
log() << "replSet have you ran replSetInitiate yet?" << rsLog;
2010-05-07 16:42:55 -04:00
log() << "replSet sleeping 1 minute and will try again." << rsLog;
2010-04-22 16:17:18 -04:00
}
else {
2010-04-23 17:35:05 -04:00
startupStatus = EMPTYUNREACHABLE;
2010-05-09 15:16:14 -04:00
startupStatusMsg = "can't currently get " + rsConfigNs + " config from self or any seed (EMPTYUNREACHABLE)";
log() << "replSet can't get " << rsConfigNs << " config from self or any seed." << rsLog;
2010-05-07 16:42:55 -04:00
log() << "replSet sleeping 1 minute and will try again." << rsLog;
2010-04-22 16:17:18 -04:00
}
2010-04-21 17:40:24 -04:00
sleepsecs(60);
continue;
}
2010-05-03 13:33:49 -04:00
finishLoadingConfig(configs);
2010-04-21 17:40:24 -04:00
}
2010-05-10 11:26:02 -04:00
catch(DBException& e) {
2010-04-22 16:17:18 -04:00
startupStatus = BADCONFIG;
startupStatusMsg = "replSet error loading set config (BADCONFIG)";
2010-05-10 11:26:02 -04:00
log() << "replSet error loading configurations " << e.toString() << rsLog;
2010-05-07 16:42:55 -04:00
log() << "replSet replication will not start" << rsLog;
2010-05-07 15:35:16 -04:00
fatal();
2010-04-21 17:40:24 -04:00
throw;
}
break;
2010-04-21 17:13:25 -04:00
}
2010-05-11 15:58:44 -04:00
startupStatusMsg = "? started";
startupStatus = STARTED;
2010-04-21 16:43:51 -04:00
}
2010-05-13 17:18:17 -04:00
/* forked as a thread during startup */
2010-04-21 16:43:51 -04:00
void startReplSets() {
2010-05-13 17:18:17 -04:00
Client::initThread("startReplSets");
2010-04-21 16:43:51 -04:00
mongo::lastError.reset( new LastError() );
try {
assert( theReplSet == 0 );
2010-04-21 21:19:37 -04:00
if( cmdLine.replSet.empty() ) {
assert(!replSet);
2010-04-21 16:43:51 -04:00
return;
2010-04-21 21:19:37 -04:00
}
2010-05-05 14:00:25 -04:00
(theReplSet = new ReplSet(cmdLine.replSet))->go();
2010-04-21 16:43:51 -04:00
}
2010-04-27 17:07:07 -04:00
catch(std::exception& e) {
2010-05-07 16:42:55 -04:00
log() << "replSet Caught exception in management thread: " << e.what() << rsLog;
2010-04-21 16:43:51 -04:00
if( theReplSet )
2010-05-07 15:35:16 -04:00
theReplSet->fatal();
2010-04-21 16:43:51 -04:00
}
2010-05-13 17:18:17 -04:00
cc().shutdown();
2010-04-13 13:22:42 -04:00
}
}