Skip to content

perf: optimize fsck with verified_at timestamp - #998

Merged
kalbasit merged 2 commits into
mainfrom
02-28-perf_optimize_fsck_with_verified_at_timestamp
Mar 1, 2026
Merged

kalbasit merged 2 commits into
mainfrom
02-28-perf_optimize_fsck_with_verified_at_timestamp

Conversation

@kalbasit

@kalbasit kalbasit commented Mar 1, 2026

Copy link
Copy Markdown
Owner

This change introduces a verified_at timestamp to the nar_files table and a new --verified-since flag to the fsck command. This allows the tool to skip NARs that have been verified recently, significantly reducing the time required for subsequent fsck runs, especially for large chunked NARs.

How:

  1. Created database migrations for SQLite, PostgreSQL, and MySQL to add the verified_at column.
  2. Updated SQL queries and regenerated Go code to support the new column.
  3. Added a shouldCheckNar helper and updated fsck logic to skip NARs verified within the specified duration.
  4. Updated fsck to update verified_at whenever a successful check is performed.
  5. Added a new test case TestFsckBackends/.../VerifiedSince to verify the optimization works as expected.

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request go Pull requests that update go code labels Mar 1, 2026

kalbasit commented Mar 1, 2026 •

Copy link
Copy Markdown
Owner Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request delivers a significant performance enhancement to the fsck command by introducing a mechanism to skip recently verified NAR files. By persisting a verified_at timestamp in the database and exposing a new --verified-since flag, the tool can intelligently bypass redundant checks, leading to faster execution times, especially for large datasets. This change involves database schema modifications, updates to data access layers, and core fsck logic adjustments to leverage this new optimization.

Highlights

  • Performance Optimization for fsck: Introduced a verified_at timestamp to the nar_files table across MySQL, PostgreSQL, and SQLite databases to track when a NAR (Nix Archive) file was last successfully verified.
  • New --verified-since Flag: Added a new command-line flag --verified-since to the fsck command, allowing users to specify a duration. NARs verified more recently than this duration will be skipped during the fsck run, significantly reducing execution time for subsequent checks.
  • Database and Code Integration: Updated SQL queries, Go models, and database wrappers to incorporate the new verified_at column. The fsck logic now updates this timestamp upon successful verification of a NAR file.
  • Skipping Logic: Implemented shouldCheckNar helper function to determine if a NAR file should be skipped based on its verified_at timestamp and the --verified-since flag, improving efficiency by avoiding redundant checks.
  • Comprehensive Testing: Added a dedicated test case (TestFsckBackends/.../VerifiedSince) to ensure the --verified-since optimization functions correctly across different database backends.
