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
use std::thread;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::ops::Deref;

use config::Config;
use log::trace;

use crate::protocol::*;
use crate::system::LogEntry;
use crate::actor::{Actor, BoxActor, ActorRef, ActorUri, ActorId};
use crate::actor::{ActorCell, Context, CellPublic, CellInternal, dead_letter, SysTell};
use crate::kernel::{KernelRef, QueueWriter, QueueReader, queue};
use crate::kernel::queue::{EnqueueResult, QueueEmpty};

#[derive(Clone)]
pub struct MailboxSender<Msg: Message> {
    pub uid: ActorId,
    queue: QueueWriter<Msg>,
    sys_queue: QueueWriter<Msg>,
    scheduled: Arc<AtomicBool>,
}

impl<Msg> MailboxSender<Msg>
    where Msg: Message {
    
    pub fn try_enqueue(&self, msg: Enqueued<Msg>) -> EnqueueResult<Msg> {
        self.queue.try_enqueue(msg)
    }

    pub fn try_sys_enqueue(&self, msg: Enqueued<Msg>) -> EnqueueResult<Msg> {
        self.sys_queue.try_enqueue(msg)
    }

    pub fn is_scheduled(&self) -> bool {
        self.scheduled.load(Ordering::Relaxed)
    }
}

unsafe impl<Msg: Message> Send for MailboxSender<Msg> {}
unsafe impl<Msg: Message> Sync for MailboxSender<Msg> {}

#[derive(Clone)]
pub struct Mailbox<Msg: Message> {
    inner: Arc<MailboxInner<Msg>>,
}

pub struct MailboxInner<Msg: Message> {
    uid: ActorId,
    msg_process_limit: u32,
    queue: QueueReader<Msg>,
    sys_queue: QueueReader<Msg>,
    kernel: KernelRef<Msg>,
    suspended: Arc<AtomicBool>,
    scheduled: Arc<AtomicBool>,
}

impl<Msg: Message> Mailbox<Msg> {
    pub fn dequeue(&self) -> Enqueued<Msg> {
        self.inner.queue.dequeue()
    }

    pub fn try_dequeue(&self) -> Result<Enqueued<Msg>, QueueEmpty> {
        self.inner.queue.try_dequeue()
    }

    pub fn sys_try_dequeue(&self) -> Result<Enqueued<Msg>, QueueEmpty> {
        self.inner.sys_queue.try_dequeue()
    }

    pub fn has_msgs(&self) -> bool {
        self.inner.queue.has_msgs()
    }

    pub fn has_sys_msgs(&self) -> bool {
        self.inner.sys_queue.has_msgs()
    }

    fn set_scheduled(&self, b: bool) {
        self.inner.scheduled.store(b, Ordering::Relaxed);
    }

    pub fn is_scheduled(&self) -> bool {
        self.inner.scheduled.load(Ordering::Relaxed)
    }

    fn set_suspended(&self, b: bool) {
        self.inner.suspended.store(b, Ordering::Relaxed);
    }

    fn is_suspended(&self) -> bool {
        self.inner.suspended.load(Ordering::Relaxed)
    }

    fn msg_process_limit(&self) -> u32 {
        self.inner.msg_process_limit
    }
}

pub trait MailboxSchedule {
    type Msg: Message;

    fn uid(&self) -> ActorId;

    fn set_scheduled(&self, b: bool);
}

impl<Msg> MailboxSchedule for MailboxSender<Msg>
    where Msg: Message
{
    type Msg = Msg;

    fn uid(&self) -> ActorId {
        self.uid
    }

    fn set_scheduled(&self, b: bool) {
        self.scheduled.store(b, Ordering::Relaxed);
    }
}

impl<Msg> MailboxSchedule for Mailbox<Msg>
    where Msg: Message
{
    type Msg = Msg;

    fn uid(&self) -> ActorId {
        self.inner.uid
    }

    fn set_scheduled(&self, b: bool) {
        self.inner.scheduled.store(b, Ordering::Relaxed);
    }
}

pub fn mailbox<Msg>(uid: ActorId,
                    msg_process_limit: u32,
                    kernel: KernelRef<Msg>)
                    -> (MailboxSender<Msg>, Mailbox<Msg>)
    where Msg: Message
{
    let (qw, qr) = queue::<Msg>();
    let (sqw, sqr) = queue::<Msg>();

    let scheduled = Arc::new(AtomicBool::new(false));

    let sender = MailboxSender {
        uid,
        queue: qw,
        sys_queue: sqw,
        scheduled: scheduled.clone()
    };

    let mailbox = MailboxInner {
        uid,
        msg_process_limit,
        queue: qr,
        sys_queue: sqr,
        kernel,
        suspended: Arc::new(AtomicBool::new(true)), //todo this can't be a bool?
        scheduled
    };

    let mailbox = Mailbox {
        inner: Arc::new(mailbox)
    };

    (sender, mailbox)
}

