From 065118bc22d420d22f89c60fb1d85955e9a64394 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Thu, 13 Mar 2025 15:02:56 +0100 Subject: [PATCH] Make DependencyGroup::State an enum The previous struct used two bools to represent three useful states. Make this more explicit by having these three states as an enum. --- lib/icinga/checkable-dependency.cpp | 2 +- lib/icinga/dependency-group.cpp | 22 ++++++++++++++++------ lib/icinga/dependency.hpp | 7 +------ lib/icingadb/icingadb-utility.cpp | 4 ++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/icinga/checkable-dependency.cpp b/lib/icinga/checkable-dependency.cpp index 27ea5c0ae..65d9dd902 100644 --- a/lib/icinga/checkable-dependency.cpp +++ b/lib/icinga/checkable-dependency.cpp @@ -206,7 +206,7 @@ bool Checkable::IsReachable(DependencyType dt, int rstack) const } for (auto& dependencyGroup : GetDependencyGroups()) { - if (auto state(dependencyGroup->GetState(this, dt, rstack + 1)); !state.Reachable || !state.OK) { + if (auto state(dependencyGroup->GetState(this, dt, rstack + 1)); state != DependencyGroup::State::Ok) { Log(LogDebug, "Checkable") << "Dependency group '" << dependencyGroup->GetRedundancyGroupName() << "' have failed for checkable '" << GetName() << "': Marking as unreachable."; diff --git a/lib/icinga/dependency-group.cpp b/lib/icinga/dependency-group.cpp index 0ffe33741..aa7d5fc06 100644 --- a/lib/icinga/dependency-group.cpp +++ b/lib/icinga/dependency-group.cpp @@ -325,14 +325,24 @@ DependencyGroup::State DependencyGroup::GetState(const Checkable* child, Depende if (IsRedundancyGroup()) { // The state of a redundancy group is determined by the best state of any parent. If any parent ist reachable, - // the redundancy group is reachable, analogously for availability. Note that an unreachable group cannot be - // available as reachable = 0 implies available = 0. - return {reachable > 0, available > 0}; + // the redundancy group is reachable, analogously for availability. + if (reachable == 0) { + return State::Unreachable; + } else if (available == 0) { + return State::Failed; + } else { + return State::Ok; + } } else { // For dependencies without a redundancy group, dependencies.size() will be 1 in almost all cases. It will only // contain more elements if there are duplicate dependency config objects between two checkables. In this case, - // all of them have to be reachable or available as they don't provide redundancy. Note that unreachable implies - // unavailable here as well as only reachable parents count towards the number of available parents. - return {reachable == dependencies.size(), available == dependencies.size()}; + // all of them have to be reachable/available as they don't provide redundancy. + if (reachable < dependencies.size()) { + return State::Unreachable; + } else if (available < dependencies.size()) { + return State::Failed; + } else { + return State::Ok; + } } } diff --git a/lib/icinga/dependency.hpp b/lib/icinga/dependency.hpp index 26a7dffe0..b4e206b7f 100644 --- a/lib/icinga/dependency.hpp +++ b/lib/icinga/dependency.hpp @@ -163,12 +163,7 @@ public: const String& GetRedundancyGroupName() const; String GetCompositeKey(); - struct State - { - bool Reachable; // Whether the dependency group is reachable. - bool OK; // Whether the dependency group is reachable and OK. - }; - + enum class State { Ok, Failed, Unreachable }; State GetState(const Checkable* child, DependencyType dt = DependencyState, int rstack = 0) const; static boost::signals2::signal OnChildRegistered; diff --git a/lib/icingadb/icingadb-utility.cpp b/lib/icingadb/icingadb-utility.cpp index b93aff67a..89e5a5031 100644 --- a/lib/icingadb/icingadb-utility.cpp +++ b/lib/icingadb/icingadb-utility.cpp @@ -213,8 +213,8 @@ Dictionary::Ptr IcingaDB::SerializeRedundancyGroupState(const Checkable::Ptr& ch {"id", redundancyGroup->GetIcingaDBIdentifier()}, {"environment_id", m_EnvironmentId}, {"redundancy_group_id", redundancyGroup->GetIcingaDBIdentifier()}, - {"failed", !state.Reachable || !state.OK}, - {"is_reachable", state.Reachable}, + {"failed", state != DependencyGroup::State::Ok}, + {"is_reachable", state != DependencyGroup::State::Unreachable}, {"last_state_change", TimestampToMilliseconds(Utility::GetTime())}, }; }