fix(wiki): prevent silent work loss and cross-batch doc races

Two correctness fixes on the Phase 3 concurrent claim-based ingest path:

- Slug-lock timeout / reduce failure no longer silently drops work. When a
  per-slug lock can't be acquired within wikiSlugLockWait, or reduce returns
  an error, the page's read-modify-write never lands. Previously the owning
  document was still counted as successful and its pending row was deleted at
  trim time, permanently losing the contribution (finalize only rebuilds the
  index / cross-links, it does not re-run reduce). Such documents are now
  folded into failedOps: their rows stay queued, their finalizing slot is
  held, and requeueFailedOps' fail_count budget retries them (dead-lettering
  a permanently hot/broken slug).

- ClaimBatch now claims by dedup_key affinity so a document is never split
  across concurrent batches. limit counts distinct dedup_keys (documents),
  and ALL eligible rows sharing a key are claimed together. On Postgres the
  per-key anchor row is locked with FOR UPDATE SKIP LOCKED, handing
  concurrent claimers disjoint key sets. This restores in-document ordering
  (e.g. ingest-then-retract collapses correctly) that the per-batch dedup
  could not guarantee once the exclusive per-KB lock was removed, preventing
  a stale ingest from resurrecting a retracted document.

Adds repository tests for same-key affinity and in-flight sibling rows.
This commit is contained in:
wizardchen
2026-07-09 15:25:05 +08:00
committed by lyingbug
parent 70e2e85fa6
commit c8a4fd5ba3
4 changed files with 208 additions and 29 deletions
+10 -7
View File
@@ -35,13 +35,16 @@ type TaskPendingOpsRepository interface {
// IncrFailCount and leave them for the next pass).
PeekBatch(ctx context.Context, taskType, scope, scopeID string, limit int) ([]*types.TaskPendingOp, error)
// ClaimBatch atomically claims up to `limit` rows for the tuple,
// ordered by id ASC (FIFO), and returns them with claimed_at set to
// now. A row is eligible when it is unclaimed (claimed_at IS NULL) or
// its claim is stale (claimed_at < staleBefore) — the latter recovers
// rows abandoned by a crashed worker. On Postgres the selection uses
// FOR UPDATE SKIP LOCKED so concurrent claimers never block on or
// double-claim each other's rows.
// ClaimBatch atomically claims eligible rows for the tuple, grouped by
// dedup_key, and returns them with claimed_at set to now. `limit`
// counts DISTINCT dedup_keys (documents), NOT rows: ALL eligible rows
// sharing a chosen dedup_key are claimed together so a document with
// several queued ops is never split across two concurrent batches.
// A row is eligible when it is unclaimed (claimed_at IS NULL) or its
// claim is stale (claimed_at < staleBefore) — the latter recovers rows
// abandoned by a crashed worker. On Postgres the per-key anchor row is
// locked with FOR UPDATE SKIP LOCKED so concurrent claimers take
// disjoint key sets without blocking or double-claiming.
//
// Claimed rows are NOT removed: the consumer must DeleteByIDs on
// success, or ReleaseByIDs to hand a still-retryable row back to the