1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "icinga/service.hpp"
#include "icinga/dependency.hpp"
#include "base/logger.hpp"
#include <unordered_map>
using namespace icinga;
void Checkable::AddDependency(const Dependency::Ptr& dep)
{
std::unique_lock<std::mutex> lock(m_DependencyMutex);
m_Dependencies.insert(dep);
}
void Checkable::RemoveDependency(const Dependency::Ptr& dep)
{
std::unique_lock<std::mutex> lock(m_DependencyMutex);
m_Dependencies.erase(dep);
}
std::vector<Dependency::Ptr> Checkable::GetDependencies() const
{
std::unique_lock<std::mutex> lock(m_DependencyMutex);
return std::vector<Dependency::Ptr>(m_Dependencies.begin(), m_Dependencies.end());
}
void Checkable::AddReverseDependency(const Dependency::Ptr& dep)
{
std::unique_lock<std::mutex> lock(m_DependencyMutex);
m_ReverseDependencies.insert(dep);
}
void Checkable::RemoveReverseDependency(const Dependency::Ptr& dep)
{
std::unique_lock<std::mutex> lock(m_DependencyMutex);
m_ReverseDependencies.erase(dep);
}
std::vector<Dependency::Ptr> Checkable::GetReverseDependencies() const
{
std::unique_lock<std::mutex> lock(m_DependencyMutex);
return std::vector<Dependency::Ptr>(m_ReverseDependencies.begin(), m_ReverseDependencies.end());
}
bool Checkable::IsReachable(DependencyType dt, Dependency::Ptr *failedDependency, int rstack) const
{
/* Anything greater than 256 causes recursion bus errors. */
int limit = 256;
if (rstack > limit) {
Log(LogWarning, "Checkable")
<< "Too many nested dependencies (>" << limit << ") for checkable '" << GetName() << "': Dependency failed.";
return false;
}
for (const Checkable::Ptr& checkable : GetParents()) {
if (!checkable->IsReachable(dt, failedDependency, rstack + 1))
return false;
}
/* implicit dependency on host if this is a service */
const auto *service = dynamic_cast<const Service *>(this);
if (service && (dt == DependencyState || dt == DependencyNotification)) {
Host::Ptr host = service->GetHost();
if (host && host->GetState() != HostUp && host->GetStateType() == StateTypeHard) {
if (failedDependency)
*failedDependency = nullptr;
return false;
}
}
auto deps = GetDependencies();
std::unordered_map<std::string, Dependency::Ptr> violated; // key: redundancy group, value: nullptr if satisfied, violating dependency otherwise
for (const Dependency::Ptr& dep : deps) {
std::string redundancy_group = dep->GetRedundancyGroup();
if (!dep->IsAvailable(dt)) {
if (redundancy_group.empty()) {
Log(LogDebug, "Checkable")
<< "Non-redundant dependency '" << dep->GetName() << "' failed for checkable '" << GetName() << "': Marking as unreachable.";
if (failedDependency)
*failedDependency = dep;
return false;
}
// tentatively mark this dependency group as failed unless it is already marked;
// so it either passed before (don't overwrite) or already failed (so don't care)
// note that std::unordered_map::insert() will not overwrite an existing entry
violated.insert(std::make_pair(redundancy_group, dep));
} else if (!redundancy_group.empty()) {
violated[redundancy_group] = nullptr;
}
}
auto violator = std::find_if(violated.begin(), violated.end(), [](auto& v) { return v.second != nullptr; });
if (violator != violated.end()) {
Log(LogDebug, "Checkable")
<< "All dependencies in redundancy group '" << violator->first << "' have failed for checkable '" << GetName() << "': Marking as unreachable.";
if (failedDependency)
*failedDependency = violator->second;
return false;
}
if (failedDependency)
*failedDependency = nullptr;
return true;
}
std::set<Checkable::Ptr> Checkable::GetParents() const
{
std::set<Checkable::Ptr> parents;
for (const Dependency::Ptr& dep : GetDependencies()) {
Checkable::Ptr parent = dep->GetParent();
if (parent && parent.get() != this)
parents.insert(parent);
}
return parents;
}
std::set<Checkable::Ptr> Checkable::GetChildren() const
{
std::set<Checkable::Ptr> parents;
for (const Dependency::Ptr& dep : GetReverseDependencies()) {
Checkable::Ptr service = dep->GetChild();
if (service && service.get() != this)
parents.insert(service);
}
return parents;
}
std::set<Checkable::Ptr> Checkable::GetAllChildren() const
{
std::set<Checkable::Ptr> children = GetChildren();
GetAllChildrenInternal(children, 0);
return children;
}
void Checkable::GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level) const
{
if (level > 32)
return;
std::set<Checkable::Ptr> localChildren;
for (const Checkable::Ptr& checkable : children) {
std::set<Checkable::Ptr> cChildren = checkable->GetChildren();
if (!cChildren.empty()) {
GetAllChildrenInternal(cChildren, level + 1);
localChildren.insert(cChildren.begin(), cChildren.end());
}
localChildren.insert(checkable);
}
children.insert(localChildren.begin(), localChildren.end());
}
|