infra: the shadow guard survives the shadow's crash-restart delay; check needs a live guard and a baseline; refuse a verdict dated in the future
Review round 3, G1-a: the guard stops only when flyshadow's ActiveState is inactive or failed (systemctl show), not while it is activating/auto-restart. check also requires the guard timer active and baseline.json. allows_cutover refuses an updatedAt more than 60 s ahead of the clock. Lint drives the guard and check with a fake systemctl; a real systemd 257 user manager run is kept in the review notes.
This commit is contained in:
parent
d2d65a4a60
commit
f7ca35ba2c
4 changed files with 133 additions and 15 deletions
|
|
@ -47,7 +47,9 @@
|
||||||
# Tripping stops flyshadow.service and the guard timer, removes the trace drop-in, restarts
|
# Tripping stops flyshadow.service and the guard timer, removes the trace drop-in, restarts
|
||||||
# flysim.service without the trace, and writes /srv/fly/shadow/guard-tripped.json. When the shadow
|
# flysim.service without the trace, and writes /srv/fly/shadow/guard-tripped.json. When the shadow
|
||||||
# is no longer running (it diverged, passed and stopped, or crashed), the guard stops its own
|
# is no longer running (it diverged, passed and stopped, or crashed), the guard stops its own
|
||||||
# timer and changes nothing: it only ever acts against a running shadow.
|
# timer and changes nothing: it only ever acts against a running shadow. "No longer running" means
|
||||||
|
# ActiveState inactive or failed (systemctl show), never activating: the shadow's 10-s crash-restart
|
||||||
|
# delay must not switch the guard off. `check` also requires the guard timer active and baseline.json.
|
||||||
#
|
#
|
||||||
# Files (all owned by fly):
|
# Files (all owned by fly):
|
||||||
# /srv/fly/shadow/trace/trace-<ms>-<pid>.jsonl flysim's per-process trace (4 GiB each, 8 in all)
|
# /srv/fly/shadow/trace/trace-<ms>-<pid>.jsonl flysim's per-process trace (4 GiB each, 8 in all)
|
||||||
|
|
@ -62,7 +64,7 @@ set -euo pipefail
|
||||||
SHADOW_DIR="${FLY_SHADOW_DIR:-/srv/fly/shadow}"
|
SHADOW_DIR="${FLY_SHADOW_DIR:-/srv/fly/shadow}"
|
||||||
DROPIN_DIR=/etc/systemd/system/flysim.service.d
|
DROPIN_DIR=/etc/systemd/system/flysim.service.d
|
||||||
DROPIN="$DROPIN_DIR/shadow-trace.conf"
|
DROPIN="$DROPIN_DIR/shadow-trace.conf"
|
||||||
BIN=/opt/fly/current/fly-shadow
|
BIN="${FLY_SHADOW_BIN:-/opt/fly/current/fly-shadow}"
|
||||||
METRICS_URL="${FLY_METRICS_URL:-http://127.0.0.1:9101}"
|
METRICS_URL="${FLY_METRICS_URL:-http://127.0.0.1:9101}"
|
||||||
CONTROL_URL="${FLY_CONTROL_URL:-http://127.0.0.1:7401}"
|
CONTROL_URL="${FLY_CONTROL_URL:-http://127.0.0.1:7401}"
|
||||||
# The ledger digest period in transitions: about one a second. Every boundary is proved in the
|
# The ledger digest period in transitions: about one a second. Every boundary is proved in the
|
||||||
|
|
@ -278,6 +280,19 @@ stop() {
|
||||||
log "the verdict stays in $SHADOW_DIR/verdict.json"
|
log "the verdict stays in $SHADOW_DIR/verdict.json"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# "ActiveState/SubState" of a unit, from `systemctl show` (`is-active` cannot tell a crashed unit
|
||||||
|
# waiting out its restart delay, activating/auto-restart, from a stopped one).
|
||||||
|
unit_state() {
|
||||||
|
local out active
|
||||||
|
out="$(systemctl show -p ActiveState,SubState "$1" 2>/dev/null)" || true
|
||||||
|
active="$(printf '%s\n' "$out" | sed -n 's/^ActiveState=//p')"
|
||||||
|
# An unreadable state is not "stopped": the guard keeps running and judges the fly.
|
||||||
|
case "$active" in
|
||||||
|
active|inactive|failed|activating|deactivating|reloading) printf '%s\n' "$active" ;;
|
||||||
|
*) printf 'unknown\n' ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
guard() {
|
guard() {
|
||||||
need_root
|
need_root
|
||||||
if [ ! -f "$SHADOW_DIR/baseline.json" ]; then
|
if [ ! -f "$SHADOW_DIR/baseline.json" ]; then
|
||||||
|
|
@ -285,13 +300,21 @@ guard() {
|
||||||
systemctl stop flyshadow-guard.timer 2>/dev/null || true
|
systemctl stop flyshadow-guard.timer 2>/dev/null || true
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
# The guard acts only against a running shadow: one that diverged, stopped or crashed costs
|
# The guard acts only against a shadow that is up or on its way up. One that diverged, passed
|
||||||
# the live fly nothing more, and flysim stops its trace without the shadow's heartbeat.
|
# and stopped, or was stopped costs the live fly nothing more (ActiveState inactive/failed), and
|
||||||
if ! systemctl is-active --quiet flyshadow.service; then
|
# flysim stops its trace without the shadow's heartbeat. During the shadow's crash-restart delay
|
||||||
log "flyshadow.service is not running: the guard stops itself and changes nothing"
|
# (Restart=on-failure, RestartSec=10) the unit is `activating` (auto-restart): the guard must
|
||||||
|
# keep running, or the restarted shadow runs unguarded while `check` still allows a cutover
|
||||||
|
# (SHADOW-01 review round 3, G1-a).
|
||||||
|
local active_state
|
||||||
|
active_state="$(unit_state flyshadow.service)"
|
||||||
|
case "$active_state" in
|
||||||
|
inactive|failed)
|
||||||
|
log "flyshadow.service is $active_state: the guard stops itself and changes nothing"
|
||||||
systemctl stop flyshadow-guard.timer 2>/dev/null || true
|
systemctl stop flyshadow-guard.timer 2>/dev/null || true
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
;;
|
||||||
|
esac
|
||||||
[ -f "$SHADOW_DIR/guard-state.json" ] || printf '{}\n' > "$SHADOW_DIR/guard-state.json"
|
[ -f "$SHADOW_DIR/guard-state.json" ] || printf '{}\n' > "$SHADOW_DIR/guard-state.json"
|
||||||
local now tmp verdict
|
local now tmp verdict
|
||||||
now="$(sample)" || { log "could not read flysim's metrics; not judged this minute"; exit 0; }
|
now="$(sample)" || { log "could not read flysim's metrics; not judged this minute"; exit 0; }
|
||||||
|
|
@ -334,9 +357,9 @@ PY
|
||||||
}
|
}
|
||||||
|
|
||||||
check() {
|
check() {
|
||||||
[ -x "$BIN" ] || die "$BIN is missing"
|
# A tripped guard, a shadow that is not running, or a run that no guard watched never allows a
|
||||||
# A tripped guard, or a shadow that is not running, never allows a cutover, whatever the
|
# cutover, whatever the verdict file says: the guard timer must be active and its baseline
|
||||||
# verdict file says.
|
# present (G1-a).
|
||||||
if [ -f "$SHADOW_DIR/guard-tripped.json" ]; then
|
if [ -f "$SHADOW_DIR/guard-tripped.json" ]; then
|
||||||
echo "cutover refused: the guard tripped ($(cat "$SHADOW_DIR/guard-tripped.json"))"
|
echo "cutover refused: the guard tripped ($(cat "$SHADOW_DIR/guard-tripped.json"))"
|
||||||
exit 1
|
exit 1
|
||||||
|
|
@ -345,6 +368,15 @@ check() {
|
||||||
echo "cutover refused: flyshadow.service is not running"
|
echo "cutover refused: flyshadow.service is not running"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
if ! systemctl is-active --quiet flyshadow-guard.timer; then
|
||||||
|
echo "cutover refused: flyshadow-guard.timer is not active (this run is not guarded)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ ! -f "$SHADOW_DIR/baseline.json" ]; then
|
||||||
|
echo "cutover refused: no baseline.json (this run was not baselined by fly-shadow-run start)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
[ -x "$BIN" ] || die "$BIN is missing"
|
||||||
# The live service's configuration, for its compatibility string.
|
# The live service's configuration, for its compatibility string.
|
||||||
set -a
|
set -a
|
||||||
# shellcheck disable=SC1091
|
# shellcheck disable=SC1091
|
||||||
|
|
|
||||||
|
|
@ -477,8 +477,9 @@ pct exec <ctid> -- /opt/fly/bin/fly-shadow-run stop --restart-flysim # shadow,
|
||||||
real degradation begins. A tripped guard stops the shadow and the guard, removes the drop-in and
|
real degradation begins. A tripped guard stops the shadow and the guard, removes the drop-in and
|
||||||
restarts flysim without the trace. It records why in `/srv/fly/shadow/guard-tripped.json`, and
|
restarts flysim without the trace. It records why in `/srv/fly/shadow/guard-tripped.json`, and
|
||||||
`status` shows it. A paused fly is not judged, and a restarted one starts its window again.
|
`status` shows it. A paused fly is not judged, and a restarted one starts its window again.
|
||||||
Once the shadow is no longer running (diverged, stopped or crashed), the guard stops itself and
|
Once the shadow is down for good (`ActiveState` inactive or failed: diverged, stopped, given up),
|
||||||
changes nothing. A trip is not a divergence: fix the resources, then `start` again.
|
the guard stops itself and changes nothing. While the shadow waits out its 10-s crash-restart delay
|
||||||
|
(`activating`) the guard keeps running. A trip is not a divergence: fix the resources, then `start` again.
|
||||||
- **While it runs**, every later flysim restart (the unstick rule, the watchdog,
|
- **While it runs**, every later flysim restart (the unstick rule, the watchdog,
|
||||||
`fly-loop-recover`, `fly-reset-to-milestone`) starts a new trace file. The shadow follows it on
|
`fly-loop-recover`, `fly-reset-to-milestone`) starts a new trace file. The shadow follows it on
|
||||||
its own; nothing needs doing. The 3 h window is live *brain* time summed over those processes.
|
its own; nothing needs doing. The 3 h window is live *brain* time summed over those processes.
|
||||||
|
|
@ -490,10 +491,13 @@ pct exec <ctid> -- /opt/fly/bin/fly-shadow-run stop --restart-flysim # shadow,
|
||||||
The shadow keeps following, and the verdict stays `pass` only while nothing diverges. CUT-01
|
The shadow keeps following, and the verdict stays `pass` only while nothing diverges. CUT-01
|
||||||
runs `check` at the moment it cuts over. `check` also requires all of these:
|
runs `check` at the moment it cuts over. `check` also requires all of these:
|
||||||
- at least 10,800 brain seconds, whatever window the shadow was started with;
|
- at least 10,800 brain seconds, whatever window the shadow was started with;
|
||||||
- the guard has not tripped and `flyshadow.service` is running;
|
- the guard has not tripped, `flyshadow.service` is running, `flyshadow-guard.timer` is active and
|
||||||
|
`baseline.json` exists (a run no guard watched is never cut over);
|
||||||
- the verdict is for the release `/opt/fly/current` points to, with `flysim-session` and every
|
- the verdict is for the release `/opt/fly/current` points to, with `flysim-session` and every
|
||||||
other shadowed binary unchanged;
|
other shadowed binary unchanged;
|
||||||
- the shadow is caught up (at most about a minute behind);
|
- the shadow is caught up (at most about a minute behind);
|
||||||
|
- the verdict's `updatedAt` is neither older than the max age nor more than 60 s in the future
|
||||||
|
(a clock stepped back must not keep an old pass fresh);
|
||||||
- no segment was skipped for a reason other than the live side's own.
|
- no segment was skipped for a reason other than the live side's own.
|
||||||
- **Divergence**: the shadow stops itself (exit status 3; the unit does not restart it). A failure
|
- **Divergence**: the shadow stops itself (exit status 3; the unit does not restart it). A failure
|
||||||
of the session runtime itself counts: it did not start, the restore gate refused the live save,
|
of the session runtime itself counts: it did not start, the restore gate refused the live save,
|
||||||
|
|
|
||||||
|
|
@ -586,6 +586,72 @@ if command -v python3 >/dev/null 2>&1; then
|
||||||
[ -f "$d/expect" ] || continue
|
[ -f "$d/expect" ] || continue
|
||||||
guard_trace "recorded $(basename "$d")" "$d" "$(cat "$d/expect")"
|
guard_trace "recorded $(basename "$d")" "$d" "$(cat "$d/expect")"
|
||||||
done
|
done
|
||||||
|
# The guard's lifecycle and `check`'s preconditions (review round 3, G1-a), with a fake systemctl
|
||||||
|
# (`show` answers FAKE_SHADOW_STATE; the timer is active when FAKE_TIMER_ACTIVE=yes), a fake `id`
|
||||||
|
# (root) and a metrics URL nobody listens on (a live guard then reports "not judged" and stays).
|
||||||
|
gl_bin="$g_tmp/lifecycle-bin"; gl_dir="$g_tmp/lifecycle-shadow"
|
||||||
|
mkdir -p "$gl_bin" "$gl_dir"
|
||||||
|
cat > "$gl_bin/systemctl" <<'GLSYSTEMCTL'
|
||||||
|
#!/bin/sh
|
||||||
|
echo "$*" >> "$FAKE_SYSTEMCTL_LOG"
|
||||||
|
case "$1" in
|
||||||
|
show) printf 'ActiveState=%s\nSubState=x\n' "$FAKE_SHADOW_STATE" ;;
|
||||||
|
is-active)
|
||||||
|
case "$3" in
|
||||||
|
flyshadow.service) [ "$FAKE_SHADOW_STATE" = active ]; exit $? ;;
|
||||||
|
flyshadow-guard.timer) [ "$FAKE_TIMER_ACTIVE" = yes ]; exit $? ;;
|
||||||
|
*) exit 3 ;;
|
||||||
|
esac ;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
|
GLSYSTEMCTL
|
||||||
|
cat > "$gl_bin/id" <<'GLID'
|
||||||
|
#!/bin/sh
|
||||||
|
[ "$1" = -u ] && { echo 0; exit 0; }
|
||||||
|
exec /usr/bin/id "$@"
|
||||||
|
GLID
|
||||||
|
chmod +x "$gl_bin/systemctl" "$gl_bin/id"
|
||||||
|
gl_run() { # args to fly-shadow-run; env FAKE_SHADOW_STATE, FAKE_TIMER_ACTIVE from the caller
|
||||||
|
: > "$g_tmp/systemctl.log"
|
||||||
|
PATH="$gl_bin:$PATH" FAKE_SYSTEMCTL_LOG="$g_tmp/systemctl.log" FLY_SHADOW_DIR="$gl_dir" \
|
||||||
|
FLY_METRICS_URL=http://127.0.0.1:1 FLY_SHADOW_BIN="$g_tmp/none" \
|
||||||
|
"$INFRA_DIR/bin/fly-shadow-run" "$@" 2>&1
|
||||||
|
}
|
||||||
|
printf '{"rtfMean":0.66,"rtfSd":0.01,"lagRate":0.3,"samples":60,"margin":0.05}\n' > "$gl_dir/baseline.json"
|
||||||
|
printf '{}\n' > "$gl_dir/guard-state.json"
|
||||||
|
for st in activating deactivating; do
|
||||||
|
FAKE_SHADOW_STATE=$st FAKE_TIMER_ACTIVE=yes gl_run guard > "$g_tmp/out" || true
|
||||||
|
if grep -q 'stop flyshadow-guard.timer' "$g_tmp/systemctl.log"; then
|
||||||
|
fail "fly-shadow-run guard stopped its timer while flyshadow was $st (auto-restart): $(cat "$g_tmp/out")"
|
||||||
|
else
|
||||||
|
pass "fly-shadow-run guard keeps running while flyshadow is $st (crash-restart delay)"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
for st in inactive failed; do
|
||||||
|
FAKE_SHADOW_STATE=$st FAKE_TIMER_ACTIVE=yes gl_run guard > "$g_tmp/out" || true
|
||||||
|
if grep -q 'stop flyshadow-guard.timer' "$g_tmp/systemctl.log"; then
|
||||||
|
pass "fly-shadow-run guard stops its timer when flyshadow is $st"
|
||||||
|
else
|
||||||
|
fail "fly-shadow-run guard kept running with flyshadow $st: $(cat "$g_tmp/out")"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
gl_check() { # name, expected (refused|passes-gates), then the env
|
||||||
|
local out rc=0
|
||||||
|
out="$(gl_run check)" || rc=$?
|
||||||
|
case "$2:$rc" in
|
||||||
|
refused:1) pass "fly-shadow-run check, $1: ${out%%(*}" ;;
|
||||||
|
passes-gates:2) pass "fly-shadow-run check, $1: past the guard preconditions" ;;
|
||||||
|
*) fail "fly-shadow-run check, $1: expected $2, got exit $rc: $out" ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
FAKE_SHADOW_STATE=active FAKE_TIMER_ACTIVE=yes gl_check "guarded run" passes-gates
|
||||||
|
FAKE_SHADOW_STATE=active FAKE_TIMER_ACTIVE=no gl_check "guard timer not active" refused
|
||||||
|
FAKE_SHADOW_STATE=activating FAKE_TIMER_ACTIVE=yes gl_check "shadow restarting" refused
|
||||||
|
mv "$gl_dir/baseline.json" "$gl_dir/baseline.json.off"
|
||||||
|
FAKE_SHADOW_STATE=active FAKE_TIMER_ACTIVE=yes gl_check "no baseline.json" refused
|
||||||
|
mv "$gl_dir/baseline.json.off" "$gl_dir/baseline.json"
|
||||||
|
echo '{}' > "$gl_dir/guard-tripped.json"
|
||||||
|
FAKE_SHADOW_STATE=active FAKE_TIMER_ACTIVE=yes gl_check "guard tripped" refused
|
||||||
rm -rf "$g_tmp"
|
rm -rf "$g_tmp"
|
||||||
else
|
else
|
||||||
fail "python3 is needed to test fly-shadow-run's guard rule"
|
fail "python3 is needed to test fly-shadow-run's guard rule"
|
||||||
|
|
|
||||||
|
|
@ -363,6 +363,9 @@ pub struct Release<'a> {
|
||||||
pub binaries: &'a std::collections::BTreeMap<String, String>,
|
pub binaries: &'a std::collections::BTreeMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// How far ahead of the clock a verdict's `updatedAt` may be before it is refused.
|
||||||
|
const MAX_CLOCK_SKEW_S: i64 = 60;
|
||||||
|
|
||||||
/// Reads a verdict file and says whether it allows the cutover, by the rule in the module notes.
|
/// Reads a verdict file and says whether it allows the cutover, by the rule in the module notes.
|
||||||
/// `binary` is the file name of the session-runtime binary CUT-01 switches to, inside `release`
|
/// `binary` is the file name of the session-runtime binary CUT-01 switches to, inside `release`
|
||||||
/// (what `/opt/fly/current` resolves to now), and `compatibility` the live service's;
|
/// (what `/opt/fly/current` resolves to now), and `compatibility` the live service's;
|
||||||
|
|
@ -443,6 +446,14 @@ pub fn allows_cutover(
|
||||||
.as_str()
|
.as_str()
|
||||||
.and_then(parse_iso)
|
.and_then(parse_iso)
|
||||||
.ok_or("no updatedAt")?;
|
.ok_or("no updatedAt")?;
|
||||||
|
// A verdict from the future (beyond a small skew) is refused: after a backward clock step an
|
||||||
|
// old pass would otherwise stay "fresh" for the size of the step plus `max_age_s`.
|
||||||
|
if updated - now_ms > MAX_CLOCK_SKEW_S * 1000 {
|
||||||
|
return Err(format!(
|
||||||
|
"the verdict is dated {} s in the future",
|
||||||
|
(updated - now_ms) / 1000
|
||||||
|
));
|
||||||
|
}
|
||||||
if now_ms - updated > max_age_s * 1000 {
|
if now_ms - updated > max_age_s * 1000 {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"the verdict is {} s old",
|
"the verdict is {} s old",
|
||||||
|
|
@ -547,6 +558,11 @@ mod tests {
|
||||||
assert!(
|
assert!(
|
||||||
allows_cutover(&pass, "flysim-session", &release, "c", now + 301_000, 300).is_err()
|
allows_cutover(&pass, "flysim-session", &release, "c", now + 301_000, 300).is_err()
|
||||||
);
|
);
|
||||||
|
// A clock stepped back: a verdict dated well ahead of now is refused, a small skew is not.
|
||||||
|
assert!(
|
||||||
|
allows_cutover(&pass, "flysim-session", &release, "c", now - 120_000, 300).is_err()
|
||||||
|
);
|
||||||
|
assert!(allows_cutover(&pass, "flysim-session", &release, "c", now - 30_000, 300).is_ok());
|
||||||
assert!(ok(&verdict(Status::Pass, 10_799_000.0)).is_err());
|
assert!(ok(&verdict(Status::Pass, 10_799_000.0)).is_err());
|
||||||
// A verdict that asked for less than the operator's 3 h never passes the check.
|
// A verdict that asked for less than the operator's 3 h never passes the check.
|
||||||
let mut short_window = verdict(Status::Pass, 67_000.0);
|
let mut short_window = verdict(Status::Pass, 67_000.0);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue