Skip to content

Commit

Permalink
Schema: change sort order of history event type enum
Browse files Browse the repository at this point in the history
This improves the resulting sort order when `ORDER BY event_time, event_type`
is used. `state_change` comes first as it can cause many of the other events
like trigger downtimes, remove acknowledgements and send notifications.
Similarly, `notification` comes last as any other event can result in a
notification. This will result in history events for scenarios like state
changes, triggers downtime, sends downtime start notification being sorted in
that order.

Apart from that, end events sort before the corresponding start events as any
ack/comment/downtime/flapping period should last for more than a millisecond,
therefore if there should be two events within the same millisecond, the end
event corresponds to the older period and is sorted first. The remaining order
is alphabetically.
  • Loading branch information
julianbrost committed Aug 3, 2023
1 parent 7c7ba9f commit 9fe5f99
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
7 changes: 6 additions & 1 deletion schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,12 @@ CREATE TABLE history (
flapping_history_id binary(20) DEFAULT NULL COMMENT 'flapping_history.id',
acknowledgement_history_id binary(20) DEFAULT NULL COMMENT 'acknowledgement_history.id',

event_type enum('notification','state_change','downtime_start', 'downtime_end','comment_add','comment_remove','flapping_start','flapping_end','ack_set','ack_clear') NOT NULL,
-- The enum values are ordered in a way that event_type provides a meaningful sort order for history entries with
-- the same event_time. state_change comes first as it can cause many of the other events like trigger downtimes,
-- remove acknowledgements and send notifications. Similarly, notification comes last as any other event can result
-- in a notification. End events sort before the corresponding start events as any ack/comment/downtime/flapping
-- period should last for more than a millisecond, therefore, the old period ends first and then the new one starts.
event_type enum('state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification') NOT NULL,
event_time bigint unsigned NOT NULL,

PRIMARY KEY (id),
Expand Down
14 changes: 14 additions & 0 deletions schema/mysql/upgrades/1.2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ ALTER TABLE servicegroup
ADD INDEX idx_servicegroup_name_ci (name_ci) COMMENT 'Servicegroup list filtered using quick search',
DROP INDEX idx_servicegroup_name,
ADD INDEX idx_servicegroup_name (name) COMMENT 'Host/service/service group list filtered by service group name; Servicegroup detail filter';

-- The following sequence of statements changes the type of history.event_type like the following statement would:
--
-- ALTER TABLE history MODIFY event_type enum('state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification') NOT NULL;
--
-- It's just much faster to add a second column, copy the column using an UPDATE statement and then replace the
-- old column with the one just generated. Table locking ensures that no other connection inserts data in the meantime.
LOCK TABLES history WRITE;
ALTER TABLE history ADD COLUMN event_type_new enum('state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification') NOT NULL AFTER event_type;
UPDATE history SET event_type_new = event_type;
ALTER TABLE history
DROP COLUMN event_type,
CHANGE COLUMN event_type_new event_type enum('state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification') NOT NULL;
UNLOCK TABLES;
10 changes: 8 additions & 2 deletions schema/pgsql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ CREATE TYPE state_type AS ENUM ( 'hard', 'soft' );
CREATE TYPE checkable_type AS ENUM ( 'host', 'service' );
CREATE TYPE comment_type AS ENUM ( 'comment', 'ack' );
CREATE TYPE notification_type AS ENUM ( 'downtime_start', 'downtime_end', 'downtime_removed', 'custom', 'acknowledgement', 'problem', 'recovery', 'flapping_start', 'flapping_end' );
CREATE TYPE history_type AS ENUM ( 'notification', 'state_change', 'downtime_start', 'downtime_end', 'comment_add', 'comment_remove', 'flapping_start', 'flapping_end', 'ack_set', 'ack_clear' );

-- The enum values are ordered in a way that event_type provides a meaningful sort order for history entries with
-- the same event_time. state_change comes first as it can cause many of the other events like trigger downtimes,
-- remove acknowledgements and send notifications. Similarly, notification comes last as any other event can result
-- in a notification. End events sort before the corresponding start events as any ack/comment/downtime/flapping
-- period should last for more than a millisecond, therefore, the old period ends first and then the new one starts.
CREATE TYPE history_type AS ENUM ( 'state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification' );

CREATE OR REPLACE FUNCTION get_sla_ok_percent(
in_host_id bytea20,
Expand Down Expand Up @@ -2040,7 +2046,7 @@ CREATE TABLE history (
flapping_history_id bytea20 DEFAULT NULL,
acknowledgement_history_id bytea20 DEFAULT NULL,

event_type history_type NOT NULL DEFAULT 'notification',
event_type history_type NOT NULL DEFAULT 'state_change',
event_time biguint NOT NULL,

CONSTRAINT pk_history PRIMARY KEY (id),
Expand Down
8 changes: 8 additions & 0 deletions schema/pgsql/upgrades/1.2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ CREATE INDEX idx_servicegroup_name_ci ON servicegroup(name_ci);
COMMENT ON INDEX idx_servicegroup_display_name IS 'Servicegroup list filtered/ordered by display_name';
COMMENT ON INDEX idx_servicegroup_name_ci IS 'Servicegroup list filtered using quick search';
COMMENT ON INDEX idx_servicegroup_name IS 'Host/service/service group list filtered by service group name; Servicegroup detail filter';

ALTER TYPE history_type RENAME TO history_type_old;
CREATE TYPE history_type AS ENUM ( 'state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification' );
ALTER TABLE history
ALTER COLUMN event_type DROP DEFAULT,
ALTER COLUMN event_type TYPE history_type USING event_type::text::history_type,
ALTER COLUMN event_type SET DEFAULT 'state_change'::history_type;
DROP TYPE history_type_old;

0 comments on commit 9fe5f99

Please sign in to comment.