Skip to content

Commit 131f68c

Browse files
committed
Update BucketUtils#suggestShardSideQueueSize signature (#33210)
`BucketUtils#suggestShardSideQueueSize` used to calculate the shard_size based on the number of shards. It returns now a different value only based on whether we are querying a single shard or multiple shards. This commit replaces the numberOfShards argument with a boolean that tells whether we are querying a single shard or not.
1 parent 923b33c commit 131f68c

File tree

6 files changed

+12
-22
lines changed

6 files changed

+12
-22
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketUtils.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,21 @@ private BucketUtils() {}
3131
*
3232
* @param finalSize
3333
* The number of terms required in the final reduce phase.
34-
* @param numberOfShards
35-
* The number of shards being queried.
34+
* @param singleShard
35+
* whether a single shard is being queried, or multiple shards
3636
* @return A suggested default for the size of any shard-side PriorityQueues
3737
*/
38-
public static int suggestShardSideQueueSize(int finalSize, int numberOfShards) {
38+
public static int suggestShardSideQueueSize(int finalSize, boolean singleShard) {
3939
if (finalSize < 1) {
4040
throw new IllegalArgumentException("size must be positive, got " + finalSize);
4141
}
42-
if (numberOfShards < 1) {
43-
throw new IllegalArgumentException("number of shards must be positive, got " + numberOfShards);
44-
}
45-
46-
if (numberOfShards == 1) {
42+
if (singleShard) {
4743
// In the case of a single shard, we do not need to over-request
4844
return finalSize;
4945
}
50-
5146
// Request 50% more buckets on the shards in order to improve accuracy
5247
// as well as a small constant that should help with small values of 'size'
5348
final long shardSampleSize = (long) (finalSize * 1.5 + 10);
5449
return (int) Math.min(Integer.MAX_VALUE, shardSampleSize);
5550
}
56-
5751
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregationBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public int shardSize() {
157157
if (shardSize < 0) {
158158
// Use default heuristic to avoid any wrong-ranking caused by
159159
// distributed counting
160-
shardSize = BucketUtils.suggestShardSideQueueSize(requiredSize, context.numberOfShards());
160+
shardSize = BucketUtils.suggestShardSideQueueSize(requiredSize, context.numberOfShards() == 1);
161161
}
162162

163163
if (requiredSize <= 0 || shardSize <= 0) {

server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregatorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource, Aggregator pare
195195
// such are impossible to differentiate from non-significant terms
196196
// at that early stage.
197197
bucketCountThresholds.setShardSize(2 * BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize(),
198-
context.numberOfShards()));
198+
context.numberOfShards() == 1));
199199
}
200200

201201
if (valuesSource instanceof ValuesSource.Bytes) {

server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTextAggregatorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected Aggregator createInternal(Aggregator parent, boolean collectsFromSingl
176176
// such are impossible to differentiate from non-significant terms
177177
// at that early stage.
178178
bucketCountThresholds.setShardSize(2 * BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize(),
179-
context.numberOfShards()));
179+
context.numberOfShards() == 1));
180180
}
181181

182182
// TODO - need to check with mapping that this is indeed a text field....

server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource, Aggregator pare
122122
// heuristic to avoid any wrong-ranking caused by distributed
123123
// counting
124124
bucketCountThresholds.setShardSize(BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize(),
125-
context.numberOfShards()));
125+
context.numberOfShards() == 1));
126126
}
127127
bucketCountThresholds.ensureValidity();
128128
if (valuesSource instanceof ValuesSource.Bytes) {

server/src/test/java/org/elasticsearch/search/aggregations/bucket/BucketUtilsTests.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,22 @@ public class BucketUtilsTests extends ESTestCase {
2727

2828
public void testBadInput() {
2929
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
30-
() -> BucketUtils.suggestShardSideQueueSize(0, 10));
30+
() -> BucketUtils.suggestShardSideQueueSize(0, randomBoolean()));
3131
assertEquals(e.getMessage(), "size must be positive, got 0");
32-
33-
e = expectThrows(IllegalArgumentException.class,
34-
() -> BucketUtils.suggestShardSideQueueSize(10, 0));
35-
assertEquals(e.getMessage(), "number of shards must be positive, got 0");
3632
}
3733

3834
public void testOptimizesSingleShard() {
3935
for (int iter = 0; iter < 10; ++iter) {
4036
final int size = randomIntBetween(1, Integer.MAX_VALUE);
41-
assertEquals(size, BucketUtils.suggestShardSideQueueSize( size, 1));
37+
assertEquals(size, BucketUtils.suggestShardSideQueueSize( size, true));
4238
}
4339
}
4440

4541
public void testOverFlow() {
4642
for (int iter = 0; iter < 10; ++iter) {
4743
final int size = Integer.MAX_VALUE - randomInt(10);
4844
final int numberOfShards = randomIntBetween(1, 10);
49-
final int shardSize = BucketUtils.suggestShardSideQueueSize( size, numberOfShards);
45+
final int shardSize = BucketUtils.suggestShardSideQueueSize( size, numberOfShards == 1);
5046
assertThat(shardSize, greaterThanOrEqualTo(shardSize));
5147
}
5248
}
@@ -55,7 +51,7 @@ public void testShardSizeIsGreaterThanGlobalSize() {
5551
for (int iter = 0; iter < 10; ++iter) {
5652
final int size = randomIntBetween(1, Integer.MAX_VALUE);
5753
final int numberOfShards = randomIntBetween(1, 10);
58-
final int shardSize = BucketUtils.suggestShardSideQueueSize( size, numberOfShards);
54+
final int shardSize = BucketUtils.suggestShardSideQueueSize( size, numberOfShards == 1);
5955
assertThat(shardSize, greaterThanOrEqualTo(size));
6056
}
6157
}

0 commit comments

Comments
 (0)