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
#![cfg_attr(docsrs, feature(doc_cfg))]
//! Distributed computation library for Rust.
//!
//! Paladin aims to simplify the challenge of writing distributed programs. It
//! provides a declarative API, allowing developers to articulate their
//! distributed programs clearly and concisely, without thinking about the
//! complexities of distributed systems programming.
//!
//! Features:
//! - **Declarative API**: Express distributed computations with clarity and
//! ease.
//! - **Automated Distribution**: Paladin’s runtime seamlessly handles task
//! distribution and execution across the cluster.
//! - **Simplified Development**: Concentrate on the program logic, leaving the
//! complexities of distributed systems to Paladin.
//! - **Infrastructure Agnostic**: Paladin is generic over its messaging backend
//! and infrastructure provider. Bring your own infra!
//!
//! # How to use Paladin
//!
//! When writing your programs, you will interact with two core APIs,
//! [`Operation`](crate::operation::Operation)s and
//! [`Directive`](crate::directive::Directive)s. You will define your
//! distributed computations in terms of
//! [`Operation`](crate::operation::Operation)s, and construct your programs
//! using Paladin's provided [`Directive`](crate::directive::Directive)s.
//!
//! In general, Paladin assumes that distributed programs will fundamentally
//! operate over a stream or async-iterator-like data structure. In particular,
//! Paladin's [`Directive`](crate::directive::Directive) API operates over
//! [functorial](crate::directive::Functor) or
//! [foldable](crate::directive::Foldable) data structures, providing methods
//! [`map`](crate::directive::Directive::map) and
//! [`fold`](crate::directive::Directive::fold), respectively. This
//! generalization allows Paladin to support a wide variety of parallel data
//! structures. Paladin provides one such data structure out of the box,
//! [`IndexedStream`](crate::directive::indexed_stream::IndexedStream).
//! [`IndexedStream`](crate::directive::indexed_stream::IndexedStream)
//! implements both the [`Functor`](crate::directive::Functor) and
//! [`Foldable`](crate::directive::Foldable) traits, and as such should be able
//! to handle any distributed algorithm that can be expressed in terms of
//! [`map`](crate::directive::Directive::map) and
//! [`fold`](crate::directive::Directive::fold). We recommend using
//! [`IndexedStream`](crate::directive::indexed_stream::IndexedStream) for all
//! your programs, as it has been highly optimized for parallelism, although
//! of course you are free to define your own.
//!
//! ## Defining Operations
//!
//! Operations are the semantic building blocks of your system. By implementing
//! [`Operation`](crate::operation::Operation) for a type, it can be given to
//! [`Directive`](crate::directive::Directive)s and remotely executed.
//!
//! ```
//! use paladin::operation::{Operation, Result};
//! # use paladin::opkind_derive::OpKind;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
//! struct FibAt;
//!
//! impl Operation for FibAt {
//! type Input = u64;
//! type Output = u64;
//! # type Kind = MyOps;
//!
//! fn execute(&self, input: Self::Input) -> Result<Self::Output> {
//! match input {
//! 0 => Ok(0),
//! 1 => Ok(1),
//! _ => {
//! let mut a = 0;
//! let mut b = 1;
//! for _ in 2..=input {
//! let temp = a;
//! a = b;
//! b = temp + b;
//! }
//! Ok(b)
//! }
//! }
//! }
//! }
//!
//! # #[derive(OpKind, Serialize, Deserialize, Debug, Clone, Copy)]
//! # enum MyOps {
//! # FibAt(FibAt),
//! # }
//! # fn main() {
//! assert_eq!(FibAt.execute(10).unwrap(), 55);
//! # }
//! ```
//!
//! ### Operation Registry
//!
//! To enable remote machines to execute your operations, you must declare a
//! registry of all available operations. This ensures remote executors can
//! deserialize and execute them correctly. Paladin provides a macro that wires
//! up all the necessary machinery on your registry. Registries must be declared
//! as an enum where the variants are single tuple structs containing the
//! operation.
//!
//! ```
//! use paladin::{operation::{Operation, Result}, opkind_derive::OpKind};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
//! struct FibAt;
//!
//! impl Operation for FibAt {
//! // ...
//! # type Input = u64;
//! # type Output = u64;
//! // Associate the operation with the registry type.
//! type Kind = MyOps;
//! // ...
//! #
//! # fn execute(&self, input: Self::Input) -> Result<Self::Output> {
//! # match input {
//! # 0 => Ok(0),
//! # 1 => Ok(1),
//! # _ => {
//! # let mut a = 0;
//! # let mut b = 1;
//! # for _ in 2..=input {
//! # let temp = a;
//! # a = b;
//! # b = temp + b;
//! # }
//! # Ok(b)
//! # }
//! # }
//! # }
//! }
//!
//! // Declare the registry.
//! #[derive(OpKind, Serialize, Deserialize, Debug, Clone, Copy)]
//! enum MyOps {
//! FibAt(FibAt),
//! }
//! ```
//!
//! ## Constructing a program
//!
//! Once operations have been defined, they can be plugged into Paladin's
//! [`Directive`](crate::directive::Directive)s to construct a distributed
//! program.
//!
//! ```
//! # use paladin::{operation::{Operation, Result}, opkind_derive::OpKind};
//! # use serde::{Deserialize, Serialize};
//! #
//! # #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
//! # struct FibAt;
//! #
//! # impl Operation for FibAt {
//! # type Input = u64;
//! # type Output = u64;
//! # type Kind = MyOps;
//! #
//! # fn execute(&self, input: Self::Input) -> Result<Self::Output> {
//! # match input {
//! # 0 => Ok(0),
//! # 1 => Ok(1),
//! # _ => {
//! # let mut a = 0;
//! # let mut b = 1;
//! # for _ in 2..=input {
//! # let temp = a;
//! # a = b;
//! # b = temp + b;
//! # }
//! # Ok(b)
//! # }
//! # }
//! # }
//! # }
//! #
//! # #[derive(OpKind, Serialize, Deserialize, Debug, Clone, Copy)]
//! # enum MyOps {
//! # FibAt(FibAt),
//! # Sum(Sum),
//! # }
//! #
//! use paladin::{
//! operation::Monoid,
//! directive::{indexed_stream::IndexedStream, Directive},
//! runtime::Runtime,
//! };
//!
//! // Define a Sum monoid.
//! # #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
//! struct Sum;
//! impl Monoid for Sum {
//! type Elem = u64;
//! type Kind = MyOps;
//!
//! fn combine(&self, a: Self::Elem, b: Self::Elem) -> Result<Self::Elem> {
//! Ok(a + b)
//! }
//!
//! fn empty(&self) -> Self::Elem {
//! 0
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let runtime = Runtime::in_memory().await.unwrap();
//! let stream = IndexedStream::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
//! // Compute the fibonacci number at each element in the stream with our
//! // previously declared `FibAt` operation.
//! let fibs = stream.map(FibAt);
//! // Sum the fibonacci numbers.
//! let sum = fibs.fold(Sum);
//!
//! // Run the computation.
//! let result = sum.run(&runtime).await;
//!
//! // Close the runtime
//! runtime.close().await?;
//!
//! assert_eq!(result?, 143);
//! # Ok(())
//! }
//! ```
//!
//! In this example program, we define an algorithm for computing the fibonacci
//! number at each element in a stream, and then summing the results. Behind the
//! scenes, Paladin will distribute the computations across the cluster and
//! return the result back to the main thread. Note that in this example, we're
//! using Paladin's multi-threaded in-memory runtime, which can be useful for
//! testing and debugging. In a real-world setting, one would use a distributed
//! runtime, such as Paladin's AMQP runtime.
//!
//! ## Application and deployment architecture
//!
//! We suggest the following project layout:
//! ```bash
//! ops
//! ├── Cargo.toml
//! └── src
//! └── lib.rs
//! worker
//! ├── Cargo.toml
//! └── src
//! └── main.rs
//! leader
//! ├── Cargo.toml
//! └── src
//! └── main.rs
//! ```
//!
//! Here's a breakdown:
//! - `ops`: A library with your operation definitions and their registry,
//! shared between `worker` and `leader`.
//! - `worker`: Executes operations on remote machines.
//! - `leader`: Coordinates the distributed computation.
//!
//! For deployment, use one `leader` for multiple `workers`. Currently, only one
//! `leader` deployment is supported. Future versions might offer state
//! persistence for better failover and fault tolerance.
//!
//! ### Worker main
//!
//! Given that virtually all workers will do exactly the same thing, Paladin's
//! worker runtime provides a
//! [`WorkerRuntime::main_loop`](crate::runtime::WorkerRuntime::main_loop). In
//! general, most of your logic should exist in `ops` and `leader`.
pub mod acker;
pub mod channel;
pub mod config;
pub mod contiguous;
pub mod directive;
pub mod operation;
pub mod queue;
pub mod runtime;
pub mod serializer;
pub mod task;
pub mod opkind_derive {
pub use paladin_opkind_derive::*;
}
pub use async_trait::async_trait;
pub use futures;
pub use tracing;