-
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
Changes from 2 commits
ddbe410
dfe508e
c7a564a
c95621c
15f2c13
722f620
452884e
eb5028a
4f2abf8
6afe28a
eccb29d
74a4f50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() { | ||
drempapis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
---|---|---|
|
@@ -350,6 +350,10 @@ public String[] indices() { | |
public IndicesOptions indicesOptions() { | ||
return searchRequest.indicesOptions(); | ||
} | ||
|
||
public List<ShardToQuery> shards() { | ||
|
||
return shards; | ||
} | ||
} | ||
|
||
private record ShardToQuery(float boost, String[] originalIndices, int shardIndex, ShardId shardId, ShardSearchContextId contextId) | ||
|
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.