1
2 /**
3 * Copyright (C) 2018-present MongoDB, Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the Server Side Public License, version 1,
7 * as published by MongoDB, Inc.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * Server Side Public License for more details.
13 *
14 * You should have received a copy of the Server Side Public License
15 * along with this program. If not, see
16 * <http://www.mongodb.com/licensing/server-side-public-license>.
17 *
18 * As a special exception, the copyright holders give permission to link the
19 * code of portions of this program with the OpenSSL library under certain
20 * conditions as described in each individual source file and distribute
21 * linked combinations including the program with the OpenSSL library. You
22 * must comply with the Server Side Public License in all respects for
23 * all of the code used other than as permitted herein. If you modify file(s)
24 * with this exception, you may extend this exception to your version of the
25 * file(s), but you are not obligated to do so. If you do not wish to do so,
26 * delete this exception statement from your version. If you delete this
27 * exception statement from all source files in the program, then also delete
28 * it in the license file.
29 */
30
31 #define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kReplication
32
33 #include "mongo/platform/basic.h"
34
35 #include <climits>
36
37 #include "mongo/db/repl/member_data.h"
38 #include "mongo/db/repl/rslog.h"
39 #include "mongo/util/log.h"
40
41 namespace mongo {
42 namespace repl {
43
MemberData()44 MemberData::MemberData() : _health(-1), _authIssue(false), _configIndex(-1), _isSelf(false) {
45 _lastResponse.setState(MemberState::RS_UNKNOWN);
46 _lastResponse.setElectionTime(Timestamp());
47 _lastResponse.setAppliedOpTime(OpTime());
48 }
49
setUpValues(Date_t now,ReplSetHeartbeatResponse && hbResponse)50 bool MemberData::setUpValues(Date_t now, ReplSetHeartbeatResponse&& hbResponse) {
51 _health = 1;
52 if (_upSince == Date_t()) {
53 _upSince = now;
54 }
55 _authIssue = false;
56 _lastHeartbeat = now;
57 _lastUpdate = now;
58 _lastUpdateStale = false;
59 _updatedSinceRestart = true;
60
61 if (!hbResponse.hasState()) {
62 hbResponse.setState(MemberState::RS_UNKNOWN);
63 }
64 if (!hbResponse.hasElectionTime()) {
65 hbResponse.setElectionTime(_lastResponse.getElectionTime());
66 }
67 if (!hbResponse.hasAppliedOpTime()) {
68 hbResponse.setAppliedOpTime(_lastResponse.getAppliedOpTime());
69 }
70 // Log if the state changes
71 if (_lastResponse.getState() != hbResponse.getState()) {
72 log() << "Member " << _hostAndPort.toString() << " is now in state "
73 << hbResponse.getState().toString() << rsLog;
74 }
75
76 bool opTimeAdvanced = advanceLastAppliedOpTime(hbResponse.getAppliedOpTime(), now);
77 auto durableOpTime = hbResponse.hasDurableOpTime() ? hbResponse.getDurableOpTime() : OpTime();
78 opTimeAdvanced = advanceLastDurableOpTime(durableOpTime, now) || opTimeAdvanced;
79 _lastResponse = std::move(hbResponse);
80 return opTimeAdvanced;
81 }
82
setDownValues(Date_t now,const std::string & heartbeatMessage)83 void MemberData::setDownValues(Date_t now, const std::string& heartbeatMessage) {
84 _health = 0;
85 _upSince = Date_t();
86 _lastHeartbeat = now;
87 _authIssue = false;
88 _updatedSinceRestart = true;
89
90 if (_lastResponse.getState() != MemberState::RS_DOWN) {
91 log() << "Member " << _hostAndPort.toString() << " is now in state RS_DOWN" << rsLog;
92 }
93
94 _lastResponse = ReplSetHeartbeatResponse();
95 _lastResponse.setState(MemberState::RS_DOWN);
96 _lastResponse.setElectionTime(Timestamp());
97 _lastResponse.setAppliedOpTime(OpTime());
98 _lastResponse.setHbMsg(heartbeatMessage);
99 _lastResponse.setSyncingTo(HostAndPort());
100
101 // The _lastAppliedOpTime/_lastDurableOpTime fields don't get cleared merely by missing a
102 // heartbeat.
103 }
104
setAuthIssue(Date_t now)105 void MemberData::setAuthIssue(Date_t now) {
106 _health = 0; // set health to 0 so that this doesn't count towards majority.
107 _upSince = Date_t();
108 _lastHeartbeat = now;
109 _authIssue = true;
110 _updatedSinceRestart = true;
111
112 if (_lastResponse.getState() != MemberState::RS_UNKNOWN) {
113 log() << "Member " << _hostAndPort.toString()
114 << " is now in state RS_UNKNOWN due to authentication issue." << rsLog;
115 }
116
117 _lastResponse = ReplSetHeartbeatResponse();
118 _lastResponse.setState(MemberState::RS_UNKNOWN);
119 _lastResponse.setElectionTime(Timestamp());
120 _lastResponse.setAppliedOpTime(OpTime());
121 _lastResponse.setHbMsg("");
122 _lastResponse.setSyncingTo(HostAndPort());
123 }
124
setLastAppliedOpTime(OpTime opTime,Date_t now)125 void MemberData::setLastAppliedOpTime(OpTime opTime, Date_t now) {
126 _lastUpdate = now;
127 _lastUpdateStale = false;
128 _lastAppliedOpTime = opTime;
129 }
130
setLastDurableOpTime(OpTime opTime,Date_t now)131 void MemberData::setLastDurableOpTime(OpTime opTime, Date_t now) {
132 _lastUpdate = now;
133 _lastUpdateStale = false;
134 if (_lastAppliedOpTime < opTime) {
135 // TODO(russotto): We think this should never happen, rollback or no rollback. Make this an
136 // invariant and see what happens.
137 log() << "Durable progress (" << opTime << ") is ahead of the applied progress ("
138 << _lastAppliedOpTime << ". This is likely due to a "
139 "rollback."
140 << " memberid: " << _memberId << " rid: " << _rid << " host "
141 << _hostAndPort.toString() << " previous durable progress: " << _lastDurableOpTime;
142 } else {
143 _lastDurableOpTime = opTime;
144 }
145 }
146
advanceLastAppliedOpTime(OpTime opTime,Date_t now)147 bool MemberData::advanceLastAppliedOpTime(OpTime opTime, Date_t now) {
148 _lastUpdate = now;
149 _lastUpdateStale = false;
150 if (_lastAppliedOpTime < opTime) {
151 setLastAppliedOpTime(opTime, now);
152 return true;
153 }
154 return false;
155 }
156
advanceLastDurableOpTime(OpTime opTime,Date_t now)157 bool MemberData::advanceLastDurableOpTime(OpTime opTime, Date_t now) {
158 _lastUpdate = now;
159 _lastUpdateStale = false;
160 if (_lastDurableOpTime < opTime) {
161 setLastDurableOpTime(opTime, now);
162 return true;
163 }
164 return false;
165 }
166
167 } // namespace repl
168 } // namespace mongo
169