0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
This commit is contained in:
Dwight 2010-05-24 11:16:37 -04:00
parent c5d40feed1
commit 7c1dcb6c1a
3 changed files with 24 additions and 2 deletions

View File

@ -39,6 +39,21 @@ namespace mongo {
static const int VETO = -10000;
class VoteException : public std::exception {
};
const time_t LeaseTime = 30;
void ReplSet::Consensus::yea(unsigned memberId) {
Atomic<LastYea>::tran t(ly);
LastYea &ly = t.ref();
time_t now = time(0);
if( ly.when + LeaseTime >= now )
throw VoteException();
ly.when = now;
ly.who = memberId;
}
void ReplSet::Consensus::_electSelf() {
ReplSet::Member& me = *rs._self;
BSONObj electCmd = BSON(

View File

@ -84,6 +84,13 @@ namespace mongo {
class Consensus {
ReplSet &rs;
struct LastYea {
LastYea() : when(0), who(0xffffffff) { }
time_t when;
unsigned who;
};
Atomic<LastYea> ly;
void yea(unsigned memberId);
void _electSelf();
public:
Consensus(ReplSet *t) : rs(*t) { }

View File

@ -66,8 +66,8 @@ namespace mongo {
class tran : private scoped_lock {
Atomic<T>& _a;
public:
tran(Atomic<T>& a) : scoped_lock(_atomicMutex) { }
T& ref() { return _a; }
tran(Atomic<T>& a) : scoped_lock(_atomicMutex), _a(a) { }
T& ref() { return _a.val; }
};
};