#!/usr/bin/env bash set -euo pipefail bash /scripts/guard.sh mode=${1:?seed|verify|full} [[ $mode == seed || $mode == verify || $mode == full ]] cd /lab/data mkdir -p db pid='' stop() { if [[ -n $pid ]] && kill -0 "$pid" 2>/dev/null; then kill -TERM "$pid" wait "$pid" || true fi pid='' } trap stop EXIT start() { mongod --dbpath /lab/data/db --port 27129 --bind_ip 127.0.0.1 --wiredTigerCacheSizeGB 0.25 --logpath /lab/data/mongo.log --logappend --setParameter diagnosticDataCollectionEnabled=false & pid=$! for ((i=0;i<90;i++)); do if timeout 5 mongosh --quiet --host 127.0.0.1 --port 27129 --eval 'quit(db.adminCommand({ping:1}).ok?0:1)' >/dev/null 2>&1; then return; fi kill -0 "$pid" sleep 2 done return 1 } js() { timeout 30 mongosh --quiet --host 127.0.0.1 --port 27129 --eval "$1"; } verify() { js 'const d=db.getSiblingDB("node4_synthetic_pilot"); if(d.marker.countDocuments({_id:"acknowledged-before-rebuild",value:"preserve-synthetic-marker-v1"})!==1)throw Error("marker missing"); print("MARKER_PRESERVED");'; } start if [[ $mode == seed ]]; then js 'const d=db.getSiblingDB("node4_synthetic_pilot"); d.marker.updateOne({_id:"acknowledged-before-rebuild"},{$set:{value:"preserve-synthetic-marker-v1"}},{upsert:true,writeConcern:{w:1,j:true}}); print("SEED_JOURNALED");' verify elif [[ $mode == verify ]]; then verify else verify fallocate -l 33554432 recovery-reserve available=$(stat -f -c '%a %S' . | awk '{printf "%.0f",$1*$2}') (( available > 8388608 )) fallocate -l "$((available-8388608))" synthetic-filler set +e dd if=/dev/zero of=synthetic-filler bs=1048576 count=12 oflag=append conv=notrunc,fdatasync 2>/tmp/fill.log fill_exit=$? set -e [[ $fill_exit != 0 ]] grep -q 'No space left on device' /tmp/fill.log printf 'FILESYSTEM_ENOSPC bytes=%s\n' "$(stat -c %s synthetic-filler)" set +e js 'const d=db.getSiblingDB("node4_synthetic_pilot"); d.full_probe.insertOne({_id:"space-exhaustion-probe",payload:"x".repeat(65536)},{writeConcern:{w:1,j:true}}); print("WRITE_DURING_FULL_ACKNOWLEDGED");' >/tmp/full-write.log 2>&1 write_exit=$? set -e printf 'MONGO_FULL_WRITE_EXIT=%s\n' "$write_exit" tail -n 5 /tmp/full-write.log rm -- recovery-reserve stop rm -- synthetic-filler start verify js 'const d=db.getSiblingDB("node4_synthetic_pilot"); d.marker.updateOne({_id:"after-full-recovery"},{$set:{ok:true}},{upsert:true,writeConcern:{w:1,j:true}}); if(d.marker.countDocuments({_id:"after-full-recovery",ok:true})!==1)throw Error("recovery write missing"); print("RECOVERY_WRITE_OK");' printf 'FULL_RECOVERY_OK\n' fi stop printf 'MONGO_PROBE_OK mode=%s pod=%s\n' "$mode" "$POD_UID"