iroh_docs/engine/state.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
use std::{
collections::BTreeMap,
time::{Instant, SystemTime},
};
use anyhow::Result;
use iroh::NodeId;
use serde::{Deserialize, Serialize};
use tracing::{debug, warn};
use crate::{
net::{AbortReason, AcceptOutcome, SyncFinished},
NamespaceId,
};
/// Why we started a sync request
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Copy)]
pub enum SyncReason {
/// Direct join request via API
DirectJoin,
/// Peer showed up as new neighbor in the gossip swarm
NewNeighbor,
/// We synced after receiving a sync report that indicated news for us
SyncReport,
/// We received a sync report while a sync was running, so run again afterwars
Resync,
}
/// Why we performed a sync exchange
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub enum Origin {
/// We initiated the exchange
Connect(SyncReason),
/// A node connected to us and we accepted the exchange
Accept,
}
/// The state we're in for a node and a namespace
#[derive(Debug, Clone)]
pub enum SyncState {
Idle,
Running { start: SystemTime, origin: Origin },
}
impl Default for SyncState {
fn default() -> Self {
Self::Idle
}
}
/// Contains an entry for each active (syncing) namespace, and in there an entry for each node we
/// synced with.
#[derive(Default)]
pub struct NamespaceStates(BTreeMap<NamespaceId, NamespaceState>);
#[derive(Default)]
struct NamespaceState {
nodes: BTreeMap<NodeId, PeerState>,
may_emit_ready: bool,
}
impl NamespaceStates {
/// Are we syncing this namespace?
pub fn is_syncing(&self, namespace: &NamespaceId) -> bool {
self.0.contains_key(namespace)
}
/// Insert a namespace into the set of syncing namespaces.
pub fn insert(&mut self, namespace: NamespaceId) {
self.0.entry(namespace).or_default();
}
/// Start a sync request.
///
/// Returns true if the request should be performed, and false if it should be aborted.
pub fn start_connect(
&mut self,
namespace: &NamespaceId,
node: NodeId,
reason: SyncReason,
) -> bool {
match self.entry(namespace, node) {
None => {
debug!("abort connect: namespace is not in sync set");
false
}
Some(state) => state.start_connect(reason),
}
}
/// Accept a sync request.
///
/// Returns the [`AcceptOutcome`] to be performed.
pub fn accept_request(
&mut self,
me: &NodeId,
namespace: &NamespaceId,
node: NodeId,
) -> AcceptOutcome {
let Some(state) = self.entry(namespace, node) else {
return AcceptOutcome::Reject(AbortReason::NotFound);
};
state.accept_request(me, &node)
}
/// Insert a finished sync operation into the state.
///
/// Returns the time when the operation was started, and a `bool` that is true if another sync
/// request should be triggered right afterwards.
///
/// Returns `None` if the namespace is not syncing or the sync state doesn't expect a finish
/// event.
pub fn finish(
&mut self,
namespace: &NamespaceId,
node: NodeId,
origin: &Origin,
result: Result<SyncFinished>,
) -> Option<(SystemTime, bool)> {
let state = self.entry(namespace, node)?;
state.finish(origin, result)
}
/// Set whether a [`super::live::Event::PendingContentReady`] may be emitted once the pending queue
/// becomes empty.
///
/// This should be set to `true` if there are pending content hashes after a sync finished, and
/// to `false` whenever a `PendingContentReady` was emitted.
pub fn set_may_emit_ready(&mut self, namespace: &NamespaceId, value: bool) -> Option<()> {
let state = self.0.get_mut(namespace)?;
state.may_emit_ready = value;
Some(())
}
/// Returns whether a [`super::live::Event::PendingContentReady`] event may be emitted once the
/// pending queue becomes empty.
///
/// If this returns `false`, an event should not be emitted even if the queue becomes empty,
/// because a currently running sync did not yet terminate. Once it terminates, the event will
/// be emitted from the handler for finished syncs.
pub fn may_emit_ready(&mut self, namespace: &NamespaceId) -> Option<bool> {
let state = self.0.get_mut(namespace)?;
if state.may_emit_ready {
state.may_emit_ready = false;
Some(true)
} else {
Some(false)
}
}
/// Remove a namespace from the set of syncing namespaces.
pub fn remove(&mut self, namespace: &NamespaceId) -> bool {
self.0.remove(namespace).is_some()
}
/// Get the [`PeerState`] for a namespace and node.
/// If the namespace is syncing and the node so far unknown, initialize and return a default [`PeerState`].
/// If the namespace is not syncing return None.
fn entry(&mut self, namespace: &NamespaceId, node: NodeId) -> Option<&mut PeerState> {
self.0
.get_mut(namespace)
.map(|n| n.nodes.entry(node).or_default())
}
}
/// State of a node with regard to a namespace.
#[derive(Default)]
struct PeerState {
state: SyncState,
resync_requested: bool,
last_sync: Option<(Instant, Result<SyncFinished>)>,
}
impl PeerState {
fn finish(
&mut self,
origin: &Origin,
result: Result<SyncFinished>,
) -> Option<(SystemTime, bool)> {
let start = match &self.state {
SyncState::Running {
start,
origin: origin2,
} => {
if origin2 != origin {
warn!(actual = ?origin, expected = ?origin2, "finished sync origin does not match state")
}
Some(*start)
}
SyncState::Idle => {
warn!("sync state finish called but not in running state");
None
}
};
self.last_sync = Some((Instant::now(), result));
self.state = SyncState::Idle;
start.map(|s| (s, self.resync_requested))
}
fn start_connect(&mut self, reason: SyncReason) -> bool {
debug!(?reason, "start connect");
match self.state {
// never run two syncs at the same time
SyncState::Running { .. } => {
debug!("abort connect: sync already running");
if matches!(reason, SyncReason::SyncReport) {
debug!("resync queued");
self.resync_requested = true;
}
false
}
SyncState::Idle => {
self.set_sync_running(Origin::Connect(reason));
true
}
}
}
fn accept_request(&mut self, me: &NodeId, node: &NodeId) -> AcceptOutcome {
let outcome = match &self.state {
SyncState::Idle => AcceptOutcome::Allow,
SyncState::Running { origin, .. } => match origin {
Origin::Accept => AcceptOutcome::Reject(AbortReason::AlreadySyncing),
// Incoming sync request while we are dialing ourselves.
// In this case, compare the binary representations of our and the other node's id
// to deterministically decide which of the two concurrent connections will succeed.
Origin::Connect(_reason) => match expected_sync_direction(me, node) {
SyncDirection::Accept => AcceptOutcome::Allow,
SyncDirection::Connect => AcceptOutcome::Reject(AbortReason::AlreadySyncing),
},
},
};
if let AcceptOutcome::Allow = outcome {
self.set_sync_running(Origin::Accept);
}
outcome
}
fn set_sync_running(&mut self, origin: Origin) {
self.state = SyncState::Running {
origin,
start: SystemTime::now(),
};
self.resync_requested = false;
}
}
#[derive(Debug)]
enum SyncDirection {
Accept,
Connect,
}
fn expected_sync_direction(self_node_id: &NodeId, other_node_id: &NodeId) -> SyncDirection {
if self_node_id.as_bytes() > other_node_id.as_bytes() {
SyncDirection::Accept
} else {
SyncDirection::Connect
}
}