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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! Contains a set of types for constructing nodes.
//!
//! See [`node_builder`][crate::node_builder] or
//! [`node::builder`][crate::node::builder].
use futures::future::FutureExt;
use futures::future::LocalBoxFuture;
use futures::future::TryFutureExt;

use crate::buffer::Buffer;
use crate::buffer::InMemoryBuffer;
use crate::communicator::Communicator;
use crate::decoration::Decoration;
use crate::error::SpawnError;
use crate::executor;
use crate::invocation::AbstainOf;
use crate::invocation::CommunicationErrorOf;
use crate::invocation::CoordNumOf;
use crate::invocation::FrozenStateOf;
use crate::invocation::Invocation;
use crate::invocation::LogEntryOf;
use crate::invocation::NayOf;
use crate::invocation::NodeIdOf;
use crate::invocation::NodeOf;
use crate::invocation::RoundNumOf;
use crate::invocation::SnapshotFor;
use crate::invocation::YeaOf;
use crate::node;
#[cfg(feature = "tracer")]
use crate::tracer::Tracer;
use crate::voting::IndiscriminateVoter;
use crate::voting::Voter;
use crate::Node;
use crate::Shell;
use crate::State;

use super::snapshot::Snapshot;
use super::Core;
use super::IndiscriminateVoterFor;
use super::InvocationOf;
use super::NodeImpl;
use super::NodeKit;
use super::RequestHandlerFor;

/// Result returned by [`NodeBuilder::spawn`] or [`NodeBuilder::spawn_in`].
pub type SpawnResult<N, E> = std::result::Result<(RequestHandlerFor<N>, Shell<N>), SpawnError<E>>;

/// Blank node builder.
#[derive(Default)]
pub struct NodeBuilderBlank<I>(std::marker::PhantomData<I>);

impl<I: Invocation> NodeBuilderBlank<I> {
    /// Constructs a new blank builder.
    pub fn new() -> Self {
        Self(std::marker::PhantomData)
    }

    /// Specifies identifier of the node to be built.
    pub fn for_node(self, node_id: NodeIdOf<I>) -> NodeBuilderWithNodeId<I> {
        NodeBuilderWithNodeId { node_id }
    }
}

/// Node builder with node id already set.
pub struct NodeBuilderWithNodeId<I: Invocation> {
    node_id: NodeIdOf<I>,
}

impl<I: Invocation> NodeBuilderWithNodeId<I> {
    /// Sets the communicator with which to communicate.
    pub fn communicating_via<C>(self, communicator: C) -> NodeBuilderWithNodeIdAndCommunicator<I, C>
    where
        C: Communicator<
            Node = NodeOf<I>,
            RoundNum = RoundNumOf<I>,
            CoordNum = CoordNumOf<I>,
            LogEntry = LogEntryOf<I>,
            Error = CommunicationErrorOf<I>,
            Yea = YeaOf<I>,
            Nay = NayOf<I>,
            Abstain = AbstainOf<I>,
        >,
    {
        NodeBuilderWithNodeIdAndCommunicator {
            node_id: self.node_id,
            communicator,
        }
    }
}

/// Node builder with node id and communicator already set.
pub struct NodeBuilderWithNodeIdAndCommunicator<I: Invocation, C: Communicator> {
    node_id: NodeIdOf<I>,
    communicator: C,
}

/// Defines how to start a node w/r/t its state.
pub enum Starter<I: Invocation> {
    /// Start without a snapshot.
    ///
    /// Calling [`with(Starter::None)`][with] is equivalent to calling
    /// [`without_state()`][without_state].
    ///
    /// [with]: NodeBuilderWithNodeIdAndCommunicator::with
    /// [without_state]: NodeBuilderWithNodeIdAndCommunicator::without_state
    None,
    /// Resume from the given snapshot.
    ///
    /// Calling [`with(Starter::Resume(…))`][with] is equivalent to calling
    /// [`resuming_from(…)`][resuming_from].
    ///
    /// [with]: NodeBuilderWithNodeIdAndCommunicator::with
    /// [resuming_from]: NodeBuilderWithNodeIdAndCommunicator::resuming_from
    Resume(SnapshotFor<I>),
    /// Recover from the given snapshot.
    ///
    /// Calling [`with(Starter::Recover(…))`][with] is equivalent to calling
    /// [`recovering_with(…)`][recovering_with].
    ///
    /// [with]: NodeBuilderWithNodeIdAndCommunicator::with
    /// [recovering_with]: NodeBuilderWithNodeIdAndCommunicator::recovering_with
    Recover(SnapshotFor<I>),
}

impl<I: Invocation> From<Starter<I>> for SnapshotFor<I> {
    fn from(val: Starter<I>) -> Self {
        match val {
            Starter::None => Snapshot::stale_without_state(),
            Starter::Resume(s) => s,
            Starter::Recover(s) => s.into_stale(),
        }
    }
}

impl<I, C> NodeBuilderWithNodeIdAndCommunicator<I, C>
where
    I: Invocation,
    C: Communicator<
        Node = NodeOf<I>,
        RoundNum = RoundNumOf<I>,
        CoordNum = CoordNumOf<I>,
        LogEntry = LogEntryOf<I>,
        Error = CommunicationErrorOf<I>,
        Yea = YeaOf<I>,
        Nay = NayOf<I>,
        Abstain = AbstainOf<I>,
    >,
{
    ///
    pub fn with(
        self,
        starter: Starter<I>,
    ) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        self.with_snapshot(starter.into())
    }

    /// Starts the node without any state and in passive mode.
    pub fn without_state(self) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        self.with_snapshot(Snapshot::stale_without_state())
    }

    /// Starts a new cluster with the given initial state.
    ///
    /// The round number will be [zero](num_traits::Zero).
    pub fn with_initial_state<S: Into<Option<FrozenStateOf<I>>>>(
        self,
        initial_state: S,
    ) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        let snapshot = initial_state
            .into()
            .map(Snapshot::initial_with)
            .unwrap_or_else(Snapshot::initial_without_state);

        self.with_snapshot(snapshot)
    }

    /// Resume operation from the given snapshot.
    ///
    /// # Soundness
    ///
    /// It is assumed that the given snapshot was yielded from the [`Final`]
    /// event of a clean shutdown and that the node hasn't run in the meantime.
    /// As such the node will start in active participation mode. This is
    /// unsound if the assumptions are violated.
    ///
    /// Use [recovering_with] to have a failed node recover.
    ///
    /// [`Final`]: crate::event::ShutdownEvent::Final
    /// [recovering_with]: NodeBuilderWithNodeIdAndCommunicator::recovering_with
    pub fn resuming_from<S: Into<SnapshotFor<I>>>(
        self,
        snapshot: S,
    ) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        self.with_snapshot(snapshot.into())
    }

    /// Resume operation from the given snapshot.
    ///
    /// The node will participate passively until it can be certain that it is
    /// not breaking any previous commitments.
    pub fn recovering_with<S: Into<Option<SnapshotFor<I>>>>(
        self,
        snapshot: S,
    ) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        let snapshot = snapshot
            .into()
            .unwrap_or_else(Snapshot::stale_without_state);

        self.with_snapshot(snapshot.into_stale())
    }

    /// Resume operation without a snapshot.
    ///
    /// The node will participate passively until it can be certain that it is
    /// not breaking any previous commitments.
    pub fn recovering_without_state(
        self,
    ) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        self.with_snapshot(Snapshot::stale_without_state())
    }

    /// Commence operation from the given snapshot.
    ///
    /// # Soundness
    ///
    /// This method assumes that the node is  (re-)joining the Paxos cluster.
    /// Use [recovering_with] to have a failed node recover.
    ///
    /// A node is considered to rejoin iff there is a previous round `r` such
    /// that this node
    ///  - was not considered a member of the cluster for `r` and
    ///  - it did not participate in any rounds since `r`.
    ///
    /// [recovering_with]: NodeBuilderWithNodeIdAndCommunicator::recovering_with
    pub fn joining_with<S: Into<Option<SnapshotFor<I>>>>(
        self,
        snapshot: S,
    ) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        let snapshot = snapshot
            .into()
            .unwrap_or_else(Snapshot::initial_without_state);

        self.with_snapshot(snapshot.into_fresh())
    }

    /// Commence operation without a snapshot.
    ///
    /// # Soundness
    ///
    /// This method assumes that the node is  (re-)joining the Paxos cluster.
    /// Use [recovering_with] to have a failed node recover.
    ///
    /// A node is considered to rejoin iff there is a previous round `r` such
    /// that this node
    ///  - was not considered a member of the cluster for `r` and
    ///  - it did not participate in any rounds since `r`.
    ///
    /// [recovering_with]: NodeBuilderWithNodeIdAndCommunicator::recovering_with
    pub fn joining_without_state(
        self,
    ) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        self.with_snapshot(Snapshot::initial_without_state())
    }

    #[doc(hidden)]
    fn with_snapshot(
        self,
        snapshot: SnapshotFor<I>,
    ) -> NodeBuilder<Core<I, C>, impl Finisher<Node = Core<I, C>>> {
        NodeBuilder {
            kit: NodeKit::new(),
            node_id: self.node_id,
            communicator: self.communicator,
            snapshot,
            voter: IndiscriminateVoter::new(),
            buffer: InMemoryBuffer::new(1024),
            executor: executor::StdThread::default(),
            finisher: CoreFinisher::new(),

            #[cfg(feature = "tracer")]
            tracer: None,
        }
    }
}

/// Used to construct the node when `spawn_in` is called.
///
/// As decorations are added via `ExtensibleNodeBuilder`, the builder keeps a
/// stack of the `Decoration::wrap` calls to make. This stack is encoded via
/// implementations of the `Finisher` trait.
pub trait Finisher: 'static {
    /// Type of node constructed by this finisher.
    type Node: Node;

    /// Type of communicator used by the constructed node.
    type Communicator: Communicator<
        Node = node::NodeOf<Self::Node>,
        RoundNum = node::RoundNumOf<Self::Node>,
        CoordNum = node::CoordNumOf<Self::Node>,
        LogEntry = node::LogEntryOf<Self::Node>,
        Error = node::CommunicationErrorOf<Self::Node>,
        Yea = node::YeaOf<Self::Node>,
        Nay = node::NayOf<Self::Node>,
        Abstain = node::AbstainOf<Self::Node>,
    >;

    /// Wraps the configured decorations around the given `core` node.
    fn finish(
        self,
        core: Core<InvocationOf<Self::Node>, Self::Communicator>,
    ) -> Result<Self::Node, Box<dyn std::error::Error + Send + Sync + 'static>>;
}

struct CoreFinisher<I, C>(std::marker::PhantomData<(I, C)>);

impl<I, C> CoreFinisher<I, C> {
    fn new() -> Self {
        Self(std::marker::PhantomData)
    }
}

impl<I, C> Finisher for CoreFinisher<I, C>
where
    I: Invocation,
    C: Communicator<
        Node = NodeOf<I>,
        RoundNum = RoundNumOf<I>,
        CoordNum = CoordNumOf<I>,
        LogEntry = LogEntryOf<I>,
        Error = CommunicationErrorOf<I>,
        Yea = YeaOf<I>,
        Nay = NayOf<I>,
        Abstain = AbstainOf<I>,
    >,
{
    type Node = Core<I, C>;
    type Communicator = C;

    fn finish(
        self,
        core: Core<InvocationOf<Self::Node>, Self::Communicator>,
    ) -> Result<Core<I, C>, Box<dyn std::error::Error + Send + Sync + 'static>> {
        Ok(core)
    }
}

struct DecorationFinisher<D: Decoration, I> {
    arguments: <D as Decoration>::Arguments,
    inner: I,
}

