Skip to content

[storage] Keep fewer files open during a checkpoint - #8248

Open
blp wants to merge 3 commits into
mainfrom
checkpoint-fewer-fds
Open

blp wants to merge 3 commits into
mainfrom
checkpoint-fewer-fds

Conversation

@blp

@blp blp commented Dec 5, 2026

Copy link
Copy Markdown
Member

A checkpoint kept one file descriptor open for each file that it wrote, until the commit. The number of open descriptors grew with workers × checkpointed operators. At 16 workers, a large program passed the 1,048,576 limit, and a merge panicked with "too many open files".

Issue: #8173

Commit Change
[dbsp] Report how many storage files are open. New files_open gauge: the storage files that the process has open.
[storage] Collect a checkpoint's files in a shared CommitSet. StorageBackend::commit_set() returns a CommitSet that the checkpoint threads share. It replaces commit_all() and sync_files(). Under syncfs, the POSIX set only counts the files and keeps none open. The memory backend uses the new NoopCommitSet.
[storage] Make a checkpoint's files durable in groups under per-file sync. Under per-file sync, the set makes its files durable in groups of 1,024, one group at a time. The set does not close a file and open it again to fsync it, because then the kernel can lose a write error.

Open files during a checkpoint:

Sync mode Before After
syncfs (default on Linux) every file in the checkpoint none kept by the checkpoint
per-file every file in the checkpoint about 1,024 for each worker thread, at most

storage_commit_all_latency_seconds still has one observation for each checkpoint. Under per-file sync, the observation includes the groups that were made durable before the commit.

No changelog entry: this is an internal fix and a new metric.

Later work, not in this PR:

  • The cloud ObjectBackend can use NoopCommitSet.
  • Remove the default commit_set, so that each backend supplies its own set.

Testing

  • New integration tests crates/dbsp/tests/open_files.rs and crates/dbsp/tests/checkpoint_sync.rs check files_open and the sync and commit counts for each sync mode. They also check a second commit, with groups and without.
  • New unit tests: per_file_commit_set_syncs_in_groups (POSIX) and commit_set_keeps_no_files (memory).
  • I reverted each fix and checked that the matching test fails.
  • cargo test: dbsp storage, spine, checkpoint and lazy_input_upsert tests, all dbsp integration tests, feldera-storage, and static-connectors storage:: tests pass. Clippy is clean.

Written by Claude

blp added 3 commits December 3, 2026 16:44
A pipeline at 16 workers ran out of file descriptors in storage, but
process_open_fds does not say how many of them storage holds, or when.
The new files_open gauge counts the storage files that the process has
open: one for each file being written and one for each file being read.

Issue: #8173
Signed-off-by: Ben Pfaff <blp@feldera.com>
A checkpoint collects a committer for each of its files, for every spine
in every worker, and keeps them all until commit_all. Each committer was
the file's reader, so each kept its file descriptor: the 2 metadata
files of each spine, the copy on storage of each batch in memory, and
any batch on storage that a merge replaced before the commit. A large
program at 16 workers passed the 1,048,576 descriptor limit, and then a
merge failed with "too many open files".

Under syncfs, which commits every file with one system call, the
checkpoint does not need the committers at all.

Now StorageBackend::commit_set() returns a CommitSet, a trait object
that all of the threads that write a checkpoint share, and that makes
the files durable in CommitSet::commit(). It replaces commit_all() and
sync_files(). Under syncfs, the POSIX backend's set only counts its
files and drops each one as it is added, which closes the file unless a
batch still uses it. Under per-file sync, the set keeps the files until
the commit, as before.

Issue: #8173
Signed-off-by: Ben Pfaff <blp@feldera.com>
…sync.

Under per-file sync, a checkpoint keeps each of its files open until it
commits, because the committer of each file is its reader. A large
program at 16 workers passed the 1,048,576 descriptor limit this way.

Closing each file and opening it again to fsync it would bound the
open files, but it can lose a write error: the kernel records the error
in the file's inode, and once nothing keeps the file open, it can evict
the inode, so that the later fsync reports success.

Now the per-file CommitSet makes its files durable each time it has
collected 1,024 of them, and keeps them open until then. It makes one
group durable at a time, and a thread that fills a group waits for its
turn before it writes more files, so at most about one group for each
worker thread is open. The time it records for the checkpoint is the
time that passed.

