Files
mongo/db/repl/manager.cpp

103 lines
3.1 KiB
C++
Raw Normal View History

2010-05-11 15:58:44 -04:00
/* @file manager.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,b
* 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 "pch.h"
2010-05-29 15:45:47 -04:00
#include "rs.h"
2010-05-11 15:58:44 -04:00
namespace mongo {
enum {
NOPRIMARY = -2,
SELFPRIMARY = -1
};
2010-05-12 17:43:21 -04:00
/* check members OTHER THAN US to see if they think they are primary */
2010-06-01 16:25:47 -04:00
const Member * Manager::findOtherPrimary() {
2010-05-24 17:11:47 -04:00
Member *m = rs->head();
2010-05-12 17:43:21 -04:00
Member *p = 0;
while( m ) {
2010-05-20 12:40:22 -04:00
if( m->state() == PRIMARY ) {
2010-05-12 17:43:21 -04:00
if( p ) throw "twomasters"; // our polling is asynchronous, so this is often ok.
p = m;
}
m = m->next();
}
2010-05-24 17:11:47 -04:00
if( p )
noteARemoteIsPrimary(p);
2010-05-12 17:43:21 -04:00
return p;
2010-05-12 16:03:09 -04:00
}
2010-06-01 16:25:47 -04:00
Manager::Manager(ReplSetImpl *_rs) :
task::Server("Manager"), rs(_rs), _primary(NOPRIMARY)
2010-05-11 15:58:44 -04:00
{
}
2010-06-01 16:25:47 -04:00
void Manager::noteARemoteIsPrimary(const Member *m) {
2010-06-01 11:08:27 -04:00
rs->_currentPrimary = m;
2010-05-24 17:11:47 -04:00
rs->_self->lhb() = "";
2010-06-01 11:08:27 -04:00
rs->_myState = RECOVERING;
2010-05-12 17:43:21 -04:00
}
2010-05-12 16:03:09 -04:00
/** called as the health threads get new results */
2010-06-01 16:25:47 -04:00
void Manager::msgCheckNewState() {
2010-05-24 17:11:47 -04:00
const Member *p = rs->currentPrimary();
const Member *p2;
try { p2 = findOtherPrimary(); }
catch(string s) {
/* two other nodes think they are primary (asynchronously polled) -- wait for things to settle down. */
log() << "replSet warning DIAG TODO 2primary" << s << rsLog;
return;
}
2010-05-12 16:03:09 -04:00
2010-05-24 17:11:47 -04:00
if( p == p2 && p ) return;
2010-05-12 17:43:21 -04:00
2010-05-24 17:11:47 -04:00
if( p2 ) {
/* someone else thinks they are primary. */
if( p == p2 ) // already match
2010-05-15 18:20:55 -04:00
return;
2010-05-24 17:11:47 -04:00
if( p == 0 )
noteARemoteIsPrimary(p2); return;
if( p != rs->_self )
noteARemoteIsPrimary(p2); return;
/* we thought we were primary, yet now someone else thinks they are. */
if( !rs->elect.aMajoritySeemsToBeUp() )
noteARemoteIsPrimary(p2); return;
/* ignore for now, keep thinking we are master */
return;
2010-05-12 17:43:21 -04:00
}
2010-05-11 15:58:44 -04:00
2010-05-24 17:11:47 -04:00
if( p ) {
/* we are already primary, and nothing significant out there has changed. */
/* todo: if !aMajoritySeemsToBeUp, relinquish */
assert( p == rs->_self );
return;
2010-05-19 14:40:10 -04:00
}
2010-05-24 17:11:47 -04:00
/* no one seems to be primary. shall we try to elect ourself? */
if( !rs->elect.aMajoritySeemsToBeUp() ) {
rs->_self->lhb() = "can't see a majority, won't consider electing self";
return;
2010-05-19 14:40:10 -04:00
}
2010-05-24 17:11:47 -04:00
rs->_self->lhb() = "";
rs->elect.electSelf();
}
2010-05-19 14:21:41 -04:00
2010-05-11 15:58:44 -04:00
}