[icinga-checkins] icinga.org: icinga2/support/2.0: Cache parent and child object for dependencies

git at icinga.org git at icinga.org
Tue Jun 24 11:53:39 CEST 2014


Module: icinga2
Branch: support/2.0
Commit: 8d3632caf85b69a306b7f7b50095425eb177eb77
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=8d3632caf85b69a306b7f7b50095425eb177eb77

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Jun 24 11:46:53 2014 +0200

Cache parent and child object for dependencies

fixes #6565

---

 lib/icinga/dependency.cpp |   57 ++++++++++++++++++++++++---------------------
 lib/icinga/dependency.hpp |    3 +++
 2 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/lib/icinga/dependency.cpp b/lib/icinga/dependency.cpp
index e8d906e..fc9dff2 100644
--- a/lib/icinga/dependency.cpp
+++ b/lib/icinga/dependency.cpp
@@ -62,15 +62,39 @@ void Dependency::OnStateLoaded(void)
 
 	ASSERT(!OwnsLock());
 
-	if (!GetChild())
+	Host::Ptr childHost = Host::GetByName(GetChildHostName());
+
+	if (childHost) {
+		if (GetChildServiceName().IsEmpty()) {
+			Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' child host '" + GetChildHostName() + ".");
+			m_Child = childHost;
+		} else {
+			Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' child host '" + GetChildHostName() + "' service '" + GetChildServiceName() + "' .");
+			m_Child = childHost->GetServiceByShortName(GetChildServiceName());
+		}
+	}
+	
+	if (!m_Child)
 		Log(LogWarning, "Dependency", "Dependency '" + GetName() + "' references an invalid child object and will be ignored.");
 	else
-		GetChild()->AddDependency(GetSelf());
-
-	if (!GetParent())
+		m_Child->AddDependency(GetSelf());
+
+	Host::Ptr parentHost = Host::GetByName(GetParentHostName());
+
+	if (parentHost) {
+		if (GetParentServiceName().IsEmpty()) {
+			Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' parent host '" + GetParentHostName() + ".");
+			m_Parent = parentHost;
+		} else {
+			Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' parent host '" + GetParentHostName() + "' service '" + GetParentServiceName() + "' .");
+			m_Parent = parentHost->GetServiceByShortName(GetParentServiceName());
+		}
+	}
+	
+	if (!m_Parent)
 		Log(LogWarning, "Dependency", "Dependency '" + GetName() + "' references an invalid parent object and will always fail.");
 	else
-		GetParent()->AddReverseDependency(GetSelf());
+		m_Parent->AddReverseDependency(GetSelf());
 }
 
 void Dependency::Stop(void)
@@ -149,31 +173,12 @@ bool Dependency::IsAvailable(DependencyType dt) const
 
 Checkable::Ptr Dependency::GetChild(void) const
 {
-	Host::Ptr host = Host::GetByName(GetChildHostName());
-
-	if (!host)
-		return Service::Ptr();
-
-	if (GetChildServiceName().IsEmpty())
-		return host;
-	else
-		return host->GetServiceByShortName(GetChildServiceName());
+	return m_Child;
 }
 
 Checkable::Ptr Dependency::GetParent(void) const
 {
-	Host::Ptr host = Host::GetByName(GetParentHostName());
-
-	if (!host)
-		return Service::Ptr();
-
-	if (GetParentServiceName().IsEmpty()) {
-		Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' parent host '" + GetParentHostName() + ".");
-		return host;
-	} else {
-		Log(LogDebug, "Dependency", "Dependency '" + GetName() + "' parent host '" + GetParentHostName() + "' service '" + GetParentServiceName() + "' .");
-		return host->GetServiceByShortName(GetParentServiceName());
-	}
+	return m_Parent;
 }
 
 TimePeriod::Ptr Dependency::GetPeriod(void) const
diff --git a/lib/icinga/dependency.hpp b/lib/icinga/dependency.hpp
index 4d310c9..08e04e8 100644
--- a/lib/icinga/dependency.hpp
+++ b/lib/icinga/dependency.hpp
@@ -57,6 +57,9 @@ protected:
 	virtual void Stop(void);
 
 private:
+	Checkable::Ptr m_Parent;
+	Checkable::Ptr m_Child;
+
 	static bool EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule);
 	static void EvaluateApplyRule(const ApplyRule& rule);
 	static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);



More information about the icinga-checkins mailing list