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
|
# emergency-storeall.sh
|
||||||
#
|
#
|
||||||
# JVM heap-pressure safety valve. Meant to be run frequently (e.g. every
|
# JVM heap-pressure watcher. Meant to be run frequently (e.g. every 10-15
|
||||||
# 10-15 minutes) on its own cron schedule, independent of and much more
|
# minutes) on its own cron schedule, independent of and much more often
|
||||||
# often than nightly-storeall.sh.
|
# than nightly-storeall.sh.
|
||||||
#
|
#
|
||||||
# storeall is the only thing that flushes in-memory dirty state to
|
# Cron-driven, same shape as nightly-storeall.sh: a single check-and-log
|
||||||
# Postgres, and it's also the only thing that lets the JVM
|
# per invocation, no internal sleep/loop - cron provides the cadence, and
|
||||||
# garbage-collect anything currently marked dirty (the registry's
|
# a missed or killed run just gets picked up again at the next tick.
|
||||||
# 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
|
# IMPORTANT: this script currently only WATCHES and LOGS heap pressure -
|
||||||
# cheap and safe to run this on a tight schedule.
|
# 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
|
set -u
|
||||||
cd "$(dirname "$0")"
|
cd "$(dirname "$0")"
|
||||||
. ./upgrade-funs.sh
|
. ./upgrade-funs.sh
|
||||||
|
|
||||||
# TODO: this is a starting guess, not yet validated against a real jstat
|
# ---- configuration - tune for your environment ----
|
||||||
# reading from production - tune once you've observed normal vs. tight
|
|
||||||
# old-gen occupancy on the actual server.
|
# TODO: both still starting guesses, not yet validated against real
|
||||||
HEAP_FORCE_THRESHOLD_PCT="${HEAP_FORCE_THRESHOLD_PCT:-85}"
|
# 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() {
|
log() {
|
||||||
echo "$(date '+%Y-%m-%d %H:%M:%S') emergency-storeall: $*"
|
echo "$(date '+%Y-%m-%d %H:%M:%S') emergency-storeall: $*"
|
||||||
}
|
}
|
||||||
|
|
||||||
# NOTE: untested against a real JVM in this session - the "O" (old gen)
|
# Reads jstat -gcutil for the running Tomcat and prints five
|
||||||
# column is located dynamically from jstat's own header rather than
|
# space-separated numbers: old-gen occupancy pct, full GC count (total
|
||||||
# hardcoded by position, but the whole function is worth a dry run
|
# since JVM start), full GC total time in seconds, young GC count, young
|
||||||
# against production before trusting it to force a live storeall.
|
# GC total time in seconds. Prints nothing and returns 1 on any failure -
|
||||||
heap_pressure_critical() {
|
# every failure reason is logged so a silent/broken cron run is still
|
||||||
local tomcat_pid out header data col util
|
# 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)
|
tomcat_pid=$(sudo supervisorctl pid tomcat 2>/dev/null)
|
||||||
if [ -z "$tomcat_pid" ]; then
|
if [ -z "$tomcat_pid" ]; then
|
||||||
log "could not resolve tomcat pid via supervisorctl, skipping heap check"
|
log "could not resolve tomcat pid via supervisorctl, skipping heap check"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
out=$(sudo jstat -gcutil "$tomcat_pid" 2>/dev/null)
|
out=$(sudo jstat -gcutil "$tomcat_pid" 2>/dev/null)
|
||||||
if [ -z "$out" ]; then
|
if [ -z "$out" ]; then
|
||||||
log "jstat returned nothing for pid $tomcat_pid, skipping heap check"
|
log "jstat returned nothing for pid $tomcat_pid, skipping heap check"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
header=$(head -n 1 <<< "$out")
|
header=$(head -n 1 <<< "$out")
|
||||||
data=$(tail -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
|
col_o=$(awk '{for (i=1;i<=NF;i++) if ($i=="O") print i}' <<< "$header")
|
||||||
log "could not find old-gen (O) column in jstat output, skipping heap check"
|
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
|
return 1
|
||||||
fi
|
fi
|
||||||
util=$(awk -v c="$col" '{print $c}' <<< "$data" | cut -d. -f1)
|
|
||||||
if [ -z "$util" ]; then
|
o=$(awk -v c="$col_o" '{print $c}' <<< "$data" | cut -d. -f1)
|
||||||
log "could not read old-gen utilization from jstat output, skipping heap check"
|
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
|
return 1
|
||||||
fi
|
fi
|
||||||
if [ "$util" -ge "$HEAP_FORCE_THRESHOLD_PCT" ]; then
|
|
||||||
log "old-gen at ${util}% (>= ${HEAP_FORCE_THRESHOLD_PCT}%) - forcing storeall regardless of traffic"
|
echo "$o $fgc $fgct $ygc $ygct"
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# ---- main ----
|
# ---- main ----
|
||||||
|
|
||||||
if ! heap_pressure_critical; then
|
stats=$(read_heap_stats) || exit 1
|
||||||
exit 0
|
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
|
fi
|
||||||
|
|
||||||
pid=$$
|
# ---- condition ----
|
||||||
runcmd result storeall
|
|
||||||
if [ "x$result" == "xsuccess" ]; then
|
if [ "$old_gen_pct" -ge "$HEAP_CRITICAL_THRESHOLD_PCT" ]; then
|
||||||
log "storeall completed successfully"
|
condition="critical"
|
||||||
exit 0
|
severity="error"
|
||||||
|
elif [ "$old_gen_pct" -ge "$HEAP_ELEVATED_THRESHOLD_PCT" ]; then
|
||||||
|
condition="elevated"
|
||||||
|
severity="warning"
|
||||||
else
|
else
|
||||||
log "storeall reported '${result}' (not 'success') - check the server log for details"
|
condition="normal"
|
||||||
exit 1
|
severity="info"
|
||||||
fi
|
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