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
pub mod differential;
pub mod materialized;
pub mod reachability;
pub mod timely;
use std::time::Duration;
use ::timely::communication::Push;
use ::timely::dataflow::channels::Bundle;
use ::timely::dataflow::operators::capture::{Event, EventPusher};
use ::timely::dataflow::operators::generic::OutputHandle;
use ::timely::dataflow::operators::Capability;
use ::timely::dataflow::operators::CapabilityRef;
use ::timely::progress::Timestamp as TimelyTimestamp;
use differential_dataflow::consolidation::consolidate_updates;
use differential_dataflow::difference::Semigroup;
use differential_dataflow::lattice::Lattice;
use differential_dataflow::ExchangeData;
use dataflow_types::logging::{DifferentialLog, LogVariant, MaterializedLog, TimelyLog};
use repr::Timestamp;
pub struct BatchLogger<T, E, P>
where
P: EventPusher<Timestamp, (Duration, E, T)>,
{
time_ms: Timestamp,
event_pusher: P,
_phantom: ::std::marker::PhantomData<(E, T)>,
granularity_ms: u64,
buffer: Vec<(Duration, E, T)>,
}
impl<T, E, P> BatchLogger<T, E, P>
where
P: EventPusher<Timestamp, (Duration, E, T)>,
{
const BATCH_SIZE_BYTES: usize = 1 << 13;
fn buffer_capacity() -> usize {
let size = ::std::mem::size_of::<(Duration, E, T)>();
if size == 0 {
Self::BATCH_SIZE_BYTES
} else if size <= Self::BATCH_SIZE_BYTES {
Self::BATCH_SIZE_BYTES / size
} else {
1
}
}
pub fn new(event_pusher: P, granularity_ms: u64) -> Self {
BatchLogger {
time_ms: 0,
event_pusher,
_phantom: ::std::marker::PhantomData,
granularity_ms,
buffer: Vec::with_capacity(Self::buffer_capacity()),
}
}
pub fn publish_batch(&mut self, time: &Duration, data: &mut Vec<(Duration, E, T)>) {
let new_time_ms =
(((time.as_millis() as Timestamp) / self.granularity_ms) + 1) * self.granularity_ms;
if !data.is_empty() {
if data.len() > self.buffer.capacity() - self.buffer.len() {
self.event_pusher.push(Event::Messages(
self.time_ms as Timestamp,
self.buffer.drain(..).collect(),
));
}
self.buffer.append(data);
}
if self.time_ms < new_time_ms {
self.event_pusher.push(Event::Messages(
self.time_ms as Timestamp,
self.buffer.drain(..).collect(),
));
if self.buffer.capacity() > Self::buffer_capacity() {
self.buffer = Vec::with_capacity(Self::buffer_capacity())
}
self.event_pusher
.push(Event::Progress(vec![(new_time_ms, 1), (self.time_ms, -1)]));
}
self.time_ms = new_time_ms;
}
}
impl<T, E, P> Drop for BatchLogger<T, E, P>
where
P: EventPusher<Timestamp, (Duration, E, T)>,
{
fn drop(&mut self) {
self.event_pusher
.push(Event::Progress(vec![(self.time_ms as Timestamp, -1)]));
}
}
pub struct ConsolidateBuffer<'a, T, D: ExchangeData, R: Semigroup, P>
where
P: Push<Bundle<T, (D, T, R)>> + 'a,
T: Clone + Lattice + Ord + TimelyTimestamp + 'a,
D: 'a,
{
buffer: Vec<(D, T, R)>,
output_handle: OutputHandle<'a, T, (D, T, R), P>,
cap: Option<Capability<T>>,
port: usize,
}
impl<'a, T, D: ExchangeData, R: Semigroup, P> ConsolidateBuffer<'a, T, D, R, P>
where
T: Clone + Lattice + Ord + TimelyTimestamp + 'a,
P: Push<Bundle<T, (D, T, R)>> + 'a,
{
pub fn new(output_handle: OutputHandle<'a, T, (D, T, R), P>, port: usize) -> Self {
Self {
output_handle,
port,
cap: None,
buffer: Vec::with_capacity(::timely::container::buffer::default_capacity::<(D, T, R)>()),
}
}
pub fn give(&mut self, cap: &CapabilityRef<T>, data: (D, T, R)) {
if self.cap.as_ref().map_or(true, |t| t.time() != cap.time()) {
self.flush();
self.cap = Some(cap.delayed_for_output(cap.time(), self.port));
}
self.buffer.push(data);
if self.buffer.len() == self.buffer.capacity() {
consolidate_updates(&mut self.buffer);
if self.buffer.len() > self.buffer.capacity() / 2 {
self.flush();
}
}
}
pub fn flush(&mut self) {
if let Some(cap) = &self.cap {
self.output_handle.session(cap).give_vec(&mut self.buffer);
}
}
}
impl<'a, T, D: ExchangeData, R: Semigroup, P> Drop for ConsolidateBuffer<'a, T, D, R, P>
where
P: Push<Bundle<T, (D, T, R)>> + 'a,
T: Clone + Lattice + Ord + TimelyTimestamp + 'a,
D: 'a,
{
fn drop(&mut self) {
self.flush();
}
}