0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/repl/rs_member.h

101 lines
3.4 KiB
C
Raw Normal View History

2010-05-18 20:47:11 +02:00
// @file rsmember.h
2010-06-04 16:25:07 +02:00
/*
* Copyright (C) 2010 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/>.
*/
2010-05-18 20:47:11 +02:00
/** replica set member */
#pragma once
namespace mongo {
2010-07-20 17:05:27 +02:00
/*
RS_STARTUP serving still starting up, or still trying to initiate the set
RS_PRIMARY this server thinks it is primary
RS_SECONDARY this server thinks it is a secondary (slave mode)
RS_RECOVERING recovering/resyncing; after recovery usually auto-transitions to secondary
RS_FATAL something bad has occurred and server is not completely offline with regard to the replica set. fatal error.
RS_STARTUP2 loaded config, still determining who is primary
*/
2010-07-22 23:50:54 +02:00
struct MemberState {
enum MS {
RS_STARTUP,
RS_PRIMARY,
RS_SECONDARY,
RS_RECOVERING,
RS_FATAL,
RS_STARTUP2,
RS_UNKNOWN, /* remote node not yet reached */
RS_ARBITER,
RS_DOWN, /* node not reachable for a report */
RS_ROLLBACK
2010-07-22 23:50:54 +02:00
} s;
MemberState(MS ms = RS_UNKNOWN) : s(ms) { }
explicit MemberState(int ms) : s((MS) ms) { }
bool primary() const { return s == RS_PRIMARY; }
bool secondary() const { return s == RS_SECONDARY; }
bool recovering() const { return s == RS_RECOVERING; }
bool startup2() const { return s == RS_STARTUP2; }
bool fatal() const { return s == RS_FATAL; }
bool rollback() const { return s == RS_ROLLBACK; }
2010-07-22 23:50:54 +02:00
2010-08-14 00:21:32 +02:00
string toString() const;
2010-07-22 23:50:54 +02:00
bool operator==(const MemberState& r) const { return s == r.s; }
bool operator!=(const MemberState& r) const { return s != r.s; }
2010-05-18 20:47:11 +02:00
};
/* this is supposed to be just basic information on a member,
and copy constructable. */
2010-05-20 19:52:30 +02:00
class HeartbeatInfo {
2010-05-18 20:47:11 +02:00
unsigned _id;
public:
HeartbeatInfo() : _id(0xffffffff),hbstate(MemberState::RS_UNKNOWN),health(-1.0),downSince(0),skew(INT_MIN) { }
2010-05-20 19:52:30 +02:00
HeartbeatInfo(unsigned id);
2010-05-18 20:47:11 +02:00
bool up() const { return health > 0; }
unsigned id() const { return _id; }
2010-05-20 18:40:22 +02:00
MemberState hbstate;
2010-05-18 20:47:11 +02:00
double health;
time_t upSince;
long long downSince;
2010-05-18 20:47:11 +02:00
time_t lastHeartbeat;
string lastHeartbeatMsg;
OpTime opTime;
2010-07-29 23:35:09 +02:00
int skew;
2010-07-29 21:35:32 +02:00
long long timeDown() const; // ms
2010-07-29 21:35:32 +02:00
/* true if changed in a way of interest to the repl set manager. */
2010-05-20 19:52:30 +02:00
bool changed(const HeartbeatInfo& old) const;
2010-05-18 20:47:11 +02:00
};
2010-05-20 19:52:30 +02:00
inline HeartbeatInfo::HeartbeatInfo(unsigned id) : _id(id) {
hbstate = MemberState::RS_UNKNOWN;
health = -1.0;
downSince = 0;
lastHeartbeat = upSince = 0;
skew = INT_MIN;
2010-05-18 20:47:11 +02:00
}
2010-05-20 19:52:30 +02:00
inline bool HeartbeatInfo::changed(const HeartbeatInfo& old) const {
2010-05-18 20:47:11 +02:00
return health != old.health ||
2010-05-20 18:40:22 +02:00
hbstate != old.hbstate;
2010-05-18 20:47:11 +02:00
}
}