New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incremental Configuration Updates #191
base: main
Are you sure you want to change the base?
Conversation
The changed icingadb dependencies contains the latest state of the noma branch[0] with an additional fix for utils.FromUnixMilli[1,2]. [0]: Icinga/icingadb#578 [1]: Icinga/icingadb#753 [2]: Icinga/icingadb@066abea
d6f826b
to
6fc5530
Compare
5643bae
to
c16adef
Compare
c16adef
to
433760f
Compare
After talking with @sukhwinder33445 about this change as he implements the web counterpart, I have removed the For historical reasons and if it might become relevant again, I have attached the diff below. incr_cfg_delete diffdiff --git a/schema/pgsql/schema.sql b/schema/pgsql/schema.sql
index a5abfea..08f5406 100644
--- a/schema/pgsql/schema.sql
+++ b/schema/pgsql/schema.sql
@@ -88,6 +88,23 @@ CREATE FUNCTION incr_cfg_bump_changed_at_relation_2nd() -- join_tbl text, foreig
END;
$$;
+-- incr_cfg_delete is a BEFORE TRIGGER for DELETE, which sets changed_at to the current timestamp and raises the deleted
+-- column instead of actually deleting the row.
+CREATE FUNCTION incr_cfg_delete()
+ RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ -- Cannot change OLD's attributes, as this would require returning OLD which implies continuing with deletion.
+ EXECUTE format('
+ UPDATE %s
+ SET changed_at = EXTRACT(EPOCH FROM NOW()) * 1000, deleted = ''y''
+ WHERE id = %s',
+ TG_TABLE_NAME, OLD.id);
+ RETURN NULL;
+ END;
+ $$;
+
CREATE TABLE available_channel_type (
type text NOT NULL,
name text NOT NULL,
@@ -119,6 +136,11 @@ CREATE TRIGGER trg_channel_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_channel_incr_cfg_delete
+ BEFORE DELETE ON channel
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TABLE contact (
id bigserial,
full_name text NOT NULL,
@@ -140,6 +162,11 @@ CREATE TRIGGER trg_contact_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_contact_incr_cfg_delete
+ BEFORE DELETE ON contact
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TABLE contact_address (
id bigserial,
contact_id bigint NOT NULL REFERENCES contact(id),
@@ -160,6 +187,11 @@ CREATE TRIGGER trg_contact_address_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_contact_address_incr_cfg_delete
+ BEFORE DELETE ON contact_address
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TABLE contactgroup (
id bigserial,
name text NOT NULL,
@@ -178,6 +210,11 @@ CREATE TRIGGER trg_contactgroup_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_contactgroup_incr_cfg_delete
+ BEFORE DELETE ON contactgroup
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
-- Changes to contactgroup_member should be notified by an updated contactgroup.changed_at.
CREATE TABLE contactgroup_member (
contactgroup_id bigint NOT NULL REFERENCES contactgroup(id),
@@ -208,6 +245,11 @@ CREATE TRIGGER trg_schedule_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_schedule_incr_cfg_delete
+ BEFORE DELETE ON schedule
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TABLE timeperiod (
id bigserial,
owned_by_schedule_id bigint REFERENCES schedule(id), -- nullable for future standalone timeperiods
@@ -225,6 +267,11 @@ CREATE TRIGGER trg_timeperiod_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_timeperiod_incr_cfg_delete
+ BEFORE DELETE ON timeperiod
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
-- Changes to timeperiod_entry should be notified by an updated timeperiod.changed_at.
CREATE TABLE timeperiod_entry (
id bigserial,
@@ -315,6 +362,11 @@ CREATE TRIGGER trg_source_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_source_incr_cfg_delete
+ BEFORE DELETE ON source
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TABLE object (
id bytea NOT NULL, -- SHA256 of identifying tags and the source.id
source_id bigint NOT NULL REFERENCES source(id),
@@ -377,6 +429,11 @@ CREATE TRIGGER trg_rule_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_rule_incr_cfg_delete
+ BEFORE DELETE ON rule
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
-- Changes to rule_escalation should be notified by an updated rule.changed_at.
CREATE TABLE rule_escalation (
id bigserial,
diff --git a/schema/pgsql/upgrades/025.sql b/schema/pgsql/upgrades/025.sql
index ee5f595..3c36de4 100644
--- a/schema/pgsql/upgrades/025.sql
+++ b/schema/pgsql/upgrades/025.sql
@@ -52,6 +52,21 @@ CREATE FUNCTION incr_cfg_bump_changed_at_relation_2nd() -- join_tbl text, foreig
END;
$$;
+CREATE FUNCTION incr_cfg_delete()
+ RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ -- Cannot change OLD's attributes, as this would require returning OLD which implies continuing with deletion.
+ EXECUTE format('
+ UPDATE %s
+ SET changed_at = EXTRACT(EPOCH FROM NOW()) * 1000, deleted = ''y''
+ WHERE id = %s',
+ TG_TABLE_NAME, OLD.id);
+ RETURN NULL;
+ END;
+ $$;
+
ALTER TABLE channel
ADD COLUMN changed_at bigint NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()) * 1000,
ADD COLUMN deleted boolenum NOT NULL DEFAULT 'n';
@@ -98,21 +113,41 @@ CREATE TRIGGER trg_channel_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_channel_incr_cfg_delete
+ BEFORE DELETE ON channel
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TRIGGER trg_contact_incr_cfg_update
BEFORE INSERT OR UPDATE ON contact
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_contact_incr_cfg_delete
+ BEFORE DELETE ON contact
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TRIGGER trg_contact_address_incr_cfg_update
BEFORE INSERT OR UPDATE ON contact_address
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_contact_address_incr_cfg_delete
+ BEFORE DELETE ON contact_address
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TRIGGER trg_contactgroup_incr_cfg_update
BEFORE INSERT OR UPDATE ON contactgroup
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_contactgroup_incr_cfg_delete
+ BEFORE DELETE ON contactgroup
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TRIGGER trg_contactgroup_member_changed_at_relation
AFTER INSERT OR UPDATE OR DELETE ON contactgroup_member
FOR EACH ROW
@@ -123,11 +158,21 @@ CREATE TRIGGER trg_schedule_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_schedule_incr_cfg_delete
+ BEFORE DELETE ON schedule
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TRIGGER trg_timeperiod_incr_cfg_update
BEFORE INSERT OR UPDATE ON timeperiod
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_timeperiod_incr_cfg_delete
+ BEFORE DELETE ON timeperiod
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TRIGGER trg_timeperiod_entry_changed_at_relation
AFTER INSERT OR UPDATE OR DELETE ON timeperiod_entry
FOR EACH ROW
@@ -143,11 +188,21 @@ CREATE TRIGGER trg_source_incr_cfg_update
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_source_incr_cfg_delete
+ BEFORE DELETE ON source
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TRIGGER trg_rule_incr_cfg_update
BEFORE INSERT OR UPDATE ON rule
FOR EACH ROW
EXECUTE FUNCTION incr_cfg_bump_changed_at();
+CREATE TRIGGER trg_rule_incr_cfg_delete
+ BEFORE DELETE ON rule
+ FOR EACH ROW
+ EXECUTE FUNCTION incr_cfg_delete();
+
CREATE TRIGGER trg_rule_escalation_changed_at_relation
AFTER INSERT OR UPDATE OR DELETE ON rule_escalation
FOR EACH ROW |
My thinking when suggesting to not have Now this turned in to quite a bit of complex trigger mechanics instead. I would definitely consider adding these two columns to all tables instead of requiring three variants of the trigger functions. Is everything you implemented in PostgreSQL triggers also possible with MySQL and MariaDB?
Edit: this part became irrelevant by itself, I wrote this comment before seeing the previous comment. |
I would say that if for example the |
Why do you want to make a distinction here? Would that make anything easier for web?
Does this even work here? Reads like this is a magic feature just for |
Yes. For example, if the rule name is changed, the form returns new values that we can write directly to
But Update: Web should do all that manually, and the triggers can be removed. |
Enable incremental configuration updates by introducing two new columns - changed_at and deleted - for all tables directly referenced in the ConfigSet. The other relationship tables are requiring a changed_at update in their relative parent table. As a limitation, deleted rows within the database cannot be detected as the deletion logic now completely relies on the deleted column. Thus, deletions must be performed by setting both changed_at and deleted. Closes #5.
433760f
to
b39adec
Compare
22c5dbc
to
7c983b0
Compare
7c983b0
to
fe367d4
Compare
While the triggers allowed me to offload a lot of complexity to the database, it now went back to the notification daemon. I hope that I have found most of the obvious bugs that can occur, especially during partial updates. This version now works "trigger-free" and has the two columns in all tables, including relationship tables. Please give it a try and report back. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few comments that caught my eyes!
pending ConfigSet | ||
pending *ConfigSet | ||
// pendingLastChange holds the changed_at timestamp for incremental config updates. | ||
pendingLastChange map[string]types.UnixMilli |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not quite sure about the naming pendingLastChange
, as this map does not cache pending timestamps, but changed_at
timestamp of a given table already seen by the daemon.
// The rows will be ordered by `changed_at`, allowing to update the last change timestamp when iterating over it. | ||
func (r *RuntimeConfig) buildSelectStmtWhereChangedAt(typePtr interface{}) string { | ||
return r.db.Rebind(r.db.BuildSelectStmt(typePtr, typePtr) + | ||
` WHERE "changed_at" > COALESCE(?, CAST(0 AS BIGINT)) ORDER BY "changed_at"`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make sure that the timestamp given to SelectContext()
everywhere is not zero instead of using such DBMs specific queries. You can simply introduce a helper method that retrieves the last seen ChangedAt
timestamp from the cache store and sets it to types.UnixMill(0)
if required. Such log messages look pretty confusing anyway: ... "changed_at_after": "0001-01-01T00:00:00.000Z
.
changedAt types.UnixMilli, | ||
dest interface{}, | ||
) error { | ||
stmt := r.db.BuildSelectStmt(typePtr, typePtr) + ` WHERE "changed_at" > COALESCE(?, CAST(0 AS BIGINT))` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here!
if len(ids) > 0 { | ||
stmt += ` OR "` + idField + `" IN (` + strings.Join(strings.Split(strings.Repeat("?", len(ids)), ""), ",") + `)` | ||
} | ||
stmt += ` ORDER BY "changed_at"` | ||
stmt = r.db.Rebind(stmt) | ||
|
||
args := make([]interface{}, 0, 1+len(ids)) | ||
args = append(args, changedAt) | ||
for id := range ids { | ||
args = append(args, id) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a sqlx.In()
method for expanding the args within the ... IN()
clause. It is also already used in our code base.
icinga-notifications/internal/utils/utils.go
Lines 97 to 101 in 17a0abe
query := fmt.Sprintf("%s WHERE %q IN (?)", db.BuildSelectStmt(subject, subject), idColumn) | |
stmt, args, err := sqlx.In(query, ids) | |
if err != nil { | |
return errors.Wrapf(err, "cannot build placeholders for %q", query) | |
} |
tx, | ||
escalationPtr, | ||
"rule_id", | ||
utils.MapKeys(rulesByID), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just add a ruleIDs
slice here and everywhere you use utils.MapKeys()
and fill it in the loop above, as already done for rulesByID
instead of utils.MapKeys()
.
Bump github.com/icinga/icingadb dependency
The changed
icingadb
dependencies contains the latest state of the noma branch, Add extension required by Icinga Notifications icingadb#578, with an additional fix for utils.FromUnixMilli, utils.FromUnixMilli has floating point rounding issues icingadb#753 and Icinga/icingadb@066abea.Incremental Config Updates
Enable incremental configuration updates by introducing two new columns-
changed_at
anddeleted
- for all tables directly referenced in theConfigSet
. The other relationship tables are requiring achanged_at
update in their relative parent table.As a limitation, deleted rows within the database cannot be detected as the deletion logic now completely relies on the
deleted
column. Thus, deletions must be performed by setting bothchanged_at
anddeleted
.PostgreSQL Triggers for Incremental Config Changes
On an
INSERT
orUPDATE
on each row of a "main configuration tables", as listed in theConfigSet
Go struct, thechanged_at
column field will automatically be bumped to the current timestamp.In addition, aDELETE
on those tables will result in also settingchanged_at
to the current time as well as raising thedeleted
column instead of actually performing theDELETE
command.Furthermore, the "relationship tables" will update the
changed_at
field in its referred row in the main table forINSERT
,UPDATE
orDELETE
.Closes #5.
The web counterpart is being developed in Icinga/icinga-notifications-web#187.