fix(wiki): detach ingest cleanup from task cancellation
Use bounded detached contexts for wiki ingest tail cleanup so pending row settlement, span closure, finalize enqueueing, and follow-up scheduling can finish even after the asynq task context is cancelled. Return settlement errors from pending row trimming and failed-op requeueing, and only mark claims settled after rows are deleted, released, or dead-lettered successfully. Requeue documents whose reduce updates were not applied after context cancellation to avoid silently dropping wiki work. Add unit coverage for detached cleanup contexts and settlement error handling.
This commit is contained in:
@@ -200,6 +200,10 @@ const (
|
||||
// top of the asynq.TaskID coalescing).
|
||||
wikiFinalizeLockTTL = 60 * time.Second
|
||||
wikiFinalizeLockRenew = 20 * time.Second
|
||||
|
||||
// wikiIngestCleanupTimeout bounds detached tail cleanup after the asynq
|
||||
// task context has been cancelled or hit its timeout.
|
||||
wikiIngestCleanupTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
// wikiFinalizeChange is a doc-level add/remove entry for the index-intro
|
||||
@@ -537,6 +541,13 @@ func (s *wikiIngestService) Handle(ctx context.Context, t *asynq.Task) error {
|
||||
}
|
||||
}
|
||||
|
||||
func wikiIngestCleanupContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
return context.WithTimeout(context.WithoutCancel(ctx), wikiIngestCleanupTimeout)
|
||||
}
|
||||
|
||||
// enqueueFinalize persists this batch's KB-global convergence work into the
|
||||
// finalize lane of task_pending_ops and schedules a debounced trigger. One
|
||||
// "slug" row per affected page (carrying its fresh title when this batch wrote
|
||||
@@ -920,13 +931,15 @@ func (s *wikiIngestService) decodePendingRows(ctx context.Context, rows []*types
|
||||
// trimPendingList deletes consumed rows from task_pending_ops. Empty
|
||||
// input is a no-op so callers can invoke unconditionally at the end
|
||||
// of a batch.
|
||||
func (s *wikiIngestService) trimPendingList(ctx context.Context, ids []int64) {
|
||||
func (s *wikiIngestService) trimPendingList(ctx context.Context, ids []int64) error {
|
||||
if s.pendingRepo == nil || len(ids) == 0 {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if err := s.pendingRepo.DeleteByIDs(ctx, ids); err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to trim %d pending rows: %v", len(ids), err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// finalizeWikiSubtask releases this knowledge's slot in the finalizing
|
||||
@@ -960,13 +973,13 @@ func (s *wikiIngestService) finalizeWikiSubtask(ctx context.Context, knowledgeID
|
||||
// (rows are ordered by id ASC and we never moved/touched it).
|
||||
// - If the count exceeds the retry cap: archive the op into
|
||||
// task_dead_letters and DeleteByIDs to remove it from the queue.
|
||||
// Both writes are best-effort — a DB failure here is logged and
|
||||
// swallowed so a single transient blip doesn't recursively spawn
|
||||
// more failures.
|
||||
func (s *wikiIngestService) requeueFailedOps(ctx context.Context, payload WikiIngestPayload, ops []WikiPendingOp) {
|
||||
// Settlement failures are returned so the caller does not mark claims
|
||||
// settled while rows are still claimed or undeleted.
|
||||
func (s *wikiIngestService) requeueFailedOps(ctx context.Context, payload WikiIngestPayload, ops []WikiPendingOp) error {
|
||||
if s.pendingRepo == nil || len(ops) == 0 {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
var settleErrs []error
|
||||
for _, op := range ops {
|
||||
if op.dbID == 0 {
|
||||
// Op was never persisted (synthetic / test) — nothing to
|
||||
@@ -976,6 +989,7 @@ func (s *wikiIngestService) requeueFailedOps(ctx context.Context, payload WikiIn
|
||||
count, err := s.pendingRepo.IncrFailCount(ctx, op.dbID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to increment fail count for %s (id=%d): %v", op.KnowledgeID, op.dbID, err)
|
||||
settleErrs = append(settleErrs, fmt.Errorf("increment fail count id=%d: %w", op.dbID, err))
|
||||
// Without a fresh count we can't tell whether to drop. Be
|
||||
// conservative: leave the row in place; the next PeekBatch
|
||||
// will see it again and we'll try once more.
|
||||
@@ -989,6 +1003,7 @@ func (s *wikiIngestService) requeueFailedOps(ctx context.Context, payload WikiIn
|
||||
// budget still counts down.
|
||||
if err := s.pendingRepo.ReleaseByIDs(ctx, []int64{op.dbID}); err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to release claim for retry id=%d: %v", op.dbID, err)
|
||||
settleErrs = append(settleErrs, fmt.Errorf("release retry claim id=%d: %w", op.dbID, err))
|
||||
}
|
||||
logger.Infof(ctx, "wiki ingest: re-queued failed op %s (%s) for retry (attempt %d/%d)", op.KnowledgeID, op.DocTitle, count, wikiMaxFailRetries)
|
||||
continue
|
||||
@@ -1016,12 +1031,15 @@ func (s *wikiIngestService) requeueFailedOps(ctx context.Context, payload WikiIn
|
||||
FailCount: count,
|
||||
}); dlErr != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to archive op %s to dead letters: %v", op.KnowledgeID, dlErr)
|
||||
settleErrs = append(settleErrs, fmt.Errorf("archive dead letter id=%d: %w", op.dbID, dlErr))
|
||||
}
|
||||
}
|
||||
if err := s.pendingRepo.DeleteByIDs(ctx, []int64{op.dbID}); err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to drop dead-lettered row id=%d: %v", op.dbID, err)
|
||||
settleErrs = append(settleErrs, fmt.Errorf("drop dead-lettered row id=%d: %w", op.dbID, err))
|
||||
}
|
||||
}
|
||||
return errors.Join(settleErrs...)
|
||||
}
|
||||
|
||||
// docIngestResult captures per-document info for batch post-processing.
|
||||
|
||||
Reference in New Issue
Block a user