Files
rustfs/scripts/wait_cluster_disk_queue.sh
T
Hauser Zhi a368c669a2 feat(bench): record write latency in preflight queue samples (#9850)
Co-authored-by: heihutu <heihutu@gmail.com>
Co-authored-by: zhi22915 <qiuzgang@gmail.com>
Co-authored-by: qunjiazhu <qunjiazhu@gmail.com>
2026-12-02 16:06:24 +08:00

173 lines
5.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Wait for two consecutive low-queue samples across the SSH inventory.
set -euo pipefail
export LC_ALL=C
targets=()
mappings=()
identity=""
budget=60
validate_only=false
while (($#)); do
case "$1" in
--validate-only) validate_only=true; shift; continue ;;
--target|--devices|--identity-file|--timeout-secs)
[[ $# -ge 2 ]] || { echo "missing value for $1" >&2; exit 2; }
case "$1" in
--target) targets+=("$2") ;;
--devices) mappings+=("$2") ;;
--identity-file) identity="$2" ;;
--timeout-secs) budget="$2" ;;
esac
shift 2 ;;
*) echo "unknown disk queue option: $1" >&2; exit 2 ;;
esac
done
if ! [[ "$budget" =~ ^[1-9][0-9]{0,5}$ ]] || ((budget < 11)); then
echo "disk queue timeout must be at least 11 seconds" >&2
exit 2
fi
(("${#targets[@]}" > 0 && "${#targets[@]}" == "${#mappings[@]}")) || { echo "disk queue check requires one device mapping per SSH target" >&2; exit 2; }
nodes=""
hosts=""
device_lists=()
for target in "${targets[@]}"; do
[[ "$target" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*=[A-Za-z0-9][A-Za-z0-9@._:%+-]*$ ]] || { echo "invalid disk queue SSH target" >&2; exit 2; }
node="${target%%=*}"
host="${target#*=}"
[[ "$nodes" != *" $node "* ]] || { echo "duplicate disk queue node: $node" >&2; exit 2; }
[[ "$hosts" != *" $host "* ]] || { echo "duplicate disk queue SSH host: $host" >&2; exit 2; }
hosts="$hosts $host "
nodes="$nodes $node "
matches=0
for mapping in "${mappings[@]}"; do
[[ "${mapping%%=*}" == "$node" ]] || continue
[[ "$mapping" =~ ^[^=]+=([A-Za-z0-9][A-Za-z0-9._-]*)(\+[A-Za-z0-9][A-Za-z0-9._-]*)*$ ]] || { echo "invalid device list for $node" >&2; exit 2; }
devices="${mapping#*=}"
if ! awk -v devices="$devices" 'BEGIN { n=split(devices,d,"+"); for(i=1;i<=n;i++) if(seen[d[i]]++) exit 1 }'; then
echo "duplicate disk queue device for $node" >&2; exit 2
fi
device_lists+=("${devices//+/ }")
matches=$((matches+1))
done
[[ "$matches" == 1 ]] || { echo "expected one device mapping for $node" >&2; exit 2; }
done
[[ "$validate_only" == false ]] || exit 0
ssh_args=(-o BatchMode=yes -o ConnectTimeout=5)
if [[ -n "$identity" ]]; then ssh_args+=(-i "$identity"); fi
artifacts=$(mktemp -d)
pids=()
cleanup() {
local pid
if ((${#pids[@]})); then
kill -TERM "${pids[@]}" 2>/dev/null || true
for pid in "${pids[@]}"; do wait "$pid" 2>/dev/null || true; done
fi
rm -rf -- "$artifacts"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
# Only validated device names are placed in the SSH command. The sampler uses
# monotonic uptime and the weighted I/O milliseconds from /proc/diskstats.
# Write completions and write milliseconds describe the same sampling window.
sample=$(cat <<'REMOTE'
set -eu
export LC_ALL=C
snapshot() {
awk -v devices="$devices" '
BEGIN { n=split(devices,d," "); for(i=1;i<=n;i++) want[d[i]]=1 }
FNR==NR { t=$1; next }
$3 in want {
if (NF<14 || $8 !~ /^[0-9]+$/ || $11 !~ /^[0-9]+$/ || $14 !~ /^[0-9]+$/) exit 2
print $3,t,$14,$8,$11
got++
}
END { if (got!=n) exit 2 }
' /proc/uptime /proc/diskstats
}
before=$(snapshot)
sleep 5
after=$(snapshot)
printf '%s\n--\n%s\n' "$before" "$after" | awk '
$0=="--" { second=1; next }
!second {
if ($1 in t) bad=1
t[$1]=$2; w[$1]=$3; writes[$1]=$4; write_ms[$1]=$5
count++
next
}
{
if (!($1 in t) || seen[$1]++ || $2<=t[$1] || $3<w[$1] || $4<writes[$1] || $5<write_ms[$1]) {
bad=1
next
}
q=($3-w[$1])/(1000*($2-t[$1]))
sum+=q
if (q>peak) peak=q
completed=$4-writes[$1]
if (completed>0) {
elapsed_ms=$5-write_ms[$1]
total_completed+=completed
total_write_ms+=elapsed_ms
await_ms=elapsed_ms/completed
if (await_ms>peak_write_ms) peak_write_ms=await_ms
}
got++
}
END {
if (bad||!count||got!=count) exit 2
printf "%.6f %.6f %.0f",sum/count,peak,total_completed
if (total_completed>0) printf " %.6f %.6f\n",total_write_ms/total_completed,peak_write_ms
else printf " NA NA\n"
}
'
REMOTE
)
end=$((SECONDS+budget))
streak=0
sample_number=0
printf 'sample\tnode\tmean_queue\tpeak_queue\tcompleted_writes\twrite_ms_per_op\tpeak_write_ms_per_op\n'
while ((end-SECONDS >= 5)); do
sample_number=$((sample_number+1))
remaining=$((end-SECONDS))
for i in "${!targets[@]}"; do
timeout --foreground --kill-after=1s "${remaining}s" ssh "${ssh_args[@]}" -- "${targets[i]#*=}" \
"devices='${device_lists[i]}'; $sample" >"$artifacts/$i" &
pids+=("$!")
done
valid=true
for pid in "${pids[@]}"; do
if ! wait "$pid"; then valid=false; fi
done
pids=()
if [[ "$valid" != true ]]; then
echo "disk queue sample failed or timed out" >&2
exit 1
fi
quiet=true
for i in "${!targets[@]}"; do
result=$(cat "$artifacts/$i")
if [[ ! "$result" =~ ^([0-9]+\.[0-9]+)\ ([0-9]+\.[0-9]+)\ ([0-9]+)\ (NA|[0-9]+\.[0-9]+)\ (NA|[0-9]+\.[0-9]+)$ ]]; then
echo "invalid disk queue sample from ${targets[i]%%=*}" >&2; exit 1
fi
mean="${BASH_REMATCH[1]}"
peak="${BASH_REMATCH[2]}"
printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$sample_number" "${targets[i]%%=*}" "$mean" "$peak" \
"${BASH_REMATCH[3]}" "${BASH_REMATCH[4]}" "${BASH_REMATCH[5]}"
if ! awk -v mean="$mean" -v peak="$peak" 'BEGIN {exit !(mean<=0.25 && peak<=0.75)}'; then quiet=false; fi
done
if [[ "$quiet" == true ]]; then
streak=$((streak+1))
if ((streak == 2 && SECONDS <= end)); then exit 0; fi
else
streak=0
fi
done
echo "disk queues did not settle within ${budget}s" >&2
exit 1