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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
pub mod error;
pub use error::{LiftbridgeError, Result};

mod api {
    include!(concat!(env!("OUT_DIR"), "/proto.rs"));
}
pub mod metadata {
    use crate::api::{FetchMetadataResponse, StreamMetadata};
    use crate::{LiftbridgeError, Result};
    use chrono::{DateTime, Utc};
    use rand::Rng;
    use std::collections::HashMap;
    use std::sync::{RwLock, RwLockReadGuard};

    struct Metadata {
        last_updated: DateTime<Utc>,
        brokers: HashMap<String, String>,
        streams: HashMap<String, StreamMetadata>,
    }

    impl Default for Metadata {
        fn default() -> Self {
            Metadata {
                last_updated: Utc::now(),
                brokers: HashMap::new(),
                streams: HashMap::new(),
            }
        }
    }

    impl Metadata {
        pub fn last_updated(&self) -> DateTime<Utc> {
            self.last_updated
        }

        fn get_addrs(&self) -> Vec<String> {
            self.brokers
                .values()
                .into_iter()
                .map(|s| s.to_string())
                .collect()
        }
    }

    pub struct MetadataCache {
        metadata: RwLock<Metadata>,
        bootstrap_addrs: Vec<String>,
    }

    impl MetadataCache {
        pub fn new(addrs: Vec<String>) -> Self {
            MetadataCache {
                metadata: RwLock::new(Metadata::default()),
                bootstrap_addrs: addrs,
            }
        }

        pub fn update(&self, resp: FetchMetadataResponse) {
            let mut brokers: HashMap<String, String> = HashMap::new();
            let mut streams: HashMap<String, StreamMetadata> = HashMap::new();

            for b in resp.brokers {
                let addr = format!("{}:{}", b.host, b.port);
                brokers.insert(b.id, addr);
            }

            for s in resp.metadata {
                streams.insert(s.name.clone(), s);
            }

            let mut metadata = self.metadata.write().unwrap();
            metadata.streams = streams;
            metadata.brokers = brokers;
            metadata.last_updated = Utc::now();
        }

        pub fn get_addrs(&self) -> Vec<String> {
            let mut addrs = self.bootstrap_addrs.clone();
            let cached_addrs = &mut self.metadata.read().unwrap().get_addrs();

            addrs.append(cached_addrs);
            addrs
        }

        pub fn get_addr(
            &self,
            stream: &str,
            partition: i32,
            read_isr_replica: bool,
        ) -> Result<String> {
            let metadata: RwLockReadGuard<Metadata> = self.metadata.read().unwrap();
            let stream = metadata
                .streams
                .get(stream)
                .ok_or(LiftbridgeError::NoSuchStream)?;
            let partition = stream
                .partitions
                .get(&partition)
                .ok_or(LiftbridgeError::NoSuchPartition)?;

            if read_isr_replica {
                let replicas = &partition.isr;
                let replica = rand::thread_rng().gen_range(0, replicas.len());

                let replica = replicas.get(replica).unwrap();
                return Ok(metadata.brokers.get(replica).unwrap().clone());
            }

            if partition.leader.is_empty() {
                Err(LiftbridgeError::NoLeader)?
            }

            Ok(metadata.brokers.get(&partition.leader).unwrap().clone())
        }
    }
}

pub mod client {
    use crate::api::api_client::ApiClient;
    use crate::api::{
        AckPolicy, CreateStreamRequest, CreateStreamResponse, DeleteStreamRequest,
        DeleteStreamResponse, FetchMetadataRequest, FetchMetadataResponse, Message,
        PauseStreamRequest, PauseStreamResponse, PublishRequest, PublishResponse, StartPosition,
        SubscribeRequest,
    };
    use crate::{LiftbridgeError, Result};
    use std::convert::TryFrom;

    use std::time::Duration;

    use chrono::{DateTime, Utc};

    use crate::metadata::MetadataCache;

