diff --git a/internal/app/consumer/warmup_mail_retention.go b/internal/app/consumer/warmup_mail_retention.go index bdfad36fd..88493163b 100644 --- a/internal/app/consumer/warmup_mail_retention.go +++ b/internal/app/consumer/warmup_mail_retention.go @@ -5,6 +5,7 @@ import ( "errors" "time" + "github.com/google/uuid" "github.com/rs/zerolog/log" "github.com/warmbly/warmbly/internal/config" @@ -34,7 +35,9 @@ import ( // retention.warmup_event_days once a full pass has completed. const ( - warmupRetentionBatch = 100 + warmupRetentionBatch = 500 + // warmupRetentionRunBudget is how long one run keeps taking batches, so a backlog clears in hours, not weeks. + warmupRetentionRunBudget = 40 * time.Second // warmupRetentionPassEvery is how often a completed pass is repeated. The // windows are whole days, so nothing is gained by walking the tables // more often than this. @@ -52,11 +55,20 @@ func (s *JobsService) StartWarmupMailRetention(ctx context.Context) { if time.Now().Before(nextPass) { return nil } - batchCtx, cancel := context.WithTimeout(ctx, 45*time.Second) + batchCtx, cancel := context.WithTimeout(ctx, 55*time.Second) defer cancel() - done, err := s.retireWarmupMailBatch(batchCtx) - if err != nil || !done { - return err + started := time.Now() + for { + done, err := s.retireWarmupMailBatch(batchCtx) + if err != nil { + return err + } + if done { + break + } + if time.Since(started) > warmupRetentionRunBudget { + return nil + } } if err := s.pruneWarmupEvents(batchCtx); err != nil { return err @@ -83,8 +95,7 @@ func (s *JobsService) warmupRetentionDays(ctx context.Context) (mail, events int } // retireWarmupMailBatch offers one batch of received copies and one of sent -// copies to the workers. It reports done when both listings came back short, -// which means nothing older is waiting. +// copies to the workers. It reports done when both listings came back drained. func (s *JobsService) retireWarmupMailBatch(ctx context.Context) (bool, error) { days, _ := s.warmupRetentionDays(ctx) @@ -124,7 +135,21 @@ func (s *JobsService) retireWarmupMailBatch(ctx context.Context) (bool, error) { // the pass is not complete until every row of the batch went. return false, errors.Join(failures...) } - return len(received) < warmupRetentionBatch && len(sent) < warmupRetentionBatch, nil + return drained(received) && drained(sent), nil +} + +// drained is a listing that came back short with no mailbox at its per-mailbox share, so nothing older is waiting. +func drained(batch []repository.WarmupMailToRetire) bool { + if len(batch) >= warmupRetentionBatch { + return false + } + per := map[uuid.UUID]int{} + for _, m := range batch { + if per[m.EmailAccountID]++; per[m.EmailAccountID] >= repository.WarmupRetirePerMailbox { + return false + } + } + return true } // publishWarmupDelete sends the delete for one message to the worker holding diff --git a/internal/app/consumer/warmup_retention_test.go b/internal/app/consumer/warmup_retention_test.go index 85fdf66ff..c3eacb55e 100644 --- a/internal/app/consumer/warmup_retention_test.go +++ b/internal/app/consumer/warmup_retention_test.go @@ -412,3 +412,18 @@ func TestPruneWarmupEventsUsesTheInstanceWindow(t *testing.T) { t.Fatalf("pruned before %v, want about %v", repo.pruned, want) } } + +// A mailbox at its per-mailbox share may hold more, so the pass is not done even when the batch is short. +func TestRetirementPassWaitsForAMailboxAtItsShare(t *testing.T) { + busy := uuid.New() + var batch []repository.WarmupMailToRetire + for range repository.WarmupRetirePerMailbox { + batch = append(batch, repository.WarmupMailToRetire{EmailAccountID: busy}) + } + if drained(batch) { + t.Fatal("a mailbox that filled its share was treated as caught up") + } + if !drained(batch[:repository.WarmupRetirePerMailbox-1]) { + t.Fatal("a short batch below every share must complete the pass") + } +} diff --git a/internal/repository/pg_warmup.go b/internal/repository/pg_warmup.go index 7b879b3bb..34f924c79 100644 --- a/internal/repository/pg_warmup.go +++ b/internal/repository/pg_warmup.go @@ -1986,6 +1986,9 @@ func (r *warmupRepository) GetWarmupReceived(ctx context.Context, accountID, int return &w, nil } +// WarmupRetirePerMailbox bounds one mailbox's share of a retirement batch, so a long backlog cannot queue a whole batch on one worker. +const WarmupRetirePerMailbox = 50 + // ListWarmupMailToRetire lists received warmup copies past their window: // the mailbox's own when it set one, else the instance default handed in as // $1, on active mailboxes that have a worker to act. The provider key comes @@ -2002,7 +2005,7 @@ func (r *warmupRepository) ListWarmupMailToRetire(ctx context.Context, defaultDa WHERE r.email_account_id = ea.id AND r.retired_at IS NULL AND r.created_at < NOW() - make_interval(days => COALESCE(ea.warmup_retention_days, $1)) ORDER BY r.created_at - LIMIT $2 + LIMIT LEAST($2, ` + strconv.Itoa(WarmupRetirePerMailbox) + `) ) wr LEFT JOIN LATERAL ( SELECT em.message_id FROM email_message_map em @@ -2033,7 +2036,7 @@ func (r *warmupRepository) ListWarmupSentCopiesToRetire(ctx context.Context, def WHERE t.sender_account_id = ea.id AND t.sent_message_id <> '' AND t.sent_retired_at IS NULL AND t.created_at < NOW() - make_interval(days => COALESCE(ea.warmup_retention_days, $1)) ORDER BY t.created_at - LIMIT $2 + LIMIT LEAST($2, ` + strconv.Itoa(WarmupRetirePerMailbox) + `) ) wt LEFT JOIN LATERAL ( SELECT em.message_id FROM email_message_map em