reductstore 1.19.6

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
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0

use crate::core::file_cache::FILE_CACHE;
use crate::core::sync::AsyncRwLock;
use crate::replication::Transaction;
use log::{debug, warn};
use reduct_base::error::ReductError;
use reduct_base::internal_server_error;
use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::PathBuf;
use std::sync::Arc;

pub(super) type TransactionLogRef = Arc<AsyncRwLock<TransactionLog>>;
pub(super) type TransactionLogMap = Arc<AsyncRwLock<HashMap<String, TransactionLogRef>>>;

/// Transaction log for replication.
///
/// Format
///
/// | 8 byte - write position | 8 byte - read position |
/// | byte - transaction type 0 | 8 byte - timestamp 0 |
///  .........
/// | byte - transaction type n | 8 byte - timestamp n |
///
pub(super) struct TransactionLog {
    file_path: PathBuf,
    capacity_in_bytes: usize,
    write_pos: usize,
    read_pos: usize,
}

const HEADER_SIZE: usize = 16;
const ENTRY_SIZE: usize = 9;

impl TransactionLog {
    /// Create a new transaction log or load an existing one.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the transaction log file.
    /// * `capacity` - Maximum number of transactions to store. Ignored if the file already exists.
    ///
    /// # Returns
    ///
    /// A new transaction log instance or an error.
    pub async fn try_load_or_create(path: &PathBuf, capacity: usize) -> Result<Self, ReductError> {
        let init_capacity_in_bytes = capacity * ENTRY_SIZE + HEADER_SIZE;

        let instance = if !path.try_exists()? {
            let mut file = FILE_CACHE
                .write_or_create(&path, SeekFrom::Current(0))
                .await?;

            file.set_len(init_capacity_in_bytes as u64)?;
            file.write_all(HEADER_SIZE.to_be_bytes().as_ref())?;
            file.write_all(HEADER_SIZE.to_be_bytes().as_ref())?;
            file.sync_all().await?;

            Self {
                file_path: path.clone(),
                capacity_in_bytes: init_capacity_in_bytes,
                write_pos: HEADER_SIZE,
                read_pos: HEADER_SIZE,
            }
        } else {
            let (buf, capacity_in_bytes) = {
                let mut file = FILE_CACHE.read(&path, SeekFrom::Start(0)).await?;
                let mut buf = [0u8; 16];
                file.read_exact(&mut buf)?;
                let capacity_in_bytes = file.metadata()?.len() as usize;
                (buf, capacity_in_bytes)
            };
            let write_pos = u64::from_be_bytes(buf[0..8].try_into().unwrap()) as usize;
            let read_pos = u64::from_be_bytes(buf[8..16].try_into().unwrap()) as usize;

            Self::integrity_check(
                path,
                init_capacity_in_bytes,
                capacity_in_bytes,
                write_pos,
                read_pos,
            )?;

            let capacity_in_bytes = if init_capacity_in_bytes != capacity_in_bytes {
                // If the capacity is changed, we need to check if the log is empty
                // then we can change the capacity.
                if read_pos == write_pos {
                    debug!(
                        "Transaction log {:?}' size changed from {} to {} bytes",
                        path, capacity_in_bytes, init_capacity_in_bytes
                    );
                    let mut file = FILE_CACHE
                        .write_or_create(&path, SeekFrom::Start(0))
                        .await?;

                    file.set_len(init_capacity_in_bytes as u64)?;
                    init_capacity_in_bytes
                } else {
                    warn!("Cannot change the capacity of the transaction log {:?} from {} to {} bytes because it is not empty", path, capacity_in_bytes, init_capacity_in_bytes);
                    capacity_in_bytes
                }
            } else {
                capacity_in_bytes
            };

            Self {
                file_path: path.to_path_buf(),
                capacity_in_bytes,
                write_pos,
                read_pos,
            }
        };

        Ok(instance)
    }

    fn integrity_check(
        path: &PathBuf,
        init_capacity_in_bytes: usize,
        capacity_in_bytes: usize,
        write_pos: usize,
        read_pos: usize,
    ) -> Result<(), ReductError> {
        if init_capacity_in_bytes < HEADER_SIZE
            || (capacity_in_bytes - HEADER_SIZE) % ENTRY_SIZE != 0
        {
            return Err(internal_server_error!(
                "Invalid size {} of transaction log {}",
                capacity_in_bytes,
                path.to_str().unwrap_or("unknown path")
            ));
        }

        let check_pos = |pos: usize, name: &str| {
            if pos < HEADER_SIZE
                || pos >= capacity_in_bytes
                || (pos - HEADER_SIZE) % ENTRY_SIZE != 0
            {
                return Err(internal_server_error!(
                    "Invalid {} position {} in transaction log {}",
                    name,
                    pos,
                    path.to_str().unwrap_or("unknown path")
                ));
            }
            Ok(())
        };

        check_pos(write_pos, "write")?;
        check_pos(read_pos, "read")?;

        Ok(())
    }

    /// Push a new transaction to the log.
    ///
    /// # Arguments
    ///
    /// * `transaction` - Transaction to push.
    ///
    /// # Returns
    ///
    /// The oldest transaction if the log is full, otherwise `None`.
    pub async fn push_back(
        &mut self,
        transaction: Transaction,
    ) -> Result<Option<Transaction>, ReductError> {
        {
            let mut file = FILE_CACHE
                .write_or_create(&self.file_path, SeekFrom::Start(self.write_pos as u64))
                .await?;
            let mut buf = [0u8; ENTRY_SIZE];
            buf[0] = transaction.clone().into();
            buf[1..9].copy_from_slice(&transaction.timestamp().to_be_bytes());

            file.write_all(&buf)?;
            self.write_pos += ENTRY_SIZE;

            if self.write_pos >= self.capacity_in_bytes {
                self.write_pos = HEADER_SIZE;
            }

            file.seek(SeekFrom::Start(0))?;
            file.write_all(&self.write_pos.to_be_bytes())?;
        }

        if self.write_pos == self.read_pos {
            let transaction = self.unsafe_head(1).await?.get(0).cloned();

            self.unsafe_pop().await?;
            Ok(transaction)
        } else {
            Ok(None)
        }
    }

    pub fn is_empty(&self) -> bool {
        self.read_pos == self.write_pos
    }

    pub fn len(&self) -> usize {
        let len_in_bytes = if self.read_pos <= self.write_pos {
            self.write_pos - self.read_pos
        } else {
            self.capacity_in_bytes
                .wrapping_sub(HEADER_SIZE)
                .wrapping_sub(self.read_pos)
                .wrapping_add(self.write_pos)
        };
        (len_in_bytes / ENTRY_SIZE) as usize
    }

    pub async fn front(&self, n: usize) -> Result<Vec<Transaction>, ReductError> {
        if self.is_empty() {
            return Ok(Vec::new());
        }
        let transaction = self.unsafe_head(n).await?;
        Ok(transaction)
    }

    pub async fn pop_front(&mut self, n: usize) -> Result<usize, ReductError> {
        let mut popped = 0usize;
        for _ in 0..n {
            if self.read_pos == self.write_pos {
                break;
            }
            self.unsafe_pop().await?;
            popped += 1;
        }

        Ok(popped)
    }

    async fn unsafe_head(&self, n: usize) -> Result<Vec<Transaction>, ReductError> {
        let mut buf = [0u8; ENTRY_SIZE];
        let mut read_pos = self.read_pos;
        let mut transactions = Vec::with_capacity(n);
        let mut file = FILE_CACHE
            .read(&self.file_path, SeekFrom::Start(read_pos as u64))
            .await?;

        for _ in 0..n {
            file.seek(SeekFrom::Start(read_pos as u64))?;
            file.read_exact(&mut buf)?;
            let transaction_type = buf[0];
            let timestamp = u64::from_be_bytes(buf[1..9].try_into().unwrap());

            match transaction_type {
                0 => transactions.push(Transaction::WriteRecord(timestamp)),
                1 => transactions.push(Transaction::UpdateRecord(timestamp)),
                _ => return Err(internal_server_error!("Invalid transaction type",)),
            }

            read_pos += ENTRY_SIZE;
            if read_pos >= self.capacity_in_bytes {
                read_pos = HEADER_SIZE;
            }

            if read_pos == self.write_pos {
                break;
            }
        }

        Ok(transactions)
    }

