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

use crate::cfg::{Cfg, InstanceRole};
use crate::core::file_cache::FILE_CACHE;
use crate::core::sync::AsyncRwLock;
use async_trait::async_trait;
use log::{error, info, warn};
use reduct_base::error::ReductError;
use std::io::Read;
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;

#[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>;
}
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> {
        // Each process instance keeps its own owner token in the lock file.
        let unique_id = format!("{}-{}", std::process::id(), uuid::Uuid::new_v4());

        // Main loop to acquire and maintain the lock

        let mut locked = false;
        while !(locked || stop_flag.load(std::sync::atomic::Ordering::SeqCst)) {
            // 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;
            }

            match role {
                InstanceRole::Primary => {
                    locked = true;
                }
                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? {
                        locked = true;
                    } else {
                        info!("Secondary instance could not acquire lock file (already held by primary): {:?}", file_path);
                    }
                }
                InstanceRole::Replica | InstanceRole::Standalone => {}
            }
        }

        // 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) {
            if let Err(e) = Self::write_lock_owner_id(&file_path, &unique_id).await {
                error!("Error while recreating lock file: {}", e);
            }

            if locked {
                *state.write().await? = State::Locked;
                locked = false; // don't touch the lock
            }

            tokio::time::sleep(cfg.polling_interval).await;
            Self::check_lock_owner_id(&file_path, &unique_id, role.clone()).await?;
        }

        Ok(())
    }

    async fn read_lock_owner_id(file_path: &PathBuf) -> Result<Option<String>, ReductError> {
        // we need to download lockfile in case it is cached with stale content
        if let Err(err) = FILE_CACHE.invalidate_local_cache_file(file_path).await {
            warn!(
                "Failed to invalidate local cache for lock file {:?}: {}",
                file_path, err
            );
        }

        let mut file = match FILE_CACHE.read(file_path, Start(0)).await {
            Ok(file) => file,
            Err(err) => {
                warn!("Failed to read lock file {:?}: {}", file_path, err);
                return Ok(None);
            }
        };

        let mut content = Vec::new();
        file.read_to_end(&mut content)?;
        let content = String::from_utf8_lossy(&content);
        let content = content.trim();
        if content.is_empty() {
            Ok(None)
        } else {
            Ok(Some(content.to_string()))
        }
    }

    async fn write_lock_owner_id(file_path: &PathBuf, owner_id: &str) -> Result<(), ReductError> {
        let mut file = FILE_CACHE.write_or_create(file_path, Start(0)).await?;
        file.set_len(0)?;
        file.write_all(owner_id.as_bytes())?;
        file.sync_all().await?;
        Ok(())
    }

    async fn check_lock_owner_id(
        file_path: &PathBuf,
        unique_id: &str,
        role: InstanceRole,
    ) -> Result<(), ReductError> {
        if let Some(owner_id) = Self::read_lock_owner_id(file_path).await? {
            if owner_id != unique_id {
                match role {
                    InstanceRole::Primary => {
                        error!(
                            "Lock file {:?} is owned by '{}', but primary is overwriting it with '{}'",
                            file_path, owner_id, unique_id
                        );
                    }
                    InstanceRole::Secondary => {
                        error!(
                            "Lock file {:?} is owned by '{}', secondary cannot acquire it",
                            file_path, owner_id
                        );
                        panic!(
                            "Lock file {:?} is owned by '{}', secondary cannot acquire it",
                            file_path, owner_id
                        );
                    }
                    InstanceRole::Replica | InstanceRole::Standalone => {}
                }
            }
        }

        Ok(())
    }
}

impl ImplLockFile {
    async fn remove_lock_file(path: &PathBuf) {
        if let Err(err) = FILE_CACHE.remove(path).await {
            error!("Failed to remove lock file: {:?}", err);
        }
    }
}

#[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)
    }
}

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;
                ImplLockFile::remove_lock_file(&path).await;
            });
        })
        .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)
    }
}

#[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_drop(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"
            );
        }

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

    #[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 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();

        {
            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();

            // 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());

            // Secondary should wait while primary lock exists.
            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());
        }

        wait_for_lock_file_cleanup(&lock_file_path).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());
    }

    #[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_read_lock_owner_id_returns_none_for_empty_file(lock_file_path: PathBuf) {
        fs::write(&lock_file_path, "").unwrap();

        assert_eq!(
            LockFileBuilder::read_lock_owner_id(&lock_file_path)
                .await
                .unwrap(),
            None
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_read_lock_owner_id_returns_none_when_read_fails(lock_file_path: PathBuf) {
        let _ = fs::remove_file(&lock_file_path);

        assert_eq!(
            LockFileBuilder::read_lock_owner_id(&lock_file_path)
                .await
                .unwrap(),
            None
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_check_lock_owner_id_allows_same_owner(lock_file_path: PathBuf) {
        let owner_id = format!("owner-{}", uuid::Uuid::new_v4());
        fs::write(&lock_file_path, &owner_id).unwrap();

        LockFileBuilder::check_lock_owner_id(&lock_file_path, &owner_id, InstanceRole::Secondary)
            .await
            .unwrap();
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    #[should_panic(expected = "secondary cannot acquire it")]
    async fn test_check_lock_owner_id_panics_for_secondary_with_foreign_uid(
        lock_file_path: PathBuf,
    ) {
        fs::write(&lock_file_path, format!("foreign-{}", uuid::Uuid::new_v4())).unwrap();
        let _ = LockFileBuilder::check_lock_owner_id(
            &lock_file_path,
            &format!("secondary-{}", uuid::Uuid::new_v4()),
            InstanceRole::Secondary,
        )
        .await
        .unwrap();
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_check_lock_owner_id_allows_primary_to_overwrite_foreign_uid(
        lock_file_path: PathBuf,
    ) {
        let owner_id = format!("primary-{}", uuid::Uuid::new_v4());
        fs::write(&lock_file_path, format!("foreign-{}", uuid::Uuid::new_v4())).unwrap();

        LockFileBuilder::check_lock_owner_id(&lock_file_path, &owner_id, InstanceRole::Primary)
            .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(250)).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();
    }

    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
    }

    async fn wait_for_lock_file_cleanup(lock_file_path: &PathBuf) {
        timeout(Duration::from_secs(3), async {
            while lock_file_path.exists() {
                tokio::time::sleep(Duration::from_millis(100)).await;
            }
        })
        .await
        .expect("Lock file must be cleaned up in time");
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_remove_lock_file_when_path_exists(lock_file_path: PathBuf) {
        fs::write(&lock_file_path, "lock").unwrap();

        ImplLockFile::remove_lock_file(&lock_file_path).await;

        assert!(!lock_file_path.exists());
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_remove_lock_file_when_already_removed(lock_file_path: PathBuf) {
        let _ = fs::remove_file(&lock_file_path);

        ImplLockFile::remove_lock_file(&lock_file_path).await;

        assert!(!lock_file_path.exists());
    }

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

        ImplLockFile::remove_lock_file(&lock_file_path).await;

        assert!(lock_file_path.exists());
    }

    #[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 and wait for cleanup.
        drop(lock_file);
        wait_for_lock_file_cleanup(&lock_file_path).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()
        }
    }
}