Skip to content

Commit 42712ba

Browse files
astefancbuescher
authored andcommitted
ES|QL: EsqlAsyncSecurityIT workaround for lazy .async-search indexing (elastic#112287)
1 parent c2e9bcf commit 42712ba

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ tests:
137137
- class: org.elasticsearch.xpack.ml.integration.MlJobIT
138138
method: testDeleteJobAfterMissingIndex
139139
issue: https://github.com/elastic/elasticsearch/issues/112088
140-
- class: org.elasticsearch.xpack.esql.EsqlAsyncSecurityIT
141-
method: testLimitedPrivilege
142-
issue: https://github.com/elastic/elasticsearch/issues/112110
143140
- class: org.elasticsearch.xpack.esql.qa.mixed.MixedClusterEsqlSpecIT
144141
method: test {stats.ByTwoCalculatedSecondOverwrites SYNC}
145142
issue: https://github.com/elastic/elasticsearch/issues/112117

x-pack/plugin/esql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/esql/EsqlAsyncSecurityIT.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testUnauthorizedIndices() throws IOException {
6767
var getResponse = runAsyncGet("user1", id); // sanity
6868
assertOK(getResponse);
6969
ResponseException error;
70-
error = expectThrows(ResponseException.class, () -> runAsyncGet("user2", id));
70+
error = expectThrows(ResponseException.class, () -> runAsyncGet("user2", id, true));
7171
// resource not found exception if the authenticated user is not the creator of the original task
7272
assertThat(error.getResponse().getStatusLine().getStatusCode(), equalTo(404));
7373

@@ -85,7 +85,7 @@ public void testUnauthorizedIndices() throws IOException {
8585
var getResponse = runAsyncGet("user2", id); // sanity
8686
assertOK(getResponse);
8787
ResponseException error;
88-
error = expectThrows(ResponseException.class, () -> runAsyncGet("user1", id));
88+
error = expectThrows(ResponseException.class, () -> runAsyncGet("user1", id, true));
8989
assertThat(error.getResponse().getStatusLine().getStatusCode(), equalTo(404));
9090

9191
error = expectThrows(ResponseException.class, () -> runAsyncDelete("user1", id));
@@ -117,6 +117,10 @@ private Response runAsync(String user, String command) throws IOException {
117117
}
118118

119119
private Response runAsyncGet(String user, String id) throws IOException {
120+
return runAsyncGet(user, id, false);
121+
}
122+
123+
private Response runAsyncGet(String user, String id, boolean isAsyncIdNotFound_Expected) throws IOException {
120124
int tries = 0;
121125
while (tries < 10) {
122126
// Sometimes we get 404s fetching the task status.
@@ -129,22 +133,32 @@ private Response runAsyncGet(String user, String id) throws IOException {
129133
logResponse(response);
130134
return response;
131135
} catch (ResponseException e) {
132-
if (e.getResponse().getStatusLine().getStatusCode() == 404
133-
&& EntityUtils.toString(e.getResponse().getEntity()).contains("no such index [.async-search]")) {
134-
/*
135-
* Work around https://github.com/elastic/elasticsearch/issues/110304 - the .async-search
136-
* index may not exist when we try the fetch, but it should exist on next attempt.
137-
*/
136+
var statusCode = e.getResponse().getStatusLine().getStatusCode();
137+
var message = EntityUtils.toString(e.getResponse().getEntity());
138+
139+
if (statusCode == 404 && message.contains("no such index [.async-search]")) {
140+
// Work around https://github.com/elastic/elasticsearch/issues/110304 - the .async-search
141+
// index may not exist when we try the fetch, but it should exist on next attempt.
138142
logger.warn("async-search index does not exist", e);
139143
try {
140144
Thread.sleep(1000);
141145
} catch (InterruptedException ex) {
142146
throw new RuntimeException(ex);
143147
}
148+
} else if (statusCode == 404 && false == isAsyncIdNotFound_Expected && message.contains("resource_not_found_exception")) {
149+
// Work around for https://github.com/elastic/elasticsearch/issues/112110
150+
// The async id is not indexed quickly enough in .async-search index for us to retrieve it.
151+
logger.warn("async id not found", e);
152+
try {
153+
Thread.sleep(500);
154+
} catch (InterruptedException ex) {
155+
throw new RuntimeException(ex);
156+
}
144157
} else {
145158
throw e;
146159
}
147160
tries++;
161+
logger.warn("retry [" + tries + "] for GET /_query/async/" + id);
148162
}
149163
}
150164
throw new IllegalStateException("couldn't find task status");

0 commit comments

Comments
 (0)