Skip to content

Commit

Permalink
Rename ClearProtected to GcStart
Browse files Browse the repository at this point in the history
  • Loading branch information
rklaehn committed Mar 14, 2024
1 parent f6775ab commit 7e3601b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
7 changes: 7 additions & 0 deletions iroh-bytes/src/store/bao_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,20 @@ impl BaoFileStorage {
}
}

/// A weak reference to a bao file handle.
#[derive(Debug, Clone)]
pub struct BaoFileHandleWeak(Weak<BaoFileHandleInner>);

impl BaoFileHandleWeak {
/// Upgrade to a strong reference if possible.
pub fn upgrade(&self) -> Option<BaoFileHandle> {
self.0.upgrade().map(BaoFileHandle)
}

/// True if the handle is still live (has strong references)
pub fn is_live(&self) -> bool {
self.0.strong_count() > 0
}
}

#[derive(Debug)]
Expand Down
37 changes: 15 additions & 22 deletions iroh-bytes/src/store/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,10 @@ pub(crate) enum ActorMessage {
/// Modification method: inline size was exceeded for a partial entry.
/// If the entry is complete, this is a no-op. If the entry is partial and in
/// memory, it will be written to a file and created in redb.
OnMemSizeExceeded {
hash: Hash,
},
OnMemSizeExceeded { hash: Hash },
/// Modification method: marks a partial entry as complete.
/// Calling this on a complete entry is a no-op.
OnComplete {
handle: BaoFileHandle,
},
OnComplete { handle: BaoFileHandle },
/// Modification method: import data into a redb store
///
/// At this point the size, hash and outboard must already be known.
Expand Down Expand Up @@ -585,9 +581,7 @@ pub(crate) enum ActorMessage {
/// Sync the entire database to disk.
///
/// This just makes sure that there is no write transaction open.
Sync {
tx: oneshot::Sender<()>,
},
Sync { tx: oneshot::Sender<()> },
/// Internal method: dump the entire database to stdout.
Dump,
/// Internal method: validate the entire database.
Expand All @@ -599,9 +593,10 @@ pub(crate) enum ActorMessage {
progress: tokio::sync::mpsc::Sender<ValidateProgress>,
tx: oneshot::Sender<ActorResult<()>>,
},
ClearProtected {
tx: oneshot::Sender<()>,
},
/// Internal method: notify the actor that a new gc epoch has started.
///
/// This will be called periodically and can be used to do misc cleanups.
GcStart { tx: oneshot::Sender<()> },
/// Internal method: shutdown the actor.
Shutdown,
}
Expand Down Expand Up @@ -883,11 +878,9 @@ impl StoreInner {
Ok(rx.await??)
}

async fn clear_protected(&self) -> OuterResult<()> {
async fn gc_start(&self) -> OuterResult<()> {
let (tx, rx) = oneshot::channel();
self.tx
.send_async(ActorMessage::ClearProtected { tx })
.await?;
self.tx.send_async(ActorMessage::GcStart { tx }).await?;
Ok(rx.await?)
}

Expand Down Expand Up @@ -1386,7 +1379,7 @@ impl crate::store::traits::Store for Store {
}

async fn gc_start(&self) -> io::Result<()> {
self.0.clear_protected().await?;
self.0.gc_start().await?;
Ok(())
}

Expand Down Expand Up @@ -1480,10 +1473,7 @@ impl ActorState {
tables: &impl ReadableTables,
hash: Hash,
) -> ActorResult<EntryStateResponse> {
let mem = self
.handles
.get(&hash)
.and_then(|weak| Some(weak.upgrade()?));
let mem = self.handles.get(&hash).and_then(|weak| weak.upgrade());
let db = match tables.blobs().get(hash)? {
Some(entry) => Some({
match entry.value() {
Expand Down Expand Up @@ -2203,9 +2193,12 @@ impl ActorState {
txn.commit()?;
tx.send(res).ok();
}
ActorMessage::ClearProtected { tx } => {
ActorMessage::GcStart { tx } => {
tracing::info!("clear_protected");
// clear the protected set from last gc epoch
self.protected.clear();
// clean up dead weak refs so the memory can be freed
self.handles.retain(|_, v| v.is_live());
tx.send(()).ok();
}
ActorMessage::ImportFlatStore { paths, tx } => {
Expand Down

0 comments on commit 7e3601b

Please sign in to comment.