    async fn unsafe_pop(&mut self) -> Result<(), ReductError> {
        self.read_pos += ENTRY_SIZE;

        if self.read_pos >= self.capacity_in_bytes {
            self.read_pos = HEADER_SIZE;
        }

        {
            let mut file = FILE_CACHE
                .write_or_create(&self.file_path, SeekFrom::Start(8))
                .await?;
            file.write_all(&self.read_pos.to_be_bytes())?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rstest::*;
    use std::fs;
    use tempfile::tempdir;

    #[rstest]
    #[tokio::test]
    async fn test_new_transaction_log(path: PathBuf) {
        let transaction_log = TransactionLog::try_load_or_create(&path, 100)
            .await
            .unwrap();
        assert!(transaction_log.is_empty());
    }

    #[rstest]
    #[tokio::test]
    async fn test_write_read_transaction_log(path: PathBuf) {
        let mut transaction_log = TransactionLog::try_load_or_create(&path, 100)
            .await
            .unwrap();
        assert_eq!(
            transaction_log
                .push_back(Transaction::WriteRecord(1))
                .await
                .unwrap(),
            None
        );

        assert_eq!(transaction_log.len(), 1);

        assert_eq!(
            transaction_log
                .push_back(Transaction::UpdateRecord(2))
                .await
                .unwrap(),
            None
        );
        assert_eq!(transaction_log.len(), 2);
        assert!(!transaction_log.is_empty());
        assert_eq!(
            transaction_log.front(2).await.unwrap(),
            vec![Transaction::WriteRecord(1), Transaction::UpdateRecord(2),]
        );

        assert_eq!(transaction_log.pop_front(2).await.unwrap(), 2);
        assert!(transaction_log.is_empty());

        assert_eq!(transaction_log.pop_front(1).await.unwrap(), 0);
    }

    #[rstest]
    #[tokio::test]
    async fn test_write_broken_type(path: PathBuf) {
        let mut transaction_log = TransactionLog::try_load_or_create(&path, 100)
            .await
            .unwrap();
        assert_eq!(
            transaction_log
                .push_back(Transaction::WriteRecord(1))
                .await
                .unwrap(),
            None
        );
        {
            let mut file = FILE_CACHE
                .write_or_create(
                    &path,
                    SeekFrom::Start((transaction_log.write_pos - ENTRY_SIZE) as u64),
                )
                .await
                .unwrap();
            file.write_all(&[99]).unwrap();
            file.sync_all().await.unwrap();
        }

        assert_eq!(
            transaction_log.front(1).await.unwrap_err(),
            internal_server_error!("Invalid transaction type",)
        );
    }

    #[rstest]
    #[tokio::test]
    async fn test_out_of_range(path: PathBuf) {
        let mut transaction_log = TransactionLog::try_load_or_create(&path, 100)
            .await
            .unwrap();
        assert_eq!(
            transaction_log
                .push_back(Transaction::WriteRecord(1))
                .await
                .unwrap(),
            None
        );
        assert_eq!(
            transaction_log
                .push_back(Transaction::WriteRecord(2))
                .await
                .unwrap(),
            None
        );
        assert_eq!(transaction_log.len(), 2);
        assert!(!transaction_log.is_empty());

        assert_eq!(
            transaction_log.front(3).await.unwrap(),
            vec![Transaction::WriteRecord(1), Transaction::WriteRecord(2),],
            "We return only the available transactions."
        );

        assert_eq!(
            transaction_log.pop_front(3).await.unwrap(),
            2,
            "We pop only the available transactions."
        );
        assert!(transaction_log.is_empty());
    }

    #[rstest]
    #[tokio::test]
    async fn test_overflow(path: PathBuf) {
        let mut transaction_log = TransactionLog::try_load_or_create(&path, 3).await.unwrap();
        for i in 1..5 {
            transaction_log
                .push_back(Transaction::WriteRecord(i))
                .await
                .unwrap();
        }

        assert_eq!(transaction_log.len(), 2);
        assert_eq!(
            transaction_log.front(2).await.unwrap(),
            vec![Transaction::WriteRecord(3), Transaction::WriteRecord(4),]
        );
    }

    #[rstest]
    #[tokio::test]
    async fn test_recovery(path: PathBuf) {
        let mut transaction_log = TransactionLog::try_load_or_create(&path, 3).await.unwrap();
        for i in 1..5 {
            transaction_log
                .push_back(Transaction::WriteRecord(i))
                .await
                .unwrap();
        }

        let mut transaction_log = TransactionLog::try_load_or_create(&path, 3).await.unwrap();
        assert_eq!(transaction_log.len(), 2);
        assert_eq!(
            transaction_log.front(2).await.unwrap(),
            vec![Transaction::WriteRecord(3), Transaction::WriteRecord(4),]
        );

        assert_eq!(transaction_log.pop_front(2).await.unwrap(), 2);
        assert!(transaction_log.is_empty());
    }

    #[rstest]
    #[tokio::test]
    async fn test_recovery_init(path: PathBuf) {
        let mut transaction_log = TransactionLog::try_load_or_create(&path, 3).await.unwrap();
        transaction_log
            .push_back(Transaction::WriteRecord(1))
            .await
            .unwrap();
        drop(transaction_log);

        let transaction_log = TransactionLog::try_load_or_create(&path, 3).await.unwrap();
        assert_eq!(transaction_log.write_pos, HEADER_SIZE + ENTRY_SIZE);
        assert_eq!(transaction_log.read_pos, HEADER_SIZE);
    }

    #[rstest]
    #[tokio::test]
    async fn test_recovery_empty_cache(path: PathBuf) {
        let mut transaction_log = TransactionLog::try_load_or_create(&path, 3).await.unwrap();
        transaction_log
            .push_back(Transaction::WriteRecord(1))
            .await
            .unwrap();

        FILE_CACHE.discard_recursive(&path).await.unwrap(); // discard the cache to simulate restart

        let mut transaction_log = TransactionLog::try_load_or_create(&path, 3).await.unwrap();

        // check if the transaction log is still working after cache discard
        assert_eq!(
            transaction_log.front(1).await.unwrap(),
            vec![Transaction::WriteRecord(1)]
        );
        assert_eq!(transaction_log.pop_front(1).await.unwrap(), 1);
        assert!(transaction_log.is_empty());
    }

    #[rstest]
    #[tokio::test]
    async fn test_resize_empty_log(path: PathBuf) {
        TransactionLog::try_load_or_create(&path, 3).await.unwrap();
        assert_eq!(
            fs::metadata(&path).unwrap().len() as usize,
            ENTRY_SIZE * 3 + HEADER_SIZE
        );

        TransactionLog::try_load_or_create(&path, 5).await.unwrap();
        assert_eq!(
            fs::metadata(&path).unwrap().len() as usize,
            ENTRY_SIZE * 5 + HEADER_SIZE
        );
    }

    #[rstest]
    #[tokio::test]
    async fn test_resize_non_empty_log(path: PathBuf) {
        let mut transaction_log = TransactionLog::try_load_or_create(&path, 3).await.unwrap();
        transaction_log
            .push_back(Transaction::WriteRecord(1))
            .await
            .unwrap();
        assert_eq!(
            fs::metadata(&path).unwrap().len() as usize,
            ENTRY_SIZE * 3 + HEADER_SIZE
        );

        TransactionLog::try_load_or_create(&path, 5).await.unwrap();
        assert_eq!(
            fs::metadata(&path).unwrap().len() as usize,
            ENTRY_SIZE * 3 + HEADER_SIZE,
            "The log is not empty, so the capacity should not be changed."
        );
    }

    mod integrity_tests {
        use super::*;

        #[rstest]
        #[case([0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 16], "Invalid write position 17 in transaction log"
        )]
        #[case([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 16], "Invalid write position 1 in transaction log"
        )]
        #[case([0, 0, 0, 0, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 16], "Invalid write position 34 in transaction log"
        )]
        #[case([0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 17], "Invalid read position 17 in transaction log"
        )]
        #[case([0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 1], "Invalid read position 1 in transaction log"
        )]
        #[case([0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 34], "Invalid read position 34 in transaction log"
        )]
        #[tokio::test]
        async fn test_invalid_position(
            #[case] buf: [u8; 16],
            #[case] expected_error: &str,
            path: PathBuf,
        ) {
            {
                let mut file = FILE_CACHE
                    .write_or_create(&path, SeekFrom::Start(0))
                    .await
                    .unwrap();
                file.write_all(&buf).unwrap();
                file.set_len((HEADER_SIZE + ENTRY_SIZE * 2) as u64).unwrap();
            }

            let result = TransactionLog::try_load_or_create(&path, 3);
            assert_eq!(
                result.await.err().unwrap(),
                internal_server_error!("{} {}", expected_error, path.to_str().unwrap())
            );
        }

        #[rstest]
        #[tokio::test]
        async fn test_invalid_size(path: PathBuf) {
            {
                let mut file = FILE_CACHE
                    .write_or_create(&path, SeekFrom::Start(0))
                    .await
                    .unwrap();
                file.write_all(&[0u8; 16]).unwrap();
                file.set_len((HEADER_SIZE + 1) as u64).unwrap();
            }

            let result = TransactionLog::try_load_or_create(&path, 3);
            assert_eq!(
                result.await.err().unwrap(),
                internal_server_error!(
                    "Invalid size 17 of transaction log {}",
                    path.to_str().unwrap()
                )
            );
        }
    }

    #[fixture]
    fn path() -> PathBuf {
        let path = tempdir().unwrap().keep().join("transaction_log");
        path
    }
}