Skip to content

WindowOperatorQueryFrameProcessor: Avoid unnecessary re-runs of runIncrementally()#17211

Merged
cryptoe merged 1 commit intoapache:masterfrom
Akshat-Jain:msq-wf-runIncrementally-optimization
Oct 3, 2024
Merged

WindowOperatorQueryFrameProcessor: Avoid unnecessary re-runs of runIncrementally()#17211
cryptoe merged 1 commit intoapache:masterfrom
Akshat-Jain:msq-wf-runIncrementally-optimization

Conversation

@Akshat-Jain
Copy link
Copy Markdown
Contributor

Description

This PR optimizes the scenarios where we had all window functions with some partition by clause. Currently, we were triggering another run of runIncrementally() on every partition by change. But this is unnecessary overhead. This PR changes the behavior to only trigger a re-run when we write a frame to the output channel.

For example, the below query that processes almost 50M rows was run with maxNumTasks=2 against the current master code vs this PR's code.

select c1, count(c1) from (select trip_id, row_number() over(partition by trip_id) as c1 from "trips_xaa" where __time < TIMESTAMP '2016-10-20 11:15:25' group by trip_id) group by c1

We can see in the following screenshots that the window stage took 5:37 minutes in the current master code compared to 35 seconds with this PR's code, making the overall query time to go down from 23 minutes to 18 minutes.

Current master code This PR's code
image image

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.

@github-actions github-actions Bot added Area - Batch Ingestion Area - MSQ For multi stage queries - https://github.com/apache/druid/issues/12262 labels Oct 1, 2024
} else {
lastPartitionIndex = rowsToProcess.size() - 1;
outputRow = currentRow.copy();
return ReturnOrAwait.runAgain();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this return was insane... no wonder it have lead to performance degradation...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah.. 😅

Comment on lines +266 to +271
if (rowsToProcess.size() > maxRowsMaterialized) {
// We don't want to materialize more than maxRowsMaterialized rows at any point in time, so process the pending batch.
processRowsUpToLastPartition();
ensureMaxRowsInAWindowConstraint(rowsToProcess.size());
return ReturnOrAwait.runAgain();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

moving this check outside here - will not necessarily enforce that maxRowsMaterialized is honored - but I guess the input frame is already materialized so we may go up to asymptotically twice in mem usage....we can leave it as is for now as this will be improved later

ensureMaxRowsInAWindowConstraint(rowsToProcess.size());
} else {
lastPartitionIndex = rowsToProcess.size() - 1;
outputRow = currentRow.copy();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is odd; outputRow holds a direct reference to currentRow at line 254; but here its being copy-ed; I think they both should be a copy or neither of them...

but again....this will be removed so no big deal

@cryptoe cryptoe added this to the 31.0.0 milestone Oct 3, 2024
@cryptoe cryptoe merged commit edc235c into apache:master Oct 3, 2024
kgyrtkirk pushed a commit that referenced this pull request Oct 3, 2024
…17231)

* WindowOperatorQueryFrameProcessor: Fix frame writer capacity issues + adhere to FrameProcessor's contract (#17209)
* WindowOperatorQueryFrameProcessor: Avoid unnecessary re-runs of runIncrementally() (#17211)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area - Batch Ingestion Area - MSQ For multi stage queries - https://github.com/apache/druid/issues/12262

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants