-   Notifications  
You must be signed in to change notification settings  - Fork 25.6k
 
Add IT for num_reduced_phases with batched query execution #134312
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
   Merged  
  benchaplin merged 12 commits into elastic:main from benchaplin:batched_add_it_num_reduce_phases        Sep 17, 2025  
    Merged  
 Changes from all commits
 Commits 
  Show all changes 
  12 commits   Select commit Hold shift + click to select a range 
 ddbe410  Add IT for num_reduce_phases coverage in batched 
  benchaplin dfe508e  Merge branch 'main' into batched_add_it_num_reduce_phases 
  benchaplin c7a564a  Correct assertion, reduce scope of field exposed in NodeQueryRequest 
  benchaplin c95621c  Merge branch 'main' into batched_add_it_num_reduce_phases 
  benchaplin 15f2c13  Deduce batched shards from cluster state 
  benchaplin 722f620  Remove new getter - no longer needed 
  benchaplin 452884e  [CI] Auto commit changes from spotless 
  eb5028a  Typo 
  benchaplin 4f2abf8  Typo 
  benchaplin 6afe28a  Add assertion that num_reduce_phases >= 1 
  benchaplin eccb29d  Revert "Add assertion that num_reduce_phases >= 1" 
  benchaplin 74a4f50  Merge branch 'main' into batched_add_it_num_reduce_phases 
  benchaplin File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
There are no files selected for viewing
   3 changes: 0 additions & 3 deletions  3   rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search/120_batch_reduce_size.yml          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        92 changes: 92 additions & 0 deletions  92   server/src/internalClusterTest/java/org/elasticsearch/action/search/BatchedQueryPhaseIT.java          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|   |  ||
| package org.elasticsearch.action.search; | ||
|   |  ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.cluster.metadata.ProjectId; | ||
| import org.elasticsearch.cluster.routing.IndexRoutingTable; | ||
| import org.elasticsearch.cluster.routing.IndexShardRoutingTable; | ||
| import org.elasticsearch.cluster.routing.ShardRouting; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.test.ESIntegTestCase; | ||
|   |  ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|   |  ||
| import static org.elasticsearch.action.search.SearchType.QUERY_THEN_FETCH; | ||
| import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; | ||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailuresAndResponse; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|   |  ||
| public class BatchedQueryPhaseIT extends ESIntegTestCase { | ||
|   |  ||
| public void testNumReducePhases() { | ||
    drempapis marked this conversation as resolved.    Show resolved Hide resolved  |  ||
| String indexName = "test-idx"; | ||
| assertAcked( | ||
| prepareCreate(indexName).setMapping("title", "type=keyword") | ||
| .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)) | ||
| ); | ||
| for (int i = 0; i < 100; i++) { | ||
| prepareIndex(indexName).setId(Integer.toString(i)).setSource("title", "testing" + i).get(); | ||
| } | ||
| refresh(); | ||
|   |  ||
| final String coordinatorNode = internalCluster().getRandomNodeName(); | ||
| final String coordinatorNodeId = getNodeId(coordinatorNode); | ||
| assertNoFailuresAndResponse( | ||
| client(coordinatorNode).prepareSearch(indexName) | ||
| .setBatchedReduceSize(2) | ||
| .addAggregation(terms("terms").field("title")) | ||
| .setSearchType(QUERY_THEN_FETCH), | ||
| response -> { | ||
| Map<String, Integer> shardsPerNode = getNodeToShardCountMap(indexName); | ||
| // Shards are not batched if they are already on the coordinating node or if there is only one per data node. | ||
| final int coordinatorShards = shardsPerNode.getOrDefault(coordinatorNodeId, 0); | ||
| final long otherSingleShardNodes = shardsPerNode.entrySet() | ||
| .stream() | ||
| .filter(entry -> entry.getKey().equals(coordinatorNodeId) == false) | ||
| .filter(entry -> entry.getValue() == 1) | ||
| .count(); | ||
| final int numNotBatchedShards = coordinatorShards + (int) otherSingleShardNodes; | ||
|   |  ||
| // Because batched_reduce_size = 2, whenever two or more shard results exist on the coordinating node, they will be | ||
| // partially reduced (batched queries do not count towards num_reduce_phases). | ||
| // Hence, the formula: (# of NOT batched shards) - 1. | ||
| final int expectedNumReducePhases = Math.max(1, numNotBatchedShards - 1); | ||
| assertThat(response.getNumReducePhases(), equalTo(expectedNumReducePhases)); | ||
| } | ||
| ); | ||
| } | ||
|   |  ||
| private Map<String, Integer> getNodeToShardCountMap(String indexName) { | ||
| ClusterState clusterState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState(); | ||
| IndexRoutingTable indexRoutingTable = clusterState.routingTable(ProjectId.DEFAULT).index(indexName); | ||
| if (indexRoutingTable == null) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|   |  ||
| Map<String, Integer> nodeToShardCount = new HashMap<>(); | ||
| for (int shardId = 0; shardId < indexRoutingTable.size(); shardId++) { | ||
| IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(shardId); | ||
| for (int copy = 0; copy < shardRoutingTable.size(); copy++) { | ||
| ShardRouting shardRouting = shardRoutingTable.shard(copy); | ||
| String nodeId = shardRouting.currentNodeId(); | ||
| if (nodeId != null) { | ||
| nodeToShardCount.merge(nodeId, 1, Integer::sum); | ||
| } | ||
| } | ||
| } | ||
|   |  ||
| return nodeToShardCount; | ||
| } | ||
| } | ||
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
      Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we just get away with unskipping this ? I was under the impression that it's going to fail. Or does it run in a controlled scenario where the result is predictable?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you take a look at #121885 the line that would make this test fail was removed. Now it essentially just tests batched_reduce_size validation. I'm not sure if it's worth keeping, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks. I'd keep it. Can we have some simpler check on num_reduce_phases, like greather than some threshold that's easier to predict?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think we can assume anything about num_reduce_phases now - if it's 1, it's left out of the response entirely. And it can be 1 if all shards are batched.