foundationdb/fdbserver/LeaderElection.h

79 lines
3.7 KiB
C
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* LeaderElection.h
*
* 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.
*/
#ifndef FDBSERVER_LEADERELECTION_H
#define FDBSERVER_LEADERELECTION_H
#pragma once
#include "fdbrpc/fdbrpc.h"
#include "fdbrpc/Locality.h"
2019-04-12 04:24:00 +08:00
#include "fdbclient/FDBTypes.h"
2017-05-26 04:48:44 +08:00
class ServerCoordinators;
// Participates in the given coordination group's leader election process, nominating the given
// LeaderInterface (presumed to be a local interface) as leader. The leader election process is
// "sticky" - once a leader becomes leader, as long as its communications with other processes are
// good it will remain leader. The outKnownLeader variable is updated to reflect a best guess of
// the current leader. If the proposed interface becomes the leader, the outKnownLeader will be
// set to the proposedInterface, and then if it is displaced by another leader, the return value will
// eventually be set. If the return value is cancelled, the candidacy or leadership of the proposedInterface
// will eventually end.
template <class LeaderInterface>
2022-04-28 12:54:13 +08:00
Future<Void> tryBecomeLeader(ServerCoordinators const& coordinators,
LeaderInterface const& proposedInterface,
Reference<AsyncVar<Optional<LeaderInterface>>> const& outKnownLeader,
bool hasConnected,
Reference<AsyncVar<ClusterControllerPriorityInfo>> const& asyncPriorityInfo);
2017-05-26 04:48:44 +08:00
// Inform all the coordinators that they have been replaced with a new connection string
Future<Void> changeLeaderCoordinators(ServerCoordinators const& coordinators, Value const& forwardingInfo);
2017-05-26 04:48:44 +08:00
2019-06-21 00:29:01 +08:00
#ifndef __INTEL_COMPILER
2017-05-26 04:48:44 +08:00
#pragma region Implementation
2019-06-21 00:29:01 +08:00
#endif // __INTEL_COMPILER
2017-05-26 04:48:44 +08:00
2022-04-28 12:54:13 +08:00
Future<Void> tryBecomeLeaderInternal(ServerCoordinators const& coordinators,
Value const& proposedSerializedInterface,
Reference<AsyncVar<Value>> const& outSerializedLeader,
bool const& hasConnected,
Reference<AsyncVar<ClusterControllerPriorityInfo>> const& asyncPriorityInfo);
2017-05-26 04:48:44 +08:00
template <class LeaderInterface>
2022-04-28 12:54:13 +08:00
Future<Void> tryBecomeLeader(ServerCoordinators const& coordinators,
LeaderInterface const& proposedInterface,
Reference<AsyncVar<Optional<LeaderInterface>>> const& outKnownLeader,
bool hasConnected,
Reference<AsyncVar<ClusterControllerPriorityInfo>> const& asyncPriorityInfo) {
2020-11-07 15:50:55 +08:00
auto serializedInfo = makeReference<AsyncVar<Value>>();
2022-04-28 12:54:13 +08:00
Future<Void> m = tryBecomeLeaderInternal(coordinators,
ObjectWriter::toValue(proposedInterface, IncludeVersion()),
serializedInfo,
hasConnected,
asyncPriorityInfo);
2020-02-13 02:41:52 +08:00
return m || asyncDeserialize(serializedInfo, outKnownLeader);
2017-05-26 04:48:44 +08:00
}
2019-06-21 00:29:01 +08:00
#ifndef __INTEL_COMPILER
2017-05-26 04:48:44 +08:00
#pragma endregion
2019-06-21 00:29:01 +08:00
#endif // __INTEL_COMPILER
2017-05-26 04:48:44 +08:00
#endif