forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options_impl.cc
309 lines (281 loc) · 15.7 KB
/
options_impl.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "server/options_impl.h"
#include <chrono>
#include <cstdint>
#include <iostream>
#include <string>
#include "common/common/fmt.h"
#include "common/common/logger.h"
#include "common/common/macros.h"
#include "common/common/version.h"
#include "common/protobuf/utility.h"
#include "server/options_impl_platform.h"
#include "absl/strings/str_split.h"
#include "spdlog/spdlog.h"
#include "tclap/CmdLine.h"
namespace Envoy {
OptionsImpl::OptionsImpl(int argc, const char* const* argv,
const HotRestartVersionCb& hot_restart_version_cb,
spdlog::level::level_enum default_log_level)
: signal_handling_enabled_(true) {
std::string log_levels_string = "Log levels: ";
for (auto level_string_view : spdlog::level::level_string_views) {
log_levels_string += fmt::format("[{}]", level_string_view);
}
log_levels_string +=
fmt::format("\nDefault is [{}]", spdlog::level::level_string_views[default_log_level]);
const std::string component_log_level_string =
"Comma separated list of component log levels. For example upstream:debug,config:trace";
const std::string log_format_string =
fmt::format("Log message format in spdlog syntax "
"(see https://github.com/gabime/spdlog/wiki/3.-Custom-formatting)"
"\nDefault is \"{}\"",
Logger::Logger::DEFAULT_LOG_FORMAT);
TCLAP::CmdLine cmd("envoy", ' ', VersionInfo::version());
TCLAP::ValueArg<uint32_t> base_id(
"", "base-id", "base ID so that multiple envoys can run on the same host if needed", false, 0,
"uint32_t", cmd);
TCLAP::ValueArg<uint32_t> concurrency("", "concurrency", "# of worker threads to run", false,
std::thread::hardware_concurrency(), "uint32_t", cmd);
TCLAP::ValueArg<std::string> config_path("c", "config-path", "Path to configuration file", false,
"", "string", cmd);
TCLAP::ValueArg<std::string> config_yaml(
"", "config-yaml", "Inline YAML configuration, merges with the contents of --config-path",
false, "", "string", cmd);
TCLAP::SwitchArg allow_unknown_fields("", "allow-unknown-fields",
"allow unknown fields in static configuration (DEPRECATED)",
cmd, false);
TCLAP::SwitchArg allow_unknown_static_fields("", "allow-unknown-static-fields",
"allow unknown fields in static configuration", cmd,
false);
TCLAP::SwitchArg reject_unknown_dynamic_fields("", "reject-unknown-dynamic-fields",
"reject unknown fields in dynamic configuration",
cmd, false);
TCLAP::ValueArg<std::string> admin_address_path("", "admin-address-path", "Admin address path",
false, "", "string", cmd);
TCLAP::ValueArg<std::string> local_address_ip_version("", "local-address-ip-version",
"The local "
"IP address version (v4 or v6).",
false, "v4", "string", cmd);
TCLAP::ValueArg<std::string> log_level(
"l", "log-level", log_levels_string, false,
spdlog::level::level_string_views[default_log_level].data(), "string", cmd);
TCLAP::ValueArg<std::string> component_log_level(
"", "component-log-level", component_log_level_string, false, "", "string", cmd);
TCLAP::ValueArg<std::string> log_format("", "log-format", log_format_string, false,
Logger::Logger::DEFAULT_LOG_FORMAT, "string", cmd);
TCLAP::ValueArg<std::string> log_path("", "log-path", "Path to logfile", false, "", "string",
cmd);
TCLAP::ValueArg<uint32_t> restart_epoch("", "restart-epoch", "hot restart epoch #", false, 0,
"uint32_t", cmd);
TCLAP::SwitchArg hot_restart_version_option("", "hot-restart-version",
"hot restart compatibility version", cmd);
TCLAP::ValueArg<std::string> service_cluster("", "service-cluster", "Cluster name", false, "",
"string", cmd);
TCLAP::ValueArg<std::string> service_node("", "service-node", "Node name", false, "", "string",
cmd);
TCLAP::ValueArg<std::string> service_zone("", "service-zone", "Zone name", false, "", "string",
cmd);
TCLAP::ValueArg<uint32_t> file_flush_interval_msec("", "file-flush-interval-msec",
"Interval for log flushing in msec", false,
10000, "uint32_t", cmd);
TCLAP::ValueArg<uint32_t> drain_time_s("", "drain-time-s", "Hot restart drain time in seconds",
false, 600, "uint32_t", cmd);
TCLAP::ValueArg<uint32_t> parent_shutdown_time_s("", "parent-shutdown-time-s",
"Hot restart parent shutdown time in seconds",
false, 900, "uint32_t", cmd);
TCLAP::ValueArg<std::string> mode("", "mode",
"One of 'serve' (default; validate configs and then serve "
"traffic normally) or 'validate' (validate configs and exit).",
false, "serve", "string", cmd);
TCLAP::ValueArg<uint64_t> max_stats("", "max-stats",
"Deprecated and unused; please do not specify.", false, 123,
"uint64_t", cmd);
TCLAP::ValueArg<uint64_t> max_obj_name_len("", "max-obj-name-len",
"Deprecated and unused; please do not specify.", false,
123, "uint64_t", cmd);
TCLAP::SwitchArg disable_hot_restart("", "disable-hot-restart",
"Disable hot restart functionality", cmd, false);
TCLAP::SwitchArg enable_mutex_tracing(
"", "enable-mutex-tracing", "Enable mutex contention tracing functionality", cmd, false);
TCLAP::SwitchArg cpuset_threads(
"", "cpuset-threads", "Get the default # of worker threads from cpuset size", cmd, false);
TCLAP::ValueArg<bool> use_libevent_buffer("", "use-libevent-buffers",
"Use the original libevent buffer implementation",
false, false, "bool", cmd);
TCLAP::ValueArg<bool> use_fake_symbol_table("", "use-fake-symbol-table",
"Use fake symbol table implementation", false, true,
"bool", cmd);
cmd.setExceptionHandling(false);
try {
cmd.parse(argc, argv);
count_ = cmd.getArgList().size();
} catch (TCLAP::ArgException& e) {
try {
cmd.getOutput()->failure(cmd, e);
} catch (const TCLAP::ExitException&) {
// failure() has already written an informative message to stderr, so all that's left to do
// is throw our own exception with the original message.
throw MalformedArgvException(e.what());
}
} catch (const TCLAP::ExitException& e) {
// parse() throws an ExitException with status 0 after printing the output for --help and
// --version.
throw NoServingException();
}
hot_restart_disabled_ = disable_hot_restart.getValue();
mutex_tracing_enabled_ = enable_mutex_tracing.getValue();
libevent_buffer_enabled_ = use_libevent_buffer.getValue();
fake_symbol_table_enabled_ = use_fake_symbol_table.getValue();
cpuset_threads_ = cpuset_threads.getValue();
log_level_ = default_log_level;
for (size_t i = 0; i < ARRAY_SIZE(spdlog::level::level_string_views); i++) {
if (log_level.getValue() == spdlog::level::level_string_views[i]) {
log_level_ = static_cast<spdlog::level::level_enum>(i);
}
}
log_format_ = log_format.getValue();
parseComponentLogLevels(component_log_level.getValue());
if (mode.getValue() == "serve") {
mode_ = Server::Mode::Serve;
} else if (mode.getValue() == "validate") {
mode_ = Server::Mode::Validate;
} else if (mode.getValue() == "init_only") {
mode_ = Server::Mode::InitOnly;
} else {
const std::string message = fmt::format("error: unknown mode '{}'", mode.getValue());
throw MalformedArgvException(message);
}
if (local_address_ip_version.getValue() == "v4") {
local_address_ip_version_ = Network::Address::IpVersion::v4;
} else if (local_address_ip_version.getValue() == "v6") {
local_address_ip_version_ = Network::Address::IpVersion::v6;
} else {
const std::string message =
fmt::format("error: unknown IP address version '{}'", local_address_ip_version.getValue());
throw MalformedArgvException(message);
}
// For base ID, scale what the user inputs by 10 so that we have spread for domain sockets.
base_id_ = base_id.getValue() * 10;
if (!concurrency.isSet() && cpuset_threads_) {
// The 'concurrency' command line option wasn't set but the 'cpuset-threads'
// option was set. Use the number of CPUs assigned to the process cpuset, if
// that can be known.
concurrency_ = OptionsImplPlatform::getCpuCount();
} else {
if (concurrency.isSet() && cpuset_threads_ && cpuset_threads.isSet()) {
ENVOY_LOG(warn, "Both --concurrency and --cpuset-threads options are set; not applying "
"--cpuset-threads.");
}
concurrency_ = std::max(1U, concurrency.getValue());
}
config_path_ = config_path.getValue();
config_yaml_ = config_yaml.getValue();
if (allow_unknown_fields.getValue()) {
ENVOY_LOG(warn,
"--allow-unknown-fields is deprecated, use --allow-unknown-static-fields instead.");
}
allow_unknown_static_fields_ =
allow_unknown_static_fields.getValue() || allow_unknown_fields.getValue();
reject_unknown_dynamic_fields_ = reject_unknown_dynamic_fields.getValue();
admin_address_path_ = admin_address_path.getValue();
log_path_ = log_path.getValue();
restart_epoch_ = restart_epoch.getValue();
service_cluster_ = service_cluster.getValue();
service_node_ = service_node.getValue();
service_zone_ = service_zone.getValue();
file_flush_interval_msec_ = std::chrono::milliseconds(file_flush_interval_msec.getValue());
drain_time_ = std::chrono::seconds(drain_time_s.getValue());
parent_shutdown_time_ = std::chrono::seconds(parent_shutdown_time_s.getValue());
if (hot_restart_version_option.getValue()) {
std::cerr << hot_restart_version_cb(!hot_restart_disabled_);
throw NoServingException();
}
}
void OptionsImpl::parseComponentLogLevels(const std::string& component_log_levels) {
if (component_log_levels.empty()) {
return;
}
component_log_level_str_ = component_log_levels;
std::vector<std::string> log_levels = absl::StrSplit(component_log_levels, ',');
for (auto& level : log_levels) {
std::vector<std::string> log_name_level = absl::StrSplit(level, ':');
if (log_name_level.size() != 2) {
logError(fmt::format("error: component log level not correctly specified '{}'", level));
}
std::string log_name = log_name_level[0];
std::string log_level = log_name_level[1];
size_t level_to_use = std::numeric_limits<size_t>::max();
for (size_t i = 0; i < ARRAY_SIZE(spdlog::level::level_string_views); i++) {
if (log_level == spdlog::level::level_string_views[i]) {
level_to_use = i;
break;
}
}
if (level_to_use == std::numeric_limits<size_t>::max()) {
logError(fmt::format("error: invalid log level specified '{}'", log_level));
}
Logger::Logger* logger_to_change = Logger::Registry::logger(log_name);
if (!logger_to_change) {
logError(fmt::format("error: invalid component specified '{}'", log_name));
}
component_log_levels_.push_back(
std::make_pair(log_name, static_cast<spdlog::level::level_enum>(level_to_use)));
}
}
uint32_t OptionsImpl::count() const { return count_; }
void OptionsImpl::logError(const std::string& error) const { throw MalformedArgvException(error); }
Server::CommandLineOptionsPtr OptionsImpl::toCommandLineOptions() const {
Server::CommandLineOptionsPtr command_line_options =
std::make_unique<envoy::admin::v2alpha::CommandLineOptions>();
command_line_options->set_base_id(baseId());
command_line_options->set_concurrency(concurrency());
command_line_options->set_config_path(configPath());
command_line_options->set_config_yaml(configYaml());
command_line_options->set_allow_unknown_static_fields(allow_unknown_static_fields_);
command_line_options->set_reject_unknown_dynamic_fields(reject_unknown_dynamic_fields_);
command_line_options->set_admin_address_path(adminAddressPath());
command_line_options->set_component_log_level(component_log_level_str_);
command_line_options->set_log_level(spdlog::level::to_string_view(logLevel()).data(),
spdlog::level::to_string_view(logLevel()).size());
command_line_options->set_log_format(logFormat());
command_line_options->set_log_path(logPath());
command_line_options->set_service_cluster(serviceClusterName());
command_line_options->set_service_node(serviceNodeName());
command_line_options->set_service_zone(serviceZone());
if (mode() == Server::Mode::Serve) {
command_line_options->set_mode(envoy::admin::v2alpha::CommandLineOptions::Serve);
} else if (mode() == Server::Mode::Validate) {
command_line_options->set_mode(envoy::admin::v2alpha::CommandLineOptions::Validate);
} else {
command_line_options->set_mode(envoy::admin::v2alpha::CommandLineOptions::InitOnly);
}
if (localAddressIpVersion() == Network::Address::IpVersion::v4) {
command_line_options->set_local_address_ip_version(
envoy::admin::v2alpha::CommandLineOptions::v4);
} else {
command_line_options->set_local_address_ip_version(
envoy::admin::v2alpha::CommandLineOptions::v6);
}
command_line_options->mutable_file_flush_interval()->MergeFrom(
Protobuf::util::TimeUtil::MillisecondsToDuration(fileFlushIntervalMsec().count()));
command_line_options->mutable_parent_shutdown_time()->MergeFrom(
Protobuf::util::TimeUtil::SecondsToDuration(parentShutdownTime().count()));
command_line_options->mutable_drain_time()->MergeFrom(
Protobuf::util::TimeUtil::SecondsToDuration(drainTime().count()));
command_line_options->set_disable_hot_restart(hotRestartDisabled());
command_line_options->set_enable_mutex_tracing(mutexTracingEnabled());
command_line_options->set_cpuset_threads(cpusetThreadsEnabled());
command_line_options->set_restart_epoch(restartEpoch());
return command_line_options;
}
OptionsImpl::OptionsImpl(const std::string& service_cluster, const std::string& service_node,
const std::string& service_zone, spdlog::level::level_enum log_level)
: base_id_(0u), concurrency_(1u), config_path_(""), config_yaml_(""),
local_address_ip_version_(Network::Address::IpVersion::v4), log_level_(log_level),
log_format_(Logger::Logger::DEFAULT_LOG_FORMAT), restart_epoch_(0u),
service_cluster_(service_cluster), service_node_(service_node), service_zone_(service_zone),
file_flush_interval_msec_(10000), drain_time_(600), parent_shutdown_time_(900),
mode_(Server::Mode::Serve), hot_restart_disabled_(false), signal_handling_enabled_(true),
mutex_tracing_enabled_(false), cpuset_threads_(false), libevent_buffer_enabled_(false),
fake_symbol_table_enabled_(false) {}
} // namespace Envoy