|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.esql; |
9 | 9 |
|
| 10 | +import org.apache.http.util.EntityUtils; |
10 | 11 | import org.elasticsearch.client.Request; |
11 | 12 | import org.elasticsearch.client.RequestOptions; |
12 | 13 | import org.elasticsearch.client.Response; |
@@ -36,7 +37,8 @@ protected Response runESQLCommand(String user, String command) throws IOExceptio |
36 | 37 | var respMap = entityAsMap(response.getEntity()); |
37 | 38 | String id = (String) respMap.get("id"); |
38 | 39 | assertThat((boolean) respMap.get("is_running"), either(is(true)).or(is(false))); |
39 | | - var getResponse = runAsyncGet(user, id); |
| 40 | + int tries = 0; |
| 41 | + Response getResponse = runAsyncGet(user, id); |
40 | 42 | assertOK(getResponse); |
41 | 43 | var deleteResponse = runAsyncDelete(user, id); |
42 | 44 | assertOK(deleteResponse); |
@@ -98,26 +100,53 @@ private Response runAsync(String user, String command) throws IOException { |
98 | 100 | Request request = new Request("POST", "_query/async"); |
99 | 101 | request.setJsonEntity(Strings.toString(json)); |
100 | 102 | request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", user)); |
| 103 | + request.addParameter("error_trace", "true"); |
101 | 104 | logRequest(request); |
102 | 105 | Response response = client().performRequest(request); |
103 | 106 | logResponse(response); |
104 | 107 | return response; |
105 | 108 | } |
106 | 109 |
|
107 | 110 | private Response runAsyncGet(String user, String id) throws IOException { |
108 | | - Request getRequest = new Request("GET", "_query/async/" + id + "?wait_for_completion_timeout=60s"); |
109 | | - getRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", user)); |
110 | | - logRequest(getRequest); |
111 | | - var response = client().performRequest(getRequest); |
112 | | - logResponse(response); |
113 | | - return response; |
| 111 | + int tries = 0; |
| 112 | + while (tries < 10) { |
| 113 | + // Sometimes we get 404s fetching the task status. |
| 114 | + try { |
| 115 | + Request getRequest = new Request("GET", "_query/async/" + id + "?wait_for_completion_timeout=60s"); |
| 116 | + getRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", user)); |
| 117 | + getRequest.addParameter("error_trace", "true"); |
| 118 | + logRequest(getRequest); |
| 119 | + var response = client().performRequest(getRequest); |
| 120 | + logResponse(response); |
| 121 | + return response; |
| 122 | + } catch (ResponseException e) { |
| 123 | + if (e.getResponse().getStatusLine().getStatusCode() == 404 |
| 124 | + && EntityUtils.toString(e.getResponse().getEntity()).contains("no such index [.async-search]")) { |
| 125 | + /* |
| 126 | + * Work around https://github.com/elastic/elasticsearch/issues/110304 - the .async-search |
| 127 | + * index may not exist when we try the fetch, but it should exist on next attempt. |
| 128 | + */ |
| 129 | + logger.warn("async-search index does not exist", e); |
| 130 | + try { |
| 131 | + Thread.sleep(1000); |
| 132 | + } catch (InterruptedException ex) { |
| 133 | + throw new RuntimeException(ex); |
| 134 | + } |
| 135 | + } else { |
| 136 | + throw e; |
| 137 | + } |
| 138 | + tries++; |
| 139 | + } |
| 140 | + } |
| 141 | + throw new IllegalStateException("couldn't find task status"); |
114 | 142 | } |
115 | 143 |
|
116 | 144 | private Response runAsyncDelete(String user, String id) throws IOException { |
117 | | - Request getRequest = new Request("DELETE", "_query/async/" + id); |
118 | | - getRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", user)); |
119 | | - logRequest(getRequest); |
120 | | - var response = client().performRequest(getRequest); |
| 145 | + Request deleteRequest = new Request("DELETE", "_query/async/" + id); |
| 146 | + deleteRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", user)); |
| 147 | + deleteRequest.addParameter("error_trace", "true"); |
| 148 | + logRequest(deleteRequest); |
| 149 | + var response = client().performRequest(deleteRequest); |
121 | 150 | logResponse(response); |
122 | 151 | return response; |
123 | 152 | } |
|
0 commit comments