feat(routing): 完善平台满载避让与故障轮转

为未配置并发上限的平台增加基于运行和等待任务数的软负载,并按非满载、有效优先级、缓存亲和与负载稳定排序。\n\n平台模型 RPM、TPM 和并发额度竞争失败时只轮转候选,不触发冷却、禁用或降级;用户组额度保持不可绕过。补齐异步冷却排队恢复、满载原因、选择原因与低基数指标。\n\n验证:go test ./...;go vet ./...;PostgreSQL 原子额度、准入队列、Worker 容量回收及故障策略 HTTP acceptance。
This commit is contained in:
2026-08-03 17:42:34 +08:00
parent b125599354
commit 8362d6d27a
15 changed files with 468 additions and 152 deletions
@@ -79,6 +79,13 @@ type Metrics struct {
taskAdmissionCancelled atomic.Uint64
taskAdmissionExpired atomic.Uint64
taskAdmissionMigrated atomic.Uint64
candidateNormalRotation atomic.Uint64
candidateFullAvoided atomic.Uint64
candidateQuotaRaceRotated atomic.Uint64
candidateCooldownSkipped atomic.Uint64
candidateDisabledSkipped atomic.Uint64
candidateAllFullQueued atomic.Uint64
candidateRoutingOther atomic.Uint64
taskAdmissionWaitBuckets [11]atomic.Uint64
taskAdmissionWaitMicros atomic.Uint64
}
@@ -239,6 +246,25 @@ func (m *Metrics) ObserveTaskAdmission(event string) {
}
}
func (m *Metrics) ObserveCandidateRouting(reason string) {
switch reason {
case "normal_rotation":
m.candidateNormalRotation.Add(1)
case "full_avoided":
m.candidateFullAvoided.Add(1)
case "quota_race_rotated":
m.candidateQuotaRaceRotated.Add(1)
case "cooldown_skipped":
m.candidateCooldownSkipped.Add(1)
case "disabled_skipped":
m.candidateDisabledSkipped.Add(1)
case "all_full_queued":
m.candidateAllFullQueued.Add(1)
default:
m.candidateRoutingOther.Add(1)
}
}
func (m *Metrics) ObserveTaskAdmissionWait(wait time.Duration) {
if wait < 0 {
wait = 0
@@ -436,6 +462,15 @@ func (m *Metrics) Handler(provider MetricsSnapshotProvider, issuer, audience str
{"concurrent", m.providerQuotaWaitConcurrent.Load()},
{"other", m.providerQuotaWaitOther.Load()},
})
outcomeCounters(w, "easyai_gateway_candidate_routing_total", "Candidate routing decisions by bounded reason.", []outcomeValue{
{"normal_rotation", m.candidateNormalRotation.Load()},
{"full_avoided", m.candidateFullAvoided.Load()},
{"quota_race_rotated", m.candidateQuotaRaceRotated.Load()},
{"cooldown_skipped", m.candidateCooldownSkipped.Load()},
{"disabled_skipped", m.candidateDisabledSkipped.Load()},
{"all_full_queued", m.candidateAllFullQueued.Load()},
{"other", m.candidateRoutingOther.Load()},
})
platformModelRateLimitUtilizationGauges(w, modelRateLimits)
plainGauge(w, "easyai_gateway_postgres_pool_max_connections", "Maximum PostgreSQL connections in this process pool.", int64(postgresPool.MaxConnections))
plainGauge(w, "easyai_gateway_postgres_pool_total_connections", "Current PostgreSQL connections in this process pool.", int64(postgresPool.TotalConnections))
@@ -46,6 +46,12 @@ func TestMetricsExposeBoundedOutcomesAndState(t *testing.T) {
metrics.SetWorkerLoad(12, 3, 8, 2, 5, 1, "busy")
metrics.ObserveProviderQuotaWait("rpm")
metrics.ObserveProviderQuotaWait("tpm_total")
metrics.ObserveCandidateRouting("normal_rotation")
metrics.ObserveCandidateRouting("full_avoided")
metrics.ObserveCandidateRouting("quota_race_rotated")
metrics.ObserveCandidateRouting("cooldown_skipped")
metrics.ObserveCandidateRouting("disabled_skipped")
metrics.ObserveCandidateRouting("all_full_queued")
metrics.ObserveAsyncWorkerResize("success")
metrics.ObserveConcurrencyLeaseRenewal("success")
metrics.ObserveConcurrencyLeaseRenewal("lost")
@@ -86,6 +92,12 @@ func TestMetricsExposeBoundedOutcomesAndState(t *testing.T) {
`easyai_gateway_worker_pressure_state 1`,
`easyai_gateway_provider_quota_waits_total{outcome="rpm"} 1`,
`easyai_gateway_provider_quota_waits_total{outcome="tpm"} 1`,
`easyai_gateway_candidate_routing_total{outcome="normal_rotation"} 1`,
`easyai_gateway_candidate_routing_total{outcome="full_avoided"} 1`,
`easyai_gateway_candidate_routing_total{outcome="quota_race_rotated"} 1`,
`easyai_gateway_candidate_routing_total{outcome="cooldown_skipped"} 1`,
`easyai_gateway_candidate_routing_total{outcome="disabled_skipped"} 1`,
`easyai_gateway_candidate_routing_total{outcome="all_full_queued"} 1`,
`easyai_gateway_platform_model_rate_limit_utilization{platform_model_id="platform-model-1",metric="concurrent"} 0.750000`,
`easyai_gateway_async_worker_resizes_total{outcome="success"} 1`,
`easyai_gateway_concurrency_lease_renewals_total{outcome="success"} 1`,