Issue: #8173
Signed-off-by: Ben Pfaff <blp@feldera.com>
@blp blp added bug Something isn't working DBSP core Related to the core DBSP library storage Persistence for internal state in DBSP operators metrics Metrics about feldera pipelines labels Dec 5, 2026
@blp blp self-assigned this Dec 5, 2026
@blp
blp requested a review from swanandx December 5, 2026 00:50
}

impl StorageBackend for MemoryBackend {
/// A [MemoryReader] needs nothing to become durable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug (medium, CI): this breaks the cargo-doc pre-commit hook (RUSTDOCFLAGS="-D warnings" cargo doc --document-private-items): public documentation for commit_set links to private item MemoryReader (rustdoc::private-intra-doc-links). Drop the link, e.g.:

Suggested change
/// A [MemoryReader] needs nothing to become durable.
/// A memory file needs nothing to become durable.

@mythical-fred-oss

Copy link
Copy Markdown
Contributor

Review: shared CommitSet keeps fewer files open during a checkpoint (#8173)

What I ran (debug build):

Check Result
cargo test -p feldera-storage ✅
cargo test -p dbsp --lib filtered to storage::backend, checkpoint and remove_stamp ✅ 26 / 36 / 6 tests
cargo test -p dbsp --test open_files --test checkpoint_sync --test checkpoint_blocking_phase ✅
pre-commit run --files <changed> ❌ Cargo Documentation fails (see the inline comment). Clippy, fmt and machete pass.
  • bug (medium): the pre-merge-queue check fails. The cargo-doc hook rejects a private intra-doc link. The fix is one line, suggested inline.
  • missing (medium): no test runs PerFileCommitSet from several threads. In production, all of the dbsp-commit-* threads share the set, but every test adds from a single thread. The error path is also untested: a group sync that fails inside add(), and a failure in commit(). I wrote this test locally, and it passes. Consider adding it to crates/storage/src/lib.rs:
test diff
#[cfg(test)]
mod commit_set_tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    static COMMITTED: AtomicUsize = AtomicUsize::new(0);
    #[derive(Debug)]
    struct Fake(StoragePath, bool);
    impl FileRw for Fake {
        fn file_id(&self) -> FileId { FileId::new() }
        fn path(&self) -> &StoragePath { &self.0 }
    }
    impl FileCommitter for Fake {
        fn commit(&self) -> Result<(), StorageError> {
            if self.1 { return Err(StorageError::stdio(ErrorKind::Other, "fsync", "x")); }
            COMMITTED.fetch_add(1, Ordering::Relaxed);
            Ok(())
        }
    }
    #[test]
    fn concurrent_adds_and_errors() {
        let set = Arc::new(PerFileCommitSet::new(commit_each).with_group_files(7));
        let hs: Vec<_> = (0..8).map(|_| { let set = set.clone(); std::thread::spawn(move || {
            for i in 0..1000 {
                set.add(Arc::new(Fake(format!("f{i}").into(), false))).unwrap();
                assert!(set.pending() <= 7);
            }
        })}).collect();
        hs.into_iter().for_each(|h| h.join().unwrap());
        set.commit().unwrap();
        assert_eq!(COMMITTED.load(Ordering::Relaxed), 8000);
        // A failing file in a full group fails `add`; in the remainder, `commit`.
        let set = PerFileCommitSet::new(commit_each).with_group_files(2);
        set.add(Arc::new(Fake("a".into(), true))).unwrap();
        assert!(set.add(Arc::new(Fake("b".into(), false))).is_err());
        set.add(Arc::new(Fake("c".into(), true))).unwrap();
        assert!(set.commit().is_err());
    }
}

Everything else checks out. The design is sound: syncfs on Linux 5.8 or later reports writeback errors through the superblock, so it is safe to close files early, and the per-file path keeps each file open until it is durable. The files_open metric is documented, and the PR adds no unsafe code, no new dependencies and no breaking changes. The process-global counter tests are each the only test in their file, so I see no flakiness risk.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working DBSP core Related to the core DBSP library metrics Metrics about feldera pipelines storage Persistence for internal state in DBSP operators

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant