Skip to content

Conversation

anoadragon453
Copy link
Member

@anoadragon453 anoadragon453 commented Sep 17, 2025

Hopefully fixes #18925.

Carries out a number of optimisations to this bit of the codebase (note that on element.io we have about 7mil state deltas to process):

  • Replace a get_event DB query per state delta with a single, smaller query for all state deltas.
  • Bail out early if there are no delayed events in the DB to update.
  • Set the next "scheduled at" time to the minimum of all delayed events once, instead of doing so for every delta. this ended up causing tests to fail, and has been reverted.

Highly recommended to review commit-by-commit.

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)

This is preferable to pulling out entire events and only fetching the `sender` field.
We also only call it once for all events in the batch, instead of per state delta.
There's no need to do this for every event in the batch. Just find the min time for all delayed events.
There are zero delayed events on matrix.org's database. We can save a
lot of time pulling out state deltas by first just checking if there are
any delayed events to actually deal with.
@anoadragon453 anoadragon453 marked this pull request as ready for review September 17, 2025 12:42
@anoadragon453 anoadragon453 requested a review from a team as a code owner September 17, 2025 12:42
@anoadragon453 anoadragon453 changed the title Add a storage function to pull out senders for event _ids Fix performance regression related to delayed events processing Sep 17, 2025
@anoadragon453
Copy link
Member Author

Initial theory is that the delayed_state_events_are_sent_on_timeout Complement failure is related to 9f1c7e3.

Copy link
Contributor

@MadLittleMods MadLittleMods left a comment

Choose a reason for hiding this comment

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

I haven't look at whether this is a good solution holistically with the delayed events feature. But the change itself looks like an improvement to what was there before.

@anoadragon453 anoadragon453 force-pushed the anoa/delayed_event_perf_regression branch from 13fd4bd to 4ff8184 Compare September 18, 2025 10:46
This reverts commit 9f1c7e3.

This change caused delayed events to not be scheduled, thus in turn
causing the `TestDelayedEvents/delayed_.*_events_are_sent_on_timeout`
tests to fail.

I don't fully understand why my change didn't work, but have spent
enough time looking at the code. The optimisation was very slight
anyhow, so I don't think it's worth persuing for longer.
@anoadragon453 anoadragon453 force-pushed the anoa/delayed_event_perf_regression branch from 1500d26 to 1eb25f1 Compare September 18, 2025 14:14
Apply the same fix to a similar loop in the presence handling code.
If a room has been purged, then the associated event would be gone as
well. Thus we wouldn't be able to pull out the sender.

This is different from the `sender` field being null on an event.
Comment on lines 263 to 270
if sender_str is sentinel:
# This can happen if a room is purged. State deltas related to
# the room are left behind, but the event/room no longer exist.
logger.warning(
"Skipping state delta with event ID '%s' as it is an unknown event - the room may have been purged",
delta.event_id,
)
continue
Copy link
Member Author

Choose a reason for hiding this comment

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

This was added after an exception was found when running an earlier draft of this PR on element.io.

@anoadragon453
Copy link
Member Author

CI failures are due to flakes.

@anoadragon453 anoadragon453 merged commit ddc7627 into develop Sep 23, 2025
42 of 44 checks passed
@anoadragon453 anoadragon453 deleted the anoa/delayed_event_perf_regression branch September 23, 2025 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

High CPU and DB usage upon start on Synapse v1.138.0

2 participants