|
| 1 | +#!/bin/bash |
| 2 | +# Per-cluster circuit breaker for environment-wide outages. |
| 3 | +# |
| 4 | +# Some failures are not the node's fault and not the code's fault: pypi.org |
| 5 | +# unreachable from a login node, a module tree mid-upgrade, a full project |
| 6 | +# filesystem. Requeuing elsewhere cannot help, and every job that starts pays |
| 7 | +# the same discovery cost -- on 2026-08-28, 17 Frontier jobs each spent ~33 |
| 8 | +# minutes learning that PyPI was down. |
| 9 | +# |
| 10 | +# The first job to notice records a marker on the shared filesystem (every |
| 11 | +# self-hosted runner for a cluster shares $HOME); later jobs check it and exit |
| 12 | +# immediately instead of submitting a SLURM job that is going to fail. |
| 13 | +# |
| 14 | +# The breaker is deliberately self-healing. A marker expires after |
| 15 | +# MFC_CI_OUTAGE_TTL_SECONDS, and a marker that cannot be parsed is ignored, so |
| 16 | +# neither a stale file nor a truncated write can wedge CI. Only jobs that |
| 17 | +# actually observe the outage re-mark it, so once the outage clears the breaker |
| 18 | +# closes on its own. |
| 19 | +# |
| 20 | +# Usage: |
| 21 | +# ci-outage.sh mark <cluster> <reason> record an outage |
| 22 | +# ci-outage.sh check <cluster> exit 0 = clear, 1 = outage active |
| 23 | +# ci-outage.sh clear <cluster> reset the breaker |
| 24 | +# |
| 25 | +# Env: |
| 26 | +# MFC_CI_STATE_DIR where markers live (default ~/.mfc-ci-state) |
| 27 | +# MFC_CI_OUTAGE_TTL_SECONDS marker lifetime in seconds (default 1200) |
| 28 | + |
| 29 | +set -uo pipefail |
| 30 | + |
| 31 | +STATE_DIR="${MFC_CI_STATE_DIR:-$HOME/.mfc-ci-state}" |
| 32 | +TTL="${MFC_CI_OUTAGE_TTL_SECONDS:-1200}" |
| 33 | + |
| 34 | +# A non-numeric TTL would make the age comparison below emit "integer expression |
| 35 | +# expected" and exit with a code the caller reads as neither clear nor tripped. |
| 36 | +# Fall back to the default rather than letting a typo gate CI. |
| 37 | +case "$TTL" in |
| 38 | + ''|*[!0-9]*) |
| 39 | + echo "Ignoring non-numeric MFC_CI_OUTAGE_TTL_SECONDS='$TTL'; using 1200." >&2 |
| 40 | + TTL=1200 |
| 41 | + ;; |
| 42 | +esac |
| 43 | + |
| 44 | +EXIT_CLEAR=0 |
| 45 | +EXIT_TRIPPED=1 |
| 46 | +EXIT_USAGE=2 |
| 47 | + |
| 48 | +usage() { |
| 49 | + echo "Usage: $0 {mark <cluster> <reason>|check <cluster>|clear <cluster>}" >&2 |
| 50 | +} |
| 51 | + |
| 52 | +# Keep the marker name filesystem-safe regardless of what the caller passes. |
| 53 | +marker_for() { |
| 54 | + local cluster |
| 55 | + cluster=$(printf '%s' "$1" | tr -c 'A-Za-z0-9_.-' '_') |
| 56 | + printf '%s/outage-%s' "$STATE_DIR" "$cluster" |
| 57 | +} |
| 58 | + |
| 59 | +cmd="${1:-}" |
| 60 | +cluster="${2:-}" |
| 61 | + |
| 62 | +if [ -z "$cmd" ] || [ -z "$cluster" ]; then |
| 63 | + usage |
| 64 | + exit $EXIT_USAGE |
| 65 | +fi |
| 66 | + |
| 67 | +marker=$(marker_for "$cluster") |
| 68 | + |
| 69 | +case "$cmd" in |
| 70 | + mark) |
| 71 | + reason="${3:-unspecified}" |
| 72 | + mkdir -p "$STATE_DIR" || exit $EXIT_USAGE |
| 73 | + # Write to a temporary file and rename so a concurrent `check` never |
| 74 | + # observes a half-written marker. |
| 75 | + tmp="${marker}.$$.tmp" |
| 76 | + { |
| 77 | + date +%s |
| 78 | + printf '%s\n' "$reason" |
| 79 | + } > "$tmp" && mv -f "$tmp" "$marker" |
| 80 | + echo "Recorded $cluster outage: $reason" |
| 81 | + echo " marker: $marker (expires after ${TTL}s)" |
| 82 | + ;; |
| 83 | + |
| 84 | + check) |
| 85 | + [ -f "$marker" ] || exit $EXIT_CLEAR |
| 86 | + |
| 87 | + stamp=$(head -n1 "$marker" 2>/dev/null) |
| 88 | + reason=$(tail -n +2 "$marker" 2>/dev/null) |
| 89 | + |
| 90 | + # A marker we cannot parse is treated as absent: an unreadable breaker |
| 91 | + # must never be an un-clearable one. |
| 92 | + case "$stamp" in |
| 93 | + ''|*[!0-9]*) |
| 94 | + echo "Ignoring unparseable outage marker $marker" |
| 95 | + exit $EXIT_CLEAR |
| 96 | + ;; |
| 97 | + esac |
| 98 | + |
| 99 | + age=$(( $(date +%s) - stamp )) |
| 100 | + if [ "$age" -ge "$TTL" ] || [ "$age" -lt 0 ]; then |
| 101 | + exit $EXIT_CLEAR |
| 102 | + fi |
| 103 | + |
| 104 | + echo "::warning::Skipping: known $cluster outage recorded ${age}s ago: ${reason:-unspecified}" |
| 105 | + echo "Clear it early by deleting $marker" |
| 106 | + exit $EXIT_TRIPPED |
| 107 | + ;; |
| 108 | + |
| 109 | + clear) |
| 110 | + rm -f "$marker" |
| 111 | + echo "Cleared any $cluster outage marker ($marker)" |
| 112 | + ;; |
| 113 | + |
| 114 | + *) |
| 115 | + usage |
| 116 | + exit $EXIT_USAGE |
| 117 | + ;; |
| 118 | +esac |
0 commit comments