Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some more SearchResponse Leaks in tests #104022

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,12 @@ private RolloverResponse rollover(String dataStreamName) throws ExecutionExcepti
}

private Aggregations aggregate(final String index, AggregationBuilder aggregationBuilder) {
return client().prepareSearch(index).addAggregation(aggregationBuilder).get().getAggregations();
var resp = client().prepareSearch(index).addAggregation(aggregationBuilder).get();
try {
return resp.getAggregations();
} finally {
resp.decRef();
}
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,25 +353,24 @@ protected <Request extends ActionRequest, Response extends ActionResponse> void
searchFailures.add(new ShardSearchFailure(new RuntimeException("shard failed")));
}
}

final SearchResponse response = new SearchResponse(
SearchHits.EMPTY_WITH_TOTAL_HITS,
null,
null,
false,
null,
null,
1,
null,
10,
searchFailures.size() > 0 ? 0 : 5,
0,
0,
searchFailures.toArray(new ShardSearchFailure[searchFailures.size()]),
null
listener.onResponse(
(Response) new SearchResponse(
SearchHits.EMPTY_WITH_TOTAL_HITS,
null,
null,
false,
null,
null,
1,
null,
10,
searchFailures.size() > 0 ? 0 : 5,
0,
0,
searchFailures.toArray(new ShardSearchFailure[searchFailures.size()]),
null
)
);

listener.onResponse((Response) response);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,16 @@ public void testFindTriggeredWatchesGoodCase() {
SearchResponse searchResponse1 = mock(SearchResponse.class);
when(searchResponse1.getSuccessfulShards()).thenReturn(1);
when(searchResponse1.getTotalShards()).thenReturn(1);
BytesArray source = new BytesArray("{}");
SearchHit hit = new SearchHit(0, "first_foo");
hit.version(1L);
hit.shard(new SearchShardTarget("_node_id", new ShardId(index, 0), null));
hit.sourceRef(source);
SearchHits hits = new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f);
when(searchResponse1.getHits()).thenReturn(hits);
final BytesArray source = new BytesArray("{}");
{
final SearchHit hit = new SearchHit(0, "first_foo");
hit.version(1L);
hit.shard(new SearchShardTarget("_node_id", new ShardId(index, 0), null));
hit.sourceRef(source);
when(searchResponse1.getHits()).thenReturn(
new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f)
);
}
when(searchResponse1.getScrollId()).thenReturn("_scrollId");
doAnswer(invocation -> {
@SuppressWarnings("unchecked")
Expand All @@ -226,22 +229,36 @@ public void testFindTriggeredWatchesGoodCase() {
}).when(client).execute(eq(TransportSearchAction.TYPE), any(), any());

// First return a scroll response with a single hit and then with no hits
hit = new SearchHit(0, "second_foo");
hit.version(1L);
hit.shard(new SearchShardTarget("_node_id", new ShardId(index, 0), null));
hit.sourceRef(source);
hits = new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f);
SearchResponse searchResponse2 = new SearchResponse(hits, null, null, false, null, null, 1, "_scrollId1", 1, 1, 0, 1, null, null);
SearchResponse searchResponse3 = SearchResponseUtils.emptyWithTotalHits("_scrollId2", 1, 1, 0, 1, null, null);

doAnswer(invocation -> {
SearchScrollRequest request = (SearchScrollRequest) invocation.getArguments()[1];
@SuppressWarnings("unchecked")
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) invocation.getArguments()[2];
if (request.scrollId().equals("_scrollId")) {
listener.onResponse(searchResponse2);
final var hit2 = new SearchHit(0, "second_foo");
hit2.version(1L);
hit2.shard(new SearchShardTarget("_node_id", new ShardId(index, 0), null));
hit2.sourceRef(source);
ActionListener.respondAndRelease(
listener,
new SearchResponse(
new SearchHits(new SearchHit[] { hit2 }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f),
null,
null,
false,
null,
null,
1,
"_scrollId1",
1,
1,
0,
1,
null,
null
)
);
} else if (request.scrollId().equals("_scrollId1")) {
listener.onResponse(searchResponse3);
listener.onResponse(SearchResponseUtils.emptyWithTotalHits("_scrollId2", 1, 1, 0, 1, null, null));
} else {
listener.onFailure(new ElasticsearchException("test issue"));
}
Expand Down