@@ -923,7 +923,8 @@ async def backfill(self, dest, room_id, limit, extremities):
923923 )
924924 )
925925
926- await self ._handle_new_events (dest , ev_infos , backfilled = True )
926+ if ev_infos :
927+ await self ._handle_new_events (dest , room_id , ev_infos , backfilled = True )
927928
928929 # Step 2: Persist the rest of the events in the chunk one by one
929930 events .sort (key = lambda e : e .depth )
@@ -1216,7 +1217,7 @@ async def get_event(event_id: str):
12161217 event_infos .append (_NewEventInfo (event , None , auth ))
12171218
12181219 await self ._handle_new_events (
1219- destination , event_infos ,
1220+ destination , room_id , event_infos ,
12201221 )
12211222
12221223 def _sanity_check_event (self , ev ):
@@ -1363,15 +1364,15 @@ async def do_invite_join(
13631364 )
13641365
13651366 max_stream_id = await self ._persist_auth_tree (
1366- origin , auth_chain , state , event , room_version_obj
1367+ origin , room_id , auth_chain , state , event , room_version_obj
13671368 )
13681369
13691370 # We wait here until this instance has seen the events come down
13701371 # replication (if we're using replication) as the below uses caches.
1371- #
1372- # TODO: Currently the events stream is written to from master
13731372 await self ._replication .wait_for_stream_position (
1374- self .config .worker .writers .events , "events" , max_stream_id
1373+ self .config .worker .events_shard_config .get_instance (room_id ),
1374+ "events" ,
1375+ max_stream_id ,
13751376 )
13761377
13771378 # Check whether this room is the result of an upgrade of a room we already know
@@ -1625,7 +1626,7 @@ async def on_invite_request(
16251626 )
16261627
16271628 context = await self .state_handler .compute_event_context (event )
1628- await self .persist_events_and_notify ([(event , context )])
1629+ await self .persist_events_and_notify (event . room_id , [(event , context )])
16291630
16301631 return event
16311632
@@ -1652,7 +1653,9 @@ async def do_remotely_reject_invite(
16521653 await self .federation_client .send_leave (host_list , event )
16531654
16541655 context = await self .state_handler .compute_event_context (event )
1655- stream_id = await self .persist_events_and_notify ([(event , context )])
1656+ stream_id = await self .persist_events_and_notify (
1657+ event .room_id , [(event , context )]
1658+ )
16561659
16571660 return event , stream_id
16581661
@@ -1900,7 +1903,7 @@ async def _handle_new_event(
19001903 )
19011904
19021905 await self .persist_events_and_notify (
1903- [(event , context )], backfilled = backfilled
1906+ event . room_id , [(event , context )], backfilled = backfilled
19041907 )
19051908 except Exception :
19061909 run_in_background (
@@ -1913,6 +1916,7 @@ async def _handle_new_event(
19131916 async def _handle_new_events (
19141917 self ,
19151918 origin : str ,
1919+ room_id : str ,
19161920 event_infos : Iterable [_NewEventInfo ],
19171921 backfilled : bool = False ,
19181922 ) -> None :
@@ -1944,6 +1948,7 @@ async def prep(ev_info: _NewEventInfo):
19441948 )
19451949
19461950 await self .persist_events_and_notify (
1951+ room_id ,
19471952 [
19481953 (ev_info .event , context )
19491954 for ev_info , context in zip (event_infos , contexts )
@@ -1954,6 +1959,7 @@ async def prep(ev_info: _NewEventInfo):
19541959 async def _persist_auth_tree (
19551960 self ,
19561961 origin : str ,
1962+ room_id : str ,
19571963 auth_events : List [EventBase ],
19581964 state : List [EventBase ],
19591965 event : EventBase ,
@@ -1968,6 +1974,7 @@ async def _persist_auth_tree(
19681974
19691975 Args:
19701976 origin: Where the events came from
1977+ room_id,
19711978 auth_events
19721979 state
19731980 event
@@ -2042,17 +2049,20 @@ async def _persist_auth_tree(
20422049 events_to_context [e .event_id ].rejected = RejectedReason .AUTH_ERROR
20432050
20442051 await self .persist_events_and_notify (
2052+ room_id ,
20452053 [
20462054 (e , events_to_context [e .event_id ])
20472055 for e in itertools .chain (auth_events , state )
2048- ]
2056+ ],
20492057 )
20502058
20512059 new_event_context = await self .state_handler .compute_event_context (
20522060 event , old_state = state
20532061 )
20542062
2055- return await self .persist_events_and_notify ([(event , new_event_context )])
2063+ return await self .persist_events_and_notify (
2064+ room_id , [(event , new_event_context )]
2065+ )
20562066
20572067 async def _prep_event (
20582068 self ,
@@ -2903,21 +2913,27 @@ async def _check_key_revocation(self, public_key, url):
29032913
29042914 async def persist_events_and_notify (
29052915 self ,
2916+ room_id : str ,
29062917 event_and_contexts : Sequence [Tuple [EventBase , EventContext ]],
29072918 backfilled : bool = False ,
29082919 ) -> int :
29092920 """Persists events and tells the notifier/pushers about them, if
29102921 necessary.
29112922
29122923 Args:
2913- event_and_contexts:
2924+ room_id: The room ID of events being persisted.
2925+ event_and_contexts: Sequence of events with their associated
2926+ context that should be persisted. All events must belong to
2927+ the same room.
29142928 backfilled: Whether these events are a result of
29152929 backfilling or not
29162930 """
2917- if self .config .worker .writers .events != self ._instance_name :
2931+ instance = self .config .worker .events_shard_config .get_instance (room_id )
2932+ if instance != self ._instance_name :
29182933 result = await self ._send_events (
2919- instance_name = self . config . worker . writers . events ,
2934+ instance_name = instance ,
29202935 store = self .store ,
2936+ room_id = room_id ,
29212937 event_and_contexts = event_and_contexts ,
29222938 backfilled = backfilled ,
29232939 )
0 commit comments