Files
junction/tests/cluster.sh
T
Hermes c8b4e51a16 Cluster: mehrere Instanzen auf einem Port, Drain bei SIGTERM, gemeinsamer Cache
Drei Teile, die zusammengehören, ohne dass ein Dienst dazwischen nötig wäre.

so_reuseport: mehrere Instanzen binden dieselbe Adresse, der Kernel verteilt. Zusammen
mit dem neuen Drain ergibt das ein Rolling Update — die neue Instanz nimmt Verkehr an,
während die alte noch bedient. SIGTERM schließt zuerst den Listener, bedient Laufendes
fertig und beendet dann; ein Request, der nach drain_timeout (Standard 15 s) noch hängt,
wird verworfen und geloggt. Vorher war SIGTERM ein Abbruch mitten in der Antwort.
packaging/junction@.service macht daraus zwei Befehle (start junction@blue, stop
junction@green); auf n95 durchgespielt: zwei Listener auf einem Port, blue loggt
draining/drained, green antwortet weiter mit 200.

Gemeinsamer Cache ohne Redis: jeder Knoten gibt seinen Speicher über einen eigenen Port
aus, cache_peer fragt ihn bei einem lokalen Fehlschlag, ein Fill wird asynchron
weitergereicht (höchstens vier gleichzeitig, ein verworfener Push ist kein Fehler). Der
Transport ist eine kleine HTTP-Teilmenge mit einem Rahmen aus zwei 32-Bit-Längen —
Head und Body so, wie der Knoten sie gesendet hätte. Ein Treffer beim Nachbarn landet
mit der Restlaufzeit lokal, damit der nächste Aufruf nicht wieder fragt. Ohne
cache_secret_env öffnet der Port gar nicht erst.

Zwei echte Funde dabei: SO_REUSEPORT ist unter -D_POSIX_C_SOURCE=200809L nicht
deklariert, ein #ifdef darum kompiliert stillschweigend weg (Symptom: "Address already
in use" trotz so_reuseport on) — der Makefile setzt jetzt -D_DEFAULT_SOURCE. Und ein
Peer-Aufruf ohne Half-Close lässt den Nachbarn auf seinen Empfangs-Timeout warten,
womit jeder Lookup in den 150 ms verhungert und der Mesh wie kaputt aussieht.

--check prüft die Konfiguration jetzt wirklich, ohne zu binden (stand schon als
Behauptung in den Beispielen).

Tests: neuer Abschnitt 33/34 (15 Prüfungen, eigener Portbereich PORT+200…207):
Peer-Treffer ohne Anbieter-Aufruf, Geheimnis erzwungen, fehlendes Geheimnis = kein
Port, stiller Peer kostet genau den Timeout (203 ms gemessen), zwei Instanzen auf einem
Port, Drain, Übergabe ohne Ausfall, exclusiver Port ohne so_reuseport.
make test: PASS, 210 Prüfungen, 154 s.
2026-11-08 09:08:43 +00:00

201 lines
7.8 KiB
Bash
Executable File

