Avoid unnecessary updates for the is_active column.

Fixes #5124
This commit is contained in:
Gunnar Beutner 2013-11-22 10:13:42 +01:00
parent 0978e46d5a
commit b915494157
5 changed files with 39 additions and 49 deletions

View File

@ -206,10 +206,8 @@ void IdoMysqlConnection::Reconnect(void)
/* clear config tables for the initial config dump */ /* clear config tables for the initial config dump */
ClearConfigTables(); ClearConfigTables();
Query("UPDATE " + GetTablePrefix() + "objects SET is_active = 0");
std::ostringstream q1buf; 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()); rows = Query(q1buf.str());
ObjectLock olock(rows); ObjectLock olock(rows);
@ -221,6 +219,7 @@ void IdoMysqlConnection::Reconnect(void)
DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2")); DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
SetObjectID(dbobj, DbReference(row->Get("object_id"))); SetObjectID(dbobj, DbReference(row->Get("object_id")));
SetObjectActive(dbobj, row->Get("is_active"));
} }
Query("BEGIN"); Query("BEGIN");
@ -239,7 +238,9 @@ void IdoMysqlConnection::ClearConfigTables(void)
ClearConfigTable("contactgroup_members"); ClearConfigTable("contactgroup_members");
ClearConfigTable("contactgroups"); ClearConfigTable("contactgroups");
ClearConfigTable("contacts"); ClearConfigTable("contacts");
ClearConfigTable("contactstatus");
ClearConfigTable("customvariables"); ClearConfigTable("customvariables");
ClearConfigTable("customvariablestatus");
ClearConfigTable("host_contactgroups"); ClearConfigTable("host_contactgroups");
ClearConfigTable("host_contacts"); ClearConfigTable("host_contacts");
ClearConfigTable("host_parenthosts"); ClearConfigTable("host_parenthosts");
@ -247,6 +248,8 @@ void IdoMysqlConnection::ClearConfigTables(void)
ClearConfigTable("hostgroup_members"); ClearConfigTable("hostgroup_members");
ClearConfigTable("hostgroups"); ClearConfigTable("hostgroups");
ClearConfigTable("hosts"); ClearConfigTable("hosts");
ClearConfigTable("hoststatus");
ClearConfigTable("programstatus");
ClearConfigTable("scheduleddowntime"); ClearConfigTable("scheduleddowntime");
ClearConfigTable("service_contactgroups"); ClearConfigTable("service_contactgroups");
ClearConfigTable("service_contacts"); ClearConfigTable("service_contacts");
@ -254,6 +257,7 @@ void IdoMysqlConnection::ClearConfigTables(void)
ClearConfigTable("servicegroup_members"); ClearConfigTable("servicegroup_members");
ClearConfigTable("servicegroups"); ClearConfigTable("servicegroups");
ClearConfigTable("services"); ClearConfigTable("services");
ClearConfigTable("servicestatus");
ClearConfigTable("timeperiod_timeranges"); ClearConfigTable("timeperiod_timeranges");
ClearConfigTable("timeperiods"); ClearConfigTable("timeperiods");
} }
@ -349,14 +353,8 @@ Dictionary::Ptr IdoMysqlConnection::FetchRow(MYSQL_RES *result)
Dictionary::Ptr dict = make_shared<Dictionary>(); Dictionary::Ptr dict = make_shared<Dictionary>();
mysql_field_seek(result, 0); mysql_field_seek(result, 0);
for (field = mysql_fetch_field(result), i = 0; field; field = mysql_fetch_field(result), i++) { for (field = mysql_fetch_field(result), i = 0; field; field = mysql_fetch_field(result), i++)
Value value; dict->Set(field->name, String(row[i], row[i] + lengths[i]));
if (field)
value = String(row[i], row[i] + lengths[i]);
dict->Set(field->name, value);
}
return dict; return dict;
} }
@ -519,12 +517,8 @@ void IdoMysqlConnection::InternalExecuteQuery(const DbQuery& query)
if (hasid) if (hasid)
type = DbQueryUpdate; type = DbQueryUpdate;
else { else
if (query.WhereCriteria)
Query("DELETE FROM " + GetTablePrefix() + query.Table + where.str());
type = DbQueryInsert; type = DbQueryInsert;
}
} else } else
type = query.Type; type = query.Type;

View File

