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
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
//! Support for routing groups and policies

use crate::{
    actors::{ActorPath, DynActorRef},
    messaging::NetMessage,
    utils::IterExtras,
    KompactLogger,
};
#[allow(unused_imports)]
use slog::{crit, debug, error, info, trace, warn};
use std::{
    fmt,
    hash::{BuildHasher, BuildHasherDefault, Hash},
    ops::Deref,
    sync::atomic::{AtomicUsize, Ordering},
};

/// The default policy associated with the broadcast marker
pub static DEFAULT_BROADCAST_POLICY: BroadcastRouting = BroadcastRouting;

/// The default hasher builder
///
/// This is equivalent to `HasherBuilderDefault<std::collections::hash_map::DefaultHasher>`
/// but it is statically assignable.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DefaultHasherBuilder;
impl BuildHasher for DefaultHasherBuilder {
    type Hasher = std::collections::hash_map::DefaultHasher;

    fn build_hasher(&self) -> Self::Hasher {
        std::collections::hash_map::DefaultHasher::default()
    }
}

/// The default policy associated with the select marker
pub static DEFAULT_SELECT_POLICY: SenderHashBucketRouting<DefaultHasherBuilder> =
    FieldHashBucketRouting {
        hasher_builder: DefaultHasherBuilder,
        field_extractor: NetMessage::sender,
    };

/// Cloneable [RoutingPolicy](RoutingPolicy) wrapper used inside the `ActorStore`
#[derive(Debug)]
pub struct StorePolicy(Box<dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync>);
impl StorePolicy {
    /// Wrap a boxed [RoutingPolicy](RoutingPolicy)
    pub fn new(policy: Box<dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync>) -> Self {
        StorePolicy(policy)
    }
}
impl Clone for StorePolicy {
    fn clone(&self) -> Self {
        StorePolicy(self.0.boxed_clone())
    }
}
impl Deref for StorePolicy {
    type Target = dyn RoutingPolicy<DynActorRef, NetMessage>;

    fn deref(&self) -> &Self::Target {
        self.0.deref()
    }
}
impl<R> From<R> for StorePolicy
where
    R: RoutingPolicy<DynActorRef, NetMessage> + Send + Sync + 'static,
{
    fn from(policy: R) -> Self {
        let boxed = Box::new(policy);
        Self::new(boxed)
    }
}

/// A routing group keeps group membership together with a routing policy
#[derive(Debug)]
pub struct RoutingGroup<'a> {
    members: Vec<&'a DynActorRef>,
    policy: &'a dyn RoutingPolicy<DynActorRef, NetMessage>,
}
impl<'a> RoutingGroup<'a> {
    /// Create a new routing group
    pub fn new(
        members: Vec<&'a DynActorRef>,
        policy: &'a dyn RoutingPolicy<DynActorRef, NetMessage>,
    ) -> Self {
        RoutingGroup { members, policy }
    }

    /// Route `msg` to our members as instructed by our policy
    pub fn route(&self, msg: NetMessage, logger: &KompactLogger) {
        let members: &[&DynActorRef] = &self.members;
        self.policy.route(members, msg, logger);
    }
}

/// A routing policy described how to route a given `msg` over
/// a given set of `members`
pub trait RoutingPolicy<Ref, M>: fmt::Debug {
    /// Route the `msg` to the appropriate `members`
    ///
    /// A logger should be provided to allow debugging and handle potential routing errors.
    fn route(&self, members: &[&Ref], msg: M, logger: &KompactLogger);

    /// Create a boxed copy of this policy
    ///
    /// Used to make trait objects of this policy cloneable
    fn boxed_clone(&self) -> Box<dyn RoutingPolicy<Ref, M> + Send + Sync>;

    /// Provide the broadcast part of this policy, if any
    fn broadcast(&self) -> Option<&(dyn RoutingPolicy<Ref, M> + Send + Sync)>;

    /// Provide the select part of this policy, if any
    fn select(&self) -> Option<&(dyn RoutingPolicy<Ref, M> + Send + Sync)>;
}

/// Round-robin dispatch policy
///
/// This policy can handle changing membership,
/// but won't start a new round immediately on change.
#[derive(Debug)]
pub struct RoundRobinRouting {
    offset: AtomicUsize,
}
impl RoundRobinRouting {
    /// Create a new instance starting at index 0
    pub fn new() -> Self {
        RoundRobinRouting {
            offset: AtomicUsize::new(0),
        }
    }

    /// Get and return the current index and increment it for the next invocation
    pub fn get_and_increment_index(&self, length: usize) -> usize {
        // fetch_add wraps around automatically
        self.offset.fetch_add(1, Ordering::Relaxed) % length
    }
}
impl Default for RoundRobinRouting {
    fn default() -> Self {
        Self::new()
    }
}
impl RoutingPolicy<DynActorRef, NetMessage> for RoundRobinRouting {
    fn route(&self, members: &[&DynActorRef], msg: NetMessage, logger: &KompactLogger) {
        let index = self.get_and_increment_index(members.len());
        trace!(logger, "Routing msg to member at index={}", index);
        members[index].tell(msg);
    }

    fn boxed_clone(&self) -> Box<dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync> {
        // just use the last value for the new router...it doesn't matter if there is some overlap between the two versions
        let offset = self.offset.load(Ordering::Relaxed);
        let routing = RoundRobinRouting {
            offset: AtomicUsize::new(offset),
        };
        Box::new(routing)
    }

    fn broadcast(&self) -> Option<&(dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync)> {
        None
    }

    fn select(&self) -> Option<&(dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync)> {
        Some(self)
    }
}

/// A router using consistent hashing on some field of the message
///
/// Changing the membership is supported,
/// but will result in bucket reassignments.
pub struct FieldHashBucketRouting<M, T: Hash, H: BuildHasher + Clone> {
    hasher_builder: H,
    field_extractor: fn(&M) -> &T,
}
impl<M, T: Hash, H: BuildHasher + Clone> Clone for FieldHashBucketRouting<M, T, H> {
    fn clone(&self) -> Self {
        FieldHashBucketRouting {
            hasher_builder: self.hasher_builder.clone(),
            field_extractor: self.field_extractor,
        }
    }
}
impl<M, T: Hash, H: BuildHasher + Clone + Copy> Copy for FieldHashBucketRouting<M, T, H> {}

impl<M, T: Hash, H: BuildHasher + Clone> fmt::Debug for FieldHashBucketRouting<M, T, H> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "FieldHashBucketRouting {{ hasher_builder: {}, field_extractor: {} }}",
            std::any::type_name::<H>(),
            std::any::type_name::<fn(&M) -> &T>()
        )
    }
}
impl<M, T: Hash, H: BuildHasher + Clone> FieldHashBucketRouting<M, T, H> {
    /// Create a new instance of this router using the provided hasher and field extractor function
    pub fn new(hasher_builder: H, field_extractor: fn(&M) -> &T) -> Self {
        FieldHashBucketRouting {
            hasher_builder,
            field_extractor,
        }
    }

    /// Hashes the `field` using this router's `Hasher`
    pub fn hash_field(&self, field: &T) -> u64 {
        self.hasher_builder.hash_one(field)
    }

    /// Extracts and hashes the `msg` field using this router's `Hasher`
    pub fn hash_message(&self, msg: &M) -> u64 {
        let field_ref = (self.field_extractor)(msg);
        self.hash_field(field_ref)
    }

    /// Provides the bucket index for the `msg` for `length` buckets
    ///
    /// It does so by calling [hash_message](Self::hash_message) internally
    /// and then using modulo arithmentic to decide a bucket from the hash.
    pub fn get_bucket(&self, msg: &M, length: usize) -> usize {
        let hash = self.hash_message(msg);
        let index = hash % (length as u64);
        index as usize
    }
}

/// A hash bucket router using the `sender` field of a `NetMessage` to decide the bucket
pub type SenderHashBucketRouting<H> = FieldHashBucketRouting<NetMessage, ActorPath, H>;
/// A hash bucket router using the `sender` field of a `NetMessage` to decide the bucket
///
/// Uses Rust's default hasher.
pub type SenderDefaultHashBucketRouting =
    SenderHashBucketRouting<BuildHasherDefault<std::collections::hash_map::DefaultHasher>>;

impl<H: BuildHasher + Default + Clone> Default for SenderHashBucketRouting<H> {
    fn default() -> Self {
        let hasher_builder = H::default();
        Self::new(hasher_builder, NetMessage::sender)
    }
}

impl<H: BuildHasher + Clone + Send + Sync + 'static> RoutingPolicy<DynActorRef, NetMessage>
    for SenderHashBucketRouting<H>
{
    fn route(&self, members: &[&DynActorRef], msg: NetMessage, logger: &KompactLogger) {
        let index = self.get_bucket(&msg, members.len());
        trace!(
            logger,
            "Routing msg with sender={} to member at index={}",
            msg.sender,
            index
        );
        members[index].tell(msg);
    }

    fn boxed_clone(&self) -> Box<dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync> {
        let cloned: SenderHashBucketRouting<H> = self.clone();
        Box::new(cloned)
    }

    fn broadcast(&self) -> Option<&(dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync)> {
        None
    }

    fn select(&self) -> Option<&(dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync)> {
        Some(self)
    }
}

