reductstore 1.19.0

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
// Copyright 2025-2026 ReductSoftware UG
// Licensed under the Business Source License 1.1

use crate::cfg::{Cfg, InstanceRole};
use crate::core::file_cache::FILE_CACHE;
use crate::core::sync::AsyncRwLock;
use async_trait::async_trait;
use log::{debug, error, info, warn};
use reduct_base::error::ReductError;
use std::io::SeekFrom::Start;
use std::io::Write;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Duration;
use tokio::time::sleep;

#[derive(Debug, PartialEq)]
pub enum State {
    Waiting,
    Locked,
    Failed,
}

#[derive(Debug, PartialEq, Clone, Default)]
pub enum FailureAction {
    Proceed,
    #[default]
    Abort,
}

#[async_trait]
pub trait LockFile {
    async fn is_locked(&self) -> Result<bool, ReductError>;
    async fn is_failed(&self) -> Result<bool, ReductError>;
    async fn is_waiting(&self) -> Result<bool, ReductError>;
    async fn release(&self);
}
pub type BoxedLockFile = Box<dyn LockFile + Sync + Send>;
struct ImplLockFile {
    path: PathBuf,
    stop_on_drop: Arc<AtomicBool>,
    handle: tokio::task::JoinHandle<()>,
    state: Arc<AsyncRwLock<State>>,
}

pub(crate) struct LockFileBuilder {
    path_buf: PathBuf,
    config: Cfg,
}
impl LockFileBuilder {
    pub fn noop() -> BoxedLockFile {
        Box::new(NoopLockFile {})
    }

    pub fn new(path_buf: PathBuf) -> Self {
        Self {
            path_buf,
            config: Cfg::default(),
        }
    }

    pub fn with_config(mut self, config: Cfg) -> Self {
        self.config = config;
        self
    }

    pub fn build(self) -> BoxedLockFile {
        Self::from_config(self.path_buf, self.config)
    }

    fn from_config(path: PathBuf, cfg: Cfg) -> BoxedLockFile {
        let role = cfg.role;
        let cfg = cfg.lock_file_config;

        // Atomic flag to signal the background task to stop
        let stop_on_drop = Arc::new(AtomicBool::new(false));
        let stop_flag = Arc::clone(&stop_on_drop);
        let file_path = path.clone();
        let state = Arc::new(AsyncRwLock::new(State::Waiting));
        let state_clone = Arc::clone(&state);

        let mut this = Box::new(ImplLockFile {
            path,
            stop_on_drop,
            handle: tokio::spawn(async {}),
            state,
        });

        let handle = tokio::spawn(async move {
            if let Err(err) =
                Self::run_lock_task(file_path, cfg, role, state_clone, stop_flag).await
            {
                error!("Lock file task failed: {}", err);
            }
        });

        this.handle = handle;
        this
    }

    async fn run_lock_task(
        file_path: PathBuf,
        cfg: crate::cfg::lock_file::LockFileConfig,
        role: InstanceRole,
        state: Arc<AsyncRwLock<State>>,
        stop_flag: Arc<AtomicBool>,
    ) -> Result<(), ReductError> {
        // for future use, we generate a unique id for the lock file
        let unique_id = format!("{}-{}", std::process::id(), uuid::Uuid::new_v4());

        // Main loop to acquire and maintain the lock
        loop {
            // Check if the file is already locked
            let time_start = std::time::Instant::now();
            while FILE_CACHE.try_exists(&file_path).await?
                && !stop_flag.load(std::sync::atomic::Ordering::SeqCst)
            {
                if let Some(last_modified) = FILE_CACHE
                    .get_stats(&file_path)
                    .await?
                    .and_then(|meta| meta.modified_time)
                {
                    // elapsed can fail if system time is changed backwards, so we default to 0 duration
                    if last_modified.elapsed().unwrap_or(Duration::from_secs(0)) > cfg.ttl
                        && cfg.ttl.as_secs() > 0
                    {
                        warn!(
                            "Lock file is stale (last modified over {:?} ago), removing: {:?}",
                            last_modified.elapsed().unwrap(),
                            file_path
                        );
                        if let Err(err) = FILE_CACHE.remove(&file_path).await {
                            error!("Failed to remove stale lock file: {:?}", err);
                        }
                        break;
                    }
                }

                if time_start.elapsed() > cfg.timeout && cfg.timeout.as_secs() > 0 {
                    match cfg.failure_action {
                        FailureAction::Proceed => {
                            warn!(
                                "Timeout while waiting for lock file, proceeding anyway: {:?}",
                                file_path
                            );
                            break;
                        }
                        FailureAction::Abort => {
                            error!(
                                "Timeout while waiting for lock file, aborting: {:?}",
                                file_path
                            );
                            *state.write().await? = State::Failed;
                            return Ok(());
                        }
                    }
                }
                tokio::time::sleep(cfg.polling_interval).await;
            }

            // Final check to see if we should stop
            if stop_flag.load(std::sync::atomic::Ordering::SeqCst) {
                break;
            }

            match role {
                InstanceRole::Primary => {
                    // Primary instance acquires the lock immediately
                    info!("Primary instance acquiring lock file: {:?}", file_path);
                    *state.write().await? = State::Locked;
                }
                InstanceRole::Secondary => {
                    // Secondary instance waits a bit to ensure the primary has created the lock file
                    tokio::time::sleep(cfg.polling_interval * 3).await;
                    if !FILE_CACHE.try_exists(&file_path).await? {
                        info!("Secondary instance acquiring lock file: {:?}", file_path);
                        *state.write().await? = State::Locked;
                    } else {
                        info!("Secondary instance could not acquire lock file (already held by primary): {:?}", file_path);
                    }
                }
                InstanceRole::Replica | InstanceRole::Standalone => {}
            }

            if *state.read().await? == State::Locked {
                break;
            }
        }

        // we need to sync the file to ensure that the lock is visible to other processes
        // With Blue/Green deployments, the file system is shared between instances
        // so we need to keep the file locked as long as the process is running and recreate it if it gets deleted
        // during deployments
        while !stop_flag.load(std::sync::atomic::Ordering::SeqCst) {
            let recreate = async {
                let mut file = FILE_CACHE.write_or_create(&file_path, Start(0)).await?;
                file.write_all(unique_id.as_bytes())?;
                file.sync_all().await?;
                Ok::<(), ReductError>(())
            };

            if let Err(e) = recreate.await {
                error!("Error while recreating lock file: {}", e);
            }

            tokio::time::sleep(cfg.polling_interval).await;
        }

        Ok(())
    }
}

impl ImplLockFile {}

#[async_trait]
impl LockFile for ImplLockFile {
    async fn is_locked(&self) -> Result<bool, ReductError> {
        Ok(*self.state.read().await? == State::Locked)
    }

    async fn is_failed(&self) -> Result<bool, ReductError> {
        Ok(*self.state.read().await? == State::Failed)
    }

    async fn is_waiting(&self) -> Result<bool, ReductError> {
        Ok(*self.state.read().await? == State::Waiting)
    }

    async fn release(&self) {
        self.stop_on_drop
            .store(true, std::sync::atomic::Ordering::SeqCst);

        let start_time = std::time::Instant::now();
        while !self.handle.is_finished() && start_time.elapsed() < Duration::from_secs(5) {
            sleep(Duration::from_millis(100)).await;
        }

        debug!("Releasing lock file: {:?}", self.path);
        let path = self.path.clone();
        if let Err(err) = FILE_CACHE.remove(&path).await {
            error!("Failed to remove lock file: {:?}", err);
        }
    }
}

impl Drop for ImplLockFile {
    fn drop(&mut self) {
        self.stop_on_drop
            .store(true, std::sync::atomic::Ordering::SeqCst);

        let path = self.path.clone();
        // Use block_in_place to handle async cleanup in drop
        let handle =
            tokio::runtime::Handle::try_current().expect("Failed to get current Tokio handle");
        let _ = std::thread::spawn(move || {
            handle.block_on(async {
                tokio::time::sleep(Duration::from_millis(100)).await;
                match FILE_CACHE.remove(&path).await {
                    Ok(_) => {}
                    Err(err) => {
                        error!("Failed to remove lock file: {:?}", err);
                    }
                }
            });
        })
        .join();
    }
}