pub fn run_mailbox<Msg>(mbox: Mailbox<Msg>,
                        cell: ActorCell<Msg>,
                        mut actor: Option<BoxActor<Msg>>)
    where Msg: Message
{
    let c = &cell;
    let ctx: Context<Msg> = c.into();

    process_sys_msgs(&mbox, &cell, &ctx, &mut actor);

    if actor.is_some() && !mbox.is_suspended() {
        process_msgs(&mbox, &cell, &ctx, &mut actor);
    }

    mbox.inner.kernel.park_actor(mbox.inner.uid, actor);
    mbox.set_scheduled(false);

    if (mbox.has_msgs() && !mbox.is_suspended() && !mbox.is_scheduled())|| mbox.has_sys_msgs() {
        mbox.inner.kernel.schedule_actor(&mbox);
    }
}

fn process_msgs<Msg>(mbox: &Mailbox<Msg>,
                    cell: &ActorCell<Msg>,
                    ctx: &Context<Msg>,
                    actor: &mut Option<BoxActor<Msg>>)
    where Msg: Message
{
    let mut count = 0;
    
    let _sentinel = Sentinel {
        parent: cell.parent(),
        actor: cell.myself(),
        mbox: mbox.clone(),
    };

    loop {
        if count < mbox.msg_process_limit() && !cell.is_persisting() {
            match mbox.try_dequeue() {
                Ok(msg) => {
                    handle_msg(msg, cell, &ctx, actor, mbox);
                    process_sys_msgs(mbox, cell, &ctx, actor);
                    count +=1;
                },
                Err(_) => {
                    break;
                }
            }
        } else {
            break;
        }
    }
}

fn process_sys_msgs<Msg>(mbox: &Mailbox<Msg>,
                        cell: &ActorCell<Msg>,
                        ctx: &Context<Msg>,
                        actor: &mut Option<BoxActor<Msg>>)
    where Msg: Message
{
    // All system messages are processed in this mailbox execution
    // and we prevent any new messages that have since been added to the queue
    // from being processed by staging them in a Vec.
    // This prevents during actor restart.
    let mut sys_messages: Vec<Enqueued<Msg>> = Vec::new();
    loop {
        match mbox.sys_try_dequeue() {
            Ok(sys_msg) => {
                sys_messages.push(sys_msg);
            }
            Err(_) => break
        }
    }

    for sys_msg in sys_messages.into_iter() {
        handle_msg(sys_msg, cell, ctx, actor, mbox);
    }
}

pub fn flush_to_deadletters<Msg>(mbox: &Mailbox<Msg>,
                                dl: &ActorRef<Msg>,
                                uri: &ActorUri)
    where Msg: Message
{
    loop {
        match mbox.try_dequeue() {
            Ok(msg) => {
                match msg {
                    Enqueued::ActorMsg(am) => {
                        if let ActorMsg::User(_) = am.msg {
                            // TODO candidate for improving code readability
                            let sp = am.sender.clone().map(|s| s.uri.path.deref().clone());
                            let mp = uri.path.deref().clone();
                            dead_letter(dl,
                                        sp,
                                        mp,
                                        am.msg);
                        }
                    }
                    _ => {} // TODO handle system messages?
                }
            },
            Err(_) => {
                break;
            }
        }
    }
}

fn handle_msg<Msg>(msg: Enqueued<Msg>,
                    cell: &ActorCell<Msg>,
                    ctx: &Context<Msg>,
                    actor: &mut Option<BoxActor<Msg>>,
                    mbox: &Mailbox<Msg>)
    where Msg: Message
{
    match msg {
        Enqueued::ActorMsg(envelope) => {
            if actor.is_some() {
                handle_actor_msg(envelope, cell, ctx, actor);
            }
        }
        Enqueued::SystemMsg(envelope) => handle_sys_msg(envelope, cell, ctx, actor, mbox)
    }
}

fn handle_actor_msg<Msg>(msg: Envelope<Msg>,
                        cell: &ActorCell<Msg>,
                        ctx: &Context<Msg>, 
                        actor: &mut Option<BoxActor<Msg>>)
    where Msg: Message
{
    match (msg.msg, msg.sender) {
        (ActorMsg::User(msg), sender) => actor.as_mut().unwrap().receive(ctx, msg, sender),
        (ActorMsg::Identify, sender) => handle_identify(sender, cell),
        (msg, sender) => actor.as_mut().unwrap().other_receive(ctx, msg, sender)
    }
}

fn handle_sys_msg<Msg>(msg: SystemEnvelope<Msg>,
                        cell: &ActorCell<Msg>,
                        ctx: &Context<Msg>,
                        actor: &mut Option<BoxActor<Msg>>,
                        mbox: &Mailbox<Msg>)
    where Msg: Message
{
    match msg.msg {
        SystemMsg::ActorInit => handle_init(cell, ctx, actor, mbox),
        SystemMsg::ActorCmd(cmd) => handle_cmd(cmd, cell, actor),
        SystemMsg::Event(ref evt) => handle_evt(evt.clone(), msg.clone(), cell, ctx, actor),
        SystemMsg::Failed(failed) => handle_failed(failed, cell, actor),
        SystemMsg::Persisted(evt, sender) => handle_persisted(evt, cell, ctx, actor, sender),
        SystemMsg::Replay(evts) => handle_replay(evts, cell, ctx, actor, mbox),
        SystemMsg::Log(entry) => handle_log_msg(entry, ctx, actor),
    }
}

