iroh_docs/engine.rs
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
//! Handlers and actors to for live syncing replicas.
//!
//! [`crate::Replica`] is also called documents here.
use std::{
io,
path::PathBuf,
str::FromStr,
sync::{Arc, RwLock},
};
use anyhow::{bail, Context, Result};
use futures_lite::{Stream, StreamExt};
use iroh::{Endpoint, NodeAddr, PublicKey};
use iroh_blobs::{
downloader::Downloader, net_protocol::ProtectCb, store::EntryStatus,
util::local_pool::LocalPoolHandle, Hash,
};
use iroh_gossip::net::Gossip;
use serde::{Deserialize, Serialize};
use tokio::sync::{mpsc, oneshot};
use tokio_util::task::AbortOnDropHandle;
use tracing::{error, error_span, Instrument};
use self::live::{LiveActor, ToLiveActor};
pub use self::{
live::SyncEvent,
state::{Origin, SyncReason},
};
use crate::{
actor::SyncHandle, Author, AuthorId, ContentStatus, ContentStatusCallback, Entry, NamespaceId,
};
mod gossip;
mod live;
mod state;
/// Capacity of the channel for the [`ToLiveActor`] messages.
const ACTOR_CHANNEL_CAP: usize = 64;
/// Capacity for the channels for [`Engine::subscribe`].
const SUBSCRIBE_CHANNEL_CAP: usize = 256;
/// The sync engine coordinates actors that manage open documents, set-reconciliation syncs with
/// peers and a gossip swarm for each syncing document.
#[derive(derive_more::Debug)]
pub struct Engine<D> {
/// [`Endpoint`] used by the engine.
pub endpoint: Endpoint,
/// Handle to the actor thread.
pub sync: SyncHandle,
/// The persistent default author for this engine.
pub default_author: DefaultAuthor,
to_live_actor: mpsc::Sender<ToLiveActor>,
#[allow(dead_code)]
actor_handle: AbortOnDropHandle<()>,
#[debug("ContentStatusCallback")]
content_status_cb: ContentStatusCallback,
local_pool_handle: LocalPoolHandle,
blob_store: D,
}
impl<D: iroh_blobs::store::Store> Engine<D> {
/// Start the sync engine.
///
/// This will spawn two tokio tasks for the live sync coordination and gossip actors, and a
/// thread for the [`crate::actor::SyncHandle`].
pub async fn spawn(
endpoint: Endpoint,
gossip: Gossip,
replica_store: crate::store::Store,
bao_store: D,
downloader: Downloader,
default_author_storage: DefaultAuthorStorage,
local_pool_handle: LocalPoolHandle,
) -> anyhow::Result<Self> {
let (live_actor_tx, to_live_actor_recv) = mpsc::channel(ACTOR_CHANNEL_CAP);
let me = endpoint.node_id().fmt_short();
let content_status_cb = {
let bao_store = bao_store.clone();
Arc::new(move |hash| entry_to_content_status(bao_store.entry_status_sync(&hash)))
};
let sync = SyncHandle::spawn(replica_store, Some(content_status_cb.clone()), me.clone());
let actor = LiveActor::new(
sync.clone(),
endpoint.clone(),
gossip.clone(),
bao_store.clone(),
downloader,
to_live_actor_recv,
live_actor_tx.clone(),
);
let actor_handle = tokio::task::spawn(
async move {
if let Err(err) = actor.run().await {
error!("sync actor failed: {err:?}");
}
}
.instrument(error_span!("sync", %me)),
);
let default_author = match DefaultAuthor::load(default_author_storage, &sync).await {
Ok(author) => author,
Err(err) => {
// If loading the default author failed, make sure to shutdown the sync actor before
// returning.
let _store = sync.shutdown().await.ok();
return Err(err);
}
};
Ok(Self {
endpoint,
sync,
to_live_actor: live_actor_tx,
actor_handle: AbortOnDropHandle::new(actor_handle),
content_status_cb,
default_author,
local_pool_handle,
blob_store: bao_store,
})
}
/// Return a callback that can be added to blobs to protect the content of
/// all docs from garbage collection.
pub fn protect_cb(&self) -> ProtectCb {
let sync = self.sync.clone();
Box::new(move |live| {
let sync = sync.clone();
Box::pin(async move {
let doc_hashes = match sync.content_hashes().await {
Ok(hashes) => hashes,
Err(err) => {
tracing::warn!("Error getting doc hashes: {}", err);
return;
}
};
for hash in doc_hashes {
match hash {
Ok(hash) => {
live.insert(hash);
}
Err(err) => {
tracing::error!("Error getting doc hash: {}", err);
}
}
}
})
})
}
/// Get the blob store.
pub fn blob_store(&self) -> &D {
&self.blob_store
}
/// Start to sync a document.
///
/// If `peers` is non-empty, it will both do an initial set-reconciliation sync with each peer,
/// and join an iroh-gossip swarm with these peers to receive and broadcast document updates.
pub async fn start_sync(&self, namespace: NamespaceId, peers: Vec<NodeAddr>) -> Result<()> {
let (reply, reply_rx) = oneshot::channel();
self.to_live_actor
.send(ToLiveActor::StartSync {
namespace,
peers,
reply,
})
.await?;
reply_rx.await??;
Ok(())
}
/// Stop the live sync for a document and leave the gossip swarm.
///
/// If `kill_subscribers` is true, all existing event subscribers will be dropped. This means
/// they will receive `None` and no further events in case of rejoining the document.
pub async fn leave(&self, namespace: NamespaceId, kill_subscribers: bool) -> Result<()> {
let (reply, reply_rx) = oneshot::channel();
self.to_live_actor
.send(ToLiveActor::Leave {
namespace,
kill_subscribers,
reply,
})
.await?;
reply_rx.await??;
Ok(())
}
/// Subscribe to replica and sync progress events.
pub async fn subscribe(
&self,
namespace: NamespaceId,
) -> Result<impl Stream<Item = Result<LiveEvent>> + Unpin + 'static> {
let content_status_cb = self.content_status_cb.clone();
// Create a future that sends channel senders to the respective actors.
// We clone `self` so that the future does not capture any lifetimes.
let this = self;
// Subscribe to insert events from the replica.
let a = {
let (s, r) = async_channel::bounded(SUBSCRIBE_CHANNEL_CAP);
this.sync.subscribe(namespace, s).await?;
Box::pin(r).map(move |ev| LiveEvent::from_replica_event(ev, &content_status_cb))
};
// Subscribe to events from the [`live::Actor`].
let b = {
let (s, r) = async_channel::bounded(SUBSCRIBE_CHANNEL_CAP);
let r = Box::pin(r);
let (reply, reply_rx) = oneshot::channel();
this.to_live_actor
.send(ToLiveActor::Subscribe {
namespace,
sender: s,
reply,
})
.await?;
reply_rx.await??;
r.map(|event| Ok(LiveEvent::from(event)))
};
Ok(a.or(b))
}
/// Handle an incoming iroh-docs connection.
pub async fn handle_connection(&self, conn: iroh::endpoint::Connection) -> anyhow::Result<()> {
self.to_live_actor
.send(ToLiveActor::HandleConnection { conn })
.await?;
Ok(())
}
/// Shutdown the engine.
pub async fn shutdown(&self) -> Result<()> {
let (reply, reply_rx) = oneshot::channel();
self.to_live_actor
.send(ToLiveActor::Shutdown { reply })
.await?;
reply_rx.await?;
Ok(())
}
/// Returns the stored `LocalPoolHandle`.
pub fn local_pool_handle(&self) -> &LocalPoolHandle {
&self.local_pool_handle
}
}
/// Converts an [`EntryStatus`] into a ['ContentStatus'].
pub fn entry_to_content_status(entry: io::Result<EntryStatus>) -> ContentStatus {
match entry {
Ok(EntryStatus::Complete) => ContentStatus::Complete,
Ok(EntryStatus::Partial) => ContentStatus::Incomplete,
Ok(EntryStatus::NotFound) => ContentStatus::Missing,
Err(cause) => {
tracing::warn!("Error while checking entry status: {cause:?}");
ContentStatus::Missing
}
}
}
/// Events informing about actions of the live sync progress.
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, strum::Display)]
pub enum LiveEvent {
/// A local insertion.
InsertLocal {
/// The inserted entry.
entry: Entry,
},
/// Received a remote insert.
InsertRemote {
/// The peer that sent us the entry.
from: PublicKey,
/// The inserted entry.
entry: Entry,
/// If the content is available at the local node
content_status: ContentStatus,
},
/// The content of an entry was downloaded and is now available at the local node
ContentReady {
/// The content hash of the newly available entry content
hash: Hash,
},
/// All pending content is now ready.
///
/// This event signals that all queued content downloads from the last sync run have either
/// completed or failed.
///
/// It will only be emitted after a [`Self::SyncFinished`] event, never before.
///
/// Receiving this event does not guarantee that all content in the document is available. If
/// blobs failed to download, this event will still be emitted after all operations completed.
PendingContentReady,
/// We have a new neighbor in the swarm.
NeighborUp(PublicKey),
/// We lost a neighbor in the swarm.
NeighborDown(PublicKey),
/// A set-reconciliation sync finished.
SyncFinished(SyncEvent),
}
impl From<live::Event> for LiveEvent {
fn from(ev: live::Event) -> Self {
match ev {
live::Event::ContentReady { hash } => Self::ContentReady { hash },
live::Event::NeighborUp(peer) => Self::NeighborUp(peer),
live::Event::NeighborDown(peer) => Self::NeighborDown(peer),
live::Event::SyncFinished(ev) => Self::SyncFinished(ev),
live::Event::PendingContentReady => Self::PendingContentReady,
}
}
}
impl LiveEvent {
fn from_replica_event(
ev: crate::Event,
content_status_cb: &ContentStatusCallback,
) -> Result<Self> {
Ok(match ev {
crate::Event::LocalInsert { entry, .. } => Self::InsertLocal {
entry: entry.into(),
},
crate::Event::RemoteInsert { entry, from, .. } => Self::InsertRemote {
content_status: content_status_cb(entry.content_hash()),
entry: entry.into(),
from: PublicKey::from_bytes(&from)?,
},
})
}
}
/// Where to persist the default author.
///
/// If set to `Mem`, a new author will be created in the docs store before spawning the sync
/// engine. Changing the default author will not be persisted.
///
/// If set to `Persistent`, the default author will be loaded from and persisted to the specified
/// path (as hex encoded string of the author's public key).
#[derive(Debug)]
pub enum DefaultAuthorStorage {
/// Memory storage.
Mem,
/// File based persistent storage.
Persistent(PathBuf),
}
impl DefaultAuthorStorage {
/// Load the default author from the storage.
///
/// Will create and save a new author if the storage is empty.
///
/// Returns an error if the author can't be parsed or if the uathor does not exist in the docs
/// store.
pub async fn load(&self, docs_store: &SyncHandle) -> anyhow::Result<AuthorId> {
match self {
Self::Mem => {
let author = Author::new(&mut rand::thread_rng());
let author_id = author.id();
docs_store.import_author(author).await?;
Ok(author_id)
}
Self::Persistent(ref path) => {
if path.exists() {
let data = tokio::fs::read_to_string(path).await.with_context(|| {
format!(
"Failed to read the default author file at `{}`",
path.to_string_lossy()
)
})?;
let author_id = AuthorId::from_str(&data).with_context(|| {
format!(
"Failed to parse the default author from `{}`",
path.to_string_lossy()
)
})?;
if docs_store.export_author(author_id).await?.is_none() {
bail!("The default author is missing from the docs store. To recover, delete the file `{}`. Then iroh will create a new default author.", path.to_string_lossy())
}
Ok(author_id)
} else {
let author = Author::new(&mut rand::thread_rng());
let author_id = author.id();
docs_store.import_author(author).await?;
// Make sure to write the default author to the store
// *before* we write the default author ID file.
// Otherwise the default author ID file is effectively a dangling reference.
docs_store.flush_store().await?;
self.persist(author_id).await?;
Ok(author_id)
}
}
}
}
/// Save a new default author.
pub async fn persist(&self, author_id: AuthorId) -> anyhow::Result<()> {
match self {
Self::Mem => {
// persistence is not possible for the mem storage so this is a noop.
}
Self::Persistent(ref path) => {
tokio::fs::write(path, author_id.to_string())
.await
.with_context(|| {
format!(
"Failed to write the default author to `{}`",
path.to_string_lossy()
)
})?;
}
}
Ok(())
}
}
/// Persistent default author for a docs engine.
#[derive(Debug)]
pub struct DefaultAuthor {
value: RwLock<AuthorId>,
storage: DefaultAuthorStorage,
}
impl DefaultAuthor {
/// Load the default author from storage.
///
/// If the storage is empty creates a new author and persists it.
pub async fn load(storage: DefaultAuthorStorage, docs_store: &SyncHandle) -> Result<Self> {
let value = storage.load(docs_store).await?;
Ok(Self {
value: RwLock::new(value),
storage,
})
}
/// Get the current default author.
pub fn get(&self) -> AuthorId {
*self.value.read().unwrap()
}
/// Set the default author.
pub async fn set(&self, author_id: AuthorId, docs_store: &SyncHandle) -> Result<()> {
if docs_store.export_author(author_id).await?.is_none() {
bail!("The author does not exist");
}
self.storage.persist(author_id).await?;
*self.value.write().unwrap() = author_id;
Ok(())
}
}