Skip to content

Stop leftover MSE mailboxes and finished tasks from keeping queries in memory - #20379

Open
yashmayya wants to merge 1 commit into
apache:masterfrom
yashmayya:mse-mailbox-context-leak
Open

yashmayya wants to merge 1 commit into
apache:masterfrom
yashmayya:mse-mailbox-context-leak

Conversation

@yashmayya

@yashmayya yashmayya commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Idle servers can keep the leaf-stage operators of finished multi-stage queries in heap. In one heap dump taken after a full GC, 12.2 GB was live, and about 11 GB of it was the per-segment operator trees of the last 3 queries. Every segment had its own AggregationOperator, ProjectionOperator, filter operators, transform functions and DataFetcher, with their buffers. The next queries then went over the heap limits of the resource accountant and were killed.

Root cause

The heap dump shows this reference chain from the GC root:

  1. MailboxService._receivingMailboxCache holds a ReceivingMailbox.
  2. The mailbox holds _receiveOperatorThreadContext, the QueryThreadContext of the receive operator.
  3. The thread context holds the QueryExecutionContext. All the stages of the query on the server share it.
  4. QueryExecutionContext._tasks holds every Future that the query submitted through QueryThreadContext.contextAwareExecutorService(). Only terminate() clears it. A done FutureTask keeps its result.
  5. CombinePlanNode builds the per-segment operators in parallel with QueryMultiThreadingUtils.runTasksWithDeadline(). Each task returns a List<Operator>. So _tasks keeps every per-segment operator of the leaf stage.

Why the mailboxes stayed in the cache:

  • A receive operator releases a mailbox only when it reads a successful EOS from it. After an error, a cancel, or a close without a read, the mailbox stays in the cache. This is on purpose: a sender that still runs must find the cancelled mailbox, and not create a new one that nobody reads.
  • The cache expires a mailbox 300 s after the last access. But Guava removes expired entries only during later reads and writes of the cache. With no more multi-stage queries, the mailboxes stayed for hours.
  • So the chain starts whenever a receive operator stops before it reads every mailbox to the end: after an error or a cancel, or when a receive operator is built but never read (for example, when the op chain fails before it runs).

Fix

  • QueryExecutionContext keeps only the tasks that are not done. A task submitted through contextAwareExecutorService() removes itself when it is done. The op chain task of OpChainSchedulerService does the same.
  • When a receive operator will not read a mailbox anymore (after an error, a cancel or a close), the mailbox drops its reference to the QueryThreadContext. If the sender of the mailbox has finished, the operator also releases the mailbox from the cache. A mailbox whose sender still runs stays in the cache, as before (see Stop a lingering receiving mailbox from pinning the receive operator #19465), but it no longer keeps the query.

No new configs or metrics.

Notes:

  • contextAwareExecutorService().submit() now builds the FutureTask itself and gives it to execute() of the wrapped executor. A task that is submitted after the query is terminated is cancelled and not given to the executor.
  • I did not add a periodic cleanUp() of the mailbox cache. The cache expires a mailbox 300 s after the last lookup, not the last read. So a periodic clean-up would cancel the mailboxes of an exchange that runs longer than 300 s, also on quiet servers.

Tests

  • QueryExecutionContextTest (new), QueryMultiThreadingUtilsTest, OpChainSchedulerServiceTest: the context does not keep done tasks, and terminate() still cancels the running ones.
  • MailboxReceiveOperatorTest: after close, the mailboxes drop the thread context. Mailboxes of finished senders are released, and mailboxes of running senders stay.
  • ReceivingMailboxTest: when the sender counts as finished.

The 5 retention tests fail without the fix.

@yashmayya yashmayya added multi-stage Related to the multi-stage query engine bugfix labels Nov 25, 2026
@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.59259% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.46%. Comparing base (8a9266e) to head (3e82aff).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...org/apache/pinot/spi/query/QueryThreadContext.java 85.18% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             master   #20379       +/-   ##
=============================================
+ Coverage     61.16%   71.46%   +10.30%     
- Complexity        1     1639     +1638     
=============================================
  Files          2789     3611      +822     
  Lines        180164   244203    +64039     
  Branches      30156    39699     +9543     
=============================================
+ Hits         110198   174528    +64330     
+ Misses        60646    56512     -4134     
- Partials       9320    13163     +3843     
Flag Coverage Δ
integration 100.00% <ø> (?)
integration1 100.00% <ø> (?)
integration2 0.00% <ø> (?)
java-25 71.46% <92.59%> (+10.30%) ⬆️
lane-a 100.00% <ø> (?)
lane-b 0.00% <ø> (?)
temurin 71.46% <92.59%> (+10.30%) ⬆️
unittests 71.46% <92.59%> (+10.30%) ⬆️
unittests1 61.19% <90.74%> (+0.02%) ⬆️
unittests2 43.03% <38.88%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix multi-stage Related to the multi-stage query engine

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants