Repository navigation
Allow shorter reads in IoUring - #8466
Conversation
There was a problem hiding this comment.
There's an issue with this approach, because with this change we can't distinguish between EOF vs spurious short read.
When you request to read 10 bytes, it does not guarantee that all 10 bytes would be read. It can read less bytes, even if it did not yet reach EOF. To ensure that you reached EOF, you need to make another read (at current offset), and if that read returned 0, that should mean you reached EOF.
So, to not have to deal with this yet, I've just put assert_eq to ensure all reads match requested length.
UPD: Also I'm not sure if/how often these short reads actually happen. I'd guess it did not happen with old UringReader, but Qdrant is used with a lot of weird FS, and we want to enable uring for a lot more stuff, so I'd prefer to play it safe with short reads...
|
As partially discussed, in 9d755dc I have added the Within the io-uring implementation, it is used to dynamically choose to allow short reads. |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/common/common/src/universal_io/io_uring.rs`:
- Around line 108-110: The call to rt.state.read currently passes
self.uses_o_direct which makes every direct-I/O read permissively accept short
reads; change this so short-read tolerance is only enabled when the aligned
request actually crosses the current file length or by passing an explicit flag
from tail-read callers. Concretely, in the read method (and the analogous sites
at the other occurrences) compute the request end and compare it against the
file's current length (via metadata/file_len) and only set the short-read flag
when end > file_len (EOF overrun), or add an explicit parameter (e.g.,
allow_short_read) to the read/read_batch/read_multi call sites and propagate
that from tail-read callers; update the rt.state.read invocation to use that
derived/explicit boolean instead of self.uses_o_direct.
- Around line 88-94: The code opens the file descriptor with O_DIRECT when
prevent_caching is true (see the OpenOptions setup using
opts.custom_flags(nix::libc::O_DIRECT)), but the request buffers used by
read()/write()/populate() are not guaranteed to meet Linux O_DIRECT alignment
requirements; allocate and use page-aligned buffers for all direct-I/O paths
(e.g., use posix_memalign/mmap to create buffers aligned to the system
page/sector size and ensure io_uring opcode::Read/opcode::Write submissions
reference those aligned buffers), or alternatively keep the existing O_DIRECT fd
for direct-I/O only and open a second non-O_DIRECT descriptor for generic
read/write/populate paths so they use ordinary (non-aligned)
Vec<MaybeUninit<u8>> buffers; apply this change to the code paths that call
read(), write(), and populate() so all O_DIRECT I/O uses the aligned buffer or
the non-O_DIRECT fd.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 5eb3c9ca-f982-47ea-af08-181d4b70dd42
📒 Files selected for processing (8)
lib/common/common/src/universal_io/io_uring.rslib/common/common/src/universal_io/mmap.rslib/common/common/src/universal_io/mod.rslib/gridstore/src/pages.rslib/gridstore/src/tracker.rslib/segment/src/index/field_index/stored_point_to_values.rslib/segment/src/vector_storage/chunked_vectors.rslib/segment/src/vector_storage/dense/immutable_dense_vectors.rs
| let mut opts = fs::OpenOptions::new(); | ||
| opts.read(true); | ||
| opts.write(true); | ||
| opts.create(false); | ||
| if prevent_caching.unwrap_or_default() { | ||
| opts.custom_flags(nix::libc::O_DIRECT); | ||
| } |
There was a problem hiding this comment.
O_DIRECT is enabled, but the request buffers are not direct-I/O aligned.
Vec<MaybeUninit<T>> only guarantees alignment for T. For the u8/small-POD cases this option targets, that is below the block/page alignment Linux requires for O_DIRECT, so these reads can fail with EINVAL before the new short-read logic ever runs. Because the same fd is also reused by write() and populate(), those paths inherit the same constraint too. Please use a page-aligned buffer for direct I/O, or keep a separate non-O_DIRECT descriptor for the generic paths.
On Linux, when a file descriptor is opened with O_DIRECT, what alignment requirements apply to the userspace buffer address, I/O length, and file offset for read/write syscalls and io_uring opcode::Read/opcode::Write?
Also applies to: 462-479
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@lib/common/common/src/universal_io/io_uring.rs` around lines 88 - 94, The
code opens the file descriptor with O_DIRECT when prevent_caching is true (see
the OpenOptions setup using opts.custom_flags(nix::libc::O_DIRECT)), but the
request buffers used by read()/write()/populate() are not guaranteed to meet
Linux O_DIRECT alignment requirements; allocate and use page-aligned buffers for
all direct-I/O paths (e.g., use posix_memalign/mmap to create buffers aligned to
the system page/sector size and ensure io_uring opcode::Read/opcode::Write
submissions reference those aligned buffers), or alternatively keep the existing
O_DIRECT fd for direct-I/O only and open a second non-O_DIRECT descriptor for
generic read/write/populate paths so they use ordinary (non-aligned)
Vec<MaybeUninit<u8>> buffers; apply this change to the code paths that call
read(), write(), and populate() so all O_DIRECT I/O uses the aligned buffer or
the non-O_DIRECT fd.
There was a problem hiding this comment.
🧹 Nitpick comments (2)
lib/common/common/src/universal_io/io_uring.rs (2)
581-586: Use explicit field ignoring per coding guidelines.The struct pattern uses
..to ignoreallow_short_read. Per coding guidelines, prefer explicit field ignoring with: _.📝 Suggested change
pub fn expect_read(&mut self) -> &mut Vec<MaybeUninit<T>> { #[expect(clippy::match_wildcard_for_single_variants)] match self { - IoUringRequest::Read { buffer, .. } => buffer, + IoUringRequest::Read { buffer, allow_short_read: _ } => buffer, _ => panic!(), } }As per coding guidelines: "Prefer explicit field ignoring using
: _over using..in Rust struct destructuring".🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/common/common/src/universal_io/io_uring.rs` around lines 581 - 586, The match arm in expect_read uses a wildcard struct rest (`..`) to ignore the allow_short_read field; update the pattern to explicitly ignore that field by naming it and using `: _` (i.e., match IoUringRequest::Read { buffer, allow_short_read: _, .. } but remove any other `..` so only allow_short_read is explicitly ignored) so the Read variant pattern uses explicit field ignoring per guidelines; modify the IoUringRequest::Read pattern in expect_read accordingly.
530-537: Add debug_assert for element-aligned byte count.The floor division on line 531 will silently drop any trailing bytes if the kernel returns a partial element. While this is unlikely for well-formed files, a debug_assert would catch unexpected kernel behavior during development.
🛡️ Suggested guard
IoUringRequest::Read { buffer: mut items, allow_short_read, } => { if allow_short_read { + debug_assert_eq!( + byte_length as usize % mem::size_of::<T>(), + 0, + "read returned a partial item" + ); let actual_items = byte_length as usize / mem::size_of::<T>(); debug_assert!( actual_items <= items.len(), "read returned more bytes than requested" );Based on learnings:
debug_assert!is preferred over runtime checks that would panic in release builds to ensure production stability.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/common/common/src/universal_io/io_uring.rs` around lines 530 - 537, Add a debug-only assertion in the allow_short_read branch to ensure the returned byte_length is aligned to the element size: after computing actual_items from byte_length and mem::size_of::<T>(), add debug_assert!(byte_length as usize % mem::size_of::<T>() == 0, "read returned a partial element") so we detect unexpected partial-element reads during development before truncating items (references: byte_length, T, mem::size_of::<T>(), actual_items, items.truncate).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lib/common/common/src/universal_io/io_uring.rs`:
- Around line 581-586: The match arm in expect_read uses a wildcard struct rest
(`..`) to ignore the allow_short_read field; update the pattern to explicitly
ignore that field by naming it and using `: _` (i.e., match IoUringRequest::Read
{ buffer, allow_short_read: _, .. } but remove any other `..` so only
allow_short_read is explicitly ignored) so the Read variant pattern uses
explicit field ignoring per guidelines; modify the IoUringRequest::Read pattern
in expect_read accordingly.
- Around line 530-537: Add a debug-only assertion in the allow_short_read branch
to ensure the returned byte_length is aligned to the element size: after
computing actual_items from byte_length and mem::size_of::<T>(), add
debug_assert!(byte_length as usize % mem::size_of::<T>() == 0, "read returned a
partial element") so we detect unexpected partial-element reads during
development before truncating items (references: byte_length, T,
mem::size_of::<T>(), actual_items, items.truncate).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 68a134aa-f8d0-4226-8b9e-f14221112069
📒 Files selected for processing (8)
lib/common/common/src/universal_io/io_uring.rslib/common/common/src/universal_io/mmap.rslib/common/common/src/universal_io/mod.rslib/gridstore/src/pages.rslib/gridstore/src/tracker.rslib/segment/src/index/field_index/stored_point_to_values.rslib/segment/src/vector_storage/chunked_vectors.rslib/segment/src/vector_storage/dense/immutable_dense_vectors.rs
✅ Files skipped from review due to trivial changes (5)
- lib/segment/src/vector_storage/chunked_vectors.rs
- lib/gridstore/src/tracker.rs
- lib/segment/src/vector_storage/dense/immutable_dense_vectors.rs
- lib/segment/src/index/field_index/stored_point_to_values.rs
- lib/common/common/src/universal_io/mmap.rs
🚧 Files skipped from review as they are similar to previous changes (1)
- lib/common/common/src/universal_io/mod.rs
* use assume_init_vec * allow shorter reads at EOF * add prevent_caching openoption, allow short read dynamically * codespell * fix rebase
* use assume_init_vec * allow shorter reads at EOF * add prevent_caching openoption, allow short read dynamically * codespell * fix rebase
For using io-uring for disk-cache, sometimes the aligned reads mean that we will go past EOF.
O_DIRECTrequires reads to be aligned to some page/block size.Imagine having a file with a slice of 128b items. When having block-size 4096, the file can have length of 4864, so we necessarily have to issue a read for the last bytes as 4096..8012, even when we will use only 4096..4864
This PR allows this to happen, and truncates the returned vector to account for it.