perf(wiki-ingest): scale to 4w-doc KBs + generic task queue / dead-letter
The wiki ingest post-process pipeline OOM'd and ran for hours on KBs
with ~40k documents. The dominant tail was a per-batch ListAllPages
that pulled every page (multi-MB content blobs) into Go memory, plus a
24h-TTL Redis pending list whose data could be evicted before a long
serialized backlog drained. dedup did O(P × N) Jaccard scoring in Go
on every batch. Lint loaded the full graph. The index page kept the
entire wiki directory in its content column and rewrote the TOAST
chunk on every ingest. None of these survive at 4w docs.
This change reworks the write path end to end and pulls the durable-
queue + dead-letter primitives out of wiki and into shared
infrastructure that every asynq task type now benefits from.
- migration 000041: task_pending_ops + task_dead_letters tables, plus
three GIN indexes on wiki_pages (source_refs jsonb_path_ops,
source_refs text fallback, lower(title) trgm).
- TaskPendingOpsRepository + TaskDeadLetterRepository in
internal/application/repository/task_queue.go: cursor-paginated
list, atomic IncrFailCount via UPDATE…RETURNING, dedup-key scoped
delete that refuses empty keys so a buggy caller can't wipe a KB's
queue.
- internal/middleware/asynqdl: writes a task_dead_letters row when an
asynq task exhausts its retry budget. Payload-agnostic — a small
probe struct extracts TenantID + scope hints across every existing
payload type, so summary:generation / image:multimodal /
faq_import / kb:clone / etc. all dead-letter without per-handler
code. Best-effort: an insert failure never masks the underlying
task error. Installed in router/task.go before the langfuse mw so
it sees raw errors.
- Pending queue moved Redis → PG (no TTL, restart-durable). Redis
keeps just the active-batch lock and the delete tombstone.
- In-batch retry budget (wikiMaxFailRetries=5) tracked via
pendingRepo.IncrFailCount; over the cap the row is moved to
task_dead_letters with task_type=wiki:ingest, related_id=knowledge
id. Asynq retries (10) are handled by the new middleware.
- Removed the per-batch ListAllPages. WikiBatchContext now carries
lazy fetcher closures (SlugTitleMany, SummaryByKnowledgeID) with
mutex-protected caches; reduce reaches for titles / summaries on
demand instead of pre-loading the whole KB.
- getExistingPageSlugsForKnowledge now uses ListSlugsBySourceRef,
which the new GIN index on source_refs serves as a Bitmap Index
Scan instead of a sequential text LIKE.
- Dedup pre-filter uses pg_trgm via FindSimilarPages
(idx_wiki_pages_title_trgm). For each new entity/concept (and each
of its aliases) we ask the DB for the top-K trigram-similar
existing pages and union the results — bounded prompt size, no Go-
side O(P × M) loop. Small KBs (≤25 entities) bypass the pre-filter.
- cleanDeadLinks / injectCrossLinks are scoped to the batch's
affected slugs (a few dozen) instead of every page in the KB. Both
use the new lite ListBySlugs / ExistsSlugs repo methods so they
pull only slug + title + outlinks, not full content.
- Slug fuzzy resolve (slug_fuzzy.go): when the LLM emits
[[bad-slug|display]] the cleanup path now tries display-text
reverse lookup → hyphen/case normalized equality → char-bigram
Jaccard ≥ 0.8 before stripping. Recovers the common pinyin-word-
break drift case ("shang-hai-tower" vs "shanghai-tower") in place
instead of replacing the link with plain text.
- rebuildIndexPage uses ListByTypeRecent(200) for the first-time
intro and drops the full DocumentSummaries blob from the
incremental update prompt, so its context stays bounded regardless
of KB size.
- Concurrency tunables surfaced in WikiConfig: IngestBatchSize /
IngestMapParallel / IngestReduceParallel, with sensible defaults
via OrDefault helpers. scheduleFollowUp drops to ProcessIn(0) so
follow-ups don't waste asynq retry slots bouncing on the active
lock.
- RunLint walks pages via the new ListPagesCursor in 200-page
windows and computes the live-slug set with a one-column
ListAllSlugs Pluck instead of a Limit:0 GetGraph that materialized
every node + edge. Memory is now bounded; 40k pages walks in
constant ~4MB.
- 14 GORM tests for both repos against an in-memory SQLite mirror of
the production DDL (task_queue_test.go).
- 6 tests for the dead-letter middleware covering retry budget
detection, payload-agnostic scope inference, error truncation, and
repo-failure isolation.
- 7 tests for the slug fuzzy resolve helper covering the three
resolution stages, display-text priority over normalized equality,
bigram fallback acceptance, and rejection of unrelated slugs.
- Existing wiki_ingest / wiki_lint / wiki_page tests updated to the
new fetcher / cursor APIs.
This commit is contained in:
@@ -282,7 +282,7 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kno
|
||||
// so the knowledge's disappearance is reflected in the UI.
|
||||
lang, _ := types.LanguageFromContext(ctx)
|
||||
tenantID, _ := types.TenantIDFromContext(ctx)
|
||||
EnqueueWikiRetract(ctx, s.task, s.redisClient, WikiRetractPayload{
|
||||
EnqueueWikiRetract(ctx, s.task, s.taskPendingRepo, WikiRetractPayload{
|
||||
TenantID: tenantID,
|
||||
KnowledgeBaseID: kbID,
|
||||
KnowledgeID: knowledgeID,
|
||||
@@ -309,59 +309,25 @@ func (s *knowledgeService) markKnowledgeDeletedForWiki(ctx context.Context, kbID
|
||||
}
|
||||
|
||||
// scrubWikiPendingIngest removes queued WikiOpIngest entries for a knowledge
|
||||
// from the debounced pending list. Used by both the delete path (we're about
|
||||
// to soft-delete the doc, no point ingesting it) and the reparse path (the
|
||||
// from task_pending_ops. Used by both the delete path (we're about to
|
||||
// soft-delete the doc, no point ingesting it) and the reparse path (the
|
||||
// old chunks are about to vanish, so any pending ingest would either race
|
||||
// with the cleanup or no-op on an empty chunk set — and the post-process
|
||||
// task will enqueue a fresh ingest once new chunks land anyway).
|
||||
//
|
||||
// Retract entries stay put — delete still needs them to unlink referencing
|
||||
// pages, and reparse never enqueues retracts for the doc being reparsed.
|
||||
//
|
||||
// We use LREM against JSON-encoded entries plus a best-effort raw-UUID
|
||||
// fallback for backward compatibility with the legacy format documented in
|
||||
// peekPendingList.
|
||||
// We pass op=WikiOpIngest so DeleteByDedupKey filters to the ingest rows
|
||||
// only.
|
||||
func (s *knowledgeService) scrubWikiPendingIngest(ctx context.Context, kbID, knowledgeID, reason string) {
|
||||
if s.redisClient == nil || kbID == "" || knowledgeID == "" {
|
||||
if s.taskPendingRepo == nil || kbID == "" || knowledgeID == "" {
|
||||
return
|
||||
}
|
||||
pendingKey := wikiPendingKeyPrefix + kbID
|
||||
|
||||
// Best-effort: inspect the list, remove matching ingest entries one by one.
|
||||
// The list is bounded (wikiMaxDocsPerBatch at a time on the consumer
|
||||
// side, practical uploads rarely exceed a few dozen), so a single LRange
|
||||
// is safe.
|
||||
items, err := s.redisClient.LRange(ctx, pendingKey, 0, -1).Result()
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "wiki %s: failed to read pending list %s: %v", reason, pendingKey, err)
|
||||
if err := s.taskPendingRepo.DeleteByDedupKey(ctx, wikiTaskType, wikiTaskScope, kbID, knowledgeID, WikiOpIngest); err != nil {
|
||||
logger.Warnf(ctx, "wiki %s: failed to scrub pending ingest ops for knowledge %s: %v", reason, knowledgeID, err)
|
||||
return
|
||||
}
|
||||
removed := 0
|
||||
for _, item := range items {
|
||||
// Legacy raw-UUID form
|
||||
if item == knowledgeID {
|
||||
if n, err := s.redisClient.LRem(ctx, pendingKey, 0, item).Result(); err == nil {
|
||||
removed += int(n)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(item, "{") {
|
||||
continue
|
||||
}
|
||||
var op WikiPendingOp
|
||||
if err := json.Unmarshal([]byte(item), &op); err != nil {
|
||||
continue
|
||||
}
|
||||
if op.KnowledgeID != knowledgeID || op.Op != WikiOpIngest {
|
||||
continue
|
||||
}
|
||||
if n, err := s.redisClient.LRem(ctx, pendingKey, 0, item).Result(); err == nil {
|
||||
removed += int(n)
|
||||
}
|
||||
}
|
||||
if removed > 0 {
|
||||
logger.Infof(ctx, "wiki %s: scrubbed %d pending ingest ops for knowledge %s", reason, removed, knowledgeID)
|
||||
}
|
||||
logger.Infof(ctx, "wiki %s: scrubbed pending ingest ops for knowledge %s", reason, knowledgeID)
|
||||
}
|
||||
|
||||
// prepareWikiForReparse is the reparse counterpart to
|
||||
|
||||
Reference in New Issue
Block a user