2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* network.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 FLOW_OPENNETWORK_H
|
|
|
|
#define FLOW_OPENNETWORK_H
|
2020-09-29 01:58:49 +08:00
|
|
|
#include "flow/ProtocolVersion.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-27 10:04:03 +08:00
|
|
|
#include <array>
|
2021-11-03 13:59:28 +08:00
|
|
|
#include <regex>
|
2017-05-26 04:48:44 +08:00
|
|
|
#include <string>
|
|
|
|
#include <stdint.h>
|
2019-04-10 05:29:21 +08:00
|
|
|
#include <variant>
|
2021-03-16 10:16:43 +08:00
|
|
|
#include <atomic>
|
2019-03-06 03:57:49 +08:00
|
|
|
#include "boost/asio.hpp"
|
2020-02-20 10:37:47 +08:00
|
|
|
#ifndef TLS_DISABLED
|
2020-02-05 06:56:40 +08:00
|
|
|
#include "boost/asio/ssl.hpp"
|
2020-02-20 10:37:47 +08:00
|
|
|
#endif
|
2020-11-06 02:25:21 +08:00
|
|
|
#include "flow/Arena.h"
|
2021-11-03 13:59:28 +08:00
|
|
|
#include "flow/BooleanParam.h"
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "flow/IRandom.h"
|
2020-11-06 02:25:21 +08:00
|
|
|
#include "flow/Trace.h"
|
2021-06-04 06:10:04 +08:00
|
|
|
#include "flow/WriteOnlySet.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-25 17:47:35 +08:00
|
|
|
enum class TaskPriority {
|
|
|
|
Max = 1000000,
|
2020-01-16 07:51:15 +08:00
|
|
|
RunLoop = 30000,
|
2019-08-29 05:35:48 +08:00
|
|
|
ASIOReactor = 20001,
|
2019-06-25 17:47:35 +08:00
|
|
|
RunCycleFunction = 20000,
|
|
|
|
FlushTrace = 10500,
|
|
|
|
WriteSocket = 10000,
|
|
|
|
PollEIO = 9900,
|
|
|
|
DiskIOComplete = 9150,
|
|
|
|
LoadBalancedEndpoint = 9000,
|
|
|
|
ReadSocket = 9000,
|
2020-03-05 06:25:30 +08:00
|
|
|
AcceptSocket = 8950,
|
2020-02-21 02:11:43 +08:00
|
|
|
Handshake = 8900,
|
2019-06-25 17:47:35 +08:00
|
|
|
CoordinationReply = 8810,
|
|
|
|
Coordination = 8800,
|
|
|
|
FailureMonitor = 8700,
|
|
|
|
ResolutionMetrics = 8700,
|
2019-11-12 07:17:11 +08:00
|
|
|
Worker = 8660,
|
2019-12-05 08:28:41 +08:00
|
|
|
ClusterControllerWorker = 8656,
|
|
|
|
ClusterControllerRecruit = 8654,
|
|
|
|
ClusterControllerRegister = 8652,
|
2019-06-25 17:47:35 +08:00
|
|
|
ClusterController = 8650,
|
2019-07-20 07:55:04 +08:00
|
|
|
MasterTLogRejoin = 8646,
|
2019-06-25 17:47:35 +08:00
|
|
|
ProxyStorageRejoin = 8645,
|
|
|
|
TLogQueuingMetrics = 8620,
|
|
|
|
TLogPop = 8610,
|
|
|
|
TLogPeekReply = 8600,
|
|
|
|
TLogPeek = 8590,
|
|
|
|
TLogCommitReply = 8580,
|
|
|
|
TLogCommit = 8570,
|
2020-07-01 15:12:50 +08:00
|
|
|
ReportLiveCommittedVersion = 8567,
|
2019-06-25 17:47:35 +08:00
|
|
|
ProxyGetRawCommittedVersion = 8565,
|
2020-01-27 11:47:12 +08:00
|
|
|
ProxyMasterVersionReply = 8560,
|
2019-12-19 08:56:35 +08:00
|
|
|
ProxyCommitYield2 = 8557,
|
2020-01-27 11:47:12 +08:00
|
|
|
ProxyTLogCommitReply = 8555,
|
|
|
|
ProxyCommitYield1 = 8550,
|
|
|
|
ProxyResolverReply = 8547,
|
2019-12-19 08:56:35 +08:00
|
|
|
ProxyCommit = 8545,
|
|
|
|
ProxyCommitBatcher = 8540,
|
2019-06-25 17:47:35 +08:00
|
|
|
TLogConfirmRunningReply = 8530,
|
|
|
|
TLogConfirmRunning = 8520,
|
|
|
|
ProxyGRVTimer = 8510,
|
2020-03-17 03:11:32 +08:00
|
|
|
GetConsistentReadVersion = 8500,
|
2020-06-17 11:37:30 +08:00
|
|
|
GetLiveCommittedVersionReply = 8490,
|
|
|
|
GetLiveCommittedVersion = 8480,
|
2022-01-07 04:15:51 +08:00
|
|
|
UpdateRecoveryTransactionVersion = 8470,
|
2019-06-25 17:47:35 +08:00
|
|
|
DefaultPromiseEndpoint = 8000,
|
|
|
|
DefaultOnMainThread = 7500,
|
|
|
|
DefaultDelay = 7010,
|
|
|
|
DefaultYield = 7000,
|
|
|
|
DiskRead = 5010,
|
|
|
|
DefaultEndpoint = 5000,
|
|
|
|
UnknownEndpoint = 4000,
|
|
|
|
MoveKeys = 3550,
|
|
|
|
DataDistributionLaunch = 3530,
|
|
|
|
Ratekeeper = 3510,
|
2020-02-08 06:34:24 +08:00
|
|
|
DataDistribution = 3502,
|
|
|
|
DataDistributionLow = 3501,
|
|
|
|
DataDistributionVeryLow = 3500,
|
2021-10-05 06:26:36 +08:00
|
|
|
BlobManager = 3490,
|
2019-07-04 11:44:15 +08:00
|
|
|
DiskWrite = 3010,
|
2019-06-25 17:47:35 +08:00
|
|
|
UpdateStorage = 3000,
|
2019-11-13 05:01:29 +08:00
|
|
|
CompactCache = 2900,
|
2019-06-25 17:47:35 +08:00
|
|
|
TLogSpilledPeekReply = 2800,
|
2021-10-06 05:51:19 +08:00
|
|
|
BlobWorkerReadChangeFeed = 2720,
|
|
|
|
BlobWorkerUpdateFDB = 2710,
|
|
|
|
BlobWorkerUpdateStorage = 2700,
|
2019-07-04 02:14:32 +08:00
|
|
|
FetchKeys = 2500,
|
2020-06-17 13:48:28 +08:00
|
|
|
RestoreApplierWriteDB = 2310,
|
|
|
|
RestoreApplierReceiveMutations = 2300,
|
|
|
|
RestoreLoaderFinishVersionBatch = 2220,
|
|
|
|
RestoreLoaderSendMutations = 2210,
|
2020-02-11 12:26:56 +08:00
|
|
|
RestoreLoaderLoadFiles = 2200,
|
2021-02-09 01:20:28 +08:00
|
|
|
LowPriorityRead = 2100,
|
2019-06-25 17:47:35 +08:00
|
|
|
Low = 2000,
|
|
|
|
|
|
|
|
Min = 1000,
|
|
|
|
Zero = 0
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2019-07-04 12:03:58 +08:00
|
|
|
// These have been given long, annoying names to discourage their use.
|
|
|
|
|
2019-06-25 17:47:35 +08:00
|
|
|
inline TaskPriority incrementPriority(TaskPriority p) {
|
2021-03-11 02:06:03 +08:00
|
|
|
return static_cast<TaskPriority>(static_cast<uint64_t>(p) + 1);
|
2019-06-25 17:47:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline TaskPriority decrementPriority(TaskPriority p) {
|
2021-03-11 02:06:03 +08:00
|
|
|
return static_cast<TaskPriority>(static_cast<uint64_t>(p) - 1);
|
2019-07-04 12:03:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline TaskPriority incrementPriorityIfEven(TaskPriority p) {
|
2021-03-11 02:06:03 +08:00
|
|
|
return static_cast<TaskPriority>(static_cast<uint64_t>(p) | 1);
|
2019-06-25 17:47:35 +08:00
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
class Void;
|
|
|
|
|
2021-11-03 13:59:28 +08:00
|
|
|
struct Hostname {
|
|
|
|
std::string host;
|
|
|
|
std::string service; // decimal port number
|
2022-01-21 04:19:58 +08:00
|
|
|
bool isTLS;
|
2021-11-03 13:59:28 +08:00
|
|
|
|
2022-01-21 04:19:58 +08:00
|
|
|
Hostname(std::string host, std::string service, bool isTLS) : host(host), service(service), isTLS(isTLS) {}
|
2022-01-21 07:32:03 +08:00
|
|
|
Hostname() : host(""), service(""), isTLS(false) {}
|
2021-11-03 13:59:28 +08:00
|
|
|
|
2022-02-25 08:34:30 +08:00
|
|
|
bool operator==(const Hostname& r) const { return host == r.host && service == r.service && isTLS == r.isTLS; }
|
|
|
|
bool operator!=(const Hostname& r) const { return !(*this == r); }
|
|
|
|
bool operator<(const Hostname& r) const {
|
|
|
|
if (isTLS != r.isTLS)
|
|
|
|
return isTLS < r.isTLS;
|
|
|
|
else if (host != r.host)
|
|
|
|
return host < r.host;
|
|
|
|
return service < r.service;
|
|
|
|
}
|
|
|
|
bool operator>(const Hostname& r) const { return r < *this; }
|
|
|
|
bool operator<=(const Hostname& r) const { return !(*this > r); }
|
|
|
|
bool operator>=(const Hostname& r) const { return !(*this < r); }
|
|
|
|
|
2021-11-03 13:59:28 +08:00
|
|
|
// Allow hostnames in forms like following:
|
|
|
|
// hostname:1234
|
|
|
|
// host.name:1234
|
|
|
|
// host-name:1234
|
|
|
|
// host-name_part1.host-name_part2:1234:tls
|
2022-02-25 08:34:30 +08:00
|
|
|
static bool isHostname(const std::string& s) {
|
2021-11-03 13:59:28 +08:00
|
|
|
std::regex validation("^([\\w\\-]+\\.?)+:([\\d]+){1,}(:tls)?$");
|
|
|
|
std::regex ipv4Validation("^([\\d]{1,3}\\.?){4,}:([\\d]+){1,}(:tls)?$");
|
|
|
|
return !std::regex_match(s, ipv4Validation) && std::regex_match(s, validation);
|
|
|
|
}
|
|
|
|
|
2022-02-25 08:34:30 +08:00
|
|
|
static Hostname parse(const std::string& s);
|
2021-11-03 13:59:28 +08:00
|
|
|
|
2022-01-21 04:19:58 +08:00
|
|
|
std::string toString() const { return host + ":" + service + (isTLS ? ":tls" : ""); }
|
2021-11-03 13:59:28 +08:00
|
|
|
};
|
|
|
|
|
2019-02-27 10:04:03 +08:00
|
|
|
struct IPAddress {
|
2019-03-06 03:57:49 +08:00
|
|
|
typedef boost::asio::ip::address_v6::bytes_type IPAddressStore;
|
|
|
|
static_assert(std::is_same<IPAddressStore, std::array<uint8_t, 16>>::value,
|
|
|
|
"IPAddressStore must be std::array<uint8_t, 16>");
|
2019-02-27 10:04:03 +08:00
|
|
|
|
2019-06-19 05:30:30 +08:00
|
|
|
public:
|
|
|
|
// Represents both IPv4 and IPv6 address. For IPv4 addresses,
|
|
|
|
// only the first 32bits are relevant and rest are initialized to
|
|
|
|
// 0.
|
2019-04-10 05:29:21 +08:00
|
|
|
IPAddress() : addr(uint32_t(0)) {}
|
|
|
|
explicit IPAddress(const IPAddressStore& v6addr) : addr(v6addr) {}
|
|
|
|
explicit IPAddress(uint32_t v4addr) : addr(v4addr) {}
|
2019-02-27 10:04:03 +08:00
|
|
|
|
2020-06-28 06:20:39 +08:00
|
|
|
bool isV6() const { return std::holds_alternative<IPAddressStore>(addr); }
|
2019-04-10 05:29:21 +08:00
|
|
|
bool isV4() const { return !isV6(); }
|
2019-02-27 10:04:03 +08:00
|
|
|
bool isValid() const;
|
|
|
|
|
|
|
|
// Returns raw v4/v6 representation of address. Caller is responsible
|
|
|
|
// to call these functions safely.
|
2020-06-28 06:20:39 +08:00
|
|
|
uint32_t toV4() const { return std::get<uint32_t>(addr); }
|
|
|
|
const IPAddressStore& toV6() const { return std::get<IPAddressStore>(addr); }
|
2019-02-27 10:04:03 +08:00
|
|
|
|
|
|
|
std::string toString() const;
|
2021-06-10 02:37:14 +08:00
|
|
|
static Optional<IPAddress> parse(std::string const& str);
|
2019-02-27 10:04:03 +08:00
|
|
|
|
|
|
|
bool operator==(const IPAddress& addr) const;
|
|
|
|
bool operator!=(const IPAddress& addr) const;
|
|
|
|
bool operator<(const IPAddress& addr) const;
|
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2019-04-10 05:29:21 +08:00
|
|
|
if constexpr (is_fb_function<Ar>) {
|
|
|
|
serializer(ar, addr);
|
2019-02-27 10:04:03 +08:00
|
|
|
} else {
|
2019-04-13 05:30:46 +08:00
|
|
|
if (Ar::isDeserializing) {
|
|
|
|
bool v6;
|
|
|
|
serializer(ar, v6);
|
|
|
|
if (v6) {
|
|
|
|
IPAddressStore store;
|
|
|
|
serializer(ar, store);
|
|
|
|
addr = store;
|
|
|
|
} else {
|
|
|
|
uint32_t res;
|
|
|
|
serializer(ar, res);
|
|
|
|
addr = res;
|
|
|
|
}
|
2019-04-10 05:29:21 +08:00
|
|
|
} else {
|
2019-04-13 05:30:46 +08:00
|
|
|
bool v6 = isV6();
|
|
|
|
serializer(ar, v6);
|
|
|
|
if (v6) {
|
|
|
|
auto res = toV6();
|
|
|
|
serializer(ar, res);
|
|
|
|
} else {
|
|
|
|
auto res = toV4();
|
|
|
|
serializer(ar, res);
|
|
|
|
}
|
2019-04-10 05:29:21 +08:00
|
|
|
}
|
2019-02-27 10:04:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-06-28 06:20:39 +08:00
|
|
|
std::variant<uint32_t, IPAddressStore> addr;
|
2019-04-10 05:29:21 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-03-15 09:40:28 +08:00
|
|
|
struct Traceable<IPAddress> : std::true_type {
|
2021-03-11 02:06:03 +08:00
|
|
|
static std::string toString(const IPAddress& value) { return value.toString(); }
|
2019-03-15 09:40:28 +08:00
|
|
|
};
|
|
|
|
|
2021-11-03 13:59:28 +08:00
|
|
|
FDB_DECLARE_BOOLEAN_PARAM(NetworkAddressFromHostname);
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
struct NetworkAddress {
|
2019-04-10 05:29:21 +08:00
|
|
|
constexpr static FileIdentifier file_identifier = 14155727;
|
2017-05-26 04:48:44 +08:00
|
|
|
// A NetworkAddress identifies a particular running server (i.e. a TCP endpoint).
|
2019-02-27 10:04:03 +08:00
|
|
|
IPAddress ip;
|
2017-05-26 04:48:44 +08:00
|
|
|
uint16_t port;
|
|
|
|
uint16_t flags;
|
2021-11-23 18:08:17 +08:00
|
|
|
bool fromHostname;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
enum { FLAG_PRIVATE = 1, FLAG_TLS = 2 };
|
|
|
|
|
2021-11-03 13:59:28 +08:00
|
|
|
NetworkAddress()
|
|
|
|
: ip(IPAddress(0)), port(0), flags(FLAG_PRIVATE), fromHostname(NetworkAddressFromHostname::False) {}
|
|
|
|
NetworkAddress(const IPAddress& address,
|
|
|
|
uint16_t port,
|
|
|
|
bool isPublic,
|
|
|
|
bool isTLS,
|
|
|
|
NetworkAddressFromHostname fromHostname = NetworkAddressFromHostname::False)
|
|
|
|
: ip(address), port(port), flags((isPublic ? 0 : FLAG_PRIVATE) | (isTLS ? FLAG_TLS : 0)),
|
|
|
|
fromHostname(fromHostname) {}
|
|
|
|
NetworkAddress(uint32_t ip,
|
|
|
|
uint16_t port,
|
|
|
|
bool isPublic,
|
|
|
|
bool isTLS,
|
|
|
|
NetworkAddressFromHostname fromHostname = NetworkAddressFromHostname::False)
|
|
|
|
: NetworkAddress(IPAddress(ip), port, isPublic, isTLS, fromHostname) {}
|
|
|
|
|
|
|
|
NetworkAddress(uint32_t ip, uint16_t port)
|
|
|
|
: NetworkAddress(ip, port, false, false, NetworkAddressFromHostname::False) {}
|
|
|
|
NetworkAddress(const IPAddress& ip, uint16_t port)
|
|
|
|
: NetworkAddress(ip, port, false, false, NetworkAddressFromHostname::False) {}
|
2019-02-27 10:04:03 +08:00
|
|
|
|
|
|
|
bool operator==(NetworkAddress const& r) const { return ip == r.ip && port == r.port && flags == r.flags; }
|
2022-02-25 08:34:30 +08:00
|
|
|
bool operator!=(NetworkAddress const& r) const { return !(*this == r); }
|
2019-02-27 10:04:03 +08:00
|
|
|
bool operator<(NetworkAddress const& r) const {
|
|
|
|
if (flags != r.flags)
|
|
|
|
return flags < r.flags;
|
|
|
|
else if (ip != r.ip)
|
|
|
|
return ip < r.ip;
|
|
|
|
return port < r.port;
|
|
|
|
}
|
2020-07-11 05:37:47 +08:00
|
|
|
bool operator>(NetworkAddress const& r) const { return r < *this; }
|
|
|
|
bool operator<=(NetworkAddress const& r) const { return !(*this > r); }
|
|
|
|
bool operator>=(NetworkAddress const& r) const { return !(*this < r); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-02-27 10:04:03 +08:00
|
|
|
bool isValid() const { return ip.isValid() || port != 0; }
|
2017-05-26 04:48:44 +08:00
|
|
|
bool isPublic() const { return !(flags & FLAG_PRIVATE); }
|
|
|
|
bool isTLS() const { return (flags & FLAG_TLS) != 0; }
|
2019-02-27 10:04:03 +08:00
|
|
|
bool isV6() const { return ip.isV6(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-05-21 03:21:57 +08:00
|
|
|
size_t hash() const {
|
|
|
|
size_t result = 0;
|
2020-05-21 03:23:14 +08:00
|
|
|
if (ip.isV6()) {
|
|
|
|
uint16_t* ptr = (uint16_t*)ip.toV6().data();
|
2020-05-21 03:21:57 +08:00
|
|
|
result = ((size_t)ptr[5] << 32) | ((size_t)ptr[6] << 16) | ptr[7];
|
|
|
|
} else {
|
2020-05-21 03:23:14 +08:00
|
|
|
result = ip.toV4();
|
2020-05-21 03:21:57 +08:00
|
|
|
}
|
2020-05-21 03:23:14 +08:00
|
|
|
return (result << 16) + port;
|
2020-05-21 03:21:57 +08:00
|
|
|
}
|
|
|
|
|
2020-02-04 07:25:30 +08:00
|
|
|
static NetworkAddress parse(std::string const&); // May throw connection_string_invalid
|
|
|
|
static Optional<NetworkAddress> parseOptional(std::string const&);
|
2021-03-11 02:06:03 +08:00
|
|
|
static std::vector<NetworkAddress> parseList(std::string const&);
|
2017-05-26 04:48:44 +08:00
|
|
|
std::string toString() const;
|
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2019-04-10 05:29:21 +08:00
|
|
|
if constexpr (is_fb_function<Ar>) {
|
2021-11-23 18:08:17 +08:00
|
|
|
serializer(ar, ip, port, flags, fromHostname);
|
2019-04-10 05:29:21 +08:00
|
|
|
} else {
|
2019-06-19 08:55:27 +08:00
|
|
|
if (ar.isDeserializing && !ar.protocolVersion().hasIPv6()) {
|
2019-04-10 05:29:21 +08:00
|
|
|
uint32_t ipV4;
|
|
|
|
serializer(ar, ipV4, port, flags);
|
|
|
|
ip = IPAddress(ipV4);
|
|
|
|
} else {
|
|
|
|
serializer(ar, ip, port, flags);
|
|
|
|
}
|
2021-11-03 13:59:28 +08:00
|
|
|
if (ar.protocolVersion().hasNetworkAddressHostnameFlag()) {
|
2021-11-23 18:08:17 +08:00
|
|
|
serializer(ar, fromHostname);
|
2021-11-03 13:59:28 +08:00
|
|
|
}
|
2019-02-27 10:04:03 +08:00
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
2019-03-15 09:40:28 +08:00
|
|
|
struct Traceable<NetworkAddress> : std::true_type {
|
2021-03-11 02:06:03 +08:00
|
|
|
static std::string toString(const NetworkAddress& value) { return value.toString(); }
|
2019-03-15 09:40:28 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
namespace std {
|
|
|
|
template <>
|
|
|
|
struct hash<NetworkAddress> {
|
|
|
|
size_t operator()(const NetworkAddress& na) const { return na.hash(); }
|
|
|
|
};
|
|
|
|
} // namespace std
|
2019-03-28 09:21:13 +08:00
|
|
|
|
2019-03-24 08:54:46 +08:00
|
|
|
struct NetworkAddressList {
|
|
|
|
NetworkAddress address;
|
2021-10-29 04:52:52 +08:00
|
|
|
Optional<NetworkAddress> secondaryAddress{};
|
2019-03-24 08:54:46 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
bool operator==(NetworkAddressList const& r) const {
|
|
|
|
return address == r.address && secondaryAddress == r.secondaryAddress;
|
|
|
|
}
|
|
|
|
bool operator!=(NetworkAddressList const& r) const {
|
|
|
|
return address != r.address || secondaryAddress != r.secondaryAddress;
|
|
|
|
}
|
2019-03-24 08:54:46 +08:00
|
|
|
bool operator<(NetworkAddressList const& r) const {
|
|
|
|
if (address != r.address)
|
|
|
|
return address < r.address;
|
|
|
|
return secondaryAddress < r.secondaryAddress;
|
|
|
|
}
|
|
|
|
|
2020-02-11 06:00:21 +08:00
|
|
|
NetworkAddress getTLSAddress() const {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!secondaryAddress.present() || address.isTLS()) {
|
2020-02-11 06:00:21 +08:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
return secondaryAddress.get();
|
|
|
|
}
|
|
|
|
|
2019-03-24 08:54:46 +08:00
|
|
|
std::string toString() const {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!secondaryAddress.present()) {
|
2019-03-24 08:54:46 +08:00
|
|
|
return address.toString();
|
|
|
|
}
|
|
|
|
return address.toString() + ", " + secondaryAddress.get().toString();
|
|
|
|
}
|
|
|
|
|
2021-06-18 01:13:08 +08:00
|
|
|
bool contains(const NetworkAddress& r) const {
|
2021-06-16 08:36:56 +08:00
|
|
|
return address == r || (secondaryAddress.present() && secondaryAddress.get() == r);
|
|
|
|
}
|
|
|
|
|
2019-03-24 08:54:46 +08:00
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
|
|
|
serializer(ar, address, secondaryAddress);
|
|
|
|
}
|
|
|
|
};
|
2018-10-23 08:57:09 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
std::string toIPVectorString(std::vector<uint32_t> ips);
|
2019-02-28 09:53:38 +08:00
|
|
|
std::string toIPVectorString(const std::vector<IPAddress>& ips);
|
2019-02-28 18:37:21 +08:00
|
|
|
std::string formatIpPort(const IPAddress& ip, uint16_t port);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <class T>
|
|
|
|
class Future;
|
|
|
|
template <class T>
|
|
|
|
class Promise;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-24 04:56:37 +08:00
|
|
|
// Metrics which represent various network properties
|
2017-05-26 04:48:44 +08:00
|
|
|
struct NetworkMetrics {
|
|
|
|
enum { SLOW_EVENT_BINS = 16 };
|
2020-01-13 08:44:30 +08:00
|
|
|
uint64_t countSlowEvents[SLOW_EVENT_BINS] = {};
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-01-13 08:44:30 +08:00
|
|
|
double secSquaredSubmit = 0;
|
|
|
|
double secSquaredDiskStall = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-02-08 06:34:24 +08:00
|
|
|
struct PriorityStats {
|
|
|
|
TaskPriority priority;
|
|
|
|
|
|
|
|
bool active = false;
|
|
|
|
double duration = 0;
|
|
|
|
double timer = 0;
|
|
|
|
double windowedTimer = 0;
|
|
|
|
double maxDuration = 0;
|
|
|
|
|
|
|
|
PriorityStats(TaskPriority priority) : priority(priority) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unordered_map<TaskPriority, struct PriorityStats> activeTrackers;
|
2021-03-18 02:16:18 +08:00
|
|
|
double lastRunLoopBusyness; // network thread busyness (measured every 5s by default)
|
2021-07-29 07:03:37 +08:00
|
|
|
std::atomic<double>
|
|
|
|
networkBusyness; // network thread busyness which is returned to the the client (measured every 1s by default)
|
2021-03-18 02:16:18 +08:00
|
|
|
|
|
|
|
// starvation trackers which keeps track of different task priorities
|
2021-03-17 07:29:02 +08:00
|
|
|
std::vector<struct PriorityStats> starvationTrackers;
|
|
|
|
struct PriorityStats starvationTrackerNetworkBusyness;
|
2020-02-08 06:34:24 +08:00
|
|
|
|
|
|
|
static const std::vector<int> starvationBins;
|
|
|
|
|
2021-03-17 07:29:02 +08:00
|
|
|
NetworkMetrics()
|
|
|
|
: lastRunLoopBusyness(0), networkBusyness(0),
|
|
|
|
starvationTrackerNetworkBusyness(PriorityStats(static_cast<TaskPriority>(starvationBins.at(0)))) {
|
2021-03-18 02:16:18 +08:00
|
|
|
for (int priority : starvationBins) { // initalize starvation trackers with given priorities
|
2020-02-08 06:34:24 +08:00
|
|
|
starvationTrackers.emplace_back(static_cast<TaskPriority>(priority));
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 10:16:43 +08:00
|
|
|
|
2021-03-24 06:04:32 +08:00
|
|
|
// Since networkBusyness is atomic we need to redefine copy assignment operator
|
2021-03-16 10:16:43 +08:00
|
|
|
NetworkMetrics& operator=(const NetworkMetrics& rhs) {
|
|
|
|
for (int i = 0; i < SLOW_EVENT_BINS; i++) {
|
|
|
|
countSlowEvents[i] = rhs.countSlowEvents[i];
|
|
|
|
}
|
|
|
|
secSquaredSubmit = rhs.secSquaredSubmit;
|
|
|
|
secSquaredDiskStall = rhs.secSquaredDiskStall;
|
|
|
|
activeTrackers = rhs.activeTrackers;
|
|
|
|
lastRunLoopBusyness = rhs.lastRunLoopBusyness;
|
|
|
|
networkBusyness = rhs.networkBusyness.load();
|
|
|
|
starvationTrackers = rhs.starvationTrackers;
|
2021-03-17 07:29:02 +08:00
|
|
|
starvationTrackerNetworkBusyness = rhs.starvationTrackerNetworkBusyness;
|
2021-03-16 10:16:43 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2020-01-13 08:44:30 +08:00
|
|
|
};
|
|
|
|
|
2020-07-16 17:31:17 +08:00
|
|
|
struct FlowLock;
|
2020-02-15 08:49:52 +08:00
|
|
|
|
2020-01-13 08:44:30 +08:00
|
|
|
struct NetworkInfo {
|
|
|
|
NetworkMetrics metrics;
|
|
|
|
double oldestAlternativesFailure = 0;
|
|
|
|
double newestAlternativesFailure = 0;
|
|
|
|
double lastAlternativesFailureSkipDelay = 0;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
std::map<std::pair<IPAddress, uint16_t>, std::pair<int, double>> serverTLSConnectionThrottler;
|
|
|
|
FlowLock* handshakeLock;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-02-05 10:15:10 +08:00
|
|
|
NetworkInfo();
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class IEventFD : public ReferenceCounted<IEventFD> {
|
|
|
|
public:
|
|
|
|
virtual ~IEventFD() {}
|
|
|
|
virtual int getFD() = 0;
|
|
|
|
virtual Future<int64_t> read() = 0;
|
|
|
|
};
|
|
|
|
|
2020-11-17 06:46:36 +08:00
|
|
|
// forward declare SendBuffer, declared in serialize.h
|
2021-10-29 05:16:14 +08:00
|
|
|
class SendBuffer;
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
class IConnection {
|
|
|
|
public:
|
|
|
|
// IConnection is reference-counted (use Reference<IConnection>), but the caller must explicitly call close()
|
|
|
|
virtual void addref() = 0;
|
|
|
|
virtual void delref() = 0;
|
|
|
|
|
|
|
|
// Closes the underlying connection eventually if it is not already closed.
|
|
|
|
virtual void close() = 0;
|
2019-02-01 10:20:14 +08:00
|
|
|
|
2020-02-07 08:45:54 +08:00
|
|
|
virtual Future<Void> acceptHandshake() = 0;
|
|
|
|
|
|
|
|
virtual Future<Void> connectHandshake() = 0;
|
|
|
|
|
2020-04-25 08:59:32 +08:00
|
|
|
// Precondition: write() has been called and last returned 0
|
2017-05-26 04:48:44 +08:00
|
|
|
// returns when write() can write at least one byte (or may throw an error if the connection dies)
|
|
|
|
virtual Future<Void> onWritable() = 0;
|
|
|
|
|
2020-04-25 08:59:32 +08:00
|
|
|
// Precondition: read() has been called and last returned 0
|
2017-05-26 04:48:44 +08:00
|
|
|
// returns when read() can read at least one byte (or may throw an error if the connection dies)
|
|
|
|
virtual Future<Void> onReadable() = 0;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Reads as many bytes as possible from the read buffer into [begin,end) and returns the number of bytes read (might
|
|
|
|
// be 0) (or may throw an error if the connection dies)
|
|
|
|
virtual int read(uint8_t* begin, uint8_t* end) = 0;
|
|
|
|
|
|
|
|
// Writes as many bytes as possible from the given SendBuffer chain into the write buffer and returns the number of
|
|
|
|
// bytes written (might be 0) (or may throw an error if the connection dies) The SendBuffer chain cannot be empty,
|
|
|
|
// and the limit must be positive. Important non-obvious behavior: The caller is committing to write the contents
|
|
|
|
// of the buffer chain up to the limit. If all of those bytes could not be sent in this call to write() then
|
|
|
|
// further calls must be made to write the remainder. An IConnection implementation can make decisions based on the
|
|
|
|
// entire byte set that the caller was attempting to write even if it is unable to write all of it immediately. Due
|
|
|
|
// to limitations of TLSConnection, callers must also avoid reallocations that reduce the amount of written data in
|
|
|
|
// the first buffer in the chain.
|
|
|
|
virtual int write(SendBuffer const* buffer, int limit = std::numeric_limits<int>::max()) = 0;
|
|
|
|
|
|
|
|
// Returns the network address and port of the other end of the connection. In the case of an incoming connection,
|
|
|
|
// this may not be an address we can connect to!
|
2020-06-21 10:03:33 +08:00
|
|
|
virtual NetworkAddress getPeerAddress() const = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
virtual UID getDebugID() const = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class IListener {
|
|
|
|
public:
|
|
|
|
virtual void addref() = 0;
|
|
|
|
virtual void delref() = 0;
|
|
|
|
|
|
|
|
// Returns one incoming connection when it is available. Do not cancel unless you are done with the listener!
|
|
|
|
virtual Future<Reference<IConnection>> accept() = 0;
|
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
virtual NetworkAddress getListenAddress() const = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
typedef void* flowGlobalType;
|
2017-05-26 04:48:44 +08:00
|
|
|
typedef NetworkAddress (*NetworkAddressFuncPtr)();
|
2019-02-01 10:20:14 +08:00
|
|
|
typedef NetworkAddressList (*NetworkAddressesFuncPtr)();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-03-05 12:14:47 +08:00
|
|
|
class TLSConfig;
|
2017-05-26 04:48:44 +08:00
|
|
|
class INetwork;
|
|
|
|
extern INetwork* g_network;
|
2020-03-05 12:14:47 +08:00
|
|
|
extern INetwork* newNet2(const TLSConfig& tlsConfig, bool useThreadPool = false, bool useMetrics = false);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
class INetwork {
|
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
// This interface abstracts the physical or simulated network, event loop and hardware that FoundationDB is running
|
|
|
|
// on. Note that there are tools for disk access, scheduling, etc as well as networking, and that almost all access
|
2017-05-26 04:48:44 +08:00
|
|
|
// to the network should be through FlowTransport, not directly through these low level interfaces!
|
|
|
|
|
|
|
|
enum enumGlobal {
|
2019-05-30 04:43:21 +08:00
|
|
|
enFailureMonitor = 0,
|
|
|
|
enFlowTransport = 1,
|
|
|
|
enTDMetrics = 2,
|
|
|
|
enNetworkConnections = 3,
|
|
|
|
enNetworkAddressFunc = 4,
|
|
|
|
enFileSystem = 5,
|
|
|
|
enASIOService = 6,
|
|
|
|
enEventFD = 7,
|
|
|
|
enRunCycleFunc = 8,
|
|
|
|
enASIOTimedOut = 9,
|
|
|
|
enBlobCredentialFiles = 10,
|
|
|
|
enNetworkAddressesFunc = 11,
|
2021-01-16 21:04:30 +08:00
|
|
|
enClientFailureMonitor = 12,
|
2021-02-13 10:55:01 +08:00
|
|
|
enSQLiteInjectedError = 13,
|
2021-07-01 08:05:04 +08:00
|
|
|
enGlobalConfig = 14,
|
2021-07-29 07:03:37 +08:00
|
|
|
enChaosMetrics = 15,
|
|
|
|
enDiskFailureInjector = 16,
|
2021-11-18 07:25:36 +08:00
|
|
|
enBitFlipper = 17,
|
|
|
|
COUNT // Add new fields before this enumerator
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual void longTaskCheck(const char* name) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
virtual double now() const = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
// Provides a clock that advances at a similar rate on all connected endpoints
|
|
|
|
// FIXME: Return a fixed point Time class
|
|
|
|
|
2020-01-27 11:47:12 +08:00
|
|
|
virtual double timer() = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
// A wrapper for directly getting the system time. The time returned by now() only updates in the run loop,
|
2020-02-21 06:41:49 +08:00
|
|
|
// so it cannot be used to measure times of functions that do not have wait statements.
|
2020-01-27 11:47:12 +08:00
|
|
|
|
2020-11-06 01:03:26 +08:00
|
|
|
virtual double timer_monotonic() = 0;
|
|
|
|
// Similar to timer, but monotonic
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual Future<class Void> delay(double seconds, TaskPriority taskID) = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
// The given future will be set after seconds have elapsed
|
|
|
|
|
2021-07-28 03:01:32 +08:00
|
|
|
virtual Future<class Void> orderedDelay(double seconds, TaskPriority taskID) = 0;
|
|
|
|
// The given future will be set after seconds have elapsed, delays with the same time and TaskPriority will be
|
|
|
|
// executed in the order they were issues
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual Future<class Void> yield(TaskPriority taskID) = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
// The given future will be set immediately or after higher-priority tasks have executed
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual bool check_yield(TaskPriority taskID) = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
// Returns true if a call to yield would result in a delay
|
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
virtual TaskPriority getCurrentTask() const = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
// Gets the taskID/priority of the current task
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual void setCurrentTask(TaskPriority taskID) = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
// Sets the taskID/priority of the current task, without yielding
|
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
virtual flowGlobalType global(int id) const = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
virtual void setGlobal(size_t id, flowGlobalType v) = 0;
|
|
|
|
|
|
|
|
virtual void stop() = 0;
|
|
|
|
// Terminate the program
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual void addStopCallback(std::function<void()> fn) = 0;
|
2020-04-20 17:53:07 +08:00
|
|
|
// Calls `fn` when stop() is called.
|
|
|
|
// addStopCallback can be called more than once, and each added `fn` will be run once.
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
virtual bool isSimulated() const = 0;
|
|
|
|
// Returns true if this network is a local simulation
|
|
|
|
|
2019-06-11 08:12:04 +08:00
|
|
|
virtual bool isOnMainThread() const = 0;
|
|
|
|
// Returns true if the current thread is the main thread
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual void onMainThread(Promise<Void>&& signal, TaskPriority taskID) = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
// Executes signal.send(Void()) on a/the thread belonging to this network
|
|
|
|
|
2021-07-09 06:51:01 +08:00
|
|
|
virtual THREAD_HANDLE startThread(THREAD_FUNC_RETURN (*func)(void*),
|
|
|
|
void* arg,
|
|
|
|
int stackSize = 0,
|
|
|
|
const char* name = nullptr) = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
// Starts a thread and returns a handle to it
|
|
|
|
|
|
|
|
virtual void run() = 0;
|
|
|
|
// Devotes this thread to running the network (generally until stop())
|
|
|
|
|
|
|
|
virtual void initMetrics() {}
|
|
|
|
// Metrics must be initialized after FlowTransport::createInstance has been called
|
|
|
|
|
2020-02-27 10:53:06 +08:00
|
|
|
// TLS must be initialized before using the network
|
2021-03-11 02:06:03 +08:00
|
|
|
enum ETLSInitState { NONE = 0, CONFIG = 1, CONNECT = 2, LISTEN = 3 };
|
2020-06-29 08:29:13 +08:00
|
|
|
virtual void initTLS(ETLSInitState targetState = CONFIG) {}
|
2020-02-27 10:53:06 +08:00
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
virtual const TLSConfig& getTLSConfig() const = 0;
|
2020-03-13 15:11:53 +08:00
|
|
|
// Return the TLS Configuration
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual void getDiskBytes(std::string const& directory, int64_t& free, int64_t& total) = 0;
|
|
|
|
// Gets the number of free and total bytes available on the disk which contains directory
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
virtual bool isAddressOnThisHost(NetworkAddress const& addr) const = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
// Returns true if it is reasonably certain that a connection to the given address would be a fast loopback
|
|
|
|
// connection
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// If the network has not been run and this function has not been previously called, returns true. Otherwise,
|
|
|
|
// returns false.
|
2020-05-23 04:31:06 +08:00
|
|
|
virtual bool checkRunnable() = 0;
|
|
|
|
|
2021-07-24 08:18:13 +08:00
|
|
|
#ifdef ENABLE_SAMPLING
|
2021-06-04 06:10:04 +08:00
|
|
|
// Returns the shared memory data structure used to store actor lineages.
|
|
|
|
virtual ActorLineageSet& getActorLineageSet() = 0;
|
2021-07-24 08:18:13 +08:00
|
|
|
#endif
|
2021-06-04 06:10:04 +08:00
|
|
|
|
2021-10-20 08:37:03 +08:00
|
|
|
virtual ProtocolVersion protocolVersion() const = 0;
|
2020-09-29 01:58:49 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
// Shorthand for transport().getLocalAddress()
|
2021-03-11 02:06:03 +08:00
|
|
|
static NetworkAddress getLocalAddress() {
|
|
|
|
flowGlobalType netAddressFuncPtr =
|
|
|
|
reinterpret_cast<flowGlobalType>(g_network->global(INetwork::enNetworkAddressFunc));
|
2017-05-26 04:48:44 +08:00
|
|
|
return (netAddressFuncPtr) ? reinterpret_cast<NetworkAddressFuncPtr>(netAddressFuncPtr)() : NetworkAddress();
|
|
|
|
}
|
|
|
|
|
2019-02-01 10:20:14 +08:00
|
|
|
// Shorthand for transport().getLocalAddresses()
|
2021-03-11 02:06:03 +08:00
|
|
|
static NetworkAddressList getLocalAddresses() {
|
|
|
|
flowGlobalType netAddressesFuncPtr =
|
|
|
|
reinterpret_cast<flowGlobalType>(g_network->global(INetwork::enNetworkAddressesFunc));
|
|
|
|
return (netAddressesFuncPtr) ? reinterpret_cast<NetworkAddressesFuncPtr>(netAddressesFuncPtr)()
|
|
|
|
: NetworkAddressList();
|
2019-02-01 10:20:14 +08:00
|
|
|
}
|
|
|
|
|
2020-01-13 08:44:30 +08:00
|
|
|
NetworkInfo networkInfo;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
protected:
|
|
|
|
INetwork() {}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
~INetwork() {} // Please don't try to delete through this interface!
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2020-07-24 01:20:42 +08:00
|
|
|
class IUDPSocket {
|
|
|
|
public:
|
2020-08-07 04:06:50 +08:00
|
|
|
// see https://en.wikipedia.org/wiki/User_Datagram_Protocol - the max size of a UDP packet
|
|
|
|
// This is enforced in simulation
|
|
|
|
constexpr static size_t MAX_PACKET_SIZE = 65535;
|
2020-07-24 01:20:42 +08:00
|
|
|
virtual ~IUDPSocket();
|
|
|
|
virtual void addref() = 0;
|
|
|
|
virtual void delref() = 0;
|
|
|
|
|
2020-08-07 04:06:50 +08:00
|
|
|
virtual void close() = 0;
|
|
|
|
virtual Future<int> send(uint8_t const* begin, uint8_t const* end) = 0;
|
|
|
|
virtual Future<int> sendTo(uint8_t const* begin, uint8_t const* end, NetworkAddress const& peer) = 0;
|
|
|
|
virtual Future<int> receive(uint8_t* begin, uint8_t* end) = 0;
|
|
|
|
virtual Future<int> receiveFrom(uint8_t* begin, uint8_t* end, NetworkAddress* sender) = 0;
|
|
|
|
virtual void bind(NetworkAddress const& addr) = 0;
|
2020-07-24 01:20:42 +08:00
|
|
|
|
|
|
|
virtual UID getDebugID() const = 0;
|
2020-08-07 04:06:50 +08:00
|
|
|
virtual NetworkAddress localAddress() const = 0;
|
2020-11-17 03:40:06 +08:00
|
|
|
virtual boost::asio::ip::udp::socket::native_handle_type native_handle() = 0;
|
2020-07-24 01:20:42 +08:00
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
class INetworkConnections {
|
|
|
|
public:
|
|
|
|
// Methods for making and accepting network connections. Logically this is part of the INetwork abstraction
|
|
|
|
// that abstracts all interaction with the physical world; it is separated out to make it easy for e.g. transport
|
|
|
|
// security to override only these operations without having to delegate everything in INetwork.
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Make an outgoing connection to the given address. May return an error or block indefinitely in case of
|
|
|
|
// connection problems!
|
|
|
|
virtual Future<Reference<IConnection>> connect(NetworkAddress toAddr, const std::string& host = "") = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual Future<Reference<IConnection>> connectExternal(NetworkAddress toAddr, const std::string& host = "") = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-08-07 04:06:50 +08:00
|
|
|
// Make an outgoing udp connection and connect to the passed address.
|
|
|
|
virtual Future<Reference<IUDPSocket>> createUDPSocket(NetworkAddress toAddr) = 0;
|
|
|
|
// Make an outgoing udp connection without establishing a connection
|
|
|
|
virtual Future<Reference<IUDPSocket>> createUDPSocket(bool isV6 = false) = 0;
|
|
|
|
|
2021-11-16 06:34:52 +08:00
|
|
|
virtual void addMockTCPEndpoint(const std::string& host,
|
|
|
|
const std::string& service,
|
|
|
|
const std::vector<NetworkAddress>& addresses) = 0;
|
2022-01-15 04:02:21 +08:00
|
|
|
virtual void removeMockTCPEndpoint(const std::string& host, const std::string& service) = 0;
|
2022-01-15 11:02:35 +08:00
|
|
|
virtual void parseMockDNSFromString(const std::string& s) = 0;
|
|
|
|
virtual std::string convertMockDNSToString() = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
// Resolve host name and service name (such as "http" or can be a plain number like "80") to a list of 1 or more
|
|
|
|
// NetworkAddresses
|
|
|
|
virtual Future<std::vector<NetworkAddress>> resolveTCPEndpoint(const std::string& host,
|
|
|
|
const std::string& service) = 0;
|
2022-01-28 06:25:50 +08:00
|
|
|
// Resolve host name and service name. This one should only be used when resolving asynchronously is impossible. For
|
|
|
|
// all other cases, resolveTCPEndpoint() should be preferred.
|
|
|
|
virtual std::vector<NetworkAddress> resolveTCPEndpointBlocking(const std::string& host,
|
|
|
|
const std::string& service) = 0;
|
2017-10-16 12:51:11 +08:00
|
|
|
|
|
|
|
// Convenience function to resolve host/service and connect to one of its NetworkAddresses randomly
|
2022-01-21 04:19:58 +08:00
|
|
|
// isTLS has to be a parameter here because it is passed to connect() as part of the toAddr object.
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual Future<Reference<IConnection>> connect(const std::string& host,
|
|
|
|
const std::string& service,
|
2022-01-21 04:19:58 +08:00
|
|
|
bool isTLS = false);
|
2017-10-16 12:51:11 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
// Listen for connections on the given local address
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual Reference<IListener> listen(NetworkAddress localAddr) = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static INetworkConnections* net() {
|
|
|
|
return static_cast<INetworkConnections*>((void*)g_network->global(INetwork::enNetworkConnections));
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
// Returns the interface that should be used to make and accept socket connections
|
|
|
|
};
|
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
// Chaos Metrics - We periodically log chaosMetrics to make sure that chaos events are happening
|
|
|
|
// Only includes DiskDelays which encapsulates all type delays and BitFlips for now
|
|
|
|
// Expand as per need
|
|
|
|
struct ChaosMetrics {
|
2021-07-13 08:51:01 +08:00
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
ChaosMetrics() { clear(); }
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
memset(this, 0, sizeof(ChaosMetrics));
|
2021-07-30 16:32:43 +08:00
|
|
|
startTime = g_network ? g_network->now() : 0;
|
2021-07-13 08:51:01 +08:00
|
|
|
}
|
2021-07-29 07:03:37 +08:00
|
|
|
|
|
|
|
unsigned int diskDelays;
|
|
|
|
unsigned int bitFlips;
|
|
|
|
double startTime;
|
|
|
|
|
|
|
|
void getFields(TraceEvent* e) {
|
|
|
|
std::pair<const char*, unsigned int> metrics[] = { { "DiskDelays", diskDelays }, { "BitFlips", bitFlips } };
|
|
|
|
if (e != nullptr) {
|
|
|
|
for (auto& m : metrics) {
|
|
|
|
char c = m.first[0];
|
|
|
|
if (c != 0) {
|
|
|
|
e->detail(m.first, m.second);
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 08:51:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
// This class supports injecting two type of disk failures
|
|
|
|
// 1. Stalls: Every interval seconds, the disk will stall and no IO will complete for x seconds, where x is a randomly
|
|
|
|
// chosen interval
|
|
|
|
// 2. Slowdown: Random slowdown is injected to each disk operation for specified period of time
|
|
|
|
struct DiskFailureInjector {
|
2021-07-01 08:05:04 +08:00
|
|
|
static DiskFailureInjector* injector() {
|
2021-07-20 02:13:57 +08:00
|
|
|
auto res = g_network->global(INetwork::enDiskFailureInjector);
|
2021-07-01 08:05:04 +08:00
|
|
|
if (!res) {
|
|
|
|
res = new DiskFailureInjector();
|
2021-07-20 02:13:57 +08:00
|
|
|
g_network->setGlobal(INetwork::enDiskFailureInjector, res);
|
2021-07-01 08:05:04 +08:00
|
|
|
}
|
|
|
|
return static_cast<DiskFailureInjector*>(res);
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
void setDiskFailure(double interval, double stallFor, double throttleFor) {
|
|
|
|
stallInterval = interval;
|
|
|
|
stallPeriod = stallFor;
|
2021-07-30 16:32:43 +08:00
|
|
|
stallUntil = std::max(stallUntil, g_network->now() + stallFor);
|
2021-07-29 07:03:37 +08:00
|
|
|
// random stall duration in ms (chosen once)
|
2021-11-18 01:45:50 +08:00
|
|
|
// TODO: make this delay configurable
|
2021-07-29 07:03:37 +08:00
|
|
|
stallDuration = 0.001 * deterministicRandom()->randomInt(1, 5);
|
|
|
|
throttlePeriod = throttleFor;
|
2021-07-30 16:32:43 +08:00
|
|
|
throttleUntil = std::max(throttleUntil, g_network->now() + throttleFor);
|
2021-07-29 07:03:37 +08:00
|
|
|
TraceEvent("SetDiskFailure")
|
2021-09-11 06:41:22 +08:00
|
|
|
.detail("Now", g_network->now())
|
2021-07-29 07:03:37 +08:00
|
|
|
.detail("StallInterval", interval)
|
|
|
|
.detail("StallPeriod", stallFor)
|
|
|
|
.detail("StallUntil", stallUntil)
|
|
|
|
.detail("ThrottlePeriod", throttleFor)
|
|
|
|
.detail("ThrottleUntil", throttleUntil);
|
2021-07-01 08:05:04 +08:00
|
|
|
}
|
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
double getStallDelay() {
|
|
|
|
// If we are in a stall period and a stallInterval was specified, determine the
|
|
|
|
// delay to be inserted
|
2021-07-30 16:32:43 +08:00
|
|
|
if (((stallUntil - g_network->now()) > 0.0) && stallInterval) {
|
|
|
|
auto timeElapsed = fmod(g_network->now(), stallInterval);
|
2021-07-29 07:03:37 +08:00
|
|
|
return std::max(0.0, stallDuration - timeElapsed);
|
2021-07-01 08:05:04 +08:00
|
|
|
}
|
2021-07-29 07:03:37 +08:00
|
|
|
return 0.0;
|
2021-07-01 08:05:04 +08:00
|
|
|
}
|
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
double getThrottleDelay() {
|
|
|
|
// If we are in the throttle period, insert a random delay (in ms)
|
2021-11-18 01:45:50 +08:00
|
|
|
// TODO: make this delay configurable
|
2021-07-30 16:32:43 +08:00
|
|
|
if ((throttleUntil - g_network->now()) > 0.0)
|
2021-07-29 07:03:37 +08:00
|
|
|
return (0.001 * deterministicRandom()->randomInt(1, 3));
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double getDiskDelay() { return getStallDelay() + getThrottleDelay(); }
|
|
|
|
|
2021-07-01 08:05:04 +08:00
|
|
|
private: // members
|
2021-07-29 07:03:37 +08:00
|
|
|
double stallInterval = 0.0; // how often should the disk be stalled (0 meaning once, 10 meaning every 10 secs)
|
|
|
|
double stallPeriod; // Period of time disk stalls will be injected for
|
|
|
|
double stallUntil; // End of disk stall period
|
|
|
|
double stallDuration; // Duration of each stall
|
|
|
|
double throttlePeriod; // Period of time the disk will be slowed down for
|
|
|
|
double throttleUntil; // End of disk slowdown period
|
2021-07-01 08:05:04 +08:00
|
|
|
|
|
|
|
private: // construction
|
|
|
|
DiskFailureInjector() = default;
|
|
|
|
DiskFailureInjector(DiskFailureInjector const&) = delete;
|
|
|
|
};
|
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
struct BitFlipper {
|
2021-07-19 08:35:05 +08:00
|
|
|
static BitFlipper* flipper() {
|
|
|
|
auto res = g_network->global(INetwork::enBitFlipper);
|
|
|
|
if (!res) {
|
|
|
|
res = new BitFlipper();
|
|
|
|
g_network->setGlobal(INetwork::enBitFlipper, res);
|
|
|
|
}
|
|
|
|
return static_cast<BitFlipper*>(res);
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
double getBitFlipPercentage() { return bitFlipPercentage; }
|
2021-07-19 08:35:05 +08:00
|
|
|
|
2021-07-29 07:03:37 +08:00
|
|
|
void setBitFlipPercentage(double percentage) { bitFlipPercentage = percentage; }
|
2021-07-19 08:35:05 +08:00
|
|
|
|
|
|
|
private: // members
|
2021-07-29 07:03:37 +08:00
|
|
|
double bitFlipPercentage = 0.0;
|
2021-07-19 08:35:05 +08:00
|
|
|
|
|
|
|
private: // construction
|
|
|
|
BitFlipper() = default;
|
|
|
|
BitFlipper(BitFlipper const&) = delete;
|
|
|
|
};
|
2017-05-26 04:48:44 +08:00
|
|
|
#endif
|