summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/replication_coordinator_impl_elect.cpp
blob: 9fe983e859a09e607cd0fbe86e3b6a322ce0cc74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
 *    Copyright (C) 2014 MongoDB 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/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kReplication

#include "mongo/platform/basic.h"

#include "mongo/base/disallow_copying.h"
#include "mongo/db/repl/elect_cmd_runner.h"
#include "mongo/db/repl/freshness_checker.h"
#include "mongo/db/repl/replication_coordinator_impl.h"
#include "mongo/db/repl/topology_coordinator.h"
#include "mongo/stdx/mutex.h"
#include "mongo/util/log.h"
#include "mongo/util/scopeguard.h"

namespace mongo {
namespace repl {

namespace {
class LoseElectionGuard {
    MONGO_DISALLOW_COPYING(LoseElectionGuard);

public:
    LoseElectionGuard(TopologyCoordinator* topCoord,
                      executor::TaskExecutor* executor,
                      std::unique_ptr<FreshnessChecker>* freshnessChecker,
                      std::unique_ptr<ElectCmdRunner>* electCmdRunner,
                      executor::TaskExecutor::EventHandle* electionFinishedEvent)
        : _topCoord(topCoord),
          _executor(executor),
          _freshnessChecker(freshnessChecker),
          _electCmdRunner(electCmdRunner),
          _electionFinishedEvent(electionFinishedEvent),
          _dismissed(false) {}

    ~LoseElectionGuard() {
        if (_dismissed) {
            return;
        }
        _topCoord->processLoseElection();
        _freshnessChecker->reset(NULL);
        _electCmdRunner->reset(NULL);
        if (_electionFinishedEvent->isValid()) {
            _executor->signalEvent(*_electionFinishedEvent);
        }
    }

    void dismiss() {
        _dismissed = true;
    }

private:
    TopologyCoordinator* const _topCoord;
    executor::TaskExecutor* const _executor;
    std::unique_ptr<FreshnessChecker>* const _freshnessChecker;
    std::unique_ptr<ElectCmdRunner>* const _electCmdRunner;
    const executor::TaskExecutor::EventHandle* _electionFinishedEvent;
    bool _dismissed;
};

}  // namespace

void ReplicationCoordinatorImpl::_startElectSelf_inlock() {
    invariant(!_freshnessChecker);
    invariant(!_electCmdRunner);

    switch (_rsConfigState) {
        case kConfigSteady:
            break;
        case kConfigInitiating:
        case kConfigReconfiguring:
        case kConfigHBReconfiguring:
            LOG(2) << "Not standing for election; processing a configuration change";
            // Transition out of candidate role.
            _topCoord->processLoseElection();
            return;
        default:
            severe() << "Entered replica set election code while in illegal config state "
                     << int(_rsConfigState);
            fassertFailed(18913);
    }

    log() << "Standing for election";
    const StatusWith<executor::TaskExecutor::EventHandle> finishEvh = _replExecutor->makeEvent();
    if (finishEvh.getStatus() == ErrorCodes::ShutdownInProgress) {
        return;
    }
    fassert(18680, finishEvh.getStatus());
    _electionFinishedEvent = finishEvh.getValue();
    LoseElectionGuard lossGuard(_topCoord.get(),
                                _replExecutor.get(),
                                &_freshnessChecker,
                                &_electCmdRunner,
                                &_electionFinishedEvent);


    invariant(_rsConfig.getMemberAt(_selfIndex).isElectable());
    OpTime lastOpTimeApplied(_getMyLastAppliedOpTime_inlock());

    if (lastOpTimeApplied.isNull()) {
        log() << "not trying to elect self, "
                 "do not yet have a complete set of data from any point in time"
                 " -- lastAppliedOpTime is null";
        return;
    }

    _freshnessChecker.reset(new FreshnessChecker);

    StatusWith<executor::TaskExecutor::EventHandle> nextPhaseEvh =
        _freshnessChecker->start(_replExecutor.get(),
                                 lastOpTimeApplied.getTimestamp(),
                                 _rsConfig,
                                 _selfIndex,
                                 _topCoord->getMaybeUpHostAndPorts());
    if (nextPhaseEvh.getStatus() == ErrorCodes::ShutdownInProgress) {
        return;
    }
    fassert(18681, nextPhaseEvh.getStatus());
    _replExecutor
        ->onEvent(nextPhaseEvh.getValue(),
                  [this](const mongo::executor::TaskExecutor::CallbackArgs&) {
                      _onFreshnessCheckComplete();
                  })
        .status_with_transitional_ignore();
    lossGuard.dismiss();
}

void ReplicationCoordinatorImpl::_onFreshnessCheckComplete() {
    stdx::lock_guard<stdx::mutex> lk(_mutex);
    invariant(_freshnessChecker);
    invariant(!_electCmdRunner);
    LoseElectionGuard lossGuard(_topCoord.get(),
                                _replExecutor.get(),
                                &_freshnessChecker,
                                &_electCmdRunner,
                                &_electionFinishedEvent);

    if (_freshnessChecker->isCanceled()) {
        LOG(2) << "Election canceled during freshness check phase";
        return;
    }

    const Date_t now(_replExecutor->now());
    const FreshnessChecker::ElectionAbortReason abortReason =
        _freshnessChecker->shouldAbortElection();

    // need to not sleep after last time sleeping,
    switch (abortReason) {
        case FreshnessChecker::None:
            break;
        case FreshnessChecker::FreshnessTie:
            if ((_selfIndex != 0) && !_sleptLastElection) {
                const auto ms = Milliseconds(_nextRandomInt64_inlock(1000) + 50);
                const Date_t nextCandidateTime = now + ms;
                log() << "possible election tie; sleeping " << ms << " until "
                      << dateToISOStringLocal(nextCandidateTime);
                _topCoord->setElectionSleepUntil(nextCandidateTime);
                _scheduleWorkAt(nextCandidateTime,
                                [=](const executor::TaskExecutor::CallbackArgs& cbData) {
                                    _recoverFromElectionTie(cbData);
                                });
                _sleptLastElection = true;
                return;
            }
            _sleptLastElection = false;
            break;
        case FreshnessChecker::FresherNodeFound:
            log() << "not electing self, we are not freshest";
            return;
        case FreshnessChecker::QuorumUnreachable:
            log() << "not electing self, we could not contact enough voting members";
            return;
        default:
            log() << "not electing self due to election abort message :"
                  << static_cast<int>(abortReason);
            return;
    }

    log() << "running for election"
          << (abortReason == FreshnessChecker::FreshnessTie
                  ? "; slept last election, so running regardless of possible tie"
                  : "");

    // Secure our vote for ourself first
    if (!_topCoord->voteForMyself(now)) {
        return;
    }

    _electCmdRunner.reset(new ElectCmdRunner);
    StatusWith<executor::TaskExecutor::EventHandle> nextPhaseEvh = _electCmdRunner->start(
        _replExecutor.get(), _rsConfig, _selfIndex, _topCoord->getMaybeUpHostAndPorts());
    if (nextPhaseEvh.getStatus() == ErrorCodes::ShutdownInProgress) {
        return;
    }
    fassert(18685, nextPhaseEvh.getStatus());

    _replExecutor
        ->onEvent(nextPhaseEvh.getValue(),
                  [=](const executor::TaskExecutor::CallbackArgs&) { _onElectCmdRunnerComplete(); })
        .status_with_transitional_ignore();
    lossGuard.dismiss();
}

void ReplicationCoordinatorImpl::_onElectCmdRunnerComplete() {
    stdx::unique_lock<stdx::mutex> lk(_mutex);
    LoseElectionGuard lossGuard(_topCoord.get(),
                                _replExecutor.get(),
                                &_freshnessChecker,
                                &_electCmdRunner,
                                &_electionFinishedEvent);

    invariant(_freshnessChecker);
    invariant(_electCmdRunner);
    if (_electCmdRunner->isCanceled()) {
        LOG(2) << "Election canceled during elect self phase";
        return;
    }

    const int receivedVotes = _electCmdRunner->getReceivedVotes();

    if (receivedVotes < _rsConfig.getMajorityVoteCount()) {
        log() << "couldn't elect self, only received " << receivedVotes
              << " votes, but needed at least " << _rsConfig.getMajorityVoteCount();
        // Suppress ourselves from standing for election again, giving other nodes a chance
        // to win their elections.
        const auto ms = Milliseconds(_nextRandomInt64_inlock(1000) + 50);
        const Date_t now(_replExecutor->now());
        const Date_t nextCandidateTime = now + ms;
        log() << "waiting until " << nextCandidateTime << " before standing for election again";
        _topCoord->setElectionSleepUntil(nextCandidateTime);
        _scheduleWorkAt(nextCandidateTime, [=](const executor::TaskExecutor::CallbackArgs& cbData) {
            _recoverFromElectionTie(cbData);
        });
        return;
    }

    if (_rsConfig.getConfigVersion() != _freshnessChecker->getOriginalConfigVersion()) {
        log() << "config version changed during our election, ignoring result";
        return;
    }

    log() << "election succeeded, assuming primary role";

    lossGuard.dismiss();
    _freshnessChecker.reset(NULL);
    _electCmdRunner.reset(NULL);
    auto electionFinishedEvent = _electionFinishedEvent;
    lk.unlock();
    _performPostMemberStateUpdateAction(kActionWinElection);
    _replExecutor->signalEvent(electionFinishedEvent);
}

void ReplicationCoordinatorImpl::_recoverFromElectionTie(
    const executor::TaskExecutor::CallbackArgs& cbData) {
    stdx::unique_lock<stdx::mutex> lk(_mutex);

    auto now = _replExecutor->now();
    const auto status = _topCoord->checkShouldStandForElection(now);
    if (!status.isOK()) {
        LOG(2) << "ReplicationCoordinatorImpl::_recoverFromElectionTie -- " << status.reason();
    } else {
        fassert(28817,
                _topCoord->becomeCandidateIfElectable(
                    now, TopologyCoordinator::StartElectionReason::kElectionTimeout));
        _startElectSelf_inlock();
    }
}

}  // namespace repl
}  // namespace mongo