1616import org .elasticsearch .action .search .MultiSearchRequest ;
1717import org .elasticsearch .action .search .SearchRequest ;
1818import org .elasticsearch .client .Request ;
19- import org .elasticsearch .cluster .metadata .IndexMetadata ;
2019import org .elasticsearch .common .settings .Settings ;
2120import org .elasticsearch .common .util .CollectionUtils ;
2221import org .elasticsearch .plugins .Plugin ;
@@ -62,17 +61,14 @@ public void resetSettings() {
6261 updateClusterSettings (Settings .builder ().putNull (SearchService .BATCHED_QUERY_PHASE .getKey ()));
6362 }
6463
65- private int setupIndexWithDocs () {
66- int numShards = numberOfShards ();
67- createIndex ("test1" , Settings .builder ().put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , numShards ).build ());
68- createIndex ("test2" , Settings .builder ().put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , numShards ).build ());
64+ private void setupIndexWithDocs () {
65+ createIndex ("test1" , "test2" );
6966 indexRandom (
7067 true ,
7168 prepareIndex ("test1" ).setId ("1" ).setSource ("field" , "foo" ),
7269 prepareIndex ("test2" ).setId ("10" ).setSource ("field" , 5 )
7370 );
7471 refresh ();
75- return numShards ;
7672 }
7773
7874 public void testSearchFailingQueryErrorTraceDefault () throws IOException {
@@ -131,8 +127,8 @@ public void testSearchFailingQueryErrorTraceFalse() throws IOException {
131127 assertFalse (hasStackTrace .getAsBoolean ());
132128 }
133129
134- public void testNoLoggingInSearchFailingQueryErrorTraceTrue () throws IOException {
135- int numShards = setupIndexWithDocs ();
130+ public void testDataNodeDoesNotLogStackTraceWhenErrorTraceTrue () throws IOException {
131+ setupIndexWithDocs ();
136132
137133 Request searchRequest = new Request ("POST" , "/_search" );
138134 searchRequest .setJsonEntity ("""
@@ -147,6 +143,7 @@ public void testNoLoggingInSearchFailingQueryErrorTraceTrue() throws IOException
147143 """ );
148144
149145 String errorTriggeringIndex = "test2" ;
146+ int numShards = getNumShards (errorTriggeringIndex ).numPrimaries ;
150147 try (var mockLog = MockLog .capture (SearchService .class )) {
151148 ErrorTraceHelper .addUnseenLoggingExpectations (numShards , mockLog , errorTriggeringIndex );
152149
@@ -156,8 +153,8 @@ public void testNoLoggingInSearchFailingQueryErrorTraceTrue() throws IOException
156153 }
157154 }
158155
159- public void testLoggingInSearchFailingQueryErrorTraceFalse () throws IOException {
160- int numShards = setupIndexWithDocs ();
156+ public void testDataNodeLogsStackTraceWhenErrorTraceFalseOrEmpty () throws IOException {
157+ setupIndexWithDocs ();
161158
162159 Request searchRequest = new Request ("POST" , "/_search" );
163160 searchRequest .setJsonEntity ("""
@@ -172,6 +169,7 @@ public void testLoggingInSearchFailingQueryErrorTraceFalse() throws IOException
172169 """ );
173170
174171 String errorTriggeringIndex = "test2" ;
172+ int numShards = getNumShards (errorTriggeringIndex ).numPrimaries ;
175173 try (var mockLog = MockLog .capture (SearchService .class )) {
176174 ErrorTraceHelper .addSeenLoggingExpectations (numShards , mockLog , errorTriggeringIndex );
177175
@@ -235,8 +233,8 @@ public void testMultiSearchFailingQueryErrorTraceFalse() throws IOException {
235233 assertFalse (hasStackTrace .getAsBoolean ());
236234 }
237235
238- public void testLoggingInMultiSearchFailingQueryErrorTraceTrue () throws IOException {
239- int numShards = setupIndexWithDocs ();
236+ public void testDataNodeDoesNotLogStackTraceWhenErrorTraceTrueMultiSearch () throws IOException {
237+ setupIndexWithDocs ();
240238
241239 XContentType contentType = XContentType .JSON ;
242240 MultiSearchRequest multiSearchRequest = new MultiSearchRequest ().add (
@@ -251,6 +249,7 @@ public void testLoggingInMultiSearchFailingQueryErrorTraceTrue() throws IOExcept
251249 searchRequest .addParameter ("error_trace" , "true" );
252250
253251 String errorTriggeringIndex = "test2" ;
252+ int numShards = getNumShards (errorTriggeringIndex ).numPrimaries ;
254253 try (var mockLog = MockLog .capture (SearchService .class )) {
255254 ErrorTraceHelper .addUnseenLoggingExpectations (numShards , mockLog , errorTriggeringIndex );
256255
@@ -259,8 +258,8 @@ public void testLoggingInMultiSearchFailingQueryErrorTraceTrue() throws IOExcept
259258 }
260259 }
261260
262- public void testLoggingInMultiSearchFailingQueryErrorTraceFalse () throws IOException {
263- int numShards = setupIndexWithDocs ();
261+ public void testDataNodeLogsStackTraceWhenErrorTraceFalseOrEmptyMultiSearch () throws IOException {
262+ setupIndexWithDocs ();
264263
265264 XContentType contentType = XContentType .JSON ;
266265 MultiSearchRequest multiSearchRequest = new MultiSearchRequest ().add (
@@ -278,6 +277,7 @@ public void testLoggingInMultiSearchFailingQueryErrorTraceFalse() throws IOExcep
278277 }
279278
280279 String errorTriggeringIndex = "test2" ;
280+ int numShards = getNumShards (errorTriggeringIndex ).numPrimaries ;
281281 try (var mockLog = MockLog .capture (SearchService .class )) {
282282 ErrorTraceHelper .addSeenLoggingExpectations (numShards , mockLog , errorTriggeringIndex );
283283
0 commit comments