]> granicus.if.org Git - icinga2/commitdiff
Avoid unnecessary updates for the is_active column.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 22 Nov 2013 09:13:42 +0000 (10:13 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 22 Nov 2013 09:13:42 +0000 (10:13 +0100)
Fixes #5124

components/db_ido_mysql/idomysqlconnection.cpp
components/db_ido_pgsql/idopgsqlconnection.cpp
lib/db_ido/dbconnection.cpp
lib/db_ido/dbconnection.h
lib/db_ido/hostdbobject.cpp

index 093fb651b340d14a6804eca9560bf94c06a55976..c99b3f49c86fb917ddc40a1dcf0ad5f8e3bf0386 100644 (file)
@@ -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<long>(m_InstanceID);
+               q1buf << "SELECT object_id, objecttype_id, name1, name2, is_active FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast<long>(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<Dictionary>();
 
        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;
 
index a0eb66df5c17d2fc0d05347a266aa99c77abcb32..8bfaff2e1b5e090d43ef6ae8407acbd9e71d2f06 100644 (file)
@@ -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<long>(m_InstanceID);
+               q1buf << "SELECT object_id, objecttype_id, name1, name2, is_active FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast<long>(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;
 
index e325ea59f094d88a9d22a7849137f4110dec1780..cc32870272560d5a7129dc0c9d39d62296b1eb37 100644 (file)
@@ -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();
                        }
index 1e5decac5e6819e3c04f40b54ed33d2937ea6e30..8bb450900d526142b91af8588999dabba46863e9 100644 (file)
@@ -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<DbObject::Ptr, DbReference> m_ObjectIDs;
        std::map<DbObject::Ptr, DbReference> m_InsertIDs;
+       std::set<DbObject::Ptr> m_ActiveObjects;
        std::set<DbObject::Ptr> m_ConfigUpdates;
        std::set<DbObject::Ptr> m_StatusUpdates;
        Timer::Ptr m_CleanUpTimer;
index b95513d968b5573d0eb9261c7bb805bf111daf24..801e9ff7e4e429f343a163aee0590bde1841ff17 100644 (file)
@@ -194,22 +194,6 @@ void HostDbObject::OnConfigUpdate(void)
        Host::Ptr host = static_pointer_cast<Host>(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<Dictionary>();
-       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<Dictionary>();
-       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<Dictionary>();
-       query_del3.WhereCriteria->Set("object_id", host);
-       OnQuery(query_del3);
-
        Dictionary::Ptr customvars;
        {
                ObjectLock olock(host);