Implement explicit directory locking for index operations
Introduces explicit directory locking mechanisms (`DirLock`) to ensure exclusive access during index operations across command stages. This includes modifying index opening logic to acquire locks before proceeding, handling lock acquisition failures by exiting, and implementing safe conditional building of sibling annexes using locking to prevent concurrent corruption.
This commit is contained in:
1 parent
7c7d7615cb
commit
9ac3a37529
11 files changed
+242
-72
No files matched your search
@@ -93,7 +93,11 @@ fn run_annotate(args: &AnnotateArgs) {
|
||||
}
|
||||
};
|
||||
|
||||
let idx = open_index(&args.index);
|
||||
// Rewrites the index metadata in place: exclusive for the whole command.
|
||||
let idx = KmerIndex::open_lock(&args.index).unwrap_or_else(|e| {
|
||||
eprintln!("error opening index: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
let mut genomes = idx.meta().genomes().unwrap_or_else(|e| {
|
||||
eprintln!("error reading index metadata: {e}");
|
||||
|
||||
@@ -70,14 +70,8 @@ struct ModeArgs {
|
||||
}
|
||||
|
||||
pub fn run(args: ConvertArgs) {
|
||||
// Modifies the index in place; acquired before opening so a concurrent
|
||||
// writer can't slip in between the open and the convert below.
|
||||
let _lock = obisys::DirLock::acquire(&args.index).unwrap_or_else(|e| {
|
||||
eprintln!("error locking index directory {}: {e}", args.index.display());
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
let idx = KmerIndex::open(&args.index).unwrap_or_else(|e| {
|
||||
// Modifies the index in place: exclusive for the whole command.
|
||||
let idx = KmerIndex::open_lock(&args.index).unwrap_or_else(|e| {
|
||||
eprintln!("error opening index: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
@@ -26,14 +26,8 @@ pub struct PackArgs {
|
||||
}
|
||||
|
||||
pub fn run(args: PackArgs) {
|
||||
// Modifies the index in place; acquired before opening so a concurrent
|
||||
// writer can't slip in between the open and the pack below.
|
||||
let _lock = obisys::DirLock::acquire(&args.index).unwrap_or_else(|e| {
|
||||
eprintln!("error locking index directory {}: {e}", args.index.display());
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
let idx = Arc::new(KmerIndex::open(&args.index).unwrap_or_else(|e| {
|
||||
// Modifies the index in place: exclusive for the whole command.
|
||||
let idx = Arc::new(KmerIndex::open_lock(&args.index).unwrap_or_else(|e| {
|
||||
eprintln!("error opening index: {e}");
|
||||
std::process::exit(1);
|
||||
}));
|
||||
|
||||
@@ -78,13 +78,9 @@ pub fn run(args: PhyloArgs) {
|
||||
// Meant to be (re)built routinely, ahead of any SNP-family distance
|
||||
// computation that will later consume it.
|
||||
if args.sibling_annex {
|
||||
// Writes into the index directory — hold an exclusive lock for the
|
||||
// duration so a second, concurrent `--sibling-annex` run on the
|
||||
// same index can't corrupt these writes (see obisys::DirLock).
|
||||
let _lock = obisys::DirLock::acquire(&args.index).unwrap_or_else(|e| {
|
||||
eprintln!("error locking index directory {}: {e}", args.index.display());
|
||||
std::process::exit(1);
|
||||
});
|
||||
// Writes into the index directory; `build_sibling_annex` holds the
|
||||
// index's exclusive lock for the duration (`KmerIndex::with_lock`),
|
||||
// so a concurrent run on the same index can't corrupt these writes.
|
||||
info!("building sibling-count/minorant annex");
|
||||
let t = Stage::start("sibling_annex");
|
||||
cache.build_sibling_annex().unwrap_or_else(|e| {
|
||||
|
||||
@@ -53,7 +53,9 @@ pub(super) fn run_bits_per_kmer(index_path: &PathBuf) {
|
||||
pub(super) fn run_rename(index_path: &PathBuf, spec: &str) {
|
||||
let (old_label, new_label) = parse_rename_spec(spec);
|
||||
|
||||
let idx = KmerIndex::open(index_path).unwrap_or_else(|e| {
|
||||
// Rewrites the index metadata and renames a spectrum file in place:
|
||||
// exclusive for the whole command.
|
||||
let idx = KmerIndex::open_lock(index_path).unwrap_or_else(|e| {
|
||||
eprintln!("error opening index: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user