/// A router that simply hands a copy of the message to every member
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct BroadcastRouting;
impl Default for BroadcastRouting {
    fn default() -> Self {
        BroadcastRouting
    }
}
impl RoutingPolicy<DynActorRef, NetMessage> for BroadcastRouting {
    fn route(&self, members: &[&DynActorRef], msg: NetMessage, logger: &KompactLogger) {
        trace!(logger, "Trying to broadcast message: {:?}", msg);
        let res = members
            .iter()
            .for_each_try_with(msg, |member, my_msg| member.tell(my_msg));
        match res {
            Ok(_) => trace!(logger, "The message was broadcast."),
            Err(e) => error!(logger, "Could not broadcast a message! Error was: {}", e),
        }
    }

    fn boxed_clone(&self) -> Box<dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync> {
        Box::new(*self)
    }

    fn broadcast(&self) -> Option<&(dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync)> {
        Some(self)
    }

    fn select(&self) -> Option<&(dyn RoutingPolicy<DynActorRef, NetMessage> + Send + Sync)> {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{prelude::*, routing::test_helpers::*};

    const GROUP_SIZE: usize = 3;
    const NUM_MESSAGES: usize = 30;
    // TODO: Replace the sleeps
    const SLEEP_TIME: Duration = Duration::from_millis(1000);

    #[test]
    fn router_debug() {
        {
            let router = RoundRobinRouting::default();
            println!("Router: {:?}", router);
            let group = RoutingGroup::new(Vec::new(), &router);
            println!("Group: {:?}", group);
        }
        {
            let router = SenderDefaultHashBucketRouting::default();
            println!("Router: {:?}", router);
            let group = RoutingGroup::new(Vec::new(), &router);
            println!("Group: {:?}", group);
        }
        {
            let router = BroadcastRouting;
            println!("Router: {:?}", router);
            let group = RoutingGroup::new(Vec::new(), &router);
            println!("Group: {:?}", group);
        }
    }

    #[test]
    fn round_robin_routing() {
        let system = KompactConfig::default().build().expect("system");

        let receivers: Vec<Arc<Component<ReceiverComponent>>> = (0..GROUP_SIZE)
            .map(|_i| system.create(ReceiverComponent::default))
            .collect();
        let receiver_refs: Vec<DynActorRef> =
            receivers.iter().map(|c| c.actor_ref().dyn_ref()).collect();
        receivers.iter().for_each(|c| system.start(c));

        let router = RoundRobinRouting::default();
        let group = RoutingGroup::new(receiver_refs.iter().collect(), &router);
        let group_ref: ActorPath = NamedPath::with_system(
            system.system_path(),
            vec!["routing_group".to_string(), "?".to_string()],
        )
        .into();
        let source_ref = system.deadletter_path();

        let msg = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(1, total_count(&receivers));
        let counts = individual_count(&receivers);
        assert_eq!(1, counts[0]);
        assert_eq!(0, counts[1]);
        assert_eq!(0, counts[2]);

        let msg2 = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg2, system.logger());
        let msg3 = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg3, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(3, total_count(&receivers));
        let counts = individual_count(&receivers);
        assert_eq!(1, counts[0]);
        assert_eq!(1, counts[1]);
        assert_eq!(1, counts[2]);

        let msg4 = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg4, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(4, total_count(&receivers));
        let counts = individual_count(&receivers);
        assert_eq!(2, counts[0]);
        assert_eq!(1, counts[1]);
        assert_eq!(1, counts[2]);

        for _i in 0..NUM_MESSAGES {
            let msg = NetMessage::with_box(
                CountMe::SER_ID,
                source_ref.clone(),
                group_ref.clone(),
                Box::new(CountMe),
            );
            group.route(msg, system.logger());
        }
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(NUM_MESSAGES + 4, total_count(&receivers));

        system.shutdown().expect("shutdown");
    }

