sportrum-deploy/nightly-storeall.sh

137 lines
5.5 KiB
Bash
Raw Normal View History

Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
#!/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
# once the retry budget runs out.
#
# This script is traffic-driven only and meant to run once a night. The
# separate, independent safety valve for JVM heap pressure - which needs
# to run much more often, on its own schedule, regardless of traffic -
# lives in emergency-storeall.sh.
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
#
# 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, the live-stream ingest
# pipeline actively receiving chunks, or the timing system actively
# receiving live race data:
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
# ProductionCommand.get_clipcast_screenplay
# ProductionCommand.start_production
# ProductionCommand.update_production
# ProductionCommand.finish_production
# ProductionCommand.append_stream_batch
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
# 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.
#
# IMPORTANT: match against the log without anchoring at the start of the
# line. Some handlers (append_stream_batch in particular) print an
# unterminated progress fragment ("- processed N chunks") immediately
# before the request-completion log line with no newline in between, so
# the two get glued onto one physical line and "sportrum:" doesn't start
# at column 1. An anchored regex missed 99.9% of append_stream_batch's
# occurrences in testing (and ~2-4% of the other handlers') - see the
# grep -oE approach below, which finds the timestamp+handler substring
# anywhere in the line instead.
#
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
# Validated against a real two-day event's log (Cykelvasan, Aug 2026):
# these six handlers sit at exactly zero for the entire overnight lull
# on both nights, and never dip below several hundred matches in any
# 10-minute window during live racing - see the excerpt-based tests run
# alongside this script's development for the numbers.
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
#
# 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
BUSY_PATTERN='ProductionCommand\.get_clipcast_screenplay|ProductionCommand\.start_production|ProductionCommand\.update_production|ProductionCommand\.finish_production|ProductionCommand\.append_stream_batch|ESTPushServlet\.null'
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
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.
#
# Uses grep -oE to pull out just the "day mon HH:MM:SS <-> Handler"
# substring wherever it occurs on the line, rather than anchoring at the
# start of the line - see the note above BUSY_PATTERN for why an anchored
# match silently misses most append_stream_batch lines.
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
last_busy_epoch() {
local last day mon t
last=$(tail -n "$TAIL_LINES" "$TOMCAT_LOG" \
| grep -oE "[0-9]+ [a-z]+ [0-9]{2}:[0-9]{2}:[0-9]{2} <-> (${BUSY_PATTERN})" \
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
| tail -n 1)
[ -z "$last" ] && return 1
day=$(awk '{print $1}' <<< "$last")
mon=$(awk '{print $2}' <<< "$last")
t=$(awk '{print $3}' <<< "$last")
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
# 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" ]
}
# ---- main ----
attempt=0
forced_reason=""
while is_busy; do
Add nightly-storeall.sh: wait-for-idle gate before triggering storeall Adds the script that decides when it's safe to flush in-memory state via the app's storeall command, so a nightly backup's freeze doesn't land on top of live race/production traffic. Busy signal is presence, within a trailing window, of any of five Tomcat stdout log handlers (get_clipcast_screenplay/start_production/ update_production/finish_production/ESTPushServlet) - deliberately excludes the idle-node polling handlers (peek_productions_queue_new, request_batch, producer_log), which fire continuously regardless of whether real work is happening. Validated the exact bash logic (not just the concept) against excerpts of a real two-day event's production log: zero false positives/negatives across quiet-night, pre-event, ramp-up, and peak-racing cases at a 10-minute window. Two escape hatches force the run regardless of traffic: a bounded retry budget (default ~2h) and a JVM old-gen heap-pressure check via supervisorctl+jstat, since storeall is the only thing that flushes state to Postgres and the only thing that unblocks the registry's WeakHashMap GC eviction for anything currently dirty - it can't be deferred forever. Two things are still placeholders, not yet validated against the real server: TOMCAT_LOG's actual path (depends on supervisord's stdout_logfile setting) and HEAP_FORCE_THRESHOLD_PCT (needs a real jstat baseline). Both are called out inline. Scope is deliberately narrow: this script only decides when to call storeall and calls it. Taking the actual pg_dump/webapp backup is a separate step to chain after a successful run. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-09-30 11:29:45 +00:00
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