opendata-buffer 0.2.0

Stateless object storage buffer library for OpenData systems
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};

use bytes::Bytes;
use common::clock::Clock;
use slatedb::object_store::path::Path;
use slatedb::object_store::{ObjectStore, PutPayload};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

use crate::config::ProducerConfig;
use crate::error::{Error, Result};
use crate::metric_names;
use crate::model::{CompressionType, encode_batch};
use crate::queue::{Metadata, QueueProducer};
use crate::util::millis;

type Notifier = tokio::sync::watch::Sender<Option<Result<()>>>;

#[derive(Clone)]
pub struct DurabilityWatcher {
    receiver: tokio::sync::watch::Receiver<Option<Result<()>>>,
}

impl DurabilityWatcher {
    /// Return the outcome of the write if the batch has already been flushed,
    /// or `None` if the flush has not completed yet.
    pub fn result(&self) -> Option<Result<()>> {
        self.receiver.borrow().clone()
    }

    /// Wait until the batch containing this write has been durably flushed.
    pub async fn await_durable(&mut self) -> Result<()> {
        self.receiver
            .wait_for(|v| v.is_some())
            .await
            .map_err(|_| Error::Storage("buffer shut down".to_string()))?
            .clone()
            .expect("value must be present after wait_for")
    }
}

pub struct WriteHandle {
    pub watcher: DurabilityWatcher,
    pub ingestion_time_ms: i64,
}

enum ProduceMessage {
    Add {
        entries: Vec<Bytes>,
        metadata: Bytes,
        ingestion_time_ms: i64,
        notifier: Notifier,
    },
    Flush {
        result_sender: tokio::sync::oneshot::Sender<Result<()>>,
    },
}

#[derive(Default)]
struct DataAndNotifiers {
    entries: Vec<Bytes>,
    metadata: Vec<Metadata>,
    notifiers: Vec<Notifier>,
}

impl DataAndNotifiers {
    fn add(
        &mut self,
        entries: Vec<Bytes>,
        metadata: Bytes,
        ingestion_time_ms: i64,
        notifier: Notifier,
    ) -> Result<()> {
        let start_index = self.entries.len() as u32;
        self.entries.extend(entries);
        if self.entries.len() > u32::MAX as usize {
            return Err(Error::InvalidInput(format!(
                "batch entry count {} exceeds u32::MAX",
                self.entries.len()
            )));
        }
        self.metadata.push(Metadata {
            start_index,
            ingestion_time_ms,
            payload: metadata,
        });
        self.notifiers.push(notifier);
        Ok(())
    }

    fn is_empty(&self) -> bool {
        self.entries.is_empty() && self.metadata.is_empty() && self.notifiers.is_empty()
    }
}

struct Batch {
    data_and_notifiers: DataAndNotifiers,
    size_bytes: usize,
    started_at: Option<SystemTime>,
}

impl Batch {
    fn new() -> Self {
        Self {
            data_and_notifiers: DataAndNotifiers::default(),
            size_bytes: 0,
            started_at: None,
        }
    }

    fn add(
        &mut self,
        entries: Vec<Bytes>,
        metadata: Bytes,
        ingestion_time_ms: i64,
        notifier: Notifier,
        now: SystemTime,
    ) -> Result<()> {
        let mut entry_size_sum = 0usize;
        for e in &entries {
            if e.len() > u32::MAX as usize {
                return Err(Error::InvalidInput(format!(
                    "entry size {} exceeds u32::MAX",
                    e.len()
                )));
            }
            entry_size_sum += e.len();
        }
        self.size_bytes += entry_size_sum + metadata.len();
        self.data_and_notifiers
            .add(entries, metadata, ingestion_time_ms, notifier)?;
        if self.started_at.is_none() {
            self.started_at = Some(now);
        }
        Ok(())
    }

    fn take(&mut self) -> DataAndNotifiers {
        self.size_bytes = 0;
        self.started_at = None;
        std::mem::take(&mut self.data_and_notifiers)
    }

    fn is_empty(&self) -> bool {
        self.data_and_notifiers.is_empty()
    }
}

struct BatchWriterTask {
    object_store: Arc<dyn ObjectStore>,
    producer: Arc<QueueProducer>,
    data_path_prefix: String,
    flush_interval: Duration,
    flush_size_bytes: usize,
    batch_compression: CompressionType,
    batch: Batch,
    clock: Arc<dyn Clock>,
}

