From 14084735da90e5f4dcc250876ccb3cccf21b7919 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 29 Mar 2014 23:18:31 +0100 Subject: [PATCH] Make sure we evaluate "apply" rules in the right order. Refs #5870 --- lib/config/applyrule.cpp | 36 +++++++++++++++++++------- lib/config/applyrule.h | 4 +-- lib/icinga/dependency-apply.cpp | 2 +- lib/icinga/notification-apply.cpp | 2 +- lib/icinga/scheduleddowntime-apply.cpp | 2 +- lib/icinga/service-apply.cpp | 2 +- 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/config/applyrule.cpp b/lib/config/applyrule.cpp index fc959107f..58c58662d 100644 --- a/lib/config/applyrule.cpp +++ b/lib/config/applyrule.cpp @@ -69,21 +69,39 @@ bool ApplyRule::EvaluateFilter(const Dictionary::Ptr& scope) const void ApplyRule::EvaluateRules(void) { - // TODO: priority - std::pair > kv; - BOOST_FOREACH(kv, m_Callbacks) { - RuleMap::const_iterator it = m_Rules.find(kv.first); + std::set completedTypes; - if (it == m_Rules.end()) - continue; + while (completedTypes.size() < m_Callbacks.size()) { + std::pair > kv; + BOOST_FOREACH(kv, m_Callbacks) { + const String& sourceType = kv.first; - kv.second.first(it->second); + if (completedTypes.find(sourceType) != completedTypes.end()) + continue; + + const Callback& callback = kv.second.first; + const String& targetType = kv.second.second; + + if (IsValidType(targetType) && completedTypes.find(targetType) == completedTypes.end()) + continue; + + completedTypes.insert(sourceType); + + RuleMap::const_iterator it = m_Rules.find(kv.first); + + if (it == m_Rules.end()) + continue; + + callback(it->second); + } } + + m_Rules.clear(); } -void ApplyRule::RegisterType(const String& sourceType, const ApplyRule::Callback& callback, int priority) +void ApplyRule::RegisterType(const String& sourceType, const String& targetType, const ApplyRule::Callback& callback) { - m_Callbacks[sourceType] = make_pair(callback, priority); + m_Callbacks[sourceType] = make_pair(callback, targetType); } bool ApplyRule::IsValidType(const String& sourceType) diff --git a/lib/config/applyrule.h b/lib/config/applyrule.h index b7eac024a..90ebeecdf 100644 --- a/lib/config/applyrule.h +++ b/lib/config/applyrule.h @@ -35,7 +35,7 @@ class I2_CONFIG_API ApplyRule { public: typedef boost::function& rules)> Callback; - typedef std::map > CallbackMap; + typedef std::map > CallbackMap; typedef std::map > RuleMap; String GetName(void) const; @@ -50,7 +50,7 @@ public: const AExpression::Ptr& filter, const DebugInfo& di, const Dictionary::Ptr& scope); static void EvaluateRules(void); - static void RegisterType(const String& sourceType, const ApplyRule::Callback& callback, int priority); + static void RegisterType(const String& sourceType, const String& targetType, const ApplyRule::Callback& callback); static bool IsValidType(const String& sourceType); private: diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp index 989090e0f..24d614206 100644 --- a/lib/icinga/dependency-apply.cpp +++ b/lib/icinga/dependency-apply.cpp @@ -32,7 +32,7 @@ INITIALIZE_ONCE(&Dependency::RegisterApplyRuleHandler); void Dependency::RegisterApplyRuleHandler(void) { - ApplyRule::RegisterType("Dependency", &Dependency::EvaluateApplyRules, 2); + ApplyRule::RegisterType("Dependency", "Service", &Dependency::EvaluateApplyRules); } void Dependency::EvaluateApplyRules(const std::vector& rules) diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp index b5390bebe..48e6bdd22 100644 --- a/lib/icinga/notification-apply.cpp +++ b/lib/icinga/notification-apply.cpp @@ -32,7 +32,7 @@ INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler); void Notification::RegisterApplyRuleHandler(void) { - ApplyRule::RegisterType("Notification", &Notification::EvaluateApplyRules, 2); + ApplyRule::RegisterType("Notification", "Service", &Notification::EvaluateApplyRules); } void Notification::EvaluateApplyRules(const std::vector& rules) diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp index f7ad097ef..3ade63bf0 100644 --- a/lib/icinga/scheduleddowntime-apply.cpp +++ b/lib/icinga/scheduleddowntime-apply.cpp @@ -32,7 +32,7 @@ INITIALIZE_ONCE(&ScheduledDowntime::RegisterApplyRuleHandler); void ScheduledDowntime::RegisterApplyRuleHandler(void) { - ApplyRule::RegisterType("ScheduledDowntime", &ScheduledDowntime::EvaluateApplyRules, 2); + ApplyRule::RegisterType("ScheduledDowntime", "Service", &ScheduledDowntime::EvaluateApplyRules); } void ScheduledDowntime::EvaluateApplyRules(const std::vector& rules) diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp index 61aa9fe4b..89044285c 100644 --- a/lib/icinga/service-apply.cpp +++ b/lib/icinga/service-apply.cpp @@ -32,7 +32,7 @@ INITIALIZE_ONCE(&Service::RegisterApplyRuleHandler); void Service::RegisterApplyRuleHandler(void) { - ApplyRule::RegisterType("Service", &Service::EvaluateApplyRules, 1); + ApplyRule::RegisterType("Service", "Host", &Service::EvaluateApplyRules); } void Service::EvaluateApplyRules(const std::vector& rules)