Rework emergency-storeall.sh into a watch-only heap monitor
Follows nightly-storeall.sh's cron-driven, single-check-per-invocation shape and logs via maintenance_log(). No longer forces storeall - that stays a manual decision until real incident data is available to calibrate a trigger. Every run logs old-gen occupancy plus Full/Young GC numbers and a normal/elevated/critical condition label, building a GC history dataset for whenever that calibration work resumes. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
This commit is contained in:
parent
177a8d3702
commit
be650ea16c
1 changed files with 144 additions and 45 deletions
|
|
@ -2,84 +2,183 @@
|
|||
#
|
||||
# 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.
|
||||
# JVM heap-pressure watcher. 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.
|
||||
# Cron-driven, same shape as nightly-storeall.sh: a single check-and-log
|
||||
# per invocation, no internal sleep/loop - cron provides the cadence, and
|
||||
# a missed or killed run just gets picked up again at the next tick.
|
||||
#
|
||||
# Exits 0 with nothing done when heap pressure isn't critical, so it's
|
||||
# cheap and safe to run this on a tight schedule.
|
||||
# IMPORTANT: this script currently only WATCHES and LOGS heap pressure -
|
||||
# it does NOT force storeall. That decision is still made manually for
|
||||
# now (see the "GC death-spiral vs. a normal single Full GC" discussion
|
||||
# this was parked on - a real historical example of a run bad enough to
|
||||
# justify forcing it has never actually been found yet). Every run writes
|
||||
# a MaintenanceLog entry with the current heap/GC numbers regardless of
|
||||
# condition, so this quietly builds exactly the historical dataset that
|
||||
# was missing last time - once there's real incident data to calibrate
|
||||
# against, the forcing logic can be added on top of this without changing
|
||||
# anything about how the numbers are gathered.
|
||||
#
|
||||
# Condition label is deliberately based on old-gen occupancy ALONE - the
|
||||
# one signal already validated against real production GC logs (see
|
||||
# sportrum-ansible's group_vars/production.yml for the 14-event history
|
||||
# behind the 3072MB heap sizing). Full GC count/time are surfaced in the
|
||||
# log purely as data, not fed into the condition label: a single Full GC
|
||||
# at high occupancy is normal operation, not a warning sign by itself,
|
||||
# and there isn't yet a validated way to tell a normal Full GC apart from
|
||||
# the start of a death spiral just from GC frequency alone. Once the
|
||||
# older incident logs are recovered, that can change.
|
||||
#
|
||||
# normal old-gen below HEAP_ELEVATED_THRESHOLD_PCT
|
||||
# elevated old-gen at/above HEAP_ELEVATED_THRESHOLD_PCT, below CRITICAL
|
||||
# critical old-gen at/above HEAP_CRITICAL_THRESHOLD_PCT
|
||||
#
|
||||
# Exits 0 on every completed check, whatever the condition - this script
|
||||
# only observes. Exits 1 only if the heap check itself couldn't be
|
||||
# performed (jstat missing, pid not found, etc.), so a broken monitor is
|
||||
# still visible to cron/whatever watches this script's exit code.
|
||||
|
||||
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}"
|
||||
# ---- configuration - tune for your environment ----
|
||||
|
||||
# TODO: both still starting guesses, not yet validated against real
|
||||
# jstat readings from production. HEAP_CRITICAL_THRESHOLD_PCT carries
|
||||
# over the same 85% guess used before this script stopped forcing
|
||||
# anything; HEAP_ELEVATED_THRESHOLD_PCT is new and even less validated -
|
||||
# tune both once you've observed normal vs. tight old-gen occupancy on
|
||||
# the actual server.
|
||||
HEAP_ELEVATED_THRESHOLD_PCT="${HEAP_ELEVATED_THRESHOLD_PCT:-75}"
|
||||
HEAP_CRITICAL_THRESHOLD_PCT="${HEAP_CRITICAL_THRESHOLD_PCT:-85}"
|
||||
|
||||
STATE_DIR="${STATE_DIR:-$(dirname "$0")/state}"
|
||||
STATE_FILE="$STATE_DIR/emergency-storeall.state"
|
||||
mkdir -p "$STATE_DIR"
|
||||
|
||||
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
|
||||
# Reads jstat -gcutil for the running Tomcat and prints five
|
||||
# space-separated numbers: old-gen occupancy pct, full GC count (total
|
||||
# since JVM start), full GC total time in seconds, young GC count, young
|
||||
# GC total time in seconds. Prints nothing and returns 1 on any failure -
|
||||
# every failure reason is logged so a silent/broken cron run is still
|
||||
# diagnosable from its own log line.
|
||||
#
|
||||
# NOTE: same caveat as before this rewrite - untested against a real JVM
|
||||
# in this session. Columns are located dynamically from jstat's own
|
||||
# header rather than hardcoded by position, worth a dry run against
|
||||
# production before trusting the numbers it produces.
|
||||
read_heap_stats() {
|
||||
local tomcat_pid out header data
|
||||
local col_o col_fgc col_fgct col_ygc col_ygct
|
||||
local o fgc fgct ygc ygct
|
||||
|
||||
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"
|
||||
|
||||
col_o=$(awk '{for (i=1;i<=NF;i++) if ($i=="O") print i}' <<< "$header")
|
||||
col_fgc=$(awk '{for (i=1;i<=NF;i++) if ($i=="FGC") print i}' <<< "$header")
|
||||
col_fgct=$(awk '{for (i=1;i<=NF;i++) if ($i=="FGCT") print i}' <<< "$header")
|
||||
col_ygc=$(awk '{for (i=1;i<=NF;i++) if ($i=="YGC") print i}' <<< "$header")
|
||||
col_ygct=$(awk '{for (i=1;i<=NF;i++) if ($i=="YGCT") print i}' <<< "$header")
|
||||
|
||||
if [ -z "$col_o" ] || [ -z "$col_fgc" ] || [ -z "$col_fgct" ] \
|
||||
|| [ -z "$col_ygc" ] || [ -z "$col_ygct" ]
|
||||
then
|
||||
log "could not find all expected columns (O/FGC/FGCT/YGC/YGCT) in jstat output"
|
||||
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"
|
||||
|
||||
o=$(awk -v c="$col_o" '{print $c}' <<< "$data" | cut -d. -f1)
|
||||
fgc=$(awk -v c="$col_fgc" '{print $c}' <<< "$data" | cut -d. -f1)
|
||||
fgct=$(awk -v c="$col_fgct" '{print $c}' <<< "$data")
|
||||
ygc=$(awk -v c="$col_ygc" '{print $c}' <<< "$data" | cut -d. -f1)
|
||||
ygct=$(awk -v c="$col_ygct" '{print $c}' <<< "$data")
|
||||
|
||||
if [ -z "$o" ] || [ -z "$fgc" ] || [ -z "$fgct" ] || [ -z "$ygc" ] || [ -z "$ygct" ]; then
|
||||
log "could not parse one or more numbers out of jstat output"
|
||||
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
|
||||
|
||||
echo "$o $fgc $fgct $ygc $ygct"
|
||||
}
|
||||
|
||||
# ---- main ----
|
||||
|
||||
if ! heap_pressure_critical; then
|
||||
exit 0
|
||||
stats=$(read_heap_stats) || exit 1
|
||||
read -r old_gen_pct fgc_count fgc_time_s ygc_count ygc_time_s <<< "$stats"
|
||||
|
||||
# "Since last check" deltas for the Full GC numbers - purely informational
|
||||
# (see the condition-label note above for why these don't drive the label
|
||||
# itself yet), but this is the shape a future frequency-based signal would
|
||||
# need, so it's captured now rather than only the point-in-time totals.
|
||||
have_prev=false
|
||||
prev_fgc_count=0
|
||||
prev_fgc_time_s=0
|
||||
if [ -f "$STATE_FILE" ]; then
|
||||
have_prev=true
|
||||
read -r prev_fgc_count prev_fgc_time_s < "$STATE_FILE"
|
||||
fi
|
||||
prev_fgc_count="${prev_fgc_count:-0}"
|
||||
prev_fgc_time_s="${prev_fgc_time_s:-0}"
|
||||
|
||||
echo "$fgc_count $fgc_time_s" > "$STATE_FILE"
|
||||
|
||||
if [ "$have_prev" = true ]; then
|
||||
new_fgc_count=$(( fgc_count - prev_fgc_count ))
|
||||
# Negative means Tomcat restarted (counters reset) since the last check,
|
||||
# not that GCs somehow went backwards - treat as "no new data" rather
|
||||
# than a nonsense negative number.
|
||||
[ "$new_fgc_count" -lt 0 ] && new_fgc_count=0
|
||||
new_fgc_time_s=$(awk -v a="$fgc_time_s" -v b="$prev_fgc_time_s" \
|
||||
'BEGIN { d = a - b; if (d < 0) d = 0; printf "%.3f", d }')
|
||||
else
|
||||
new_fgc_count=0
|
||||
new_fgc_time_s="0.000"
|
||||
log "no previous reading found (first run, or state file missing/reset) - since-last-check numbers start from this run"
|
||||
fi
|
||||
|
||||
pid=$$
|
||||
runcmd result storeall
|
||||
if [ "x$result" == "xsuccess" ]; then
|
||||
log "storeall completed successfully"
|
||||
exit 0
|
||||
# ---- condition ----
|
||||
|
||||
if [ "$old_gen_pct" -ge "$HEAP_CRITICAL_THRESHOLD_PCT" ]; then
|
||||
condition="critical"
|
||||
severity="error"
|
||||
elif [ "$old_gen_pct" -ge "$HEAP_ELEVATED_THRESHOLD_PCT" ]; then
|
||||
condition="elevated"
|
||||
severity="warning"
|
||||
else
|
||||
log "storeall reported '${result}' (not 'success') - check the server log for details"
|
||||
exit 1
|
||||
condition="normal"
|
||||
severity="info"
|
||||
fi
|
||||
|
||||
log "old-gen ${old_gen_pct}% - condition: ${condition} (+${new_fgc_count} full GC(s) since last check, +${new_fgc_time_s}s; ${fgc_count} full GC(s) total, ${fgc_time_s}s total)"
|
||||
|
||||
maintenance_log "$severity" emergency_storeall nop "$condition" \
|
||||
"Heap check: old-gen ${old_gen_pct}%, condition ${condition}." \
|
||||
" <oldGenPct>${old_gen_pct}</oldGenPct>
|
||||
<fullGcCountSinceLastCheck>${new_fgc_count}</fullGcCountSinceLastCheck>
|
||||
<fullGcTimeSinceLastCheckSec>${new_fgc_time_s}</fullGcTimeSinceLastCheckSec>
|
||||
<fullGcCountTotal>${fgc_count}</fullGcCountTotal>
|
||||
<fullGcTimeTotalSec>${fgc_time_s}</fullGcTimeTotalSec>
|
||||
<youngGcCountTotal>${ygc_count}</youngGcCountTotal>
|
||||
<youngGcTimeTotalSec>${ygc_time_s}</youngGcTimeTotalSec>"
|
||||
|
||||
exit 0
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue