You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "jimczi (via GitHub)" <gi...@apache.org> on 2026/09/18 09:18:58 UTC
[PR] Let merges read stored fields sequentially without disturbing searches [lucene]
jimczi opened a new pull request, #16684:
URL: https://github.com/apache/lucene/pull/16684
A merge reads stored fields front to back, while searches read the same file at random.
`getMergeInstance()` clones the mapping the reader already holds, so whether a merge gets sequential
reads comes down to who opened the segment first. Asking for them in place is not an option, since
read advice applies to a whole mapping and searches are on it.
So a merge opens the data file for itself with `DataAccessHint.SEQUENTIAL`, and searches keep the
mapping they have. The original reader owns that second mapping and closes it, since merge instances
are never closed.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "jimczi (via GitHub)" <gi...@apache.org>.
jimczi commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5810855604
@uschindler you designed the merge-time read advice for vectors in #13920, so I'd like your opinion on the approach here before I take it further.
The merge maps the `.fdt` a second time and advises that one sequential, instead of re-advising the mapping searches are on. Two things pushed me off `updateIOContext`: it changes the advice under the searchers for as long as the merge runs, and it never reaches the directory, so `DirectIODirectory` and anything else routing on context can't act on a merge that borrows a search reader.
The second open is one mapping per stored-fields reader that gets merged, opened lazily, page cache shared so nothing is cached twice. It belongs to the reader that made it and is closed with it, merge instances only get clones.
If it holds up, I'd like to do the same for vectors: let a merge get its own decision, and let the directory be the one making it.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5831679338
Hi,
I know what you mean. But behind scenes two files will still have a single entry in page cache. I am just not sure how the read-advice is applied to different mappings and how this interferes. Therefor I would have proposed to be on safe side and manage a single mapping inside Lucene. By that we have control over when which read advice is there for the mapped file (and the FS cache behind).
I can do some investigations how this is handled in Linux (not sure about windows, macos) when multiple mappings on same file exists.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "jimczi (via GitHub)" <gi...@apache.org>.
jimczi commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5832949557
Thanks for digging into it, we ended up at the same conclusion on our side.
On the file handles you raised, I tried to mitigate it by closing earlier. `StoredFieldsReader` now gets the `finishMerge` hook that `KnnVectorsReader` already has, called from `SegmentMerger.cleanupMerge`, so the extra open lives for the merge instead of for as long as the segment stays open. On NIOFS the handle comes back as soon as the merge ends. On mmap the unmap still waits for the segment's shared arena, since `MMapDirectory` groups arenas per segment, but that costs address space rather than a handle.
On reusing one fd and mapping it twice: `MMapDirectory` closes the channel right after mapping, and two mappings from the same fd would also share `file_ra_state`, so the merge's readahead window and the `mmap_miss` counter would mix with the searchers'. A separate open avoids that.
I will add a note to #13920 with what we found about read advice when the same file is mapped more than once. And if you are happy with a separate open for merging, I will do the same for vectors, where `getMergeInstance` re-advises the searchers' mapping today.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "neoremind (via GitHub)" <gi...@apache.org>.
neoremind commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5864620351
Interesting discussion. I also want to include a test to solidify the proposed "different fd, different VMA" approach over the current "same fd, same VMA via clone". And indeed, "same fd, different VMA" is not possible in Lucene today.
The test is a single Java file, each case is one run: open MMapDirectory with advice, do cold read of a page in the middle of a large file, count what the kernel loads.
<details>
<summary>
test code
</summary>
```
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.lucene.store.DataAccessHint;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.MMapDirectory;
public class ReadAdviceCases {
static final int PAGE_SIZE = 4096;
static final long SPACING = 4096; // pages between reads = 16 MB
static long basePage;
static int numOfReads = 0;
public static void main(String[] args) throws Exception {
Path file = Path.of(args[0]).toAbsolutePath();
String name = file.getFileName().toString();
int testCase = Integer.parseInt(args[1]);
basePage = Files.size(file) / PAGE_SIZE / 4; // purposely reading starts from 1/4 of the file
IOContext RANDOM = IOContext.DEFAULT.withHints(DataAccessHint.RANDOM);
IOContext SEQUENTIAL = IOContext.DEFAULT.withHints(DataAccessHint.SEQUENTIAL);
switch (testCase) {
case 1 -> {
System.out.println(
"case 1: two opens (two mmaps): RANDOM then SEQUENTIAL"
+ " | read via RANDOM | expected pages: 1");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput r = mMapDir.openInput(name, RANDOM);
IndexInput s = mMapDir.openInput(name, SEQUENTIAL);
pause(testCase);
read("RANDOM", r);
}
}
case 2 -> {
System.out.println(
"case 2: two opens (two mmaps): RANDOM then SEQUENTIAL"
+ " | read via SEQUENTIAL | expected pages: 32");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput r = mMapDir.openInput(name, RANDOM);
IndexInput s = mMapDir.openInput(name, SEQUENTIAL);
pause(testCase);
read("SEQUENTIAL", s);
}
}
case 3 -> {
System.out.println(
"case 3: two opens (two mmaps): SEQUENTIAL then RANDOM"
+ " | read via SEQUENTIAL | expected pages: 32");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput s = mMapDir.openInput(name, SEQUENTIAL);
IndexInput r = mMapDir.openInput(name, RANDOM);
pause(testCase);
read("SEQUENTIAL", s);
}
}
case 4 -> {
System.out.println(
"case 4: two opens (two mmaps): SEQUENTIAL then RANDOM"
+ " | read via RANDOM | expected pages: 1");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput s = mMapDir.openInput(name, SEQUENTIAL);
IndexInput r = mMapDir.openInput(name, RANDOM);
pause(testCase);
read("RANDOM", r);
}
}
case 5 -> {
System.out.println(
"case 5: one mmap, clone->RANDOM then clone->SEQUENTIAL (last)"
+ " | read via RANDOM clone | expected pages: 32 (last advice wins)");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput base = mMapDir.openInput(name, IOContext.DEFAULT); // NORMAL, no madvise
IndexInput r = base.clone();
r.updateIOContext(RANDOM);
IndexInput s = base.clone();
s.updateIOContext(SEQUENTIAL); // overwrites RANDOM on the shared VMA
pause(testCase);
read("RANDOM-clone", r);
}
}
case 6 -> {
System.out.println(
"case 6: one mmap, clone->SEQUENTIAL then clone->RANDOM (last)"
+ " | read via SEQUENTIAL clone | expected pages: 1 (last advice wins)");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput base = mMapDir.openInput(name, IOContext.DEFAULT); // NORMAL, no madvise
IndexInput s = base.clone();
s.updateIOContext(SEQUENTIAL);
IndexInput r = base.clone();
r.updateIOContext(RANDOM); // overwrites SEQUENTIAL on the shared VMA
pause(testCase);
read("SEQUENTIAL-clone", s);
}
}
case 7 -> {
System.out.println(
"case 7: as case 5, then close the SEQUENTIAL clone"
+ " | read via RANDOM clone | expected pages: 32 (closing a clone re-advises nothing)");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput base = mMapDir.openInput(name, IOContext.DEFAULT); // NORMAL, no madvise
IndexInput r = base.clone();
r.updateIOContext(RANDOM);
IndexInput s = base.clone();
s.updateIOContext(SEQUENTIAL); // overwrites RANDOM on the shared VMA
pause(testCase);
s.close();
read("RANDOM-clone", r);
}
}
case 8 -> {
System.out.println(
"case 8: as case 6, then close the RANDOM clone"
+ " | read via SEQUENTIAL clone | expected pages: 1 (closing a clone re-advises nothing)");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput base = mMapDir.openInput(name, IOContext.DEFAULT); // NORMAL, no madvise
IndexInput s = base.clone();
s.updateIOContext(SEQUENTIAL);
IndexInput r = base.clone();
r.updateIOContext(RANDOM); // overwrites SEQUENTIAL on the shared VMA
pause(testCase);
r.close();
read("SEQUENTIAL-clone", s);
}
}
case 9 -> {
System.out.println(
"case 9: one open NORMAL (no madvise); 100 cold misses, then 5 more cold reads"
+ " | read via NORMAL | expected pages: fault_around_bytes=65536 (default): 105 x 32 pages,"
+ " no fallback; fault_around_bytes=4096: 100 x 32 pages, then fallback to 5 x 1 after 100 misses."
+ " Events are pages/4 (8 per read-around, not 32) if the fs uses 16 KB folios, so the perf count is"
+ " 840 = 100 x 8 + 5 x 8 with fault-around on (default) and 805 = 100 x 8 + 5 x 1 with fault-around off");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput n = mMapDir.openInput(name, IOContext.DEFAULT); // NORMAL
pause(testCase);
readMany("NORMAL", n, 100);
for (int i = 0; i < 5; i++) {
read("NORMAL", n);
}
}
}
case 10 -> {
System.out.println(
"case 10: one open SEQUENTIAL; 100 cold misses, then 5 more cold reads"
+ " | read via SEQUENTIAL | expected pages: phase 1 = 100 x 32 = 3200,"
+ " phase 2 = 5 x 32 = 160 (no fallback for SEQUENTIAL), total 3360");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput s = mMapDir.openInput(name, SEQUENTIAL);
pause(testCase);
readMany("SEQUENTIAL", s, 100);
for (int i = 0; i < 5; i++) {
read("SEQUENTIAL", s);
}
}
}
case 11 -> {
System.out.println(
"case 11: two opens NORMAL + SEQUENTIAL; 100 cold misses via NORMAL, then 5 via NORMAL and 5 via"
+ " SEQUENTIAL | expected pages: fault_around_bytes=65536: 105 x 32 + 5 x 32;"
+ " fault_around_bytes=4096: 100 x 32 + 5 x 1 + 5 x 32 (SEQUENTIAL never falls back; it does not consult mmap_miss)."
+ " Events are pages/4 for the NORMAL reads if the fs uses 16 KB folios, so the perf count is"
+ " 1000 = 100 x 8 + 5 x 8 + 5 x 32 with fault-around on (default) and 965 = 100 x 8 + 5 x 1 + 5 x 32 with fault-around off");
try (MMapDirectory mMapDir = dir(file)) {
IndexInput n = mMapDir.openInput(name, IOContext.DEFAULT); // NORMAL
IndexInput s = mMapDir.openInput(name, SEQUENTIAL);
pause(testCase);
readMany("NORMAL", n, 100);
for (int i = 0; i < 5; i++) {
read("NORMAL", n);
}
for (int i = 0; i < 5; i++) {
read("SEQUENTIAL", s);
}
}
}
default -> throw new IllegalArgumentException("case must be 1..11");
}
System.out.println("done");
}
static MMapDirectory dir(Path file) throws Exception {
MMapDirectory d = new MMapDirectory(file.getParent());
d.setReadAdvice(MMapDirectory.ADVISE_BY_CONTEXT);
d.setGroupingFunction(MMapDirectory.NO_GROUPING);
return d;
}
static void pause(int testCase) throws Exception {
pause("case " + testCase + " pid " + ProcessHandle.current().pid() + " mapped. Attach perf, press Enter.");
}
static void pause(String message) throws Exception {
System.out.println(message);
new BufferedReader(new InputStreamReader(System.in)).readLine();
}
static void read(String advice, IndexInput input) throws Exception {
long page = nextPage(input);
input.seek(page * PAGE_SIZE);
byte b = input.readByte();
System.out.println("read #" + numOfReads + " with advice " + advice + " at page " + page);
numOfReads++;
}
static void readMany(String advice, IndexInput in, int count) throws Exception {
int first = numOfReads;
for (int i = 0; i < count; i++) {
in.seek(nextPage(in) * PAGE_SIZE);
byte b = in.readByte();
numOfReads++;
}
System.out.println(
"read #" + first + ".." + (numOfReads - 1) + " with advice " + advice + " (" + count
+ " cold pages, " + (SPACING * PAGE_SIZE / 1024 / 1024) + " MB apart)");
}
static long nextPage(IndexInput input) {
long page = basePage + numOfReads * SPACING;
if ((page + SPACING) * PAGE_SIZE > input.length()) {
throw new IllegalStateException("file too small: read #" + numOfReads + " needs page " + page);
}
return page;
}
}
```
Run with
```
F=/root/bench-16G.dat
INO=$(stat -c %i $F)
CP=~/lucene/lucene/core/build/libs/lucene-core-11.0.0-SNAPSHOT.jar
javac -cp $CP -d . ReadAdviceCases.java
for N in 1 2 3 4 5 6 7 8 9 10 11; do
sync && echo 3 > /proc/sys/vm/drop_caches
echo | perf stat -e filemap:mm_filemap_add_to_page_cache --filter "i_ino == $INO" \
java --enable-native-access=ALL-UNNAMED -cp $CP:. com.neoremind.mylucene.ReadAdviceCases $F $N 2>&1 \
| grep -E "^case [0-9]+:|read #|filemap|Exception"
echo
done
```
</details>
I use `perf stat -e filemap:mm_filemap_add_to_page_cache --filter` to measure the readahead loading behavior, it fires every time a page (or folio) of file data is added into the page cache, which is exactly what we need to see the difference between RANDOM and SEQUENTIAL hint on how much readahead happens behind one fault.
Run on ecs.g9i.xlarge, kernel 6.6.102-8.alnx4.x86_64, ext4, read_ahead_kb=128 ( ra_pages=32).
The result is
```
case 1: two opens (two mmaps): RANDOM then SEQUENTIAL | read via RANDOM | expected pages: 1
1 filemap:mm_filemap_add_to_page_cache
case 2: two opens (two mmaps): RANDOM then SEQUENTIAL | read via SEQUENTIAL | expected pages: 32
32 filemap:mm_filemap_add_to_page_cache
case 3: two opens (two mmaps): SEQUENTIAL then RANDOM | read via SEQUENTIAL | expected pages: 32
32 filemap:mm_filemap_add_to_page_cache
case 4: two opens (two mmaps): SEQUENTIAL then RANDOM | read via RANDOM | expected pages: 1
1 filemap:mm_filemap_add_to_page_cache
case 5: one mmap, clone->RANDOM then clone->SEQUENTIAL (last) | read via RANDOM clone | expected pages: 32 (last advice wins)
32 filemap:mm_filemap_add_to_page_cache
case 6: one mmap, clone->SEQUENTIAL then clone->RANDOM (last) | read via SEQUENTIAL clone | expected pages: 1 (last advice wins)
1 filemap:mm_filemap_add_to_page_cache
case 7: as case 5, then close the SEQUENTIAL clone | read via RANDOM clone | expected pages: 32 (closing a clone re-advises nothing)
32 filemap:mm_filemap_add_to_page_cache
case 8: as case 6, then close the RANDOM clone | read via SEQUENTIAL clone | expected pages: 1 (closing a clone re-advises nothing)
1 filemap:mm_filemap_add_to_page_cache
case 9: one open NORMAL (no madvise); 100 cold misses, then 5 more cold reads | read via NORMAL | expected pages: fault_around_bytes=65536 (default): 105 x 32 pages, no fallback; fault_around_bytes=4096: 100 x 32 pages, then fallback to 5 x 1 after 100 misses. Events are pages/4 (8 per read-around, not 32) if the fs uses 16 KB folios, so the perf count is 840 = 100 x 8 + 5 x 8 with fault-around on (default) and 805 = 100 x 8 + 5 x 1 with fault-around off
840 filemap:mm_filemap_add_to_page_cache
case 10: one open SEQUENTIAL; 100 cold misses, then 5 more cold reads | read via SEQUENTIAL | expected pages: phase 1 = 100 x 32 = 3200, phase 2 = 5 x 32 = 160 (no fallback for SEQUENTIAL), total 3360
3,360 filemap:mm_filemap_add_to_page_cache
case 11: two opens NORMAL + SEQUENTIAL; 100 cold misses via NORMAL, then 5 via NORMAL and 5 via SEQUENTIAL | expected pages: fault_around_bytes=65536: 105 x 32 + 5 x 32; fault_around_bytes=4096: 100 x 32 + 5 x 1 + 5 x 32 (SEQUENTIAL never falls back; it does not consult mmap_miss). Events are pages/4 for the NORMAL reads if the fs uses 16 KB folios, so the perf count is 1000 = 100 x 8 + 5 x 8 + 5 x 32 with fault-around on (default) and 965 = 100 x 8 + 5 x 1 + 5 x 32 with fault-around off
1,000 filemap:mm_filemap_add_to_page_cache
```
`echo 4096 > /sys/kernel/debug/fault_around_bytes` to disable fault-around, then cases 9-11 again:
```
case 9: one open NORMAL (no madvise); 100 cold misses, then 5 more cold reads | read via NORMAL | expected pages: fault_around_bytes=65536 (default): 105 x 32 pages, no fallback; fault_around_bytes=4096: 100 x 32 pages, then fallback to 5 x 1 after 100 misses. Events are pages/4 (8 per read-around, not 32) if the fs uses 16 KB folios, so the perf count is 840 = 100 x 8 + 5 x 8 with fault-around on (default) and 805 = 100 x 8 + 5 x 1 with fault-around off
805 filemap:mm_filemap_add_to_page_cache
case 10: one open SEQUENTIAL; 100 cold misses, then 5 more cold reads | read via SEQUENTIAL | expected pages: phase 1 = 100 x 32 = 3200, phase 2 = 5 x 32 = 160 (no fallback for SEQUENTIAL), total 3360
3,360 filemap:mm_filemap_add_to_page_cache
case 11: two opens NORMAL + SEQUENTIAL; 100 cold misses via NORMAL, then 5 via NORMAL and 5 via SEQUENTIAL | expected pages: fault_around_bytes=65536: 105 x 32 + 5 x 32; fault_around_bytes=4096: 100 x 32 + 5 x 1 + 5 x 32 (SEQUENTIAL never falls back; it does not consult mmap_miss). Events are pages/4 for the NORMAL reads if the fs uses 16 KB folios, so the perf count is 1000 = 100 x 8 + 5 x 8 + 5 x 32 with fault-around on (default) and 965 = 100 x 8 + 5 x 1 + 5 x 32 with fault-around off
965 filemap:mm_filemap_add_to_page_cache
```
As we can see in cases 1-8, advice is a per-VMA flag. Two fds, two mappings (cases 1-4): RANDOM reads 1 page and SEQUENTIAL reads 32, no matter the open order. One fd, one mapping, two clones (cases 5-8): the last `updateIOContext()` wins and decides the readahead behaviour for both clones, and closing a clone changes nothing. That's the benefit of a separate openInput (different fd, different VMA) that avoids, and I think it strengthens the choice made in this PR.
--
But I want to share one finding and call out here, for the idea of "same fd, different VMA", the concern above that "the merge's readahead window and the mmap_miss counter would mix with the searchers'", and the Claude's summary of "mmap_miss counter that disables readahead after ~100 misses", it's like a weaker point. The justification is "in theory" rather than something that can be seen in practice. Two reasons and vet in the test (I use one mapping same fd since it can also represent two mappings with same fd as they share a struct file).
First, mmap_miss is only incremented or consulted by an un-hinted (NORMAL) mapping (The Claude narrative doesn't emphasis on this), In do_sync_mmap_readahead, the VM_RAND_READ
and VM_SEQ_READ checks return before the mmap_miss++ (see kernel code [here](https://github.com/torvalds/linux/blob/v6.6/mm/filemap.c#L3168)). So a searcher advised RANDOM and a merge advised SEQUENTIAL cannot interfere even if they share a struct file. Case 10 (SEQUENTIAL alone): 100 cold misses and then 5 more reads, loads total 3360 = 105 x 32 pages. Case 11 (a NORMAL open + a SEQUENTIAL open, fault-around off): after the NORMAL mapping's 100 misses, it collapsed to 1 page, yet the SEQUENTIAL reads still load 32 aggressive pages, the mmap_miss counter doesn't affect SEQUENTIAL.
Second, even with two NORMAL mappings, the "~100 misses disables readahead" is practically unreachable with the default fault_around_bytes=65536. Case 9 shows: 105 cold misses through a NORMAL mapping, and read-around ran on each random read (840 = 105 x 8 with folios) with no degradation to 1 page after 100 misses. The reason is that a cold fault is retried, and on the retry fault-around runs ([code](https://github.com/torvalds/linux/blob/v6.6/mm/memory.c#L4560-L4564)) and maps the 16 pages from middle, counts each of them as a hit [erasing the misses counter](https://github.com/torvalds/linux/blob/v6.6/mm/filemap.c#L3612-L3616). Only by [disabling fault-around](https://github.com/torvalds/linux/blob/v6.6/mm/memory.c#L4547) (`echo 4096 > /sys/kernel/debug/fault_around_bytes`), case 9 drops to total 805 = 100 x 8 + 5 x 1 pages loading, read-around for the first 100 misses and exactly the one needed page from the 101st read.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "jimczi (via GitHub)" <gi...@apache.org>.
jimczi merged PR #16684:
URL: https://github.com/apache/lucene/pull/16684
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5830908707
So basically, I would open a clone for merging and change its read advice with a new IndexInput method when merging starts and revert at end of merging.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5831885275
To me this reads: It should be fine to have multiple mappings with different madvise. If a page is in cache already it won't affect anything (it just reads from cache). But if one of the call paths cause a page fault, the "handling of that page fault" is done using the madvise of the mapping. So indeed the merging code will preload pages due to sequential, but if a search thread causes a page fault, it will only load the requested page.
As the last section says, if only have an impact gloablly if you use `MADV_PAGEOUT` to remove pages from cache (which is Linux specific and not POSIX).
So I agree, opening the files two times is fine. In fact we could also change the whole thing to maybe use the same open file, but remap a second time to another virtual address. But as MMapDirectory closes the file directly after mapping, this won't work anyways. So basically I think your approach should work.
I think your approach with opening a seperate FD and mapping for merging is fine, but we should still look at how it behaves in production. For NIOFSDirectory we need to make sure that we don't run out of file handles! Maybe also leave a comment in #13920.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "jimczi (via GitHub)" <gi...@apache.org>.
jimczi commented on code in PR #16684:
URL: https://github.com/apache/lucene/pull/16684#discussion_r4146027129
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java:
##########
@@ -253,9 +272,12 @@ private void ensureOpen() throws AlreadyClosedException {
/** Close the underlying {@link IndexInput}s. */
@Override
- public void close() throws IOException {
+ public synchronized void close() throws IOException {
Review Comment:
Done in `286e95c`. `close()` wasn't synchronized before this PR, so that was mine to undo. Also made `closed` volatile in `3650061`, since it's read under the lock.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "jimczi (via GitHub)" <gi...@apache.org>.
jimczi commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5831413298
Thanks for looking. One correction on the mechanism first, since it's easy to check: read advice lives on the mapping, not on the file or the pages. `MADV_RANDOM` and `MADV_SEQUENTIAL` set `VM_RAND_READ` / `VM_SEQ_READ` on the VMAs of the range you pass. So a second open has its own advice, the mapping searches are on is untouched, and when the merge mapping is unmapped its flags go with it. The page cache is shared, which is the part we want.
On opening a clone and overriding its advice: a clone shares the same `MemorySegment` array (`buildSlice` returns `segments` when it is a clone) and `updateReadAdvice` walks that array, so advising a clone advises the original. Only a fresh open has advice of its own.
Doing this with `updateIOContext` is easy, nothing blocks it. My objection is not cost, it is that it makes the searchers' mapping sequential for as long as the merge runs. These are the big files, a `.fdt` is often larger than the whole page cache, which is why it is advised random in the first place, and the file a merge reads is a source that gets deleted at commit, so warming it cannot help a later search. That is what you said in #13920 as well, that signalling we read it once means it is good to drop it from the cache soon.
The other thing the second open buys is that the directory gets to decide. `updateIOContext` is an mmap notion, it re-advises a mapping that already exists, so `DirectIODirectory` or a hybrid directory never sees the merge and cannot pick direct I/O or a different input for it. Re-opening is the only point where that choice can be made.
The cost here is one extra mapping per stored fields reader that gets merged, opened lazily, page cache shared so nothing is cached twice, closed when the segment's reader is closed. If you would rather it went away as soon as the merge ends, we can add a finishMerge hook for stored fields like `KnnVectorsReader` has.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "ChrisHegarty (via GitHub)" <gi...@apache.org>.
ChrisHegarty commented on code in PR #16684:
URL: https://github.com/apache/lucene/pull/16684#discussion_r4144309582
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java:
##########
@@ -253,9 +272,12 @@ private void ensureOpen() throws AlreadyClosedException {
/** Close the underlying {@link IndexInput}s. */
@Override
- public void close() throws IOException {
+ public synchronized void close() throws IOException {
Review Comment:
You might want to avoid making this whole method synchronized, and just synchronize over the field access ( similar to https://github.com/apache/lucene/pull/16749#discussion_r4144167001)
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5830793543
Hi, no problem. I was involved in the original design of the how to differentiate between merges and searches. As you noted above, I am in favor of the following approach:
If a segment is already open for searches and it needs to do merging, I originally said: "chage the read advice" for the already open IndexInputs. When mergin is done, revert.
I still think this is the best approach, because the file system cache is a shared resource. So when a merge is done, the whole file will load into FS cache anyways, and the goal here is to optimize that. If there are searches in parallel
Basically your approach will do exactly the same: It opens a second instance on the same file, gets a fully new virtual memory slice and then uses it with a different read advice. But as it is the same file and some of the pages are shared between searcher and merger, the last one wins. When the merger is closed the changed read advices stay alive.
Therefor I'd like to have some method in indexinput to "temporarily override" read advice globally till the merge is done. After that it reverts to the original read advice.
I have not yet looked into the PR here, but as stated in #13920 - and repeated here - I'd like to investiagte this "temporary override" and not open the file multiple times.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5831759083
**That is what Claude Max (Fable 5.1 High) says:**
Short version: neither POSIX nor Linux has a notion of "advice on a file region". Advice is attached to an address range in the calling process, and on Linux that means to the VMA (`vm_area_struct`). What's shared between your two mappings is the page cache of the inode (and, if both come from the same `open()`, the `struct file` with its readahead state). So the question for each advice type is: does it change a per-VMA flag, act on page-table entries of that VMA, or act on the shared pages/inode?
**POSIX**
`posix_madvise()` is defined on a range of the process's address space, is purely advisory, and must not change program semantics. The spec says nothing about two mappings of the same file with conflicting advice; the only guarantee you get is MAP_SHARED coherence (both see the same data). Everything else is implementation-defined. Side note: glibc's `posix_madvise(POSIX_MADV_DONTNEED)` is a no-op on Linux, precisely because Linux's `MADV_DONTNEED` is destructive on anonymous memory and POSIX's version must not be.
**Linux, by advice type**
Per-VMA flag, independent for each mapping:
- `MADV_NORMAL` / `MADV_RANDOM` / `MADV_SEQUENTIAL` set `VM_RAND_READ` / `VM_SEQ_READ` on the VMA. They're consulted in `filemap_fault` → `do_sync_mmap_readahead`: `VM_SEQ_READ` forces a full-window readahead, `VM_RAND_READ` suppresses it, no hint uses the heuristic (`mmap_miss` counting). Since readahead fills the shared page cache, a `MADV_RANDOM` mapping still profits from pages that a `MADV_SEQUENTIAL` mapping of the same range pulled in — the flag only governs what faults *through this VMA* trigger.
- Subtlety: `file_ra_state` (window size and the `mmap_miss` counter that disables readahead after ~100 misses) lives in `struct file`, not the VMA. Two mappings created from the same fd share it; two `open()`s of the same file don't. A random-access mapping without a hint can thus throttle readahead for an un-hinted mapping of the same fd — while a `MADV_SEQUENTIAL` mapping bypasses the miss check entirely.
- `MADV_HUGEPAGE` / `MADV_NOHUGEPAGE`, `MADV_DONTFORK`, `MADV_WIPEONFORK`, `MADV_DODUMP`/`DONTDUMP`, `MADV_MERGEABLE`: all VMA flags, no cross-mapping effect.
Acts on this VMA's page tables only:
- `MADV_DONTNEED` on a file-backed shared mapping just zaps the PTEs of that range (`zap_page_range_single`). The page cache is untouched; the other mapping keeps its PTEs and the next access through the first one simply refaults. It is *not* a "drop this from cache" operation — that's `posix_fadvise(POSIX_FADV_DONTNEED)`, which works on the inode.
- `MADV_POPULATE_READ`/`WRITE` (5.14+) prefault this VMA; the page-cache side effect benefits everyone.
- `MADV_FREE` returns `EINVAL` on file-backed mappings.
Acts on shared state, visible through every mapping:
- `MADV_WILLNEED` on file mappings is basically forced readahead into the page cache (no PTEs installed). All mappings benefit.
- `MADV_COLD` / `MADV_PAGEOUT` (5.4+) walk the PTEs of this VMA and deactivate/reclaim the underlying folios — but they skip folios that are mapped elsewhere (the `mapcount != 1` / `folio_likely_mapped_shared` check), so a second mapping of the same range effectively pins pages against `MADV_PAGEOUT` from the first. Also, since ~5.11 `MADV_PAGEOUT` on file pages requires you to be the file's owner or have write permission/`CAP_SYS_ADMIN`.
- `MADV_REMOVE` punches a hole in the file itself. `MADV_COLLAPSE` (6.1+) collapses the page-cache folios, so it affects all mappings.
**Practical consequences**
Conflicting advice on overlapping mappings doesn't "fight"; each fault path just looks at the VMA it came through. The one real cross-talk is the shared `file_ra_state`, and the one surprise is that a duplicate mapping defeats `MADV_PAGEOUT`. Two other things worth knowing when you create many mappings: adjacent mappings of the same file with identical flags can be merged into one VMA, and `madvise` on a sub-range then splits it again — which is what eats into `vm.max_map_count`. And the flag-changing advices (`NORMAL`/`RANDOM`/`SEQUENTIAL`/`HUGEPAGE`) take the mmap lock in write mode, so hammering them concurrently from many threads serializes against page faults in the whole process.
--
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] Let merges read stored fields sequentially without disturbing searches [lucene]
Posted by "uschindler (via GitHub)" <gi...@apache.org>.
uschindler commented on PR #16684:
URL: https://github.com/apache/lucene/pull/16684#issuecomment-5887917858
Hi,
I like where this is going to! Maybe we can get rid of the `updateReadAdvice()` in `IndexInput` if we do the same on all merges :-)
I have not checked the whole implementation, so I am not the best person to do a in-depth review of the code. I have some questions, maybe I will just comment in some lines.
Anyways, looks good!
--
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