#!/bin/bash
# Wird von tests/run.sh eingebunden: mehrere Instanzen, gemeinsamer Cache,
# SO_REUSEPORT und Drain.
#
# Eigener Portbereich (PORT+200 … PORT+207): die kleinen Offsets sind längst vergeben,
# und eine Kollision sieht nicht wie ein Fehler im Code aus, sondern wie ein 501 von
# irgendeinem anderen Test.
echo
echo "== 33. Zwei Knoten, ein Cache (kein Dienst dazwischen) =="
CA=$((PORT+200)) # Knoten A
CB=$((PORT+201)) # Knoten B
PCA=$((PORT+202)) # Cache-Port von A
PCB=$((PORT+203)) # Cache-Port von B
SECRET="clustertest-$RANDOM"
MESH=$(echo '{"model":"m","messages":[{"role":"user","content":"wo war ich"}],"max_tokens":5,"temperature":0}')
# A liefert seinen Cache aus, B fragt A. Beide cachen 300 s.
cat > "$TMP/meshA.conf" <<CFG
listen 127.0.0.1:$CA
log_prefix meshA
cache_ttl 300
cache_entries 50
cache_bytes 2000000
cache tcp 127.0.0.1:$PCA
cache_secret_env J_MESH_SECRET
cache_peer_timeout_ms 300
endpoint a { base http://127.0.0.1:$P_GATED/v1 }
req for "*" pass a
CFG
cat > "$TMP/meshB.conf" <<CFG
listen 127.0.0.1:$CB
log_prefix meshB
cache_ttl 300
cache_entries 50
cache_bytes 2000000
cache tcp 127.0.0.1:$PCB
cache_secret_env J_MESH_SECRET
cache_peer 127.0.0.1:$PCA
cache_peer_timeout_ms 300
endpoint a { base http://127.0.0.1:$P_GATED/v1 }
req for "*" pass a
CFG
J_MESH_SECRET="$SECRET" ./junction --config "$TMP/meshA.conf" > "$TMP/meshA.log" 2>&1 &
PIDS+=($!)
J_MESH_SECRET="$SECRET" ./junction --config "$TMP/meshB.conf" > "$TMP/meshB.log" 2>&1 &
PIDS+=($!)
wait_port "$CA" 10 || bad "node A did not come up"
wait_port "$CB" 10 || bad "node B did not come up"
# Der Secret-Schutz ist keine Zierde: ohne Kopfzeile gibt es nichts.
code=$(curl -s -o /dev/null -w '%{http_code}' -m 5 "http://127.0.0.1:$PCA/cache/stats")
[ "$code" = "401" ] && ok "cache port refuses a caller without the peer secret" \
|| bad "cache port answered $code without the peer secret"
code=$(curl -s -o /dev/null -w '%{http_code}' -m 5 -H "X-Junction-Peer: falsch" \
"http://127.0.0.1:$PCA/cache/stats")
[ "$code" = "401" ] && ok "a wrong peer secret is refused too" \
|| bad "a wrong secret was accepted ($code)"
# Anfrage an A: füllt den Cache und stößt die Weitergabe an B an.
c1=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' \
-d "$MESH" "http://127.0.0.1:$CA/v1/chat/completions" \
| tr -d '\r' | sed -n 's/^[Xx]-[Jj]unction-[Cc]ache: //p')
[ "$c1" = "miss" ] && ok "node A misses and pays the provider" \
|| bad "node A answered cache=$c1 on the first request"
sleep 0.4 # die Weitergabe läuft in einem eigenen Faden
# Dieselbe Anfrage an B: sie darf den Anbieter nicht mehr fragen.
c2=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' \
-d "$MESH" "http://127.0.0.1:$CB/v1/chat/completions" \
| tr -d '\r' | sed -n 's/^[Xx]-[Jj]unction-[Cc]ache: //p')
[ "$c2" = "hit" ] && ok "node B answers the same question from the shared cache" \
|| bad "node B answered cache=$c2 instead of a hit from the peer"
log_has "$TMP/meshB.log" "via=peer" "node B names the peer as the source"
if grep -q "served_by" "$TMP/meshB.log"; then
bad "node B asked the provider although the peer had the answer"
else
ok "node B did not call the provider at all"
fi
# Ein Treffer beim Nachbarn ist auch lokal gelandet — der zweite Aufruf ist billig.
c3=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' \
-d "$MESH" "http://127.0.0.1:$CB/v1/chat/completions" \
| tr -d '\r' | sed -n 's/^[Xx]-[Jj]unction-[Cc]ache: //p')
[ "$c3" = "hit" ] && ok "the peer hit stayed in B's own cache" \
|| bad "the peer hit was not kept locally (cache=$c3)"
# Und der Cache-Port gibt Auskunft, wenn man das Geheimnis hat.
stats=$(curl -s -m 5 -H "X-Junction-Peer: $SECRET" "http://127.0.0.1:$PCA/cache/stats")
contains "$stats" '"entries"' "the cache port reports its own numbers"
# Ein Nachbar, der nicht antwortet, darf eine Anfrage nicht aufhalten: der Zeitablauf
# ist die Obergrenze, und danach geht es zum Anbieter wie ohne Mesh.
cat > "$TMP/meshD.conf" <<CFG
listen 127.0.0.1:$((PORT+207))
log_prefix meshD
cache_ttl 300
cache_secret_env J_MESH_SECRET
cache_peer 127.0.0.1:$P_SLOW
cache_peer_timeout_ms 200
endpoint a { base http://127.0.0.1:$P_GATED/v1 }
req for "*" pass a
CFG
J_MESH_SECRET="$SECRET" ./junction --config "$TMP/meshD.conf" > "$TMP/meshD.log" 2>&1 &
PIDS+=($!)
wait_port "$((PORT+207))" 10 || bad "node D did not come up"
read -r dcode dtime < <(curl -s -o /dev/null -m 10 -w '%{http_code} %{time_total}' \
-X POST -H 'Content-Type: application/json' -d "$MESH" \
"http://127.0.0.1:$((PORT+207))/v1/chat/completions")
if [ "$dcode" = "200" ] && awk "BEGIN{exit !($dtime < 2.0)}"; then
ok "a silent peer costs only the timeout ($dtime s), the request still succeeds"
else
bad "a silent peer stalled the request (http=$dcode after ${dtime}s)"
fi
# Ein Knoten ohne Geheimnis öffnet den Port gar nicht erst.
cat > "$TMP/meshC.conf" <<CFG
listen 127.0.0.1:$((PORT+204))
log_prefix meshC
cache_ttl 60
cache tcp 127.0.0.1:$((PORT+205))
endpoint a { base http://127.0.0.1:$P_GATED/v1 }
req for "*" pass a
CFG
./junction --config "$TMP/meshC.conf" > "$TMP/meshC.log" 2>&1 &
PIDS+=($!)
sleep 0.6
log_has "$TMP/meshC.log" "disabled: cache tcp without cache secret_env" \
"a peer port without a secret stays closed"
echo
echo "== 34. Rolling Update: zwei Instanzen auf einem Port, Drain bei SIGTERM =="
RP=$((PORT+206))
cat > "$TMP/roll1.conf" <<CFG
listen 127.0.0.1:$RP
log_prefix roll1
so_reuseport on
drain_timeout 5
cache_ttl 60
endpoint a { base http://127.0.0.1:$P_GATED/v1 }
req for "*" pass a
CFG
sed 's/roll1/roll2/' "$TMP/roll1.conf" > "$TMP/roll2.conf"
./junction --config "$TMP/roll1.conf" > "$TMP/roll1.log" 2>&1 &
R1=$!
PIDS+=($R1)
wait_port "$RP" 10 || bad "instance 1 did not come up"
./junction --config "$TMP/roll2.conf" > "$TMP/roll2.log" 2>&1 &
R2=$!
PIDS+=($R2)
# Beide müssen gleichzeitig auf demselben Port hören — das ist SO_REUSEPORT.
both=0 i=0
while [ "$i" -lt 40 ]; do
if grep -q "listening addr=127.0.0.1:$RP" "$TMP/roll2.log" 2>/dev/null; then both=1; break; fi
sleep 0.05; i=$((i+1))
done
if [ "$both" = "1" ]; then
ok "a second instance binds the same port while the first serves"
else
bad "the second instance could not bind the same port"
sed 's/^/ /' "$TMP/roll2.log" | tail -3
fi
# Ein hängender Upstream, damit beim Drain wirklich etwas läuft.
SHORT='{"model":"m","messages":[{"role":"user","content":"drain"}],"max_tokens":5,"temperature":0}'
( curl -s -m 10 -o /dev/null -X POST -H 'Content-Type: application/json' -d "$SHORT" \
"http://127.0.0.1:$RP/v1/chat/completions" ) &
CURLPID=$!
kill -TERM "$R1" 2>/dev/null
i=0
while [ "$i" -lt 60 ]; do kill -0 "$R1" 2>/dev/null || break; sleep 0.05; i=$((i+1)); done
log_has "$TMP/roll1.log" "draining closed_listener" "the retired instance stops accepting"
log_has "$TMP/roll1.log" "drained " "it finishes what was running and exits"
wait "$CURLPID" 2>/dev/null
# Derweil bedient die zweite Instanz weiter — ohne Ausfall.
code=$(curl -s -o /dev/null -w '%{http_code}' -m 10 -X POST -H 'Content-Type: application/json' \
-d "$SHORT" "http://127.0.0.1:$RP/v1/chat/completions")
[ "$code" = "200" ] && ok "the new instance serves during and after the handover" \
|| bad "requests failed after the handover (http=$code)"
# Ein zweiter Start ohne so_reuseport darf nicht mehr binden.
sed 's/so_reuseport on/so_reuseport off/; s/roll1/roll3/' "$TMP/roll1.conf" > "$TMP/roll3.conf"
./junction --config "$TMP/roll3.conf" > "$TMP/roll3.log" 2>&1 &
PIDS+=($!)
sleep 0.6
log_has "$TMP/roll3.log" "Address already in use" \
"without so_reuseport the port stays exclusive"
kill -TERM "$R2" 2>/dev/null