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
@@ -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)
}
}