1 /*
2 * MonitorLeader.h
3 *
4 * This source file is part of the FoundationDB open source project
5 *
6 * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 #ifndef FDBCLIENT_MONITORLEADER_H
22 #define FDBCLIENT_MONITORLEADER_H
23 #pragma once
24
25 #include "fdbclient/FDBTypes.h"
26 #include "fdbclient/CoordinationInterface.h"
27
28 #define CLUSTER_FILE_ENV_VAR_NAME "FDB_CLUSTER_FILE"
29
30 class ClientCoordinators;
31
32 template <class LeaderInterface>
33 Future<Void> monitorLeader( Reference<ClusterConnectionFile> const& connFile, Reference<AsyncVar<Optional<LeaderInterface>>> const& outKnownLeader, Reference<AsyncVar<int>> connectedCoordinatorsNum = Reference<AsyncVar<int>>() );
34 // Monitors the given coordination group's leader election process and provides a best current guess
35 // of the current leader. If a leader is elected for long enough and communication with a quorum of
36 // coordinators is possible, eventually outKnownLeader will be that leader's interface.
37
38 #pragma region Implementation
39
40 Future<Void> monitorLeaderInternal( Reference<ClusterConnectionFile> const& connFile, Reference<AsyncVar<Value>> const& outSerializedLeaderInfo, Reference<AsyncVar<int>> const& connectedCoordinatorsNum );
41
42 template <class LeaderInterface>
monitorLeader(Reference<ClusterConnectionFile> const & connFile,Reference<AsyncVar<Optional<LeaderInterface>>> const & outKnownLeader,Reference<AsyncVar<int>> connectedCoordinatorsNum)43 Future<Void> monitorLeader( Reference<ClusterConnectionFile> const& connFile, Reference<AsyncVar<Optional<LeaderInterface>>> const& outKnownLeader, Reference<AsyncVar<int>> connectedCoordinatorsNum ) {
44 Reference<AsyncVar<Value>> serializedInfo( new AsyncVar<Value> );
45 Future<Void> m = monitorLeaderInternal( connFile, serializedInfo, connectedCoordinatorsNum );
46 return m || asyncDeserialize( serializedInfo, outKnownLeader );
47 }
48
49 #pragma endregion
50
51 #endif