@ -210,10 +210,8 @@ void IdoPgsqlConnection::Reconnect(void)
/* clear config tables for the initial config dump */ /* clear config tables for the initial config dump */
ClearConfigTables(); ClearConfigTables();
Query("UPDATE " + GetTablePrefix() + "objects SET is_active = 0");
std::ostringstream q1buf; 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()); rows = Query(q1buf.str());
ObjectLock olock(rows); ObjectLock olock(rows);
@ -225,6 +223,7 @@ void IdoPgsqlConnection::Reconnect(void)
DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2")); DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
SetObjectID(dbobj, DbReference(row->Get("object_id"))); SetObjectID(dbobj, DbReference(row->Get("object_id")));
SetObjectActive(dbobj, row->Get("is_active"));
} }
Query("BEGIN"); Query("BEGIN");
@ -243,7 +242,9 @@ void IdoPgsqlConnection::ClearConfigTables(void)
ClearConfigTable("contactgroup_members"); ClearConfigTable("contactgroup_members");
ClearConfigTable("contactgroups"); ClearConfigTable("contactgroups");
ClearConfigTable("contacts"); ClearConfigTable("contacts");
ClearConfigTable("contactstatus");
ClearConfigTable("customvariables"); ClearConfigTable("customvariables");
ClearConfigTable("customvariablestatus");
ClearConfigTable("host_contactgroups"); ClearConfigTable("host_contactgroups");
ClearConfigTable("host_contacts"); ClearConfigTable("host_contacts");
ClearConfigTable("host_parenthosts"); ClearConfigTable("host_parenthosts");
@ -251,6 +252,8 @@ void IdoPgsqlConnection::ClearConfigTables(void)
ClearConfigTable("hostgroup_members"); ClearConfigTable("hostgroup_members");
ClearConfigTable("hostgroups"); ClearConfigTable("hostgroups");
ClearConfigTable("hosts"); ClearConfigTable("hosts");
ClearConfigTable("hoststatus");
ClearConfigTable("programstatus");
ClearConfigTable("scheduleddowntime"); ClearConfigTable("scheduleddowntime");
ClearConfigTable("service_contactgroups"); ClearConfigTable("service_contactgroups");
ClearConfigTable("service_contacts"); ClearConfigTable("service_contacts");
@ -258,6 +261,7 @@ void IdoPgsqlConnection::ClearConfigTables(void)
ClearConfigTable("servicegroup_members"); ClearConfigTable("servicegroup_members");
ClearConfigTable("servicegroups"); ClearConfigTable("servicegroups");
ClearConfigTable("services"); ClearConfigTable("services");
ClearConfigTable("servicestatus");
ClearConfigTable("timeperiod_timeranges"); ClearConfigTable("timeperiod_timeranges");
ClearConfigTable("timeperiods"); ClearConfigTable("timeperiods");
} }
@ -529,12 +533,8 @@ void IdoPgsqlConnection::InternalExecuteQuery(const DbQuery& query)
if (hasid) if (hasid)
type = DbQueryUpdate; type = DbQueryUpdate;
else { else
if (query.WhereCriteria)
Query("DELETE FROM " + GetTablePrefix() + query.Table + where.str());
type = DbQueryInsert; type = DbQueryInsert;
}
} else } else
type = query.Type; type = query.Type;

View File

@ -201,10 +201,24 @@ DbReference DbConnection::GetInsertID(const DbObject::Ptr& dbobj) const
return it->second; 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) void DbConnection::ClearIDCache(void)
{ {
m_ObjectIDs.clear(); m_ObjectIDs.clear();
m_InsertIDs.clear(); m_InsertIDs.clear();
m_ActiveObjects.clear();
} }
void DbConnection::SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate) void DbConnection::SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate)
@ -246,7 +260,9 @@ void DbConnection::UpdateAllObjects(void)
DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object); DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object);
if (dbobj) { if (dbobj) {
if (!GetObjectActive(dbobj))
ActivateObject(dbobj); ActivateObject(dbobj);
dbobj->SendConfigUpdate(); dbobj->SendConfigUpdate();
dbobj->SendStatusUpdate(); dbobj->SendStatusUpdate();
} }

View File

@ -47,6 +47,9 @@ public:
void SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref); void SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref);
DbReference GetInsertID(const DbObject::Ptr& dbobj) const; 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 ClearIDCache(void);
void SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate); void SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
@ -69,6 +72,7 @@ protected:
private: private:
std::map<DbObject::Ptr, DbReference> m_ObjectIDs; std::map<DbObject::Ptr, DbReference> m_ObjectIDs;
std::map<DbObject::Ptr, DbReference> m_InsertIDs; std::map<DbObject::Ptr, DbReference> m_InsertIDs;
std::set<DbObject::Ptr> m_ActiveObjects;
std::set<DbObject::Ptr> m_ConfigUpdates; std::set<DbObject::Ptr> m_ConfigUpdates;
std::set<DbObject::Ptr> m_StatusUpdates; std::set<DbObject::Ptr> m_StatusUpdates;
Timer::Ptr m_CleanUpTimer; Timer::Ptr m_CleanUpTimer;

View File

@ -194,22 +194,6 @@ void HostDbObject::OnConfigUpdate(void)
Host::Ptr host = static_pointer_cast<Host>(GetObject()); Host::Ptr host = static_pointer_cast<Host>(GetObject());
/* parents, host dependencies */ /* 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()) { BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) {
Log(LogDebug, "db_ido", "host parents: " + parent->GetName()); Log(LogDebug, "db_ido", "host parents: " + parent->GetName());
@ -284,14 +268,6 @@ void HostDbObject::OnConfigUpdate(void)
/* custom variables */ /* custom variables */
Log(LogDebug, "ido", "host customvars for '" + host->GetName() + "'"); 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; Dictionary::Ptr customvars;
{ {
ObjectLock olock(host); ObjectLock olock(host);