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
Fix background schema updates failing over a large upgrade gap #15887
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 @@ | ||
Fix background schema updates failing over a large upgrade gap. |
70 changes: 70 additions & 0 deletions
70
synapse/storage/schema/main/delta/78/05_mitigate_stream_ordering_update_race.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,70 @@ | ||
# 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. | ||
|
||
|
||
from synapse.storage.database import LoggingTransaction | ||
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine | ||
|
||
|
||
def run_create( | ||
cur: LoggingTransaction, | ||
database_engine: BaseDatabaseEngine, | ||
) -> None: | ||
""" | ||
An attempt to mitigate a painful race between foreground and background updates | ||
touching the `stream_ordering` column of the events table. More info can be found | ||
at https://github.com/matrix-org/synapse/issues/15677. | ||
""" | ||
|
||
# technically the bg update we're concerned with below should only have been added in | ||
# postgres but it doesn't hurt to be extra careful | ||
if isinstance(database_engine, PostgresEngine): | ||
select_sql = """ | ||
SELECT 1 FROM background_updates | ||
WHERE update_name = 'replace_stream_ordering_column' | ||
""" | ||
cur.execute(select_sql) | ||
res = cur.fetchone() | ||
|
||
# if the background update `replace_stream_ordering_column` is still pending, we need | ||
# to drop the indexes added in 7403, and re-add them to the column `stream_ordering2` | ||
# with the idea that they will be preserved when the column is renamed `stream_ordering` | ||
# after the background update has finished | ||
if res: | ||
drop_cse_sql = """ | ||
ALTER TABLE current_state_events DROP CONSTRAINT event_stream_ordering_fkey | ||
""" | ||
cur.execute(drop_cse_sql) | ||
|
||
drop_lcm_sql = """ | ||
ALTER TABLE local_current_membership DROP CONSTRAINT event_stream_ordering_fkey | ||
""" | ||
cur.execute(drop_lcm_sql) | ||
|
||
drop_rm_sql = """ | ||
ALTER TABLE room_memberships DROP CONSTRAINT event_stream_ordering_fkey | ||
""" | ||
cur.execute(drop_rm_sql) | ||
|
||
add_cse_sql = """ | ||
ALTER TABLE current_state_events ADD CONSTRAINT event_stream_ordering_fkey | ||
FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering2) NOT VALID; | ||
""" | ||
cur.execute(add_cse_sql) | ||
|
||
add_lcm_sql = """ | ||
ALTER TABLE local_current_membership ADD CONSTRAINT event_stream_ordering_fkey | ||
FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering2) NOT VALID; | ||
""" | ||
cur.execute(add_lcm_sql) | ||
|
||
add_rm_sql = """ | ||
ALTER TABLE room_memberships ADD CONSTRAINT event_stream_ordering_fkey | ||
FOREIGN KEY (event_stream_ordering) REFERENCES events(stream_ordering2) NOT VALID; | ||
""" | ||
cur.execute(add_rm_sql) | ||
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.
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.
Hmm, I'm not seeing anywhere in the code where we actually validate these constraits?
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 don't think that has happened yet. These columns/constraints were introduced in #15356. On the discussion of that PR there is mention of validating them in the future, after a background job migrates historical data from the
stream_ordering
column ofevents
to the new stream ordering columns added, but I don't see anywhere in the deltas after this (for reference these were added in74
) that the background job was added and run or that the constraints were validated.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.
Ah, I see, fair!