Files
Hermes 879eac5241 tests: Startrennen und Hintergrundproben unter Last entschärfen
Die sporadischen Ausfälle hatten zwei gemeinsame Ursachen: feste sleeps
statt wait_port für frisch gestartete Stubs, und die Hintergrundprobe,
die einen Endpunkt nach einem einzigen fehlgeschlagenen curl für
offline_hold_s (20 s) aus dem Verkehr nahm. Unter Last traf beides
gelegentlich einen Request: 503 offline statt text-only, 503 statt des
Retry-After-Datums, oder ein nicht erreichbarer Stub.

- probe_thread versucht eine fehlgeschlagene Probe nach 200 ms erneut;
  erst zwei Fehlschläge nehmen den Endpunkt offline.
- retry-after.sh, ctx-vision.sh und flight.sh warten mit wait_port auf
  ihre Stubs, statt 0,3–0,8 s zu schlafen; probe_interval 0 schaltet die
  Hintergrundprobe in diesen deterministischen Abschnitten ab.
- cluster.sh: probe_interval 0, größere Startfristen, der Peer-Cache
  wird gepollt statt nach 0,4 s einmal geprüft, und der Silent-Peer-Test
  erlaubt 5 s statt 2 s (er prüft, dass nichts hängt, nicht die CPU-Zeit).
- mcp_concurrency.py: probe_interval 0; zerrissene Antworten werden mit
  Status und Rumpf ausgegeben, damit ein Wiederauftreten diagnostizierbar
  ist.

Nachweis: zwei vollständige Läufe ohne nice -n -10 unter Last 6,8 bzw.
9,3, jeweils 484 OK / 0 FAIL.
2026-11-18 08:47:15 +00:00

