WIP: feat: Per-room PDU queue workers #2539

Draft
nex wants to merge 8 commits from nex/feat/per-room-txn-queue into main
Owner

Important

This pull request is a draft. It is not yet suitable for deployment. It may never be suitable for deployment. This pull request is being opened provisionally for early & iterative design feedback.

This pull request introduces a new "per-room queue worker" system that allows for handling incoming events in parallel in the background. This has advantages over the current system of handling entire transactions in the background namely: one PDU taking a long time to handle does not block entire transactions from completing.

Every time a new transaction is received, it is split up in work per-room. Once the transaction's inputs have been divided and sorted, the server will now add a "job" to a master queue in the rooms queue service. From there, the master queue will pick up a job, and will try to discover a room worker channel. If there is an existing one, the job is forwarded to that channel. If there is not an existing one, a new channel is created, the job forwarded to the channel, and a "room worker" future is spawned.

The room worker, once spawned, will wait for a job from the channel earlier. If the server starts shutting down before it can get a job, it returns. If the worker does not get a job within 5 minutes, it reaps itself (i.e. tells the master worker to remove it from the worker map). Upon receiving a job, the worker will lock the room, handle the incoming PDU, and report metrics once it finishes. It then loops. This process repeats until the server shuts down, or the worker self-reaps.

Fixes: #2430 (by making long-running PDU handles a non-issue)
Fixes: #2644 (by making room handling a parallel, asynchronous operation)

Pull request checklist:

  • This pull request targets the main branch, and the branch is named something other than
    main.
  • I have written an appropriate pull request title and my description is clear.
  • I understand I am responsible for the contents of this pull request.
  • I have followed the contributing guidelines:
