forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overload_manager_impl.cc
253 lines (209 loc) · 9.29 KB
/
overload_manager_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
#include "server/overload_manager_impl.h"
#include "envoy/stats/scope.h"
#include "common/common/fmt.h"
#include "common/config/utility.h"
#include "common/protobuf/utility.h"
#include "common/stats/symbol_table_impl.h"
#include "server/resource_monitor_config_impl.h"
#include "absl/strings/str_cat.h"
namespace Envoy {
namespace Server {
namespace {
class ThresholdTriggerImpl : public OverloadAction::Trigger {
public:
ThresholdTriggerImpl(const envoy::config::overload::v2alpha::ThresholdTrigger& config)
: threshold_(config.value()) {}
bool updateValue(double value) override {
const bool fired = isFired();
value_ = value;
return fired != isFired();
}
bool isFired() const override { return value_.has_value() && value_ >= threshold_; }
private:
const double threshold_;
absl::optional<double> value_;
};
Stats::Counter& makeCounter(Stats::Scope& scope, absl::string_view a, absl::string_view b) {
Stats::StatNameManagedStorage stat_name(absl::StrCat("overload.", a, ".", b),
scope.symbolTable());
return scope.counterFromStatName(stat_name.statName());
}
Stats::Gauge& makeGauge(Stats::Scope& scope, absl::string_view a, absl::string_view b,
Stats::Gauge::ImportMode import_mode) {
Stats::StatNameManagedStorage stat_name(absl::StrCat("overload.", a, ".", b),
scope.symbolTable());
return scope.gaugeFromStatName(stat_name.statName(), import_mode);
}
} // namespace
OverloadAction::OverloadAction(const envoy::config::overload::v2alpha::OverloadAction& config,
Stats::Scope& stats_scope)
: active_gauge_(
makeGauge(stats_scope, config.name(), "active", Stats::Gauge::ImportMode::Accumulate)) {
for (const auto& trigger_config : config.triggers()) {
TriggerPtr trigger;
switch (trigger_config.trigger_oneof_case()) {
case envoy::config::overload::v2alpha::Trigger::kThreshold:
trigger = std::make_unique<ThresholdTriggerImpl>(trigger_config.threshold());
break;
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
if (!triggers_.insert(std::make_pair(trigger_config.name(), std::move(trigger))).second) {
throw EnvoyException(
fmt::format("Duplicate trigger resource for overload action {}", config.name()));
}
}
active_gauge_.set(0);
}
bool OverloadAction::updateResourcePressure(const std::string& name, double pressure) {
const bool active = isActive();
auto it = triggers_.find(name);
ASSERT(it != triggers_.end());
if (it->second->updateValue(pressure)) {
if (it->second->isFired()) {
active_gauge_.set(1);
const auto result = fired_triggers_.insert(name);
ASSERT(result.second);
} else {
active_gauge_.set(0);
const auto result = fired_triggers_.erase(name);
ASSERT(result == 1);
}
}
return active != isActive();
}
bool OverloadAction::isActive() const { return !fired_triggers_.empty(); }
OverloadManagerImpl::OverloadManagerImpl(
Event::Dispatcher& dispatcher, Stats::Scope& stats_scope,
ThreadLocal::SlotAllocator& slot_allocator,
const envoy::config::overload::v2alpha::OverloadManager& config,
ProtobufMessage::ValidationVisitor& validation_visitor, Api::Api& api)
: started_(false), dispatcher_(dispatcher), tls_(slot_allocator.allocateSlot()),
refresh_interval_(
std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(config, refresh_interval, 1000))) {
Configuration::ResourceMonitorFactoryContextImpl context(dispatcher, api, validation_visitor);
for (const auto& resource : config.resource_monitors()) {
const auto& name = resource.name();
ENVOY_LOG(debug, "Adding resource monitor for {}", name);
auto& factory =
Config::Utility::getAndCheckFactory<Configuration::ResourceMonitorFactory>(name);
auto config = Config::Utility::translateToFactoryConfig(resource, validation_visitor, factory);
auto monitor = factory.createResourceMonitor(*config, context);
auto result =
resources_.emplace(std::piecewise_construct, std::forward_as_tuple(name),
std::forward_as_tuple(name, std::move(monitor), *this, stats_scope));
if (!result.second) {
throw EnvoyException(fmt::format("Duplicate resource monitor {}", name));
}
}
for (const auto& action : config.actions()) {
const auto& name = action.name();
ENVOY_LOG(debug, "Adding overload action {}", name);
auto result = actions_.emplace(std::piecewise_construct, std::forward_as_tuple(name),
std::forward_as_tuple(action, stats_scope));
if (!result.second) {
throw EnvoyException(fmt::format("Duplicate overload action {}", name));
}
for (const auto& trigger : action.triggers()) {
const std::string& resource = trigger.name();
if (resources_.find(resource) == resources_.end()) {
throw EnvoyException(
fmt::format("Unknown trigger resource {} for overload action {}", resource, name));
}
resource_to_actions_.insert(std::make_pair(resource, name));
}
}
}
void OverloadManagerImpl::start() {
ASSERT(!started_);
started_ = true;
tls_->set([](Event::Dispatcher&) -> ThreadLocal::ThreadLocalObjectSharedPtr {
return std::make_shared<ThreadLocalOverloadState>();
});
if (resources_.empty()) {
return;
}
timer_ = dispatcher_.createTimer([this]() -> void {
for (auto& resource : resources_) {
resource.second.update();
}
timer_->enableTimer(refresh_interval_);
});
timer_->enableTimer(refresh_interval_);
}
void OverloadManagerImpl::stop() {
// Disable any pending timeouts.
if (timer_) {
timer_->disableTimer();
}
// Clear the resource map to block on any pending updates.
resources_.clear();
}
bool OverloadManagerImpl::registerForAction(const std::string& action,
Event::Dispatcher& dispatcher,
OverloadActionCb callback) {
ASSERT(!started_);
if (actions_.find(action) == actions_.end()) {
ENVOY_LOG(debug, "No overload action is configured for {}.", action);
return false;
}
action_to_callbacks_.emplace(std::piecewise_construct, std::forward_as_tuple(action),
std::forward_as_tuple(dispatcher, callback));
return true;
}
ThreadLocalOverloadState& OverloadManagerImpl::getThreadLocalOverloadState() {
return tls_->getTyped<ThreadLocalOverloadState>();
}
void OverloadManagerImpl::updateResourcePressure(const std::string& resource, double pressure) {
auto action_range = resource_to_actions_.equal_range(resource);
std::for_each(action_range.first, action_range.second,
[&](ResourceToActionMap::value_type& entry) {
const std::string& action = entry.second;
auto action_it = actions_.find(action);
ASSERT(action_it != actions_.end());
if (action_it->second.updateResourcePressure(resource, pressure)) {
const bool is_active = action_it->second.isActive();
const auto state =
is_active ? OverloadActionState::Active : OverloadActionState::Inactive;
ENVOY_LOG(info, "Overload action {} became {}", action,
is_active ? "active" : "inactive");
tls_->runOnAllThreads([this, action, state] {
tls_->getTyped<ThreadLocalOverloadState>().setState(action, state);
});
auto callback_range = action_to_callbacks_.equal_range(action);
std::for_each(callback_range.first, callback_range.second,
[&](ActionToCallbackMap::value_type& cb_entry) {
auto& cb = cb_entry.second;
cb.dispatcher_.post([&, state]() { cb.callback_(state); });
});
}
});
}
OverloadManagerImpl::Resource::Resource(const std::string& name, ResourceMonitorPtr monitor,
OverloadManagerImpl& manager, Stats::Scope& stats_scope)
: name_(name), monitor_(std::move(monitor)), manager_(manager), pending_update_(false),
pressure_gauge_(
makeGauge(stats_scope, name, "pressure", Stats::Gauge::ImportMode::NeverImport)),
failed_updates_counter_(makeCounter(stats_scope, name, "failed_updates")),
skipped_updates_counter_(makeCounter(stats_scope, name, "skipped_updates")) {}
void OverloadManagerImpl::Resource::update() {
if (!pending_update_) {
pending_update_ = true;
monitor_->updateResourceUsage(*this);
return;
}
ENVOY_LOG(debug, "Skipping update for resource {} which has pending update", name_);
skipped_updates_counter_.inc();
}
void OverloadManagerImpl::Resource::onSuccess(const ResourceUsage& usage) {
pending_update_ = false;
manager_.updateResourcePressure(name_, usage.resource_pressure_);
pressure_gauge_.set(usage.resource_pressure_ * 100); // convert to percent
}
void OverloadManagerImpl::Resource::onFailure(const EnvoyException& error) {
pending_update_ = false;
ENVOY_LOG(info, "Failed to update resource {}: {}", name_, error.what());
failed_updates_counter_.inc();
}
} // namespace Server
} // namespace Envoy