mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 09:50:21 +08:00
refactor: 抽离 AI pipeline 与调度共享能力逻辑
This commit is contained in:
@@ -49,6 +49,7 @@ pub(super) async fn build_admin_monitoring_cache_stats_response(
|
||||
"affinity_stats": {
|
||||
"storage_type": snapshot.storage_type,
|
||||
"total_affinities": snapshot.total_affinities,
|
||||
"active_affinities": snapshot.total_affinities,
|
||||
"cache_hits": snapshot.cache_hits,
|
||||
"cache_misses": snapshot.cache_misses,
|
||||
"cache_hit_rate": snapshot.cache_hit_rate,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use super::cache_types::AdminMonitoringCacheAffinityRecord;
|
||||
use crate::cache::SchedulerAffinityTarget;
|
||||
use crate::handlers::admin::request::AdminAppState;
|
||||
use crate::GatewayError;
|
||||
use std::time::Duration;
|
||||
|
||||
fn parse_admin_monitoring_cache_affinity_key(raw_key: &str) -> Option<(String, String, String)> {
|
||||
let parts = raw_key.split(':').collect::<Vec<_>>();
|
||||
@@ -25,6 +27,48 @@ fn parse_admin_monitoring_cache_affinity_key(raw_key: &str) -> Option<(String, S
|
||||
Some((affinity_key.to_string(), api_format, model_name))
|
||||
}
|
||||
|
||||
fn parse_admin_monitoring_scheduler_affinity_key(
|
||||
raw_key: &str,
|
||||
) -> Option<(String, String, String)> {
|
||||
let parts = raw_key.split(':').collect::<Vec<_>>();
|
||||
let start = parts
|
||||
.iter()
|
||||
.position(|segment| *segment == "scheduler_affinity")?;
|
||||
let affinity_key = parts.get(start + 1)?.trim();
|
||||
if affinity_key.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let remaining = parts.get(start + 2..)?;
|
||||
if remaining.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (api_format, model_name_parts) = if remaining.len() == 2 {
|
||||
(remaining[0].trim().to_string(), &remaining[1..])
|
||||
} else {
|
||||
(
|
||||
format!("{}:{}", remaining[0].trim(), remaining[1].trim()),
|
||||
&remaining[2..],
|
||||
)
|
||||
};
|
||||
if api_format.trim().is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let model_name = model_name_parts
|
||||
.iter()
|
||||
.map(|segment| segment.trim())
|
||||
.filter(|segment| !segment.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join(":");
|
||||
if model_name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some((affinity_key.to_string(), api_format, model_name))
|
||||
}
|
||||
|
||||
pub(super) fn admin_monitoring_scheduler_affinity_cache_key(
|
||||
record: &AdminMonitoringCacheAffinityRecord,
|
||||
) -> Option<String> {
|
||||
@@ -92,6 +136,93 @@ pub(super) fn admin_monitoring_cache_affinity_record(
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn admin_monitoring_scheduler_affinity_record(
|
||||
cache_key: &str,
|
||||
target: &SchedulerAffinityTarget,
|
||||
age: Duration,
|
||||
ttl: Duration,
|
||||
now_unix_secs: u64,
|
||||
) -> Option<AdminMonitoringCacheAffinityRecord> {
|
||||
let (affinity_key, api_format, model_name) =
|
||||
parse_admin_monitoring_scheduler_affinity_key(cache_key)?;
|
||||
let age_secs = age.as_secs();
|
||||
let created_at = now_unix_secs.saturating_sub(age_secs);
|
||||
let expire_at = created_at.saturating_add(ttl.as_secs());
|
||||
|
||||
Some(AdminMonitoringCacheAffinityRecord {
|
||||
raw_key: cache_key.to_string(),
|
||||
affinity_key,
|
||||
api_format,
|
||||
model_name,
|
||||
provider_id: Some(target.provider_id.clone()),
|
||||
endpoint_id: Some(target.endpoint_id.clone()),
|
||||
key_id: Some(target.key_id.clone()),
|
||||
created_at: Some(serde_json::json!(created_at)),
|
||||
expire_at: Some(serde_json::json!(expire_at)),
|
||||
request_count: 0,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn admin_monitoring_scheduler_affinity_record_from_raw(
|
||||
raw_key: &str,
|
||||
raw_value: &str,
|
||||
) -> Option<AdminMonitoringCacheAffinityRecord> {
|
||||
let payload = serde_json::from_str::<serde_json::Value>(raw_value).ok()?;
|
||||
let object = payload.as_object()?;
|
||||
let (affinity_key, parsed_api_format, parsed_model_name) =
|
||||
parse_admin_monitoring_scheduler_affinity_key(raw_key)?;
|
||||
let api_format = object
|
||||
.get("api_format")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or(parsed_api_format.as_str())
|
||||
.to_string();
|
||||
let model_name = object
|
||||
.get("model_name")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or(parsed_model_name.as_str())
|
||||
.to_string();
|
||||
let request_count = object
|
||||
.get("request_count")
|
||||
.and_then(|value| {
|
||||
value
|
||||
.as_u64()
|
||||
.or_else(|| value.as_i64().and_then(|number| u64::try_from(number).ok()))
|
||||
})
|
||||
.unwrap_or(0);
|
||||
|
||||
Some(AdminMonitoringCacheAffinityRecord {
|
||||
raw_key: raw_key.to_string(),
|
||||
affinity_key,
|
||||
api_format,
|
||||
model_name,
|
||||
provider_id: object
|
||||
.get("provider_id")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(ToOwned::to_owned),
|
||||
endpoint_id: object
|
||||
.get("endpoint_id")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(ToOwned::to_owned),
|
||||
key_id: object
|
||||
.get("key_id")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(ToOwned::to_owned),
|
||||
created_at: object.get("created_at").cloned(),
|
||||
expire_at: object.get("expire_at").cloned(),
|
||||
request_count,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn admin_monitoring_cache_affinity_record_identity(
|
||||
record: &AdminMonitoringCacheAffinityRecord,
|
||||
) -> String {
|
||||
admin_monitoring_scheduler_affinity_cache_key(record).unwrap_or_else(|| record.raw_key.clone())
|
||||
}
|
||||
|
||||
pub(super) fn clear_admin_monitoring_scheduler_affinity_entries(
|
||||
state: &AdminAppState<'_>,
|
||||
records: &[AdminMonitoringCacheAffinityRecord],
|
||||
|
||||
@@ -8,6 +8,7 @@ use super::super::cache_route_helpers::{
|
||||
admin_monitoring_cache_affinity_unavailable_response,
|
||||
};
|
||||
use super::super::cache_store::{
|
||||
admin_monitoring_has_runtime_scheduler_affinity_entries,
|
||||
list_admin_monitoring_cache_affinity_records_by_affinity_keys,
|
||||
load_admin_monitoring_cache_affinity_entries_for_tests,
|
||||
};
|
||||
@@ -33,6 +34,7 @@ pub(in super::super) async fn build_admin_monitoring_cache_affinity_delete_respo
|
||||
|
||||
if state.redis_kv_runner().is_none()
|
||||
&& load_admin_monitoring_cache_affinity_entries_for_tests(state).is_empty()
|
||||
&& !admin_monitoring_has_runtime_scheduler_affinity_entries(state)
|
||||
{
|
||||
return Ok(admin_monitoring_cache_affinity_unavailable_response());
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use super::super::cache_route_helpers::{
|
||||
admin_monitoring_cache_users_user_identifier_from_path,
|
||||
};
|
||||
use super::super::cache_store::{
|
||||
admin_monitoring_has_runtime_scheduler_affinity_entries,
|
||||
list_admin_monitoring_cache_affinity_records_by_affinity_keys,
|
||||
load_admin_monitoring_cache_affinity_entries_for_tests,
|
||||
};
|
||||
@@ -37,6 +38,7 @@ pub(in super::super) async fn build_admin_monitoring_cache_users_delete_response
|
||||
|
||||
if state.redis_kv_runner().is_none()
|
||||
&& load_admin_monitoring_cache_affinity_entries_for_tests(state).is_empty()
|
||||
&& !admin_monitoring_has_runtime_scheduler_affinity_entries(state)
|
||||
{
|
||||
return Ok(admin_monitoring_cache_affinity_unavailable_response());
|
||||
}
|
||||
|
||||
@@ -1,41 +1,25 @@
|
||||
use super::cache_affinity::admin_monitoring_cache_affinity_record;
|
||||
use super::cache_affinity::{
|
||||
admin_monitoring_cache_affinity_record, admin_monitoring_cache_affinity_record_identity,
|
||||
admin_monitoring_scheduler_affinity_record,
|
||||
admin_monitoring_scheduler_affinity_record_from_raw,
|
||||
};
|
||||
use super::cache_types::{AdminMonitoringCacheAffinityRecord, AdminMonitoringCacheSnapshot};
|
||||
use crate::handlers::admin::observability::stats::round_to;
|
||||
use crate::handlers::admin::request::AdminAppState;
|
||||
use crate::scheduler::affinity::SCHEDULER_AFFINITY_TTL;
|
||||
use crate::GatewayError;
|
||||
use aether_data_contracts::repository::usage::UsageAuditListQuery;
|
||||
|
||||
async fn count_admin_monitoring_cache_affinity_entries(state: &AdminAppState<'_>) -> usize {
|
||||
let Some(runner) = state.redis_kv_runner() else {
|
||||
return 0;
|
||||
};
|
||||
let mut connection = match runner.client().get_multiplexed_async_connection().await {
|
||||
Ok(value) => value,
|
||||
Err(_) => return 0,
|
||||
};
|
||||
let pattern = runner.keyspace().key("cache_affinity:*");
|
||||
let mut cursor = 0u64;
|
||||
let mut total = 0usize;
|
||||
loop {
|
||||
let (next_cursor, keys) = match redis::cmd("SCAN")
|
||||
.arg(cursor)
|
||||
.arg("MATCH")
|
||||
.arg(&pattern)
|
||||
.arg("COUNT")
|
||||
.arg(200)
|
||||
.query_async::<(u64, Vec<String>)>(&mut connection)
|
||||
.await
|
||||
{
|
||||
Ok(value) => value,
|
||||
Err(_) => return total,
|
||||
};
|
||||
total += keys.len();
|
||||
if next_cursor == 0 {
|
||||
break;
|
||||
}
|
||||
cursor = next_cursor;
|
||||
}
|
||||
total
|
||||
list_admin_monitoring_cache_affinity_records(state)
|
||||
.await
|
||||
.map(|items| items.len())
|
||||
.unwrap_or_else(|_| {
|
||||
state
|
||||
.as_ref()
|
||||
.list_scheduler_affinity_entries(SCHEDULER_AFFINITY_TTL)
|
||||
.len()
|
||||
})
|
||||
}
|
||||
|
||||
async fn scan_admin_monitoring_namespaced_keys(
|
||||
@@ -191,12 +175,27 @@ pub(super) async fn list_admin_monitoring_cache_affinity_records_by_affinity_key
|
||||
list_admin_monitoring_cache_affinity_records_matching(state, Some(affinity_keys)).await
|
||||
}
|
||||
|
||||
pub(super) fn admin_monitoring_has_runtime_scheduler_affinity_entries(
|
||||
state: &AdminAppState<'_>,
|
||||
) -> bool {
|
||||
!state
|
||||
.as_ref()
|
||||
.list_scheduler_affinity_entries(SCHEDULER_AFFINITY_TTL)
|
||||
.is_empty()
|
||||
}
|
||||
|
||||
async fn list_admin_monitoring_cache_affinity_records_matching(
|
||||
state: &AdminAppState<'_>,
|
||||
affinity_keys: Option<&std::collections::BTreeSet<String>>,
|
||||
) -> Result<Vec<AdminMonitoringCacheAffinityRecord>, GatewayError> {
|
||||
let mut records = Vec::new();
|
||||
let mut seen_raw_keys = std::collections::BTreeSet::new();
|
||||
let mut seen_record_ids = std::collections::BTreeSet::new();
|
||||
|
||||
let mut push_record = |record: AdminMonitoringCacheAffinityRecord| {
|
||||
if seen_record_ids.insert(admin_monitoring_cache_affinity_record_identity(&record)) {
|
||||
records.push(record);
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(runner) = state.redis_kv_runner() {
|
||||
let mut connection = runner
|
||||
@@ -209,14 +208,24 @@ async fn list_admin_monitoring_cache_affinity_records_matching(
|
||||
let patterns = affinity_keys
|
||||
.map(|keys| {
|
||||
keys.iter()
|
||||
.map(|affinity_key| {
|
||||
runner
|
||||
.keyspace()
|
||||
.key(&format!("cache_affinity:{affinity_key}:*"))
|
||||
.flat_map(|affinity_key| {
|
||||
[
|
||||
runner
|
||||
.keyspace()
|
||||
.key(&format!("cache_affinity:{affinity_key}:*")),
|
||||
runner
|
||||
.keyspace()
|
||||
.key(&format!("scheduler_affinity:{affinity_key}:*")),
|
||||
]
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_else(|| vec![runner.keyspace().key("cache_affinity:*")]);
|
||||
.unwrap_or_else(|| {
|
||||
vec![
|
||||
runner.keyspace().key("cache_affinity:*"),
|
||||
runner.keyspace().key("scheduler_affinity:*"),
|
||||
]
|
||||
});
|
||||
|
||||
for pattern in patterns {
|
||||
let mut cursor = 0u64;
|
||||
@@ -246,16 +255,18 @@ async fn list_admin_monitoring_cache_affinity_records_matching(
|
||||
let Some(raw_value) = raw_value else {
|
||||
continue;
|
||||
};
|
||||
let Some(record) = admin_monitoring_cache_affinity_record(&key, &raw_value)
|
||||
else {
|
||||
let record = if key.contains("scheduler_affinity:") {
|
||||
admin_monitoring_scheduler_affinity_record_from_raw(&key, &raw_value)
|
||||
} else {
|
||||
admin_monitoring_cache_affinity_record(&key, &raw_value)
|
||||
};
|
||||
let Some(record) = record else {
|
||||
continue;
|
||||
};
|
||||
if affinity_keys.is_some_and(|keys| !keys.contains(&record.affinity_key)) {
|
||||
continue;
|
||||
}
|
||||
if seen_raw_keys.insert(record.raw_key.clone()) {
|
||||
records.push(record);
|
||||
}
|
||||
push_record(record);
|
||||
}
|
||||
}
|
||||
if next_cursor == 0 {
|
||||
@@ -264,7 +275,6 @@ async fn list_admin_monitoring_cache_affinity_records_matching(
|
||||
cursor = next_cursor;
|
||||
}
|
||||
}
|
||||
return Ok(records);
|
||||
}
|
||||
|
||||
for (key, raw_value) in load_admin_monitoring_cache_affinity_entries_for_tests(state) {
|
||||
@@ -274,9 +284,27 @@ async fn list_admin_monitoring_cache_affinity_records_matching(
|
||||
if affinity_keys.is_some_and(|keys| !keys.contains(&record.affinity_key)) {
|
||||
continue;
|
||||
}
|
||||
if seen_raw_keys.insert(record.raw_key.clone()) {
|
||||
records.push(record);
|
||||
push_record(record);
|
||||
}
|
||||
|
||||
let now_unix_secs = chrono::Utc::now().timestamp().max(0) as u64;
|
||||
for entry in state
|
||||
.as_ref()
|
||||
.list_scheduler_affinity_entries(SCHEDULER_AFFINITY_TTL)
|
||||
{
|
||||
let Some(record) = admin_monitoring_scheduler_affinity_record(
|
||||
&entry.cache_key,
|
||||
&entry.target,
|
||||
entry.age,
|
||||
SCHEDULER_AFFINITY_TTL,
|
||||
now_unix_secs,
|
||||
) else {
|
||||
continue;
|
||||
};
|
||||
if affinity_keys.is_some_and(|keys| !keys.contains(&record.affinity_key)) {
|
||||
continue;
|
||||
}
|
||||
push_record(record);
|
||||
}
|
||||
|
||||
Ok(records)
|
||||
|
||||
@@ -161,7 +161,7 @@ pub(super) async fn build_admin_monitoring_resilience_snapshot(
|
||||
.filter(admin_monitoring_usage_is_error)
|
||||
.collect::<Vec<_>>();
|
||||
recent_usage_errors
|
||||
.sort_by(|left, right| right.created_at_unix_secs.cmp(&left.created_at_unix_secs));
|
||||
.sort_by(|left, right| right.created_at_unix_ms.cmp(&left.created_at_unix_ms));
|
||||
|
||||
let total_errors = recent_usage_errors.len();
|
||||
let mut error_breakdown = BTreeMap::<String, usize>::new();
|
||||
@@ -200,7 +200,7 @@ pub(super) async fn build_admin_monitoring_resilience_snapshot(
|
||||
"error_id": item.id,
|
||||
"error_type": error_type,
|
||||
"operation": operation,
|
||||
"timestamp": unix_secs_to_rfc3339(item.created_at_unix_secs),
|
||||
"timestamp": unix_secs_to_rfc3339(item.created_at_unix_ms),
|
||||
"context": {
|
||||
"request_id": item.request_id,
|
||||
"provider_id": item.provider_id,
|
||||
|
||||
@@ -31,7 +31,7 @@ pub(super) fn sample_usage(
|
||||
total_cost_usd: f64,
|
||||
status: &str,
|
||||
status_code: Option<i32>,
|
||||
created_at_unix_secs: i64,
|
||||
created_at_unix_ms: i64,
|
||||
) -> StoredRequestUsageAudit {
|
||||
let is_error = status_code.is_some_and(|value| value >= 400)
|
||||
|| status.trim().eq_ignore_ascii_case("failed")
|
||||
@@ -70,9 +70,9 @@ pub(super) fn sample_usage(
|
||||
Some(30),
|
||||
status.to_string(),
|
||||
"billed".to_string(),
|
||||
created_at_unix_secs,
|
||||
created_at_unix_secs,
|
||||
Some(created_at_unix_secs),
|
||||
created_at_unix_ms,
|
||||
created_at_unix_ms,
|
||||
Some(created_at_unix_ms),
|
||||
)
|
||||
.expect("usage should build")
|
||||
}
|
||||
@@ -82,7 +82,7 @@ pub(super) fn sample_candidate(
|
||||
request_id: &str,
|
||||
candidate_index: i32,
|
||||
status: RequestCandidateStatus,
|
||||
started_at_unix_secs: Option<i64>,
|
||||
started_at_unix_ms: Option<i64>,
|
||||
latency_ms: Option<i32>,
|
||||
status_code: Option<i32>,
|
||||
) -> StoredRequestCandidate {
|
||||
@@ -108,9 +108,9 @@ pub(super) fn sample_candidate(
|
||||
Some(1),
|
||||
None,
|
||||
Some(json!({"cache_1h": true})),
|
||||
100 + i64::from(candidate_index),
|
||||
started_at_unix_secs,
|
||||
started_at_unix_secs.map(|value| value + 1),
|
||||
(100 + i64::from(candidate_index)) * 1_000,
|
||||
started_at_unix_ms.map(|v| v * 1_000),
|
||||
started_at_unix_ms.map(|value| (value + 1) * 1_000),
|
||||
)
|
||||
.expect("candidate should build")
|
||||
}
|
||||
|
||||
@@ -258,6 +258,56 @@ async fn admin_monitoring_resilience_status_returns_local_payload() {
|
||||
assert!(payload["timestamp"].as_str().is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_monitoring_cache_stats_count_runtime_scheduler_affinities() {
|
||||
let state = AppState::new().expect("state should build");
|
||||
let affinity_cache_key =
|
||||
aether_scheduler_core::build_scheduler_affinity_cache_key_for_api_key_id(
|
||||
"user-key-1",
|
||||
"openai:chat",
|
||||
"model-alpha",
|
||||
)
|
||||
.expect("scheduler affinity cache key should build");
|
||||
state.scheduler_affinity_cache.insert(
|
||||
affinity_cache_key,
|
||||
crate::cache::SchedulerAffinityTarget {
|
||||
provider_id: "provider-1".to_string(),
|
||||
endpoint_id: "endpoint-1".to_string(),
|
||||
key_id: "provider-key-1".to_string(),
|
||||
},
|
||||
crate::scheduler::affinity::SCHEDULER_AFFINITY_TTL,
|
||||
128,
|
||||
);
|
||||
|
||||
let response = local_monitoring_response(
|
||||
&state,
|
||||
&request_context(http::Method::GET, "/api/admin/monitoring/cache/stats"),
|
||||
)
|
||||
.await
|
||||
.expect("handler should not error")
|
||||
.expect("monitoring route should be handled locally");
|
||||
|
||||
assert_eq!(response.status(), http::StatusCode::OK);
|
||||
let body = to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read");
|
||||
let payload: serde_json::Value = serde_json::from_slice(&body).expect("json body should parse");
|
||||
assert_eq!(payload["status"], json!("ok"));
|
||||
assert_eq!(payload["data"]["total_affinities"], json!(1));
|
||||
assert_eq!(
|
||||
payload["data"]["affinity_stats"]["total_affinities"],
|
||||
json!(1)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["data"]["affinity_stats"]["active_affinities"],
|
||||
json!(1)
|
||||
);
|
||||
assert_eq!(
|
||||
payload["data"]["affinity_stats"]["storage_type"],
|
||||
json!("memory")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_monitoring_cache_stats_returns_local_payload() {
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
|
||||
@@ -209,6 +209,136 @@ async fn admin_monitoring_cache_affinities_and_affinity_return_local_payload_fro
|
||||
assert_eq!(detail_payload["total_endpoints"], json!(1));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_monitoring_cache_affinities_and_delete_use_runtime_scheduler_affinity_cache() {
|
||||
let user_repository = Arc::new(
|
||||
InMemoryUserReadRepository::seed_auth_users(vec![sample_monitoring_auth_user("user-1")])
|
||||
.with_export_users(vec![sample_monitoring_export_user("user-1")]),
|
||||
);
|
||||
let auth_repository = Arc::new(
|
||||
InMemoryAuthApiKeySnapshotRepository::default().with_export_records(vec![
|
||||
sample_monitoring_export_api_key("user-1", "user-key-1"),
|
||||
]),
|
||||
);
|
||||
let provider_catalog = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![sample_provider()],
|
||||
vec![sample_monitoring_catalog_endpoint()],
|
||||
vec![sample_monitoring_catalog_key()],
|
||||
));
|
||||
let state = AppState::new()
|
||||
.expect("state should build")
|
||||
.with_data_state_for_tests(
|
||||
crate::data::GatewayDataState::with_provider_catalog_reader_for_tests(provider_catalog)
|
||||
.with_user_reader(user_repository)
|
||||
.with_auth_api_key_reader(auth_repository),
|
||||
);
|
||||
let affinity_cache_key =
|
||||
aether_scheduler_core::build_scheduler_affinity_cache_key_for_api_key_id(
|
||||
"user-key-1",
|
||||
"openai:chat",
|
||||
"model-alpha",
|
||||
)
|
||||
.expect("scheduler affinity cache key should build");
|
||||
state.scheduler_affinity_cache.insert(
|
||||
affinity_cache_key.clone(),
|
||||
crate::cache::SchedulerAffinityTarget {
|
||||
provider_id: "provider-1".to_string(),
|
||||
endpoint_id: "endpoint-1".to_string(),
|
||||
key_id: "provider-key-1".to_string(),
|
||||
},
|
||||
crate::scheduler::affinity::SCHEDULER_AFFINITY_TTL,
|
||||
128,
|
||||
);
|
||||
|
||||
let list_context = request_context(
|
||||
http::Method::GET,
|
||||
"/api/admin/monitoring/cache/affinities?keyword=alice&limit=20&offset=0",
|
||||
);
|
||||
let list_response = local_monitoring_response(&state, &list_context)
|
||||
.await
|
||||
.expect("handler should not error")
|
||||
.expect("route should be handled locally");
|
||||
assert_eq!(list_response.status(), http::StatusCode::OK);
|
||||
let list_body = to_bytes(list_response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read");
|
||||
let list_payload: serde_json::Value =
|
||||
serde_json::from_slice(&list_body).expect("json body should parse");
|
||||
assert_eq!(list_payload["status"], json!("ok"));
|
||||
assert_eq!(list_payload["data"]["meta"]["total"], json!(1));
|
||||
assert_eq!(list_payload["data"]["matched_user_id"], json!("user-1"));
|
||||
assert_eq!(
|
||||
list_payload["data"]["items"][0]["affinity_key"],
|
||||
json!("user-key-1")
|
||||
);
|
||||
assert_eq!(
|
||||
list_payload["data"]["items"][0]["api_format"],
|
||||
json!("openai:chat")
|
||||
);
|
||||
assert_eq!(
|
||||
list_payload["data"]["items"][0]["provider_name"],
|
||||
json!("OpenAI")
|
||||
);
|
||||
assert_eq!(
|
||||
list_payload["data"]["items"][0]["endpoint_url"],
|
||||
json!("https://api.openai.example/v1")
|
||||
);
|
||||
assert_eq!(list_payload["data"]["items"][0]["request_count"], json!(0));
|
||||
assert!(list_payload["data"]["items"][0]["expire_at"]
|
||||
.as_u64()
|
||||
.is_some_and(|value| value > 0));
|
||||
|
||||
let detail_context = request_context(
|
||||
http::Method::GET,
|
||||
"/api/admin/monitoring/cache/affinity/alice",
|
||||
);
|
||||
let detail_response = local_monitoring_response(&state, &detail_context)
|
||||
.await
|
||||
.expect("handler should not error")
|
||||
.expect("route should be handled locally");
|
||||
assert_eq!(detail_response.status(), http::StatusCode::OK);
|
||||
let detail_body = to_bytes(detail_response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read");
|
||||
let detail_payload: serde_json::Value =
|
||||
serde_json::from_slice(&detail_body).expect("json body should parse");
|
||||
assert_eq!(detail_payload["status"], json!("ok"));
|
||||
assert_eq!(
|
||||
detail_payload["affinities"][0]["api_format"],
|
||||
json!("openai:chat")
|
||||
);
|
||||
assert_eq!(detail_payload["total_endpoints"], json!(1));
|
||||
|
||||
let delete_response = local_monitoring_response(
|
||||
&state,
|
||||
&request_context(
|
||||
http::Method::DELETE,
|
||||
"/api/admin/monitoring/cache/affinity/user-key-1/endpoint-1/model-alpha/openai:chat",
|
||||
),
|
||||
)
|
||||
.await
|
||||
.expect("handler should not error")
|
||||
.expect("route should be handled locally");
|
||||
assert_eq!(delete_response.status(), http::StatusCode::OK);
|
||||
let delete_body = to_bytes(delete_response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read");
|
||||
let delete_payload: serde_json::Value =
|
||||
serde_json::from_slice(&delete_body).expect("json body should parse");
|
||||
assert_eq!(
|
||||
delete_payload["message"],
|
||||
json!("已清除缓存亲和性: Alice Key")
|
||||
);
|
||||
assert_eq!(delete_payload["affinity_key"], json!("user-key-1"));
|
||||
assert_eq!(
|
||||
state.read_scheduler_affinity_target(
|
||||
&affinity_cache_key,
|
||||
crate::scheduler::affinity::SCHEDULER_AFFINITY_TTL,
|
||||
),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_monitoring_cache_users_delete_returns_local_payload_from_test_store() {
|
||||
let user_repository = Arc::new(
|
||||
|
||||
@@ -13,7 +13,7 @@ use axum::{
|
||||
body::Body,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use tracing::warn;
|
||||
use tracing::debug;
|
||||
|
||||
pub(super) async fn build_admin_monitoring_trace_request_response(
|
||||
state: &AdminAppState<'_>,
|
||||
@@ -38,7 +38,7 @@ pub(super) async fn build_admin_monitoring_trace_request_response(
|
||||
.await
|
||||
.map_err(|err| GatewayError::Internal(err.to_string()))?
|
||||
else {
|
||||
warn!(
|
||||
debug!(
|
||||
event_name = "admin_monitoring_request_trace_not_found",
|
||||
log_type = "admin_monitoring",
|
||||
request_id = %short_request_id(request_id.as_str()),
|
||||
|
||||
@@ -20,8 +20,8 @@ pub(in super::super) async fn list_recent_completed_usage_for_cache_affinity(
|
||||
.await?;
|
||||
items.retain(|item| item.status == "completed");
|
||||
items.sort_by(|left, right| {
|
||||
left.created_at_unix_secs
|
||||
.cmp(&right.created_at_unix_secs)
|
||||
left.created_at_unix_ms
|
||||
.cmp(&right.created_at_unix_ms)
|
||||
.then_with(|| left.id.cmp(&right.id))
|
||||
});
|
||||
Ok(items)
|
||||
|
||||
@@ -50,14 +50,14 @@ pub(super) async fn build_admin_usage_cache_affinity_interval_timeline_response(
|
||||
}
|
||||
}
|
||||
|
||||
let mut previous_created_at_unix_secs = None;
|
||||
let mut previous_created_at_unix_ms = None;
|
||||
for item in items {
|
||||
if let Some(previous) = previous_created_at_unix_secs {
|
||||
if let Some(previous) = previous_created_at_unix_ms {
|
||||
let interval_minutes =
|
||||
item.created_at_unix_secs.saturating_sub(previous) as f64 / 60.0;
|
||||
item.created_at_unix_ms.saturating_sub(previous) as f64 / 60.0;
|
||||
if interval_minutes <= 120.0 {
|
||||
let mut point = json!({
|
||||
"x": unix_secs_to_rfc3339(item.created_at_unix_secs),
|
||||
"x": unix_secs_to_rfc3339(item.created_at_unix_ms),
|
||||
"y": ((interval_minutes * 100.0).round()) / 100.0,
|
||||
});
|
||||
if !item.model.trim().is_empty() {
|
||||
@@ -78,7 +78,7 @@ pub(super) async fn build_admin_usage_cache_affinity_interval_timeline_response(
|
||||
.push(point);
|
||||
}
|
||||
}
|
||||
previous_created_at_unix_secs = Some(item.created_at_unix_secs);
|
||||
previous_created_at_unix_ms = Some(item.created_at_unix_ms);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,8 +78,8 @@ pub(super) async fn maybe_build_local_admin_usage_summary_response(
|
||||
.collect();
|
||||
items.sort_by(|left, right| {
|
||||
right
|
||||
.created_at_unix_secs
|
||||
.cmp(&left.created_at_unix_secs)
|
||||
.created_at_unix_ms
|
||||
.cmp(&left.created_at_unix_ms)
|
||||
.then_with(|| left.id.cmp(&right.id))
|
||||
});
|
||||
if requested_ids.is_none() && items.len() > 50 {
|
||||
@@ -147,8 +147,8 @@ pub(super) async fn maybe_build_local_admin_usage_summary_response(
|
||||
});
|
||||
usage.sort_by(|left, right| {
|
||||
right
|
||||
.created_at_unix_secs
|
||||
.cmp(&left.created_at_unix_secs)
|
||||
.created_at_unix_ms
|
||||
.cmp(&left.created_at_unix_ms)
|
||||
.then_with(|| left.id.cmp(&right.id))
|
||||
});
|
||||
let total = usage.len();
|
||||
|
||||
Reference in New Issue
Block a user