forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overload_manager_impl_test.cc
846 lines (714 loc) · 30.7 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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include <chrono>
#include <initializer_list>
#include "envoy/config/overload/v3/overload.pb.h"
#include "envoy/event/scaled_range_timer_manager.h"
#include "envoy/server/overload/overload_manager.h"
#include "envoy/server/overload/thread_local_overload_state.h"
#include "envoy/server/resource_monitor.h"
#include "envoy/server/resource_monitor_config.h"
#include "source/common/stats/isolated_store_impl.h"
#include "source/extensions/resource_monitors/common/factory_base.h"
#include "source/server/overload_manager_impl.h"
#include "test/common/stats/stat_test_utility.h"
#include "test/mocks/event/mocks.h"
#include "test/mocks/protobuf/mocks.h"
#include "test/mocks/server/options.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/registry.h"
#include "test/test_common/test_runtime.h"
#include "test/test_common/utility.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::_;
using testing::AllOf;
using testing::AnyNumber;
using testing::ByMove;
using testing::DoAll;
using testing::FloatNear;
using testing::Invoke;
using testing::InvokeArgument;
using testing::NiceMock;
using testing::Pointee;
using testing::Property;
using testing::Return;
using testing::SaveArg;
using testing::UnorderedElementsAreArray;
namespace Envoy {
namespace Server {
namespace {
using TimerType = Event::ScaledTimerType;
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(ResourceUpdateCallbacks& 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(ResourceUpdateCallbacks& 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<ResourceUpdateCallbacks>> callbacks_;
};
class FakeProactiveResourceMonitor : public ProactiveResourceMonitor {
public:
FakeProactiveResourceMonitor(uint64_t max) : max_(max), current_(0){};
bool tryAllocateResource(int64_t increment) override {
int64_t new_val = (current_ += increment);
if (new_val > static_cast<int64_t>(max_) || new_val < 0) {
current_ -= increment;
return false;
}
return true;
}
bool tryDeallocateResource(int64_t decrement) override {
RELEASE_ASSERT(decrement <= current_,
"Cannot deallocate resource, current resource usage is lower than decrement");
int64_t new_val = (current_ -= decrement);
if (new_val < 0) {
current_ += decrement;
return false;
}
return true;
}
int64_t currentResourceUsage() const override { return current_.load(); }
int64_t maxResourceUsage() const override { return max_; }
private:
int64_t max_;
std::atomic<int64_t> current_;
};
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.mainThreadDispatcher());
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_;
};
template <class ConfigType>
class FakeProactiveResourceMonitorFactory
: public Server::Configuration::ProactiveResourceMonitorFactory {
public:
FakeProactiveResourceMonitorFactory(const std::string& name) : monitor_(nullptr), name_(name) {}
Server::ProactiveResourceMonitorPtr
createProactiveResourceMonitor(const Protobuf::Message&,
Server::Configuration::ResourceMonitorFactoryContext&) override {
auto monitor = std::make_unique<FakeProactiveResourceMonitor>(3);
monitor_ = monitor.get();
return monitor;
}
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return ProtobufTypes::MessagePtr{new ConfigType()};
}
std::string name() const override { return name_; }
FakeProactiveResourceMonitor* monitor_; // not owned
const std::string name_;
};
class TestOverloadManager : public OverloadManagerImpl {
public:
TestOverloadManager(Event::Dispatcher& dispatcher, Stats::Scope& stats_scope,
ThreadLocal::SlotAllocator& slot_allocator,
const envoy::config::overload::v3::OverloadManager& config,
ProtobufMessage::ValidationVisitor& validation_visitor, Api::Api& api,
const Server::MockOptions& options)
: OverloadManagerImpl(dispatcher, stats_scope, slot_allocator, config, validation_visitor,
api, options) {
EXPECT_CALL(*this, createScaledRangeTimerManager)
.Times(AnyNumber())
.WillRepeatedly(Invoke(this, &TestOverloadManager::createDefaultScaledRangeTimerManager));
}
MOCK_METHOD(Event::ScaledRangeTimerManagerPtr, createScaledRangeTimerManager,
(Event::Dispatcher&, const Event::ScaledTimerTypeMapConstSharedPtr&),
(const, override));
private:
Event::ScaledRangeTimerManagerPtr createDefaultScaledRangeTimerManager(
Event::Dispatcher& dispatcher,
const Event::ScaledTimerTypeMapConstSharedPtr& timer_minimums) const {
return OverloadManagerImpl::createScaledRangeTimerManager(dispatcher, timer_minimums);
}
};
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"),
factory5_("envoy.resource_monitors.global_downstream_max_connections"),
register_factory1_(factory1_), register_factory2_(factory2_), register_factory3_(factory3_),
register_factory4_(factory4_), register_factory5_(factory5_),
api_(Api::createApiForTest(stats_)) {
scoped_runtime_.mergeValues(
{{"envoy.reloadable_features.no_extension_lookup_by_name", "false"}});
}
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;
TestUtility::loadFromYaml(config, proto);
return proto;
}
std::unique_ptr<TestOverloadManager> createOverloadManager(const std::string& config) {
return std::make_unique<TestOverloadManager>(dispatcher_, stats_, thread_local_,
parseConfig(config), validation_visitor_, *api_,
options_);
}
FakeResourceMonitorFactory<Envoy::ProtobufWkt::Struct> factory1_;
FakeResourceMonitorFactory<Envoy::ProtobufWkt::Timestamp> factory2_;
FakeResourceMonitorFactory<Envoy::ProtobufWkt::Duration> factory3_;
FakeResourceMonitorFactory<Envoy::ProtobufWkt::StringValue> factory4_;
FakeProactiveResourceMonitorFactory<Envoy::ProtobufWkt::Timestamp> factory5_;
Registry::InjectFactory<Configuration::ResourceMonitorFactory> register_factory1_;
Registry::InjectFactory<Configuration::ResourceMonitorFactory> register_factory2_;
Registry::InjectFactory<Configuration::ResourceMonitorFactory> register_factory3_;
Registry::InjectFactory<Configuration::ResourceMonitorFactory> register_factory4_;
Registry::InjectFactory<Configuration::ProactiveResourceMonitorFactory> register_factory5_;
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_;
Server::MockOptions options_;
TestScopedRuntime scoped_runtime_;
};
constexpr char kRegularStateConfig[] = R"YAML(
refresh_interval:
seconds: 1
resource_monitors:
- name: envoy.resource_monitors.fake_resource1
- name: envoy.resource_monitors.fake_resource2
- name: envoy.resource_monitors.fake_resource3
- name: envoy.resource_monitors.fake_resource4
actions:
- name: envoy.overload_actions.dummy_action
triggers:
- name: envoy.resource_monitors.fake_resource1
threshold:
value: 0.9
- name: envoy.resource_monitors.fake_resource2
threshold:
value: 0.8
- name: envoy.resource_monitors.fake_resource3
scaled:
scaling_threshold: 0.5
saturation_threshold: 0.8
- name: envoy.resource_monitors.fake_resource4
scaled:
scaling_threshold: 0.5
saturation_threshold: 0.8
)YAML";
constexpr char proactiveResourceConfig[] = R"YAML(
refresh_interval:
seconds: 1
resource_monitors:
- name: envoy.resource_monitors.fake_resource1
- name: envoy.resource_monitors.global_downstream_max_connections
actions:
- name: envoy.overload_actions.dummy_action
triggers:
- name: envoy.resource_monitors.fake_resource1
threshold:
value: 0.9
)YAML";
TEST_F(OverloadManagerImplTest, CallbackOnlyFiresWhenStateChanges) {
setDispatcherExpectation();
auto manager(createOverloadManager(kRegularStateConfig));
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();
EXPECT_FALSE(manager->getThreadLocalOverloadState().isResourceMonitorEnabled(
OverloadProactiveResourceName::GlobalDownstreamMaxConnections));
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, UnitFloat::min())));
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, UnitFloat::min())));
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, UnitFloat::min())));
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(kRegularStateConfig));
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, UnitFloat::min())));
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(), UnitFloat(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(kRegularStateConfig));
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(kRegularStateConfig));
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(kRegularStateConfig));
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(), UnitFloat::min());
// 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(), UnitFloat(0.5) /* = (0.65 - 0.5) / (0.8 - 0.5) */);
}
TEST_F(OverloadManagerImplTest, FlushesUpdatesEvenWithOneUnresponsive) {
setDispatcherExpectation();
auto manager(createOverloadManager(kRegularStateConfig));
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(kRegularStateConfig));
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();
}
constexpr char kReducedTimeoutsConfig[] = R"YAML(
refresh_interval:
seconds: 1
resource_monitors:
- name: envoy.resource_monitors.fake_resource1
actions:
- name: envoy.overload_actions.reduce_timeouts
typed_config:
"@type": type.googleapis.com/envoy.config.overload.v3.ScaleTimersOverloadActionConfig
timer_scale_factors:
- timer: HTTP_DOWNSTREAM_CONNECTION_IDLE
min_timeout: 2s
- timer: HTTP_DOWNSTREAM_STREAM_IDLE
min_scale: { value: 10 } # percent
- timer: TRANSPORT_SOCKET_CONNECT
min_scale: { value: 40 } # percent
triggers:
- name: "envoy.resource_monitors.fake_resource1"
scaled:
scaling_threshold: 0.5
saturation_threshold: 1.0
)YAML";
// These are the timer types according to the reduced timeouts config above.
constexpr std::pair<TimerType, Event::ScaledTimerMinimum> kReducedTimeoutsMinimums[]{
{TimerType::HttpDownstreamIdleConnectionTimeout,
Event::AbsoluteMinimum(std::chrono::seconds(2))},
{TimerType::HttpDownstreamIdleStreamTimeout, Event::ScaledMinimum(UnitFloat(0.1))},
{TimerType::TransportSocketConnectTimeout, Event::ScaledMinimum(UnitFloat(0.4))},
};
TEST_F(OverloadManagerImplTest, CreateScaledTimerManager) {
auto manager(createOverloadManager(kReducedTimeoutsConfig));
auto* mock_scaled_timer_manager = new Event::MockScaledRangeTimerManager();
Event::ScaledTimerTypeMapConstSharedPtr timer_minimums;
EXPECT_CALL(*manager, createScaledRangeTimerManager)
.WillOnce(
DoAll(SaveArg<1>(&timer_minimums),
Return(ByMove(Event::ScaledRangeTimerManagerPtr{mock_scaled_timer_manager}))));
Event::MockDispatcher mock_dispatcher;
auto scaled_timer_manager = manager->scaledTimerFactory()(mock_dispatcher);
EXPECT_EQ(scaled_timer_manager.get(), mock_scaled_timer_manager);
EXPECT_THAT(timer_minimums, Pointee(UnorderedElementsAreArray(kReducedTimeoutsMinimums)));
}
TEST_F(OverloadManagerImplTest, AdjustScaleFactor) {
setDispatcherExpectation();
auto manager(createOverloadManager(kReducedTimeoutsConfig));
auto* mock_scaled_timer_manager = new Event::MockScaledRangeTimerManager();
EXPECT_CALL(*manager, createScaledRangeTimerManager)
.WillOnce(Return(ByMove(Event::ScaledRangeTimerManagerPtr{mock_scaled_timer_manager})));
Event::MockDispatcher mock_dispatcher;
auto scaled_timer_manager = manager->scaledTimerFactory()(mock_dispatcher);
manager->start();
EXPECT_CALL(mock_dispatcher, post).WillOnce(InvokeArgument<0>());
// The scaled trigger has range [0.5, 1.0] so 0.6 should map to a scale value of 0.2, which means
// a timer scale factor of 0.8 (1 - 0.2).
EXPECT_CALL(*mock_scaled_timer_manager,
setScaleFactor(Property(&UnitFloat::value, FloatNear(0.8, 0.00001))));
factory1_.monitor_->setPressure(0.6);
timer_cb_();
}
TEST_F(OverloadManagerImplTest, DuplicateResourceMonitor) {
const std::string config = R"EOF(
resource_monitors:
- name: "envoy.resource_monitors.fake_resource1"
- name: "envoy.resource_monitors.fake_resource1"
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"Duplicate resource monitor .*");
}
TEST_F(OverloadManagerImplTest, DuplicateProactiveResourceMonitor) {
const std::string config = R"EOF(
resource_monitors:
- name: "envoy.resource_monitors.global_downstream_max_connections"
- name: "envoy.resource_monitors.global_downstream_max_connections"
)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"
- name: "envoy.overload_actions.dummy_action"
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"Duplicate overload action .*");
}
TEST_F(OverloadManagerImplTest, ActionWithUnexpectedTypedConfig) {
const std::string config = R"EOF(
actions:
- name: "envoy.overload_actions.dummy_action"
typed_config:
"@type": type.googleapis.com/google.protobuf.Empty
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
".* unexpected .* typed_config .*");
}
TEST_F(OverloadManagerImplTest, ReduceTimeoutsWithoutAction) {
const std::string config = R"EOF(
actions:
- name: "envoy.overload_actions.reduce_timeouts"
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"Unable to unpack as .*ScaleTimersOverloadActionConfig");
}
TEST_F(OverloadManagerImplTest, ReduceTimeoutsWithWrongTypedConfigMessage) {
const std::string config = R"EOF(
actions:
- name: "envoy.overload_actions.reduce_timeouts"
typed_config:
"@type": type.googleapis.com/google.protobuf.Empty
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
"Unable to unpack as .*ScaleTimersOverloadActionConfig");
}
TEST_F(OverloadManagerImplTest, ReduceTimeoutsWithNoTimersSpecified) {
const std::string config = R"EOF(
actions:
- name: "envoy.overload_actions.reduce_timeouts"
typed_config:
"@type": type.googleapis.com/envoy.config.overload.v3.ScaleTimersOverloadActionConfig
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException,
".* constraint validation failed.*");
}
// 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
- name: "envoy.resource_monitors.fake_resource1"
threshold:
value: 0.8
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(config), EnvoyException, "Duplicate trigger .*");
}
TEST_F(OverloadManagerImplTest, ShouldThrowIfUsingResetStreamsWithoutBufferFactoryConfig) {
const std::string lower_greater_than_upper_config = R"EOF(
actions:
- name: envoy.overload_actions.reset_high_memory_stream
)EOF";
EXPECT_THROW_WITH_REGEX(createOverloadManager(lower_greater_than_upper_config), EnvoyException,
"Overload action .* requires buffer_factory_config.");
}
TEST_F(OverloadManagerImplTest, Shutdown) {
setDispatcherExpectation();
auto manager(createOverloadManager(kRegularStateConfig));
manager->start();
EXPECT_CALL(*timer_, disableTimer());
manager->stop();
}
TEST_F(OverloadManagerImplTest, MissingConfigTriggerType) {
constexpr char missingTriggerTypeConfig[] = R"YAML(
actions:
- name: envoy.overload_actions.dummy_action
triggers:
- name: envoy.resource_monitors.fake_resource1
)YAML";
EXPECT_THROW_WITH_REGEX(createOverloadManager(missingTriggerTypeConfig), EnvoyException,
"action not set for trigger.*");
}
TEST_F(OverloadManagerImplTest, ProactiveResourceAllocateAndDeallocateResourceTest) {
setDispatcherExpectation();
auto manager(createOverloadManager(proactiveResourceConfig));
Stats::Counter& failed_updates =
stats_.counter("overload.envoy.resource_monitors.global_downstream_max_connections."
"failed_updates");
manager->start();
EXPECT_TRUE(manager->getThreadLocalOverloadState().isResourceMonitorEnabled(
OverloadProactiveResourceName::GlobalDownstreamMaxConnections));
bool resource_allocated = manager->getThreadLocalOverloadState().tryAllocateResource(
Server::OverloadProactiveResourceName::GlobalDownstreamMaxConnections, 1);
EXPECT_TRUE(resource_allocated);
resource_allocated = manager->getThreadLocalOverloadState().tryAllocateResource(
Server::OverloadProactiveResourceName::GlobalDownstreamMaxConnections, 3);
EXPECT_FALSE(resource_allocated);
EXPECT_EQ(1, failed_updates.value());
bool resource_deallocated = manager->getThreadLocalOverloadState().tryDeallocateResource(
Server::OverloadProactiveResourceName::GlobalDownstreamMaxConnections, 1);
EXPECT_TRUE(resource_deallocated);
EXPECT_DEATH(manager->getThreadLocalOverloadState().tryDeallocateResource(
Server::OverloadProactiveResourceName::GlobalDownstreamMaxConnections, 1),
".*Cannot deallocate resource, current resource usage is lower than decrement.*");
manager->stop();
}
} // namespace
} // namespace Server
} // namespace Envoy