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

Use newSearcher instead of new IndexSearcher in tests where possible #98110

Merged
merged 3 commits into from
Aug 22, 2023

Conversation

cbuescher
Copy link
Member

@cbuescher cbuescher commented Aug 1, 2023

This change swaps test code that directly creates IndexSearcher instances with LuceneTestCase#newSearcher calls
that have the advantage of randomly using concurrency and also randomly use assertion wrappers internally.
While this doesn't guarantee testing the concurrent code path, it should generally increase the likelihood of doing so.

simScorer = searcher.getSimilarity().scorer(boost, collectionStatistics, termStats.toArray(TermStatistics[]::new));
approximationWeight = searcher.createWeight(approximate(in), ScoreMode.COMPLETE_NO_SCORES, 1f);
} else {
simScorer = null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was necessary because the test now randomly wraps the similarity into an AssertingSimilarity which checks that the termStats array is not empty. This is not the case for the last part in SourceConfirmedTextQueryTests#testTerm, which is avoided by this additional condition.

Fields luceneTermVectors = getTermVectorsFromLucene(directoryReader, test.doc);
validateResponse(response, luceneTermVectors, test);
} finally {
IOUtils.close(directoryReader);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing to close the reader can lead to thread leaks if the randomized searcher enables concurrency.

Fields luceneTermVectors = getTermVectorsFromLucene(directoryReader, test.doc);
validateResponse(item.getResponse(), luceneTermVectors, test);
} finally {
IOUtils.close(directoryReader);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing to close the reader can lead to thread leaks if the randomized searcher enables concurrency.

@cbuescher cbuescher requested a review from javanna August 1, 2023 20:43
@cbuescher cbuescher added >test Issues or PRs that are addressing/adding tests :Search/Search Search-related issues that do not fall into other categories and removed WIP labels Aug 1, 2023
@cbuescher cbuescher marked this pull request as ready for review August 1, 2023 20:44
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

@elasticsearchmachine elasticsearchmachine added the Team:Search Meta label for search team label Aug 1, 2023
Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@javanna
Copy link
Member

javanna commented Aug 11, 2023

@elasticmachine update branch

@javanna
Copy link
Member

javanna commented Aug 14, 2023

@martijnvg could you have a look at the aggs related tests? There are some where we are now potentially adding concurrency to the mix, and I wonder if we may see precision errors etc.

Copy link
Member

@martijnvg martijnvg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to a&g related tests LGTM.

@javanna javanna changed the title User newSearcher instead of new IndexSearcher in tests where possible Use newSearcher instead of new IndexSearcher in tests where possible Aug 15, 2023
@quux00 quux00 added v8.11.0 and removed v8.10.0 labels Aug 16, 2023
Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one comment on changes made to aggs tests, maybe @iverase has an opinion here.

@@ -33,7 +33,7 @@ public void testNoData() throws Exception {
indexWriter.addDocument(Collections.singleton(new StringField("another_field", "value", Field.Store.NO)));
}
try (IndexReader reader = indexWriter.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
IndexSearcher searcher = newSearcher(reader);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking deeper, I wonder if these aggs tests should rather use a ContextIndexSearcher

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do this as part of: #98672
I don't think this should block merging this PR. This is already an improvement.

@martijnvg martijnvg merged commit 207a995 into elastic:main Aug 22, 2023
martijnvg added a commit to martijnvg/elasticsearch that referenced this pull request Aug 22, 2023
As part elastic#98110, EngineTestCase now uses newSearcher() to create a new index searcher (instead of just creating a new index searcher). This is for increasing test coverage. However, this test can't deal with wrapping the index reader as it leads to assertion errors. So instead use `newSearcher(leaf, false)` to still keep to test benefits of wrapping the index searcher.

Closes elastic#98716
@martijnvg
Copy link
Member

martijnvg commented Aug 23, 2023

Note that we changed a number of newSearcher(...) method usages to newIndexSearcher(...) method usages in #98761.
Lucene's newSearcher(...) handles exceptions during multi threaded searches very different than we do.

elasticsearchmachine pushed a commit that referenced this pull request Aug 23, 2023
As part #98110, EngineTestCase now uses newSearcher() to create a new
index searcher (instead of just creating a new index searcher). This is
for increasing test coverage. However, this test can't deal with
wrapping the index reader as it leads to assertion errors. So instead
use `newSearcher(leaf, false)` to still keep to test benefits of
wrapping the index searcher.

Closes #98716
elasticsearchmachine pushed a commit that referenced this pull request Aug 29, 2023
…her (#98932)

Related to: #98110

Lucene test searcher will randomly wrap the passed reader. That means
any usages that mix the two (the searcher and the previously reader),
could cause tests to fail.

Some of these locations maybe don't strictly need the
searcher.getReader, but I just wanted to make sure we didn't have flaky
tests.

closes: #98925
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team >test Issues or PRs that are addressing/adding tests v8.11.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants