2026-08-16 01:27:30 +08:00
|
|
|
package backendupdate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
|
|
"yms-daemon/internal/hostnginx"
|
|
|
|
|
"yms-daemon/internal/transaction"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// gatewaySwitchIntent 构造原生后端 Nginx 上游切换步骤的意图,记录切换前、
|
|
|
|
|
// 切换后各自的端口与配置摘要,供审计与回放时确认两侧快照一致。
|
2026-08-16 01:27:30 +08:00
|
|
|
func gatewaySwitchIntent(request persistedRequest, before hostnginx.Snapshot, after hostnginx.Snapshot) transaction.StepIntent {
|
|
|
|
|
return stepIntent("backend.gateway.switch", "switch host Nginx backend upstream", struct {
|
|
|
|
|
BeforePort int `json:"beforePort"`
|
|
|
|
|
BeforeHash string `json:"beforeHash"`
|
|
|
|
|
AfterPort int `json:"afterPort"`
|
|
|
|
|
AfterHash string `json:"afterHash"`
|
|
|
|
|
}{before.ActivePort, snapshotDigest(before), after.ActivePort, snapshotDigest(after)})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// activeLinkIntent 构造替换原生后端兼容性链接步骤的意图,记录链接路径、
|
|
|
|
|
// 更新前状态以及即将指向的安装路径。
|
2026-08-16 01:27:30 +08:00
|
|
|
func activeLinkIntent(request persistedRequest, installedPath string) transaction.StepIntent {
|
|
|
|
|
return stepIntent("backend.native.active-link", "replace backend compatibility link", struct {
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
Before pathState `json:"before"`
|
|
|
|
|
InstalledPath string `json:"installedPath"`
|
|
|
|
|
}{request.ActiveJARPath, request.ActiveJARBefore, installedPath})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// stopPreviousUnitIntent 构造停止前一原生后端单元的步骤意图,记录待停止单元名。
|
2026-08-16 01:27:30 +08:00
|
|
|
func stopPreviousUnitIntent(request persistedRequest) transaction.StepIntent {
|
|
|
|
|
return stepIntent("backend.previous-unit.stop", "stop previous backend unit after drain", struct {
|
|
|
|
|
Unit string `json:"unit"`
|
|
|
|
|
}{request.PreviousUnit})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// activeLinkRestoreIntent 构造恢复原生后端兼容性链接步骤的意图,记录链接
|
|
|
|
|
// 路径及其更新前状态,供补偿流程回滚。
|
2026-08-16 01:27:30 +08:00
|
|
|
func activeLinkRestoreIntent(request persistedRequest) transaction.StepIntent {
|
|
|
|
|
return stepIntent("backend.native.active-link.restore", "restore backend compatibility path", struct {
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
Before pathState `json:"before"`
|
|
|
|
|
}{request.ActiveJARPath, request.ActiveJARBefore})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// gatewayRestoreIntent 构造恢复原生后端 Nginx 上游步骤的意图,记录需恢复到的端口。
|
2026-08-16 01:27:30 +08:00
|
|
|
func gatewayRestoreIntent(request persistedRequest) transaction.StepIntent {
|
|
|
|
|
return stepIntent("backend.gateway.restore", "restore host Nginx backend upstream", struct {
|
|
|
|
|
Port int `json:"port"`
|
|
|
|
|
}{request.PreviousGatewayPort})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// stopTargetUnitIntent 构造停止被补偿的原生后端目标单元步骤的意图,记录待停止单元名。
|
2026-08-16 01:27:30 +08:00
|
|
|
func stopTargetUnitIntent(request persistedRequest) transaction.StepIntent {
|
|
|
|
|
return stepIntent("backend.target-unit.stop", "stop compensated backend target unit", struct {
|
|
|
|
|
Unit string `json:"unit"`
|
|
|
|
|
}{request.TargetUnit})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// restoreTargetSlotIntent 构造恢复被补偿的原生后端目标槽位步骤的意图,记录
|
|
|
|
|
// 槽位链接路径、其安装路径以及更新前的链接目标。
|
2026-08-16 01:27:30 +08:00
|
|
|
func restoreTargetSlotIntent(request persistedRequest, installedPath string) transaction.StepIntent {
|
|
|
|
|
return stepIntent("backend.target-slot.restore", "restore compensated backend target slot", struct {
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
InstalledPath string `json:"installedPath"`
|
|
|
|
|
PreviousTarget string `json:"previousTarget"`
|
|
|
|
|
}{request.TargetSlotJAR, installedPath, request.PreviousSlotTarget})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// stepIntent 以键名、显示名和任意值构造一个事务步骤意图。意图值会被序列化为
|
|
|
|
|
// JSON;若序列化失败则直接 panic,因为该失败属于程序缺陷而非运行期错误。
|
2026-08-16 01:27:30 +08:00
|
|
|
func stepIntent(key string, name string, value any) transaction.StepIntent {
|
|
|
|
|
content, err := json.Marshal(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return transaction.StepIntent{Key: key, Name: name, Intent: content}
|
|
|
|
|
}
|