ax_core 0.3.2

Core library implementing the functions of ax
Documentation
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
use super::{formats::ShutdownReason, node_settings::Settings, util::spawn_with_name};
use anyhow::Result;
use crossbeam::{channel, select};
use std::thread::JoinHandle;

pub mod android;
pub mod logging;
pub mod node_api;
pub mod store;
pub mod swarm_observer;

#[derive(Debug)]
pub enum ComponentRequest<A> {
    /// Component specific request
    Individual(A),
    /// Register a supervisor, which is informed about the component's state
    /// changes. Each Component is considered a singleton within the codebase.
    RegisterSupervisor(channel::Sender<(ComponentType, ComponentState)>),
    /// Global Settings have changed
    SettingsChanged(Box<Settings>),
    /// Trigger a stop and restart
    Restart,
    /// Trigger graceful shutdown
    Shutdown(ShutdownReason),
}

#[derive(Debug)]
pub enum ComponentState {
    Errored(anyhow::Error),
    Starting,
    Started,
    Stopped,
}

#[cfg(test)]
impl PartialEq for ComponentState {
    fn eq(&self, other: &Self) -> bool {
        std::mem::discriminant(self) == std::mem::discriminant(other)
    }
}

macro_rules! continue_on_error {
    ($c:expr, $l:expr) => {
        match $l {
            Err(e) => {
                tracing::error!("Component \"{}\": {}", $c, e);
                continue;
            }
            Ok(x) => x,
        }
    };
}

macro_rules! state_change {
    ($maybe_supervisor:expr, $c_name:expr, $target:expr, $result_transition:expr) => {
        let new_state = if let Err(e) = $result_transition {
            ComponentState::Errored(e)
        } else {
            $target
        };
        tracing::debug!("Component \"{}\": State change to {:?}", $c_name, new_state);
        match $maybe_supervisor.as_ref() {
            Some(snd) => {
                snd.send(($c_name.to_string().into(), new_state))?;
            }
            None => {
                tracing::error!("Component \"{}\": No supervisor registered.", $c_name)
            }
        }
    };
}

#[derive(Debug, Clone, Eq, PartialOrd, Ord, PartialEq, derive_more::Display, derive_more::From)]
pub struct ComponentType(String);

impl From<&str> for ComponentType {
    fn from(s: &str) -> Self {
        Self(s.to_owned())
    }
}

/// A `Component` is a self-contained package encapsulating a set of
/// functionality. This trait exposes defined ways to interact with the component
/// and manage its lifecycle. A component can provide individual `RequestType`s,
/// and provides distinct `ComponentSettings`. A standard implementation to drive
/// the component is provided in `Component::loop_on_rx`.
pub trait Component<RequestType, ComponentSettings>
where
    Self: Sized + Send + 'static,
    ComponentSettings: Clone + PartialEq,
    RequestType: std::fmt::Debug,
{
    /// Returns the type of the `Component`
    fn get_type() -> &'static str;

    /// Borrowed access to the held Receiver
    fn get_rx(&self) -> &channel::Receiver<ComponentRequest<RequestType>>;

    /// Handle a component specific request
    fn handle_request(&mut self, req: RequestType) -> Result<()>;

    /// Transform a complete `Settings` object into component specific
    /// `ComponentSettings`. In some cases a simple `Into` might not be
    /// sufficient, and access to self is necessary.
    fn extract_settings(&self, s: Settings) -> Result<ComponentSettings>;

    /// New component specific `ComponentSettings`. Returned bool indicates
    /// whether a restart of the component is required.
    fn set_up(&mut self, _: ComponentSettings) -> bool {
        false
    }

    /// Start the component. This function should be idempotent and must be
    /// lock-free. Errors that happen during the startup or runtime of the
    /// component can be signalled using the provided `notifier`. Successful
    /// startup must be signalled by sending `Ok(())`.
    fn start(&mut self, notifier: channel::Sender<anyhow::Result<()>>) -> Result<()>;

    /// Stop the component. This function should be idempotent. It must be
    /// ensured, that all resources are cleaned up when returning from this
    /// method.
    fn stop(&mut self) -> Result<()>;

    /// Convenience implementation managing the lifecycle of a `Component` as
    /// driven by `ComponentRequest`s: New settings are converted to component
    /// specific ones; if they have been changed (as determined by Eq), the
    /// component is going to be stopped and started. This function will block
    /// and only return after receiving a `ComponentRequest::Shutdown` message.
    fn loop_on_rx(mut self) -> Result<()> {
        let mut last_settings: Option<ComponentSettings> = None;
        let mut supervisor: Option<channel::Sender<(ComponentType, ComponentState)>> = None;
        let (err_tx, err_rx) = channel::bounded::<anyhow::Result<()>>(8);
        let mut has_started = false;
        loop {
            select! {
                recv(err_rx) -> result => {
                    tracing::debug!("Component \"{}\": started", Self::get_type());
                    let result = result.expect("We keep another Sender around, thus channel can't be disconnected");
                    state_change!(
                        supervisor,
                        Self::get_type(),
                        ComponentState::Started,
                        result
                    );
                },
                recv(self.get_rx()) -> node_msg => {
                    if let Ok(m) = node_msg {
                        tracing::debug!("Component \"{}\": {:?}", Self::get_type(), m);
                        match m {
                            ComponentRequest::<RequestType>::Individual(m) => {
                                continue_on_error!(Self::get_type(), self.handle_request(m))
                            }
                            ComponentRequest::<RequestType>::RegisterSupervisor(snd) => {
                                if supervisor.replace(snd).is_some() {
                                    tracing::warn!("Component \"{}\": Supervisor registered twice!", Self::get_type());
                                } else {
                                    tracing::debug!("Component \"{}\": Supervisor registered", Self::get_type());
                                }
                            }
                            ComponentRequest::SettingsChanged(settings) => {
                                let s = continue_on_error!(Self::get_type(), self.extract_settings(*settings));
                                let config_changed = !last_settings.iter().any(|c| *c == s);
                                if config_changed {
                                    tracing::debug!("Component \"{}\": Settings changed.", Self::get_type());
                                    let needs_restart =  self.set_up(s.clone());
                                    last_settings.replace(s);
                                    if !has_started || needs_restart {
                                        if has_started {
                                            state_change!(supervisor, Self::get_type(), ComponentState::Stopped, self.stop());
                                        }
                                        has_started = true;
                                        state_change!(
                                            supervisor,
                                            Self::get_type(),
                                            ComponentState::Starting,
                                            self.start(err_tx.clone())
                                        );
                                    }
                                }
                            }
                            ComponentRequest::Restart => {
                                if has_started {
                                    state_change!(supervisor, Self::get_type(), ComponentState::Stopped, self.stop());
                                }
                                has_started = true;
                                state_change!(
                                    supervisor,
                                    Self::get_type(),
                                    ComponentState::Starting,
                                    self.start(err_tx.clone())
                                );
                            }
                            ComponentRequest::<RequestType>::Shutdown(_) => break,
                        }

                    } else {
                        // Channel returned by `self.get_rx` is disconnected.
                        // Nothing else we can do but shut down
                        break;
                    }
                }
            }
            tracing::debug!("Component \"{}\": event handled", Self::get_type());
        }

        tracing::debug!("Component \"{}\": Shutting down", Self::get_type());
        state_change!(supervisor, Self::get_type(), ComponentState::Stopped, self.stop());
        Ok(())
    }

    /// Spawn the component into its own thread, where `Component::loop_on_rx` is
    /// executed. Returns a `std::thread::JoinHandle` to the spawned thread.
    fn spawn(self) -> Result<JoinHandle<()>> {
        let name = Self::get_type().to_string();
        let h = spawn_with_name(name.clone(), move || {
            if let Err(e) = self.loop_on_rx() {
                tracing::error!("Component \"{}\": Thread exited ({})", name, e);
            }
        });

        Ok(h)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use anyhow::Result;
    use crossbeam::channel::Sender;
    use std::{
        sync::{Arc, Mutex},
        time::Duration,
    };

    struct SimpleComponent {
        rx: channel::Receiver<ComponentRequest<SimpleRequest>>,
        random_config: bool,
        last_cnt: usize,
        notifier: Arc<Mutex<Option<channel::Sender<Result<()>>>>>,
    }

    #[derive(Debug)]
    enum SimpleRequest {
        ToggleRandomConfigCreation,
        Ping(Sender<()>),
    }

    #[derive(Clone, PartialEq, Eq)]
    struct SimpleSettings {
        cnt: usize,
    }

    impl SimpleComponent {
        fn new(
            rx: channel::Receiver<ComponentRequest<SimpleRequest>>,
            notifier: Arc<Mutex<Option<channel::Sender<Result<()>>>>>,
        ) -> Self {
            Self {
                rx,
                random_config: false,
                last_cnt: 0,
                notifier,
            }
        }
    }
    impl Component<SimpleRequest, SimpleSettings> for SimpleComponent {
        fn get_type() -> &'static str {
            "test"
        }
        fn get_rx(&self) -> &channel::Receiver<ComponentRequest<SimpleRequest>> {
            &self.rx
        }
        fn set_up(&mut self, s: SimpleSettings) -> bool {
            self.last_cnt = s.cnt;
            true
        }
        fn handle_request(&mut self, x: SimpleRequest) -> Result<()> {
            match x {
                SimpleRequest::Ping(ponger) => ponger.send(()).unwrap(),
                SimpleRequest::ToggleRandomConfigCreation => {
                    self.random_config = !self.random_config;
                }
            }
            Ok(())
        }
        fn start(&mut self, notifier: Sender<Result<()>>) -> Result<()> {
            notifier.send(Ok(()))?;
            *self.notifier.lock().unwrap() = Some(notifier);
            Ok(())
        }
        fn stop(&mut self) -> Result<()> {
            let _ = self.notifier.lock().unwrap().take();
            Ok(())
        }
        fn extract_settings(&self, _: Settings) -> Result<SimpleSettings> {
            if self.random_config {
                Ok(SimpleSettings { cnt: self.last_cnt + 1 })
            } else {
                Ok(SimpleSettings { cnt: self.last_cnt })
            }
        }
    }

    #[test]
    fn lifecycle_shutdown() -> Result<()> {
        let (tx, rx) = channel::bounded(42);
        let (stx, _srx) = channel::bounded(42);
        let c = SimpleComponent::new(rx, Default::default());
        let h = c.spawn()?;
        tx.send(ComponentRequest::RegisterSupervisor(stx)).unwrap();
        tx.send(ComponentRequest::Shutdown(ShutdownReason::TriggeredByHost))?;
        h.join().unwrap();
        Ok(())
    }

    const _3SEC: Duration = Duration::from_secs(3);

    #[test]
    fn setup_start_shutdown() -> Result<()> {
        let (tx, rx) = channel::bounded(42);
        let c = SimpleComponent::new(rx, Default::default());
        let h = c.spawn()?;
        let (tx_supervisor, rx_supervisor) = channel::bounded(42);
        tx.send(ComponentRequest::RegisterSupervisor(tx_supervisor))?;

        // Start on initial config
        tx.send(ComponentRequest::SettingsChanged(Box::new(Settings::sample())))?;
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Starting)
        );
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Started)
        );

        // Shutdown and yield
        tx.send(ComponentRequest::Shutdown(ShutdownReason::TriggeredByHost))?;
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Stopped)
        );
        h.join().unwrap();
        Ok(())
    }

    #[test]
    fn setup_start_runtime_error() -> Result<()> {
        let (tx, rx) = channel::bounded(42);
        let err_notifier: Arc<Mutex<_>> = Default::default();
        let c = SimpleComponent::new(rx, err_notifier.clone());
        let h = c.spawn()?;
        let (tx_supervisor, rx_supervisor) = channel::bounded(42);
        tx.send(ComponentRequest::RegisterSupervisor(tx_supervisor))?;

        // Start on initial config
        tx.send(ComponentRequest::SettingsChanged(Box::new(Settings::sample())))?;
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Starting)
        );
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Started)
        );

        // trigger runtime error
        err_notifier
            .lock()
            .unwrap()
            .as_ref()
            .unwrap()
            .send(Err(anyhow::anyhow!("Sad cat is sad :-(")))
            .unwrap();
        if let (ComponentType(t), ComponentState::Errored { .. }) = rx_supervisor.recv_timeout(_3SEC)? {
            assert_eq!(t, "test");
        } else {
            panic!()
        };

        // Shutdown and yield
        tx.send(ComponentRequest::Shutdown(ShutdownReason::TriggeredByHost))?;
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Stopped)
        );
        h.join().unwrap();
        Ok(())
    }

    #[test]
    fn setup_start_configchange_shutdown() -> Result<()> {
        let (tx, rx) = channel::bounded(42);
        let c = SimpleComponent::new(rx, Default::default());
        let h = c.spawn()?;
        let (tx_supervisor, rx_supervisor) = channel::bounded(42);
        tx.send(ComponentRequest::RegisterSupervisor(tx_supervisor))?;

        // Start on initial config
        tx.send(ComponentRequest::SettingsChanged(Box::new(Settings::sample())))?;
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Starting)
        );
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Started)
        );

        // Don't restart on unchanged config
        tx.send(ComponentRequest::SettingsChanged(Box::new(Settings::sample())))?;
        assert!(rx_supervisor.recv_timeout(Duration::from_secs(1)).is_err());

        // Restart on changed config
        tx.send(ComponentRequest::Individual(SimpleRequest::ToggleRandomConfigCreation))?;
        tx.send(ComponentRequest::SettingsChanged(Box::new(Settings::sample())))?;
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Stopped)
        );
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Starting)
        );
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Started)
        );

        // Shutdown and yield
        tx.send(ComponentRequest::Shutdown(ShutdownReason::TriggeredByHost))?;
        assert_eq!(
            rx_supervisor.recv_timeout(_3SEC)?,
            ("test".into(), ComponentState::Stopped)
        );
        h.join().unwrap();
        Ok(())
    }

    #[test]
    fn respond_to_individual_request() -> Result<()> {
        let (tx, rx) = channel::bounded(42);
        let (stx, _srx) = channel::bounded(42);
        let c = SimpleComponent::new(rx, Default::default());
        let h = c.spawn()?;
        tx.send(ComponentRequest::RegisterSupervisor(stx)).unwrap();

        let (pong_tx, pong_rx) = channel::bounded(1);

        tx.send(ComponentRequest::Individual(SimpleRequest::Ping(pong_tx)))?;
        assert!(pong_rx.recv_timeout(_3SEC).is_ok());
        assert!(pong_rx.try_recv().is_err());

        // Shutdown and yield
        tx.send(ComponentRequest::Shutdown(ShutdownReason::TriggeredByHost))?;
        h.join().unwrap();
        Ok(())
    }
}