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
#![doc = include_str!("../../README.md")]
#![cfg_attr(test, feature(is_sorted))]
#![feature(impl_trait_projections)]
#![feature(type_alias_impl_trait)]
#![feature(default_free_fn)]
#![feature(drain_filter)]
#![feature(let_chains)]
use std::{io, net::SocketAddr, time::Duration};
use color_eyre::{eyre::bail, Result};
use tap::Pipe;
pub use tokio;
use tokio::task::{JoinError, JoinSet};
use tokio_util::sync::CancellationToken;
use tracing::{info, log::warn};
use uuid7::uuid7;
use crate::{
model::{Envelope, Log, LogList, Message, Topic},
tasks::{spawn_background, ContextRef, Swim, TopicEntry},
};
mod codec;
pub mod consts;
pub mod model;
mod tasks;
mod util;
pub use codec::{adapt, adapt_with_option, SerdeBincodeCodec};
pub use model::OrkasConfig;
pub use util::{CRDTReader, CRDTUpdater};
// TODO: enum Status { Starting, Running, Stopped }
/// The main struct of Orkas.
///
/// This is the entry point of the library. This
/// controls the lifecycle of the background tasks and cannot be cloned. To
/// actually interact with the cluster, you need to create a [`Handle`] instance
/// by calling [`Orkas::handle`]. When dropped, background tasks will be stopped
/// forcefully. To gracefully stop the background tasks, call [`Orkas::stop`].
pub struct Orkas {
ctx: ContextRef,
addr: SocketAddr,
join_set: JoinSet<Result<()>>,
}
impl Drop for Orkas {
fn drop(&mut self) {
if self.is_running() {
warn!("Background tasks are not properly stopped. Forcefully stopping.");
self.cancel();
}
}
}
impl Orkas {
// TODO: more ergonomic `start_with_*` options
#[inline]
pub async fn start(config: OrkasConfig) -> io::Result<Self> {
spawn_background(config.into()).await
}
/// Check if all background tasks are still running.
pub fn is_running(&self) -> bool {
!(self.ctx.cancel_token.is_cancelled() || self.join_set.is_empty())
}
/// Get the context of the background tasks.
pub fn ctx(&self) -> &ContextRef {
&self.ctx
}
/// Get the address of the background tasks.
pub fn local_addr(&self) -> SocketAddr {
self.addr
}
/// Get the cancellation token of the background tasks.
pub fn cancel_token(&self) -> &CancellationToken {
&self.ctx.cancel_token
}
/// Issue a cancellation request to the background tasks. Use
/// [`Orkas::join`] to wait for the tasks to finish.
pub fn cancel(&self) {
if !self.is_running() {
return;
}
self.ctx.cancel();
}
/// Forcefully shutdown the background tasks with [`JoinHandle::abort`].
///
/// [`JoinHandle::abort`]: tokio::task::JoinHandle::abort
pub fn abort(&mut self) {
if !self.is_running() {
return;
}
self.ctx.stop();
self.join_set.abort_all()
}
/// Wait for all background tasks to finish. Use [`Orkas::cancel`]
/// beforehand to gracefully stop the tasks.
pub async fn join(&mut self) -> Option<Vec<Result<Result<()>, JoinError>>> {
if self.join_set.is_empty() {
return None;
}
self.cancel();
let mut results = Vec::with_capacity(self.join_set.len());
while let Some(res) = self.join_set.join_next().await {
results.push(res);
}
Some(results)
}
#[inline]
/// Get a handle to interact with the cluster.
pub fn handle(&self) -> Handle {
Handle {
ctx: self.ctx().clone(),
addr: self.local_addr(),
}
}
}
/// A cheap-to-clone handle to interact with the cluster. This cannot control
/// the lifecycle of the background tasks. For that, you need to use the
/// [`Orkas`] struct.
#[derive(Clone, Debug)]
pub struct Handle {
ctx: ContextRef,
addr: SocketAddr,
}
impl Handle {
async fn connect_to(&self, addr: SocketAddr) -> Result<()> {
let (send, recv) = tokio::net::TcpStream::connect(addr) // TODO: fine tuning connection or just use quinn
.await?
.into_split()
.pipe(adapt);
info!(%addr, "Connected");
// Send the streams to the background task
self.ctx.conn_inbound.send(send).await?;
self.ctx.conn_outbound.send((addr, recv)).await?;
Ok(())
}
#[inline]
fn make_swim(&self, topic: String) -> Result<Swim> {
Swim::new(self.addr, self.ctx.clone(), topic)
}
/// Update a topic and sync with other nodes in the cluster. This update
/// will be applied to local cluster state first and distributed to
/// other nodes in the cluster via swim broadcast.
///
/// A [`CRDTUpdater`] will receive a [`LogList`] and `actor` of the node
/// upon updating, then optionally returns an [`Op`] to be applied to the
/// [`LogList`].
///
/// [`Op`]: crdts::list::Op
pub async fn update<F>(&self, topic: impl AsRef<str>, updater: F) -> Result<bool>
where
F: CRDTUpdater,
F::Error: std::error::Error + Send + Sync + 'static,
{
self.ctx.update(topic, updater).await
}
/// Read a topic and derive data from it
#[inline]
pub fn read<F: CRDTReader>(&self, topic: impl AsRef<str>, func: F) -> Option<F::Return> {
self.ctx.get_topic(topic).map(|x| func.read(x.crdt()))
}
/// Emit an event and broadcast it
#[inline]
pub fn log(&self, topic: impl AsRef<str>, log: Log) -> Result<()> {
self.ctx.log(topic, log)
}
#[inline]
pub fn get_topic(&self, topic: impl AsRef<str>) -> Option<TopicEntry<'_>> {
self.ctx.get_topic(topic)
}
pub fn has_topic(&self, topic: impl AsRef<str>) -> bool {
self.ctx.has_topic(topic)
}
/// Create a new topic in current node
pub async fn new_topic(&self, topic: impl Into<String>) -> Result<()> {
let topic = topic.into();
// Spawn the task and create local record
let swim = self.make_swim(topic.clone())?.spawn();
let logs = LogList::new();
let map = limlog::Topic::builder(topic.clone())?.build().await?;
let topic_record = Topic { swim, logs, map };
// Insert the topic record
self.ctx.insert_topic(topic, topic_record);
Ok(())
}
/// Leave a topic cluster
pub fn quit_topic(&self, topic: impl Into<String>) -> Result<Option<TopicEntry<'_>>> {
let topic = topic.into();
let Some(topic_record) = self.ctx.remove_topic(&topic) else { return Ok(None) };
topic_record.swim().stop();
Ok(Some(topic_record))
}
/// Join a topic cluster with given address and topic name.
///
/// This will start handshake with correspoding SWIM node. Note that initial
/// state syncing is not garanteed to be completed within this function.
pub async fn join_one(&self, topic: impl Into<String>, addr: SocketAddr) -> Result<()> {
// TODO: use `ToSocketAddrs` instead of `SocketAddr`.
// TODO: join with multiple initial nodes
let topic = topic.into();
if self.has_topic(&topic) {
return Ok(());
}
self.connect_to(addr).await?;
let mut swim = Swim::new(self.addr, self.ctx.clone(), topic.clone())?;
let mut rt = self.ctx.swim_runtime(&topic);
// Start announcing
swim.announce(addr.into(), &mut rt)?;
// Spawn the SWIM task
let swim = swim.spawn();
let logs = LogList::new();
let map = limlog::Topic::builder(topic.clone())?.build().await?;
let topic_record = Topic { swim, logs, map };
// Insert the topic record
self.ctx.insert_topic(&topic, topic_record);
// Send all messages from swim to outbound
rt.flush().await?;
drop(rt);
if !self.ctx.wait_for(addr, Duration::from_secs(5)).await {
bail!("Timeout waiting for initial join")
}
// Now swim is online, we can send the snapshot request
self.ctx
.msg
.send(Envelope {
addr,
topic,
body: Message::RequestSnapshot,
id: uuid7(),
})
.await?;
Ok(())
}
}