impl BatchWriterTask {
    fn new(
        object_store: Arc<dyn ObjectStore>,
        producer: Arc<QueueProducer>,
        data_path_prefix: String,
        flush_interval: Duration,
        flush_size_bytes: usize,
        batch_compression: CompressionType,
        clock: Arc<dyn Clock>,
    ) -> Self {
        Self {
            object_store,
            producer,
            data_path_prefix,
            flush_interval,
            flush_size_bytes,
            batch_compression,
            batch: Batch::new(),
            clock,
        }
    }

    async fn run(&mut self, mut rx: mpsc::Receiver<ProduceMessage>, shutdown: CancellationToken) {
        loop {
            let sleep_duration = match self.batch.started_at {
                Some(started) => (started + self.flush_interval)
                    .duration_since(self.clock.now())
                    .unwrap_or(Duration::ZERO),
                None => self.flush_interval,
            };

            tokio::select! {
                biased;
                _ = shutdown.cancelled() => {
                    return;
                },
                msg = rx.recv() => {
                    match msg {
                        Some(ProduceMessage::Add { entries, metadata, ingestion_time_ms, notifier }) => {
                            if let Err(e) = self.batch.add(entries, metadata, ingestion_time_ms, notifier.clone(), self.clock.now()) {
                                let _ = notifier.send(Some(Err(e)));
                            } else if self.batch.size_bytes >= self.flush_size_bytes {
                                let _ = self.write_batch().await;
                            }
                        }
                        Some(ProduceMessage::Flush { result_sender }) => {
                            let _ = result_sender.send(self.write_batch().await);
                        }
                        None => break,
                    }
                },
                _ = tokio::time::sleep(sleep_duration) => {
                    let _ = self.write_batch().await;
                },
            }
        }
    }

    async fn write_batch(&mut self) -> Result<()> {
        if self.batch.is_empty() {
            return Ok(());
        }
        let data_and_notifiers = self.batch.take();
        let result = self
            .write_and_enqueue(data_and_notifiers.entries, data_and_notifiers.metadata)
            .await;

        for notifier in data_and_notifiers.notifiers {
            let _ = notifier.send(Some(result.clone()));
        }

        result
    }

    async fn write_and_enqueue(&self, entries: Vec<Bytes>, metadata: Vec<Metadata>) -> Result<()> {
        let start = Instant::now();
        let raw_bytes: u64 = entries.iter().map(|e| e.len() as u64).sum();
        let entry_count = entries.len() as u64;

        let payload = encode_batch(&entries, self.batch_compression)?;
        let written_bytes = payload.len() as u64;

        let id = ulid::Ulid::new();
        let path = Path::from(format!("{}/{}.batch", self.data_path_prefix, id));
        self.object_store
            .put(&path, PutPayload::from(payload))
            .await
            .map_err(|e| Error::Storage(e.to_string()))?;

        self.producer.enqueue(path.to_string(), metadata).await?;

        metrics::counter!(metric_names::BATCHES_FLUSHED).increment(1);
        metrics::counter!(metric_names::ENTRIES_FLUSHED).increment(entry_count);
        metrics::counter!(metric_names::BYTES_FLUSHED).increment(raw_bytes);
        metrics::counter!(metric_names::BYTES_WRITTEN).increment(written_bytes);
        metrics::histogram!(metric_names::FLUSH_DURATION_SECONDS)
            .record(start.elapsed().as_secs_f64());

        Ok(())
    }
}

struct BatchWriter {
    producer: Arc<QueueProducer>,
    sender: mpsc::Sender<ProduceMessage>,
    cancellation_token: CancellationToken,
    handle: tokio::task::JoinHandle<()>,
}

impl BatchWriter {
    fn new(
        object_store: Arc<dyn ObjectStore>,
        config: &ProducerConfig,
        clock: Arc<dyn Clock>,
    ) -> Self {
        let (sender, receiver) = mpsc::channel(config.max_buffered_inputs);
        let producer = Arc::new(QueueProducer::with_object_store(
            config.manifest_path.clone(),
            object_store.clone(),
        ));
        let mut task = BatchWriterTask::new(
            object_store,
            producer.clone(),
            config.data_path_prefix.clone(),
            config.flush_interval,
            config.flush_size_bytes,
            config.batch_compression,
            clock,
        );
        let shutdown = CancellationToken::new();
        let cancellation_token = shutdown.clone();
        let handle = tokio::spawn(async move { task.run(receiver, shutdown).await });
        Self {
            producer,
            sender,
            cancellation_token,
            handle,
        }
    }