    use lru::LruCache;
    use rand::seq::SliceRandom;
    use rand::{thread_rng, Rng};
    use std::collections::HashMap;
    use std::sync::{Mutex, RwLock};
    use tonic::transport::{Channel, Endpoint};
    use tonic::{Code, Status, Streaming};

    // To be implemented via load-balancing two endpoints connected to the same broker
    const MAX_BROKER_CONNECTIONS: usize = 2;
    const KEEP_ALIVE_DURATION: Duration = Duration::from_secs(30);
    const RESUBSCRIBE_WAIT_TIME: Duration = Duration::from_secs(30);
    const ACK_WAIT_TIME: Duration = Duration::from_secs(5);
    const NO_OFFSET: i64 = -1;
    const DEFAULT_POOL_SIZE: usize = 100;

    pub struct Config {
        timeout: Option<Duration>,
    }

    #[derive(Clone)]
    enum Request {
        CreateStream(CreateStreamRequest),
        PauseStream(PauseStreamRequest),
        DeleteStream(DeleteStreamRequest),
        Subscribe(SubscribeRequest),
        FetchMetadata(FetchMetadataRequest),
        Publish(PublishRequest),
    }

    enum Response {
        CreateStream(tonic::Response<CreateStreamResponse>),
        PauseStream(tonic::Response<PauseStreamResponse>),
        DeleteStream(tonic::Response<DeleteStreamResponse>),
        FetchMetadata(tonic::Response<FetchMetadataResponse>),
        Subscribe(tonic::Response<Streaming<Message>>),
        Publish(tonic::Response<PublishResponse>),
    }

    //TODO: Make a builder for this
    // consider using https://github.com/colin-kiegel/rust-derive-builder
    #[derive(Default)]
    pub struct StreamOptions {
        pub group: String,
        pub replication_factor: i32,
        pub partitions: i32,
        pub retention_max_bytes: Option<i64>,
        pub retention_max_messages: Option<i64>,
        pub retention_max_age: Option<Duration>,
        pub cleaner_interval: Option<Duration>,
        pub segment_max_bytes: Option<i64>,
        pub segment_max_age: Option<Duration>,
        pub compact_max_goroutines: Option<i32>,
        pub compact_enabled: Option<bool>,
    }

    #[derive(Default)]
    pub struct PauseOptions {
        pub partitions: Vec<i32>,
        pub resume_all: bool,
    }

    pub struct Client {
        //TODO: cleanup the pool on metadata update or pool access
        // to avoid leaking memory
        pool: Mutex<LruCache<String, ApiClient<Channel>>>,
        client: RwLock<ApiClient<Channel>>,
        metadata: MetadataCache,
    }

    #[derive(Clone)]
    pub struct SubscriptionOptions {
        start_position: StartPosition,
        start_offset: i64,
        start_timestamp: i64,
        partition: i32,
        read_isr_replica: bool,
    }

    pub struct Subscription<'a> {
        last_offset: i64,
        stream_name: &'a str,
        stream: Streaming<Message>,
        client: &'a Client,
        options: SubscriptionOptions,
    }

    impl<'a> Subscription<'a> {
        pub(crate) async fn _next(&mut self) -> Result<Option<Message>, Status> {
            self.stream.message().await
        }

        pub async fn next(&mut self) -> Result<Option<Message>> {
            let deadline: DateTime<Utc> =
                Utc::now() + chrono::Duration::from_std(RESUBSCRIBE_WAIT_TIME).unwrap();
            loop {
                if deadline.lt(&Utc::now()) {
                    break;
                }
                let msg: Result<Option<Message>, Status> = self._next().await;

                if let Ok(message) = msg {
                    return match message {
                        Some(msg) => {
                            self.last_offset = msg.offset;
                            Ok(Some(msg))
                        }
                        _ => Ok(message),
                    };
                }

                if let Err(err) = msg {
                    if err.code() == Code::Unavailable {
                        let mut options = self.options.clone();

                        if self.last_offset != NO_OFFSET {
                            options.start_offset = self.last_offset + 1;
                            options.start_position = StartPosition::Offset;
                        }

                        let sub = self
                            .client
                            .subscribe(self.stream_name, self.options.clone())
                            .await;
                        match sub {
                            Err(e) => {
                                if let LiftbridgeError::UnableToSubscribe = e {
                                    tokio::time::delay_for(
                                        Duration::from_secs(1)
                                            + Duration::from_millis(
                                                rand::thread_rng().gen_range(1, 500),
                                            ),
                                    )
                                    .await;
                                    continue;
                                }
                                Err(e)?
                            }
                            Ok(sub) => self.stream = sub.stream,
                        }
                        continue;
                    }
                    return Err(match err.code() {
                        Code::NotFound => LiftbridgeError::StreamDeleted,
                        Code::FailedPrecondition => LiftbridgeError::PartitionPaused,
                        _ => LiftbridgeError::GrpcError(err),
                    });
                }
            }

            return Err(LiftbridgeError::UnableToSubscribe);
        }
    }

    impl Default for SubscriptionOptions {
        fn default() -> Self {
            Self {
                start_position: StartPosition::NewOnly,
                start_offset: 0,
                start_timestamp: 0,
                read_isr_replica: false,
                partition: 0,
            }
        }
    }

    impl SubscriptionOptions {
        pub fn start_at_offset(mut self, offset: i64) -> Self {
            self.start_position = StartPosition::Offset;
            self.start_offset = offset;
            self
        }

        pub fn start_at_time(mut self, time: chrono::DateTime<Utc>) -> Self {
            self.start_position = StartPosition::Timestamp;
            self.start_timestamp = time.timestamp();
            self
        }

        pub fn start_at_time_delta(mut self, ago: Duration) -> Self {
            self.start_position = StartPosition::Timestamp;
            self.start_timestamp = Utc::now().timestamp() - ago.as_secs() as i64;
            self
        }

        pub fn start_at_earliest(mut self) -> Self {
            self.start_position = StartPosition::Earliest;
            self
        }

        pub fn partition(mut self, partition: u32) -> Self {
            self.partition = partition as i32;
            self
        }
    }

    trait Partitioner {
        // fn partition(stream: &str, key: Vec<u8>, value: Vec<u8>, metadata: Option<Metadata>)
    }

    struct MessageOptions {
        // Key to set on the Message. If Liftbridge has stream compaction enabled,
        // the stream will retain only the last value for each key.
        key: Vec<u8>,

        // AckInbox sets the NATS subject Liftbridge should publish the Message ack
        // to. If this is not set, the server will generate a random inbox.
        ack_inbox: String,

        // correlation_id sets the identifier used to correlate an ack with the
        // published Message. If this is not set, the ack will not have a
        // correlation id.
        correlation_id: String,

        // ack_policy controls the behavior of Message acks sent by the server. By
        // default, Liftbridge will send an ack when the stream leader has written
        // the Message to its write-ahead log.
        ack_policy: AckPolicy,

        // Headers are key-value pairs to set on the Message.
        headers: HashMap<String, Vec<u8>>,

        // Partitioner specifies the strategy for mapping a Message to a stream
        // partition.
        // partitioner: Box<dyn Partitioner>,

        // Partition specifies the stream partition to publish the Message to. If
        // this is set, any Partitioner will not be used. This is a pointer to
        // allow distinguishing between unset and 0.
        partition: Option<i32>,
    }

    impl Client {
        pub async fn new(addrs: Vec<&str>) -> Result<Client> {
            let mut addrs = addrs.into_iter().map(String::from).collect();
            let client = Client {
                pool: Mutex::new(LruCache::new(DEFAULT_POOL_SIZE)),
                client: RwLock::new(Client::connect_any(&mut addrs).await?),
                metadata: MetadataCache::new(addrs),
            };
            Ok(client)
        }

        async fn change_broker(&self) -> Result<()> {
            let new_client = Client::connect_any(&mut self.metadata.get_addrs()).await?;
            let mut client = self.client.write().unwrap();
            *client = new_client;
            Ok(())
        }

        async fn connect_any(addrs: &mut Vec<String>) -> Result<ApiClient<Channel>> {
            addrs.shuffle(&mut thread_rng());
            for addr in addrs.iter() {
                let client = Self::connect(addr).await;
                match client {
                    Ok(_) => return client,
                    _ => continue,
                }
            }
            Err(LiftbridgeError::BrokersUnavailable)?
        }

        async fn connect(addr: &str) -> Result<ApiClient<Channel>> {
            let endpoint = Endpoint::try_from(format!("grpc://{}", addr))
                .map(|e| e.tcp_keepalive(Some(KEEP_ALIVE_DURATION)))
                .unwrap();
            let channel = endpoint.connect().await;
            Ok(channel.map(|chan| ApiClient::new(chan))?)
        }

        async fn request(&self, msg: Request) -> Result<Response> {
            for _ in 0..9 {
                let client = self.client.read().unwrap();
                let res = self._request(client.clone(), &msg).await;
                match res {
                    Err(LiftbridgeError::GrpcError(status))
                        if status.code() == tonic::Code::Unavailable =>
                    {
                        drop(client);
                        self.change_broker().await?;
                        continue;
                    }
                    _ => return res,
                }
            }
            return Err(LiftbridgeError::BrokersUnavailable.into());
        }

        async fn _request(
            &self,
            mut client: ApiClient<Channel>,
            msg: &Request,
        ) -> Result<Response> {
            // TODO: it would be nice to do without the clone
            let res = match msg.clone() {
                Request::Publish(req) => client.publish(req).await.map(Response::Publish)?,
                Request::CreateStream(req) => client
                    .create_stream(tonic::Request::new(req))
                    .await
                    .map_err(|err| match err.code() {
                        tonic::Code::AlreadyExists => LiftbridgeError::StreamExists,
                        _ => LiftbridgeError::from(err),
                    })
                    .map(Response::CreateStream)?,
                Request::PauseStream(req) => client
                    .pause_stream(tonic::Request::new(req))
                    .await
                    .map_err(|err| match err.code() {
                        tonic::Code::NotFound => LiftbridgeError::NoSuchPartition,
                        _ => LiftbridgeError::from(err),
                    })
                    .map(Response::PauseStream)?,
                Request::DeleteStream(req) => client
                    .delete_stream(tonic::Request::new(req))
                    .await
                    .map_err(|err| match err.code() {
                        tonic::Code::NotFound => LiftbridgeError::NoSuchStream,
                        _ => LiftbridgeError::from(err),
                    })
                    .map(Response::DeleteStream)?,
                Request::Subscribe(req) => client
                    .subscribe(tonic::Request::new(req))
                    .await
                    .map(Response::Subscribe)?,
                Request::FetchMetadata(req) => client
                    .fetch_metadata(tonic::Request::new(req))
                    .await
                    .map_err(LiftbridgeError::from)
                    .map(Response::FetchMetadata)?,
            };
            Ok(res)
        }

        //TODO: This is subject to change as it needs to receive StreamOptions
        pub async fn create_stream(&self, subject: &str, name: &str) -> Result<()> {
            let req = CreateStreamRequest {
                subject: subject.to_string(),
                name: name.to_string(),
                ..Default::default()
            };
            self.request(Request::CreateStream(req)).await?;
            Ok(())
        }

        pub async fn delete_stream(&self, name: &str) -> Result<()> {
            let req = DeleteStreamRequest {
                name: name.to_string(),
            };
            self.request(Request::DeleteStream(req)).await?;
            Ok(())
        }

        pub async fn pause_stream(&self, name: &str, options: PauseOptions) -> Result<()> {
            let req = PauseStreamRequest {
                name: name.to_string(),
                partitions: options.partitions,
                resume_all: options.resume_all,
            };

            self.request(Request::PauseStream(req)).await?;
            Ok(())
        }

        pub async fn subscribe<'a>(
            &'a self,
            stream: &'a str,
            options: SubscriptionOptions,
        ) -> Result<Subscription<'a>> {
            let req = SubscribeRequest {
                partition: options.partition,
                start_timestamp: options.start_timestamp,
                start_offset: options.start_offset,
                start_position: options.start_position.into(),
                stream: stream.to_string(),
                read_isr_replica: options.read_isr_replica,
            };

            for i in 0..4 {
                let client = self
                    .get_conn(stream, options.partition, options.read_isr_replica)
                    .await;
                match client {
                    Err(_) => {
                        tokio::time::delay_for(Duration::from_millis(50)).await;
                        //TODO: Ignore the result of this, if it fails on all the retries
                        // this will result in a general failure
                        self.update_metadata().await;
                    }
                    Ok(client) => {
                        let sub = self
                            ._request(client, &Request::Subscribe(req.clone()))
                            .await;
                        match sub {
                            Ok(resp) => {
                                if let Response::Subscribe(resp) = resp {
                                    let mut sub = Subscription {
                                        stream: resp.into_inner(),
                                        stream_name: stream,
                                        client: self,
                                        last_offset: NO_OFFSET,
                                        options: options.clone(),
                                    };
                                    // if the subscription is a success - the server sends
                                    // an empty message first
                                    let msg = sub._next().await;
                                    match msg {
                                        Err(status) => {
                                            if status.code() == tonic::Code::FailedPrecondition {
                                                // ignore the result, this will result in general failure
                                                tokio::time::delay_for(Duration::from_millis(
                                                    10 + i * 50,
                                                ))
                                                .await;
                                                self.update_metadata().await;
                                                continue;
                                            }

                                            if status.code() == tonic::Code::NotFound {
                                                Err(LiftbridgeError::NoSuchPartition)?
                                            }
                                        }
                                        _ => (),
                                    }
                                    return Ok(sub);
                                }
                                //This should never happen,
                                panic!("Subscribe request should return an appropriate response")
                            }
                            Err(LiftbridgeError::GrpcError(status))
                                if status.code() == tonic::Code::Unavailable =>
                            {
                                tokio::time::delay_for(Duration::from_millis(50)).await;
                                // ignore the result on purpose as we will fail eventually anyway
                                self.update_metadata().await;
                                continue;
                            }
                            _ => {
                                sub?;
                            }
                        }
                    }
                }
            }
            Err(LiftbridgeError::UnableToSubscribe)?
        }

        async fn update_metadata(&self) -> Result<()> {
            let resp = self._fetch_metadata().await?;
            self.metadata.update(resp);
            Ok(())
        }

        pub async fn fetch_metadata(&self) {
            unimplemented!("fetch_metadata is currently not implemented")
        }

        //TODO: subject to change, need to support MessageOptions and proper Ack handling
        pub async fn publish(&self, stream: &str, value: Vec<u8>) -> Result<()> {
            let req = PublishRequest {
                value,
                stream: stream.to_string(),
                ack_policy: AckPolicy::Leader as i32,
                partition: 0,
                headers: HashMap::new(),
                ack_inbox: "".to_string(),
                correlation_id: "".to_string(),
                key: Vec::new(),
            };
            self.request(Request::Publish(req)).await?;
            Ok(())
        }

        pub(crate) async fn _fetch_metadata(&self) -> Result<FetchMetadataResponse> {
            let req = FetchMetadataRequest {
                streams: Vec::new(),
            };
            self.request(Request::FetchMetadata(req))
                .await
                .map(|resp| match resp {
                    Response::FetchMetadata(r) => r.into_inner(),
                    _ => panic!("wrong response type"),
                })
        }

        async fn get_conn(
            &self,
            stream: &str,
            partition: i32,
            read_isr_replica: bool,
        ) -> Result<ApiClient<Channel>> {
            let addr = self
                .metadata
                .get_addr(stream, partition, read_isr_replica)?;

            let mut pool = self.pool.lock().unwrap();
            if pool.contains(&addr) {
                return Ok(pool.get(&addr).unwrap().clone());
            }

            let client = Self::connect(&addr).await?;
            pool.put(addr.clone(), client);
            Ok(pool.get(&addr).unwrap().clone())
        }
    }
}