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
//! Adaptation of `iroh-blobs` as an `iroh` protocol.
// TODO: reduce API surface and add documentation
#![allow(missing_docs)]
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Debug,
ops::DerefMut,
sync::{Arc, OnceLock},
};
use anyhow::{anyhow, bail, Result};
use futures_lite::future::Boxed as BoxedFuture;
use futures_util::future::BoxFuture;
use iroh::{endpoint::Connecting, protocol::ProtocolHandler, Endpoint, NodeAddr};
use iroh_base::hash::{BlobFormat, Hash};
use serde::{Deserialize, Serialize};
use tracing::{debug, warn};
use crate::{
downloader::{DownloadRequest, Downloader},
get::{
db::{DownloadProgress, GetState},
Stats,
},
provider::EventSender,
store::GcConfig,
util::{
local_pool::{self, LocalPoolHandle},
progress::{AsyncChannelProgressSender, ProgressSender},
SetTagOption,
},
HashAndFormat, TempTag,
};
/// A callback that blobs can ask about a set of hashes that should not be garbage collected.
pub type ProtectCb = Box<dyn Fn(&mut BTreeSet<Hash>) -> BoxFuture<()> + Send + Sync>;
/// The state of the gc loop.
#[derive(derive_more::Debug)]
enum GcState {
// Gc loop is not yet running. Other protocols can add protect callbacks
Initial(#[debug(skip)] Vec<ProtectCb>),
// Gc loop is running. No more protect callbacks can be added.
Started(#[allow(dead_code)] Option<local_pool::Run<()>>),
}
impl Default for GcState {
fn default() -> Self {
Self::Initial(Vec::new())
}
}
#[derive(Debug)]
pub struct Blobs<S> {
rt: LocalPoolHandle,
store: S,
events: EventSender,
downloader: Downloader,
batches: tokio::sync::Mutex<BlobBatches>,
endpoint: Endpoint,
gc_state: Arc<std::sync::Mutex<GcState>>,
#[cfg(feature = "rpc")]
pub(crate) rpc_handler: Arc<OnceLock<crate::rpc::RpcHandler>>,
}
/// Name used for logging when new node addresses are added from gossip.
const BLOB_DOWNLOAD_SOURCE_NAME: &str = "blob_download";
/// Keeps track of all the currently active batch operations of the blobs api.
#[derive(Debug, Default)]
pub(crate) struct BlobBatches {
/// Currently active batches
batches: BTreeMap<BatchId, BlobBatch>,
/// Used to generate new batch ids.
max: u64,
}
/// A single batch of blob operations
#[derive(Debug, Default)]
struct BlobBatch {
/// The tags in this batch.
tags: BTreeMap<HashAndFormat, Vec<TempTag>>,
}
impl BlobBatches {
/// Create a new unique batch id.
pub fn create(&mut self) -> BatchId {
let id = self.max;
self.max += 1;
BatchId(id)
}
/// Store a temp tag in a batch identified by a batch id.
pub fn store(&mut self, batch: BatchId, tt: TempTag) {
let entry = self.batches.entry(batch).or_default();
entry.tags.entry(tt.hash_and_format()).or_default().push(tt);
}
/// Remove a tag from a batch.
pub fn remove_one(&mut self, batch: BatchId, content: &HashAndFormat) -> Result<()> {
if let Some(batch) = self.batches.get_mut(&batch) {
if let Some(tags) = batch.tags.get_mut(content) {
tags.pop();
if tags.is_empty() {
batch.tags.remove(content);
}
return Ok(());
}
}
// this can happen if we try to upgrade a tag from an expired batch
anyhow::bail!("tag not found in batch");
}
/// Remove an entire batch.
pub fn remove(&mut self, batch: BatchId) {
self.batches.remove(&batch);
}
}
/// Builder for the Blobs protocol handler
#[derive(Debug)]
pub struct Builder<S> {
store: S,
events: Option<EventSender>,
gc_config: Option<crate::store::GcConfig>,
}
impl<S: crate::store::Store> Builder<S> {
/// Set the event sender for the blobs protocol.
pub fn events(mut self, value: EventSender) -> Self {
self.events = Some(value);
self
}
pub fn gc_config(mut self, value: crate::store::GcConfig) -> Self {
self.gc_config = Some(value);
self
}
/// Build the Blobs protocol handler.
/// You need to provide a local pool handle and an endpoint.
pub fn build(self, rt: &LocalPoolHandle, endpoint: &Endpoint) -> Arc<Blobs<S>> {
let downloader = Downloader::new(self.store.clone(), endpoint.clone(), rt.clone());
Arc::new(Blobs::new(
self.store,
rt.clone(),
self.events.unwrap_or_default(),
downloader,
endpoint.clone(),
))
}
}
impl<S> Blobs<S> {
/// Create a new Blobs protocol handler builder, given a store.
pub fn builder(store: S) -> Builder<S> {
Builder {
store,
events: None,
gc_config: None,
}
}
}
impl Blobs<crate::store::mem::Store> {
/// Create a new memory-backed Blobs protocol handler.
pub fn memory() -> Builder<crate::store::mem::Store> {
Self::builder(crate::store::mem::Store::new())
}
}
impl Blobs<crate::store::fs::Store> {
/// Load a persistent Blobs protocol handler from a path.
pub async fn persistent(
path: impl AsRef<std::path::Path>,
) -> anyhow::Result<Builder<crate::store::fs::Store>> {
Ok(Self::builder(crate::store::fs::Store::load(path).await?))
}
}
impl<S: crate::store::Store> Blobs<S> {
pub fn new(
store: S,
rt: LocalPoolHandle,
events: EventSender,
downloader: Downloader,
endpoint: Endpoint,
) -> Self {
Self {
rt,
store,
events,
downloader,
endpoint,
batches: Default::default(),
gc_state: Default::default(),
#[cfg(feature = "rpc")]
rpc_handler: Arc::new(OnceLock::new()),
}
}
pub fn store(&self) -> &S {
&self.store
}
pub fn rt(&self) -> &LocalPoolHandle {
&self.rt
}
pub fn downloader(&self) -> &Downloader {
&self.downloader
}
pub fn endpoint(&self) -> &Endpoint {
&self.endpoint
}
/// Add a callback that will be called before the garbage collector runs.
///
/// This can only be called before the garbage collector has started, otherwise it will return an error.
pub fn add_protected(&self, cb: ProtectCb) -> Result<()> {
let mut state = self.gc_state.lock().unwrap();
match &mut *state {
GcState::Initial(cbs) => {
cbs.push(cb);
}
GcState::Started(_) => {
anyhow::bail!("cannot add protected blobs after gc has started");
}
}
Ok(())
}
/// Start garbage collection with the given settings.
pub fn start_gc(&self, config: GcConfig) -> Result<()> {
let mut state = self.gc_state.lock().unwrap();
let protected = match state.deref_mut() {
GcState::Initial(items) => std::mem::take(items),
GcState::Started(_) => bail!("gc already started"),
};
let protected = Arc::new(protected);
let protected_cb = move || {
let protected = protected.clone();
async move {
let mut set = BTreeSet::new();
for cb in protected.iter() {
cb(&mut set).await;
}
set
}
};
let store = self.store.clone();
let run = self
.rt
.spawn(move || async move { store.gc_run(config, protected_cb).await });
*state = GcState::Started(Some(run));
Ok(())
}
pub(crate) async fn batches(&self) -> tokio::sync::MutexGuard<'_, BlobBatches> {
self.batches.lock().await
}
pub(crate) async fn download(
&self,
endpoint: Endpoint,
req: BlobDownloadRequest,
progress: AsyncChannelProgressSender<DownloadProgress>,
) -> Result<()> {
let BlobDownloadRequest {
hash,
format,
nodes,
tag,
mode,
} = req;
let hash_and_format = HashAndFormat { hash, format };
let temp_tag = self.store.temp_tag(hash_and_format);
let stats = match mode {
DownloadMode::Queued => {
self.download_queued(endpoint, hash_and_format, nodes, progress.clone())
.await?
}
DownloadMode::Direct => {
self.download_direct_from_nodes(endpoint, hash_and_format, nodes, progress.clone())
.await?
}
};
progress.send(DownloadProgress::AllDone(stats)).await.ok();
match tag {
SetTagOption::Named(tag) => {
self.store.set_tag(tag, Some(hash_and_format)).await?;
}
SetTagOption::Auto => {
self.store.create_tag(hash_and_format).await?;
}
}
drop(temp_tag);
Ok(())
}
async fn download_queued(
&self,
endpoint: Endpoint,
hash_and_format: HashAndFormat,
nodes: Vec<NodeAddr>,
progress: AsyncChannelProgressSender<DownloadProgress>,
) -> Result<Stats> {
let mut node_ids = Vec::with_capacity(nodes.len());
let mut any_added = false;
for node in nodes {
node_ids.push(node.node_id);
if !node.info.is_empty() {
endpoint.add_node_addr_with_source(node, BLOB_DOWNLOAD_SOURCE_NAME)?;
any_added = true;
}
}
let can_download = !node_ids.is_empty() && (any_added || endpoint.discovery().is_some());
anyhow::ensure!(can_download, "no way to reach a node for download");
let req = DownloadRequest::new(hash_and_format, node_ids).progress_sender(progress);
let handle = self.downloader.queue(req).await;
let stats = handle.await?;
Ok(stats)
}
#[tracing::instrument("download_direct", skip_all, fields(hash=%hash_and_format.hash.fmt_short()))]
async fn download_direct_from_nodes(
&self,
endpoint: Endpoint,
hash_and_format: HashAndFormat,
nodes: Vec<NodeAddr>,
progress: AsyncChannelProgressSender<DownloadProgress>,
) -> Result<Stats> {
let mut last_err = None;
let mut remaining_nodes = nodes.len();
let mut nodes_iter = nodes.into_iter();
'outer: loop {
match crate::get::db::get_to_db_in_steps(
self.store.clone(),
hash_and_format,
progress.clone(),
)
.await?
{
GetState::Complete(stats) => return Ok(stats),
GetState::NeedsConn(needs_conn) => {
let (conn, node_id) = 'inner: loop {
match nodes_iter.next() {
None => break 'outer,
Some(node) => {
remaining_nodes -= 1;
let node_id = node.node_id;
if node_id == endpoint.node_id() {
debug!(
?remaining_nodes,
"skip node {} (it is the node id of ourselves)",
node_id.fmt_short()
);
continue 'inner;
}
match endpoint.connect(node, crate::protocol::ALPN).await {
Ok(conn) => break 'inner (conn, node_id),
Err(err) => {
debug!(
?remaining_nodes,
"failed to connect to {}: {err}",
node_id.fmt_short()
);
continue 'inner;
}
}
}
}
};
match needs_conn.proceed(conn).await {
Ok(stats) => return Ok(stats),
Err(err) => {
warn!(
?remaining_nodes,
"failed to download from {}: {err}",
node_id.fmt_short()
);
last_err = Some(err);
}
}
}
}
}
match last_err {
Some(err) => Err(err.into()),
None => Err(anyhow!("No nodes to download from provided")),
}
}
}
// trait BlobsInner: Debug + Send + Sync + 'static {
// fn shutdown(self: Arc<Self>) -> BoxedFuture<()>;
// fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>>;
// fn client(self: Arc<Self>) -> MemClient;
// fn local_pool_handle(&self) -> &LocalPoolHandle;
// fn downloader(&self) -> &Downloader;
// }
// #[derive(Debug)]
// struct Blobs2 {
// inner: Arc<dyn BlobsInner>,
// }
// impl Blobs2 {
// fn client(&self) -> MemClient {
// self.inner.clone().client()
// }
// fn local_pool_handle(&self) -> &LocalPoolHandle {
// self.inner.local_pool_handle()
// }
// fn downloader(&self) -> &Downloader {
// self.inner.downloader()
// }
// }
// impl<S: crate::store::Store> BlobsInner for Blobs<S> {
// fn shutdown(self: Arc<Self>) -> BoxedFuture<()> {
// ProtocolHandler::shutdown(self)
// }
// fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>> {
// ProtocolHandler::accept(self, conn)
// }
// fn client(self: Arc<Self>) -> MemClient {
// Blobs::client(self)
// }
// fn local_pool_handle(&self) -> &LocalPoolHandle {
// self.rt()
// }
// fn downloader(&self) -> &Downloader {
// self.downloader()
// }
// }
// impl ProtocolHandler for Blobs2 {
// fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>> {
// self.inner.clone().accept(conn)
// }
// fn shutdown(self: Arc<Self>) -> BoxedFuture<()> {
// self.inner.clone().shutdown()
// }
// }
impl<S: crate::store::Store> ProtocolHandler for Blobs<S> {
fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>> {
Box::pin(async move {
crate::provider::handle_connection(
conn.await?,
self.store.clone(),
self.events.clone(),
self.rt.clone(),
)
.await;
Ok(())
})
}
fn shutdown(self: Arc<Self>) -> BoxedFuture<()> {
Box::pin(async move {
self.store.shutdown().await;
})
}
}
/// A request to the node to download and share the data specified by the hash.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlobDownloadRequest {
/// This mandatory field contains the hash of the data to download and share.
pub hash: Hash,
/// If the format is [`BlobFormat::HashSeq`], all children are downloaded and shared as
/// well.
pub format: BlobFormat,
/// This mandatory field specifies the nodes to download the data from.
///
/// If set to more than a single node, they will all be tried. If `mode` is set to
/// [`DownloadMode::Direct`], they will be tried sequentially until a download succeeds.
/// If `mode` is set to [`DownloadMode::Queued`], the nodes may be dialed in parallel,
/// if the concurrency limits permit.
pub nodes: Vec<NodeAddr>,
/// Optional tag to tag the data with.
pub tag: SetTagOption,
/// Whether to directly start the download or add it to the download queue.
pub mode: DownloadMode,
}
/// Set the mode for whether to directly start the download or add it to the download queue.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DownloadMode {
/// Start the download right away.
///
/// No concurrency limits or queuing will be applied. It is up to the user to manage download
/// concurrency.
Direct,
/// Queue the download.
///
/// The download queue will be processed in-order, while respecting the downloader concurrency limits.
Queued,
}
/// Newtype for a batch id
#[derive(Debug, PartialEq, Eq, PartialOrd, Serialize, Deserialize, Ord, Clone, Copy, Hash)]
pub struct BatchId(pub u64);