Skip to content

Commit

Permalink
DB IDO: Use upsert and session token for comment/downtime updates
Browse files Browse the repository at this point in the history
refs #12258
fixes #12288
  • Loading branch information
Michael Friedrich committed Aug 2, 2016
1 parent 53930e3 commit cd5c936
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lib/db_ido/dbevents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,6 @@ void DbEvents::AddComments(const Checkable::Ptr& checkable)

std::vector<DbQuery> queries;

/* Ensure to delete all comments and then insert any or none.
* We must purge obsolete comments in the database at all cost. */

DbQuery query1;
query1.Table = "comments";
query1.Type = DbQueryDelete;
query1.Category = DbCatComment;
query1.WhereCriteria = new Dictionary();
query1.WhereCriteria->Set("object_id", checkable);
queries.push_back(query1);

BOOST_FOREACH(const Comment::Ptr& comment, comments) {
AddCommentInternal(queries, comment, false);
}
Expand All @@ -326,7 +315,6 @@ void DbEvents::AddComments(const Checkable::Ptr& checkable)
void DbEvents::AddComment(const Comment::Ptr& comment)
{
std::vector<DbQuery> queries;
RemoveCommentInternal(queries, comment);
AddCommentInternal(queries, comment, false);
DbObject::OnMultipleQueries(queries);
}
Expand Down Expand Up @@ -382,10 +370,19 @@ void DbEvents::AddCommentInternal(std::vector<DbQuery>& queries, const Comment::
DbQuery query1;
if (!historical) {
query1.Table = "comments";
query1.Type = DbQueryInsert | DbQueryUpdate;

fields1->Set("session_token", 0); /* DbConnection class fills in real ID */

query1.WhereCriteria = new Dictionary();
query1.WhereCriteria->Set("internal_comment_id", comment->GetLegacyId());
query1.WhereCriteria->Set("object_id", checkable);
query1.WhereCriteria->Set("comment_time", DbValue::FromTimestamp(entry_time));
query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
} else {
query1.Table = "commenthistory";
query1.Type = DbQueryInsert;
}
query1.Type = DbQueryInsert;
query1.Category = DbCatComment;
query1.Fields = fields1;

Expand Down Expand Up @@ -520,12 +517,22 @@ void DbEvents::AddDowntimeInternal(std::vector<DbQuery>& queries, const Downtime

DbQuery query1;

if (!historical)
if (!historical) {
query1.Table = "scheduleddowntime";
else
query1.Type = DbQueryInsert | DbQueryUpdate;

fields1->Set("session_token", 0); /* DbConnection class fills in real ID */

query1.WhereCriteria = new Dictionary();
query1.WhereCriteria->Set("object_id", checkable);
query1.WhereCriteria->Set("internal_downtime_id", downtime->GetLegacyId());
query1.WhereCriteria->Set("entry_time", DbValue::FromTimestamp(downtime->GetEntryTime()));
query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
} else {
query1.Table = "downtimehistory";
query1.Type = DbQueryInsert;
}

query1.Type = DbQueryInsert;
query1.Category = DbCatDowntime;
query1.Fields = fields1;

Expand Down
2 changes: 2 additions & 0 deletions lib/db_ido_mysql/idomysqlconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ void IdoMysqlConnection::ClearTablesBySession(void)
ClearTableBySession("hostgroup_members");
ClearTableBySession("servicegroup_members");
ClearTableBySession("contactgroup_members");
ClearTableBySession("comments");
ClearTableBySession("scheduleddowntime");
}

void IdoMysqlConnection::ClearTableBySession(const String& table)
Expand Down
3 changes: 3 additions & 0 deletions lib/db_ido_mysql/schema/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ CREATE TABLE IF NOT EXISTS icinga_comments (
expires smallint default 0,
expiration_time timestamp default '0000-00-00 00:00:00',
name TEXT character set latin1 default NULL,
session_token int default NULL,
PRIMARY KEY (comment_id),
UNIQUE KEY instance_id (instance_id,object_id,comment_time,internal_comment_id)
) ENGINE=InnoDB COMMENT='Usercomments on Icinga objects';
Expand Down Expand Up @@ -1669,6 +1670,8 @@ CREATE INDEX idx_cg_session_del ON icinga_contactgroup_members (instance_id, ses
CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token);
CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token);

