1use super::StoreDatabase;
4use crate::{
5 retry::{self, RetrySubject, RetryWait},
6 DbError,
7};
8
9impl StoreDatabase {
10 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 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 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 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 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 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 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 #[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 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 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 #[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 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}