|
| 1 | +#include "exe/main_common.h" |
| 2 | + |
1 | 3 | #include <iostream> |
2 | 4 | #include <memory> |
3 | 5 |
|
4 | 6 | #include "common/common/compiler_requirements.h" |
5 | 7 | #include "common/event/libevent.h" |
6 | 8 | #include "common/network/utility.h" |
7 | 9 | #include "common/stats/stats_impl.h" |
8 | | -#include "common/stats/thread_local_store.h" |
9 | 10 |
|
10 | 11 | #include "server/config_validation/server.h" |
11 | 12 | #include "server/drain_manager_impl.h" |
|
22 | 23 | #include "ares.h" |
23 | 24 |
|
24 | 25 | namespace Envoy { |
25 | | -namespace Server { |
26 | | - |
27 | | -class ProdComponentFactory : public ComponentFactory { |
28 | | -public: |
29 | | - // Server::DrainManagerFactory |
30 | | - DrainManagerPtr createDrainManager(Instance& server) override { |
31 | | - return DrainManagerPtr{ |
32 | | - // The global drain manager only triggers on listener modification, which effectively is |
33 | | - // hot restart at the global level. The per-listener drain managers decide whether to |
34 | | - // to include /healthcheck/fail status. |
35 | | - new DrainManagerImpl(server, envoy::api::v2::Listener_DrainType_MODIFY_ONLY)}; |
36 | | - } |
37 | 26 |
|
38 | | - Runtime::LoaderPtr createRuntime(Server::Instance& server, |
39 | | - Server::Configuration::Initial& config) override { |
40 | | - return Server::InstanceUtil::createRuntime(server, config); |
41 | | - } |
42 | | -}; |
| 27 | +Server::DrainManagerPtr ProdComponentFactory::createDrainManager(Server::Instance& server) { |
| 28 | + // The global drain manager only triggers on listener modification, which effectively is |
| 29 | + // hot restart at the global level. The per-listener drain managers decide whether to |
| 30 | + // to include /healthcheck/fail status. |
| 31 | + return std::make_unique<Server::DrainManagerImpl>(server, |
| 32 | + envoy::api::v2::Listener_DrainType_MODIFY_ONLY); |
| 33 | +} |
43 | 34 |
|
44 | | -} // namespace Server |
| 35 | +Runtime::LoaderPtr ProdComponentFactory::createRuntime(Server::Instance& server, |
| 36 | + Server::Configuration::Initial& config) { |
| 37 | + return Server::InstanceUtil::createRuntime(server, config); |
| 38 | +} |
45 | 39 |
|
46 | | -int main_common(OptionsImpl& options) { |
47 | | - Stats::RawStatData::configure(options); |
| 40 | +MainCommonBase::MainCommonBase(OptionsImpl& options, bool hot_restart) : options_(options) { |
| 41 | + ares_library_init(ARES_LIB_INIT_ALL); |
| 42 | + Event::Libevent::Global::initialize(); |
| 43 | + RELEASE_ASSERT(Envoy::Server::validateProtoDescriptors()); |
48 | 44 |
|
| 45 | + switch (options_.mode()) { |
| 46 | + case Server::Mode::Serve: { |
49 | 47 | #ifdef ENVOY_HOT_RESTART |
50 | | - std::unique_ptr<Server::HotRestartImpl> restarter; |
51 | | - try { |
52 | | - restarter.reset(new Server::HotRestartImpl(options)); |
53 | | - } catch (Envoy::EnvoyException& e) { |
54 | | - std::cerr << "unable to initialize hot restart: " << e.what() << std::endl; |
55 | | - return 1; |
| 48 | + if (hot_restart) { |
| 49 | + restarter_.reset(new Server::HotRestartImpl(options_)); |
| 50 | + } |
| 51 | +#endif |
| 52 | + if (!hot_restart) { |
| 53 | + restarter_.reset(new Server::HotRestartNopImpl()); |
| 54 | + } |
| 55 | + |
| 56 | + Stats::RawStatData::configure(options_); |
| 57 | + tls_.reset(new ThreadLocal::InstanceImpl); |
| 58 | + Thread::BasicLockable& log_lock = restarter_->logLock(); |
| 59 | + Thread::BasicLockable& access_log_lock = restarter_->accessLogLock(); |
| 60 | + auto local_address = Network::Utility::getLocalAddress(options_.localAddressIpVersion()); |
| 61 | + Logger::Registry::initialize(options_.logLevel(), log_lock); |
| 62 | + |
| 63 | + stats_store_.reset(new Stats::ThreadLocalStoreImpl(restarter_->statsAllocator())); |
| 64 | + server_.reset(new Server::InstanceImpl(options_, local_address, default_test_hooks_, |
| 65 | + *restarter_, *stats_store_, access_log_lock, |
| 66 | + component_factory_, *tls_)); |
| 67 | + break; |
56 | 68 | } |
| 69 | + case Server::Mode::Validate: |
| 70 | + break; |
| 71 | + } |
| 72 | +} |
57 | 73 |
|
58 | | - Thread::BasicLockable& log_lock = restarter->logLock(); |
59 | | - Thread::BasicLockable& access_log_lock = restarter->accessLogLock(); |
60 | | - Stats::RawStatDataAllocator& stats_allocator = *restarter; |
61 | | -#else |
62 | | - std::unique_ptr<Server::HotRestartNopImpl> restarter; |
63 | | - restarter.reset(new Server::HotRestartNopImpl()); |
64 | | - |
65 | | - Thread::MutexBasicLockable log_lock, access_log_lock; |
66 | | - Stats::HeapRawStatDataAllocator stats_allocator; |
67 | | -#endif |
| 74 | +MainCommonBase::~MainCommonBase() { ares_library_cleanup(); } |
68 | 75 |
|
69 | | - RELEASE_ASSERT(Envoy::Server::validateProtoDescriptors()); |
70 | | - Event::Libevent::Global::initialize(); |
71 | | - Server::ProdComponentFactory component_factory; |
72 | | - auto local_address = Network::Utility::getLocalAddress(options.localAddressIpVersion()); |
73 | | - switch (options.mode()) { |
| 76 | +bool MainCommonBase::run() { |
| 77 | + switch (options_.mode()) { |
74 | 78 | case Server::Mode::Serve: |
75 | | - break; |
76 | | - case Server::Mode::Validate: |
77 | | - Thread::MutexBasicLockable log_lock; |
78 | | - Logger::Registry::initialize(options.logLevel(), log_lock); |
79 | | - return Server::validateConfig(options, local_address, component_factory) ? 0 : 1; |
| 79 | + server_->run(); |
| 80 | + return true; |
| 81 | + case Server::Mode::Validate: { |
| 82 | + auto local_address = Network::Utility::getLocalAddress(options_.localAddressIpVersion()); |
| 83 | + return Server::validateConfig(options_, local_address, component_factory_); |
80 | 84 | } |
| 85 | + } |
| 86 | + NOT_REACHED; |
| 87 | +} |
81 | 88 |
|
82 | | - ares_library_init(ARES_LIB_INIT_ALL); |
| 89 | +MainCommon::MainCommon(int argc, char** argv, bool hot_restart) |
| 90 | + : options_(computeOptions(argc, argv, hot_restart)), base_(*options_, hot_restart) {} |
| 91 | + |
| 92 | +std::unique_ptr<OptionsImpl> MainCommon::computeOptions(int argc, char** argv, bool hot_restart) { |
| 93 | + OptionsImpl::HotRestartVersionCb hot_restart_version_cb = [](uint64_t, uint64_t) { |
| 94 | + return "disabled"; |
| 95 | + }; |
83 | 96 |
|
84 | | - Logger::Registry::initialize(options.logLevel(), log_lock); |
85 | | - DefaultTestHooks default_test_hooks; |
86 | | - ThreadLocal::InstanceImpl tls; |
87 | | - Stats::ThreadLocalStoreImpl stats_store(stats_allocator); |
| 97 | +#ifdef ENVOY_HOT_RESTART |
| 98 | + if (hot_restart) { |
| 99 | + // Enabled by default, except on OS X. Control with "bazel --define=hot_restart=disabled" |
| 100 | + hot_restart_version_cb = [](uint64_t max_num_stats, uint64_t max_stat_name_len) { |
| 101 | + return Server::HotRestartImpl::hotRestartVersion(max_num_stats, max_stat_name_len); |
| 102 | + }; |
| 103 | + } |
| 104 | +#else |
| 105 | + // Hot-restart should not be specified if the support is not compiled in. |
| 106 | + RELEASE_ASSERT(!hot_restart); |
| 107 | +#endif |
| 108 | + return std::make_unique<OptionsImpl>(argc, argv, hot_restart_version_cb, spdlog::level::info); |
| 109 | +} |
| 110 | + |
| 111 | +// Legacy implementation of main_common. |
| 112 | +// |
| 113 | +// TODO(jmarantz): Remove this when all callers are removed. At that time, MainCommonBase |
| 114 | +// and MainCommon can be merged. The current theory is that only Google calls this. |
| 115 | +int main_common(OptionsImpl& options) { |
88 | 116 | try { |
89 | | - Server::InstanceImpl server(options, local_address, default_test_hooks, *restarter, stats_store, |
90 | | - access_log_lock, component_factory, tls); |
91 | | - server.run(); |
92 | | - } catch (const EnvoyException& e) { |
93 | | - ares_library_cleanup(); |
94 | | - return 1; |
| 117 | +#if ENVOY_HOT_RESTART |
| 118 | + MainCommonBase main_common(options, true); |
| 119 | +#else |
| 120 | + MainCommonBase main_common(options, false); |
| 121 | +#endif |
| 122 | + return main_common.run() ? EXIT_SUCCESS : EXIT_FAILURE; |
| 123 | + } catch (EnvoyException& e) { |
| 124 | + return EXIT_FAILURE; |
95 | 125 | } |
96 | | - ares_library_cleanup(); |
97 | | - return 0; |
| 126 | + return EXIT_SUCCESS; |
98 | 127 | } |
99 | 128 |
|
100 | 129 | } // namespace Envoy |
0 commit comments