yashmayya opened a new pull request, #20379: URL: https://github.com/apache/pinot/pull/20379
## Problem On a POC cluster, idle servers kept the leaf-stage operators of finished multi-stage queries in heap. 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. - On the POC cluster, an `OpChainConverter` plugin started it. The plugin receives the data of an exchange natively, but it also builds the Java receive operator for explain and stats. That operator creates Java mailboxes and registers its thread context on them, and nothing reads them. Java-only queries get the same chain after errors and cancels. ## 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 #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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
