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"
|
2019-07-24 10:22:44 +08:00
|
|
|
#include "fdbclient/MasterProxyInterface.h"
|
|
|
|
#include "fdbclient/ClusterInterface.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
const int MAX_CLUSTER_FILE_BYTES = 60000;
|
|
|
|
|
|
|
|
struct ClientLeaderRegInterface {
|
|
|
|
RequestStream< struct GetLeaderRequest > getLeader;
|
2019-07-24 10:22:44 +08:00
|
|
|
RequestStream< struct OpenDatabaseCoordRequest > openDatabase;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
ClientLeaderRegInterface() {}
|
|
|
|
ClientLeaderRegInterface( NetworkAddress remote );
|
|
|
|
ClientLeaderRegInterface( INetwork* local );
|
|
|
|
};
|
|
|
|
|
|
|
|
class ClusterConnectionString {
|
|
|
|
public:
|
|
|
|
ClusterConnectionString() {}
|
|
|
|
ClusterConnectionString( std::string const& connectionString );
|
|
|
|
ClusterConnectionString( vector<NetworkAddress>, Key );
|
|
|
|
vector<NetworkAddress> const& coordinators() const { return coord; }
|
|
|
|
Key clusterKey() const { return key; }
|
|
|
|
Key clusterKeyName() const { return keyDesc; } // Returns the "name" or "description" part of the clusterKey (the part before the ':')
|
|
|
|
std::string toString() const;
|
|
|
|
static std::string getErrorString(std::string const& source, Error const& e);
|
|
|
|
private:
|
|
|
|
void parseKey( std::string const& key );
|
|
|
|
|
|
|
|
vector<NetworkAddress> coord;
|
|
|
|
Key key, keyDesc;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ClusterConnectionFile : NonCopyable, public ReferenceCounted<ClusterConnectionFile> {
|
|
|
|
public:
|
|
|
|
ClusterConnectionFile() {}
|
|
|
|
// Loads and parses the file at 'path', throwing errors if the file cannot be read or the format is invalid.
|
|
|
|
//
|
|
|
|
// The format of the file 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
|
|
|
|
explicit ClusterConnectionFile( std::string const& path );
|
|
|
|
explicit ClusterConnectionFile(ClusterConnectionString const& cs) : cs(cs), setConn(false) {}
|
|
|
|
explicit ClusterConnectionFile(std::string const& filename, ClusterConnectionString const& contents);
|
|
|
|
|
|
|
|
// returns <resolved name, was default file>
|
|
|
|
static std::pair<std::string, bool> lookupClusterFileName( std::string const& filename );
|
|
|
|
// get a human readable error message describing the error returned from the constructor
|
|
|
|
static std::string getErrorString( std::pair<std::string, bool> const& resolvedFile, Error const& e );
|
|
|
|
|
2018-10-23 08:57:09 +08:00
|
|
|
ClusterConnectionString const& getConnectionString() const;
|
2017-05-26 04:48:44 +08:00
|
|
|
bool writeFile();
|
|
|
|
void setConnectionString( ClusterConnectionString const& );
|
|
|
|
std::string const& getFilename() const { ASSERT( filename.size() ); return filename; }
|
2018-09-22 06:58:14 +08:00
|
|
|
bool canGetFilename() const { return filename.size() != 0; }
|
2017-05-26 04:48:44 +08:00
|
|
|
bool fileContentsUpToDate() const;
|
|
|
|
bool fileContentsUpToDate(ClusterConnectionString &fileConnectionString) const;
|
|
|
|
void notifyConnected();
|
|
|
|
private:
|
|
|
|
ClusterConnectionString cs;
|
|
|
|
std::string filename;
|
|
|
|
bool setConn;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LeaderInfo {
|
2019-01-31 05:53:23 +08:00
|
|
|
constexpr static FileIdentifier file_identifier = 8338794;
|
2017-05-26 04:48:44 +08:00
|
|
|
UID changeID;
|
2018-06-02 09:23:24 +08:00
|
|
|
static const uint64_t mask = ~(127ll << 57);
|
2017-05-26 04:48:44 +08:00
|
|
|
Value serializedInfo;
|
|
|
|
bool forward; // If true, serializedInfo is a connection string instead!
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
bool operator < (LeaderInfo const& r) const { return changeID < r.changeID; }
|
|
|
|
bool operator == (LeaderInfo const& r) const { return changeID == r.changeID; }
|
|
|
|
|
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) {
|
2018-02-27 05:13:37 +08:00
|
|
|
changeID = UID( ((uint64_t)info.processClassFitness << 57) | ((uint64_t)info.isExcluded << 60) | ((uint64_t)info.dcFitness << 61) | (changeID.first() & mask), 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 {
|
2018-08-14 08:59:47 +08:00
|
|
|
return ((changeID.first() & mask) == (leaderInfo.changeID.first() & mask)) && 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 {
|
2018-08-14 11:56:02 +08:00
|
|
|
return ((changeID.first() & ~mask) > (candidate.changeID.first() & ~mask) && !equalInternalId(candidate)) || ((changeID.first() & ~mask) < (candidate.changeID.first() & ~mask) && equalInternalId(candidate));
|
2017-10-13 08:11:58 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
ReplyPromise< Optional<LeaderInfo> > reply;
|
|
|
|
|
|
|
|
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;
|
|
|
|
// Sent by the native API to the cluster controller to open a database and track client
|
|
|
|
// 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)
|
|
|
|
Arena arena;
|
|
|
|
StringRef traceLogGroup;
|
|
|
|
VectorRef<StringRef> issues;
|
|
|
|
VectorRef<ClientVersionRef> supportedVersions;
|
|
|
|
UID knownClientInfoID;
|
|
|
|
Key key;
|
|
|
|
Key serializedInfo;
|
|
|
|
ReplyPromise< struct ClientDBInfo > reply;
|
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
|
|
|
serializer(ar, issues, supportedVersions, traceLogGroup, knownClientInfoID, key, serializedInfo, reply, arena);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
class ClientCoordinators {
|
|
|
|
public:
|
|
|
|
vector< ClientLeaderRegInterface > clientLeaderServers;
|
|
|
|
Key clusterKey;
|
|
|
|
Reference<ClusterConnectionFile> ccf;
|
|
|
|
|
|
|
|
explicit ClientCoordinators( Reference<ClusterConnectionFile> ccf );
|
|
|
|
ClientCoordinators() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|