reductstore 1.20.3

ReductStore is a time series database designed specifically for storing and managing large amounts of blob data.
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0

#![allow(dead_code)]

use crate::core::file_cache::FILE_CACHE;
use crate::storage::block_manager::{
    BlockManager, COMPRESSED_DATA_FILE_EXT, COMPRESSED_DESCRIPTOR_FILE_EXT,
};
use crate::storage::engine::MAX_IO_BUFFER_SIZE;
use crate::storage::proto::block_index::CompressionAlgorithm as ProtoCompressionAlgorithm;
use crc64fast::Digest;
use reduct_base::error::ReductError;
use reduct_base::{conflict, internal_server_error, not_found};
use std::cmp::min;
use std::fs::OpenOptions;
use std::io::{Read, SeekFrom, Write};
use std::path::{Path, PathBuf};
use zstd::stream::write::Encoder as ZstdEncoder;

const ZSTD_COMPRESSION_LEVEL: i32 = 3;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CompressionAlgorithm {
    None,
    Zstd,
}

impl From<CompressionAlgorithm> for ProtoCompressionAlgorithm {
    fn from(value: CompressionAlgorithm) -> Self {
        match value {
            CompressionAlgorithm::None => ProtoCompressionAlgorithm::None,
            CompressionAlgorithm::Zstd => ProtoCompressionAlgorithm::Zstd,
        }
    }
}

impl From<ProtoCompressionAlgorithm> for CompressionAlgorithm {
    fn from(value: ProtoCompressionAlgorithm) -> Self {
        match value {
            ProtoCompressionAlgorithm::None => CompressionAlgorithm::None,
            ProtoCompressionAlgorithm::Zstd => CompressionAlgorithm::Zstd,
        }
    }
}

impl From<CompressionAlgorithm> for i32 {
    fn from(value: CompressionAlgorithm) -> Self {
        i32::from(ProtoCompressionAlgorithm::from(value))
    }
}

impl BlockManager {
    pub(crate) async fn compress_block(
        &mut self,
        block_id: u64,
        algorithm: CompressionAlgorithm,
    ) -> Result<(), ReductError> {
        let block_size = {
            let block = self.block_index.get_block(block_id).ok_or_else(|| {
                not_found!(
                    "Block {} not found in entry {}/{}",
                    block_id,
                    self.bucket,
                    self.entry
                )
            })?;

            if block
                .compression
                .unwrap_or(i32::from(CompressionAlgorithm::None))
                != i32::from(CompressionAlgorithm::None)
            {
                return Err(conflict!(
                    "Block {}/{}/{} is already compressed",
                    self.bucket,
                    self.entry,
                    block_id
                ));
            }

            block.size
        };

        match algorithm {
            CompressionAlgorithm::None => return Ok(()),
            CompressionAlgorithm::Zstd => {
                let (data_size, desc_size) = self.compress_block_zstd(block_id, block_size).await?;
                let block = self.block_index.get_block_mut(block_id).ok_or_else(|| {
                    not_found!(
                        "Block {} not found in entry {}/{}",
                        block_id,
                        self.bucket,
                        self.entry
                    )
                })?;
                block.size = data_size;
                block.metadata_size = desc_size;
            }
        }

        let block = self.block_index.get_block_mut(block_id).ok_or_else(|| {
            not_found!(
                "Block {} not found in entry {}/{}",
                block_id,
                self.bucket,
                self.entry
            )
        })?;
        block.compression = Some(i32::from(algorithm));
        self.block_index.save().await?;

        let data_path = self.path_to_data(block_id);
        let desc_path = self.path_to_desc(block_id);
        FILE_CACHE.remove(&data_path).await?;
        FILE_CACHE.remove(&desc_path).await?;
        FILE_CACHE.discard_recursive(&data_path).await?;
        FILE_CACHE.discard_recursive(&desc_path).await?;

        Ok(())
    }

