Skip to content

Commit

Permalink
Aol is already thread-safe, no need for another lock (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsserge authored Feb 10, 2025
1 parent 999cf29 commit 9e97fb1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/compaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ impl Store {
let commit_lock = self.core.commit_write_lock.lock();

// Rotate the commit log and get the new segment ID
let clog = self.core.clog.as_ref().unwrap().write();
let clog = self.core.clog.as_ref().unwrap();
let new_segment_id = clog.rotate()?;
let last_updated_segment_id = new_segment_id - 1;
drop(clog); // Explicitly drop the lock

// Create a temporary directory for compaction
fs::create_dir_all(&tmp_merge_dir)?;
Expand Down
11 changes: 4 additions & 7 deletions src/repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn repair_last_corrupted_segment(
/// Currently it is only being used for testing purposes.
#[allow(unused)]
pub(crate) fn repair_corrupted_segment(
aol: &mut Aol,
aol: &Aol,
corrupted_segment_id: u64,
corrupted_offset_marker: u64,
) -> Result<()> {
Expand Down Expand Up @@ -117,7 +117,7 @@ pub(crate) fn repair_corrupted_segment(
///
/// If any of these operations fail, the function returns an error.
fn repair_segment(
aol: &mut Aol,
aol: &Aol,
corrupted_segment_id: u64,
corrupted_offset_marker: u64,
corrupted_segment_file_path: PathBuf,
Expand Down Expand Up @@ -314,7 +314,7 @@ mod tests {
segment_num: usize,
corruption_offset: u64,
) {
let mut clog = store.core.clog.as_ref().unwrap().write();
let clog = store.core.clog.as_ref().unwrap();
let clog_subdir = opts.dir.join("clog");
let sr =
SegmentRef::read_segments_from_directory(&clog_subdir).expect("should read segments");
Expand All @@ -325,10 +325,7 @@ mod tests {
let (corrupted_segment_id, corrupted_offset_marker) =
find_corrupted_segment(sr, opts.clone());

repair_corrupted_segment(&mut clog, corrupted_segment_id, corrupted_offset_marker).unwrap();

// drop lock over commit log
drop(clog);
repair_corrupted_segment(clog, corrupted_segment_id, corrupted_offset_marker).unwrap();
}

#[allow(unused)]
Expand Down
10 changes: 5 additions & 5 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub struct Core {
/// Options for store.
pub(crate) opts: Options,
/// Commit log for store.
pub(crate) clog: Option<Arc<RwLock<Aol>>>,
pub(crate) clog: Option<Arc<Aol>>,
/// Manifest for store to track Store state.
pub(crate) manifest: Option<RwLock<Aol>>,
/// Transaction ID Oracle for store.
Expand Down Expand Up @@ -206,7 +206,7 @@ impl Core {
indexer: RwLock::new(indexer),
opts,
manifest: manifest.map(RwLock::new),
clog: clog.map(|c| Arc::new(RwLock::new(c))),
clog: clog.map(Arc::new),
oracle,
value_cache,
is_closed: AtomicBool::new(false),
Expand Down Expand Up @@ -412,7 +412,7 @@ impl Core {
//
// If we ever need more control over the closing process, we might
// need to extend this protocol with additional locks.
clog.write().close()?;
clog.close()?;
}

// Close the manifest if it exists
Expand Down Expand Up @@ -472,7 +472,7 @@ impl Core {
}

fn append_log(&self, tx_record: &BytesMut, durability: Durability) -> Result<(u64, u64)> {
let clog = self.clog.as_ref().unwrap().write();
let clog = self.clog.as_ref().unwrap();

let (segment_id, offset) = match durability {
Durability::Immediate => {
Expand Down Expand Up @@ -563,7 +563,7 @@ impl Core {

// If the value is not in the cache, read it from the commit log
let mut buf = vec![0; value_len];
let clog = self.clog.as_ref().unwrap().read();
let clog = self.clog.as_ref().unwrap();
clog.read_at(&mut buf, segment_id, value_offset)?;

// Cache the newly read value for future use
Expand Down

0 comments on commit 9e97fb1

Please sign in to comment.