Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Dependency#parents #7465

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update DependencyGraph on Dependency#parents changes
refs #1869
  • Loading branch information
Al2Klimov committed Aug 30, 2019
commit f2cd03c4c230cd296adbd97d39f1c3f138a26f52
40 changes: 38 additions & 2 deletions lib/icinga/dependency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
#include "base/defer.hpp"
#include "base/logger.hpp"
#include "base/exception.hpp"
#include <algorithm>
#include <iterator>
#include <set>
#include <sstream>
#include <vector>

using namespace icinga;

Expand Down Expand Up @@ -249,6 +252,38 @@ std::unique_ptr<Dependency::ParentsTree> Dependency::RequireParents(const Value&
}
}

void Dependency::SetParentsTree(std::unique_ptr<ParentsTree> parentsTree)
{
std::vector<Checkable::Ptr> added, removed;

{
std::set<Checkable::Ptr> old, neww;

if (m_ParentsTree) {
m_ParentsTree->GetAllLeavesFlat(old);
}

if (parentsTree) {
parentsTree->GetAllLeavesFlat(neww);
}

std::set_difference(neww.begin(), neww.end(), old.begin(), old.end(), std::inserter(added, added.begin()));
std::set_difference(old.begin(), old.end(), neww.begin(), neww.end(), std::inserter(removed, removed.begin()));
}

for (auto& checkable : added) {
checkable->AddReverseDependency(this);
DependencyGraph::AddDependency(this, checkable.get());
}

for (auto& checkable : removed) {
checkable->RemoveReverseDependency(this);
DependencyGraph::RemoveDependency(this, checkable.get());
}

m_ParentsTree = std::move(parentsTree);
}

void Dependency::OnAllConfigLoaded()
{
ObjectImpl<Dependency>::OnAllConfigLoaded();
Expand Down Expand Up @@ -284,7 +319,7 @@ void Dependency::OnAllConfigLoaded()
auto parents (GetParents());

if (!parents.IsEmpty()) {
m_ParentsTree = RequireParents(parents);
SetParentsTree(RequireParents(parents));
}

m_ParentsTreeValidated = true;
Expand All @@ -296,6 +331,7 @@ void Dependency::Stop(bool runtimeRemoved)

GetChild()->RemoveDependency(this);
GetParent()->RemoveReverseDependency(this);
SetParentsTree(nullptr);
}

bool Dependency::IsAvailable(DependencyType dt) const
Expand Down Expand Up @@ -437,7 +473,7 @@ void Dependency::SetParents(const Value& value, bool suppress_events, const Valu
FreezeRecursively(clone);

if (m_ParentsTreeValidated) {
m_ParentsTree = value.IsEmpty() ? nullptr : RequireParents(clone);
SetParentsTree(value.IsEmpty() ? nullptr : RequireParents(clone));
}

ObjectImpl<Dependency>::SetParents(clone, suppress_events, cookie);
Expand Down
1 change: 1 addition & 0 deletions lib/icinga/dependency.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Dependency final : public ObjectImpl<Dependency>
void BlameInvalidParents(const std::vector<size_t>& currentBranch);
std::unique_ptr<ParentsTree> RequireParents(const Value& parents);
void BlameBadParents(String checkable);
void SetParentsTree(std::unique_ptr<ParentsTree> parentsTree);
};

}
Expand Down