fix(tenant): 修复生产备份迁移与未读查询性能

This commit is contained in:
yml
2026-11-16 19:50:49 +08:00
parent fed950ea5a
commit ac9717a5d8
5 changed files with 341 additions and 3 deletions
@@ -2,6 +2,7 @@ package middleware
import (
"context"
"errors"
"strings"
"hfb_sys/backend/internal/config"
@@ -62,6 +63,10 @@ func TenantContext(resolver TenantResolver, appEnv string, logger *zap.Logger, d
scope, err = resolver.ResolveCode(c.Request.Context(), defaultTenantCode)
}
if err != nil {
if errors.Is(err, context.Canceled) {
c.Abort()
return
}
if logger != nil {
logger.Error("经营租户解析失败,拒绝按旧站点上下文继续",
zap.String("host", host),
@@ -127,6 +127,36 @@ func TestTenantContextFailsClosedOnResolveError(t *testing.T) {
}
}
func TestTenantContextIgnoresClientCancellation(t *testing.T) {
gin.SetMode(gin.TestMode)
engine := gin.New()
engine.Use(TenantContext(
&fakeTenantResolver{hostErr: context.Canceled},
"production",
nil,
))
handlerCalled := false
engine.GET("/probe", func(c *gin.Context) {
handlerCalled = true
c.Status(http.StatusNoContent)
})
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
req.Host = "jieyou.example.com"
ctx, cancel := context.WithCancel(req.Context())
cancel()
req = req.WithContext(ctx)
rec := httptest.NewRecorder()
engine.ServeHTTP(rec, req)
if handlerCalled {
t.Fatal("客户端取消后业务处理器不应继续执行")
}
if rec.Code == http.StatusServiceUnavailable {
t.Fatalf("客户端取消不应返回 503, body=%s", rec.Body.String())
}
}
func TestTenantContextSkipsLegacyDistributionFallback(t *testing.T) {
tenantResolver := &fakeTenantResolver{
hostScope: &tenancy.Scope{TenantID: 202, TenantCode: "jieyou", TenantName: "解忧商行"},
@@ -1,8 +1,35 @@
-- +goose Up
ALTER TABLE game_accounts
ADD COLUMN source_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_game_accounts_source_tenant (source_tenant_id, status);
-- 生产库曾提前手工补过来源站点字段,迁移需兼容已有列和索引。
SET @has_source_tenant_column = (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'game_accounts'
AND COLUMN_NAME = 'source_tenant_id'
);
SET @add_source_tenant_column = IF(
@has_source_tenant_column = 0,
'ALTER TABLE game_accounts ADD COLUMN source_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id',
'DO 0'
);
PREPARE add_source_tenant_column FROM @add_source_tenant_column;
EXECUTE add_source_tenant_column;
DEALLOCATE PREPARE add_source_tenant_column;
SET @has_source_tenant_index = (
SELECT COUNT(*) FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'game_accounts'
AND INDEX_NAME = 'idx_game_accounts_source_tenant'
);
SET @add_source_tenant_index = IF(
@has_source_tenant_index = 0,
'ALTER TABLE game_accounts ADD KEY idx_game_accounts_source_tenant (source_tenant_id, status)',
'DO 0'
);
PREPARE add_source_tenant_index FROM @add_source_tenant_index;
EXECUTE add_source_tenant_index;
DEALLOCATE PREPARE add_source_tenant_index;
UPDATE game_accounts a
JOIN users u ON u.id = a.owner_id
@@ -0,0 +1,25 @@
-- +goose Up
-- 未读统计按会话和已读时间取消息,覆盖索引可避免逐会话回表。
ALTER TABLE chat_messages
ADD KEY idx_chat_messages_conversation_created_sender (conversation_id, created_at, sender_type, sender_id);
ALTER TABLE chat_participants
ADD KEY idx_chat_participants_unread (participant_type, participant_id, conversation_id, last_read_at);
-- 通知未读需要同时过滤站点并计数,将站点字段纳入覆盖索引。
ALTER TABLE notifications
DROP KEY idx_notifications_tenant_user_read,
ADD KEY idx_notifications_tenant_user_unread_site (tenant_id, user_id, read_at, distribution_site_id, id);
-- +goose Down
ALTER TABLE notifications
DROP KEY idx_notifications_tenant_user_unread_site,
ADD KEY idx_notifications_tenant_user_read (tenant_id, user_id, read_at, id);
ALTER TABLE chat_participants
DROP KEY idx_chat_participants_unread;
ALTER TABLE chat_messages
DROP KEY idx_chat_messages_conversation_created_sender;
+251
View File
@@ -0,0 +1,251 @@
#!/usr/bin/env bash
set -euo pipefail
umask 077
# 将生产 XtraBackup 全量备份导入本地隔离 MySQL,并验证当前迁移链。
# 不接触本地 hfb_sys,也不接触生产数据库。
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BACKUP_DIR="${BACKUP_LOCAL_DIR:-${ROOT_DIR}/bakup/备份}"
WORK_ROOT="${BACKUP_RESTORE_WORK_DIR:-${ROOT_DIR}/artifacts/restore}"
XTRABACKUP_IMAGE="${XTRABACKUP_IMAGE:-percona/percona-xtrabackup:8.4}"
GOOSE_VERSION="${GOOSE_VERSION:-v3.27.1}"
RESTORE_MYSQL_MEMORY="${RESTORE_MYSQL_MEMORY:-768m}"
RESTORE_MYSQL_BUFFER_POOL_BYTES="${RESTORE_MYSQL_BUFFER_POOL_BYTES:-67108864}"
LATEST_MIGRATION="$(
find "${ROOT_DIR}/backend/migrations" -maxdepth 1 -type f -name '*.sql' -exec basename {} \; |
awk -F_ '{ sub(/^0+/, "", $1); print ($1 == "" ? "0" : $1) }' |
sort -n |
tail -n 1
)"
KEEP_RESTORE_WORK="${KEEP_RESTORE_WORK:-0}"
KEEP_TEST_MYSQL="${KEEP_TEST_MYSQL:-0}"
log() { printf '[import] %s\n' "$*"; }
fail() { printf '[import] ERROR: %s\n' "$*" >&2; exit 1; }
require_cmd() { command -v "$1" >/dev/null 2>&1 || fail "缺少命令:$1"; }
field_value() {
local file="$1" key="$2"
awk -F= -v key="${key}" '$1 == key { print substr($0, index($0, "=") + 1); exit }' "${file}"
}
sha256_file() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
else
shasum -a 256 "$1" | awk '{print $1}'
fi
}
file_size() {
if [[ "$(uname)" == 'Darwin' ]]; then
stat -f%z "$1"
else
stat -c%s "$1"
fi
}
require_cmd docker
require_cmd openssl
require_cmd tar
[[ -d "${BACKUP_DIR}" ]] || fail "备份目录不存在:${BACKUP_DIR}"
payload="${BACKUP_DIR}/mysql-full.tgz.enc"
checksum_file="${BACKUP_DIR}/mysql-full.tgz.enc.sha256"
complete_file="${BACKUP_DIR}/complete.env"
manifest_file="${BACKUP_DIR}/manifest.env"
for file in "${payload}" "${checksum_file}" "${complete_file}" "${manifest_file}"; do
[[ -f "${file}" ]] || fail "备份文件缺失:${file}"
done
if [[ -n "${BACKUP_PASSPHRASE_FILE:-}" ]]; then
[[ -f "${BACKUP_PASSPHRASE_FILE}" ]] || fail "密码文件不存在:${BACKUP_PASSPHRASE_FILE}"
BACKUP_PASSPHRASE="$(<"${BACKUP_PASSPHRASE_FILE}")"
fi
[[ -n "${BACKUP_PASSPHRASE:-}" ]] || fail "缺少 BACKUP_PASSPHRASE 或 BACKUP_PASSPHRASE_FILE"
expected_sha="$(field_value "${complete_file}" payload_sha256)"
expected_size="$(field_value "${complete_file}" payload_size_bytes)"
[[ "${expected_sha}" =~ ^[0-9a-f]{64}$ ]] || fail "complete.env 中 SHA-256 非法"
[[ "${expected_size}" =~ ^[0-9]+$ ]] || fail "complete.env 中文件大小非法"
actual_sha="$(sha256_file "${payload}")"
actual_size="$(file_size "${payload}")"
[[ "${actual_sha}" == "${expected_sha}" ]] || fail "加密备份 SHA-256 校验失败"
[[ "${actual_size}" == "${expected_size}" ]] || fail "加密备份文件大小校验失败"
grep -Fqx "${expected_sha} $(basename "${payload}")" "${checksum_file}" ||
fail "checksum 文件内容与 complete.env 不一致"
log "加密备份校验通过:$(basename "${payload}")"
backup_created_at="$(field_value "${manifest_file}" created_at)"
backup_server_uuid="$(field_value "${manifest_file}" server_uuid)"
backup_goose_version="$(field_value "${manifest_file}" goose_version)"
[[ -n "${backup_created_at}" && -n "${backup_server_uuid}" && -n "${backup_goose_version}" ]] ||
fail "manifest.env 缺少关键字段"
mkdir -p "${WORK_ROOT}"
work_dir="$(mktemp -d "${WORK_ROOT}/backup-$(date +%Y%m%d-%H%M%S).XXXXXX")"
data_dir="${work_dir}/data"
decrypted_file="${work_dir}/mysql-full.tgz"
restore_container="hfb-restore-$(date +%Y%m%d%H%M%S)-$$"
restore_volume="hfb-restore-data-$(date +%Y%m%d%H%M%S)-$$"
restore_started=0
volume_started=0
cleanup() {
local code=$?
if [[ "${restore_started}" == '1' ]]; then
if [[ "${code}" -eq 0 && "${KEEP_TEST_MYSQL}" == '1' ]]; then
printf '[import] 迁移测试 MySQL 保留:%s\n' "${restore_container}" >&2
elif [[ "${code}" -eq 0 ]]; then
docker rm -f "${restore_container}" >/dev/null 2>&1 || true
else
printf '[import] 失败现场容器保留:%s\n' "${restore_container}" >&2
docker logs --tail=60 "${restore_container}" >&2 || true
fi
fi
if [[ "${volume_started}" == '1' && "${code}" -eq 0 && "${KEEP_TEST_MYSQL}" != '1' ]]; then
docker volume rm "${restore_volume}" >/dev/null 2>&1 || true
fi
if [[ "${code}" -eq 0 && "${KEEP_RESTORE_WORK}" != '1' ]]; then
chmod -R u+w "${work_dir}" 2>/dev/null || true
rm -rf -- "${work_dir}" 2>/dev/null || printf '[import] 警告:临时目录未完全清理:%s\n' "${work_dir}" >&2
else
printf '[import] 恢复工作目录保留:%s\n' "${work_dir}" >&2
fi
exit "${code}"
}
trap cleanup EXIT
log "解密并解包全量备份"
BACKUP_PASSPHRASE="${BACKUP_PASSPHRASE}" openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 \
-pass env:BACKUP_PASSPHRASE \
-in "${payload}" \
-out "${decrypted_file}"
mkdir -p "${data_dir}"
tar -xzf "${decrypted_file}" -C "${data_dir}"
rm -f -- "${decrypted_file}"
restored_manifest="${data_dir}/manifest.env"
[[ -f "${restored_manifest}" ]] || fail "备份归档内缺少 manifest.env"
[[ "$(field_value "${restored_manifest}" server_uuid)" == "${backup_server_uuid}" ]] ||
fail "归档内 server_uuid 与 manifest.env 不一致"
[[ "$(field_value "${restored_manifest}" goose_version)" == "${backup_goose_version}" ]] ||
fail "归档内 goose_version 与 manifest.env 不一致"
if ! docker image inspect "${XTRABACKUP_IMAGE}" >/dev/null 2>&1; then
log "拉取 XtraBackup 镜像:${XTRABACKUP_IMAGE}"
docker pull "${XTRABACKUP_IMAGE}" >/dev/null
fi
log "将恢复数据写入 Docker 命名卷"
docker volume create "${restore_volume}" >/dev/null
volume_started=1
docker run --rm \
--entrypoint sh \
-v "${data_dir}:/source:ro" \
-v "${restore_volume}:/target" \
mysql:8.4 \
-c 'cp -a /source/. /target/ && chown -R mysql:mysql /target'
log "执行 xtrabackup --prepare"
docker run --rm \
--user root \
-v "${restore_volume}:/restore" \
"${XTRABACKUP_IMAGE}" \
xtrabackup --prepare --target-dir=/restore
log "启动隔离 MySQL:${restore_container}"
docker run -d \
--name "${restore_container}" \
--network none \
--memory="${RESTORE_MYSQL_MEMORY}" \
--memory-swap="${RESTORE_MYSQL_MEMORY}" \
-v "${restore_volume}:/var/lib/mysql" \
mysql:8.4 \
--skip-log-bin \
--skip-grant-tables \
--skip-networking=1 \
--innodb-redo-log-capacity=536870912 \
--innodb-buffer-pool-size="${RESTORE_MYSQL_BUFFER_POOL_BYTES}" \
--innodb-log-buffer-size=8388608 \
--performance-schema=0 \
--max-connections=20 \
--table-open-cache=128 \
--innodb-flush-log-at-trx-commit=2 \
--max-allowed-packet=268435456 >/dev/null
restore_started=1
for ((attempt = 1; attempt <= 90; attempt++)); do
if docker exec "${restore_container}" mysqladmin -uroot --protocol=socket ping >/dev/null 2>&1; then
break
fi
if [[ "${attempt}" -eq 90 ]]; then
docker logs --tail=80 "${restore_container}" >&2 || true
fail "隔离 MySQL 启动超时"
fi
sleep 1
done
source_db="hfb_sys"
docker exec "${restore_container}" mysql -uroot -N -B -e \
"SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME='${source_db}'" |
grep -qx "${source_db}" || fail "恢复数据中未找到数据库:${source_db}"
before_version="$(
docker exec "${restore_container}" mysql -uroot -N -B "${source_db}" \
-e "SELECT COALESCE(MAX(version_id), 0) FROM goose_db_version WHERE is_applied=1" 2>/dev/null || printf '0'
)"
[[ "${before_version}" =~ ^[0-9]+$ ]] || before_version=0
require_cmd go
mkdir -p "${work_dir}/gopath" "${work_dir}/gocache"
go_proxy="$(go env GOPROXY)"
go_sumdb="$(go env GOSUMDB)"
go_mod_cache="$(go env GOMODCACHE)"
goose_os=linux
goose_arch=amd64
log "构建容器内 Goose ${GOOSE_VERSION}"
(
cd "${ROOT_DIR}/backend"
GOENV=off GOBIN='' GOPATH="${work_dir}/gopath" GOCACHE="${work_dir}/gocache" \
GOMODCACHE="${GOMODCACHE:-${go_mod_cache}}" \
GOPROXY="${GOPROXY:-${go_proxy}}" GOSUMDB="${GOSUMDB:-${go_sumdb}}" \
GOOS="${goose_os}" GOARCH="${goose_arch}" go install \
"github.com/pressly/goose/v3/cmd/goose@${GOOSE_VERSION}"
)
goose_bin="${work_dir}/gopath/bin/${goose_os}_${goose_arch}/goose"
if [[ ! -f "${goose_bin}" ]]; then
goose_bin="${work_dir}/gopath/bin/goose"
fi
[[ -x "${goose_bin}" ]] || fail "Goose 构建完成但未找到二进制"
docker cp "${goose_bin}" "${restore_container}:/tmp/goose"
docker cp "${ROOT_DIR}/backend/migrations/." "${restore_container}:/tmp/migrations"
dsn="root:@unix(/var/run/mysqld/mysqld.sock)/${source_db}?charset=utf8mb4&parseTime=True&loc=Local&multiStatements=true"
log "执行 Goose 迁移:${before_version} -> 当前代码"
docker exec "${restore_container}" /tmp/goose -dir /tmp/migrations mysql "${dsn}" up
after_version="$(
docker exec "${restore_container}" mysql -uroot -N -B "${source_db}" \
-e "SELECT COALESCE(MAX(version_id), 0) FROM goose_db_version WHERE is_applied=1"
)"
failed_migrations="$(
docker exec "${restore_container}" mysql -uroot -N -B "${source_db}" \
-e "SELECT COUNT(*) FROM goose_db_version WHERE is_applied=0"
)"
table_count="$(
docker exec "${restore_container}" mysql -uroot -N -B \
-e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${source_db}'"
)"
tenant_count="$(
docker exec "${restore_container}" mysql -uroot -N -B "${source_db}" \
-e "SELECT COUNT(*) FROM tenants WHERE code IN ('dachui','jieyou') AND status='active'"
)"
[[ "${failed_migrations}" == '0' ]] || fail "存在未成功应用的 Goose 迁移"
[[ "${after_version}" == "${LATEST_MIGRATION}" ]] ||
fail "迁移后 Goose 版本异常:${after_version},期望:${LATEST_MIGRATION}"
[[ "${tenant_count}" == '2' ]] || fail "经营租户初始化异常:${tenant_count}"
log "导入与迁移测试完成"
log "备份版本:${backup_goose_version},迁移前:${before_version},迁移后:${after_version}"
log "恢复数据库:${source_db},表数量:${table_count}"
log "连接:docker exec -it ${restore_container} mysql -uroot ${source_db}"