    async fn add(
        &self,
        entries: Vec<Bytes>,
        metadata: Bytes,
        ingestion_time_ms: i64,
    ) -> Result<DurabilityWatcher> {
        let (notifier_sender, notifier_receiver) = tokio::sync::watch::channel(None);
        self.sender
            .send(ProduceMessage::Add {
                entries,
                metadata,
                ingestion_time_ms,
                notifier: notifier_sender,
            })
            .await
            .map_err(|_| Error::Storage("buffer shut down".to_string()))?;
        Ok(DurabilityWatcher {
            receiver: notifier_receiver,
        })
    }

    async fn flush(&self) -> Result<()> {
        let (result_sender, result_receiver) = tokio::sync::oneshot::channel();
        self.sender
            .send(ProduceMessage::Flush { result_sender })
            .await
            .map_err(|_| Error::Storage("batch writer task not running".to_string()))?;
        result_receiver
            .await
            .map_err(|_| Error::Storage("batch writer task not running".to_string()))?
    }

    fn conflict_rate(&self) -> f64 {
        self.producer.conflict_rate()
    }
    async fn close(self) -> Result<()> {
        self.flush().await?;
        self.cancellation_token.cancel();
        let _ = self.handle.await;
        Ok(())
    }
}

pub struct Producer {
    writer: BatchWriter,
    clock: Arc<dyn Clock>,
}

impl Producer {
    /// Create a new buffer from the given configuration and clock.
    pub fn new(config: ProducerConfig, clock: Arc<dyn Clock>) -> Result<Self> {
        let object_store = common::storage::factory::create_object_store(&config.object_store)
            .map_err(|e| Error::Storage(e.to_string()))?;
        Self::with_object_store(config, object_store, clock)
    }

    /// Create a new buffer using a pre-built object store.
    ///
    /// This is useful when you need to share an object store instance
    /// between a `Producer` and a [`Consumer`](crate::Consumer) (e.g. in tests).
    pub fn with_object_store(
        config: ProducerConfig,
        object_store: Arc<dyn ObjectStore>,
        clock: Arc<dyn Clock>,
    ) -> Result<Self> {
        metric_names::describe_buffer_metrics();
        let writer = BatchWriter::new(object_store, &config, clock.clone());
        Ok(Self { writer, clock })
    }

    /// Submit a set of entries and associated metadata for ingestion.
    ///
    /// Returns a [`WriteHandle`] that can be used to check or await durability.
    /// Applies backpressure when the message buffer is full.
    ///
    /// Returns [`Error::InvalidInput`] if `entries` is empty or if `metadata`
    /// exceeds 2³²−1 bytes.
    pub async fn produce(&self, entries: Vec<Bytes>, metadata: Bytes) -> Result<WriteHandle> {
        if entries.is_empty() {
            return Err(Error::InvalidInput("entries must not be empty".to_string()));
        }
        if metadata.len() > u32::MAX as usize {
            return Err(Error::InvalidInput(format!(
                "metadata size {} exceeds u32::MAX",
                metadata.len()
            )));
        }
        let ingestion_time_ms = millis(self.clock.now());
        let durability_watcher = self
            .writer
            .add(entries, metadata, ingestion_time_ms)
            .await?;
        Ok(WriteHandle {
            watcher: durability_watcher,
            ingestion_time_ms,
        })
    }

    /// Flush the current batch, blocking until all pending entries are durably written.
    pub async fn flush(&self) -> Result<()> {
        self.writer.flush().await
    }

    /// Return the fraction of manifest writes that encountered optimistic-concurrency conflicts.
    pub fn conflict_rate(&self) -> f64 {
        self.writer.conflict_rate()
    }

