This repository was 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
Reintroduce membership tables event stream ordering #15128
Merged
DMRobertson
merged 13 commits into
matrix-org:develop
from
beeper:membership-tables-event-stream-ordering-redux
Mar 24, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9e15a7f
Add `event_stream_ordering` column to membership state tables
Fizzadar da71761
Add background job to populate `event_stream_ordering` on membership …
Fizzadar eec814d
Changelog file
Fizzadar 6901682
Bump `SCHEMA_COMPAT_VERSION`
Fizzadar bc1a1a9
Fix formatting
Fizzadar 5af0453
Use cast for clearer typing
Fizzadar 845c9b9
Revert "Add background job to populate `event_stream_ordering` on mem…
Fizzadar 8979abc
Make denormalised `event_stream_ordering` columns foreign keys
Fizzadar bc3845c
Add comment in schema file explaining new denormalised columns
Fizzadar 36533a9
Add triggers to enforce consistency of `event_stream_ordering` columns
Fizzadar b3e9bbc
Re-order purge room tables to account for foreign keys
Fizzadar 27b469f
Bump schema version to 75
Fizzadar f55d163
Correct typo
Fizzadar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
synapse/storage/schema/main/delta/74/01membership_tables_event_stream_ordering.sql
This file contains hidden or 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,20 @@ | ||
| /* 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. | ||
| 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/02membership_tables_event_stream_ordering_triggers.py
This file contains hidden or 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") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.