|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <future> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +#include "envoy/common/pure.h" |
| 7 | +#include "envoy/event/timer.h" |
| 8 | +#include "envoy/network/listen_socket.h" |
| 9 | +#include "envoy/stats/scope.h" |
| 10 | +#include "envoy/stats/stats_macros.h" |
| 11 | + |
| 12 | +#include "absl/container/flat_hash_map.h" |
| 13 | + |
| 14 | +namespace Envoy { |
| 15 | +namespace Network { |
| 16 | + |
| 17 | +/** |
| 18 | + * All ReverseConnectionHandler stats. @see stats_macros.h |
| 19 | + * This encompasses the stats for all accepted reverse connections by the responder envoy. |
| 20 | + * The initiated reverse connections by the initiator envoy are logged by the RCManager. |
| 21 | + */ |
| 22 | +#define ALL_RCHANDLER_STATS(GAUGE) \ |
| 23 | + GAUGE(reverse_conn_cx_idle, NeverImport) \ |
| 24 | + GAUGE(reverse_conn_cx_used, NeverImport) \ |
| 25 | + GAUGE(reverse_conn_cx_total, NeverImport) |
| 26 | + |
| 27 | +/** |
| 28 | + * Struct definition for all ReverseConnectionHandler stats. @see stats_macros.h |
| 29 | + */ |
| 30 | +struct RCHandlerStats { |
| 31 | + ALL_RCHANDLER_STATS(GENERATE_GAUGE_STRUCT) |
| 32 | +}; |
| 33 | + |
| 34 | +using RCHandlerStatsPtr = std::unique_ptr<RCHandlerStats>; |
| 35 | +using RCSocketPair = std::pair<Network::ConnectionSocketPtr, bool>; |
| 36 | + |
| 37 | +/** |
| 38 | + * class to store reverse connection sockets. |
| 39 | + */ |
| 40 | +class ReverseConnectionHandler { |
| 41 | +public: |
| 42 | + virtual ~ReverseConnectionHandler() = default; |
| 43 | + |
| 44 | + /** Add the accepted connection and remote cluster mapping to RCHandler maps. |
| 45 | + * @param node_id node_id of initiating node. |
| 46 | + * @param cluster_id cluster_id of receiving(acceptor) cluster. |
| 47 | + * @param socket the socket to be added. |
| 48 | + * @param expects_proxy_protocol whether the proxy protocol header is expected. This is used |
| 49 | + * in legacy versions. |
| 50 | + * @param ping_interval the interval at which ping keepalives are sent on accepted reverse conns. |
| 51 | + * @param rebalanced is true if we are adding to the socket after `pickMinHandler` is used |
| 52 | + * to pick the most appropriate thread. |
| 53 | + */ |
| 54 | + virtual void |
| 55 | + addConnectionSocket(const std::string& node_id, const std::string& cluster_id, |
| 56 | + Network::ConnectionSocketPtr socket, bool expects_proxy_protocol, |
| 57 | + const std::chrono::seconds& ping_interval = std::chrono::seconds::zero(), |
| 58 | + bool rebalanced = false) PURE; |
| 59 | + |
| 60 | + /** Add the accepted connection and remote cluster mapping to RCHandler maps |
| 61 | + * through the thread local dispatcher. |
| 62 | + * @param node_id node_id of initiating node. |
| 63 | + * @param cluster_id cluster_id of receiving(acceptor) cluster. |
| 64 | + * @param socket the socket to be added. |
| 65 | + * @param expects_proxy_protocol whether the proxy protocol header is expected. This is used |
| 66 | + * in legacy versions. |
| 67 | + * @param ping_interval the interval at which ping keepalives are sent on accepted reverse conns. |
| 68 | + */ |
| 69 | + virtual void post(const std::string& node_id, const std::string& cluster_id, |
| 70 | + Network::ConnectionSocketPtr socket, bool expects_proxy_protocol, |
| 71 | + const std::chrono::seconds& ping_interval = std::chrono::seconds::zero()) PURE; |
| 72 | + |
| 73 | + /** Called by the responder envoy when a request is received, that could be sent through a reverse |
| 74 | + * connection. This returns an accepted connection socket, if present. |
| 75 | + * @param key the remote cluster ID/ node ID. |
| 76 | + * @param rebalanced is true if we are calling the function after `pickTargetHandler` is used |
| 77 | + * to pick the most appropriate thread. |
| 78 | + */ |
| 79 | + virtual std::pair<Network::ConnectionSocketPtr, bool> getConnectionSocket(const std::string& key, |
| 80 | + bool rebalanced) PURE; |
| 81 | + |
| 82 | + /** Called by the responder envoy when the local worker does not have any accepted reverse |
| 83 | + * connections for the key, to rebalance the request to a different worker and return the |
| 84 | + * connection socket. |
| 85 | + * @param key the remote cluster ID/ node ID. |
| 86 | + * @param rebalanced is true if we are calling the function after `pickTargetHandler` is used |
| 87 | + * to pick the most appropriate thread. |
| 88 | + * @param socket_promise the promise to be set with the connection socket. |
| 89 | + */ |
| 90 | + virtual void rebalanceGetConnectionSocket( |
| 91 | + const std::string& key, bool rebalanced, |
| 92 | + std::shared_ptr<std::promise<Network::RCSocketPair>> socket_promise) PURE; |
| 93 | + |
| 94 | + /** |
| 95 | + * @return the number of reverse connections across all workers |
| 96 | + * for the given node id. |
| 97 | + */ |
| 98 | + virtual size_t getNumberOfSocketsByNode(const std::string& node_id) PURE; |
| 99 | + |
| 100 | + /** |
| 101 | + * @return the number of reverse connections across all workers for |
| 102 | + * the given cluster id. |
| 103 | + */ |
| 104 | + virtual size_t getNumberOfSocketsByCluster(const std::string& cluster_id) PURE; |
| 105 | + |
| 106 | + using SocketCountMap = absl::flat_hash_map<std::string, size_t>; |
| 107 | + /** |
| 108 | + * |
| 109 | + * @return the cluster -> reverse conn count mapping. |
| 110 | + */ |
| 111 | + virtual SocketCountMap getSocketCountMap() PURE; |
| 112 | + /** |
| 113 | + * Mark the connection socket dead and remove it from internal maps. |
| 114 | + * @param fd the FD for the socket to be marked dead. |
| 115 | + * @param used is true, when the connection the fd belongs to has been used by a cluster. |
| 116 | + */ |
| 117 | + virtual void markSocketDead(int fd, bool used) PURE; |
| 118 | + |
| 119 | + /** |
| 120 | + * Sets the stats scope for logging stats for accepted reverse connections |
| 121 | + * with the local envoy as responder. |
| 122 | + * @param scope the base scope to be used. |
| 123 | + * @return the parent scope for RCHandler stats. |
| 124 | + */ |
| 125 | + virtual void initializeStats(Stats::Scope& scope) PURE; |
| 126 | + |
| 127 | + virtual absl::flat_hash_map<std::string, size_t> getConnectionStats() PURE; |
| 128 | +}; |
| 129 | + |
| 130 | +} // namespace Network |
| 131 | +} // namespace Envoy |
0 commit comments