Skip to content

[ES|QL] Convert PackedValuesBlockHash.bytes to BreakingBytesRefBuilder for better memory tracking#140171

Merged
fang-xing-esql merged 12 commits intoelastic:mainfrom
fang-xing-esql:PackedValuesBlockHash-BreakingBytesRefBuilder
Jan 9, 2026
Merged

[ES|QL] Convert PackedValuesBlockHash.bytes to BreakingBytesRefBuilder for better memory tracking#140171
fang-xing-esql merged 12 commits intoelastic:mainfrom
fang-xing-esql:PackedValuesBlockHash-BreakingBytesRefBuilder

Conversation

@fang-xing-esql
Copy link
Member

It is found during investigating OOM triggered by new heap attack tests added by #139058. Converting PackedValuesBlockHash.bytes to BreakingBytesRefBuilder doesn't fix the OOM directly, however it does improve the memory tracking for queries that have long grouping keys.

@elasticsearchmachine
Copy link
Collaborator

Hi @fang-xing-esql, I've created a changelog YAML for you.

@fang-xing-esql fang-xing-esql changed the title [ES|QL] Converted PackedValuesBlockHash.bytes to BreakingBytesRefBuilder for better memory tracking [ES|QL] Convert PackedValuesBlockHash.bytes to BreakingBytesRefBuilder for better memory tracking Jan 6, 2026
@fang-xing-esql fang-xing-esql marked this pull request as ready for review January 7, 2026 14:35
@fang-xing-esql fang-xing-esql requested a review from nik9000 January 7, 2026 14:35
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Jan 7, 2026
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

// close bytesRefHash and bytes to prevent memory leaks in case of the initialization fails
close();
throw e;
}
Copy link
Member

Choose a reason for hiding this comment

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

This is valid and fine, but we usually write this as:

boolean success = false;
        try {
            this.bytesRefHash = HashImplFactory.newBytesRefHash(blockFactory);
            this.bytes = new BreakingBytesRefBuilder(blockFactory.breaker(), "PackedValuesBlockHash", this.nullTrackingBytes);
         } finally {
if (success == false) {
  close();
}

Mostly out of paranoia around eating the stack trace for e. This is just the more "normal" way of writing this for us.

* , which is reused for each grouping set will trigger CBE. CBE happens when adding around 25th group to bytes.
*/
public void testCircuitBreakerWithManyGroups() {
CircuitBreaker breaker = new MockBigArrays.LimitedBreaker(CircuitBreaker.REQUEST, ByteSizeValue.ofBytes(220000));
Copy link
Member

Choose a reason for hiding this comment

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

I think this test will fail even without the BreakingBytesRefBuilder. You could force it by passing a second CircuitBreaker into the ctor for PackedValuesBlockHash - the second one just for the bytes scratch. Then in prod code you'd just send the circuit breaker in twice. It's.... annoying. But it's nice and paranoid.

}

// For circuit breaker testing only {@code PackedValuesBlockHashCircuitBreakerTests}
PackedValuesBlockHash(List<GroupSpec> specs, BlockFactory blockFactory, CircuitBreaker circuitBreaker, int emitBatchSize) {
Copy link
Member

Choose a reason for hiding this comment

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

The primary ctor can call this one like this(specs, blockFactory, blockFactory.circiutBreaker(), emitBatchSize).

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! More refactor is needed. :)

@fang-xing-esql
Copy link
Member Author

Thanks for the review @nik9000 !

@fang-xing-esql fang-xing-esql merged commit 444c182 into elastic:main Jan 9, 2026
35 checks passed
jimczi pushed a commit to jimczi/elasticsearch that referenced this pull request Jan 12, 2026
…lder` for better memory tracking (elastic#140171)

* converted PackedValuesBlockHash.bytes to BreakingBytesRefBuilder
@fang-xing-esql fang-xing-esql deleted the PackedValuesBlockHash-BreakingBytesRefBuilder branch February 18, 2026 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >enhancement Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants