1313import org .apache .http .nio .entity .NByteArrayEntity ;
1414import org .apache .logging .log4j .Level ;
1515import org .apache .logging .log4j .core .config .Configurator ;
16- import org .elasticsearch .ExceptionsHelper ;
1716import org .elasticsearch .action .search .MultiSearchRequest ;
1817import org .elasticsearch .action .search .SearchRequest ;
1918import org .elasticsearch .client .Request ;
19+ import org .elasticsearch .common .util .CollectionUtils ;
20+ import org .elasticsearch .plugins .Plugin ;
2021import org .elasticsearch .search .ErrorTraceHelper ;
2122import org .elasticsearch .search .SearchService ;
2223import org .elasticsearch .search .builder .SearchSourceBuilder ;
2324import org .elasticsearch .test .MockLog ;
24- import org .elasticsearch .transport .TransportMessageListener ;
25- import org .elasticsearch .transport .TransportService ;
25+ import org .elasticsearch .test .transport .MockTransportService ;
2626import org .elasticsearch .xcontent .XContentType ;
2727import org .junit .Before ;
2828import org .junit .BeforeClass ;
2929
3030import java .io .IOException ;
3131import java .nio .charset .Charset ;
32- import java .util .Optional ;
33- import java .util .concurrent . atomic . AtomicBoolean ;
32+ import java .util .Collection ;
33+ import java .util .function . BooleanSupplier ;
3434
3535import static org .elasticsearch .index .query .QueryBuilders .simpleQueryStringQuery ;
3636
3737public class SearchErrorTraceIT extends HttpSmokeTestCase {
38- private AtomicBoolean hasStackTrace ;
38+ private BooleanSupplier hasStackTrace ;
39+
40+ @ Override
41+ protected Collection <Class <? extends Plugin >> nodePlugins () {
42+ return CollectionUtils .appendToCopyNoNullElements (super .nodePlugins (), MockTransportService .TestPlugin .class );
43+ }
3944
4045 @ BeforeClass
4146 public static void setDebugLogLevel () {
4247 Configurator .setLevel (SearchService .class , Level .DEBUG );
4348 }
4449
4550 @ Before
46- private void setupMessageListener () {
47- internalCluster ().getDataNodeInstances (TransportService .class ).forEach (ts -> {
48- ts .addMessageListener (new TransportMessageListener () {
49- @ Override
50- public void onResponseSent (long requestId , String action , Exception error ) {
51- TransportMessageListener .super .onResponseSent (requestId , action , error );
52- if (action .startsWith ("indices:data/read/search" )) {
53- Optional <Throwable > throwable = ExceptionsHelper .unwrapCausesAndSuppressed (
54- error ,
55- t -> t .getStackTrace ().length > 0
56- );
57- hasStackTrace .set (throwable .isPresent ());
58- }
59- }
60- });
61- });
51+ public void setupMessageListener () {
52+ hasStackTrace = ErrorTraceHelper .setupErrorTraceListener (internalCluster ());
6253 }
6354
6455 private void setupIndexWithDocs () {
@@ -72,7 +63,6 @@ private void setupIndexWithDocs() {
7263 }
7364
7465 public void testSearchFailingQueryErrorTraceDefault () throws IOException {
75- hasStackTrace = new AtomicBoolean ();
7666 setupIndexWithDocs ();
7767
7868 Request searchRequest = new Request ("POST" , "/_search" );
@@ -87,11 +77,10 @@ public void testSearchFailingQueryErrorTraceDefault() throws IOException {
8777 }
8878 """ );
8979 getRestClient ().performRequest (searchRequest );
90- assertFalse (hasStackTrace .get ());
80+ assertFalse (hasStackTrace .getAsBoolean ());
9181 }
9282
9383 public void testSearchFailingQueryErrorTraceTrue () throws IOException {
94- hasStackTrace = new AtomicBoolean ();
9584 setupIndexWithDocs ();
9685
9786 Request searchRequest = new Request ("POST" , "/_search" );
@@ -107,11 +96,10 @@ public void testSearchFailingQueryErrorTraceTrue() throws IOException {
10796 """ );
10897 searchRequest .addParameter ("error_trace" , "true" );
10998 getRestClient ().performRequest (searchRequest );
110- assertTrue (hasStackTrace .get ());
99+ assertTrue (hasStackTrace .getAsBoolean ());
111100 }
112101
113102 public void testSearchFailingQueryErrorTraceFalse () throws IOException {
114- hasStackTrace = new AtomicBoolean ();
115103 setupIndexWithDocs ();
116104
117105 Request searchRequest = new Request ("POST" , "/_search" );
@@ -127,11 +115,10 @@ public void testSearchFailingQueryErrorTraceFalse() throws IOException {
127115 """ );
128116 searchRequest .addParameter ("error_trace" , "false" );
129117 getRestClient ().performRequest (searchRequest );
130- assertFalse (hasStackTrace .get ());
118+ assertFalse (hasStackTrace .getAsBoolean ());
131119 }
132120
133121 public void testDataNodeDoesNotLogStackTraceWhenErrorTraceTrue () throws IOException {
134- hasStackTrace = new AtomicBoolean ();
135122 setupIndexWithDocs ();
136123
137124 Request searchRequest = new Request ("POST" , "/_search" );
@@ -158,7 +145,6 @@ public void testDataNodeDoesNotLogStackTraceWhenErrorTraceTrue() throws IOExcept
158145 }
159146
160147 public void testDataNodeLogsStackTraceWhenErrorTraceFalseOrEmpty () throws IOException {
161- hasStackTrace = new AtomicBoolean ();
162148 setupIndexWithDocs ();
163149
164150 Request searchRequest = new Request ("POST" , "/_search" );
@@ -188,7 +174,6 @@ public void testDataNodeLogsStackTraceWhenErrorTraceFalseOrEmpty() throws IOExce
188174 }
189175
190176 public void testMultiSearchFailingQueryErrorTraceDefault () throws IOException {
191- hasStackTrace = new AtomicBoolean ();
192177 setupIndexWithDocs ();
193178
194179 XContentType contentType = XContentType .JSON ;
@@ -201,11 +186,10 @@ public void testMultiSearchFailingQueryErrorTraceDefault() throws IOException {
201186 new NByteArrayEntity (requestBody , ContentType .create (contentType .mediaTypeWithoutParameters (), (Charset ) null ))
202187 );
203188 getRestClient ().performRequest (searchRequest );
204- assertFalse (hasStackTrace .get ());
189+ assertFalse (hasStackTrace .getAsBoolean ());
205190 }
206191
207192 public void testMultiSearchFailingQueryErrorTraceTrue () throws IOException {
208- hasStackTrace = new AtomicBoolean ();
209193 setupIndexWithDocs ();
210194
211195 XContentType contentType = XContentType .JSON ;
@@ -219,11 +203,10 @@ public void testMultiSearchFailingQueryErrorTraceTrue() throws IOException {
219203 );
220204 searchRequest .addParameter ("error_trace" , "true" );
221205 getRestClient ().performRequest (searchRequest );
222- assertTrue (hasStackTrace .get ());
206+ assertTrue (hasStackTrace .getAsBoolean ());
223207 }
224208
225209 public void testMultiSearchFailingQueryErrorTraceFalse () throws IOException {
226- hasStackTrace = new AtomicBoolean ();
227210 setupIndexWithDocs ();
228211
229212 XContentType contentType = XContentType .JSON ;
@@ -238,11 +221,10 @@ public void testMultiSearchFailingQueryErrorTraceFalse() throws IOException {
238221 searchRequest .addParameter ("error_trace" , "false" );
239222 getRestClient ().performRequest (searchRequest );
240223
241- assertFalse (hasStackTrace .get ());
224+ assertFalse (hasStackTrace .getAsBoolean ());
242225 }
243226
244227 public void testDataNodeDoesNotLogStackTraceWhenErrorTraceTrueMultiSearch () throws IOException {
245- hasStackTrace = new AtomicBoolean ();
246228 setupIndexWithDocs ();
247229
248230 XContentType contentType = XContentType .JSON ;
@@ -268,7 +250,6 @@ public void testDataNodeDoesNotLogStackTraceWhenErrorTraceTrueMultiSearch() thro
268250 }
269251
270252 public void testDataNodeLogsStackTraceWhenErrorTraceFalseOrEmptyMultiSearch () throws IOException {
271- hasStackTrace = new AtomicBoolean ();
272253 setupIndexWithDocs ();
273254
274255 XContentType contentType = XContentType .JSON ;
0 commit comments