#!/bin/bash # # nightly-storeall.sh # # Waits for a safe moment to flush in-memory state via the app's storeall # command, so the nightly long-lived backup's freeze doesn't land on top # of live race/production traffic. Falls back to running unconditionally # if either the retry budget runs out or JVM heap pressure looks urgent - # storeall is the only thing that flushes dirty state to Postgres, and # it's also the only thing that lets the JVM garbage-collect anything # currently marked dirty (the registry's WeakHashMap eviction is blocked # for anything a Modified/Copy record still points to), so it can't be # deferred indefinitely no matter how busy the server looks. # # Busy signal: any of these handlers appearing in Tomcat's stdout log # within the trailing window means real work is happening right now - # video production nodes actively producing, or the timing system # actively receiving live race data: # ProductionCommand.get_clipcast_screenplay # ProductionCommand.start_production # ProductionCommand.update_production # ProductionCommand.finish_production # ESTPushServlet.null # Deliberately excludes ProductionCommand.peek_productions_queue_new, # ProductionCommand.request_batch and ProductionCommand.producer_log - # those fire continuously from idle nodes kept warm during an event and # don't indicate real work happening. # # Validated against a real two-day event's log (Cykelvasan, Aug 2026): # these five handlers sit at exactly zero for the entire overnight lull # on both nights, and never dip anywhere near zero during a single # minute of live racing - see the excerpt-based tests run alongside this # script's development for the numbers. # # Scope: this script only decides *when* to call storeall and calls it. # It does not take the actual pg_dump/webapp backup - that's a separate # step to chain after a successful run. set -u cd "$(dirname "$0")" . ./upgrade-funs.sh # ---- configuration - tune for your environment ---- # TODO: fill in the real path once you've found where supervisord writes # Tomcat's stdout (its stdout_logfile setting for the tomcat program). TOMCAT_LOG="${TOMCAT_LOG:-/path/to/tomcat/stdout.log}" TAIL_LINES=20000 # comfortably covers CHECK_WINDOW_MIN even at peak traffic (~800 req/min observed) CHECK_WINDOW_MIN=10 # trailing window used to decide "busy right now" - validated against real data RETRY_INTERVAL_SEC=300 # wait 5 min between retries while busy MAX_RETRIES=24 # ~2 hours of retrying before forcing the run anyway # TODO: this is a starting guess, not yet validated against a real jstat # reading from production - tune once you've observed normal vs. tight # old-gen occupancy on the actual server. HEAP_FORCE_THRESHOLD_PCT=85 BUSY_PATTERN='ProductionCommand\.(get_clipcast_screenplay|start_production|update_production|finish_production)|ESTPushServlet\.null' log() { echo "$(date '+%Y-%m-%d %H:%M:%S') nightly-storeall: $*" } # Prints the epoch of the most recent busy-pattern line seen in the # trailing $TAIL_LINES lines of the log. Returns 1 (prints nothing) if # none found in that tail at all. last_busy_epoch() { local last day mon t last=$(tail -n "$TAIL_LINES" "$TOMCAT_LOG" \ | grep -E '^sportrum: 6: [0-9]+ [a-z]+ [0-9:]+ <->' \ | grep -E "$BUSY_PATTERN" \ | tail -n 1) [ -z "$last" ] && return 1 day=$(awk '{print $3}' <<< "$last") mon=$(awk '{print $4}' <<< "$last") t=$(awk '{print $5}' <<< "$last") # NOTE: log lines carry no year, and the month name is locale-dependent # (this JVM logs in Swedish locale - "maj"/"okt" would not parse here, # though every month actually exercised so far happens to be spelled # the same in Swedish and English). Revisit if a false parse ever shows # up around May or October. date -d "$day $mon $(date +%Y) $t" +%s 2>/dev/null } # Exit 0 = busy (real work within the trailing window), 1 = quiet. is_busy() { local last_epoch now age window_sec last_epoch=$(last_busy_epoch) || return 1 now=$(date +%s) age=$(( now - last_epoch )) window_sec=$(( CHECK_WINDOW_MIN * 60 )) [ "$age" -ge 0 ] && [ "$age" -le "$window_sec" ] } # Heap pressure escape hatch: force a run regardless of traffic once # old-gen occupancy crosses HEAP_FORCE_THRESHOLD_PCT. # # NOTE: untested against a real JVM in this session - the "O" (old gen) # column is located dynamically from jstat's own header rather than # hardcoded by position, but the whole function is worth a dry run # against production before trusting it to force a live storeall. heap_pressure_critical() { local tomcat_pid out header data col util tomcat_pid=$(sudo supervisorctl pid tomcat 2>/dev/null) if [ -z "$tomcat_pid" ]; then log "could not resolve tomcat pid via supervisorctl, skipping heap check" return 1 fi out=$(sudo jstat -gcutil "$tomcat_pid" 2>/dev/null) if [ -z "$out" ]; then log "jstat returned nothing for pid $tomcat_pid, skipping heap check" return 1 fi header=$(head -n 1 <<< "$out") data=$(tail -n 1 <<< "$out") col=$(awk '{for (i=1;i<=NF;i++) if ($i=="O") print i}' <<< "$header") if [ -z "$col" ]; then log "could not find old-gen (O) column in jstat output, skipping heap check" return 1 fi util=$(awk -v c="$col" '{print $c}' <<< "$data" | cut -d. -f1) if [ -z "$util" ]; then log "could not read old-gen utilization from jstat output, skipping heap check" return 1 fi if [ "$util" -ge "$HEAP_FORCE_THRESHOLD_PCT" ]; then log "old-gen at ${util}% (>= ${HEAP_FORCE_THRESHOLD_PCT}%) - forcing storeall regardless of traffic" return 0 fi return 1 } # ---- main ---- attempt=0 forced_reason="" while true; do if heap_pressure_critical; then forced_reason="heap pressure" break fi if ! is_busy; then break fi attempt=$((attempt + 1)) if [ "$attempt" -ge "$MAX_RETRIES" ]; then forced_reason="retry budget exhausted (${MAX_RETRIES} attempts)" break fi log "server busy, waiting ${RETRY_INTERVAL_SEC}s before retry ${attempt}/${MAX_RETRIES}" sleep "$RETRY_INTERVAL_SEC" done if [ -n "$forced_reason" ]; then log "proceeding with storeall - forced (${forced_reason})" else log "proceeding with storeall - server is quiet" fi pid=$$ runcmd result storeall if [ "x$result" == "xsuccess" ]; then log "storeall completed successfully" exit 0 else log "storeall reported '${result}' (not 'success') - check the server log for details" exit 1 fi