fn handle_init<Msg>(cell: &ActorCell<Msg>,
                    ctx: &Context<Msg>,
                    actor: &mut Option<BoxActor<Msg>>,
                    mbox: &Mailbox<Msg>)
    where Msg: Message
{
    trace!("ACTOR INIT");
    actor.as_mut().unwrap().pre_start(ctx);

    // todo the intent here can be made clearer
    // if persistence is not configured then set as not suspended
    if cell.load_events(actor) {
        actor.as_mut().unwrap().post_start(ctx);
        mbox.set_suspended(false);
    }
}

fn handle_identify<Msg>(sender: Option<ActorRef<Msg>>,
                        cell: &ActorCell<Msg>)
    where Msg: Message
{
    trace!("ACTOR IDENTIFY");
    cell.identify(sender);
}

fn handle_cmd<Msg>(cmd: ActorCmd,
                    cell: &ActorCell<Msg>,
                    actor: &mut Option<BoxActor<Msg>>)
    where Msg: Message
{
    // trace!("{}: ACTOR CMD {:?}", cell.myself().uri.path(), cmd);
    cell.receive_cmd(cmd, actor);
}

fn handle_evt<Msg>(evt: SystemEvent<Msg>,
                    msg: SystemEnvelope<Msg>,
                    cell: &ActorCell<Msg>,
                    ctx: &Context<Msg>,
                    actor: &mut Option<BoxActor<Msg>>)
    where Msg: Message
{
    // trace!("{} ACTOR EVT {:?}", cell.into().path(), evt);
    if actor.is_some() {
        actor.as_mut().unwrap().system_receive(ctx, msg.msg, msg.sender);
    }
    
    if let SystemEvent::ActorTerminated(ref actor_ref) = evt {
        cell.death_watch(actor_ref, actor);
    }
}

fn handle_failed<Msg>(failed: ActorRef<Msg>,
                        cell: &ActorCell<Msg>,
                        actor: &mut Option<BoxActor<Msg>>)
    where Msg: Message
{
    trace!("ACTOR HANDLE FAILED");
    cell.handle_failure(failed, actor.as_mut().unwrap().supervisor_strategy())
}

fn handle_persisted<Msg>(evt: Msg,
                        cell: &ActorCell<Msg>,
                        ctx: &Context<Msg>,
                        actor: &mut Option<BoxActor<Msg>>,
                        sender: Option<ActorRef<Msg>>)
    where Msg: Message
{
    trace!("ACTOR HANDLE PERSISTED");
    cell.set_persisting(false);
    actor.as_mut().unwrap().apply_event(ctx, evt, sender);
}

fn handle_replay<Msg>(evts: Vec<Msg>,
                        cell: &ActorCell<Msg>,
                        ctx: &Context<Msg>,
                        actor: &mut Option<BoxActor<Msg>>,
                        mbox: &Mailbox<Msg>)
    where Msg: Message
{
    trace!("ACTOR REPLAY");

    cell.replay(ctx, evts, actor);

    actor.as_mut().unwrap().post_start(ctx);
    mbox.set_suspended(false);
}

fn handle_log_msg<Msg>(entry: LogEntry,
                        ctx: &Context<Msg>,
                        actor: &mut Option<BoxActor<Msg>>)
    where Msg: Message
{
    if actor.is_some() {
        actor.as_mut().unwrap().system_receive(ctx, SystemMsg::Log(entry), None);
    }
}

struct Sentinel<Msg: Message> {
    parent: ActorRef<Msg>,
    actor: ActorRef<Msg>,
    mbox: Mailbox<Msg>,
}

impl<Msg> Drop for Sentinel<Msg>
    where Msg: Message
{
    fn drop(&mut self) {
        if thread::panicking() {
            // Suspend the mailbox to prevent further message processing
            self.mbox.set_suspended(true);

            // There is no actor to park but kernel still needs to mark as no longer scheduled
            // self.kernel.park_actor(self.actor.uri.uid, None);
            self.mbox.set_scheduled(false);

            // Message the parent (this failed actor's supervisor) to decide how to handle the failure
            self.parent.sys_tell(SystemMsg::Failed(self.actor.clone()), None);
        }
    }
}

#[derive(Clone, Debug)]
pub struct MailboxConfig {
    pub msg_process_limit: u32,
}

impl<'a> From<&'a Config> for MailboxConfig {
    fn from(cfg: &Config) -> Self {
        MailboxConfig {
            msg_process_limit: cfg.get_int("mailbox.msg_process_limit").unwrap() as u32
        }
    }
}