-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
mod.rs
356 lines (317 loc) · 10.1 KB
/
mod.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
use crate::{config::Resource, internal_events::EventOut, Event};
#[cfg(feature = "leveldb")]
use futures::compat::{Sink01CompatExt, Stream01CompatExt};
use futures::{channel::mpsc, Sink, SinkExt, Stream};
use futures01::task::AtomicTask;
use pin_project::pin_project;
use serde::{Deserialize, Serialize};
use std::{
path::PathBuf,
pin::Pin,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
task::{Context, Poll},
};
#[cfg(feature = "leveldb")]
use tokio::stream::StreamExt;
#[cfg(feature = "leveldb")]
pub mod disk;
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum BufferConfig {
Memory {
#[serde(default = "BufferConfig::memory_max_events")]
max_events: usize,
#[serde(default)]
when_full: WhenFull,
},
#[cfg(feature = "leveldb")]
Disk {
max_size: usize,
#[serde(default)]
when_full: WhenFull,
},
}
impl Default for BufferConfig {
fn default() -> Self {
BufferConfig::Memory {
max_events: BufferConfig::memory_max_events(),
when_full: Default::default(),
}
}
}
#[derive(Deserialize, Serialize, Debug, PartialEq, Copy, Clone)]
#[serde(rename_all = "snake_case")]
pub enum WhenFull {
Block,
DropNewest,
}
impl Default for WhenFull {
fn default() -> Self {
WhenFull::Block
}
}
#[derive(Clone)]
pub enum BufferInputCloner {
Memory(mpsc::Sender<Event>, WhenFull),
#[cfg(feature = "leveldb")]
Disk(disk::Writer, WhenFull),
}
impl BufferInputCloner {
pub fn get(&self) -> Box<dyn Sink<Event, Error = ()> + Send> {
match self {
BufferInputCloner::Memory(tx, when_full) => {
let inner = tx
.clone()
.sink_map_err(|error| error!(message = "Sender error.", %error));
if when_full == &WhenFull::DropNewest {
Box::new(DropWhenFull::new(inner))
} else {
Box::new(inner)
}
}
#[cfg(feature = "leveldb")]
BufferInputCloner::Disk(writer, when_full) => {
let inner = writer.clone().sink_compat();
if when_full == &WhenFull::DropNewest {
Box::new(DropWhenFull::new(inner))
} else {
Box::new(inner)
}
}
}
}
}
impl BufferConfig {
#[inline]
const fn memory_max_events() -> usize {
500
}
#[cfg_attr(not(feature = "leveldb"), allow(unused))]
pub fn build(
&self,
data_dir: &Option<PathBuf>,
sink_name: &str,
) -> Result<
(
BufferInputCloner,
Box<dyn Stream<Item = Event> + Send>,
Acker,
),
String,
> {
match &self {
BufferConfig::Memory {
max_events,
when_full,
} => {
let (tx, rx) = mpsc::channel(*max_events);
let tx = BufferInputCloner::Memory(tx, *when_full);
let rx = Box::new(rx);
Ok((tx, rx, Acker::Null))
}
#[cfg(feature = "leveldb")]
BufferConfig::Disk {
max_size,
when_full,
} => {
let data_dir = data_dir
.as_ref()
.ok_or_else(|| "Must set data_dir to use on-disk buffering.".to_string())?;
let buffer_dir = format!("{}_buffer", sink_name);
let (tx, rx, acker) = disk::open(&data_dir, buffer_dir.as_ref(), *max_size)
.map_err(|error| error.to_string())?;
let tx = BufferInputCloner::Disk(tx, *when_full);
let rx = Box::new(
rx.compat()
.take_while(|event| event.is_ok())
.map(|event| event.unwrap()),
);
Ok((tx, rx, acker))
}
}
}
/// Resources that the sink is using.
#[cfg_attr(not(feature = "leveldb"), allow(unused))]
pub fn resources(&self, sink_name: &str) -> Vec<Resource> {
match self {
BufferConfig::Memory { .. } => Vec::new(),
#[cfg(feature = "leveldb")]
BufferConfig::Disk { .. } => vec![Resource::DiskBuffer(sink_name.to_string())],
}
}
}
#[derive(Debug, Clone)]
pub enum Acker {
Disk(Arc<AtomicUsize>, Arc<AtomicTask>),
Null,
}
impl Acker {
// This method should be called by a sink to indicate that it has successfully
// flushed the next `num` events from its input stream. If there are events that
// have flushed, but events that came before them in the stream have not been flushed,
// the later events must _not_ be acked until all preceding elements are also acked.
// This is primary used by the on-disk buffer to know which events are okay to
// delete from disk.
pub fn ack(&self, num: usize) {
// Only ack items if the amount to ack is larger than zero.
if num > 0 {
match self {
Acker::Null => {}
Acker::Disk(counter, notifier) => {
counter.fetch_add(num, Ordering::Relaxed);
notifier.notify();
}
}
emit!(EventOut { count: num });
}
}
pub fn new_for_testing() -> (Self, Arc<AtomicUsize>) {
let ack_counter = Arc::new(AtomicUsize::new(0));
let notifier = Arc::new(AtomicTask::new());
let acker = Acker::Disk(Arc::clone(&ack_counter), Arc::clone(¬ifier));
(acker, ack_counter)
}
}
#[pin_project]
pub struct DropWhenFull<S> {
#[pin]
inner: S,
drop: bool,
}
impl<S> DropWhenFull<S> {
pub fn new(inner: S) -> Self {
Self { inner, drop: false }
}
}
impl<T, S: Sink<T> + Unpin> Sink<T> for DropWhenFull<S> {
type Error = S::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let this = self.project();
match this.inner.poll_ready(cx) {
Poll::Ready(Ok(())) => {
*this.drop = false;
Poll::Ready(Ok(()))
}
Poll::Pending => {
*this.drop = true;
Poll::Ready(Ok(()))
}
error => error,
}
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
if self.drop {
debug!(
message = "Shedding load; dropping event.",
internal_log_rate_secs = 10
);
Ok(())
} else {
self.project().inner.start_send(item)
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().inner.poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.project().inner.poll_close(cx)
}
}
#[cfg(test)]
mod test {
use super::{Acker, BufferConfig, DropWhenFull, WhenFull};
use crate::sink::BoundedSink;
use futures::{future, Sink, Stream};
use futures01::task::AtomicTask;
use std::{
sync::{atomic::AtomicUsize, Arc},
task::Poll,
};
use tokio::sync::mpsc;
use tokio01_test::task::MockTask;
#[tokio::test]
async fn drop_when_full() {
future::lazy(|cx| {
let (tx, rx) = mpsc::channel(3);
let mut tx = Box::pin(DropWhenFull::new(BoundedSink::new(tx)));
assert_eq!(tx.as_mut().poll_ready(cx), Poll::Ready(Ok(())));
assert_eq!(tx.as_mut().start_send(1), Ok(()));
assert_eq!(tx.as_mut().poll_ready(cx), Poll::Ready(Ok(())));
assert_eq!(tx.as_mut().start_send(2), Ok(()));
assert_eq!(tx.as_mut().poll_ready(cx), Poll::Ready(Ok(())));
assert_eq!(tx.as_mut().start_send(3), Ok(()));
assert_eq!(tx.as_mut().poll_ready(cx), Poll::Ready(Ok(())));
assert_eq!(tx.as_mut().start_send(4), Ok(()));
let mut rx = Box::pin(rx);
assert_eq!(rx.as_mut().poll_next(cx), Poll::Ready(Some(1)));
assert_eq!(rx.as_mut().poll_next(cx), Poll::Ready(Some(2)));
assert_eq!(rx.as_mut().poll_next(cx), Poll::Ready(Some(3)));
assert_eq!(rx.as_mut().poll_next(cx), Poll::Pending);
})
.await;
}
#[test]
fn ack_with_none() {
let counter = Arc::new(AtomicUsize::new(0));
let task = Arc::new(AtomicTask::new());
let acker = Acker::Disk(counter, Arc::clone(&task));
let mut mock = MockTask::new();
mock.enter(|| task.register());
assert!(!mock.is_notified());
acker.ack(0);
assert!(!mock.is_notified());
acker.ack(1);
assert!(mock.is_notified());
}
#[test]
fn config_default_values() {
fn check(source: &str, config: BufferConfig) {
let conf: BufferConfig = toml::from_str(source).unwrap();
assert_eq!(toml::to_string(&conf), toml::to_string(&config));
}
check(
r#"
type = "memory"
"#,
BufferConfig::Memory {
max_events: 500,
when_full: WhenFull::Block,
},
);
check(
r#"
type = "memory"
max_events = 100
"#,
BufferConfig::Memory {
max_events: 100,
when_full: WhenFull::Block,
},
);
check(
r#"
type = "memory"
when_full = "drop_newest"
"#,
BufferConfig::Memory {
max_events: 500,
when_full: WhenFull::DropNewest,
},
);
#[cfg(feature = "leveldb")]
check(
r#"
type = "disk"
max_size = 1024
"#,
BufferConfig::Disk {
max_size: 1024,
when_full: WhenFull::Block,
},
);
}
}