2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* CoordinationInterface.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
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 FDBCLIENT_COORDINATIONINTERFACE_H
|
|
|
|
#define FDBCLIENT_COORDINATIONINTERFACE_H
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/FDBTypes.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "fdbrpc/fdbrpc.h"
|
2017-10-13 08:11:58 +08:00
|
|
|
#include "fdbrpc/Locality.h"
|
2020-09-11 08:44:15 +08:00
|
|
|
#include "fdbclient/CommitProxyInterface.h"
|
2019-07-24 10:22:44 +08:00
|
|
|
#include "fdbclient/ClusterInterface.h"
|
2021-09-21 00:46:34 +08:00
|
|
|
#include "fdbclient/WellKnownEndpoints.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
const int MAX_CLUSTER_FILE_BYTES = 60000;
|
|
|
|
|
|
|
|
struct ClientLeaderRegInterface {
|
2021-03-11 02:06:03 +08:00
|
|
|
RequestStream<struct GetLeaderRequest> getLeader;
|
|
|
|
RequestStream<struct OpenDatabaseCoordRequest> openDatabase;
|
2021-05-18 05:22:19 +08:00
|
|
|
RequestStream<struct CheckDescriptorMutableRequest> checkDescriptorMutable;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
ClientLeaderRegInterface() {}
|
2021-03-11 02:06:03 +08:00
|
|
|
ClientLeaderRegInterface(NetworkAddress remote);
|
|
|
|
ClientLeaderRegInterface(INetwork* local);
|
2021-04-16 02:45:14 +08:00
|
|
|
|
|
|
|
bool operator==(const ClientLeaderRegInterface& rhs) const {
|
|
|
|
return getLeader == rhs.getLeader && openDatabase == rhs.openDatabase;
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-10-11 11:44:56 +08:00
|
|
|
// A string containing the information necessary to connect to a cluster.
|
|
|
|
//
|
|
|
|
// The format of the connection string is: description:id@[addrs]+
|
|
|
|
// The description and id together are called the "key"
|
|
|
|
//
|
|
|
|
// The following is enforced about the format of the file:
|
|
|
|
// - The key must contain one (and only one) ':' character
|
|
|
|
// - The description contains only allowed characters (a-z, A-Z, 0-9, _)
|
|
|
|
// - The ID contains only allowed characters (a-z, A-Z, 0-9)
|
|
|
|
// - At least one address is specified
|
|
|
|
// - There is no address present more than once
|
2017-05-26 04:48:44 +08:00
|
|
|
class ClusterConnectionString {
|
|
|
|
public:
|
|
|
|
ClusterConnectionString() {}
|
2021-03-11 02:06:03 +08:00
|
|
|
ClusterConnectionString(std::string const& connectionString);
|
2021-09-17 08:42:34 +08:00
|
|
|
ClusterConnectionString(std::vector<NetworkAddress>, Key);
|
2021-10-11 11:44:56 +08:00
|
|
|
|
2021-09-17 08:42:34 +08:00
|
|
|
std::vector<NetworkAddress> const& coordinators() const { return coord; }
|
2017-05-26 04:48:44 +08:00
|
|
|
Key clusterKey() const { return key; }
|
2021-03-11 02:06:03 +08:00
|
|
|
Key clusterKeyName() const {
|
|
|
|
return keyDesc;
|
|
|
|
} // Returns the "name" or "description" part of the clusterKey (the part before the ':')
|
2017-05-26 04:48:44 +08:00
|
|
|
std::string toString() const;
|
|
|
|
static std::string getErrorString(std::string const& source, Error const& e);
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2021-03-11 02:06:03 +08:00
|
|
|
void parseKey(std::string const& key);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-09-17 08:42:34 +08:00
|
|
|
std::vector<NetworkAddress> coord;
|
2017-05-26 04:48:44 +08:00
|
|
|
Key key, keyDesc;
|
|
|
|
};
|
|
|
|
|
2021-10-12 05:39:05 +08:00
|
|
|
FDB_DECLARE_BOOLEAN_PARAM(ConnectionStringNeedsPersisted);
|
|
|
|
|
2021-10-11 11:44:56 +08:00
|
|
|
// A record that stores the connection string used to connect to a cluster. This record can be updated when a cluster
|
|
|
|
// notifies a connected party that the connection string has changed.
|
|
|
|
//
|
|
|
|
// The typically used cluster connection record is a cluster file (implemented in ClusterConnectionFile). This interface
|
|
|
|
// provides an abstraction over the cluster file so that we can persist the connection string in other locations or have
|
|
|
|
// one that is only stored in memory.
|
|
|
|
class IClusterConnectionRecord {
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2021-10-12 05:39:05 +08:00
|
|
|
IClusterConnectionRecord(ConnectionStringNeedsPersisted connectionStringNeedsPersisted)
|
2021-10-11 11:44:56 +08:00
|
|
|
: connectionStringNeedsPersisted(connectionStringNeedsPersisted) {}
|
|
|
|
virtual ~IClusterConnectionRecord() {}
|
|
|
|
|
|
|
|
// Returns the connection string currently held in this object. This may not match the stored record if it hasn't
|
|
|
|
// been persisted or if the persistent storage for the record has been modified externally.
|
|
|
|
virtual ClusterConnectionString const& getConnectionString() const = 0;
|
|
|
|
|
2021-10-12 05:39:05 +08:00
|
|
|
// Sets the connections string held by this object and persists it.
|
2021-10-11 11:44:56 +08:00
|
|
|
virtual Future<Void> setConnectionString(ClusterConnectionString const&) = 0;
|
|
|
|
|
|
|
|
// If this record is backed by persistent storage, get the connection string from that storage. Otherwise, return
|
|
|
|
// the connection string stored in memory.
|
|
|
|
virtual Future<ClusterConnectionString> getStoredConnectionString() = 0;
|
|
|
|
|
|
|
|
// Checks whether the connection string in persisten storage matches the connection string stored in memory.
|
|
|
|
Future<bool> upToDate();
|
|
|
|
|
|
|
|
// Checks whether the connection string in persisten storage matches the connection string stored in memory. The
|
|
|
|
// cluster string stored in persistent storage is returned via the reference parameter connectionString.
|
|
|
|
virtual Future<bool> upToDate(ClusterConnectionString& connectionString) = 0;
|
|
|
|
|
|
|
|
// Returns a string representing the location of the cluster record. For example, this could be the filename or key
|
|
|
|
// that stores the connection string.
|
|
|
|
virtual std::string getLocation() const = 0;
|
|
|
|
|
|
|
|
// Creates a copy of this object with a modified connection string but that isn't persisted.
|
|
|
|
virtual Reference<IClusterConnectionRecord> makeIntermediateRecord(
|
|
|
|
ClusterConnectionString const& connectionString) const = 0;
|
|
|
|
|
|
|
|
// Returns a string representation of this cluster connection record. This will include the type and location of the
|
|
|
|
// record.
|
|
|
|
virtual std::string toString() const = 0;
|
|
|
|
|
|
|
|
// Signals to the connection record that it was successfully used to connect to a cluster.
|
2017-05-26 04:48:44 +08:00
|
|
|
void notifyConnected();
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2021-10-11 11:44:56 +08:00
|
|
|
virtual void addref() = 0;
|
|
|
|
virtual void delref() = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Writes the connection string to the backing persistent storage, if applicable.
|
|
|
|
virtual Future<bool> persist() = 0;
|
|
|
|
|
2021-10-12 05:39:05 +08:00
|
|
|
// Returns whether the connection record contains a connection string that needs to be persisted upon connection.
|
2021-10-11 11:44:56 +08:00
|
|
|
bool needsToBePersisted() const;
|
|
|
|
|
|
|
|
// Clears the flag needs persisted flag.
|
|
|
|
void setPersisted();
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2021-10-12 05:39:05 +08:00
|
|
|
// A flag that indicates whether this connection record needs to be persisted when it succesfully establishes a
|
2021-10-11 11:44:56 +08:00
|
|
|
// connection.
|
|
|
|
bool connectionStringNeedsPersisted;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LeaderInfo {
|
2019-01-31 05:53:23 +08:00
|
|
|
constexpr static FileIdentifier file_identifier = 8338794;
|
2021-04-15 01:30:21 +08:00
|
|
|
// The first 7 bits of changeID represent cluster controller process class fitness, the lower the better
|
2017-05-26 04:48:44 +08:00
|
|
|
UID changeID;
|
2021-04-15 01:30:21 +08:00
|
|
|
static const uint64_t changeIDMask = ~(uint64_t(0b1111111) << 57);
|
2017-05-26 04:48:44 +08:00
|
|
|
Value serializedInfo;
|
2021-03-11 02:06:03 +08:00
|
|
|
bool forward; // If true, serializedInfo is a connection string instead!
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
LeaderInfo() : forward(false) {}
|
2017-10-13 08:11:58 +08:00
|
|
|
LeaderInfo(UID changeID) : changeID(changeID), forward(false) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-07-11 05:37:47 +08:00
|
|
|
bool operator<(LeaderInfo const& r) const { return changeID < r.changeID; }
|
|
|
|
bool operator>(LeaderInfo const& r) const { return r < *this; }
|
|
|
|
bool operator<=(LeaderInfo const& r) const { return !(*this > r); }
|
|
|
|
bool operator>=(LeaderInfo const& r) const { return !(*this < r); }
|
|
|
|
bool operator==(LeaderInfo const& r) const { return changeID == r.changeID; }
|
|
|
|
bool operator!=(LeaderInfo const& r) const { return !(*this == r); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2018-02-10 08:48:55 +08:00
|
|
|
// The first 7 bits of ChangeID represent cluster controller process class fitness, the lower the better
|
|
|
|
void updateChangeID(ClusterControllerPriorityInfo info) {
|
2021-03-11 02:06:03 +08:00
|
|
|
changeID = UID(((uint64_t)info.processClassFitness << 57) | ((uint64_t)info.isExcluded << 60) |
|
2021-04-15 01:30:21 +08:00
|
|
|
((uint64_t)info.dcFitness << 61) | (changeID.first() & changeIDMask),
|
2021-03-11 02:06:03 +08:00
|
|
|
changeID.second());
|
2017-10-13 08:11:58 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 08:48:55 +08:00
|
|
|
// All but the first 7 bits are used to represent process id
|
2017-11-15 05:57:37 +08:00
|
|
|
bool equalInternalId(LeaderInfo const& leaderInfo) const {
|
2021-04-15 01:30:21 +08:00
|
|
|
return ((changeID.first() & changeIDMask) == (leaderInfo.changeID.first() & changeIDMask)) &&
|
2021-03-11 02:06:03 +08:00
|
|
|
changeID.second() == leaderInfo.changeID.second();
|
2017-10-13 08:11:58 +08:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:56:02 +08:00
|
|
|
// Change leader only if
|
|
|
|
// 1. the candidate has better process class fitness and the candidate is not the leader
|
|
|
|
// 2. the leader process class fitness becomes worse
|
2017-11-15 05:57:37 +08:00
|
|
|
bool leaderChangeRequired(LeaderInfo const& candidate) const {
|
2021-04-15 01:30:21 +08:00
|
|
|
return ((changeID.first() & ~changeIDMask) > (candidate.changeID.first() & ~changeIDMask) &&
|
|
|
|
!equalInternalId(candidate)) ||
|
|
|
|
((changeID.first() & ~changeIDMask) < (candidate.changeID.first() & ~changeIDMask) &&
|
|
|
|
equalInternalId(candidate));
|
2017-10-13 08:11:58 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 05:20:50 +08:00
|
|
|
ClusterControllerPriorityInfo getPriorityInfo() const {
|
|
|
|
ClusterControllerPriorityInfo info;
|
|
|
|
info.processClassFitness = (changeID.first() >> 57) & 7;
|
|
|
|
info.isExcluded = (changeID.first() >> 60) & 1;
|
|
|
|
info.dcFitness = (changeID.first() >> 61) & 7;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2018-12-29 02:49:26 +08:00
|
|
|
serializer(ar, changeID, serializedInfo, forward);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GetLeaderRequest {
|
2019-01-31 05:53:23 +08:00
|
|
|
constexpr static FileIdentifier file_identifier = 214727;
|
2017-05-26 04:48:44 +08:00
|
|
|
Key key;
|
|
|
|
UID knownLeader;
|
2021-03-11 02:06:03 +08:00
|
|
|
ReplyPromise<Optional<LeaderInfo>> reply;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
GetLeaderRequest() {}
|
|
|
|
explicit GetLeaderRequest(Key key, UID kl) : key(key), knownLeader(kl) {}
|
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2018-12-29 02:49:26 +08:00
|
|
|
serializer(ar, key, knownLeader, reply);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-24 10:22:44 +08:00
|
|
|
struct OpenDatabaseCoordRequest {
|
|
|
|
constexpr static FileIdentifier file_identifier = 214728;
|
2019-07-26 08:15:31 +08:00
|
|
|
// Sent by the native API to the coordinator to open a database and track client
|
2019-07-24 10:22:44 +08:00
|
|
|
// info changes. Returns immediately if the current client info id is different from
|
|
|
|
// knownClientInfoID; otherwise returns when it next changes (or perhaps after a long interval)
|
2019-07-26 08:15:31 +08:00
|
|
|
Key traceLogGroup;
|
|
|
|
Standalone<VectorRef<StringRef>> issues;
|
|
|
|
Standalone<VectorRef<ClientVersionRef>> supportedVersions;
|
2019-07-24 10:22:44 +08:00
|
|
|
UID knownClientInfoID;
|
2019-07-27 04:23:56 +08:00
|
|
|
Key clusterKey;
|
2021-09-17 08:42:34 +08:00
|
|
|
std::vector<NetworkAddress> coordinators;
|
2021-03-11 02:06:03 +08:00
|
|
|
ReplyPromise<CachedSerialization<struct ClientDBInfo>> reply;
|
2019-07-24 10:22:44 +08:00
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2019-07-27 04:23:56 +08:00
|
|
|
serializer(ar, issues, supportedVersions, traceLogGroup, knownClientInfoID, clusterKey, coordinators, reply);
|
2019-07-24 10:22:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
class ClientCoordinators {
|
|
|
|
public:
|
2021-09-17 08:42:34 +08:00
|
|
|
std::vector<ClientLeaderRegInterface> clientLeaderServers;
|
2017-05-26 04:48:44 +08:00
|
|
|
Key clusterKey;
|
2021-10-11 11:44:56 +08:00
|
|
|
Reference<IClusterConnectionRecord> ccr;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-10-11 11:44:56 +08:00
|
|
|
explicit ClientCoordinators(Reference<IClusterConnectionRecord> ccr);
|
2021-03-11 02:06:03 +08:00
|
|
|
explicit ClientCoordinators(Key clusterKey, std::vector<NetworkAddress> coordinators);
|
2017-05-26 04:48:44 +08:00
|
|
|
ClientCoordinators() {}
|
|
|
|
};
|
|
|
|
|
2020-09-19 09:32:32 +08:00
|
|
|
struct ProtocolInfoReply {
|
|
|
|
constexpr static FileIdentifier file_identifier = 7784298;
|
|
|
|
ProtocolVersion version;
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
|
|
|
uint64_t version_ = 0;
|
|
|
|
if (Ar::isSerializing) {
|
|
|
|
version_ = version.versionWithFlags();
|
|
|
|
}
|
|
|
|
serializer(ar, version_);
|
|
|
|
if (Ar::isDeserializing) {
|
|
|
|
version = ProtocolVersion(version_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProtocolInfoRequest {
|
|
|
|
constexpr static FileIdentifier file_identifier = 13261233;
|
|
|
|
ReplyPromise<ProtocolInfoReply> reply{ PeerCompatibilityPolicy{ RequirePeer::AtLeast,
|
|
|
|
ProtocolVersion::withStableInterfaces() } };
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
|
|
|
serializer(ar, reply);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-23 21:19:48 +08:00
|
|
|
// Returns true if the cluster descriptor may be modified.
|
|
|
|
struct CheckDescriptorMutableReply {
|
2021-04-23 01:01:21 +08:00
|
|
|
constexpr static FileIdentifier file_identifier = 7784299;
|
2021-04-23 21:19:48 +08:00
|
|
|
CheckDescriptorMutableReply() = default;
|
2021-05-18 05:22:19 +08:00
|
|
|
explicit CheckDescriptorMutableReply(bool isMutable) : isMutable(isMutable) {}
|
|
|
|
bool isMutable;
|
2021-04-23 01:01:21 +08:00
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2021-05-18 05:22:19 +08:00
|
|
|
serializer(ar, isMutable);
|
2021-04-23 01:01:21 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-23 21:19:48 +08:00
|
|
|
// Allows client to check if allowed to change the cluster descriptor.
|
2021-05-18 05:22:19 +08:00
|
|
|
struct CheckDescriptorMutableRequest {
|
2021-04-28 20:48:15 +08:00
|
|
|
constexpr static FileIdentifier file_identifier = 214729;
|
|
|
|
ReplyPromise<CheckDescriptorMutableReply> reply;
|
2021-05-18 05:22:19 +08:00
|
|
|
CheckDescriptorMutableRequest() {}
|
2021-04-28 20:48:15 +08:00
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2021-05-28 23:09:41 +08:00
|
|
|
serializer(ar, reply);
|
2021-04-28 20:48:15 +08:00
|
|
|
}
|
2021-04-23 01:01:21 +08:00
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
#endif
|