#!/usr/bin/env bash # What happens to open files, environment variables and secrets across a checkpoint. source "$(dirname "$0")/lib.sh" cd "$WORK"; stop_port AUDIT="$WORK/audit.log" J="$JDK_CRAC/bin/java" echo "== 1. A file opened in a constructor and never closed (profile 'leaky'), checkpoint at start-up" | tee "$OUT/03-leaky.txt" rm -rf cp-leaky "$AUDIT" run_env "$J" -XX:CRaCCheckpointTo="$WORK/cp-leaky" -Dspring.context.checkpoint=onRefresh -jar "$APPJAR" --server.port=$PORT --spring.profiles.active=leaky --demo.audit-file="$AUDIT" > leaky.log 2>&1 echo "java exit code: $?" | tee -a "$OUT/03-leaky.txt" grep -E 'ERROR|ApplicationContextException|Caused by|CheckpointOpenFileException|Suppressed' leaky.log | scrub | tee -a "$OUT/03-leaky.txt" echo "checkpoint directory exists: $([ -d cp-leaky ] && echo yes || echo no)" | tee -a "$OUT/03-leaky.txt" echo | tee -a "$OUT/03-leaky.txt"; echo "== 2. The same mistake, checkpoint requested later with jcmd JDK.checkpoint" | tee -a "$OUT/03-leaky.txt" rm -rf cp-leaky2 "$AUDIT" setsid env -u JAVA_TOOL_OPTIONS -u _JAVA_OPTIONS "$J" -XX:CRaCCheckpointTo="$WORK/cp-leaky2" -jar "$APPJAR" --server.port=$PORT --spring.profiles.active=leaky --demo.audit-file="$AUDIT" > leaky2.log 2>&1 < /dev/null & wait_ready; PID=$(pgrep -f "CRaCCheckpointTo=$WORK/cp-leaky2" | head -1) run_env "$JDK_CRAC/bin/jcmd" "$PID" JDK.checkpoint 2>&1 | grep -v '^$' | scrub | head -8 | tee -a "$OUT/03-leaky.txt" sleep 1 echo "application still running and answering: $(curl -s localhost:$PORT/ping)" | tee -a "$OUT/03-leaky.txt" kill -9 "$PID"; wait_exit "$PID" echo "== 3. The fix: open in start(), close in stop() (profile 'managed'), checkpoint with jcmd, restore" | tee "$OUT/04-managed.txt" rm -rf cp-managed "$AUDIT" setsid env -u JAVA_TOOL_OPTIONS -u _JAVA_OPTIONS "$J" -XX:CRaCCheckpointTo="$WORK/cp-managed" -jar "$APPJAR" --server.port=$PORT --spring.profiles.active=managed --demo.audit-file="$AUDIT" > managed1.log 2>&1 < /dev/null & wait_ready; PID=$(pgrep -f "CRaCCheckpointTo=$WORK/cp-managed" | head -1) for i in 1 2 3; do curl -s -o /dev/null -X POST -H 'Content-Type: application/json' -d '{"customer":"ada","sku":"X","quantity":1}' localhost:$PORT/orders; done echo "orders before checkpoint; audit file has $(wc -l < "$AUDIT") lines" | tee -a "$OUT/04-managed.txt" run_env "$JDK_CRAC/bin/jcmd" "$PID" JDK.checkpoint > /dev/null 2>&1; wait_exit "$PID" echo "--- log of the checkpointed process (hooks only)" | tee -a "$OUT/04-managed.txt" grep -E 'ManagedAuditLog|RestoreTracker|Spring-managed|Stopping|Tomcat' managed1.log | scrub | tee -a "$OUT/04-managed.txt" setsid env -u JAVA_TOOL_OPTIONS -u _JAVA_OPTIONS "$J" -XX:CRaCRestoreFrom="$WORK/cp-managed" > managed2.log 2>&1 < /dev/null & wait_ready; sleep 0.3 echo "--- log of the restored process (hooks only)" | tee -a "$OUT/04-managed.txt" grep -E 'ManagedAuditLog|RestoreTracker|Spring-managed|Tomcat|Restored' managed2.log | scrub | tee -a "$OUT/04-managed.txt" curl -s -o /dev/null -X POST -H 'Content-Type: application/json' -d '{"customer":"ada","sku":"X","quantity":1}' localhost:$PORT/orders echo "--- after restore: new order (id continues from the checkpointed counter), audit file now has $(wc -l < "$AUDIT") lines:" | tee -a "$OUT/04-managed.txt" curl -s localhost:$PORT/orders/4 | tee -a "$OUT/04-managed.txt"; echo | tee -a "$OUT/04-managed.txt" cat "$AUDIT" | tee -a "$OUT/04-managed.txt" stop_port echo "== 4. Environment variables and secrets" | tee "$OUT/05-environment.txt" rm -rf cp-env setsid env -u JAVA_TOOL_OPTIONS -u _JAVA_OPTIONS DEMO_GREETING=value-at-checkpoint-S3CRET-7f3a91 "$J" -XX:CRaCCheckpointTo="$WORK/cp-env" -jar "$APPJAR" --server.port=$PORT > env1.log 2>&1 < /dev/null & wait_ready; PID=$(pgrep -f "CRaCCheckpointTo=$WORK/cp-env" | head -1) echo "before checkpoint: $(curl -s localhost:$PORT/info)" | tee -a "$OUT/05-environment.txt" run_env "$JDK_CRAC/bin/jcmd" "$PID" JDK.checkpoint > /dev/null 2>&1; wait_exit "$PID" setsid env -u JAVA_TOOL_OPTIONS -u _JAVA_OPTIONS DEMO_GREETING=value-at-restore "$J" -XX:CRaCRestoreFrom="$WORK/cp-env" > env2.log 2>&1 < /dev/null & wait_ready echo "restored with DEMO_GREETING=value-at-restore:" | tee -a "$OUT/05-environment.txt" echo "after restore: $(curl -s localhost:$PORT/info)" | tee -a "$OUT/05-environment.txt" echo "occurrences of the checkpoint-time value in core.img: $(grep -a -c 'value-at-checkpoint-S3CRET-7f3a91' cp-env/core.img)" | tee -a "$OUT/05-environment.txt" echo "checkpoint file mode: $(stat -c '%a %U' cp-env/core.img)" | tee -a "$OUT/05-environment.txt" stop_port