1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
//! Functions that use the iroh-bytes protocol in conjunction with a bao store.
use std::path::PathBuf;
use std::time::Duration;
use bytes::Bytes;
use futures::{future::LocalBoxFuture, FutureExt, StreamExt};
use iroh_base::{hash::Hash, rpc::RpcError};
use serde::{Deserialize, Serialize};
use crate::protocol::RangeSpec;
use std::io;
use crate::hashseq::parse_hash_seq;
use crate::store::PossiblyPartialEntry;
use crate::{
get::{
self,
fsm::{AtBlobHeader, AtEndBlob, ConnectedNext, EndBlobNext},
Stats,
},
protocol::{GetRequest, RangeSpecSeq},
store::{MapEntry, PartialMap, PartialMapEntry, Store as BaoStore},
util::progress::{IdGenerator, ProgressSender},
BlobFormat, HashAndFormat, IROH_BLOCK_SIZE,
};
use anyhow::Context;
use bao_tree::io::fsm::OutboardMut;
use bao_tree::{ByteNum, ChunkRanges};
use iroh_io::{AsyncSliceReader, AsyncSliceWriter};
use tracing::trace;
/// Get a blob or collection into a store.
///
/// This considers data that is already in the store, and will only request
/// the remaining data.
pub async fn get_to_db<D: BaoStore>(
db: &D,
conn: quinn::Connection,
hash_and_format: &HashAndFormat,
sender: impl ProgressSender<Msg = DownloadProgress> + IdGenerator,
) -> anyhow::Result<Stats> {
let HashAndFormat { hash, format } = hash_and_format;
match format {
BlobFormat::Raw => get_blob(db, conn, hash, sender).await,
BlobFormat::HashSeq => get_hash_seq(db, conn, hash, sender).await,
}
}
/// Get a blob that was requested completely.
///
/// We need to create our own files and handle the case where an outboard
/// is not needed.
async fn get_blob<D: BaoStore>(
db: &D,
conn: quinn::Connection,
hash: &Hash,
progress: impl ProgressSender<Msg = DownloadProgress> + IdGenerator,
) -> anyhow::Result<Stats> {
let end = match db.get_possibly_partial(hash) {
PossiblyPartialEntry::Complete(entry) => {
tracing::info!("already got entire blob");
progress
.send(DownloadProgress::FoundLocal {
child: 0,
hash: *hash,
size: entry.size(),
valid_ranges: RangeSpec::all(),
})
.await?;
return Ok(Stats::default());
}
PossiblyPartialEntry::Partial(entry) => {
trace!("got partial data for {}", hash);
let valid_ranges = valid_ranges::<D>(&entry)
.await
.ok()
.unwrap_or_else(ChunkRanges::all);
progress
.send(DownloadProgress::FoundLocal {
child: 0,
hash: *hash,
size: entry.size(),
valid_ranges: RangeSpec::new(&valid_ranges),
})
.await?;
let required_ranges: ChunkRanges = ChunkRanges::all().difference(&valid_ranges);
let request = GetRequest::new(*hash, RangeSpecSeq::from_ranges([required_ranges]));
// full request
let request = get::fsm::start(conn, request);
// create a new bidi stream
let connected = request.next().await?;
// next step. we have requested a single hash, so this must be StartRoot
let ConnectedNext::StartRoot(start) = connected.next().await? else {
anyhow::bail!("expected StartRoot");
};
// move to the header
let header = start.next();
// do the ceremony of getting the blob and adding it to the database
get_blob_inner_partial(db, header, entry, progress).await?
}
PossiblyPartialEntry::NotFound => {
// full request
let request = get::fsm::start(conn, GetRequest::single(*hash));
// create a new bidi stream
let connected = request.next().await?;
// next step. we have requested a single hash, so this must be StartRoot
let ConnectedNext::StartRoot(start) = connected.next().await? else {
anyhow::bail!("expected StartRoot");
};
// move to the header
let header = start.next();
// do the ceremony of getting the blob and adding it to the database
get_blob_inner(db, header, progress).await?
}
};
// we have requested a single hash, so we must be at closing
let EndBlobNext::Closing(end) = end.next() else {
anyhow::bail!("expected Closing");
};
// this closes the bidi stream. Do something with the stats?
let stats = end.next().await?;
anyhow::Ok(stats)
}
/// Given a partial entry, get the valid ranges.
pub async fn valid_ranges<D: PartialMap>(entry: &D::PartialEntry) -> anyhow::Result<ChunkRanges> {
use tracing::trace as log;
// compute the valid range from just looking at the data file
let mut data_reader = entry.data_reader().await?;
let data_size = data_reader.len().await?;
let valid_from_data = ChunkRanges::from(..ByteNum(data_size).full_chunks());
// compute the valid range from just looking at the outboard file
let mut outboard = entry.outboard().await?;
let valid_from_outboard = bao_tree::io::fsm::valid_ranges(&mut outboard).await?;
let valid: ChunkRanges = valid_from_data.intersection(&valid_from_outboard);
log!("valid_from_data: {:?}", valid_from_data);
log!("valid_from_outboard: {:?}", valid_from_data);
Ok(valid)
}
/// Get a blob that was requested completely.
///
/// We need to create our own files and handle the case where an outboard
/// is not needed.
async fn get_blob_inner<D: BaoStore>(
db: &D,
at_header: AtBlobHeader,
sender: impl ProgressSender<Msg = DownloadProgress> + IdGenerator,
) -> anyhow::Result<AtEndBlob> {
// read the size
let (at_content, size) = at_header.next().await?;
let hash = at_content.hash();
let child_offset = at_content.offset();
// create the temp file pair
let entry = db.get_or_create_partial(hash, size)?;
// open the data file in any case
let df = entry.data_writer().await?;
let mut of: Option<D::OutboardMut> = if needs_outboard(size) {
Some(entry.outboard_mut().await?)
} else {
None
};
// allocate a new id for progress reports for this transfer
let id = sender.new_id();
sender
.send(DownloadProgress::Found {
id,
hash,
size,
child: child_offset,
})
.await?;
let sender2 = sender.clone();
let on_write = move |offset: u64, _length: usize| {
// if try send fails it means that the receiver has been dropped.
// in that case we want to abort the write_all_with_outboard.
sender2
.try_send(DownloadProgress::Progress { id, offset })
.map_err(|e| {
tracing::info!("aborting download of {}", hash);
e
})?;
Ok(())
};
let mut pw = ProgressSliceWriter2::new(df, on_write);
// use the convenience method to write all to the two vfs objects
let end = at_content
.write_all_with_outboard(of.as_mut(), &mut pw)
.await?;
// sync the data file
pw.sync().await?;
// sync the outboard file, if we wrote one
if let Some(mut of) = of {
of.sync().await?;
}
db.insert_complete(entry).await?;
// notify that we are done
sender.send(DownloadProgress::Done { id }).await?;
Ok(end)
}
fn needs_outboard(size: u64) -> bool {
size > (IROH_BLOCK_SIZE.bytes() as u64)
}
/// Get a blob that was requested partially.
///
/// We get passed the data and outboard ids. Partial downloads are only done
/// for large blobs where the outboard is present.
async fn get_blob_inner_partial<D: BaoStore>(
db: &D,
at_header: AtBlobHeader,
entry: D::PartialEntry,
sender: impl ProgressSender<Msg = DownloadProgress> + IdGenerator,
) -> anyhow::Result<AtEndBlob> {
// TODO: the data we get is validated at this point, but we need to check
// that it actually contains the requested ranges. Or DO WE?
// read the size
let (at_content, size) = at_header.next().await?;
// open the data file in any case
let df = entry.data_writer().await?;
let mut of = if needs_outboard(size) {
Some(entry.outboard_mut().await?)
} else {
None
};
// allocate a new id for progress reports for this transfer
let id = sender.new_id();
let hash = at_content.hash();
let child_offset = at_content.offset();
sender
.send(DownloadProgress::Found {
id,
hash,
size,
child: child_offset,
})
.await?;
let sender2 = sender.clone();
let on_write = move |offset: u64, _length: usize| {
// if try send fails it means that the receiver has been dropped.
// in that case we want to abort the write_all_with_outboard.
sender2
.try_send(DownloadProgress::Progress { id, offset })
.map_err(|e| {
tracing::info!("aborting download of {}", hash);
e
})?;
Ok(())
};
let mut pw = ProgressSliceWriter2::new(df, on_write);
// use the convenience method to write all to the two vfs objects
let at_end = at_content
.write_all_with_outboard(of.as_mut(), &mut pw)
.await?;
// sync the data file
pw.sync().await?;
// sync the outboard file
if let Some(mut of) = of {
of.sync().await?;
}
// actually store the data. it is up to the db to decide if it wants to
// rename the files or not.
db.insert_complete(entry).await?;
// notify that we are done
sender.send(DownloadProgress::Done { id }).await?;
Ok(at_end)
}
/// Get information about a blob in a store.
///
/// This will compute the valid ranges for partial blobs, so it is somewhat expensive for those.
pub async fn blob_info<D: BaoStore>(db: &D, hash: &Hash) -> io::Result<BlobInfo<D>> {
io::Result::Ok(match db.get_possibly_partial(hash) {
PossiblyPartialEntry::Partial(entry) => {
let valid_ranges = valid_ranges::<D>(&entry)
.await
.ok()
.unwrap_or_else(ChunkRanges::all);
BlobInfo::Partial {
entry,
valid_ranges,
}
}
PossiblyPartialEntry::Complete(entry) => BlobInfo::Complete { size: entry.size() },
PossiblyPartialEntry::NotFound => BlobInfo::Missing,
})
}
/// Like `get_blob_info`, but for multiple hashes
async fn blob_infos<D: BaoStore>(db: &D, hash_seq: &[Hash]) -> io::Result<Vec<BlobInfo<D>>> {
let items = futures::stream::iter(hash_seq)
.then(|hash| blob_info(db, hash))
.collect::<Vec<_>>();
items.await.into_iter().collect()
}
/// Get a sequence of hashes
async fn get_hash_seq<D: BaoStore>(
db: &D,
conn: quinn::Connection,
root_hash: &Hash,
sender: impl ProgressSender<Msg = DownloadProgress> + IdGenerator,
) -> anyhow::Result<Stats> {
use tracing::info as log;
let finishing =
if let PossiblyPartialEntry::Complete(entry) = db.get_possibly_partial(root_hash) {
log!("already got collection - doing partial download");
// send info that we have the hashseq itself entirely
sender
.send(DownloadProgress::FoundLocal {
child: 0,
hash: *root_hash,
size: entry.size(),
valid_ranges: RangeSpec::all(),
})
.await?;
// got the collection
let reader = entry.data_reader().await?;
let (mut hash_seq, children) = parse_hash_seq(reader).await?;
sender
.send(DownloadProgress::FoundHashSeq {
hash: *root_hash,
children,
})
.await?;
let mut children: Vec<Hash> = vec![];
while let Some(hash) = hash_seq.next().await? {
children.push(hash);
}
let missing_info = blob_infos(db, &children).await?;
// send the info about what we have
for (i, info) in missing_info.iter().enumerate() {
if let Some(size) = info.size() {
sender
.send(DownloadProgress::FoundLocal {
child: (i as u64) + 1,
hash: children[i],
size,
valid_ranges: RangeSpec::new(&info.valid_ranges()),
})
.await?;
}
}
if missing_info
.iter()
.all(|x| matches!(x, BlobInfo::Complete { .. }))
{
log!("nothing to do");
return Ok(Stats::default());
}
let missing_iter = std::iter::once(ChunkRanges::empty())
.chain(missing_info.iter().map(|x| x.missing_ranges()))
.collect::<Vec<_>>();
log!("requesting chunks {:?}", missing_iter);
let request = GetRequest::new(*root_hash, RangeSpecSeq::from_ranges(missing_iter));
let request = get::fsm::start(conn, request);
// create a new bidi stream
let connected = request.next().await?;
log!("connected");
// we have not requested the root, so this must be StartChild
let ConnectedNext::StartChild(start) = connected.next().await? else {
anyhow::bail!("expected StartChild");
};
let mut next = EndBlobNext::MoreChildren(start);
// read all the children
loop {
let start = match next {
EndBlobNext::MoreChildren(start) => start,
EndBlobNext::Closing(finish) => break finish,
};
let child_offset =
usize::try_from(start.child_offset()).context("child offset too large")?;
let (child_hash, info) =
match (children.get(child_offset), missing_info.get(child_offset)) {
(Some(blob), Some(info)) => (*blob, info),
_ => break start.finish(),
};
tracing::info!(
"requesting child {} {:?}",
child_hash,
info.missing_ranges()
);
let header = start.next(child_hash);
let end_blob = match info {
BlobInfo::Missing => get_blob_inner(db, header, sender.clone()).await?,
BlobInfo::Partial { entry, .. } => {
get_blob_inner_partial(db, header, entry.clone(), sender.clone()).await?
}
BlobInfo::Complete { .. } => anyhow::bail!("got data we have not requested"),
};
next = end_blob.next();
}
} else {
tracing::info!("don't have collection - doing full download");
// don't have the collection, so probably got nothing
let request = get::fsm::start(conn, GetRequest::all(*root_hash));
// create a new bidi stream
let connected = request.next().await?;
// next step. we have requested a single hash, so this must be StartRoot
let ConnectedNext::StartRoot(start) = connected.next().await? else {
anyhow::bail!("expected StartRoot");
};
// move to the header
let header = start.next();
// read the blob and add it to the database
let end_root = get_blob_inner(db, header, sender.clone()).await?;
// read the collection fully for now
let entry = db.get(root_hash).context("just downloaded")?;
let reader = entry.data_reader().await?;
let (mut collection, count) = parse_hash_seq(reader).await?;
sender
.send(DownloadProgress::FoundHashSeq {
hash: *root_hash,
children: count,
})
.await?;
let mut children = vec![];
while let Some(hash) = collection.next().await? {
children.push(hash);
}
let mut next = end_root.next();
// read all the children
loop {
let start = match next {
EndBlobNext::MoreChildren(start) => start,
EndBlobNext::Closing(finish) => break finish,
};
let child_offset =
usize::try_from(start.child_offset()).context("child offset too large")?;
let child_hash = match children.get(child_offset) {
Some(blob) => *blob,
None => break start.finish(),
};
let header = start.next(child_hash);
let end_blob = get_blob_inner(db, header, sender.clone()).await?;
next = end_blob.next();
}
};
// this closes the bidi stream. Do something with the stats?
let stats = finishing.next().await?;
anyhow::Ok(stats)
}
/// Information about a the status of a blob in a store.
#[derive(Debug, Clone)]
pub enum BlobInfo<D: BaoStore> {
/// we have the blob completely
Complete {
/// The size of the entry in bytes.
size: u64,
},
/// we have the blob partially
Partial {
/// The partial entry.
entry: D::PartialEntry,
/// The ranges that are available locally.
valid_ranges: ChunkRanges,
},
/// we don't have the blob at all
Missing,
}
impl<D: BaoStore> BlobInfo<D> {
/// The size of the blob, if known.
pub fn size(&self) -> Option<u64> {
match self {
BlobInfo::Complete { size } => Some(*size),
BlobInfo::Partial { entry, .. } => Some(entry.size()),
BlobInfo::Missing => None,
}
}
/// Ranges that are valid locally.
///
/// This will be all for complete blobs, empty for missing blobs,
/// and a set with possibly open last range for partial blobs.
pub fn valid_ranges(&self) -> ChunkRanges {
match self {
BlobInfo::Complete { .. } => ChunkRanges::all(),
BlobInfo::Partial { valid_ranges, .. } => valid_ranges.clone(),
BlobInfo::Missing => ChunkRanges::empty(),
}
}
/// Ranges that are missing locally and need to be requested.
///
/// This will be empty for complete blobs, all for missing blobs, and
/// a set with possibly open last range for partial blobs.
pub fn missing_ranges(&self) -> ChunkRanges {
match self {
BlobInfo::Complete { .. } => ChunkRanges::empty(),
BlobInfo::Partial { valid_ranges, .. } => ChunkRanges::all().difference(valid_ranges),
BlobInfo::Missing => ChunkRanges::all(),
}
}
}
/// Progress updates for the get operation.
#[derive(Debug, Serialize, Deserialize)]
pub enum DownloadProgress {
/// Data was found locally.
FoundLocal {
/// child offset
child: u64,
/// The hash of the entry.
hash: Hash,
/// The size of the entry in bytes.
size: u64,
/// The ranges that are available locally.
valid_ranges: RangeSpec,
},
/// A new connection was established.
Connected,
/// An item was found with hash `hash`, from now on referred to via `id`.
Found {
/// A new unique id for this entry.
id: u64,
/// child offset
child: u64,
/// The hash of the entry.
hash: Hash,
/// The size of the entry in bytes.
size: u64,
},
/// An item was found with hash `hash`, from now on referred to via `id`.
FoundHashSeq {
/// The name of the entry.
hash: Hash,
/// Number of children in the collection, if known.
children: u64,
},
/// We got progress ingesting item `id`.
Progress {
/// The unique id of the entry.
id: u64,
/// The offset of the progress, in bytes.
offset: u64,
},
/// We are done with `id`, and the hash is `hash`.
Done {
/// The unique id of the entry.
id: u64,
},
/// We are done with the network part - all data is local.
NetworkDone {
/// The number of bytes written.
bytes_written: u64,
/// The number of bytes read.
bytes_read: u64,
/// The time it took to transfer the data.
elapsed: Duration,
},
/// The download part is done for this id, we are now exporting the data
/// to the specified out path.
Export {
/// Unique id of the entry.
id: u64,
/// The hash of the entry.
hash: Hash,
/// The size of the entry in bytes.
size: u64,
/// The path to the file where the data is exported.
target: PathBuf,
},
/// We have made progress exporting the data.
///
/// This is only sent for large blobs.
ExportProgress {
/// Unique id of the entry that is being exported.
id: u64,
/// The offset of the progress, in bytes.
offset: u64,
},
/// We got an error and need to abort.
Abort(RpcError),
/// We are done with the whole operation.
AllDone,
}
/// A slice writer that adds a synchronous progress callback
#[derive(Debug)]
struct ProgressSliceWriter2<W, F>(W, F);
impl<W: AsyncSliceWriter, F: Fn(u64, usize) -> io::Result<()> + 'static>
ProgressSliceWriter2<W, F>
{
/// Create a new `ProgressSliceWriter` from an inner writer and a progress callback
pub fn new(inner: W, on_write: F) -> Self {
Self(inner, on_write)
}
}
impl<W: AsyncSliceWriter + 'static, F: Fn(u64, usize) -> io::Result<()> + 'static> AsyncSliceWriter
for ProgressSliceWriter2<W, F>
{
type WriteBytesAtFuture<'a> = LocalBoxFuture<'a, io::Result<()>>;
fn write_bytes_at(&mut self, offset: u64, data: Bytes) -> Self::WriteBytesAtFuture<'_> {
// todo: get rid of the boxing
async move {
(self.1)(offset, data.len())?;
self.0.write_bytes_at(offset, data).await
}
.boxed_local()
}
type WriteAtFuture<'a> = W::WriteAtFuture<'a>;
fn write_at<'a>(&'a mut self, offset: u64, bytes: &'a [u8]) -> Self::WriteAtFuture<'a> {
self.0.write_at(offset, bytes)
}
type SyncFuture<'a> = W::SyncFuture<'a>;
fn sync(&mut self) -> Self::SyncFuture<'_> {
self.0.sync()
}
type SetLenFuture<'a> = W::SetLenFuture<'a>;
fn set_len(&mut self, size: u64) -> Self::SetLenFuture<'_> {
self.0.set_len(size)
}
}