|
9 | 9 |
|
10 | 10 | import org.apache.logging.log4j.LogManager; |
11 | 11 | import org.apache.logging.log4j.Logger; |
12 | | -import org.elasticsearch.ResourceNotFoundException; |
13 | 12 | import org.elasticsearch.action.ActionListener; |
14 | 13 | import org.elasticsearch.action.ActionListenerResponseHandler; |
15 | 14 | import org.elasticsearch.action.support.ActionFilters; |
|
18 | 17 | import org.elasticsearch.cluster.node.DiscoveryNode; |
19 | 18 | import org.elasticsearch.cluster.service.ClusterService; |
20 | 19 | import org.elasticsearch.common.util.concurrent.EsExecutors; |
21 | | -import org.elasticsearch.compute.EsqlRefCountingListener; |
22 | 20 | import org.elasticsearch.compute.data.BlockFactory; |
23 | 21 | import org.elasticsearch.compute.operator.exchange.ExchangeService; |
24 | 22 | import org.elasticsearch.injection.guice.Inject; |
|
33 | 31 | import org.elasticsearch.xpack.core.async.GetAsyncResultRequest; |
34 | 32 | import org.elasticsearch.xpack.core.security.SecurityContext; |
35 | 33 | import org.elasticsearch.xpack.esql.action.EsqlAsyncStopAction; |
| 34 | +import org.elasticsearch.xpack.esql.action.EsqlExecutionInfo; |
36 | 35 | import org.elasticsearch.xpack.esql.action.EsqlQueryResponse; |
37 | 36 | import org.elasticsearch.xpack.esql.action.EsqlQueryTask; |
38 | 37 |
|
39 | 38 | import java.io.IOException; |
40 | | -import java.util.concurrent.atomic.AtomicReference; |
41 | 39 |
|
42 | 40 | import static org.elasticsearch.xpack.core.ClientHelper.ASYNC_SEARCH_ORIGIN; |
43 | 41 |
|
@@ -108,36 +106,26 @@ private String sessionID(AsyncExecutionId asyncId) { |
108 | 106 |
|
109 | 107 | private void stopQueryAndReturnResult(Task task, AsyncExecutionId asyncId, ActionListener<EsqlQueryResponse> listener) { |
110 | 108 | String asyncIdStr = asyncId.getEncoded(); |
111 | | - TransportEsqlQueryAction.EsqlQueryListener asyncListener = queryAction.getAsyncListener(asyncIdStr); |
112 | 109 | EsqlQueryTask asyncTask = getEsqlQueryTask(asyncId); |
113 | | - if (asyncListener == null) { |
114 | | - logger.debug("Async stop for task {}, no listener - collecting the result", asyncIdStr); |
| 110 | + GetAsyncResultRequest getAsyncResultRequest = new GetAsyncResultRequest(asyncIdStr); |
| 111 | + if (asyncTask == null) { |
115 | 112 | // This should mean one of the two things: either bad request ID, or the query has already finished |
116 | 113 | // In both cases, let regular async get deal with it. |
117 | | - var getAsyncResultRequest = new GetAsyncResultRequest(asyncIdStr); |
118 | | - Runnable getResults = () -> getResultsAction.execute(task, getAsyncResultRequest, listener); |
119 | | - // If the listener is not present but the task is still alive, this may mean it's not finished writing the response |
120 | | - // We will wait for the task to be done and then collect the results. |
121 | | - if (asyncTask == null || asyncTask.addCompletionListener(() -> ActionListener.running(getResults)) == false) { |
122 | | - getResults.run(); |
123 | | - } |
| 114 | + logger.debug("Async stop for task {}, no task present - passing to GetAsyncResultRequest", asyncIdStr); |
| 115 | + getResultsAction.execute(task, getAsyncResultRequest, listener); |
124 | 116 | return; |
125 | 117 | } |
126 | | - |
127 | | - if (asyncTask == null) { |
128 | | - throw new ResourceNotFoundException(asyncId + " not found"); |
129 | | - } |
130 | 118 | logger.debug("Async stop for task {} - stopping", asyncIdStr); |
131 | | - // Here we will wait for both the response to become available and for the finish operation to complete |
132 | | - var responseHolder = new AtomicReference<EsqlQueryResponse>(); |
133 | | - try (var refs = new EsqlRefCountingListener(listener.map(unused -> responseHolder.get()))) { |
134 | | - asyncListener.addListener(refs.acquire().map(r -> { |
135 | | - responseHolder.set(r); |
136 | | - return null; |
137 | | - })); |
138 | | - asyncListener.markAsPartial(); |
139 | | - exchangeService.finishSessionEarly(sessionID(asyncId), refs.acquire()); |
| 119 | + final EsqlExecutionInfo esqlExecutionInfo = asyncTask.executionInfo(); |
| 120 | + if (esqlExecutionInfo != null) { |
| 121 | + esqlExecutionInfo.markAsPartial(); |
140 | 122 | } |
| 123 | + Runnable getResults = () -> getResultsAction.execute(task, getAsyncResultRequest, listener); |
| 124 | + exchangeService.finishSessionEarly(sessionID(asyncId), ActionListener.running(() -> { |
| 125 | + if (asyncTask.addCompletionListener(() -> ActionListener.running(getResults)) == false) { |
| 126 | + getResults.run(); |
| 127 | + } |
| 128 | + })); |
141 | 129 | } |
142 | 130 |
|
143 | 131 | private EsqlQueryTask getEsqlQueryTask(AsyncExecutionId asyncId) { |
|
0 commit comments