diff --git a/infra/bin/fly-runtime b/infra/bin/fly-runtime index 5b8d10a..5af9b41 100755 --- a/infra/bin/fly-runtime +++ b/infra/bin/fly-runtime @@ -69,6 +69,9 @@ # (05-deploy's refresh of the drop-in) and `--no-probation` (rehearsals) start none. If the # probation cannot be started, `session` falls back: an unguarded cutover is not allowed. # +# `session` also stops a running shadow and its guard (fly-shadow-run stop, no flysim restart) once +# flysim is healthy on the session runtime, so the procedure cannot be done wrong (review N1). +# # Every switch is logged to the journal (tag fly-runtime) and appended to ${RUNTIME_LOG}. set -euo pipefail @@ -90,6 +93,7 @@ FALLBACK_UNIT=fly-runtime-fallback.service SESSION_BIN="${FLY_RELEASE_DIR}/flysim-session" LEGACY_BIN="${FLY_RELEASE_DIR}/flysim" SESSION_DIR=/run/fly/session +SHADOW_RUN_BIN="${FLY_SHADOW_RUN_BIN:-$(dirname "$(readlink -f "$0")")/fly-shadow-run}" PROBATION_BIN="${FLY_PROBATION_BIN:-$(dirname "$(readlink -f "$0")")/fly-runtime-probation}" log() { echo "fly-runtime: $*" >&2; } @@ -101,6 +105,20 @@ probation() { } die() { log "$*"; exit 1; } +# CUT-01 (review N1): once flysim runs the session runtime the shadow and its guard are over. The +# guard would judge the session against the legacy baseline and restart flysim on a trip; the +# session writes no trace. Stops both WITHOUT restarting flysim. Best effort, never fails the switch. +stop_shadow() { + if systemctl is-active --quiet flyshadow.service 2>/dev/null \ + || systemctl is-active --quiet flyshadow-guard.timer 2>/dev/null; then + if [ -x "$SHADOW_RUN_BIN" ] && "$SHADOW_RUN_BIN" stop; then + log "the shadow and its guard are stopped (flysim.service not restarted)" + else + log "WARNING: could not stop the shadow; run: fly-shadow-run stop (never with --restart-flysim)" + fi + fi +} + usage() { echo "usage: fly-runtime status | session [--no-restart] [--no-fallback] [--no-probation] | legacy [--no-restart] | fallback [reason]" >&2 exit 2 @@ -384,6 +402,7 @@ if [ "$command" = session ]; then # check above too): only report success if the session drop-in is still what runs. if [ -f "$DROPIN" ] && [ ! -f "$FELLBACK_FILE" ]; then switching=0 + stop_shadow if [ "$probe" -eq 1 ]; then if [ ! -x "$PROBATION_BIN" ] || ! "$PROBATION_BIN" start; then log "the speed probation could not be started; an unguarded cutover is not allowed" diff --git a/infra/bin/fly-shadow-run b/infra/bin/fly-shadow-run index 902ad6d..18195ca 100755 --- a/infra/bin/fly-shadow-run +++ b/infra/bin/fly-shadow-run @@ -200,10 +200,46 @@ elif mode == "simulate": print("no trip over %d checks (baseline %s)" % (n, json.dumps(base))) ' +# The shadow must never share a CPU with the live fly (review N2): refuse unless the unit's cpuset +# drop-in exists (05-deploy skips every cpuset drop-in when the CT conf and CPUSET disagree) and its +# AllowedCPUs is non-empty and disjoint from flysim.service's. +check_cpuset() { + local dropin="${FLY_SHADOW_CPUSET_DROPIN:-/etc/systemd/system/flyshadow.service.d/cpuset.conf}" + [ -f "$dropin" ] || die "$dropin is missing: flyshadow would run on every CPU, including flysim's. Run 05-deploy with CPUSET set; nothing started" + local shadow_cpus sim_cpus + shadow_cpus="$(systemctl show -p AllowedCPUs --value flyshadow.service 2>/dev/null || true)" + sim_cpus="$(systemctl show -p AllowedCPUs --value flysim.service 2>/dev/null || true)" + [ -n "$shadow_cpus" ] || die "flyshadow.service has an empty AllowedCPUs; nothing started" + [ -n "$sim_cpus" ] || die "flysim.service has an empty AllowedCPUs, so the shadow cannot be kept off it; nothing started" + local overlap rc=0 + overlap="$(python3 -c ' +import sys +def cpus(t): + out = set() + for part in t.replace(",", " ").split(): + a, _, b = part.partition("-") + out.update(range(int(a), int(b or a) + 1)) + return out +try: + both = cpus(sys.argv[1]) & cpus(sys.argv[2]) +except ValueError: + sys.exit(2) +if both: + print(",".join(str(c) for c in sorted(both))) + sys.exit(1) +' "$shadow_cpus" "$sim_cpus" 2>/dev/null)" || rc=$? + case "$rc" in + 0) ;; + 1) die "flyshadow.service AllowedCPUs ($shadow_cpus) overlaps flysim.service's ($sim_cpus) on CPU $overlap; nothing started" ;; + *) die "cannot parse AllowedCPUs (flyshadow '$shadow_cpus', flysim '$sim_cpus'); nothing started" ;; + esac +} + start() { need_root [ -x "$BIN" ] || die "$BIN is missing: this release predates SHADOW-01" systemctl is-active --quiet flyshadow.service && die "flyshadow.service is already running" + check_cpuset install -d -o fly -g fly -m 0755 "$SHADOW_DIR" "$SHADOW_DIR/trace" # A previous run's verdict is kept beside, never read as this run's. if [ -f "$SHADOW_DIR/verdict.json" ]; then diff --git a/infra/docs/runbook.md b/infra/docs/runbook.md index 0033077..3d8047c 100644 --- a/infra/docs/runbook.md +++ b/infra/docs/runbook.md @@ -592,6 +592,9 @@ pct exec -- /opt/fly/bin/fly-shadow-run stop --restart-flysim # shadow, `cpuset.conf`). It pauses for a minute while flysim's lag grows faster than its baseline rate, and it stops pausing when a pause does not help. Disk: about 160 MB of trace per live hour, and a spool of at most 512 MiB. +- **Start** refuses, and starts nothing, unless `flyshadow.service.d/cpuset.conf` exists and the + unit's `AllowedCPUs` (`systemctl show`) is disjoint from flysim.service's (05-deploy skips every + cpuset drop-in when the CT conf and CPUSET disagree). - **Stop** stops the unit and the guard, removes the drop-in, and deletes the trace and the spool (`--keep` keeps them). Without `--restart-flysim`, the running flysim stops its current trace within a minute, because its consumer is gone. @@ -614,8 +617,7 @@ pct exec $CTID -- sh -c '/opt/fly/bin/fly-shadow-run check && /opt/fly/bin/fly-r # 4. Watch the probation (30 minutes after the switch): pct exec $CTID -- /opt/fly/bin/fly-runtime-probation status pct exec $CTID -- /opt/fly/bin/fly-runtime status -# 5. Pass, or automatic fallback (below). Then stop the shadow: -pct exec $CTID -- /opt/fly/bin/fly-shadow-run stop --restart-flysim +# 5. Pass, or automatic fallback (below). Nothing to stop: step 3 already stopped the shadow. # Manual rollback, any time (cancels the probation too): pct exec $CTID -- /opt/fly/bin/fly-runtime legacy ``` @@ -624,9 +626,12 @@ pct exec $CTID -- /opt/fly/bin/fly-runtime legacy up (Shadow run above). `session` then does what "Switch the runtime" describes (compatibility check, drop-in, restart, health wait) and, once flysim is healthy on the session runtime, starts the probation. `session` returns 0 at that point: the probation is judged later, by the timer. - Stop the shadow only after the probation has passed (step 5): its trace costs the live fly, and - the session runtime is being measured. Until then `fly-shadow-run stop --restart-flysim` would - restart flysim inside the probation, which only restarts the warm-up. + `session` also stops the shadow and its guard (`fly-shadow-run stop`, never + `--restart-flysim`) as soon as flysim is healthy on the session runtime, before the probation + starts. A guard left running would judge the session runtime against the legacy baseline and + restart flysim on a trip (a second start inside the 600 s start-limit window, and a reset + warm-up), and the session writes no trace, so the shadow has nothing to follow. If you stop the + shadow by hand at any time during the probation, do it without `--restart-flysim`. - **The probation** (`infra/bin/fly-runtime-probation`, `fly-runtime-probation.timer` every 60 s, root). Each tick takes one sample of flysim's `/metrics` (`fly_realtime_factor` averaged over 10 one-second reads, `fly_lag_seconds`, `fly_uptime_seconds`; the same sampling as the shadow diff --git a/infra/tests/lint.sh b/infra/tests/lint.sh index 9907caa..7b2240a 100755 --- a/infra/tests/lint.sh +++ b/infra/tests/lint.sh @@ -656,6 +656,61 @@ GLID else fail "python3 is needed to test fly-shadow-run's guard rule" fi +# N2: `start` refuses, starting nothing, unless the cpuset drop-in exists and is disjoint from flysim's. +cs_tmp="$(mktemp -d "${TMPDIR:-/tmp}/fly-lint-cpuset.XXXXXX")" +mkdir -p "$cs_tmp/bin" "$cs_tmp/shadow" +cat > "$cs_tmp/bin/systemctl" <<'CSSTUB' +#!/bin/sh +echo "$*" >> "$CS_DIR/systemctl.log" +case "$1" in + is-active) exit 3 ;; + show) case "$5" in + flyshadow.service) echo "$CS_SHADOW_CPUS" ;; + flysim.service) echo "$CS_SIM_CPUS" ;; + esac ;; +esac +exit 0 +CSSTUB +printf '#!/bin/sh\n[ "$1" = -u ] && { echo 0; exit 0; }\nexec /usr/bin/id "$@"\n' > "$cs_tmp/bin/id" +printf '#!/bin/sh\nexit 0\n' > "$cs_tmp/fly-shadow" +printf '#!/bin/sh\necho "stub install reached"\nexit 1\n' > "$cs_tmp/bin/install" +chmod +x "$cs_tmp/bin/install" "$cs_tmp/bin/systemctl" "$cs_tmp/bin/id" "$cs_tmp/fly-shadow" +cs_start() { # name, expect (refuse|proceed), shadow cpus, sim cpus, drop-in (yes|no) + local rc=0 out + : > "$cs_tmp/systemctl.log" + rm -f "$cs_tmp/dropin.conf" + [ "$5" = yes ] && : > "$cs_tmp/dropin.conf" + out="$(PATH="$cs_tmp/bin:$PATH" CS_DIR="$cs_tmp" CS_SHADOW_CPUS="$3" CS_SIM_CPUS="$4" \ + FLY_SHADOW_DIR="$cs_tmp/shadow" FLY_SHADOW_BIN="$cs_tmp/fly-shadow" \ + FLY_SHADOW_CPUSET_DROPIN="$cs_tmp/dropin.conf" FLY_SHADOW_BASELINE_SECONDS=1 \ + FLY_METRICS_URL=http://127.0.0.1:1 "$INFRA_DIR/bin/fly-shadow-run" start 2>&1)" || rc=$? + case "$2" in + refuse) + if [ "$rc" -ne 0 ] && ! grep -qE '^(start|restart|daemon-reload)' "$cs_tmp/systemctl.log" \ + && [ ! -d "$cs_tmp/shadow/trace" ] && echo "$out" | grep -q 'nothing started'; then + pass "fly-shadow-run start refuses, $1" + else + fail "fly-shadow-run start must refuse and start nothing, $1 (rc=$rc): $out" + fi ;; + proceed) + # past the cpuset check it goes on to install -d (stubbed to stop there) + if echo "$out" | grep -q 'stub install reached'; then + pass "fly-shadow-run start proceeds, $1" + else + fail "fly-shadow-run start must pass the cpuset check, $1 (rc=$rc): $out" + fi ;; + esac +} +if command -v python3 >/dev/null 2>&1; then + cs_start "no cpuset drop-in" refuse "0-7" "1,3,5,7" no + cs_start "AllowedCPUs overlapping flysim's" refuse "0 2 3" "1 3 5 7" yes + cs_start "AllowedCPUs empty" refuse "" "1 3 5 7" yes + cs_start "AllowedCPUs disjoint (ranges)" proceed "0 2 4 6" "1 3 5 7" yes + cs_start "AllowedCPUs disjoint (range syntax)" proceed "0-2" "3-7" yes +else + fail "python3 is needed to test fly-shadow-run's cpuset check" +fi +rm -rf "$cs_tmp" if grep -qF 'flyshadow) cpus="${page_cpus},${encoder_cpus}" ;;' "$INFRA_DIR/05-deploy.sh"; then pass "05-deploy.sh keeps flyshadow.service off flysim's CPUs" else @@ -766,6 +821,13 @@ mkdir -p "$rt_dir/bin" "$rt_dir/release" "$rt_dir/systemd" cat > "$rt_dir/bin/systemctl" <<'RTSTUB' #!/usr/bin/env bash echo "$*" >> "$RT_DIR/systemctl.log" +if [ "$1" = is-active ]; then + case "$3" in flyshadow.service|flyshadow-guard.timer) [ "${RT_SHADOW_ACTIVE:-0}" = 1 ]; exit $? ;; esac +fi +RTSTUB +cat > "$rt_dir/release/fly-shadow-run" <<'RTSTUB' +#!/usr/bin/env bash +echo "$*" >> "$RT_DIR/shadow-run.log" RTSTUB cat > "$rt_dir/bin/id" <<'RTSTUB' #!/usr/bin/env bash @@ -795,6 +857,7 @@ fly_runtime() { FLY_ENV_FILE="$rt_dir/fly.env" FLY_SYSTEMD_DIR="$rt_dir/systemd" \ FLY_RUNTIME_LOG="$rt_dir/runtime.log" FLY_RUNTIME_HEALTH_TIMEOUT=4 \ FLY_RUNTIME_FELLBACK="$rt_dir/run/runtime-fellback.json" FLY_RUNTIME_LOCK="$rt_dir/run/runtime.lock" \ + FLY_SHADOW_RUN_BIN="$rt_dir/release/fly-shadow-run" \ FLY_PROBATION_DIR="$rt_dir/probation" FLY_PROBATION_RESULT="$rt_dir/run/runtime-probation.json" \ "$@" bash "$INFRA_DIR/bin/fly-runtime" "${RT_ARGS[@]}" >/dev/null 2>&1 } @@ -814,6 +877,24 @@ if fly_runtime && [ -f "$rt_dropin" ] \ else fail "fly-runtime session: drop-in/restart/health wrong ($(cat "$rt_dropin" 2>/dev/null | tr '\n' ' '))" fi +# N1: a running shadow and guard are stopped by `session`, without restarting flysim; none running: untouched. +[ ! -e "$rt_dir/shadow-run.log" ] \ + && pass "fly-runtime session leaves fly-shadow-run alone when no shadow is running" \ + || fail "fly-runtime session called fly-shadow-run with no shadow running" +RT_ARGS=(legacy); fly_runtime || true +RT_ARGS=(session) +rm -f "$rt_dir/shadow-run.log" +if fly_runtime RT_SHADOW_ACTIVE=1 && [ "$(cat "$rt_dir/shadow-run.log" 2>/dev/null)" = stop ]; then + pass "fly-runtime session stops the running shadow and guard (fly-shadow-run stop, no --restart-flysim)" +else + fail "fly-runtime session must run exactly 'fly-shadow-run stop' when a shadow is running ($(cat "$rt_dir/shadow-run.log" 2>/dev/null))" +fi +rm -f "$rt_dir/shadow-run.log" +RT_ARGS=(session --no-restart) +fly_runtime RT_SHADOW_ACTIVE=1 || true +[ ! -e "$rt_dir/shadow-run.log" ] \ + && pass "fly-runtime session --no-restart (deploy refresh) does not touch the shadow" \ + || fail "fly-runtime session --no-restart must not stop the shadow" RT_ARGS=(legacy) if fly_runtime && [ ! -f "$rt_dropin" ]; then pass "fly-runtime legacy: the drop-in is gone, flysim.service restarted"