Skip to content

Commit

Permalink
Type: Simplify sort by load dependencies algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Sep 20, 2024
1 parent 68fa436 commit 9b5c8c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
56 changes: 19 additions & 37 deletions lib/base/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
#include "base/scriptglobal.hpp"
#include "base/namespace.hpp"
#include "base/objectlock.hpp"
#include <algorithm>
#include <functional>
#include <unordered_map>

using namespace icinga;

Expand Down Expand Up @@ -41,53 +39,37 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() {
static std::vector<Type::Ptr> l_SortedByLoadDependencies;
static Atomic l_SortingByLoadDependenciesDone (false);

typedef std::unordered_map<Type*, bool> Visited; // https://stackoverflow.com/a/8942986

INITIALIZE_ONCE_WITH_PRIORITY([] {
auto types (Type::GetAllTypes());

types.erase(std::remove_if(types.begin(), types.end(), [](auto& type) {
return !ConfigObject::TypeInstance->IsAssignableFrom(type);
}), types.end());

// Depth-first search
std::unordered_set<Type*> unsorted;
Visited visited;
std::vector<Type::Ptr> sorted;

for (auto type : types) {
unsorted.emplace(type.get());
}
std::unordered_set<Type*> visited;

std::function<void(Type*)> visit ([&visit, &unsorted, &visited, &sorted](Type* type) {
if (unsorted.find(type) == unsorted.end()) {
std::function<void(Type*)> visit;
// Please note that this callback does not detect any cyclic load dependencies,
// instead, it relies on the "sort_by_load_after" unit test to fail.
visit = ([&visit, &visited](Type* type) {
if (visited.find(type) != visited.end()) {
return;
}
visited.emplace(type);

bool& alreadyVisited (visited.at(type));
VERIFY(!alreadyVisited);
alreadyVisited = true;

for (auto dep : type->GetLoadDependencies()) {
visit(dep);
for (auto dependency : type->GetLoadDependencies()) {
visit(dependency);
}

unsorted.erase(type);
sorted.emplace_back(type);
// We have managed to reach the final/top node in this dependency graph,
// so let's place them in reverse order to their final place.
l_SortedByLoadDependencies.emplace_back(type);
});

while (!unsorted.empty()) {
for (auto& type : types) {
visited[type.get()] = false;
// Sort the types by their load_after dependencies in a Depth-First search manner.
for (const Type::Ptr& type : Type::GetAllTypes()) {
// Note that only those types that are assignable to the dynamic ConfigObject type can have "load_after"
// dependencies, otherwise they are just some Icinga 2 primitive types such as Number, String, etc. and
// we need to ignore them.
if (ConfigObject::TypeInstance->IsAssignableFrom(type)) {
visit(type.get());
}

visit(*unsorted.begin());
}

VERIFY(sorted.size() == types.size());
VERIFY(sorted[0]->GetLoadDependencies().empty());

std::swap(sorted, l_SortedByLoadDependencies);
l_SortingByLoadDependenciesDone.store(true);
}, InitializePriority::SortTypes);

Expand Down
14 changes: 14 additions & 0 deletions lib/base/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ class Type : public Object
static void Register(const Type::Ptr& type);
static Type::Ptr GetByName(const String& name);
static std::vector<Type::Ptr> GetAllTypes();

/**
* Returns a list of config types sorted by their "load_after" dependencies.
*
* All dependencies of a given type are listed at a lower index than that of the type itself. In other words,
* if a `Service` type load depends on the `Host` and `ApiListener` types, the Host and ApiListener types are
* guaranteed to appear first on the list. Nevertheless, the order of the Host and ApiListener types themselves
* is arbitrary if the two types are not dependent.
*
* It should be noted that this method will fail fatally when used prior to the completion
* of namespace initialization.
*
* @return std::vector<Type::Ptr>
*/
static const std::vector<Ptr>& GetConfigTypesSortedByLoadDependencies();

void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
Expand Down

0 comments on commit 9b5c8c3

Please sign in to comment.