Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
setup:
- skip:
awaits_fix: "TODO fix this test, the response with batched execution is not deterministic enough for the available matchers"
Copy link
Member

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?

Copy link
Contributor Author

@benchaplin benchaplin Sep 15, 2025

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?

Copy link
Member

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?

Copy link
Contributor Author

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.


- do:
indices.create:
index: test_1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.action.search.SearchQueryThenFetchAsyncAction.NodeQueryRequest;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.plugins.NetworkPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.transport.TransportInterceptor;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.transport.TransportRequestHandler;
import org.junit.Before;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;

import static org.elasticsearch.action.search.SearchQueryThenFetchAsyncAction.NODE_SEARCH_ACTION_NAME;
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 {

// All the batched query requests that were made in each test
private static final List<NodeQueryRequest> batchedQueryRequests = new CopyOnWriteArrayList<>();

@Before
public void clear() {
batchedQueryRequests.clear();
}

public static class BatchedQueryCapturePlugin extends Plugin implements NetworkPlugin {
@Override
public List<TransportInterceptor> getTransportInterceptors(
NamedWriteableRegistry namedWriteableRegistry,
ThreadContext threadContext
) {
return List.of(new TransportInterceptor() {
@Override
public <T extends TransportRequest> TransportRequestHandler<T> interceptHandler(
String action,
Executor executor,
boolean forceExecution,
TransportRequestHandler<T> actualHandler
) {
if (NODE_SEARCH_ACTION_NAME.equals(action)) {
return (request, channel, task) -> {
batchedQueryRequests.add((NodeQueryRequest) request);
actualHandler.messageReceived(request, channel, task);
};
}
return actualHandler;
}
});
}
}

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return List.of(BatchedQueryCapturePlugin.class);
}

/**
* num_reduce_phases tracks the number of times a partial reduction occurs on the coordinating node.
* This test must be aware of how batched queries are executed because reductions on the data nodes are
* not counted.
*/
public void testNumReducePhases() {
assertAcked(prepareCreate("test-idx").setMapping("title", "type=keyword"));
for (int i = 0; i < 100; i++) {
prepareIndex("test-idx").setId(Integer.toString(i)).setSource("title", "testing" + i).get();
}
refresh();

assertNoFailuresAndResponse(
prepareSearch("test-idx").setBatchedReduceSize(2).addAggregation(terms("terms").field("title")).setSearchType(QUERY_THEN_FETCH),
response -> {
final int totalShards = response.getTotalShards();
final List<Integer> shardsPerDataNode = batchedQueryRequests.stream()
.map(nodeQueryRequest -> nodeQueryRequest.shards().size())
.toList();
final int coordNodeShards = totalShards - shardsPerDataNode.stream().mapToInt(Integer::intValue).sum();

// Because batched_reduce_size = 2, whenever two or more shard results exist on the coordinating node, they will be
// partially reduced. This reduction happens when either:
// - The search fans out directly to shards on the coordinating node
// - A batched result comes back
// Hence the formula: (# of shards on the coordinating node) + (# of batched requests) - 1
final int expectedNumReducePhases = Math.max(1, coordNodeShards + shardsPerDataNode.size() - 1);
assertThat(response.getNumReducePhases(), equalTo(expectedNumReducePhases));
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ public String[] indices() {
public IndicesOptions indicesOptions() {
return searchRequest.indicesOptions();
}

public List<ShardToQuery> shards() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider limiting this to a package-protected scope or exposing only shard.size(), as the test doesn’t need the complete list.

Copy link
Member

Choose a reason for hiding this comment

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

++ another way to do this would be to inspect the indices service in the test itself, and extract the info about the how many nodes and how many shards per node from there, as opposed to from the request.

Copy link
Member

Choose a reason for hiding this comment

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

Another another way to go about this test is to run it in a more controlled scenario, along the same lines as Dimi suggested above: decide upfront how many shards and nodes, and make the execution more predictable that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, thanks @drempapis - reduced to package-private and just the size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And thanks @javanna, I agree this is cleaner without the request intercepting stuff. I've reworked the test a bit to deduce how many shards aren't batched from cluster state.

return shards;
}
}

private record ShardToQuery(float boost, String[] originalIndices, int shardIndex, ShardId shardId, ShardSearchContextId contextId)
Expand Down