feed bus in production: fly-feed switch, flysim wants the edge, watchdog checks the edge and falls back, deploy refuses a release without fly-edge
This commit is contained in:
parent
12321bb513
commit
6612f9ae9c
7 changed files with 805 additions and 40 deletions
|
|
@ -124,6 +124,19 @@ need pct
|
|||
FLY_FEED_VIA_EFFECTIVE="$(feed_via_normalize "${FLY_FEED_VIA:-}")" \
|
||||
|| die "05-deploy: FLY_FEED_VIA must be 'direct' or 'bus', got '${FLY_FEED_VIA}'"
|
||||
|
||||
# The container's own value, read before anything changes. A `fly-feed bus` (or `direct`) on the
|
||||
# container is not in the env file unless the operator put it there, and this deploy writes the env
|
||||
# file's value into fly.env. Nothing breaks (flysim and flyedge.service both follow fly.env at
|
||||
# their next start, so the feed stays served either way) but the mode would change at the next
|
||||
# restart of flysim, which is not what anyone meant. Said once, loudly, here.
|
||||
FLY_FEED_VIA_LIVE="$(ct_exec "$CTID" -- awk -F= '/^FLY_FEED_VIA=/ { v=$2 } END { print v }' /etc/fly/fly.env 2>/dev/null \
|
||||
| tr -d ' \r"' | tr '[:upper:]' '[:lower:]' || true)"
|
||||
if [ -n "$FLY_FEED_VIA_LIVE" ] && [ "$FLY_FEED_VIA_LIVE" != "$FLY_FEED_VIA_EFFECTIVE" ]; then
|
||||
log "05-deploy: WARNING: the container's fly.env says FLY_FEED_VIA=${FLY_FEED_VIA_LIVE} and $1 says ${FLY_FEED_VIA_EFFECTIVE};" \
|
||||
"this deploy writes ${FLY_FEED_VIA_EFFECTIVE}, which flysim applies at its next restart (no restart is done here)." \
|
||||
"If ${FLY_FEED_VIA_LIVE} is what you want, set FLY_FEED_VIA=${FLY_FEED_VIA_LIVE} in $1 and deploy again; \`fly-feed status\` shows both."
|
||||
fi
|
||||
|
||||
# CHROMIUM_PROFILE is validated here, not left to the launcher: a typo or a
|
||||
# `vgl` on a container that never had VirtualGL installed would only show up as
|
||||
# flystage refusing to start, i.e. a black stream, minutes after the deploy
|
||||
|
|
@ -422,6 +435,17 @@ The 'current' symlink has NOT been moved."
|
|||
log "05-deploy: flysim.service runs the session runtime; this release ships flysim-session with flysim's compatibility string"
|
||||
fi
|
||||
|
||||
# The feed bus (EDGE-02). Bus mode needs the release's fly-edge: without it flyedge.service's
|
||||
# ConditionPathExists= skips the unit, flysim has left :7400 to it, and the stage has no feed.
|
||||
# Refused when the env file asks for bus, and when the container is in bus mode now (an edge
|
||||
# that is running keeps running, but the first time it restarts there is nothing to start; and
|
||||
# the way back is `fly-feed direct`, which needs no binary of this release).
|
||||
if [ "$FLY_FEED_VIA_EFFECTIVE" = bus ] || [ "$FLY_FEED_VIA_LIVE" = bus ]; then
|
||||
ct_exec "$CTID" -- test -x "${release_path}/fly-edge" \
|
||||
|| die "05-deploy: REFUSING to deploy release ${version}: the feed is on the bus (FLY_FEED_VIA=bus: $([ "$FLY_FEED_VIA_EFFECTIVE" = bus ] && echo "in $1" || echo "on the container")) and this release has no fly-edge. Run \`fly-feed direct\` in the container and set FLY_FEED_VIA=direct in $1 first, or deploy a release that ships fly-edge. The 'current' symlink has NOT been moved."
|
||||
log "05-deploy: the feed is on the bus; this release ships fly-edge"
|
||||
fi
|
||||
|
||||
log "05-deploy: flipping /opt/fly/current -> ${release_path} atomically"
|
||||
ct_exec "$CTID" -- sh -c "ln -sfn '$release_path' /opt/fly/current.tmp && mv -T /opt/fly/current.tmp /opt/fly/current"
|
||||
else
|
||||
|
|
@ -770,7 +794,7 @@ fi
|
|||
# ---------------------------------------------------------------------------
|
||||
log "05-deploy: converging bin/ helpers to /opt/fly/bin"
|
||||
ct_exec "$CTID" -- mkdir -p /opt/fly/bin
|
||||
for name in fly-watchdog fly-loop-recover fly-loop-reset fly-recap fly-retention fly-reset-to-milestone fly-cpu-confine fly-runtime fly-runtime-probation fly-shadow-run flypush flystage-launch flycast-launch wait-for-x wait-for-stage wait-for-health; do
|
||||
for name in fly-watchdog fly-loop-recover fly-loop-reset fly-recap fly-retention fly-reset-to-milestone fly-cpu-confine fly-feed fly-runtime fly-runtime-probation fly-shadow-run flypush flystage-launch flycast-launch wait-for-x wait-for-stage wait-for-health; do
|
||||
converge_file "$CTID" "$INFRA_DIR/bin/$name" "/opt/fly/bin/$name" 0755 root:root >/dev/null
|
||||
done
|
||||
|
||||
|
|
|
|||
316
infra/bin/fly-feed
Executable file
316
infra/bin/fly-feed
Executable file
|
|
@ -0,0 +1,316 @@
|
|||
#!/usr/bin/env bash
|
||||
# infra/bin/fly-feed — who serves the feed WebSocket (ws://127.0.0.1:7400/feed): flysim itself
|
||||
# (`direct`) or fly-edge over flysim's feed bus (`bus`). EDGE-02; docs/design/flybus.md, "Feed over
|
||||
# the bus"; the runbook's "Feed over the bus".
|
||||
#
|
||||
# Usage (INSIDE the container, as root):
|
||||
# fly-feed status configured, running, the edge and its counters (any user)
|
||||
# fly-feed bus [--no-restart] [--no-rollback]
|
||||
# fly-feed direct [--no-restart] [--no-wait]
|
||||
#
|
||||
# One line decides: FLY_FEED_VIA in /etc/fly/fly.env. flysim reads it, flyedge.service's
|
||||
# ExecCondition= reads it, flysim.service has Wants=flyedge.service, so every start of flysim brings
|
||||
# up the edge in bus mode and skips it in direct mode, whoever started flysim and on whichever
|
||||
# runtime (flysim or flysim-session, `fly-runtime`): nothing else has to be enabled, started or
|
||||
# remembered. `fly-feed bus|direct` rewrites that line, restarts flysim.service (the feed changes
|
||||
# hands at the start, so the stream is down for the length of a flysim restart, exactly like the
|
||||
# unstick rule's) and waits until the page is on the feed again.
|
||||
#
|
||||
# What "healthy" means after a switch, in this order, within FLY_FEED_HEALTH_TIMEOUT (300 s):
|
||||
# 1. flysim's /healthz answers (a NEW invocation: the old process is not mistaken for it);
|
||||
# 2. bus: the edge's /healthz answers 200 (subscribed, snapshots arriving);
|
||||
# 3. if flystage.service is active: its page is on the feed (`fly_feed_clients` >= 1 on the
|
||||
# counters of whoever serves it) and `fly_frames_sent_total` advances. Without a page unit
|
||||
# (a box that only serves the feed) the wait ends at 2.
|
||||
# If that does not happen, `bus` puts FLY_FEED_VIA back to what it was, restarts flysim and checks
|
||||
# the same way (--no-rollback leaves it to the operator); `direct` is already the way back, so it
|
||||
# only reports. A switch that is interrupted (ssh drops, INT/TERM/HUP) rolls back the same way.
|
||||
# `--no-restart` only rewrites the line (the next start of flysim applies it). `--no-wait` restarts
|
||||
# flysim without blocking and does not wait for health or roll anything back: it is what the
|
||||
# watchdog runs (as a sudoers-granted `fly-feed direct --no-wait`) when the edge cannot be kept up.
|
||||
#
|
||||
# 05-deploy.sh writes FLY_FEED_VIA into fly.env from the operator's env file on every deploy and
|
||||
# does not restart flysim. After a `fly-feed bus`, put FLY_FEED_VIA=bus in the env file too, or the
|
||||
# next deploy writes `direct` back (the next flysim start would then follow it) and says so.
|
||||
#
|
||||
# Restarts count against flysim.service's start limit (3 in 10 minutes while the session runtime is
|
||||
# selected, `fly-runtime`): a switch and its rollback are two.
|
||||
#
|
||||
# Every switch is logged to the journal (tag fly-feed) and ${FEED_LOG}.
|
||||
set -euo pipefail
|
||||
|
||||
: "${FLY_ENV_FILE:=/etc/fly/fly.env}"
|
||||
: "${FLY_SERVICE:=flysim.service}"
|
||||
: "${FLY_EDGE_SERVICE:=flyedge.service}"
|
||||
: "${FLY_PAGE_SERVICE:=flystage.service}"
|
||||
: "${FLY_RELEASE_DIR:=/opt/fly/current}"
|
||||
: "${FLY_CONTROL_URL:=http://127.0.0.1:7401}"
|
||||
: "${FLY_METRICS_URL:=http://127.0.0.1:9101}"
|
||||
: "${FLY_EDGE_METRICS_URL:=http://127.0.0.1:9102}"
|
||||
: "${FLY_PROC_DIR:=/proc}"
|
||||
: "${FLY_FEED_HEALTH_TIMEOUT:=300}"
|
||||
: "${FLY_FEED_LOG:=/var/lib/fly/feed.log}"
|
||||
: "${FLY_FEED_LOCK:=/run/fly/feed.lock}"
|
||||
: "${FLY_FEED_SAMPLE_SECONDS:=3}"
|
||||
FEED_LOG="$FLY_FEED_LOG"
|
||||
|
||||
log() { echo "fly-feed: $*" >&2; }
|
||||
die() { log "$*"; exit 1; }
|
||||
|
||||
record() {
|
||||
if command -v logger >/dev/null 2>&1; then
|
||||
logger -t fly-feed -- "$*" || true
|
||||
fi
|
||||
mkdir -p "$(dirname "$FEED_LOG")" 2>/dev/null || true
|
||||
echo "$(date -u '+%Y-%m-%dT%H:%M:%SZ') $*" >>"$FEED_LOG" 2>/dev/null || log "could not append to $FEED_LOG"
|
||||
}
|
||||
|
||||
usage() {
|
||||
echo "usage: fly-feed status | bus [--no-restart] [--no-rollback] [--no-wait] | direct [--no-restart] [--no-wait]" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
# direct | bus | invalid:<value>. flysim reads the value case-insensitively; a missing line is direct.
|
||||
configured() {
|
||||
local value=""
|
||||
if [ -f "$FLY_ENV_FILE" ]; then
|
||||
value="$(awk -F= '/^FLY_FEED_VIA=/ { v=$0; sub(/^FLY_FEED_VIA=/, "", v); last=v } END { print last }' "$FLY_ENV_FILE" 2>/dev/null || true)"
|
||||
fi
|
||||
value="$(printf '%s' "$value" | tr -d ' \r"' | tr '[:upper:]' '[:lower:]')"
|
||||
case "$value" in
|
||||
""|direct) echo direct ;;
|
||||
bus) echo bus ;;
|
||||
*) echo "invalid:${value}" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# What the running flysim process started with (its environment, not the file): direct | bus |
|
||||
# stopped | unknown.
|
||||
running() {
|
||||
local pid value
|
||||
pid="$(systemctl show -p MainPID --value "$FLY_SERVICE" 2>/dev/null | head -n1 || true)"
|
||||
if [ -z "$pid" ] || [ "$pid" = 0 ]; then echo stopped; return; fi
|
||||
if [ ! -r "${FLY_PROC_DIR}/${pid}/environ" ]; then echo unknown; return; fi
|
||||
value="$(tr '\0' '\n' <"${FLY_PROC_DIR}/${pid}/environ" 2>/dev/null | awk -F= '/^FLY_FEED_VIA=/ { print $2; exit }' | tr -d ' \r"' | tr '[:upper:]' '[:lower:]' || true)"
|
||||
case "$value" in
|
||||
bus) echo bus ;;
|
||||
*) echo direct ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# The counters of whoever serves the feed in `via` mode.
|
||||
metrics_url() {
|
||||
if [ "$1" = bus ]; then echo "$FLY_EDGE_METRICS_URL"; else echo "$FLY_METRICS_URL"; fi
|
||||
}
|
||||
|
||||
# metric URL NAME -> the value of the first sample of NAME, or empty.
|
||||
metric() {
|
||||
curl -fsS --max-time 5 "$1/metrics" 2>/dev/null | awk -v n="$2" '$1 == n { print $2; exit }' || true
|
||||
}
|
||||
|
||||
# Whole-number part of a Prometheus value ("3", "3.0", "1e+06" is not expected for these).
|
||||
whole() { printf '%.0f' "${1:-0}" 2>/dev/null || echo 0; }
|
||||
|
||||
# The page is on the feed: at least one client, and the frames counter moves over a sample.
|
||||
page_on_feed() {
|
||||
local url first second clients
|
||||
url="$(metrics_url "$1")"
|
||||
clients="$(metric "$url" fly_feed_clients)"
|
||||
[ "$(whole "$clients")" -ge 1 ] || return 1
|
||||
first="$(metric "$url" fly_frames_sent_total)"
|
||||
sleep "$FLY_FEED_SAMPLE_SECONDS"
|
||||
second="$(metric "$url" fly_frames_sent_total)"
|
||||
[ -n "$first" ] && [ -n "$second" ] && [ "$(whole "$second")" -gt "$(whole "$first")" ]
|
||||
}
|
||||
|
||||
page_unit_active() { systemctl is-active --quiet "$FLY_PAGE_SERVICE" 2>/dev/null; }
|
||||
|
||||
old_invocation=""
|
||||
# Wait for a NEW flysim invocation to be healthy and the feed to be flowing in `via` mode.
|
||||
healthy() {
|
||||
local via="$1" deadline
|
||||
deadline=$(( $(date +%s) + FLY_FEED_HEALTH_TIMEOUT ))
|
||||
while [ "$(date +%s)" -lt "$deadline" ]; do
|
||||
if [ -n "$old_invocation" ] \
|
||||
&& [ "$(systemctl show -p InvocationID --value "$FLY_SERVICE" 2>/dev/null | head -n1 || true)" = "$old_invocation" ]; then
|
||||
sleep 1
|
||||
continue
|
||||
fi
|
||||
if curl -fsS -o /dev/null --max-time 5 "${FLY_CONTROL_URL}/healthz" 2>/dev/null; then
|
||||
if [ "$via" = bus ] && ! curl -fsS -o /dev/null --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>/dev/null; then
|
||||
sleep 1
|
||||
continue
|
||||
fi
|
||||
if ! page_unit_active; then
|
||||
log "healthy (${via}): ${FLY_PAGE_SERVICE} is not active, so no page to wait for"
|
||||
return 0
|
||||
fi
|
||||
if page_on_feed "$via"; then
|
||||
log "healthy (${via}): the page is on the feed and frames are moving"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Rewrite (or add) the FLY_FEED_VIA line, keeping every other line, the mode and the owner.
|
||||
set_via() {
|
||||
local value="$1" tmp
|
||||
[ -f "$FLY_ENV_FILE" ] || die "no ${FLY_ENV_FILE}; deploy first (infra/05-deploy.sh writes it)"
|
||||
tmp="$(mktemp "${FLY_ENV_FILE}.XXXXXX")"
|
||||
awk -v v="$value" '
|
||||
/^FLY_FEED_VIA=/ { if (!done) { print "FLY_FEED_VIA=" v; done=1 } ; next }
|
||||
{ print }
|
||||
END { if (!done) print "FLY_FEED_VIA=" v }
|
||||
' "$FLY_ENV_FILE" >"$tmp"
|
||||
chmod --reference="$FLY_ENV_FILE" "$tmp" 2>/dev/null || chmod 0640 "$tmp"
|
||||
chown --reference="$FLY_ENV_FILE" "$tmp" 2>/dev/null || true
|
||||
mv -f "$tmp" "$FLY_ENV_FILE"
|
||||
}
|
||||
|
||||
locked=0
|
||||
take_lock() {
|
||||
[ "$locked" -eq 0 ] || return 0
|
||||
mkdir -p "$(dirname "$FLY_FEED_LOCK")" 2>/dev/null || true
|
||||
exec 8>>"$FLY_FEED_LOCK"
|
||||
flock -w 60 8 || die "another fly-feed holds ${FLY_FEED_LOCK}; not switching"
|
||||
locked=1
|
||||
}
|
||||
|
||||
restart_flysim() {
|
||||
systemctl reset-failed "$FLY_SERVICE" 2>/dev/null || true
|
||||
old_invocation="$(systemctl show -p InvocationID --value "$FLY_SERVICE" 2>/dev/null | head -n1 || true)"
|
||||
log "restarting ${FLY_SERVICE}"
|
||||
# --no-block: a start that never becomes READY must not hold this process (and the lock);
|
||||
# `healthy` proves it is talking to the new invocation.
|
||||
systemctl restart --no-block "$FLY_SERVICE"
|
||||
}
|
||||
|
||||
status_report() {
|
||||
local conf run edge_state url
|
||||
conf="$(configured)"
|
||||
run="$(running)"
|
||||
echo "configured: ${conf}"
|
||||
echo "running: ${run}"
|
||||
if [ "$run" != stopped ] && [ "$run" != unknown ] && [ "$conf" != "$run" ] && [[ "$conf" != invalid:* ]]; then
|
||||
echo " fly.env and the running flysim disagree: the next start of ${FLY_SERVICE} applies ${conf} (fly-feed ${conf} does it now)"
|
||||
fi
|
||||
edge_state="$(systemctl is-active "$FLY_EDGE_SERVICE" 2>/dev/null || true)"
|
||||
echo "${FLY_EDGE_SERVICE}: ${edge_state:-unknown}"
|
||||
url="$(metrics_url "$run")"
|
||||
if [ "$run" = bus ]; then
|
||||
if curl -fsS -o /dev/null --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>/dev/null; then
|
||||
echo "edge healthz: ok"
|
||||
else
|
||||
echo "edge healthz: NOT OK ($(curl -sS --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>&1 | head -c 100))"
|
||||
fi
|
||||
echo "edge snapshot age: $(metric "$url" fly_edge_snapshot_age_seconds) s; bus lost: $(metric "$url" fly_edge_bus_lost_total); bind failures: $(metric "$url" fly_edge_bind_failures_total); decode failures: $(metric "$url" fly_edge_decode_failures_total)"
|
||||
fi
|
||||
echo "feed clients: $(metric "$url" fly_feed_clients) (counters of the ${run} server); frames sent: $(metric "$url" fly_frames_sent_total)"
|
||||
if [ -f "$FEED_LOG" ]; then echo "last switch: $(tail -n1 "$FEED_LOG")"; fi
|
||||
}
|
||||
|
||||
[ $# -ge 1 ] || usage
|
||||
command="$1"; shift
|
||||
restart=1
|
||||
rollback=1
|
||||
wait_for_health=1
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--no-restart) restart=0 ;;
|
||||
--no-rollback) rollback=0 ;;
|
||||
--no-wait) wait_for_health=0 ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
done
|
||||
|
||||
case "$command" in
|
||||
status) status_report; exit 0 ;;
|
||||
bus|direct) ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
|
||||
[ "$(id -u)" -eq 0 ] || die "run as root (it writes ${FLY_ENV_FILE} and restarts ${FLY_SERVICE})"
|
||||
before="$(configured)"
|
||||
[[ "$before" != invalid:* ]] || die "FLY_FEED_VIA in ${FLY_ENV_FILE} is '${before#invalid:}', not direct or bus; fix it (a deploy rewrites it) before switching"
|
||||
|
||||
if [ "$command" = bus ]; then
|
||||
[ -x "${FLY_RELEASE_DIR}/fly-edge" ] || die "this release has no ${FLY_RELEASE_DIR}/fly-edge; bus mode needs one (deploy a release that ships it)"
|
||||
fi
|
||||
|
||||
take_lock
|
||||
running_before="$(running)"
|
||||
if [ "$before" = "$command" ] && [ "$running_before" = "$command" ] && [ "$restart" -eq 1 ]; then
|
||||
log "already ${command}: fly.env says it and the running flysim started that way; nothing to do"
|
||||
status_report
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# An interrupted or failed `bus` puts the previous value back and restarts flysim on it. When
|
||||
# the line already said bus (a deploy wrote it, the running flysim never started that way), the
|
||||
# way back is direct: the state that worked.
|
||||
back="$before"
|
||||
if [ "$before" = bus ] && [ "$command" = bus ]; then back=direct; fi
|
||||
switching=0
|
||||
restarted=0
|
||||
# shellcheck disable=SC2317 # runs from the EXIT trap
|
||||
on_exit() {
|
||||
local rc=$?
|
||||
trap - EXIT INT TERM HUP
|
||||
if [ "$switching" -eq 1 ]; then
|
||||
switching=0
|
||||
set_via "$back" || true
|
||||
record "${back} (switch to ${command} interrupted or failed (exit ${rc}); rolled back)"
|
||||
if [ "$restarted" -eq 1 ]; then restart_flysim || true; fi
|
||||
fi
|
||||
exit "$rc"
|
||||
}
|
||||
trap on_exit EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
trap 'exit 129' HUP
|
||||
|
||||
switching=1
|
||||
set_via "$command"
|
||||
record "${command} (was ${before}; flysim was running ${running_before})"
|
||||
if [ "$restart" -ne 1 ]; then
|
||||
switching=0
|
||||
log "FLY_FEED_VIA=${command} written; the next start of ${FLY_SERVICE} applies it"
|
||||
exit 0
|
||||
fi
|
||||
restarted=1
|
||||
restart_flysim
|
||||
if [ "$wait_for_health" -ne 1 ]; then
|
||||
switching=0
|
||||
record "${command}: flysim restart queued (--no-wait); health not checked"
|
||||
log "restart queued; fly-feed status shows the result"
|
||||
exit 0
|
||||
fi
|
||||
if healthy "$command"; then
|
||||
switching=0
|
||||
if [ "$command" = direct ] && systemctl is-active --quiet "$FLY_EDGE_SERVICE" 2>/dev/null; then
|
||||
log "WARNING: ${FLY_EDGE_SERVICE} is still active in direct mode; stopping it"
|
||||
systemctl stop "$FLY_EDGE_SERVICE" || true
|
||||
fi
|
||||
record "${command}: healthy"
|
||||
log "the feed is served ${command}"
|
||||
exit 0
|
||||
fi
|
||||
log "the feed did not come up healthy (${command}) within ${FLY_FEED_HEALTH_TIMEOUT}s"
|
||||
if [ "$command" = bus ] && [ "$rollback" -eq 1 ]; then
|
||||
set_via "$back"
|
||||
switching=0
|
||||
record "${back} (automatic rollback: ${command} was not healthy within ${FLY_FEED_HEALTH_TIMEOUT}s)"
|
||||
restart_flysim
|
||||
if healthy "$back"; then
|
||||
log "rolled back to ${back}; it is healthy"
|
||||
else
|
||||
log "rolled back to ${back}, and it is NOT healthy either: journalctl -u ${FLY_SERVICE} -u ${FLY_EDGE_SERVICE}"
|
||||
fi
|
||||
else
|
||||
switching=0
|
||||
record "${command}: NOT healthy; left as it is (journalctl -u ${FLY_SERVICE} -u ${FLY_EDGE_SERVICE})"
|
||||
fi
|
||||
exit 1
|
||||
|
|
@ -48,6 +48,7 @@ log_info() {
|
|||
# encoder actually opened (docs/design/gpu.md section 5).
|
||||
: "${FLY_ENCODER_METRIC:=${TEXTFILE_DIR}/fly_encoder.prom}"
|
||||
: "${FLY_ENV_FILE:=/etc/fly/fly.env}"
|
||||
: "${FLY_PROC_DIR:=/proc}"
|
||||
# --- check 9 (capture freeze) inputs, infra/docs/capture-freeze.md ---------
|
||||
: "${FLY_REC_DIR:=/srv/fly/media/rec}" # flycast's 10-minute mpegts segments
|
||||
: "${FFMPEG:=/usr/bin/ffmpeg}"
|
||||
|
|
@ -156,7 +157,7 @@ write_textfile_metrics() {
|
|||
{
|
||||
echo "# HELP fly_watchdog_restarts_total Restarts issued because that unit's own check failed (cascade restarts from another unit's escalation are counted only in fly_watchdog_escalations_total, not here)."
|
||||
echo "# TYPE fly_watchdog_restarts_total counter"
|
||||
for unit in flysim flystage flycast mediamtx flypush flystage-web; do
|
||||
for unit in flysim flystage flycast mediamtx flypush flystage-web flyedge; do
|
||||
echo "fly_watchdog_restarts_total{unit=\"${unit}\"} $(get_counter restarts_total "$unit")"
|
||||
done
|
||||
# Check 7's own restart reason, kept as a separate labelset on the
|
||||
|
|
@ -357,25 +358,81 @@ check_flysim() {
|
|||
# bus"), which exports them under the same names. FLY_FEED_METRICS_URL in the
|
||||
# watchdog's own environment overrides both.
|
||||
# ============================================================================
|
||||
# `direct` or `bus`: who serves the feed RIGHT NOW. What fly.env says is what the next start of
|
||||
# flysim does; what the running flysim started with (its environment: `fly-feed status` reads the
|
||||
# same) is what is true. They differ between a deploy that changed FLY_FEED_VIA and the next
|
||||
# restart, and judging the edge in that window would restart a flysim that is serving the feed
|
||||
# itself. So: the running process when its environment can be read (flysim runs as the same user),
|
||||
# else the file. Lowercased, because flysim reads the value case-insensitively; anything else, or
|
||||
# no line, is direct.
|
||||
feed_via() {
|
||||
local via="" pid=""
|
||||
pid="$(systemctl show -p MainPID --value flysim.service 2>/dev/null | head -n1 || true)"
|
||||
if [ -n "$pid" ] && [ "$pid" != 0 ] && [ -r "${FLY_PROC_DIR}/${pid}/environ" ]; then
|
||||
via="$(tr '\0' '\n' <"${FLY_PROC_DIR}/${pid}/environ" 2>/dev/null | awk -F= '/^FLY_FEED_VIA=/ { print $2; exit }' | tr -d ' \r"' | tr '[:upper:]' '[:lower:]' || true)"
|
||||
elif [ -f "$FLY_ENV_FILE" ]; then
|
||||
via="$(awk -F= '/^FLY_FEED_VIA=/ { print $2; exit }' "$FLY_ENV_FILE" 2>/dev/null | tr -d ' \r"' | tr '[:upper:]' '[:lower:]' || true)"
|
||||
fi
|
||||
if [ "$via" = "bus" ]; then echo bus; else echo direct; fi
|
||||
}
|
||||
|
||||
feed_metrics_url() {
|
||||
if [ -n "${FLY_FEED_METRICS_URL:-}" ]; then
|
||||
echo "$FLY_FEED_METRICS_URL"
|
||||
return
|
||||
fi
|
||||
local via=""
|
||||
if [ -f "$FLY_ENV_FILE" ]; then
|
||||
# Lowercased: flysim reads the value case-insensitively, so `Bus` is bus mode.
|
||||
via="$(awk -F= '/^FLY_FEED_VIA=/ { print $2; exit }' "$FLY_ENV_FILE" 2>/dev/null | tr -d ' \r"' | tr '[:upper:]' '[:lower:]' || true)"
|
||||
fi
|
||||
if [ "$via" = "bus" ]; then
|
||||
if [ "$(feed_via)" = "bus" ]; then
|
||||
echo "$FLY_EDGE_METRICS_URL"
|
||||
else
|
||||
echo "$FLY_METRICS_URL"
|
||||
fi
|
||||
}
|
||||
|
||||
# Check 2a (FLY_FEED_VIA=bus only): the edge itself. flyedge.service active and its /healthz 200
|
||||
# (subscribed to flysim's bus, a snapshot within the last 15 s). Not judged while flysim is down
|
||||
# or restarting (check 1's business: the edge follows it by itself), and one failed pass is let go:
|
||||
# a flysim that has only just answered may not have published yet. After that:
|
||||
# 2nd consecutive failure restart flyedge (the escalation's restart)
|
||||
# 3rd and the page and the encoder (escalate's chain)
|
||||
# 4th the edge cannot be kept up: the feed goes back to flysim, `fly-feed
|
||||
# direct --no-wait` (a restart of flysim, one stream gap, then the page
|
||||
# is served again by the process that always worked). It is logged as
|
||||
# an escalation and leaves FLY_FEED_VIA=direct until an operator
|
||||
# switches again; this check then does nothing.
|
||||
check_flyedge() {
|
||||
if [ "$(feed_via)" != "bus" ]; then
|
||||
record_ok flyedge
|
||||
return 0
|
||||
fi
|
||||
if ! curl -fsS -o /dev/null --max-time 5 "${FLY_CONTROL_URL}/healthz" 2>/dev/null; then
|
||||
return 0
|
||||
fi
|
||||
if is_active flyedge.service \
|
||||
&& curl -fsS -o /dev/null --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>/dev/null; then
|
||||
record_ok flyedge
|
||||
return 0
|
||||
fi
|
||||
record_fail flyedge
|
||||
local n
|
||||
n="$(fail_count flyedge)"
|
||||
if [ "$n" -ge 4 ] && fails_within_minutes flyedge 15; then
|
||||
log_err "flyedge: ${n} consecutive failures, restarts did not bring it back: switching the feed to direct (fly-feed direct)"
|
||||
inc_counter "escalations_total"
|
||||
sudo /opt/fly/bin/fly-feed direct --no-wait || log_err "fly-feed direct FAILED; the edge stays down"
|
||||
record_ok flyedge
|
||||
elif [ "$n" -ge 2 ]; then
|
||||
escalate flyedge flystage flycast
|
||||
fi
|
||||
}
|
||||
|
||||
check_flystage() {
|
||||
local metrics frames clients ok=1
|
||||
local metrics frames clients ok=1 chain=(flycast)
|
||||
if [ "$(feed_via)" = "bus" ] && [ -z "${FLY_FEED_METRICS_URL:-}" ]; then
|
||||
# The edge serves the page. While it is down or silent that is check 2a's to fix, and a
|
||||
# restarted page would only reconnect to nothing (and count towards a reboot).
|
||||
curl -fsS -o /dev/null --max-time 5 "${FLY_EDGE_METRICS_URL}/healthz" 2>/dev/null || return 0
|
||||
chain=(flyedge flycast)
|
||||
fi
|
||||
metrics="$(curl -fsS "$(feed_metrics_url)/metrics" 2>/dev/null || true)"
|
||||
if [ -z "$metrics" ]; then
|
||||
ok=0
|
||||
|
|
@ -402,7 +459,7 @@ check_flystage() {
|
|||
record_ok flystage
|
||||
else
|
||||
record_fail flystage
|
||||
escalate flystage flycast
|
||||
escalate flystage "${chain[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
|
@ -1227,6 +1284,7 @@ check_loop() {
|
|||
|
||||
main() {
|
||||
check_flysim
|
||||
check_flyedge
|
||||
check_flystage
|
||||
check_flycast
|
||||
check_encoder
|
||||
|
|
|
|||
|
|
@ -6,12 +6,13 @@
|
|||
# <unit>` and, on the 5-consecutive-failure escalation, a container
|
||||
# reboot. This is the minimum NOPASSWD surface for exactly what
|
||||
# bin/fly-watchdog's checks actually restart (flysim, flystage, flycast,
|
||||
# mediamtx, flypush), plus reboot. No `systemctl stop`, no
|
||||
# mediamtx, flypush, flyedge in bus mode), plus reboot. No `systemctl stop`, no
|
||||
# `enable`/`disable`, no arbitrary unit name, and no units this script
|
||||
# does not itself restart (xvfb, pulse, flystage-web are never touched by
|
||||
# fly-watchdog's checks, so they are not granted here).
|
||||
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flysim.service
|
||||
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flystage.service
|
||||
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flyedge.service
|
||||
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flycast.service
|
||||
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart mediamtx.service
|
||||
fly ALL=(root) NOPASSWD: /usr/bin/systemctl restart flypush.service
|
||||
|
|
@ -20,3 +21,6 @@ fly ALL=(root) NOPASSWD: /usr/sbin/reboot
|
|||
# resets to a milestone archive through this one root wrapper, which validates the rung,
|
||||
# stops/resets/starts flysim and nothing else (infra/docs/loop-recovery.md).
|
||||
fly ALL=(root) NOPASSWD: /opt/fly/bin/fly-loop-reset
|
||||
# Watchdog check 2a (FLY_FEED_VIA=bus): an edge that restarts cannot fix puts the feed back on
|
||||
# flysim, with exactly these arguments and nothing else (infra/bin/fly-feed).
|
||||
fly ALL=(root) NOPASSWD: /opt/fly/bin/fly-feed direct --no-wait
|
||||
|
|
|
|||
|
|
@ -459,9 +459,6 @@ else
|
|||
grep -qE '^ConditionPathExists=/opt/fly/current/fly-edge$' "$EDGE_UNIT" \
|
||||
&& pass "flyedge.service stays inactive on a release without fly-edge" \
|
||||
|| fail "flyedge.service needs ConditionPathExists=/opt/fly/current/fly-edge (a release before it has none)"
|
||||
grep -qE '^Environment=FLY_EDGE_METRICS_ADDR=127\.0\.0\.1:' "$EDGE_UNIT" \
|
||||
&& pass "flyedge.service keeps its metrics on loopback" \
|
||||
|| fail "flyedge.service FLY_EDGE_METRICS_ADDR must be a 127.0.0.1 address"
|
||||
fi
|
||||
# Every unit a target's Wants=/Requires= names, with backslash continuations joined and
|
||||
# comments dropped: fly.target spreads both lists over several physical lines, and the
|
||||
|
|
@ -1400,6 +1397,354 @@ else
|
|||
rm -rf "$fe_fixture"
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 3b2b. EDGE-02: the feed on the bus in production. fly.env's FLY_FEED_VIA is the one switch:
|
||||
# flysim.service Wants= the edge, the edge's ExecCondition= skips it in direct mode, `fly-feed`
|
||||
# rewrites the line and restarts flysim, the watchdog judges the edge only while flysim itself
|
||||
# serves it through the bus. Driven for real against stubs.
|
||||
# ---------------------------------------------------------------------------
|
||||
echo "--- EDGE-02: flyedge is skipped in direct mode, started by every flysim start in bus mode ---"
|
||||
grep -qE '^Wants=.*\bflyedge\.service\b' "$INFRA_DIR/units/flysim.service" \
|
||||
&& pass "flysim.service Wants=flyedge.service (every flysim start brings the edge up in bus mode)" \
|
||||
|| fail "flysim.service must Want flyedge.service: a stop and start of flysim (fly-loop-reset) would leave the edge down"
|
||||
grep -qE '^ExecCondition=.*FLY_FEED_VIA.*\[Bb\]\[Uu\]\[Ss\]' "$EDGE_UNIT" \
|
||||
&& pass "flyedge.service runs only when FLY_FEED_VIA is bus (ExecCondition=, any case)" \
|
||||
|| fail "flyedge.service needs an ExecCondition= on FLY_FEED_VIA (case-insensitive bus)"
|
||||
if grep -qE '^\[Install\]' "$EDGE_UNIT"; then
|
||||
fail "flyedge.service has an [Install] section; flysim's Wants= is what starts it"
|
||||
else
|
||||
pass "flyedge.service has no [Install] section"
|
||||
fi
|
||||
grep -qE '^Environment=FLY_EDGE_METRICS_ADDR=[0-9.]+:9102$' "$EDGE_UNIT" \
|
||||
&& pass "flyedge.service serves its metrics on :9102 (the dashboard's second target; the watchdog uses loopback)" \
|
||||
|| fail "flyedge.service FLY_EDGE_METRICS_ADDR must be an address on :9102"
|
||||
if grep -qE '^Wants=.*flyedge' "$INFRA_DIR/units/flysim-session.service"; then
|
||||
fail "flysim-session.service Wants flyedge.service, whose Requires=flysim.service would start the unit it Conflicts with"
|
||||
else
|
||||
pass "flysim-session.service (the standalone rehearsal unit) does not pull the edge in"
|
||||
fi
|
||||
grep -qE 'for name in .*\bfly-feed\b.*; do$' "$INFRA_DIR/05-deploy.sh" \
|
||||
&& pass "05-deploy.sh installs fly-feed" \
|
||||
|| fail "05-deploy.sh must converge bin/fly-feed to /opt/fly/bin"
|
||||
grep -qF 'this release has no fly-edge' "$INFRA_DIR/05-deploy.sh" \
|
||||
&& grep -qF '"${release_path}/fly-edge"' "$INFRA_DIR/05-deploy.sh" \
|
||||
&& pass "05-deploy.sh refuses a release without fly-edge while the feed is on the bus (before the symlink moves)" \
|
||||
|| fail "05-deploy.sh must refuse a release without fly-edge when FLY_FEED_VIA is bus (env file or container)"
|
||||
grep -qE '^fly ALL=\(root\) NOPASSWD: /usr/bin/systemctl restart flyedge\.service$' "$INFRA_DIR/config/fly-sudoers" \
|
||||
&& grep -qE '^fly ALL=\(root\) NOPASSWD: /opt/fly/bin/fly-feed direct --no-wait$' "$INFRA_DIR/config/fly-sudoers" \
|
||||
&& pass "sudoers lets the watchdog restart flyedge and run exactly 'fly-feed direct --no-wait'" \
|
||||
|| fail "config/fly-sudoers must grant the watchdog 'systemctl restart flyedge.service' and 'fly-feed direct --no-wait'"
|
||||
|
||||
echo "--- fly-feed: bus, direct, status, rollback ---"
|
||||
ff_dir="$(mktemp -d "${TMPDIR:-/tmp}/fly-lint-feed.XXXXXX")"
|
||||
mkdir -p "$ff_dir/bin" "$ff_dir/release" "$ff_dir/proc/4242" "$ff_dir/run"
|
||||
cat > "$ff_dir/bin/systemctl" <<'FFSTUB'
|
||||
#!/usr/bin/env bash
|
||||
echo "$*" >> "$FF_DIR/systemctl.log"
|
||||
case "$1" in
|
||||
restart) n=$(( $(cat "$FF_DIR/invocation" 2>/dev/null || echo 1) + 1 )); echo "$n" > "$FF_DIR/invocation"
|
||||
# the new flysim's environment is whatever fly.env says now
|
||||
v="$(awk -F= '/^FLY_FEED_VIA=/ { v=$2 } END { print v }' "$FF_ENV")"
|
||||
printf 'FLY_FEED_VIA=%s\0' "$v" > "$FF_DIR/proc/4242/environ" ;;
|
||||
show) case "$*" in
|
||||
*MainPID*) echo 4242 ;;
|
||||
*InvocationID*) echo "inv$(cat "$FF_DIR/invocation" 2>/dev/null || echo 1)" ;;
|
||||
esac ;;
|
||||
is-active)
|
||||
case "${*: -1}" in
|
||||
flystage.service) [ "${FF_PAGE:-1}" = 1 ]; exit $? ;;
|
||||
flyedge.service) [ "${FF_EDGE_ACTIVE:-0}" = 1 ]; exit $? ;;
|
||||
esac ;;
|
||||
esac
|
||||
exit 0
|
||||
FFSTUB
|
||||
cat > "$ff_dir/bin/curl" <<'FFSTUB'
|
||||
#!/usr/bin/env bash
|
||||
url="${*: -1}"
|
||||
quiet=0; for a in "$@"; do [ "$a" = /dev/null ] && quiet=1; done
|
||||
case "$url" in
|
||||
http://control/healthz) [ "${FF_CONTROL_OK:-1}" = 1 ] || exit 22; exit 0 ;;
|
||||
http://edge/healthz) [ "${FF_EDGE_OK:-1}" = 1 ] || exit 22; exit 0 ;;
|
||||
http://sim/metrics|http://edge/metrics)
|
||||
n=$(( $(cat "$FF_DIR/frames" 2>/dev/null || echo 1000) + 30 )); echo "$n" > "$FF_DIR/frames"
|
||||
[ "${FF_FLAT:-0}" = 1 ] && n=1000
|
||||
echo "fly_feed_clients ${FF_CLIENTS:-1}"; echo "fly_frames_sent_total $n"
|
||||
echo "fly_edge_snapshot_age_seconds 0.03"; echo "fly_edge_bus_lost_total 0" ;;
|
||||
*) exit 22 ;;
|
||||
esac
|
||||
FFSTUB
|
||||
printf '#!/usr/bin/env bash\necho 0\n' > "$ff_dir/bin/id"
|
||||
printf '#!/usr/bin/env bash\ntrue\n' > "$ff_dir/bin/logger"
|
||||
printf '#!/usr/bin/env bash\ntrue\n' > "$ff_dir/release/fly-edge"
|
||||
chmod +x "$ff_dir"/bin/* "$ff_dir/release/fly-edge"
|
||||
ff_env="$ff_dir/fly.env"
|
||||
fly_feed() {
|
||||
env FF_DIR="$ff_dir" FF_ENV="$ff_env" PATH="$ff_dir/bin:$PATH" FLY_ENV_FILE="$ff_env" \
|
||||
FLY_RELEASE_DIR="$ff_dir/release" FLY_PROC_DIR="$ff_dir/proc" FLY_FEED_LOG="$ff_dir/feed.log" \
|
||||
FLY_FEED_LOCK="$ff_dir/run/feed.lock" FLY_CONTROL_URL=http://control FLY_METRICS_URL=http://sim \
|
||||
FLY_EDGE_METRICS_URL=http://edge FLY_FEED_HEALTH_TIMEOUT=3 FLY_FEED_SAMPLE_SECONDS=0 \
|
||||
"$@" bash "$INFRA_DIR/bin/fly-feed" "${FF_ARGS[@]}"
|
||||
}
|
||||
ff_reset() { # ff_reset VIA_IN_FILE VIA_RUNNING
|
||||
printf 'FLY_GAME=pokemon-red\nFLY_FEED_VIA=%s\nFLY_MACRO_MODE=raw\n' "$1" > "$ff_env"
|
||||
chmod 0640 "$ff_env"
|
||||
printf 'FLY_FEED_VIA=%s\0' "$2" > "$ff_dir/proc/4242/environ"
|
||||
: > "$ff_dir/systemctl.log"; rm -f "$ff_dir/feed.log" "$ff_dir/frames" "$ff_dir/invocation"
|
||||
}
|
||||
ff_restarts() { grep -c '^restart --no-block flysim.service$' "$ff_dir/systemctl.log" || true; }
|
||||
|
||||
ff_reset direct direct
|
||||
FF_ARGS=(status)
|
||||
ff_out="$(fly_feed 2>&1 || true)"
|
||||
if printf '%s\n' "$ff_out" | grep -qx 'configured: direct' && printf '%s\n' "$ff_out" | grep -qx 'running: direct'; then
|
||||
pass "fly-feed status: direct, running direct"
|
||||
else
|
||||
fail "fly-feed status wrong: $ff_out"
|
||||
fi
|
||||
|
||||
ff_reset direct direct
|
||||
FF_ARGS=(bus)
|
||||
if fly_feed && [ "$(grep -c '^FLY_FEED_VIA=' "$ff_env")" = 1 ] && grep -qx 'FLY_FEED_VIA=bus' "$ff_env" \
|
||||
&& grep -qx 'FLY_GAME=pokemon-red' "$ff_env" && grep -qx 'FLY_MACRO_MODE=raw' "$ff_env" \
|
||||
&& [ "$(stat -c %a "$ff_env")" = 640 ] && [ "$(ff_restarts)" = 1 ] && grep -q 'bus: healthy' "$ff_dir/feed.log"; then
|
||||
pass "fly-feed bus: one FLY_FEED_VIA line, the rest and the mode kept, flysim restarted once, healthy on the edge's counters"
|
||||
else
|
||||
fail "fly-feed bus: wrong ($(tr '\n' ' ' < "$ff_env"); restarts $(ff_restarts); $(cat "$ff_dir/feed.log" 2>/dev/null))"
|
||||
fi
|
||||
FF_ARGS=(bus)
|
||||
: > "$ff_dir/systemctl.log"
|
||||
if fly_feed && [ "$(ff_restarts)" = 0 ]; then
|
||||
pass "fly-feed bus when already bus and running bus: nothing to do, no restart"
|
||||
else
|
||||
fail "fly-feed bus must not restart flysim when fly.env and the running process already say bus"
|
||||
fi
|
||||
# A deploy wrote bus but the running flysim started direct: `bus` applies it (and a bus fly.env is not its own way back).
|
||||
ff_reset bus direct
|
||||
FF_ARGS=(bus)
|
||||
if fly_feed && [ "$(ff_restarts)" = 1 ] && grep -qx 'FLY_FEED_VIA=bus' "$ff_env"; then
|
||||
pass "fly-feed bus applies a fly.env that says bus to a flysim that started direct"
|
||||
else
|
||||
fail "fly-feed bus must restart flysim when fly.env says bus and it is running direct"
|
||||
fi
|
||||
ff_reset bus bus
|
||||
FF_ARGS=(status)
|
||||
ff_out="$(fly_feed 2>&1 || true)"
|
||||
if printf '%s\n' "$ff_out" | grep -q '^edge healthz: ok'; then
|
||||
pass "fly-feed status in bus mode reads the edge"
|
||||
else
|
||||
fail "fly-feed status in bus mode must report the edge's healthz"
|
||||
fi
|
||||
ff_reset Bus direct
|
||||
FF_ARGS=(status)
|
||||
ff_out="$(fly_feed 2>&1 || true)"
|
||||
printf '%s\n' "$ff_out" | grep -qx 'configured: bus' \
|
||||
&& pass "fly-feed reads FLY_FEED_VIA=Bus as bus (flysim lowercases it)" \
|
||||
|| fail "fly-feed must read FLY_FEED_VIA case-insensitively"
|
||||
|
||||
# A bus that never becomes healthy (the edge answers 503) rolls back to direct and says so.
|
||||
ff_reset direct direct
|
||||
FF_ARGS=(bus)
|
||||
if fly_feed FF_EDGE_OK=0; then
|
||||
fail "fly-feed bus must exit non-zero when the edge never becomes healthy"
|
||||
elif grep -qx 'FLY_FEED_VIA=direct' "$ff_env" && [ "$(ff_restarts)" = 2 ] && grep -q 'automatic rollback' "$ff_dir/feed.log"; then
|
||||
pass "fly-feed bus rolls back to direct, restarting flysim again, when the edge is not healthy"
|
||||
else
|
||||
fail "fly-feed bus rollback wrong ($(grep FLY_FEED_VIA "$ff_env"); restarts $(ff_restarts))"
|
||||
fi
|
||||
# A page that never reaches the feed (no clients) is not healthy either; --no-rollback leaves the switch in place.
|
||||
ff_reset direct direct
|
||||
FF_ARGS=(bus)
|
||||
if fly_feed FF_CLIENTS=0; then
|
||||
fail "fly-feed bus must not call a feed with no page on it healthy while flystage is active"
|
||||
elif grep -qx 'FLY_FEED_VIA=direct' "$ff_env"; then
|
||||
pass "fly-feed bus: a page that never connects is a failed switch, rolled back"
|
||||
else
|
||||
fail "fly-feed bus with no client must roll back"
|
||||
fi
|
||||
ff_reset direct direct
|
||||
FF_ARGS=(bus --no-rollback)
|
||||
if fly_feed FF_EDGE_OK=0; then
|
||||
fail "--no-rollback: must still fail"
|
||||
elif grep -qx 'FLY_FEED_VIA=bus' "$ff_env" && [ "$(ff_restarts)" = 1 ]; then
|
||||
pass "fly-feed bus --no-rollback leaves the switch as it is when unhealthy"
|
||||
else
|
||||
fail "--no-rollback must leave FLY_FEED_VIA=bus and restart once"
|
||||
fi
|
||||
# Frames that never advance are not a feed.
|
||||
ff_reset direct direct
|
||||
FF_ARGS=(bus)
|
||||
if fly_feed FF_FLAT=1; then fail "flat frames counter must not be healthy"; else pass "fly-feed bus: a frames counter that does not move is not healthy"; fi
|
||||
# No page unit: the wait ends at the edge's healthz.
|
||||
ff_reset direct direct
|
||||
FF_ARGS=(bus)
|
||||
fly_feed FF_PAGE=0 FF_CLIENTS=0 \
|
||||
&& pass "fly-feed bus on a box with no active page unit waits for the edge only" \
|
||||
|| fail "fly-feed bus with flystage inactive must succeed on the edge's healthz alone"
|
||||
# A release without fly-edge refuses before touching anything.
|
||||
ff_reset direct direct
|
||||
rm -f "$ff_dir/release/fly-edge"
|
||||
FF_ARGS=(bus)
|
||||
if fly_feed; then
|
||||
fail "fly-feed bus must refuse a release without fly-edge"
|
||||
elif grep -qx 'FLY_FEED_VIA=direct' "$ff_env" && [ "$(ff_restarts)" = 0 ]; then
|
||||
pass "fly-feed bus refuses a release without fly-edge, changing and restarting nothing"
|
||||
else
|
||||
fail "refusal must leave fly.env and flysim alone"
|
||||
fi
|
||||
printf '#!/usr/bin/env bash\ntrue\n' > "$ff_dir/release/fly-edge"; chmod +x "$ff_dir/release/fly-edge"
|
||||
# direct: the way back, which works with no fly-edge at all and stops a lingering edge.
|
||||
ff_reset bus bus
|
||||
rm -f "$ff_dir/release/fly-edge"
|
||||
FF_ARGS=(direct)
|
||||
if fly_feed FF_EDGE_ACTIVE=1 && grep -qx 'FLY_FEED_VIA=direct' "$ff_env" && [ "$(ff_restarts)" = 1 ] \
|
||||
&& grep -qx 'stop flyedge.service' "$ff_dir/systemctl.log" && grep -q 'direct: healthy' "$ff_dir/feed.log"; then
|
||||
pass "fly-feed direct: back to flysim on :7400 with no fly-edge in the release, a lingering edge stopped"
|
||||
else
|
||||
fail "fly-feed direct wrong ($(grep FLY_FEED_VIA "$ff_env"); $(cat "$ff_dir/systemctl.log" | tr '\n' ';'))"
|
||||
fi
|
||||
printf '#!/usr/bin/env bash\ntrue\n' > "$ff_dir/release/fly-edge"; chmod +x "$ff_dir/release/fly-edge"
|
||||
ff_reset bus bus
|
||||
FF_ARGS=(direct --no-restart)
|
||||
fly_feed && grep -qx 'FLY_FEED_VIA=direct' "$ff_env" && [ "$(ff_restarts)" = 0 ] \
|
||||
&& pass "fly-feed direct --no-restart writes the line and restarts nothing" \
|
||||
|| fail "--no-restart must only write fly.env"
|
||||
ff_reset bus bus
|
||||
FF_ARGS=(direct --no-wait)
|
||||
fly_feed FF_CONTROL_OK=0 && [ "$(ff_restarts)" = 1 ] && grep -qx 'FLY_FEED_VIA=direct' "$ff_env" \
|
||||
&& pass "fly-feed direct --no-wait (the watchdog's fallback) restarts flysim and returns without a health wait" \
|
||||
|| fail "--no-wait must restart flysim and return 0 whatever the health"
|
||||
ff_reset 'buss' direct
|
||||
FF_ARGS=(bus)
|
||||
if fly_feed; then fail "fly-feed must refuse an invalid FLY_FEED_VIA"; else pass "fly-feed refuses an invalid FLY_FEED_VIA in fly.env, touching nothing"; fi
|
||||
# An interrupted switch (INT/TERM/HUP) rolls back: the stub restart hangs the health wait; kill it mid-way.
|
||||
ff_reset direct direct
|
||||
FF_ARGS=(bus)
|
||||
env FF_DIR="$ff_dir" FF_ENV="$ff_env" PATH="$ff_dir/bin:$PATH" FLY_ENV_FILE="$ff_env" \
|
||||
FLY_RELEASE_DIR="$ff_dir/release" FLY_PROC_DIR="$ff_dir/proc" FLY_FEED_LOG="$ff_dir/feed.log" \
|
||||
FLY_FEED_LOCK="$ff_dir/run/feed.lock" FLY_CONTROL_URL=http://control FLY_METRICS_URL=http://sim \
|
||||
FLY_EDGE_METRICS_URL=http://edge FLY_FEED_HEALTH_TIMEOUT=30 FLY_FEED_SAMPLE_SECONDS=0 FF_EDGE_OK=0 \
|
||||
bash "$INFRA_DIR/bin/fly-feed" bus >/dev/null 2>&1 &
|
||||
ff_pid=$!
|
||||
sleep 2
|
||||
kill -TERM "$ff_pid" 2>/dev/null || true
|
||||
for _ in 1 2 3 4 5 6 7 8 9 10; do kill -0 "$ff_pid" 2>/dev/null || break; sleep 0.5; done
|
||||
(kill -0 "$ff_pid" 2>/dev/null && kill -KILL "$ff_pid" 2>/dev/null) || true
|
||||
wait "$ff_pid" 2>/dev/null || true
|
||||
if grep -qx 'FLY_FEED_VIA=direct' "$ff_env" && grep -q 'interrupted or failed' "$ff_dir/feed.log" 2>/dev/null; then
|
||||
pass "fly-feed bus interrupted mid-switch puts FLY_FEED_VIA back"
|
||||
else
|
||||
fail "an interrupted fly-feed bus must roll back ($(grep FLY_FEED_VIA "$ff_env"); $(cat "$ff_dir/feed.log" 2>/dev/null | tr '\n' ';'))"
|
||||
fi
|
||||
rm -rf "$ff_dir"
|
||||
|
||||
echo "--- fly-watchdog: check 2a (the edge) and check 2 in bus mode ---"
|
||||
wf_dir="$(mktemp -d "${TMPDIR:-/tmp}/fly-lint-wdedge.XXXXXX")"
|
||||
mkdir -p "$wf_dir/bin" "$wf_dir/proc/4242" "$wf_dir/run" "$wf_dir/state" "$wf_dir/textfile"
|
||||
cat > "$wf_dir/bin/systemctl" <<'WFSTUB'
|
||||
#!/usr/bin/env bash
|
||||
case "$1" in
|
||||
show) echo 4242 ;;
|
||||
is-active) [ "${WF_EDGE_ACTIVE:-1}" = 1 ]; exit $? ;;
|
||||
restart) echo "restart $2" >> "$WF_DIR/actions.log" ;;
|
||||
esac
|
||||
exit 0
|
||||
WFSTUB
|
||||
cat > "$wf_dir/bin/sudo" <<'WFSTUB'
|
||||
#!/usr/bin/env bash
|
||||
if [ "$1" = /usr/bin/systemctl ]; then shift; exec systemctl "$@"; fi
|
||||
echo "sudo $*" >> "$WF_DIR/actions.log"
|
||||
exit 0
|
||||
WFSTUB
|
||||
cat > "$wf_dir/bin/curl" <<'WFSTUB'
|
||||
#!/usr/bin/env bash
|
||||
url="${*: -1}"
|
||||
case "$url" in
|
||||
http://control/healthz) [ "${WF_CONTROL_OK:-1}" = 1 ] || exit 22 ;;
|
||||
http://edge/healthz) [ "${WF_EDGE_OK:-1}" = 1 ] || exit 22 ;;
|
||||
http://edge/metrics) echo "fly_frames_sent_total ${WF_EDGE_FRAMES:-100}"; echo "fly_feed_clients 1" ;;
|
||||
http://sim/metrics) echo "fly_frames_sent_total ${WF_SIM_FRAMES:-0}"; echo "fly_feed_clients 0" ;;
|
||||
*) exit 22 ;;
|
||||
esac
|
||||
WFSTUB
|
||||
printf '#!/usr/bin/env bash\ncat >/dev/null\n' > "$wf_dir/bin/systemd-cat"
|
||||
chmod +x "$wf_dir"/bin/*
|
||||
sed '$d' "$INFRA_DIR/bin/fly-watchdog" > "$wf_dir/wd.sh"
|
||||
wd_edge() { # wd_edge VIA_RUNNING 'ENV...' -- COMMANDS
|
||||
local via="$1"; shift
|
||||
printf 'FLY_FEED_VIA=%s\0' "$via" > "$wf_dir/proc/4242/environ"
|
||||
env WF_DIR="$wf_dir" PATH="$wf_dir/bin:$PATH" FLY_PROC_DIR="$wf_dir/proc" FLY_ENV_FILE="$wf_dir/fly.env" \
|
||||
FLY_CONTROL_URL=http://control FLY_METRICS_URL=http://sim FLY_EDGE_METRICS_URL=http://edge \
|
||||
WD_RUN_DIR="$wf_dir/run" WD_STATE_DIR="$wf_dir/state" TEXTFILE_DIR="$wf_dir/textfile" \
|
||||
"$@" bash -c "source '$wf_dir/wd.sh'; $WD_CMDS" >/dev/null 2>&1 || true
|
||||
}
|
||||
wd_acts() { cat "$wf_dir/actions.log" 2>/dev/null | tr '\n' ';'; }
|
||||
wd_reset() { : > "$wf_dir/actions.log"; rm -f "$wf_dir/run"/*; }
|
||||
echo 'FLY_FEED_VIA=bus' > "$wf_dir/fly.env"
|
||||
|
||||
wd_reset; WD_CMDS='check_flyedge; check_flyedge; check_flyedge'
|
||||
wd_edge direct
|
||||
[ -z "$(wd_acts)" ] && pass "check 2a does nothing in direct mode" || fail "check 2a acted in direct mode: $(wd_acts)"
|
||||
wd_reset; WD_CMDS='check_flyedge; check_flyedge; check_flyedge; check_flyedge'
|
||||
wd_edge bus WF_EDGE_OK=0 WF_CONTROL_OK=0
|
||||
[ -z "$(wd_acts)" ] && pass "check 2a leaves a down flysim to check 1" || fail "check 2a acted while flysim was down: $(wd_acts)"
|
||||
wd_reset; WD_CMDS='check_flyedge; check_flyedge; check_flyedge'
|
||||
wd_edge bus WF_EDGE_OK=1
|
||||
[ -z "$(wd_acts)" ] && pass "check 2a: a healthy edge is left alone" || fail "check 2a acted on a healthy edge: $(wd_acts)"
|
||||
wd_reset; WD_CMDS='check_flyedge'
|
||||
wd_edge bus WF_EDGE_OK=0
|
||||
[ -z "$(wd_acts)" ] && pass "check 2a lets the first failed pass go (a flysim that only just answered)" || fail "check 2a acted on the first failure: $(wd_acts)"
|
||||
wd_reset; WD_CMDS='check_flyedge; check_flyedge'
|
||||
wd_edge bus WF_EDGE_OK=0
|
||||
case "$(wd_acts)" in
|
||||
"restart flyedge.service;") pass "check 2a: the second failed pass restarts flyedge" ;;
|
||||
*) fail "check 2a second failure should restart flyedge only: $(wd_acts)" ;;
|
||||
esac
|
||||
wd_reset; WD_CMDS='check_flyedge; check_flyedge; check_flyedge'
|
||||
wd_edge bus WF_EDGE_OK=0
|
||||
case "$(wd_acts)" in
|
||||
"restart flyedge.service;restart flyedge.service;restart flystage.service;restart flycast.service;") pass "check 2a: the third restarts the page and the encoder as well" ;;
|
||||
*) fail "check 2a third failure: $(wd_acts)" ;;
|
||||
esac
|
||||
wd_reset; WD_CMDS='check_flyedge; check_flyedge; check_flyedge; check_flyedge'
|
||||
wd_edge bus WF_EDGE_OK=0
|
||||
case "$(wd_acts)" in
|
||||
*"sudo /opt/fly/bin/fly-feed direct --no-wait;") pass "check 2a: the fourth failed pass puts the feed back on flysim (fly-feed direct --no-wait)" ;;
|
||||
*) fail "check 2a fourth failure must run fly-feed direct --no-wait: $(wd_acts)" ;;
|
||||
esac
|
||||
wd_reset; WD_CMDS='check_flyedge; check_flyedge'
|
||||
wd_edge bus WF_EDGE_OK=1 WF_EDGE_ACTIVE=0
|
||||
case "$(wd_acts)" in
|
||||
"restart flyedge.service;") pass "check 2a: an inactive edge in bus mode is a failure like an unhealthy one" ;;
|
||||
*) fail "check 2a inactive edge: $(wd_acts)" ;;
|
||||
esac
|
||||
# fly.env says bus but the running flysim started direct (a deploy, not yet restarted): it serves the feed itself.
|
||||
wd_reset; WD_CMDS='check_flyedge; check_flyedge; check_flyedge; check_flyedge'
|
||||
wd_edge direct WF_EDGE_OK=0 WF_EDGE_ACTIVE=0
|
||||
[ -z "$(wd_acts)" ] && pass "check 2a follows the running flysim, not fly.env: a pending switch is not an outage" || fail "check 2a acted on a pending switch: $(wd_acts)"
|
||||
WD_CMDS='feed_metrics_url'
|
||||
got="$(printf 'FLY_FEED_VIA=direct\0' > "$wf_dir/proc/4242/environ"; env WF_DIR="$wf_dir" PATH="$wf_dir/bin:$PATH" FLY_PROC_DIR="$wf_dir/proc" FLY_ENV_FILE="$wf_dir/fly.env" FLY_METRICS_URL=http://sim FLY_EDGE_METRICS_URL=http://edge WD_RUN_DIR="$wf_dir/run" WD_STATE_DIR="$wf_dir/state" TEXTFILE_DIR="$wf_dir/textfile" bash -c "source '$wf_dir/wd.sh'; feed_metrics_url" 2>/dev/null || true)"
|
||||
[ "$got" = http://sim ] && pass "check 2 reads flysim's counters while the running flysim serves the feed itself" || fail "check 2 URL for a pending switch: $got"
|
||||
|
||||
# Check 2 in bus mode: an unhealthy edge is check 2a's, not a reason to restart the page; a healthy
|
||||
# edge with a flat frames counter restarts the page and, after three, the edge and the encoder.
|
||||
wd_reset; WD_CMDS='check_flystage; check_flystage; check_flystage'
|
||||
wd_edge bus WF_EDGE_OK=0
|
||||
[ -z "$(wd_acts)" ] && pass "check 2 in bus mode leaves a silent edge to check 2a (the page is not restarted)" || fail "check 2 restarted things while the edge was down: $(wd_acts)"
|
||||
wd_reset; WD_CMDS='check_flystage; check_flystage; check_flystage; check_flystage'
|
||||
wd_edge bus WF_EDGE_OK=1 WF_EDGE_FRAMES=100
|
||||
case "$(wd_acts)" in
|
||||
"restart flystage.service;restart flystage.service;restart flystage.service;restart flyedge.service;restart flycast.service;")
|
||||
pass "check 2 in bus mode: frames flat restarts the page, then the edge and the encoder in the chain" ;;
|
||||
*) fail "check 2 bus chain: $(wd_acts)" ;;
|
||||
esac
|
||||
wd_reset; WD_CMDS='check_flystage; check_flystage; check_flystage; check_flystage'
|
||||
wd_edge direct WF_SIM_FRAMES=100
|
||||
case "$(wd_acts)" in
|
||||
*"restart flyedge"*) fail "check 2 in direct mode must not touch the edge: $(wd_acts)" ;;
|
||||
*) pass "check 2 in direct mode never restarts the edge" ;;
|
||||
esac
|
||||
rm -rf "$wf_dir"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 3c. lib/common.sh cpuset_partition — the three-way cpuset split used by
|
||||
# 05-deploy.sh section 3b (flysim / page-capture / flycast). Run as its own
|
||||
|
|
|
|||
|
|
@ -1,30 +1,41 @@
|
|||
# infra/units/flyedge.service — pushed to /etc/systemd/system/flyedge.service.
|
||||
#
|
||||
# The feed WebSocket served from flysim's feed bus (docs/design/flybus.md,
|
||||
# "Feed over the bus"; services/flysim/crates/fly-edge). DISABLED BY DEFAULT:
|
||||
# it is in no target's Wants=/Requires= and 07-enable.sh does not enable it.
|
||||
# With FLY_FEED_VIA=direct (the default, written into /etc/fly/fly.env by
|
||||
# 05-deploy.sh) flysim binds 127.0.0.1:7400 itself and this unit has nothing
|
||||
# to do. To move the feed onto the bus on one container:
|
||||
# "Feed over the bus"; services/flysim/crates/fly-edge). Whether it runs is
|
||||
# decided by one line, FLY_FEED_VIA in /etc/fly/fly.env (05-deploy.sh writes it
|
||||
# from the env file's FLY_FEED_VIA, default `direct`):
|
||||
#
|
||||
# 1. FLY_FEED_VIA=bus in the env file, then 05-deploy.sh (rewrites fly.env);
|
||||
# 2. systemctl enable --now flyedge.service; systemctl restart flysim.service
|
||||
# (flysim stops binding :7400, the edge binds it once the first snapshot
|
||||
# is on the bus);
|
||||
# 3. nothing for the watchdog: check 2 reads FLY_FEED_VIA from fly.env and
|
||||
# follows the feed counters to this unit's loopback /metrics.
|
||||
# direct flysim binds 127.0.0.1:7400 itself. ExecCondition= below fails, so
|
||||
# this unit starts as "skipped" (inactive, no failure, no restart).
|
||||
# bus flysim publishes on its feed bus and leaves :7400 to this unit.
|
||||
#
|
||||
# Back: FLY_FEED_VIA=direct, deploy, systemctl disable --now flyedge.service,
|
||||
# restart flysim.
|
||||
# flysim.service has Wants= on this unit, so EVERY start of flysim (the unstick
|
||||
# rule's restart, fly-loop-reset's stop and start, fly-reset-to-milestone, a
|
||||
# crash-restart, a reboot through fly.target) starts the edge in bus mode and
|
||||
# skips it in direct mode; fly.env alone decides, and flysim and the edge can
|
||||
# never disagree. This unit is in no target and has no [Install] section:
|
||||
# `systemctl enable` has nothing to do.
|
||||
#
|
||||
# Switching a running container (infra/bin/fly-feed, "Feed over the bus" in
|
||||
# docs/runbook.md; it is the whole procedure, with a health check and its own
|
||||
# rollback):
|
||||
#
|
||||
# fly-feed bus FLY_FEED_VIA=bus in fly.env, restart flysim, wait for
|
||||
# the edge and a client
|
||||
# fly-feed direct the way back
|
||||
#
|
||||
# Ordering (docs/design/flybus.md, amendment "Feed store lifecycle"): flysim
|
||||
# owns the router and its store under /run/fly/bus, so it starts first and
|
||||
# the edge follows it. Requires= makes an explicit stop or restart of flysim
|
||||
# (the unstick rule's `systemctl restart flysim.service` included) stop or
|
||||
# restart the edge with it. A crash-restart of flysim is covered by the edge
|
||||
# itself: it drops its clients, unbinds :7400 and reconnects every 500 ms,
|
||||
# so nothing here has to be restarted by hand. The edge holds no state; the
|
||||
# store is flysim's and a new router removes the previous one's directory.
|
||||
# owns the router and its store under /run/fly/bus, so it starts first and the
|
||||
# edge follows it. Requires= makes an explicit stop or restart of flysim stop
|
||||
# or restart the edge with it. A crash-restart of flysim is covered by the edge
|
||||
# itself: it drops its clients, unbinds :7400 and reconnects every 500 ms, so
|
||||
# nothing here has to be restarted by hand. The edge holds no state; the store
|
||||
# is flysim's and a new router removes the previous one's directory.
|
||||
#
|
||||
# CPUs: 05-deploy.sh writes this unit's cpuset drop-in on the page CPUs, never
|
||||
# flysim's (the sim's CPUs are the sim's alone, docs/runbook.md "CPU partition").
|
||||
# The router and the publisher run inside flysim, on its own two-thread bus
|
||||
# runtime.
|
||||
[Unit]
|
||||
Description=flyedge: the feed WebSocket served from flysim's feed bus
|
||||
After=flysim.service
|
||||
|
|
@ -42,15 +53,17 @@ User=fly
|
|||
EnvironmentFile=/etc/fly/fly.env
|
||||
Environment=FLY_FEED_BIND=127.0.0.1:7400
|
||||
Environment=FLY_BUS_DIR=/run/fly/bus
|
||||
# Its own read-only /metrics and /healthz, for watchdog check 2 in bus mode.
|
||||
# Loopback only: nothing off the container needs the edge's counters.
|
||||
Environment=FLY_EDGE_METRICS_ADDR=127.0.0.1:9102
|
||||
# Its own read-only /metrics and /healthz: watchdog check 2 reads them in bus mode (over
|
||||
# loopback), and so can the metrics dashboard, as a second target beside flysim's :9101, which
|
||||
# reads 0 for the feed counters in bus mode. Same exposure as :9101 (the container's network).
|
||||
Environment=FLY_EDGE_METRICS_ADDR=0.0.0.0:9102
|
||||
# Only in bus mode. EnvironmentFile= is the same fly.env flysim reads, so this is the value
|
||||
# flysim started with (flysim reads it case-insensitively, so does this). Exit 1 means
|
||||
# "skip": the unit goes inactive without failing and without being restarted.
|
||||
ExecCondition=/bin/sh -c 'case "$$FLY_FEED_VIA" in [Bb][Uu][Ss]) ;; *) exit 1;; esac'
|
||||
ExecStart=/opt/fly/current/fly-edge
|
||||
Restart=always
|
||||
RestartSec=2
|
||||
# A few snapshots in flight and a WebSocket per client; the store itself is
|
||||
# flysim's (tmpfs, bounded by feedbus::limits at 32 MiB).
|
||||
MemoryMax=256M
|
||||
|
||||
[Install]
|
||||
WantedBy=fly.target
|
||||
|
|
|
|||
|
|
@ -14,6 +14,11 @@
|
|||
[Unit]
|
||||
Description=flysim: the fly brain, emulator, and control/feed APIs
|
||||
After=network.target
|
||||
# Every start of flysim starts the feed edge when fly.env says FLY_FEED_VIA=bus, and skips it in
|
||||
# direct mode (units/flyedge.service decides): a stop and start of flysim (fly-loop-reset,
|
||||
# fly-reset-to-milestone) would otherwise leave the stage with nothing on :7400, because
|
||||
# flyedge.service's Requires= stops the edge with flysim but does not bring it back.
|
||||
Wants=flyedge.service
|
||||
StartLimitIntervalSec=300
|
||||
StartLimitBurst=5
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue