Skip to content

Commit fa3afdd

Browse files
committed
Introduce Checkable#scheduler_shuffle_cap
1 parent 338d0aa commit fa3afdd

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

lib/compat/checkresultreader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,5 @@ void CheckResultReader::ProcessCheckResultFile(const String& path) const
162162
/* Reschedule the next check. The side effect of this is that for as long
163163
* as we receive check result files for a host/service we won't execute any
164164
* active checks. */
165-
checkable->SetNextCheck(Utility::GetTime() + checkable->GetCheckInterval());
165+
checkable->SetNextCheck(Utility::GetTime() + checkable->GetCheckInterval() * checkable->GetIntervalShuffleFactor());
166166
}

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")
@@ -278,7 +279,7 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig
278279
if (!parent->GetEnableActiveChecks())
279280
continue;
280281

281-
if (parent->GetNextCheck() >= now + parent->GetRetryInterval()) {
282+
if (parent->GetNextCheck() >= now + parent->GetRetryInterval() * parent->GetIntervalShuffleFactor()) {
282283
ObjectLock olock(parent);
283284
parent->SetNextCheck(now);
284285
}
@@ -382,7 +383,7 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig
382383
if (ttl > 0)
383384
offset = ttl;
384385
else
385-
offset = GetCheckInterval();
386+
offset = GetCheckInterval() * GetIntervalShuffleFactor();
386387

387388
SetNextCheck(Utility::GetTime() + offset, false, origin);
388389
}
@@ -667,3 +668,20 @@ void Checkable::AquirePendingCheckSlot(int maxPendingChecks)
667668

668669
m_PendingChecks++;
669670
}
671+
672+
/**
673+
* Returns a random factor derived from scheduler_shuffle_cap to multiply the check interval with.
674+
*
675+
* E.g. if scheduler_shuffle_cap is 20 (%), this function returns [0.8, 1.2].
676+
*/
677+
double Checkable::GetIntervalShuffleFactor()
678+
{
679+
if (!GetEnableActiveChecks()) {
680+
// scheduler_shuffle_cap doesn't influence external checkers.
681+
return 1;
682+
}
683+
684+
return (GetSchedulerShuffleCap() / 100) // scheduler_shuffle_cap as non-%, i.e. 10 => 0.1
685+
* (rand() / (double)RAND_MAX * 2 - 1) // random number [-1, 1]
686+
+ 1;
687+
}

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

@@ -72,9 +73,9 @@ void Checkable::Start(bool runtimeCreated)
7273
}
7374

7475
if (GetNextCheck() < now + 60) {
75-
double delta = std::min(GetCheckInterval(), 60.0);
76+
double delta = std::min(GetCheckInterval() * GetIntervalShuffleFactor(), 60.0);
7677
delta *= (double)std::rand() / RAND_MAX;
77-
SetNextCheck(now + delta);
78+
SetNextCheck(now + delta + GetCheckInterval() * fabs(GetIntervalShuffleFactor() - 1));
7879
}
7980

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

lib/icinga/checkable.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class Checkable : public ObjectImpl<Checkable>
173173

174174
bool NotificationReasonApplies(NotificationType type);
175175
bool IsLikelyToBeCheckedSoon();
176+
double GetIntervalShuffleFactor();
176177

177178
static void IncreasePendingChecks();
178179
static void DecreasePendingChecks();

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)