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
use futures::{
channel::mpsc::{
channel, unbounded, Receiver, SendError as FuturesSendError, Sender, TrySendError,
UnboundedReceiver, UnboundedSender,
},
stream, SinkExt, Stream, StreamExt,
};
use grpcio::{Result as GrpcResult, WriteFlags};
use kvproto::cdcpb::ChangeDataEvent;
use tikv_util::impl_display_as_debug;
use crate::service::{CdcEvent, EventBatcher};
const CDC_MSG_MAX_BATCH_SIZE: usize = 128;
pub const CDC_EVENT_MAX_BATCH_SIZE: usize = 2;
pub fn canal(buffer: usize) -> (Sink, Drain) {
let (unbounded_sender, unbounded_receiver) = unbounded();
let (bounded_sender, bounded_receiver) = channel(buffer);
(
Sink {
unbounded_sender,
bounded_sender,
},
Drain {
unbounded_receiver,
bounded_receiver,
},
)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SendError {
Full,
Disconnected,
Congested,
}
impl std::error::Error for SendError {}
impl_display_as_debug!(SendError);
macro_rules! impl_from_future_send_error {
($($f:ty,)+) => {
$(
impl From<$f> for SendError {
fn from(e: $f) -> Self {
if e.is_disconnected() {
SendError::Disconnected
} else if e.is_full() {
Self::Full
} else {
unreachable!()
}
}
}
)+
};
}
impl_from_future_send_error! {
FuturesSendError,
TrySendError<(CdcEvent, usize)>,
}
#[derive(Clone)]
pub struct Sink {
unbounded_sender: UnboundedSender<(CdcEvent, usize)>,
bounded_sender: Sender<(CdcEvent, usize)>,
}
impl Sink {
pub fn unbounded_send(&self, event: CdcEvent) -> Result<(), SendError> {
let bytes = event.size() as usize;
self.unbounded_sender
.unbounded_send((event, bytes))
.map_err(SendError::from)
}
pub async fn send_all(&mut self, events: Vec<CdcEvent>) -> Result<(), SendError> {
for event in events {
let bytes = event.size() as usize;
self.bounded_sender.feed((event, bytes)).await?;
}
self.bounded_sender.flush().await?;
Ok(())
}
}
pub struct Drain {
unbounded_receiver: UnboundedReceiver<(CdcEvent, usize)>,
bounded_receiver: Receiver<(CdcEvent, usize)>,
}
impl Drain {
pub fn drain(self) -> impl Stream<Item = (CdcEvent, usize)> {
stream::select(self.bounded_receiver, self.unbounded_receiver).map(|(mut event, size)| {
if let CdcEvent::Barrier(ref mut barrier) = event {
if let Some(barrier) = barrier.take() {
barrier(());
}
}
(event, size)
})
}
pub fn drain_grpc_message(
self,
) -> impl Stream<Item = GrpcResult<(ChangeDataEvent, WriteFlags)>> {
self.drain()
.ready_chunks(CDC_MSG_MAX_BATCH_SIZE)
.map(move |events| {
let mut bytes = 0;
let mut batcher = EventBatcher::with_capacity(CDC_EVENT_MAX_BATCH_SIZE);
events.into_iter().for_each(|(e, size)| {
bytes += size;
batcher.push(e);
});
let resps = batcher.build();
let last_idx = resps.len() - 1;
stream::iter(resps.into_iter().enumerate().map(move |(i, e)| {
let write_flags = WriteFlags::default().buffer_hint(i != last_idx);
GrpcResult::Ok((e, write_flags))
}))
})
.flatten()
}
}
#[cfg(test)]
pub fn recv_timeout<S, I>(s: &mut S, dur: std::time::Duration) -> Result<Option<I>, ()>
where
S: Stream<Item = I> + Unpin,
{
poll_timeout(&mut s.next(), dur)
}
#[cfg(test)]
pub fn poll_timeout<F, I>(fut: &mut F, dur: std::time::Duration) -> Result<I, ()>
where
F: std::future::Future<Output = I> + Unpin,
{
use futures::FutureExt;
let mut timeout = futures_timer::Delay::new(dur).fuse();
let mut f = fut.fuse();
futures::executor::block_on(async {
futures::select! {
() = timeout => Err(()),
item = f => Ok(item),
}
})
}
#[cfg(test)]
mod tests {
use super::*;
use futures::executor::block_on;
use std::sync::mpsc;
use std::time::Duration;
type Send = Box<dyn FnMut(CdcEvent) -> Result<(), SendError>>;
fn new_test_cancal(buffer: usize) -> (Send, Drain) {
let (mut tx, rx) = canal(buffer);
let mut flag = true;
let send = move |event| {
flag = !flag;
if flag {
tx.unbounded_send(event)
} else {
block_on(tx.send_all(vec![event]))
}
};
(Box::new(send), rx)
}
#[test]
fn test_barrier() {
let (mut send, rx) = new_test_cancal(10);
send(CdcEvent::Event(Default::default())).unwrap();
let (btx1, brx1) = mpsc::channel();
send(CdcEvent::Barrier(Some(Box::new(move |()| {
btx1.send(()).unwrap();
}))))
.unwrap();
send(CdcEvent::ResolvedTs(Default::default())).unwrap();
let (btx2, brx2) = mpsc::channel();
send(CdcEvent::Barrier(Some(Box::new(move |()| {
btx2.send(()).unwrap();
}))))
.unwrap();
let mut drain = rx.drain();
brx1.recv_timeout(Duration::from_millis(100)).unwrap_err();
brx2.recv_timeout(Duration::from_millis(100)).unwrap_err();
assert_matches!(block_on(drain.next()), Some((CdcEvent::Event(_), _)));
brx1.recv_timeout(Duration::from_millis(100)).unwrap_err();
brx2.recv_timeout(Duration::from_millis(100)).unwrap_err();
assert_matches!(block_on(drain.next()), Some((CdcEvent::Barrier(_), _)));
brx1.recv_timeout(Duration::from_millis(100)).unwrap();
brx2.recv_timeout(Duration::from_millis(100)).unwrap_err();
assert_matches!(block_on(drain.next()), Some((CdcEvent::ResolvedTs(_), _)));
brx2.recv_timeout(Duration::from_millis(100)).unwrap_err();
assert_matches!(block_on(drain.next()), Some((CdcEvent::Barrier(_), _)));
brx2.recv_timeout(Duration::from_millis(100)).unwrap();
brx1.recv_timeout(Duration::from_millis(100)).unwrap_err();
brx2.recv_timeout(Duration::from_millis(100)).unwrap_err();
}
#[test]
fn test_nonblocking_batch() {
let (mut send, rx) = new_test_cancal(CDC_MSG_MAX_BATCH_SIZE * 2);
let mut drain = rx.drain_grpc_message();
for count in 1..CDC_EVENT_MAX_BATCH_SIZE + CDC_EVENT_MAX_BATCH_SIZE / 2 {
for _ in 0..count {
send(CdcEvent::Event(Default::default())).unwrap();
}
recv_timeout(&mut drain, Duration::from_millis(100)).unwrap();
}
if recv_timeout(&mut drain, Duration::from_millis(100)).is_ok() {
panic!("expect to be timeout");
}
}
}