CREATE INDEX idx_comments_session_del ON icinga_comments (instance_id, session_token);

-- #12107
CREATE INDEX idx_statehistory_cleanup on icinga_statehistory(instance_id, state_time);

Expand Down
9 changes: 9 additions & 0 deletions lib/db_ido_mysql/schema/upgrade/2.5.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ DROP INDEX cvs_session_del_idx ON icinga_customvariablestatus;
CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token);
CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token);

-- -----------------------------------------
-- #12258
-- -----------------------------------------
ALTER TABLE icinga_comments ADD COLUMN session_token INTEGER default NULL;
ALTER TABLE icinga_scheduleddowntime ADD COLUMN session_token INTEGER default NULL;

CREATE INDEX idx_comments_session_del ON icinga_comments (instance_id, session_token);
CREATE INDEX idx_downtimes_session_del ON icinga_scheduleddowntime (instance_id, session_token);

-- -----------------------------------------
-- set dbversion
-- -----------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions lib/db_ido_pgsql/idopgsqlconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ void IdoPgsqlConnection::ClearTablesBySession(void)
ClearTableBySession("hostgroup_members");
ClearTableBySession("servicegroup_members");
ClearTableBySession("contactgroup_members");
ClearTableBySession("comments");
ClearTableBySession("scheduleddowntime");
}

void IdoPgsqlConnection::ClearTableBySession(const String& table)
Expand Down
5 changes: 5 additions & 0 deletions lib/db_ido_pgsql/schema/pgsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ CREATE TABLE icinga_comments (
expires INTEGER default 0,
expiration_time timestamp with time zone default '1970-01-01 00:00:00+00',
name TEXT default NULL,
session_token INTEGER default NULL,
CONSTRAINT PK_comment_id PRIMARY KEY (comment_id) ,
CONSTRAINT UQ_comments UNIQUE (instance_id,object_id,comment_time,internal_comment_id)
) ;
Expand Down Expand Up @@ -1005,6 +1006,7 @@ CREATE TABLE icinga_scheduleddowntime (
is_in_effect INTEGER default 0,
trigger_time timestamp with time zone default '1970-01-01 00:00:00+00',
name TEXT default NULL,
session_token INTEGER default NULL,
CONSTRAINT PK_scheduleddowntime_id PRIMARY KEY (scheduleddowntime_id) ,
CONSTRAINT UQ_scheduleddowntime UNIQUE (instance_id,object_id,entry_time,internal_downtime_id)
) ;
Expand Down Expand Up @@ -1695,6 +1697,9 @@ CREATE INDEX idx_hg_session_del ON icinga_hostgroup_members (instance_id, sessio
CREATE INDEX idx_sg_session_del ON icinga_servicegroup_members (instance_id, session_token);
CREATE INDEX idx_cg_session_del ON icinga_contactgroup_members (instance_id, session_token);

CREATE INDEX idx_comments_session_del ON icinga_comments (instance_id, session_token);
CREATE INDEX idx_downtimes_session_del ON icinga_scheduleddowntime (instance_id, session_token);

CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token);
CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token);

Expand Down
9 changes: 9 additions & 0 deletions lib/db_ido_pgsql/schema/upgrade/2.5.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ DROP INDEX cvs_session_del_idx;
CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token);
CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token);

-- -----------------------------------------
-- #12258
-- -----------------------------------------
ALTER TABLE icinga_comments ADD COLUMN session_token INTEGER default NULL;
ALTER TABLE icinga_scheduleddowntime ADD COLUMN session_token INTEGER default NULL;

CREATE INDEX idx_comments_session_del ON icinga_comments (instance_id, session_token);
CREATE INDEX idx_downtimes_session_del ON icinga_scheduleddowntime (instance_id, session_token);

-- -----------------------------------------
-- #12107
-- -----------------------------------------
Expand Down

0 comments on commit cd5c936

Please sign in to comment.