Skip to content

Commit

Permalink
refactor: use thread local for segment remove flag
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <69835502+bsbds@users.noreply.github.com>
  • Loading branch information
bsbds committed Apr 19, 2024
1 parent f4ca558 commit d987b8c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions crates/curp/src/server/storage/wal/remover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ use super::{
util::{get_checksum, get_file_paths_with_ext, is_exist, parse_u64, validate_data, LockedFile},
};

/// Utilize thread-local variables because the tests are running concurrently,
/// and different tests might set this value, leading to a race condition.
#[cfg(test)]
static ABORT_REMOVE: AtomicBool = AtomicBool::new(false);
thread_local! {
static ABORT_REMOVE: AtomicBool = AtomicBool::new(false);
}

/// The name of the RWAL file
const REMOVER_WAL_FILE_NAME: &str = "segments.rwal";
Expand Down Expand Up @@ -125,7 +129,7 @@ impl SegmentRemover {
wal_path: impl AsRef<Path>,
) -> io::Result<()> {
#[cfg(test)]
if ABORT_REMOVE.load(std::sync::atomic::Ordering::Relaxed) {
if ABORT_REMOVE.with(|f| f.load(std::sync::atomic::Ordering::Relaxed)) {
return Ok(());
}

Expand Down Expand Up @@ -197,7 +201,7 @@ mod tests {
fn wal_remove_recover_is_ok() {
let temp_dir = tempfile::tempdir().unwrap();
let dir_path = PathBuf::from(temp_dir.path());
ABORT_REMOVE.store(true, std::sync::atomic::Ordering::Relaxed);
ABORT_REMOVE.with(|f| f.store(true, std::sync::atomic::Ordering::Relaxed));

let mut segments = vec![];
let mut file_paths = vec![];
Expand All @@ -211,7 +215,7 @@ mod tests {
file_paths.push(wal_path);
}
SegmentRemover::new_removal(&dir_path, segments.iter());
ABORT_REMOVE.store(false, std::sync::atomic::Ordering::Relaxed);
ABORT_REMOVE.with(|f| f.store(false, std::sync::atomic::Ordering::Relaxed));
assert!(file_paths.iter().find(|p| is_exist(p)).is_some());
SegmentRemover::recover(&dir_path).unwrap();
assert!(file_paths.into_iter().all(|p| !is_exist(p)));
Expand Down

0 comments on commit d987b8c

Please sign in to comment.