feat(worker): replace aggregate asynq concurrency with per-pool governance

Split the monolithic upstream worker budget into explicit core, post-process,
enrichment, maintenance, and shared elastic pools, expose live cluster capacity
on the runtime dashboard, and retire the legacy asynq.concurrency setting.
This commit is contained in:
wizardchen
2026-07-13 14:55:59 +08:00
committed by lyingbug
parent 5eefa70e6f
commit 07ba01ec93
27 changed files with 645 additions and 209 deletions
@@ -626,7 +626,7 @@ func (s *ImageMultimodalService) enqueueKnowledgePostProcessTask(ctx context.Con
}
task := asynq.NewTask(types.TypeKnowledgePostProcess, payloadBytes,
asynq.Queue(types.QueueDefault), asynq.MaxRetry(3), asynq.Timeout(30*time.Minute))
knowledgePostProcessTaskOptions()...)
if _, err := s.taskEnqueuer.Enqueue(task); err != nil {
logger.Warnf(ctx, "[ImageMultimodal] Failed to enqueue post process task for %s: %v", payload.KnowledgeID, err)
} else {
@@ -186,8 +186,9 @@ func (h *HousekeepingService) runSweep(ctx context.Context) {
if queueSkipped > 0 {
// Visibility into "stale span heartbeat but tasks still queued"
// — i.e. backpressure, not a stuck row. Persistent counts here
// mean the queue is the bottleneck (raise WEKNORA_ASYNQ_CONCURRENCY
// or document_process_timeout), not that housekeeping misfires.
// mean the queue is the bottleneck (raise the matching per-pool or
// shared asynq concurrency, or document_process_timeout), not that
// housekeeping misfires.
logger.Infof(ctx,
"[Housekeeping] %d candidate(s) skipped — tasks still queued (backpressure, not stuck)",
queueSkipped)
@@ -126,6 +126,12 @@ func (f fakeTaskInspector) QueueStats(
return nil, false, nil
}
func (f fakeTaskInspector) WorkerServerStats(
_ context.Context,
) ([]types.WorkerServerStat, bool, error) {
return nil, false, nil
}
func newHousekeepingSvcForTest(db *gorm.DB) *HousekeepingService {
return newHousekeepingSvcWithInspector(db, fakeTaskInspector{})
}
@@ -675,7 +675,7 @@ func (s *knowledgeService) processChunks(ctx context.Context,
payloadBytes, err := json.Marshal(postProcessPayload)
if err == nil {
task := asynq.NewTask(types.TypeKnowledgePostProcess, payloadBytes,
asynq.Queue(types.QueueDefault), asynq.MaxRetry(3), asynq.Timeout(30*time.Minute))
knowledgePostProcessTaskOptions()...)
if _, err := s.task.Enqueue(task); err != nil {
logger.Errorf(ctx, "Failed to enqueue knowledge post process task: %v", err)
} else {
@@ -1,6 +1,8 @@
package service
import (
"time"
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/types"
"github.com/hibiken/asynq"
@@ -15,3 +17,11 @@ func documentProcessTaskOptions(cfg *config.Config, extra ...asynq.Option) []asy
opts = append(opts, extra...)
return opts
}
func knowledgePostProcessTaskOptions() []asynq.Option {
return []asynq.Option{
asynq.Queue(types.QueuePostProcess),
asynq.MaxRetry(3),
asynq.Timeout(30 * time.Minute),
}
}
@@ -79,3 +79,12 @@ func TestDocumentProcessTaskOptions_extraMaxRetry(t *testing.T) {
require.NotNil(t, maxRetry)
assert.Equal(t, 3, *maxRetry)
}
func TestKnowledgePostProcessTaskOptionsUseDedicatedQueue(t *testing.T) {
t.Parallel()
queue, timeout, maxRetry := parseDocumentProcessOpts(t, knowledgePostProcessTaskOptions())
assert.Equal(t, types.QueuePostProcess, queue)
assert.Equal(t, 30*time.Minute, timeout)
require.NotNil(t, maxRetry)
assert.Equal(t, 3, *maxRetry)
}
+47 -19
View File
@@ -183,24 +183,50 @@ var registry = map[string]settingSpec{
"用于兼容旧版本「创建租户即下发默认 API Key」的行为(属于破坏性变更的回退开关)。" +
"每次创建租户时实时读取,修改后立即生效。默认 false(不自动创建,需通过 API Key 管理显式创建)。",
},
// asynq.concurrency is the total per-process upstream worker budget.
// It is divided deterministically across independent core, enrichment,
// and maintenance servers. Read once at startup; changing it requires a
// process restart. Mirrors WEKNORA_ASYNQ_CONCURRENCY (default 32).
"asynq.concurrency": {
"asynq.core_concurrency": {
Type: "int",
EnvName: "WEKNORA_ASYNQ_CONCURRENCY",
Default: int64(types.DefaultUpstreamWorkerConcurrency),
EnvName: "WEKNORA_ASYNQ_CORE_CONCURRENCY",
Default: int64(types.DefaultCoreWorkerConcurrency),
Category: "worker",
RequiresRestart: true,
Description: "上游任务的每实例 worker 总并发预算(不含 Wiki)。系统会按核心解析 1/2、" +
"内容富化 3/8、维护与同步为剩余容量,拆分为相互隔离的 worker 池。" +
"最小值为 3;修改后需重启服务进程方可生效。",
Description: "文档解析、手工重解析等核心任务的每实例保底并发。可额外使用共享弹性池;修改后需重启。",
},
"asynq.postprocess_concurrency": {
Type: "int",
EnvName: "WEKNORA_ASYNQ_POSTPROCESS_CONCURRENCY",
Default: int64(types.DefaultPostProcessWorkerConcurrency),
Category: "worker",
RequiresRestart: true,
Description: "解析完成后的轻量编排与富化扇出专用并发,避免被长时间文档解析阻塞;修改后需重启。",
},
"asynq.enrichment_concurrency": {
Type: "int",
EnvName: "WEKNORA_ASYNQ_ENRICHMENT_CONCURRENCY",
Default: int64(types.DefaultEnrichmentWorkerConcurrency),
Category: "worker",
RequiresRestart: true,
Description: "摘要、图片、图谱和问题生成的每实例保底并发。可额外使用共享弹性池;修改后需重启。",
},
"asynq.maintenance_concurrency": {
Type: "int",
EnvName: "WEKNORA_ASYNQ_MAINTENANCE_CONCURRENCY",
Default: int64(types.DefaultMaintenanceWorkerConcurrency),
Category: "worker",
RequiresRestart: true,
Description: "数据源同步、批处理、移动和删除清理的每实例保底并发,与用户面流水线硬隔离;修改后需重启。",
},
"asynq.shared_concurrency": {
Type: "int",
EnvName: "WEKNORA_ASYNQ_SHARED_CONCURRENCY",
Default: int64(types.DefaultSharedWorkerConcurrency),
Category: "worker",
RequiresRestart: true,
Description: "核心解析与内容富化共用的每实例弹性并发。空闲容量由有积压的一侧借用;修改后需重启。",
},
// asynq.wiki_concurrency is the size of the DEDICATED wiki worker pool,
// separate from asynq.concurrency. Read once when the wiki asynq server
// separate from the upstream pools. Read once when the wiki asynq server
// starts — changing it in the UI requires a process restart. Mirrors
// WEKNORA_WIKI_ASYNQ_CONCURRENCY (default 16).
// WEKNORA_WIKI_ASYNQ_CONCURRENCY (default 8).
"asynq.wiki_concurrency": {
Type: "int",
EnvName: "WEKNORA_WIKI_ASYNQ_CONCURRENCY",
@@ -669,6 +695,10 @@ func (s *systemSettingService) List(ctx context.Context) ([]*types.SystemSetting
for _, row := range rows {
byKey[row.Key] = row
}
// asynq.concurrency was the old fixed-ratio aggregate. Keeping an old DB
// row visible would suggest it still controls runtime capacity, so retire it
// explicitly while preserving all genuinely unknown rows for diagnostics.
delete(byKey, "asynq.concurrency")
keys := make([]string, 0, len(registry))
for key := range registry {
@@ -1228,17 +1258,15 @@ func encodeForType(declared string, rawValue any) (types.JSON, error) {
// 400 body verbatim).
func validateRegistryEntry(key string, rawValue any) error {
switch key {
case "asynq.concurrency", "asynq.wiki_concurrency":
case "asynq.core_concurrency", "asynq.postprocess_concurrency",
"asynq.enrichment_concurrency", "asynq.maintenance_concurrency",
"asynq.shared_concurrency", "asynq.wiki_concurrency":
n, err := coerceToPositiveInt64(rawValue)
if err != nil {
return err
}
minimum := int64(1)
if key == "asynq.concurrency" {
minimum = 3
}
if n < minimum {
return fmt.Errorf("concurrency must be at least %d", minimum)
if n < 1 {
return errors.New("concurrency must be at least 1")
}
case "ssrf.whitelist":
// Coerce into the same shape encodeForType produced. We don't
@@ -9,9 +9,12 @@ func TestValidateWorkerConcurrencyMinimums(t *testing.T) {
value any
wantErr bool
}{
{name: "upstream below one worker per pool", key: "asynq.concurrency", value: 2, wantErr: true},
{name: "upstream minimum", key: "asynq.concurrency", value: 3},
{name: "upstream normal", key: "asynq.concurrency", value: 32},
{name: "core zero", key: "asynq.core_concurrency", value: 0, wantErr: true},
{name: "core minimum", key: "asynq.core_concurrency", value: 1},
{name: "postprocess minimum", key: "asynq.postprocess_concurrency", value: 1},
{name: "enrichment minimum", key: "asynq.enrichment_concurrency", value: 1},
{name: "maintenance minimum", key: "asynq.maintenance_concurrency", value: 1},
{name: "shared minimum", key: "asynq.shared_concurrency", value: 1},
{name: "wiki zero", key: "asynq.wiki_concurrency", value: 0, wantErr: true},
{name: "wiki minimum", key: "asynq.wiki_concurrency", value: 1},
}
+1 -1
View File
@@ -79,7 +79,7 @@ const (
// wikiInflightPrefix keys the per-KB slot set: wiki:inflight:{kbID}.
wikiInflightPrefix = "wiki:inflight:"
// wikiInflightDefault is the fallback cap when WikiConfig.IngestMaxInflight
// is unset. 4 leaves headroom in a default 16-worker wiki pool for other
// is unset. 4 leaves half of the default 8-worker wiki pool for other
// KBs while still giving one KB solid parallelism.
wikiInflightDefault = 4
// wikiInflightTTL is how long a reserved slot survives without renewal.
+5 -4
View File
@@ -264,13 +264,14 @@ func BuildContainer(container *dig.Container) *dig.Container {
redisAvailable := os.Getenv("REDIS_ADDR") != ""
if redisAvailable {
must(container.Provide(router.NewAsyncqClient, dig.As(new(interfaces.TaskEnqueuer))))
// Independent worker pools keep user-facing parsing, high-fanout
// enrichment, maintenance/sync, and Wiki generation from consuming one
// another's capacity. All pools share one handler mux but subscribe to
// disjoint queues from the central topology registry.
// Dedicated pools guarantee capacity for each stage. The shared pool
// additionally subscribes to core/enrichment queues to provide elastic
// borrowing while post-process and maintenance remain hard-isolated.
must(container.Provide(router.NewCoreAsynqServer, dig.Name("coreAsynqServer")))
must(container.Provide(router.NewPostProcessAsynqServer, dig.Name("postProcessAsynqServer")))
must(container.Provide(router.NewEnrichmentAsynqServer, dig.Name("enrichmentAsynqServer")))
must(container.Provide(router.NewMaintenanceAsynqServer, dig.Name("maintenanceAsynqServer")))
must(container.Provide(router.NewSharedAsynqServer, dig.Name("sharedAsynqServer")))
must(container.Provide(router.NewWikiAsynqServer, dig.Name("wikiAsynqServer")))
// Asynq inspector for cancel-by-knowledge-id (best-effort
// dequeue of pending/scheduled/retry tasks + active-task cancel).
+78 -23
View File
@@ -1385,13 +1385,16 @@ func (h *SystemHandler) ListSystemAdmins(c *gin.Context) {
// RuntimeQueuesResponse is the payload for the SystemAdmin runtime queue
// dashboard. `available` is false in Lite mode (no Redis/asynq) so the
// UI can render an "unavailable in this deployment" state instead of an
// empty table. Pool concurrency is the configured per-process capacity
// (resolved DB > ENV > default), not live busy-worker count or cluster-wide
// capacity (see QueueStat.Active for currently running tasks).
// empty table. Each pool reports configured per-process concurrency plus live
// cluster capacity/active workers aggregated from asynq server heartbeats.
type RuntimeWorkerPool struct {
Name string `json:"name"`
Concurrency int `json:"concurrency"`
QueueCount int `json:"queue_count"`
Name string `json:"name"`
Concurrency int `json:"concurrency"` // configured per instance
QueueCount int `json:"queue_count"`
Instances int `json:"instances"` // live asynq server heartbeats
ClusterCapacity int `json:"cluster_capacity"` // sum of live server concurrency
Active int `json:"active"` // live workers assigned to this pool
Utilization float64 `json:"utilization"` // Active / ClusterCapacity
}
type RuntimeQueuesResponse struct {
@@ -1406,6 +1409,56 @@ type RuntimeQueuesResponse struct {
Timestamp int64 `json:"timestamp"`
}
func aggregateRuntimeWorkerPools(pools []RuntimeWorkerPool, servers []types.WorkerServerStat) {
queueConfigs := map[string]map[string]int{
types.WorkerPoolCore: types.QueueWeightsForPool(types.WorkerPoolCore),
types.WorkerPoolPostProcess: types.QueueWeightsForPool(types.WorkerPoolPostProcess),
types.WorkerPoolEnrichment: types.QueueWeightsForPool(types.WorkerPoolEnrichment),
types.WorkerPoolMaintenance: types.QueueWeightsForPool(types.WorkerPoolMaintenance),
types.WorkerPoolShared: types.QueueWeightsForSharedPool(),
types.WorkerPoolWiki: types.QueueWeightsForPool(types.WorkerPoolWiki),
}
indexes := make(map[string]int, len(pools))
for i := range pools {
indexes[pools[i].Name] = i
}
for _, server := range servers {
if server.Status != "active" {
continue
}
for poolName, expectedQueues := range queueConfigs {
if !sameQueueWeights(server.Queues, expectedQueues) {
continue
}
idx, ok := indexes[poolName]
if !ok {
break
}
pools[idx].Instances++
pools[idx].ClusterCapacity += server.Concurrency
pools[idx].Active += server.Active
break
}
}
for i := range pools {
if pools[i].ClusterCapacity > 0 {
pools[i].Utilization = float64(pools[i].Active) / float64(pools[i].ClusterCapacity)
}
}
}
func sameQueueWeights(left, right map[string]int) bool {
if len(left) != len(right) {
return false
}
for name, weight := range left {
if right[name] != weight {
return false
}
}
return true
}
// GetRuntimeQueues godoc
// @Summary 获取解析任务队列运行时状态
// @Description 返回各 asynq 队列的实时深度(pending/active/scheduled/retry 等)与 worker 并发配置,仅系统管理员可见
@@ -1415,34 +1468,30 @@ type RuntimeQueuesResponse struct {
// @Router /system/admin/runtime/queues [get]
func (h *SystemHandler) GetRuntimeQueues(c *gin.Context) {
ctx := logger.CloneContext(c.Request.Context())
upstreamConcurrency := types.DefaultUpstreamWorkerConcurrency
wikiConcurrency := types.DefaultWikiWorkerConcurrency
var allocation types.WorkerPoolConcurrency
if h.systemSettingSvc != nil {
if configured := h.systemSettingSvc.GetInt(
ctx, "asynq.concurrency", "WEKNORA_ASYNQ_CONCURRENCY", types.DefaultUpstreamWorkerConcurrency,
); configured > 0 {
upstreamConcurrency = int(configured)
}
if configured := h.systemSettingSvc.GetInt(
ctx, "asynq.wiki_concurrency", "WEKNORA_WIKI_ASYNQ_CONCURRENCY", types.DefaultWikiWorkerConcurrency,
); configured > 0 {
wikiConcurrency = int(configured)
}
allocation = types.ResolveWorkerPoolConcurrency(func(key, env string, fallback int) int {
configured := h.systemSettingSvc.GetInt(ctx, key, env, int64(fallback))
return int(configured)
})
} else {
allocation = types.DefaultWorkerPoolConcurrency()
}
allocation := types.AllocateWorkerPoolConcurrency(upstreamConcurrency)
queueCounts := make(map[string]int)
for _, definition := range types.QueueDefinitions() {
queueCounts[definition.Pool]++
}
resp := RuntimeQueuesResponse{
UpstreamConcurrency: allocation.Total,
ParseConcurrency: allocation.Total,
WikiConcurrency: wikiConcurrency,
UpstreamConcurrency: allocation.UpstreamTotal(),
ParseConcurrency: allocation.UpstreamTotal(),
WikiConcurrency: allocation.Wiki,
Pools: []RuntimeWorkerPool{
{Name: types.WorkerPoolCore, Concurrency: allocation.Core, QueueCount: queueCounts[types.WorkerPoolCore]},
{Name: types.WorkerPoolPostProcess, Concurrency: allocation.PostProcess, QueueCount: queueCounts[types.WorkerPoolPostProcess]},
{Name: types.WorkerPoolEnrichment, Concurrency: allocation.Enrichment, QueueCount: queueCounts[types.WorkerPoolEnrichment]},
{Name: types.WorkerPoolMaintenance, Concurrency: allocation.Maintenance, QueueCount: queueCounts[types.WorkerPoolMaintenance]},
{Name: types.WorkerPoolWiki, Concurrency: wikiConcurrency, QueueCount: queueCounts[types.WorkerPoolWiki]},
{Name: types.WorkerPoolShared, Concurrency: allocation.Shared, QueueCount: len(types.QueueWeightsForSharedPool())},
{Name: types.WorkerPoolWiki, Concurrency: allocation.Wiki, QueueCount: queueCounts[types.WorkerPoolWiki]},
},
Timestamp: time.Now().Unix(),
}
@@ -1456,6 +1505,12 @@ func (h *SystemHandler) GetRuntimeQueues(c *gin.Context) {
if stats != nil {
resp.Queues = stats
}
serverStats, _, serverErr := h.taskInspector.WorkerServerStats(ctx)
if serverErr != nil {
logger.Errorf(ctx, "get worker server stats failed: %v", serverErr)
} else {
aggregateRuntimeWorkerPools(resp.Pools, serverStats)
}
}
// Always emit a non-nil array so the JSON serialises to `[]` rather
// than `null`, keeping the frontend iteration simple.
+29 -5
View File
@@ -15,10 +15,18 @@ type runtimeTestSettings struct{}
func (runtimeTestSettings) GetInt(_ context.Context, key, _ string, def int64) int64 {
switch key {
case "asynq.concurrency":
return 32
case "asynq.core_concurrency":
return 8
case "asynq.postprocess_concurrency":
return 2
case "asynq.enrichment_concurrency":
return 12
case "asynq.maintenance_concurrency":
return 4
case "asynq.shared_concurrency":
return 6
case "asynq.wiki_concurrency":
return 16
return 8
default:
return def
}
@@ -55,6 +63,17 @@ func (runtimeTestInspector) HasQueuedTasksForKnowledge(context.Context, string)
func (runtimeTestInspector) QueueStats(context.Context) ([]types.QueueStat, bool, error) {
return []types.QueueStat{}, true, nil
}
func (runtimeTestInspector) WorkerServerStats(context.Context) ([]types.WorkerServerStat, bool, error) {
return []types.WorkerServerStat{
{Concurrency: 8, Active: 4, Status: "active", Queues: types.QueueWeightsForPool(types.WorkerPoolCore)},
{Concurrency: 2, Active: 1, Status: "active", Queues: types.QueueWeightsForPool(types.WorkerPoolPostProcess)},
{Concurrency: 12, Active: 6, Status: "active", Queues: types.QueueWeightsForPool(types.WorkerPoolEnrichment)},
{Concurrency: 4, Active: 1, Status: "active", Queues: types.QueueWeightsForPool(types.WorkerPoolMaintenance)},
{Concurrency: 6, Active: 3, Status: "active", Queues: types.QueueWeightsForSharedPool()},
{Concurrency: 8, Active: 2, Status: "active", Queues: types.QueueWeightsForPool(types.WorkerPoolWiki)},
{Concurrency: 99, Active: 0, Status: "stopped", Queues: types.QueueWeightsForPool(types.WorkerPoolCore)},
}, true, nil
}
func TestGetRuntimeQueuesReportsIsolatedPoolCapacity(t *testing.T) {
gin.SetMode(gin.TestMode)
@@ -85,10 +104,12 @@ func TestGetRuntimeQueuesReportsIsolatedPoolCapacity(t *testing.T) {
concurrency int
queueCount int
}{
types.WorkerPoolCore: {16, 1},
types.WorkerPoolCore: {8, 1},
types.WorkerPoolPostProcess: {2, 1},
types.WorkerPoolEnrichment: {12, 4},
types.WorkerPoolMaintenance: {4, 2},
types.WorkerPoolWiki: {16, 1},
types.WorkerPoolShared: {6, 5},
types.WorkerPoolWiki: {8, 1},
}
if len(response.Pools) != len(want) {
t.Fatalf("pool count = %d, want %d", len(response.Pools), len(want))
@@ -102,6 +123,9 @@ func TestGetRuntimeQueuesReportsIsolatedPoolCapacity(t *testing.T) {
t.Fatalf("pool %q = %+v, want concurrency=%d queue_count=%d",
pool.Name, pool, expected.concurrency, expected.queueCount)
}
if pool.Instances != 1 || pool.ClusterCapacity != expected.concurrency {
t.Fatalf("pool %q live capacity = %+v", pool.Name, pool)
}
}
}
+47 -30
View File
@@ -24,13 +24,15 @@ import (
type AsynqTaskParams struct {
dig.In
// Four independent servers provide hard capacity isolation between the
// user-facing parse path, high-fanout enrichment, maintenance/sync work,
// and Wiki generation. They share one handler mux but subscribe to disjoint
// queue sets from types.QueueDefinitions.
// Dedicated servers provide minimum capacity for parse, post-process,
// enrichment, maintenance, and Wiki work. SharedServer is the elastic tier:
// it also subscribes to core/enrichment queues so either stage can borrow
// otherwise-idle capacity without consuming the other stage's guarantee.
CoreServer *asynq.Server `name:"coreAsynqServer"`
PostProcessServer *asynq.Server `name:"postProcessAsynqServer"`
EnrichmentServer *asynq.Server `name:"enrichmentAsynqServer"`
MaintenanceServer *asynq.Server `name:"maintenanceAsynqServer"`
SharedServer *asynq.Server `name:"sharedAsynqServer"`
WikiServer *asynq.Server `name:"wikiAsynqServer"`
KnowledgeService interfaces.KnowledgeService
KnowledgeBaseService interfaces.KnowledgeBaseService
@@ -151,32 +153,41 @@ func backgroundTaskMiddleware() asynq.MiddlewareFunc {
}
}
func resolveUpstreamPoolConcurrency(svc interfaces.SystemSettingService) types.WorkerPoolConcurrency {
total := types.DefaultUpstreamWorkerConcurrency
if svc != nil {
n := svc.GetInt(context.Background(), "asynq.concurrency", "WEKNORA_ASYNQ_CONCURRENCY", types.DefaultUpstreamWorkerConcurrency)
if n > 0 {
total = int(n)
}
func resolveWorkerPoolConcurrency(svc interfaces.SystemSettingService) types.WorkerPoolConcurrency {
if svc == nil {
return types.DefaultWorkerPoolConcurrency()
}
return types.AllocateWorkerPoolConcurrency(total)
ctx := context.Background()
return types.ResolveWorkerPoolConcurrency(func(key, env string, fallback int) int {
value := svc.GetInt(ctx, key, env, int64(fallback))
return int(value)
})
}
// NewCoreAsynqServer runs the latency-sensitive document parse and
// post-process orchestration path.
// NewCoreAsynqServer runs document and manual parsing with guaranteed capacity.
func NewCoreAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
allocation := resolveUpstreamPoolConcurrency(svc)
allocation := resolveWorkerPoolConcurrency(svc)
log.Printf("asynq core-pool server starting with concurrency=%d total_upstream=%d redis_op_timeout=%dms",
allocation.Core, allocation.Total, readRedisOpTimeoutMs())
allocation.Core, allocation.UpstreamTotal(), readRedisOpTimeoutMs())
return newAsynqServer(allocation.Core, types.QueueWeightsForPool(types.WorkerPoolCore))
}
// NewPostProcessAsynqServer reserves capacity for the lightweight but
// latency-sensitive fan-out stage. It cannot be trapped behind a burst of
// long-running document parses in QueueDefault.
func NewPostProcessAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
allocation := resolveWorkerPoolConcurrency(svc)
log.Printf("asynq postprocess-pool server starting with concurrency=%d total_upstream=%d",
allocation.PostProcess, allocation.UpstreamTotal())
return newAsynqServer(allocation.PostProcess, types.QueueWeightsForPool(types.WorkerPoolPostProcess))
}
// NewEnrichmentAsynqServer runs high-fanout summary, multimodal, graph, and
// question generation without consuming core parsing capacity.
func NewEnrichmentAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
allocation := resolveUpstreamPoolConcurrency(svc)
allocation := resolveWorkerPoolConcurrency(svc)
log.Printf("asynq enrichment-pool server starting with concurrency=%d total_upstream=%d",
allocation.Enrichment, allocation.Total)
allocation.Enrichment, allocation.UpstreamTotal())
return newAsynqServer(allocation.Enrichment, types.QueueWeightsForPool(types.WorkerPoolEnrichment))
}
@@ -184,25 +195,29 @@ func NewEnrichmentAsynqServer(svc interfaces.SystemSettingService) *asynq.Server
// dispatch work. QueueMaintenance keeps the legacy Redis name "low", so old
// tasks are drained safely during rolling upgrades.
func NewMaintenanceAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
allocation := resolveUpstreamPoolConcurrency(svc)
allocation := resolveWorkerPoolConcurrency(svc)
log.Printf("asynq maintenance-pool server starting with concurrency=%d total_upstream=%d",
allocation.Maintenance, allocation.Total)
allocation.Maintenance, allocation.UpstreamTotal())
return newAsynqServer(allocation.Maintenance, types.QueueWeightsForPool(types.WorkerPoolMaintenance))
}
// NewSharedAsynqServer is the elastic tier. Asynq dequeue is atomic, so it is
// safe for this server and the dedicated servers to subscribe to the same
// queues: every task is still executed by exactly one worker.
func NewSharedAsynqServer(svc interfaces.SystemSettingService) *asynq.Server {
allocation := resolveWorkerPoolConcurrency(svc)
log.Printf("asynq shared-pool server starting with concurrency=%d total_upstream=%d",
allocation.Shared, allocation.UpstreamTotal())
return newAsynqServer(allocation.Shared, types.QueueWeightsForSharedPool())
}
// NewWikiAsynqServer builds the dedicated wiki pool: QueueWiki only. It runs
// the shared handler mux but only ever pulls wiki tasks, so its concurrency
// budget (WEKNORA_WIKI_ASYNQ_CONCURRENCY, default 16) is spent exclusively on
// budget (WEKNORA_WIKI_ASYNQ_CONCURRENCY, default 8) 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 := types.DefaultWikiWorkerConcurrency
if svc != nil {
n := svc.GetInt(context.Background(), "asynq.wiki_concurrency", "WEKNORA_WIKI_ASYNQ_CONCURRENCY", types.DefaultWikiWorkerConcurrency)
if n > 0 {
concurrency = int(n)
}
}
concurrency := resolveWorkerPoolConcurrency(svc).Wiki
log.Printf("asynq wiki-pool server starting with concurrency=%d", concurrency)
return newAsynqServer(concurrency, types.QueueWeightsForPool(types.WorkerPoolWiki))
}
@@ -293,8 +308,8 @@ func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
mux.HandleFunc(types.TypeWikiIngest, params.WikiIngest.Handle)
mux.HandleFunc(types.TypeWikiFinalize, params.WikiIngest.Handle)
// Run the same mux on every pool. Queue definitions are disjoint, so a given
// task is dequeued by exactly one pool while handlers stay registered once.
// Run the same mux on every pool. Shared and dedicated servers intentionally
// overlap, but Redis dequeue is atomic, so each task still executes once.
runPool := func(name string, srv *asynq.Server) {
go func() {
if err := srv.Run(mux); err != nil {
@@ -303,8 +318,10 @@ func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
}()
}
runPool("core-pool", params.CoreServer)
runPool("postprocess-pool", params.PostProcessServer)
runPool("enrichment-pool", params.EnrichmentServer)
runPool("maintenance-pool", params.MaintenanceServer)
runPool("shared-pool", params.SharedServer)
runPool("wiki-pool", params.WikiServer)
return mux
}
+35
View File
@@ -183,6 +183,35 @@ func (a *asynqTaskInspector) QueueStats(
return stats, true, nil
}
func (a *asynqTaskInspector) WorkerServerStats(
ctx context.Context,
) ([]types.WorkerServerStat, bool, error) {
if a == nil || a.inspector == nil {
return nil, false, nil
}
servers, err := a.inspector.Servers()
if err != nil {
return nil, true, err
}
stats := make([]types.WorkerServerStat, 0, len(servers))
for _, server := range servers {
if server == nil {
continue
}
queues := make(map[string]int, len(server.Queues))
for name, weight := range server.Queues {
queues[name] = weight
}
stats = append(stats, types.WorkerServerStat{
Concurrency: server.Concurrency,
Active: len(server.ActiveWorkers),
Status: server.Status,
Queues: queues,
})
}
return stats, true, nil
}
// queueStateHasMatch pages through one (queue, state) list looking for a
// task that references knowledgeID. Mirrors the delete* scanners but is
// strictly read-only and returns early on the first hit. A backend error
@@ -390,3 +419,9 @@ func (noopTaskInspector) QueueStats(
) ([]types.QueueStat, bool, error) {
return nil, false, nil
}
func (noopTaskInspector) WorkerServerStats(
ctx context.Context,
) ([]types.WorkerServerStat, bool, error) {
return nil, false, nil
}
@@ -57,4 +57,9 @@ type TaskInspector interface {
// surfaced as a zeroed row (so the full lane set stays visible) rather
// than failing the whole call.
QueueStats(ctx context.Context) (stats []types.QueueStat, supported bool, err error)
// WorkerServerStats returns live asynq server heartbeats across all
// replicas. The runtime dashboard uses them to aggregate actual cluster
// capacity and busy workers for each configured pool.
WorkerServerStats(ctx context.Context) (stats []types.WorkerServerStat, supported bool, err error)
}
+99 -52
View File
@@ -5,15 +5,24 @@ package types
// between pools instead of being only a weighted dequeue preference.
const (
WorkerPoolCore = "core"
WorkerPoolPostProcess = "postprocess"
WorkerPoolEnrichment = "enrichment"
WorkerPoolMaintenance = "maintenance"
WorkerPoolShared = "shared"
WorkerPoolWiki = "wiki"
// DefaultUpstreamWorkerConcurrency is split across core, enrichment,
// and maintenance. Wiki stays separately configurable because its model
// workload has a different capacity profile.
DefaultUpstreamWorkerConcurrency = 32
DefaultWikiWorkerConcurrency = 16
// Upstream defaults are explicit guarantees plus an elastic pool. The
// shared pool may consume core and enrichment queues, so idle capacity in
// either stage can be borrowed without sacrificing the dedicated minimums.
DefaultCoreWorkerConcurrency = 8
DefaultPostProcessWorkerConcurrency = 2
DefaultEnrichmentWorkerConcurrency = 12
DefaultMaintenanceWorkerConcurrency = 4
DefaultSharedWorkerConcurrency = 6
DefaultWikiWorkerConcurrency = 8
DefaultUpstreamWorkerConcurrency = DefaultCoreWorkerConcurrency +
DefaultPostProcessWorkerConcurrency + DefaultEnrichmentWorkerConcurrency +
DefaultMaintenanceWorkerConcurrency + DefaultSharedWorkerConcurrency
)
// Asynq queue names. QueueMaintenance intentionally keeps the physical Redis
@@ -21,6 +30,7 @@ const (
// rolling deployment. New code uses the business-semantic constant.
const (
QueueDefault = "default"
QueuePostProcess = "postprocess"
QueueSummary = "summary"
QueueMultimodal = "multimodal"
QueueGraph = "graph"
@@ -35,22 +45,26 @@ const (
// scheduling weights shown to operators from drifting from the actual server
// configuration.
type QueueDefinition struct {
Name string
Pool string
Weight int
TaskTypes []string
Name string
Pool string
Weight int
SharedWeight int
TaskTypes []string
}
var queueDefinitions = []QueueDefinition{
{Name: QueueDefault, Pool: WorkerPoolCore, Weight: 1, TaskTypes: []string{
TypeDocumentProcess, TypeManualProcess, TypeKnowledgePostProcess,
{Name: QueueDefault, Pool: WorkerPoolCore, Weight: 1, SharedWeight: 3, TaskTypes: []string{
TypeDocumentProcess, TypeManualProcess,
}},
{Name: QueueSummary, Pool: WorkerPoolEnrichment, Weight: 2, TaskTypes: []string{
{Name: QueuePostProcess, Pool: WorkerPoolPostProcess, Weight: 1, TaskTypes: []string{
TypeKnowledgePostProcess,
}},
{Name: QueueSummary, Pool: WorkerPoolEnrichment, Weight: 2, SharedWeight: 2, TaskTypes: []string{
TypeSummaryGeneration, TypeDataTableSummary,
}},
{Name: QueueMultimodal, Pool: WorkerPoolEnrichment, Weight: 1, TaskTypes: []string{TypeImageMultimodal}},
{Name: QueueGraph, Pool: WorkerPoolEnrichment, Weight: 1, TaskTypes: []string{TypeChunkExtract}},
{Name: QueueQuestion, Pool: WorkerPoolEnrichment, Weight: 1, TaskTypes: []string{TypeQuestionGeneration}},
{Name: QueueMultimodal, Pool: WorkerPoolEnrichment, Weight: 1, SharedWeight: 1, TaskTypes: []string{TypeImageMultimodal}},
{Name: QueueGraph, Pool: WorkerPoolEnrichment, Weight: 1, SharedWeight: 1, TaskTypes: []string{TypeChunkExtract}},
{Name: QueueQuestion, Pool: WorkerPoolEnrichment, Weight: 1, SharedWeight: 1, TaskTypes: []string{TypeQuestionGeneration}},
{Name: QueueSync, Pool: WorkerPoolMaintenance, Weight: 2, TaskTypes: []string{TypeDataSourceSync}},
{Name: QueueMaintenance, Pool: WorkerPoolMaintenance, Weight: 1, TaskTypes: []string{
TypeFAQImport, TypeKBClone, TypeIndexDelete, TypeKBDelete,
@@ -95,49 +109,71 @@ func QueueWeightsForPool(pool string) map[string]int {
return weights
}
// WorkerPoolConcurrency is the deterministic split of the total upstream
// worker budget. Keeping this pure allocation function in types lets the
// server constructors and the System Admin API report identical values.
type WorkerPoolConcurrency struct {
Total int
Core int
Enrichment int
Maintenance int
}
// AllocateWorkerPoolConcurrency reserves 1/2 of the upstream budget for the
// latency-sensitive core pipeline, 3/8 for LLM/VLM enrichment, and the
// remainder for sync/cleanup/batch work. A minimum total of three guarantees
// every independent server has at least one worker.
func AllocateWorkerPoolConcurrency(total int) WorkerPoolConcurrency {
if total < 3 {
total = 3
}
core := total / 2
enrichment := (total * 3) / 8
if core < 1 {
core = 1
}
if enrichment < 1 {
enrichment = 1
}
maintenance := total - core - enrichment
if maintenance < 1 {
maintenance = 1
if core >= enrichment && core > 1 {
core--
} else if enrichment > 1 {
enrichment--
// QueueWeightsForSharedPool returns the core/enrichment queues eligible for
// elastic capacity. Post-process and maintenance are deliberately excluded:
// post-process needs a small latency guarantee, while long maintenance tasks
// must not pin burst capacity intended for the user-facing pipeline.
func QueueWeightsForSharedPool() map[string]int {
weights := make(map[string]int)
for _, definition := range queueDefinitions {
if definition.SharedWeight > 0 {
weights[definition.Name] = definition.SharedWeight
}
}
return weights
}
// WorkerPoolConcurrency contains the explicit per-instance pool capacities.
// Unlike the old ratio allocator, each field is independently configurable.
type WorkerPoolConcurrency struct {
Core int
PostProcess int
Enrichment int
Maintenance int
Shared int
Wiki int
}
func DefaultWorkerPoolConcurrency() WorkerPoolConcurrency {
return WorkerPoolConcurrency{
Total: total,
Core: core,
Enrichment: enrichment,
Maintenance: maintenance,
Core: DefaultCoreWorkerConcurrency,
PostProcess: DefaultPostProcessWorkerConcurrency,
Enrichment: DefaultEnrichmentWorkerConcurrency,
Maintenance: DefaultMaintenanceWorkerConcurrency,
Shared: DefaultSharedWorkerConcurrency,
Wiki: DefaultWikiWorkerConcurrency,
}
}
// ResolveWorkerPoolConcurrency centralizes setting keys, environment names,
// defaults, and invalid-value fallback for both server construction and the
// runtime API. The callback lets this package stay independent of the system
// setting service interface.
func ResolveWorkerPoolConcurrency(read func(key, env string, fallback int) int) WorkerPoolConcurrency {
allocation := DefaultWorkerPoolConcurrency()
if read == nil {
return allocation
}
positive := func(key, env string, fallback int) int {
value := read(key, env, fallback)
if value < 1 {
return fallback
}
return value
}
allocation.Core = positive("asynq.core_concurrency", "WEKNORA_ASYNQ_CORE_CONCURRENCY", allocation.Core)
allocation.PostProcess = positive("asynq.postprocess_concurrency", "WEKNORA_ASYNQ_POSTPROCESS_CONCURRENCY", allocation.PostProcess)
allocation.Enrichment = positive("asynq.enrichment_concurrency", "WEKNORA_ASYNQ_ENRICHMENT_CONCURRENCY", allocation.Enrichment)
allocation.Maintenance = positive("asynq.maintenance_concurrency", "WEKNORA_ASYNQ_MAINTENANCE_CONCURRENCY", allocation.Maintenance)
allocation.Shared = positive("asynq.shared_concurrency", "WEKNORA_ASYNQ_SHARED_CONCURRENCY", allocation.Shared)
allocation.Wiki = positive("asynq.wiki_concurrency", "WEKNORA_WIKI_ASYNQ_CONCURRENCY", allocation.Wiki)
return allocation
}
func (c WorkerPoolConcurrency) UpstreamTotal() int {
return c.Core + c.PostProcess + c.Enrichment + c.Maintenance + c.Shared
}
// QueueStat is a read-only depth snapshot of a single asynq queue, used
// by the System Admin runtime dashboard. Field names mirror the counts
// exposed by asynq.QueueInfo. Pool / Weight are static metadata (which
@@ -170,6 +206,17 @@ type QueueStat struct {
MemoryUsageBytes int64 `json:"memory_usage_bytes"`
}
// WorkerServerStat is one live asynq server heartbeat. Queue weights identify
// which logical pool the server belongs to; the runtime handler aggregates
// these records across replicas so configured per-instance capacity is not
// confused with actual cluster capacity.
type WorkerServerStat struct {
Concurrency int
Active int
Status string
Queues map[string]int
}
const (
TypeChunkExtract = "chunk:extract"
TypeDocumentProcess = "document:process" // 文档处理任务
+33 -16
View File
@@ -10,6 +10,7 @@ func TestQueueDefinitionsAreUniqueAndConsumable(t *testing.T) {
validPools := map[string]bool{
WorkerPoolCore: true,
WorkerPoolPostProcess: true,
WorkerPoolEnrichment: true,
WorkerPoolMaintenance: true,
WorkerPoolWiki: true,
@@ -47,6 +48,13 @@ func TestQueueDefinitionsAreUniqueAndConsumable(t *testing.T) {
t.Fatalf("worker pool %q has no queues", pool)
}
}
shared := QueueWeightsForSharedPool()
if len(shared) == 0 || shared[QueueDefault] <= 0 || shared[QueueSummary] <= 0 {
t.Fatalf("shared pool must cover core and enrichment queues: %+v", shared)
}
if shared[QueuePostProcess] != 0 || shared[QueueMaintenance] != 0 {
t.Fatalf("shared pool must not consume post-process or maintenance: %+v", shared)
}
}
func TestQueueMaintenanceKeepsLegacyPhysicalName(t *testing.T) {
@@ -71,22 +79,31 @@ func TestEveryAsynqTaskTypeHasADeclaredQueue(t *testing.T) {
}
}
func TestAllocateWorkerPoolConcurrencyPreservesBudget(t *testing.T) {
for _, total := range []int{3, 4, 8, 32, 64} {
allocation := AllocateWorkerPoolConcurrency(total)
if allocation.Total != total {
t.Fatalf("total=%d: allocation reported total %d", total, allocation.Total)
}
if allocation.Core < 1 || allocation.Enrichment < 1 || allocation.Maintenance < 1 {
t.Fatalf("total=%d: every pool must receive capacity: %+v", total, allocation)
}
if allocation.Core+allocation.Enrichment+allocation.Maintenance != total {
t.Fatalf("total=%d: split does not preserve budget: %+v", total, allocation)
}
func TestDefaultWorkerPoolConcurrencyIsExplicitBudget(t *testing.T) {
allocation := DefaultWorkerPoolConcurrency()
if allocation.UpstreamTotal() != DefaultUpstreamWorkerConcurrency {
t.Fatalf("upstream total = %d, want %d", allocation.UpstreamTotal(), DefaultUpstreamWorkerConcurrency)
}
minimum := AllocateWorkerPoolConcurrency(1)
if minimum.Total != 3 || minimum.Core+minimum.Enrichment+minimum.Maintenance != 3 {
t.Fatalf("undersized budget should clamp to three workers: %+v", minimum)
if allocation.Core < 1 || allocation.PostProcess < 1 || allocation.Enrichment < 1 ||
allocation.Maintenance < 1 || allocation.Shared < 1 || allocation.Wiki < 1 {
t.Fatalf("every pool must have positive explicit capacity: %+v", allocation)
}
}
func TestResolveWorkerPoolConcurrencyFallsBackPerPool(t *testing.T) {
allocation := ResolveWorkerPoolConcurrency(func(key, _ string, fallback int) int {
if key == "asynq.core_concurrency" {
return 15
}
if key == "asynq.shared_concurrency" {
return 0
}
return fallback
})
if allocation.Core != 15 {
t.Fatalf("core = %d, want override 15", allocation.Core)
}
if allocation.Shared != DefaultSharedWorkerConcurrency {
t.Fatalf("shared = %d, want fallback %d", allocation.Shared, DefaultSharedWorkerConcurrency)
}
}