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

Send federation events concurrently #3078

Merged
merged 6 commits into from
Apr 10, 2018
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions synapse/federation/transaction_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,38 @@ def _process_event_queue_loop(self):
while True:
last_token = yield self.store.get_federation_out_pos("events")
next_token, events = yield self.store.get_all_new_events_stream(
last_token, self._last_poked_id, limit=20,
last_token, self._last_poked_id, limit=100,
)

logger.debug("Handling %s -> %s", last_token, next_token)

if not events and next_token >= self._last_poked_id:
break

for event in events:
@defer.inlineCallbacks
def handle_event(event):
# Only send events for this server.
send_on_behalf_of = event.internal_metadata.get_send_on_behalf_of()
is_mine = self.is_mine_id(event.event_id)
if not is_mine and send_on_behalf_of is None:
continue

# Get the state from before the event.
# We need to make sure that this is the state from before
# the event and not from after it.
# Otherwise if the last member on a server in a room is
# banned then it won't receive the event because it won't
# be in the room after the ban.
destinations = yield self.state.get_current_hosts_in_room(
event.room_id, latest_event_ids=[
prev_id for prev_id, _ in event.prev_events
],
)
return

try:
# Get the state from before the event.
# We need to make sure that this is the state from before
# the event and not from after it.
# Otherwise if the last member on a server in a room is
# banned then it won't receive the event because it won't
# be in the room after the ban.
destinations = yield self.state.get_current_hosts_in_room(
event.room_id, latest_event_ids=[
prev_id for prev_id, _ in event.prev_events
],
)
except Exception:
logger.exception("Failed to calculate hosts in room")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would this happen?

if we're going to do this, surely we should at least log the event id?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't happen really, but it empirically seems to have on matrix.org in the past so is probably worth keeping for paranoia's sake.

return

destinations = set(destinations)

if send_on_behalf_of is not None:
Expand All @@ -207,6 +213,20 @@ def _process_event_queue_loop(self):

self._send_pdu(event, destinations)

@defer.inlineCallbacks
def handle_room_events(events):
for event in events:
yield handle_event(event)

events_by_room = {}
for event in events:
events_by_room.setdefault(event.room_id, []).append(event)

yield logcontext.make_deferred_yieldable(defer.gatherResults(
[handle_room_events(evs) for evs in events_by_room.itervalues()],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like it needs a preserve_fn

consumeErrors=True
))

events_processed_counter.inc_by(len(events))

yield self.store.update_federation_out_pos(
Expand Down