From 632160667171041177e7a16320b7ffbdb667a3f7 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Mon, 2 Dec 2024 14:37:09 +0100 Subject: [PATCH] IcingaDB: Sync `affects_children` as part of runtime state updates --- lib/icinga/checkable-dependency.cpp | 29 +++++++++++++++++++++++++++++ lib/icinga/checkable.hpp | 1 + lib/icingadb/icingadb-objects.cpp | 1 + 3 files changed, 31 insertions(+) diff --git a/lib/icinga/checkable-dependency.cpp b/lib/icinga/checkable-dependency.cpp index a58b55c05..d10d6f3ce 100644 --- a/lib/icinga/checkable-dependency.cpp +++ b/lib/icinga/checkable-dependency.cpp @@ -122,6 +122,35 @@ bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency return true; } +/** + * Checks whether the last check result of this Checkable affects its child dependencies. + * + * A Checkable affects its child dependencies if it runs into a non-OK state and results in any of its child + * Checkables to become unreachable. Though, that unavailable dependency may not necessarily cause the child + * Checkable to be in unreachable state as it might have some other dependencies that are still reachable, instead + * it just indicates whether the edge/connection between this and the child Checkable is broken or not. + * + * @return bool - Returns true if the Checkable affects its child dependencies, otherwise false. + */ +bool Checkable::AffectsChildren() const +{ + if (!GetLastCheckResult() || !IsReachable()) { + // If there is no check result, or the Checkable is not reachable, we can't safely determine whether + // the Checkable affects its child dependencies. + return false; + } + + for (auto& dep : GetReverseDependencies()) { + if (!dep->IsAvailable(DependencyState)) { + // If one of the child dependency is not available, then it's definitely due to the + // current Checkable state, so we don't need to verify the remaining ones. + return true; + } + } + + return false; +} + std::set Checkable::GetParents() const { std::set parents; diff --git a/lib/icinga/checkable.hpp b/lib/icinga/checkable.hpp index c9e54b0f5..c6413fa1b 100644 --- a/lib/icinga/checkable.hpp +++ b/lib/icinga/checkable.hpp @@ -82,6 +82,7 @@ public: void AddGroup(const String& name); bool IsReachable(DependencyType dt = DependencyState, intrusive_ptr *failedDependency = nullptr, int rstack = 0) const; + bool AffectsChildren() const; AcknowledgementType GetAcknowledgement(); diff --git a/lib/icingadb/icingadb-objects.cpp b/lib/icingadb/icingadb-objects.cpp index ac111c745..a7618e75a 100644 --- a/lib/icingadb/icingadb-objects.cpp +++ b/lib/icingadb/icingadb-objects.cpp @@ -2828,6 +2828,7 @@ Dictionary::Ptr IcingaDB::SerializeState(const Checkable::Ptr& checkable) attrs->Set("check_attempt", checkable->GetCheckAttempt()); attrs->Set("is_active", checkable->IsActive()); + attrs->Set("affects_children", checkable->AffectsChildren()); CheckResult::Ptr cr = checkable->GetLastCheckResult();