rlink 0.6.16

High performance Stream Processing Framework
Documentation
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
use std::borrow::BorrowMut;
use std::collections::LinkedList;
use std::convert::TryFrom;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;

use bytes::BytesMut;
use futures::{SinkExt, StreamExt};
use tokio::io::AsyncWriteExt;
use tokio::net::tcp::ReadHalf;
use tokio::net::TcpStream;
use tokio_util::codec::FramedRead;
use tokio_util::codec::LengthDelimitedCodec;

use crate::channel::{
    bounded, named_channel_with_base, ElementReceiver, ElementSender, Receiver, Sender,
    TryRecvError, TrySendError,
};
use crate::core::element::Element;
use crate::core::properties::ChannelBaseOn;
use crate::core::runtime::{ChannelKey, ClusterDescriptor, TaskId};
use crate::metrics::{register_counter, Tag};
use crate::pub_sub::network::{
    new_framed_read, new_framed_write, ElementRequest, ElementResponse, ResponseCode,
};
use crate::runtime::worker::heart_beat::get_coordinator_status;
use crate::utils::thread::{async_runtime_multi, async_sleep};

pub(crate) static ENABLE_LOG: AtomicBool = AtomicBool::new(false);

#[inline]
fn is_enable_log() -> bool {
    ENABLE_LOG.load(Ordering::Relaxed)
}

#[inline]
pub(crate) fn enable_log() {
    ENABLE_LOG.store(true, Ordering::Relaxed)
}

#[inline]
pub(crate) fn disable_log() {
    ENABLE_LOG.store(false, Ordering::Relaxed)
}

const BATCH_PULL_SIZE: u16 = 6000;

lazy_static! {
    static ref C: (
        Sender<(ChannelKey, ElementSender)>,
        Receiver<(ChannelKey, ElementSender)>
    ) = bounded(32);
}

pub(crate) fn subscribe(
    source_task_ids: &Vec<TaskId>,
    target_task_id: &TaskId,
    channel_size: usize,
    channel_base_on: ChannelBaseOn,
) -> ElementReceiver {
    let (sender, receiver) = named_channel_with_base(
        "NetworkSubscribe",
        vec![
            Tag::new("source_job_id", source_task_ids[0].job_id.0),
            Tag::new("target_job_id", target_task_id.job_id.0),
            Tag::new("target_task_number", target_task_id.task_number),
        ],
        channel_size,
        channel_base_on,
    );

    for source_task_id in source_task_ids {
        let sub_key = ChannelKey {
            source_task_id: source_task_id.clone(),
            target_task_id: target_task_id.clone(),
        };

        subscribe_post(sub_key, sender.clone());
    }

    receiver
}

fn subscribe_post(channel_key: ChannelKey, sender: ElementSender) {
    let c: &(
        Sender<(ChannelKey, ElementSender)>,
        Receiver<(ChannelKey, ElementSender)>,
    ) = &*C;
    c.0.send((channel_key, sender)).unwrap()
}

pub(crate) fn run_subscribe(cluster_descriptor: Arc<ClusterDescriptor>) {
    async_runtime_multi("client", 4).block_on(subscribe_listen(cluster_descriptor));
    info!("network subscribe task stop");
}

async fn subscribe_listen(cluster_descriptor: Arc<ClusterDescriptor>) {
    let c: &(
        Sender<(ChannelKey, ElementSender)>,
        Receiver<(ChannelKey, ElementSender)>,
    ) = &*C;

    let delay = Duration::from_millis(50);
    let mut idle_counter = 0usize;
    let mut join_handles = Vec::new();
    loop {
        match c.1.try_recv() {
            Ok((channel_key, sender)) => {
                let worker_manager_descriptor = cluster_descriptor
                    .get_worker_manager(&channel_key.source_task_id)
                    .expect("WorkerManagerDescriptor not found");
                let addr = SocketAddr::from_str(&worker_manager_descriptor.task_manager_address)
                    .expect("parse address error");

                let join_handle = tokio::spawn(async move {
                    loop_client_task(channel_key.clone(), sender, addr, BATCH_PULL_SIZE).await;
                    channel_key
                });
                join_handles.push(join_handle);

                idle_counter = 0;
            }
            Err(TryRecvError::Empty) => {
                idle_counter += 1;
                if idle_counter < 20 * 10 {
                    async_sleep(delay).await;
                } else {
                    // all task registration must be completed within 10 seconds
                    info!("subscribe listen task finish");
                    break;
                }
            }
            Err(TryRecvError::Disconnected) => {
                info!("subscribe_listen channel is Disconnected")
            }
        }
    }

    for join_handle in join_handles {
        match join_handle.await {
            Ok(channel_key) => info!("channel({:?}) network subscribe stop", channel_key),
            Err(e) => error!("Client task error. {}", e),
        }
    }
}

