mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-12-03 00:00:58 +00:00
fix(scheduling): score recent model load alongside concurrency
Include recent request pressure in Smart load scores before near-best selection, keeping the concurrency penalty independent. Remove the separate recent-count filter and share the score with diagnostics and queue selection. Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -1053,6 +1053,8 @@ impl AccountSelector {
|
||||
|
||||
// 首输出 10 秒时延迟得分减半;固定尺度不随其他候选账号变化
|
||||
const SMART_LATENCY_HALF_SCORE_MS: f64 = 10_000.0;
|
||||
// 最近一分钟 10 次请求对应 0.5 的近期负载压力,固定尺度不代表上游 RPM 限额
|
||||
const SMART_RECENT_REQUEST_HALF_LOAD: f64 = 10.0;
|
||||
|
||||
fn capacity_utilization(candidate: &AccountCandidate, context: &AccountSelectionContext) -> f64 {
|
||||
context
|
||||
@@ -1078,16 +1080,6 @@ fn select_smart_candidate<'a>(
|
||||
ranked.retain(|(_, score)| {
|
||||
best_score - score <= context.policy.smart_scheduling().score_tolerance()
|
||||
});
|
||||
// 只在近似最优集合内比较近期负载,保留其它评分维度的取舍
|
||||
if context.policy.smart_scheduling().weights()[0] > 0.0 {
|
||||
let least_recent = ranked
|
||||
.iter()
|
||||
.map(|(candidate, _)| candidate.signals.recent_requests)
|
||||
.min()?;
|
||||
ranked.retain(|(candidate, _)| {
|
||||
candidate.signals.recent_requests <= least_recent.saturating_add(1)
|
||||
});
|
||||
}
|
||||
// 轮换顺序保持稳定,避免分数轻微交错与 cursor 同步后仍反复命中同一账号
|
||||
ranked.sort_unstable_by(|(left, _), (right, _)| left.account.id().cmp(right.account.id()));
|
||||
let index = (context.round_robin_cursor % ranked.len() as u64) as usize;
|
||||
@@ -1097,7 +1089,10 @@ fn select_smart_candidate<'a>(
|
||||
pub(crate) fn smart_score(candidate: &AccountCandidate, context: &AccountSelectionContext) -> f64 {
|
||||
let config = context.policy.smart_scheduling();
|
||||
let now = context.now;
|
||||
let load = 1.0 - capacity_utilization(candidate, context).clamp(0.0, 1.0);
|
||||
// 两类压力独立扣分,近期热度不能稀释当前并发占用的代价
|
||||
let recent_requests = candidate.signals.recent_requests as f64;
|
||||
let recent_load = recent_requests / (SMART_RECENT_REQUEST_HALF_LOAD + recent_requests);
|
||||
let load = 1.0 - capacity_utilization(candidate, context).clamp(0.0, 1.0) - recent_load;
|
||||
let quota = candidate
|
||||
.signals
|
||||
.quota_remaining_rank
|
||||
|
||||
@@ -432,11 +432,11 @@ fn missing_expired_and_duplicate_periods_do_not_inflate_reset_score() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smart_spreads_near_best_candidates_by_recent_load_when_load_weight_is_enabled() {
|
||||
fn smart_prefers_recently_idle_accounts_and_disables_recent_load_with_zero_weight() {
|
||||
let candidates = (0..130)
|
||||
.map(|index| {
|
||||
let mut candidate = candidate_with_concurrency(&format!("acct_{index:03}"), 0, 8);
|
||||
candidate.signals.recent_requests = index;
|
||||
candidate.signals.recent_requests = index / 2;
|
||||
candidate
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
@@ -464,7 +464,7 @@ fn smart_spreads_near_best_candidates_by_recent_load_when_load_weight_is_enabled
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_load_respects_affinity_weight_capacity_and_the_near_best_score_boundary() {
|
||||
fn recent_load_respects_affinity_weight_capacity_and_configured_quota_weight() {
|
||||
let mut candidates = [
|
||||
candidate_with_concurrency("acct_busy", 0, 8),
|
||||
candidate_with_concurrency("acct_idle", 0, 8),
|
||||
@@ -527,3 +527,92 @@ fn recent_load_respects_affinity_weight_capacity_and_the_near_best_score_boundar
|
||||
"acct_busy"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_model_load_competes_with_quota_and_cross_model_concurrency_before_score_filtering() {
|
||||
for weights in [
|
||||
[1.0, 0.1, 1.0, 0.0, 0.0, 0.0],
|
||||
[1.0, 0.8, 1.0, 0.5, 0.0, 0.0],
|
||||
] {
|
||||
for limit in [6, 8] {
|
||||
for (hot_requests, cool_requests, cool_in_flight, cool_quota) in [
|
||||
(18, 0, 0, 9_000),
|
||||
(18, 0, 0, 7_000),
|
||||
(18, 0, 1, 10_000),
|
||||
(16, 4, 1, 9_000),
|
||||
(27, 3, 1, 7_000),
|
||||
] {
|
||||
let mut candidates = [
|
||||
candidate_with_concurrency("acct_hot", 0, limit),
|
||||
candidate_with_concurrency("acct_cool", cool_in_flight, limit),
|
||||
];
|
||||
candidates[0].signals.quota_remaining_rank = Some(10_000);
|
||||
candidates[0].signals.recent_requests = hot_requests;
|
||||
candidates[1].signals.quota_remaining_rank = Some(cool_quota);
|
||||
candidates[1].signals.recent_requests = cool_requests;
|
||||
let mut context = context(RotationStrategy::Smart);
|
||||
context.policy = context
|
||||
.policy
|
||||
.with_smart_scheduling(SmartSchedulingConfig::new(weights).unwrap());
|
||||
let selected = AccountSelector.select(&candidates, &context).unwrap();
|
||||
assert_eq!(selected.candidate().account.id().as_str(), "acct_cool");
|
||||
|
||||
let trace = gateway_core::diagnostics::TraceContext::new("req_recent_score");
|
||||
trace.account_selection(&candidates, &context, Some(&selected));
|
||||
let snapshot = trace.snapshot().unwrap();
|
||||
let scores = &snapshot["events"][0]["data"]["candidates"];
|
||||
assert!(
|
||||
scores[0]["smartScore"].as_f64().unwrap()
|
||||
> scores[1]["smartScore"].as_f64().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_heat_does_not_dilute_concurrency_pressure_or_require_nonnegative_load_scores() {
|
||||
let mut candidates = [
|
||||
candidate_with_concurrency("acct_busy", 5, 6),
|
||||
candidate_with_concurrency("acct_idle", 4, 6),
|
||||
];
|
||||
candidates[0].signals.quota_remaining_rank = Some(10_000);
|
||||
candidates[1].signals.quota_remaining_rank = Some(9_000);
|
||||
for candidate in &mut candidates {
|
||||
candidate.signals.recent_requests = 30;
|
||||
}
|
||||
let mut context = context(RotationStrategy::Smart);
|
||||
for quota_weight in [0.0, 0.8] {
|
||||
context.policy = context.policy.with_smart_scheduling(
|
||||
SmartSchedulingConfig::new([1.0, quota_weight, 0.0, 0.0, 0.0, 0.0]).unwrap(),
|
||||
);
|
||||
let selected = AccountSelector.select(&candidates, &context).unwrap();
|
||||
assert_eq!(selected.candidate().account.id().as_str(), "acct_idle");
|
||||
if quota_weight == 0.0 {
|
||||
let trace = gateway_core::diagnostics::TraceContext::new("req_negative_load");
|
||||
trace.account_selection(&candidates, &context, Some(&selected));
|
||||
assert!(
|
||||
trace.snapshot().unwrap()["events"][0]["data"]["candidates"][0]["smartScore"]
|
||||
.as_f64()
|
||||
.unwrap()
|
||||
< 0.0
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_load_remains_effective_without_a_concurrency_limit() {
|
||||
let mut candidates = [
|
||||
super::candidate("acct_hot", 0, None),
|
||||
super::candidate("acct_cool", 1, None),
|
||||
];
|
||||
candidates[0].signals.recent_requests = u64::MAX;
|
||||
let mut context = context(RotationStrategy::Smart);
|
||||
context.policy = gateway_core::account::AccountSelectionPolicy::new(
|
||||
RotationStrategy::Smart,
|
||||
gateway_core::account::AccountConcurrency::new(0),
|
||||
);
|
||||
let selected = AccountSelector.select(&candidates, &context).unwrap();
|
||||
assert_eq!(selected.candidate().account.id().as_str(), "acct_cool");
|
||||
}
|
||||
|
||||
+3
-2
@@ -2682,8 +2682,9 @@ Chat Completions 和 Responses 通过原生 OpenAI Provider 选号时,缺少
|
||||
缺少必填系数、系数为 `null`、未知字段及无效数值均返回 `422`,不写入配置。读取设置额外返回只读
|
||||
`smartSchedulingDefaults`,内容为上述默认对象,用于恢复默认与自定义状态比较,不可提交到更新接口
|
||||
|
||||
`loadWeight` 控制当前并发空闲比例的评分权重,大于 `0` 时,还会在近似最优候选中优先选择同模型最近 60 秒请求较少的账号,
|
||||
计数与最小值相差不超过 1 次的账号继续轮换。窗口按秒滚动,选号读取内存,后台异步保存短期快照,重启恢复仍在窗口内的计数;快照缺失或恢复失败时重新积累。可用会话绑定优先于该比较
|
||||
`loadWeight` 将当前并发与同模型近期请求量计入总分。负载分为 `1 - 当前并发占用比例 - R / (10 + R)`,其中 `R` 为同模型最近 60 秒请求数。
|
||||
两类压力独立扣分,负载分可为负数;每分钟 10 次请求对应 `0.5` 的近期压力,不是 RPM 限额。系数为 `0` 时并发负载和近期请求量均不参与评分,并发上限检查仍生效。
|
||||
窗口按秒滚动,选号读取内存,后台异步保存短期快照,重启恢复仍在窗口内的计数;快照缺失或恢复失败时重新积累。可用会话绑定优先于评分
|
||||
|
||||
`resetWeight` 控制重置分在总评分中的比重。`resetWindowWeights` 的 `fiveHour`、`weekly`、`monthly` 分别是 5 小时、周和月的相对系数,取值为 0~10,最多一位小数。省略整个字段时三项均为 0。
|
||||
全部为 0 时不计重置分。设为 `{"fiveHour":0,"weekly":1,"monthly":0}` 可只按周重置评分。只计算账号通用窗口,未知、缺失或已过期的周期贡献为零
|
||||
|
||||
@@ -788,7 +788,7 @@ Core 定义、校验并统一拥有评分规则,内置选号与诊断复用同
|
||||
- 智能调度的窗口系数全为 0 时,重置不参与评分及容差计算。额度调度只按独立窗口系数比较重置分,严格选择最高分,不参与近似同分轮换;全零时沿用最早有效重置时间排序。同分继续按并发占用、上次使用时间和账号 ID 决定,资格、权重层和亲和约束保持在策略排序之前
|
||||
- 排队系数只在选择等待队列时结合其他评分和进程内实时等待人数,不影响立即选号分数与容差;系数为零时选最短队列。
|
||||
`ConcurrencyWaitQueue` 在同一锁内读取队长并入队,保持已有位置、同级 FIFO、取消回收、容量和共享等待预算;Provider 传入候选须已限定续写范围
|
||||
- `loadWeight > 0` 时,近似最优集合进一步保留同模型最近 60 秒请求数不超过最小值加 1 的账号,再按稳定顺序轮换;不改变前置资格、权重层和可用亲和绑定
|
||||
- 当前并发与同模型最近 60 秒请求分别形成负载压力,由 `loadWeight` 加权后参与总分,再筛选近似最优集合并轮换;[评分尺度](api.md#智能调度)不代表上游频控阈值。选号、等待队列与诊断复用同一评分函数,前置资格、权重层和可用亲和绑定保持优先
|
||||
- Core 的 `AccountFeedbackStats` 按 Provider、账号和模型维护 60 个秒桶,记录每个 attempt 首次确认发送时的开始时间;响应片段和最终结果不重复累计,明确已发送的失败也计数,未知或未发送不计数。尚未确认发送的在途请求仍由当前并发负载反映
|
||||
- 选号只读取进程内计数,按需推进窗口并定期清理过期键,诊断输出 `recentRequests`。Core 的快照任务由 Store 装配并交给 Host 监督,按 1 秒间隔合并保存到 Redis,快照 TTL 为 120 秒,替换写入不重复累加
|
||||
- 启动接收请求前恢复一次快照,按停机时长剔除超过 60 秒的桶,墙钟回拨时重新积累。恢复失败不阻断启动,后台保存失败由监督器重试,选号继续使用内存;每次存储等待最多 2 秒,关闭时尝试补刷,异常退出可能丢失尚未保存的统计
|
||||
|
||||
Reference in New Issue
Block a user