Line data Source code
1 : #pragma once 2 : 3 : #include <cstdint> 4 : #include <string> 5 : 6 : #include "envoy/common/pure.h" 7 : #include "envoy/event/dispatcher.h" 8 : #include "envoy/stats/allocator.h" 9 : #include "envoy/stats/store.h" 10 : #include "envoy/thread/thread.h" 11 : 12 : namespace Envoy { 13 : namespace Server { 14 : 15 : class Instance; 16 : 17 : /** 18 : * Abstracts functionality required to "hot" (live) restart the server including code and 19 : * configuration. Right now this interface assumes a UNIX like socket interface for fd passing 20 : * but it could be relatively easily swapped with something else if necessary. 21 : */ 22 : class HotRestart { 23 : public: 24 : struct ServerStatsFromParent { 25 : uint64_t parent_memory_allocated_ = 0; 26 : uint64_t parent_connections_ = 0; 27 : }; 28 : 29 : struct AdminShutdownResponse { 30 : time_t original_start_time_; 31 : bool enable_reuse_port_default_; 32 : }; 33 : 34 455 : virtual ~HotRestart() = default; 35 : 36 : /** 37 : * Shutdown listeners in the parent process if applicable. Listeners will begin draining to 38 : * clear out old connections. 39 : */ 40 : virtual void drainParentListeners() PURE; 41 : 42 : /** 43 : * Retrieve a listening socket on the specified address from the parent process. The socket will 44 : * be duplicated across process boundaries. 45 : * @param address supplies the address of the socket to duplicate, e.g. tcp://127.0.0.1:5000. 46 : * @param worker_index supplies the socket/worker index to fetch. When using reuse_port sockets 47 : * each socket is fetched individually to ensure no connection loss. 48 : * @return int the fd or -1 if there is no bound listen port in the parent. 49 : */ 50 : virtual int duplicateParentListenSocket(const std::string& address, uint32_t worker_index) PURE; 51 : 52 : /** 53 : * Registers a UdpListenerConfig as a possible receiver of udp packets forwarded from the 54 : * parent process to the child process. This is used to forward QUIC packets that are not for 55 : * connections belonging to the parent process during draining (in the absence of BPF delivery to 56 : * the correct process), via its listenerWorkerRouter. 57 : * The HotRestart instance is responsible for recognizing "any" addresses (e.g. "0.0.0.0"). 58 : * @param address supplies the address and port of the listening socket. 59 : * @param listener_config is the UdpListenerConfig to receive packets forwarded for the given 60 : * address. 61 : */ 62 : virtual void 63 : registerUdpForwardingListener(Network::Address::InstanceConstSharedPtr address, 64 : std::shared_ptr<Network::UdpListenerConfig> listener_config) PURE; 65 : /** 66 : * Initialize the parent logic of our restarter. Meant to be called after initialization of a 67 : * new child has begun. The hot restart implementation needs to be created early to deal with 68 : * shared memory, logging, etc. so late initialization of needed interfaces is done here. 69 : */ 70 : virtual void initialize(Event::Dispatcher& dispatcher, Server::Instance& server) PURE; 71 : 72 : /** 73 : * Shutdown admin processing in the parent process if applicable. This allows admin processing 74 : * to start up in the new process. 75 : * @return response if the parent is alive. 76 : */ 77 : virtual absl::optional<AdminShutdownResponse> sendParentAdminShutdownRequest() PURE; 78 : 79 : /** 80 : * Tell our parent process to gracefully terminate itself. 81 : */ 82 : virtual void sendParentTerminateRequest() PURE; 83 : 84 : /** 85 : * Retrieve stats from our parent process and merges them into stats_store, taking into account 86 : * the stats values we've already seen transferred. 87 : * Skips all of the above and returns 0s if there is not currently a parent. 88 : * @param stats_store the store whose stats will be updated. 89 : * @param stats_proto the stats values we are updating with. 90 : * @return special values relating to the "server" stats scope, whose 91 : * merging has to be handled by Server::InstanceImpl. 92 : */ 93 : virtual ServerStatsFromParent mergeParentStatsIfAny(Stats::StoreRoot& stats_store) PURE; 94 : 95 : /** 96 : * Shutdown the half of our hot restarter that acts as a parent. 97 : */ 98 : virtual void shutdown() PURE; 99 : 100 : /** 101 : * Return the base id used to generate a domain socket name. 102 : */ 103 : virtual uint32_t baseId() PURE; 104 : 105 : /** 106 : * Return the hot restart compatibility version so that operations code can decide whether to 107 : * perform a full or hot restart. 108 : */ 109 : virtual std::string version() PURE; 110 : 111 : /** 112 : * @return Thread::BasicLockable& a lock for logging. 113 : */ 114 : virtual Thread::BasicLockable& logLock() PURE; 115 : 116 : /** 117 : * @return Thread::BasicLockable& a lock for access logs. 118 : */ 119 : virtual Thread::BasicLockable& accessLogLock() PURE; 120 : }; 121 : 122 : /** 123 : * HotRestartDomainSocketInUseException is thrown during HotRestart construction only when the 124 : * underlying domain socket is in use. 125 : */ 126 : class HotRestartDomainSocketInUseException : public EnvoyException { 127 : public: 128 0 : HotRestartDomainSocketInUseException(const std::string& what) : EnvoyException(what) {} 129 : }; 130 : 131 : } // namespace Server 132 : } // namespace Envoy