Skip to main content

coven_sync/
detection_record.rs

1//! The record of an S3 detection run (§11.6, §20.7): kept before the run's
2//! first probe, and ended once a cleanup has confirmed its probes and its
3//! unfinished multipart uploads gone. A run a crash cut leaves its record,
4//! which hands the run to the next detection at that bucket, or
5//! to the store's next connection there, to clean up after.
6
7use crate::SyncError;
8use coven_database::{
9    operation_kind::S3_DETECTION, NewOperation, OperationId, OperationRecord, StoreDatabase,
10};
11use coven_storage::StorageConfig;
12use serde::{Deserialize, Serialize};
13
14/// What a run's record holds.
15#[derive(Serialize, Deserialize)]
16struct DetectionWork {
17    /// Where its probes go under the store's `.coven-check` prefix.
18    run: uuid::Uuid,
19    /// The location it probed, as detection was given it.
20    location: StorageConfig,
21}
22
23/// One detection run's record, as committed.
24pub struct DetectionRecord {
25    id: OperationId,
26    work: DetectionWork,
27}
28
29impl DetectionRecord {
30    /// Record `run` at `location` before its first probe.
31    pub async fn start(
32        database: &StoreDatabase,
33        location: &StorageConfig,
34        run: uuid::Uuid,
35    ) -> Result<Self, SyncError> {
36        let work = DetectionWork { run, location: location.clone() };
37        let id = database
38            .start_operation(NewOperation {
39                kind: S3_DETECTION.into(),
40                data: serde_json::to_vec(&work)?,
41                started_by: S3_DETECTION.into(),
42                pauses_writes: false,
43            })
44            .await?;
45        Ok(Self { id, work })
46    }
47
48    /// The recorded runs whose probes sit in `location`'s bucket: the same
49    /// bucket at the same endpoint and region, whatever profile reached them.
50    pub async fn at(
51        database: &StoreDatabase,
52        location: &StorageConfig,
53    ) -> Result<Vec<Self>, SyncError> {
54        let mut records = Vec::new();
55        for record in database.operations().await? {
56            if record.kind != S3_DETECTION {
57                continue;
58            }
59            let work: DetectionWork = serde_json::from_slice(&record.data)?;
60            if work.location.same_s3_bucket(location) {
61                records.push(Self { id: record.id, work });
62            }
63        }
64        Ok(records)
65    }
66
67    /// The run this records.
68    pub fn run(&self) -> uuid::Uuid {
69        self.work.run
70    }
71
72    /// End the record, once a cleanup has confirmed the run's probes and
73    /// uploads gone.
74    pub async fn end(&self, database: &StoreDatabase) -> Result<(), SyncError> {
75        Ok(database.finish_operation(self.id).await?)
76    }
77
78    /// The detection runs a replaced installation's journal still owes
79    /// cleanup for, as new rows for its replacement's journal (§19.3).
80    pub fn carried(records: &[OperationRecord]) -> Vec<NewOperation> {
81        records
82            .iter()
83            .filter(|record| record.kind == S3_DETECTION)
84            .map(|record| NewOperation {
85                kind: S3_DETECTION.into(),
86                data: record.data.clone(),
87                started_by: S3_DETECTION.into(),
88                pauses_writes: false,
89            })
90            .collect()
91    }
92}
93
94#[cfg(test)]
95#[path = "detection_record_tests.rs"]
96mod tests;