Skip to content

Commit f53e534

Browse files
authored
Merge pull request #10292 from Icinga/rpc-sync-failures
Runtime RPC sync failures
2 parents 14b854d + 156ba26 commit f53e534

8 files changed

Lines changed: 189 additions & 71 deletions

File tree

lib/base/dependencygraph.cpp

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,68 @@
55
using namespace icinga;
66

77
std::mutex DependencyGraph::m_Mutex;
8-
std::map<Object *, std::map<Object *, int> > DependencyGraph::m_Dependencies;
8+
DependencyGraph::DependencyMap DependencyGraph::m_Dependencies;
99

10-
void DependencyGraph::AddDependency(Object *parent, Object *child)
10+
void DependencyGraph::AddDependency(ConfigObject* child, ConfigObject* parent)
1111
{
1212
std::unique_lock<std::mutex> lock(m_Mutex);
13-
m_Dependencies[child][parent]++;
13+
if (auto [it, inserted] = m_Dependencies.insert(Edge(parent, child)); !inserted) {
14+
m_Dependencies.modify(it, [](Edge& e) { e.count++; });
15+
}
1416
}
1517

16-
void DependencyGraph::RemoveDependency(Object *parent, Object *child)
18+
void DependencyGraph::RemoveDependency(ConfigObject* child, ConfigObject* parent)
1719
{
1820
std::unique_lock<std::mutex> lock(m_Mutex);
1921

20-
auto& refs = m_Dependencies[child];
21-
auto it = refs.find(parent);
22-
23-
if (it == refs.end())
24-
return;
22+
if (auto it(m_Dependencies.find(Edge(parent, child))); it != m_Dependencies.end()) {
23+
if (it->count > 1) {
24+
// Remove a duplicate edge from child to node, i.e. decrement the corresponding counter.
25+
m_Dependencies.modify(it, [](Edge& e) { e.count--; });
26+
} else {
27+
// Remove the last edge from child to node (decrementing the counter would set it to 0),
28+
// thus remove that connection from the data structure completely.
29+
m_Dependencies.erase(it);
30+
}
31+
}
32+
}
2533

26-
it->second--;
34+
/**
35+
* Returns all the parent objects of the given child object.
36+
*
37+
* @param child The child object.
38+
*
39+
* @returns A list of the parent objects.
40+
*/
41+
std::vector<ConfigObject::Ptr> DependencyGraph::GetParents(const ConfigObject::Ptr& child)
42+
{
43+
std::vector<ConfigObject::Ptr> objects;
2744

28-
if (it->second == 0)
29-
refs.erase(it);
45+
std::unique_lock lock(m_Mutex);
46+
auto [begin, end] = m_Dependencies.get<2>().equal_range(child.get());
47+
std::transform(begin, end, std::back_inserter(objects), [](const Edge& edge) {
48+
return edge.parent;
49+
});
3050

31-
if (refs.empty())
32-
m_Dependencies.erase(child);
51+
return objects;
3352
}
3453

35-
std::vector<Object::Ptr> DependencyGraph::GetParents(const Object::Ptr& child)
54+
/**
55+
* Returns all the dependent objects of the given parent object.
56+
*
57+
* @param parent The parent object.
58+
*
59+
* @returns A list of the dependent objects.
60+
*/
61+
std::vector<ConfigObject::Ptr> DependencyGraph::GetChildren(const ConfigObject::Ptr& parent)
3662
{
37-
std::vector<Object::Ptr> objects;
38-
39-
std::unique_lock<std::mutex> lock(m_Mutex);
40-
auto it = m_Dependencies.find(child.get());
63+
std::vector<ConfigObject::Ptr> objects;
4164

42-
if (it != m_Dependencies.end()) {
43-
typedef std::pair<Object *, int> kv_pair;
44-
for (const kv_pair& kv : it->second) {
45-
objects.emplace_back(kv.first);
46-
}
47-
}
65+
std::unique_lock lock(m_Mutex);
66+
auto [begin, end] = m_Dependencies.get<1>().equal_range(parent.get());
67+
std::transform(begin, end, std::back_inserter(objects), [](const Edge& edge) {
68+
return edge.child;
69+
});
4870

4971
return objects;
5072
}

lib/base/dependencygraph.hpp

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#define DEPENDENCYGRAPH_H
55

66
#include "base/i2-base.hpp"
7-
#include "base/object.hpp"
8-
#include <map>
7+
#include "base/configobject.hpp"
8+
#include <boost/multi_index_container.hpp>
9+
#include <boost/multi_index/hashed_index.hpp>
10+
#include <boost/multi_index/member.hpp>
911
#include <mutex>
1012

