use std::collections::HashMap;
use std::net::{SocketAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex, Weak};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};
use super::copy::Copy;
use super::follow::{Direct, Upstream};
use super::links::Follower;
use super::log::{Acks, Bounds, Log, Notify};
use super::{DIR, Node};
use crate::sqlite::Switch;
use crate::store::WeakStore;
use crate::sync::lock;
use crate::{Error, Result, Store};
mod claims;
mod copies;
mod logs;
mod routes;
pub(crate) use claims::Claimed;
use copies::Verifying;
pub(crate) use logs::files_logged_in;
use routes::Routes;
pub(crate) const READY_WAIT: Duration = Duration::from_secs(30);
pub(crate) struct State {
pub(crate) dir: PathBuf,
pub(crate) options: Node,
store: Mutex<Option<WeakStore>>,
bounds: Bounds,
pub(crate) notify: Arc<Notify>,
pub(crate) acks: Arc<Acks>,
logs: Mutex<HashMap<String, Weak<Log>>>,
copies: Mutex<HashMap<String, Arc<Copy>>>,
copied: Condvar,
routes: Mutex<Routes>,
rerouted: Condvar,
pub(crate) upstream: Upstream,
pub(crate) stopping: AtomicBool,
threads: Mutex<Vec<JoinHandle<()>>>,
links: Mutex<Vec<TcpStream>>,
pub(crate) listening: Mutex<Option<SocketAddr>>,
queue: Mutex<Option<Arc<super::queue::Queue>>>,
pub(crate) tls: super::transport::Tls,
pub(crate) snapshots: std::sync::atomic::AtomicU64,
pub(crate) lost: std::sync::atomic::AtomicU64,
pub(crate) wanted: AtomicBool,
opened: Mutex<HashMap<String, Vec<Switch>>>,
pub(crate) followers: Mutex<HashMap<u64, Follower>>,
next_follower: std::sync::atomic::AtomicU64,
pub(crate) upstream_at: Mutex<Option<String>>,
pub(crate) id: String,
pub(crate) served: Mutex<Option<Vec<String>>>,
catalog: super::catalog::Held,
direct: Mutex<HashMap<String, Arc<Direct>>>,
verifying: Mutex<HashMap<String, Verifying>>,
granted: Mutex<HashMap<String, Instant>>,
epochs: Mutex<HashMap<String, u64>>,
pub(crate) witness: Option<super::witness::Witness>,
}
pub(super) const DIRECT: &str = "from";
pub(crate) struct Following {
state: Arc<State>,
id: u64,
node: String,
address: String,
}
impl Following {
pub(crate) fn node(&self) -> &str {
&self.node
}
pub(crate) fn address(&self) -> &str {
&self.address
}
pub(crate) fn applied(&self, file: &str, sequence: u64) {
if let Some(follower) = lock(&self.state.followers).get_mut(&self.id) {
follower.applied.insert(file.to_owned(), sequence);
}
}
}
impl Drop for Following {
fn drop(&mut self) {
lock(&self.state.followers).remove(&self.id);
}
}
impl State {
pub(crate) fn new(dir: &Path, options: Node, keep_free: u64) -> Result<State> {
super::check_files(&options)?;
let tls = super::transport::Tls::of(&options)?;
let acks = Arc::new(Acks::waiting_for(options.synchronous_with.clone()));
Ok(State {
id: super::queue::node_id(&dir.join(DIR))?,
acks,
served: Mutex::default(),
catalog: Mutex::default(),
direct: Mutex::default(),
verifying: Mutex::default(),
granted: Mutex::default(),
epochs: Mutex::default(),
witness: if options.witness { Some(super::witness::Witness::open(dir)?) } else { None },
queue: Mutex::default(),
tls,
dir: dir.to_owned(),
options,
store: Mutex::default(),
bounds: Bounds { keep_free, ..Bounds::default() },
notify: Arc::default(),
logs: Mutex::default(),
copies: Mutex::default(),
copied: Condvar::new(),
routes: Mutex::default(),
rerouted: Condvar::new(),
upstream: Upstream::default(),
stopping: AtomicBool::new(false),
threads: Mutex::default(),
links: Mutex::default(),
listening: Mutex::default(),
snapshots: std::sync::atomic::AtomicU64::new(0),
lost: std::sync::atomic::AtomicU64::new(0),
wanted: AtomicBool::new(false),
opened: Mutex::default(),
followers: Mutex::default(),
next_follower: std::sync::atomic::AtomicU64::new(0),
upstream_at: Mutex::default(),
})
}
pub(crate) fn start(self: &Arc<Self>, store: &Store) -> Result<()> {
if self.options.failover.is_some() && self.options.synchronous && self.options.synchronous_with.is_empty() {
return Err(Error::invalid(
"a synchronous node a witness judges names the copies its commits wait for, synchronous_with: only \
they hold every commit it answered, and the witness gives its lease to one of them",
));
}
*lock(&self.store) = Some(store.downgrade());
let _ = self.acks.probe.set(Arc::clone(store.probe()));
let sampler: Weak<dyn crate::probe::Sample> = Arc::downgrade(self) as Weak<State>;
store.probe().sampled(sampler);
if self.options.catalog {
self.catalog(store)?;
}
if let Some(address) = &self.options.serve {
super::serve::start(self, address)?;
}
let direct = self.files_taken_directly()?;
if !self.options.from.is_empty() || !direct.is_empty() {
self.open_copies_held(store)?;
}
if !self.options.from.is_empty() {
super::follow::start(self, store);
}
for (name, address) in direct {
self.take_directly(store, &name, &address)?;
}
let writing = Arc::clone(self);
self.spawn("tinystore node: links", move || super::links::keep_written(&writing));
if let Some(witness) = &self.options.failover {
super::judge::start(self, store, witness.clone());
}
if self.options.catalog || !self.options.from.is_empty() {
let claiming = Arc::clone(self);
self.spawn("tinystore node: catalog", move || claiming.claim_its_files());
}
if self.dir.join(DIR).join(super::queue::FILE).exists() {
self.queue()?;
}
Ok(())
}
pub(crate) fn stop(&self) {
if self.stopping.swap(true, Ordering::SeqCst) {
return;
}
for log in lock(&self.logs).values().filter_map(Weak::upgrade) {
log.stop_waiting();
}
if let Some(mut address) = *lock(&self.listening) {
if address.ip().is_unspecified() {
address.set_ip(match address {
std::net::SocketAddr::V4(_) => std::net::Ipv4Addr::LOCALHOST.into(),
std::net::SocketAddr::V6(_) => std::net::Ipv6Addr::LOCALHOST.into(),
});
}
let _ = TcpStream::connect_timeout(&address, Duration::from_secs(1));
}
for link in lock(&self.links).drain(..) {
let _ = link.shutdown(std::net::Shutdown::Both);
}
self.upstream.stop();
for direct in lock(&self.direct).values() {
direct.upstream.stop();
}
self.notify.appended();
let threads: Vec<_> = lock(&self.threads).drain(..).collect();
for thread in threads {
if thread.thread().id() != thread::current().id() {
let _ = thread.join();
}
}
lock(&self.copies).clear();
if let Some(queue) = lock(&self.queue).take() {
queue.close();
}
if let Some(catalog) = lock(&self.catalog).take() {
catalog.close();
}
}
pub(crate) fn address(&self) -> String {
match &self.options.advertise {
Some(address) => address.clone(),
None => lock(&self.listening).map(|at| at.to_string()).unwrap_or_default(),
}
}
pub(crate) fn queue(self: &Arc<Self>) -> Result<Arc<super::queue::Queue>> {
let mut held = lock(&self.queue);
if let Some(queue) = &*held {
return Ok(Arc::clone(queue));
}
let queue = Arc::new(super::queue::Queue::open(&self.dir, self.id.clone())?);
*held = Some(Arc::clone(&queue));
let (state, sending) = (Arc::clone(self), Arc::clone(&queue));
self.spawn("tinystore node: queue", move || super::queue::send(&state, &sending));
Ok(queue)
}
pub(crate) fn store(&self) -> Option<Store> {
lock(&self.store).as_ref().and_then(WeakStore::upgrade)
}
pub(crate) fn stopping(&self) -> bool {
self.stopping.load(Ordering::SeqCst)
}
pub(crate) fn hello(&self, address: String) -> super::message::Message {
super::message::Message::Hello {
version: super::message::VERSION,
token: self.options.token.clone().unwrap_or_default(),
node: self.id.clone(),
address,
}
}
pub(crate) fn pause(&self, span: Duration) {
let until = Instant::now() + span;
while Instant::now() < until && !self.stopping() {
thread::sleep(Duration::from_millis(50));
}
}
pub(crate) fn spawn(&self, name: &str, work: impl FnOnce() + Send + 'static) {
let inside = tracing::Span::current().metadata().is_some_and(|span| span.name() == "node");
let span = if inside {
tracing::Span::none()
} else {
tracing::info_span!(target: "tinystore", "node", node = %self.id)
};
let thread = thread::Builder::new().name(name.to_owned()).spawn(move || span.in_scope(work));
match thread {
Ok(thread) => {
let mut threads = lock(&self.threads);
threads.retain(|kept| !kept.is_finished());
threads.push(thread);
}
Err(error) => tracing::warn!(target: "tinystore", %error, "a node could not start a thread of its links"),
}
}
pub(crate) fn keep_link(&self, link: &TcpStream) {
if let Ok(link) = link.try_clone() {
let mut links = lock(&self.links);
links.retain(|kept| kept.peer_addr().is_ok());
links.push(link);
}
}
pub(crate) fn following(self: &Arc<Self>, node: String, address: String) -> Following {
let id = self.next_follower.fetch_add(1, Ordering::Relaxed);
lock(&self.followers).insert(id, Follower { address: address.clone(), applied: Default::default() });
Following { state: Arc::clone(self), id, node, address }
}
pub(crate) fn bounds(&self) -> Bounds {
self.bounds
}
}