forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_common.cc
127 lines (109 loc) · 4.35 KB
/
main_common.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "exe/main_common.h"
#include <iostream>
#include <memory>
#include "common/common/compiler_requirements.h"
#include "common/common/perf_annotation.h"
#include "common/event/libevent.h"
#include "common/network/utility.h"
#include "common/stats/stats_impl.h"
#include "server/config_validation/server.h"
#include "server/drain_manager_impl.h"
#include "server/hot_restart_nop_impl.h"
#include "server/options_impl.h"
#include "server/proto_descriptors.h"
#include "server/server.h"
#include "server/test_hooks.h"
#ifdef ENVOY_HOT_RESTART
#include "server/hot_restart_impl.h"
#endif
#include "ares.h"
namespace Envoy {
Server::DrainManagerPtr ProdComponentFactory::createDrainManager(Server::Instance& server) {
// The global drain manager only triggers on listener modification, which effectively is
// hot restart at the global level. The per-listener drain managers decide whether to
// to include /healthcheck/fail status.
return std::make_unique<Server::DrainManagerImpl>(server,
envoy::api::v2::Listener_DrainType_MODIFY_ONLY);
}
Runtime::LoaderPtr ProdComponentFactory::createRuntime(Server::Instance& server,
Server::Configuration::Initial& config) {
return Server::InstanceUtil::createRuntime(server, config);
}
MainCommonBase::MainCommonBase(OptionsImpl& options) : options_(options) {
ares_library_init(ARES_LIB_INIT_ALL);
Event::Libevent::Global::initialize();
RELEASE_ASSERT(Envoy::Server::validateProtoDescriptors());
Stats::RawStatData::configure(options_);
switch (options_.mode()) {
case Server::Mode::InitOnly:
case Server::Mode::Serve: {
#ifdef ENVOY_HOT_RESTART
if (!options.hotRestartDisabled()) {
restarter_.reset(new Server::HotRestartImpl(options_));
}
#endif
if (restarter_.get() == nullptr) {
restarter_.reset(new Server::HotRestartNopImpl());
}
tls_.reset(new ThreadLocal::InstanceImpl);
Thread::BasicLockable& log_lock = restarter_->logLock();
Thread::BasicLockable& access_log_lock = restarter_->accessLogLock();
auto local_address = Network::Utility::getLocalAddress(options_.localAddressIpVersion());
Logger::Registry::initialize(options_.logLevel(), options_.logFormat(), log_lock);
stats_store_.reset(new Stats::ThreadLocalStoreImpl(restarter_->statsAllocator()));
server_.reset(new Server::InstanceImpl(options_, local_address, default_test_hooks_,
*restarter_, *stats_store_, access_log_lock,
component_factory_, *tls_));
break;
}
case Server::Mode::Validate:
restarter_.reset(new Server::HotRestartNopImpl());
Logger::Registry::initialize(options_.logLevel(), options_.logFormat(), restarter_->logLock());
break;
}
}
MainCommonBase::~MainCommonBase() { ares_library_cleanup(); }
bool MainCommonBase::run() {
switch (options_.mode()) {
case Server::Mode::Serve:
server_->run();
return true;
case Server::Mode::Validate: {
auto local_address = Network::Utility::getLocalAddress(options_.localAddressIpVersion());
return Server::validateConfig(options_, local_address, component_factory_);
}
case Server::Mode::InitOnly:
PERF_DUMP();
return true;
}
NOT_REACHED;
}
MainCommon::MainCommon(int argc, char** argv)
: options_(argc, argv, &MainCommon::hotRestartVersion, spdlog::level::info), base_(options_) {}
std::string MainCommon::hotRestartVersion(uint64_t max_num_stats, uint64_t max_stat_name_len,
bool hot_restart_enabled) {
#ifdef ENVOY_HOT_RESTART
if (hot_restart_enabled) {
return Server::HotRestartImpl::hotRestartVersion(max_num_stats, max_stat_name_len);
}
#else
UNREFERENCED_PARAMETER(hot_restart_enabled);
UNREFERENCED_PARAMETER(max_num_stats);
UNREFERENCED_PARAMETER(max_stat_name_len);
#endif
return "disabled";
}
// Legacy implementation of main_common.
//
// TODO(jmarantz): Remove this when all callers are removed. At that time, MainCommonBase
// and MainCommon can be merged. The current theory is that only Google calls this.
int main_common(OptionsImpl& options) {
try {
MainCommonBase main_common(options);
return main_common.run() ? EXIT_SUCCESS : EXIT_FAILURE;
} catch (EnvoyException& e) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
} // namespace Envoy