async fn loop_client_task(
    channel_key: ChannelKey,
    sender: ElementSender,
    addr: SocketAddr,
    batch_pull_size: u16,
) {
    loop {
        match client_task(channel_key, sender.clone(), addr, batch_pull_size).await {
            Ok(_) => {
                info!("client close({:?})", channel_key);
                break;
            }
            Err(e) => {
                error!("client({}) task error. {}", addr, e);
                if get_coordinator_status().is_terminated() {
                    break;
                }
            }
        }

        async_sleep(Duration::from_secs(3)).await;
    }
}

async fn client_task(
    channel_key: ChannelKey,
    sender: ElementSender,
    addr: SocketAddr,
    batch_pull_size: u16,
) -> anyhow::Result<()> {
    let mut client = Client::new(channel_key, sender.clone(), addr, batch_pull_size).await?;
    let rt = client.send().await;
    client.close_rough().await;

    rt
}

pub(crate) struct Client {
    channel_key: ChannelKey,
    sender: ElementSender,

    pub(crate) addr: SocketAddr,
    batch_pull_size: u16,
    stream: TcpStream,
}

impl Client {
    pub async fn new(
        channel_key: ChannelKey,
        sender: ElementSender,
        addr: SocketAddr,
        batch_pull_size: u16,
    ) -> anyhow::Result<Self> {
        let std_stream = std::net::TcpStream::connect(addr)?;
        std_stream.set_nonblocking(true)?;
        std_stream.set_read_timeout(Some(Duration::from_secs(20)))?;
        std_stream.set_write_timeout(Some(Duration::from_secs(20)))?;

        let stream = TcpStream::from_std(std_stream)?;

        Ok(Client {
            channel_key,
            sender,
            addr,
            batch_pull_size,
            stream,
        })
    }

    pub async fn send(&mut self) -> anyhow::Result<()> {
        info!(
            "Pull remote={}, local={}, channel_key={:?}",
            self.addr,
            self.stream.local_addr().unwrap(),
            self.channel_key,
        );

        let (read_half, write_half) = self.stream.split();
        let mut framed_write = new_framed_write(write_half);
        let mut framed_read = new_framed_read(read_half);

        let counter = register_counter("NetWorkClient", self.channel_key.to_tags());

        let mut batch_id = 0u16;
        let timeout = Duration::from_secs(6);
        loop {
            let request = ElementRequest {
                channel_key: self.channel_key.clone(),
                batch_pull_size: self.batch_pull_size,
                batch_id,
            };

            let (n, _) = batch_id.overflowing_add(1);
            batch_id = n;

            if is_enable_log() {
                info!("send request: {:?}", request);
            }

            let buffer: BytesMut = request.into();
            framed_write.send(buffer.freeze()).await?;

            let element_list = tokio::time::timeout(
                timeout,
                Self::recv_element(
                    framed_read.borrow_mut(),
                    self.channel_key,
                    self.batch_pull_size,
                ),
            )
            .await??;

            let len = element_list.len();
            if len > 0 {
                for element in element_list {
                    debug!("receive remote element: {:?}", element);
                    match self.sender.try_send_opt(element) {
                        Some(t) => send_to_channel(&self.sender, t).await?,
                        None => {}
                    }
                }

                counter.fetch_add(len as u64);
            }
            if len < 100 {
                async_sleep(Duration::from_secs(3)).await;
            }
        }
    }

    async fn recv_element(
        framed_read: &mut FramedRead<ReadHalf<'_>, LengthDelimitedCodec>,
        channel_key: ChannelKey,
        batch_size: u16,
    ) -> anyhow::Result<LinkedList<Element>> {
        if is_enable_log() {
            info!("begin loop recv elements. channel: {:?}", channel_key);
        }
        let mut element_list = LinkedList::new();
        for n in 0..batch_size + 1 {
            let message = framed_read
                .next()
                .await
                .ok_or(anyhow!("framed read nothing"))?;

            let bytes = message.map_err(|e| anyhow!("framed read error {}", e))?;

            let ElementResponse { code, element } = ElementResponse::try_from(bytes)?;

            match code {
                ResponseCode::Ok => {
                    let mut element = element.unwrap();
                    element.set_channel_key(channel_key);
                    element_list.push_back(element);
                }
                ResponseCode::BatchFinish => {
                    if n != batch_size {
                        error!(
                            "inconsistent response and request, channel: {:?}",
                            channel_key
                        );
                    }
                    if is_enable_log() {
                        info!("batch finish, channel: {:?}", channel_key);
                    }
                    return Ok(element_list);
                }
                ResponseCode::Empty => {
                    if is_enable_log() {
                        info!(
                            "recv `Empty` code from remoting, total recv size {}, channel: {:?}",
                            n, channel_key
                        );
                    }

                    return Ok(element_list);
                }
                ResponseCode::NoService => {
                    return Err(anyhow!(
                        "remoting no service. unreachable! after an end flag, the client has close"
                    ));
                }
                _ => {
                    return Err(anyhow!("unrecognized remoting code {:?}", code));
                }
            }
        }

        // Err(anyhow!(
        //     "unreachable, should be interrupted at `Empty` or `BatchFinish` code"
        // ))
        unreachable!()
    }

    // maybe lost data in send/recv buffer
    #[allow(dead_code)]
    pub async fn close(mut self) -> std::io::Result<()> {
        self.stream.shutdown().await
    }

    #[allow(dead_code)]
    pub async fn close_rough(self) {
        match self.close().await {
            Ok(_) => {}
            Err(e) => {
                error!("close client error. {}", e);
            }
        }
    }
}

async fn send_to_channel(sender: &ElementSender, element: Element) -> anyhow::Result<()> {
    let mut ele = element;
    let mut loops = 0;
    loop {
        ele = match sender.try_send(ele) {
            Ok(_) => {
                return Ok(());
            }
            Err(TrySendError::Full(ele)) => {
                if is_enable_log() {
                    error!("> net input channel block, channel: {:?}", ele);
                }

                if loops == 60 {
                    error!("net input channel block and try with 60 times");
                    loops = 0;
                }
                loops += 1;

                async_sleep(Duration::from_secs(1)).await;
                error!("< net input channel block, channel: {:?}", ele);

                ele
            }
            Err(TrySendError::Disconnected(_)) => {
                return Err(anyhow!("client network send to input channel Disconnected. the next job channel must live longer than the client or the application has `Terminated`"));
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::channel::named_channel;
    use crate::core::element::Element;
    use crate::core::runtime::{ChannelKey, JobId, TaskId};
    use crate::pub_sub::network::client::Client;

    #[tokio::test]
    pub async fn client_test() {
        let channel_key = ChannelKey {
            source_task_id: TaskId {
                job_id: JobId(0),
                task_number: 5,
                num_tasks: 30,
            },
            target_task_id: TaskId {
                job_id: JobId(4),
                task_number: 14,
                num_tasks: 30,
            },
        };
        let (sender, receiver) = named_channel::<Element>("test", vec![], 10000);
        std::thread::spawn(move || {
            while let Ok(v) = receiver.recv() {
                println!("{:?}", v);
            }
        });

        let addr = "127.0.0.1:28820".parse().unwrap();

        let mut client = Client::new(channel_key, sender, addr, 100).await.unwrap();
        client.send().await.unwrap();
        client.close().await.unwrap();
    }
}