foundationdb/fdbserver/LeaderElection.actor.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

294 lines
12 KiB
C++
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* LeaderElection.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
2022-03-22 04:36:23 +08:00
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
*
2017-05-26 04:48:44 +08:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2017-05-26 04:48:44 +08:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fdbrpc/FailureMonitor.h"
#include "fdbrpc/Locality.h"
2017-05-26 04:48:44 +08:00
#include "fdbserver/CoordinationInterface.h"
#include "fdbserver/Knobs.h"
2017-05-26 04:48:44 +08:00
#include "fdbclient/MonitorLeader.h"
#include "flow/actorcompiler.h" // This must be the last #include.
2017-05-26 04:48:44 +08:00
// Keep trying to become a leader by submitting itself to all coordinators.
// Monitor the health of all coordinators at the same time.
ACTOR Future<Void> submitCandidacy(Key key,
LeaderElectionRegInterface coord,
LeaderInfo myInfo,
UID prevChangeID,
AsyncTrigger* nomineeChange,
2022-04-28 12:54:13 +08:00
Optional<LeaderInfo>* nominee) {
2017-05-26 04:48:44 +08:00
loop {
state Optional<LeaderInfo> li;
2022-04-28 12:54:13 +08:00
if (coord.hostname.present()) {
wait(store(
li,
retryGetReplyFromHostname(
CandidacyRequest(key, myInfo, nominee->present() ? nominee->get().changeID : UID(), prevChangeID),
coord.hostname.get(),
WLTOKEN_LEADERELECTIONREG_CANDIDACY,
TaskPriority::CoordinationReply)));
} else {
2022-04-28 12:54:13 +08:00
wait(store(
li,
retryBrokenPromise(
coord.candidacy,
CandidacyRequest(key, myInfo, nominee->present() ? nominee->get().changeID : UID(), prevChangeID),
TaskPriority::CoordinationReply)));
}
wait(Future<Void>(Void())); // Make sure we weren't cancelled
if (li != *nominee) {
*nominee = li;
nomineeChange->trigger();
2017-05-26 04:48:44 +08:00
if (li.present() && li.get().forward)
wait(Future<Void>(Never()));
2017-05-26 04:48:44 +08:00
}
}
}
ACTOR template <class T>
Future<Void> buggifyDelayedAsyncVar(Reference<AsyncVar<T>> in, Reference<AsyncVar<T>> out) {
try {
loop {
wait(delay(SERVER_KNOBS->BUGGIFIED_EVENTUAL_CONSISTENCY * deterministicRandom()->random01()));
2017-05-26 04:48:44 +08:00
out->set(in->get());
wait(in->onChange());
2017-05-26 04:48:44 +08:00
}
} catch (Error& e) {
out->set(in->get());
throw;
}
}
template <class T>
Future<Void> buggifyDelayedAsyncVar(Reference<AsyncVar<T>>& var) {
2020-11-07 15:50:55 +08:00
auto in = makeReference<AsyncVar<T>>();
2017-05-26 04:48:44 +08:00
auto f = buggifyDelayedAsyncVar(in, var);
var = in;
return f;
}
ACTOR Future<Void> changeLeaderCoordinators(ServerCoordinators coordinators, Value forwardingInfo) {
std::vector<Future<Void>> forwardRequests;
forwardRequests.reserve(coordinators.leaderElectionServers.size());
2022-04-28 12:54:13 +08:00
for (int i = 0; i < coordinators.leaderElectionServers.size(); i++) {
if (coordinators.leaderElectionServers[i].hostname.present()) {
forwardRequests.push_back(retryGetReplyFromHostname(ForwardRequest(coordinators.clusterKey, forwardingInfo),
coordinators.leaderElectionServers[i].hostname.get(),
WLTOKEN_LEADERELECTIONREG_FORWARD));
} else {
forwardRequests.push_back(retryBrokenPromise(coordinators.leaderElectionServers[i].forward,
ForwardRequest(coordinators.clusterKey, forwardingInfo)));
}
}
2017-05-26 04:48:44 +08:00
int quorum_size = forwardRequests.size() / 2 + 1;
wait(quorum(forwardRequests, quorum_size));
2017-05-26 04:48:44 +08:00
return Void();
}
2022-04-28 12:54:13 +08:00
ACTOR Future<Void> tryBecomeLeaderInternal(ServerCoordinators coordinators,
Value proposedSerializedInterface,
Reference<AsyncVar<Value>> outSerializedLeader,
bool hasConnected,
Reference<AsyncVar<ClusterControllerPriorityInfo>> asyncPriorityInfo) {
state AsyncTrigger nomineeChange;
state std::vector<Optional<LeaderInfo>> nominees;
2017-05-26 04:48:44 +08:00
state LeaderInfo myInfo;
state Future<Void> candidacies;
state bool iAmLeader = false;
state UID prevChangeID;
2017-05-26 04:48:44 +08:00
if (asyncPriorityInfo->get().dcFitness == ClusterControllerPriorityInfo::FitnessBad ||
asyncPriorityInfo->get().dcFitness == ClusterControllerPriorityInfo::FitnessRemote ||
asyncPriorityInfo->get().dcFitness == ClusterControllerPriorityInfo::FitnessNotPreferred ||
asyncPriorityInfo->get().isExcluded) {
wait(delay(SERVER_KNOBS->WAIT_FOR_GOOD_REMOTE_RECRUITMENT_DELAY));
} else if (asyncPriorityInfo->get().processClassFitness > ProcessClass::UnsetFit) {
wait(delay(SERVER_KNOBS->WAIT_FOR_GOOD_RECRUITMENT_DELAY));
}
2022-04-28 12:54:13 +08:00
nominees.resize(coordinators.leaderElectionServers.size());
2017-05-26 04:48:44 +08:00
myInfo.serializedInfo = proposedSerializedInterface;
outSerializedLeader->set(Value());
state Future<Void> buggifyDelay =
(SERVER_KNOBS->BUGGIFY_ALL_COORDINATION || BUGGIFY) ? buggifyDelayedAsyncVar(outSerializedLeader) : Void();
while (!iAmLeader) {
state Future<Void> badCandidateTimeout;
myInfo.changeID = deterministicRandom()->randomUniqueID();
prevChangeID = myInfo.changeID;
myInfo.updateChangeID(asyncPriorityInfo->get());
2017-05-26 04:48:44 +08:00
std::vector<Future<Void>> cand;
cand.reserve(coordinators.leaderElectionServers.size());
for (int i = 0; i < coordinators.leaderElectionServers.size(); i++) {
cand.push_back(submitCandidacy(coordinators.clusterKey,
coordinators.leaderElectionServers[i],
myInfo,
prevChangeID,
&nomineeChange,
2022-04-28 12:54:13 +08:00
&nominees[i]));
}
2017-05-26 04:48:44 +08:00
candidacies = waitForAll(cand);
loop {
state Optional<std::pair<LeaderInfo, bool>> leader = getLeader(nominees);
2018-06-01 06:32:47 +08:00
if (leader.present() && leader.get().first.forward) {
2017-05-26 04:48:44 +08:00
// These coordinators are forwarded to another set. But before we change our own cluster file, we need
// to make sure that a majority of coordinators know that. SOMEDAY: Wait briefly to see if other
// coordinators will tell us they already know, to save communication?
wait(changeLeaderCoordinators(coordinators, leader.get().first.serializedInfo));
2017-05-26 04:48:44 +08:00
if (!hasConnected) {
TraceEvent(SevWarnAlways, "IncorrectClusterFileContentsAtConnection")
.detail("ClusterFile", coordinators.ccr->toString())
.detail("StoredConnectionString", coordinators.ccr->getConnectionString().toString())
.detail("CurrentConnectionString", leader.get().first.serializedInfo.toString());
2017-05-26 04:48:44 +08:00
}
wait(coordinators.ccr->setAndPersistConnectionString(
ClusterConnectionString(leader.get().first.serializedInfo.toString())));
TraceEvent("LeaderForwarding")
.detail("ConnStr", coordinators.ccr->getConnectionString().toString())
.trackLatest("LeaderForwarding");
2017-05-26 04:48:44 +08:00
throw coordinators_changed();
}
if (leader.present() && leader.get().second) {
2017-05-26 04:48:44 +08:00
hasConnected = true;
coordinators.ccr->notifyConnected();
2017-05-26 04:48:44 +08:00
}
if (leader.present() && leader.get().second && leader.get().first.equalInternalId(myInfo)) {
TraceEvent("BecomingLeader", myInfo.changeID).log();
ASSERT(leader.get().first.serializedInfo == proposedSerializedInterface);
outSerializedLeader->set(leader.get().first.serializedInfo);
2017-05-26 04:48:44 +08:00
iAmLeader = true;
break;
}
if (leader.present()) {
TraceEvent("LeaderChanged", myInfo.changeID).detail("ToID", leader.get().first.changeID);
if (leader.get().first.serializedInfo !=
proposedSerializedInterface) // We never set outSerializedLeader to our own interface unless we are
// ready to become leader!
outSerializedLeader->set(leader.get().first.serializedInfo);
2017-05-26 04:48:44 +08:00
}
// If more than 2*SERVER_KNOBS->POLLING_FREQUENCY elapses while we are nominated by some coordinator but
// there is no leader, we might be breaking the leader election process for someone with better
// communications but lower ID, so change IDs.
if ((!leader.present() || !leader.get().second) && std::count(nominees.begin(), nominees.end(), myInfo)) {
2017-05-26 04:48:44 +08:00
if (!badCandidateTimeout.isValid())
badCandidateTimeout = delay(SERVER_KNOBS->POLLING_FREQUENCY * 2, TaskPriority::CoordinationReply);
2017-05-26 04:48:44 +08:00
} else
badCandidateTimeout = Future<Void>();
2022-04-28 12:54:13 +08:00
choose {
when(wait(nomineeChange.onTrigger())) {}
when(wait(badCandidateTimeout.isValid() ? badCandidateTimeout : Never())) {
CODE_PROBE(true, "Bad candidate timeout");
2022-04-28 12:54:13 +08:00
TraceEvent("LeaderBadCandidateTimeout", myInfo.changeID).log();
2017-05-26 04:48:44 +08:00
break;
}
when(wait(candidacies)) {
ASSERT(false);
}
when(wait(asyncPriorityInfo->onChange())) {
break;
}
2017-05-26 04:48:44 +08:00
}
}
candidacies.cancel();
}
ASSERT(iAmLeader && outSerializedLeader->get() == proposedSerializedInterface);
loop {
prevChangeID = myInfo.changeID;
myInfo.updateChangeID(asyncPriorityInfo->get());
2017-11-15 05:57:37 +08:00
if (myInfo.changeID != prevChangeID) {
TraceEvent("ChangeLeaderChangeID")
.detail("PrevChangeID", prevChangeID)
.detail("NewChangeID", myInfo.changeID);
2017-09-30 07:34:55 +08:00
}
state std::vector<Future<Void>> true_heartbeats;
state std::vector<Future<Void>> false_heartbeats;
2017-05-26 04:48:44 +08:00
for (int i = 0; i < coordinators.leaderElectionServers.size(); i++) {
2022-04-28 12:54:13 +08:00
Future<LeaderHeartbeatReply> hb;
if (coordinators.leaderElectionServers[i].hostname.present()) {
hb = retryGetReplyFromHostname(LeaderHeartbeatRequest(coordinators.clusterKey, myInfo, prevChangeID),
coordinators.leaderElectionServers[i].hostname.get(),
WLTOKEN_LEADERELECTIONREG_LEADERHEARTBEAT,
TaskPriority::CoordinationReply);
} else {
hb = retryBrokenPromise(coordinators.leaderElectionServers[i].leaderHeartbeat,
LeaderHeartbeatRequest(coordinators.clusterKey, myInfo, prevChangeID),
TaskPriority::CoordinationReply);
}
true_heartbeats.push_back(onEqual(hb, LeaderHeartbeatReply{ true }));
false_heartbeats.push_back(onEqual(hb, LeaderHeartbeatReply{ false }));
2017-05-26 04:48:44 +08:00
}
state Future<Void> rate = delay(SERVER_KNOBS->HEARTBEAT_FREQUENCY, TaskPriority::CoordinationReply) ||
asyncPriorityInfo->onChange(); // SOMEDAY: Move to server side?
2017-05-26 04:48:44 +08:00
choose {
when(wait(quorum(true_heartbeats, true_heartbeats.size() / 2 + 1))) {
2017-05-26 04:48:44 +08:00
//TraceEvent("StillLeader", myInfo.changeID);
} // We are still leader
when(wait(quorum(false_heartbeats, false_heartbeats.size() / 2 + 1))) {
TraceEvent("ReplacedAsLeader", myInfo.changeID).log();
break;
} // We are definitely not leader
when(wait(delay(SERVER_KNOBS->POLLING_FREQUENCY))) {
2017-05-26 04:48:44 +08:00
for (int i = 0; i < coordinators.leaderElectionServers.size(); ++i) {
if (true_heartbeats[i].isReady())
TraceEvent("LeaderTrueHeartbeat", myInfo.changeID)
.detail("Coordinator",
coordinators.leaderElectionServers[i].candidacy.getEndpoint().getPrimaryAddress());
2017-05-26 04:48:44 +08:00
else if (false_heartbeats[i].isReady())
TraceEvent("LeaderFalseHeartbeat", myInfo.changeID)
.detail("Coordinator",
coordinators.leaderElectionServers[i].candidacy.getEndpoint().getPrimaryAddress());
2017-05-26 04:48:44 +08:00
else
TraceEvent("LeaderNoHeartbeat", myInfo.changeID)
.detail("Coordinator",
coordinators.leaderElectionServers[i].candidacy.getEndpoint().getPrimaryAddress());
2017-05-26 04:48:44 +08:00
}
TraceEvent("ReleasingLeadership", myInfo.changeID).log();
break;
} // Give up on being leader, because we apparently have poor communications
when(wait(asyncPriorityInfo->onChange())) {}
2017-05-26 04:48:44 +08:00
}
wait(rate);
2017-05-26 04:48:44 +08:00
}
if (SERVER_KNOBS->BUGGIFY_ALL_COORDINATION || BUGGIFY)
wait(delay(SERVER_KNOBS->BUGGIFIED_EVENTUAL_CONSISTENCY * deterministicRandom()->random01()));
2017-05-26 04:48:44 +08:00
return Void(); // We are no longer leader
}