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,
|
2010-08-12 22:06:16 +02:00
|
|
|
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; }
|
2010-08-12 22:06:16 +02:00
|
|
|
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:
|
2010-08-09 18:01:27 +02:00
|
|
|
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;
|
2010-08-09 18:01:27 +02:00
|
|
|
long long downSince;
|
2010-05-18 20:47:11 +02:00
|
|
|
time_t lastHeartbeat;
|
|
|
|
string lastHeartbeatMsg;
|
2010-06-27 03:31:17 +02:00
|
|
|
OpTime opTime;
|
2010-07-29 23:35:09 +02:00
|
|
|
int skew;
|
2010-07-29 21:35:32 +02:00
|
|
|
|
2010-08-09 18:01:27 +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) {
|
2010-08-08 19:53:48 +02:00
|
|
|
hbstate = MemberState::RS_UNKNOWN;
|
|
|
|
health = -1.0;
|
2010-08-09 18:01:27 +02:00
|
|
|
downSince = 0;
|
2010-08-08 19:53:48 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|