Files
easyai-ai-gateway/scripts/cluster/configure-control-plane-io.sh
T
wangbo e20688bbe2 fix(k3s): 持久化香港控制面磁盘调度缓解
原因:香港虚拟磁盘在低吞吐下仍出现高同步写延迟,触发 etcd slow apply、Handler timeout 和 PostgreSQL API Server 连通性异常。\n\n影响:新增仅针对香港根块设备的 mq-deadline 配置脚本,应用时不重启 K3s,并提供显式 rollback 恢复 none;运行手册明确该项仅为缓解,不能替代高性能云盘。\n\n验证:新香港节点同步写探针从 13.28ms 降至 6.25ms;bash -n、ShellCheck、manual-release-test.sh 通过。
2026-08-02 10:39:55 +08:00

85 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=scripts/cluster/common.sh
source "$script_dir/common.sh"
action=${1:-status}
[[ $action == status || $action == apply || $action == rollback ]] || {
echo 'usage: configure-control-plane-io.sh {status|apply|rollback}' >&2
exit 64
}
load_cluster_env
unit_name=easyai-k3s-io-scheduler.service
unit_path=/etc/systemd/system/$unit_name
cluster_ssh "$CLUSTER_HONGKONG_HOST" bash -s -- "$action" "$unit_name" "$unit_path" <<'REMOTE'
set -euo pipefail
action=$1
unit_name=$2
unit_path=$3
root_source=$(findmnt -n -o SOURCE /)
root_partition=${root_source#/dev/}
device=$(lsblk -no PKNAME "/dev/$root_partition" | head -n 1)
[[ $device =~ ^[a-zA-Z0-9._-]+$ && -f /sys/block/$device/queue/scheduler ]]
current_scheduler() {
sed -n 's/.*\[\([^]]*\)\].*/\1/p' "/sys/block/$device/queue/scheduler"
}
case $action in
status)
configured=false
systemctl is-enabled --quiet "$unit_name" 2>/dev/null && configured=true
printf 'control_plane_io=STATUS node=hongkong device=%s scheduler=%s configured=%s\n' \
"$device" "$(current_scheduler)" "$configured"
;;
apply)
grep -qw mq-deadline "/sys/block/$device/queue/scheduler" || {
echo 'mq-deadline is not supported by the Hong Kong root block device' >&2
exit 1
}
temporary=$(mktemp /etc/systemd/system/.easyai-k3s-io-scheduler.XXXXXX)
cleanup() {
[[ -f ${temporary:-} ]] && rm -f -- "$temporary" || true
}
trap cleanup EXIT
cat >"$temporary" <<EOF
[Unit]
Description=EasyAI K3s control-plane disk scheduler
After=local-fs.target
Before=k3s.service
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'printf "%s\\n" mq-deadline > /sys/block/$device/queue/scheduler'
ExecStop=/bin/sh -c 'printf "%s\\n" none > /sys/block/$device/queue/scheduler'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 "$temporary"
install -o root -g root -m 0644 "$temporary" "$unit_path"
systemctl daemon-reload
systemctl enable --now "$unit_name" >/dev/null
[[ $(current_scheduler) == mq-deadline ]]
printf 'control_plane_io=APPLIED node=hongkong device=%s scheduler=mq-deadline restart=false\n' "$device"
;;
rollback)
if [[ -f $unit_path ]]; then
systemctl disable --now "$unit_name" >/dev/null 2>&1 || true
rm -f -- "$unit_path"
systemctl daemon-reload
else
printf '%s\n' none >"/sys/block/$device/queue/scheduler"
fi
[[ $(current_scheduler) == none ]]
printf 'control_plane_io=ROLLED_BACK node=hongkong device=%s scheduler=none\n' "$device"
;;
esac
REMOTE