Skip to content

Commit 1dcf1df

Browse files
authored
Remove the query builder serialization from QueryShardException message (#51885)
QueryBuilders that throw exceptions on shards when building the Lucene query returns the full serialization of the query builder in the exception message. For large queries that fails to execute due to the max boolean clause, this means that we keep a reference of these big messages for every shard that participate in the request. In order to limit the memory needed to hold these query shard exceptions in the coordinating node, this change removes the query builder serialization from the shard exception. The query is known by the user so there should be no need to repeat it on every shard exception. We could also omit the entire stack trace for known bad request exception but it would deserve a separate issue/pr. Closes #51843 Closes #48910
1 parent 62b5b95 commit 1dcf1df

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,10 @@ private ParsedQuery toQuery(QueryBuilder queryBuilder, CheckedFunction<QueryBuil
309309
try {
310310
QueryBuilder rewriteQuery = Rewriteable.rewrite(queryBuilder, this, true);
311311
return new ParsedQuery(filterOrQuery.apply(rewriteQuery), copyNamedQueries());
312-
} catch(QueryShardException | ParsingException e ) {
312+
} catch(QueryShardException | ParsingException e) {
313313
throw e;
314314
} catch(Exception e) {
315-
throw new QueryShardException(this, "failed to create query: {}", e, queryBuilder);
315+
throw new QueryShardException(this, "failed to create query: {}", e, e.getMessage());
316316
} finally {
317317
reset();
318318
}

server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java

+40
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
*/
1919
package org.elasticsearch.index.query;
2020

21+
import org.apache.lucene.search.Query;
2122
import org.elasticsearch.Version;
2223
import org.elasticsearch.cluster.metadata.IndexMetaData;
2324
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
25+
import org.elasticsearch.common.io.stream.StreamOutput;
2426
import org.elasticsearch.common.settings.Settings;
2527
import org.elasticsearch.common.util.BigArrays;
2628
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
29+
import org.elasticsearch.common.xcontent.XContentBuilder;
2730
import org.elasticsearch.index.IndexSettings;
2831
import org.elasticsearch.index.fielddata.IndexFieldData;
2932
import org.elasticsearch.index.fielddata.plain.AbstractAtomicOrdinalsFieldData;
@@ -73,6 +76,43 @@ public void testFailIfFieldMappingNotFound() {
7376
assertThat(result.name(), equalTo("name"));
7477
}
7578

79+
public void testToQueryFails() {
80+
QueryShardContext context = createQueryShardContext(IndexMetaData.INDEX_UUID_NA_VALUE, null);
81+
Exception exc = expectThrows(Exception.class,
82+
() -> context.toQuery(new AbstractQueryBuilder() {
83+
@Override
84+
public String getWriteableName() {
85+
return null;
86+
}
87+
88+
@Override
89+
protected void doWriteTo(StreamOutput out) throws IOException {
90+
91+
}
92+
93+
@Override
94+
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
95+
96+
}
97+
98+
@Override
99+
protected Query doToQuery(QueryShardContext context) throws IOException {
100+
throw new RuntimeException("boom");
101+
}
102+
103+
@Override
104+
protected boolean doEquals(AbstractQueryBuilder other) {
105+
return false;
106+
}
107+
108+
@Override
109+
protected int doHashCode() {
110+
return 0;
111+
}
112+
}));
113+
assertThat(exc.getMessage(), equalTo("failed to create query: boom"));
114+
}
115+
76116
public void testClusterAlias() throws IOException {
77117
final String clusterAlias = randomBoolean() ? null : "remote_cluster";
78118
QueryShardContext context = createQueryShardContext(IndexMetaData.INDEX_UUID_NA_VALUE, clusterAlias);

0 commit comments

Comments
 (0)