-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add experimental support for sharding event persister. #8170
Changes from all commits
67bfbb6
164450e
25e78f7
ac494a8
da98a2b
d54d6e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add experimental support for sharding event persister. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -923,7 +923,8 @@ async def backfill(self, dest, room_id, limit, extremities): | |
) | ||
) | ||
|
||
await self._handle_new_events(dest, ev_infos, backfilled=True) | ||
if ev_infos: | ||
await self._handle_new_events(dest, room_id, ev_infos, backfilled=True) | ||
|
||
# Step 2: Persist the rest of the events in the chunk one by one | ||
events.sort(key=lambda e: e.depth) | ||
|
@@ -1216,7 +1217,7 @@ async def get_event(event_id: str): | |
event_infos.append(_NewEventInfo(event, None, auth)) | ||
|
||
await self._handle_new_events( | ||
destination, event_infos, | ||
destination, room_id, event_infos, | ||
) | ||
|
||
def _sanity_check_event(self, ev): | ||
|
@@ -1363,15 +1364,15 @@ async def do_invite_join( | |
) | ||
|
||
max_stream_id = await self._persist_auth_tree( | ||
origin, auth_chain, state, event, room_version_obj | ||
origin, room_id, auth_chain, state, event, room_version_obj | ||
) | ||
|
||
# We wait here until this instance has seen the events come down | ||
# replication (if we're using replication) as the below uses caches. | ||
# | ||
# TODO: Currently the events stream is written to from master | ||
await self._replication.wait_for_stream_position( | ||
self.config.worker.writers.events, "events", max_stream_id | ||
self.config.worker.events_shard_config.get_instance(room_id), | ||
"events", | ||
max_stream_id, | ||
) | ||
|
||
# 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( | |
) | ||
|
||
context = await self.state_handler.compute_event_context(event) | ||
await self.persist_events_and_notify([(event, context)]) | ||
await self.persist_events_and_notify(event.room_id, [(event, context)]) | ||
|
||
return event | ||
|
||
|
@@ -1652,7 +1653,9 @@ async def do_remotely_reject_invite( | |
await self.federation_client.send_leave(host_list, event) | ||
|
||
context = await self.state_handler.compute_event_context(event) | ||
stream_id = await self.persist_events_and_notify([(event, context)]) | ||
stream_id = await self.persist_events_and_notify( | ||
event.room_id, [(event, context)] | ||
) | ||
|
||
return event, stream_id | ||
|
||
|
@@ -1900,7 +1903,7 @@ async def _handle_new_event( | |
) | ||
|
||
await self.persist_events_and_notify( | ||
[(event, context)], backfilled=backfilled | ||
event.room_id, [(event, context)], backfilled=backfilled | ||
) | ||
except Exception: | ||
run_in_background( | ||
|
@@ -1913,6 +1916,7 @@ async def _handle_new_event( | |
async def _handle_new_events( | ||
self, | ||
origin: str, | ||
room_id: str, | ||
event_infos: Iterable[_NewEventInfo], | ||
backfilled: bool = False, | ||
) -> None: | ||
|
@@ -1944,6 +1948,7 @@ async def prep(ev_info: _NewEventInfo): | |
) | ||
|
||
await self.persist_events_and_notify( | ||
room_id, | ||
[ | ||
(ev_info.event, context) | ||
for ev_info, context in zip(event_infos, contexts) | ||
|
@@ -1954,6 +1959,7 @@ async def prep(ev_info: _NewEventInfo): | |
async def _persist_auth_tree( | ||
self, | ||
origin: str, | ||
room_id: str, | ||
auth_events: List[EventBase], | ||
state: List[EventBase], | ||
event: EventBase, | ||
|
@@ -1968,6 +1974,7 @@ async def _persist_auth_tree( | |
|
||
Args: | ||
origin: Where the events came from | ||
room_id, | ||
auth_events | ||
state | ||
event | ||
|
@@ -2042,17 +2049,20 @@ async def _persist_auth_tree( | |
events_to_context[e.event_id].rejected = RejectedReason.AUTH_ERROR | ||
|
||
await self.persist_events_and_notify( | ||
room_id, | ||
[ | ||
(e, events_to_context[e.event_id]) | ||
for e in itertools.chain(auth_events, state) | ||
] | ||
], | ||
) | ||
|
||
new_event_context = await self.state_handler.compute_event_context( | ||
event, old_state=state | ||
) | ||
|
||
return await self.persist_events_and_notify([(event, new_event_context)]) | ||
return await self.persist_events_and_notify( | ||
room_id, [(event, new_event_context)] | ||
) | ||
|
||
async def _prep_event( | ||
self, | ||
|
@@ -2903,21 +2913,27 @@ async def _check_key_revocation(self, public_key, url): | |
|
||
async def persist_events_and_notify( | ||
self, | ||
room_id: str, | ||
event_and_contexts: Sequence[Tuple[EventBase, EventContext]], | ||
backfilled: bool = False, | ||
) -> int: | ||
"""Persists events and tells the notifier/pushers about them, if | ||
necessary. | ||
|
||
Args: | ||
event_and_contexts: | ||
room_id: The room ID of events being persisted. | ||
event_and_contexts: Sequence of events with their associated | ||
context that should be persisted. All events must belong to | ||
the same room. | ||
backfilled: Whether these events are a result of | ||
backfilling or not | ||
""" | ||
if self.config.worker.writers.events != self._instance_name: | ||
instance = self.config.worker.events_shard_config.get_instance(room_id) | ||
if instance != self._instance_name: | ||
Comment on lines
+2931
to
+2932
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. Edit: I wrote the below before realizing that
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. nods. The problem with giving 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. Actually, I think we do need to use 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. Though I guess the fact that we go on to do a HTTP replication hit means that 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 it is OK to use |
||
result = await self._send_events( | ||
instance_name=self.config.worker.writers.events, | ||
instance_name=instance, | ||
store=self.store, | ||
room_id=room_id, | ||
event_and_contexts=event_and_contexts, | ||
backfilled=backfilled, | ||
) | ||
|
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 if-statement just an optimization?
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.
Oh err, somewhere along the lines I think something got very unhappy about empty lists. I forget the details now or if its even necessary 😕