LakshSingla opened a new pull request, #15420: URL: https://github.com/apache/druid/pull/15420
### Description When trying to merge the higher order group by results on broker (primarily), and perhaps on historicals (under special conditions when we push the subquery down to the historical), a deadlock can occur depending on the structure of the query. The primary cause of this deadlock is that currently, we are trying to acquire the merge buffer resources at two places - `GroupByQueryQueryToolchest` (which is the place where we merge the higher-level results from the runners), and at `GroupByMergingQueryRunnerV2` (to merge the results from the runners of the multiple segments). These two were incorrectly considered to be mutually exclusive till now: 1. `GroupByQueryQueryToolchest` - Acquires the merge buffers on the brokers only, as the historicals get the query wit the subqueries and the grouping sets to null, therefore this doesn't acquire any merge buffers. However, this might not be true, because when we push down the subquery itself, the historicals will also acquire the merge buffers at this place. 2. `GroupByMergingQueryRunnerV2` - Acquires merge buffers on the historicals only, as it primarily merges the results from the query runners that run on the segments. This is also incorrect because the broker can act as a data server itself when the data source is an inline data source, and we attempt to run nested group bys on that. Therefore, we have conditions when the `GroupByQueryQueryToolchest` is holding some merge buffers to merge the results of the returned runners, however, the runner itself is `GroupByMergingQueryRunnerV2` (or a decorated runner on top of it), and it also requires merge buffers to merge the "segment-level" runners to provide to the mergeResults of the toolchest. As we donot acquire the resources in a single go, following situations happen: Total merge buffers on the broker: 2 QueryA = QueryB = A query that needs 1 merge buffer to `merge results` (in toolchest), and 1 mergeBuffer for `GroupByMergingQueryRunnerV2` on the broker QueryA & QueryB are running simultaneously on the cluster | Time | QueryA actions | QueryB actions | Buffers in system |--------|--------|--------|--------| | 0 | | | 2 | | 1 | toolchest acquires 1 buffer | | 1 | | 2 | | toolchest acquires 1 buffer | 0 | | 3 | mergingRunner acquires 1 buffer (blocked) | | 0 | | 4 | | mergingRunner acquires 1 buffer (blocked) | 0 | The queries could have passed in isolation, however now they are waiting on the other to release the single merge buffer they hold to proceed. This PR prevents such a deadlock from happening by acquiring the merge buffers in a single place and passing it down to the runner that might need it. #### Release note <!-- Give your best effort to summarize your changes in a couple of sentences aimed toward Druid users. If your change doesn't have end user impact, you can skip this section. For tips about how to write a good release note, see [Release notes](https://github.com/apache/druid/blob/master/CONTRIBUTING.md#release-notes). --> <hr> ##### Key changed/added classes in this PR * `MyFoo` * `OurBar` * `TheirBaz` <hr> <!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. --> This PR has: - [ ] been self-reviewed. - [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.) - [ ] 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](https://github.com/apache/druid/blob/master/dev/license.md) - [ ] 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](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met. - [ ] added integration tests. - [ ] been tested in a test Druid cluster. -- 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]
