-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adding support for passing an ExecutorService into DirectoryReader.open() to enable concurrent segment reader initialization #15428
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
Open
BryceKan3
wants to merge
3
commits into
apache:main
Choose a base branch
from
BryceKan3:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,12 +24,15 @@ | |
| import java.nio.file.Path; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Comparator; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.Set; | ||
| import java.util.TreeSet; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import org.apache.lucene.document.Document; | ||
| import org.apache.lucene.document.Field; | ||
| import org.apache.lucene.document.FieldType; | ||
|
|
@@ -49,6 +52,7 @@ | |
| import org.apache.lucene.util.Bits; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.apache.lucene.util.IOUtils; | ||
| import org.apache.lucene.util.NamedThreadFactory; | ||
| import org.apache.lucene.util.SuppressForbidden; | ||
| import org.apache.lucene.util.Version; | ||
| import org.junit.Assume; | ||
|
|
@@ -1121,4 +1125,125 @@ public void testOpenWithInvalidMinCompatVersion() throws IOException { | |
| DirectoryReader.open(commit, random().nextInt(Version.LATEST.major + 1), null).close(); | ||
| } | ||
| } | ||
|
|
||
| public void testOpenWithExecutorService() throws IOException { | ||
| Directory dir = newDirectory(); | ||
| createMultiSegmentIndex(dir, 5); | ||
|
|
||
| ExecutorService executor = | ||
| Executors.newFixedThreadPool(1, new NamedThreadFactory("TestDirectoryReader")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to only test with 1 thread in these tests? Can we add more threads to test for edge cases and races, like only one thread sees an exception while opening a segment reader? |
||
| try { | ||
| DirectoryReader reader = DirectoryReader.open(dir, executor); | ||
| assertNotNull(reader); | ||
| assertTrue(reader.numDocs() > 0); | ||
| reader.close(); | ||
| } finally { | ||
| executor.shutdown(); | ||
| dir.close(); | ||
| } | ||
| } | ||
|
|
||
| public void testOpenWithLeafSorterAndExecutor() throws IOException { | ||
| Directory dir = newDirectory(); | ||
| createMultiSegmentIndex(dir, 3); | ||
|
|
||
| ExecutorService executor = | ||
| Executors.newFixedThreadPool(1, new NamedThreadFactory("TestDirectoryReader")); | ||
| Comparator<LeafReader> sorter = (r1, r2) -> Integer.compare(r1.numDocs(), r2.numDocs()); | ||
|
|
||
| try { | ||
| DirectoryReader reader = DirectoryReader.open(dir, sorter, executor); | ||
| assertNotNull(reader); | ||
| reader.close(); | ||
| } finally { | ||
| executor.shutdown(); | ||
| dir.close(); | ||
| } | ||
| } | ||
|
|
||
| public void testOpenCommitWithExecutor() throws IOException { | ||
| Directory dir = newDirectory(); | ||
| IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig()); | ||
| Document doc = new Document(); | ||
| doc.add(newStringField("id", "1", Field.Store.YES)); | ||
| writer.addDocument(doc); | ||
| writer.commit(); | ||
| writer.close(); | ||
|
|
||
| IndexCommit commit = DirectoryReader.listCommits(dir).get(0); | ||
| ExecutorService executor = | ||
| Executors.newFixedThreadPool(1, new NamedThreadFactory("TestDirectoryReader")); | ||
| try { | ||
| DirectoryReader reader = DirectoryReader.open(commit, executor); | ||
| assertNotNull(reader); | ||
| assertEquals(1, reader.numDocs()); | ||
| reader.close(); | ||
| } finally { | ||
| executor.shutdown(); | ||
| dir.close(); | ||
| } | ||
| } | ||
|
|
||
| public void testOpenCommitWithMinVersionAndExecutor() throws IOException { | ||
| Directory dir = newDirectory(); | ||
| createMultiSegmentIndex(dir, 3); | ||
|
|
||
| ExecutorService executor = | ||
| Executors.newFixedThreadPool(1, new NamedThreadFactory("TestDirectoryReader")); | ||
| Comparator<LeafReader> sorter = (r1, r2) -> Integer.compare(r1.numDocs(), r2.numDocs()); | ||
|
|
||
| try (DirectoryReader baseReader = DirectoryReader.open(dir)) { | ||
| IndexCommit commit = baseReader.getIndexCommit(); | ||
| DirectoryReader reader = | ||
| DirectoryReader.open(commit, Version.MIN_SUPPORTED_MAJOR, sorter, executor); | ||
| assertNotNull(reader); | ||
| assertEquals(baseReader.numDocs(), reader.numDocs()); | ||
| reader.close(); | ||
| } finally { | ||
| executor.shutdown(); | ||
| dir.close(); | ||
| } | ||
| } | ||
|
|
||
| public void testNullExecutorService() throws IOException { | ||
| Directory dir = newDirectory(); | ||
| createMultiSegmentIndex(dir, 3); | ||
|
|
||
| // Should work fine with null executor (fallback to sequential) | ||
| DirectoryReader reader = DirectoryReader.open(dir, (ExecutorService) null); | ||
| assertNotNull(reader); | ||
| assertTrue(reader.numDocs() > 0); | ||
| reader.close(); | ||
| dir.close(); | ||
| } | ||
|
|
||
| public void testExecutorServiceExceptionHandling() throws IOException { | ||
| Directory dir = newDirectory(); | ||
| createMultiSegmentIndex(dir, 3); | ||
|
|
||
| // Test with shutdown executor | ||
| ExecutorService executor = | ||
| Executors.newFixedThreadPool(1, new NamedThreadFactory("TestDirectoryReader")); | ||
| executor.shutdown(); | ||
|
|
||
| expectThrows( | ||
| RuntimeException.class, | ||
| () -> { | ||
| DirectoryReader.open(dir, executor); | ||
| }); | ||
|
|
||
| dir.close(); | ||
| } | ||
|
|
||
| private void createMultiSegmentIndex(Directory dir, int numSegments) throws IOException { | ||
| IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig().setMaxBufferedDocs(2)); | ||
| for (int i = 0; i < numSegments; i++) { | ||
| Document doc = new Document(); | ||
| doc.add(newStringField("id", String.valueOf(i), Field.Store.YES)); | ||
| doc.add(newTextField("content", "segment " + i + " content", Field.Store.YES)); | ||
| writer.addDocument(doc); | ||
| writer.commit(); // Force new segment | ||
| } | ||
| writer.close(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered using the existing
TaskExecutorpattern and instead implementing this as such:Based on the comments in that class, there are some optimizations we could inherit here: