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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//! Remote operation traits.
//!
//! This module defines the core traits that enable users to define the
//! semantics of their system. Operations define the signature and semantics of
//! a computation. Akin to a function that maps an input to an output.
//!
//! Key components of this module include:
//! ## [`Operation`]
//! Represents a generic operation that can be executed by a remote machine.
//! It defines the signature and semantics of a computation.
//!
//! ## [`Monoid`]
//! Represents a binary [`Operation`] whose elements can be combined in an
//! associative manner. It's a specialized form of an [`Operation`] that has an
//! identity element and an associative binary operation. This makes it
//! _foldable_.
//!
//! ## [`Operation`] implementation of [`Monoid`]
//! An automatic implementation of the [`Operation`] trait for [`Monoid`]s trait
//! is provided. An [`Operation`] is strictly more general than a [`Monoid`], so
//! we can trivially derive an [`Operation`] for every [`Monoid`].
//!
//! # Usage:
//! Operations are the semantic building blocks of the system. They define what
//! computations can be performed by the system. By implementing the
//! [`Operation`] trait for a type, it can be remotely executed. Operations are
//! not necessarily meant to be executed directly, but rather embedded into
//! [`Task`](crate::task::Task)s, which contain operation arguments, additional
//! metadata, and routing information to facilitate remote execution.
//!
//! ## Example
//! ### Defining an [`Operation`]:
//!
//! ```
//! use paladin::{RemoteExecute, operation::{Operation, Result}};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, RemoteExecute)]
//! struct StringLength;
//!
//! impl Operation for StringLength {
//! type Input = String;
//! type Output = usize;
//!
//! fn execute(&self, input: Self::Input) -> Result<Self::Output> {
//! Ok(input.len())
//! }
//! }
//! ```
//!
//! ### Defining a [`Monoid`]:
//!
//! ```
//! use paladin::{RemoteExecute, operation::{Monoid, Operation, Result}};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, RemoteExecute)]
//! struct StringConcat;
//!
//! impl Monoid for StringConcat {
//! type Elem = String;
//!
//! fn combine(&self, a: Self::Elem, b: Self::Elem) -> Result<Self::Elem> {
//! Ok(a + &b)
//! }
//!
//! fn empty(&self) -> Self::Elem {
//! String::new()
//! }
//! }
//! ```
//!
//! ### An [`Operation`] with constructor arguments:
//!
//! ```
//! use paladin::{RemoteExecute, operation::{Monoid, Operation, Result}};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, RemoteExecute)]
//! struct MultiplyBy(i32);
//!
//! impl Operation for MultiplyBy {
//! type Input = i32;
//! type Output = i32;
//!
//! fn execute(&self, input: Self::Input) -> Result<Self::Output> {
//! Ok(self.0 * input)
//! }
//! }
//! ```
use Debug;
use Bytes;
use crate;
/// An operation that is identifiable and executable by the runtime in a
/// distributed environment.
///
/// It is used to facilitate serialization, deserialization, and dynamic
/// execution of operations.
///
/// These will be automatically implemented by the
/// [`RemoteExecute`](crate::RemoteExecute) derive macro.
/// An operation that can be performed by a worker.
///
/// Akin to a function that maps an input to an output, it defines the signature
/// and semantics of a computation.
/// An associative binary [`Operation`].
/// Implement the [`Operation`] trait for types that support the binary
/// operation
/// Marker types for [`Operation`]s.
/// Generate an operation registry for external crates.
///
/// This macro generates a `register()` function that can be used to force
/// module inclusion of [`Operation`] implementations.
///
/// Note this is only necessary for operations that are defined in a crate
/// external to the worker runtime instantiation. If the operations are defined
/// in the same crate as the worker runtime instantiation, then the operations
/// will naturally be included in the worker runtime binary.
///
/// The `register()` function must be defined _within_ the external operations
/// module such that it is imported and called _from_ the worker module. This
/// scheme ensures that the compiler does not exclude the external operations
/// from its compilation.
///
/// **You will have problems if your operations are defined in a separate crate
/// from your worker runtime and you do not call this macro from within your
/// operation module.**
///
/// # Example
/// Let's say you have a workspace with the following structure:
/// ```text
/// my_workspace
/// ├── my_operations
/// │ ├── Cargo.toml
/// │ └── src
/// │ └── lib.rs
/// └── my_worker
/// ├── Cargo.toml
/// └── src
/// └── main.rs
/// ```
///
/// In `my_operations`, you define your operations:
/// ```
/// use paladin::{registry, RemoteExecute, operation::{Operation, Result}};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize, RemoteExecute)]
/// struct MyOperation;
///
/// impl Operation for MyOperation {
/// type Input = ();
/// type Output = ();
///
/// fn execute(&self, _input: Self::Input) -> Result<Self::Output> {
/// Ok(())
/// }
/// }
///
/// // A `register()` function is generated and exported by the `registry!()` macro.
/// // This must be called from within the operation module.
/// registry!();
/// ```
///
/// In `my_worker`, you instantiate the worker runtime:
/// ```
/// # mod my_operations {
/// # use paladin::{registry, RemoteExecute, operation::{Operation, Result}};
/// # use serde::{Deserialize, Serialize};
/// #
/// # #[derive(Serialize, Deserialize, RemoteExecute)]
/// # struct MyOperation;
/// #
/// # impl Operation for MyOperation {
/// # type Input = ();
/// # type Output = ();
/// #
/// # fn execute(&self, _input: Self::Input) -> Result<Self::Output> {
/// # Ok(())
/// # }
/// # }
/// #
/// # registry!();
/// # }
/// #
/// use paladin::{runtime::WorkerRuntime, config::{Config, Runtime}};
/// use my_operations::register;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let config = Config {
/// runtime: Runtime::InMemory,
/// ..Default::default()
/// };
///
/// let runtime = WorkerRuntime::from_config(&config, register()).await?;
///
/// Ok(())
/// }
/// ```
pub use *;