feat: anti-502 LLM — heartbeat (litellm) + watchdog auto-réparation (llama_server) (#23)

* feat(litellm): heartbeat anti-502 + request_timeout 20s

Cause racine du 502 « 1ʳᵉ demande après une pause » : la connexion keep-alive
LiteLLM↔llama-server devient inactive → llama-server la ferme → LiteLLM garde le
socket mort → la requête suivante part dans le vide → timeout → 502.

- llm-heartbeat : service systemd qui appelle hermes-default toutes les 15s
  (max_tokens:1, /no_think → ~10ms GPU) → la connexion n'est jamais inactive,
  jamais périmée. Logge les échecs → sert aussi de sonde (vraie génération).
- request_timeout 60→20s : un socket périmé échoue vite, dans la fenêtre où
  num_retries:2 peut rejouer sur une connexion neuve (sinon le client abandonnait
  avant le retry → 502 sec).
- Doc : admin/incidents-llm-gpu.md (fix racine) + README rôle.

⚠️ heartbeat appelle hermes-default en continu → si bascule sur Claude (facturé),
mettre llm_heartbeat_enabled: false. request_timeout global → remonter si Claude.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(llama_server): watchdog auto-réparation du wedge ROCm

Service systemd local sur gpu-01 qui sonde une vraie génération sur :1234
(pas juste /health, qui ment quand le slot d'inférence est figé). Sur N
échecs consécutifs → systemctl restart llama-server en local (root, sans
SSH/sudo distant). Gère le 503 "Loading model" post-restart sans le compter
comme échec.

Complète le llm-heartbeat (rôle litellm) : le heartbeat empêche la péremption
par inactivité de la connexion ; le watchdog répare le figeage du serveur lui-même.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ALI YESILKAYA 2026-06-19 20:52:29 +02:00 committed by GitHub
parent d6ec83af9d
commit 577d61ebaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 293 additions and 10 deletions

View file

@ -12,6 +12,28 @@ Compile llama.cpp (backend HIP/ROCm) et déploie le service **llama-server GPU**
`llama-embed` servant un modèle d'embedding spécialisé (ex. `nomic-embed-text`) sur
`:1238`, GPU. Télécharge le GGUF depuis `llama_embed_model_url` si absent. Partage le
binaire et le GPU avec `:1234` (modèle ~300 Mo VRAM).
- **Watchdog** (optionnel, `llama_watchdog_enabled`, défaut `true`) : service systemd
`llama-watchdog` qui sonde une **vraie génération** sur `:1234` toutes les
`llama_watchdog_interval` s. Sur `llama_watchdog_failures` échecs consécutifs →
`systemctl restart llama-server` **en local** (tourne en root, pas de SSH). Répare
automatiquement le wedge ROCm où `/health` reste vert mais l'inférence est morte
(cf. `admin/incidents-llm-gpu.md`). Logs : `journalctl -u llama-watchdog`.
## Watchdog — variables
| Variable | Défaut |
|---|---|
| `llama_watchdog_enabled` | `true` |
| `llama_watchdog_port` | `{{ llama_server_port }}` (1234) |
| `llama_watchdog_model` | `{{ llama_model_alias }}` (`qwen3-8b`) |
| `llama_watchdog_interval` | `20` (s entre deux sondes) |
| `llama_watchdog_timeout` | `15` (s max par sonde) |
| `llama_watchdog_failures` | `3` (échecs consécutifs avant restart) |
> Complémentaire au `llm-heartbeat` du rôle `litellm` : le heartbeat empêche la
> *péremption par inactivité* de la connexion LiteLLM↔llama-server ; le watchdog répare le
> *figeage* du llama-server lui-même. Le watchdog gère le `503 "Loading model"` post-restart
> (rechargement VRAM) sans le compter comme échec.
## Variables principales

View file

@ -25,3 +25,15 @@ llama_embed_model_alias: "nomic-embed-text"
llama_embed_ctx_size: 2048
llama_embed_n_gpu_layers: 99
llama_embed_pooling: "mean"
# Watchdog — auto-réparation du llama-server figé (wedge ROCm gfx1031).
# Sonde une VRAIE génération sur :1234 (pas seulement /health, qui ment quand le
# slot d'inférence est bloqué — cf. admin/incidents-llm-gpu.md). Sur N échecs
# consécutifs → `systemctl restart llama-server` en local (tourne en root).
llama_watchdog_enabled: true
llama_watchdog_port: "{{ llama_server_port }}"
llama_watchdog_model: "{{ llama_model_alias }}"
llama_watchdog_service: "llama-server"
llama_watchdog_interval: 20 # s entre deux sondes
llama_watchdog_timeout: 15 # s max par sonde (génération triviale)
llama_watchdog_failures: 3 # échecs consécutifs avant restart

View file

@ -12,3 +12,8 @@
ansible.builtin.systemd:
name: llama-embed
state: restarted
- name: Restart llama-watchdog
ansible.builtin.systemd:
name: llama-watchdog
state: restarted

View file

@ -123,3 +123,31 @@
state: enabled
immediate: true
when: llama_embed_enabled
# --- Watchdog auto-réparation (wedge ROCm) — optionnel ------------------------
- name: Deploy llama-watchdog script
ansible.builtin.template:
src: llama-watchdog.sh.j2
dest: /usr/local/bin/llama-watchdog
mode: '0755'
notify: Restart llama-watchdog
when: llama_watchdog_enabled
- name: Deploy llama-watchdog service
ansible.builtin.template:
src: llama-watchdog.service.j2
dest: /etc/systemd/system/llama-watchdog.service
mode: '0644'
notify:
- Reload systemd
- Restart llama-watchdog
when: llama_watchdog_enabled
- name: Enable llama-watchdog service
ansible.builtin.systemd:
name: llama-watchdog
enabled: true
daemon_reload: true
state: started
when: llama_watchdog_enabled

View file

@ -0,0 +1,15 @@
[Unit]
Description=llama-watchdog (auto-réparation du llama-server figé — ROCm gfx1031)
After=llama-server.service
Wants=llama-server.service
[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/llama-watchdog
Restart=always
RestartSec=10
SyslogIdentifier=llama-watchdog
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,52 @@
#!/usr/bin/env bash
# llama-watchdog — auto-réparation du llama-server figé (instabilité ROCm gfx1031).
#
# Le piège (cf. admin/incidents-llm-gpu.md) : un llama-server wedgé répond encore à
# /health et /v1/models, mais la GÉNÉRATION est morte. On sonde donc par une VRAIE
# génération (max_tokens:1, /no_think). Sur N échecs consécutifs → restart local.
#
# Tourne en root (service systemd) → peut `systemctl restart` en local, sans SSH ni sudo
# distant. Déployé/géré par le rôle Ansible `llama_server`.
set -u
URL="http://127.0.0.1:{{ llama_watchdog_port }}/v1/chat/completions"
MODEL="{{ llama_watchdog_model }}"
SERVICE="{{ llama_watchdog_service }}"
INTERVAL="{{ llama_watchdog_interval }}"
TIMEOUT="{{ llama_watchdog_timeout }}"
THRESHOLD="{{ llama_watchdog_failures }}"
payload="{\"model\":\"${MODEL}\",\"messages\":[{\"role\":\"user\",\"content\":\"ping /no_think\"}],\"max_tokens\":1}"
probe() { # echo le code HTTP ; "000" si pas de réponse (timeout/refus)
curl -s -m "$TIMEOUT" -o /tmp/llama-watchdog.body -w '%{http_code}' \
"$URL" -H 'Content-Type: application/json' -d "$payload" 2>/dev/null || echo "000"
}
fails=0
while true; do
code=$(probe)
if [ "$code" = "200" ]; then
fails=0
elif [ "$code" = "503" ] && grep -q "Loading model" /tmp/llama-watchdog.body 2>/dev/null; then
# Rechargement VRAM (juste après un restart) → ni échec ni succès : on patiente.
:
else
fails=$((fails + 1))
echo "watchdog: probe KO (HTTP ${code}) — ${fails}/${THRESHOLD}"
if [ "$fails" -ge "$THRESHOLD" ]; then
echo "watchdog: ${SERVICE} figé (${THRESHOLD} échecs consécutifs) → restart"
systemctl restart "$SERVICE"
echo "watchdog: restart émis ; attente du rechargement du modèle…"
# Cooldown : attendre que la génération remarche (couvre mort de l'instance + load VRAM).
for _ in $(seq 1 30); do
sleep 5
[ "$(probe)" = "200" ] && { echo "watchdog: ${SERVICE} de nouveau réactif"; break; }
done
fails=0
fi
fi
sleep "$INTERVAL"
done