Skip to content
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

Minimize search source of shard level search requests #80634

Merged
merged 5 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -1809,4 +1809,18 @@ public String toString(Params params) {
}
}

/**
* Returns a minimal source that discards information that aren't required by shard-level search requests.
* The purpose is to minimize the memory usage and serialization cost of shard-level search requests.
*/
public SearchSourceBuilder minimalSourceForShardRequests() {
if (pointInTimeBuilder != null || collapse != null) {
final SearchSourceBuilder source = shallowCopy();
source.pointInTimeBuilder(null);
source.collapse(null);
dnhatn marked this conversation as resolved.
Show resolved Hide resolved
return source;
} else {
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ public void setAliasFilter(AliasFilter aliasFilter) {
}

public void source(SearchSourceBuilder source) {
if (source != null) {
source = source.minimalSourceForShardRequests();
dnhatn marked this conversation as resolved.
Show resolved Hide resolved
}
this.source = source;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import org.elasticsearch.action.OriginalIndices;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.ClearScrollRequest;
import org.elasticsearch.action.search.ClosePointInTimeAction;
import org.elasticsearch.action.search.ClosePointInTimeRequest;
import org.elasticsearch.action.search.OpenPointInTimeAction;
import org.elasticsearch.action.search.OpenPointInTimeRequest;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
Expand Down Expand Up @@ -78,6 +82,7 @@
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.builder.PointInTimeBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.FetchSearchResult;
import org.elasticsearch.search.fetch.ShardFetchRequest;
Expand Down Expand Up @@ -105,6 +110,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
Expand All @@ -113,6 +119,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
Expand All @@ -126,6 +133,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.Matchers.not;
import static org.mockito.Mockito.mock;

public class SearchServiceTests extends ESSingleNodeTestCase {
Expand Down Expand Up @@ -1824,6 +1832,37 @@ public void testWaitOnRefreshTimeout() {
assertThat(ex.getMessage(), containsString("Wait for seq_no [0] refreshed timed out ["));
}

public void testMinimalSearchSourceInShardRequests() {
createIndex("test");
int numDocs = between(0, 10);
for (int i = 0; i < numDocs; i++) {
client().prepareIndex("test").setSource("id", Integer.toString(i)).get();
}
client().admin().indices().prepareRefresh("test").get();

String pitId = client().execute(
OpenPointInTimeAction.INSTANCE,
new OpenPointInTimeRequest("test").keepAlive(TimeValue.timeValueMinutes(10))
).actionGet().getPointInTimeId();
final MockSearchService searchService = (MockSearchService) getInstanceFromNode(SearchService.class);
final List<ShardSearchRequest> shardRequests = new CopyOnWriteArrayList<>();
searchService.setOnCreateSearchContext(ctx -> shardRequests.add(ctx.request()));
try {
SearchRequest searchRequest = new SearchRequest().source(
new SearchSourceBuilder().size(between(numDocs, numDocs * 2)).pointInTimeBuilder(new PointInTimeBuilder(pitId))
);
final SearchResponse searchResponse = client().search(searchRequest).actionGet();
assertHitCount(searchResponse, numDocs);
} finally {
client().execute(ClosePointInTimeAction.INSTANCE, new ClosePointInTimeRequest(pitId)).actionGet();
}
assertThat(shardRequests, not(emptyList()));
for (ShardSearchRequest shardRequest : shardRequests) {
assertNotNull(shardRequest.source());
assertNotNull(shardRequest.source().pointInTimeBuilder());
}
}

private ReaderContext createReaderContext(IndexService indexService, IndexShard indexShard) {
return new ReaderContext(
new ShardSearchContextId(UUIDs.randomBase64UUID(), randomNonNegativeLong()),
Expand Down