use std::sync::Arc;
use std::time::{Duration, Instant};
use std::collections::{HashMap, VecDeque};
use super::entry::Point;
use super::log::Log;
use super::message::{Frames, LeaseAsk, LeaseHolder, Message, timed_out};
use super::node::State;
use super::serve::Out;
use super::witness::{Holder, TTL};
use crate::store::WeakStore;
use crate::{Error, ErrorKind, Result, Store};
const RENEW: Duration = Duration::from_secs(1);
pub(crate) const MARGIN: Duration = Duration::from_millis(1500);
pub(crate) fn start(state: &Arc<State>, store: &Store, witness: String) {
let (state, store) = (Arc::clone(state), store.downgrade());
state.clone().spawn("tinystore node: judge", move || {
let mut said = None;
while !state.stopping() {
if let Err(error) = judged(&state, &store, &witness)
&& said.as_deref() != Some(error.to_string().as_str())
&& !state.stopping()
{
tracing::warn!(target: "tinystore", witness = %witness, %error, "a node's link to its witness");
said = Some(error.to_string());
}
state.pause(RENEW);
}
});
}
fn judged(state: &Arc<State>, store: &WeakStore, witness: &str) -> Result<()> {
let (mut reading, writing) = super::follow::dial(state, witness)?;
reading.set_timeout(Duration::from_millis(100))?;
writing.set_timeout(Duration::from_secs(5))?;
let out = Out::new(writing);
out.send(&state.hello(state.address()))?;
let mut frames = Frames::default();
let (mut welcomed, mut turn, mut asks) = (false, None::<Instant>, Asks::default());
loop {
if state.stopping() {
return Ok(());
}
while let Some(message) = frames.next()? {
match message {
Message::Welcome { .. } if !welcomed => {
welcomed = true;
frames.unpack_from_here()?;
out.pack()?;
}
Message::Refused { why } => return Err(Error::new(ErrorKind::Invalid, format!("refused: {why}"))),
Message::Leased { holders } => {
let Some(store) = store.upgrade() else {
return Ok(());
};
let Some(sent) = asks.unanswered.pop_front() else {
return Err(Error::invalid("a witness answered an ask this node did not make"));
};
asks.answered(state, &store, holders, sent);
}
_ => {}
}
}
if welcomed && turn.is_none_or(|turn| turn.elapsed() >= RENEW) {
let now = Instant::now();
turn = Some(now);
let asked = state.judged_files();
tracing::debug!(target: "tinystore", asks = asked.len(), "a node asks its witness for its leases");
if !asked.is_empty() {
out.send(&Message::Lease { node: state.id.clone(), address: state.address(), asks: asked })?;
asks.unanswered.push_back(now);
}
}
match frames.fill(&mut reading) {
Ok(true) => {}
Ok(false) => return Err(Error::new(ErrorKind::Unavailable, "the witness closed the link")),
Err(error) if timed_out(&error) => {}
Err(error) => return Err(Error::io("a link to the witness", error)),
}
}
}
#[derive(Default)]
struct Asks {
unanswered: VecDeque<Instant>,
failing: HashMap<String, String>,
}
impl Asks {
fn answered(&mut self, state: &Arc<State>, store: &Store, holders: Vec<LeaseHolder>, sent: Instant) {
for held in holders {
let file = held.file;
match leased(state, store, &file, &held.holder, sent) {
Err(error) if self.failing.get(&file) != Some(&error.to_string()) => {
tracing::warn!(target: "tinystore", %file, %error, "a node could not act on its lease");
self.failing.insert(file, error.to_string());
}
Err(_) => {}
Ok(()) => {
if self.failing.remove(&file).is_some() {
tracing::info!(target: "tinystore", %file, "a node acts on its lease again");
}
}
}
}
}
}
fn leased(state: &Arc<State>, store: &Store, file: &str, holder: &Holder, sent: Instant) -> Result<()> {
let until = sent + TTL - MARGIN;
if holder.node == state.id {
if let Some(copy) = state.copy(file).filter(|copy| holder.epoch > copy.point().epoch) {
state.granted(file, until);
state.take_over_at(store, file, copy, holder.epoch)?;
tracing::info!(
target: "tinystore",
file,
epoch = holder.epoch,
"a copy took a file over, its lease granted",
);
state.claim_taken(file, holder.epoch);
}
state.lease_until(file, until);
return Ok(());
}
if holder.node.is_empty() || state.copy(file).is_some() || super::copy::is_copy(&state.log_dir(file)) {
return Ok(());
}
if state.epoch_held(file).is_none_or(|ours| holder.epoch <= ours) {
return Ok(());
}
let dir = state.log_dir(file);
if Log::fenced_in(&dir)?.is_none_or(|fenced| fenced < holder.epoch) {
state.fence(file, holder.epoch)?;
}
let ours = match state.point_of(file) {
Some(point) => point,
None => Log::last_in(&dir)?
.or(super::promote::promoted_at(&dir)?)
.ok_or_else(|| Error::not_found(format!("{file}: its log")))?,
};
super::handover::let_go(state, store, file, &holder.address, ours)
}
impl State {
pub(crate) fn judged_files(&self) -> Vec<LeaseAsk> {
let Ok(logged) = self.files_logged_now() else {
return Vec::new();
};
let mut asks = Vec::new();
for file in logged {
if super::NODE_LOCAL.contains(&file.as_str()) || file == super::catalog::FILE {
continue;
}
let (holding, at) = match self.copy(&file) {
Some(copy) => (false, copy.point()),
None if super::copy::is_copy(&self.log_dir(&file)) => continue,
None => match self.epoch_held(&file) {
Some(epoch) => (true, Point { epoch, ..Point::default() }),
None => continue,
},
};
let synchronous = if holding { self.options.synchronous_with.clone() } else { Vec::new() };
asks.push(LeaseAsk { file, epoch: at.epoch, position: at.sequence, holding, synchronous });
}
asks
}
}