From e020a80bf8aaeace76a2f912c171b74b6a425e9d Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 30 Oct 2013 14:07:26 +0100 Subject: [PATCH 1/3] Clean up DbConnection / IdoMysqlConnection. Refs #4969 --- components/db_ido_mysql/idomysqlconnection.cpp | 13 ++++++------- lib/db_ido/dbconnection.h | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/components/db_ido_mysql/idomysqlconnection.cpp b/components/db_ido_mysql/idomysqlconnection.cpp index b6a0a3749..ce2c0bf7e 100644 --- a/components/db_ido_mysql/idomysqlconnection.cpp +++ b/components/db_ido_mysql/idomysqlconnection.cpp @@ -65,7 +65,7 @@ void IdoMysqlConnection::Stop(void) void IdoMysqlConnection::AssertOnWorkQueue(void) { - VERIFY(boost::this_thread::get_id() == m_QueryQueue.GetThreadId()); + ASSERT(boost::this_thread::get_id() == m_QueryQueue.GetThreadId()); } void IdoMysqlConnection::Disconnect(void) @@ -570,12 +570,12 @@ void IdoMysqlConnection::InternalExecuteQuery(const DbQuery& query) } } -void IdoMysqlConnection::CleanUpExecuteQuery(const String& table, const String& time_key, double time_value) +void IdoMysqlConnection::CleanUpExecuteQuery(const String& table, const String& time_column, double max_age) { - m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::InternalCleanUpExecuteQuery, this, table, time_key, time_value)); + m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::InternalCleanUpExecuteQuery, this, table, time_column, max_age)); } -void IdoMysqlConnection::InternalCleanUpExecuteQuery(const String& table, const String& time_key, double time_value) +void IdoMysqlConnection::InternalCleanUpExecuteQuery(const String& table, const String& time_column, double max_age) { boost::mutex::scoped_lock lock(m_ConnectionMutex); @@ -583,7 +583,6 @@ void IdoMysqlConnection::InternalCleanUpExecuteQuery(const String& table, const return; Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " + - Convert::ToString(static_cast(m_InstanceID)) + " AND " + time_key + - "(time_value)) + ")"); + Convert::ToString(static_cast(m_InstanceID)) + " AND " + time_column + + " < FROM_UNIXTIME(" + Convert::ToString(static_cast(max_age)) + ")"); } - diff --git a/lib/db_ido/dbconnection.h b/lib/db_ido/dbconnection.h index 2fbfbf58a..94f870d98 100644 --- a/lib/db_ido/dbconnection.h +++ b/lib/db_ido/dbconnection.h @@ -75,7 +75,7 @@ protected: virtual void ActivateObject(const DbObject::Ptr& dbobj) = 0; virtual void DeactivateObject(const DbObject::Ptr& dbobj) = 0; - virtual void CleanUpExecuteQuery(const String& table, const String& time_key, double time_value) = 0; + virtual void CleanUpExecuteQuery(const String& table, const String& time_column, double max_age); void UpdateAllObjects(void); From 00c596665a3f2a66d4076453685dddd5eb4c8039 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 30 Oct 2013 14:07:50 +0100 Subject: [PATCH 2/3] Fix incorrect column name in the documentation. Refs #4969 --- doc/4.3-object-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/4.3-object-types.md b/doc/4.3-object-types.md index 3a7c98c12..4916abc55 100644 --- a/doc/4.3-object-types.md +++ b/doc/4.3-object-types.md @@ -547,7 +547,7 @@ Cleanup Items: Name | Description ----------------|---------------- - acknowledgement_age |**Optional.** Max age for acknowledgement table rows (entry_time). Defaults to 0 (never). + acknowledgements_age |**Optional.** Max age for acknowledgements table rows (entry_time). Defaults to 0 (never). commenthistory_age |**Optional.** Max age for commenthistory table rows (entry_time). Defaults to 0 (never). contactnotifications_age |**Optional.** Max age for contactnotifications table rows (start_time). Defaults to 0 (never). contactnotificationmethods_age |**Optional.** Max age for contactnotificationmethods table rows (start_time). Defaults to 0 (never). From e73197bc2383879f471313e664914e6fefa64795 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 30 Oct 2013 14:08:09 +0100 Subject: [PATCH 3/3] Refactor DbConnection::CleanUpHandler. Refs #4969 --- lib/db_ido/dbconnection.cpp | 196 ++++++------------------------------ lib/db_ido/dbconnection.h | 16 --- 2 files changed, 32 insertions(+), 180 deletions(-) diff --git a/lib/db_ido/dbconnection.cpp b/lib/db_ido/dbconnection.cpp index 1d1b35e09..d9f819446 100644 --- a/lib/db_ido/dbconnection.cpp +++ b/lib/db_ido/dbconnection.cpp @@ -118,178 +118,46 @@ void DbConnection::CleanUpHandler(void) { long now = static_cast(Utility::GetTime()); - if (GetCleanupAcknowledgementsAge() > 0) { - CleanUpExecuteQuery("acknowledgements", "entry_time", now - GetCleanupAcknowledgementsAge()); - Log(LogDebug, "db_ido", "GetCleanupAcknowledgementsAge: " + Convert::ToString(GetCleanupAcknowledgementsAge()) + + struct { + String name; + String time_column; + } tables[] = { + { "acknowledgements", "entry_time" }, + { "commenthistory", "entry_time" }, + { "contactnotifications", "start_time" }, + { "contactnotificationmethods", "start_time" }, + { "downtimehistory", "entry_time" }, + { "eventhandlers", "start_time" }, + { "externalcommands", "entry_time" }, + { "flappinghistory" "event_time" }, + { "hostchecks", "start_time" }, + { "logentries", "logentry_time" }, + { "notifications", "start_time" }, + { "processevents", "event_time" }, + { "statehistory", "state_time" }, + { "servicechecks", "start_time" }, + { "systemcommands", "start_time" } + }; + + for (int i = 0; i < sizeof(tables) / sizeof(tables[0]); i++) { + double max_age = GetCleanup()->Get(tables[i].name + "_age"); + + if (max_age == 0) + continue; + + CleanUpExecuteQuery(tables[i].name, tables[i].time_column, now - max_age); + Log(LogDebug, "db_ido", "Cleanup (" + tables[i].name + "): " + Convert::ToString(max_age) + " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupAcknowledgementsAge())); - } - if (GetCleanupCommentHistoryAge() > 0) { - CleanUpExecuteQuery("commenthistory", "entry_time", now - GetCleanupCommentHistoryAge()); - Log(LogDebug, "db_ido", "GetCleanupCommentHistoryAge: " + Convert::ToString(GetCleanupCommentHistoryAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupCommentHistoryAge())); - } - if (GetCleanupContactNotificationsAge() > 0) { - CleanUpExecuteQuery("contactnotifications", "start_time", now - GetCleanupContactNotificationsAge()); - Log(LogDebug, "db_ido", "GetCleanupContactNotificationsAge: " + Convert::ToString(GetCleanupContactNotificationsAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupContactNotificationsAge())); - } - if (GetCleanupContactNotificationMethodsAge() > 0) { - CleanUpExecuteQuery("contactnotificationmethods", "start_time", now - GetCleanupContactNotificationMethodsAge()); - Log(LogDebug, "db_ido", "GetCleanupContactNotificationMethodsAge: " + Convert::ToString(GetCleanupContactNotificationMethodsAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupContactNotificationMethodsAge())); - } - if (GetCleanupDowntimeHistoryAge() > 0) { - CleanUpExecuteQuery("downtimehistory", "entry_time", now - GetCleanupDowntimeHistoryAge()); - Log(LogDebug, "db_ido", "CleanUpDowntimeHistoryAge: " + Convert::ToString(GetCleanupDowntimeHistoryAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupDowntimeHistoryAge())); - } - if (GetCleanupEventHandlersAge() > 0) { - CleanUpExecuteQuery("eventhandlers", "start_time", now - GetCleanupEventHandlersAge()); - Log(LogDebug, "db_ido", "GetCleanupEventHandlersAge: " + Convert::ToString(GetCleanupEventHandlersAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupEventHandlersAge())); - } - if (GetCleanupExternalCommandsAge() > 0) { - CleanUpExecuteQuery("externalcommands", "entry_time", now - GetCleanupExternalCommandsAge()); - Log(LogDebug, "db_ido", "GetCleanupExternalCommandsAge: " + Convert::ToString(GetCleanupExternalCommandsAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupExternalCommandsAge())); - } - if (GetCleanupFlappingHistoryAge() > 0) { - CleanUpExecuteQuery("flappinghistory", "event_time", now - GetCleanupFlappingHistoryAge()); - Log(LogDebug, "db_ido", "GetCleanupFlappingHistoryAge: " + Convert::ToString(GetCleanupFlappingHistoryAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupFlappingHistoryAge())); - } - if (GetCleanupHostChecksAge() > 0) { - CleanUpExecuteQuery("hostchecks", "start_time", now - GetCleanupHostChecksAge()); - Log(LogDebug, "db_ido", "GetCleanupHostChecksAge: " + Convert::ToString(GetCleanupHostChecksAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupHostChecksAge())); - } - if (GetCleanupLogEntriesAge() > 0) { - CleanUpExecuteQuery("logentries", "logentry_time", now - GetCleanupLogEntriesAge()); - Log(LogDebug, "db_ido", "GetCleanupLogEntriesAge: " + Convert::ToString(GetCleanupLogEntriesAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupLogEntriesAge())); - } - if (GetCleanupNotificationsAge() > 0) { - CleanUpExecuteQuery("notifications", "start_time", now - GetCleanupNotificationsAge()); - Log(LogDebug, "db_ido", "GetCleanupNotificationsAge: " + Convert::ToString(GetCleanupNotificationsAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupNotificationsAge())); - } - if (GetCleanupProcessEventsAge() > 0) { - CleanUpExecuteQuery("processevents", "event_time", now - GetCleanupProcessEventsAge()); - Log(LogDebug, "db_ido", "GetCleanupProcessEventsAge: " + Convert::ToString(GetCleanupProcessEventsAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupProcessEventsAge())); - } - if (GetCleanupStateHistoryAge() > 0) { - CleanUpExecuteQuery("statehistory", "state_time", now - GetCleanupStateHistoryAge()); - Log(LogDebug, "db_ido", "GetCleanupStateHistoryAge: " + Convert::ToString(GetCleanupStateHistoryAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupStateHistoryAge())); - } - if (GetCleanupServiceChecksAge() > 0) { - CleanUpExecuteQuery("servicechecks", "start_time", now - GetCleanupServiceChecksAge()); - Log(LogDebug, "db_ido", "GetCleanupServiceChecksAge: " + Convert::ToString(GetCleanupServiceChecksAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupServiceChecksAge())); - } - if (GetCleanupSystemCommandsAge() > 0) { - CleanUpExecuteQuery("systemcommands", "start_time", now - GetCleanupSystemCommandsAge()); - Log(LogDebug, "db_ido", "GetCleanupSystemCommandsAge: " + Convert::ToString(GetCleanupSystemCommandsAge()) + - " now: " + Convert::ToString(now) + - " old: " + Convert::ToString(now - GetCleanupSystemCommandsAge())); + " old: " + Convert::ToString(now - max_age)); } + } -void DbConnection::CleanUpExecuteQuery(const String& table, const String& time_key, double time_value) +void DbConnection::CleanUpExecuteQuery(const String& table, const String& time_column, double max_age) { /* Default handler does nothing. */ } -double DbConnection::GetCleanupAcknowledgementsAge(void) const -{ - return GetCleanup()->Get("acknowledgement_age"); -} - -double DbConnection::GetCleanupCommentHistoryAge(void) const -{ - return GetCleanup()->Get("commenthistory_age"); -} - -double DbConnection::GetCleanupContactNotificationsAge(void) const -{ - return GetCleanup()->Get("contactnotifications_age"); -} - -double DbConnection::GetCleanupContactNotificationMethodsAge(void) const -{ - return GetCleanup()->Get("contactnotificationmethods_age"); -} - -double DbConnection::GetCleanupDowntimeHistoryAge(void) const -{ - return GetCleanup()->Get("downtimehistory_age"); -} - -double DbConnection::GetCleanupEventHandlersAge(void) const -{ - return GetCleanup()->Get("eventhandlers_age"); -} - -double DbConnection::GetCleanupExternalCommandsAge(void) const -{ - return GetCleanup()->Get("externalcommands_age"); -} - -double DbConnection::GetCleanupFlappingHistoryAge(void) const -{ - return GetCleanup()->Get("flappinghistory_age"); -} - -double DbConnection::GetCleanupHostChecksAge(void) const -{ - return GetCleanup()->Get("hostchecks_age"); -} - -double DbConnection::GetCleanupLogEntriesAge(void) const -{ - return GetCleanup()->Get("logentries_age"); -} - -double DbConnection::GetCleanupNotificationsAge(void) const -{ - return GetCleanup()->Get("notifications_age"); -} - -double DbConnection::GetCleanupProcessEventsAge(void) const -{ - return GetCleanup()->Get("processevents_age"); -} - -double DbConnection::GetCleanupStateHistoryAge(void) const -{ - return GetCleanup()->Get("statehistory_age"); -} - -double DbConnection::GetCleanupServiceChecksAge(void) const -{ - return GetCleanup()->Get("servicechecks_age"); -} - -double DbConnection::GetCleanupSystemCommandsAge(void) const -{ - return GetCleanup()->Get("systemcommands_age"); -} - void DbConnection::SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref) { if (dbref.IsValid()) diff --git a/lib/db_ido/dbconnection.h b/lib/db_ido/dbconnection.h index 94f870d98..503793eff 100644 --- a/lib/db_ido/dbconnection.h +++ b/lib/db_ido/dbconnection.h @@ -52,22 +52,6 @@ public: void SetStatusUpdate(const DbObject::Ptr& dbobj, bool hasupdate); bool GetStatusUpdate(const DbObject::Ptr& dbobj) const; - double GetCleanupAcknowledgementsAge(void) const; - double GetCleanupCommentHistoryAge(void) const; - double GetCleanupContactNotificationsAge(void) const; - double GetCleanupContactNotificationMethodsAge(void) const; - double GetCleanupDowntimeHistoryAge(void) const; - double GetCleanupEventHandlersAge(void) const; - double GetCleanupExternalCommandsAge(void) const; - double GetCleanupFlappingHistoryAge(void) const; - double GetCleanupHostChecksAge(void) const; - double GetCleanupLogEntriesAge(void) const; - double GetCleanupNotificationsAge(void) const; - double GetCleanupProcessEventsAge(void) const; - double GetCleanupStateHistoryAge(void) const; - double GetCleanupServiceChecksAge(void) const; - double GetCleanupSystemCommandsAge(void) const; - protected: virtual void Start(void);