Skip to content

Fix DirectIOIndexInput seek to not read when position is within buffer #14320

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 5 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Bug Fixes

* GITHUB#14215: Fix degenerate case in HNSW where all vectors have the same score. (Ben Trent)

* GITHUB#14320: Fix DirectIOIndexInput seek to not read when position is within buffer (Chris Hegarty)

Changes in Runtime Behavior
---------------------
* GITHUB#14189: Bump floor segment size to 16MB in TieredMergePolicy and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,13 @@ public long getFilePointer() {
@Override
public void seek(long pos) throws IOException {
if (pos != getFilePointer()) {
seekInternal(pos);
final long absolutePos = pos + offset;
if (absolutePos >= filePos && absolutePos <= filePos + buffer.limit()) {
// the new position is within the existing buffer
buffer.position(Math.toIntExact(absolutePos - filePos));
} else {
seekInternal(pos); // do an actual seek/read
}
}
assert pos == getFilePointer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,30 @@ public void testUseDirectIODefaults() throws Exception {
OptionalLong.of(largeSize)));
}
}

// Ping-pong seeks should be really fast, since the position should be within buffer.
// The test should complete within sub-second times, not minutes.
public void testSeekSmall() throws IOException {
Path tmpDir = createTempDir("testSeekSmall");
try (Directory dir = getDirectory(tmpDir)) {
int len = atLeast(100);
try (IndexOutput o = dir.createOutput("out", newIOContext(random()))) {
byte[] b = new byte[len];
for (int i = 0; i < len; i++) {
b[i] = (byte) i;
}
o.writeBytes(b, 0, len);
}
try (IndexInput in = dir.openInput("out", newIOContext(random()))) {
for (int i = 0; i < 100_000; i++) {
in.seek(2);
assertEquals(2, in.readByte());
in.seek(1);
assertEquals(1, in.readByte());
in.seek(0);
assertEquals(0, in.readByte());
}
}
}
}
}