    /// Shut down the buffer, flushing any remaining buffered entries before returning.
    pub async fn close(self) -> Result<()> {
        self.writer.close().await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::ProducerConfig;
    use crate::model::decode_batch;
    use crate::queue::{Manifest, QueueEntry};
    use bytes::Bytes;
    use common::ObjectStoreConfig;
    use common::clock::{MockClock, SystemClock};
    use slatedb::object_store::ObjectStore;
    use slatedb::object_store::memory::InMemory;
    use std::time::UNIX_EPOCH;

    async fn read_manifest_entries(store: &Arc<dyn ObjectStore>, path: &str) -> Vec<QueueEntry> {
        let path = slatedb::object_store::path::Path::from(path);
        let data = store.get(&path).await.unwrap().bytes().await.unwrap();
        let manifest = Manifest::from_bytes(data).unwrap();
        manifest.iter().map(|e| e.unwrap()).collect()
    }

    fn test_config() -> ProducerConfig {
        ProducerConfig {
            object_store: ObjectStoreConfig::InMemory,
            data_path_prefix: "test-ingest".to_string(),
            manifest_path: "test/manifest".to_string(),
            flush_interval: Duration::from_hours(24),
            flush_size_bytes: 64 * 1024 * 1024,
            max_buffered_inputs: 1000,
            batch_compression: CompressionType::None,
        }
    }

    #[tokio::test]
    async fn should_ingest_entries_and_enqueue_location() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        buffer
            .produce(vec![Bytes::from("data1")], Bytes::new())
            .await
            .unwrap();
        buffer
            .produce(vec![Bytes::from("data2")], Bytes::new())
            .await
            .unwrap();
        buffer.flush().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);
        assert!(entries[0].location.starts_with("test-ingest/"));
        assert!(entries[0].location.ends_with(".batch"));
    }

    #[tokio::test]
    async fn should_write_valid_batch_to_object_store() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        buffer
            .produce(vec![Bytes::from("mydata")], Bytes::new())
            .await
            .unwrap();
        buffer.flush().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        let path = Path::from(entries[0].location.as_str());
        let data = store.get(&path).await.unwrap().bytes().await.unwrap();
        let parsed = decode_batch(data).unwrap();

        assert_eq!(parsed.len(), 1);
        assert_eq!(parsed[0], Bytes::from("mydata"));
    }

    #[tokio::test]
    async fn should_flush_when_batch_size_exceeded() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());

        let mut config = test_config();
        config.flush_size_bytes = 10;

        let buffer =
            Producer::with_object_store(config, store.clone(), Arc::new(SystemClock)).unwrap();

        let mut watcher = buffer
            .produce(vec![Bytes::from("some-long-data")], Bytes::new())
            .await
            .unwrap();
        watcher.watcher.await_durable().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);
    }

    #[tokio::test]
    async fn should_flush_when_interval_elapsed() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());

        let mut config = test_config();
        config.flush_interval = Duration::from_millis(50);
        config.flush_size_bytes = 64 * 1024 * 1024;

        let buffer =
            Producer::with_object_store(config, store.clone(), Arc::new(SystemClock)).unwrap();

        let mut watcher = buffer
            .produce(vec![Bytes::from("v1")], Bytes::new())
            .await
            .unwrap();

        assert!(watcher.watcher.result().is_none());
        let manifest_path = slatedb::object_store::path::Path::from("test/manifest");
        assert!(store.get(&manifest_path).await.is_err());

        watcher.watcher.await_durable().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);
    }

    #[tokio::test]
    async fn should_not_flush_below_thresholds() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        let watcher = buffer
            .produce(vec![Bytes::from("v")], Bytes::new())
            .await
            .unwrap();

        assert!(watcher.watcher.result().is_none());

        let manifest_path = slatedb::object_store::path::Path::from("test/manifest");
        assert!(store.get(&manifest_path).await.is_err());

        buffer.flush().await.unwrap();

        assert!(watcher.watcher.result().unwrap().is_ok());

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);
    }

    #[tokio::test]
    async fn should_batch_multiple_ingests_into_single_file() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        let watcher1 = buffer
            .produce(vec![Bytes::from("data1")], Bytes::new())
            .await
            .unwrap();
        let watcher2 = buffer
            .produce(vec![Bytes::from("data2")], Bytes::new())
            .await
            .unwrap();

        buffer.flush().await.unwrap();

        assert!(watcher1.watcher.result().unwrap().is_ok());
        assert!(watcher2.watcher.result().unwrap().is_ok());

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);

        let path = Path::from(entries[0].location.as_str());
        let data = store.get(&path).await.unwrap().bytes().await.unwrap();
        let parsed = decode_batch(data).unwrap();
        assert_eq!(parsed.len(), 2);
        assert_eq!(parsed[0], Bytes::from("data1"));
        assert_eq!(parsed[1], Bytes::from("data2"));
    }

    #[tokio::test]
    async fn should_apply_backpressure_when_buffer_full() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());

        let mut config = test_config();
        config.max_buffered_inputs = 1;

        let buffer =
            Producer::with_object_store(config, store.clone(), Arc::new(SystemClock)).unwrap();

        // First ingest fills the single-slot buffer
        buffer
            .produce(vec![Bytes::from("data1")], Bytes::new())
            .await
            .unwrap();

        // Second ingest succeeds once the background task consumes the first message
        buffer
            .produce(vec![Bytes::from("data2")], Bytes::new())
            .await
            .unwrap();

        buffer.flush().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert!(!entries.is_empty());
    }

    #[tokio::test]
    async fn should_record_metadata_and_ingestion_time_in_queue_entry() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let millis = 1_700_000_000_000i64;
        let fixed_time = UNIX_EPOCH + Duration::from_millis(millis as u64);
        let clock = Arc::new(MockClock::with_time(fixed_time));

        let buffer = Producer::with_object_store(test_config(), store.clone(), clock).unwrap();

        let metadata = Bytes::from(r#"{"topic":"events"}"#);
        let handle = buffer
            .produce(vec![Bytes::from("payload")], metadata.clone())
            .await
            .unwrap();
        assert_eq!(handle.ingestion_time_ms, millis);
        buffer.flush().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].metadata.len(), 1);
        assert_eq!(entries[0].metadata[0].payload, metadata);
        assert_eq!(entries[0].metadata[0].ingestion_time_ms, millis);
    }

    #[tokio::test]
    async fn should_flush_remaining_entries_on_close() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        buffer
            .produce(vec![Bytes::from("unflushed")], Bytes::new())
            .await
            .unwrap();

        buffer.close().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);

        let path = Path::from(entries[0].location.as_str());
        let data = store.get(&path).await.unwrap().bytes().await.unwrap();
        let parsed = decode_batch(data).unwrap();
        assert_eq!(parsed, vec![Bytes::from("unflushed")]);
    }

    #[tokio::test]
    async fn should_produce_separate_batches_per_flush() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        buffer
            .produce(vec![Bytes::from("batch1")], Bytes::new())
            .await
            .unwrap();
        buffer.flush().await.unwrap();

        buffer
            .produce(vec![Bytes::from("batch2")], Bytes::new())
            .await
            .unwrap();
        buffer.flush().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 2);
        assert_ne!(entries[0].location, entries[1].location);

        let data1 = store
            .get(&Path::from(entries[0].location.as_str()))
            .await
            .unwrap()
            .bytes()
            .await
            .unwrap();
        assert_eq!(decode_batch(data1).unwrap(), vec![Bytes::from("batch1")]);

        let data2 = store
            .get(&Path::from(entries[1].location.as_str()))
            .await
            .unwrap()
            .bytes()
            .await
            .unwrap();
        assert_eq!(decode_batch(data2).unwrap(), vec![Bytes::from("batch2")]);
    }

    #[tokio::test]
    async fn should_not_flush_empty_batch() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        buffer.flush().await.unwrap();

        let manifest_path = slatedb::object_store::path::Path::from("test/manifest");
        assert!(store.get(&manifest_path).await.is_err());
    }

    #[tokio::test]
    async fn should_preserve_all_empty_entries_batch() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        buffer
            .produce(vec![Bytes::new(), Bytes::new()], Bytes::from("meta"))
            .await
            .unwrap();
        buffer.flush().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);
        assert!(!entries[0].location.is_empty());
        assert_eq!(entries[0].metadata.len(), 1);
        assert_eq!(entries[0].metadata[0].payload, Bytes::from("meta"));
        assert_eq!(entries[0].metadata[0].start_index, 0);

        let data = store
            .get(&Path::from(entries[0].location.clone()))
            .await
            .unwrap()
            .bytes()
            .await
            .unwrap();
        let records = decode_batch(data).unwrap();
        assert_eq!(records, vec![Bytes::new(), Bytes::new()]);
    }

    #[tokio::test]
    async fn should_preserve_empty_and_non_empty_entries_in_batch() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        buffer
            .produce(
                vec![
                    Bytes::from("a"),
                    Bytes::new(),
                    Bytes::from("b"),
                    Bytes::new(),
                ],
                Bytes::from("meta"),
            )
            .await
            .unwrap();
        buffer.flush().await.unwrap();

        let entries = read_manifest_entries(&store, "test/manifest").await;
        assert_eq!(entries.len(), 1);
        assert!(!entries[0].location.is_empty());
        assert_eq!(entries[0].metadata.len(), 1);
        assert_eq!(entries[0].metadata[0].payload, Bytes::from("meta"));
        assert_eq!(entries[0].metadata[0].start_index, 0);

        let data = store
            .get(&Path::from(entries[0].location.clone()))
            .await
            .unwrap()
            .bytes()
            .await
            .unwrap();
        let records = decode_batch(data).unwrap();
        assert_eq!(
            records,
            vec![
                Bytes::from("a"),
                Bytes::new(),
                Bytes::from("b"),
                Bytes::new()
            ]
        );
    }

    #[tokio::test]
    async fn should_reject_empty_entries() {
        let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let buffer =
            Producer::with_object_store(test_config(), store.clone(), Arc::new(SystemClock))
                .unwrap();

        let result = buffer.produce(vec![], Bytes::new()).await;
        assert!(matches!(result, Err(Error::InvalidInput(_))));

        let result = buffer.produce(vec![], Bytes::from("meta")).await;
        assert!(matches!(result, Err(Error::InvalidInput(_))));
    }
}