|
12 | 12 | import org.elasticsearch.action.ActionListenerResponseHandler; |
13 | 13 | import org.elasticsearch.action.support.ActionFilters; |
14 | 14 | import org.elasticsearch.action.support.HandledTransportAction; |
| 15 | +import org.elasticsearch.action.support.RefCountingListener; |
15 | 16 | import org.elasticsearch.client.internal.Client; |
16 | 17 | import org.elasticsearch.cluster.node.DiscoveryNode; |
17 | 18 | import org.elasticsearch.cluster.service.ClusterService; |
18 | 19 | import org.elasticsearch.common.util.concurrent.EsExecutors; |
| 20 | +import org.elasticsearch.compute.EsqlRefCountingListener; |
19 | 21 | import org.elasticsearch.compute.data.BlockFactory; |
20 | 22 | import org.elasticsearch.compute.operator.exchange.ExchangeService; |
21 | 23 | import org.elasticsearch.core.TimeValue; |
|
36 | 38 |
|
37 | 39 | import java.io.IOException; |
38 | 40 | import java.util.concurrent.TimeUnit; |
| 41 | +import java.util.concurrent.atomic.AtomicReference; |
39 | 42 |
|
40 | 43 | import static org.elasticsearch.xpack.core.ClientHelper.ASYNC_SEARCH_ORIGIN; |
41 | 44 |
|
@@ -123,7 +126,15 @@ private void stopQueryAndReturnResult(Task task, AsyncExecutionId asyncId, Actio |
123 | 126 | } catch (IOException e) { |
124 | 127 | throw new ResourceNotFoundException(asyncId + " not found", e); |
125 | 128 | } |
126 | | - asyncListener.addListener(listener); |
127 | | - exchangeService.finishSessionEarly(sessionID(asyncId), ActionListener.noop()); |
| 129 | + // Here we will wait for both the response to become available and for the finish operation to complete |
| 130 | + var responseHolder = new AtomicReference<EsqlQueryResponse>(); |
| 131 | + ActionListener<Void> resultListener = listener.delegateFailureIgnoreResponseAndWrap(l -> l.onResponse(responseHolder.get())); |
| 132 | + try (var refs = new EsqlRefCountingListener(resultListener)) { |
| 133 | + asyncListener.addListener(refs.acquire().map(r -> { |
| 134 | + responseHolder.set(r); |
| 135 | + return null; |
| 136 | + })); |
| 137 | + exchangeService.finishSessionEarly(sessionID(asyncId), refs.acquire()); |
| 138 | + } |
128 | 139 | } |
129 | 140 | } |
0 commit comments