-
Notifications
You must be signed in to change notification settings - Fork 278
/
mailbox.rs
555 lines (503 loc) · 18.1 KB
/
mailbox.rs
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
// Copyright (C) 2022 Quickwit, Inc.
//
// Quickwit is offered under the AGPL v3.0 and as commercial software.
// For commercial licensing, contact us at hello@quickwit.io.
//
// AGPL:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use std::any::Any;
use std::convert::Infallible;
use std::fmt;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Weak};
use std::time::Instant;
use async_trait::async_trait;
use quickwit_common::metrics::IntCounter;
use tokio::sync::oneshot;
use crate::channel_with_priority::{Receiver, Sender, TrySendError};
use crate::envelope::{wrap_in_envelope, Envelope};
use crate::{
Actor, ActorContext, ActorExitStatus, AskError, Handler, QueueCapacity, RecvError, SendError,
};
/// A mailbox is the object that makes it possible to send a message
/// to an actor.
///
/// It is lightweight to clone.
///
/// The actor holds its `Inbox` counterpart.
///
/// The mailbox can receive high priority and low priority messages.
/// Commands are typically sent as high priority messages, whereas regular
/// actor messages are sent to the low priority channel.
///
/// Whenever a high priority message is available, it is processed
/// before low priority messages.
///
/// If all mailboxes are dropped, the actor will process all of the pending messages
/// and gracefully exit with [`crate::actor::ActorExitStatus::Success`].
pub struct Mailbox<A: Actor> {
inner: Arc<Inner<A>>,
// We do not rely on the `Arc:strong_count` here to avoid an intricate
// race condition. We want to make sure the processing of the `Nudge`
// message happens AFTER we decrement the refcount.
ref_count: Arc<AtomicUsize>,
}
impl<A: Actor> Mailbox<A> {
pub fn downgrade(&self) -> WeakMailbox<A> {
WeakMailbox {
inner: Arc::downgrade(&self.inner),
ref_count: Arc::downgrade(&self.ref_count),
}
}
}
impl<A: Actor> Drop for Mailbox<A> {
fn drop(&mut self) {
let old_val = self.ref_count.fetch_sub(1, Ordering::SeqCst);
if old_val == 2 {
// This was the last mailbox.
// `ref_count == 1` means that only the mailbox in the ActorContext
// is remaining.
let _ = self.send_message_with_high_priority(LastMailbox);
}
}
}
#[derive(Debug)]
struct LastMailbox;
#[async_trait]
impl<A: Actor> Handler<LastMailbox> for A {
type Reply = ();
async fn handle(
&mut self,
_: LastMailbox,
_ctx: &ActorContext<Self>,
) -> Result<(), ActorExitStatus> {
// Being the last mailbox does not necessarily mean that we
// want to stop the processing.
//
// There could be pending message in the queue that will
// spawn actors which will get a new copy of the mailbox
// etc.
//
// For that reason, the logic that really detects
// the last mailbox happens when all message have been drained.
//
// The `LastMailbox` message is just here to make sure the actor
// loop does not get stuck waiting for a message that does
// will never come.
Ok(())
}
}
impl<A: Actor> Clone for Mailbox<A> {
fn clone(&self) -> Self {
self.ref_count.fetch_add(1, Ordering::SeqCst);
Mailbox {
inner: self.inner.clone(),
ref_count: self.ref_count.clone(),
}
}
}
impl<A: Actor> Mailbox<A> {
pub(crate) fn is_last_mailbox(&self) -> bool {
self.ref_count.load(Ordering::SeqCst) == 1
}
pub fn id(&self) -> &str {
&self.inner.instance_id
}
}
pub(crate) struct Inner<A: Actor> {
pub(crate) tx: Sender<Envelope<A>>,
instance_id: String,
}
impl<A: Actor> fmt::Debug for Mailbox<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Mailbox")
.field(&self.actor_instance_id())
.finish()
}
}
impl<A: Actor> Mailbox<A> {
pub fn actor_instance_id(&self) -> &str {
&self.inner.instance_id
}
pub fn is_disconnected(&self) -> bool {
self.inner.tx.is_disconnected()
}
/// Sends a message to the actor owning the associated inbox.
///
/// From an actor context, use the `ActorContext::send_message` method instead.
///
/// SendError is returned if the actor has already exited.
pub async fn send_message<M>(
&self,
message: M,
) -> Result<oneshot::Receiver<A::Reply>, SendError>
where
A: Handler<M>,
M: 'static + Send + Sync + fmt::Debug,
{
self.send_message_with_backpressure_counter(message, None)
.await
}
/// Attempts to queue a message in the low priority channel of the mailbox.
///
/// If sending the message would block, the method simply returns `TrySendError::Full(message)`.
pub fn try_send_message<M>(
&self,
message: M,
) -> Result<oneshot::Receiver<A::Reply>, TrySendError<M>>
where
A: Handler<M>,
M: 'static + Send + Sync + fmt::Debug,
{
let (envelope, response_rx) = wrap_in_envelope(message);
self.inner
.tx
.try_send_low_priority(envelope)
.map_err(|err| {
match err {
TrySendError::Disconnected => TrySendError::Disconnected,
TrySendError::Full(mut envelope) => {
// We need to un pack the envelope.
let message: M = envelope.message_typed().unwrap();
TrySendError::Full(message)
}
}
})?;
Ok(response_rx)
}
/// Sends a message to the actor owning the associated inbox.
///
/// If the actor experiences some backpressure, then
/// `backpressure_micros` will be increased by the amount of
/// microseconds of backpressure experienced.
pub async fn send_message_with_backpressure_counter<M>(
&self,
message: M,
backpressure_micros_counter_opt: Option<&IntCounter>,
) -> Result<oneshot::Receiver<A::Reply>, SendError>
where
A: Handler<M>,
M: 'static + Send + Sync + fmt::Debug,
{
let (envelope, response_rx) = wrap_in_envelope(message);
if let Some(backpressure_micros_counter) = backpressure_micros_counter_opt {
match self.inner.tx.try_send_low_priority(envelope) {
Ok(()) => Ok(response_rx),
Err(TrySendError::Full(msg)) => {
let now = Instant::now();
self.inner.tx.send_low_priority(msg).await?;
let elapsed = now.elapsed();
backpressure_micros_counter.inc_by(elapsed.as_micros() as u64);
Ok(response_rx)
}
Err(TrySendError::Disconnected) => Err(SendError::Disconnected),
}
} else {
self.inner.tx.send_low_priority(envelope).await?;
Ok(response_rx)
}
}
pub(crate) fn send_message_with_high_priority<M>(&self, message: M) -> Result<(), SendError>
where
A: Handler<M>,
M: 'static + Send + Sync + fmt::Debug,
{
let (envelope, _response_rx) = wrap_in_envelope(message);
self.inner.tx.send_high_priority(envelope)
}
/// Similar to `send_message`, except this method
/// waits asynchronously for the actor reply.
///
/// From an actor context, use the `ActorContext::ask` method instead.
pub async fn ask<M, T>(&self, message: M) -> Result<T, AskError<Infallible>>
where
A: Handler<M, Reply = T>,
M: 'static + Send + Sync + fmt::Debug,
{
self.ask_with_backpressure_counter(message, None).await
}
/// Similar to `ask`, but if a backpressure counter is passed,
/// it increments the amount of time spent in the backpressure.
///
/// The backpressure duration only includes the amount of time
/// it took to `queue` the request into the actor pipeline.
///
/// It does not include
/// - the amount spent waiting in the queue,
/// - the amount spent processing the message.
///
/// From an actor context, use the `ActorContext::ask` method instead.
pub async fn ask_with_backpressure_counter<M, T>(
&self,
message: M,
backpressure_micros_counter_opt: Option<&IntCounter>,
) -> Result<T, AskError<Infallible>>
where
A: Handler<M, Reply = T>,
M: 'static + Send + Sync + fmt::Debug,
{
let resp = self
.send_message_with_backpressure_counter(message, backpressure_micros_counter_opt)
.await;
resp.map_err(|_send_error| AskError::MessageNotDelivered)?
.await
.map_err(|_| AskError::ProcessMessageError)
}
/// Similar to `send_message`, except this method
/// waits asynchronously for the actor reply.
///
/// From an actor context, use the `ActorContext::ask` method instead.
pub async fn ask_for_res<M, T, E: fmt::Debug>(&self, message: M) -> Result<T, AskError<E>>
where
A: Handler<M, Reply = Result<T, E>>,
M: 'static + Send + Sync + fmt::Debug,
{
self.send_message(message)
.await
.map_err(|_send_error| AskError::MessageNotDelivered)?
.await
.map_err(|_| AskError::ProcessMessageError)?
.map_err(AskError::from)
}
}
pub struct Inbox<A: Actor> {
rx: Arc<Receiver<Envelope<A>>>,
}
impl<A: Actor> Clone for Inbox<A> {
fn clone(&self) -> Self {
Inbox {
rx: self.rx.clone(),
}
}
}
impl<A: Actor> Inbox<A> {
pub(crate) async fn recv(&self) -> Result<Envelope<A>, RecvError> {
self.rx.recv().await
}
pub(crate) async fn recv_cmd_and_scheduled_msg_only(&self) -> Envelope<A> {
self.rx.recv_high_priority().await
}
#[allow(dead_code)] // temporary
pub(crate) fn try_recv(&self) -> Result<Envelope<A>, RecvError> {
self.rx.try_recv()
}
#[allow(dead_code)] // temporary
pub(crate) fn try_recv_cmd_and_scheduled_msg_only(&self) -> Result<Envelope<A>, RecvError> {
self.rx.try_recv_high_priority_message()
}
/// Destroys the inbox and returns the list of pending messages or commands
/// in the low priority channel.
///
/// Warning this iterator might never be exhausted if there is a living
/// mailbox associated to it.
pub fn drain_for_test(&self) -> Vec<Box<dyn Any>> {
self.rx
.drain_low_priority()
.into_iter()
.map(|mut envelope| envelope.message())
.collect()
}
/// Destroys the inbox and returns the list of pending messages or commands
/// in the low priority channel.
///
/// Warning this iterator might never be exhausted if there is a living
/// mailbox associated to it.
pub fn drain_for_test_typed<M: 'static>(&self) -> Vec<M> {
self.rx
.drain_low_priority()
.into_iter()
.flat_map(|mut envelope| envelope.message_typed())
.collect()
}
}
pub fn create_mailbox<A: Actor>(
actor_name: String,
queue_capacity: QueueCapacity,
) -> (Mailbox<A>, Inbox<A>) {
let (tx, rx) = crate::channel_with_priority::channel(queue_capacity);
let ref_count = Arc::new(AtomicUsize::new(1));
let mailbox = Mailbox {
inner: Arc::new(Inner {
tx,
instance_id: quickwit_common::new_coolid(&actor_name),
}),
ref_count,
};
let inbox = Inbox { rx: Arc::new(rx) };
(mailbox, inbox)
}
pub fn create_test_mailbox<A: Actor>() -> (Mailbox<A>, Inbox<A>) {
create_mailbox("test-mailbox".to_string(), QueueCapacity::Unbounded)
}
pub struct WeakMailbox<A: Actor> {
inner: Weak<Inner<A>>,
ref_count: Weak<AtomicUsize>,
}
impl<A: Actor> WeakMailbox<A> {
pub fn upgrade(&self) -> Option<Mailbox<A>> {
let inner = self.inner.upgrade()?;
let ref_count = self.ref_count.upgrade()?;
Some(Mailbox { inner, ref_count })
}
}
#[cfg(test)]
mod tests {
use std::mem;
use std::time::Duration;
use super::*;
use crate::tests::{Ping, PingReceiverActor};
use crate::Universe;
#[test]
fn test_weak_mailbox_downgrade_upgrade() {
let (mailbox, _inbox) = create_test_mailbox::<PingReceiverActor>();
let weak_mailbox = mailbox.downgrade();
assert!(weak_mailbox.upgrade().is_some());
}
#[test]
fn test_weak_mailbox_failing_upgrade() {
let (mailbox, _inbox) = create_test_mailbox::<PingReceiverActor>();
let weak_mailbox = mailbox.downgrade();
drop(mailbox);
assert!(weak_mailbox.upgrade().is_none());
}
struct BackPressureActor;
impl Actor for BackPressureActor {
type ObservableState = ();
fn observable_state(&self) -> Self::ObservableState {}
fn queue_capacity(&self) -> QueueCapacity {
QueueCapacity::Bounded(0)
}
fn yield_after_each_message(&self) -> bool {
false
}
}
use async_trait::async_trait;
#[async_trait]
impl Handler<Duration> for BackPressureActor {
type Reply = ();
async fn handle(
&mut self,
sleep_duration: Duration,
_ctx: &ActorContext<Self>,
) -> Result<(), ActorExitStatus> {
if !sleep_duration.is_zero() {
tokio::time::sleep(sleep_duration).await;
}
Ok(())
}
}
#[tokio::test]
async fn test_mailbox_send_with_backpressure_counter_no_backpressure_cleansheet() {
let universe = Universe::new();
let back_pressure_actor = BackPressureActor;
let (mailbox, _handle) = universe.spawn_builder().spawn(back_pressure_actor);
// We send a first message to make sure the actor has been properly spawned and is listening
// for new messages.
mailbox
.ask_with_backpressure_counter(Duration::default(), None)
.await
.unwrap();
// At this point the actor was started and even processed a message entirely.
let backpressure_micros_counter =
IntCounter::new("test_counter", "help for test_counter").unwrap();
let wait_duration = Duration::from_millis(1);
let processed = mailbox
.send_message_with_backpressure_counter(
wait_duration,
Some(&backpressure_micros_counter),
)
.await
.unwrap();
assert_eq!(backpressure_micros_counter.get(), 0u64);
processed.await.unwrap();
assert_eq!(backpressure_micros_counter.get(), 0u64);
}
#[tokio::test]
async fn test_mailbox_send_with_backpressure_counter_backpressure() {
let universe = Universe::new();
let back_pressure_actor = BackPressureActor;
let (mailbox, _handle) = universe.spawn_builder().spawn(back_pressure_actor);
// We send a first message to make sure the actor has been properly spawned and is listening
// for new messages.
mailbox
.ask_with_backpressure_counter(Duration::default(), None)
.await
.unwrap();
let backpressure_micros_counter =
IntCounter::new("test_counter", "help for test_counter").unwrap();
let wait_duration = Duration::from_millis(1);
mailbox
.send_message_with_backpressure_counter(
wait_duration,
Some(&backpressure_micros_counter),
)
.await
.unwrap();
// That second message will present some backpressure, since the capacity is 0 and
// the first message willl take 1000 micros to be processed.
mailbox
.send_message_with_backpressure_counter(
Duration::default(),
Some(&backpressure_micros_counter),
)
.await
.unwrap();
assert!(backpressure_micros_counter.get() > 1_000u64);
}
#[tokio::test]
async fn test_mailbox_waiting_for_processing_does_not_counter_as_backpressure() {
let universe = Universe::new();
let back_pressure_actor = BackPressureActor;
let (mailbox, _handle) = universe.spawn_builder().spawn(back_pressure_actor);
mailbox
.ask_with_backpressure_counter(Duration::default(), None)
.await
.unwrap();
let backpressure_micros_counter =
IntCounter::new("test_counter", "help for test_counter").unwrap();
let start = Instant::now();
mailbox
.ask_with_backpressure_counter(Duration::from_millis(1), None)
.await
.unwrap();
let elapsed = start.elapsed();
assert!(elapsed.as_micros() > 1000);
assert_eq!(backpressure_micros_counter.get(), 0);
}
#[test]
fn test_try_send() {
let (mailbox, _inbox) = super::create_mailbox::<PingReceiverActor>(
"hello".to_string(),
QueueCapacity::Bounded(1),
);
assert!(mailbox.try_send_message(Ping).is_ok());
assert!(matches!(
mailbox.try_send_message(Ping).unwrap_err(),
TrySendError::Full(Ping)
));
}
#[test]
fn test_try_send_disconnect() {
let (mailbox, inbox) = super::create_mailbox::<PingReceiverActor>(
"hello".to_string(),
QueueCapacity::Bounded(1),
);
assert!(mailbox.try_send_message(Ping).is_ok());
mem::drop(inbox);
assert!(matches!(
mailbox.try_send_message(Ping).unwrap_err(),
TrySendError::Disconnected
));
}
}