-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathvariant.rs
163 lines (148 loc) · 4.34 KB
/
variant.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
use std::{
num::{NonZeroU16, NonZeroU64, NonZeroUsize},
path::PathBuf,
};
#[cfg(test)]
use quickcheck::{Arbitrary, Gen};
use tracing::Span;
use vector_common::finalization::Finalizable;
use crate::{
topology::{
builder::TopologyBuilder,
channel::{BufferReceiver, BufferSender},
},
variants::{DiskV2Buffer, MemoryBuffer},
Bufferable, WhenFull,
};
#[cfg(test)]
const MAX_STR_SIZE: usize = 128;
#[cfg(test)]
const ALPHABET: [&str; 27] = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "_",
];
// Memory buffers are also used with transforms which is opaque to users. We use
// an instrument flag in the Memory variant to disable instrumentation to avoid
// emitting metrics for such buffers.
#[derive(Debug, Clone)]
pub enum Variant {
Memory {
max_events: NonZeroUsize,
when_full: WhenFull,
},
DiskV2 {
max_size: NonZeroU64,
when_full: WhenFull,
data_dir: PathBuf,
id: String,
},
}
impl Variant {
pub async fn create_sender_receiver<T>(&self) -> (BufferSender<T>, BufferReceiver<T>)
where
T: Bufferable + Clone + Finalizable,
{
let mut builder = TopologyBuilder::default();
match self {
Variant::Memory {
max_events,
when_full,
..
} => {
builder.stage(MemoryBuffer::new(*max_events), *when_full);
}
Variant::DiskV2 {
max_size,
when_full,
data_dir,
id,
} => {
builder.stage(
DiskV2Buffer::new(id.clone(), data_dir.clone(), *max_size),
*when_full,
);
}
};
let (sender, receiver) = builder
.build(String::from("benches"), Span::none())
.await
.unwrap_or_else(|_| unreachable!("topology build should not fail"));
(sender, receiver)
}
}
#[cfg(test)]
#[derive(Debug, Clone)]
struct Id {
inner: String,
}
#[cfg(test)]
impl Arbitrary for Id {
fn arbitrary(g: &mut Gen) -> Self {
let mut id = String::with_capacity(MAX_STR_SIZE);
for _ in 0..(g.size() % MAX_STR_SIZE) {
let idx: usize = usize::arbitrary(g) % ALPHABET.len();
id.push_str(ALPHABET[idx]);
}
Id { inner: id }
}
}
#[cfg(test)]
impl Arbitrary for Variant {
fn arbitrary(g: &mut Gen) -> Self {
let use_memory_buffer = bool::arbitrary(g);
// Using a u16 ensures we avoid any allocation errors for our holding buffers, etc.
let max_events = NonZeroU16::arbitrary(g)
.try_into()
.expect("we don't support 16-bit platforms");
let max_size = NonZeroU16::arbitrary(g)
.try_into()
.expect("we don't support 16-bit platforms");
let when_full = WhenFull::arbitrary(g);
if use_memory_buffer {
Variant::Memory {
max_events,
when_full,
}
} else {
Variant::DiskV2 {
max_size,
when_full,
id: Id::arbitrary(g).inner,
data_dir: PathBuf::arbitrary(g),
}
}
}
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
match self {
Variant::Memory {
max_events,
when_full,
..
} => {
let when_full = *when_full;
Box::new(max_events.shrink().map(move |me| Variant::Memory {
max_events: me,
when_full,
}))
}
Variant::DiskV2 {
max_size,
when_full,
id,
data_dir,
..
} => {
let max_size = *max_size;
let when_full = *when_full;
let id = id.clone();
let data_dir = data_dir.clone();
Box::new(max_size.shrink().map(move |ms| Variant::DiskV2 {
max_size: ms,
when_full,
id: id.clone(),
data_dir: data_dir.clone(),
}))
}
}
}
}