feat(wiki): dedicated worker pool with concurrent claim-based batching

Isolate the wiki generation pipeline from the document parse pipeline so
the two can no longer starve each other during concurrent uploads.

- Dedicated asynq worker pool for QueueWiki (NewWikiAsynqServer) with its
  own concurrency budget (WEKNORA_WIKI_ASYNQ_CONCURRENCY / asynq.wiki_
  concurrency, default 16), split from the parse pool (NewParseAsynqServer).
  Both run the same mux over disjoint queue sets. Replaces the old
  weighted-lottery approach where wiki sat on the low queue (weight 1).
- Task queue: add ClaimBatch (SELECT ... FOR UPDATE SKIP LOCKED + claimed_at)
  and ReleaseByIDs so multiple concurrent consumers pull disjoint rows
  without an external per-KB lock; PeekBatch retained for Lite mode.
- New TypeWikiFinalize debounced KB-global finalize task (index rebuild /
  dead-link cleanup / cross-linking), registered on both Redis and Lite paths.
- Two-level wiki ingest concurrency: IngestMaxInflight caps concurrent
  batches per KB; Map/Reduce fan-out sized within a batch. New config knob
  + system setting + swagger docs.
This commit is contained in:
wizardchen
2026-07-09 13:27:47 +08:00
committed by lyingbug
parent 7642117b70
commit 70e2e85fa6
17 changed files with 1282 additions and 377 deletions
+84 -26
View File
@@ -24,7 +24,15 @@ import (
type AsynqTaskParams struct {
dig.In
Server *asynq.Server
// ParseServer drains every queue EXCEPT QueueWiki: the user-facing
// document parsing / post-processing / enrichment pipeline. WikiServer
// drains QueueWiki only. Two independent worker pools give hard capacity
// isolation between the upstream parse pipeline and the downstream wiki
// pipeline (see NewParseAsynqServer / NewWikiAsynqServer). The same mux is
// run on both; asynq routes by the queue each server subscribes to, and
// queue↔task-type is 1:1 by convention, so no task is processed twice.
ParseServer *asynq.Server `name:"parseAsynqServer"`
WikiServer *asynq.Server `name:"wikiAsynqServer"`
KnowledgeService interfaces.KnowledgeService
KnowledgeBaseService interfaces.KnowledgeBaseService
TagService interfaces.KnowledgeTagService
@@ -122,8 +130,36 @@ func asynqRetryDelayFunc(n int, e error, t *asynq.Task) time.Duration {
// not on local CPU).
const defaultAsynqConcurrency = 32
func NewAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
// defaultWikiAsynqConcurrency is the worker pool size for the dedicated wiki
// pool (QueueWiki) when WEKNORA_WIKI_ASYNQ_CONCURRENCY is unset. Kept
// independent from the parse pool so the two can be sized separately for the
// host / LLM-provider budget. The wiki pipeline is LLM-bound (synthesis model
// calls dominate), so a moderate default avoids overrunning upstream provider
// concurrency while still letting many documents' wiki generation run in
// parallel across knowledge bases.
const defaultWikiAsynqConcurrency = 16
// newAsynqServer builds an asynq server bound to a specific queue set and
// concurrency. Shared by the parse and wiki pools so both inherit the same
// Redis options and retry-delay policy.
func newAsynqServer(concurrency int, queues map[string]int) *asynq.Server {
opt := getAsynqRedisClientOpt()
return asynq.NewServer(
opt,
asynq.Config{
Concurrency: concurrency,
Queues: queues,
RetryDelayFunc: asynqRetryDelayFunc,
},
)
}
// NewParseAsynqServer builds the upstream pool: every queue EXCEPT QueueWiki.
// This is the user-facing document parse / post-process / enrichment pipeline.
// Concurrency comes from WEKNORA_ASYNQ_CONCURRENCY (default 32), unchanged from
// the historical single-pool behaviour so existing deployments see the same
// parse throughput.
func NewParseAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
concurrency := defaultAsynqConcurrency
if svc != nil {
n := svc.GetInt(context.Background(), "asynq.concurrency", "WEKNORA_ASYNQ_CONCURRENCY", defaultAsynqConcurrency)
@@ -131,24 +167,35 @@ func NewAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
concurrency = int(n)
}
}
log.Printf("asynq server starting with concurrency=%d redis_op_timeout=%dms",
log.Printf("asynq parse-pool server starting with concurrency=%d redis_op_timeout=%dms",
concurrency, readRedisOpTimeoutMs())
srv := asynq.NewServer(
opt,
asynq.Config{
Concurrency: concurrency,
Queues: map[string]int{
types.QueueCritical: 6, // Highest priority queue
types.QueueDefault: 3, // Default priority queue
types.QueueLow: 1, // Lowest priority queue
types.QueueMultimodal: 1, // Isolated lane for high-volume slow VLM image tasks
types.QueueGraph: 1, // Isolated lane for high-volume slow graph-extraction tasks
types.QueueQuestion: 1, // Isolated lane for high-volume slow question-generation tasks
},
RetryDelayFunc: asynqRetryDelayFunc,
},
)
return srv
return newAsynqServer(concurrency, map[string]int{
types.QueueCritical: 6, // Highest priority queue
types.QueueDefault: 3, // Default priority queue
types.QueueLow: 1, // Lowest priority queue
types.QueueMultimodal: 1, // Isolated lane for high-volume slow VLM image tasks
types.QueueGraph: 1, // Isolated lane for high-volume slow graph-extraction tasks
types.QueueQuestion: 1, // Isolated lane for high-volume slow question-generation tasks
})
}
// NewWikiAsynqServer builds the dedicated wiki pool: QueueWiki only. Runs the
// same mux as the parse pool but only ever pulls wiki tasks, so its concurrency
// budget (WEKNORA_WIKI_ASYNQ_CONCURRENCY, default 16) is spent exclusively on
// wiki generation. This is the hard capacity isolation that prevents the parse
// pipeline from starving wiki (and vice-versa) during concurrent uploads.
func NewWikiAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
concurrency := defaultWikiAsynqConcurrency
if svc != nil {
n := svc.GetInt(context.Background(), "asynq.wiki_concurrency", "WEKNORA_WIKI_ASYNQ_CONCURRENCY", defaultWikiAsynqConcurrency)
if n > 0 {
concurrency = int(n)
}
}
log.Printf("asynq wiki-pool server starting with concurrency=%d", concurrency)
return newAsynqServer(concurrency, map[string]int{
types.QueueWiki: 1,
})
}
func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
@@ -225,15 +272,26 @@ func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
// Register data source sync handler
mux.HandleFunc(types.TypeDataSourceSync, params.DataSourceService.ProcessSync)
// Register wiki ingest handler
// Register wiki ingest handler + the debounced KB-global finalize handler.
// Both route to the same dispatch (WikiIngest.Handle switches on task type)
// and both land on QueueWiki, so the dedicated wiki pool serves them.
mux.HandleFunc(types.TypeWikiIngest, params.WikiIngest.Handle)
mux.HandleFunc(types.TypeWikiFinalize, params.WikiIngest.Handle)
go func() {
// Start the server
if err := params.Server.Run(mux); err != nil {
log.Fatalf("could not run server: %v", err)
}
}()
// Run the same mux on both pools. Each server subscribes to a disjoint set
// of queues (parse pool = everything except QueueWiki; wiki pool =
// QueueWiki only), so a given task is dequeued by exactly one pool. This is
// how the two pools get independent concurrency budgets while sharing one
// handler registration.
runPool := func(name string, srv *asynq.Server) {
go func() {
if err := srv.Run(mux); err != nil {
log.Fatalf("could not run %s asynq server: %v", name, err)
}
}()
}
runPool("parse-pool", params.ParseServer)
runPool("wiki-pool", params.WikiServer)
return mux
}