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.
This commit is contained in:
Julian Brost 2025-03-13 15:02:56 +01:00 committed by Yonas Habteab
parent 864e2aaae0
commit 065118bc22
4 changed files with 20 additions and 15 deletions

View File

@ -206,7 +206,7 @@ bool Checkable::IsReachable(DependencyType dt, int rstack) const
} }
for (auto& dependencyGroup : GetDependencyGroups()) { 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") Log(LogDebug, "Checkable")
<< "Dependency group '" << dependencyGroup->GetRedundancyGroupName() << "' have failed for checkable '" << "Dependency group '" << dependencyGroup->GetRedundancyGroupName() << "' have failed for checkable '"
<< GetName() << "': Marking as unreachable."; << GetName() << "': Marking as unreachable.";

View File

@ -325,14 +325,24 @@ DependencyGroup::State DependencyGroup::GetState(const Checkable* child, Depende
if (IsRedundancyGroup()) { if (IsRedundancyGroup()) {
// The state of a redundancy group is determined by the best state of any parent. If any parent ist reachable, // 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 // the redundancy group is reachable, analogously for availability.
// available as reachable = 0 implies available = 0. if (reachable == 0) {
return {reachable > 0, available > 0}; return State::Unreachable;
} else if (available == 0) {
return State::Failed;
} else {
return State::Ok;
}
} else { } else {
// For dependencies without a redundancy group, dependencies.size() will be 1 in almost all cases. It will only // 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, // 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 // all of them have to be reachable/available as they don't provide redundancy.
// unavailable here as well as only reachable parents count towards the number of available parents. if (reachable < dependencies.size()) {
return {reachable == dependencies.size(), available == dependencies.size()}; return State::Unreachable;
} else if (available < dependencies.size()) {
return State::Failed;
} else {
return State::Ok;
}
} }
} }

View File

@ -163,12 +163,7 @@ public:
const String& GetRedundancyGroupName() const; const String& GetRedundancyGroupName() const;
String GetCompositeKey(); String GetCompositeKey();
struct State enum class State { Ok, Failed, Unreachable };
{
bool Reachable; // Whether the dependency group is reachable.
bool OK; // Whether the dependency group is reachable and OK.
};
State GetState(const Checkable* child, DependencyType dt = DependencyState, int rstack = 0) const; State GetState(const Checkable* child, DependencyType dt = DependencyState, int rstack = 0) const;
static boost::signals2::signal<void(const Checkable::Ptr&, const DependencyGroup::Ptr&)> OnChildRegistered; static boost::signals2::signal<void(const Checkable::Ptr&, const DependencyGroup::Ptr&)> OnChildRegistered;

View File

@ -213,8 +213,8 @@ Dictionary::Ptr IcingaDB::SerializeRedundancyGroupState(const Checkable::Ptr& ch
{"id", redundancyGroup->GetIcingaDBIdentifier()}, {"id", redundancyGroup->GetIcingaDBIdentifier()},
{"environment_id", m_EnvironmentId}, {"environment_id", m_EnvironmentId},
{"redundancy_group_id", redundancyGroup->GetIcingaDBIdentifier()}, {"redundancy_group_id", redundancyGroup->GetIcingaDBIdentifier()},
{"failed", !state.Reachable || !state.OK}, {"failed", state != DependencyGroup::State::Ok},
{"is_reachable", state.Reachable}, {"is_reachable", state != DependencyGroup::State::Unreachable},
{"last_state_change", TimestampToMilliseconds(Utility::GetTime())}, {"last_state_change", TimestampToMilliseconds(Utility::GetTime())},
}; };
} }