    pub(crate) async fn decompress_block(&mut self, block_id: u64) -> Result<(), ReductError> {
        if !self.block_is_compressed(block_id) {
            return Ok(());
        }

        let compressed_data_path = self.path_to_compressed_data(block_id);
        let compressed_desc_path = self.path_to_compressed_desc(block_id);
        let data_path = self.path_to_data(block_id);
        let desc_path = self.path_to_desc(block_id);

        decompress_file_zstd(&compressed_data_path, &data_path).await?;
        decompress_file_zstd(&compressed_desc_path, &desc_path).await?;

        FILE_CACHE.remove(&compressed_data_path).await?;
        FILE_CACHE.remove(&compressed_desc_path).await?;
        FILE_CACHE.discard_recursive(&compressed_data_path).await?;
        FILE_CACHE.discard_recursive(&compressed_desc_path).await?;

        let data_size = tokio::fs::metadata(&data_path)
            .await
            .map_err(|err| {
                internal_server_error!("Failed to get data file size {:?}: {}", data_path, err)
            })?
            .len();
        let desc = std::fs::read(&desc_path).map_err(|err| {
            internal_server_error!("Failed to read descriptor file {:?}: {}", desc_path, err)
        })?;
        let desc_size = desc.len() as u64;
        let mut crc = Digest::new();
        crc.write(&desc);

        let block = self.block_index.get_block_mut(block_id).ok_or_else(|| {
            not_found!(
                "Block {} not found in entry {}/{}",
                block_id,
                self.bucket,
                self.entry
            )
        })?;
        block.compression = None;
        block.size = data_size;
        block.metadata_size = desc_size;
        block.crc64 = Some(crc.sum64());
        self.block_index.save().await?;

        self.decompress_cache.invalidate(&self.path, block_id).await;

        Ok(())
    }

    async fn compress_block_zstd(
        &self,
        block_id: u64,
        block_size: u64,
    ) -> Result<(u64, u64), ReductError> {
        let data_path = self.path_to_data(block_id);
        let compressed_data_path = self.path_to_compressed_data(block_id);
        let compressed_data_tmp_path = self
            .path
            .join(format!("{}{}.tmp", block_id, COMPRESSED_DATA_FILE_EXT));

        let desc_path = self.path_to_desc(block_id);
        let compressed_desc_path = self.path_to_compressed_desc(block_id);
        let compressed_desc_tmp_path = self.path.join(format!(
            "{}{}.tmp",
            block_id, COMPRESSED_DESCRIPTOR_FILE_EXT
        ));

        if let Err(err) =
            compress_file_zstd(&data_path, &compressed_data_tmp_path, block_size).await
        {
            cleanup_tmp(&compressed_data_tmp_path);
            return Err(err);
        }

        let desc_size = FILE_CACHE
            .read(&desc_path, SeekFrom::Start(0))
            .await?
            .metadata()?
            .len();
        if let Err(err) = compress_file_zstd(&desc_path, &compressed_desc_tmp_path, desc_size).await
        {
            cleanup_tmp(&compressed_data_tmp_path);
            cleanup_tmp(&compressed_desc_tmp_path);
            return Err(err);
        }

        if let Err(err) = tokio::fs::rename(&compressed_data_tmp_path, &compressed_data_path).await
        {
            cleanup_tmp(&compressed_data_tmp_path);
            cleanup_tmp(&compressed_desc_tmp_path);
            return Err(internal_server_error!(
                "Failed to rename compressed data file {:?} to {:?}: {}",
                compressed_data_tmp_path,
                compressed_data_path,
                err
            ));
        }

        if let Err(err) = tokio::fs::rename(&compressed_desc_tmp_path, &compressed_desc_path).await
        {
            cleanup_tmp(&compressed_desc_tmp_path);
            let _ = std::fs::remove_file(&compressed_data_path);
            return Err(internal_server_error!(
                "Failed to rename compressed descriptor file {:?} to {:?}: {}",
                compressed_desc_tmp_path,
                compressed_desc_path,
                err
            ));
        }

        let compressed_data_size = tokio::fs::metadata(&compressed_data_path)
            .await
            .map_err(|err| {
                internal_server_error!(
                    "Failed to get compressed data file size {:?}: {}",
                    compressed_data_path,
                    err
                )
            })?
            .len();
        let compressed_desc_size = tokio::fs::metadata(&compressed_desc_path)
            .await
            .map_err(|err| {
                internal_server_error!(
                    "Failed to get compressed descriptor file size {:?}: {}",
                    compressed_desc_path,
                    err
                )
            })?
            .len();

        Ok((compressed_data_size, compressed_desc_size))
    }
}

