From 52ae34a6f82a0fbb478b887a7ab82ea644dc328d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 15 Oct 2021 10:43:15 +0200 Subject: [PATCH] Make {NotificationHistory,StateHistory}#Id UUID -> SHA1 --- pkg/icingadb/history/sync.go | 4 ++-- pkg/icingadb/v1/history/meta.go | 18 ++---------------- pkg/icingadb/v1/history/notification.go | 4 ++-- pkg/icingadb/v1/history/state.go | 2 +- schema/mysql/schema.sql | 10 +++++----- schema/mysql/upgrades/1.0.0-rc2.sql | 9 ++++++++- 6 files changed, 20 insertions(+), 27 deletions(-) diff --git a/pkg/icingadb/history/sync.go b/pkg/icingadb/history/sync.go index d4f2a51b..4953fc86 100644 --- a/pkg/icingadb/history/sync.go +++ b/pkg/icingadb/history/sync.go @@ -314,7 +314,7 @@ func writeMultiEntityStage(entryToEntities func(entry redis.XMessage) ([]v1.Upse // user_notification_history relation table for each user ID. func userNotificationStage(ctx context.Context, s Sync, key string, in <-chan redis.XMessage, out chan<- redis.XMessage) error { type NotificationHistory struct { - Id types.UUID `structify:"id"` + Id types.Binary `structify:"id"` EnvironmentId types.Binary `structify:"environment_id"` EndpointId types.Binary `structify:"endpoint_id"` UserIds types.String `structify:"users_notified_ids"` @@ -345,7 +345,7 @@ func userNotificationStage(ctx context.Context, s Sync, key string, in <-chan re userNotifications = append(userNotifications, &v1.UserNotificationHistory{ EntityWithoutChecksum: v1types.EntityWithoutChecksum{ IdMeta: v1types.IdMeta{ - Id: utils.Checksum(append(append([]byte(nil), notificationHistory.Id.UUID[:]...), user...)), + Id: utils.Checksum(append(append([]byte(nil), notificationHistory.Id...), user...)), }, }, EnvironmentMeta: v1types.EnvironmentMeta{ diff --git a/pkg/icingadb/v1/history/meta.go b/pkg/icingadb/v1/history/meta.go index b2c817f9..bb9aad2f 100644 --- a/pkg/icingadb/v1/history/meta.go +++ b/pkg/icingadb/v1/history/meta.go @@ -2,6 +2,7 @@ package history import ( "github.com/icinga/icingadb/pkg/contracts" + "github.com/icinga/icingadb/pkg/icingadb/v1" "github.com/icinga/icingadb/pkg/types" ) @@ -13,22 +14,7 @@ type UpserterEntity interface { // HistoryTableEntity is embedded by every concrete history type that has its own table. type HistoryTableEntity struct { - Id types.UUID `json:"id"` -} - -// Fingerprint implements part of the contracts.Entity interface. -func (hte HistoryTableEntity) Fingerprint() contracts.Fingerprinter { - return hte -} - -// ID implements part of the contracts.Entity interface. -func (hte HistoryTableEntity) ID() contracts.ID { - return hte.Id -} - -// SetID implements part of the contracts.Entity interface. -func (hte *HistoryTableEntity) SetID(id contracts.ID) { - hte.Id = id.(types.UUID) + v1.EntityWithoutChecksum `json:",inline"` } // Upsert implements the contracts.Upserter interface. diff --git a/pkg/icingadb/v1/history/notification.go b/pkg/icingadb/v1/history/notification.go index ecc552ad..1ebc27aa 100644 --- a/pkg/icingadb/v1/history/notification.go +++ b/pkg/icingadb/v1/history/notification.go @@ -22,7 +22,7 @@ type NotificationHistory struct { type UserNotificationHistory struct { v1.EntityWithoutChecksum `json:",inline"` v1.EnvironmentMeta `json:",inline"` - NotificationHistoryId types.UUID `json:"notification_history_id"` + NotificationHistoryId types.Binary `json:"notification_history_id"` UserId types.Binary `json:"user_id"` } @@ -32,7 +32,7 @@ func (u *UserNotificationHistory) Upsert() interface{} { type HistoryNotification struct { HistoryMeta `json:",inline"` - NotificationHistoryId types.UUID `json:"id"` + NotificationHistoryId types.Binary `json:"id"` EventTime types.UnixMilli `json:"send_time"` } diff --git a/pkg/icingadb/v1/history/state.go b/pkg/icingadb/v1/history/state.go index a06725d1..ad5f4703 100644 --- a/pkg/icingadb/v1/history/state.go +++ b/pkg/icingadb/v1/history/state.go @@ -24,7 +24,7 @@ type StateHistory struct { type HistoryState struct { HistoryMeta `json:",inline"` - StateHistoryId types.UUID `json:"id"` + StateHistoryId types.Binary `json:"id"` EventTime types.UnixMilli `json:"event_time"` } diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index a6e281a0..18f9cb3d 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -916,7 +916,7 @@ CREATE TABLE zone ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC; CREATE TABLE notification_history ( - id binary(16) NOT NULL COMMENT 'UUID', + id binary(20) NOT NULL COMMENT 'sha1(environment.name + notification.name + type + send_time)', environment_id binary(20) NOT NULL COMMENT 'environment.id', endpoint_id binary(20) DEFAULT NULL COMMENT 'endpoint.id', object_type enum('host', 'service') NOT NULL, @@ -940,7 +940,7 @@ CREATE TABLE notification_history ( CREATE TABLE user_notification_history ( id binary(20) NOT NULL COMMENT 'sha1(notification_history_id + user_id)', environment_id binary(20) NOT NULL COMMENT 'environment.id', - notification_history_id binary(16) NOT NULL COMMENT 'UUID notification_history.id', + notification_history_id binary(20) NOT NULL COMMENT 'UUID notification_history.id', user_id binary(20) NOT NULL COMMENT 'user.id', PRIMARY KEY (id), @@ -949,7 +949,7 @@ CREATE TABLE user_notification_history ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC; CREATE TABLE state_history ( - id binary(16) NOT NULL COMMENT 'UUID', + id binary(20) NOT NULL COMMENT 'sha1(environment.name + host|service.name + event_time)', environment_id binary(20) NOT NULL COMMENT 'environment.id', endpoint_id binary(20) DEFAULT NULL COMMENT 'endpoint.id', object_type enum('host', 'service') NOT NULL, @@ -1067,8 +1067,8 @@ CREATE TABLE history ( object_type enum('host', 'service') NOT NULL, host_id binary(20) NOT NULL COMMENT 'host.id', service_id binary(20) DEFAULT NULL COMMENT 'service.id', - notification_history_id binary(16) DEFAULT NULL COMMENT 'notification_history.id', - state_history_id binary(16) DEFAULT NULL COMMENT 'state_history.id', + notification_history_id binary(20) DEFAULT NULL COMMENT 'notification_history.id', + state_history_id binary(20) DEFAULT NULL COMMENT 'state_history.id', downtime_history_id binary(20) DEFAULT NULL COMMENT 'downtime_history.downtime_id', comment_history_id binary(20) DEFAULT NULL COMMENT 'comment_history.comment_id', flapping_history_id binary(20) DEFAULT NULL COMMENT 'flapping_history.id', diff --git a/schema/mysql/upgrades/1.0.0-rc2.sql b/schema/mysql/upgrades/1.0.0-rc2.sql index d9450127..6c3ff620 100644 --- a/schema/mysql/upgrades/1.0.0-rc2.sql +++ b/schema/mysql/upgrades/1.0.0-rc2.sql @@ -165,6 +165,7 @@ ALTER TABLE host_state ALTER TABLE state_history ADD COLUMN scheduling_source text DEFAULT NULL AFTER check_source, + MODIFY id binary(20) NOT NULL COMMENT 'sha1(environment.name + host|service.name + event_time)', MODIFY output longtext DEFAULT NULL, MODIFY long_output longtext DEFAULT NULL; @@ -173,10 +174,16 @@ ALTER TABLE service_state MODIFY long_output longtext DEFAULT NULL, MODIFY performance_data longtext DEFAULT NULL; +ALTER TABLE notification_history + MODIFY id binary(20) NOT NULL COMMENT 'sha1(environment.name + notification.name + type + send_time)'; + ALTER TABLE user_notification_history - MODIFY id binary(20) NOT NULL COMMENT 'sha1(notification_history_id + user_id)'; + MODIFY id binary(20) NOT NULL COMMENT 'sha1(notification_history_id + user_id)', + MODIFY notification_history_id binary(20) NOT NULL COMMENT 'UUID notification_history.id'; ALTER TABLE history + MODIFY notification_history_id binary(20) DEFAULT NULL COMMENT 'notification_history.id', + MODIFY state_history_id binary(20) DEFAULT NULL COMMENT 'state_history.id', ADD CONSTRAINT fk_history_acknowledgement_history FOREIGN KEY (acknowledgement_history_id) REFERENCES acknowledgement_history (id) ON DELETE CASCADE, ADD CONSTRAINT fk_history_comment_history FOREIGN KEY (comment_history_id) REFERENCES comment_history (comment_id) ON DELETE CASCADE, ADD CONSTRAINT fk_history_downtime_history FOREIGN KEY (downtime_history_id) REFERENCES downtime_history (downtime_id) ON DELETE CASCADE,