From b9154941573101b8abd35d915f06d53a3dbe9123 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 22 Nov 2013 10:13:42 +0100 Subject: [PATCH] Avoid unnecessary updates for the is_active column. Fixes #5124 --- .../db_ido_mysql/idomysqlconnection.cpp | 26 +++++++------------ .../db_ido_pgsql/idopgsqlconnection.cpp | 16 ++++++------ lib/db_ido/dbconnection.cpp | 18 ++++++++++++- lib/db_ido/dbconnection.h | 4 +++ lib/db_ido/hostdbobject.cpp | 24 ----------------- 5 files changed, 39 insertions(+), 49 deletions(-) diff --git a/components/db_ido_mysql/idomysqlconnection.cpp b/components/db_ido_mysql/idomysqlconnection.cpp index 093fb651b..c99b3f49c 100644 --- a/components/db_ido_mysql/idomysqlconnection.cpp +++ b/components/db_ido_mysql/idomysqlconnection.cpp @@ -206,10 +206,8 @@ void IdoMysqlConnection::Reconnect(void) /* clear config tables for the initial config dump */ ClearConfigTables(); - Query("UPDATE " + GetTablePrefix() + "objects SET is_active = 0"); - std::ostringstream q1buf; - q1buf << "SELECT object_id, objecttype_id, name1, name2 FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast(m_InstanceID); + q1buf << "SELECT object_id, objecttype_id, name1, name2, is_active FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast(m_InstanceID); rows = Query(q1buf.str()); ObjectLock olock(rows); @@ -221,6 +219,7 @@ void IdoMysqlConnection::Reconnect(void) DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2")); SetObjectID(dbobj, DbReference(row->Get("object_id"))); + SetObjectActive(dbobj, row->Get("is_active")); } Query("BEGIN"); @@ -239,7 +238,9 @@ void IdoMysqlConnection::ClearConfigTables(void) ClearConfigTable("contactgroup_members"); ClearConfigTable("contactgroups"); ClearConfigTable("contacts"); + ClearConfigTable("contactstatus"); ClearConfigTable("customvariables"); + ClearConfigTable("customvariablestatus"); ClearConfigTable("host_contactgroups"); ClearConfigTable("host_contacts"); ClearConfigTable("host_parenthosts"); @@ -247,6 +248,8 @@ void IdoMysqlConnection::ClearConfigTables(void) ClearConfigTable("hostgroup_members"); ClearConfigTable("hostgroups"); ClearConfigTable("hosts"); + ClearConfigTable("hoststatus"); + ClearConfigTable("programstatus"); ClearConfigTable("scheduleddowntime"); ClearConfigTable("service_contactgroups"); ClearConfigTable("service_contacts"); @@ -254,6 +257,7 @@ void IdoMysqlConnection::ClearConfigTables(void) ClearConfigTable("servicegroup_members"); ClearConfigTable("servicegroups"); ClearConfigTable("services"); + ClearConfigTable("servicestatus"); ClearConfigTable("timeperiod_timeranges"); ClearConfigTable("timeperiods"); } @@ -349,14 +353,8 @@ Dictionary::Ptr IdoMysqlConnection::FetchRow(MYSQL_RES *result) Dictionary::Ptr dict = make_shared(); mysql_field_seek(result, 0); - for (field = mysql_fetch_field(result), i = 0; field; field = mysql_fetch_field(result), i++) { - Value value; - - if (field) - value = String(row[i], row[i] + lengths[i]); - - dict->Set(field->name, value); - } + for (field = mysql_fetch_field(result), i = 0; field; field = mysql_fetch_field(result), i++) + dict->Set(field->name, String(row[i], row[i] + lengths[i])); return dict; } @@ -519,12 +517,8 @@ void IdoMysqlConnection::InternalExecuteQuery(const DbQuery& query) if (hasid) type = DbQueryUpdate; - else { - if (query.WhereCriteria) - Query("DELETE FROM " + GetTablePrefix() + query.Table + where.str()); - + else type = DbQueryInsert; - } } else type = query.Type; diff --git a/components/db_ido_pgsql/idopgsqlconnection.cpp b/components/db_ido_pgsql/idopgsqlconnection.cpp index a0eb66df5..8bfaff2e1 100644 --- a/components/db_ido_pgsql/idopgsqlconnection.cpp +++ b/components/db_ido_pgsql/idopgsqlconnection.cpp @@ -210,10 +210,8 @@ void IdoPgsqlConnection::Reconnect(void) /* clear config tables for the initial config dump */ ClearConfigTables(); - Query("UPDATE " + GetTablePrefix() + "objects SET is_active = 0"); - std::ostringstream q1buf; - q1buf << "SELECT object_id, objecttype_id, name1, name2 FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast(m_InstanceID); + q1buf << "SELECT object_id, objecttype_id, name1, name2, is_active FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast(m_InstanceID); rows = Query(q1buf.str()); ObjectLock olock(rows); @@ -225,6 +223,7 @@ void IdoPgsqlConnection::Reconnect(void) DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2")); SetObjectID(dbobj, DbReference(row->Get("object_id"))); + SetObjectActive(dbobj, row->Get("is_active")); } Query("BEGIN"); @@ -243,7 +242,9 @@ void IdoPgsqlConnection::ClearConfigTables(void) ClearConfigTable("contactgroup_members"); ClearConfigTable("contactgroups"); ClearConfigTable("contacts"); + ClearConfigTable("contactstatus"); ClearConfigTable("customvariables"); + ClearConfigTable("customvariablestatus"); ClearConfigTable("host_contactgroups"); ClearConfigTable("host_contacts"); ClearConfigTable("host_parenthosts"); @@ -251,6 +252,8 @@ void IdoPgsqlConnection::ClearConfigTables(void) ClearConfigTable("hostgroup_members"); ClearConfigTable("hostgroups"); ClearConfigTable("hosts"); + ClearConfigTable("hoststatus"); + ClearConfigTable("programstatus"); ClearConfigTable("scheduleddowntime"); ClearConfigTable("service_contactgroups"); ClearConfigTable("service_contacts"); @@ -258,6 +261,7 @@ void IdoPgsqlConnection::ClearConfigTables(void) ClearConfigTable("servicegroup_members"); ClearConfigTable("servicegroups"); ClearConfigTable("services"); + ClearConfigTable("servicestatus"); ClearConfigTable("timeperiod_timeranges"); ClearConfigTable("timeperiods"); } @@ -529,12 +533,8 @@ void IdoPgsqlConnection::InternalExecuteQuery(const DbQuery& query) if (hasid) type = DbQueryUpdate; - else { - if (query.WhereCriteria) - Query("DELETE FROM " + GetTablePrefix() + query.Table + where.str()); - + else type = DbQueryInsert; - } } else type = query.Type; diff --git a/lib/db_ido/dbconnection.cpp b/lib/db_ido/dbconnection.cpp index e325ea59f..cc3287027 100644 --- a/lib/db_ido/dbconnection.cpp +++ b/lib/db_ido/dbconnection.cpp @@ -201,10 +201,24 @@ DbReference DbConnection::GetInsertID(const DbObject::Ptr& dbobj) const return it->second; } +void DbConnection::SetObjectActive(const DbObject::Ptr& dbobj, bool active) +{ + if (active) + m_ActiveObjects.insert(dbobj); + else + m_ActiveObjects.erase(dbobj); +} + +bool DbConnection::GetObjectActive(const DbObject::Ptr& dbobj) const +{ + return (m_ActiveObjects.find(dbobj) != m_ActiveObjects.end()); +} + void DbConnection::ClearIDCache(void) { m_ObjectIDs.clear(); m_InsertIDs.clear(); + m_ActiveObjects.clear(); } void DbConnection::SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate) @@ -246,7 +260,9 @@ void DbConnection::UpdateAllObjects(void) DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object); if (dbobj) { - ActivateObject(dbobj); + if (!GetObjectActive(dbobj)) + ActivateObject(dbobj); + dbobj->SendConfigUpdate(); dbobj->SendStatusUpdate(); } diff --git a/lib/db_ido/dbconnection.h b/lib/db_ido/dbconnection.h index 1e5decac5..8bb450900 100644 --- a/lib/db_ido/dbconnection.h +++ b/lib/db_ido/dbconnection.h @@ -47,6 +47,9 @@ public: void SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref); DbReference GetInsertID(const DbObject::Ptr& dbobj) const; + void SetObjectActive(const DbObject::Ptr& dbobj, bool active); + bool GetObjectActive(const DbObject::Ptr& dbobj) const; + void ClearIDCache(void); void SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate); @@ -69,6 +72,7 @@ protected: private: std::map m_ObjectIDs; std::map m_InsertIDs; + std::set m_ActiveObjects; std::set m_ConfigUpdates; std::set m_StatusUpdates; Timer::Ptr m_CleanUpTimer; diff --git a/lib/db_ido/hostdbobject.cpp b/lib/db_ido/hostdbobject.cpp index b95513d96..801e9ff7e 100644 --- a/lib/db_ido/hostdbobject.cpp +++ b/lib/db_ido/hostdbobject.cpp @@ -194,22 +194,6 @@ void HostDbObject::OnConfigUpdate(void) Host::Ptr host = static_pointer_cast(GetObject()); /* parents, host dependencies */ - DbQuery query_del1; - query_del1.Table = GetType()->GetTable() + "_parenthosts"; - query_del1.Type = DbQueryDelete; - query_del1.Category = DbCatConfig; - query_del1.WhereCriteria = make_shared(); - query_del1.WhereCriteria->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject())); - OnQuery(query_del1); - - DbQuery query_del2; - query_del2.Table = GetType()->GetTable() + "dependencies"; - query_del2.Type = DbQueryDelete; - query_del2.Category = DbCatConfig; - query_del2.WhereCriteria = make_shared(); - query_del2.WhereCriteria->Set("dependent_host_object_id", host); - OnQuery(query_del2); - BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) { Log(LogDebug, "db_ido", "host parents: " + parent->GetName()); @@ -284,14 +268,6 @@ void HostDbObject::OnConfigUpdate(void) /* custom variables */ Log(LogDebug, "ido", "host customvars for '" + host->GetName() + "'"); - DbQuery query_del3; - query_del3.Table = "customvariables"; - query_del3.Type = DbQueryDelete; - query_del3.Category = DbCatConfig; - query_del3.WhereCriteria = make_shared(); - query_del3.WhereCriteria->Set("object_id", host); - OnQuery(query_del3); - Dictionary::Ptr customvars; { ObjectLock olock(host);