Changelog
  • db/migrations/mysql/20260301000000_add_verified_at_to_nar_files.sql
    • Added verified_at column of type TIMESTAMP NULL to the nar_files table.
  • db/migrations/postgres/20260301000000_add_verified_at_to_nar_files.sql
    • Added verified_at column of type TIMESTAMP WITH TIME ZONE to the nar_files table.
  • db/migrations/sqlite/20260301000000_add_verified_at_to_nar_files.sql
    • Added verified_at column of type TIMESTAMP to the nar_files table.
  • db/query.mysql.sql
    • Updated SELECT statements for GetNarFileByHashAndCompressionAndQuery, GetNarFileByNarInfoID, GetOrphanedNarFiles, and GetAllNarFiles to include the new verified_at column.
    • Added a new UpdateNarFileVerifiedAt query to set the verified_at timestamp to CURRENT_TIMESTAMP.
  • db/query.postgres.sql
    • Updated SELECT statements for GetNarFileByHashAndCompressionAndQuery, GetNarFileByNarInfoID, GetOrphanedNarFiles, and GetAllNarFiles to include the new verified_at column.
    • Added a new UpdateNarFileVerifiedAt query to set the verified_at timestamp to CURRENT_TIMESTAMP.
  • db/query.sqlite.sql
    • Updated SELECT statements for GetNarFileByHashAndCompressionAndQuery, GetNarFileByNarInfoID, GetOrphanedNarFiles, and GetAllNarFiles to include the new verified_at column.
    • Added a new UpdateNarFileVerifiedAt query to set the verified_at timestamp to CURRENT_TIMESTAMP.
  • pkg/database/generated_models.go
    • Added VerifiedAt field of type sql.NullTime to GetAllNarFilesRow and NarFile structs.
  • pkg/database/generated_querier.go
    • Updated CreateNarFile query comment to include verified_at in the returned fields.
    • Updated GetAllNarFiles query comment to include verified_at.
    • Updated GetNarFileByHashAndCompressionAndQuery query comment to include verified_at.
    • Updated GetNarFileByNarInfoID query comment to include verified_at.
    • Updated GetOrphanedNarFiles query comment to include verified_at.
    • Added UpdateNarFileVerifiedAt method to the Querier interface.
  • pkg/database/generated_wrapper_mysql.go
    • Updated GetAllNarFiles to map the VerifiedAt field.
    • Updated GetNarFileByHashAndCompressionAndQuery to map the VerifiedAt field.
    • Updated GetNarFileByID to include verified_at in the SQL query and map the field.
    • Updated GetNarFileByNarInfoID to map the VerifiedAt field.
    • Updated GetOrphanedNarFiles to map the VerifiedAt field.
    • Added UpdateNarFileVerifiedAt wrapper method.
  • pkg/database/generated_wrapper_postgres.go
    • Updated CreateNarFile to map the VerifiedAt field.
    • Updated GetAllNarFiles to map the VerifiedAt field.
    • Updated GetNarFileByHashAndCompressionAndQuery to map the VerifiedAt field.
    • Updated GetNarFileByID to include verified_at in the SQL query and map the field.
    • Updated GetNarFileByNarInfoID to map the VerifiedAt field.
    • Updated GetOrphanedNarFiles to map the VerifiedAt field.
    • Added UpdateNarFileVerifiedAt wrapper method.
  • pkg/database/generated_wrapper_sqlite.go
    • Updated CreateNarFile to map the VerifiedAt field.
    • Updated GetAllNarFiles to map the VerifiedAt field.
    • Updated GetNarFileByHashAndCompressionAndQuery to map the VerifiedAt field.
    • Updated GetNarFileByID to include verified_at in the SQL query and map the field.
    • Updated GetNarFileByNarInfoID to map the VerifiedAt field.
    • Updated GetOrphanedNarFiles to map the VerifiedAt field.
    • Added UpdateNarFileVerifiedAt wrapper method.
  • pkg/database/mysqldb/models.go
    • Added VerifiedAt field of type sql.NullTime to the NarFile struct.
  • pkg/database/mysqldb/querier.go
    • Updated GetAllNarFiles query comment to include verified_at.
    • Updated GetNarFileByHashAndCompressionAndQuery query comment to include verified_at.
    • Updated GetNarFileByNarInfoID query comment to include verified_at.
    • Updated GetOrphanedNarFiles query comment to include verified_at.
    • Added UpdateNarFileVerifiedAt method to the Querier interface.
  • pkg/database/mysqldb/query.mysql.sql.go
    • Updated getAllNarFiles SQL query and GetAllNarFilesRow struct to include verified_at.
    • Updated getNarFileByHashAndCompressionAndQuery SQL query and GetNarFileByHashAndCompressionAndQueryRow struct to include verified_at.
    • Updated getNarFileByNarInfoID SQL query and GetNarFileByNarInfoIDRow struct to include verified_at.
    • Updated getOrphanedNarFiles SQL query and GetOrphanedNarFilesRow struct to include verified_at.
    • Added updateNarFileVerifiedAt SQL query and corresponding UpdateNarFileVerifiedAt function.
  • pkg/database/postgresdb/models.go
    • Added VerifiedAt field of type sql.NullTime to the NarFile struct.
  • pkg/database/postgresdb/querier.go
    • Updated CreateNarFile query comment to include verified_at.
    • Updated GetAllNarFiles query comment to include verified_at.
    • Updated GetNarFileByHashAndCompressionAndQuery query comment to include verified_at.
    • Updated GetNarFileByNarInfoID query comment to include verified_at.
    • Updated GetOrphanedNarFiles query comment to include verified_at.
    • Added UpdateNarFileVerifiedAt method to the Querier interface.
  • pkg/database/postgresdb/query.postgres.sql.go
    • Updated createNarFile SQL query and CreateNarFile function to include verified_at.
    • Updated getAllNarFiles SQL query and GetAllNarFilesRow struct to include verified_at.
    • Updated getNarFileByHashAndCompressionAndQuery SQL query and GetNarFileByHashAndCompressionAndQuery function to include verified_at.
    • Updated getNarFileByNarInfoID SQL query and GetNarFileByNarInfoID function to include verified_at.
    • Updated getOrphanedNarFiles SQL query and GetOrphanedNarFiles function to include verified_at.
    • Added updateNarFileVerifiedAt SQL query and corresponding UpdateNarFileVerifiedAt function.
  • pkg/database/sqlitedb/models.go
    • Added VerifiedAt field of type sql.NullTime to the NarFile struct.
  • pkg/database/sqlitedb/querier.go
    • Updated CreateNarFile query comment to include verified_at.
    • Updated GetAllNarFiles query comment to include verified_at.
    • Updated GetNarFileByHashAndCompressionAndQuery query comment to include verified_at.
    • Updated GetNarFileByNarInfoID query comment to include verified_at.
    • Updated GetOrphanedNarFiles query comment to include verified_at.
    • Added UpdateNarFileVerifiedAt method to the Querier interface.
  • pkg/database/sqlitedb/query.sqlite.sql.go
    • Updated createNarFile SQL query and CreateNarFile function to include verified_at.
    • Updated getAllNarFiles SQL query and GetAllNarFilesRow struct to include verified_at.
    • Updated getNarFileByHashAndCompressionAndQuery SQL query and GetNarFileByHashAndCompressionAndQuery function to include verified_at.
    • Updated getNarFileByNarInfoID SQL query and GetNarFileByNarInfoID function to include verified_at.
    • Updated getOrphanedNarFiles SQL query and GetOrphanedNarFiles function to include verified_at.
    • Added updateNarFileVerifiedAt SQL query and corresponding UpdateNarFileVerifiedAt function.
  • pkg/ncps/fsck.go
    • Added a DurationFlag named verified-since to the fsck command.
    • Modified collectFsckSuspects function signature to accept verifiedSince duration.
    • Added a skipped counter to fsck progress logging.
    • Implemented shouldCheckNar helper function to determine if a NAR should be skipped based on verified_at and verifiedSince.
    • Integrated shouldCheckNar into collectFsckSuspects to skip recently verified NARs.
    • Added logic to update verified_at for successfully checked NAR files in collectFsckSuspects and collectNarFilesWithChunkIssues.
  • pkg/ncps/fsck_test.go
    • Imported the time package.
    • Added testFsckVerifiedSince to the runFsckSuite to test the new --verified-since flag functionality.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@dosubot

dosubot Bot commented Mar 1, 2026 •

Copy link
Copy Markdown

Related Documentation

Checked 7 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request introduces a performance optimization for the fsck command by adding a verified_at timestamp to the nar_files table. This allows skipping recently verified NARs, which is a great improvement for large caches. The implementation looks solid, with database migrations for all supported backends, updated queries, and a new test case to verify the optimization.

I have two suggestions for improvement:

  • In pkg/ncps/fsck.go, the new shouldCheckNar helper function contains some dead code and could be made more type-safe.
  • The SQLite down migration is incomplete, which could affect development workflows. Implementing it would improve maintainability.

Comment thread pkg/ncps/fsck.go Resolved
@codecov

codecov Bot commented Mar 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.04%. Comparing base (37d08fc) to head (0694488).

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #998   +/-   ##
=====================================
  Coverage   5.04%   5.04%           
=====================================
  Files          6       6           
  Lines        436     436           
=====================================
  Hits          22      22           
  Misses       410     410           
  Partials       4       4           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This change introduces a verified_at timestamp to the nar_files table and a new --verified-since flag to the fsck command. This allows the tool to skip NARs that have been verified recently, significantly reducing the time required for subsequent fsck runs, especially for large chunked NARs.

How:
1. Created database migrations for SQLite, PostgreSQL, and MySQL to add the verified_at column.
2. Updated SQL queries and regenerated Go code to support the new column.
3. Added a shouldCheckNar helper and updated fsck logic to skip NARs verified within the specified duration.
4. Updated fsck to update verified_at whenever a successful check is performed.
5. Added a new test case TestFsckBackends/.../VerifiedSince to verify the optimization works as expected.
@kalbasit
kalbasit force-pushed the 02-28-perf_optimize_fsck_with_verified_at_timestamp branch from 44278a1 to eb32c1b Compare March 1, 2026 05:57
@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Mar 1, 2026
@kalbasit kalbasit added the backport release-0.9 Backport PR automatically label Mar 1, 2026
@kalbasit
kalbasit merged commit fb770a8 into main Mar 1, 2026
17 checks passed
@kalbasit
kalbasit deleted the 02-28-perf_optimize_fsck_with_verified_at_timestamp branch March 1, 2026 06:20
kalbasit added a commit that referenced this pull request Mar 1, 2026
This change introduces a verified_at timestamp to the nar_files table
and a new --verified-since flag to the fsck command. This allows the
tool to skip NARs that have been verified recently, significantly
reducing the time required for subsequent fsck runs, especially for
large chunked NARs.

How:
1. Created database migrations for SQLite, PostgreSQL, and MySQL to add
   the verified_at column.
2. Updated SQL queries and regenerated Go code to support the new
   column.
3. Added a shouldCheckNar helper and updated fsck logic to skip NARs
   verified within the specified duration.
4. Updated fsck to update verified_at whenever a successful check is
   performed.
5. Added a new test case TestFsckBackends/.../VerifiedSince to verify
   the optimization works as expected.

(cherry picked from commit fb770a8)
@kalbasit

kalbasit commented Mar 1, 2026

Copy link
Copy Markdown
Owner Author

Successfully created backport PR for release-0.9:

kalbasit added a commit that referenced this pull request Mar 1, 2026
Bot-based backport to `release-0.9`, triggered by a label in #998.

This change introduces a verified_at timestamp to the nar_files table
and a new --verified-since flag to the fsck command. This allows the
tool to skip NARs that have been verified recently, significantly
reducing the time required for subsequent fsck runs, especially for
large chunked NARs.

How:
1. Created database migrations for SQLite, PostgreSQL, and MySQL to add
the verified_at column.
2. Updated SQL queries and regenerated Go code to support the new
column.
3. Added a shouldCheckNar helper and updated fsck logic to skip NARs
verified within the specified duration.
4. Updated fsck to update verified_at whenever a successful check is
performed.
5. Added a new test case TestFsckBackends/.../VerifiedSince to verify
the optimization works as expected.
kalbasit added a commit that referenced this pull request Jun 8, 2026
## Summary

The `[Unreleased]` section was populated in #1309 but skewed toward the
recent CDC-drain, in-flight-staging, and Ent-migration work, omitting
several user-facing features and fixes that also landed since v0.9.4.
This backfills the missing notable changes, each cross-referenced by PR.

**Added**
- `ncps fsck` command + Helm fsck CronJob (#975, #996, #998, #999,
#1000, #1004, #1006, #1176)
- Closure pinning HTTP endpoints `POST`/`DELETE /pin/{hash}.narinfo`,
`GET /pins` (#1108)
- pprof server via `--pprof-addr` (#1063)
- CDC lazy chunking as a feature + `--cache-cdc-lazy-cleanup-schedule` +
recovery cron (#1082, #1083, #1089, #1096)
- Helm: auto `GOMEMLIMIT` (#1061), configurable temp volume /
`volumeClaimTemplate` (#1074, #1077), SQLite `Recreate` strategy (#1012)
- Startup storage reporting + deprecated-narinfo warning (#959, #961)
- `docker-dev` image (#1097)

**Fixed**
- `store/tmp` no longer wiped on startup (#1313)
- `nix-hash` NAR verification (#1101)
- Prometheus counters primed at startup (#1337, #1343)
- 404 on un-reassemblable chunked NARs (#1319), purged narinfo no longer
500 (#1318), fail-fast tmp validation (#1086)
- Further CDC/cache reliability fixes (#1317, #1346, #1341, #1342,
#1331, #1347)

**Changed**
- CDC config now persisted in the DB config table (#971)

Dependency bumps, CI/test-only changes, and the internal Ent-conversion
PRs are intentionally excluded; the existing Ent-migration entry already
covers that work.

## Test plan

- [x] `nix fmt` reports no changes (docs-only edit)
- [x] CHANGELOG section structure intact
(Added/Fixed/Changed/Removed/Migration/CI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport release-0.9 Backport PR automatically enhancement New feature or request go Pull requests that update go code size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant