feat: MLX /status API 통합 — GPU 무접촉 부하 측정
- model_adapter.status_check(): GET /status, 미지원 백엔드는 None - backend_registry.get_load_status(): /status API 1순위, hybrid fallback - active_jobs >=4 매우 바쁨, >=2 바쁨, =1 보통, =0 여유 - source: status_api | hybrid 표시 - worker._build_system_status(): source별 분기, active_jobs/total_requests 표시 - hybrid 경로의 self-job 제외 (active > 1) - health loop에서 status도 같이 캐시 (지원 백엔드만) Mac mini /opt/mlx-proxy.py에 active_jobs 카운터 + /status 엔드포인트 추가됨 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -72,6 +72,8 @@ class BackendRegistry:
|
||||
self._sample_count: dict[str, int] = {}
|
||||
self._inference_latency: dict[str, float | None] = {}
|
||||
self._inference_latency_at: dict[str, float] = {}
|
||||
self._status_cache: dict[str, dict | None] = {}
|
||||
self._status_cached_at: dict[str, float] = {}
|
||||
self._measure_lock = asyncio.Lock()
|
||||
self._health_task: asyncio.Task | None = None
|
||||
|
||||
@@ -118,6 +120,10 @@ class BackendRegistry:
|
||||
self._latency[role] = elapsed
|
||||
if healthy:
|
||||
self._update_baseline(role, elapsed)
|
||||
# /status 같이 캐시 (지원하는 백엔드만)
|
||||
status_data = await adapter.status_check()
|
||||
self._status_cache[role] = status_data
|
||||
self._status_cached_at[role] = time.time()
|
||||
if prev != healthy:
|
||||
status = "UP" if healthy else "DOWN"
|
||||
logger.warning("%s (%s) → %s (%.0fms)", adapter.name, role, status, elapsed)
|
||||
@@ -150,9 +156,46 @@ class BackendRegistry:
|
||||
return None
|
||||
|
||||
async def get_load_status(self, role: str, force_measure: bool = False) -> dict:
|
||||
"""role의 부하 상태 판단. inference latency는 캐시 또는 조건부 측정."""
|
||||
"""role의 부하 상태 판단. /status API 우선, 없으면 hybrid (health baseline + 조건부 ping)."""
|
||||
health_latency = self._latency.get(role, 0)
|
||||
baseline = self._health_baseline.get(role, 50)
|
||||
adapter = self.classifier if role == "classifier" else self.reasoner
|
||||
|
||||
# === 1순위: /status API (GPU 무접촉) ===
|
||||
# force_measure 시 fresh 호출, 아니면 캐시 사용
|
||||
status_data = None
|
||||
if force_measure and adapter:
|
||||
status_data = await adapter.status_check()
|
||||
if status_data:
|
||||
self._status_cache[role] = status_data
|
||||
self._status_cached_at[role] = time.time()
|
||||
else:
|
||||
status_data = self._status_cache.get(role)
|
||||
|
||||
if status_data is not None:
|
||||
active_jobs = status_data.get("active_jobs", 0)
|
||||
# 부드러운 임계치
|
||||
if active_jobs >= 4:
|
||||
load = "매우 바쁨"
|
||||
elif active_jobs >= 2:
|
||||
load = "바쁨"
|
||||
elif active_jobs == 1:
|
||||
load = "보통"
|
||||
else:
|
||||
load = "여유"
|
||||
|
||||
return {
|
||||
"load": load,
|
||||
"source": "status_api",
|
||||
"active_jobs": active_jobs,
|
||||
"total_requests": status_data.get("total_requests", 0),
|
||||
"health_ms": health_latency,
|
||||
"health_baseline_ms": round(baseline, 1),
|
||||
"inference_ms": None,
|
||||
"measured": False,
|
||||
}
|
||||
|
||||
# === 2순위: hybrid fallback (status 미지원 백엔드) ===
|
||||
cached_inference = self._inference_latency.get(role)
|
||||
cached_at = self._inference_latency_at.get(role, 0)
|
||||
|
||||
@@ -160,7 +203,6 @@ class BackendRegistry:
|
||||
cache_valid = (now - cached_at) < 30
|
||||
cooldown_active = (now - cached_at) < 10
|
||||
|
||||
# 조건부 측정 (상대값 + 쿨다운)
|
||||
should_measure = False
|
||||
if force_measure and not cooldown_active:
|
||||
should_measure = True
|
||||
@@ -173,7 +215,6 @@ class BackendRegistry:
|
||||
else:
|
||||
inference_latency = cached_inference if cache_valid else None
|
||||
|
||||
# 부하 판단 (inference 우선, health는 fallback)
|
||||
if inference_latency is not None and inference_latency > 8000:
|
||||
load = "매우 바쁨"
|
||||
elif inference_latency is not None and inference_latency > 4000:
|
||||
@@ -185,20 +226,23 @@ class BackendRegistry:
|
||||
else:
|
||||
load = "여유"
|
||||
|
||||
# local queue로 보조 판단 (한 단계 상향)
|
||||
# local queue 보조 판단 (자기 자신 제외 — active > 1)
|
||||
try:
|
||||
from services import job_queue as jq_module
|
||||
if jq_module.job_queue:
|
||||
active = jq_module.job_queue.stats.get("active", 0)
|
||||
if active > 0 and load == "보통":
|
||||
if active > 1 and load == "보통":
|
||||
load = "바쁨"
|
||||
elif active > 0 and load == "여유":
|
||||
elif active > 1 and load == "여유":
|
||||
load = "보통"
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {
|
||||
"load": load,
|
||||
"source": "hybrid",
|
||||
"active_jobs": None,
|
||||
"total_requests": None,
|
||||
"health_ms": health_latency,
|
||||
"health_baseline_ms": round(baseline, 1),
|
||||
"inference_ms": inference_latency,
|
||||
|
||||
Reference in New Issue
Block a user