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 Flaky Test In TestBlockJoinBulkScorer #13785

Merged
merged 3 commits into from
Sep 16, 2024
Merged
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 @@ -27,6 +27,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
Expand Down Expand Up @@ -260,6 +261,15 @@ private static void assertScores(
Float minScore,
Map<Integer, Float> expectedScores)
throws IOException {
assertScores(bulkScorer, scoreMode, minScore, List.of(expectedScores));
}

private static void assertScores(
BulkScorer bulkScorer,
org.apache.lucene.search.ScoreMode scoreMode,
Float minScore,
List<Map<Integer, Float>> expectedScoresList)
throws IOException {
Map<Integer, Float> actualScores = new HashMap<>();
bulkScorer.score(
new LeafCollector() {
Expand All @@ -283,7 +293,26 @@ public void collect(int doc) throws IOException {
null,
0,
NO_MORE_DOCS);
assertEquals(expectedScores, actualScores);

if (expectedScoresList.size() == 1) {
assertEquals(expectedScoresList.getFirst(), actualScores);
} else {
assertEqualsToOneOf(expectedScoresList, actualScores);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious why you don't call assertEqualsToOneOf all the time, shouldn't it also work in the size==1 case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should and I originally did that, but if you use assertEquals there are nicer IDE integrations for value comparison on failure. Given that this method is called with a single map of expected scores in nearly all cases, I tried to preserve that functionality. This could help in the case of a failure in testScoreRandomIndices, which could have a large random score map.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK

}

private static void assertEqualsToOneOf(List<?> expectedList, Object actual) {
boolean foundMatch = false;
for (Object expected : expectedList) {
if (Objects.equals(expected, actual)) {
foundMatch = true;
break;
}
Mikep86 marked this conversation as resolved.
Show resolved Hide resolved
}

if (!foundMatch) {
throw new AssertionError("expected one of: " + expectedList + " but was: " + actual);
}
}

public void testScoreRandomIndices() throws IOException {
Expand Down Expand Up @@ -370,18 +399,26 @@ public void testSetMinCompetitiveScoreWithScoreModeMax() throws IOException {
}

{
// Doc 16 is returned because MaxScoreBulkScorer scores assuming A will match in doc 13,
// leading to a potential max score of 6. By the time it determines that A doesn't match,
// scoring is complete and thus there is no advantage to not collecting the doc.
Map<Integer, Float> expectedScores =
// This test case has two potential results.
// If all docs are scored in the same batch, then doc 16 is collected because
// MaxScoreBulkScorer scores assuming A will match in doc 13, leading to a potential max
// score of 6.
// If the scoring is split across two or more batches, then doc 16 is not collected
// because MaxScoreBulkScorer does not assume A will match in doc 13, leading to a
// potential max score of 5.
Map<Integer, Float> expectedScores1 =
Map.of(
2, 6.0f,
10, 10.0f);
Map<Integer, Float> expectedScores2 =
Map.of(
2, 6.0f,
10, 10.0f,
16, 5.0f);

ScorerSupplier ss = weight.scorerSupplier(searcher.getIndexReader().leaves().get(0));
ss.setTopLevelScoringClause();
assertScores(ss.bulkScorer(), scoreMode, 6.0f, expectedScores);
assertScores(ss.bulkScorer(), scoreMode, 6.0f, List.of(expectedScores1, expectedScores2));
}

{
Expand Down