Skip to content

Commit d0980df

Browse files
julianbrostyhabteab
authored andcommitted
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.
1 parent ab7adf8 commit d0980df

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

lib/icinga/checkable-dependency.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ bool Checkable::IsReachable(DependencyType dt, int rstack) const
206206
}
207207

208208
for (auto& dependencyGroup : GetDependencyGroups()) {
209-
if (auto state(dependencyGroup->GetState(this, dt, rstack + 1)); !state.Reachable || !state.OK) {
209+
if (auto state(dependencyGroup->GetState(this, dt, rstack + 1)); state != DependencyGroup::State::Ok) {
210210
Log(LogDebug, "Checkable")
211211
<< "Dependency group '" << dependencyGroup->GetRedundancyGroupName() << "' have failed for checkable '"
212212
<< GetName() << "': Marking as unreachable.";

lib/icinga/dependency-group.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,24 @@ DependencyGroup::State DependencyGroup::GetState(const Checkable* child, Depende
325325

326326
if (IsRedundancyGroup()) {
327327
// The state of a redundancy group is determined by the best state of any parent. If any parent ist reachable,
328-
// the redundancy group is reachable, analogously for availability. Note that an unreachable group cannot be
329-
// available as reachable = 0 implies available = 0.
330-
return {reachable > 0, available > 0};
328+
// the redundancy group is reachable, analogously for availability.
329+
if (reachable == 0) {
330+
return State::Unreachable;
331+
} else if (available == 0) {
332+
return State::Failed;
333+
} else {
334+
return State::Ok;
335+
}
331336
} else {
332337
// For dependencies without a redundancy group, dependencies.size() will be 1 in almost all cases. It will only
333338
// contain more elements if there are duplicate dependency config objects between two checkables. In this case,
334-
// all of them have to be reachable or available as they don't provide redundancy. Note that unreachable implies
335-
// unavailable here as well as only reachable parents count towards the number of available parents.
336-
return {reachable == dependencies.size(), available == dependencies.size()};
339+
// all of them have to be reachable/available as they don't provide redundancy.
340+
if (reachable < dependencies.size()) {
341+
return State::Unreachable;
342+
} else if (available < dependencies.size()) {
343+
return State::Failed;
344+
} else {
345+
return State::Ok;
346+
}
337347
}
338348
}

lib/icinga/dependency.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,7 @@ class DependencyGroup final : public SharedObject
163163
const String& GetRedundancyGroupName() const;
164164
String GetCompositeKey();
165165

166-
struct State
167-
{
168-
bool Reachable; // Whether the dependency group is reachable.
169-
bool OK; // Whether the dependency group is reachable and OK.
170-
};
171-
166+
enum class State { Ok, Failed, Unreachable };
172167
State GetState(const Checkable* child, DependencyType dt = DependencyState, int rstack = 0) const;
173168

174169
static boost::signals2::signal<void(const Checkable::Ptr&, const DependencyGroup::Ptr&)> OnChildRegistered;

lib/icingadb/icingadb-utility.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ Dictionary::Ptr IcingaDB::SerializeRedundancyGroupState(const Checkable::Ptr& ch
219219
{"id", redundancyGroup->GetIcingaDBIdentifier()},
220220
{"environment_id", m_EnvironmentId},
221221
{"redundancy_group_id", redundancyGroup->GetIcingaDBIdentifier()},
222-
{"failed", !state.Reachable || !state.OK},
223-
{"is_reachable", state.Reachable},
222+
{"failed", state != DependencyGroup::State::Ok},
223+
{"is_reachable", state != DependencyGroup::State::Unreachable},
224224
{"last_state_change", TimestampToMilliseconds(Utility::GetTime())},
225225
};
226226
}

0 commit comments

Comments
 (0)