Skip to content

Commit e622195

Browse files
Make SearchResponseSections Releasable instead of RefCounted (elastic#116404) (elastic#119707)
This is not used as ref-counted, we never increment the count so we can simplify things a little and make it just a releasable.
1 parent 12765de commit e622195

File tree

6 files changed

+16
-61
lines changed

6 files changed

+16
-61
lines changed

server/src/main/java/org/elasticsearch/action/search/FetchSearchPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private void moveToNextPhase(
271271
) {
272272
context.executeNextPhase(this, () -> {
273273
var resp = SearchPhaseController.merge(context.getRequest().scroll() != null, reducedQueryPhase, fetchResultsArr);
274-
context.addReleasable(resp::decRef);
274+
context.addReleasable(resp);
275275
return nextPhaseFactory.apply(resp, searchPhaseShardResults);
276276
});
277277
}

server/src/main/java/org/elasticsearch/action/search/SearchResponseSections.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99

1010
package org.elasticsearch.action.search;
1111

12-
import org.elasticsearch.core.RefCounted;
13-
import org.elasticsearch.core.SimpleRefCounted;
12+
import org.elasticsearch.core.Releasable;
1413
import org.elasticsearch.search.SearchHits;
1514
import org.elasticsearch.search.aggregations.InternalAggregations;
1615
import org.elasticsearch.search.profile.SearchProfileResults;
1716
import org.elasticsearch.search.profile.SearchProfileShardResult;
1817
import org.elasticsearch.search.suggest.Suggest;
19-
import org.elasticsearch.transport.LeakTracker;
2018

2119
import java.util.Collections;
2220
import java.util.Map;
@@ -25,7 +23,7 @@
2523
* Holds some sections that a search response is composed of (hits, aggs, suggestions etc.) during some steps of the search response
2624
* building.
2725
*/
28-
public class SearchResponseSections implements RefCounted {
26+
public class SearchResponseSections implements Releasable {
2927

3028
public static final SearchResponseSections EMPTY_WITH_TOTAL_HITS = new SearchResponseSections(
3129
SearchHits.EMPTY_WITH_TOTAL_HITS,
@@ -53,8 +51,6 @@ public class SearchResponseSections implements RefCounted {
5351
protected final Boolean terminatedEarly;
5452
protected final int numReducePhases;
5553

56-
private final RefCounted refCounted;
57-
5854
public SearchResponseSections(
5955
SearchHits hits,
6056
InternalAggregations aggregations,
@@ -72,7 +68,6 @@ public SearchResponseSections(
7268
this.timedOut = timedOut;
7369
this.terminatedEarly = terminatedEarly;
7470
this.numReducePhases = numReducePhases;
75-
refCounted = hits.getHits().length > 0 ? LeakTracker.wrap(new SimpleRefCounted()) : ALWAYS_REFERENCED;
7671
}
7772

7873
public final SearchHits hits() {
@@ -97,26 +92,7 @@ public final Map<String, SearchProfileShardResult> profile() {
9792
}
9893

9994
@Override
100-
public void incRef() {
101-
refCounted.incRef();
102-
}
103-
104-
@Override
105-
public boolean tryIncRef() {
106-
return refCounted.tryIncRef();
107-
}
108-
109-
@Override
110-
public boolean decRef() {
111-
if (refCounted.decRef()) {
112-
hits.decRef();
113-
return true;
114-
}
115-
return false;
116-
}
117-
118-
@Override
119-
public boolean hasReferences() {
120-
return refCounted.hasReferences();
95+
public void close() {
96+
hits.decRef();
12197
}
12298
}

server/src/main/java/org/elasticsearch/action/search/SearchScrollAsyncAction.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ protected final void sendResponse(
246246
if (request.scroll() != null) {
247247
scrollId = request.scrollId();
248248
}
249-
var sections = SearchPhaseController.merge(true, queryPhase, fetchResults);
250-
try {
249+
try (var sections = SearchPhaseController.merge(true, queryPhase, fetchResults)) {
251250
ActionListener.respondAndRelease(
252251
listener,
253252
new SearchResponse(
@@ -262,8 +261,6 @@ protected final void sendResponse(
262261
null
263262
)
264263
);
265-
} finally {
266-
sections.decRef();
267264
}
268265
} catch (Exception e) {
269266
listener.onFailure(new ReduceSearchPhaseException("fetch", "inner finish failed", e, buildShardFailures()));

server/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
9696

9797
List<MultiSearchResponse.Item> mSearchResponses = new ArrayList<>(numInnerHits);
9898
for (int innerHitNum = 0; innerHitNum < numInnerHits; innerHitNum++) {
99-
var sections = new SearchResponseSections(collapsedHits.get(innerHitNum), null, null, false, null, null, 1);
100-
try {
99+
try (
100+
var sections = new SearchResponseSections(collapsedHits.get(innerHitNum), null, null, false, null, null, 1)
101+
) {
101102
mockSearchPhaseContext.sendSearchResponse(sections, null);
102-
} finally {
103-
sections.decRef();
104103
}
105104
mSearchResponses.add(new MultiSearchResponse.Item(mockSearchPhaseContext.searchResponse.get(), null));
106105
// transferring ownership to the multi-search response so no need to release here
@@ -121,11 +120,8 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
121120
ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, hits, () -> new SearchPhase("test") {
122121
@Override
123122
public void run() {
124-
var sections = new SearchResponseSections(hits, null, null, false, null, null, 1);
125-
try {
123+
try (var sections = new SearchResponseSections(hits, null, null, false, null, null, 1)) {
126124
mockSearchPhaseContext.sendSearchResponse(sections, null);
127-
} finally {
128-
sections.decRef();
129125
}
130126
}
131127
});
@@ -215,11 +211,8 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
215211
ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, hits, () -> new SearchPhase("test") {
216212
@Override
217213
public void run() {
218-
var sections = new SearchResponseSections(hits, null, null, false, null, null, 1);
219-
try {
214+
try (var sections = new SearchResponseSections(hits, null, null, false, null, null, 1)) {
220215
mockSearchPhaseContext.sendSearchResponse(sections, null);
221-
} finally {
222-
sections.decRef();
223216
}
224217
}
225218
});
@@ -254,11 +247,8 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
254247
ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, hits, () -> new SearchPhase("test") {
255248
@Override
256249
public void run() {
257-
var sections = new SearchResponseSections(hits, null, null, false, null, null, 1);
258-
try {
250+
try (var sections = new SearchResponseSections(hits, null, null, false, null, null, 1)) {
259251
mockSearchPhaseContext.sendSearchResponse(sections, null);
260-
} finally {
261-
sections.decRef();
262252
}
263253
}
264254
});

server/src/test/java/org/elasticsearch/action/search/FetchLookupFieldsPhaseTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
4747
searchHits[i] = SearchHitTests.createTestItem(randomBoolean(), randomBoolean());
4848
}
4949
SearchHits hits = new SearchHits(searchHits, new TotalHits(numHits, TotalHits.Relation.EQUAL_TO), 1.0f);
50-
var sections = new SearchResponseSections(hits, null, null, false, null, null, 1);
51-
try {
50+
try (var sections = new SearchResponseSections(hits, null, null, false, null, null, 1)) {
5251
FetchLookupFieldsPhase phase = new FetchLookupFieldsPhase(searchPhaseContext, sections, null);
5352
phase.run();
5453
} finally {
55-
sections.decRef();
5654
hits.decRef();
5755
}
5856
searchPhaseContext.assertNoFailure();
@@ -189,12 +187,10 @@ void sendExecuteMultiSearch(
189187
new TotalHits(2, TotalHits.Relation.EQUAL_TO),
190188
1.0f
191189
);
192-
var sections = new SearchResponseSections(searchHits, null, null, false, null, null, 1);
193-
try {
190+
try (var sections = new SearchResponseSections(searchHits, null, null, false, null, null, 1)) {
194191
FetchLookupFieldsPhase phase = new FetchLookupFieldsPhase(searchPhaseContext, sections, null);
195192
phase.run();
196193
} finally {
197-
sections.decRef();
198194
searchHits.decRef();
199195
}
200196
assertTrue(requestSent.get());

server/src/test/java/org/elasticsearch/action/search/SearchPhaseControllerTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ public void testMerge() {
292292
reducedQueryPhase.suggest(),
293293
profile
294294
);
295-
final SearchResponseSections mergedResponse = SearchPhaseController.merge(false, reducedQueryPhase, fetchResults);
296-
try {
295+
try (SearchResponseSections mergedResponse = SearchPhaseController.merge(false, reducedQueryPhase, fetchResults)) {
297296
if (trackTotalHits == SearchContext.TRACK_TOTAL_HITS_DISABLED) {
298297
assertNull(mergedResponse.hits.getTotalHits());
299298
} else {
@@ -346,7 +345,6 @@ public void testMerge() {
346345
assertThat(mergedResponse.profile(), is(anEmptyMap()));
347346
}
348347
} finally {
349-
mergedResponse.decRef();
350348
fetchResults.asList().forEach(TransportMessage::decRef);
351349
}
352350
} finally {
@@ -410,8 +408,7 @@ protected boolean lessThan(RankDoc a, RankDoc b) {
410408
reducedQueryPhase.suggest(),
411409
false
412410
);
413-
SearchResponseSections mergedResponse = SearchPhaseController.merge(false, reducedQueryPhase, fetchResults);
414-
try {
411+
try (SearchResponseSections mergedResponse = SearchPhaseController.merge(false, reducedQueryPhase, fetchResults)) {
415412
if (trackTotalHits == SearchContext.TRACK_TOTAL_HITS_DISABLED) {
416413
assertNull(mergedResponse.hits.getTotalHits());
417414
} else {
@@ -427,7 +424,6 @@ protected boolean lessThan(RankDoc a, RankDoc b) {
427424
assertThat(mergedResponse.hits().getHits().length, equalTo(reducedQueryPhase.sortedTopDocs().scoreDocs().length));
428425
assertThat(mergedResponse.profile(), is(anEmptyMap()));
429426
} finally {
430-
mergedResponse.decRef();
431427
fetchResults.asList().forEach(TransportMessage::decRef);
432428
}
433429
} finally {

0 commit comments

Comments
 (0)