Repository navigation
perf: optimize fsck with verified_at timestamp [backport #998] - #1001
Conversation
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)
Summary of ChangesHello, 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 implements a crucial performance enhancement for the Highlights
Changelog
Using Gemini Code AssistThe 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
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 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
|
|
Related Documentation 1 document(s) may need updating based on files changed in this PR: kalbasit's Space Cache and Storage Configuration FlagsView Suggested Changes@@ -288,6 +288,19 @@
```
**Note:** The `verified_at` timestamp is only updated when a NAR passes verification. Failed checks do not update the timestamp, ensuring problematic NARs will continue to be checked on subsequent runs.
+
+### Implementation Details
+
+**Database Schema:** A `verified_at` TIMESTAMP column has been added to the `nar_files` table. Database migrations are provided for SQLite, PostgreSQL, and MySQL to add this column to existing installations.
+
+**Update Mechanism:** When fsck successfully verifies a NAR file (whether non-chunked or CDC-chunked), it calls the `UpdateNarFileVerifiedAt` database function to set the `verified_at` timestamp to the current time. This update is skipped if the verification fails, ensuring that problematic files will be checked again on the next run.
+
+**Filtering Logic:** When `--verified-since` is specified, fsck uses the `shouldCheckNar` helper function to determine whether to verify each NAR file. The filtering query checks:
+- If `verified_at` IS NULL (never verified), the NAR is checked
+- If `verified_at < (NOW() - specified duration)`, the NAR is checked
+- Otherwise, the NAR is skipped
+
+This allows fsck to focus resources on files that haven't been verified recently, while maintaining confidence that all files are periodically checked. The optimization is especially beneficial for large caches with many chunked NARs, where chunk-level verification can be time-consuming.
## Summary Table of Storage Flags
| Purpose | CLI Flag | YAML Key | Required |Note: You must be authenticated to accept/decline updates. |
There was a problem hiding this comment.
Code Review
This pull request introduces a performance optimization for the fsck command by adding a verified_at timestamp to nar_files. This allows skipping recently verified NARs, which is a great improvement for large repositories. The implementation is well-rounded, covering database migrations for all supported backends, updating queries, and adding a new command-line flag with corresponding logic. The new test case for verified-since is also well-written and covers the main scenarios. My main concern is with the down migration for SQLite, which could lead to broken foreign key constraints.
| -- migrate:down | ||
| CREATE TABLE nar_files_new ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| hash TEXT NOT NULL, | ||
| compression TEXT NOT NULL DEFAULT '', | ||
| file_size INTEGER NOT NULL, | ||
| "query" TEXT NOT NULL DEFAULT '', | ||
| total_chunks BIGINT NOT NULL DEFAULT 0, | ||
| chunking_started_at TIMESTAMP NULL, | ||
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
| updated_at TIMESTAMP, | ||
| last_accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
| UNIQUE (hash, compression, "query") | ||
| ); | ||
|
|
||
| INSERT INTO nar_files_new ( | ||
| id, hash, compression, file_size, "query", total_chunks, chunking_started_at, created_at, updated_at, last_accessed_at | ||
| ) | ||
| SELECT | ||
| id, | ||
| hash, | ||
| compression, | ||
| file_size, | ||
| "query", | ||
| total_chunks, | ||
| chunking_started_at, | ||
| created_at, | ||
| updated_at, | ||
| last_accessed_at | ||
| FROM nar_files; | ||
|
|
||
| PRAGMA foreign_keys = OFF; | ||
| DROP TABLE nar_files; | ||
| ALTER TABLE nar_files_new RENAME TO nar_files; | ||
| PRAGMA foreign_keys = ON; | ||
|
|
||
| CREATE INDEX idx_nar_files_last_accessed_at ON nar_files (last_accessed_at); |
There was a problem hiding this comment.
The down migration for SQLite is implemented by recreating the nar_files table without the verified_at column. While this is a standard approach for dropping a column in older SQLite versions, it has a significant side effect here. The nar_files table is referenced by foreign keys in narinfo_nar_files and nar_file_chunks. When nar_files is dropped (even with PRAGMA foreign_keys=OFF), these foreign key constraints are broken and are not automatically re-established when a new nar_files table is created.
This can lead to a loss of data integrity, as the relationships will no longer be enforced by the database for existing data.
To fix this, the down migration should also recreate the tables that reference nar_files (narinfo_nar_files and nar_file_chunks) after recreating nar_files. This would involve:
- Saving data from
narinfo_nar_filesandnar_file_chunksto temporary tables. - Dropping
narinfo_nar_filesandnar_file_chunks. - Performing the existing
downmigration fornar_files. - Recreating
narinfo_nar_filesandnar_file_chunkswith their foreign key constraints. - Restoring data into the newly created
narinfo_nar_filesandnar_file_chunks.
Given the complexity, and since down migrations are not as common, an alternative is to accept this limitation but add a prominent comment in the migration file warning users about the consequences of running it.
References
- To maintain consistency, schema changes (like adding
ON DELETE CASCADE) should be applied across all supported database backends (e.g., SQLite and PostgreSQL) in a coordinated manner, potentially through a new, separate migration. This comment highlights a specific challenge in SQLite migrations that needs careful handling to ensure data integrity, aligning with the need for coordinated and robust schema changes across backends.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release-0.9 #1001 +/- ##
===========================================
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. 🚀 New features to boost your workflow:
|
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: