scopedb-client 0.3.0

A Rust client for ScopeDB
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
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// Copyright 2024 ScopeDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;

use mea::mpsc;
use mea::mutex::Mutex;
use mea::oneshot;
use mea::semaphore::OwnedSemaphorePermit;
use mea::semaphore::Semaphore;
use serde::Serialize;
use tokio::task::JoinHandle;

use crate::Client;
use crate::Error;
use crate::ErrorKind;
use crate::IngestResult;

const DEFAULT_BATCH_BYTES: usize = 16 * 1024 * 1024;
const DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(1);
const DEFAULT_CHANNEL_CAPACITY: usize = 1024;
const DEFAULT_MAX_PENDING_BYTES: usize = DEFAULT_BATCH_BYTES * 4;
const DEFAULT_MAX_RETRIES: usize = 8;
const DEFAULT_INITIAL_BACKOFF: Duration = Duration::from_millis(100);
const DEFAULT_MAX_BACKOFF: Duration = Duration::from_secs(5);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FatalStatus {
    Permanent,
    Temporary,
    Persistent,
}

#[derive(Debug, Clone)]
struct FatalState {
    message: String,
    status: FatalStatus,
}

impl FatalState {
    fn from_error(error: &Error) -> Self {
        let status = if error.is_temporary() {
            FatalStatus::Temporary
        } else if error.is_persistent() {
            FatalStatus::Persistent
        } else {
            FatalStatus::Permanent
        };

        Self {
            message: error.to_string(),
            status,
        }
    }

    fn into_error(self) -> Error {
        let error = Error::new(ErrorKind::Unexpected, self.message);
        match self.status {
            FatalStatus::Temporary => error.set_temporary(),
            FatalStatus::Persistent => error.set_persistent(),
            FatalStatus::Permanent => error.set_permanent(),
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct RetryConfig {
    max_retries: usize,
    initial_backoff: Duration,
    max_backoff: Duration,
}

#[derive(Debug)]
struct BufferedRecord {
    payload: String,
    _reservation: PendingBytesReservation,
}

enum BatchCommand {
    Record(BufferedRecord),
    Flush(oneshot::Sender<Result<Option<IngestResult>, Error>>),
    Shutdown(oneshot::Sender<Result<Option<IngestResult>, Error>>),
}

#[derive(Debug)]
struct PendingBytesBudget {
    capacity: usize,
    semaphore: Arc<Semaphore>,
    closed: AtomicBool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PendingBytesAcquireError {
    Closed,
    ExceedsCapacity { requested: usize, capacity: usize },
}

type PendingBytesReservation = OwnedSemaphorePermit;

impl PendingBytesBudget {
    fn new(capacity: usize) -> Self {
        let capacity = capacity.max(1).min(u32::MAX as usize);
        Self {
            capacity,
            semaphore: Arc::new(Semaphore::new(capacity)),
            closed: AtomicBool::new(false),
        }
    }

    async fn acquire(
        &self,
        requested: usize,
    ) -> Result<PendingBytesReservation, PendingBytesAcquireError> {
        if requested > self.capacity {
            return Err(PendingBytesAcquireError::ExceedsCapacity {
                requested,
                capacity: self.capacity,
            });
        }

        if self.closed.load(Ordering::Acquire) {
            return Err(PendingBytesAcquireError::Closed);
        }

        let reservation = self.semaphore.clone().acquire_owned(requested).await;
        if self.closed.load(Ordering::Acquire) {
            drop(reservation);
            return Err(PendingBytesAcquireError::Closed);
        }

        Ok(reservation)
    }

    fn close(&self) {
        if !self.closed.swap(true, Ordering::AcqRel) {
            self.semaphore.release(self.capacity);
        }
    }
}

impl IngestStreamBuilder {
    pub(crate) fn new(client: Client, statement: String) -> Self {
        Self {
            client,
            statement,
            batch_bytes: DEFAULT_BATCH_BYTES,
            flush_interval: DEFAULT_FLUSH_INTERVAL,
            channel_capacity: DEFAULT_CHANNEL_CAPACITY,
            max_pending_bytes: DEFAULT_MAX_PENDING_BYTES,
            retry: RetryConfig {
                max_retries: DEFAULT_MAX_RETRIES,
                initial_backoff: DEFAULT_INITIAL_BACKOFF,
                max_backoff: DEFAULT_MAX_BACKOFF,
            },
        }
    }

    pub fn batch_bytes(mut self, batch_bytes: usize) -> Self {
        self.batch_bytes = batch_bytes;
        self
    }

    pub fn flush_interval(mut self, flush_interval: Duration) -> Self {
        self.flush_interval = flush_interval;
        self
    }

    pub fn channel_capacity(mut self, channel_capacity: usize) -> Self {
        self.channel_capacity = channel_capacity.max(1);
        self
    }

    pub fn max_pending_bytes(mut self, max_pending_bytes: usize) -> Self {
        self.max_pending_bytes = max_pending_bytes.max(1);
        self
    }

    pub fn max_retries(mut self, max_retries: usize) -> Self {
        self.retry.max_retries = max_retries;
        self
    }

    pub fn initial_backoff(mut self, initial_backoff: Duration) -> Self {
        self.retry.initial_backoff = initial_backoff;
        self
    }

    pub fn max_backoff(mut self, max_backoff: Duration) -> Self {
        self.retry.max_backoff = max_backoff;
        self
    }

    pub fn build(self) -> IngestStream {
        IngestStream::new(
            self.client,
            self.statement,
            self.batch_bytes,
            self.flush_interval,
            self.channel_capacity,
            self.max_pending_bytes,
            self.retry,
        )
    }
}

pub struct IngestStreamBuilder {
    client: Client,
    statement: String,
    batch_bytes: usize,
    flush_interval: Duration,
    channel_capacity: usize,
    max_pending_bytes: usize,
    retry: RetryConfig,
}

pub struct IngestStream {
    tx: mpsc::BoundedSender<BatchCommand>,
    task: Mutex<Option<JoinHandle<()>>>,
    fatal: Arc<Mutex<Option<FatalState>>>,
    pending_bytes: Arc<PendingBytesBudget>,
}

impl IngestStream {
    fn new(
        client: Client,
        statement: String,
        batch_bytes: usize,
        flush_interval: Duration,
        channel_capacity: usize,
        max_pending_bytes: usize,
        retry: RetryConfig,
    ) -> Self {
        let (tx, rx) = mpsc::bounded(channel_capacity.max(1));
        let fatal = Arc::new(Mutex::new(None));
        let pending_bytes = Arc::new(PendingBytesBudget::new(max_pending_bytes.max(1)));
        let task = tokio::spawn(run_batch_worker(
            rx,
            batch_bytes,
            flush_interval,
            retry,
            fatal.clone(),
            pending_bytes.clone(),
            move |rows| {
                let client = client.clone();
                let statement = statement.clone();
                Box::pin(async move { client.insert(rows, statement).await })
            },
        ));

        Self {
            tx,
            task: Mutex::new(Some(task)),
            fatal,
            pending_bytes,
        }
    }

    pub async fn send<T: Serialize>(&self, record: &T) -> Result<(), Error> {
        self.check_fatal().await?;
        let payload = serde_json::to_string(record).map_err(|err| {
            Error::new(
                ErrorKind::Unexpected,
                "failed to serialize batched ingest record".to_string(),
            )
            .set_source(err)
        })?;

        let reserved = match self.pending_bytes.acquire(buffered_bytes(&payload)).await {
            Ok(reserved) => reserved,
            Err(PendingBytesAcquireError::Closed) => {
                return Err(self.closed_or_fatal_error().await);
            }
            Err(PendingBytesAcquireError::ExceedsCapacity {
                requested,
                capacity,
            }) => {
                return Err(
                    Error::new(
                        ErrorKind::Unexpected,
                        format!(
                            "ingest stream record requires {requested} buffered bytes, exceeds max_pending_bytes={capacity}"
                        ),
                    )
                    .set_permanent(),
                );
            }
        };

        if self
            .tx
            .send(BatchCommand::Record(BufferedRecord {
                payload,
                _reservation: reserved,
            }))
            .await
            .is_err()
        {
            return Err(self.closed_or_fatal_error().await);
        }
        self.check_fatal().await
    }

    pub async fn flush(&self) -> Result<Option<IngestResult>, Error> {
        self.check_fatal().await?;
        let (tx, rx) = oneshot::channel();
        if self.tx.send(BatchCommand::Flush(tx)).await.is_err() {
            return Err(self.closed_or_fatal_error().await);
        }
        match rx.await {
            Ok(result) => result,
            Err(_) => Err(self.closed_or_fatal_error().await),
        }
    }

    pub async fn shutdown(self) -> Result<Option<IngestResult>, Error> {
        let (tx, rx) = oneshot::channel();
        if self.tx.send(BatchCommand::Shutdown(tx)).await.is_err() {
            return Err(self.closed_or_fatal_error().await);
        }

        let result = match rx.await {
            Ok(result) => result,
            Err(_) => return Err(self.closed_or_fatal_error().await),
        };
        let task = self.task.lock().await.take();
        if let Some(task) = task {
            task.await.map_err(|err| {
                Error::new(
                    ErrorKind::Unexpected,
                    "ingest stream background task panicked".to_string(),
                )
                .set_source(err)
            })?;
        }
        result
    }

    async fn check_fatal(&self) -> Result<(), Error> {
        if let Some(state) = self.fatal.lock().await.clone() {
            Err(state.into_error())
        } else {
            Ok(())
        }
    }

    async fn closed_or_fatal_error(&self) -> Error {
        self.fatal
            .lock()
            .await
            .clone()
            .map(FatalState::into_error)
            .unwrap_or_else(|| {
                Error::new(ErrorKind::Unexpected, "ingest stream is closed".to_string())
                    .set_persistent()
            })
    }
}

type BoxFutureIngest = Pin<Box<dyn Future<Output = Result<IngestResult, Error>> + Send>>;

async fn run_batch_worker<F>(
    mut rx: mpsc::BoundedReceiver<BatchCommand>,
    batch_bytes: usize,
    flush_interval: Duration,
    retry: RetryConfig,
    fatal: Arc<Mutex<Option<FatalState>>>,
    pending_bytes: Arc<PendingBytesBudget>,
    mut flush_fn: F,
) where
    F: FnMut(String) -> BoxFutureIngest + Send + 'static,
{
    let mut rows = Vec::new();
    let mut current_bytes = 0usize;
    let mut ticker = tokio::time::interval(flush_interval);
    ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);

    loop {
        tokio::select! {
            _ = ticker.tick() => {
                if rows.is_empty() {
                    continue;
                }
                if let Err(err) = flush_pending(&mut rows, &mut current_bytes, retry, &mut flush_fn).await {
                    *fatal.lock().await = Some(FatalState::from_error(&err));
                    break;
                }
            }
            command = rx.recv() => {
                let Ok(command) = command else {
                    if let Err(err) = flush_pending(&mut rows, &mut current_bytes, retry, &mut flush_fn).await {
                        *fatal.lock().await = Some(FatalState::from_error(&err));
                    }
                    break;
                };

                match command {
                    BatchCommand::Record(record) => {
                        if !rows.is_empty() {
                            current_bytes = current_bytes.saturating_add(1);
                        }
                        current_bytes = current_bytes.saturating_add(record.payload.len());
                        rows.push(record);
                        if !rows.is_empty()
                            && current_bytes >= batch_bytes
                            && let Err(err) = flush_pending(
                                &mut rows,
                                &mut current_bytes,
                                retry,
                                &mut flush_fn,
                            )
                            .await
                        {
                            *fatal.lock().await = Some(FatalState::from_error(&err));
                            break;
                        }
                    }
                    BatchCommand::Flush(ack) => {
                        let result = flush_pending(&mut rows, &mut current_bytes, retry, &mut flush_fn).await;
                        if let Err(err) = &result {
                            *fatal.lock().await = Some(FatalState::from_error(err));
                        }
                        let _ = ack.send(result);
                        if fatal.lock().await.is_some() {
                            break;
                        }
                    }
                    BatchCommand::Shutdown(ack) => {
                        let result = flush_pending(&mut rows, &mut current_bytes, retry, &mut flush_fn).await;
                        if let Err(err) = &result {
                            *fatal.lock().await = Some(FatalState::from_error(err));
                        }
                        let _ = ack.send(result);
                        break;
                    }
                }
            }
        }
    }

    pending_bytes.close();
}

async fn flush_pending<F>(
    rows: &mut Vec<BufferedRecord>,
    current_bytes: &mut usize,
    retry: RetryConfig,
    flush_fn: &mut F,
) -> Result<Option<IngestResult>, Error>
where
    F: FnMut(String) -> BoxFutureIngest,
{
    if rows.is_empty() {
        return Ok(None);
    }

    let payload = rows
        .iter()
        .map(|row| row.payload.as_str())
        .collect::<Vec<_>>()
        .join("\n");

    let mut retries = 0usize;
    let mut backoff = retry.initial_backoff;

    loop {
        match flush_fn(payload.clone()).await {
            Ok(result) => {
                rows.clear();
                *current_bytes = 0;
                return Ok(Some(result));
            }
            Err(err) if err.is_temporary() && retries < retry.max_retries => {
                retries += 1;
                if !backoff.is_zero() {
                    tokio::time::sleep(backoff).await;
                }
                backoff = next_backoff(backoff, retry.max_backoff);
            }
            Err(err) if err.is_temporary() => return Err(retry_exhausted_error(retries, err)),
            Err(err) => return Err(err),
        }
    }
}

fn next_backoff(current: Duration, max_backoff: Duration) -> Duration {
    if current.is_zero() {
        return Duration::ZERO;
    }

    match current.checked_mul(2) {
        Some(next) => next.min(max_backoff),
        None => max_backoff,
    }
}

fn retry_exhausted_error(retries: usize, err: Error) -> Error {
    let last_error = err.to_string();
    Error::new(
        ErrorKind::Unexpected,
        "ingest stream flush exhausted retry budget".to_string(),
    )
    .with_context("retries", retries)
    .with_context("last_error", last_error)
    .set_source(err)
    .set_persistent()
}

fn buffered_bytes(payload: &str) -> usize {
    payload.len().saturating_add(1)
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::AtomicUsize;
    use std::sync::atomic::Ordering;

    use super::*;

    fn test_error(message: &str) -> Error {
        Error::new(ErrorKind::Unexpected, message.to_string())
    }

    fn temporary_error(message: &str) -> Error {
        test_error(message).set_temporary()
    }

    async fn test_record(payload: &str) -> BufferedRecord {
        let budget = Arc::new(PendingBytesBudget::new(1024));
        let reservation = budget
            .acquire(buffered_bytes(payload))
            .await
            .expect("reservation should succeed");
        BufferedRecord {
            payload: payload.to_string(),
            _reservation: reservation,
        }
    }

    fn test_retry() -> RetryConfig {
        RetryConfig {
            max_retries: 2,
            initial_backoff: Duration::ZERO,
            max_backoff: Duration::ZERO,
        }
    }

    #[tokio::test]
    async fn test_flush_pending_joins_rows() {
        let mut rows = vec![
            test_record("{\"a\":1}").await,
            test_record("{\"a\":2}").await,
        ];
        let mut current_bytes = rows.iter().map(|row| row.payload.len()).sum::<usize>() + 1;
        let result = flush_pending(
            &mut rows,
            &mut current_bytes,
            test_retry(),
            &mut |payload| {
                Box::pin(async move {
                    assert_eq!(payload, "{\"a\":1}\n{\"a\":2}");
                    Ok(IngestResult {
                        num_rows_inserted: 2,
                    })
                })
            },
        )
        .await
        .unwrap();

        assert_eq!(result.unwrap().num_rows_inserted, 2);
        assert!(rows.is_empty());
        assert_eq!(current_bytes, 0);
    }

    #[tokio::test]
    async fn test_flush_pending_empty_batch() {
        let mut rows = Vec::new();
        let mut current_bytes = 0;
        let result = flush_pending(
            &mut rows,
            &mut current_bytes,
            test_retry(),
            &mut |_payload| Box::pin(async move { Err(test_error("must not be called")) }),
        )
        .await
        .unwrap();

        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_flush_pending_keeps_batch_on_error() {
        let mut rows = vec![
            test_record("{\"a\":1}").await,
            test_record("{\"a\":2}").await,
        ];
        let original_rows = rows
            .iter()
            .map(|row| row.payload.clone())
            .collect::<Vec<_>>();
        let mut current_bytes = rows.iter().map(|row| row.payload.len()).sum::<usize>() + 1;

        let err = flush_pending(
            &mut rows,
            &mut current_bytes,
            test_retry(),
            &mut |_payload| Box::pin(async move { Err(test_error("flush failed")) }),
        )
        .await
        .unwrap_err();

        assert_eq!(err.to_string(), test_error("flush failed").to_string());
        assert_eq!(
            rows.iter()
                .map(|row| row.payload.as_str())
                .collect::<Vec<_>>(),
            original_rows
                .iter()
                .map(|row| row.as_str())
                .collect::<Vec<_>>()
        );
        assert_eq!(
            current_bytes,
            original_rows.iter().map(|row| row.len()).sum::<usize>() + 1
        );
    }

    #[tokio::test]
    async fn test_flush_pending_retries_temporary_errors() {
        let mut rows = vec![test_record("{\"a\":1}").await];
        let mut current_bytes = rows[0].payload.len();
        let attempts = Arc::new(AtomicUsize::new(0));

        let result = flush_pending(&mut rows, &mut current_bytes, test_retry(), &mut {
            let attempts = attempts.clone();
            move |_payload| {
                let attempts = attempts.clone();
                Box::pin(async move {
                    let attempt = attempts.fetch_add(1, Ordering::SeqCst);
                    if attempt < 2 {
                        Err(temporary_error("retry me"))
                    } else {
                        Ok(IngestResult {
                            num_rows_inserted: 1,
                        })
                    }
                })
            }
        })
        .await
        .unwrap()
        .unwrap();

        assert_eq!(attempts.load(Ordering::SeqCst), 3);
        assert_eq!(result.num_rows_inserted, 1);
        assert!(rows.is_empty());
        assert_eq!(current_bytes, 0);
    }

    #[tokio::test]
    async fn test_flush_pending_exhausts_retry_budget() {
        let mut rows = vec![test_record("{\"a\":1}").await];
        let mut current_bytes = rows[0].payload.len();

        let err = flush_pending(
            &mut rows,
            &mut current_bytes,
            test_retry(),
            &mut |_payload| Box::pin(async move { Err(temporary_error("still rate limited")) }),
        )
        .await
        .unwrap_err();

        assert!(err.is_persistent());
        assert!(err.to_string().contains("retry budget"));
        assert_eq!(rows.len(), 1);
        assert_eq!(current_bytes, rows[0].payload.len());
    }

    #[tokio::test]
    async fn test_pending_bytes_budget_blocks_until_release() {
        let budget = Arc::new(PendingBytesBudget::new(8));
        let reservation = budget.acquire(8).await.unwrap();

        let waiter = tokio::spawn({
            let budget = budget.clone();
            async move { budget.acquire(1).await }
        });

        tokio::time::sleep(Duration::from_millis(10)).await;
        assert!(!waiter.is_finished());

        drop(reservation);

        let reservation = tokio::time::timeout(Duration::from_secs(1), waiter)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
        drop(reservation);
    }

    #[tokio::test]
    async fn test_pending_bytes_budget_close_unblocks_waiters() {
        let budget = Arc::new(PendingBytesBudget::new(8));
        let reservation = budget.acquire(8).await.unwrap();

        let waiter = tokio::spawn({
            let budget = budget.clone();
            async move { budget.acquire(1).await }
        });

        tokio::time::sleep(Duration::from_millis(10)).await;
        budget.close();
        drop(reservation);

        let result = tokio::time::timeout(Duration::from_secs(1), waiter)
            .await
            .unwrap()
            .unwrap();
        assert!(matches!(result, Err(PendingBytesAcquireError::Closed)));
    }
}