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