|
26 | 26 | import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
27 | 27 | import org.elasticsearch.common.util.Maps;
|
28 | 28 | import org.elasticsearch.common.util.concurrent.AtomicArray;
|
| 29 | +import org.elasticsearch.common.util.concurrent.ListenableFuture; |
29 | 30 | import org.elasticsearch.core.Releasable;
|
30 |
| -import org.elasticsearch.core.Releasables; |
31 | 31 | import org.elasticsearch.index.shard.ShardId;
|
32 | 32 | import org.elasticsearch.search.SearchContextMissingException;
|
33 | 33 | import org.elasticsearch.search.SearchPhaseResult;
|
@@ -102,7 +102,7 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
|
102 | 102 | private final AtomicBoolean requestCancelled = new AtomicBoolean();
|
103 | 103 |
|
104 | 104 | // protected for tests
|
105 |
| - protected final List<Releasable> releasables = new ArrayList<>(); |
| 105 | + protected final ListenableFuture<Void> doneFuture = new ListenableFuture<>(); |
106 | 106 |
|
107 | 107 | AbstractSearchAsyncAction(
|
108 | 108 | String name,
|
@@ -151,7 +151,7 @@ abstract class AbstractSearchAsyncAction<Result extends SearchPhaseResult> exten
|
151 | 151 | this.executor = executor;
|
152 | 152 | this.request = request;
|
153 | 153 | this.task = task;
|
154 |
| - this.listener = ActionListener.runAfter(listener, () -> Releasables.close(releasables)); |
| 154 | + this.listener = ActionListener.runBefore(listener, () -> doneFuture.onResponse(null)); |
155 | 155 | this.nodeIdToConnection = nodeIdToConnection;
|
156 | 156 | this.concreteIndexBoosts = concreteIndexBoosts;
|
157 | 157 | this.clusterStateVersion = clusterState.version();
|
@@ -182,7 +182,12 @@ protected void notifyListShards(
|
182 | 182 | * Registers a {@link Releasable} that will be closed when the search request finishes or fails.
|
183 | 183 | */
|
184 | 184 | public void addReleasable(Releasable releasable) {
|
185 |
| - releasables.add(releasable); |
| 185 | + var doneFuture = this.doneFuture; |
| 186 | + if (doneFuture.isDone()) { |
| 187 | + releasable.close(); |
| 188 | + } else { |
| 189 | + doneFuture.addListener(ActionListener.releasing((releasable))); |
| 190 | + } |
186 | 191 | }
|
187 | 192 |
|
188 | 193 | /**
|
|
0 commit comments