in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits
| 1 | //! The hourly sweeper. It removes what has expired or is no longer named by any row, and nothing else. Each |
| 2 | //! pass does these steps, and a failed step is logged while the others still run: |
| 3 | //! |
| 4 | //! - sessions past their expiry, back-channel logout ids past their expiry, and open uploads not finished in |
| 5 | //! 24 hours, with their staging directories; |
| 6 | //! - staging directories under `data_dir/uploads` that no open upload names and that are at least 24 hours old. |
| 7 | //! This is what retries a removal that failed earlier: the expiry step has already deleted the row; |
| 8 | //! the directory is the only record left; |
| 9 | //! - trash older than `purge_after_days` (a setting, 30 by default), with its bytes; |
| 10 | //! - audit rows older than `AUDIT_RETENTION_DAYS` (365), in batches of `AUDIT_PURGE_BATCH`; |
| 11 | //! - blobs under `files/`, `thumbs/` and `objects/` that no row names and that are at least 24 hours old; |
| 12 | //! - leftover files in `data_dir/tmp` at least 24 hours old; |
| 13 | //! - once a day, the quota check: every member's `used_bytes` is recomputed from their files, and any |
| 14 | //! difference is logged and corrected. |
| 15 | //! |
| 16 | //! The rows go first, in their own transactions; the bytes are deleted after the row that named them is gone. |
| 17 | //! No blob is removed while a row names it, and none younger than 24 hours is removed for lacking a row, which |
| 18 | //! covers a write whose row has not yet committed. |
| 19 | |
| 20 | use HashSet; |
| 21 | use Path; |
| 22 | use ; |
| 23 | |
| 24 | use Result; |
| 25 | use ; |
| 26 | use AUDIT_RETENTION_DAYS; |
| 27 | use ; |
| 28 | use watch; |
| 29 | |
| 30 | use crateAppState; |
| 31 | |
| 32 | /// Time between two passes: one hour. |
| 33 | pub const SWEEP_EVERY: Duration = from_secs; |
| 34 | |
| 35 | /// Age a blob or a staged file must reach before the sweeper may remove it for lacking a row: 24 hours. |
| 36 | pub const ORPHAN_GRACE: Duration = from_secs; |
| 37 | |
| 38 | /// The settings key that records when the quota check last ran. |
| 39 | const DRIFT_SETTING: &str = "quota_drift_checked_at"; |
| 40 | |
| 41 | /// Time between two quota checks: a day. |
| 42 | const DRIFT_EVERY: Duration = from_secs; |
| 43 | |
| 44 | /// The blob prefixes the sweeper owns. `objects/` is included because the `objects` table names its keys. |
| 45 | const BLOB_PREFIXES: = ; |
| 46 | |
| 47 | /// Runs passes until the stop flag is set. |
| 48 | pub async |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | /// One pass. The expiry steps come first; each later step is independent, so a failure in one is logged and |
| 61 | /// the rest still run. |
| 62 | pub async |
| 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 | /// Logs a failed step and drops its error, so one failing step does not stop the pass. |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | /// The number of days trash is kept: the setting when it holds a number, else the default. Clamped to 1 to |
| 101 | /// 3650 days. |
| 102 | pub async |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | /// Purges trash older than the retention period, and deletes the bytes the store released. The rows go first; |
| 113 | /// a blob that fails to delete is logged and left for a later pass to find as unreferenced. |
| 114 | async |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | /// Removes audit rows older than [`AUDIT_RETENTION_DAYS`]. The store deletes them in batches, so a large backlog |
| 131 | /// is cleared over several statements and the writer lock is free between them. |
| 132 | async |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | /// True when `modified` is at least [`ORPHAN_GRACE`] before `now`. A timestamp in the future counts as young. |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | /// Removes the blobs that no row names and that are at least [`ORPHAN_GRACE`] old, as of `now`. The set of |
| 148 | /// referenced keys is read before the listing, so a blob written in between is young and kept. |
| 149 | pub async |
| 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 | /// Removes the staging directories under `data_dir/uploads` that no open upload names and that are at least |
| 180 | /// [`ORPHAN_GRACE`] old, as of `now`. Only a direct child of that directory whose name is a well-formed upload |
| 181 | /// id is removed; symbolic links and other entries are left alone. |
| 182 | pub async |
| 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 | /// Removes the plain files in `data_dir/tmp` that are at least [`ORPHAN_GRACE`] old. Links are never followed, |
| 230 | /// and only regular files are removed. The directory is the fixed staging directory of the data directory. |
| 231 | pub async |
| 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 | /// Recomputes every member's used bytes from their files, at most once a day. Each difference is logged with |
| 268 | /// the stored and the true value, and then corrected. Returns the number of members corrected. |
| 269 | pub async |
| 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 |