kompact 0.11.3

Kompact is a Rust implementation of the Kompics component model combined with the Actor model.
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
use super::*;

use crate::net::buffers::{BufferConfig, ChunkAllocator, ChunkRef};
use std::task::Poll;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub(super) enum StateTransition {
    #[default]
    Active,
    Passive,
    Destroyed,
}

#[derive(Debug)]
struct BlockingState {
    future: BlockingFuture,
    unblock_state: StateTransition,
}

/// The contextual object for a Kompact component
///
/// Gives access compact internal features like
/// timers, logging, confguration, an the self reference.
pub struct ComponentContext<CD: ComponentTraits> {
    inner: Option<ComponentContextInner<CD>>,
    buffer: RefCell<Option<EncodeBuffer>>,
    blocking_future: Option<BlockingState>,
    pub(super) non_blocking_futures: FxHashMap<Uuid, NonBlockingFuture>,
}

struct ComponentContextInner<CD: ComponentTraits> {
    timer_manager: TimerManager<CD>,
    pub(super) component: Weak<Component<CD>>,
    logger: KompactLogger,
    actor_ref: ActorRef<CD::Message>,
    config: Arc<Hocon>,
    id: Uuid,
}

impl<CD> ComponentContext<CD>
where
    CD: ComponentTraits + ComponentLifecycle,
{
    /// Create a new, uninitialised component context
    ///
    /// # Note
    ///
    /// Nothing in this context may be used *before* the parent component is actually initialised!
    pub fn uninitialised() -> ComponentContext<CD> {
        ComponentContext {
            inner: None,
            buffer: RefCell::new(None),
            blocking_future: None,
            non_blocking_futures: FxHashMap::default(),
        }
    }

    /// Initialise the component context with the actual component instance
    ///
    /// This *must* be invoked from [setup](ComponentDefinition::setup).
    pub fn initialise(&mut self, c: Arc<Component<CD>>) -> () {
        let system = c.system();
        let id = c.id();
        let inner = ComponentContextInner {
            timer_manager: TimerManager::new(system.timer_ref()),
            component: Arc::downgrade(&c),
            logger: c.logger().new(o!("ctype" => CD::type_name())),
            actor_ref: c.actor_ref(),
            config: system.config_owned(),
            id,
        };
        self.inner = Some(inner);
        trace!(self.log(), "Initialised.");
    }

    fn inner_ref(&self) -> &ComponentContextInner<CD> {
        match self.inner {
            Some(ref c) => c,
            None => panic!("Component improperly initialised!"),
        }
    }

    fn inner_mut(&mut self) -> &mut ComponentContextInner<CD> {
        match self.inner {
            Some(ref mut c) => c,
            None => panic!("Component improperly initialised!"),
        }
    }

    /// The components logger instance
    ///
    /// This instance will already be preloaded
    /// with component specific information in the MDC.
    ///
    /// # Example
    ///
    /// ```
    /// use kompact::prelude::*;
    ///
    /// #[derive(ComponentDefinition, Actor)]
    /// struct HelloLogging {
    ///    ctx: ComponentContext<Self>,
    /// }
    /// impl HelloLogging {
    ///     fn new() -> HelloLogging {
    ///         HelloLogging {
    ///             ctx: ComponentContext::uninitialised(),
    ///         }
    ///     }    
    /// }
    /// impl ComponentLifecycle for HelloLogging {
    ///     fn on_start(&mut self) -> Handled {
    ///         info!(self.ctx().log(), "Hello Start event");
    ///         self.ctx().system().shutdown_async();
    ///         Handled::Ok
    ///     }    
    /// }
    ///
    /// let system = KompactConfig::default().build().expect("system");
    /// let c = system.create(HelloLogging::new);
    /// system.start(&c);
    /// system.await_termination();
    /// ```
    pub fn log(&self) -> &KompactLogger {
        &self.inner_ref().logger
    }

    /// Get a reference to the system configuration
    ///
    /// Use [load_config_str](KompactConfig::load_config_str) or
    /// or [load_config_file](KompactConfig::load_config_file)
    /// to load values into the config object.
    ///
    /// # Example
    ///
    /// ```
    /// use kompact::prelude::*;
    ///
    /// #[derive(ComponentDefinition, Actor)]
    /// struct ConfigComponent {
    ///    ctx: ComponentContext<Self>,
    /// }
    /// impl ConfigComponent {
    ///     fn new() -> ConfigComponent {
    ///         ConfigComponent {
    ///             ctx: ComponentContext::uninitialised(),
    ///         }
    ///     }    
    /// }
    /// impl ComponentLifecycle for ConfigComponent {
    ///     fn on_start(&mut self) -> Handled {
    ///         assert_eq!(Some(7i64), self.ctx().config()["a"].as_i64());
    ///         self.ctx().system().shutdown_async();
    ///         Handled::Ok
    ///     }    
    /// }
    /// let default_values = r#"{ a = 7 }"#;
    /// let mut conf = KompactConfig::default();
    /// conf.load_config_str(default_values);
    /// let system = conf.build().expect("system");
    /// let c = system.create(ConfigComponent::new);
    /// system.start(&c);
    /// system.await_termination();
    /// ```
    pub fn config(&self) -> &Hocon {
        self.inner_ref().config.as_ref()
    }

    pub(crate) fn timer_manager_mut(&mut self) -> &mut TimerManager<CD> {
        &mut self.inner_mut().timer_manager
    }

    pub(super) fn set_blocking_with_state(
        &mut self,
        future: BlockingFuture,
        unblock_state: StateTransition,
    ) {
        let blocking_state = BlockingState {
            future,
            unblock_state,
        };
        assert!(
            self.blocking_future.replace(blocking_state).is_none(),
            "Replacing a blocking future without completing it first is invalid!"
        );
        let component = self.typed_component();
        component.set_blocking();
    }

    /// Sets the component to block on the provided blocking `future`
    ///
    /// This should *only* be used when implementing custom [execute](ComponentDefinition::execute) logic!
    /// Otherwise the correct way to block is to return the [Handled::BlockOn](Handled::BlockOn) variant from a handler.
    ///
    /// If this is used for custom [execute](ComponentDefinition::execute) logic, then the next call
    /// should be a `return ExecuteResult::new(true, count, skip)`, as continuing to execute handlers violates
    /// blocking semantics.
    pub fn set_blocking(&mut self, future: BlockingFuture) {
        self.set_blocking_with_state(future, StateTransition::default())
    }

    /// Return `true` if the component is set up for blocking
    ///
    /// If this does return `true`, port handling **must** be immediately
    /// aborted and the returned result must have the form
    /// `ExecuteResult::new(true, count, skip)`.
    pub fn is_blocking(&self) -> bool {
        self.blocking_future.is_some()
    }

    pub(super) fn run_blocking_task(&mut self) -> SchedulingDecision {
        let mut blocking_state = self
            .blocking_future
            .take()
            .expect("Called run_blocking while not blocking!");
        let component = self.typed_component();
        match blocking_state.future.run(&component) {
            BlockingRunResult::BlockOn(f) => {
                blocking_state.future = f;
                assert!(
                    self.blocking_future.replace(blocking_state).is_none(),
                    "Replacing a blocking future without completing it first is invalid!"
                );
                SchedulingDecision::Blocked
            }
            BlockingRunResult::Unblock => {
                assert!(
                    self.blocking_future.is_none(),
                    "Don't block within a blocking future! Just call await on the future instead."
                );
                match blocking_state.unblock_state {
                    StateTransition::Active => component.set_active(),
                    StateTransition::Passive => component.set_passive(),
                    StateTransition::Destroyed => component.set_destroyed(),
                }
                // Don't change this to resume!
                // Merge it with a new read on the work count,
                // to catch changes during running of the handler!
                SchedulingDecision::NoWork
            }
        }
    }

    pub(super) fn run_nonblocking_task(&mut self, tag: Uuid) -> Handled {
        if let Some(future) = self.non_blocking_futures.get_mut(&tag) {
            match future.run() {
                Poll::Pending => Handled::Ok,
                Poll::Ready(handled) => {
                    self.non_blocking_futures.remove(&tag);
                    handled
                }
            }
        } else {
            warn!(self.log(), "Future with tag {} was scheduled but not available. May have been scheduled after completion.", tag);
            Handled::Ok
        }
    }

    pub(crate) fn context_system(&self) -> ContextSystemHandle {
        ContextSystemHandle::from(self.component())
    }

    /// Returns the component instance wrapping this component definition
    ///
    /// This is mostly meant to be passed along for scheduling or registrations.
    /// Don't try to lock anything on the thread already executing the component!
    pub fn typed_component(&self) -> Arc<Component<CD>> {
        match self.inner_ref().component.upgrade() {
            Some(ac) => ac,
            None => panic!("Component already deallocated!"),
        }
    }

    /// Returns the component instance wrapping this component definition
    ///
    /// This is mostly meant to be passed along for scheduling and the like.
    /// Don't try to lock anything on the thread already executing the component!
    pub fn component(&self) -> Arc<dyn CoreContainer> {
        self.typed_component()
    }

    /// Returns a handle to the Kompact system this component is a part of
    pub fn system(&self) -> impl SystemHandle {
        self.context_system()
    }

    /// Returns a reference to the system dispatcher
    pub fn dispatcher_ref(&self) -> DispatcherRef {
        self.system().dispatcher_ref()
    }

    /// Returns a reference to the system's deadletter box
    pub fn deadletter_ref(&self) -> ActorRef<Never> {
        self.system().deadletter_ref()
    }

    /// Returns a reference to this components unique id
    pub fn id(&self) -> &Uuid {
        &self.inner_ref().id
    }

    /// Destroys this component lazily
    ///
    /// This simply sends a `Kill` event to itself,
    /// which means that other events may still be handled
    /// before the component is actually killed.
    ///
    /// For a more immediate alternative
    /// see [Handled::DieNow](Handled::DieNow).
    pub fn suicide(&self) -> () {
        self.component().enqueue_control(ControlEvent::Kill);
    }

    pub(crate) fn with_buffer<R>(&self, f: impl FnOnce(&mut EncodeBuffer) -> R) -> R {
        {
            // Scoping the borrow
            if let Some(buffer) = self.buffer.borrow_mut().as_mut() {
                return f(buffer);
            }
        }
        self.init_buffers(None, None);
        self.with_buffer(f)
    }

    /// Attempts to create a [ChunkRef](net::buffers::ChunkRef) of the data using the local
    /// [EncodeBuffer](net::buffers::EncodeBuffer), to be used with
    /// [tell_preserialised](actors::ActorPath#method.tell_preserialised)
    pub fn preserialise<B>(&self, content: &B) -> Result<ChunkRef, SerError>
    where
        B: Serialisable + Sized,
    {
        {
            // Scoping the borrow
            if let Some(buffer) = self.buffer.borrow_mut().as_mut() {
                return crate::ser_helpers::preserialise_msg(
                    content,
                    &mut buffer.get_buffer_encoder()?,
                );
            }
        }
        self.init_buffers(None, None);
        self.preserialise(content)
    }

    /// May be used for manual initialization of a components local
    /// [EncodeBuffer](net::buffers::EncodeBuffer). If this method is never called explicitly
    /// the actor will implicitly call it when it tries to serialise its first message.
    ///
    /// If buffers have already been initialized, explicitly or implicitly, the method does nothing.
    ///
    /// A custom [BufferConfig](net::buffers::BufferConfig) may be specified, if `None` is given the
    /// actor will first try to parse the configuration from the system wide `Hocon`-Configuration,
    /// if that fails, the default config will be used.
    ///
    /// A custom [ChunkAllocator](net::buffers::ChunkAllocator) may also be specified.
    /// if `None` is given the actor will use the default allocator.
    ///
    /// Note: The `BufferConfig` within the systems [NetworkConfig](dispatch::NetworkConfig) never
    /// affects the Actors Buffers (i.e. this method).
    pub fn init_buffers(
        &self,
        buffer_config: Option<BufferConfig>,
        custom_allocator: Option<Arc<dyn ChunkAllocator>>,
    ) -> () {
        let mut buffer_location = self.buffer.borrow_mut();

        if buffer_location.as_mut().is_none() {
            // Need to create a new Buffer, fetch BufferConfig
            let cfg = {
                if let Some(cfg) = buffer_config {
                    cfg
                } else {
                    self.get_buffer_config()
                }
            };

            if custom_allocator.is_some() {
                debug!(
                    self.log(),
                    "init_buffers with custom allocator, config {:?}.", &cfg
                );
            } else {
                debug!(
                    self.log(),
                    "init_buffers with default allocator, config {:?}.", &cfg
                );
            }

            let buffer =
                EncodeBuffer::with_dispatcher_ref(self.dispatcher_ref(), &cfg, custom_allocator);
            *buffer_location = Some(buffer);
        }
    }

    /// Returns a BufferConfig from the global configuration or the default configuration.
    fn get_buffer_config(&self) -> BufferConfig {
        BufferConfig::from_config(self.config())
    }

    /// We use this method for assertions in tests
    #[allow(dead_code)]
    pub(crate) fn get_buffer_location(&self) -> &RefCell<Option<EncodeBuffer>> {
        &self.buffer
    }

    /// Set the recovery function for this component
    ///
    /// See [RecoveryHandler](crate::prelude::RecoveryHandler) for more information.
    ///
    /// You can perform action repeatedly in order to store state
    /// to use while recovering from a failure.
    /// Do note, however, that this call causes an additional mutex lock
    /// which is not necessarily cheap and can theoretically deadlock,
    /// if you call this from more than one place.
    pub fn set_recovery_function<F>(&self, f: F) -> ()
    where
        F: FnOnce(FaultContext) -> RecoveryHandler + Send + 'static,
    {
        self.typed_component().set_recovery_function(f);
    }
}

impl<CD> ActorRefFactory for ComponentContext<CD>
where
    CD: ComponentTraits + ComponentLifecycle,
{
    type Message = CD::Message;

    fn actor_ref(&self) -> ActorRef<CD::Message> {
        self.inner_ref().actor_ref.clone()
    }
}

impl<CD> ActorPathFactory for ComponentContext<CD>
where
    CD: ComponentTraits + ComponentLifecycle,
{
    fn actor_path(&self) -> ActorPath {
        let id = *self.id();
        ActorPath::Unique(UniquePath::with_system(self.system().system_path(), id))
    }
}