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
Clear out old rows from event_push_actions_staging
#14020
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
368dc3d
Add BG task to clear stale staged push actions
erikjohnston 481bbb1
Handle existing rows
erikjohnston 8f04bbe
Newsfile
erikjohnston 0593cbd
Merge remote-tracking branch 'origin/develop' into erikj/push_actions…
erikjohnston 4d0995f
Typing
erikjohnston 58f0c2c
Fix SQLite CI
erikjohnston 608eb06
Update synapse/storage/databases/main/event_push_actions.py
erikjohnston bfc259d
Update synapse/storage/databases/main/event_push_actions.py
erikjohnston 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 @@ | ||
Clear out stale entries in `event_push_actions_staging` table. | ||
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 |
---|---|---|
|
@@ -205,6 +205,9 @@ def __init__( | |
): | ||
super().__init__(database, db_conn, hs) | ||
|
||
# Track when the process started. | ||
self._started_ts = self._clock.time_msec() | ||
|
||
# These get correctly set by _find_stream_orderings_for_times_txn | ||
self.stream_ordering_month_ago: Optional[int] = None | ||
self.stream_ordering_day_ago: Optional[int] = None | ||
|
@@ -224,6 +227,10 @@ def __init__( | |
self._rotate_notifs, 30 * 1000 | ||
) | ||
|
||
self._clear_old_staging_loop = self._clock.looping_call( | ||
self._clear_old_push_actions_staging, 30 * 60 * 1000 | ||
) | ||
|
||
self.db_pool.updates.register_background_index_update( | ||
"event_push_summary_unique_index", | ||
index_name="event_push_summary_unique_index", | ||
|
@@ -791,7 +798,7 @@ async def add_push_actions_to_staging( | |
# can be used to insert into the `event_push_actions_staging` table. | ||
def _gen_entry( | ||
user_id: str, actions: Collection[Union[Mapping, str]] | ||
) -> Tuple[str, str, str, int, int, int, str]: | ||
) -> Tuple[str, str, str, int, int, int, str, int]: | ||
is_highlight = 1 if _action_has_highlight(actions) else 0 | ||
notif = 1 if "notify" in actions else 0 | ||
return ( | ||
|
@@ -802,6 +809,7 @@ def _gen_entry( | |
is_highlight, # highlight column | ||
int(count_as_unread), # unread column | ||
thread_id, # thread_id column | ||
self._clock.time_msec(), # inserted_ts column | ||
) | ||
|
||
await self.db_pool.simple_insert_many( | ||
|
@@ -814,6 +822,7 @@ def _gen_entry( | |
"highlight", | ||
"unread", | ||
"thread_id", | ||
"inserted_ts", | ||
), | ||
values=[ | ||
_gen_entry(user_id, actions) | ||
|
@@ -1340,6 +1349,53 @@ def remove_old_push_actions_that_have_rotated_txn( | |
if done: | ||
break | ||
|
||
@wrap_as_background_process("_clear_old_push_actions_staging") | ||
async def _clear_old_push_actions_staging(self) -> None: | ||
"""Clear out any old event push actions from the staging table for | ||
events that we failed to persist. | ||
""" | ||
|
||
# We delete anything more than an hour old, on the assumption that we'll | ||
# never take more than an hour to persist an event. | ||
delete_before_ts = self._clock.time_msec() - 60 * 60 * 1000 | ||
Comment on lines
+1358
to
+1360
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is building in an assumption that the background worker and event persister workers are restarted within 1 hour of each other. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, no, since we add a default to (I think, i spent quite a while trying to figure out how to make this work) |
||
|
||
if self._started_ts > delete_before_ts: | ||
# We need to wait for at least an hour before we started deleting, | ||
# so that we know it's safe to delete rows with NULL `inserted_ts`. | ||
return | ||
|
||
# We don't have an index on `inserted_ts`, instead we assume that the | ||
# number of "live" rows in `event_push_actions_staging` is small enough | ||
# that an infrequent periodic scan won't cause a problem. | ||
# | ||
# Note: we also delete any columns with NULL `inserted_ts`, this is safe | ||
# as we added a default value to new rows and so they must be at least | ||
# an hour old. | ||
limit = 1000 | ||
sql = """ | ||
DELETE FROM event_push_actions_staging WHERE event_id IN ( | ||
SELECT event_id FROM event_push_actions_staging WHERE | ||
inserted_ts < ? OR inserted_ts IS NULL | ||
LIMIT ? | ||
) | ||
""" | ||
|
||
def _clear_old_push_actions_staging_txn(txn: LoggingTransaction) -> bool: | ||
txn.execute(sql, (delete_before_ts, limit)) | ||
return txn.rowcount >= limit | ||
|
||
while True: | ||
# Returns true if we have more stuff to delete from the table. | ||
deleted = await self.db_pool.runInteraction( | ||
"_clear_old_push_actions_staging", _clear_old_push_actions_staging_txn | ||
) | ||
|
||
if not deleted: | ||
return | ||
|
||
# We sleep to ensure that we don't overwhelm the DB. | ||
await self._clock.sleep(1.0) | ||
|
||
|
||
class EventPushActionsStore(EventPushActionsWorkerStore): | ||
EPA_HIGHLIGHT_INDEX = "epa_highlight_index" | ||
|
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
22 changes: 22 additions & 0 deletions
22
synapse/storage/schema/main/delta/73/05old_push_actions.sql.postgres
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,22 @@ | ||
/* Copyright 2022 The Matrix.org Foundation C.I.C | ||
* | ||
* 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. | ||
*/ | ||
|
||
-- Add a column so that we know when a push action was inserted, to make it | ||
-- easier to clear out old ones. | ||
ALTER TABLE event_push_actions_staging ADD COLUMN inserted_ts BIGINT; | ||
|
||
-- We now add a default for *new* rows. We don't do this above as we don't want | ||
-- to have to update every remove with the new default. | ||
ALTER TABLE event_push_actions_staging ALTER COLUMN inserted_ts SET DEFAULT extract(epoch from now()) * 1000; |
24 changes: 24 additions & 0 deletions
24
synapse/storage/schema/main/delta/73/05old_push_actions.sql.sqlite
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,24 @@ | ||
/* Copyright 2022 The Matrix.org Foundation C.I.C | ||
* | ||
* 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. | ||
*/ | ||
|
||
-- On SQLite we must be in monolith mode and updating the database from Synapse, | ||
-- so its safe to assume that `event_push_actions_staging` should be empty (as | ||
-- over restart an event must either have been fully persisted or we'll | ||
-- recalculate the push actions) | ||
DELETE FROM event_push_actions_staging; | ||
|
||
-- Add a column so that we know when a push action was inserted, to make it | ||
-- easier to clear out old ones. | ||
ALTER TABLE event_push_actions_staging ADD COLUMN inserted_ts BIGINT; |
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.