aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / tools / ops / spark-build-once
| 1 | #!/bin/sh |
| 2 | # spark-build-once [--out PATH]... [--link] [--] CMD... |
| 3 | # |
| 4 | # Build once, reuse everywhere. Run from inside a git worktree. The cache key is |
| 5 | # the source tree as it is on disk (HEAD tree + uncommitted diff + untracked, |
| 6 | # not-ignored files), the folder you run from, the exact command, the --out |
| 7 | # list, the machine type, and the env knobs PHYSICS_DIR / AIENOS_LOCK_REPO (their |
| 8 | # values, plus their own tree hash when they point into a git worktree). |
| 9 | # |
| 10 | # hit : copies the stored artifacts into your --out paths (or symlinks with |
| 11 | # --link) and returns in seconds; the build does not run. |
| 12 | # miss: runs CMD through quiet-build, then stores every --out path under |
| 13 | # $SPARK_BUILD_CACHE/<key> by atomic rename. A failed build stores nothing. |
| 14 | # |
| 15 | # Concurrent callers with the same key: one builds, the others wait on the |
| 16 | # per-key lock (they print who holds it and for how long) and then take the hit. |
| 17 | # Every call appends one line to ~/yardsticks/build-once.log. |
| 18 | # Exit code: the build's own on a miss, 0 on a hit, 2 on usage errors. |
| 19 | set -u |
| 20 | C=${SPARK_BUILD_CACHE:-$HOME/.cache/spark-build-once} |
| 21 | LOG=$HOME/yardsticks/build-once.log |
| 22 | outs=""; link=0 |
| 23 | while [ $# -gt 0 ]; do |
| 24 | case $1 in |
| 25 | --out) [ $# -ge 2 ] || { echo "spark-build-once: --out needs a path" >&2; exit 2; } |
| 26 | case $2 in /*|*..*) echo "spark-build-once: --out must be a relative path inside the worktree: $2" >&2; exit 2;; esac |
| 27 | outs="$outs |
| 28 | $2"; shift 2 ;; |
| 29 | --link) link=1; shift ;; |
| 30 | --) shift; break ;; |
| 31 | -h|--help) sed -n '2,20p' "$0"; exit 0 ;; |
| 32 | *) break ;; |
| 33 | esac |
| 34 | done |
| 35 | [ $# -gt 0 ] || { echo "usage: spark-build-once [--out PATH]... [--link] [--] CMD..." >&2; exit 2; } |
| 36 | outs=$(printf '%s\n' "$outs" | sed '/^$/d') |
| 37 | [ -n "$outs" ] || { echo "spark-build-once: name at least one artifact with --out PATH" >&2; exit 2; } |
| 38 | top=$(git rev-parse --show-toplevel 2>/dev/null) || { echo "spark-build-once: not inside a git worktree" >&2; exit 2; } |
| 39 | mkdir -p "$C" "$(dirname "$LOG")" |
| 40 | |
| 41 | # tree_hash DIR: HEAD tree + hash of uncommitted diff and untracked files (writes no git objects) |
| 42 | tree_hash() { |
| 43 | ( cd "$1" || exit 1 |
| 44 | t=$(git rev-parse -q --verify 'HEAD^{tree}' 2>/dev/null || echo none) |
| 45 | d=$( { git diff HEAD --binary 2>/dev/null; git ls-files -o --exclude-standard -z | xargs -0 -r sha256sum; } | sha256sum | cut -c1-64) |
| 46 | echo "$t $d" ) |
| 47 | } |
| 48 | src=$(tree_hash "$top") |
| 49 | knobs="" |
| 50 | for v in PHYSICS_DIR AIENOS_LOCK_REPO; do |
| 51 | eval "val=\${$v:-}" |
| 52 | h="" |
| 53 | if [ -n "$val" ] && [ -d "$val" ] && kt=$(git -C "$val" rev-parse --show-toplevel 2>/dev/null); then h=$(tree_hash "$kt"); fi |
| 54 | knobs="$knobs $v=$val[$h]" |
| 55 | done |
| 56 | cmdh=$(printf '%s\0' "$@" | sha256sum | cut -c1-64) |
| 57 | key=$(printf 'v1\n%s\n%s\n%s\n%s\n%s\n%s\n' "$src" "$(git rev-parse --show-prefix)" "$cmdh" "$outs" "$knobs" "$(uname -m)" | sha256sum | cut -c1-24) |
| 58 | D=$C/$key |
| 59 | t0=$(date +%s) |
| 60 | log() { printf '%s\t%s\t%s\t%ss\t%s\t' "$(date -u +%FT%TZ)" "$key" "$1" "$(( $(date +%s) - t0 ))" "$PWD" >> "$LOG"; shift; printf '%s ' "$@" | tr '\t\n' ' ' >> "$LOG"; echo >> "$LOG"; } |
| 61 | echo "spark-build-once: key $key (source $(echo "$src" | cut -c1-12), cmd: $*)" >&2 |
| 62 | |
| 63 | restore() { |
| 64 | while IFS= read -r p; do |
| 65 | s=$D/out/$p |
| 66 | [ -e "$s" ] || [ -L "$s" ] || { echo "spark-build-once: cache entry $key lacks $p" >&2; return 1; } |
| 67 | mkdir -p "$(dirname "$p")" |
| 68 | if [ "$link" = 1 ]; then |
| 69 | if [ -d "$p" ] && [ ! -L "$p" ]; then echo "spark-build-once: --link refuses to replace the real directory $p" >&2; return 1; fi |
| 70 | ln -sfn "$s" "$p" || return 1 |
| 71 | elif [ -d "$s" ] && [ ! -L "$s" ]; then |
| 72 | mkdir -p "$p" && cp -a "$s/." "$p/" || return 1 |
| 73 | else |
| 74 | cp -a --remove-destination "$s" "$p" || return 1 |
| 75 | fi |
| 76 | done <<EOF |
| 77 | $outs |
| 78 | EOF |
| 79 | } |
| 80 | |
| 81 | hit() { |
| 82 | restore || { log FAIL-RESTORE "$@"; exit 1; } |
| 83 | echo "spark-build-once: HIT $key (built $(sed -n 's/^built //p' "$D/meta") in $(sed -n 's/^from //p' "$D/meta"); $(( $(date +%s) - t0 )) s)" >&2 |
| 84 | log HIT "$@"; exit 0 |
| 85 | } |
| 86 | |
| 87 | # fast path: already stored, no lock needed (entries are only ever created by rename) |
| 88 | [ -f "$D/meta" ] && hit "$@" |
| 89 | |
| 90 | exec 8>"$D.lock" |
| 91 | if ! flock -n 8; then |
| 92 | ( exec 8>&-; while sleep 60; do echo "spark-build-once: waiting $(( $(date +%s) - t0 )) s for key $key, held by: $(cat "$D.who" 2>/dev/null || echo unknown)" >&2; done ) & |
| 93 | tick=$! |
| 94 | flock 8 |
| 95 | kill $tick 2>/dev/null |
| 96 | fi |
| 97 | echo "pid $$ since $(date -u +%FT%TZ) in $PWD" > "$D.who" |
| 98 | [ -f "$D/meta" ] && hit "$@" |
| 99 | |
| 100 | echo "spark-build-once: MISS $key, building through quiet-build" >&2 |
| 101 | quiet-build "$@"; rc=$? |
| 102 | if [ $rc -ne 0 ]; then echo "spark-build-once: build failed (exit $rc), nothing stored" >&2; log "FAIL-$rc" "$@"; exit $rc; fi |
| 103 | T=$C/.tmp.$key.$$ |
| 104 | rm -rf -- "$T"; mkdir -p "$T/out" |
| 105 | ok=1 |
| 106 | while IFS= read -r p; do |
| 107 | if [ ! -e "$p" ]; then echo "spark-build-once: build passed but --out $p does not exist; not stored" >&2; ok=0; continue; fi |
| 108 | mkdir -p "$T/out/$(dirname "$p")" && cp -a "$p" "$T/out/$p" || ok=0 |
| 109 | done <<EOF |
| 110 | $outs |
| 111 | EOF |
| 112 | if [ $ok = 1 ]; then |
| 113 | { echo "key $key"; echo "built $(date -u +%FT%TZ)"; echo "from $PWD"; echo "seconds $(( $(date +%s) - t0 ))" |
| 114 | echo "source $src"; echo "knobs$knobs"; printf 'cmd'; printf ' %s' "$@"; echo; echo "outs"; echo "$outs"; } > "$T/meta" |
| 115 | mv -T "$T" "$D" 2>/dev/null || { rm -rf -- "$T"; echo "spark-build-once: another caller stored $key first; kept theirs" >&2; } |
| 116 | echo "spark-build-once: stored $key ($(du -sh "$D" | cut -f1))" >&2 |
| 117 | log MISS-STORED "$@" |
| 118 | else |
| 119 | rm -rf -- "$T"; log MISS-NOTSTORED "$@" |
| 120 | fi |
| 121 | exit 0 |