Skip to content

Commit

Permalink
Ensure search contexts are released after tests
Browse files Browse the repository at this point in the history
These assertions are introduced in elastic#71354
  • Loading branch information
dnhatn committed Apr 7, 2021
1 parent 6a081e1 commit ab2b698
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,13 @@ public int getActiveContexts() {
return this.activeReaders.size();
}

/**
* Returns the number of scroll contexts opened on the node
*/
public int getOpenScrollContexts() {
return openScrollContexts.get();
}

public ResponseCollectorService getResponseCollectorService() {
return this.responseCollectorService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.elasticsearch.node.NodeValidationException;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.MockScriptService;
import org.elasticsearch.search.SearchService;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.transport.TransportSettings;
Expand Down Expand Up @@ -117,6 +118,9 @@ public void setUp() throws Exception {
public void tearDown() throws Exception {
logger.trace("[{}#{}]: cleaning up after test", getTestClass().getSimpleName(), getTestName());
ensureNoInitializingShards();
SearchService searchService = getInstanceFromNode(SearchService.class);
assertThat(searchService.getActiveContexts(), equalTo(0));
assertThat(searchService.getOpenScrollContexts(), equalTo(0));
super.tearDown();
assertAcked(
client().admin().indices().prepareDelete("*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,7 @@ public void ensureEstimatedStats() {
public synchronized void assertAfterTest() throws Exception {
super.assertAfterTest();
assertRequestsFinished();
assertSearchContextsReleased();
assertNoInFlightDocsInEngine();
for (NodeAndClient nodeAndClient : nodes.values()) {
NodeEnvironment env = nodeAndClient.node().getNodeEnvironment();
Expand Down Expand Up @@ -2356,4 +2357,18 @@ public void assertRequestsFinished() {
}
}
}

private void assertSearchContextsReleased() {
for (NodeAndClient nodeAndClient : nodes.values()) {
SearchService searchService = getInstance(SearchService.class, nodeAndClient.name);
try {
assertBusy(() -> {
assertThat(searchService.getActiveContexts(), equalTo(0));
assertThat(searchService.getOpenScrollContexts(), equalTo(0));
});
} catch (Exception e) {
throw new AssertionError("Failed to verify search contexts", e);
}
}
}
}

0 comments on commit ab2b698

Please sign in to comment.