Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 956b47a

Browse files
committed
Simplify cache invalidation after event persist txn
1 parent 9772e36 commit 956b47a

File tree

3 files changed

+28
-95
lines changed

3 files changed

+28
-95
lines changed

changelog.d/13796.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deleteme.

synapse/storage/databases/main/cache.py

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def _invalidate_caches_for_event(
253253
self.get_applicable_edit.invalidate((relates_to,))
254254
self.get_thread_summary.invalidate((relates_to,))
255255
self.get_thread_participated.invalidate((relates_to,))
256+
self.get_mutual_event_relations_for_rel_type.invalidate((relates_to,))
256257

257258
async def invalidate_cache_and_stream(
258259
self, cache_name: str, keys: Tuple[Any, ...]

synapse/storage/databases/main/events.py

+26-95
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from prometheus_client import Counter
3636

3737
import synapse.metrics
38-
from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
38+
from synapse.api.constants import EventContentFields, EventTypes
3939
from synapse.api.errors import Codes, SynapseError
4040
from synapse.api.room_versions import RoomVersions
4141
from synapse.events import EventBase, relation_from_event
@@ -459,8 +459,33 @@ def _persist_events_txn(
459459

460460
# We call this last as it assumes we've inserted the events into
461461
# room_memberships, where applicable.
462+
# NB: This function invalidates all state related caches
462463
self._update_current_state_txn(txn, state_delta_for_room, min_stream_order)
463464

465+
# Finally, invalidate relevant caches for each event once the txn completes
466+
for event, _ in events_and_contexts:
467+
self.store.invalidate_get_event_cache_after_txn(txn, event.event_id)
468+
if event.redacts:
469+
self.store.invalidate_get_event_cache_after_txn(txn, event.redacts)
470+
471+
relates_to = None
472+
relation = relation_from_event(event)
473+
if relation:
474+
relates_to = relation.parent_id
475+
476+
assert event.internal_metadata.stream_ordering is not None
477+
txn.call_after(
478+
self.store._invalidate_caches_for_event,
479+
event.internal_metadata.stream_ordering,
480+
event.event_id,
481+
event.room_id,
482+
event.type,
483+
getattr(event, "state_key", None),
484+
event.redacts,
485+
relates_to,
486+
backfilled=False,
487+
)
488+
464489
def _persist_event_auth_chain_txn(
465490
self,
466491
txn: LoggingTransaction,
@@ -1222,9 +1247,6 @@ def _update_forward_extremities_txn(
12221247
self.db_pool.simple_delete_txn(
12231248
txn, table="event_forward_extremities", keyvalues={"room_id": room_id}
12241249
)
1225-
txn.call_after(
1226-
self.store.get_latest_event_ids_in_room.invalidate, (room_id,)
1227-
)
12281250

12291251
self.db_pool.simple_insert_many_txn(
12301252
txn,
@@ -1294,8 +1316,6 @@ def _update_room_depths_txn(
12941316
"""
12951317
depth_updates: Dict[str, int] = {}
12961318
for event, context in events_and_contexts:
1297-
# Remove the any existing cache entries for the event_ids
1298-
self.store.invalidate_get_event_cache_after_txn(txn, event.event_id)
12991319
# Then update the `stream_ordering` position to mark the latest
13001320
# event as the front of the room. This should not be done for
13011321
# backfilled events because backfilled events have negative
@@ -1697,16 +1717,7 @@ async def prefill() -> None:
16971717
txn.async_call_after(prefill)
16981718

16991719
def _store_redaction(self, txn: LoggingTransaction, event: EventBase) -> None:
1700-
"""Invalidate the caches for the redacted event.
1701-
1702-
Note that these caches are also cleared as part of event replication in
1703-
_invalidate_caches_for_event.
1704-
"""
17051720
assert event.redacts is not None
1706-
self.store.invalidate_get_event_cache_after_txn(txn, event.redacts)
1707-
txn.call_after(self.store.get_relations_for_event.invalidate, (event.redacts,))
1708-
txn.call_after(self.store.get_applicable_edit.invalidate, (event.redacts,))
1709-
17101721
self.db_pool.simple_upsert_txn(
17111722
txn,
17121723
table="redactions",
@@ -1807,35 +1818,11 @@ def _store_room_members_txn(
18071818

18081819
for event in events:
18091820
assert event.internal_metadata.stream_ordering is not None
1810-
txn.call_after(
1811-
self.store._membership_stream_cache.entity_has_changed,
1812-
event.state_key,
1813-
event.internal_metadata.stream_ordering,
1814-
)
1815-
txn.call_after(
1816-
self.store.get_invited_rooms_for_local_user.invalidate,
1817-
(event.state_key,),
1818-
)
1819-
txn.call_after(
1820-
self.store.get_local_users_in_room.invalidate,
1821-
(event.room_id,),
1822-
)
1823-
txn.call_after(
1824-
self.store.get_number_joined_users_in_room.invalidate,
1825-
(event.room_id,),
1826-
)
18271821
txn.call_after(
18281822
self.store.get_user_in_room_with_profile.invalidate,
18291823
(event.room_id, event.state_key),
18301824
)
18311825

1832-
# The `_get_membership_from_event_id` is immutable, except for the
1833-
# case where we look up an event *before* persisting it.
1834-
txn.call_after(
1835-
self.store._get_membership_from_event_id.invalidate,
1836-
(event.event_id,),
1837-
)
1838-
18391826
# We update the local_current_membership table only if the event is
18401827
# "current", i.e., its something that has just happened.
18411828
#
@@ -1883,35 +1870,6 @@ def _handle_event_relations(
18831870
},
18841871
)
18851872

1886-
txn.call_after(
1887-
self.store.get_relations_for_event.invalidate, (relation.parent_id,)
1888-
)
1889-
txn.call_after(
1890-
self.store.get_aggregation_groups_for_event.invalidate,
1891-
(relation.parent_id,),
1892-
)
1893-
txn.call_after(
1894-
self.store.get_mutual_event_relations_for_rel_type.invalidate,
1895-
(relation.parent_id,),
1896-
)
1897-
1898-
if relation.rel_type == RelationTypes.REPLACE:
1899-
txn.call_after(
1900-
self.store.get_applicable_edit.invalidate, (relation.parent_id,)
1901-
)
1902-
1903-
if relation.rel_type == RelationTypes.THREAD:
1904-
txn.call_after(
1905-
self.store.get_thread_summary.invalidate, (relation.parent_id,)
1906-
)
1907-
# It should be safe to only invalidate the cache if the user has not
1908-
# previously participated in the thread, but that's difficult (and
1909-
# potentially error-prone) so it is always invalidated.
1910-
txn.call_after(
1911-
self.store.get_thread_participated.invalidate,
1912-
(relation.parent_id, event.sender),
1913-
)
1914-
19151873
def _handle_insertion_event(
19161874
self, txn: LoggingTransaction, event: EventBase
19171875
) -> None:
@@ -2213,28 +2171,6 @@ def _set_push_actions_for_event_and_users_txn(
22132171
),
22142172
)
22152173

2216-
room_to_event_ids: Dict[str, List[str]] = {}
2217-
for e in non_outlier_events:
2218-
room_to_event_ids.setdefault(e.room_id, []).append(e.event_id)
2219-
2220-
for room_id, event_ids in room_to_event_ids.items():
2221-
rows = self.db_pool.simple_select_many_txn(
2222-
txn,
2223-
table="event_push_actions_staging",
2224-
column="event_id",
2225-
iterable=event_ids,
2226-
keyvalues={},
2227-
retcols=("user_id",),
2228-
)
2229-
2230-
user_ids = {row["user_id"] for row in rows}
2231-
2232-
for user_id in user_ids:
2233-
txn.call_after(
2234-
self.store.get_unread_event_push_actions_by_room_for_user.invalidate,
2235-
(room_id, user_id),
2236-
)
2237-
22382174
# Now we delete the staging area for *all* events that were being
22392175
# persisted.
22402176
txn.execute_batch(
@@ -2249,11 +2185,6 @@ def _set_push_actions_for_event_and_users_txn(
22492185
def _remove_push_actions_for_event_id_txn(
22502186
self, txn: LoggingTransaction, room_id: str, event_id: str
22512187
) -> None:
2252-
# Sad that we have to blow away the cache for the whole room here
2253-
txn.call_after(
2254-
self.store.get_unread_event_push_actions_by_room_for_user.invalidate,
2255-
(room_id,),
2256-
)
22572188
txn.execute(
22582189
"DELETE FROM event_push_actions WHERE room_id = ? AND event_id = ?",
22592190
(room_id, event_id),

0 commit comments

Comments
 (0)