From 2478310f55cb3d583545c4e7cedc82de8b7c1234 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 30 Sep 2026 11:46:00 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb --- emergency-storeall.sh | 85 +++++++++++++++++++++++++++++++++++++++++++ nightly-storeall.sh | 64 ++++---------------------------- 2 files changed, 92 insertions(+), 57 deletions(-) create mode 100755 emergency-storeall.sh diff --git a/emergency-storeall.sh b/emergency-storeall.sh new file mode 100755 index 0000000..35f735a --- /dev/null +++ b/emergency-storeall.sh @@ -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 diff --git a/nightly-storeall.sh b/nightly-storeall.sh index a5a4170..4a3ff8d 100755 --- a/nightly-storeall.sh +++ b/nightly-storeall.sh @@ -5,12 +5,12 @@ # 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. +# 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. # # Busy signal: any of these handlers appearing in Tomcat's stdout log # 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 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' log() { @@ -109,57 +104,12 @@ is_busy() { [ "$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 +while is_busy; do attempt=$((attempt + 1)) if [ "$attempt" -ge "$MAX_RETRIES" ]; then forced_reason="retry budget exhausted (${MAX_RETRIES} attempts)"