Skip to main content

coven_database/
database_retry.rs

1//! Persisted retry waits through the owning database (§6.7).
2
3use super::StoreDatabase;
4use crate::{
5    retry::{self, RetrySubject, RetryWait},
6    DbError,
7};
8
9impl StoreDatabase {
10    /// Persist a failed pass's connection condition and retry deadline together.
11    /// A failure already owned by a slot or operation supplies no connection record.
12    pub async fn record_sync_retry<P: crate::PendingValue>(
13        &self,
14        connection: P::Subject,
15        record: Option<P>,
16        wait: Option<RetryWait>,
17    ) -> Result<(), DbError>
18    where
19        P::Subject: Send + 'static,
20    {
21        self.call(move |inner| {
22            inner.access.with_writer(|writer, _| {
23                crate::pending::state_transaction(writer, |db| {
24                    if let Some(record) = record {
25                        crate::pending::save(db, &record)?;
26                    } else {
27                        crate::pending::clear(db, &connection)?;
28                    }
29                    retry::set(db, RetrySubject::Sync, wait.as_ref())
30                })
31            })
32        })
33        .await
34    }
35
36    /// A successful pass settles its connection condition and wait atomically.
37    pub async fn clear_sync_retry<S: serde::Serialize + Send + 'static>(
38        &self,
39        connection: S,
40    ) -> Result<(), DbError> {
41        self.call(move |inner| {
42            let changed = inner.access.with_reader(|db, _| {
43                Ok::<_, DbError>(
44                    retry::find(db, RetrySubject::Sync)?.is_some()
45                        || crate::pending::recorded(db, &connection)?,
46                )
47            })?;
48            if !changed {
49                return Ok(());
50            }
51            inner.access.with_writer(|writer, _| {
52                crate::pending::state_transaction(writer, |db| {
53                    retry::clear(db, RetrySubject::Sync)?;
54                    crate::pending::clear(db, &connection)
55                })
56            })
57        })
58        .await
59    }
60
61    /// Persist an operation's failure and its automatic retry deadline together.
62    pub async fn record_operation_retry<P: crate::PendingValue>(
63        &self,
64        record: P,
65        wait: Option<RetryWait>,
66    ) -> Result<(), DbError> {
67        self.call(move |inner| {
68            let id = record
69                .operation()
70                .ok_or(DbError::Invariant { what: "an operation retry names its journal row" })?;
71            inner.access.with_writer(|writer, _| {
72                crate::pending::state_transaction(writer, |db| {
73                    retry::save_failure(db, &record, RetrySubject::Operation(id), wait.as_ref())
74                })
75            })
76        })
77        .await
78    }
79
80    /// Persist an operation's failure and its automatic retry deadline
81    /// together, while the operation is still at `step`, the step the failed
82    /// attempt read, and return whether it did. A writer outside the attempt,
83    /// such as the app's write deciding an invite or the invite's expiry, may
84    /// have moved the operation on since, or ended it: the failure then
85    /// belongs to a step the operation has left, and recording it would hold
86    /// the operation's next step back behind a wait it never met.
87    pub async fn record_operation_retry_at<P: crate::PendingValue>(
88        &self,
89        record: P,
90        wait: Option<RetryWait>,
91        step: u32,
92    ) -> Result<bool, DbError> {
93        self.call(move |inner| {
94            let id = record
95                .operation()
96                .ok_or(DbError::Invariant { what: "an operation retry names its journal row" })?;
97            inner.access.with_writer(|writer, _| {
98                crate::pending::state_transaction(writer, |db| {
99                    if crate::operation::find(db, id)?.is_none_or(|row| row.last_step != step) {
100                        return Ok(false);
101                    }
102                    retry::save_failure(db, &record, RetrySubject::Operation(id), wait.as_ref())
103                        .map(|()| true)
104                })
105            })
106        })
107        .await
108    }
109
110    /// End the operation's pending record and retry wait while the operation
111    /// is still at `step`, the step the attempt that succeeded read: a writer
112    /// outside the attempt may have moved it on, and the record it then
113    /// holds belongs to a later step. The usual case, with nothing recorded,
114    /// reads without taking the writer.
115    pub async fn clear_operation_pending_at(
116        &self,
117        id: crate::OperationId,
118        step: u32,
119    ) -> Result<(), DbError> {
120        self.call(move |inner| {
121            let recorded =
122                inner.access.with_reader(|db, _| crate::pending::operation_recorded(db, id))?;
123            if !recorded {
124                return Ok(());
125            }
126            inner.access.with_writer(|writer, _| {
127                crate::pending::state_transaction(writer, |db| {
128                    if crate::operation::find(db, id)?.is_some_and(|row| row.last_step == step) {
129                        crate::pending::clear_operation(db, id)?;
130                    }
131                    Ok(())
132                })
133            })
134        })
135        .await
136    }
137
138    /// Every persisted wait, for arming on reopen (§6.7).
139    pub async fn retry_waits(&self) -> Result<Vec<(RetrySubject, RetryWait)>, DbError> {
140        self.call(move |inner| inner.access.with_reader(|db, _| retry::read_all(db))).await
141    }
142
143    /// The subject's persisted wait, if one is armed.
144    pub async fn retry_wait(&self, subject: RetrySubject) -> Result<Option<RetryWait>, DbError> {
145        self.call(move |inner| inner.access.with_reader(|db, _| retry::find(db, subject))).await
146    }
147
148    /// The same wait, live (E4), for a test waiting for a failure to arm it.
149    #[cfg(any(test, feature = "test-utils"))]
150    pub fn subscribe_retry_wait(
151        &self,
152        subject: RetrySubject,
153    ) -> crate::LiveQuery<Option<RetryWait>, crate::DbError> {
154        self.follow(move |db| retry::find(db, subject))
155    }
156
157    /// Persist `T` and `W` before the subject waits. A failure here fails the
158    /// initiating work; it is never permission to retry immediately.
159    pub async fn record_retry(
160        &self,
161        subject: RetrySubject,
162        wait: RetryWait,
163    ) -> Result<(), DbError> {
164        self.call(move |inner| {
165            inner
166                .access
167                .with_writer(|writer, _| writer.transaction(|db| retry::save(db, subject, &wait)))
168        })
169        .await
170    }
171
172    /// End, in one transaction, the persisted waits `select` picks after
173    /// reading every wait, every pending record, the upload queue and the pin
174    /// requests as one committed state. Each ended wait keeps its failure count; its deadline
175    /// becomes `now` and its full wait zero, so reopening arms none (§6.7).
176    /// Returns the ended waits.
177    pub async fn end_retry_waits<P: crate::PendingValue>(
178        &self,
179        now: std::time::SystemTime,
180        select: impl FnOnce(
181                &[(RetrySubject, RetryWait)],
182                &[P],
183                &[crate::FileUpload],
184                &[crate::PinRequest],
185            ) -> Vec<RetrySubject>
186            + Send
187            + 'static,
188    ) -> Result<Vec<(RetrySubject, RetryWait)>, DbError> {
189        self.call(move |inner| {
190            #[cfg(any(test, feature = "test-utils"))]
191            let injected = inner
192                .retry_end_failures
193                .try_update(
194                    std::sync::atomic::Ordering::SeqCst,
195                    std::sync::atomic::Ordering::SeqCst,
196                    |left| left.checked_sub(1),
197                )
198                .is_ok();
199            inner.access.with_writer(|writer, _| {
200                writer.transaction(|db| {
201                    #[cfg(any(test, feature = "test-utils"))]
202                    if injected {
203                        return Err(DbError::Sqlite(sqlive::rusqlite::Error::SqliteFailure(
204                            sqlive::rusqlite::ffi::Error::new(sqlive::rusqlite::ffi::SQLITE_IOERR),
205                            Some("injected retry end failure".into()),
206                        )));
207                    }
208                    let waits = retry::read_all(db)?;
209                    let pending = crate::pending::read::<P>(db)?;
210                    let uploads = crate::file_queue::read(db)?;
211                    let pins = crate::file_pins::read(db)?;
212                    let mut ended = Vec::new();
213                    for subject in select(&waits, &pending, &uploads, &pins) {
214                        let Some((_, wait)) = waits.iter().find(|(held, _)| *held == subject)
215                        else {
216                            return Err(DbError::Invariant {
217                                what: "an ended retry wait is persisted",
218                            });
219                        };
220                        retry::end(db, subject, now)?;
221                        ended.push((
222                            subject,
223                            RetryWait {
224                                failures: wait.failures,
225                                not_before: now,
226                                wait: std::time::Duration::ZERO,
227                            },
228                        ));
229                    }
230                    Ok(ended)
231                })
232            })
233        })
234        .await
235    }
236
237    /// Fail the next `count` `end_retry_waits` transactions with a disk error.
238    #[cfg(any(test, feature = "test-utils"))]
239    pub async fn fail_retry_ends(&self, count: usize) {
240        self.call(move |inner| {
241            inner.retry_end_failures.store(count, std::sync::atomic::Ordering::SeqCst);
242            Ok::<_, DbError>(())
243        })
244        .await
245        .expect("an open store arms its retry-end failures");
246    }
247
248    /// Forget a wait once its work succeeds or no longer retries automatically.
249    pub async fn clear_retry(&self, subject: RetrySubject) -> Result<(), DbError> {
250        self.call(move |inner| {
251            inner.access.with_writer(|writer, _| writer.transaction(|db| retry::clear(db, subject)))
252        })
253        .await
254    }
255}