@@ -277,6 +277,8 @@ struct Cleanup {
277277 budget : Duration ,
278278 /// Passes in a row on this replica that ran out of time.
279279 overruns : Streak ,
280+ /// Passes in a row on this replica that failed.
281+ failures : Streak ,
280282}
281283
282284impl Cleanup {
@@ -286,6 +288,9 @@ impl Cleanup {
286288 /// alert is raised: the next pass picks up what one leaves, so a single
287289 /// burst is no problem.
288290 const BEHIND_AFTER : u32 = 3 ;
291+ /// Passes in a row that fail before the [`Alert::CleanupFailing`] alert is
292+ /// raised: a single one may be the database's passing failure.
293+ const FAILING_AFTER : u32 = 3 ;
289294
290295 /// Pause between full batches, allowing live requests to use the pool.
291296 const PAUSE : Duration = Duration :: from_millis ( 100 ) ;
@@ -297,41 +302,20 @@ impl Cleanup {
297302 Self {
298303 budget : Self :: BUDGET ,
299304 overruns : Streak :: new ( Self :: BEHIND_AFTER ) ,
305+ failures : Streak :: new ( Self :: FAILING_AFTER ) ,
300306 }
301307 }
302308
303- /// Runs one batch and returns how many rows it changed. Abandoning stuck keys
304- /// needs a transaction of its own, so its locks last until it decides.
305- async fn batch ( connection : & mut ExclusiveConnection ) -> Result < u64 , DatabaseError > {
306- let mut tx = connection. begin ( ) . await ?;
307- let abandoned = tx. abandon_stuck_deliveries ( Self :: BATCH_SIZE ) . await ?;
308- tx. commit ( ) . await ?;
309- let purged = connection
310- . maintenance ( )
311- . purge_expired_auth_state ( Self :: BATCH_SIZE )
312- . await ?;
313- Ok ( abandoned + purged)
314- }
315- }
316-
317- impl ExclusiveJob for Cleanup {
318- const NAME : & ' static str = "cleanup" ;
319- const LOCK : LockKey = LockKey :: Cleanup ;
320- const EVERY : Duration = Duration :: from_mins ( 5 ) ;
321- type Error = DatabaseError ;
322-
323- /// Stops after the current batch once shutdown is requested. The
324- /// [`Self::BEHIND_AFTER`]th pass in a row that runs out of time raises the
325- /// [`Alert::CleanupBehind`] alert, and one that deletes everything due clears
326- /// it.
327- async fn run (
309+ /// Drains one pass, until it finds nothing, runs out of time or shutdown is
310+ /// requested. Returns whether it ran out of time, or `None` when shutdown
311+ /// stopped it.
312+ async fn drain (
328313 & self ,
329314 connection : & mut ExclusiveConnection ,
330315 shutdown : & Shutdown ,
331- ) -> Result < ( ) , DatabaseError > {
316+ ) -> Result < Option < bool > , DatabaseError > {
332317 let started = Instant :: now ( ) ;
333318 let deadline = started + self . budget ;
334- // Whether the pass fell behind, unless shutdown stopped it.
335319 let mut behind = Some ( false ) ;
336320 let mut changed = Self :: batch ( connection) . await ?;
337321 while changed > 0 {
@@ -353,24 +337,80 @@ impl ExclusiveJob for Cleanup {
353337 changed = Self :: batch ( connection) . await ?;
354338 }
355339 CLEANUP_DURATION . record ( started. elapsed ( ) . as_secs_f64 ( ) , & ( ) ) ;
356- let Some ( behind) = behind else {
357- return Ok ( ( ) ) ;
340+ Ok ( behind)
341+ }
342+
343+ /// Changes the alerts after a pass that ended with `drained`, and returns
344+ /// how it ended. The [`Self::FAILING_AFTER`]th pass in a row that fails
345+ /// raises [`Alert::CleanupFailing`], and one that works clears it. The
346+ /// [`Self::BEHIND_AFTER`]th in a row that runs out of time raises
347+ /// [`Alert::CleanupBehind`], and one that deletes everything due clears it. A
348+ /// pass that shutdown stopped changes neither.
349+ async fn ended (
350+ & self ,
351+ connection : & mut ExclusiveConnection ,
352+ drained : Result < Option < bool > , DatabaseError > ,
353+ ) -> Result < ( ) , DatabaseError > {
354+ let behind = match drained {
355+ Ok ( Some ( behind) ) => behind,
356+ Ok ( None ) => return Ok ( ( ) ) ,
357+ Err ( error) => {
358+ let alert = Alert :: CleanupFailing { } ;
359+ self . failures
360+ . failed ( connection, JobName :: Cleanup , alert)
361+ . await ;
362+ return Err ( error) ;
363+ }
358364 } ;
359- if !behind {
365+ self . failures . reset ( ) ;
366+ let fell_behind = if behind {
367+ self . overruns . extend ( )
368+ } else {
360369 self . overruns . reset ( ) ;
361- } else if !self . overruns . extend ( ) {
362- return Ok ( ( ) ) ;
363- }
370+ false
371+ } ;
364372 let mut tx = connection. begin ( ) . await ?;
365373 let context = EventContext :: system ( JobName :: Cleanup ) ;
366374 let mut alerts = Alerts :: new ( & mut tx, & context) ;
367- if behind {
375+ alerts. clear ( AlertKind :: CleanupFailing ) . await ?;
376+ if fell_behind {
368377 alerts. raise ( Alert :: CleanupBehind { } ) . await ?;
369- } else {
378+ } else if !behind {
370379 alerts. clear ( AlertKind :: CleanupBehind ) . await ?;
371380 }
372381 tx. commit ( ) . await
373382 }
383+
384+ /// Runs one batch and returns how many rows it changed. Abandoning stuck keys
385+ /// needs a transaction of its own, so its locks last until it decides.
386+ async fn batch ( connection : & mut ExclusiveConnection ) -> Result < u64 , DatabaseError > {
387+ let mut tx = connection. begin ( ) . await ?;
388+ let abandoned = tx. abandon_stuck_deliveries ( Self :: BATCH_SIZE ) . await ?;
389+ tx. commit ( ) . await ?;
390+ let purged = connection
391+ . maintenance ( )
392+ . purge_expired_auth_state ( Self :: BATCH_SIZE )
393+ . await ?;
394+ Ok ( abandoned + purged)
395+ }
396+ }
397+
398+ impl ExclusiveJob for Cleanup {
399+ const NAME : & ' static str = "cleanup" ;
400+ const LOCK : LockKey = LockKey :: Cleanup ;
401+ const EVERY : Duration = Duration :: from_mins ( 5 ) ;
402+ type Error = DatabaseError ;
403+
404+ /// Stops after the current batch once shutdown is requested. Raises and
405+ /// clears its alerts as [`Self::ended`] says.
406+ async fn run (
407+ & self ,
408+ connection : & mut ExclusiveConnection ,
409+ shutdown : & Shutdown ,
410+ ) -> Result < ( ) , DatabaseError > {
411+ let drained = self . drain ( connection, shutdown) . await ;
412+ self . ended ( connection, drained) . await
413+ }
374414}
375415
376416/// Runs in a row on this replica that ended one way, such as cleanup passes that
0 commit comments