struct NoopLockFile;

#[async_trait]
impl LockFile for NoopLockFile {
    async fn is_locked(&self) -> Result<bool, ReductError> {
        Ok(true)
    }

    async fn is_failed(&self) -> Result<bool, ReductError> {
        Ok(false)
    }

    async fn is_waiting(&self) -> Result<bool, ReductError> {
        Ok(false)
    }

    async fn release(&self) {}
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cfg::lock_file::LockFileConfig;
    use crate::cfg::{Cfg, InstanceRole};
    use rstest::{fixture, rstest};
    use std::fs;
    use tempfile::tempdir;
    use test_log::test as test_log;
    use tokio::time::error::Elapsed;
    use tokio::time::timeout;

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_lock_file_acquire_and_release(lock_file_path: PathBuf) {
        let lock_file = LockFileBuilder::new(lock_file_path.clone()).build();

        // The lock may be acquired quickly; only wait if we observe it waiting.
        if lock_file.is_waiting().await.unwrap() {
            let acquired = wait_new_state(&lock_file).await;
            assert!(acquired.is_ok(), "Lock file was not acquired in time");
        }
        assert!(lock_file.is_locked().await.unwrap());
        assert!(!lock_file.is_failed().await.unwrap());
        assert!(!lock_file.is_waiting().await.unwrap());

        assert_ne!(
            fs::read_to_string(&lock_file_path).unwrap(),
            "dummy",
            "Lock file must be overwritten"
        );

        lock_file.release().await;
        assert!(
            !lock_file_path.exists(),
            "Lock file must be deleted on release"
        );
    }

    #[test_log(rstest)]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_lock_file_timeout_abort(lock_file_path: PathBuf) {
        fs::write(&lock_file_path, "dummy").unwrap();
        let lock_file = LockFileBuilder::new(lock_file_path.clone())
            .with_config(test_cfg(
                LockFileConfig {
                    polling_interval: Duration::from_millis(500),
                    timeout: Duration::from_secs(2),
                    ..Default::default()
                },
                InstanceRole::Primary,
            ))
            .build();

        // Initially, the lock file should be in waiting state
        assert!(lock_file.is_waiting().await.unwrap());

        // Wait for the lock to fail due to timeout
        let failed = wait_new_state(&lock_file).await;
        assert!(failed.is_ok(), "Lock file did not fail in time");
        assert!(lock_file.is_failed().await.unwrap());
        assert!(!lock_file.is_locked().await.unwrap());
        assert!(!lock_file.is_waiting().await.unwrap());

