Skip to content

feat: add back gc protect callback #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! with a remote store via rpc calls.
use std::{io, net::SocketAddr, ops::Deref, sync::Arc};

use bao_tree::io::EncodeError;
use iroh::Endpoint;
use irpc::rpc::{listen, Handler};
use n0_snafu::SpanTrace;
Expand Down Expand Up @@ -211,6 +212,15 @@ impl std::error::Error for Error {
}
}

impl From<EncodeError> for Error {
fn from(value: EncodeError) -> Self {
match value {
EncodeError::Io(cause) => Self::Io(cause),
_ => Self::other(value),
}
}
}

pub type Result<T> = std::result::Result<T, Error>;

/// The main entry point for the store API.
Expand Down
16 changes: 12 additions & 4 deletions src/api/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,21 @@ impl Blobs {
})
}

pub async fn delete_with_opts(&self, options: DeleteOptions) -> RequestResult<()> {
/// Delete a blob.
///
/// This function is not public, because it does not work as expected when called manually,
/// because blobs are protected from deletion. This is only called from the gc task, which
/// clears the protections before.
///
/// Users should rely only on garbage collection for blob deletion.
pub(crate) async fn delete_with_opts(&self, options: DeleteOptions) -> RequestResult<()> {
trace!("{options:?}");
self.client.rpc(options).await??;
Ok(())
}

pub async fn delete(
/// See [`Self::delete_with_opts`].
pub(crate) async fn delete(
&self,
hashes: impl IntoIterator<Item = impl Into<Hash>>,
) -> RequestResult<()> {
Expand Down Expand Up @@ -294,6 +302,7 @@ impl Blobs {
ExportBaoRequest {
hash: hash.into(),
ranges: ranges.into(),
create_if_missing: false,
},
32,
)
Expand Down Expand Up @@ -962,7 +971,6 @@ impl ExportBaoProgress {
let mut data = Vec::new();
let mut stream = self.into_byte_stream();
while let Some(item) = stream.next().await {
println!("item: {item:?}");
data.extend_from_slice(&item?);
}
Ok(data)
Expand Down Expand Up @@ -1088,7 +1096,7 @@ impl ExportBaoProgress {
}
EncodedItem::Leaf(leaf) => Some(Ok(leaf.data)),
EncodedItem::Done => None,
EncodedItem::Error(cause) => Some(Err(super::Error::other(cause))),
EncodedItem::Error(cause) => Some(Err(cause.into())),
})
}

Expand Down
1 change: 1 addition & 0 deletions src/api/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub struct ObserveRequest {
pub struct ExportBaoRequest {
pub hash: Hash,
pub ranges: ChunkRanges,
pub create_if_missing: bool,
}

/// Export the given ranges as chunkks, without validation.
Expand Down
11 changes: 8 additions & 3 deletions src/api/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use nested_enum_utils::common_fields;
use ref_cast::RefCast;
use snafu::{Backtrace, IntoError, Snafu};

use super::blobs::Bitfield;
use super::blobs::{Bitfield, ExportBaoOptions};
use crate::{
api::{blobs::WriteProgress, ApiClient},
get::{fsm::DecodeError, BadRequestSnafu, GetError, GetResult, LocalFailureSnafu, Stats},
Expand Down Expand Up @@ -159,7 +159,7 @@ impl PushProgress {

async fn just_result<S, R>(stream: S) -> Option<R>
where
S: Stream,
S: Stream<Item: std::fmt::Debug>,
R: TryFrom<S::Item>,
{
tokio::pin!(stream);
Expand Down Expand Up @@ -417,7 +417,12 @@ impl Remote {
let root = request.hash;
let bitfield = self.store().observe(root).await?;
let children = if !request.ranges.is_blob() {
let bao = self.store().export_bao(root, bitfield.ranges.clone());
let opts = ExportBaoOptions {
hash: root,
ranges: bitfield.ranges.clone(),
create_if_missing: true,
};
let bao = self.store().export_bao_with_opts(opts, 32);
let mut by_index = BTreeMap::new();
let mut stream = bao.hashes_with_index();
while let Some(item) = stream.next().await {
Expand Down
38 changes: 33 additions & 5 deletions src/store/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,34 @@ impl HashContext {
Ok(())
}

pub async fn get_maybe_create(&self, hash: Hash, create: bool) -> api::Result<BaoFileHandle> {
if create {
self.get_or_create(hash).await
} else {
self.get(hash).await
}
}

pub async fn get(&self, hash: Hash) -> api::Result<BaoFileHandle> {
if hash == Hash::EMPTY {
return Ok(self.ctx.empty.clone());
}
let res = self
.slot
.get_or_create(|| async {
let res = self.db().get(hash).await.map_err(io::Error::other)?;
let res = match res {
Some(state) => open_bao_file(&hash, state, &self.ctx).await,
None => Err(io::Error::new(io::ErrorKind::NotFound, "hash not found")),
};
Ok((res?, ()))
})
.await
.map_err(api::Error::from);
let (res, _) = res?;
Ok(res)
}

pub async fn get_or_create(&self, hash: Hash) -> api::Result<BaoFileHandle> {
if hash == Hash::EMPTY {
return Ok(self.ctx.empty.clone());
Expand Down Expand Up @@ -939,7 +967,7 @@ async fn observe(cmd: ObserveMsg, ctx: HashContext) {

#[instrument(skip_all, fields(hash = %cmd.hash_short()))]
async fn export_ranges(mut cmd: ExportRangesMsg, ctx: HashContext) {
match ctx.get_or_create(cmd.hash).await {
match ctx.get(cmd.hash).await {
Ok(handle) => {
if let Err(cause) = export_ranges_impl(cmd.inner, &mut cmd.tx, handle).await {
cmd.tx
Expand Down Expand Up @@ -1000,7 +1028,7 @@ async fn export_ranges_impl(

#[instrument(skip_all, fields(hash = %cmd.hash_short()))]
async fn export_bao(mut cmd: ExportBaoMsg, ctx: HashContext) {
match ctx.get_or_create(cmd.hash).await {
match ctx.get_maybe_create(cmd.hash, cmd.create_if_missing).await {
Ok(handle) => {
if let Err(cause) = export_bao_impl(cmd.inner, &mut cmd.tx, handle).await {
cmd.tx
Expand All @@ -1010,9 +1038,9 @@ async fn export_bao(mut cmd: ExportBaoMsg, ctx: HashContext) {
}
}
Err(cause) => {
let cause = anyhow::anyhow!("failed to open file: {cause}");
let crate::api::Error::Io(cause) = cause;
cmd.tx
.send(bao_tree::io::EncodeError::Io(io::Error::other(cause)).into())
.send(bao_tree::io::EncodeError::Io(cause).into())
.await
.ok();
}
Expand All @@ -1024,7 +1052,7 @@ async fn export_bao_impl(
tx: &mut mpsc::Sender<EncodedItem>,
handle: BaoFileHandle,
) -> anyhow::Result<()> {
let ExportBaoRequest { ranges, hash } = cmd;
let ExportBaoRequest { ranges, hash, .. } = cmd;
debug_assert!(handle.hash() == hash, "hash mismatch");
let outboard = handle.outboard()?;
let size = outboard.tree.size();
Expand Down
Loading
Loading