@@ -67,7 +67,7 @@ public void testUnauthorizedIndices() throws IOException {
67
67
var getResponse = runAsyncGet ("user1" , id ); // sanity
68
68
assertOK (getResponse );
69
69
ResponseException error ;
70
- error = expectThrows (ResponseException .class , () -> runAsyncGet ("user2" , id ));
70
+ error = expectThrows (ResponseException .class , () -> runAsyncGet ("user2" , id , true ));
71
71
// resource not found exception if the authenticated user is not the creator of the original task
72
72
assertThat (error .getResponse ().getStatusLine ().getStatusCode (), equalTo (404 ));
73
73
@@ -85,7 +85,7 @@ public void testUnauthorizedIndices() throws IOException {
85
85
var getResponse = runAsyncGet ("user2" , id ); // sanity
86
86
assertOK (getResponse );
87
87
ResponseException error ;
88
- error = expectThrows (ResponseException .class , () -> runAsyncGet ("user1" , id ));
88
+ error = expectThrows (ResponseException .class , () -> runAsyncGet ("user1" , id , true ));
89
89
assertThat (error .getResponse ().getStatusLine ().getStatusCode (), equalTo (404 ));
90
90
91
91
error = expectThrows (ResponseException .class , () -> runAsyncDelete ("user1" , id ));
@@ -117,6 +117,10 @@ private Response runAsync(String user, String command) throws IOException {
117
117
}
118
118
119
119
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 {
120
124
int tries = 0 ;
121
125
while (tries < 10 ) {
122
126
// Sometimes we get 404s fetching the task status.
@@ -129,22 +133,32 @@ private Response runAsyncGet(String user, String id) throws IOException {
129
133
logResponse (response );
130
134
return response ;
131
135
} 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.
138
142
logger .warn ("async-search index does not exist" , e );
139
143
try {
140
144
Thread .sleep (1000 );
141
145
} catch (InterruptedException ex ) {
142
146
throw new RuntimeException (ex );
143
147
}
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
+ }
144
157
} else {
145
158
throw e ;
146
159
}
147
160
tries ++;
161
+ logger .warn ("retry [" + tries + "] for GET /_query/async/" + id );
148
162
}
149
163
}
150
164
throw new IllegalStateException ("couldn't find task status" );
0 commit comments