2026-04-27 16:13:24 +08:00
|
|
|
use aether_scheduler_core::{
|
2026-05-03 01:55:23 +08:00
|
|
|
build_scheduler_affinity_cache_key_for_api_key_id, candidate_affinity_hash, candidate_key,
|
|
|
|
|
matches_affinity_target, SchedulerAffinityTarget,
|
2026-04-05 20:23:16 +08:00
|
|
|
};
|
2026-04-04 01:40:24 +08:00
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
use crate::data::auth::GatewayAuthApiKeySnapshot;
|
2026-04-07 02:50:19 +08:00
|
|
|
use crate::scheduler::affinity::SCHEDULER_AFFINITY_TTL;
|
2026-04-04 01:40:24 +08:00
|
|
|
|
|
|
|
|
use super::{
|
2026-04-07 02:50:19 +08:00
|
|
|
SchedulerMinimalCandidateSelectionCandidate, SchedulerRuntimeState,
|
|
|
|
|
SCHEDULER_AFFINITY_MAX_ENTRIES,
|
2026-04-04 01:40:24 +08:00
|
|
|
};
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
pub(super) fn build_scheduler_affinity_cache_key(
|
2026-04-05 20:23:16 +08:00
|
|
|
auth_snapshot: Option<&GatewayAuthApiKeySnapshot>,
|
2026-03-31 19:19:04 +08:00
|
|
|
api_format: &str,
|
|
|
|
|
global_model_name: &str,
|
|
|
|
|
) -> Option<String> {
|
|
|
|
|
let api_key_id = auth_snapshot
|
|
|
|
|
.map(|snapshot| snapshot.api_key_id.trim())
|
|
|
|
|
.filter(|value| !value.is_empty())?;
|
2026-04-03 14:59:58 +08:00
|
|
|
build_scheduler_affinity_cache_key_for_api_key_id(api_key_id, api_format, global_model_name)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 01:55:23 +08:00
|
|
|
pub(super) fn scheduler_candidate_affinity_hash(
|
|
|
|
|
affinity_key: &str,
|
|
|
|
|
candidate: &SchedulerMinimalCandidateSelectionCandidate,
|
|
|
|
|
) -> u64 {
|
|
|
|
|
candidate_affinity_hash(affinity_key, candidate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn scheduler_candidate_matches_affinity_target(
|
|
|
|
|
candidate: &SchedulerMinimalCandidateSelectionCandidate,
|
|
|
|
|
target: &SchedulerAffinityTarget,
|
|
|
|
|
) -> bool {
|
|
|
|
|
matches_affinity_target(candidate, target)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 19:19:04 +08:00
|
|
|
#[cfg_attr(not(test), allow(dead_code))]
|
|
|
|
|
pub(super) fn remember_scheduler_affinity(
|
|
|
|
|
affinity_cache_key: Option<&str>,
|
2026-04-05 20:23:16 +08:00
|
|
|
state: &(impl SchedulerRuntimeState + ?Sized),
|
2026-04-07 02:50:19 +08:00
|
|
|
candidate: &SchedulerMinimalCandidateSelectionCandidate,
|
2026-03-31 19:19:04 +08:00
|
|
|
) {
|
|
|
|
|
let Some(cache_key) = affinity_cache_key else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2026-05-03 01:55:23 +08:00
|
|
|
let (provider_id, endpoint_id, key_id) = candidate_key(candidate);
|
2026-03-31 19:19:04 +08:00
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
state.remember_scheduler_affinity_target(
|
|
|
|
|
cache_key,
|
2026-03-31 19:19:04 +08:00
|
|
|
SchedulerAffinityTarget {
|
2026-05-03 01:55:23 +08:00
|
|
|
provider_id,
|
|
|
|
|
endpoint_id,
|
|
|
|
|
key_id,
|
2026-03-31 19:19:04 +08:00
|
|
|
},
|
|
|
|
|
SCHEDULER_AFFINITY_TTL,
|
|
|
|
|
SCHEDULER_AFFINITY_MAX_ENTRIES,
|
|
|
|
|
);
|
|
|
|
|
}
|