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

Commit 2888d7e

Browse files
authored
Faster remote room joins: invalidate caches and unblock requests when receiving un-partial-stated event notifications over replication. [rei:frrj/streams/unpsr] (#14546)
1 parent adbf0cf commit 2888d7e

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

changelog.d/14546.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Faster remote room joins: stream the un-partial-stating of events over replication.

synapse/replication/tcp/client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@
3636
TagAccountDataStream,
3737
ToDeviceStream,
3838
TypingStream,
39+
UnPartialStatedEventStream,
3940
UnPartialStatedRoomStream,
4041
)
4142
from synapse.replication.tcp.streams.events import (
4243
EventsStream,
4344
EventsStreamEventRow,
4445
EventsStreamRow,
4546
)
46-
from synapse.replication.tcp.streams.partial_state import UnPartialStatedRoomStreamRow
47+
from synapse.replication.tcp.streams.partial_state import (
48+
UnPartialStatedEventStreamRow,
49+
UnPartialStatedRoomStreamRow,
50+
)
4751
from synapse.types import PersistedEventPosition, ReadReceipt, StreamKeyType, UserID
4852
from synapse.util.async_helpers import Linearizer, timeout_deferred
4953
from synapse.util.metrics import Measure
@@ -247,6 +251,14 @@ async def on_rdata(
247251
self._state_storage_controller.notify_room_un_partial_stated(
248252
row.room_id
249253
)
254+
elif stream_name == UnPartialStatedEventStream.NAME:
255+
for row in rows:
256+
assert isinstance(row, UnPartialStatedEventStreamRow)
257+
258+
# Wake up any tasks waiting for the event to be un-partial-stated.
259+
self._state_storage_controller.notify_event_un_partial_stated(
260+
row.event_id
261+
)
250262

251263
await self._presence_handler.process_replication_rows(
252264
stream_name, instance_name, token, rows

synapse/storage/databases/main/events_worker.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
run_as_background_process,
6060
wrap_as_background_process,
6161
)
62-
from synapse.replication.tcp.streams import BackfillStream
62+
from synapse.replication.tcp.streams import BackfillStream, UnPartialStatedEventStream
6363
from synapse.replication.tcp.streams.events import EventsStream
64+
from synapse.replication.tcp.streams.partial_state import UnPartialStatedEventStreamRow
6465
from synapse.storage._base import SQLBaseStore, db_to_json, make_in_list_sql_clause
6566
from synapse.storage.database import (
6667
DatabasePool,
@@ -391,6 +392,16 @@ def process_replication_rows(
391392
self._stream_id_gen.advance(instance_name, token)
392393
elif stream_name == BackfillStream.NAME:
393394
self._backfill_id_gen.advance(instance_name, -token)
395+
elif stream_name == UnPartialStatedEventStream.NAME:
396+
for row in rows:
397+
assert isinstance(row, UnPartialStatedEventStreamRow)
398+
399+
self.is_partial_state_event.invalidate((row.event_id,))
400+
401+
if row.rejection_status_changed:
402+
# If the partial-stated event became rejected or unrejected
403+
# when it wasn't before, we need to invalidate this cache.
404+
self._invalidate_local_get_event_cache(row.event_id)
394405

395406
super().process_replication_rows(stream_name, instance_name, token, rows)
396407

@@ -2380,6 +2391,9 @@ def mark_event_rejected_txn(
23802391
23812392
This can happen, for example, when resyncing state during a faster join.
23822393
2394+
It is the caller's responsibility to ensure that other workers are
2395+
sent a notification so that they call `_invalidate_local_get_event_cache()`.
2396+
23832397
Args:
23842398
txn:
23852399
event_id: ID of event to update
@@ -2418,14 +2432,3 @@ def mark_event_rejected_txn(
24182432
)
24192433

24202434
self.invalidate_get_event_cache_after_txn(txn, event_id)
2421-
2422-
# TODO(faster_joins): invalidate the cache on workers. Ideally we'd just
2423-
# call '_send_invalidation_to_replication', but we actually need the other
2424-
# end to call _invalidate_local_get_event_cache() rather than (just)
2425-
# _get_event_cache.invalidate().
2426-
#
2427-
# One solution might be to (somehow) get the workers to call
2428-
# _invalidate_caches_for_event() (though that will invalidate more than
2429-
# strictly necessary).
2430-
#
2431-
# https://github.com/matrix-org/synapse/issues/12994

synapse/storage/databases/main/state.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
import collections.abc
1616
import logging
17-
from typing import TYPE_CHECKING, Collection, Dict, Iterable, Optional, Set, Tuple
17+
from typing import TYPE_CHECKING, Any, Collection, Dict, Iterable, Optional, Set, Tuple
1818

1919
import attr
2020

@@ -24,6 +24,8 @@
2424
from synapse.events import EventBase
2525
from synapse.events.snapshot import EventContext
2626
from synapse.logging.opentracing import trace
27+
from synapse.replication.tcp.streams import UnPartialStatedEventStream
28+
from synapse.replication.tcp.streams.partial_state import UnPartialStatedEventStreamRow
2729
from synapse.storage._base import SQLBaseStore
2830
from synapse.storage.database import (
2931
DatabasePool,
@@ -82,6 +84,20 @@ def __init__(
8284
super().__init__(database, db_conn, hs)
8385
self._instance_name: str = hs.get_instance_name()
8486

87+
def process_replication_rows(
88+
self,
89+
stream_name: str,
90+
instance_name: str,
91+
token: int,
92+
rows: Iterable[Any],
93+
) -> None:
94+
if stream_name == UnPartialStatedEventStream.NAME:
95+
for row in rows:
96+
assert isinstance(row, UnPartialStatedEventStreamRow)
97+
self._get_state_group_for_event.invalidate((row.event_id,))
98+
99+
super().process_replication_rows(stream_name, instance_name, token, rows)
100+
85101
async def get_room_version(self, room_id: str) -> RoomVersion:
86102
"""Get the room_version of a given room
87103
Raises:

0 commit comments

Comments
 (0)