gortiz opened a new pull request, #20361:
URL: https://github.com/apache/pinot/pull/20361
## Summary
This PR closes the forward index reader contexts of a query on every exit
path, not only at EOF. Before this change, a query that timed out, was
cancelled, failed, or stopped early left its direct buffers to the garbage
collector.
The change adds a per-thread resource registry to `QueryThreadContext`. The
reader contexts register in it when they are created.
`QueryThreadContext.close()` closes the contexts that are still open when the
query task ends.
## Background
A compressed raw (no-dictionary) forward index allocates a direct
`ByteBuffer` for each reader context (`ChunkReaderContext`, and the V4 and V7
contexts). The query path creates one context per column, per segment, per
query, in three places:
- `DataFetcher.ColumnValueReader` (projection)
- `SVScanDocIdIterator` and `MVScanDocIdIterator` (scan-based filters)
These owners close the context only when they read to the end.
`ProjectionOperator` closes its `DataBlockCache` only when `nextBlock()`
returns `null`. Nothing closes the context on these paths:
- a query timeout
- a cancellation (for example, by the resource accountant)
- an exception in an operator
- an early termination, for example `SelectionOnlyOperator` when the `LIMIT`
is satisfied, or a combine operator that stops when the query is satisfied
On these paths, the buffer is freed only when a GC cycle runs its `Cleaner`.
A direct buffer adds almost nothing to heap occupancy. So it does not start a
GC cycle itself, and the release can take hours or days. A server that
repeatedly times out queries on compressed raw columns can fill
`MaxDirectMemorySize` with dead buffers. The OS page cache then shrinks, and
the server slows down until a GC cycle occurs. Uncompressed (`PASS_THROUGH`)
chunk readers allocate no buffer, so they are not affected.
## How
```
combine worker task (contextAwareExecutorService)
QueryThreadContext.open()
segment 1: DataFetcher creates context A -> registerResource(A)
reads to EOF, closes A -> unregisterResource(A)
segment 2: scan iterator creates context B -> registerResource(B)
query times out / LIMIT reached / exception
QueryThreadContext.close() -> closes B
```
- `QueryThreadContext` gets `registerResource(AutoCloseable)` and
`unregisterResource(AutoCloseable)`. `close()` closes every resource that is
still registered. A failure in one resource does not stop the close of the
others.
- The three owners register the context when they create it. When they close
it on the normal path, they unregister it first. So the registry holds only the
contexts of the segments that the task did not finish.
- Each combine worker task opens and closes its own `QueryThreadContext`
through `QueryThreadContext.contextAwareExecutorService`. As a result, a
context is closed on the thread that created and used it, after the task of
that thread ends. No other thread can still read the buffer. A close from
another thread can free memory that a worker still reads, and crash the JVM.
This design prevents that.
- The operator tree does not need a `close()` hook, and the combine
operators do not change.
- Without a `QueryThreadContext` on the thread, registration does nothing,
and the behavior stays as before.
## Compatibility
No configuration, wire format, or segment format changes. The new methods on
`QueryThreadContext` are additive.
## Tests
- `QueryThreadContextTest`: the registry closes the registered resources,
skips the unregistered ones, continues after a failed close, accepts a resource
that unregisters itself in `close()`, and does not carry resources into the
next context.
- `ReaderContextReleaseTest`: a `DataFetcher`, an `SVScanDocIdIterator` and
an `MVScanDocIdIterator` that stop before EOF have their context closed when
the `QueryThreadContext` closes. A context that the owner closes at EOF is
closed exactly one time.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]