Skip to content

Commit c4b78d9

Browse files
khoi0107facebook-github-bot
authored andcommitted
Add shared garbage collection lock for dotslash execution
Summary: We want to add garbage collection for dotslash cache (discussion [post](https://fb.workplace.com/groups/dotslash.users/permalink/2064356387419058/)). For safe removal we have to ensure artifact not being used before removing them Introducing a shared gc file lock that multiple executors can acquire Later GC process has to acquire the exclusive lock on the same file to be able to clean up the artifact Without any GC process running this should have no impact on execution of dotslash so this diff itself should not introduce blocking/deadlock GC testing will be in next diff Reviewed By: dtolnay Differential Revision: D73410163 fbshipit-source-id: 9c130a090c8ac1836892a07be42281687a4bad17
1 parent 3345dc9 commit c4b78d9

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/download.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub fn acquire_download_lock_for_artifact(
253253
match FileLock::acquire(&artifact_location.lock_path) {
254254
Ok(file_lock) => return Ok(file_lock),
255255
Err(err @ FileLockError::LockExclusive(..)) => return Err(err.into()),
256-
Err(FileLockError::Create(..)) => {}
256+
Err(FileLockError::Create(..) | FileLockError::LockShared(..)) => {}
257257
}
258258
}
259259
}

src/util/file_lock.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub enum FileLockError {
2323

2424
#[error("failed to get exclusive lock `{0}`")]
2525
LockExclusive(PathBuf, #[source] io::Error),
26+
27+
#[error("failed to get shared lock `{0}`")]
28+
LockShared(PathBuf, #[source] io::Error),
2629
}
2730

2831
#[derive(Debug, Default)]
@@ -54,6 +57,29 @@ impl FileLock {
5457
}
5558
inner(path.as_ref())
5659
}
60+
61+
pub fn acquire_shared_lock<P>(path: P) -> Result<FileLock, FileLockError>
62+
where
63+
P: AsRef<Path>,
64+
{
65+
fn inner(path: &Path) -> Result<FileLock, FileLockError> {
66+
let lock_file = File::options()
67+
.read(true)
68+
.write(true)
69+
.create(true)
70+
.truncate(false)
71+
.open(path)
72+
.map_err(|e| FileLockError::Create(path.to_path_buf(), e))?;
73+
74+
fs2::FileExt::lock_shared(&lock_file)
75+
.map_err(|e| FileLockError::LockShared(path.to_path_buf(), e))?;
76+
77+
Ok(FileLock {
78+
file: Some(lock_file),
79+
})
80+
}
81+
inner(path.as_ref())
82+
}
5783
}
5884

5985
impl Drop for FileLock {

0 commit comments

Comments
 (0)