    #[test]
    fn sender_hash_routing() {
        let system = KompactConfig::default().build().expect("system");

        let receivers: Vec<Arc<Component<ReceiverComponent>>> = (0..GROUP_SIZE)
            .map(|_i| system.create(ReceiverComponent::default))
            .collect();
        let receiver_refs: Vec<DynActorRef> =
            receivers.iter().map(|c| c.actor_ref().dyn_ref()).collect();
        receivers.iter().for_each(|c| system.start(c));

        let router = SenderDefaultHashBucketRouting::default();
        let group = RoutingGroup::new(receiver_refs.iter().collect(), &router);
        let group_ref: ActorPath = NamedPath::with_system(
            system.system_path(),
            vec!["routing_group".to_string(), "?".to_string()],
        )
        .into();
        let source_ref = system.deadletter_path();

        let msg = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(1, total_count(&receivers));

        let msg2 = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg2, system.logger());
        let msg3 = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref,
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg3, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(3, total_count(&receivers));
        let counts = individual_count(&receivers);
        assert!(counts.iter().any(|&v| v == 3));

        let other_source_ref: ActorPath = system
            .system_path()
            .into_named_with_string("othersource")
            .expect("actor path")
            .into();

        let msg4 = NetMessage::with_box(
            CountMe::SER_ID,
            other_source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg4, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(4, total_count(&receivers));
        let counts = individual_count(&receivers);
        // they could technically end up in the same bucket,
        // just try to pick a name above where they don't
        assert!(counts.iter().any(|&v| v == 3));
        assert!(counts.iter().any(|&v| v == 1));

        for _i in 0..NUM_MESSAGES {
            let msg = NetMessage::with_box(
                CountMe::SER_ID,
                other_source_ref.clone(),
                group_ref.clone(),
                Box::new(CountMe),
            );
            group.route(msg, system.logger());
        }
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(NUM_MESSAGES + 4, total_count(&receivers));
        let counts = individual_count(&receivers);
        // they could technically end up in the same bucket,
        // just try to pick a name above where they don't
        assert!(counts.iter().any(|&v| v == 3));
        assert!(counts.iter().any(|&v| v == NUM_MESSAGES + 1));

        system.shutdown().expect("shutdown");
    }

    #[test]
    fn broadcast_routing() {
        let system = KompactConfig::default().build().expect("system");

        let receivers: Vec<Arc<Component<ReceiverComponent>>> = (0..GROUP_SIZE)
            .map(|_i| system.create(ReceiverComponent::default))
            .collect();
        let receiver_refs: Vec<DynActorRef> =
            receivers.iter().map(|c| c.actor_ref().dyn_ref()).collect();
        receivers.iter().for_each(|c| system.start(c));

        let router = BroadcastRouting;
        let group = RoutingGroup::new(receiver_refs.iter().collect(), &router);
        let group_ref: ActorPath = NamedPath::with_system(
            system.system_path(),
            vec!["routing_group".to_string(), "*".to_string()],
        )
        .into();
        let source_ref = system.deadletter_path();

        let msg = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(3, total_count(&receivers));
        let counts = individual_count(&receivers);
        assert_eq!(1, counts[0]);
        assert_eq!(1, counts[1]);
        assert_eq!(1, counts[2]);

        let msg2 = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg2, system.logger());
        let msg3 = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg3, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(9, total_count(&receivers));
        let counts = individual_count(&receivers);
        assert_eq!(3, counts[0]);
        assert_eq!(3, counts[1]);
        assert_eq!(3, counts[2]);

        let msg4 = NetMessage::with_box(
            CountMe::SER_ID,
            source_ref.clone(),
            group_ref.clone(),
            Box::new(CountMe),
        );
        group.route(msg4, system.logger());
        std::thread::sleep(SLEEP_TIME);
        assert_eq!(12, total_count(&receivers));
        let counts = individual_count(&receivers);
        assert_eq!(4, counts[0]);
        assert_eq!(4, counts[1]);
        assert_eq!(4, counts[2]);

        for _i in 0..NUM_MESSAGES {
            let msg = NetMessage::with_box(
                CountMe::SER_ID,
                source_ref.clone(),
                group_ref.clone(),
                Box::new(CountMe),
            );
            group.route(msg, system.logger());
        }
        std::thread::sleep(SLEEP_TIME);
        assert_eq!((NUM_MESSAGES + 4) * GROUP_SIZE, total_count(&receivers));
        let counts = individual_count(&receivers);
        assert_eq!(NUM_MESSAGES + 4, counts[0]);
        assert_eq!(NUM_MESSAGES + 4, counts[1]);
        assert_eq!(NUM_MESSAGES + 4, counts[2]);

        system.shutdown().expect("shutdown");
    }
}