From 0fbc1aac6c11adb4abf8ef4fe3ce9538cca95ca5 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 29 Dec 2022 15:20:57 +0000 Subject: [PATCH 1/4] Don't block for full state to cache hosts in room when sending events --- synapse/handlers/message.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 88fc51a4c97e..2141a890fc2f 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -1531,13 +1531,19 @@ async def cache_joined_hosts_for_events( external federation senders don't have to recalculate it themselves. """ - for event, _ in events_and_context: + for event, event_context in events_and_context: if not self._external_cache.is_enabled(): return # If external cache is enabled we should always have this. assert self._external_cache_joined_hosts_updates is not None + if event_context.partial_state: + # We can't precalculate joined hosts for a partial-state event, + # (at least not without blocking until full state). + # So skip this calculation entirely. + continue + # We actually store two mappings, event ID -> prev state group, # state group -> joined hosts, which is much more space efficient # than event ID -> joined hosts. From 4ca8e454ea13ba147d09ec7081d4c001d1d5d68c Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 29 Dec 2022 15:28:32 +0000 Subject: [PATCH 2/4] Newsfile Signed-off-by: Olivier Wilkinson (reivilibre) --- changelog.d/14749.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/14749.misc diff --git a/changelog.d/14749.misc b/changelog.d/14749.misc new file mode 100644 index 000000000000..ff813252250d --- /dev/null +++ b/changelog.d/14749.misc @@ -0,0 +1 @@ +Faster remote room joins (worker mode): do not populate external hosts-in-room cache when sending events as this requires blocking for full state. \ No newline at end of file From a9f3e285d0f7567a8c482eabdc8e701ad79deb2d Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Thu, 29 Dec 2022 15:30:53 +0000 Subject: [PATCH 3/4] Drive-by hoisting --- synapse/handlers/message.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 2141a890fc2f..a9dc978d4895 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -1531,13 +1531,13 @@ async def cache_joined_hosts_for_events( external federation senders don't have to recalculate it themselves. """ - for event, event_context in events_and_context: - if not self._external_cache.is_enabled(): - return + if not self._external_cache.is_enabled(): + return - # If external cache is enabled we should always have this. - assert self._external_cache_joined_hosts_updates is not None + # If external cache is enabled we should always have this. + assert self._external_cache_joined_hosts_updates is not None + for event, event_context in events_and_context: if event_context.partial_state: # We can't precalculate joined hosts for a partial-state event, # (at least not without blocking until full state). From 1e4e430892841373c8a9f8081b9919279210c2d6 Mon Sep 17 00:00:00 2001 From: Sean Quah Date: Tue, 10 Jan 2023 19:42:56 +0000 Subject: [PATCH 4/4] fixup: reword comment --- synapse/handlers/message.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index a9dc978d4895..3278a695ed61 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -1539,9 +1539,14 @@ async def cache_joined_hosts_for_events( for event, event_context in events_and_context: if event_context.partial_state: - # We can't precalculate joined hosts for a partial-state event, - # (at least not without blocking until full state). - # So skip this calculation entirely. + # To populate the cache for a partial-state event, we either have to + # block until full state, which the code below does, or change the + # meaning of cache values to be the list of hosts to which we plan to + # send events and calculate that instead. + # + # The federation senders don't use the external cache when sending + # events in partial-state rooms anyway, so let's not bother populating + # the cache. continue # We actually store two mappings, event ID -> prev state group,