Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TopN Query Aggregator Memory Guardrails #17439

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jtuglu-netflix
Copy link
Contributor

@jtuglu-netflix jtuglu-netflix commented Oct 31, 2024

Adds a way to monitor aggregator map entry increases for TopN queries. This is in response to a problem we've seen in queries where a topN query is done with an expensive aggregator on a high-cardinality dimension. I've reproduced this problem locally, and confirmed that this fixes the issue in most cases. The other cases are outlined in Drawbacks.

Description

Approach

Use a configurable per (query, historical) fixed size byte amount that aggregators can take up. The byte count is maintained at a segment-level during each query runner's pass through a segment (getting result sequence). I opted for this instead of doing a %-of-heap based approach as in high-traffic scenarios, there could be multiple queries racing to allocate some memory for aggregators, and these could all read say, 5% of total available heap (let's say this is permissible % to allocate). If we're already at 80-90%, this could result poorly. Instead, using a fixed amount is a bit more cumbersome, but at least guarantees a realistic upper-bound on how much memory N concurrent queries could theoretically use. Changing to either approach (or another) is easy enough. I found the latter performed more consistently in local testing with artificially low heap sizes and with parallel queries. Another alternative I was thinking was a shared buffer that queries can "borrow" from for doing their queries, where this pool would be shared amongst all running queries. This is a bit like what GroupBy does.

Drawbacks

  • No spilling to disk. This will have to come in a separate PR, ideally sharing some of the code in groupby.
  • No knowledge of # of parallel queries. GroupBy is cognizant of this with processing.numMergeBuffers. I could use this as an "automated" way of determining the available buffer size instead of hardcoding a default constant, for example: some % of maxJVMHeap / # max parallel queries

Release note


Key changed/added classes in this PR
  • extensions-contrib/distinctcount/src/test/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountTopNQueryTest.java
  • processing/src/main/java/org/apache/druid/query/topn/BaseTopNAlgorithm.java
  • processing/src/main/java/org/apache/druid/query/topn/PooledTopNAlgorithm.java
  • processing/src/main/java/org/apache/druid/query/topn/TopNAggregatorResourceHelper.java
  • processing/src/main/java/org/apache/druid/query/topn/TopNQuery.java
  • processing/src/main/java/org/apache/druid/query/topn/TopNQueryEngine.java
  • processing/src/main/java/org/apache/druid/query/topn/TopNQueryQueryToolChest.java
  • processing/src/main/java/org/apache/druid/query/topn/TopNQueryRunnerFactory.java
  • processing/src/test/java/org/apache/druid/query/topn/TopNQueryRunnerFailureTest.java
  • processing/src/test/java/org/apache/druid/segment/CursorHolderPreaggTest.java
  • processing/src/test/java/org/apache/druid/segment/incremental/IncrementalIndexCursorFactoryTest.java

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • added integration tests.
  • been tested in a test Druid cluster.

@jtuglu-netflix jtuglu-netflix changed the title Add TopN query guardrails Add TopN Query Aggregator Memory Guardrails Oct 31, 2024
@jtuglu-netflix jtuglu-netflix force-pushed the add-topn-memory-guardrails-master-head branch 2 times, most recently from 8e397ee to b94ba51 Compare October 31, 2024 22:38
@jtuglu-netflix jtuglu-netflix marked this pull request as ready for review October 31, 2024 22:39
@jtuglu-netflix jtuglu-netflix force-pushed the add-topn-memory-guardrails-master-head branch from b94ba51 to dc7d238 Compare November 1, 2024 04:41


@Rule
public ExpectedException expectedException = ExpectedException.none();

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note test

Invoking
ExpectedException.none
should be avoided because it has been deprecated.
boolean specializeHistorical1SimpleDoubleAggPooledTopN,
boolean specializeHistoricalSingleValueDimSelector1SimpleDoubleAggPooledTopN,
List<AggregatorFactory> commonAggregators,
String testName

Check notice

Code scanning / CodeQL

Useless parameter Note test

The parameter 'testName' is never used.
@jtuglu-netflix jtuglu-netflix force-pushed the add-topn-memory-guardrails-master-head branch from dc7d238 to dd2cab0 Compare November 6, 2024 23:43
@samarthjain
Copy link
Contributor

Use a configurable (per-query, per-segment) fixed size byte amount that aggregators can take up.

This part of PR description confused me a little bit because the configuration is really at a query level and not at a segment level (which would be a bit hard to set the right limit for).

@jtuglu-netflix
Copy link
Contributor Author

jtuglu-netflix commented Nov 13, 2024

Use a configurable (per-query, per-segment) fixed size byte amount that aggregators can take up.

This part of PR description confused me a little bit because the configuration is really at a query level and not at a segment level (which would be a bit hard to set the right limit for).

Since this is initialized per-runner (which can be across different segments on different historicals, etc.), this is technically unique per (query-id, segment-id), or more generally per SpecificSegmentQueryRunner.

Edit:

This is now tracks at the (query, historical) level.

@jtuglu-netflix jtuglu-netflix force-pushed the add-topn-memory-guardrails-master-head branch from 5e51e6d to f4f2554 Compare November 15, 2024 04:31
@jtuglu-netflix jtuglu-netflix force-pushed the add-topn-memory-guardrails-master-head branch from f4f2554 to 56a7eb9 Compare November 15, 2024 04:34
@@ -97,9 +99,62 @@
: postAggregatorSpecs
);

final long expectedAllocBytes = aggregatorSpecs.stream()

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
aggregatorSpecs
may be null at this access as suggested by
this
null guard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants