forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverload_manager_impl_test.cc
593 lines (503 loc) · 19.4 KB
/
overload_manager_impl_test.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "envoy/config/overload/v3/overload.pb.h"
#include "envoy/server/overload_manager.h"
#include "envoy/server/resource_monitor.h"
#include "envoy/server/resource_monitor_config.h"
#include "common/stats/isolated_store_impl.h"
#include "server/overload_manager_impl.h"
#include "extensions/resource_monitors/common/factory_base.h"
#include "test/common/stats/stat_test_utility.h"
#include "test/mocks/event/mocks.h"
#include "test/mocks/protobuf/mocks.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/registry.h"
#include "test/test_common/utility.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::_;
using testing::AllOf;
using testing::Invoke;
using testing::NiceMock;
using testing::Property;
namespace Envoy {
namespace Server {
namespace {
class FakeResourceMonitor : public ResourceMonitor {
public:
FakeResourceMonitor(Event::Dispatcher& dispatcher) : dispatcher_(dispatcher), response_(0.0) {}
void setPressure(double pressure) { response_ = pressure; }
void setError() { response_ = EnvoyException("fake_error"); }
void setUpdateAsync(bool new_update_async) {
callbacks_.reset();
update_async_ = new_update_async;
}
void updateResourceUsage(ResourceMonitor::Callbacks& callbacks) override {
if (update_async_) {
callbacks_.emplace(callbacks);
} else {
publishUpdate(callbacks);
}
}
void publishUpdate() {
if (update_async_) {
ASSERT(callbacks_.has_value());
publishUpdate(*callbacks_);
callbacks_.reset();
}
}
private:
void publishUpdate(ResourceMonitor::Callbacks& callbacks) {
if (absl::holds_alternative<double>(response_)) {
Server::ResourceUsage usage;
usage.resource_pressure_ = absl::get<double>(response_);
dispatcher_.post([&, usage]() { callbacks.onSuccess(usage); });
} else {
EnvoyException& error = absl::get<EnvoyException>(response_);
dispatcher_.post([&, error]() { callbacks.onFailure(error); });
}
}
Event::Dispatcher& dispatcher_;
absl::variant<double, EnvoyException> response_;
bool update_async_ = false;
absl::optional<std::reference_wrapper<ResourceMonitor::Callbacks>> callbacks_;
};
template <class ConfigType>
class FakeResourceMonitorFactory : public Server::Configuration::ResourceMonitorFactory {
public:
FakeResourceMonitorFactory(const std::string& name) : monitor_(nullptr), name_(name) {}
Server::ResourceMonitorPtr
createResourceMonitor(const Protobuf::Message&,
Server::Configuration::ResourceMonitorFactoryContext& context) override {
auto monitor = std::make_unique<FakeResourceMonitor>(context.dispatcher());
monitor_ = monitor.get();
return monitor;
}
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return ProtobufTypes::MessagePtr{new ConfigType()};
}
std::string name() const override { return name_; }
FakeResourceMonitor* monitor_; // not owned
const std::string name_;
};
class OverloadManagerImplTest : public testing::Test {
protected:
OverloadManagerImplTest()
: factory1_("envoy.resource_monitors.fake_resource1"),
factory2_("envoy.resource_monitors.fake_resource2"),
factory3_("envoy.resource_monitors.fake_resource3"),
factory4_("envoy.resource_monitors.fake_resource4"), register_factory1_(factory1_),
register_factory2_(factory2_), register_factory3_(factory3_), register_factory4_(factory4_),
api_(Api::createApiForTest(stats_)) {}
void setDispatcherExpectation() {
timer_ = new NiceMock<Event::MockTimer>();
EXPECT_CALL(dispatcher_, createTimer_(_)).WillOnce(Invoke([&](Event::TimerCb cb) {
timer_cb_ = cb;
return timer_;
}));
}
envoy::config::overload::v3::OverloadManager parseConfig(const std::string& config) {
envoy::config::overload::v3::OverloadManager proto;
bool success = Protobuf::TextFormat::ParseFromString(config, &proto);
ASSERT(success);
return proto;
}
std::string getConfig() {
return R"EOF(
refresh_interval {
seconds: 1
}
resource_monitors {
name: "envoy.resource_monitors.fake_resource1"
}
resource_monitors {
name: "envoy.resource_monitors.fake_resource2"
}
resource_monitors {
name: "envoy.resource_monitors.fake_resource3"
}
resource_monitors {
name: "envoy.resource_monitors.fake_resource4"
}
actions {
name: "envoy.overload_actions.dummy_action"
triggers {
name: "envoy.resource_monitors.fake_resource1"
threshold {
value: 0.9
}
}
triggers {
name: "envoy.resource_monitors.fake_resource2"
threshold {
value: 0.8
}
}
triggers {
name: "envoy.resource_monitors.fake_resource3"
scaled {
scaling_threshold: 0.5
saturation_threshold: 0.8
}
}
triggers {
name: "envoy.resource_monitors.fake_resource4"
scaled {
scaling_threshold: 0.5
saturation_threshold: 0.8
}
}
}
)EOF";
}
std::unique_ptr<OverloadManagerImpl> createOverloadManager(const std::string& config) {
return std::make_unique<OverloadManagerImpl>(dispatcher_, stats_, thread_local_,
parseConfig(config), validation_visitor_, *api_);
}
FakeResourceMonitorFactory<Envoy::ProtobufWkt::Struct> factory1_;
FakeResourceMonitorFactory<Envoy::ProtobufWkt::Timestamp> factory2_;
FakeResourceMonitorFactory<Envoy::ProtobufWkt::Timestamp> factory3_;
FakeResourceMonitorFactory<Envoy::ProtobufWkt::Timestamp> factory4_;
Registry::InjectFactory<Configuration::ResourceMonitorFactory> register_factory1_;
Registry::InjectFactory<Configuration::ResourceMonitorFactory> register_factory2_;
Registry::InjectFactory<Configuration::ResourceMonitorFactory> register_factory3_;
Registry::InjectFactory<Configuration::ResourceMonitorFactory> register_factory4_;
NiceMock<Event::MockDispatcher> dispatcher_;
NiceMock<Event::MockTimer>* timer_; // not owned
Stats::TestUtil::TestStore stats_;
NiceMock<ThreadLocal::MockInstance> thread_local_;
Event::TimerCb timer_cb_;
NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor_;
Api::ApiPtr api_;
};
TEST_F(OverloadManagerImplTest, CallbackOnlyFiresWhenStateChanges) {
setDispatcherExpectation();
auto manager(createOverloadManager(getConfig()));
bool is_active = false;
int cb_count = 0;
manager->registerForAction("envoy.overload_actions.dummy_action", dispatcher_,
[&](OverloadActionState state) {
is_active = state.isSaturated();
cb_count++;
});
manager->registerForAction("envoy.overload_actions.unknown_action", dispatcher_,
[&](OverloadActionState) { EXPECT_TRUE(false); });
manager->start();
Stats::Gauge& active_gauge = stats_.gauge("overload.envoy.overload_actions.dummy_action.active",
Stats::Gauge::ImportMode::Accumulate);
Stats::Gauge& scale_percent_gauge =
stats_.gauge("overload.envoy.overload_actions.dummy_action.scale_percent",
Stats::Gauge::ImportMode::Accumulate);
Stats::Gauge& pressure_gauge1 =
stats_.gauge("overload.envoy.resource_monitors.fake_resource1.pressure",
Stats::Gauge::ImportMode::NeverImport);
Stats::Gauge& pressure_gauge2 =
stats_.gauge("overload.envoy.resource_monitors.fake_resource2.pressure",
Stats::Gauge::ImportMode::NeverImport);
const OverloadActionState& action_state =
manager->getThreadLocalOverloadState().getState("envoy.overload_actions.dummy_action");
// Update does not exceed fake_resource1 trigger threshold, no callback expected
factory1_.monitor_->setPressure(0.5);
timer_cb_();
EXPECT_FALSE(is_active);
EXPECT_THAT(action_state, AllOf(Property(&OverloadActionState::isSaturated, false),
Property(&OverloadActionState::value, 0)));
EXPECT_EQ(0, cb_count);
EXPECT_EQ(0, active_gauge.value());
EXPECT_EQ(0, scale_percent_gauge.value());
EXPECT_EQ(50, pressure_gauge1.value());
// Update exceeds fake_resource1 trigger threshold, callback is expected
factory1_.monitor_->setPressure(0.95);
timer_cb_();
EXPECT_TRUE(is_active);
EXPECT_TRUE(action_state.isSaturated());
EXPECT_EQ(1, cb_count);
EXPECT_EQ(1, active_gauge.value());
EXPECT_EQ(100, scale_percent_gauge.value());
EXPECT_EQ(95, pressure_gauge1.value());
// Callback should not be invoked if action state does not change
factory1_.monitor_->setPressure(0.94);
timer_cb_();
EXPECT_TRUE(is_active);
EXPECT_TRUE(action_state.isSaturated());
EXPECT_EQ(1, cb_count);
EXPECT_EQ(94, pressure_gauge1.value());
// The action is already active for fake_resource1 so no callback expected
factory2_.monitor_->setPressure(0.9);
timer_cb_();
EXPECT_TRUE(is_active);
EXPECT_TRUE(action_state.isSaturated());
EXPECT_EQ(1, cb_count);
EXPECT_EQ(90, pressure_gauge2.value());
// The action remains active for fake_resource2 so no callback expected
factory1_.monitor_->setPressure(0.5);
timer_cb_();
EXPECT_TRUE(is_active);
EXPECT_TRUE(action_state.isSaturated());
EXPECT_EQ(1, cb_count);
EXPECT_EQ(50, pressure_gauge1.value());
EXPECT_EQ(90, pressure_gauge2.value());
// Both become inactive so callback is expected
factory2_.monitor_->setPressure(0.3);
timer_cb_();
EXPECT_FALSE(is_active);
EXPECT_THAT(action_state, AllOf(Property(&OverloadActionState::isSaturated, false),
Property(&OverloadActionState::value, 0)));
EXPECT_EQ(2, cb_count);
EXPECT_EQ(30, pressure_gauge2.value());
// Different triggers, both become active, only one callback expected
factory1_.monitor_->setPressure(0.97);
factory2_.monitor_->setPressure(0.96);
timer_cb_();
EXPECT_TRUE(is_active);
EXPECT_TRUE(action_state.isSaturated());
EXPECT_EQ(3, cb_count);
EXPECT_EQ(97, pressure_gauge1.value());
EXPECT_EQ(96, pressure_gauge2.value());
// Different triggers, both become inactive, only one callback expected
factory1_.monitor_->setPressure(0.41);
factory2_.monitor_->setPressure(0.42);
timer_cb_();
EXPECT_FALSE(is_active);
EXPECT_THAT(action_state, AllOf(Property(&OverloadActionState::isSaturated, false),
Property(&OverloadActionState::value, 0)));
EXPECT_EQ(4, cb_count);
EXPECT_EQ(41, pressure_gauge1.value());
EXPECT_EQ(42, pressure_gauge2.value());
manager->stop();
}
TEST_F(OverloadManagerImplTest, ScaledTrigger) {
setDispatcherExpectation();
auto manager(createOverloadManager(getConfig()));
manager->start();
const auto& action_state =
manager->getThreadLocalOverloadState().getState("envoy.overload_actions.dummy_action");
Stats::Gauge& active_gauge = stats_.gauge("overload.envoy.overload_actions.dummy_action.active",
Stats::Gauge::ImportMode::Accumulate);
Stats::Gauge& scale_percent_gauge =
stats_.gauge("overload.envoy.overload_actions.dummy_action.scale_percent",
Stats::Gauge::ImportMode::Accumulate);
factory3_.monitor_->setPressure(0.5);
timer_cb_();
EXPECT_THAT(action_state, AllOf(Property(&OverloadActionState::isSaturated, false),
Property(&OverloadActionState::value, 0)));
EXPECT_EQ(0, active_gauge.value());
EXPECT_EQ(0, scale_percent_gauge.value());
// The trigger for fake_resource3 is a scaled trigger with a min of 0.5 and a max of 0.8. Set the
// current pressure value to halfway in that range.
factory3_.monitor_->setPressure(0.65);
timer_cb_();
EXPECT_EQ(action_state.value(), 0.5 /* = 0.65 / (0.8 - 0.5) */);
EXPECT_EQ(0, active_gauge.value());
EXPECT_EQ(50, scale_percent_gauge.value());
factory3_.monitor_->setPressure(0.8);
timer_cb_();
EXPECT_TRUE(action_state.isSaturated());
EXPECT_EQ(1, active_gauge.value());
EXPECT_EQ(100, scale_percent_gauge.value());
factory3_.monitor_->setPressure(0.9);
timer_cb_();
EXPECT_TRUE(action_state.isSaturated());
EXPECT_EQ(1, active_gauge.value());
EXPECT_EQ(100, scale_percent_gauge.value());
}
TEST_F(OverloadManagerImplTest, FailedUpdates) {
setDispatcherExpectation();
auto manager(createOverloadManager(getConfig()));
manager->start();
Stats::Counter& failed_updates =
stats_.counter("overload.envoy.resource_monitors.fake_resource1.failed_updates");
factory1_.monitor_->setError();
timer_cb_();
EXPECT_EQ(1, failed_updates.value());
timer_cb_();
EXPECT_EQ(2, failed_updates.value());
manager->stop();
}
TEST_F(OverloadManagerImplTest, AggregatesMultipleResourceUpdates) {
setDispatcherExpectation();
auto manager(createOverloadManager(getConfig()));
manager->start();
const OverloadActionState& action_state =
manager->getThreadLocalOverloadState().getState("envoy.overload_actions.dummy_action");
factory1_.monitor_->setUpdateAsync(true);
// Monitor 2 will respond immediately at the timer callback, but that won't push an update to the
// thread-local state because monitor 1 hasn't finished its update yet.
factory2_.monitor_->setPressure(1.0);
timer_cb_();
EXPECT_FALSE(action_state.isSaturated());
// Once the last monitor publishes, the change to the action takes effect.
factory1_.monitor_->publishUpdate();
EXPECT_TRUE(action_state.isSaturated());
}
TEST_F(OverloadManagerImplTest, DelayedUpdatesAreCoalesced) {
setDispatcherExpectation();
auto manager(createOverloadManager(getConfig()));
manager->start();
const OverloadActionState& action_state =
manager->getThreadLocalOverloadState().getState("envoy.overload_actions.dummy_action");
factory3_.monitor_->setUpdateAsync(true);
factory4_.monitor_->setUpdateAsync(true);
timer_cb_();
// When monitor 3 publishes its update, the action won't be visible to the thread-local state
factory3_.monitor_->setPressure(0.6);
factory3_.monitor_->publishUpdate();
EXPECT_EQ(action_state.value(), 0.0);
// Now when monitor 4 publishes a larger value, the update from monitor 3 is skipped.
EXPECT_FALSE(action_state.isSaturated());
factory4_.monitor_->setPressure(0.65);
factory4_.monitor_->publishUpdate();
EXPECT_EQ(action_state.value(), 0.5 /* = (0.65 - 0.5) / (0.8 - 0.5) */);
}
TEST_F(OverloadManagerImplTest, FlushesUpdatesEvenWithOneUnresponsive) {
setDispatcherExpectation();
auto manager(createOverloadManager(getConfig()));
manager->start();
const OverloadActionState& action_state =
manager->getThreadLocalOverloadState().getState("envoy.overload_actions.dummy_action");
// Set monitor 1 to async, but never publish updates for it.
factory1_.monitor_->setUpdateAsync(true);
// Monitor 2 will respond immediately at the timer callback, but that won't push an update to the
// thread-local state because monitor 1 hasn't finished its update yet.
factory2_.monitor_->setPressure(1.0);
timer_cb_();
EXPECT_FALSE(action_state.isSaturated());
// A second timer callback will flush the update from monitor 2, even though monitor 1 is
// unresponsive.
timer_cb_();
EXPECT_TRUE(action_state.isSaturated());
}
TEST_F(OverloadManagerImplTest, SkippedUpdates) {
setDispatcherExpectation();
auto manager(createOverloadManager(getConfig()));
manager->start();
Stats::Counter& skipped_updates =
stats_.counter("overload.envoy.resource_monitors.fake_resource1.skipped_updates");
Stats::Gauge& pressure_gauge1 =
stats_.gauge("overload.envoy.resource_monitors.fake_resource1.pressure",
Stats::Gauge::ImportMode::NeverImport);
factory1_.monitor_->setUpdateAsync(true);
EXPECT_EQ(0, pressure_gauge1.value());
factory1_.monitor_->setPressure(0.3);
timer_cb_();
EXPECT_EQ(0, skipped_updates.value());
timer_cb_();
EXPECT_EQ(1, skipped_updates.value());
timer_cb_();
EXPECT_EQ(2, skipped_updates.value());
factory1_.monitor_->publishUpdate();
EXPECT_EQ(30, pressure_gauge1.value());
timer_cb_();
EXPECT_EQ(2, skipped_updates.value());
manager->stop();
}
TEST_F(OverloadManagerImplTest, DuplicateResourceMonitor) {
const std::string config = R"EOF(
resource_monitors {
name: "envoy.resource_monitors.fake_resource1"
}
resource_monitors {
name: "envoy.resource_monitors.fake_resource1"
}
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"Duplicate resource monitor .*");
}
TEST_F(OverloadManagerImplTest, DuplicateOverloadAction) {
const std::string config = R"EOF(
actions {
name: "envoy.overload_actions.dummy_action"
}
actions {
name: "envoy.overload_actions.dummy_action"
}
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"Duplicate overload action .*");
}
// A scaled trigger action's thresholds must conform to scaling < saturation.
TEST_F(OverloadManagerImplTest, ScaledTriggerSaturationLessThanScalingThreshold) {
const std::string config = R"EOF(
resource_monitors {
name: "envoy.resource_monitors.fake_resource1"
}
actions {
name: "envoy.overload_actions.dummy_action"
triggers {
name: "envoy.resource_monitors.fake_resource1"
scaled {
scaling_threshold: 0.9
saturation_threshold: 0.8
}
}
}
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"scaling_threshold must be less than saturation_threshold.*");
}
// A scaled trigger action can't have threshold values that are equal.
TEST_F(OverloadManagerImplTest, ScaledTriggerThresholdsEqual) {
const std::string config = R"EOF(
resource_monitors {
name: "envoy.resource_monitors.fake_resource1"
}
actions {
name: "envoy.overload_actions.dummy_action"
triggers {
name: "envoy.resource_monitors.fake_resource1"
scaled {
scaling_threshold: 0.9
saturation_threshold: 0.9
}
}
}
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"scaling_threshold must be less than saturation_threshold.*");
}
TEST_F(OverloadManagerImplTest, UnknownTrigger) {
const std::string config = R"EOF(
actions {
name: "envoy.overload_actions.dummy_action"
triggers {
name: "envoy.resource_monitors.fake_resource1"
threshold {
value: 0.9
}
}
}
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"Unknown trigger resource .*");
}
TEST_F(OverloadManagerImplTest, DuplicateTrigger) {
const std::string config = R"EOF(
resource_monitors {
name: "envoy.resource_monitors.fake_resource1"
}
actions {
name: "envoy.overload_actions.dummy_action"
triggers {
name: "envoy.resource_monitors.fake_resource1"
threshold {
value: 0.9
}
}
triggers {
name: "envoy.resource_monitors.fake_resource1"
threshold {
value: 0.8
}
}
}
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException, "Duplicate trigger .*");
}
TEST_F(OverloadManagerImplTest, Shutdown) {
setDispatcherExpectation();
auto manager(createOverloadManager(getConfig()));
manager->start();
EXPECT_CALL(*timer_, disableTimer());
manager->stop();
}
} // namespace
} // namespace Server
} // namespace Envoy