Skip to content

Fix DISIDocIdStream::count so that it does not try to count beyond max #14522

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

Merged
merged 3 commits into from
Apr 18, 2025

Conversation

ChrisHegarty
Copy link
Contributor

Fix DISIDocIdStream::count so that it does not try to count beyond max.

From the perspective of DISIDocIdStream it should not be necessary to check the iterator position before calling count.

Without this fix, DISIDocIdStream::count can easily fail with IOOBE, e.g.

Range [0, -5) out of bounds for length 100
java.lang.IndexOutOfBoundsException: Range [0, -5) out of bounds for length 100
	at __randomizedtesting.SeedInfo.seed([1667A192A4675B09:1A4E2A88F38511BB]:0)
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
	at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
	at java.base/java.util.Objects.checkFromToIndex(Objects.java:391)
	at org.apache.lucene.util.FixedBitSet.cardinality(FixedBitSet.java:212)
	at org.apache.lucene.search.DISIDocIdStream.count(DISIDocIdStream.java:63)
	at org.apache.lucene.search.DocIdStream.count(DocIdStream.java:50)
	at org.apache.lucene.search.TestDISIDocIdStream.testCountUpTo2(TestDISIDocIdStream.java:131)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)

Copy link
Contributor

@jpountz jpountz left a comment

Choose a reason for hiding this comment

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

Thank you! Did it fail with an existing query/collector, or did you find it while trying to take advantage of DocIdStream for a new use-case?

@ChrisHegarty
Copy link
Contributor Author

ChrisHegarty commented Apr 18, 2025

Thank you! Did it fail with an existing query/collector, or did you find it while trying to take advantage of DocIdStream for a new use-case?

We see DISIDocIdStream::count in stack traces of existing code (not new) in Elasticsearch, see elastic/elasticsearch#126939 (comment). In particular:

Caused by: java.lang.IndexOutOfBoundsException: Range [0, -14) out of bounds for length 4096
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
	at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
	at java.base/java.util.Objects.checkFromToIndex(Objects.java:391)
	at org.apache.lucene.core@10.2.0/org.apache.lucene.util.FixedBitSet.cardinality(FixedBitSet.java:212)
	at org.apache.lucene.core@10.2.0/org.apache.lucene.search.DISIDocIdStream.count(DISIDocIdStream.java:63)
	at org.apache.lucene.core@10.2.0/org.apache.lucene.search.DocIdStream.count(DocIdStream.java:50)
	at org.apache.lucene.core@10.2.0/org.apache.lucene.search.TotalHitCountCollector$1.collect(TotalHitCountCollector.java:69)
	at org.apache.lucene.core@10.2.0/org.apache.lucene.search.DenseConjunctionBulkScorer.scoreWindow(DenseConjunctionBulkScorer.java:208)
	at org.apache.lucene.core@10.2.0/org.apache.lucene.search.DenseConjunctionBulkScorer.score(DenseConjunctionBulkScorer.java:132)
	at org.apache.lucene.core@10.2.0/org.apache.lucene.search.ReqExclBulkScorer.score(ReqExclBulkScorer.java:66)
	at org.elasticsearch.server@9.1.0/org.elasticsearch.search.internal.CancellableBulkScorer.score(CancellableBulkScorer.java:46)
...

I'm not yet able to reproduce with Elasticsearch, but it seems very likely to be the root cause of some of the failures we're seeing.

@ChrisHegarty ChrisHegarty merged commit 4ac2d14 into apache:main Apr 18, 2025
7 checks passed
ChrisHegarty added a commit that referenced this pull request Apr 18, 2025
#14522)

Fix DISIDocIdStream::count so that it does not try to count beyond max.

From the perspective of DISIDocIdStream it should not be necessary to check the iterator position before calling count.
ChrisHegarty added a commit that referenced this pull request Apr 18, 2025
#14522)

Fix DISIDocIdStream::count so that it does not try to count beyond max.

From the perspective of DISIDocIdStream it should not be necessary to check the iterator position before calling count.
@ChrisHegarty ChrisHegarty deleted the DISIDocIdStream_fix branch April 18, 2025 14:28
@ChrisHegarty ChrisHegarty added this to the 10.2.1 milestone Apr 18, 2025
jpountz pushed a commit to jpountz/lucene that referenced this pull request Apr 24, 2025
apache#14522)

Fix DISIDocIdStream::count so that it does not try to count beyond max.

From the perspective of DISIDocIdStream it should not be necessary to check the iterator position before calling count.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants