1
#pragma once
2

            
3
#include "source/common/common/hash.h"
4
#include "source/server/hot_restarting_base.h"
5

            
6
namespace Envoy {
7
namespace Server {
8

            
9
class HotRestartMessageSender {
10
public:
11
  virtual void sendHotRestartMessage(envoy::HotRestartMessage&& msg) PURE;
12
36
  virtual ~HotRestartMessageSender() = default;
13
};
14

            
15
/**
16
 * The parent half of hot restarting. Listens for requests and commands from the child.
17
 * This outer class only handles evented socket I/O. The actual hot restart logic lives in
18
 * HotRestartingParent::Internal.
19
 */
20
class HotRestartingParent : public HotRestartingBase, public HotRestartMessageSender {
21
public:
22
  HotRestartingParent(int base_id, int restart_epoch, const std::string& socket_path,
23
                      mode_t socket_mode);
24
  void initialize(Event::Dispatcher& dispatcher, Server::Instance& server);
25
  void shutdown();
26
  void sendHotRestartMessage(envoy::HotRestartMessage&& msg) override;
27

            
28
  // The hot restarting parent's hot restart logic. Each function is meant to be called to fulfill a
29
  // request from the child for that action.
30
  class Internal : public Network::NonDispatchedUdpPacketHandler {
31
  public:
32
    explicit Internal(Server::Instance* server, HotRestartMessageSender& udp_sender);
33
    // Return value is the response to return to the child.
34
    envoy::HotRestartMessage shutdownAdmin();
35
    // Return value is the response to return to the child.
36
    envoy::HotRestartMessage
37
    getListenSocketsForChild(const envoy::HotRestartMessage::Request& request);
38
    // 'stats' is a field in the reply protobuf to be sent to the child, which we should populate.
39
    void exportStatsToChild(envoy::HotRestartMessage::Reply::Stats* stats);
40
    void recordDynamics(envoy::HotRestartMessage::Reply::Stats* stats, const std::string& name,
41
                        Stats::StatName stat_name);
42
    void drainListeners();
43

            
44
    // Network::NonDispatchedUdpPacketHandler
45
    void handle(uint32_t worker_index, const Network::UdpRecvData& packet) override;
46

            
47
  private:
48
    Server::Instance* const server_{};
49
    HotRestartMessageSender& udp_sender_;
50
  };
51

            
52
private:
53
  void onSocketEvent();
54

            
55
  const int restart_epoch_;
56
  sockaddr_un child_address_;
57
  sockaddr_un child_address_udp_forwarding_;
58
  Event::FileEventPtr socket_event_;
59
  OptRef<Event::Dispatcher> dispatcher_;
60
  std::unique_ptr<Internal> internal_;
61
};
62

            
63
} // namespace Server
64
} // namespace Envoy