Skip to content

CASSANDRA-21094: Use POSIX_FADV_SEQUENTIAL for SSTable reads during compaction and streaming - #5133

Open
cheeeee wants to merge 1 commit into
apache:trunkfrom
cheeeee:CASSANDRA-21094-trunk
Open

CASSANDRA-21094: Use POSIX_FADV_SEQUENTIAL for SSTable reads during compaction and streaming#5133
cheeeee wants to merge 1 commit into
apache:trunkfrom
cheeeee:CASSANDRA-21094-trunk

Conversation

@cheeeee

@cheeeee cheeeee commented Sep 9, 2026

Copy link
Copy Markdown

Jira Issue

Motivation

Cassandra performs sequential scans during compaction and streaming via openDataReaderForScan(), but does not inform the kernel of this access pattern. On Linux, posix_fadvise with POSIX_FADV_SEQUENTIAL doubles the kernel readahead window (e.g. 128KB -> 256KB) and triggers asynchronous prefetching ahead of the application buffer fill.

Changes (Dual File Descriptor Pattern)

  1. NativeLibrary: Added trySetSequential overloads invoking wrappedLibrary.callPosixFadvise(fd, offset, len, POSIX_FADV_SEQUENTIAL).
  2. FileHandle: When forScan == true and diskAccessMode != DiskAccessMode.direct, applies trySetSequential on the open channel file descriptor using zero-allocation cached file().path().
  3. SSTableReader: In openDataReaderInternal(), when forScan == true, allocates a dedicated FileHandle with OnReaderClose.CLOSE_FILE. This ensures:
    • The sequential readahead advice never pollutes the main file descriptor used by point lookups.
    • The dedicated scan file descriptor is cleanly closed immediately upon scanner completion.
  4. Point Lookups Untouched: openDataReader() continues to share the primary dfile channel with zero extra FD overhead.

Testing & Kernel eBPF Verification

  • NativeLibraryTest: 3/3 tests pass (added testSetSequential).
  • SSTableReaderDataReaderTest: 10/10 tests pass (added readahead and dedicated handle lifecycle verification).
  • RandomAccessReaderTest (14/14 pass) and CompactionControllerTest (7/7 pass).
  • Linux Kernel eBPF Trace (kfunc:vmlinux:ext4_readahead):
    • Point queries (openDataReader()): ra_pages = 32 (128KB readahead window).
    • Compaction scan (openDataReaderForScan()): ra_pages = 64 (256KB readahead window, exactly doubled).
    • Reduced block I/O requests by 36% on sequential reads (1MB read in 7 requests instead of 11).

…eaming (CASSANDRA-21094)

Cassandra performs sequential scans during compaction and streaming
via openDataReaderForScan(), but did not previously inform the kernel
of this access pattern. On Linux, posix_fadvise with POSIX_FADV_SEQUENTIAL
doubles the kernel readahead window (128KB -> 256KB) and triggers
asynchronous prefetching ahead of the application buffer fill.

This patch implements the dual file descriptor pattern:
1. NativeLibrary: add trySetSequential(fd, offset, len, path) invoking
   POSIX_FADV_SEQUENTIAL via JNA.
2. FileHandle: apply trySetSequential on the open channel FD when
   forScan is true and diskAccessMode is not direct, avoiding redundant
   calls and using cached file().path() without allocations.
3. SSTableReader: when forScan is true in openDataReaderInternal(),
   allocate a dedicated FileHandle with OnReaderClose.CLOSE_FILE,
   preventing sequential advice from polluting the main FD used by
   random point lookups and ensuring clean FD closure when scanning ends.
4. Unit tests: add testSetSequential in NativeLibraryTest and comprehensive
   readahead window verification in SSTableReaderDataReaderTest proving
   kernel readahead doubling (32 -> 64 pages) on real SSTable reads.

Fixes: CASSANDRA-21094
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant