fix(scheduling): 负载均衡模式与无亲和键场景统一走随机排序

重构 CandidateSorter.shuffle_keys_by_internal_priority 中同优先级
Key 的排序逻辑,将三分支简化为两分支:
- 随机排序:TTL=0 / 负载均衡模式 / 无 affinity_key
- 哈希确定性排序:缓存亲和模式且有 affinity_key

移除了原先"无 affinity_key 时按 ID 排序"的冗余分支,新增
对应单元测试覆盖三种场景。
This commit is contained in:
fawney19
2026-03-02 22:36:24 +08:00
parent 8e98eed5c8
commit e26ed8481f
2 changed files with 104 additions and 9 deletions

View File

@@ -299,24 +299,26 @@ class CandidateSorter:
group_keys = priority_groups[priority]
if len(group_keys) > 1:
if use_random:
# TTL=0 模式:使用随机排序实现 Key 轮换
should_randomize = (
use_random
or self._config.scheduling_mode == SchedulingConfig.SCHEDULING_MODE_LOAD_BALANCE
or not affinity_key
)
if should_randomize:
# 随机排序TTL=0 / 负载均衡模式 / 无 affinity_key
shuffled = list(group_keys)
random.shuffle(shuffled)
result.extend(shuffled)
elif affinity_key:
# 正常模式:使用哈希确定性打乱(保持缓存亲和性
else:
# 缓存亲和模式:使用哈希确定性排序should_randomize=False 蕴含 affinity_key 非空
key_scores = []
for key in group_keys:
hash_value = affinity_hash(affinity_key, key.id)
hash_value = affinity_hash(affinity_key, key.id) # type: ignore[arg-type]
key_scores.append((hash_value, key))
# 按哈希值排序
sorted_group = [key for _, key in sorted(key_scores, key=lambda x: x[0])]
result.extend(sorted_group)
else:
# 没有 affinity_key 时按 ID 排序保持稳定性
result.extend(sorted(group_keys, key=lambda k: k.id))
else:
# 单个 Key 直接添加
result.extend(group_keys)