async fn compress_file_zstd(
    source_path: &PathBuf,
    temp_path: &PathBuf,
    source_size: u64,
) -> Result<(), ReductError> {
    let temp_file = OpenOptions::new()
        .create(true)
        .truncate(true)
        .write(true)
        .read(true)
        .open(temp_path)
        .map_err(|err| {
            internal_server_error!(
                "Failed to create temporary compressed file {:?}: {}",
                temp_path,
                err
            )
        })?;

    let mut encoder = ZstdEncoder::new(temp_file, ZSTD_COMPRESSION_LEVEL).map_err(|err| {
        internal_server_error!("Failed to create zstd encoder for {:?}: {}", temp_path, err)
    })?;

    let mut read_bytes = 0u64;
    while read_bytes < source_size {
        let bytes_to_read = min(MAX_IO_BUFFER_SIZE as u64, source_size - read_bytes) as usize;
        let mut buf = vec![0; bytes_to_read];
        {
            let mut source = FILE_CACHE
                .read(source_path, SeekFrom::Start(read_bytes))
                .await?;
            source.read_exact(&mut buf).map_err(|err| {
                internal_server_error!(
                    "Failed to read file {:?} at offset {}: {}",
                    source_path,
                    read_bytes,
                    err
                )
            })?;
        }

        encoder.write_all(&buf).map_err(|err| {
            internal_server_error!("Failed to compress file {:?}: {}", source_path, err)
        })?;
        read_bytes += bytes_to_read as u64;
    }

    let compressed_file = encoder.finish().map_err(|err| {
        internal_server_error!("Failed to finish zstd stream {:?}: {}", temp_path, err)
    })?;
    compressed_file.sync_all().map_err(|err| {
        internal_server_error!("Failed to sync compressed file {:?}: {}", temp_path, err)
    })?;

    Ok(())
}

async fn decompress_file_zstd(
    compressed_path: &PathBuf,
    output_path: &PathBuf,
) -> Result<(), ReductError> {
    let mut compressed = vec![];
    {
        let mut file = FILE_CACHE.read(compressed_path, SeekFrom::Start(0)).await?;
        file.read_to_end(&mut compressed).map_err(|err| {
            internal_server_error!(
                "Failed to read compressed file {:?}: {}",
                compressed_path,
                err
            )
        })?;
    }

    let decompressed = zstd::decode_all(compressed.as_slice()).map_err(|err| {
        internal_server_error!("Failed to decompress file {:?}: {}", compressed_path, err)
    })?;

    let mut out = OpenOptions::new()
        .create(true)
        .truncate(true)
        .write(true)
        .open(output_path)
        .map_err(|err| {
            internal_server_error!("Failed to create file {:?}: {}", output_path, err)
        })?;
    out.write_all(&decompressed)
        .map_err(|err| internal_server_error!("Failed to write file {:?}: {}", output_path, err))?;
    out.sync_all()
        .map_err(|err| internal_server_error!("Failed to sync file {:?}: {}", output_path, err))?;

    Ok(())
}

