forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.h
241 lines (215 loc) · 9.56 KB
/
server.h
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
#pragma once
#include <chrono>
#include <cstdint>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include "envoy/event/timer.h"
#include "envoy/server/configuration.h"
#include "envoy/server/drain_manager.h"
#include "envoy/server/guarddog.h"
#include "envoy/server/instance.h"
#include "envoy/ssl/context_manager.h"
#include "envoy/stats/stats_macros.h"
#include "envoy/tracing/http_tracer.h"
#include "common/access_log/access_log_manager_impl.h"
#include "common/common/logger_delegates.h"
#include "common/grpc/async_client_manager_impl.h"
#include "common/runtime/runtime_impl.h"
#include "common/secret/secret_manager_impl.h"
#include "common/ssl/context_manager_impl.h"
#include "common/upstream/health_discovery_service.h"
#include "server/http/admin.h"
#include "server/init_manager_impl.h"
#include "server/listener_manager_impl.h"
#include "server/overload_manager_impl.h"
#include "server/test_hooks.h"
#include "server/worker_impl.h"
#include "absl/types/optional.h"
namespace Envoy {
namespace Server {
/**
* All server wide stats. @see stats_macros.h
*/
// clang-format off
#define ALL_SERVER_STATS(GAUGE) \
GAUGE(uptime) \
GAUGE(concurrency) \
GAUGE(memory_allocated) \
GAUGE(memory_heap_size) \
GAUGE(live) \
GAUGE(parent_connections) \
GAUGE(total_connections) \
GAUGE(version) \
GAUGE(days_until_first_cert_expiring) \
GAUGE(hot_restart_epoch)
// clang-format on
struct ServerStats {
ALL_SERVER_STATS(GENERATE_GAUGE_STRUCT)
};
/**
* Interface for creating service components during boot.
*/
class ComponentFactory {
public:
virtual ~ComponentFactory() {}
/**
* @return DrainManagerPtr a new drain manager for the server.
*/
virtual DrainManagerPtr createDrainManager(Instance& server) PURE;
/**
* @return Runtime::LoaderPtr the runtime implementation for the server.
*/
virtual Runtime::LoaderPtr createRuntime(Instance& server, Configuration::Initial& config) PURE;
};
/**
* Helpers used during server creation.
*/
class InstanceUtil : Logger::Loggable<Logger::Id::main> {
public:
enum class BootstrapVersion { V1, V2 };
/**
* Default implementation of runtime loader creation used in the real server and in most
* integration tests where a mock runtime is not needed.
*/
static Runtime::LoaderPtr createRuntime(Instance& server, Server::Configuration::Initial& config);
/**
* Helper for flushing counters, gauges and hisograms to sinks. This takes care of calling
* flush() on each sink and clearing the cache afterward.
* @param sinks supplies the list of sinks.
* @param source provides the metrics being flushed.
*/
static void flushMetricsToSinks(const std::list<Stats::SinkPtr>& sinks, Stats::Source& source);
/**
* Load a bootstrap config from either v1 or v2 and perform validation.
* @param bootstrap supplies the bootstrap to fill.
* @param config_path supplies the config path.
* @param v2_only supplies whether to attempt v1 fallback.
* @return BootstrapVersion to indicate which version of the API was parsed.
*/
static BootstrapVersion loadBootstrapConfig(envoy::config::bootstrap::v2::Bootstrap& bootstrap,
Options& options);
};
/**
* This is a helper used by InstanceImpl::run() on the stack. It's broken out to make testing
* easier.
*/
class RunHelper : Logger::Loggable<Logger::Id::main> {
public:
RunHelper(Event::Dispatcher& dispatcher, Upstream::ClusterManager& cm, HotRestart& hot_restart,
AccessLog::AccessLogManager& access_log_manager, InitManagerImpl& init_manager,
OverloadManager& overload_manager, std::function<void()> workers_start_cb);
// Helper function to inititate a shutdown. This can be triggered either by catching SIGTERM
// or be called from ServerImpl::shutdown().
void shutdown(Event::Dispatcher& dispatcher, HotRestart& hot_restart);
private:
Event::SignalEventPtr sigterm_;
Event::SignalEventPtr sig_usr_1_;
Event::SignalEventPtr sig_hup_;
bool shutdown_{};
};
/**
* This is the actual full standalone server which stiches together various common components.
*/
class InstanceImpl : Logger::Loggable<Logger::Id::main>, public Instance {
public:
/**
* @throw EnvoyException if initialization fails.
*/
InstanceImpl(Options& options, Event::TimeSystem& time_system,
Network::Address::InstanceConstSharedPtr local_address, TestHooks& hooks,
HotRestart& restarter, Stats::StoreRoot& store,
Thread::BasicLockable& access_log_lock, ComponentFactory& component_factory,
Runtime::RandomGeneratorPtr&& random_generator, ThreadLocal::Instance& tls);
~InstanceImpl() override;
void run();
// Server::Instance
Admin& admin() override { return *admin_; }
Api::Api& api() override { return *api_; }
Upstream::ClusterManager& clusterManager() override;
Ssl::ContextManager& sslContextManager() override { return *ssl_context_manager_; }
Event::Dispatcher& dispatcher() override { return *dispatcher_; }
Network::DnsResolverSharedPtr dnsResolver() override { return dns_resolver_; }
void drainListeners() override;
DrainManager& drainManager() override { return *drain_manager_; }
AccessLog::AccessLogManager& accessLogManager() override { return access_log_manager_; }
void failHealthcheck(bool fail) override;
void getParentStats(HotRestart::GetParentStatsInfo& info) override;
HotRestart& hotRestart() override { return restarter_; }
Init::Manager& initManager() override { return init_manager_; }
ListenerManager& listenerManager() override { return *listener_manager_; }
Secret::SecretManager& secretManager() override { return *secret_manager_; }
OverloadManager& overloadManager() override { return *overload_manager_; }
Runtime::RandomGenerator& random() override { return *random_generator_; }
RateLimit::ClientPtr
rateLimitClient(const absl::optional<std::chrono::milliseconds>& timeout) override {
return config_->rateLimitClientFactory().create(timeout);
}
Runtime::Loader& runtime() override;
void shutdown() override;
void shutdownAdmin() override;
Singleton::Manager& singletonManager() override { return *singleton_manager_; }
bool healthCheckFailed() override;
Options& options() override { return options_; }
time_t startTimeCurrentEpoch() override { return start_time_; }
time_t startTimeFirstEpoch() override { return original_start_time_; }
Stats::Store& stats() override { return stats_store_; }
Tracing::HttpTracer& httpTracer() override;
ThreadLocal::Instance& threadLocal() override { return thread_local_; }
const LocalInfo::LocalInfo& localInfo() override { return *local_info_; }
Event::TimeSystem& timeSystem() override { return time_system_; }
std::chrono::milliseconds statsFlushInterval() const override {
return config_->statsFlushInterval();
}
private:
ProtobufTypes::MessagePtr dumpBootstrapConfig();
void flushStats();
void initialize(Options& options, Network::Address::InstanceConstSharedPtr local_address,
ComponentFactory& component_factory);
void loadServerFlags(const absl::optional<std::string>& flags_path);
uint64_t numConnections();
void startWorkers();
void terminate();
Options& options_;
Event::TimeSystem& time_system_;
HotRestart& restarter_;
const time_t start_time_;
time_t original_start_time_;
Stats::StoreRoot& stats_store_;
std::unique_ptr<ServerStats> server_stats_;
ThreadLocal::Instance& thread_local_;
Api::ApiPtr api_;
Event::DispatcherPtr dispatcher_;
std::unique_ptr<AdminImpl> admin_;
Singleton::ManagerPtr singleton_manager_;
Network::ConnectionHandlerPtr handler_;
Runtime::RandomGeneratorPtr random_generator_;
Runtime::LoaderPtr runtime_loader_;
std::unique_ptr<Secret::SecretManager> secret_manager_;
std::unique_ptr<Ssl::ContextManagerImpl> ssl_context_manager_;
ProdListenerComponentFactory listener_component_factory_;
ProdWorkerFactory worker_factory_;
std::unique_ptr<ListenerManager> listener_manager_;
std::unique_ptr<Configuration::Main> config_;
Network::DnsResolverSharedPtr dns_resolver_;
Event::TimerPtr stat_flush_timer_;
LocalInfo::LocalInfoPtr local_info_;
DrainManagerPtr drain_manager_;
AccessLog::AccessLogManagerImpl access_log_manager_;
std::unique_ptr<Upstream::ClusterManagerFactory> cluster_manager_factory_;
InitManagerImpl init_manager_;
std::unique_ptr<Server::GuardDog> guard_dog_;
bool terminated_;
std::unique_ptr<Logger::FileSinkDelegate> file_logger_;
envoy::config::bootstrap::v2::Bootstrap bootstrap_;
ConfigTracker::EntryOwnerPtr config_tracker_entry_;
SystemTime bootstrap_config_update_time_;
Grpc::AsyncClientManagerPtr async_client_manager_;
Upstream::ProdClusterInfoFactory info_factory_;
Upstream::HdsDelegatePtr hds_delegate_;
std::unique_ptr<OverloadManagerImpl> overload_manager_;
std::unique_ptr<RunHelper> run_helper_;
};
} // namespace Server
} // namespace Envoy