From 8272f26fc9728c84d1219eb52974769f1322ed6a Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 11 Mar 2024 17:39:34 +0100 Subject: [PATCH] Unify check attempt data type to uint32 already used somewhere A float isn't necessary as in Icinga 2 Checkable#max_check_attempts and check_attempt are ints. But uint8 isn't enough for e.g. 1 check/s to get HARD after 5m (300s > 255). --- cmd/icingadb-migrate/convert.go | 8 ++++---- pkg/icingadb/db.go | 4 ++-- pkg/icingadb/v1/checkable.go | 2 +- pkg/icingadb/v1/history/state.go | 2 +- pkg/icingadb/v1/state.go | 2 +- schema/mysql/schema.sql | 8 ++++---- schema/mysql/upgrades/1.1.2-history.sql | 6 ++++++ schema/mysql/upgrades/1.1.2.sql | 10 ++++++++++ schema/pgsql/schema.sql | 8 ++++---- schema/pgsql/upgrades/1.1.2-history.sql | 7 +++++++ schema/pgsql/upgrades/1.1.2.sql | 10 ++++++++++ 11 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 schema/mysql/upgrades/1.1.2-history.sql create mode 100644 schema/mysql/upgrades/1.1.2.sql create mode 100644 schema/pgsql/upgrades/1.1.2-history.sql create mode 100644 schema/pgsql/upgrades/1.1.2.sql diff --git a/cmd/icingadb-migrate/convert.go b/cmd/icingadb-migrate/convert.go index e14746e46..5e4bdb1ba 100644 --- a/cmd/icingadb-migrate/convert.go +++ b/cmd/icingadb-migrate/convert.go @@ -739,8 +739,8 @@ type stateRow = struct { StateTimeUsec uint32 State uint8 StateType uint8 - CurrentCheckAttempt uint16 - MaxCheckAttempts uint16 + CurrentCheckAttempt uint32 + MaxCheckAttempts uint32 LastState uint8 LastHardState uint8 Output sql.NullString @@ -813,10 +813,10 @@ func convertStateRows( HardState: row.LastHardState, PreviousSoftState: row.LastState, PreviousHardState: previousHardState, - CheckAttempt: uint8(row.CurrentCheckAttempt), + CheckAttempt: row.CurrentCheckAttempt, Output: icingadbTypes.String{NullString: row.Output}, LongOutput: icingadbTypes.String{NullString: row.LongOutput}, - MaxCheckAttempts: uint32(row.MaxCheckAttempts), + MaxCheckAttempts: row.MaxCheckAttempts, CheckSource: icingadbTypes.String{NullString: row.CheckSource}, }) diff --git a/pkg/icingadb/db.go b/pkg/icingadb/db.go index 4ff3e0dde..4640f2595 100644 --- a/pkg/icingadb/db.go +++ b/pkg/icingadb/db.go @@ -85,8 +85,8 @@ func NewDb(db *sqlx.DB, logger *logging.Logger, options *Options) *DB { } const ( - expectedMysqlSchemaVersion = 4 - expectedPostgresSchemaVersion = 2 + expectedMysqlSchemaVersion = 5 + expectedPostgresSchemaVersion = 3 ) // CheckSchema asserts the database schema of the expected version being present. diff --git a/pkg/icingadb/v1/checkable.go b/pkg/icingadb/v1/checkable.go index dbb114cbc..4b1efeb9c 100644 --- a/pkg/icingadb/v1/checkable.go +++ b/pkg/icingadb/v1/checkable.go @@ -30,7 +30,7 @@ type Checkable struct { IconImageAlt string `json:"icon_image_alt"` IconImageId types.Binary `json:"icon_image_id"` IsVolatile types.Bool `json:"is_volatile"` - MaxCheckAttempts float64 `json:"max_check_attempts"` + MaxCheckAttempts uint32 `json:"max_check_attempts"` Notes string `json:"notes"` NotesUrlId types.Binary `json:"notes_url_id"` NotificationsEnabled types.Bool `json:"notifications_enabled"` diff --git a/pkg/icingadb/v1/history/state.go b/pkg/icingadb/v1/history/state.go index dec13b042..6320b738a 100644 --- a/pkg/icingadb/v1/history/state.go +++ b/pkg/icingadb/v1/history/state.go @@ -14,7 +14,7 @@ type StateHistory struct { HardState uint8 `json:"hard_state"` PreviousSoftState uint8 `json:"previous_soft_state"` PreviousHardState uint8 `json:"previous_hard_state"` - CheckAttempt uint8 `json:"check_attempt"` + CheckAttempt uint32 `json:"check_attempt"` Output types.String `json:"output"` LongOutput types.String `json:"long_output"` MaxCheckAttempts uint32 `json:"max_check_attempts"` diff --git a/pkg/icingadb/v1/state.go b/pkg/icingadb/v1/state.go index bad8f28c5..983b14d5a 100644 --- a/pkg/icingadb/v1/state.go +++ b/pkg/icingadb/v1/state.go @@ -9,7 +9,7 @@ type State struct { EnvironmentMeta `json:",inline"` AcknowledgementCommentId types.Binary `json:"acknowledgement_comment_id"` LastCommentId types.Binary `json:"last_comment_id"` - CheckAttempt uint8 `json:"check_attempt"` + CheckAttempt uint32 `json:"check_attempt"` CheckCommandline types.String `json:"check_commandline"` CheckSource types.String `json:"check_source"` SchedulingSource types.String `json:"scheduling_source"` diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index f4434f139..2b215565b 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -292,7 +292,7 @@ CREATE TABLE host_state ( hard_state tinyint unsigned NOT NULL, previous_soft_state tinyint unsigned NOT NULL, previous_hard_state tinyint unsigned NOT NULL, - check_attempt tinyint unsigned NOT NULL, + check_attempt int unsigned NOT NULL, severity smallint unsigned NOT NULL, output longtext DEFAULT NULL, @@ -460,7 +460,7 @@ CREATE TABLE service_state ( hard_state tinyint unsigned NOT NULL, previous_soft_state tinyint unsigned NOT NULL, previous_hard_state tinyint unsigned NOT NULL, - check_attempt tinyint unsigned NOT NULL, + check_attempt int unsigned NOT NULL, severity smallint unsigned NOT NULL, output longtext DEFAULT NULL, @@ -1147,7 +1147,7 @@ CREATE TABLE state_history ( hard_state tinyint unsigned NOT NULL, previous_soft_state tinyint unsigned NOT NULL, previous_hard_state tinyint unsigned NOT NULL, - check_attempt tinyint unsigned NOT NULL, + check_attempt int unsigned NOT NULL, -- May be a tinyint unsigned, see ./upgrades/1.1.2-history.sql. output longtext DEFAULT NULL, long_output longtext DEFAULT NULL, max_check_attempts int unsigned NOT NULL, @@ -1343,4 +1343,4 @@ CREATE TABLE icingadb_schema ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC; INSERT INTO icingadb_schema (version, timestamp) - VALUES (4, CURRENT_TIMESTAMP() * 1000); + VALUES (5, CURRENT_TIMESTAMP() * 1000); diff --git a/schema/mysql/upgrades/1.1.2-history.sql b/schema/mysql/upgrades/1.1.2-history.sql new file mode 100644 index 000000000..b5e5ab52f --- /dev/null +++ b/schema/mysql/upgrades/1.1.2-history.sql @@ -0,0 +1,6 @@ +-- OPTIONAL addition to ./1.1.2.sql. +-- Will take a LONG time depending on amount of already present history. +-- You HAVE TO apply it if you have some max_check_attempts > 255. +-- You SHOULD apply it if the already present history isn't large, yet. + +ALTER TABLE state_history MODIFY COLUMN check_attempt int unsigned NOT NULL; diff --git a/schema/mysql/upgrades/1.1.2.sql b/schema/mysql/upgrades/1.1.2.sql new file mode 100644 index 000000000..636ac5407 --- /dev/null +++ b/schema/mysql/upgrades/1.1.2.sql @@ -0,0 +1,10 @@ +-- See also ./1.1.2-history.sql! + +ALTER TABLE host_state MODIFY COLUMN check_attempt int unsigned NOT NULL; + +ALTER TABLE service_state MODIFY COLUMN check_attempt int unsigned NOT NULL; + +ALTER TABLE state_history MODIFY COLUMN check_attempt tinyint unsigned NOT NULL COMMENT 'optional schema/mysql/upgrades/1.1.2-history.sql not applied, yet'; + +INSERT INTO icingadb_schema (version, timestamp) + VALUES (5, CURRENT_TIMESTAMP() * 1000); diff --git a/schema/pgsql/schema.sql b/schema/pgsql/schema.sql index 9027fac52..b9cb5cc5c 100644 --- a/schema/pgsql/schema.sql +++ b/schema/pgsql/schema.sql @@ -405,7 +405,7 @@ CREATE TABLE host_state ( hard_state tinyuint NOT NULL, previous_soft_state tinyuint NOT NULL, previous_hard_state tinyuint NOT NULL, - check_attempt tinyuint NOT NULL, + check_attempt uint NOT NULL, severity smalluint NOT NULL, output text DEFAULT NULL, @@ -675,7 +675,7 @@ CREATE TABLE service_state ( hard_state tinyuint NOT NULL, previous_soft_state tinyuint NOT NULL, previous_hard_state tinyuint NOT NULL, - check_attempt tinyuint NOT NULL, + check_attempt uint NOT NULL, severity smalluint NOT NULL, output text DEFAULT NULL, @@ -1846,7 +1846,7 @@ CREATE TABLE state_history ( hard_state tinyuint NOT NULL, previous_soft_state tinyuint NOT NULL, previous_hard_state tinyuint NOT NULL, - check_attempt tinyuint NOT NULL, + check_attempt uint NOT NULL, -- May be a tinyuint, see ./upgrades/1.1.2-history.sql. output text DEFAULT NULL, long_output text DEFAULT NULL, max_check_attempts uint NOT NULL, @@ -2181,4 +2181,4 @@ CREATE TABLE icingadb_schema ( ALTER SEQUENCE icingadb_schema_id_seq OWNED BY icingadb_schema.id; INSERT INTO icingadb_schema (version, timestamp) - VALUES (2, extract(epoch from now()) * 1000); + VALUES (3, extract(epoch from now()) * 1000); diff --git a/schema/pgsql/upgrades/1.1.2-history.sql b/schema/pgsql/upgrades/1.1.2-history.sql new file mode 100644 index 000000000..c8d6e899b --- /dev/null +++ b/schema/pgsql/upgrades/1.1.2-history.sql @@ -0,0 +1,7 @@ +-- OPTIONAL addition to ./1.1.2.sql. +-- Will take a LONG time depending on amount of already present history. +-- You HAVE TO apply it if you have some max_check_attempts > 255. +-- You SHOULD apply it if the already present history isn't large, yet. + +ALTER TABLE state_history ALTER COLUMN check_attempt TYPE uint; +COMMENT ON COLUMN state_history.check_attempt IS NULL; diff --git a/schema/pgsql/upgrades/1.1.2.sql b/schema/pgsql/upgrades/1.1.2.sql new file mode 100644 index 000000000..ee607f608 --- /dev/null +++ b/schema/pgsql/upgrades/1.1.2.sql @@ -0,0 +1,10 @@ +-- See also ./1.1.2-history.sql! + +ALTER TABLE host_state ALTER COLUMN check_attempt TYPE uint; + +ALTER TABLE service_state ALTER COLUMN check_attempt TYPE uint; + +COMMENT ON COLUMN state_history.check_attempt IS 'optional schema/pgsql/upgrades/1.1.2-history.sql not applied, yet'; + +INSERT INTO icingadb_schema (version, timestamp) + VALUES (3, extract(epoch from now()) * 1000);