use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use bao_tree::io::sync::Size;
use tokio::io::{AsyncSeekExt, AsyncWriteExt, SeekFrom};
use zenoh::qos::Priority;
use zenoh::query::ConsolidationMode;
use crate::cancel::CancelToken;
use crate::chunk::{MIN_CHUNK_SIZE, TransferChunks};
use crate::error::{BlobError, Result};
use crate::hash::Hash;
use crate::id::BlobId;
use crate::keys::{
MAX_RANGE_SPANS, availability_key, manifest_key, push_offer_key, push_slice_key, slice_selector,
};
use crate::manifest::{BlobSpec, Manifest, validate_id};
use crate::obs::{TransferStats, zdebug};
use crate::prefix::QueryPrefix;
use crate::progress::{Progress, ProgressSink};
use crate::resume::ResumeState;
use crate::server::{BlobSource, DynReadAt, ReadAtSize, SourceFingerprint};
use crate::verify;
use crate::wire::{Availability, ENC_AVAIL, ENC_MANIFEST, ENC_PUSH, ENC_SLICE, decode};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Overwrite {
#[default]
Refuse,
Replace,
}
#[derive(Debug, Clone)]
pub struct RetryPolicy {
pub max_attempts: u32,
pub base_backoff: Duration,
pub max_backoff: Duration,
}
impl Default for RetryPolicy {
fn default() -> Self {
RetryPolicy {
max_attempts: 5,
base_backoff: Duration::from_millis(250),
max_backoff: Duration::from_secs(10),
}
}
}
impl RetryPolicy {
fn backoff(&self, attempt: u32) -> Duration {
let exp = self.base_backoff.saturating_mul(1u32 << attempt.min(16));
exp.min(self.max_backoff)
}
}
#[derive(Debug, Clone)]
struct ClientConfig {
query_timeout: Duration,
retry: RetryPolicy,
max_chunks_per_query: u32,
max_blob_size: u64,
max_probe_replies: usize,
overwrite: Overwrite,
priority: Priority,
}
impl Default for ClientConfig {
fn default() -> Self {
ClientConfig {
query_timeout: Duration::from_secs(30),
retry: RetryPolicy::default(),
max_chunks_per_query: 512,
max_blob_size: 1 << 40, max_probe_replies: 256,
overwrite: Overwrite::default(),
priority: Priority::DataLow,
}
}
}
#[derive(Debug, Clone)]
pub struct DownloadRequest {
pub id: String,
pub expected_root: Option<Hash>,
}
impl DownloadRequest {
pub fn new(id: impl Into<String>) -> Self {
DownloadRequest {
id: id.into(),
expected_root: None,
}
}
pub fn pinned(id: impl Into<String>, root: Hash) -> Self {
DownloadRequest {
id: id.into(),
expected_root: Some(root),
}
}
pub fn by_root(root: Hash) -> Self {
DownloadRequest {
id: root.to_string(),
expected_root: Some(root),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Staged {
pub path: PathBuf,
pub suggested: Option<String>,
pub stats: TransferStats,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct BlobProbe {
pub origin: QueryPrefix,
pub manifest: Manifest,
pub availability: Option<Availability>,
}
fn origin_of(reply_key: &str, id: &str, endpoint: &str) -> Option<String> {
let rest = reply_key.strip_suffix(endpoint)?.strip_suffix('/')?;
let rest = rest.strip_suffix(id)?.strip_suffix('/')?;
(!rest.is_empty()).then(|| rest.to_string())
}
#[derive(Debug)]
pub struct BlobClient {
session: zenoh::Session,
prefix: QueryPrefix,
cfg: ClientConfig,
active: Arc<std::sync::Mutex<std::collections::HashSet<PathBuf>>>,
}
#[derive(Debug)]
pub struct BlobClientBuilder {
session: zenoh::Session,
prefix: QueryPrefix,
cfg: ClientConfig,
}
impl BlobClientBuilder {
#[must_use]
pub fn query_timeout(mut self, t: Duration) -> Self {
self.cfg.query_timeout = t;
self
}
#[must_use]
pub fn retry(mut self, retry: RetryPolicy) -> Self {
self.cfg.retry = retry;
self
}
#[must_use]
pub fn max_chunks_per_query(mut self, n: u32) -> Self {
self.cfg.max_chunks_per_query = n.max(1);
self
}
#[must_use]
pub fn max_blob_size(mut self, bytes: u64) -> Self {
self.cfg.max_blob_size = bytes;
self
}
#[must_use]
pub fn overwrite(mut self, ow: Overwrite) -> Self {
self.cfg.overwrite = ow;
self
}
#[must_use]
pub fn priority(mut self, priority: Priority) -> Self {
self.cfg.priority = priority;
self
}
pub fn build(self) -> BlobClient {
BlobClient {
session: self.session,
prefix: self.prefix,
cfg: self.cfg,
active: Arc::new(std::sync::Mutex::new(std::collections::HashSet::new())),
}
}
}
impl BlobClient {
pub fn builder(session: &zenoh::Session, key_prefix: QueryPrefix) -> BlobClientBuilder {
BlobClientBuilder {
session: session.clone(),
prefix: key_prefix,
cfg: ClientConfig::default(),
}
}
pub fn new(session: &zenoh::Session, key_prefix: QueryPrefix) -> Self {
Self::builder(session, key_prefix).build()
}
pub async fn fetch_manifest(&self, id: &str) -> Result<Manifest> {
self.fetch_manifest_matching(id, None).await
}
pub async fn probe(&self, id: &str) -> Result<Vec<BlobProbe>> {
validate_id(id)?;
let mut found: Vec<BlobProbe> = Vec::new();
let replies = self
.session
.get(manifest_key(self.prefix.as_str(), id))
.consolidation(ConsolidationMode::None)
.priority(self.cfg.priority)
.timeout(self.cfg.query_timeout)
.await
.map_err(BlobError::zenoh)?;
while let Ok(reply) = replies.recv_async().await {
if found.len() >= self.cfg.max_probe_replies {
break;
}
let Ok(sample) = reply.result() else { continue };
if !ENC_MANIFEST.matches(sample.encoding()) {
continue;
}
let Some(origin) = origin_of(sample.key_expr().as_str(), id, "manifest") else {
continue;
};
let Ok(manifest) = decode::<Manifest>(&sample.payload().to_bytes()) else {
continue;
};
if manifest.validate(self.cfg.max_blob_size).is_err() || manifest.id != id {
continue;
}
let Ok(origin) = QueryPrefix::new(origin) else {
continue;
};
if found.iter().any(|p| p.origin == origin) {
continue; }
found.push(BlobProbe {
origin,
manifest,
availability: None,
});
}
if found.is_empty() {
return Ok(found);
}
let replies = self
.session
.get(availability_key(self.prefix.as_str(), id))
.consolidation(ConsolidationMode::None)
.priority(self.cfg.priority)
.timeout(self.cfg.query_timeout)
.await
.map_err(BlobError::zenoh)?;
while let Ok(reply) = replies.recv_async().await {
let Ok(sample) = reply.result() else { continue };
if !ENC_AVAIL.matches(sample.encoding()) {
continue;
}
let Some(origin) = origin_of(sample.key_expr().as_str(), id, "have") else {
continue;
};
let Ok(avail) = decode::<Availability>(&sample.payload().to_bytes()) else {
continue;
};
let Some(entry) = found.iter_mut().find(|p| p.origin.as_str() == origin) else {
continue; };
if avail
.validate(entry.manifest.chunk_count().unwrap_or(u32::MAX))
.is_ok()
{
entry.availability = Some(avail);
}
}
Ok(found)
}
async fn fetch_manifest_matching(
&self,
id: &str,
expected_root: Option<Hash>,
) -> Result<Manifest> {
validate_id(id)?;
let key = manifest_key(self.prefix.as_str(), id);
let replies = self
.session
.get(&key)
.consolidation(ConsolidationMode::None)
.priority(self.cfg.priority)
.timeout(self.cfg.query_timeout)
.await
.map_err(BlobError::zenoh)?;
let mut rejected: Option<BlobError> = None;
while let Ok(reply) = replies.recv_async().await {
let Ok(sample) = reply.result() else { continue };
if !ENC_MANIFEST.matches(sample.encoding()) {
continue; }
let verdict = decode::<Manifest>(&sample.payload().to_bytes())
.and_then(|m| m.validate(self.cfg.max_blob_size).map(|()| m))
.and_then(|m| {
if m.id != id {
return Err(BlobError::MalformedMessage(format!(
"manifest id {:?} does not match requested {id:?}",
m.id
)));
}
if let Some(expected) = expected_root
&& m.root != expected
{
return Err(BlobError::RootMismatch {
expected,
actual: m.root,
});
}
Ok(m)
});
match verdict {
Ok(manifest) => return Ok(manifest),
Err(e) => {
zdebug!(id, error = %e, "skipping unacceptable manifest reply");
rejected.get_or_insert(e);
}
}
}
Err(rejected.unwrap_or_else(|| BlobError::NotFound(id.to_string())))
}
#[must_use]
pub fn priority(&self) -> Priority {
self.cfg.priority
}
pub fn download_to<'a>(&'a self, req: &'a DownloadRequest, dest: &'a Path) -> Download<'a> {
Download {
client: self,
req,
dest,
sink: None,
cancel: None,
overwrite: None,
holders: None,
}
}
pub fn download_staged<'a>(
&'a self,
req: &'a DownloadRequest,
dir: &'a Path,
) -> StagedDownload<'a> {
StagedDownload {
inner: self.download_to(req, dir),
}
}
pub fn download_to_writer<'a, W>(
&'a self,
req: &'a DownloadRequest,
writer: &'a mut W,
) -> DownloadToWriter<'a, W>
where
W: tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin + Send,
{
DownloadToWriter {
client: self,
req,
writer,
sink: None,
cancel: None,
}
}
pub fn upload_file(&self, spec: BlobSpec, path: impl Into<PathBuf>) -> Upload<'_> {
Upload {
client: self,
spec,
src: UploadSrc::Path(path.into()),
token: None,
sink: None,
cancel: None,
}
}
pub fn upload_source(&self, spec: BlobSpec, source: Arc<dyn BlobSource>) -> Upload<'_> {
Upload {
client: self,
spec,
src: UploadSrc::Source(source),
token: None,
sink: None,
cancel: None,
}
}
async fn run_download_staged(
&self,
req: &DownloadRequest,
dir: &Path,
sink: &dyn ProgressSink,
cancel: &CancelToken,
overwrite: Overwrite,
) -> Result<Staged> {
validate_id(&req.id)?;
tokio::fs::create_dir_all(dir).await?;
let path = dir.join(&req.id);
let stats = self
.run_download_to(req, &path, sink, cancel, overwrite)
.await?;
let suggested = self
.fetch_manifest_matching(&req.id, req.expected_root)
.await
.ok()
.and_then(|m| m.suggested_filename());
Ok(Staged {
path,
suggested,
stats,
})
}
async fn run_download_to(
&self,
req: &DownloadRequest,
dest: &Path,
sink: &dyn ProgressSink,
cancel: &CancelToken,
overwrite: Overwrite,
) -> Result<TransferStats> {
let guard = ActiveGuard::acquire(&self.active, dest)?;
let result = self
.download_to_inner(req, dest, sink, cancel, overwrite)
.await;
drop(guard);
match &result {
Err(BlobError::Cancelled { .. }) | Ok(_) => {}
Err(e) => sink.emit(Progress::Failed {
error: e.to_string(),
}),
}
result
}
async fn run_download_striped(
&self,
req: &DownloadRequest,
dest: &Path,
holders: &[BlobProbe],
sink: &dyn ProgressSink,
cancel: &CancelToken,
overwrite: Overwrite,
) -> Result<TransferStats> {
if holders.len() < 2 {
return self
.run_download_to(req, dest, sink, cancel, overwrite)
.await;
}
let guard = ActiveGuard::acquire(&self.active, dest)?;
let result = self
.download_striped_inner(req, dest, holders, sink, cancel, overwrite)
.await;
drop(guard);
if let Err(e) = &result
&& !e.is_cancelled()
{
sink.emit(Progress::Failed {
error: e.to_string(),
});
}
result
}
async fn download_striped_inner(
&self,
req: &DownloadRequest,
dest: &Path,
holders: &[BlobProbe],
sink: &dyn ProgressSink,
cancel: &CancelToken,
overwrite: Overwrite,
) -> Result<TransferStats> {
let started_at = tokio::time::Instant::now();
if overwrite == Overwrite::Refuse && tokio::fs::try_exists(dest).await? {
return Err(BlobError::DestinationExists(dest.to_path_buf()));
}
let manifest = holders[0].manifest.clone();
if let Some(expected) = req.expected_root
&& manifest.root != expected
{
return Err(BlobError::RootMismatch {
expected,
actual: manifest.root,
});
}
if holders.iter().any(|h| h.manifest.root != manifest.root) {
return Err(BlobError::MalformedMessage(
"holders disagree about this blob's root; probe again and pick one set".into(),
));
}
let chunks = manifest.chunks()?;
let count = chunks.count();
if let Some(parent) = dest.parent()
&& !parent.as_os_str().is_empty()
{
tokio::fs::create_dir_all(parent).await?;
}
let part = part_path(dest);
let existing_len = tokio::fs::metadata(&part).await.map(|m| m.len()).ok();
let mut state = match ResumeState::load(&part).await {
Some(s) if s.matches(&manifest, count) && existing_len == Some(manifest.total_len) => {
sink.emit(Progress::Resumed {
received: s.received(),
total: count,
});
s
}
_ => {
let file = tokio::fs::File::create(&part).await?;
file.set_len(manifest.total_len).await?;
let fresh = ResumeState::fresh(&manifest, count);
fresh.save_atomic(&part).await?;
sink.emit(Progress::Started {
total_len: manifest.total_len,
chunk_count: count,
});
fresh
}
};
let mut file = tokio::fs::OpenOptions::new()
.write(true)
.open(&part)
.await?;
let mut stats = TransferStats {
chunks_resumed: state.received(),
..Default::default()
};
self.fill_holes_striped(
&manifest, &chunks, holders, &mut file, &mut state, &part, sink, cancel, &mut stats,
)
.await?;
file.sync_data().await?;
drop(file);
if overwrite == Overwrite::Refuse && tokio::fs::try_exists(dest).await? {
return Err(BlobError::DestinationExists(dest.to_path_buf()));
}
tokio::fs::rename(&part, dest).await?;
ResumeState::remove(&part).await;
sink.emit(Progress::Completed {
path: dest.to_path_buf(),
});
stats.elapsed = started_at.elapsed();
Ok(stats)
}
pub async fn fetch_availability(&self, id: &str) -> Result<Vec<Availability>> {
validate_id(id)?;
let key = availability_key(self.prefix.as_str(), id);
let replies = self
.session
.get(&key)
.consolidation(ConsolidationMode::None)
.priority(self.cfg.priority)
.timeout(self.cfg.query_timeout)
.await
.map_err(BlobError::zenoh)?;
let mut out = Vec::new();
while let Ok(reply) = replies.recv_async().await {
if out.len() >= self.cfg.max_probe_replies {
break;
}
let Ok(sample) = reply.result() else { continue };
if !ENC_AVAIL.matches(sample.encoding()) {
continue;
}
if let Ok(avail) = decode::<Availability>(&sample.payload().to_bytes())
&& avail
.validate(self.cfg.max_blob_size.div_ceil(MIN_CHUNK_SIZE as u64) as u32)
.is_ok()
{
out.push(avail);
}
}
Ok(out)
}
pub async fn delete_partial(&self, dest: &Path) {
let part = part_path(dest);
let _ = tokio::fs::remove_file(&part).await;
ResumeState::remove(&part).await;
}
async fn run_upload(
&self,
spec: BlobSpec,
src: UploadSrc,
token: Option<Vec<u8>>,
sink: &dyn ProgressSink,
cancel: &CancelToken,
) -> Result<Manifest> {
use crate::verify::{MemOutboard, chunk_range};
if !self.prefix.is_concrete() {
return Err(BlobError::Usage(format!(
"cannot upload to the wildcard prefix {} — an upload has exactly one destination",
self.prefix
)));
}
let (outboard, total_len, fingerprint) = match &src {
UploadSrc::Path(path) => {
let hash_path = path.clone();
let (ob, len) =
tokio::task::spawn_blocking(move || -> std::io::Result<(MemOutboard, u64)> {
let file = std::fs::File::open(&hash_path)?;
let total_len = file.metadata()?.len();
Ok((verify::compute_outboard(file)?, total_len))
})
.await??;
(ob, len, None)
}
UploadSrc::Source(source) => {
let source = source.clone();
tokio::task::spawn_blocking(
move || -> std::io::Result<(MemOutboard, u64, Option<SourceFingerprint>)> {
let fingerprint = source.fingerprint();
let reader = source.open()?;
let total_len = reader
.size()?
.ok_or_else(|| std::io::Error::other("source has no known size"))?;
let ob = verify::compute_outboard_sized(
verify::ReadAtCursor::new(DynReadAt(&*reader)),
total_len,
)?;
Ok((ob, total_len, fingerprint))
},
)
.await??
}
};
let outboard = Arc::new(outboard);
let chunks = TransferChunks::new(spec.chunk_size, total_len)?;
let manifest = Manifest {
version: crate::wire::WIRE_VERSION,
id: BlobId::new(spec.id.clone())?,
filename: spec.filename,
total_len,
chunk_size: spec.chunk_size,
root: crate::hash::Hash::from(outboard.root),
created_ms: spec.created_ms,
ext: crate::wire::Ext::new(),
};
manifest.validate(u64::MAX)?;
let offer_key = push_offer_key(self.prefix.as_str(), &manifest.id);
let mut builder = self
.session
.get(&offer_key)
.consolidation(ConsolidationMode::None)
.priority(self.cfg.priority)
.timeout(self.cfg.query_timeout)
.payload(crate::wire::encode(&manifest)?);
if let Some(tok) = &token {
builder = builder.attachment(tok.clone());
}
let replies = builder.await.map_err(BlobError::zenoh)?;
let mut wanted: Option<Vec<(u32, u32)>> = None;
let mut refusal: Option<String> = None;
while let Ok(reply) = replies.recv_async().await {
match reply.result() {
Ok(sample) if ENC_PUSH.matches(sample.encoding()) => {
match decode::<Vec<(u32, u32)>>(&sample.payload().to_bytes()) {
Ok(ranges) => {
wanted = Some(ranges);
break;
}
Err(e) => {
refusal.get_or_insert_with(|| format!("undecodable offer reply: {e}"));
}
}
}
Ok(_) => continue,
Err(e) => {
refusal.get_or_insert_with(|| {
String::from_utf8_lossy(&e.payload().to_bytes()).into_owned()
});
}
}
}
let wanted = wanted.ok_or_else(|| {
BlobError::PushDenied(
refusal.unwrap_or_else(|| "no push endpoint answered the offer".into()),
)
})?;
let count = chunks.count();
let mut prev_end = 0u32;
for &(a, b) in &wanted {
if a < prev_end || a >= b || b > count {
return Err(BlobError::MalformedMessage(format!(
"push offer replied malformed wanted ranges ({a}, {b}) for {count} chunks"
)));
}
prev_end = b;
}
let to_send: u32 = wanted.iter().map(|(a, b)| b - a).sum();
if to_send < count {
sink.emit(Progress::Resumed {
received: count - to_send,
total: count,
});
} else {
sink.emit(Progress::Started {
total_len,
chunk_count: count,
});
}
zdebug!(id = %manifest.id, total = count, to_send, "push offer accepted");
let mut sent = count - to_send;
let mut bytes_sent: u64 = 0;
let mut reader: Box<dyn ReadAtSize> = tokio::task::spawn_blocking({
let src = src.clone();
move || -> std::io::Result<Box<dyn ReadAtSize>> {
match &src {
UploadSrc::Path(path) => Ok(Box::new(std::fs::File::open(path)?)),
UploadSrc::Source(source) => source.open(),
}
}
})
.await??;
for (start, end) in wanted {
for index in start..end {
if cancel.is_cancelled() {
sink.emit(Progress::Cancelled {
received: sent,
total: count,
});
return Err(BlobError::Cancelled {
received: sent,
total: count,
});
}
let ob = outboard.clone();
let byte_range = chunks.byte_range(index);
let (r, slice) = tokio::task::spawn_blocking(
move || -> (Box<dyn ReadAtSize>, std::io::Result<Vec<u8>>) {
let slice = verify::encode_slice(
DynReadAt(&*reader),
&*ob,
chunk_range(byte_range),
);
(reader, slice)
},
)
.await?;
reader = r;
let slice = slice?;
let mut b = self
.session
.get(push_slice_key(self.prefix.as_str(), &manifest.id, index))
.consolidation(ConsolidationMode::None)
.priority(self.cfg.priority)
.timeout(self.cfg.query_timeout)
.payload(slice);
if let Some(tok) = &token {
b = b.attachment(tok.clone());
}
let replies = b.await.map_err(BlobError::zenoh)?;
let mut acked = false;
let mut slice_refusal: Option<String> = None;
loop {
let Some(recv) = cancel.until_cancelled(replies.recv_async()).await else {
sink.emit(Progress::Cancelled {
received: sent,
total: count,
});
return Err(BlobError::Cancelled {
received: sent,
total: count,
});
};
let Ok(reply) = recv else { break };
match reply.result() {
Ok(sample) if ENC_PUSH.matches(sample.encoding()) => {
match decode::<u32>(&sample.payload().to_bytes()) {
Ok(_remaining) => {
acked = true;
break;
}
Err(e) => {
slice_refusal
.get_or_insert_with(|| format!("undecodable ack: {e}"));
}
}
}
Ok(_) => continue,
Err(e) => {
slice_refusal.get_or_insert_with(|| {
String::from_utf8_lossy(&e.payload().to_bytes()).into_owned()
});
}
}
}
if !acked {
return match slice_refusal {
Some(reason) => Err(BlobError::PushDenied(reason)),
None => Err(BlobError::Incomplete {
received: sent,
total: count,
}),
};
}
sent += 1;
bytes_sent += chunks.len_of(index) as u64;
sink.emit(Progress::Chunk {
index,
received: sent,
total: count,
bytes_received: bytes_sent,
});
}
}
match &src {
UploadSrc::Path(path) => sink.emit(Progress::Completed { path: path.clone() }),
UploadSrc::Source(source) => {
if let (Some(then), Some(now)) = (fingerprint, source.fingerprint())
&& then != now
{
return Err(BlobError::Protocol(format!(
"the source behind blob {:?} changed during the upload (was {} bytes, now {}); the receiver holds slices hashed from bytes that no longer exist — re-upload it",
manifest.id, then.len, now.len
)));
}
}
}
Ok(manifest)
}
async fn run_download_to_writer<W>(
&self,
req: &DownloadRequest,
writer: &mut W,
sink: &dyn ProgressSink,
cancel: &CancelToken,
) -> Result<TransferStats>
where
W: tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin + Send,
{
let started_at = tokio::time::Instant::now();
let (manifest, chunks) = self.start(req).await?;
let count = chunks.count();
zdebug!(id = %manifest.id, total_len = manifest.total_len, chunks = count, "writer download start");
sink.emit(Progress::Started {
total_len: manifest.total_len,
chunk_count: count,
});
let mut state = ResumeState::fresh(&manifest, count);
let mut stats = TransferStats::default();
let mut target = WriterTarget(writer);
let result = self
.fill_holes(
&manifest,
&chunks,
&mut target,
&mut state,
None,
sink,
cancel,
&mut stats,
)
.await;
match &result {
Err(BlobError::Cancelled { .. }) | Ok(_) => {}
Err(e) => sink.emit(Progress::Failed {
error: e.to_string(),
}),
}
result?;
stats.elapsed = started_at.elapsed();
Ok(stats)
}
async fn download_to_inner(
&self,
req: &DownloadRequest,
dest: &Path,
sink: &dyn ProgressSink,
cancel: &CancelToken,
overwrite: Overwrite,
) -> Result<TransferStats> {
let started_at = tokio::time::Instant::now();
if overwrite == Overwrite::Refuse && tokio::fs::try_exists(dest).await? {
return Err(BlobError::DestinationExists(dest.to_path_buf()));
}
let (manifest, chunks) = self.start(req).await?;
let count = chunks.count();
zdebug!(id = %manifest.id, total_len = manifest.total_len, chunks = count, "download start");
if let Some(parent) = dest.parent()
&& !parent.as_os_str().is_empty()
{
tokio::fs::create_dir_all(parent).await?;
}
let part = part_path(dest);
let existing_len = tokio::fs::metadata(&part).await.map(|m| m.len()).ok();
let mut state = match ResumeState::load(&part).await {
Some(s) if s.matches(&manifest, count) && existing_len == Some(manifest.total_len) => {
sink.emit(Progress::Resumed {
received: s.received(),
total: count,
});
s
}
_ => {
let file = tokio::fs::File::create(&part).await?;
file.set_len(manifest.total_len).await?;
let fresh = ResumeState::fresh(&manifest, count);
fresh.save_atomic(&part).await?;
sink.emit(Progress::Started {
total_len: manifest.total_len,
chunk_count: count,
});
fresh
}
};
let mut file = tokio::fs::OpenOptions::new()
.write(true)
.open(&part)
.await?;
let mut stats = TransferStats {
chunks_resumed: state.received(),
..Default::default()
};
self.fill_holes(
&manifest,
&chunks,
&mut file,
&mut state,
Some(&part),
sink,
cancel,
&mut stats,
)
.await?;
file.sync_data().await?;
drop(file);
if overwrite == Overwrite::Refuse && tokio::fs::try_exists(dest).await? {
return Err(BlobError::DestinationExists(dest.to_path_buf()));
}
tokio::fs::rename(&part, dest).await?;
ResumeState::remove(&part).await;
sink.emit(Progress::Completed {
path: dest.to_path_buf(),
});
stats.elapsed = started_at.elapsed();
zdebug!(id = %manifest.id, fetched = stats.chunks_fetched, rejected = stats.rejected, "download complete");
Ok(stats)
}
async fn start(&self, req: &DownloadRequest) -> Result<(Manifest, TransferChunks)> {
let manifest = self
.fetch_manifest_matching(&req.id, req.expected_root)
.await?;
if manifest.total_len == 0 && manifest.root != Hash::of(b"") {
return Err(BlobError::RootMismatch {
expected: Hash::of(b""),
actual: manifest.root,
});
}
let chunks = manifest.chunks()?;
Ok((manifest, chunks))
}
#[allow(clippy::too_many_arguments)]
async fn fill_holes<T: SliceTarget>(
&self,
manifest: &Manifest,
chunks: &TransferChunks,
file: &mut T,
state: &mut ResumeState,
part: Option<&Path>,
sink: &dyn ProgressSink,
cancel: &CancelToken,
stats: &mut TransferStats,
) -> Result<()> {
let count = chunks.count();
let root: blake3::Hash = manifest.root.into();
let mut bytes_received: u64 = (0..count)
.filter(|i| state.is_set(*i))
.map(|i| chunks.len_of(i) as u64)
.sum();
let mut no_progress = 0u32;
let mut marks_since_save = 0u32;
let mut last_save = tokio::time::Instant::now();
while !state.is_complete(count) {
if cancel.is_cancelled() {
return Self::persist_cancel(file, state, part, sink, count).await;
}
let mut holes = state.missing_ranges(count);
holes.truncate(MAX_RANGE_SPANS);
let mut budget = manifest
.max_chunks_per_query()
.map_or(self.cfg.max_chunks_per_query, |served| {
self.cfg.max_chunks_per_query.min(served)
})
.max(1);
for r in holes.iter_mut() {
let take = (r.end - r.start).min(budget);
r.end = r.start + take;
budget -= take;
}
holes.retain(|r| !r.is_empty());
let selector = slice_selector(self.prefix.as_str(), &manifest.id, &holes);
let before = state.received();
stats.queries += 1;
let replies = self
.session
.get(&selector)
.consolidation(ConsolidationMode::None)
.priority(self.cfg.priority)
.timeout(self.cfg.query_timeout)
.await
.map_err(BlobError::zenoh)?;
loop {
let Some(recv) = cancel.until_cancelled(replies.recv_async()).await else {
drop(replies);
return Self::persist_cancel(file, state, part, sink, count).await;
};
let Ok(reply) = recv else { break };
let Ok(sample) = reply.result() else { continue };
if !ENC_SLICE.matches(sample.encoding()) {
continue;
}
let Some(index) = parse_slice_index(sample.key_expr().as_str()) else {
continue;
};
if index >= count || state.is_set(index) {
continue; }
let payload = sample.payload().to_bytes();
let byte_range = chunks.byte_range(index);
let mut leaves: Vec<(u64, Vec<u8>)> = Vec::new();
let decoded = verify::decode_slice(
&root,
manifest.total_len,
verify::chunk_range(byte_range),
&payload,
|off, data| {
leaves.push((off, data.to_vec()));
Ok(())
},
);
if decoded.is_err() {
stats.rejected += 1;
zdebug!(id = %manifest.id, index, "rejected unverifiable slice");
continue;
}
for (off, data) in leaves {
file.write_leaf(off, &data).await?;
}
if state.mark(index) {
bytes_received += chunks.len_of(index) as u64;
stats.chunks_fetched += 1;
stats.bytes_fetched += chunks.len_of(index) as u64;
marks_since_save += 1;
sink.emit(Progress::Chunk {
index,
received: state.received(),
total: count,
bytes_received,
});
if let Some(part) = part
&& (marks_since_save >= 64 || last_save.elapsed() > Duration::from_secs(2))
{
file.commit().await?;
state.save_atomic(part).await?;
marks_since_save = 0;
last_save = tokio::time::Instant::now();
}
}
}
if state.is_complete(count) {
break;
}
if state.received() == before {
no_progress += 1;
stats.retries += 1;
zdebug!(id = %manifest.id, attempt = no_progress, "no progress; backing off");
if no_progress >= self.cfg.retry.max_attempts {
file.commit().await?;
if let Some(part) = part {
state.save_atomic(part).await?;
}
return Err(BlobError::Incomplete {
received: state.received(),
total: count,
});
}
tokio::time::sleep(self.cfg.retry.backoff(no_progress - 1)).await;
} else {
no_progress = 0;
}
}
file.commit().await?;
if let Some(part) = part {
state.save_atomic(part).await?;
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
async fn fill_holes_striped(
&self,
manifest: &Manifest,
chunks: &TransferChunks,
holders: &[BlobProbe],
file: &mut tokio::fs::File,
state: &mut ResumeState,
part: &Path,
sink: &dyn ProgressSink,
cancel: &CancelToken,
stats: &mut TransferStats,
) -> Result<()> {
let count = chunks.count();
let root: blake3::Hash = manifest.root.into();
let total_len = manifest.total_len;
let mut bytes_received: u64 = (0..count)
.filter(|i| state.is_set(*i))
.map(|i| chunks.len_of(i) as u64)
.sum();
let mut no_progress = 0u32;
let mut rejects: Vec<u32> = vec![0; holders.len()];
let budget = manifest
.max_chunks_per_query()
.map_or(self.cfg.max_chunks_per_query, |served| {
self.cfg.max_chunks_per_query.min(served)
})
.max(1);
while !state.is_complete(count) {
if cancel.is_cancelled() {
return Self::persist_cancel(file, state, Some(part), sink, count).await;
}
let before = state.received();
let live: Vec<usize> = (0..holders.len())
.filter(|i| rejects[*i] < self.cfg.retry.max_attempts.max(1) * 8)
.collect();
if live.is_empty() {
return Err(BlobError::Incomplete {
received: state.received(),
total: count,
});
}
let missing: Vec<u32> = (0..count).filter(|i| !state.is_set(*i)).collect();
let endgame = missing.len() <= live.len().max(2) * 2;
let mut assignment: Vec<Vec<u32>> = vec![Vec::new(); holders.len()];
let mut cursor = 0usize;
for &idx in missing.iter().take(budget as usize * live.len()) {
let mut dealt = 0;
let copies = if endgame { 2.min(live.len()) } else { 1 };
while dealt < copies {
let h = live[cursor % live.len()];
cursor += 1;
let claims = holders[h]
.availability
.as_ref()
.is_none_or(|a| a.is_set(idx));
if claims && assignment[h].len() < budget as usize {
assignment[h].push(idx);
dealt += 1;
}
if cursor.is_multiple_of(live.len()) && dealt == 0 {
break;
}
}
}
type Verified = (u32, Vec<(u64, Vec<u8>)>, usize, u32);
let (tx, mut rx) = tokio::sync::mpsc::channel::<Verified>(live.len() * 2);
let mut tasks = tokio::task::JoinSet::new();
for h in live.iter().copied() {
let indices = std::mem::take(&mut assignment[h]);
if indices.is_empty() {
continue;
}
let ranges = coalesce(&indices);
let selector = slice_selector(holders[h].origin.as_str(), &manifest.id, &ranges);
let session = self.session.clone();
let timeout = self.cfg.query_timeout;
let priority = self.cfg.priority;
let chunks = *chunks;
let tx = tx.clone();
tasks.spawn(async move {
let Ok(replies) = session
.get(&selector)
.consolidation(ConsolidationMode::None)
.priority(priority)
.timeout(timeout)
.await
else {
return;
};
while let Ok(reply) = replies.recv_async().await {
let Ok(sample) = reply.result() else { continue };
if !ENC_SLICE.matches(sample.encoding()) {
continue;
}
let Some(index) = parse_slice_index(sample.key_expr().as_str()) else {
continue;
};
let mut leaves: Vec<(u64, Vec<u8>)> = Vec::new();
let ok = verify::decode_slice(
&root,
total_len,
verify::chunk_range(chunks.byte_range(index)),
&sample.payload().to_bytes(),
|off, data| {
leaves.push((off, data.to_vec()));
Ok(())
},
)
.is_ok();
let msg = if ok {
(index, leaves, h, 0)
} else {
(index, Vec::new(), h, 1)
};
if tx.send(msg).await.is_err() {
return;
}
}
});
}
drop(tx);
loop {
let Some(next) = cancel.until_cancelled(rx.recv()).await else {
return Self::persist_cancel(file, state, Some(part), sink, count).await;
};
let Some((index, leaves, holder, rejected)) = next else {
break;
};
if rejected > 0 {
rejects[holder] += rejected;
stats.rejected += rejected;
continue;
}
if index >= count || state.is_set(index) {
continue; }
for (off, data) in leaves {
file.write_leaf(off, &data).await?;
}
if state.mark(index) {
bytes_received += chunks.len_of(index) as u64;
stats.chunks_fetched += 1;
stats.bytes_fetched += chunks.len_of(index) as u64;
sink.emit(Progress::Chunk {
index,
received: state.received(),
total: count,
bytes_received,
});
}
}
tasks.shutdown().await;
stats.queries += live.len() as u64;
file.commit().await?;
state.save_atomic(part).await?;
if state.is_complete(count) {
break;
}
if state.received() == before {
no_progress += 1;
stats.retries += 1;
if no_progress >= self.cfg.retry.max_attempts {
return Err(BlobError::Incomplete {
received: state.received(),
total: count,
});
}
tokio::time::sleep(self.cfg.retry.backoff(no_progress - 1)).await;
} else {
no_progress = 0;
}
}
file.commit().await?;
state.save_atomic(part).await?;
Ok(())
}
async fn persist_cancel<T: SliceTarget>(
file: &mut T,
state: &ResumeState,
part: Option<&Path>,
sink: &dyn ProgressSink,
count: u32,
) -> Result<()> {
file.commit().await?;
if let Some(part) = part {
state.save_atomic(part).await?;
}
sink.emit(Progress::Cancelled {
received: state.received(),
total: count,
});
Err(BlobError::Cancelled {
received: state.received(),
total: count,
})
}
}
#[must_use = "a download does nothing until it is awaited"]
pub struct Download<'a> {
client: &'a BlobClient,
req: &'a DownloadRequest,
dest: &'a Path,
sink: Option<&'a dyn ProgressSink>,
cancel: Option<&'a CancelToken>,
overwrite: Option<Overwrite>,
holders: Option<&'a [BlobProbe]>,
}
impl std::fmt::Debug for Download<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Download")
.field("req", &self.req)
.field("dest", &self.dest)
.field("overwrite", &self.overwrite)
.field("holders", &self.holders.map(<[BlobProbe]>::len))
.finish_non_exhaustive()
}
}
impl<'a> Download<'a> {
pub fn progress(mut self, sink: &'a dyn ProgressSink) -> Self {
self.sink = Some(sink);
self
}
pub fn cancel(mut self, cancel: &'a CancelToken) -> Self {
self.cancel = Some(cancel);
self
}
pub fn overwrite(mut self, policy: Overwrite) -> Self {
self.overwrite = Some(policy);
self
}
pub fn striped(mut self, holders: &'a [BlobProbe]) -> Self {
self.holders = Some(holders);
self
}
}
pub(crate) const NO_PROGRESS: &(dyn ProgressSink + 'static) = &();
impl<'a> std::future::IntoFuture for Download<'a> {
type Output = Result<TransferStats>;
type IntoFuture =
std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let sink = self.sink.unwrap_or(NO_PROGRESS);
let fresh;
let cancel = match self.cancel {
Some(c) => c,
None => {
fresh = CancelToken::new();
&fresh
}
};
let overwrite = self.overwrite.unwrap_or(self.client.cfg.overwrite);
match self.holders {
Some(holders) => {
self.client
.run_download_striped(self.req, self.dest, holders, sink, cancel, overwrite)
.await
}
None => {
self.client
.run_download_to(self.req, self.dest, sink, cancel, overwrite)
.await
}
}
})
}
}
#[must_use = "a download does nothing until it is awaited"]
#[derive(Debug)]
pub struct StagedDownload<'a> {
inner: Download<'a>,
}
impl<'a> StagedDownload<'a> {
pub fn progress(mut self, sink: &'a dyn ProgressSink) -> Self {
self.inner = self.inner.progress(sink);
self
}
pub fn cancel(mut self, cancel: &'a CancelToken) -> Self {
self.inner = self.inner.cancel(cancel);
self
}
pub fn overwrite(mut self, policy: Overwrite) -> Self {
self.inner = self.inner.overwrite(policy);
self
}
}
impl<'a> std::future::IntoFuture for StagedDownload<'a> {
type Output = Result<Staged>;
type IntoFuture =
std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let d = self.inner;
let sink = d.sink.unwrap_or(NO_PROGRESS);
let fresh;
let cancel = match d.cancel {
Some(c) => c,
None => {
fresh = CancelToken::new();
&fresh
}
};
let overwrite = d.overwrite.unwrap_or(d.client.cfg.overwrite);
d.client
.run_download_staged(d.req, d.dest, sink, cancel, overwrite)
.await
})
}
}
#[must_use = "a download does nothing until it is awaited"]
pub struct DownloadToWriter<'a, W> {
client: &'a BlobClient,
req: &'a DownloadRequest,
writer: &'a mut W,
sink: Option<&'a dyn ProgressSink>,
cancel: Option<&'a CancelToken>,
}
impl<W> std::fmt::Debug for DownloadToWriter<'_, W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DownloadToWriter")
.field("req", &self.req)
.finish_non_exhaustive()
}
}
impl<'a, W> DownloadToWriter<'a, W> {
pub fn progress(mut self, sink: &'a dyn ProgressSink) -> Self {
self.sink = Some(sink);
self
}
pub fn cancel(mut self, cancel: &'a CancelToken) -> Self {
self.cancel = Some(cancel);
self
}
}
impl<'a, W> std::future::IntoFuture for DownloadToWriter<'a, W>
where
W: tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin + Send,
{
type Output = Result<TransferStats>;
type IntoFuture =
std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let sink = self.sink.unwrap_or(NO_PROGRESS);
let fresh;
let cancel = match self.cancel {
Some(c) => c,
None => {
fresh = CancelToken::new();
&fresh
}
};
self.client
.run_download_to_writer(self.req, self.writer, sink, cancel)
.await
})
}
}
#[derive(Clone)]
enum UploadSrc {
Path(PathBuf),
Source(Arc<dyn BlobSource>),
}
impl std::fmt::Debug for UploadSrc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UploadSrc::Path(path) => f.debug_tuple("Path").field(path).finish(),
UploadSrc::Source(_) => f.write_str("Source"),
}
}
}
#[must_use = "an upload does nothing until it is awaited"]
pub struct Upload<'a> {
client: &'a BlobClient,
spec: BlobSpec,
src: UploadSrc,
token: Option<Vec<u8>>,
sink: Option<&'a dyn ProgressSink>,
cancel: Option<&'a CancelToken>,
}
impl std::fmt::Debug for Upload<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Upload")
.field("spec", &self.spec)
.field("src", &self.src)
.field("token", &self.token.as_ref().map(Vec::len))
.finish_non_exhaustive()
}
}
impl<'a> Upload<'a> {
pub fn token(mut self, token: impl Into<Vec<u8>>) -> Self {
self.token = Some(token.into());
self
}
pub fn progress(mut self, sink: &'a dyn ProgressSink) -> Self {
self.sink = Some(sink);
self
}
pub fn cancel(mut self, cancel: &'a CancelToken) -> Self {
self.cancel = Some(cancel);
self
}
}
impl<'a> std::future::IntoFuture for Upload<'a> {
type Output = Result<Manifest>;
type IntoFuture =
std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let sink = self.sink.unwrap_or(NO_PROGRESS);
let fresh;
let cancel = match self.cancel {
Some(c) => c,
None => {
fresh = CancelToken::new();
&fresh
}
};
self.client
.run_upload(self.spec, self.src, self.token, sink, cancel)
.await
})
}
}
trait SliceTarget: Send {
async fn write_leaf(&mut self, offset: u64, data: &[u8]) -> Result<()>;
async fn commit(&mut self) -> Result<()>;
}
impl SliceTarget for tokio::fs::File {
async fn write_leaf(&mut self, offset: u64, data: &[u8]) -> Result<()> {
self.seek(SeekFrom::Start(offset)).await?;
self.write_all(data).await?;
Ok(())
}
async fn commit(&mut self) -> Result<()> {
self.sync_data().await?;
Ok(())
}
}
struct WriterTarget<'a, W>(&'a mut W);
impl<W> SliceTarget for WriterTarget<'_, W>
where
W: tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin + Send,
{
async fn write_leaf(&mut self, offset: u64, data: &[u8]) -> Result<()> {
self.0.seek(SeekFrom::Start(offset)).await?;
self.0.write_all(data).await?;
Ok(())
}
async fn commit(&mut self) -> Result<()> {
self.0.flush().await?;
Ok(())
}
}
fn coalesce(indices: &[u32]) -> Vec<std::ops::Range<u32>> {
let mut out: Vec<std::ops::Range<u32>> = Vec::new();
for &i in indices {
match out.last_mut() {
Some(last) if last.end == i => last.end = i + 1,
_ => out.push(i..i + 1),
}
}
out.truncate(MAX_RANGE_SPANS);
out
}
struct ActiveGuard {
active: Arc<std::sync::Mutex<std::collections::HashSet<PathBuf>>>,
dest: PathBuf,
}
impl ActiveGuard {
fn acquire(
active: &Arc<std::sync::Mutex<std::collections::HashSet<PathBuf>>>,
dest: &Path,
) -> Result<Self> {
let mut set = active.lock().unwrap_or_else(|e| e.into_inner());
if !set.insert(dest.to_path_buf()) {
return Err(BlobError::Usage(format!(
"a download to {dest:?} is already in progress on this client"
)));
}
Ok(ActiveGuard {
active: active.clone(),
dest: dest.to_path_buf(),
})
}
}
impl Drop for ActiveGuard {
fn drop(&mut self) {
self.active
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&self.dest);
}
}
fn part_path(dest: &Path) -> PathBuf {
let mut p = dest.as_os_str().to_os_string();
p.push(".part");
PathBuf::from(p)
}
fn parse_slice_index(key: &str) -> Option<u32> {
let (head, idx) = key.rsplit_once('/')?;
if head.ends_with("/slice") {
idx.parse().ok()
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slice_index_parsing() {
assert_eq!(parse_slice_index("p/A/slice/7"), Some(7));
assert_eq!(parse_slice_index("p/A/manifest"), None);
assert_eq!(parse_slice_index("p/A/slice/x"), None);
assert_eq!(parse_slice_index("p/A/chunk/7"), None); }
#[test]
fn part_path_appends_extension() {
assert_eq!(
part_path(Path::new("/tmp/out/file.bin")),
Path::new("/tmp/out/file.bin.part")
);
}
#[test]
fn backoff_doubles_and_caps() {
let r = RetryPolicy::default();
assert_eq!(r.backoff(0), Duration::from_millis(250));
assert_eq!(r.backoff(1), Duration::from_millis(500));
assert_eq!(r.backoff(10), Duration::from_secs(10)); }
#[test]
fn coalesce_merges_exactly_the_adjacent_runs() {
assert_eq!(coalesce(&[]), Vec::<std::ops::Range<u32>>::new());
assert_eq!(coalesce(&[5]), vec![5..6]);
assert_eq!(coalesce(&[0, 1, 2]), vec![0..3]);
assert_eq!(coalesce(&[0, 2, 4]), vec![0..1, 2..3, 4..5]);
assert_eq!(coalesce(&[0, 1, 3, 4, 5, 9]), vec![0..2, 3..6, 9..10]);
assert_eq!(coalesce(&[1, 1]), vec![1..2, 1..2]);
assert_eq!(coalesce(&[3, 1]), vec![3..4, 1..2]);
let indices: Vec<u32> = (0..50).filter(|i| i % 7 != 0).collect();
let covered: Vec<u32> = coalesce(&indices).into_iter().flatten().collect();
assert_eq!(covered, indices);
}
#[test]
fn origin_of_strips_exactly_the_id_and_endpoint() {
assert_eq!(
origin_of("v1/host-a/@blob/art/blob-1/manifest", "blob-1", "manifest").as_deref(),
Some("v1/host-a/@blob/art")
);
assert_eq!(
origin_of("p/blob-1/have", "blob-1", "have").as_deref(),
Some("p")
);
assert_eq!(origin_of("p/other/manifest", "blob-1", "manifest"), None);
assert_eq!(origin_of("p/blob-1/have", "blob-1", "manifest"), None);
assert_eq!(origin_of("blob-1/manifest", "blob-1", "manifest"), None);
assert_eq!(origin_of("", "blob-1", "manifest"), None);
assert_eq!(origin_of("p/xblob-1/manifest", "blob-1", "manifest"), None);
}
}