impl<D: Decoration, I> DecorationFinisher<D, I> {
    fn wrap(inner: I, arguments: <D as Decoration>::Arguments) -> Self {
        DecorationFinisher { arguments, inner }
    }
}

impl<D, I> Finisher for DecorationFinisher<D, I>
where
    D: Decoration + 'static,
    I: Finisher<Node = D::Decorated> + 'static,
{
    type Node = D;
    type Communicator = <I as Finisher>::Communicator;

    fn finish(
        self,
        core: Core<InvocationOf<Self::Node>, Self::Communicator>,
    ) -> Result<D, Box<dyn std::error::Error + Send + Sync + 'static>> {
        self.inner
            .finish(core)
            .and_then(move |node| D::wrap(node, self.arguments))
    }
}

/// Node builder with all essential information set.
pub struct NodeBuilder<
    N: Node,
    F: Finisher<Node = N>,
    V = IndiscriminateVoterFor<N>,
    B = InMemoryBuffer<node::RoundNumOf<N>, node::CoordNumOf<N>, node::LogEntryOf<N>>,
    E: executor::Executor = executor::StdThread,
> {
    kit: NodeKit<InvocationOf<N>>,
    node_id: node::NodeIdOf<N>,
    voter: V,
    communicator: <F as Finisher>::Communicator,
    snapshot: node::SnapshotFor<N>,
    buffer: B,
    executor: E,
    finisher: F,

    #[cfg(feature = "tracer")]
    tracer: Option<Box<dyn Tracer<InvocationOf<N>>>>,
}

impl<N, F, V, B, E> NodeBuilder<N, F, V, B, E>
where
    N: NodeImpl + 'static,
    F: Finisher<Node = N>,
    V: Voter<
        State = node::StateOf<N>,
        RoundNum = node::RoundNumOf<N>,
        CoordNum = node::CoordNumOf<N>,
        Abstain = node::AbstainOf<N>,
        Yea = node::YeaOf<N>,
        Nay = node::NayOf<N>,
    >,
    B: Buffer<
        RoundNum = node::RoundNumOf<N>,
        CoordNum = node::CoordNumOf<N>,
        Entry = node::LogEntryOf<N>,
    >,
    E: executor::Executor,
{
    /// Sets the applied entry buffer.
    pub fn buffering_applied_entries_in<
        T: Buffer<
            RoundNum = node::RoundNumOf<N>,
            CoordNum = node::CoordNumOf<N>,
            Entry = node::LogEntryOf<N>,
        >,
    >(
        self,
        buffer: T,
    ) -> NodeBuilder<N, F, V, T, E> {
        // https://github.com/rust-lang/rust/issues/86555
        NodeBuilder {
            kit: self.kit,
            node_id: self.node_id,
            voter: self.voter,
            communicator: self.communicator,
            snapshot: self.snapshot,
            buffer,
            executor: self.executor,
            finisher: self.finisher,

            #[cfg(feature = "tracer")]
            tracer: self.tracer,
        }
    }

    /// Sets the executor to be used.
    pub fn driven_by<T: executor::Executor>(self, executor: T) -> NodeBuilder<N, F, V, B, T> {
        // https://github.com/rust-lang/rust/issues/86555
        NodeBuilder {
            kit: self.kit,
            node_id: self.node_id,
            voter: self.voter,
            communicator: self.communicator,
            snapshot: self.snapshot,
            buffer: self.buffer,
            executor,
            finisher: self.finisher,

            #[cfg(feature = "tracer")]
            tracer: self.tracer,
        }
    }

    /// Tracer to record events with.
    #[cfg(feature = "tracer")]
    pub fn traced_by<T: Into<Box<dyn Tracer<InvocationOf<N>>>>>(mut self, tracer: T) -> Self {
        self.tracer = Some(tracer.into());

        self
    }

    /// Sets the voting strategy to use.
    pub fn voting_with<T>(self, voter: T) -> NodeBuilder<N, F, T, B, E> {
        // https://github.com/rust-lang/rust/issues/86555
        NodeBuilder {
            kit: self.kit,
            node_id: self.node_id,
            voter,
            communicator: self.communicator,
            snapshot: self.snapshot,
            buffer: self.buffer,
            executor: self.executor,
            finisher: self.finisher,

            #[cfg(feature = "tracer")]
            tracer: self.tracer,
        }
    }

    /// Sets the node kit to use.
    pub fn using(mut self, kit: NodeKit<InvocationOf<N>>) -> Self {
        self.kit = kit;

        self
    }

    /// Spawns the node into context `()`.
    pub fn spawn(self) -> LocalBoxFuture<'static, SpawnResult<N, executor::ErrorOf<E>>>
    where
        node::StateOf<N>: State<Context = ()>,
    {
        self.spawn_in(())
    }

    /// Spawns the node in the given context.
    pub fn spawn_in(
        self,
        context: node::ContextOf<N>,
    ) -> LocalBoxFuture<'static, SpawnResult<N, executor::ErrorOf<E>>> {
        let finisher = self.finisher;

        let receiver = self.kit.receiver;

        Core::spawn(
            self.kit.state_keeper,
            self.kit.sender,
            self.node_id,
            self.communicator,
            super::SpawnArgs {
                context,
                node_id: self.node_id,
                voter: self.voter,
                snapshot: self.snapshot,
                buffer: self.buffer,
                executor: self.executor,
                #[cfg(feature = "tracer")]
                tracer: self.tracer,
            },
        )
        .map_err(SpawnError::ExecutorError)
        .and_then(|(req_handler, core)| {
            futures::future::ready(
                finisher
                    .finish(core)
                    .map(|node| (req_handler, Shell::new(node, receiver)))
                    .map_err(SpawnError::Decoration),
            )
        })
        .boxed_local()
    }
}

