foundationdb/fdbrpc/Locality.h

383 lines
13 KiB
C
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* Locality.h
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 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 FLOW_LOCALITY_H
#define FLOW_LOCALITY_H
#pragma once
#include "flow/flow.h"
struct ProcessClass {
constexpr static FileIdentifier file_identifier = 6697257;
2017-05-26 04:48:44 +08:00
// This enum is stored in restartInfo.ini for upgrade tests, so be very careful about changing the existing items!
2019-08-02 08:00:13 +08:00
enum ClassType {
UnsetClass,
StorageClass,
TransactionClass,
ResolutionClass,
TesterClass,
2020-09-11 08:44:15 +08:00
CommitProxyClass,
2019-08-02 08:00:13 +08:00
MasterClass,
StatelessClass,
LogClass,
ClusterControllerClass,
LogRouterClass,
FastRestoreClass,
DataDistributorClass,
CoordinatorClass,
RatekeeperClass,
2019-11-13 05:01:29 +08:00
StorageCacheClass,
BackupClass,
GrvProxyClass,
2019-08-02 08:00:13 +08:00
InvalidClass = -1
};
enum Fitness {
BestFit,
GoodFit,
UnsetFit,
OkayFit,
WorstFit,
ExcludeFit,
NeverAssign
}; // cannot be larger than 7 because of leader election mask
enum ClusterRole {
Storage,
TLog,
2020-09-11 08:44:15 +08:00
CommitProxy,
GrvProxy,
Master,
Resolver,
LogRouter,
ClusterController,
DataDistributor,
Ratekeeper,
StorageCache,
Backup,
NoRole
};
2017-05-26 04:48:44 +08:00
enum ClassSource { CommandLineSource, AutoSource, DBSource, InvalidSource = -1 };
int16_t _class;
int16_t _source;
public:
ProcessClass() : _class(UnsetClass), _source(CommandLineSource) {}
ProcessClass(ClassType type, ClassSource source) : _class(type), _source(source) {}
2019-08-02 08:00:13 +08:00
// clang-format off
2017-05-26 04:48:44 +08:00
explicit ProcessClass( std::string s, ClassSource source ) : _source( source ) {
if (s=="storage") _class = StorageClass;
else if (s=="transaction") _class = TransactionClass;
else if (s=="resolution") _class = ResolutionClass;
2020-09-11 08:44:15 +08:00
else if (s=="commit_proxy") _class = CommitProxyClass;
2020-09-30 16:13:05 +08:00
else if (s=="proxy") {
_class = CommitProxyClass;
printf("WARNING: 'proxy' machine class is deprecated and will be automatically converted "
"'commit_proxy' machine class. Please use 'grv_proxy' or 'commit_proxy' specifically\n");
}
else if (s=="grv_proxy") _class = GrvProxyClass;
2017-05-26 04:48:44 +08:00
else if (s=="master") _class = MasterClass;
else if (s=="test") _class = TesterClass;
else if (s=="unset") _class = UnsetClass;
else if (s=="stateless") _class = StatelessClass;
else if (s=="log") _class = LogClass;
else if (s=="router") _class = LogRouterClass;
2017-10-17 07:21:25 +08:00
else if (s=="cluster_controller") _class = ClusterControllerClass;
else if (s=="fast_restore") _class = FastRestoreClass;
else if (s=="data_distributor") _class = DataDistributorClass;
else if (s=="coordinator") _class = CoordinatorClass;
2019-03-27 23:24:25 +08:00
else if (s=="ratekeeper") _class = RatekeeperClass;
2019-11-13 05:01:29 +08:00
else if (s=="storage_cache") _class = StorageCacheClass;
else if (s=="backup") _class = BackupClass;
2017-05-26 04:48:44 +08:00
else _class = InvalidClass;
}
ProcessClass( std::string classStr, std::string sourceStr ) {
if (classStr=="storage") _class = StorageClass;
else if (classStr=="transaction") _class = TransactionClass;
else if (classStr=="resolution") _class = ResolutionClass;
2020-09-11 08:44:15 +08:00
else if (classStr=="commit_proxy") _class = CommitProxyClass;
2020-09-30 16:13:05 +08:00
else if (classStr=="proxy") {
_class = CommitProxyClass;
printf("WARNING: 'proxy' machine class is deprecated and will be automatically converted "
"'commit_proxy' machine class. Please use 'grv_proxy' or 'commit_proxy' specifically\n");
}
else if (classStr=="grv_proxy") _class = GrvProxyClass;
2017-05-26 04:48:44 +08:00
else if (classStr=="master") _class = MasterClass;
else if (classStr=="test") _class = TesterClass;
else if (classStr=="unset") _class = UnsetClass;
else if (classStr=="stateless") _class = StatelessClass;
else if (classStr=="log") _class = LogClass;
else if (classStr=="router") _class = LogRouterClass;
2017-10-17 07:21:25 +08:00
else if (classStr=="cluster_controller") _class = ClusterControllerClass;
2019-08-02 08:00:13 +08:00
else if (classStr == "fast_restore") _class = FastRestoreClass;
else if (classStr=="data_distributor") _class = DataDistributorClass;
else if (classStr=="coordinator") _class = CoordinatorClass;
2019-03-27 23:24:25 +08:00
else if (classStr=="ratekeeper") _class = RatekeeperClass;
2019-11-13 05:01:29 +08:00
else if (classStr=="storage_cache") _class = StorageCacheClass;
else if (classStr=="backup") _class = BackupClass;
2017-05-26 04:48:44 +08:00
else _class = InvalidClass;
if (sourceStr=="command_line") _source = CommandLineSource;
else if (sourceStr=="configure_auto") _source = AutoSource;
else if (sourceStr=="set_class") _source = DBSource;
else _source = InvalidSource;
}
ClassType classType() const { return (ClassType)_class; }
ClassSource classSource() const { return (ClassSource)_source; }
bool operator == ( const ClassType& rhs ) const { return _class == rhs; }
bool operator != ( const ClassType& rhs ) const { return _class != rhs; }
bool operator == ( const ProcessClass& rhs ) const { return _class == rhs._class && _source == rhs._source; }
bool operator != ( const ProcessClass& rhs ) const { return _class != rhs._class || _source != rhs._source; }
std::string toString() const {
switch (_class) {
case UnsetClass: return "unset";
case StorageClass: return "storage";
case TransactionClass: return "transaction";
case ResolutionClass: return "resolution";
2020-09-11 08:44:15 +08:00
case CommitProxyClass: return "commit_proxy";
case GrvProxyClass: return "grv_proxy";
2017-05-26 04:48:44 +08:00
case MasterClass: return "master";
case TesterClass: return "test";
case StatelessClass: return "stateless";
case LogClass: return "log";
case LogRouterClass: return "router";
2017-10-17 07:21:25 +08:00
case ClusterControllerClass: return "cluster_controller";
2019-09-04 06:50:21 +08:00
case FastRestoreClass: return "fast_restore";
case DataDistributorClass: return "data_distributor";
case CoordinatorClass: return "coordinator";
2019-03-27 23:24:25 +08:00
case RatekeeperClass: return "ratekeeper";
2019-11-13 05:01:29 +08:00
case StorageCacheClass: return "storage_cache";
case BackupClass: return "backup";
2017-05-26 04:48:44 +08:00
default: return "invalid";
}
}
2019-08-02 08:00:13 +08:00
// clang-format on
2017-05-26 04:48:44 +08:00
std::string sourceString() const {
switch (_source) {
case CommandLineSource:
return "command_line";
case AutoSource:
return "configure_auto";
case DBSource:
return "set_class";
default:
return "invalid";
2017-05-26 04:48:44 +08:00
}
}
Fitness machineClassFitness(ClusterRole role) const;
2017-05-26 04:48:44 +08:00
// To change this serialization, ProtocolVersion::ProcessClassValue must be updated, and downgrades need to be
// considered
2017-05-26 04:48:44 +08:00
template <class Ar>
void serialize(Ar& ar) {
serializer(ar, _class, _source);
2017-05-26 04:48:44 +08:00
}
};
struct LocalityData {
std::map<Standalone<StringRef>, Optional<Standalone<StringRef>>> _data;
2017-05-26 04:48:44 +08:00
static const StringRef keyProcessId;
static const StringRef keyZoneId;
static const StringRef keyDcId;
static const StringRef keyMachineId;
static const StringRef keyDataHallId;
2017-05-26 04:48:44 +08:00
public:
LocalityData() {}
LocalityData(Optional<Standalone<StringRef>> processID,
Optional<Standalone<StringRef>> zoneID,
Optional<Standalone<StringRef>> MachineID,
Optional<Standalone<StringRef>> dcID) {
2017-05-26 04:48:44 +08:00
_data[keyProcessId] = processID;
_data[keyZoneId] = zoneID;
_data[keyMachineId] = MachineID;
_data[keyDcId] = dcID;
}
bool operator==(LocalityData const& rhs) const {
return ((_data.size() == rhs._data.size()) && (std::equal(_data.begin(), _data.end(), rhs._data.begin())));
2017-05-26 04:48:44 +08:00
}
bool operator!=(LocalityData const& rhs) const { return !(*this == rhs); }
2017-05-26 04:48:44 +08:00
Optional<Standalone<StringRef>> get(StringRef key) const {
2017-05-26 04:48:44 +08:00
auto pos = _data.find(key);
return (pos == _data.end()) ? Optional<Standalone<StringRef>>() : pos->second;
}
void set(StringRef key, Optional<Standalone<StringRef>> value) { _data[key] = value; }
2017-05-26 04:48:44 +08:00
bool isPresent(StringRef key) const { return (_data.find(key) != _data.end()); }
bool isPresent(StringRef key, Optional<Standalone<StringRef>> value) const {
2017-05-26 04:48:44 +08:00
auto pos = _data.find(key);
return (pos != _data.end()) ? false : (pos->second == value);
}
std::string describeValue(StringRef key) const {
auto value = get(key);
return (value.present()) ? value.get().toString() : "[unset]";
}
std::string describeZone() const { return describeValue(keyZoneId); }
std::string describeDataHall() const { return describeValue(keyDataHallId); }
std::string describeDcId() const { return describeValue(keyDcId); }
std::string describeMachineId() const { return describeValue(keyMachineId); }
std::string describeProcessId() const { return describeValue(keyProcessId); }
2017-05-26 04:48:44 +08:00
Optional<Standalone<StringRef>> processId() const { return get(keyProcessId); }
Optional<Standalone<StringRef>> zoneId() const { return get(keyZoneId); }
Optional<Standalone<StringRef>> machineId() const { return get(keyMachineId); } // default is ""
2017-05-26 04:48:44 +08:00
Optional<Standalone<StringRef>> dcId() const { return get(keyDcId); }
Optional<Standalone<StringRef>> dataHallId() const { return get(keyDataHallId); }
std::string toString() const {
std::string infoString;
2017-05-26 04:48:44 +08:00
for (auto it = _data.rbegin(); !(it == _data.rend()); ++it) {
if (infoString.length()) {
infoString += " ";
}
2017-05-26 04:48:44 +08:00
infoString += it->first.printable() + "=";
infoString += (it->second.present()) ? it->second.get().printable() : "[unset]";
}
return infoString;
}
template <class Ar>
void serialize(Ar& ar) {
// Locality is persisted in the database inside StorageServerInterface, so changes here have to be
// versioned carefully!
2019-01-29 11:38:13 +08:00
if constexpr (is_fb_function<Ar>) {
serializer(ar, _data);
} else {
2019-06-19 08:55:27 +08:00
if (ar.protocolVersion().hasLocality()) {
2019-01-29 11:38:13 +08:00
Standalone<StringRef> key;
Optional<Standalone<StringRef>> value;
uint64_t mapSize = (uint64_t)_data.size();
serializer(ar, mapSize);
if (ar.isDeserializing) {
for (size_t i = 0; i < mapSize; i++) {
serializer(ar, key, value);
_data[key] = value;
}
} else {
for (auto it = _data.begin(); it != _data.end(); it++) {
key = it->first;
value = it->second;
serializer(ar, key, value);
}
2017-05-26 04:48:44 +08:00
}
2019-01-29 11:38:13 +08:00
} else {
ASSERT(ar.isDeserializing);
UID zoneId, dcId, processId;
serializer(ar, zoneId, dcId);
set(keyZoneId, Standalone<StringRef>(zoneId.toString()));
set(keyDcId, Standalone<StringRef>(dcId.toString()));
2019-06-19 08:55:27 +08:00
if (ar.protocolVersion().hasProcessID()) {
2019-01-29 11:38:13 +08:00
serializer(ar, processId);
set(keyProcessId, Standalone<StringRef>(processId.toString()));
} else {
int _machineClass = ProcessClass::UnsetClass;
serializer(ar, _machineClass);
2017-05-26 04:48:44 +08:00
}
}
}
}
static const UID UNSET_ID;
};
static std::string describe(std::vector<LocalityData> const& items, StringRef const key, int max_items = -1) {
if (!items.size())
2017-05-26 04:48:44 +08:00
return "[no items]";
std::string s;
int count = 0;
for (auto const& item : items) {
if (++count > max_items && max_items >= 0)
2017-05-26 04:48:44 +08:00
break;
if (count > 1)
s += ",";
2017-05-26 04:48:44 +08:00
s += item.describeValue(key);
}
return s;
}
inline std::string describeZones(std::vector<LocalityData> const& items, int max_items = -1) {
2017-05-26 04:48:44 +08:00
return describe(items, LocalityData::keyZoneId, max_items);
}
inline std::string describeDataHalls(std::vector<LocalityData> const& items, int max_items = -1) {
2017-05-26 04:48:44 +08:00
return describe(items, LocalityData::keyDataHallId, max_items);
}
struct ProcessData {
LocalityData locality;
ProcessClass processClass;
NetworkAddress address;
ProcessData() {}
ProcessData(LocalityData locality, ProcessClass processClass, NetworkAddress address)
: locality(locality), processClass(processClass), address(address) {}
2017-05-26 04:48:44 +08:00
// To change this serialization, ProtocolVersion::WorkerListValue must be updated, and downgrades need to be
// considered
2017-05-26 04:48:44 +08:00
template <class Ar>
void serialize(Ar& ar) {
serializer(ar, locality, processClass, address);
2017-05-26 04:48:44 +08:00
}
struct sort_by_address {
bool operator()(ProcessData const& a, ProcessData const& b) const { return a.address < b.address; }
2017-05-26 04:48:44 +08:00
};
};
template <class Interface, class Enable = void>
struct LBLocalityData {
enum { Present = 0 };
static LocalityData getLocality(Interface const&) { return LocalityData(); }
static NetworkAddress getAddress(Interface const&) { return NetworkAddress(); }
static bool alwaysFresh() { return true; }
2017-05-26 04:48:44 +08:00
};
// Template specialization that only works for interfaces with a .locality member.
// If an interface has a .locality it must also have a .address()
template <class Interface>
struct LBLocalityData<Interface, typename std::enable_if<Interface::LocationAwareLoadBalance>::type> {
2017-05-26 04:48:44 +08:00
enum { Present = 1 };
static LocalityData getLocality(Interface const& i) { return i.locality; }
static NetworkAddress getAddress(Interface const& i) { return i.address(); }
static bool alwaysFresh() { return Interface::AlwaysFresh; }
2017-05-26 04:48:44 +08:00
};
struct LBDistance {
enum Type { SAME_MACHINE = 0, SAME_DC = 1, DISTANT = 2 };
2017-05-26 04:48:44 +08:00
};
LBDistance::Type loadBalanceDistance(LocalityData const& localLoc,
LocalityData const& otherLoc,
NetworkAddress const& otherAddr);
2017-05-26 04:48:44 +08:00
#endif