Repository navigation
feat(stats): expose oldest reader commit id and age - #51
Merged
Merged
Conversation
mkhairi
marked this pull request as ready for review
November 5, 2026 05:09
mkhairi
force-pushed
the
feat/stats-oldest-reader
branch
from
November 5, 2026 05:44
c0bf13f to
cddc3c2
Compare
farhan-syah
requested changes
Nov 5, 2026
farhan-syah
left a comment
Member
There was a problem hiding this comment.
Changes requested. No blockers: the census logic is correct.
Rebase onto current main first:
Dependency audit:mainnow has tokio 1.54.1 (6ec8e0b), andcargo denypasses there. The rebase reruns this job on your branch.CHANGELOG.md: this PR and #52 both add### Addedunder[Unreleased], so they conflict. The PR that merges second needs one more rebase.
Then make the four inline changes.
Checked:
- Both PRs merged onto
main:cargo clippy --all-targets --all-features -- -D warningsis clean, andcargo nextest run --all-featurespasses. .max()mutant inreader_census:stats_reports_reader_censusfails, as you state.
The issue-first rule in CONTRIBUTING.md is waived for this PR. You do not need to open an issue.
A long-lived read transaction pins the pages freed after its snapshot, so free-page reuse stalls and the file grows. `Db::stats()` reported only `tracked_readers`, which says a reader exists but not which commit it pins or how long it has held it. An operator could not tell a harmless short read from the one blocking reuse. `DbStats` gains three fields: - `oldest_reader_commit_id`: the smallest pinned commit id. - `oldest_reader_age_ms`: age of the longest-lived reader. - `tracked_readers_non_abortable`: the subset of `tracked_readers` exempt from reader-stall abort. The first two are `None` when no reader is registered. Each tracked reader records its start time through `clock::Instant`, which is `web_time::Instant` on the browser target, where `std::time::Instant::now()` panics, and `std::time::Instant` everywhere else. The clock is read before the tracked-readers lock is taken. The longest-lived reader is not always the one pinning the oldest commit: `begin_read_at` can open a new reader on an older commit. The `oldest_reader_commit_id` doc also notes that retained commit history bounds reuse, so `None` does not mean nothing blocks it. `stats()` reads all reader figures in one pass under a single lock via `reader_census`, so the count, oldest commit and age describe the same set of readers. The test checks the idle case, a pinned reader with a newer non-abortable one (the oldest commit and the age must both be the older reader's), age growth across a sleep, and a return to `None` after both drop. A second test opens a reader with `begin_read_at` on an older commit after a longer-lived one, and checks that the oldest commit and the age then come from different readers.
mkhairi
force-pushed
the
feat/stats-oldest-reader
branch
from
November 5, 2026 08:52
cddc3c2 to
d746a86
Compare
Member
Author
|
Rebased onto
All local gates pass on stable. |
farhan-syah
approved these changes
Nov 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A long-lived read transaction pins the pages freed after its snapshot. Free-page reuse stalls behind it, and the file grows.
Db::stats()reportstracked_readersonly. That says a reader exists. It does not say which commit the reader pins or how long it has held it. An operator who sees the file grow cannot tell a harmless short read from the one blocking reuse.Change
DbStatsgains three fields:oldest_reader_commit_id: Option<u64>— the smallest commit id any registered reader pins.oldest_reader_age_ms: Option<u64>— the age of the longest-lived reader.tracked_readers_non_abortable: u32— the subset oftracked_readersexempt from reader-stall abort.Both
Optionfields areNonewhen no reader is registered. Each tracked reader records its start time through a newclock::Instantre-export. It isweb_time::Instantonwasm32-unknown-unknown, wherestd::time::Instant::now()panics, andstd::time::Instanteverywhere else.register_readreads the clock before it takes the tracked-readers lock. It uses the same cfg asclock::unix_seconds, sowasm32-wasip1reports the age too.The longest-lived reader is not always the one pinning the oldest commit:
begin_read_atcan open a new reader on an older commit. Theoldest_reader_age_msdoc comment says so.Readers are not the only bound on free-page reuse. Retained commit history holds back pages freed after the oldest retained commit. The
oldest_reader_commit_iddoc says so, soNoneis not read as "nothing blocks reuse".stats()reads all reader figures in one pass under a single lock (reader_census). The count, the oldest commit and the age therefore describe the same set of readers.Compatibility
DbStatsis#[non_exhaustive], so the new fields are additive for downstream code. No on-disk format change. No durability-ordering change. No new dependency:web-timeis already a dependency of the browser target.Testing
Two tests in
tests/stats_basic.rs.stats_reports_reader_censuscovers:Optionfields areNone.max()in place of.min()in the census, the age assertion fails.Noneafter both readers dropstats_reader_census_fields_can_describe_different_readersopens a reader on the latest commit, waits 10 ms, then opens a reader withbegin_read_aton an older commit.oldest_reader_commit_idis the second reader's commit andoldest_reader_age_msis the first reader's age. Taking the age from the oldest-commit reader (min_by_key(|r| r.commit_id.value())) fails the age assertion.Gates run locally on stable, one at a time:
cargo fmt --all --check,cargo clippy --all-targets --all-features -- -D warnings,cargo nextest run --all-features,cargo test --doc --all-features,cargo doc --no-deps --all-featureswithRUSTDOCFLAGS="-D warnings", andcargo check -p pagedb --libforwasm32-unknown-unknown --features opfsandwasm32-wasip1.CHANGELOG.mdhas an entry under[Unreleased]→Added.