1113
namespace icinga {
@@ -18,15 +20,84 @@ namespace icinga {
1820
class DependencyGraph
1921
{
2022
public:
21-
static void AddDependency(Object *parent, Object *child);
22-
static void RemoveDependency(Object *parent, Object *child);
23-
static std::vector<Object::Ptr> GetParents(const Object::Ptr& child);
23+
static void AddDependency(ConfigObject* child, ConfigObject* parent);
24+
static void RemoveDependency(ConfigObject* child, ConfigObject* parent);
25+
static std::vector<ConfigObject::Ptr> GetParents(const ConfigObject::Ptr& child);
26+
static std::vector<ConfigObject::Ptr> GetChildren(const ConfigObject::Ptr& parent);
2427

2528
private:
2629
DependencyGraph();
2730

31+
/**
32+
* Represents an undirected dependency edge between two objects.
33+
*
34+
* It allows to traverse the graph in both directions, i.e. from parent to child and vice versa.
35+
*/
36+
struct Edge
37+
{
38+
ConfigObject* parent; // The parent object of the child one.
39+
ConfigObject* child; // The dependent object of the parent.
40+
// Counter for the number of parent <-> child edges to allow duplicates.
41+
int count;
42+
43+
Edge(ConfigObject* parent, ConfigObject* child, int count = 1): parent(parent), child(child), count(count)
44+
{
45+
}
46+
47+
struct Hash
48+
{
49+
/**
50+
* Generates a unique hash of the given Edge object.
51+
*
52+
* Note, the hash value is generated only by combining the hash values of the parent and child pointers.
53+
*
54+
* @param edge The Edge object to be hashed.
55+
*
56+
* @return size_t The resulting hash value of the given object.
57+
*/
58+
size_t operator()(const Edge& edge) const
59+
{
60+
size_t seed = 0;
61+
boost::hash_combine(seed, edge.parent);
62+
boost::hash_combine(seed, edge.child);
63+
64+
return seed;
65+
}
66+
};
67+
68+
struct Equal
69+
{
70+
/**
71+
* Compares whether the two Edge objects contain the same parent and child pointers.
72+
*
73+
* Note, the member property count is not taken into account for equality checks.
74+
*
75+
* @param a The first Edge object to compare.
76+
* @param b The second Edge object to compare.
77+
*
78+
* @return bool Returns true if the two objects are equal, false otherwise.
79+
*/
80+
bool operator()(const Edge& a, const Edge& b) const
81+
{
82+
return a.parent == b.parent && a.child == b.child;
83+
}
84+
};
85+
};
86+
87+
using DependencyMap = boost::multi_index_container<
88+
Edge, // The value type we want to sore in the container.
89+
boost::multi_index::indexed_by<
90+
// The first indexer is used for lookups by the Edge from child to parent, thus it
91+
// needs its own hash function and comparison predicate.
92+
boost::multi_index::hashed_unique<boost::multi_index::identity<Edge>, Edge::Hash, Edge::Equal>,
93+
// These two indexers are used for lookups by the parent and child pointers.
94+
boost::multi_index::hashed_non_unique<boost::multi_index::member<Edge, ConfigObject*, &Edge::parent>>,
95+
boost::multi_index::hashed_non_unique<boost::multi_index::member<Edge, ConfigObject*, &Edge::child>>
96+
>
97+
>;
98+
2899
static std::mutex m_Mutex;
29-
static std::map<Object *, std::map<Object *, int> > m_Dependencies;
100+
static DependencyMap m_Dependencies;
30101
};
31102

32103
}

lib/base/scriptutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ String ScriptUtils::MsiGetComponentPathShim(const String& component)
520520

521521
Array::Ptr ScriptUtils::TrackParents(const Object::Ptr& child)
522522
{
523-
return Array::FromVector(DependencyGraph::GetParents(child));
523+
return Array::FromVector(DependencyGraph::GetChildren(dynamic_pointer_cast<ConfigObject>(child)));
524524
}
525525

526526
double ScriptUtils::Ptr(const Object::Ptr& object)

lib/remote/apilistener-configsync.cpp

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
#include "remote/configobjectutility.hpp"
66
#include "remote/jsonrpc.hpp"
77
#include "base/configtype.hpp"
8-
#include "base/json.hpp"
98
#include "base/convert.hpp"
9+
#include "base/dependencygraph.hpp"
10+
#include "base/json.hpp"
1011
#include "config/vmops.hpp"
1112
#include "remote/configobjectslock.hpp"
1213
#include <fstream>
14+
#include <unordered_set>
1315

1416
using namespace icinga;
1517

@@ -393,6 +395,40 @@ void ApiListener::UpdateConfigObject(const ConfigObject::Ptr& object, const Mess
393395
}
394396
}
395397

398+
/**
399+
* Syncs the specified object and its direct and indirect parents to the provided client
400+
* in topological order of their dependency graph recursively.
401+
*
402+
* Objects that the client does not have access to are skipped without going through their dependency graph.
403+
*
404+
* Please do not use this method to forward remote generated cluster updates; it should only be used to
405+
* send local updates to that specific non-nullptr client.
406+
*
407+
* @param object The config object you want to sync.
408+
* @param azone The zone of the client you want to send the update to.
409+
* @param client The JsonRpc client you send the update to.
410+
* @param syncedObjects Used to cache the already synced objects.
411+
*/
412+
void ApiListener::UpdateConfigObjectWithParents(const ConfigObject::Ptr& object, const Zone::Ptr& azone,
413+
const JsonRpcConnection::Ptr& client, std::unordered_set<ConfigObject*>& syncedObjects)
414+
{
415+
if (syncedObjects.find(object.get()) != syncedObjects.end()) {
416+
return;
417+
}
418+
419+
/* don't sync objects for non-matching parent-child zones */
420+
if (!azone->CanAccessObject(object)) {
421+
return;
422+
}
423+
syncedObjects.emplace(object.get());
424+
425+
for (const auto& parent : DependencyGraph::GetParents(object)) {
426+
UpdateConfigObjectWithParents(parent, azone, client, syncedObjects);
427+
}
428+
429+
/* send the config object to the connected client */
430+
UpdateConfigObject(object, nullptr, client);
431+
}
396432

397433
void ApiListener::DeleteConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
398434
const JsonRpcConnection::Ptr& client)
@@ -454,19 +490,17 @@ void ApiListener::SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient
454490
Log(LogInformation, "ApiListener")
455491
<< "Syncing runtime objects to endpoint '" << endpoint->GetName() << "'.";
456492

493+
std::unordered_set<ConfigObject*> syncedObjects;
457494
for (const Type::Ptr& type : Type::GetAllTypes()) {
458-
auto *dtype = dynamic_cast<ConfigType *>(type.get());
459-
460-
if (!dtype)
461-
continue;
462-
463-
for (const ConfigObject::Ptr& object : dtype->GetObjects()) {
464-
/* don't sync objects for non-matching parent-child zones */
465-
if (!azone->CanAccessObject(object))
466-
continue;
467-
468-
/* send the config object to the connected client */
469-
UpdateConfigObject(object, nullptr, aclient);
495+
if (auto *ctype = dynamic_cast<ConfigType *>(type.get())) {
496+
for (const auto& object : ctype->GetObjects()) {
497+
// All objects must be synced sorted by their dependency graph.
498+
// Otherwise, downtimes/comments etc. might get synced before their respective Checkables, which will
499+
// result in comments and downtimes being ignored by the other endpoint since it does not yet know
500+
// about their checkables. Given that the runtime config updates event does not trigger a reload on the
501+
// remote endpoint, these objects won't be synced again until the next reload.
502+
UpdateConfigObjectWithParents(object, azone, aclient, syncedObjects);
503+
}
470504
}
471505
}
472506

lib/remote/apilistener.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ class ApiListener final : public ObjectImpl<ApiListener>
247247
/* configsync */
248248
void UpdateConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
249249
const JsonRpcConnection::Ptr& client = nullptr);
250+
void UpdateConfigObjectWithParents(const ConfigObject::Ptr& object, const Zone::Ptr& azone,
251+
const JsonRpcConnection::Ptr& client, std::unordered_set<ConfigObject*>& syncedObjects);
250252
void DeleteConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
251253
const JsonRpcConnection::Ptr& client = nullptr);
252254
void SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient);

lib/remote/configobjectutility.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
303303
bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bool cascade,
304304
const Array::Ptr& errors, const Array::Ptr& diagnosticInformation, const Value& cookie)
305305
{
306-
std::vector<Object::Ptr> parents = DependencyGraph::GetParents(object);
306+
auto parents (DependencyGraph::GetChildren(object));
307307

308308
Type::Ptr type = object->GetReflectionType();
309309

@@ -319,12 +319,7 @@ bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bo
319319
return false;
320320
}
321321

322-
for (const Object::Ptr& pobj : parents) {
323-
ConfigObject::Ptr parentObj = dynamic_pointer_cast<ConfigObject>(pobj);
324-
325-
if (!parentObj)
326-
continue;
327-
322+
for (auto& parentObj : parents) {
328323
DeleteObjectHelper(parentObj, cascade, errors, diagnosticInformation, cookie);
329324
}
330325

lib/remote/objectqueryhandler.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,7 @@ bool ObjectQueryHandler::HandleRequest(
208208
Array::Ptr used_by = new Array();
209209
metaAttrs.emplace_back("used_by", used_by);
210210

211-
for (const Object::Ptr& pobj : DependencyGraph::GetParents((obj)))
212-
{
213-
ConfigObject::Ptr configObj = dynamic_pointer_cast<ConfigObject>(pobj);
214-
215-
if (!configObj)
216-
continue;
217-
211+
for (auto& configObj : DependencyGraph::GetChildren(obj)) {
218212
used_by->Add(new Dictionary({
219213
{ "type", configObj->GetReflectionType()->GetName() },
220214
{ "name", configObj->GetName() }

0 commit comments

Comments
 (0)