Skip to content

Indexes 7: Adds a lock file around index generation and updates#1354

Open
dcookspi wants to merge 1 commit intoindex-5-zplus-1-versionfilters-not-stringsfrom
index-7-lockfile-for-indexgen
Open

Indexes 7: Adds a lock file around index generation and updates#1354
dcookspi wants to merge 1 commit intoindex-5-zplus-1-versionfilters-not-stringsfrom
index-7-lockfile-for-indexgen

Conversation

@dcookspi
Copy link
Copy Markdown
Collaborator

@dcookspi dcookspi commented Apr 16, 2026

This adds a lock file around index generation and update processing. The lock for the index file is created, via a FileLock object, before the underlying repo is read and kept until the index has been generated and written out. The same duration of locking is done for updating the index.

A FileLock has configurable settings for the maximum number of tries to get the lock before giving up, and the amound of time to sleep between tries, as well as the kind of file being locked (used in log messages). It also contains sentry integration when the sentry feature is enabled.

This is the 7th of the chained PRs for adding indexes to spk solves:

  1. Indexes 1: Change Package and related traits to not return references to fields #1336
  2. Indexes 2: Add new_unchecked() constructors to spk schema objects #1337
  3. Indexes 3: Adds flatbuffers schema and SolverPackageSpec for indexes to spk #1338
  4. Indexes 4: Adds Indexes for SPK repositories #1339
  5. Indexes 5: Adds spk repo index subcommand for index generation and updates #1340
  6. Indexes 6: Changes version_filter field in index schema #1344
  7. this PR
  8. Indexes 8: Fixes the spk build or mkb crash with indexes enabled #1355
  9. Indexes 9 - Adds messaging on package events to kafka #1356

@dcookspi dcookspi self-assigned this Apr 16, 2026
@dcookspi dcookspi added enhancement New feature or request SPI AOI Area of interest for SPI pr-chain This PR doesn't target the main branch, don't merge! labels Apr 16, 2026
@dcookspi dcookspi force-pushed the index-7-lockfile-for-indexgen branch from a0324cf to 85050e7 Compare April 16, 2026 00:58
@dcookspi dcookspi requested review from jrray and rydrman April 16, 2026 01:09
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 16, 2026

Codecov Report

❌ Patch coverage is 0% with 198 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/spk-storage/src/storage/flatbuffer_index.rs 0.00% 169 Missing ⚠️
crates/spk-cli/cmd-repo/src/cmd_repo.rs 0.00% 13 Missing ⚠️
crates/spk/src/cli.rs 0.00% 10 Missing ⚠️
crates/spk-config/src/config.rs 0.00% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

Signed-off-by: David Gilligan-Cook <dcook@imageworks.com>
@dcookspi dcookspi force-pushed the index-7-lockfile-for-indexgen branch from cddd514 to 5ac3a7b Compare May 1, 2026 18:27
Comment on lines +153 to +156
if result.is_err() {
// Need to keep these if-statements separate
// to allow for two different error handling cases.
#[allow(clippy::collapsible_if)]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you lose the outer if?

Comment on lines +191 to +194
Ok(_file) => {
// Write any given contents to the lock file as well
if let Some(ref data) = contents
&& let Err(err) = tokio::fs::write(&lock_file, data).await
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Ok(_file) => {
// Write any given contents to the lock file as well
if let Some(ref data) = contents
&& let Err(err) = tokio::fs::write(&lock_file, data).await
Ok(mut file) => {
// Write any given contents to the lock file as well
if let Some(ref data) = contents
&& let Err(err) = tokio::io::AsyncWriteExt::write_all(&mut file, data).await

Use the file handle already opened.

// file for the lock data and add it to the returned error.
use std::io::Read;
let mut lock_data = String::new();
let held_for = match std::fs::File::open(&lock_file) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's weird to switch to synchronous file apis here in an async function that was using async file apis earlier.

Comment on lines +284 to +285
/// # Safety:
/// File locks are used to ensure that only one process is writing
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// # Safety:
/// File locks are used to ensure that only one process is writing
/// # Safety
///
/// File locks are used to ensure that only one process is writing

The style convention we're using almost consistently throughout the project.

);

return Err(unlock_error);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}
} else {
self.has_been_deleted = true;
}

If the file didn't exist, then it had been deleted. Or does this variable mean to say was_deleted_by_us? Since this var is used to decide to attempt to delete the file in the drop handler then it should be set true here too, IMO.

if !self.has_been_deleted
&& let Err(err) = unsafe { self.unlock() }
{
// Can only the problem here. The unlock() method will
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Can only the problem here. The unlock() method will
// Can only <word> the problem here. The unlock() method will

let repo = &repos[0].1;

// Lock the index file until the data has been updated
let mut _lock = lock_index_file(repo).await?;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It will reduce programming bugs if the return value can be turned into a lock guard that is required as a parameter to the function call(s) that write to the index.

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

Labels

enhancement New feature or request pr-chain This PR doesn't target the main branch, don't merge! SPI AOI Area of interest for SPI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants