You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "benwtrent (via GitHub)" <gi...@apache.org> on 2025/09/23 21:31:39 UTC

[PR] Add asynchronous prefetch for DirectIO directory [lucene]

benwtrent opened a new pull request, #15224:
URL: https://github.com/apache/lucene/pull/15224

   This adds prefetching to directIO. The idea is pretty simple,
   
    - configure a number of "prefetch buffers" that are the same size as the directIO buffer
    - calling prefetch will start a prefetch virtual thread to fill an available buffer
    - On read, DirectIO will attempt to refill from any prefetched buffers that match the position before attempting to do directIO itself.
   
   When doing many prefetches and handling things in batches, this can significantly improve throughput.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-4914612480

   I am gonna close this. Honestly, all benchmarking found the tradeoffs here to not be worth it. I think for anything direct IO focused that wants to handle throughput, we need to use some native things...or just stick with mmap and be ok with the page faults.
   
   FWIW, in benchmarking we found just doing pre-fetch with MMAP and multiple threads reached IO saturation, while this PR never really did, and consequently read throughput was always worse :(


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-3411726068

   I also think that NIOFS could benefit of a prefetch implementation as well. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "dungba88 (via GitHub)" <gi...@apache.org>.
dungba88 commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-3409427122

   Do you have some number on throughput change?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-3411868849

   > If you used direct io for everything you would want to introduce an explicit disk cache somewhere, even with prefetching I don't think performance would meet expectations for a lot of workloads if most reads resulted in a syscall.
   
   100% agreed. I think we are a long ways away from making IO super cheap. 
   
   Again, MMAP has many benefits still. But virtual threads do make this way easier than it would have been before!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "mikemccand (via GitHub)" <gi...@apache.org>.
mikemccand commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-3410528334

   This is neat -- if Lucene implements enough top-down prefetch hinting, it might eventually be that DirectIO, alone, is sufficient for good query latency/throughput?  I.e. we could stop entirely relying on OS to do its prefetching/caching (buffer cache), maybe, in very cold indices?
   
   Isn't `DirectIODirectory` today only inserting itself for merge context reading & writing?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "mccullocht (via GitHub)" <gi...@apache.org>.
mccullocht commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-3411842031

   If you used direct io for everything you would want to introduce an explicit disk cache somewhere, even with prefetching I don't think performance would meet expectations for a lot of workloads if most reads resulted in a syscall.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "mccullocht (via GitHub)" <gi...@apache.org>.
mccullocht commented on code in PR #15224:
URL: https://github.com/apache/lucene/pull/15224#discussion_r2395370732


##########
lucene/misc/src/java/org/apache/lucene/misc/store/DirectIODirectory.java:
##########
@@ -554,8 +642,178 @@ public IndexInput slice(String sliceDescription, long offset, long length) throw
             "slice() " + sliceDescription + " out of bounds: " + this);
       }
       var slice = new DirectIOIndexInput(sliceDescription, this, this.offset + offset, length);
+      // TODO figure out how to make this async
       slice.seekInternal(0L);
       return slice;
     }
   }
+
+  /** A prefetcher that can prefetch multiple chunks of data from a FileChannel using direct IO. */
+  private static class DirectIOPrefetcher implements Closeable {
+    private final int maxConcurrentPrefetches;
+    private final int maxTotalPrefetches;
+    private final int blockSize;
+    private final long[] prefetchPos;
+    private final Future<?>[] prefetchThreads;
+    private final TreeMap<Long, Integer> posToSlot;
+    private final Deque<Integer> slots;
+    private final ByteBuffer[] prefetchBuffers;
+    private final IOException[] prefetchExceptions;
+    private final int prefetchBytesSize;
+    private final Deque<Long> pendingPrefetches = new ArrayDeque<>();
+    private final FileChannel channel;
+    private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();

Review Comment:
   Is the executor something you would want to share within a directory or potentially even across directories? I can't find any documentation that indicates that this pattern would be a problem.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent closed pull request #15224: Add asynchronous prefetch for DirectIO directory
URL: https://github.com/apache/lucene/pull/15224


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-3411723508

   > Isn't DirectIODirectory today only inserting itself for merge context reading & writing?
   
   Correct, its only used in certain scenarios. We are experimenting using it in more areas (e.g. vector rescoring, to keep from polluting the off-heap cache with rescoring vectors)
   
   > it might eventually be that DirectIO, alone, is sufficient for good query latency/throughput?
   
   Its not quite there yet. I have seen this improve throughput by more than 2x though depending on the read patterns. MMAP still has TONS of advantages (direct memory segment access being a HUGE one for vectors). 
   
   Virtual threads make this VERY easy, but I am sure there is a lot of headroom for improvements.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-3470803652

   This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Add asynchronous prefetch for DirectIO directory [lucene]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #15224:
URL: https://github.com/apache/lucene/pull/15224#issuecomment-3325623469

   This PR does not have an entry in lucene/CHANGES.txt. Consider adding one. If the PR doesn't need a changelog entry, then add the skip-changelog label to it and you will stop receiving this reminder on future updates to the PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org