forked from mtusi2026/mtusi2026lab3
Lab3: fair track-1 database reset, WAL metrics, rolling and clean-clone tooling
This commit is contained in:
Executable
+113
@@ -0,0 +1,113 @@
|
||||
#!/bin/sh
|
||||
# Level-4 rolling replacement under load.
|
||||
#
|
||||
# Builds a second image tag from the same source tree (VERSION=2.0.0, which is
|
||||
# visible as the non-contract X-App-Version header), starts a mixed read/write
|
||||
# probe, and replaces each API container one at a time. The migration command
|
||||
# is re-run once against the new image while old replicas still serve; the
|
||||
# schema is unchanged between these revisions, so old and new processes are
|
||||
# compatible by construction. This is an additive, advisory-locked migration
|
||||
# boundary, not a test of a destructive schema change.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
NEW_IMAGE=${NEW_IMAGE:-mtusi-messenger-lab3-melnikov:rolling}
|
||||
NEW_VERSION=${NEW_VERSION:-2.0.0}
|
||||
REPLICAS=${API_REPLICAS:-2}
|
||||
CPU=${API_CPU_LIMIT:-0.4}
|
||||
MEM=${API_MEM_LIMIT:-256M}
|
||||
GOMEM=${GOMEMLIMIT:-240MiB}
|
||||
PROBE=/tmp/mtusi-lab3-loadgen-rolling
|
||||
OUT=evidence/rolling-update
|
||||
mkdir -p "$OUT"
|
||||
rev=$(git rev-parse HEAD 2>/dev/null || echo unknown)
|
||||
compose="./scripts/compose.sh --env-file .env"
|
||||
|
||||
echo "rolling: building $NEW_IMAGE from $rev (version $NEW_VERSION)"
|
||||
GIT_REVISION="$rev" API_IMAGE="$NEW_IMAGE" docker build \
|
||||
--build-arg VERSION="$NEW_VERSION" --build-arg REVISION="$rev" \
|
||||
-t "$NEW_IMAGE" . >/dev/null
|
||||
|
||||
go build -o "$PROBE" ./cmd/loadgen
|
||||
|
||||
# Ensure the stand starts with exactly the old replicas and the requested limits.
|
||||
# Remove stale API containers first: a stopped container from an earlier
|
||||
# failover would otherwise be counted or started by compose.
|
||||
for container in $(docker ps -aq --filter label=com.docker.compose.service=api \
|
||||
--filter label=com.docker.compose.project=mtusi-lab3-melnikov); do
|
||||
docker rm -f "$container" >/dev/null 2>&1 || true
|
||||
done
|
||||
API_IMAGE=mtusi-messenger-lab3-melnikov:local GIT_REVISION="$rev" \
|
||||
API_REPLICAS="$REPLICAS" API_CPU_LIMIT="$CPU" API_MEM_LIMIT="$MEM" GOMEMLIMIT="$GOMEM" \
|
||||
$compose up -d --no-deps api >/dev/null
|
||||
./scripts/wait-ready.sh >/dev/null
|
||||
|
||||
before=$(docker ps -aq --filter label=com.docker.compose.service=api \
|
||||
--filter label=com.docker.compose.project=mtusi-lab3-melnikov)
|
||||
old_count=$(printf '%s\n' "$before" | grep -c . || true)
|
||||
if [ "$old_count" -ne "$REPLICAS" ]; then
|
||||
echo "rolling: expected $REPLICAS old API containers, found $old_count" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Pick the target containers before load starts.
|
||||
targets=$(printf '%s\n' "$before" | head -n "$REPLICAS")
|
||||
|
||||
echo "rolling: starting mixed probe"
|
||||
"$PROBE" -base-url http://127.0.0.1:8083 -duration 50s -warmup 3s -concurrency 8 \
|
||||
-read-ratio 0.5 -timeout 2s -retry-uncertain \
|
||||
-output "$OUT/loadgen.json" -note "rolling update 1.0.0 -> $NEW_VERSION" \
|
||||
> "$OUT/loadgen.stdout" 2>&1 &
|
||||
probe_pid=$!
|
||||
sleep 5
|
||||
|
||||
# Re-run the one-shot migration under running old replicas. It is a no-op for
|
||||
# this revision, which is exactly the compatibility property being shown.
|
||||
API_IMAGE="$NEW_IMAGE" GIT_REVISION="$rev" $compose run --rm migrate \
|
||||
> "$OUT/migrate.txt" 2>&1 || { echo "rolling: migration failed" >&2; cat "$OUT/migrate.txt" >&2; exit 1; }
|
||||
|
||||
step=0
|
||||
for container in $targets; do
|
||||
step=$((step + 1))
|
||||
hostname=$(docker inspect -f '{{.Config.Hostname}}' "$container")
|
||||
image_before=$(docker inspect -f '{{.Image}}' "$container")
|
||||
echo "rolling: step $step/$REPLICAS stopping $container ($hostname)"
|
||||
docker stop --time 15 "$container" >/dev/null
|
||||
docker rm "$container" >/dev/null
|
||||
API_IMAGE="$NEW_IMAGE" GIT_REVISION="$rev" API_REPLICAS="$REPLICAS" \
|
||||
API_CPU_LIMIT="$CPU" API_MEM_LIMIT="$MEM" GOMEMLIMIT="$GOMEM" \
|
||||
$compose up -d --no-deps --no-recreate api >/dev/null
|
||||
|
||||
attempts=0
|
||||
while [ "$attempts" -lt 90 ]; do
|
||||
current=$(docker ps -q --filter label=com.docker.compose.service=api \
|
||||
--filter label=com.docker.compose.project=mtusi-lab3-melnikov | wc -l | tr -d ' ')
|
||||
healthy=1
|
||||
for check in $(docker ps -q --filter label=com.docker.compose.service=api \
|
||||
--filter label=com.docker.compose.project=mtusi-lab3-melnikov); do
|
||||
status=$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$check")
|
||||
[ "$status" = "healthy" ] || healthy=0
|
||||
done
|
||||
if [ "$current" -eq "$REPLICAS" ] && [ "$healthy" -eq 1 ]; then
|
||||
break
|
||||
fi
|
||||
attempts=$((attempts + 1))
|
||||
sleep 1
|
||||
done
|
||||
if [ "$attempts" -ge 90 ]; then
|
||||
echo "rolling: replacement did not become healthy" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "rolling: step $step complete (old image $image_before removed)"
|
||||
done
|
||||
|
||||
wait "$probe_pid"
|
||||
new_images=$(for container in $(docker ps -q --filter label=com.docker.compose.service=api \
|
||||
--filter label=com.docker.compose.project=mtusi-lab3-melnikov); do
|
||||
docker inspect -f '{{.Image}}' "$container"; done | sort -u)
|
||||
if [ "$(printf '%s\n' "$new_images" | grep -c .)" -ne 1 ]; then
|
||||
echo "rolling: API containers run more than one image after the roll" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "rolling: all $REPLICAS API containers now run $NEW_IMAGE ($new_images)"
|
||||
echo "PASS: rolling replacement completed under load"
|
||||
Reference in New Issue
Block a user