/// Declares the `decorated_with` method to add decorations to the node being
/// built.
pub trait ExtensibleNodeBuilder {
    /// Node type without decoration applied.
    type Node: NodeImpl;

    /// Type of this builder after decoration `D` is applied.
    type DecoratedBuilder<D: Decoration<Decorated = Self::Node> + 'static>;

    /// Adds a decoration to wrap around the resulting node.
    fn decorated_with<D>(
        self,
        arguments: <D as Decoration>::Arguments,
    ) -> Self::DecoratedBuilder<D>
    where
        D: Decoration<Decorated = Self::Node, Invocation = InvocationOf<Self::Node>> + 'static;
}

impl<N, F, V, B, E> ExtensibleNodeBuilder for NodeBuilder<N, F, V, B, E>
where
    N: NodeImpl + 'static,
    F: Finisher<Node = N>,
    V: Voter<
        State = node::StateOf<N>,
        RoundNum = node::RoundNumOf<N>,
        CoordNum = node::CoordNumOf<N>,
        Abstain = node::AbstainOf<N>,
        Yea = node::YeaOf<N>,
        Nay = node::NayOf<N>,
    >,
    B: Buffer<
        RoundNum = node::RoundNumOf<N>,
        CoordNum = node::CoordNumOf<N>,
        Entry = node::LogEntryOf<N>,
    >,
    E: executor::Executor,
{
    type Node = N;
    type DecoratedBuilder<D: Decoration<Decorated = N> + 'static> =
        NodeBuilder<D, impl Finisher<Node = D>, V, B, E>;

    fn decorated_with<D>(self, arguments: <D as Decoration>::Arguments) -> Self::DecoratedBuilder<D>
    where
        D: Decoration<Decorated = N, Invocation = InvocationOf<N>> + 'static,
    {
        NodeBuilder {
            kit: self.kit,
            node_id: self.node_id,
            communicator: self.communicator,
            snapshot: self.snapshot,
            voter: self.voter,
            buffer: self.buffer,
            executor: self.executor,
            finisher: DecorationFinisher::wrap(self.finisher, arguments),

            #[cfg(feature = "tracer")]
            tracer: self.tracer,
        }
    }
}