fn cleanup_tmp(path: &Path) {
    let _ = std::fs::remove_file(path);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cfg::Cfg;
    use crate::storage::block_manager::block_index::BlockIndex;
    use crate::storage::block_manager::decompress_cache::DecompressedFileType;
    use crate::storage::block_manager::{BLOCK_INDEX_FILE, DATA_FILE_EXT, DESCRIPTOR_FILE_EXT};
    use crate::storage::entry::io::record_reader::read_in_chunks;
    use crate::storage::proto::{record, Record};
    use prost_wkt_types::Timestamp;
    use reduct_base::error::ErrorCode;
    use rstest::rstest;
    use serial_test::serial;
    use tempfile::tempdir;

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_compress_block_zstd() {
        let data = b"compress me".to_vec();
        let (mut block_manager, block_id, original_data, original_descriptor) =
            block_manager_with_data(data).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .unwrap();

        let compressed_data_path = block_manager.path_to_compressed_data(block_id);
        let compressed_descriptor_path = block_manager.path_to_compressed_desc(block_id);
        assert!(compressed_data_path.exists());
        assert!(compressed_descriptor_path.exists());

        let decompressed_data =
            zstd::decode_all(std::fs::File::open(&compressed_data_path).unwrap()).unwrap();
        assert_eq!(decompressed_data, original_data);

        let decompressed_descriptor =
            zstd::decode_all(std::fs::File::open(&compressed_descriptor_path).unwrap()).unwrap();
        assert_eq!(decompressed_descriptor, original_descriptor);

        assert!(!block_manager.path_to_data(block_id).exists());
        assert!(!block_manager.path_to_desc(block_id).exists());
        assert_eq!(
            block_manager
                .index()
                .get_block(block_id)
                .unwrap()
                .compression,
            Some(i32::from(CompressionAlgorithm::Zstd))
        );

        let compressed_data_size = std::fs::metadata(&compressed_data_path).unwrap().len();
        let compressed_descriptor_size = std::fs::metadata(&compressed_descriptor_path)
            .unwrap()
            .len();
        let block = block_manager.index().get_block(block_id).unwrap();
        assert_eq!(block.size, compressed_data_size);
        assert_eq!(block.metadata_size, compressed_descriptor_size);
        assert_eq!(
            block_manager.index().size(),
            compressed_data_size + compressed_descriptor_size
        );
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_compress_block_already_compressed() {
        let (mut block_manager, block_id, _, _) =
            block_manager_with_data(b"already compressed".to_vec()).await;
        block_manager
            .index_mut()
            .get_block_mut(block_id)
            .unwrap()
            .compression = Some(i32::from(CompressionAlgorithm::Zstd));

        let err = block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .err()
            .unwrap();

        assert_eq!(err.status(), ErrorCode::Conflict);
    }

    #[rstest]
    fn test_compression_algorithm_from_proto() {
        assert_eq!(
            CompressionAlgorithm::from(ProtoCompressionAlgorithm::None),
            CompressionAlgorithm::None
        );
        assert_eq!(
            CompressionAlgorithm::from(ProtoCompressionAlgorithm::Zstd),
            CompressionAlgorithm::Zstd
        );
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_compress_block_noop_with_none_algorithm() {
        let (mut block_manager, block_id, original_data, original_descriptor) =
            block_manager_with_data(b"no compression".to_vec()).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::None)
            .await
            .unwrap();

        assert!(block_manager.path_to_data(block_id).exists());
        assert!(block_manager.path_to_desc(block_id).exists());
        assert!(!block_manager.path_to_compressed_data(block_id).exists());
        assert!(!block_manager.path_to_compressed_desc(block_id).exists());
        assert_eq!(
            std::fs::read(block_manager.path_to_data(block_id)).unwrap(),
            original_data
        );
        assert_eq!(
            std::fs::read(block_manager.path_to_desc(block_id)).unwrap(),
            original_descriptor
        );
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_compress_block_not_found() {
        let path = tempdir().unwrap().keep().join("bucket").join("entry");
        let mut block_manager = BlockManager::build(
            path.clone(),
            BlockIndex::new(path.join(BLOCK_INDEX_FILE)),
            "bucket".to_string(),
            "entry".to_string(),
            Cfg::default().into(),
            Default::default(),
        )
        .await
        .unwrap();

        let err = block_manager
            .compress_block(42, CompressionAlgorithm::Zstd)
            .await
            .err()
            .unwrap();

        assert_eq!(err.status(), ErrorCode::NotFound);
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_compress_block_cleans_temp_when_data_compression_fails() {
        let (mut block_manager, block_id, _, _) =
            block_manager_with_data(b"missing source".to_vec()).await;
        FILE_CACHE
            .remove(&block_manager.path_to_data(block_id))
            .await
            .unwrap();

        let err = block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .err()
            .unwrap();

        assert_eq!(err.status(), ErrorCode::InternalServerError);
        assert!(!block_manager
            .path()
            .join(format!("{}{}.tmp", block_id, COMPRESSED_DATA_FILE_EXT))
            .exists());
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_decompress_block_restores_files() {
        let data = b"decompress me".to_vec();
        let (mut block_manager, block_id, original_data, original_descriptor) =
            block_manager_with_data(data).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .unwrap();
        let compressed_data_path = block_manager.path_to_compressed_data(block_id);
        let cached_data_path = block_manager
            .decompress_cache
            .get_or_decompress(
                block_manager.path(),
                block_id,
                DecompressedFileType::Data,
                &compressed_data_path,
            )
            .await
            .unwrap();

        block_manager.decompress_block(block_id).await.unwrap();

        assert!(block_manager.path_to_data(block_id).exists());
        assert!(block_manager.path_to_desc(block_id).exists());
        assert!(!block_manager.path_to_compressed_data(block_id).exists());
        assert!(!block_manager.path_to_compressed_desc(block_id).exists());
        assert!(!cached_data_path.exists());
        assert_eq!(
            std::fs::read(block_manager.path_to_data(block_id)).unwrap(),
            original_data
        );
        assert_eq!(
            std::fs::read(block_manager.path_to_desc(block_id)).unwrap(),
            original_descriptor
        );

        let block = block_manager.index().get_block(block_id).unwrap();
        assert_eq!(block.compression, None);
        assert_eq!(block.size, original_data.len() as u64);
        assert_eq!(block.metadata_size, original_descriptor.len() as u64);
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_decompress_block_noop_if_not_compressed() {
        let (mut block_manager, block_id, original_data, original_descriptor) =
            block_manager_with_data(b"not compressed".to_vec()).await;

        block_manager.decompress_block(block_id).await.unwrap();

        assert!(block_manager.path_to_data(block_id).exists());
        assert!(block_manager.path_to_desc(block_id).exists());
        assert!(!block_manager.path_to_compressed_data(block_id).exists());
        assert!(!block_manager.path_to_compressed_desc(block_id).exists());
        assert_eq!(
            std::fs::read(block_manager.path_to_data(block_id)).unwrap(),
            original_data
        );
        assert_eq!(
            std::fs::read(block_manager.path_to_desc(block_id)).unwrap(),
            original_descriptor
        );
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_decompress_block_with_corrupted_data() {
        let (mut block_manager, block_id, _, _) =
            block_manager_with_data(b"corrupt me".to_vec()).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .unwrap();

        std::fs::write(
            block_manager.path_to_compressed_data(block_id),
            b"not valid zstd data",
        )
        .unwrap();

        let err = block_manager
            .decompress_block(block_id)
            .await
            .err()
            .unwrap();
        assert_eq!(err.status(), ErrorCode::InternalServerError);
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_decompress_block_with_corrupted_descriptor() {
        let (mut block_manager, block_id, _, _) =
            block_manager_with_data(b"corrupt desc".to_vec()).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .unwrap();

        std::fs::write(block_manager.path_to_compressed_desc(block_id), b"garbage").unwrap();

        let err = block_manager
            .decompress_block(block_id)
            .await
            .err()
            .unwrap();
        assert_eq!(err.status(), ErrorCode::InternalServerError);
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_decompress_block_missing_compressed_file() {
        let (mut block_manager, block_id, _, _) =
            block_manager_with_data(b"missing file".to_vec()).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .unwrap();

        std::fs::remove_file(block_manager.path_to_compressed_data(block_id)).unwrap();

        let err = block_manager
            .decompress_block(block_id)
            .await
            .err()
            .unwrap();
        assert_eq!(err.status(), ErrorCode::InternalServerError);
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_compress_file_zstd_missing_temp_parent() {
        let dir = tempdir().unwrap().keep();
        let source_path = dir.join("source.blk");
        let temp_path = dir.join("missing").join("source.blk.zst.tmp");
        std::fs::write(&source_path, b"data").unwrap();

        let err = compress_file_zstd(&source_path, &temp_path, 4)
            .await
            .err()
            .unwrap();

        assert_eq!(err.status(), ErrorCode::InternalServerError);
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_compress_file_zstd_short_source() {
        let dir = tempdir().unwrap().keep();
        let source_path = dir.join("source.blk");
        let temp_path = dir.join("source.blk.zst.tmp");
        std::fs::write(&source_path, b"a").unwrap();

        let err = compress_file_zstd(&source_path, &temp_path, 2)
            .await
            .err()
            .unwrap();

        assert_eq!(err.status(), ErrorCode::InternalServerError);
        cleanup_tmp(&temp_path);
        assert!(!temp_path.exists());
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_decompress_file_zstd_missing_output_parent() {
        let dir = tempdir().unwrap().keep();
        let compressed_path = dir.join("source.blk.zst");
        let output_path = dir.join("missing").join("source.blk");
        std::fs::write(
            &compressed_path,
            zstd::encode_all("data".as_bytes(), 3).unwrap(),
        )
        .unwrap();

        let err = decompress_file_zstd(&compressed_path, &output_path)
            .await
            .err()
            .unwrap();

        assert_eq!(err.status(), ErrorCode::InternalServerError);
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_compress_block_large_data() {
        let data = (0..MAX_IO_BUFFER_SIZE + 1)
            .map(|idx| (idx % 251) as u8)
            .collect::<Vec<_>>();
        let (mut block_manager, block_id, original_data, _) = block_manager_with_data(data).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .unwrap();

        let compressed_data_path = block_manager.path_to_compressed_data(block_id);
        let decompressed_data =
            zstd::decode_all(std::fs::File::open(&compressed_data_path).unwrap()).unwrap();

        assert_eq!(decompressed_data, original_data);
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_load_compressed_block() {
        let (mut block_manager, block_id, _, _) =
            block_manager_with_data(b"load compressed descriptor".to_vec()).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .unwrap();
        block_manager.clear_cache_for_test();

        let block_ref = block_manager.load_block(block_id).await.unwrap();
        let block = block_ref.read().await.unwrap();

        assert_eq!(block.block_id(), block_id);
        assert!(block.get_record(0).is_some());
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_read_record_from_compressed_block() {
        let data = b"read compressed data".to_vec();
        let (mut block_manager, block_id, original_data, _) = block_manager_with_data(data).await;

        block_manager
            .compress_block(block_id, CompressionAlgorithm::Zstd)
            .await
            .unwrap();
        block_manager.clear_cache_for_test();

        let block_ref = block_manager.load_block(block_id).await.unwrap();
        let block = block_ref.read().await.unwrap();
        let (file_path, offset) = block_manager.begin_read_record(&block, 0).await.unwrap();
        let (content, read) = read_in_chunks(&file_path, offset, original_data.len() as u64, 0)
            .await
            .unwrap();

        assert_eq!(read, original_data.len());
        assert_eq!(content, original_data);
    }

    async fn block_manager_with_data(data: Vec<u8>) -> (BlockManager, u64, Vec<u8>, Vec<u8>) {
        let block_id = 1;
        let path = tempdir().unwrap().keep().join("bucket").join("entry");
        let mut block_manager = BlockManager::build(
            path.clone(),
            BlockIndex::new(path.join(BLOCK_INDEX_FILE)),
            "bucket".to_string(),
            "entry".to_string(),
            Cfg::default().into(),
            Default::default(),
        )
        .await
        .unwrap();
        let block_ref = block_manager
            .start_new_block(block_id, data.len() as u64)
            .await
            .unwrap();

        let mut block = block_ref.write().await.unwrap();
        block.insert_or_update_record(Record {
            timestamp: Some(Timestamp {
                seconds: 0,
                nanos: 0,
            }),
            begin: 0,
            end: data.len() as u64,
            state: 0,
            labels: vec![],
            content_type: "".to_string(),
        });

        let (file, offset) = block_manager.begin_write_record(&block, 0).unwrap();
        drop(block);

        let mut data_file = FILE_CACHE
            .write_or_create(&file, SeekFrom::Start(offset))
            .await
            .unwrap();
        data_file.write_all(&data).unwrap();
        data_file.sync_all().await.unwrap();
        drop(data_file);

        block_manager
            .finish_write_record(block_id, record::State::Finished, 0)
            .await
            .unwrap();
        block_manager
            .save_meta_on_disk(block_ref.clone())
            .await
            .unwrap();

        FILE_CACHE.force_sync_all().await.unwrap();

        let original_descriptor = std::fs::read(block_manager.path_to_desc(block_id)).unwrap();
        assert!(block_manager
            .path()
            .join(format!("{}{}", block_id, DATA_FILE_EXT))
            .exists());
        assert!(block_manager
            .path()
            .join(format!("{}{}", block_id, DESCRIPTOR_FILE_EXT))
            .exists());

        (block_manager, block_id, data, original_descriptor)
    }
}