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

Commit

Permalink
Move get_state methods into FederationHandler (#6503)
Browse files Browse the repository at this point in the history
* commit 'be294d6fd':
  Move get_state methods into FederationHandler (#6503)
  • Loading branch information
anoadragon453 committed Mar 19, 2020
2 parents 302af3d + be294d6 commit 79c0a43
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 170 deletions.
71 changes: 2 additions & 69 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from synapse.federation.federation_base import FederationBase, event_from_pdu_json
from synapse.logging.context import make_deferred_yieldable
from synapse.logging.utils import log_function
from synapse.util import batch_iter, unwrapFirstError
from synapse.util import unwrapFirstError
from synapse.util.caches.expiringcache import ExpiringCache
from synapse.util.retryutils import NotRetryingDestination

Expand Down Expand Up @@ -327,74 +327,7 @@ def get_room_state_ids(self, destination: str, room_id: str, event_id: str):
):
raise Exception("invalid response from /state_ids")

desired_events = set(state_event_ids + auth_event_ids)
event_map = yield self.get_events_from_store_or_dest(
destination, room_id, desired_events
)

failed_to_fetch = desired_events - event_map.keys()
if failed_to_fetch:
logger.warning(
"Failed to fetch missing state/auth events for %s: %s",
room_id,
failed_to_fetch,
)

pdus = [event_map[e_id] for e_id in state_event_ids if e_id in event_map]
auth_chain = [event_map[e_id] for e_id in auth_event_ids if e_id in event_map]

auth_chain.sort(key=lambda e: e.depth)

return pdus, auth_chain

@defer.inlineCallbacks
def get_events_from_store_or_dest(self, destination, room_id, event_ids):
"""Fetch events from a remote destination, checking if we already have them.
Args:
destination (str)
room_id (str)
event_ids (Iterable[str])
Returns:
Deferred[dict[str, EventBase]]: A deferred resolving to a map
from event_id to event
"""
fetched_events = yield self.store.get_events(event_ids, allow_rejected=True)

missing_events = set(event_ids) - fetched_events.keys()

if not missing_events:
return fetched_events

logger.debug(
"Fetching unknown state/auth events %s for room %s",
missing_events,
event_ids,
)

room_version = yield self.store.get_room_version(room_id)

# XXX 20 requests at once? really?
for batch in batch_iter(missing_events, 20):
deferreds = [
run_in_background(
self.get_pdu,
destinations=[destination],
event_id=e_id,
room_version=room_version,
)
for e_id in batch
]

res = yield make_deferred_yieldable(
defer.DeferredList(deferreds, consumeErrors=True)
)
for success, result in res:
if success and result:
fetched_events[result.event_id] = result

return fetched_events
return state_event_ids, auth_event_ids

@defer.inlineCallbacks
@log_function
Expand Down
Loading

0 comments on commit 79c0a43

Please sign in to comment.