Split heap-pressure escape hatch out of nightly-storeall.sh

nightly-storeall.sh and the heap-pressure safety valve have genuinely
different schedules: the former should run once a night and is patient
(wait for quiet traffic, bounded retry), the latter needs to run every
10-15 minutes around the clock and is impatient (heap occupancy only
climbs while storeall is deferred, so it forces a run immediately with
no regard for traffic once critical). Cramming both into one script
meant they could only ever run on the same cadence.

nightly-storeall.sh: dropped heap_pressure_critical and
HEAP_FORCE_THRESHOLD_PCT entirely - it's now traffic-driven only.

emergency-storeall.sh: new script, just the heap check + immediate
storeall if critical, exits 0 doing nothing otherwise so it's cheap to
run on a tight cron schedule.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
This commit is contained in:
Claude 2026-09-30 11:46:00 +00:00
parent 034baa96f6
commit 2478310f55
No known key found for this signature in database
2 changed files with 92 additions and 57 deletions

85
emergency-storeall.sh Executable file
View file

@ -0,0 +1,85 @@
#!/bin/bash
#
# emergency-storeall.sh
#
# JVM heap-pressure safety valve. Meant to be run frequently (e.g. every
# 10-15 minutes) on its own cron schedule, independent of and much more
# often than nightly-storeall.sh.
#
# storeall is the only thing that flushes in-memory 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). That means old-gen occupancy can only climb, never
# fall, for as long as storeall is deferred - so unlike
# nightly-storeall.sh, this script does not wait for a quiet moment. If
# heap pressure is critical it forces storeall immediately, traffic or
# no traffic, because the alternative is an OOM that loses whatever
# in-memory state was never flushed.
#
# Exits 0 with nothing done when heap pressure isn't critical, so it's
# cheap and safe to run this on a tight schedule.
set -u
cd "$(dirname "$0")"
. ./upgrade-funs.sh
# 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="${HEAP_FORCE_THRESHOLD_PCT:-85}"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') emergency-storeall: $*"
}
# 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 ----
if ! heap_pressure_critical; then
exit 0
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

View file

@ -5,12 +5,12 @@
# Waits for a safe moment to flush in-memory state via the app's storeall # 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 # command, so the nightly long-lived backup's freeze doesn't land on top
# of live race/production traffic. Falls back to running unconditionally # of live race/production traffic. Falls back to running unconditionally
# if either the retry budget runs out or JVM heap pressure looks urgent - # once the retry budget runs out.
# 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 # This script is traffic-driven only and meant to run once a night. The
# currently marked dirty (the registry's WeakHashMap eviction is blocked # separate, independent safety valve for JVM heap pressure - which needs
# for anything a Modified/Copy record still points to), so it can't be # to run much more often, on its own schedule, regardless of traffic -
# deferred indefinitely no matter how busy the server looks. # lives in emergency-storeall.sh.
# #
# Busy signal: any of these handlers appearing in Tomcat's stdout log # Busy signal: any of these handlers appearing in Tomcat's stdout log
# within the trailing window means real work is happening right now - # within the trailing window means real work is happening right now -
@ -63,11 +63,6 @@ CHECK_WINDOW_MIN=10 # trailing window used to decide "busy right now" -
RETRY_INTERVAL_SEC=300 # wait 5 min between retries while busy RETRY_INTERVAL_SEC=300 # wait 5 min between retries while busy
MAX_RETRIES=24 # ~2 hours of retrying before forcing the run anyway 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|ProductionCommand\.start_production|ProductionCommand\.update_production|ProductionCommand\.finish_production|ProductionCommand\.append_stream_batch|ESTPushServlet\.null' BUSY_PATTERN='ProductionCommand\.get_clipcast_screenplay|ProductionCommand\.start_production|ProductionCommand\.update_production|ProductionCommand\.finish_production|ProductionCommand\.append_stream_batch|ESTPushServlet\.null'
log() { log() {
@ -109,57 +104,12 @@ is_busy() {
[ "$age" -ge 0 ] && [ "$age" -le "$window_sec" ] [ "$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 ---- # ---- main ----
attempt=0 attempt=0
forced_reason="" forced_reason=""
while true; do while is_busy; do
if heap_pressure_critical; then
forced_reason="heap pressure"
break
fi
if ! is_busy; then
break
fi
attempt=$((attempt + 1)) attempt=$((attempt + 1))
if [ "$attempt" -ge "$MAX_RETRIES" ]; then if [ "$attempt" -ge "$MAX_RETRIES" ]; then
forced_reason="retry budget exhausted (${MAX_RETRIES} attempts)" forced_reason="retry budget exhausted (${MAX_RETRIES} attempts)"