Skip to content

Commit d3abf9d

Browse files
Dry up search error trace ITs (#122138)
This logic will need a bit of adjustment for bulk query execution. Lets dry it up before so we don't have to copy and paste the fix which will be a couple lines.
1 parent f81f717 commit d3abf9d

File tree

3 files changed

+62
-66
lines changed

3 files changed

+62
-66
lines changed

qa/smoke-test-http/src/javaRestTest/java/org/elasticsearch/http/SearchErrorTraceIT.java

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,26 @@
1111

1212
import org.apache.http.entity.ContentType;
1313
import org.apache.http.nio.entity.NByteArrayEntity;
14-
import org.elasticsearch.ExceptionsHelper;
1514
import org.elasticsearch.action.search.MultiSearchRequest;
1615
import org.elasticsearch.action.search.SearchRequest;
1716
import org.elasticsearch.client.Request;
17+
import org.elasticsearch.search.ErrorTraceHelper;
1818
import org.elasticsearch.search.builder.SearchSourceBuilder;
19-
import org.elasticsearch.transport.TransportMessageListener;
20-
import org.elasticsearch.transport.TransportService;
2119
import org.elasticsearch.xcontent.XContentType;
2220
import org.junit.Before;
2321

2422
import java.io.IOException;
2523
import java.nio.charset.Charset;
26-
import java.util.Optional;
27-
import java.util.concurrent.atomic.AtomicBoolean;
24+
import java.util.function.BooleanSupplier;
2825

2926
import static org.elasticsearch.index.query.QueryBuilders.simpleQueryStringQuery;
3027

3128
public class SearchErrorTraceIT extends HttpSmokeTestCase {
32-
private AtomicBoolean hasStackTrace;
29+
private BooleanSupplier hasStackTrace;
3330

3431
@Before
35-
private void setupMessageListener() {
36-
internalCluster().getDataNodeInstances(TransportService.class).forEach(ts -> {
37-
ts.addMessageListener(new TransportMessageListener() {
38-
@Override
39-
public void onResponseSent(long requestId, String action, Exception error) {
40-
TransportMessageListener.super.onResponseSent(requestId, action, error);
41-
if (action.startsWith("indices:data/read/search")) {
42-
Optional<Throwable> throwable = ExceptionsHelper.unwrapCausesAndSuppressed(
43-
error,
44-
t -> t.getStackTrace().length > 0
45-
);
46-
hasStackTrace.set(throwable.isPresent());
47-
}
48-
}
49-
});
50-
});
32+
public void setupMessageListener() {
33+
hasStackTrace = ErrorTraceHelper.setupErrorTraceListener(internalCluster());
5134
}
5235

5336
private void setupIndexWithDocs() {
@@ -61,7 +44,6 @@ private void setupIndexWithDocs() {
6144
}
6245

6346
public void testSearchFailingQueryErrorTraceDefault() throws IOException {
64-
hasStackTrace = new AtomicBoolean();
6547
setupIndexWithDocs();
6648

6749
Request searchRequest = new Request("POST", "/_search");
@@ -76,11 +58,10 @@ public void testSearchFailingQueryErrorTraceDefault() throws IOException {
7658
}
7759
""");
7860
getRestClient().performRequest(searchRequest);
79-
assertFalse(hasStackTrace.get());
61+
assertFalse(hasStackTrace.getAsBoolean());
8062
}
8163

8264
public void testSearchFailingQueryErrorTraceTrue() throws IOException {
83-
hasStackTrace = new AtomicBoolean();
8465
setupIndexWithDocs();
8566

8667
Request searchRequest = new Request("POST", "/_search");
@@ -96,11 +77,10 @@ public void testSearchFailingQueryErrorTraceTrue() throws IOException {
9677
""");
9778
searchRequest.addParameter("error_trace", "true");
9879
getRestClient().performRequest(searchRequest);
99-
assertTrue(hasStackTrace.get());
80+
assertTrue(hasStackTrace.getAsBoolean());
10081
}
10182

10283
public void testSearchFailingQueryErrorTraceFalse() throws IOException {
103-
hasStackTrace = new AtomicBoolean();
10484
setupIndexWithDocs();
10585

10686
Request searchRequest = new Request("POST", "/_search");
@@ -116,11 +96,10 @@ public void testSearchFailingQueryErrorTraceFalse() throws IOException {
11696
""");
11797
searchRequest.addParameter("error_trace", "false");
11898
getRestClient().performRequest(searchRequest);
119-
assertFalse(hasStackTrace.get());
99+
assertFalse(hasStackTrace.getAsBoolean());
120100
}
121101

122102
public void testMultiSearchFailingQueryErrorTraceDefault() throws IOException {
123-
hasStackTrace = new AtomicBoolean();
124103
setupIndexWithDocs();
125104

126105
XContentType contentType = XContentType.JSON;
@@ -133,11 +112,10 @@ public void testMultiSearchFailingQueryErrorTraceDefault() throws IOException {
133112
new NByteArrayEntity(requestBody, ContentType.create(contentType.mediaTypeWithoutParameters(), (Charset) null))
134113
);
135114
getRestClient().performRequest(searchRequest);
136-
assertFalse(hasStackTrace.get());
115+
assertFalse(hasStackTrace.getAsBoolean());
137116
}
138117

139118
public void testMultiSearchFailingQueryErrorTraceTrue() throws IOException {
140-
hasStackTrace = new AtomicBoolean();
141119
setupIndexWithDocs();
142120

143121
XContentType contentType = XContentType.JSON;
@@ -151,11 +129,10 @@ public void testMultiSearchFailingQueryErrorTraceTrue() throws IOException {
151129
);
152130
searchRequest.addParameter("error_trace", "true");
153131
getRestClient().performRequest(searchRequest);
154-
assertTrue(hasStackTrace.get());
132+
assertTrue(hasStackTrace.getAsBoolean());
155133
}
156134

157135
public void testMultiSearchFailingQueryErrorTraceFalse() throws IOException {
158-
hasStackTrace = new AtomicBoolean();
159136
setupIndexWithDocs();
160137

161138
XContentType contentType = XContentType.JSON;
@@ -170,6 +147,6 @@ public void testMultiSearchFailingQueryErrorTraceFalse() throws IOException {
170147
searchRequest.addParameter("error_trace", "false");
171148
getRestClient().performRequest(searchRequest);
172149

173-
assertFalse(hasStackTrace.get());
150+
assertFalse(hasStackTrace.getAsBoolean());
174151
}
175152
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.search;
11+
12+
import org.elasticsearch.ExceptionsHelper;
13+
import org.elasticsearch.test.InternalTestCluster;
14+
import org.elasticsearch.transport.TransportMessageListener;
15+
import org.elasticsearch.transport.TransportService;
16+
17+
import java.util.Optional;
18+
import java.util.concurrent.atomic.AtomicBoolean;
19+
import java.util.function.BooleanSupplier;
20+
21+
/**
22+
* Utilities around testing the `error_trace` message header in search.
23+
*/
24+
public enum ErrorTraceHelper {
25+
;
26+
27+
public static BooleanSupplier setupErrorTraceListener(InternalTestCluster internalCluster) {
28+
final AtomicBoolean transportMessageHasStackTrace = new AtomicBoolean(false);
29+
internalCluster.getDataNodeInstances(TransportService.class).forEach(ts -> ts.addMessageListener(new TransportMessageListener() {
30+
@Override
31+
public void onResponseSent(long requestId, String action, Exception error) {
32+
TransportMessageListener.super.onResponseSent(requestId, action, error);
33+
if (action.startsWith("indices:data/read/search")) {
34+
Optional<Throwable> throwable = ExceptionsHelper.unwrapCausesAndSuppressed(error, t -> t.getStackTrace().length > 0);
35+
transportMessageHasStackTrace.set(throwable.isPresent());
36+
}
37+
}
38+
}));
39+
return transportMessageHasStackTrace::get;
40+
}
41+
}

x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@
77

88
package org.elasticsearch.xpack.search;
99

10-
import org.elasticsearch.ExceptionsHelper;
1110
import org.elasticsearch.client.Request;
1211
import org.elasticsearch.client.Response;
1312
import org.elasticsearch.common.xcontent.XContentHelper;
1413
import org.elasticsearch.core.TimeValue;
1514
import org.elasticsearch.plugins.Plugin;
15+
import org.elasticsearch.search.ErrorTraceHelper;
1616
import org.elasticsearch.test.ESIntegTestCase;
17-
import org.elasticsearch.transport.TransportMessageListener;
18-
import org.elasticsearch.transport.TransportService;
1917
import org.elasticsearch.xcontent.XContentType;
2018
import org.junit.Before;
2119

2220
import java.io.IOException;
2321
import java.util.Collection;
2422
import java.util.List;
2523
import java.util.Map;
26-
import java.util.Optional;
27-
import java.util.concurrent.atomic.AtomicBoolean;
24+
import java.util.function.BooleanSupplier;
2825

2926
public class AsyncSearchErrorTraceIT extends ESIntegTestCase {
3027

@@ -38,25 +35,11 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
3835
return List.of(AsyncSearch.class);
3936
}
4037

41-
private AtomicBoolean transportMessageHasStackTrace;
38+
private BooleanSupplier transportMessageHasStackTrace;
4239

4340
@Before
44-
private void setupMessageListener() {
45-
internalCluster().getDataNodeInstances(TransportService.class).forEach(ts -> {
46-
ts.addMessageListener(new TransportMessageListener() {
47-
@Override
48-
public void onResponseSent(long requestId, String action, Exception error) {
49-
TransportMessageListener.super.onResponseSent(requestId, action, error);
50-
if (action.startsWith("indices:data/read/search")) {
51-
Optional<Throwable> throwable = ExceptionsHelper.unwrapCausesAndSuppressed(
52-
error,
53-
t -> t.getStackTrace().length > 0
54-
);
55-
transportMessageHasStackTrace.set(throwable.isPresent());
56-
}
57-
}
58-
});
59-
});
41+
public void setupMessageListener() {
42+
transportMessageHasStackTrace = ErrorTraceHelper.setupErrorTraceListener(internalCluster());
6043
}
6144

6245
private void setupIndexWithDocs() {
@@ -70,7 +53,6 @@ private void setupIndexWithDocs() {
7053
}
7154

7255
public void testAsyncSearchFailingQueryErrorTraceDefault() throws IOException, InterruptedException {
73-
transportMessageHasStackTrace = new AtomicBoolean();
7456
setupIndexWithDocs();
7557

7658
Request searchRequest = new Request("POST", "/_async_search");
@@ -93,11 +75,10 @@ public void testAsyncSearchFailingQueryErrorTraceDefault() throws IOException, I
9375
responseEntity = performRequestAndGetResponseEntityAfterDelay(request, TimeValue.timeValueSeconds(1L));
9476
}
9577
// check that the stack trace was not sent from the data node to the coordinating node
96-
assertFalse(transportMessageHasStackTrace.get());
78+
assertFalse(transportMessageHasStackTrace.getAsBoolean());
9779
}
9880

9981
public void testAsyncSearchFailingQueryErrorTraceTrue() throws IOException, InterruptedException {
100-
transportMessageHasStackTrace = new AtomicBoolean();
10182
setupIndexWithDocs();
10283

10384
Request searchRequest = new Request("POST", "/_async_search");
@@ -122,11 +103,10 @@ public void testAsyncSearchFailingQueryErrorTraceTrue() throws IOException, Inte
122103
responseEntity = performRequestAndGetResponseEntityAfterDelay(request, TimeValue.timeValueSeconds(1L));
123104
}
124105
// check that the stack trace was sent from the data node to the coordinating node
125-
assertTrue(transportMessageHasStackTrace.get());
106+
assertTrue(transportMessageHasStackTrace.getAsBoolean());
126107
}
127108

128109
public void testAsyncSearchFailingQueryErrorTraceFalse() throws IOException, InterruptedException {
129-
transportMessageHasStackTrace = new AtomicBoolean();
130110
setupIndexWithDocs();
131111

132112
Request searchRequest = new Request("POST", "/_async_search");
@@ -151,11 +131,10 @@ public void testAsyncSearchFailingQueryErrorTraceFalse() throws IOException, Int
151131
responseEntity = performRequestAndGetResponseEntityAfterDelay(request, TimeValue.timeValueSeconds(1L));
152132
}
153133
// check that the stack trace was not sent from the data node to the coordinating node
154-
assertFalse(transportMessageHasStackTrace.get());
134+
assertFalse(transportMessageHasStackTrace.getAsBoolean());
155135
}
156136

157137
public void testAsyncSearchFailingQueryErrorTraceFalseOnSubmitAndTrueOnGet() throws IOException, InterruptedException {
158-
transportMessageHasStackTrace = new AtomicBoolean();
159138
setupIndexWithDocs();
160139

161140
Request searchRequest = new Request("POST", "/_async_search");
@@ -180,11 +159,10 @@ public void testAsyncSearchFailingQueryErrorTraceFalseOnSubmitAndTrueOnGet() thr
180159
responseEntity = performRequestAndGetResponseEntityAfterDelay(request, TimeValue.timeValueSeconds(1L));
181160
}
182161
// check that the stack trace was not sent from the data node to the coordinating node
183-
assertFalse(transportMessageHasStackTrace.get());
162+
assertFalse(transportMessageHasStackTrace.getAsBoolean());
184163
}
185164

186165
public void testAsyncSearchFailingQueryErrorTraceTrueOnSubmitAndFalseOnGet() throws IOException, InterruptedException {
187-
transportMessageHasStackTrace = new AtomicBoolean();
188166
setupIndexWithDocs();
189167

190168
Request searchRequest = new Request("POST", "/_async_search");
@@ -209,7 +187,7 @@ public void testAsyncSearchFailingQueryErrorTraceTrueOnSubmitAndFalseOnGet() thr
209187
responseEntity = performRequestAndGetResponseEntityAfterDelay(request, TimeValue.timeValueSeconds(1L));
210188
}
211189
// check that the stack trace was sent from the data node to the coordinating node
212-
assertTrue(transportMessageHasStackTrace.get());
190+
assertTrue(transportMessageHasStackTrace.getAsBoolean());
213191
}
214192

215193
private Map<String, Object> performRequestAndGetResponseEntityAfterDelay(Request r, TimeValue sleep) throws IOException,

0 commit comments

Comments
 (0)