From 07d348226391b049ac51a70b201c57dfbdcbeb1d Mon Sep 17 00:00:00 2001 From: Dimitris Rempapis Date: Fri, 7 Nov 2025 11:58:47 +0200 Subject: [PATCH 1/5] optimize test class --- .../xpack/search/AsyncSearchErrorTraceIT.java | 167 ++++++++++++------ 1 file changed, 117 insertions(+), 50 deletions(-) diff --git a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java index 83e7cf9e12096..1ab1399711492 100644 --- a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java +++ b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java @@ -7,6 +7,8 @@ package org.elasticsearch.xpack.search; +import org.apache.http.HttpEntity; +import org.apache.http.util.EntityUtils; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.config.Configurator; import org.elasticsearch.client.Request; @@ -16,7 +18,6 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.ErrorTraceHelper; import org.elasticsearch.search.SearchService; -import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.MockLog; import org.elasticsearch.test.junit.annotations.TestLogging; import org.elasticsearch.test.transport.MockTransportService; @@ -26,12 +27,13 @@ import java.io.IOException; import java.util.Collection; import java.util.Map; +import java.util.concurrent.ExecutionException; @TestLogging( reason = "testing debug log output to identify race condition", value = "org.elasticsearch.xpack.search.MutableSearchResponse:DEBUG,org.elasticsearch.xpack.search.AsyncSearchTask:DEBUG" ) -public class AsyncSearchErrorTraceIT extends ESIntegTestCase { +public class AsyncSearchErrorTraceIT extends AsyncSearchIntegTestCase { @Override protected boolean addMockHttpTransport() { @@ -76,13 +78,19 @@ public void testAsyncSearchFailingQueryErrorTraceDefault() throws Exception { createAsyncRequest.addParameter("keep_on_completion", "true"); createAsyncRequest.addParameter("wait_for_completion_timeout", "0ms"); Map createAsyncResponseEntity = performRequestAndGetResponseEntity(createAsyncRequest); - if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { - String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); - Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); - awaitAsyncRequestDoneRunning(getAsyncRequest); + + try { + if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { + String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); + Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); + awaitAsyncRequestDoneRunning(getAsyncRequest); + } + + // check that the stack trace was not sent from the data node to the coordinating node + ErrorTraceHelper.assertStackTraceCleared(internalCluster()); + } finally { + deleteAsyncSearchIfPresent(createAsyncResponseEntity); } - // check that the stack trace was not sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceCleared(internalCluster()); } public void testAsyncSearchFailingQueryErrorTraceTrue() throws Exception { @@ -103,14 +111,20 @@ public void testAsyncSearchFailingQueryErrorTraceTrue() throws Exception { createAsyncRequest.addParameter("keep_on_completion", "true"); createAsyncRequest.addParameter("wait_for_completion_timeout", "0ms"); Map createAsyncResponseEntity = performRequestAndGetResponseEntity(createAsyncRequest); - if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { - String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); - Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); - getAsyncRequest.addParameter("error_trace", "true"); - awaitAsyncRequestDoneRunning(getAsyncRequest); + + try { + if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { + String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); + Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); + getAsyncRequest.addParameter("error_trace", "true"); + awaitAsyncRequestDoneRunning(getAsyncRequest); + } + + // check that the stack trace was sent from the data node to the coordinating node + ErrorTraceHelper.assertStackTraceObserved(internalCluster()); + } finally { + deleteAsyncSearchIfPresent(createAsyncResponseEntity); } - // check that the stack trace was sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceObserved(internalCluster()); } public void testAsyncSearchFailingQueryErrorTraceFalse() throws Exception { @@ -131,14 +145,20 @@ public void testAsyncSearchFailingQueryErrorTraceFalse() throws Exception { createAsyncRequest.addParameter("keep_on_completion", "true"); createAsyncRequest.addParameter("wait_for_completion_timeout", "0ms"); Map createAsyncResponseEntity = performRequestAndGetResponseEntity(createAsyncRequest); - if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { - String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); - Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); - getAsyncRequest.addParameter("error_trace", "false"); - awaitAsyncRequestDoneRunning(getAsyncRequest); + + try { + if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { + String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); + Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); + getAsyncRequest.addParameter("error_trace", "false"); + awaitAsyncRequestDoneRunning(getAsyncRequest); + } + + // check that the stack trace was not sent from the data node to the coordinating node + ErrorTraceHelper.assertStackTraceCleared(internalCluster()); + } finally { + deleteAsyncSearchIfPresent(createAsyncResponseEntity); } - // check that the stack trace was not sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceCleared(internalCluster()); } public void testDataNodeLogsStackTrace() throws Exception { @@ -172,19 +192,24 @@ public void testDataNodeLogsStackTrace() throws Exception { try (var mockLog = MockLog.capture(SearchService.class)) { ErrorTraceHelper.addSeenLoggingExpectations(numShards, mockLog, errorTriggeringIndex); Map createAsyncResponseEntity = performRequestAndGetResponseEntity(createAsyncRequest); - if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { - String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); - Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); - // Use the same value of error_trace as the search request - if (errorTraceValue == 0) { - getAsyncRequest.addParameter("error_trace", "true"); - } else if (errorTraceValue == 1) { - getAsyncRequest.addParameter("error_trace", "false"); - } // else empty - awaitAsyncRequestDoneRunning(getAsyncRequest); - } - mockLog.assertAllExpectationsMatched(); + try { + if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { + String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); + Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); + // Use the same value of error_trace as the search request + if (errorTraceValue == 0) { + getAsyncRequest.addParameter("error_trace", "true"); + } else if (errorTraceValue == 1) { + getAsyncRequest.addParameter("error_trace", "false"); + } // else empty + awaitAsyncRequestDoneRunning(getAsyncRequest); + } + + mockLog.assertAllExpectationsMatched(); + } finally { + deleteAsyncSearchIfPresent(createAsyncResponseEntity); + } } } @@ -206,14 +231,20 @@ public void testAsyncSearchFailingQueryErrorTraceFalseOnSubmitAndTrueOnGet() thr createAsyncSearchRequest.addParameter("keep_on_completion", "true"); createAsyncSearchRequest.addParameter("wait_for_completion_timeout", "0ms"); Map createAsyncResponseEntity = performRequestAndGetResponseEntity(createAsyncSearchRequest); - if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { - String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); - Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); - getAsyncRequest.addParameter("error_trace", "true"); - awaitAsyncRequestDoneRunning(getAsyncRequest); + + try { + if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { + String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); + Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); + getAsyncRequest.addParameter("error_trace", "true"); + awaitAsyncRequestDoneRunning(getAsyncRequest); + } + + // check that the stack trace was not sent from the data node to the coordinating node + ErrorTraceHelper.assertStackTraceCleared(internalCluster()); + } finally { + deleteAsyncSearchIfPresent(createAsyncResponseEntity); } - // check that the stack trace was not sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceCleared(internalCluster()); } public void testAsyncSearchFailingQueryErrorTraceTrueOnSubmitAndFalseOnGet() throws Exception { @@ -234,20 +265,56 @@ public void testAsyncSearchFailingQueryErrorTraceTrueOnSubmitAndFalseOnGet() thr createAsyncSearchRequest.addParameter("keep_on_completion", "true"); createAsyncSearchRequest.addParameter("wait_for_completion_timeout", "0ms"); Map createAsyncResponseEntity = performRequestAndGetResponseEntity(createAsyncSearchRequest); - if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { - String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); - Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); - getAsyncRequest.addParameter("error_trace", "false"); - awaitAsyncRequestDoneRunning(getAsyncRequest); + try { + if (Boolean.TRUE.equals(createAsyncResponseEntity.get("is_running"))) { + String asyncExecutionId = (String) createAsyncResponseEntity.get("id"); + Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); + getAsyncRequest.addParameter("error_trace", "false"); + awaitAsyncRequestDoneRunning(getAsyncRequest); + } + + // check that the stack trace was sent from the data node to the coordinating node + ErrorTraceHelper.assertStackTraceObserved(internalCluster()); + } finally { + deleteAsyncSearchIfPresent(createAsyncResponseEntity); } - // check that the stack trace was sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceObserved(internalCluster()); } private Map performRequestAndGetResponseEntity(Request r) throws IOException { Response response = getRestClient().performRequest(r); - XContentType entityContentType = XContentType.fromMediaType(response.getEntity().getContentType().getValue()); - return XContentHelper.convertToMap(entityContentType.xContent(), response.getEntity().getContent(), false); + + HttpEntity entity = response.getEntity(); + if (entity == null) { + return Map.of(); + } + + try { + XContentType entityContentType = XContentType.fromMediaType(entity.getContentType().getValue()); + return XContentHelper.convertToMap(entityContentType.xContent(), entity.getContent(), false); + } finally { + // Make sure the connection is released + EntityUtils.consumeQuietly(entity); + } + } + + private void deleteAsyncSearchIfPresent(Map map) throws IOException { + String id = (String) map.get("id"); + if(id != null) { + return; + } + + // Make sure the .async-search system index is green before deleting it + try { + ensureGreen(".async-search"); + } catch (Exception ignore) { + // the index may not exist + } + + Response response = getRestClient().performRequest(new Request("DELETE", "/_async_search/" + id)); + HttpEntity entity = response.getEntity(); + if (entity != null) { + EntityUtils.consumeQuietly(entity); + } } private void awaitAsyncRequestDoneRunning(Request getAsyncRequest) throws Exception { From 3eeb38299c266186249cec1ae7568e775822b73b Mon Sep 17 00:00:00 2001 From: elasticsearchmachine Date: Fri, 7 Nov 2025 10:24:03 +0000 Subject: [PATCH 2/5] [CI] Auto commit changes from spotless --- .../elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java index 1ab1399711492..c9a7d3f54ae00 100644 --- a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java +++ b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java @@ -27,7 +27,6 @@ import java.io.IOException; import java.util.Collection; import java.util.Map; -import java.util.concurrent.ExecutionException; @TestLogging( reason = "testing debug log output to identify race condition", @@ -299,7 +298,7 @@ private Map performRequestAndGetResponseEntity(Request r) throws private void deleteAsyncSearchIfPresent(Map map) throws IOException { String id = (String) map.get("id"); - if(id != null) { + if (id != null) { return; } From 7a196affecddf71af575eb9448e22b1fc3e20619 Mon Sep 17 00:00:00 2001 From: Dimitris Rempapis Date: Wed, 12 Nov 2025 10:50:18 +0200 Subject: [PATCH 3/5] update after review --- .../xpack/search/AsyncSearchErrorTraceIT.java | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java index 1ab1399711492..9011aab0c51c8 100644 --- a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java +++ b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java @@ -27,7 +27,6 @@ import java.io.IOException; import java.util.Collection; import java.util.Map; -import java.util.concurrent.ExecutionException; @TestLogging( reason = "testing debug log output to identify race condition", @@ -282,24 +281,13 @@ public void testAsyncSearchFailingQueryErrorTraceTrueOnSubmitAndFalseOnGet() thr private Map performRequestAndGetResponseEntity(Request r) throws IOException { Response response = getRestClient().performRequest(r); - - HttpEntity entity = response.getEntity(); - if (entity == null) { - return Map.of(); - } - - try { - XContentType entityContentType = XContentType.fromMediaType(entity.getContentType().getValue()); - return XContentHelper.convertToMap(entityContentType.xContent(), entity.getContent(), false); - } finally { - // Make sure the connection is released - EntityUtils.consumeQuietly(entity); - } + XContentType entityContentType = XContentType.fromMediaType(response.getEntity().getContentType().getValue()); + return XContentHelper.convertToMap(entityContentType.xContent(), response.getEntity().getContent(), false); } private void deleteAsyncSearchIfPresent(Map map) throws IOException { String id = (String) map.get("id"); - if(id != null) { + if(id == null) { return; } From a7d09d4fa15ed36b8eb27a8d5e9543aa980ddc84 Mon Sep 17 00:00:00 2001 From: elasticsearchmachine Date: Wed, 12 Nov 2025 08:57:47 +0000 Subject: [PATCH 4/5] [CI] Auto commit changes from spotless --- .../org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java index 4e88cd1192070..8482ca9a96e93 100644 --- a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java +++ b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java @@ -292,7 +292,7 @@ private Map performRequestAndGetResponseEntity(Request r) throws private void deleteAsyncSearchIfPresent(Map map) throws IOException { String id = (String) map.get("id"); - if(id == null) { + if (id == null) { return; } From 470111d15fc4b70aab6b5b9791986b2aa3d36157 Mon Sep 17 00:00:00 2001 From: Dimitris Rempapis Date: Wed, 12 Nov 2025 10:57:59 +0200 Subject: [PATCH 5/5] update after review --- .../xpack/search/AsyncSearchErrorTraceIT.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java index 4e88cd1192070..77d0b436bb9d9 100644 --- a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java +++ b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchErrorTraceIT.java @@ -85,9 +85,6 @@ public void testAsyncSearchFailingQueryErrorTraceDefault() throws Exception { Request getAsyncRequest = new Request("GET", "/_async_search/" + asyncExecutionId); awaitAsyncRequestDoneRunning(getAsyncRequest); } - - // check that the stack trace was not sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceCleared(internalCluster()); } finally { deleteAsyncSearchIfPresent(createAsyncResponseEntity); } @@ -120,9 +117,6 @@ public void testAsyncSearchFailingQueryErrorTraceTrue() throws Exception { getAsyncRequest.addParameter("error_trace", "true"); awaitAsyncRequestDoneRunning(getAsyncRequest); } - - // check that the stack trace was sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceObserved(internalCluster()); } finally { deleteAsyncSearchIfPresent(createAsyncResponseEntity); } @@ -155,9 +149,6 @@ public void testAsyncSearchFailingQueryErrorTraceFalse() throws Exception { getAsyncRequest.addParameter("error_trace", "false"); awaitAsyncRequestDoneRunning(getAsyncRequest); } - - // check that the stack trace was not sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceCleared(internalCluster()); } finally { deleteAsyncSearchIfPresent(createAsyncResponseEntity); } @@ -242,9 +233,6 @@ public void testAsyncSearchFailingQueryErrorTraceFalseOnSubmitAndTrueOnGet() thr getAsyncRequest.addParameter("error_trace", "true"); awaitAsyncRequestDoneRunning(getAsyncRequest); } - - // check that the stack trace was not sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceCleared(internalCluster()); } finally { deleteAsyncSearchIfPresent(createAsyncResponseEntity); } @@ -276,9 +264,6 @@ public void testAsyncSearchFailingQueryErrorTraceTrueOnSubmitAndFalseOnGet() thr getAsyncRequest.addParameter("error_trace", "false"); awaitAsyncRequestDoneRunning(getAsyncRequest); } - - // check that the stack trace was sent from the data node to the coordinating node - ErrorTraceHelper.assertStackTraceObserved(internalCluster()); } finally { deleteAsyncSearchIfPresent(createAsyncResponseEntity); }