forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guarddog_impl_test.cc
821 lines (705 loc) · 32.3 KB
/
guarddog_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
#include <atomic>
#include <chrono>
#include <csignal>
#include <memory>
#include <vector>
#include "envoy/common/time.h"
#include "envoy/server/configuration.h"
#include "envoy/server/guarddog_config.h"
#include "envoy/server/watchdog.h"
#include "envoy/thread/thread.h"
#include "source/common/api/api_impl.h"
#include "source/common/common/macros.h"
#include "source/common/common/utility.h"
#include "source/common/protobuf/utility.h"
#include "source/server/guarddog_impl.h"
#include "test/mocks/common.h"
#include "test/mocks/event/mocks.h"
#include "test/mocks/server/watchdog_config.h"
#include "test/mocks/stats/mocks.h"
#include "test/server/guarddog_test_interlock.h"
#include "test/test_common/registry.h"
#include "test/test_common/simulated_time_system.h"
#include "test/test_common/test_time.h"
#include "test/test_common/utility.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::ElementsAre;
using testing::InSequence;
using testing::NiceMock;
namespace Envoy {
namespace Server {
namespace {
// Kill has an explicit value that disables the feature.
const int DISABLE_KILL = 0;
const int DISABLE_MULTIKILL = 0;
// Miss / Megamiss don't have an explicit value that disables them
// so set a timeout larger than those used in tests for 'disable' it.
const int DISABLE_MISS = 1000000;
const int DISABLE_MEGAMISS = 1000000;
// We want to make sure guard-dog is tested with both simulated time and real
// time, to ensure that it works in production, and that it works in the context
// of integration tests which are much easier to control with simulated time.
enum class TimeSystemType { Real, Simulated };
class GuardDogTestBase : public testing::TestWithParam<TimeSystemType> {
protected:
GuardDogTestBase()
: time_system_(makeTimeSystem()), api_(Api::createApiForTest(stats_store_, *time_system_)) {}
static std::unique_ptr<Event::TestTimeSystem> makeTimeSystem() {
if (GetParam() == TimeSystemType::Real) {
return std::make_unique<Event::GlobalTimeSystem>();
}
ASSERT(GetParam() == TimeSystemType::Simulated);
return std::make_unique<Event::SimulatedTimeSystem>();
}
void initGuardDog(Stats::Store& stats_store, const Server::Configuration::Watchdog& config) {
guard_dog_ = std::make_unique<GuardDogImpl>(*stats_store.rootScope(), config, *api_, "server",
std::make_unique<GuardDogTestInterlock>());
}
std::unique_ptr<Event::TestTimeSystem> time_system_;
Stats::TestUtil::TestStore stats_store_;
Api::ApiPtr api_;
NiceMock<Event::MockDispatcher> mock_dispatcher_;
std::unique_ptr<GuardDogImpl> guard_dog_;
};
INSTANTIATE_TEST_SUITE_P(TimeSystemType, GuardDogTestBase,
testing::ValuesIn({TimeSystemType::Real, TimeSystemType::Simulated}));
/**
* Death test caveat: Because of the way we die gcov doesn't receive coverage
* information from the forked process that is checked for successful death.
* This means that the lines dealing with the calls to PANIC are not seen as
* green in the coverage report. However, rest assured from the results of the
* test: these lines are in fact covered.
*/
class GuardDogDeathTest : public GuardDogTestBase {
protected:
GuardDogDeathTest()
: config_kill_(1000, 1000, 100, 1000, 0, std::vector<std::string>{}),
config_multikill_(1000, 1000, 1000, 500, 0, std::vector<std::string>{}),
config_multikill_threshold_(1000, 1000, 1000, 500, 60, std::vector<std::string>{}) {}
/**
* This does everything but the final forceCheckForTest() that should cause
* death for the single kill case.
*/
void setupForDeath() {
InSequence s;
initGuardDog(fakestats_, config_kill_);
unpet_dog_ = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(), "test_thread",
mock_dispatcher_);
dogs_.emplace_back(unpet_dog_);
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(99)); // 1 ms shy of death.
}
/**
* This does everything but the final forceCheckForTest() that should cause
* death for the multiple kill case.
*/
void setupForMultiDeath() {
InSequence s;
initGuardDog(fakestats_, config_multikill_);
auto unpet_dog_ = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(),
"test_thread", mock_dispatcher_);
dogs_.emplace_back(unpet_dog_);
guard_dog_->forceCheckForTest();
auto second_dog_ = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(),
"test_thread", mock_dispatcher_);
dogs_.emplace_back(second_dog_);
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(499)); // 1 ms shy of multi-death.
}
/**
* This does everything but the final forceCheckForTest() that should cause
* death for the multiple kill case using threshold (100% of watchdogs over the threshold).
*/
void setupForMultiDeathThreshold() {
InSequence s;
initGuardDog(fakestats_, config_multikill_threshold_);
// Creates 5 watchdogs.
for (int i = 0; i < 5; ++i) {
auto dog = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(), "test_thread",
mock_dispatcher_);
dogs_.emplace_back(dog);
if (i == 0) {
unpet_dog_ = dog;
} else if (i == 1) {
second_dog_ = dog;
}
guard_dog_->forceCheckForTest();
}
time_system_->advanceTimeWait(std::chrono::milliseconds(499)); // 1 ms shy of multi-death.
}
NiceMock<Configuration::MockWatchdog> config_kill_;
NiceMock<Configuration::MockWatchdog> config_multikill_;
NiceMock<Configuration::MockWatchdog> config_multikill_threshold_;
NiceMock<Stats::MockStore> fakestats_;
WatchDogSharedPtr unpet_dog_;
WatchDogSharedPtr second_dog_;
std::vector<WatchDogSharedPtr> dogs_; // Tracks all watchdogs created.
};
INSTANTIATE_TEST_SUITE_P(TimeSystemType, GuardDogDeathTest,
testing::ValuesIn({TimeSystemType::Real, TimeSystemType::Simulated}));
// These tests use threads, and need to run after the real death tests, so we need to call them
// a different name.
class GuardDogAlmostDeadTest : public GuardDogDeathTest {};
INSTANTIATE_TEST_SUITE_P(
TimeSystemType, GuardDogAlmostDeadTest,
testing::ValuesIn({// TODO(#6465): TimeSystemType::Real -- fails in this suite 30/1000 times.
TimeSystemType::Simulated}));
TEST_P(GuardDogDeathTest, KillDeathTest) {
// Is it German for "The Function"? Almost...
auto die_function = [&]() -> void {
setupForDeath();
time_system_->advanceTimeWait(std::chrono::milliseconds(401)); // 400 ms past death.
guard_dog_->forceCheckForTest();
};
// Why do it this way? Any threads must be started inside the death test
// statement and this is the easiest way to accomplish that.
EXPECT_DEATH(die_function(), "");
}
TEST_P(GuardDogAlmostDeadTest, KillNoFinalCheckTest) {
// This does everything the death test does, except allow enough time to
// expire to reach the death panic. The death test does not verify that there
// was not a crash *before* the expected line, so this test checks that.
setupForDeath();
}
TEST_P(GuardDogDeathTest, MultiKillDeathTest) {
auto die_function = [&]() -> void {
setupForMultiDeath();
time_system_->advanceTimeWait(std::chrono::milliseconds(2)); // 1 ms past multi-death.
guard_dog_->forceCheckForTest();
};
EXPECT_DEATH(die_function(), "Watchdog MULTIKILL as 2 threads are stuck");
}
TEST_P(GuardDogAlmostDeadTest, MultiKillNoFinalCheckTest) {
// This does everything the death test does not except the final force check that
// should actually result in dying. The death test does not verify that there
// was not a crash *before* the expected line, so this test checks that.
setupForMultiDeath();
}
TEST_P(GuardDogDeathTest, MultiKillThresholdDeathTest) {
auto die_function = [&]() -> void {
setupForMultiDeathThreshold();
// Pet the last two dogs so we're just at the threshold that causes death.
dogs_.at(4)->touch();
dogs_.at(3)->touch();
time_system_->advanceTimeWait(std::chrono::milliseconds(2)); // 1 ms past multi-death.
guard_dog_->forceCheckForTest();
};
EXPECT_DEATH(die_function(), "");
}
TEST_P(GuardDogAlmostDeadTest, MultiKillUnderThreshold) {
// This does everything the death test does except it pets an additional watchdog
// that causes us to be under the threshold (60%) of multikill death.
setupForMultiDeathThreshold();
// Pet the last three dogs so we're just under the threshold that causes death.
dogs_.at(4)->touch();
dogs_.at(3)->touch();
dogs_.at(2)->touch();
time_system_->advanceTimeWait(std::chrono::milliseconds(2)); // 1 ms past multi-death.
guard_dog_->forceCheckForTest();
}
TEST_P(GuardDogAlmostDeadTest, NearDeathTest) {
// This ensures that if only one thread surpasses the multiple kill threshold
// there is no death. The positive case is covered in MultiKillDeathTest.
InSequence s;
initGuardDog(fakestats_, config_multikill_);
auto unpet_dog = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(),
"test_thread", mock_dispatcher_);
auto pet_dog = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(), "test_thread",
mock_dispatcher_);
// This part "waits" 600 milliseconds while one dog is touched every 100, and
// the other is not. 600ms is over the threshold of 500ms for multi-kill but
// only one is nonresponsive, so there should be no kill (single kill
// threshold of 1s is not reached).
for (int i = 0; i < 6; i++) {
time_system_->advanceTimeWait(std::chrono::milliseconds(100));
pet_dog->touch();
guard_dog_->forceCheckForTest();
}
}
class GuardDogMissTest : public GuardDogTestBase {
protected:
GuardDogMissTest()
: config_miss_(500, 1000, 0, 0, 0, std::vector<std::string>{}),
config_mega_(1000, 500, 0, 0, 0, std::vector<std::string>{}) {}
void checkMiss(uint64_t count, const std::string& descriptor) {
EXPECT_EQ(count, TestUtility::findCounter(stats_store_, "server.watchdog_miss")->value())
<< descriptor;
EXPECT_EQ(count,
TestUtility::findCounter(stats_store_, "server.test_thread.watchdog_miss")->value())
<< descriptor;
}
void checkMegaMiss(uint64_t count, const std::string& descriptor) {
EXPECT_EQ(count, TestUtility::findCounter(stats_store_, "server.watchdog_mega_miss")->value())
<< descriptor;
EXPECT_EQ(
count,
TestUtility::findCounter(stats_store_, "server.test_thread.watchdog_mega_miss")->value())
<< descriptor;
}
NiceMock<Configuration::MockWatchdog> config_miss_;
NiceMock<Configuration::MockWatchdog> config_mega_;
};
INSTANTIATE_TEST_SUITE_P(TimeSystemType, GuardDogMissTest,
testing::ValuesIn({TimeSystemType::Real, TimeSystemType::Simulated}));
TEST_P(GuardDogMissTest, MissTest) {
// This test checks the actual collected statistics after doing some timer
// advances that should and shouldn't increment the counters.
initGuardDog(stats_store_, config_miss_);
auto unpet_dog = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(),
"test_thread", mock_dispatcher_);
guard_dog_->forceCheckForTest();
// We'd better start at 0:
checkMiss(0, "MissTest check 1");
// At 300ms we shouldn't have hit the timeout yet:
time_system_->advanceTimeWait(std::chrono::milliseconds(300));
guard_dog_->forceCheckForTest();
checkMiss(0, "MissTest check 2");
// This should push it past the 500ms limit:
time_system_->advanceTimeWait(std::chrono::milliseconds(250));
guard_dog_->forceCheckForTest();
checkMiss(1, "MissTest check 3");
guard_dog_->stopWatching(unpet_dog);
unpet_dog = nullptr;
}
TEST_P(GuardDogMissTest, MegaMissTest) {
// TODO(#6465): This test fails in real-time 1/1000 times, but passes in simulated time.
if (GetParam() == TimeSystemType::Real) {
return;
}
// This test checks the actual collected statistics after doing some timer
// advances that should and shouldn't increment the counters.
initGuardDog(stats_store_, config_mega_);
auto unpet_dog = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(),
"test_thread", mock_dispatcher_);
guard_dog_->forceCheckForTest();
// We'd better start at 0:
checkMegaMiss(0, "MegaMissTest check 1");
// This shouldn't be enough to increment the stat:
time_system_->advanceTimeWait(std::chrono::milliseconds(499));
guard_dog_->forceCheckForTest();
checkMegaMiss(0, "MegaMissTest check 2");
// Just 2ms more will make it greater than 500ms timeout:
time_system_->advanceTimeWait(std::chrono::milliseconds(2));
guard_dog_->forceCheckForTest();
checkMegaMiss(1, "MegaMissTest check 3");
guard_dog_->stopWatching(unpet_dog);
unpet_dog = nullptr;
}
TEST_P(GuardDogMissTest, MissCountTest) {
// TODO(#6465): This test fails in real-time 9/1000 times, but passes in simulated time.
if (GetParam() == TimeSystemType::Real) {
return;
}
// This tests a flake discovered in the MissTest where real timeout or
// spurious condition_variable wakeup causes the counter to get incremented
// more than it should be.
initGuardDog(stats_store_, config_miss_);
auto sometimes_pet_dog = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(),
"test_thread", mock_dispatcher_);
guard_dog_->forceCheckForTest();
// These steps are executed once without ever touching the watchdog.
// Then the last step is to touch the watchdog and repeat the steps.
// This verifies that the behavior is reset back to baseline after a touch.
for (unsigned long i = 0; i < 2; i++) {
EXPECT_EQ(i, stats_store_.counter("server.watchdog_miss").value());
// This shouldn't be enough to increment the stat:
time_system_->advanceTimeWait(std::chrono::milliseconds(499));
guard_dog_->forceCheckForTest();
checkMiss(i, "MissCountTest check 1");
// And if we force re-execution of the loop it still shouldn't be:
guard_dog_->forceCheckForTest();
checkMiss(i, "MissCountTest check 2");
// Just 2ms more will make it greater than 500ms timeout:
time_system_->advanceTimeWait(std::chrono::milliseconds(2));
guard_dog_->forceCheckForTest();
checkMiss(i + 1, "MissCountTest check 3");
// Spurious wakeup, we should still only have one miss counted.
guard_dog_->forceCheckForTest();
checkMiss(i + 1, "MissCountTest check 4");
// When we finally touch the dog we should get one more increment once the
// timeout value expires:
sometimes_pet_dog->touch();
guard_dog_->forceCheckForTest();
}
time_system_->advanceTimeWait(std::chrono::milliseconds(1000));
sometimes_pet_dog->touch();
guard_dog_->forceCheckForTest();
// Make sure megamiss still works:
checkMegaMiss(0UL, "MissCountTest check 5");
time_system_->advanceTimeWait(std::chrono::milliseconds(1500));
guard_dog_->forceCheckForTest();
checkMegaMiss(1UL, "MissCountTest check 6");
guard_dog_->stopWatching(sometimes_pet_dog);
sometimes_pet_dog = nullptr;
}
TEST_P(GuardDogTestBase, StartStopTest) {
NiceMock<Stats::MockStore> stats;
NiceMock<Configuration::MockWatchdog> config(0, 0, 0, 0, 0, std::vector<std::string>{});
initGuardDog(stats, config);
}
TEST_P(GuardDogTestBase, LoopIntervalNoKillTest) {
NiceMock<Stats::MockStore> stats;
NiceMock<Configuration::MockWatchdog> config(40, 50, 0, 0, 0, std::vector<std::string>{});
initGuardDog(stats, config);
EXPECT_EQ(guard_dog_->loopIntervalForTest(), std::chrono::milliseconds(40));
}
TEST_P(GuardDogTestBase, LoopIntervalTest) {
NiceMock<Stats::MockStore> stats;
NiceMock<Configuration::MockWatchdog> config(100, 90, 1000, 500, 0, std::vector<std::string>{});
initGuardDog(stats, config);
EXPECT_EQ(guard_dog_->loopIntervalForTest(), std::chrono::milliseconds(90));
}
TEST_P(GuardDogTestBase, WatchDogThreadIdTest) {
NiceMock<Stats::MockStore> stats;
NiceMock<Configuration::MockWatchdog> config(100, 90, 1000, 500, 0, std::vector<std::string>{});
initGuardDog(stats, config);
auto watched_dog = guard_dog_->createWatchDog(api_->threadFactory().currentThreadId(),
"test_thread", mock_dispatcher_);
EXPECT_EQ(watched_dog->threadId().debugString(),
api_->threadFactory().currentThreadId().debugString());
guard_dog_->stopWatching(watched_dog);
}
// If this test fails it is because the std::chrono::steady_clock::duration type has become
// nontrivial or we are compiling under a compiler and library combo that makes
// std::chrono::steady_clock::duration require a lock to be atomically modified.
//
// The WatchDog/GuardDog relies on this being a lock free atomic for perf reasons so some workaround
// will be required if this test starts failing.
TEST_P(GuardDogTestBase, AtomicIsAtomicTest) {
std::atomic<std::chrono::steady_clock::duration> atomic_time;
ASSERT_EQ(atomic_time.is_lock_free(), true);
}
// A GuardDogAction used for testing the GuardDog.
// It's primary use is dumping string of the format EVENT_TYPE : tid1,.., tidN to
// the events vector passed to it.
// Instances of this class will be registered for GuardDogEvent through
// TestGuardDogActionFactory.
class RecordGuardDogAction : public Configuration::GuardDogAction {
public:
RecordGuardDogAction(std::vector<std::string>& events) : events_(events) {}
void run(envoy::config::bootstrap::v3::Watchdog::WatchdogAction::WatchdogEvent event,
const std::vector<std::pair<Thread::ThreadId, MonotonicTime>>& thread_last_checkin_pairs,
MonotonicTime /*now*/) override {
std::string event_string =
envoy::config::bootstrap::v3::Watchdog::WatchdogAction::WatchdogEvent_Name(event);
absl::StrAppend(&event_string, " : ");
std::vector<std::string> output_string_parts;
output_string_parts.reserve(thread_last_checkin_pairs.size());
for (const auto& thread_ltt_pair : thread_last_checkin_pairs) {
output_string_parts.push_back(thread_ltt_pair.first.debugString());
}
absl::StrAppend(&event_string, absl::StrJoin(output_string_parts, ","));
events_.push_back(event_string);
}
protected:
std::vector<std::string>& events_; // not owned
};
// A GuardDogAction that raises the specified signal.
class AssertGuardDogAction : public Configuration::GuardDogAction {
public:
AssertGuardDogAction() = default;
void
run(envoy::config::bootstrap::v3::Watchdog::WatchdogAction::WatchdogEvent /*event*/,
const std::vector<std::pair<Thread::ThreadId, MonotonicTime>>& /*thread_last_checkin_pairs*/,
MonotonicTime /*now*/) override {
RELEASE_ASSERT(false, "ASSERT_GUARDDOG_ACTION");
}
};
// Test factory for consuming Watchdog configs and creating GuardDogActions.
template <class ConfigType>
class RecordGuardDogActionFactory : public Configuration::GuardDogActionFactory {
public:
RecordGuardDogActionFactory(const std::string& name, std::vector<std::string>& events)
: name_(name), events_(events) {}
Configuration::GuardDogActionPtr createGuardDogActionFromProto(
const envoy::config::bootstrap::v3::Watchdog::WatchdogAction& /*config*/,
Configuration::GuardDogActionFactoryContext& /*context*/) override {
// Return different actions depending on the config.
return std::make_unique<RecordGuardDogAction>(events_);
}
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return ProtobufTypes::MessagePtr{new ConfigType()};
}
std::string name() const override { return name_; }
const std::string name_;
std::vector<std::string>& events_; // not owned
};
// Test factory for consuming Watchdog configs and creating GuardDogActions.
template <class ConfigType>
class AssertGuardDogActionFactory : public Configuration::GuardDogActionFactory {
public:
AssertGuardDogActionFactory(const std::string& name) : name_(name) {}
Configuration::GuardDogActionPtr createGuardDogActionFromProto(
const envoy::config::bootstrap::v3::Watchdog::WatchdogAction& /*config*/,
Configuration::GuardDogActionFactoryContext& /*context*/) override {
// Return different actions depending on the config.
return std::make_unique<AssertGuardDogAction>();
}
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return ProtobufTypes::MessagePtr{new ConfigType()};
}
std::string name() const override { return name_; }
const std::string name_;
};
/**
* Tests that various actions registered for the guard dog get called upon.
*/
class GuardDogActionsTest : public GuardDogTestBase {
protected:
GuardDogActionsTest()
: log_factory_("LogFactory", events_), register_log_factory_(log_factory_),
assert_factory_("AssertFactory"), register_assert_factory_(assert_factory_) {}
std::vector<std::string> getActionsConfig() {
return {
R"EOF(
{
"config": {
"name": "AssertFactory",
"typed_config": {
"@type": "type.googleapis.com/google.protobuf.Empty"
}
},
"event": "MULTIKILL"
}
)EOF",
R"EOF(
{
"config": {
"name": "AssertFactory",
"typed_config": {
"@type": "type.googleapis.com/google.protobuf.Empty"
}
},
"event": "KILL"
}
)EOF",
R"EOF(
{
"config": {
"name": "LogFactory",
"typed_config": {
"@type": "type.googleapis.com/google.protobuf.Struct"
}
},
"event": "MEGAMISS"
}
)EOF",
R"EOF(
{
"config": {
"name": "LogFactory",
"typed_config": {
"@type": "type.googleapis.com/google.protobuf.Struct"
}
},
"event": "MISS"
}
)EOF"};
}
void setupFirstDog(const NiceMock<Configuration::MockWatchdog>& config, Thread::ThreadId tid) {
initGuardDog(fake_stats_, config);
first_dog_ = guard_dog_->createWatchDog(tid, "test_thread", mock_dispatcher_);
guard_dog_->forceCheckForTest();
}
std::vector<std::string> actions_;
std::vector<std::string> events_;
RecordGuardDogActionFactory<Envoy::ProtobufWkt::Struct> log_factory_;
Registry::InjectFactory<Configuration::GuardDogActionFactory> register_log_factory_;
AssertGuardDogActionFactory<Envoy::ProtobufWkt::Empty> assert_factory_;
Registry::InjectFactory<Configuration::GuardDogActionFactory> register_assert_factory_;
NiceMock<Stats::MockStore> fake_stats_;
WatchDogSharedPtr first_dog_;
WatchDogSharedPtr second_dog_;
};
INSTANTIATE_TEST_SUITE_P(TimeSystemType, GuardDogActionsTest,
testing::ValuesIn({TimeSystemType::Real, TimeSystemType::Simulated}));
TEST_P(GuardDogActionsTest, MissShouldOnlyReportRelevantThreads) {
const NiceMock<Configuration::MockWatchdog> config(100, DISABLE_MEGAMISS, DISABLE_KILL,
DISABLE_MULTIKILL, 0, getActionsConfig());
setupFirstDog(config, Thread::ThreadId(10));
second_dog_ = guard_dog_->createWatchDog(Thread::ThreadId(11), "test_thread", mock_dispatcher_);
time_system_->advanceTimeWait(std::chrono::milliseconds(50));
second_dog_->touch();
// This will reset the loop interval timer, and should help us
// synchronize with the guard dog.
guard_dog_->forceCheckForTest();
if (GetParam() == TimeSystemType::Real) {
// Touch the second_dog in case we overslept in the real time system
// and the guard dog timer goes off.
second_dog_->touch();
}
time_system_->advanceTimeWait(std::chrono::milliseconds(51));
if (GetParam() == TimeSystemType::Real) {
// Touch the second_dog in case we overslept in the real time system
// and the prior "touch" was consumed.
second_dog_->touch();
}
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MISS : 10"));
}
TEST_P(GuardDogActionsTest, MissShouldBeAbleToReportMultipleThreads) {
const NiceMock<Configuration::MockWatchdog> config(100, DISABLE_MEGAMISS, DISABLE_KILL,
DISABLE_MULTIKILL, 0, getActionsConfig());
initGuardDog(fake_stats_, config);
first_dog_ = guard_dog_->createWatchDog(Thread::ThreadId(10), "test_thread", mock_dispatcher_);
second_dog_ = guard_dog_->createWatchDog(Thread::ThreadId(11), "test_thread", mock_dispatcher_);
first_dog_->touch();
second_dog_->touch();
// This should ensure that when the next call to step() occurs, both of the
// dogs will be over last touch time threshold and be reported in the event.
// The next call to step() will either be triggered by the timer or after
// advanceTimeWait() below, but only one of them will append to events_
// because of saturation.
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MISS : 10,11"));
}
TEST_P(GuardDogActionsTest, MissShouldSaturateOnMissEvent) {
const NiceMock<Configuration::MockWatchdog> config(100, DISABLE_MISS, DISABLE_KILL,
DISABLE_MULTIKILL, 0, getActionsConfig());
setupFirstDog(config, Thread::ThreadId(10));
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MISS : 10"));
// Should saturate and not add an additional "event_"
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MISS : 10"));
// Touch the watchdog, which should allow the event to trigger again.
first_dog_->touch();
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MISS : 10", "MISS : 10"));
}
TEST_P(GuardDogActionsTest, MegaMissShouldOnlyReportRelevantThreads) {
const NiceMock<Configuration::MockWatchdog> config(DISABLE_MISS, 100, DISABLE_KILL,
DISABLE_MULTIKILL, 0, getActionsConfig());
setupFirstDog(config, Thread::ThreadId(10));
second_dog_ = guard_dog_->createWatchDog(Thread::ThreadId(11), "test_thread", mock_dispatcher_);
time_system_->advanceTimeWait(std::chrono::milliseconds(50));
second_dog_->touch();
// This will reset the loop interval timer, and should help us
// synchronize with the guard dog.
guard_dog_->forceCheckForTest();
if (GetParam() == TimeSystemType::Real) {
// Touch the second_dog in case we overslept in the real time system
// and the guard dog timer goes off.
second_dog_->touch();
}
time_system_->advanceTimeWait(std::chrono::milliseconds(51));
if (GetParam() == TimeSystemType::Real) {
// Touch the second_dog in case we overslept in the real time system
// and the prior "touch" was consumed.
second_dog_->touch();
}
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MEGAMISS : 10"));
}
TEST_P(GuardDogActionsTest, MegaMissShouldBeAbleToReportMultipleThreads) {
const NiceMock<Configuration::MockWatchdog> config(DISABLE_MISS, 100, DISABLE_KILL,
DISABLE_MULTIKILL, 0, getActionsConfig());
initGuardDog(fake_stats_, config);
first_dog_ = guard_dog_->createWatchDog(Thread::ThreadId(10), "test_thread", mock_dispatcher_);
second_dog_ = guard_dog_->createWatchDog(Thread::ThreadId(11), "test_thread", mock_dispatcher_);
first_dog_->touch();
second_dog_->touch();
// This should ensure that when the next call to step() occurs, both of the
// dogs will be over last touch time threshold and be reported in the event.
// The next call to step() will either be triggered by the timer or after
// advanceTimeWait() below, but only one of them will append to events_
// because of saturation.
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MEGAMISS : 10,11"));
}
TEST_P(GuardDogActionsTest, MegaMissShouldSaturateOnMegaMissEvent) {
const NiceMock<Configuration::MockWatchdog> config(DISABLE_MISS, 100, DISABLE_KILL,
DISABLE_MULTIKILL, 0, getActionsConfig());
setupFirstDog(config, Thread::ThreadId(10));
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MEGAMISS : 10"));
// Should saturate and not add an additional "event_"
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MEGAMISS : 10"));
// Touch the watchdog, which should allow the event to trigger again.
first_dog_->touch();
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MEGAMISS : 10", "MEGAMISS : 10"));
}
// Disabled for coverage per #18229
#if !defined(ENVOY_CONFIG_COVERAGE)
TEST_P(GuardDogActionsTest, ShouldRespectEventPriority) {
// Priority of events are KILL, MULTIKILL, MEGAMISS and MISS
// Kill event should fire before the others
auto kill_function = [&]() -> void {
const NiceMock<Configuration::MockWatchdog> config(100, 100, 100, 100, 0, getActionsConfig());
initGuardDog(fake_stats_, config);
auto first_dog =
guard_dog_->createWatchDog(Thread::ThreadId(10), "test_thread", mock_dispatcher_);
auto second_dog =
guard_dog_->createWatchDog(Thread::ThreadId(11), "test_thread", mock_dispatcher_);
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
};
// We expect only the kill action to have fired
EXPECT_DEATH(kill_function(), "ASSERT_GUARDDOG_ACTION");
// Multikill event should fire before the others
auto multikill_function = [&]() -> void {
const NiceMock<Configuration::MockWatchdog> config(100, 100, DISABLE_KILL, 100, 0,
getActionsConfig());
initGuardDog(fake_stats_, config);
auto first_dog =
guard_dog_->createWatchDog(Thread::ThreadId(10), "test_thread", mock_dispatcher_);
auto second_dog =
guard_dog_->createWatchDog(Thread::ThreadId(11), "test_thread", mock_dispatcher_);
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
};
EXPECT_DEATH(multikill_function(), "ASSERT_GUARDDOG_ACTION");
// We expect megamiss to fire before miss
const NiceMock<Configuration::MockWatchdog> config(100, 100, DISABLE_KILL, DISABLE_MULTIKILL, 0,
getActionsConfig());
setupFirstDog(config, Thread::ThreadId(10));
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
EXPECT_THAT(events_, ElementsAre("MEGAMISS : 10", "MISS : 10"));
}
#endif
TEST_P(GuardDogActionsTest, KillShouldTriggerGuardDogActions) {
auto die_function = [&]() -> void {
const NiceMock<Configuration::MockWatchdog> config(DISABLE_MISS, DISABLE_MEGAMISS, 100, 0, 0,
getActionsConfig());
setupFirstDog(config, Thread::ThreadId(10));
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
};
EXPECT_DEATH(die_function(), "ASSERT_GUARDDOG_ACTION");
}
// Disabled for coverage per #18229
#if !defined(ENVOY_CONFIG_COVERAGE)
TEST_P(GuardDogActionsTest, MultikillShouldTriggerGuardDogActions) {
auto die_function = [&]() -> void {
const NiceMock<Configuration::MockWatchdog> config(DISABLE_MISS, DISABLE_MEGAMISS, DISABLE_KILL,
100, 0, getActionsConfig());
setupFirstDog(config, Thread::ThreadId(10));
second_dog_ = guard_dog_->createWatchDog(Thread::ThreadId(11), "test_thread", mock_dispatcher_);
guard_dog_->forceCheckForTest();
time_system_->advanceTimeWait(std::chrono::milliseconds(101));
guard_dog_->forceCheckForTest();
};
EXPECT_DEATH(die_function(), "ASSERT_GUARDDOG_ACTION");
}
#endif
} // namespace
} // namespace Server
} // namespace Envoy