>[!IMPORTANT] > This pull request is a **draft**. It is not yet suitable for deployment. It may never be suitable for deployment. This pull request is being opened provisionally for early & iterative design feedback. This pull request introduces a new "per-room queue worker" system that allows for handling incoming events in parallel in the background. This has advantages over the current system of handling entire transactions in the background namely: one PDU taking a long time to handle does not block entire transactions from completing. Every time a new transaction is received, it is split up in work per-room. Once the transaction's inputs have been divided and sorted, the server will now add a "job" to a master queue in the rooms queue service. From there, the master queue will pick up a job, and will try to discover a room worker channel. If there is an existing one, the job is forwarded to that channel. If there is not an existing one, a new channel is created, the job forwarded to the channel, and a "room worker" future is spawned. The room worker, once spawned, will wait for a job from the channel earlier. If the server starts shutting down before it can get a job, it returns. If the worker does not get a job within 5 minutes, it reaps itself (i.e. tells the master worker to remove it from the worker map). Upon receiving a job, the worker will lock the room, handle the incoming PDU, and report metrics once it finishes. It then loops. This process repeats until the server shuts down, or the worker self-reaps. Fixes: #2430 (by making long-running PDU handles a non-issue) Fixes: #2644 (by making room handling a parallel, asynchronous operation) **Pull request checklist:** <!-- You need to complete these before your PR can be considered. If you aren't sure about some, feel free to ask for clarification in #dev:continuwuity.org. --> - [x] This pull request targets the `main` branch, and the branch is named something other than `main`. - [x] I have written an appropriate pull request title and my description is clear. - [x] I understand I am responsible for the contents of this pull request. - I have followed the [contributing guidelines][c1]: - [x] My contribution follows the [code style][c2], if applicable. - [x] I ran [pre-commit checks][c1pc] before opening/drafting this pull request. - [x] I have [tested my contribution][c1t] (or proof-read it for documentation-only changes) myself, if applicable. This includes ensuring code compiles. - [x] My commit messages follow the [commit message format][c1cm] and are descriptive. <!-- Notes on these requirements: - While not required, we encourage you to sign your commits with GPG or SSH to attest the authenticity of your changes. - While we allow LLM-assisted contributions, we do not appreciate contributions that are low quality, which is typical of machine-generated contributions that have not had a lot of love and care from a human. Please do not open a PR if all you have done is asked ChatGPT to tidy up the codebase with a +-100,000 diff. - In the case of code style violations, reviewers may leave review comments/change requests indicating what the ideal change would look like. For example, a reviewer may suggest you lower a log level, or use `match` instead of `if/else` etc. - In the case of code style violations, pre-commit check failures, minor things like typos/spelling errors, and in some cases commit format violations, reviewers may modify your branch directly, typically by making changes and adding a commit. Particularly in the latter case, a reviewer may rebase your commits to squash "spammy" ones (like "fix", "fix", "actually fix"), and reword commit messages that don't satisfy the format. - Pull requests MUST pass the `Checks` CI workflows to be capable of being merged. This can only be bypassed in exceptional circumstances. If your CI flakes, let us know in matrix:r/dev:continuwuity.org. - Pull requests have to be based on the latest `main` commit before being merged. If the main branch changes while you're making your changes, you should make sure you rebase on main before opening a PR. Your branch will be rebased on main before it is merged if it has fallen behind. - We typically only do fast-forward merges, so your entire commit log will be included. Once in main, it's difficult to get out cleanly, so put on your best dress, smile for the cameras! --> [c1]: https://forgejo.ellis.link/continuwuation/continuwuity/src/branch/main/CONTRIBUTING.md [c2]: https://forgejo.ellis.link/continuwuation/continuwuity/src/branch/main/docs/development/code_style.mdx [c1pc]: https://forgejo.ellis.link/continuwuation/continuwuity/src/branch/main/CONTRIBUTING.md#pre-commit-checks [c1t]: https://forgejo.ellis.link/continuwuation/continuwuity/src/branch/main/CONTRIBUTING.md#running-tests-locally [c1cm]: https://forgejo.ellis.link/continuwuation/continuwuity/src/branch/main/CONTRIBUTING.md#commit-messages
nex self-assigned this 2026-11-16 00:44:39 +00:00
feat: Improve tracing
Some checks failed
Auto Labeler / Apply labels based on changed files (pull_request_target) Successful in 35s
Documentation / Build and Deploy Documentation (pull_request) Successful in 2m15s
Checks / Prek / Pre-commit & Formatting (pull_request) Successful in 2m54s
Checks / Prek / Check changed files (pull_request) Successful in 10s
Checks / Changelog / Check changelog is added (pull_request_target) Failing after 13s
Checks / Prek / Clippy and Cargo Tests (pull_request) Successful in 10m24s
2842aa6d23
@ -0,0 +96,4 @@
| Entry::Vacant(entry) => {
let room_id = entry.key().to_owned();
let (sender, receiver) = mpsc::unbounded_channel();
sender.send(job).unwrap(); // TODO: handle error
Author
Owner

TODO: Keeping jobs entirely in-memory means interrupting the workers (e.g. to shut down) causes them to be lost. Since we inherently already told other servers "I processed this already I promise", we won't receive that job again, which means entire transactions get lost to the aether if the room worker channels aren't empty.

This probably needs a database backing before committing it into the queue, and a startup task to resume any partially complete queues.

TODO: Keeping jobs entirely in-memory means interrupting the workers (e.g. to shut down) causes them to be lost. Since we inherently already told other servers "I processed this already I promise", we won't receive that job again, which means entire transactions get lost to the aether if the room worker channels aren't empty. This probably needs a database backing before committing it into the queue, and a startup task to resume any partially complete queues.
Author
Owner

Now backed by the database in c9a3f9f907. It's incredibly IO heavy during federation peaks though, I wonder if that can be improved.

Now backed by the database in c9a3f9f907418063e9c7c17bc671be5610a67998. It's incredibly IO heavy during federation peaks though, I wonder if that can be improved.
@ -0,0 +95,4 @@
},
| Entry::Vacant(entry) => {
let room_id = entry.key().to_owned();
let (sender, receiver) = mpsc::unbounded_channel();
Author
Owner

ok so preliminary testing has shown that federation catchup can cause room workers to gather over 1000 jobs, and this has not so great implications for memory usage: image

There will need to

  1. be a bound on the buffer size a room worker is willing to have
  2. a way to signal to the new_job function that the worker is too busy, allowing the incoming transaction to be blocked until there's free space. Although, this just delays the problem this PR is trying to solve in the first place.

Perhaps dropping the master worker and simply doing all of this spawner logic in new_job (or a subroutine) would be a smarter idea, but the new job func will need linearising to prevent concurrent spawns of room workers.

ok so preliminary testing has shown that federation catchup can cause room workers to gather over 1000 jobs, and this has not so great implications for memory usage: ![image](/attachments/4f376947-becf-4154-95a2-6211dc0b2981) There will need to 1. be a bound on the buffer size a room worker is willing to have 2. a way to signal to the `new_job` function that the worker is too busy, allowing the incoming transaction to be blocked until there's free space. Although, this just delays the problem this PR is trying to solve in the first place. Perhaps dropping the master worker and simply doing all of this spawner logic in `new_job` (or a subroutine) would be a smarter idea, but the new job func will need linearising to prevent concurrent spawns of room workers.
nex added this to the 27.1.0 milestone 2026-11-19 18:46:46 +00:00
nex force-pushed nex/feat/per-room-txn-queue from 2842aa6d23
Some checks failed
Auto Labeler / Apply labels based on changed files (pull_request_target) Successful in 35s
Documentation / Build and Deploy Documentation (pull_request) Successful in 2m15s
Checks / Prek / Pre-commit & Formatting (pull_request) Successful in 2m54s
Checks / Prek / Check changed files (pull_request) Successful in 10s
Checks / Changelog / Check changelog is added (pull_request_target) Failing after 13s
Checks / Prek / Clippy and Cargo Tests (pull_request) Successful in 10m24s
to fbc7ffff5e
Some checks failed
Checks / Changelog / Check changelog is added (pull_request_target) Failing after 7s
Checks / Prek / Check changed files (pull_request) Successful in 9s
Documentation / Build and Deploy Documentation (pull_request) Successful in 1m6s
Update flake hashes / update-flake-hashes (pull_request) Successful in 2m15s
Checks / Prek / Pre-commit & Formatting (pull_request) Successful in 4m16s
Checks / Prek / Clippy and Cargo Tests (pull_request) Successful in 8m51s
2026-12-01 16:47:04 +00:00
Compare
feat: Persist queue in database & resume on startup
Some checks failed
Checks / Prek / Check changed files (pull_request) Successful in 4s
Checks / Changelog / Check changelog is added (pull_request_target) Successful in 7s
Checks / Prek / Pre-commit & Formatting (pull_request) Successful in 44s
Documentation / Build and Deploy Documentation (pull_request) Successful in 1m7s
Checks / Prek / Clippy and Cargo Tests (pull_request) Has been cancelled
Update flake hashes / update-flake-hashes (pull_request) Has been cancelled
c9a3f9f907
fix: Actually add txn_jobid_job db map
All checks were successful
Checks / Prek / Check changed files (pull_request) Successful in 4s
Checks / Changelog / Check changelog is added (pull_request_target) Successful in 9s
Update flake hashes / update-flake-hashes (pull_request) Successful in 39s
Checks / Prek / Pre-commit & Formatting (pull_request) Successful in 1m26s
Documentation / Build and Deploy Documentation (pull_request) Successful in 1m36s
Checks / Prek / Clippy and Cargo Tests (pull_request) Successful in 3m47s
a2e9c1c964
feat: Log how many backlogged jobs will be run on worker start
All checks were successful
Checks / Prek / Check changed files (pull_request) Successful in 5s
Checks / Changelog / Check changelog is added (pull_request_target) Successful in 7s
Checks / Prek / Pre-commit & Formatting (pull_request) Successful in 42s
Documentation / Build and Deploy Documentation (pull_request) Successful in 1m6s
Update flake hashes / update-flake-hashes (pull_request) Successful in 1m27s
Checks / Prek / Clippy and Cargo Tests (pull_request) Successful in 3m15s
f28c1cef93
All checks were successful
Checks / Prek / Check changed files (pull_request) Successful in 5s
Required
Details
Checks / Changelog / Check changelog is added (pull_request_target) Successful in 7s
Required
Details
Checks / Prek / Pre-commit & Formatting (pull_request) Successful in 42s
Required
Details
Documentation / Build and Deploy Documentation (pull_request) Successful in 1m6s
Update flake hashes / update-flake-hashes (pull_request) Successful in 1m27s
Checks / Prek / Clippy and Cargo Tests (pull_request) Successful in 3m15s
Required
Details
This pull request is marked as a work in progress.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'nex/feat/per-room-txn-queue:nex/feat/per-room-txn-queue'
git switch 'nex/feat/per-room-txn-queue'
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
continuwuation/continuwuity!2539
No description provided.