Skip to content

Commit bbc25b3

Browse files
committed
Introduce Checkable#scheduler_shuffle_cap
1 parent a65f2d6 commit bbc25b3

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

doc/09-object-types.md

+2
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ Configuration Attributes:
353353
check\_timeout | Duration | **Optional.** Check command timeout in seconds. Overrides the CheckCommand's `timeout` attribute.
354354
check\_interval | Duration | **Optional.** The check interval (in seconds). This interval is used for checks when the host is in a `HARD` state. Defaults to `5m`.
355355
retry\_interval | Duration | **Optional.** The retry interval (in seconds). This interval is used for checks when the host is in a `SOFT` state. Defaults to `1m`. Note: This does not affect the scheduling [after a passive check result](08-advanced-topics.md#check-result-freshness).
356+
scheduler\_shuffle\_cap | Number | **Optional.** Number of percent by up to which Icinga is allowed to override the check interval arbitrarily and in any direction to reduce load spikes. Defaults to 0.
356357
enable\_notifications | Boolean | **Optional.** Whether notifications are enabled. Defaults to true.
357358
enable\_active\_checks | Boolean | **Optional.** Whether active checks are enabled. Defaults to true.
358359
enable\_passive\_checks | Boolean | **Optional.** Whether passive checks are enabled. Defaults to true.
@@ -719,6 +720,7 @@ Configuration Attributes:
719720
check\_timeout | Duration | **Optional.** Check command timeout in seconds. Overrides the CheckCommand's `timeout` attribute.
720721
check\_interval | Duration | **Optional.** The check interval (in seconds). This interval is used for checks when the service is in a `HARD` state. Defaults to `5m`.
721722
retry\_interval | Duration | **Optional.** The retry interval (in seconds). This interval is used for checks when the service is in a `SOFT` state. Defaults to `1m`. Note: This does not affect the scheduling [after a passive check result](08-advanced-topics.md#check-result-freshness).
723+
scheduler\_shuffle\_cap | Number | **Optional.** Number of percent by up to which Icinga is allowed to override the check interval arbitrarily and in any direction to reduce load spikes. Defaults to 0.
722724
enable\_notifications | Boolean | **Optional.** Whether notifications are enabled. Defaults to `true`.
723725
enable\_active\_checks | Boolean | **Optional.** Whether active checks are enabled. Defaults to `true`.
724726
enable\_passive\_checks | Boolean | **Optional.** Whether passive checks are enabled. Defaults to `true`.

lib/icinga/checkable-check.cpp

+21-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "base/convert.hpp"
1515
#include "base/utility.hpp"
1616
#include "base/context.hpp"
17+
#include <cstdlib>
1718

1819
using namespace icinga;
1920

@@ -67,7 +68,7 @@ void Checkable::UpdateNextCheck(const MessageOrigin::Ptr& origin)
6768
if (adj != 0.0)
6869
adj = std::min(0.5 + fmod(GetSchedulingOffset(), interval * 5) / 100.0, adj);
6970

70-
double nextCheck = now - adj + interval;
71+
double nextCheck = now - adj + interval * GetIntervalShuffleFactor();
7172
double lastCheck = GetLastCheck();
7273

7374
Log(LogDebug, "Checkable")
@@ -384,7 +385,7 @@ Checkable::ProcessingResult Checkable::ProcessCheckResult(const CheckResult::Ptr
384385
if (ttl > 0)
385386
offset = ttl;
386387
else
387-
offset = GetCheckInterval();
388+
offset = GetCheckInterval() * GetIntervalShuffleFactor();
388389

389390
SetNextCheck(Utility::GetTime() + offset);
390391
}
@@ -425,7 +426,7 @@ Checkable::ProcessingResult Checkable::ProcessCheckResult(const CheckResult::Ptr
425426
if (!parent->GetEnableActiveChecks())
426427
continue;
427428

428-
if (parent->GetNextCheck() >= now + parent->GetRetryInterval()) {
429+
if (parent->GetNextCheck() >= now + parent->GetRetryInterval() * parent->GetIntervalShuffleFactor()) {
429430
ObjectLock olock(parent);
430431
parent->SetNextCheck(now);
431432
}
@@ -720,3 +721,20 @@ void Checkable::AquirePendingCheckSlot(int maxPendingChecks)
720721

721722
m_PendingChecks++;
722723
}
724+
725+
/**
726+
* Returns a random factor derived from scheduler_shuffle_cap to multiply the check interval with.
727+
*
728+
* E.g. if scheduler_shuffle_cap is 20 (%), this function returns [0.8, 1.2].
729+
*/
730+
double Checkable::GetIntervalShuffleFactor()
731+
{
732+
if (!GetEnableActiveChecks()) {
733+
// scheduler_shuffle_cap doesn't influence external checkers.
734+
return 1;
735+
}
736+
737+
return (GetSchedulerShuffleCap() / 100) // scheduler_shuffle_cap as non-%, i.e. 10 => 0.1
738+
* (rand() / (double)RAND_MAX * 2 - 1) // random number [-1, 1]
739+
+ 1;
740+
}

lib/icinga/checkable.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "base/exception.hpp"
1010
#include "base/timer.hpp"
1111
#include <boost/thread/once.hpp>
12+
#include <cmath>
1213

1314
using namespace icinga;
1415

@@ -93,9 +94,9 @@ void Checkable::Start(bool runtimeCreated)
9394
}
9495

9596
if (GetNextCheck() < now + 60) {
96-
double delta = std::min(GetCheckInterval(), 60.0);
97+
double delta = std::min(GetCheckInterval() * GetIntervalShuffleFactor(), 60.0);
9798
delta *= (double)std::rand() / RAND_MAX;
98-
SetNextCheck(now + delta);
99+
SetNextCheck(now + delta + GetCheckInterval() * fabs(GetIntervalShuffleFactor() - 1));
99100
}
100101

101102
ObjectImpl<Checkable>::Start(runtimeCreated);

lib/icinga/checkable.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class Checkable : public ObjectImpl<Checkable>
204204
bool NotificationReasonApplies(NotificationType type);
205205
bool NotificationReasonSuppressed(NotificationType type);
206206
bool IsLikelyToBeCheckedSoon();
207+
double GetIntervalShuffleFactor();
207208

208209
void FireSuppressedNotifications();
209210

lib/icinga/checkable.ti

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ abstract class Checkable : CustomVarObject
4747
[config] double retry_interval {
4848
default {{{ return 60; }}}
4949
};
50+
[config] double scheduler_shuffle_cap {
51+
default {{{ return 0.0; }}}
52+
};
5053
[config, navigation] name(EventCommand) event_command (EventCommandRaw) {
5154
navigate {{{
5255
return EventCommand::GetByName(GetEventCommandRaw());

0 commit comments

Comments
 (0)