This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re re introduce membership tables event stream ordering (#15356)
- Loading branch information
Showing
7 changed files
with
163 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add denormalised event stream ordering column to membership state tables for future use. Contributed by Nick @ Beeper (@fizzadar). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
synapse/storage/schema/main/delta/74/03_membership_tables_event_stream_ordering.sql.postgres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Copyright 2022 Beeper | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
-- Each of these are denormalised copies of `stream_ordering` from the corresponding row in` events` which | ||
-- we use to improve database performance by reduring JOINs. | ||
|
||
-- NOTE: these are set to NOT VALID to prevent locks while adding the column on large existing tables, | ||
-- which will be validated in a later migration. For all new/updated rows the FKEY will be checked. | ||
|
||
ALTER TABLE current_state_events ADD COLUMN event_stream_ordering BIGINT; | ||
ALTER TABLE current_state_events ADD CONSTRAINT event_stream_ordering_fkey FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering) NOT VALID; | ||
|
||
ALTER TABLE local_current_membership ADD COLUMN event_stream_ordering BIGINT; | ||
ALTER TABLE local_current_membership ADD CONSTRAINT event_stream_ordering_fkey FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering) NOT VALID; | ||
|
||
ALTER TABLE room_memberships ADD COLUMN event_stream_ordering BIGINT; | ||
ALTER TABLE room_memberships ADD CONSTRAINT event_stream_ordering_fkey FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering) NOT VALID; |
23 changes: 23 additions & 0 deletions
23
synapse/storage/schema/main/delta/74/03_membership_tables_event_stream_ordering.sql.sqlite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* Copyright 2022 Beeper | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
-- Each of these are denormalised copies of `stream_ordering` from the corresponding row in` events` which | ||
-- we use to improve database performance by reduring JOINs. | ||
|
||
-- NOTE: sqlite does not support ADD CONSTRAINT so we add the new columns with FK constraint as-is | ||
|
||
ALTER TABLE current_state_events ADD COLUMN event_stream_ordering BIGINT REFERENCES events(stream_ordering); | ||
ALTER TABLE local_current_membership ADD COLUMN event_stream_ordering BIGINT REFERENCES events(stream_ordering); | ||
ALTER TABLE room_memberships ADD COLUMN event_stream_ordering BIGINT REFERENCES events(stream_ordering); |
79 changes: 79 additions & 0 deletions
79
synapse/storage/schema/main/delta/74/04_membership_tables_event_stream_ordering_triggers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright 2022 Beeper | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
""" | ||
This migration adds triggers to the room membership tables to enforce consistency. | ||
Triggers cannot be expressed in .sql files, so we have to use a separate file. | ||
""" | ||
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine, Sqlite3Engine | ||
from synapse.storage.types import Cursor | ||
|
||
|
||
def run_create(cur: Cursor, database_engine: BaseDatabaseEngine, *args, **kwargs): | ||
# Complain if the `event_stream_ordering` in membership tables doesn't match | ||
# the `stream_ordering` row with the same `event_id` in `events`. | ||
if isinstance(database_engine, Sqlite3Engine): | ||
for table in ( | ||
"current_state_events", | ||
"local_current_membership", | ||
"room_memberships", | ||
): | ||
cur.execute( | ||
f""" | ||
CREATE TRIGGER IF NOT EXISTS {table}_bad_event_stream_ordering | ||
BEFORE INSERT ON {table} | ||
FOR EACH ROW | ||
BEGIN | ||
SELECT RAISE(ABORT, 'Incorrect event_stream_ordering in {table}') | ||
WHERE EXISTS ( | ||
SELECT 1 FROM events | ||
WHERE events.event_id = NEW.event_id | ||
AND events.stream_ordering != NEW.event_stream_ordering | ||
); | ||
END; | ||
""" | ||
) | ||
elif isinstance(database_engine, PostgresEngine): | ||
cur.execute( | ||
""" | ||
CREATE OR REPLACE FUNCTION check_event_stream_ordering() RETURNS trigger AS $BODY$ | ||
BEGIN | ||
IF EXISTS ( | ||
SELECT 1 FROM events | ||
WHERE events.event_id = NEW.event_id | ||
AND events.stream_ordering != NEW.event_stream_ordering | ||
) THEN | ||
RAISE EXCEPTION 'Incorrect event_stream_ordering'; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$BODY$ LANGUAGE plpgsql; | ||
""" | ||
) | ||
|
||
for table in ( | ||
"current_state_events", | ||
"local_current_membership", | ||
"room_memberships", | ||
): | ||
cur.execute( | ||
f""" | ||
CREATE TRIGGER check_event_stream_ordering BEFORE INSERT OR UPDATE ON {table} | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE check_event_stream_ordering() | ||
""" | ||
) | ||
else: | ||
raise NotImplementedError("Unknown database engine") |