Files
junction/tests/balancing.sh
T
Hermes 6325eb3bd6 Gewichtung, Balance pro Regel, Verbindungs-Zeitlimit pro Endpunkt
Drei Lücken, die eine echte Kette braucht:

  weight N              Endpunkte sind nicht gleich viel wert. Unter round-robin bekommt
                        ein Ziel so viele Plätze in der Runde, wie es wiegt; unter cost
                        und least-usage zählt sein Verbrauch nur 1/N.
  balance <modus>       in der Regel: eine Kette darf reines Failover wollen, während
                        global round-robin gilt.
  connect_timeout_ms    pro Endpunkt: ein Ziel hinter einem Tunnel braucht ein anderes
                        Limit als eines im eigenen LAN. 0 = global erben.

Geprüft mit zwei gesunden Stubs: Gewicht 3:1 verteilt 30 zu 10 über 40 Anfragen. Eine
Regel mit "balance first" bleibt bei sechs Anfragen sechsmal auf dem ersten Ziel, obwohl
global round-robin gilt. Ein unerreichbares Ziel mit eigenem Limit von 400 ms wird nach
414 ms übersprungen und die Kette geht weiter — statt 30 s zu warten.

Dazu eine Testkorrektur: die Logprüfung für TLS wartete nicht auf den Logschreiber und
meldete gelegentlich einen Fehler, der keiner war.

80 Prüfungen, keine Fehler.
2026-11-04 22:28:22 +00:00

109 lines
3.2 KiB
Bash

#!/bin/bash
# Wird von tests/run.sh eingebunden: Gewichtung, Balance pro Regel, Zeitlimits.
# Erwartet: $TMP, $PORT, $P_GATED, $P_FAIL, ok/bad/contains, ./junction
echo "== 19. Gewichtung, Balance pro Regel, Verbindungs-Zeitlimit =="
# Für die Verteilung braucht es ZWEI gesunde Ziele. GATED antwortet; ein zweiter Stub
# mit eigenem Namen kommt dazu, weil der vorhandene "FAILING" absichtlich ausfällt und
# dann immer das erste Ziel bedient würde.
BA=$P_GATED
BB=$((PORT+21))
BJ=$((PORT+23))
tests/stub $BB "ZWEIT" > /dev/null 2>&1 &
PIDS+=($!)
sleep 0.4
cat > "$TMP/bal.conf" <<CFG
listen 127.0.0.1:$BJ
connect_timeout_ms 2000
balance round-robin
endpoint stark {
base http://127.0.0.1:$BA/v1
weight 3
timeout 10
}
endpoint schwach {
base http://127.0.0.1:$BB/v1
weight 1
timeout 10
}
# Diese Regel will reines Failover, egal was global eingestellt ist.
req for "starr*" balance first pass stark, schwach
req for "*" pass stark, schwach
CFG
./junction --config "$TMP/bal.conf" > "$TMP/bal.log" 2>&1 &
PIDS+=($!)
sleep 0.8
# 1. Gewichtetes Reihum: 3:1 soll sich in der Verteilung zeigen
A=0; B=0
for i in $(seq 1 40); do
R=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' \
-d '{"model":"x","messages":[{"role":"user","content":"hi"}]}' \
"http://127.0.0.1:$BJ/v1/chat/completions")
case "$R" in
*"served by GATED"*) A=$((A+1));;
*"served by ZWEIT"*) B=$((B+1));;
esac
done
# Ein Ziel mit Gewicht 3 soll deutlich häufiger drankommen als eines mit Gewicht 1.
if [ "$A" -ge 24 ] && [ "$B" -ge 6 ]; then
ok "weighted round-robin: $A zu $B bei Gewicht 3:1"
else
bad "weighted round-robin verteilte $A zu $B, erwartet etwa 30 zu 10"
fi
# 2. balance first in der Regel schlägt das globale round-robin
FIRST=0
for i in $(seq 1 6); do
R=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' \
-d '{"model":"starr-modell","messages":[{"role":"user","content":"hi"}]}' \
"http://127.0.0.1:$BJ/v1/chat/completions")
case "$R" in *"served by GATED"*) FIRST=$((FIRST+1));; esac
done
if [ "$FIRST" -eq 6 ]; then
ok "a rule's own 'balance first' overrides the global round-robin"
else
bad "rule-level balance did not override the global mode ($FIRST/6 on the first target)"
fi
# 3. Verbindungs-Zeitlimit pro Endpunkt: ein unerreichbares Ziel darf die Kette nicht
# aufhalten, sondern muss schnell weiterreichen.
cat > "$TMP/conn.conf" <<CFG
listen 127.0.0.1:$((PORT+24))
connect_timeout_ms 30000
endpoint langsam {
base http://10.255.255.1:9/v1
connect_timeout_ms 400
timeout 30
}
endpoint gesund {
base http://127.0.0.1:$BA/v1
timeout 10
}
req for "*" pass langsam, gesund
CFG
./junction --config "$TMP/conn.conf" > "$TMP/conn.log" 2>&1 &
PIDS+=($!)
sleep 0.8
START=$(date +%s%N)
R=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
-d '{"model":"x","messages":[{"role":"user","content":"hi"}]}' \
"http://127.0.0.1:$((PORT+24))/v1/chat/completions")
END=$(date +%s%N)
MS=$(( (END-START)/1000000 ))
contains "$R" "served by GATED" "the chain moves past an unreachable endpoint"
if [ "$MS" -lt 5000 ]; then
ok "the per-endpoint connect timeout fired after ${MS} ms, not the global 30 s"
else
bad "the per-endpoint connect timeout did not apply (${MS} ms)"
fi