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
Clear event caches when we purge history #15609
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c9d62c3
WIP clear event caches when we purge history
erikjohnston 39cbd18
Merge remote-tracking branch 'origin/develop' into erikj/clear_event_…
clokep 139e216
Remove TODO from a test which will be fixed in this PR.
clokep d60048d
Invalidate more caches
erikjohnston 5214379
Make .clear sync
erikjohnston d3a0464
Newsfile
erikjohnston 4e91fbe
Stream cache invalidation
erikjohnston 166dd75
Move cache invalidate to the end
erikjohnston a6673f8
Remove comment
erikjohnston 35fe290
Refactor
erikjohnston 765b186
fixup
erikjohnston e5c88c4
Review comments
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 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 @@ | ||
Correctly clear caches when we delete a room. |
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 |
---|---|---|
|
@@ -46,6 +46,9 @@ | |
# based on the current state when notifying workers over replication. | ||
CURRENT_STATE_CACHE_NAME = "cs_cache_fake" | ||
|
||
# As above, but for invalidating | ||
DELETE_ROOM_CACHE_NAME = "dr_cache_fake" | ||
|
||
|
||
class CacheInvalidationWorkerStore(SQLBaseStore): | ||
def __init__( | ||
|
@@ -175,6 +178,14 @@ def process_replication_rows( | |
room_id = row.keys[0] | ||
members_changed = set(row.keys[1:]) | ||
self._invalidate_state_caches(room_id, members_changed) | ||
elif row.cache_func == DELETE_ROOM_CACHE_NAME: | ||
if row.keys is None: | ||
raise Exception( | ||
"Can't send an 'invalidate all' for 'delete room' cache" | ||
) | ||
|
||
room_id = row.keys[0] | ||
self._invalidate_caches_for_room(room_id) | ||
else: | ||
self._attempt_to_invalidate_cache(row.cache_func, row.keys) | ||
|
||
|
@@ -226,6 +237,9 @@ def _invalidate_caches_for_event( | |
relates_to: Optional[str], | ||
backfilled: bool, | ||
) -> None: | ||
# XXX: If you add something to this function make sure you add it to | ||
# `_invalidate_caches_for_room` as well. | ||
|
||
# This invalidates any local in-memory cached event objects, the original | ||
# process triggering the invalidation is responsible for clearing any external | ||
# cached objects. | ||
|
@@ -271,6 +285,102 @@ def _invalidate_caches_for_event( | |
self._attempt_to_invalidate_cache("get_thread_participated", (relates_to,)) | ||
self._attempt_to_invalidate_cache("get_threads", (room_id,)) | ||
|
||
def _invalidate_caches_for_room_and_stream( | ||
self, txn: LoggingTransaction, room_id: str | ||
) -> None: | ||
"""Invalidate caches associated with rooms, and stream to replication. | ||
|
||
Used when we delete events in rooms, but don't know which events we've | ||
deleted. | ||
""" | ||
|
||
self._send_invalidation_to_replication(txn, DELETE_ROOM_CACHE_NAME, [room_id]) | ||
txn.call_after(self._invalidate_caches_for_room, room_id) | ||
|
||
def _invalidate_caches_for_room(self, room_id: str) -> None: | ||
"""Invalidate caches associated with rooms. | ||
|
||
Used when we delete events in rooms, but don't know which events we've | ||
deleted. | ||
""" | ||
|
||
self._invalidate_local_get_event_cache_all() # type: ignore[attr-defined] | ||
DMRobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self._attempt_to_invalidate_cache("have_seen_event", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_latest_event_ids_in_room", (room_id,)) | ||
self._attempt_to_invalidate_cache( | ||
"get_unread_event_push_actions_by_room_for_user", (room_id,) | ||
) | ||
|
||
self._attempt_to_invalidate_cache("_get_membership_from_event_id", None) | ||
self._attempt_to_invalidate_cache("get_relations_for_event", None) | ||
self._attempt_to_invalidate_cache("get_applicable_edit", None) | ||
self._attempt_to_invalidate_cache("get_thread_id", None) | ||
self._attempt_to_invalidate_cache("get_thread_id_for_receipts", None) | ||
self._attempt_to_invalidate_cache("get_invited_rooms_for_local_user", None) | ||
self._attempt_to_invalidate_cache( | ||
"get_rooms_for_user_with_stream_ordering", None | ||
) | ||
self._attempt_to_invalidate_cache("get_rooms_for_user", None) | ||
self._attempt_to_invalidate_cache("get_references_for_event", None) | ||
self._attempt_to_invalidate_cache("get_thread_summary", None) | ||
self._attempt_to_invalidate_cache("get_thread_participated", None) | ||
self._attempt_to_invalidate_cache("get_threads", (room_id,)) | ||
|
||
self._attempt_to_invalidate_cache("_get_state_group_for_event", None) | ||
|
||
# Also invalidate room based caches | ||
|
||
self._attempt_to_invalidate_cache("get_account_data_for_room", None) | ||
self._attempt_to_invalidate_cache("get_account_data_for_room_and_type", None) | ||
self._attempt_to_invalidate_cache("get_aliases_for_room", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_latest_event_ids_in_room", (room_id,)) | ||
self._attempt_to_invalidate_cache("_get_forward_extremeties_for_room", None) | ||
self._attempt_to_invalidate_cache( | ||
"get_unread_event_push_actions_by_room_for_user", (room_id,) | ||
) | ||
self._attempt_to_invalidate_cache("get_event_ordering", None) | ||
self._attempt_to_invalidate_cache("is_partial_state_event", None) | ||
self._attempt_to_invalidate_cache( | ||
"_get_linearized_receipts_for_room", (room_id,) | ||
) | ||
self._attempt_to_invalidate_cache("is_room_blocked", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_retention_policy_for_room", (room_id,)) | ||
self._attempt_to_invalidate_cache( | ||
"_get_partial_state_servers_at_join", (room_id,) | ||
) | ||
self._attempt_to_invalidate_cache("is_partial_state_room", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_invited_rooms_for_local_user", None) | ||
self._attempt_to_invalidate_cache("_get_joined_profile_from_event_id", None) | ||
self._attempt_to_invalidate_cache("is_host_invited", None) | ||
self._attempt_to_invalidate_cache("is_host_joined", None) | ||
self._attempt_to_invalidate_cache( | ||
"get_current_hosts_in_room_ordered", (room_id,) | ||
) | ||
self._attempt_to_invalidate_cache("did_forget", None) | ||
self._attempt_to_invalidate_cache("get_forgotten_rooms_for_user", None) | ||
self._attempt_to_invalidate_cache("_get_membership_from_event_id", None) | ||
self._attempt_to_invalidate_cache("get_room_version_id", (room_id,)) | ||
|
||
# And delete state caches. | ||
|
||
self._attempt_to_invalidate_cache("get_partial_current_state_ids", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_users_in_room", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_current_hosts_in_room", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_users_in_room_with_profiles", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_number_joined_users_in_room", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_local_users_in_room", (room_id,)) | ||
self._attempt_to_invalidate_cache("does_pair_of_users_share_a_room", None) | ||
self._attempt_to_invalidate_cache("get_user_in_room_with_profile", None) | ||
self._attempt_to_invalidate_cache( | ||
"get_rooms_for_user_with_stream_ordering", None | ||
) | ||
self._attempt_to_invalidate_cache("get_rooms_for_user", None) | ||
self._attempt_to_invalidate_cache("get_room_summary", (room_id,)) | ||
self._attempt_to_invalidate_cache("get_partial_current_state_ids", (room_id,)) | ||
DMRobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Plus we should clear the state cache in the state handler. | ||
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. Is this TODO? 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. Oh, yeah, I convinced myself that we don't need to worry about the Will remove. 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. Thanks. Can you update the PR description to reflect this too? |
||
|
||
async def invalidate_cache_and_stream( | ||
self, cache_name: str, keys: Tuple[Any, ...] | ||
) -> None: | ||
|
@@ -377,6 +487,9 @@ def _send_invalidation_to_replication( | |
"Can't stream invalidate all with magic current state cache" | ||
) | ||
|
||
if cache_name == DELETE_ROOM_CACHE_NAME and keys is None: | ||
raise Exception("Can't stream invalidate all with magic delete room cache") | ||
|
||
if isinstance(self.database_engine, PostgresEngine): | ||
assert self._cache_id_gen is not None | ||
|
||
|
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
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
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.
Is this accurate? You now call explictly call
_invalidate_caches_for_events
and_invalidate_caches_for_room
in the delete room case.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.
Whoops, that should say
_invalidate_caches_for_events