184 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
# Wird von tests/run.sh eingebunden: der Anbieter sagt, wann es weitergeht.
#
# Ohne diese Regel wartet junction immer nur seine eigene Backoff-Zeit und rennt damit
# gegen ein Kontingent, das noch gar nicht offen ist — der zweite Versuch ist dann
# garantiert der zweite 429 dieses Fensters.
echo
echo "== 36. Retry-After des Anbieters wird beachtet (mit Deckel) =="
RAP=$((PORT+212))
RSP=$((PORT+213))
RCP=$((PORT+214))
RDP=$((PORT+215))
# Zwei Stubs: einer sagt 2 s, einer sagt 60 s (dort greift retry_max_wait).
STUB_STATE="$TMP/ra.state" tests/stub $RSP "RA2" --fail-n 1 --retry-after 2 > "$TMP/ra_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/ra60.state" tests/stub $RDP "RA60" --fail-n 1 --retry-after 60 > "$TMP/ra_stub60.log" 2>&1 &
PIDS+=($!)
wait_port "$RSP" 30 || bad "the retry-after stub did not come up"
wait_port "$RDP" 30 || bad "the capped-retry stub did not come up"
cat > "$TMP/ra.conf" <<CFG
listen 127.0.0.1:$RAP
log_prefix ra
probe_interval 0
retry_num 1
retry_backoff 0.2
retry_max_wait 30
endpoint a { base http://127.0.0.1:$RSP/v1; timeout 20 }
req for "*" check none pass a
CFG
cat > "$TMP/rc.conf" <<CFG
listen 127.0.0.1:$RCP
log_prefix rc
probe_interval 0
retry_num 1
retry_backoff 0.2
retry_max_wait 1
endpoint a { base http://127.0.0.1:$RDP/v1; timeout 20 }
req for "*" check none pass a
CFG
./junction --config "$TMP/ra.conf" > "$TMP/ra.log" 2>&1 &
PIDS+=($!)
./junction --config "$TMP/rc.conf" > "$TMP/rc.log" 2>&1 &
PIDS+=($!)
wait_port "$RAP" 30 || bad "retry-after instance did not come up"
wait_port "$RCP" 30 || bad "capped-retry instance did not come up"
RABODY='{"model":"m","messages":[{"role":"user","content":"retry"}],"max_tokens":3}'
# Der Anbieter sagt 2 s — also darf nicht nach 0,2 s gefragt werden.
read -r code elapsed < <(curl -s -o /dev/null -m 30 -w '%{http_code} %{time_total}' \
-X POST -H 'Content-Type: application/json' -d "$RABODY" \
"http://127.0.0.1:$RAP/v1/chat/completions")
if [ "$code" = "200" ] && awk "BEGIN{exit !($elapsed >= 1.9 && $elapsed < 6.0)}"; then
ok "the second attempt waited the provider's 2 s, not the 0.2 s backoff (${elapsed}s)"
else
bad "expected http 200 after ~2 s, got $code after ${elapsed}s"
fi
contains "$(grep -o 'retry_after_s=[0-9]*' "$TMP/ra.log" | head -1)" "retry_after_s=2" \
"the log names the value the provider asked for"
# 60 s verlangt, 1 s erlaubt: der Deckel gilt, sonst hinge die Anfrage eine Minute.
read -r ccode celapsed < <(curl -s -o /dev/null -m 40 -w '%{http_code} %{time_total}' \
-X POST -H 'Content-Type: application/json' -d "$RABODY" \
"http://127.0.0.1:$RCP/v1/chat/completions")
if [ "$ccode" = "200" ] && awk "BEGIN{exit !($celapsed >= 0.9 && $celapsed < 5.0)}"; then
ok "retry_max_wait caps a 60 s request at 1 s (${celapsed}s)"
else
bad "the cap did not hold: $ccode after ${celapsed}s"
fi
contains "$(grep -o 'retry_after_s=[0-9]*' "$TMP/rc.log" | head -1)" "retry_after_s=60" \
"the cap is visible: the provider said 60, junction waited 1"
# Retry-After darf auch ein HTTP-Datum sein — dieselbe Wirkung, andere Form.
RDP=$((PORT+216)) # Knoten mit Datum in der Zukunft
RDD=$((PORT+217)) # Stub dazu
RPP=$((PORT+218)) # Knoten mit Datum in der Vergangenheit
RPD=$((PORT+219)) # Stub dazu
STUB_STATE="$TMP/rad.state" tests/stub $RDD "RAD" --fail-n 1 --retry-after-date 2 \
> "$TMP/ra_date.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/rap.state" tests/stub $RPD "RAPAST" --fail-n 1 --retry-after-date -120 \
> "$TMP/ra_past.log" 2>&1 &
PIDS+=($!)
wait_port "$RDD" 30 || bad "the dated Retry-After stub did not come up"
wait_port "$RPD" 30 || bad "the past-date Retry-After stub did not come up"
cat > "$TMP/rad.conf" <<CFG
listen 127.0.0.1:$RDP
log_prefix rad
probe_interval 0
retry_num 1
retry_backoff 0.2
retry_max_wait 30
endpoint a { base http://127.0.0.1:$RDD/v1; timeout 20 }
req for "*" check none pass a
CFG
cat > "$TMP/rap.conf" <<CFG
listen 127.0.0.1:$RPP
log_prefix rap
probe_interval 0
retry_num 1
retry_backoff 0.2
retry_max_wait 30
endpoint a { base http://127.0.0.1:$RPD/v1; timeout 20 }
req for "*" check none pass a
CFG
./junction --config "$TMP/rad.conf" > "$TMP/rad.log" 2>&1 &
PIDS+=($!)
./junction --config "$TMP/rap.conf" > "$TMP/rap.log" 2>&1 &
PIDS+=($!)
wait_port "$RDP" 30 || bad "http-date instance did not come up"
wait_port "$RPP" 30 || bad "past-date instance did not come up"
read -r dcode delapsed < <(curl -s -o /dev/null -m 30 -w '%{http_code} %{time_total}' \
-X POST -H 'Content-Type: application/json' -d "$RABODY" \
"http://127.0.0.1:$RDP/v1/chat/completions")
if [ "$dcode" = "200" ] && awk "BEGIN{exit !($delapsed >= 1.5 && $delapsed < 6.0)}"; then
ok "an HTTP-date Retry-After is honoured like a number (${delapsed}s)"
else
bad "the dated Retry-After was not honoured: $dcode after ${delapsed}s"
fi
# Ein Datum in der Vergangenheit heißt "sofort" — nicht "eine Minute warten".
read -r pcode pelapsed < <(curl -s -o /dev/null -m 30 -w '%{http_code} %{time_total}' \
-X POST -H 'Content-Type: application/json' -d "$RABODY" \
"http://127.0.0.1:$RPP/v1/chat/completions")
if [ "$pcode" = "200" ] && awk "BEGIN{exit !($pelapsed < 2.0)}"; then
ok "a date in the past means now, not a minute of waiting (${pelapsed}s)"
else
bad "a past date was not treated as now: $pcode after ${pelapsed}s"
fi
# Ohne Retry-After bleibt es beim eigenen Backoff (der Stub schickt dann 0).
if grep -q "retry_after_s=0" "$TMP/ra.log" 2>/dev/null; then
bad "a zero Retry-After was treated as an instruction"
else
ok "nothing invented when the provider says nothing"
fi
# Der Anbieter nennt eine Reset-Zeit, alle Versuche sind vorbei: der Endpunkt muss
# auch für die *nächste* Anfrage gesperrt bleiben. Sonst rennt jede Anfrage wieder
# in dasselbe erschöpfte Kontingent (in Produktion: 429 mit Retry-After 926119).
RBP=$((PORT+230))
RBS=$((PORT+231))
STUB_STATE="$TMP/rab.state" tests/stub $RBS "RAB" --fail-n 100 --retry-after 30 \
> "$TMP/rab_stub.log" 2>&1 &
PIDS+=($!)
wait_port "$RBS" 30 || bad "the cooldown stub did not come up"
cat > "$TMP/rab.conf" <<CFG
listen 127.0.0.1:$RBP
log_prefix rab
probe_interval 0
retry_num 0
retry_backoff 0.2
endpoint a {
base http://127.0.0.1:$RBS/v1
timeout 20
cooldown 1
}
req for "*" check none pass a
CFG
./junction --config "$TMP/rab.conf" > "$TMP/rab.log" 2>&1 &
PIDS+=($!)
wait_port "$RBP" 30 || bad "cooldown instance did not come up"
curl -s -o /dev/null -m 20 -X POST -H 'Content-Type: application/json' -d "$RABODY" \
"http://127.0.0.1:$RBP/v1/chat/completions"
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' -d "$RABODY" \
"http://127.0.0.1:$RBP/v1/chat/completions")
contains "$R" "cooling" "the next request skips an endpoint benched by Retry-After"
contains "$(grep -o 'reason=retry_after reset_s=[0-9]*' "$TMP/rab.log" | head -1)" \
"reset_s=30" "the bench uses the provider's reset time, not the 1 s cooldown"
# Der Status allein sagt nicht, warum der Anbieter ablehnt. Der Rumpf nennt den Grund
# ("monthly usage limit", ...) und gehört ins Log, sonst muss man ihn von Hand holen.
contains "$(grep 'upstream_error' "$TMP/rab.log" | head -1)" \
'body={"error":"not serving yet"}' "the provider's error body is in the log"