feat(system): expose model concurrency stats on runtime queues dashboard
Surface per-model active/waiting/limit from the background task limiter so admins can distinguish task scheduling from model-service throttling, with full i18n coverage.
This commit is contained in:
@@ -2,6 +2,7 @@ package limiter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
@@ -53,6 +54,10 @@ func Gate(ctx context.Context, modelID string) func() {
|
||||
// blocks a call permanently — a limiter/Redis outage or a cancelled context
|
||||
// fails open.
|
||||
func GateN(ctx context.Context, modelID string, modelLimit int) func() {
|
||||
return GateNamedN(ctx, modelID, "", modelLimit)
|
||||
}
|
||||
|
||||
func GateNamedN(ctx context.Context, modelID, modelName string, modelLimit int) func() {
|
||||
governorMu.RLock()
|
||||
l, defaultLimit := governor, governorN
|
||||
governorMu.RUnlock()
|
||||
@@ -64,6 +69,9 @@ func GateN(ctx context.Context, modelID string, modelLimit int) func() {
|
||||
if l == nil || limit <= 0 || !types.IsBackgroundTask(ctx) {
|
||||
return noop
|
||||
}
|
||||
if named, ok := l.(interface{ SetModelName(string, string) }); ok {
|
||||
named.SetModelName(modelID, modelName)
|
||||
}
|
||||
release, err := l.Acquire(ctx, modelID, limit)
|
||||
if err != nil || release == nil {
|
||||
return noop
|
||||
@@ -71,19 +79,38 @@ func GateN(ctx context.Context, modelID string, modelLimit int) func() {
|
||||
return release
|
||||
}
|
||||
|
||||
// RuntimeStats returns the semaphores observed by this process. Redis-backed
|
||||
// limiters report cluster-wide active holders; waiting callers remain local to
|
||||
// this instance by design.
|
||||
func RuntimeStats(ctx context.Context) ([]RuntimeStat, bool, error) {
|
||||
governorMu.RLock()
|
||||
l := governor
|
||||
governorMu.RUnlock()
|
||||
inspector, ok := l.(runtimeInspectable)
|
||||
if !ok || inspector == nil {
|
||||
return []RuntimeStat{}, false, nil
|
||||
}
|
||||
stats, err := inspector.RuntimeStats(ctx)
|
||||
if stats == nil {
|
||||
stats = []RuntimeStat{}
|
||||
}
|
||||
return stats, true, err
|
||||
}
|
||||
|
||||
// localLimiter is an in-process (single-node) counting semaphore keyed by
|
||||
// model ID. It is the Lite-mode counterpart to the Redis limiter: Lite runs a
|
||||
// single process with no Redis, so a shared distributed semaphore is neither
|
||||
// available nor needed — but background ingestion can still burst the whole
|
||||
// worker pool against one provider, so we still cap concurrency locally.
|
||||
type localLimiter struct {
|
||||
mu sync.Mutex
|
||||
sems map[string]chan struct{}
|
||||
mu sync.Mutex
|
||||
sems map[string]chan struct{}
|
||||
tracked map[string]*trackedSemaphore
|
||||
}
|
||||
|
||||
// NewLocalLimiter builds an in-process per-key concurrency limiter.
|
||||
func NewLocalLimiter() ModelConcurrencyLimiter {
|
||||
return &localLimiter{sems: make(map[string]chan struct{})}
|
||||
return &localLimiter{sems: make(map[string]chan struct{}), tracked: make(map[string]*trackedSemaphore)}
|
||||
}
|
||||
|
||||
func (l *localLimiter) Acquire(ctx context.Context, key string, limit int) (func(), error) {
|
||||
@@ -93,6 +120,12 @@ func (l *localLimiter) Acquire(ctx context.Context, key string, limit int) (func
|
||||
|
||||
l.mu.Lock()
|
||||
sem, ok := l.sems[key]
|
||||
tracked := l.tracked[key]
|
||||
if tracked == nil {
|
||||
tracked = &trackedSemaphore{}
|
||||
l.tracked[key] = tracked
|
||||
}
|
||||
tracked.limit.Store(int64(limit))
|
||||
if !ok {
|
||||
// Capacity is fixed at first use for a key; the limit is a
|
||||
// process-wide constant, so it never changes across acquires.
|
||||
@@ -100,6 +133,8 @@ func (l *localLimiter) Acquire(ctx context.Context, key string, limit int) (func
|
||||
l.sems[key] = sem
|
||||
}
|
||||
l.mu.Unlock()
|
||||
tracked.waiting.Add(1)
|
||||
defer tracked.waiting.Add(-1)
|
||||
|
||||
select {
|
||||
case sem <- struct{}{}:
|
||||
@@ -110,3 +145,30 @@ func (l *localLimiter) Acquire(ctx context.Context, key string, limit int) (func
|
||||
return noop, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (l *localLimiter) RuntimeStats(_ context.Context) ([]RuntimeStat, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
stats := make([]RuntimeStat, 0, len(l.sems))
|
||||
for modelID, sem := range l.sems {
|
||||
tracked := l.tracked[modelID]
|
||||
name, _ := tracked.name.Load().(string)
|
||||
stats = append(stats, RuntimeStat{ModelID: modelID, Name: name, Active: int64(len(sem)), Waiting: tracked.waiting.Load(), Limit: int(tracked.limit.Load())})
|
||||
}
|
||||
sort.Slice(stats, func(i, j int) bool { return stats[i].ModelID < stats[j].ModelID })
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (l *localLimiter) SetModelName(modelID, name string) {
|
||||
if modelID == "" || name == "" {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
tracked := l.tracked[modelID]
|
||||
if tracked == nil {
|
||||
tracked = &trackedSemaphore{}
|
||||
l.tracked[modelID] = tracked
|
||||
}
|
||||
l.mu.Unlock()
|
||||
tracked.name.Store(name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user