        assert_eq!(
            fs::read_to_string(&lock_file_path).unwrap(),
            "dummy",
            "Lock file must not be overwritten"
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_lock_file_timeout_proceed(lock_file_path: PathBuf) {
        fs::write(&lock_file_path, "dummy").unwrap();
        let lock_file = LockFileBuilder::new(lock_file_path.clone())
            .with_config(test_cfg(
                LockFileConfig {
                    polling_interval: Duration::from_millis(500),
                    timeout: Duration::from_secs(2),
                    failure_action: FailureAction::Proceed,
                    ..Default::default()
                },
                InstanceRole::Primary,
            ))
            .build();

        // Initially, the lock file should be in waiting state
        assert!(lock_file.is_waiting().await.unwrap());

        // Wait for the lock to be acquired despite the timeout
        let acquired = wait_new_state(&lock_file).await;
        assert!(acquired.is_ok(), "Lock file was not acquired in time");
        assert!(lock_file.is_locked().await.unwrap());
        assert!(!lock_file.is_failed().await.unwrap());
        assert!(!lock_file.is_waiting().await.unwrap());

        assert_ne!(
            fs::read_to_string(&lock_file_path).unwrap(),
            "dummy",
            "Lock file must be overwritten"
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_secondary_instance_waits(lock_file_path: PathBuf) {
        let primary_lock_file = LockFileBuilder::new(lock_file_path.clone())
            .with_config(test_cfg(
                LockFileConfig {
                    polling_interval: Duration::from_millis(500),
                    ..Default::default()
                },
                InstanceRole::Primary,
            ))
            .build();

        let secondary_lock_file = LockFileBuilder::new(lock_file_path.clone())
            .with_config(test_cfg(
                LockFileConfig {
                    polling_interval: Duration::from_millis(500),
                    ..Default::default()
                },
                InstanceRole::Secondary,
            ))
            .build();

        // Wait for the primary to acquire the lock
        let primary_acquired = wait_new_state(&primary_lock_file).await;
        assert!(
            primary_acquired.is_ok(),
            "Primary lock file was acquired in time"
        );
        assert!(primary_lock_file.is_locked().await.unwrap());

        // Wait for the secondary to acquire the lock
        let secondary_acquired = wait_new_state(&secondary_lock_file).await;
        assert!(
            secondary_acquired.is_err(),
            "Secondary lock file was not acquired in time"
        );
        assert!(secondary_lock_file.is_waiting().await.unwrap());

        // Release primary lock
        primary_lock_file.release().await;
        let secondary_acquired = wait_new_state(&secondary_lock_file).await;
        assert!(
            secondary_acquired.is_ok(),
            "Secondary lock file was acquired in time"
        );
        assert!(secondary_lock_file.is_locked().await.unwrap());

        secondary_lock_file.release().await;
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_ttl_removes_stale_lock(lock_file_path: PathBuf) {
        fs::write(&lock_file_path, "dummy").unwrap();
        let lock_file = LockFileBuilder::new(lock_file_path.clone())
            .with_config(test_cfg(
                LockFileConfig {
                    polling_interval: Duration::from_millis(500),
                    ttl: Duration::from_secs(1),
                    ..Default::default()
                },
                InstanceRole::Primary,
            ))
            .build();

        // Initially, the lock file should be in waiting state
        assert!(lock_file.is_waiting().await.unwrap());

        // Wait for the lock to be acquired after TTL expires
        let acquired = wait_new_state(&lock_file).await;
        assert!(acquired.is_ok(), "Lock file was not acquired in time");
        assert!(lock_file.is_locked().await.unwrap());
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_secondary_acquires_if_file_missing(lock_file_path: PathBuf) {
        let lock_file = LockFileBuilder::new(lock_file_path.clone())
            .with_config(test_cfg(
                LockFileConfig {
                    polling_interval: Duration::from_millis(100),
                    ..Default::default()
                },
                InstanceRole::Secondary,
            ))
            .build();

        // Secondary should grab the lock if file does not exist after grace period.
        let acquired = wait_new_state(&lock_file).await;
        assert!(acquired.is_ok(), "Secondary failed to acquire lock");
        assert!(lock_file.is_locked().await.unwrap());
    }

    #[rstest]
    #[tokio::test]
    async fn test_run_lock_task_secondary_timeout_proceed_does_not_acquire_when_file_exists(
        lock_file_path: PathBuf,
    ) {
        fs::write(&lock_file_path, "dummy").unwrap();

        let stop_flag = Arc::new(AtomicBool::new(false));
        let state = Arc::new(AsyncRwLock::new(State::Waiting));

        let cfg = LockFileConfig {
            polling_interval: Duration::from_millis(10),
            timeout: Duration::from_secs(1),
            failure_action: FailureAction::Proceed,
            ..Default::default()
        };

        let task = tokio::spawn(LockFileBuilder::run_lock_task(
            lock_file_path.clone(),
            cfg,
            InstanceRole::Secondary,
            Arc::clone(&state),
            Arc::clone(&stop_flag),
        ));

        tokio::time::sleep(Duration::from_millis(1300)).await;
        assert_eq!(*state.read().await.unwrap(), State::Waiting);

        stop_flag.store(true, std::sync::atomic::Ordering::SeqCst);
        timeout(Duration::from_secs(2), task)
            .await
            .expect("run_lock_task must stop")
            .unwrap()
            .unwrap();
    }

    #[rstest]
    #[tokio::test]
    async fn test_run_lock_task_replica_runs_once_and_stops(lock_file_path: PathBuf) {
        let _ = fs::remove_file(&lock_file_path);
        let _ = fs::remove_dir_all(&lock_file_path);

        let stop_flag = Arc::new(AtomicBool::new(false));
        let state = Arc::new(AsyncRwLock::new(State::Waiting));
        let cfg = LockFileConfig {
            polling_interval: Duration::from_millis(10),
            timeout: Duration::from_secs(1),
            ..Default::default()
        };

        let task = tokio::spawn(LockFileBuilder::run_lock_task(
            lock_file_path,
            cfg,
            InstanceRole::Replica,
            state,
            Arc::clone(&stop_flag),
        ));

        tokio::task::yield_now().await;
        stop_flag.store(true, std::sync::atomic::Ordering::SeqCst);

        timeout(Duration::from_secs(2), task)
            .await
            .expect("run_lock_task must stop")
            .unwrap()
            .unwrap();
    }

    #[rstest]
    #[tokio::test]
    async fn test_run_lock_task_recreate_error_when_path_is_directory(lock_file_path: PathBuf) {
        let _ = fs::remove_file(&lock_file_path);
        fs::create_dir(&lock_file_path).unwrap();

        let stop_flag = Arc::new(AtomicBool::new(false));
        let state = Arc::new(AsyncRwLock::new(State::Waiting));

        let cfg = LockFileConfig {
            polling_interval: Duration::from_millis(10),
            timeout: Duration::from_secs(1),
            failure_action: FailureAction::Proceed,
            ..Default::default()
        };

        let task = tokio::spawn(LockFileBuilder::run_lock_task(
            lock_file_path,
            cfg,
            InstanceRole::Primary,
            Arc::clone(&state),
            Arc::clone(&stop_flag),
        ));

        tokio::time::sleep(Duration::from_millis(1300)).await;
        assert_eq!(*state.read().await.unwrap(), State::Locked);

        stop_flag.store(true, std::sync::atomic::Ordering::SeqCst);
        timeout(Duration::from_secs(2), task)
            .await
            .expect("run_lock_task must stop")
            .unwrap()
            .unwrap();
    }

    async fn wait_new_state(lock_file: &BoxedLockFile) -> Result<(), Elapsed> {
        let acquired = timeout(Duration::from_secs(10), async {
            loop {
                if !lock_file.is_waiting().await.unwrap() {
                    break;
                }
                tokio::time::sleep(Duration::from_millis(100)).await;
            }
        })
        .await;
        acquired
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_drops_lock_file(lock_file_path: PathBuf) {
        let lock_file = LockFileBuilder::new(lock_file_path.clone())
            .with_config(test_cfg(
                LockFileConfig {
                    polling_interval: Duration::from_millis(500),
                    ..Default::default()
                },
                InstanceRole::Primary,
            ))
            .build();

        // Wait for the lock to be acquired
        let acquired = wait_new_state(&lock_file).await;

        assert!(acquired.is_ok(), "Lock file was not acquired in time");
        assert!(lock_file.is_locked().await.unwrap());

        // Drop the lock file, which should trigger release
        drop(lock_file);

        // Wait a moment to ensure the lock file is released
        tokio::time::sleep(Duration::from_secs(1)).await;

        assert!(
            !lock_file_path.exists(),
            "Lock file must be deleted on drop"
        );
    }

    #[fixture]
    fn lock_file_path() -> PathBuf {
        let dir = tempdir().unwrap().keep();

        let filepath = dir.join("test.lock");
        filepath
    }

    fn test_cfg(lock_file_config: LockFileConfig, role: InstanceRole) -> Cfg {
        Cfg {
            lock_file_config,
            role,
            ..Default::default()
        }
    }
}