1
#pragma once
2

            
3
#include "envoy/event/timer.h"
4
#include "envoy/runtime/runtime.h"
5
#include "envoy/server/platform.h"
6

            
7
#include "source/common/common/thread.h"
8
#include "source/common/event/real_time_system.h"
9
#include "source/common/grpc/google_grpc_context.h"
10
#include "source/common/stats/symbol_table.h"
11
#include "source/common/stats/thread_local_store.h"
12
#include "source/common/thread_local/thread_local_impl.h"
13
#include "source/exe/process_wide.h"
14
#include "source/server/listener_hooks.h"
15
#include "source/server/server.h"
16

            
17
#ifdef ENVOY_HANDLE_SIGNALS
18
#include "source/common/signal/signal_action.h"
19
#include "source/exe/terminate_handler.h"
20
#endif
21

            
22
namespace Envoy {
23

            
24
class ProdComponentFactory : public Server::ComponentFactory {
25
public:
26
  // Server::DrainManagerFactory
27
  Server::DrainManagerPtr createDrainManager(Server::Instance& server) override;
28
  Runtime::LoaderPtr createRuntime(Server::Instance& server,
29
                                   Server::Configuration::Initial& config) override;
30
};
31

            
32
// This is the common main between Envoy and Envoy mobile.
33
// It is stripped down to functionality required by Envoy Mobile: anything
34
// server-specific should live in MainCommonBase or MainCommon which remain
35
// separate for legacy reasons.
36
class StrippedMainBase {
37
public:
38
  using CreateInstanceFunction = std::function<std::unique_ptr<Server::Instance>(
39
      Init::Manager& init_manager, const Server::Options& options, Event::TimeSystem& time_system,
40
      ListenerHooks& hooks, Server::HotRestart& restarter, Stats::StoreRoot& store,
41
      Thread::BasicLockable& access_log_lock, Server::ComponentFactory& component_factory,
42
      Random::RandomGeneratorPtr&& random_generator, ThreadLocal::Instance& tls,
43
      Thread::ThreadFactory& thread_factory, Filesystem::Instance& file_system,
44
      std::unique_ptr<ProcessContext> process_context,
45
      Buffer::WatermarkFactorySharedPtr watermark_factory)>;
46

            
47
  static std::string hotRestartVersion(bool hot_restart_enabled);
48

            
49
  // Consumer must guarantee that all passed references are alive until this object is
50
  // destructed, except for `random_generator` which only needs to be alive until the constructor
51
  // completes execution.
52
  StrippedMainBase(const Server::Options& options, Server::ComponentFactory& component_factory,
53
                   std::unique_ptr<Server::Platform> platform_impl,
54
                   Random::RandomGenerator& random_generator);
55

            
56
  // Initialize the Envoy server instance. Must be called prior to any other method to use the
57
  // server. Separated from the constructor to allow for globals to be initialized first.
58
  void init(Event::TimeSystem& time_system, ListenerHooks& listener_hooks,
59
            std::unique_ptr<Random::RandomGenerator>&& random_generator,
60
            std::unique_ptr<ProcessContext> process_context,
61
            CreateInstanceFunction create_instance);
62

            
63
29
  void runServer() {
64
29
    ASSERT(options_.mode() == Server::Mode::Serve);
65
29
    server_->run();
66
29
  }
67

            
68
  // Will be null if options.mode() == Server::Mode::Validate
69
57
  Server::Instance* server() { return server_.get(); }
70

            
71
protected:
72
  std::unique_ptr<Server::Platform> platform_impl_;
73
  ProcessWide process_wide_; // Process-wide state setup/teardown (excluding grpc).
74
  // We instantiate this class regardless of ENVOY_GOOGLE_GRPC, to avoid having
75
  // an ifdef in a header file exposed in a C++ library. It is too easy to have
76
  // the ifdef be inconsistent across build-system boundaries.
77
  Grpc::GoogleGrpcContext google_grpc_context_;
78
  const Envoy::Server::Options& options_;
79
  Server::ComponentFactory& component_factory_;
80
  Stats::SymbolTableImpl symbol_table_;
81
  Stats::Allocator stats_allocator_;
82

            
83
  ThreadLocal::InstanceImplPtr tls_;
84
  std::unique_ptr<Server::HotRestart> restarter_;
85
  Stats::ThreadLocalStoreImplPtr stats_store_;
86
  // logging_context_ is only used in (some) subclasses, but it must be declared here so that it's
87
  // destructed after the server_. This is necessary because the Server and its threads may log
88
  // during destruction, causing data race issues with the Context's destruction and activation of
89
  // the saved context.
90
  std::unique_ptr<Logger::Context> logging_context_;
91
  std::unique_ptr<Init::Manager> init_manager_{std::make_unique<Init::ManagerImpl>("Server")};
92
  std::unique_ptr<Server::Instance> server_;
93

            
94
  // Only used for validation mode
95
  std::unique_ptr<ProcessContext> process_context_;
96

            
97
private:
98
  void configureComponentLogLevels();
99
  void configureHotRestarter(Random::RandomGenerator& random_generator);
100

            
101
  // Declaring main thread here allows custom integrations to instantiate
102
  // StrippedMainBase directly, with environment-specific dependency injection.
103
  // Note that MainThread must also be declared in MainCommon.
104
  Thread::MainThread main_thread_;
105
};
106

            
107
} // namespace Envoy