You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "cmick (via GitHub)" <gi...@apache.org> on 2025/02/27 16:57:36 UTC
[PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
cmick opened a new pull request, #17764:
URL: https://github.com/apache/druid/pull/17764
### Description
The current connection count query balancer (when `druid.broker.balancer.type=connectionCount`) behavior in Apache Druid has a deterministic server selection mechanism when multiple servers have the same number of active connections. This results in uneven query distribution, particularly in the following cases:
- A single query always targets the same node.
- If the nodes have the same number of connections, a single Historical is always "preferred".
- If the number of queries surpasses the number of active connections on Historicals, the Broker may end up targeting only one Historical node, leading to potential performance bottlenecks.
Likely the same problem is described in an old issue here: https://github.com/apache/druid/issues/3777
### Proposed Fix
This PR introduces a simple tie-breaking mechanism in the balancer to prevent deterministic selection when multiple servers have the same connection count. This will improve overall load balancing by:
- Keeping the behavior unchanged when the nodes have different number of active connections (the process with the fewest number of active connections is always picked).
- Falling back to random query balancing when the processes have the same number of connections.
- Ensuring that when query volume increases beyond active connections on Historicals, the Broker does not repeatedly select the same Historical.
### Impact:
The fix only impacts clusters with `druid.broker.balancer.type=connectionCount` set (by default it is set to `random`):
- Improves query distribution across servers, reducing load imbalance.
- Prevents performance degradation due to overloading a single Historical.
### Testing:
- Added unit tests to validate improved balancing behavior.
- Manually tested in a clustered setup to ensure even query distribution.
#### Related Issues:
- https://github.com/apache/druid/issues/3777
---
Let me know if any additional details should be included!
#### 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.
-->
<!-- 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:
- [x] 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 comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
- [x] 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.
- [x] 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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
Posted by "gianm (via GitHub)" <gi...@apache.org>.
gianm commented on PR #17764:
URL: https://github.com/apache/druid/pull/17764#issuecomment-2773073399
I'll merge this, it looks ok to me. @cmick thank you for your contribution!
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
Posted by "cmick (via GitHub)" <gi...@apache.org>.
cmick commented on code in PR #17764:
URL: https://github.com/apache/druid/pull/17764#discussion_r2012906431
##########
server/src/main/java/org/apache/druid/client/selector/ConnectionCountServerSelectorStrategy.java:
##########
@@ -30,11 +30,15 @@
import java.util.Comparator;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
public class ConnectionCountServerSelectorStrategy implements ServerSelectorStrategy
{
private static final Comparator<QueryableDruidServer> COMPARATOR =
- Comparator.comparingInt(s -> ((DirectDruidClient) s.getQueryRunner()).getNumOpenConnections());
+ Comparator.comparingDouble(s ->
+ ((DirectDruidClient) s.getQueryRunner()).getNumOpenConnections()
Review Comment:
Thanks for the comment @cryptoe ! The private `COMPARATOR` indeed compares based on the number of connections, but in the strategy (public interface), we either select the server with the minimum number of connections (line 47), or—when multiple servers are needed—we return the N servers with the least connections (line 56). So the comparator logic is preserved and functions as intended.
However, this commit actually doesn't alter that core logic. Instead, it adds a random tie-breaking factor that is only used when two or more servers have the same number of connections. In such cases, we now make the selection randomly among the tied candidates, instead of always picking one deterministically.
I’ve also included unit tests to validate the strategy. For example, `testDifferentConnectionCount` confirms that the selection still favors servers with fewer connections (same as the original behavior), while `testBalancerTieBreaking` ensures that the new tie-breaking logic is exercised and behaves as expected (it would fail under the old deterministic behavior but now passes with the updated logic).
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
Posted by "gianm (via GitHub)" <gi...@apache.org>.
gianm commented on code in PR #17764:
URL: https://github.com/apache/druid/pull/17764#discussion_r2022349852
##########
server/src/main/java/org/apache/druid/client/selector/ConnectionCountServerSelectorStrategy.java:
##########
@@ -30,11 +30,15 @@
import java.util.Comparator;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
public class ConnectionCountServerSelectorStrategy implements ServerSelectorStrategy
{
private static final Comparator<QueryableDruidServer> COMPARATOR =
- Comparator.comparingInt(s -> ((DirectDruidClient) s.getQueryRunner()).getNumOpenConnections());
+ Comparator.comparingDouble(s ->
+ ((DirectDruidClient) s.getQueryRunner()).getNumOpenConnections()
Review Comment:
Sounds right to me. Put another way, `nextDouble()` is always between 0 and 1 so this comparator change will only be used to break ties.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
Posted by "cmick (via GitHub)" <gi...@apache.org>.
cmick commented on PR #17764:
URL: https://github.com/apache/druid/pull/17764#issuecomment-2773748098
Thanks, @gianm ! Appreciate the quick review and merge. Glad to have contributed!
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #17764:
URL: https://github.com/apache/druid/pull/17764#discussion_r2012263701
##########
server/src/main/java/org/apache/druid/client/selector/ConnectionCountServerSelectorStrategy.java:
##########
@@ -30,11 +30,15 @@
import java.util.Comparator;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
public class ConnectionCountServerSelectorStrategy implements ServerSelectorStrategy
{
private static final Comparator<QueryableDruidServer> COMPARATOR =
- Comparator.comparingInt(s -> ((DirectDruidClient) s.getQueryRunner()).getNumOpenConnections());
+ Comparator.comparingDouble(s ->
+ ((DirectDruidClient) s.getQueryRunner()).getNumOpenConnections()
Review Comment:
Won't this defeat the comperator logic since a server would more open connections would get picked ?
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
Posted by "gianm (via GitHub)" <gi...@apache.org>.
gianm merged PR #17764:
URL: https://github.com/apache/druid/pull/17764
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on PR #17764:
URL: https://github.com/apache/druid/pull/17764#issuecomment-2774419847
This looks good 🚀
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix deterministic server selection in balancer by adding tie-breaking logic (druid)
Posted by "cryptoe (via GitHub)" <gi...@apache.org>.
cryptoe commented on code in PR #17764:
URL: https://github.com/apache/druid/pull/17764#discussion_r2026123875
##########
server/src/main/java/org/apache/druid/client/selector/ConnectionCountServerSelectorStrategy.java:
##########
@@ -30,11 +30,15 @@
import java.util.Comparator;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
public class ConnectionCountServerSelectorStrategy implements ServerSelectorStrategy
{
private static final Comparator<QueryableDruidServer> COMPARATOR =
- Comparator.comparingInt(s -> ((DirectDruidClient) s.getQueryRunner()).getNumOpenConnections());
+ Comparator.comparingDouble(s ->
+ ((DirectDruidClient) s.getQueryRunner()).getNumOpenConnections()
Review Comment:
Thanks @cmick / @gianm this makes sense.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org