diff --git a/.gitea/scripts/nacos-readback.mjs b/.gitea/scripts/nacos-readback.mjs new file mode 100644 index 00000000..6644cdec --- /dev/null +++ b/.gitea/scripts/nacos-readback.mjs @@ -0,0 +1,25 @@ +import { createHash } from 'node:crypto'; + +export async function verifyConfigReadback(url, expectedContent, expectExists, options = {}) { + const request = options.request || fetch; + const attempts = options.attempts || 3; + const delayMs = options.delayMs ?? 1000; + const expectedHash = expectExists ? sha256(expectedContent) : null; + + for (let attempt = 0; attempt < attempts; attempt += 1) { + const response = await request(url); + const actual = await response.text(); + const matches = expectExists + ? response.ok && sha256(actual) === expectedHash + : response.status === 404; + if (matches) return true; + if (attempt + 1 < attempts && delayMs > 0) { + await new Promise((resolve) => setTimeout(resolve, delayMs)); + } + } + return false; +} + +function sha256(value) { + return createHash('sha256').update(value ?? '', 'utf8').digest('hex'); +} diff --git a/.gitea/scripts/nacos-readback.test.mjs b/.gitea/scripts/nacos-readback.test.mjs new file mode 100644 index 00000000..5b7e09b7 --- /dev/null +++ b/.gitea/scripts/nacos-readback.test.mjs @@ -0,0 +1,45 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { verifyConfigReadback } from './nacos-readback.mjs'; + +test('retries a stale read until the restored config is visible', async () => { + const responses = [ + new Response('old config', { status: 200 }), + new Response('restored config', { status: 200 }) + ]; + let calls = 0; + + const verified = await verifyConfigReadback('http://nacos/config', 'restored config', true, { + request: async () => responses[calls++], + attempts: 3, + delayMs: 0 + }); + + assert.equal(verified, true); + assert.equal(calls, 2); +}); + +test('fails closed when the config does not match after bounded retries', async () => { + let calls = 0; + const verified = await verifyConfigReadback('http://nacos/config', 'restored config', true, { + request: async () => { + calls += 1; + return new Response('different config', { status: 200 }); + }, + attempts: 3, + delayMs: 0 + }); + + assert.equal(verified, false); + assert.equal(calls, 3); +}); + +test('confirms deletion only after the config is absent', async () => { + const verified = await verifyConfigReadback('http://nacos/config', null, false, { + request: async () => new Response(null, { status: 404 }), + attempts: 1, + delayMs: 0 + }); + + assert.equal(verified, true); +}); diff --git a/.gitea/scripts/rollback-nacos.mjs b/.gitea/scripts/rollback-nacos.mjs index a990da6f..d8ce02ef 100644 --- a/.gitea/scripts/rollback-nacos.mjs +++ b/.gitea/scripts/rollback-nacos.mjs @@ -2,6 +2,7 @@ import { createHash } from 'node:crypto'; import { readFile, readdir, rm } from 'node:fs/promises'; import { join } from 'node:path'; import { envName, recordsRoot, snapshotRoot, writeJson } from './release-store.mjs'; +import { verifyConfigReadback } from './nacos-readback.mjs'; const environment = envName(process.env.NACOS_ENVIRONMENT); const group = 'DEFAULT_GROUP'; @@ -40,9 +41,8 @@ for (const target of targets) { ? await fetch(`${base}/nacos/v1/cs/configs?accessToken=${encodeURIComponent(target.token)}`, { method: 'POST', body: new URLSearchParams({ dataId: entry.dataId, group, tenant: entry.namespace, type: 'yaml', content }) }) : await fetch(`${base}/nacos/v1/cs/configs?${query}`, { method: 'DELETE' }); if (!response.ok || (exists && (await response.text()).trim() !== 'true')) throw new Error(`回退失败:${target.id}/${entry.namespace}/${entry.dataId}`); - const check = await fetch(`${base}/nacos/v1/cs/configs?${new URLSearchParams({ dataId: entry.dataId, group, tenant: entry.namespace, accessToken: target.token })}`); - const actual = await check.text(); - if (exists ? (!check.ok || sha256(actual) !== sha256(content)) : check.status !== 404) throw new Error(`回退摘要校验失败:${target.id}/${entry.namespace}/${entry.dataId}`); + const checkUrl = `${base}/nacos/v1/cs/configs?${new URLSearchParams({ dataId: entry.dataId, group, tenant: entry.namespace, accessToken: target.token })}`; + if (!(await verifyConfigReadback(checkUrl, content, exists))) throw new Error(`回退摘要校验失败:${target.id}/${entry.namespace}/${entry.dataId}`); } } } diff --git a/docs/scripts/运行与发布配置总说明.md b/docs/scripts/运行与发布配置总说明.md index 77966a2a..94ad8d62 100644 --- a/docs/scripts/运行与发布配置总说明.md +++ b/docs/scripts/运行与发布配置总说明.md @@ -70,6 +70,7 @@ Nacos 发布流程应先保存目标环境快照,再发布全部 YAML,并从 ``` 缺少该挂载时,Prepare、Deploy 和 Rollback 任务之间无法共享发布记录、配置快照和应用包。 +Nacos 回退同样按快照恢复后校验配置摘要;考虑到集群读写传播延迟,读回校验最多重试 3 次、间隔 1 秒,仍不匹配时继续失败关闭,不记录回退成功。 Salon DEV/SIT/PROD 的 Nacos `core.yaml` 启用 Flyway,并固定目标 schema 为 `jayucloud_salon`、关闭自动建库。当前 Spring Boot 2.7.18 管理的 Flyway Community 8.5.13 要求目标数据库为 MySQL 8.0 或更高兼容版本;MySQL 5.7 会在迁移开始前导致 Core 启动失败。Gitea 现有 SIT/生产自动部署会构建并校验 Core JAR 中包含 V1 迁移;首个 Core 节点启动时由 Flyway 在已存在的空库中执行迁移,健康检查通过后才继续发布下一个 Core 节点。工作流不直接连接数据库或执行 SQL;迁移使用应用连接的 Nacos 数据源凭据,因此该账号必须具备建表、索引、数据初始化及初始化脚本所需存储过程的权限。Flyway 禁用自动 baseline、禁用 clean,并校验已应用迁移;schema 必须预先存在且为空,非空或迁移失败时 Core 启动失败并停止后续节点。升级数据库版本后不要先手动执行完整初始化 SQL,应由 Flyway 在空 schema 执行 V1。应用回退不会回滚数据库,后续修复只能追加新版本迁移。 diff --git a/docs/项目变更记录.md b/docs/项目变更记录.md index c1f55a8b..8c11eb27 100644 --- a/docs/项目变更记录.md +++ b/docs/项目变更记录.md @@ -4,8 +4,9 @@ - 检查 Gitea Actions 部署运行 #245:MyBatis-Plus 的 `OrderArtistReviewMapper.countPublicImageReference` 重复映射提示来自 Java Mapper 中两个同名重载方法;将按审核 ID 查询的方法改为独立名称,并新增 Mapper statement 注册测试,避免全局文件引用查询被忽略。 - 部署真正阻塞点是 Core 启动时 Flyway 校验发现数据库中已存在失败的 V1 初始化迁移记录,健康检查因此端口拒绝连接并以退出码 43 失败;该 Flyway 状态不是上述 MyBatis 日志触发的。 +- 自动回退运行 #246 在恢复 Nacos 后单次读回校验失败;回退脚本增加有界重试,并通过模拟短暂读旧值后读到新值的测试覆盖。 - 当前本机 SSH 凭据无法登录 SIT Core/数据库主机,本次没有修改 SIT 数据库。修复 V1 前必须先读取失败记录的 `error_message` 并核对 V1 部分执行的表、索引和种子数据;确认后再按项目 Flyway 规则处理,禁止直接清库、baseline 或自动 repair。 -- 本次无 SQL 变更;Mapper 改名不影响 `FrontendFileController` 使用的单参数全局查询。 +- 调整 `.gitea/scripts/rollback-nacos.mjs` 并新增 `.gitea/scripts/nacos-readback.mjs`、`.gitea/scripts/nacos-readback.test.mjs`;同步更新 `docs/scripts/运行与发布配置总说明.md`。本次无 SQL 变更;Mapper 改名不影响 `FrontendFileController` 使用的单参数全局查询。 # 2026-11-02 Gitea 工作流仓库域名统一