#!/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" < "$TMP/meshB.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" 30 || bad "node A did not come up" wait_port "$CB" 30 || 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" # Die Weitergabe läuft in einem eigenen Faden. Statt einer festen Pause warten # wir, bis B den Eintrag wirklich hat (unter Last kann das länger dauern). c2="" for i in $(seq 1 50); do 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" ] && break sleep 0.1 done [ "$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" < "$TMP/meshD.log" 2>&1 & PIDS+=($!) wait_port "$((PORT+207))" 30 || 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 < 5.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" < "$TMP/meshC.log" 2>&1 & PIDS+=($!) wait_log "$TMP/meshC.log" "disabled: cache tcp without cache secret_env" 10 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" < "$TMP/roll2.conf" ./junction --config "$TMP/roll1.conf" > "$TMP/roll1.log" 2>&1 & R1=$! PIDS+=($R1) wait_port "$RP" 30 || 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+=($!) wait_log "$TMP/roll3.log" "Address already in use" 10 log_has "$TMP/roll3.log" "Address already in use" \ "without so_reuseport the port stays exclusive" kill -TERM "$R2" 2>/dev/null