f536987a7e
- doc: add comment
353 lines
13 KiB
Go
353 lines
13 KiB
Go
package nativebackendexecutor
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"yms-daemon/internal/filestore"
|
|
"yms-daemon/internal/healthcheck"
|
|
"yms-daemon/internal/systemd"
|
|
"yms-daemon/internal/transaction"
|
|
)
|
|
|
|
// installJarOperation “安装后端 JAR 到发布存储”的事务操作,实现 transaction.Operation。
|
|
// 它把源文件按给定身份提交到发布存储,并通过 Inspect 判断是否已生效。
|
|
type installJarOperation struct {
|
|
store *filestore.Store
|
|
sourcePath string
|
|
releasePath string
|
|
identity filestore.Identity
|
|
}
|
|
|
|
// Apply 执行安装:打开源 JAR 并以指定身份提交到发布存储。
|
|
// 参数 ctx 未使用,仅用于满足接口签名。返回值是打开或提交失败时的错误。
|
|
func (o *installJarOperation) Apply(context.Context) error {
|
|
source, err := os.Open(o.sourcePath)
|
|
if err != nil {
|
|
return fmt.Errorf("open native backend JAR: %w", err)
|
|
}
|
|
defer source.Close()
|
|
_, err = o.store.Commit(o.releasePath, source, o.identity)
|
|
return err
|
|
}
|
|
|
|
// Inspect 检查安装是否已生效,返回事务检查结果。
|
|
// 参数 ctx 未使用,仅用于满足接口签名。目标冲突时返回未知状态,未找到时返回未应用,
|
|
// 找到时返回已应用并携带结果 JSON;其余情况返回错误。
|
|
func (o *installJarOperation) Inspect(context.Context) (transaction.Inspection, error) {
|
|
file, found, err := o.store.Inspect(o.releasePath, o.identity)
|
|
if errors.Is(err, filestore.ErrDestinationConflict) {
|
|
return transaction.Inspection{Status: transaction.InspectionUnknown}, nil
|
|
}
|
|
if err != nil {
|
|
return transaction.Inspection{}, err
|
|
}
|
|
if !found {
|
|
return transaction.Inspection{Status: transaction.InspectionNotApplied}, nil
|
|
}
|
|
return transaction.Inspection{Status: transaction.InspectionApplied, Result: resultJSON(file)}, nil
|
|
}
|
|
|
|
// slotLinkOperation “替换槽位软链接”的事务操作,实现 transaction.Operation。
|
|
// 它把槽位软链接原子地替换为期望目标,并在 desiredTarget 为空时移除软链接。
|
|
type slotLinkOperation struct {
|
|
path string
|
|
desiredTarget string
|
|
previousTarget string
|
|
}
|
|
|
|
// Apply 原子地替换槽位软链接指向 desiredTarget,空目标则移除软链接。
|
|
// ctx 用于 Inspect 调用。返回值是检查、目录校验或文件操作失败时的错误。
|
|
func (o *slotLinkOperation) Apply(ctx context.Context) error {
|
|
inspection, err := o.Inspect(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch inspection.Status {
|
|
case transaction.InspectionApplied:
|
|
return nil
|
|
case transaction.InspectionNotApplied:
|
|
case transaction.InspectionUnknown:
|
|
return fmt.Errorf("native backend slot link %s does not match the recorded previous target", o.path)
|
|
default:
|
|
return fmt.Errorf("native backend slot link %s returned invalid inspection status %q", o.path, inspection.Status)
|
|
}
|
|
|
|
parent := filepath.Dir(o.path)
|
|
parentInfo, err := os.Lstat(parent)
|
|
if err != nil {
|
|
return fmt.Errorf("inspect native backend slot directory %s: %w", parent, err)
|
|
}
|
|
if !parentInfo.IsDir() || parentInfo.Mode()&os.ModeSymlink != 0 {
|
|
return fmt.Errorf("native backend slot parent is not a direct directory: %s", parent)
|
|
}
|
|
if o.desiredTarget == "" {
|
|
if err := os.Remove(o.path); err != nil {
|
|
return fmt.Errorf("remove native backend slot link %s: %w", o.path, err)
|
|
}
|
|
return syncDirectory(parent)
|
|
}
|
|
targetInfo, err := os.Lstat(o.desiredTarget)
|
|
if err != nil {
|
|
return fmt.Errorf("inspect native backend slot target %s: %w", o.desiredTarget, err)
|
|
}
|
|
if !targetInfo.Mode().IsRegular() || targetInfo.Mode()&os.ModeSymlink != 0 {
|
|
return fmt.Errorf("native backend slot target is not a regular file: %s", o.desiredTarget)
|
|
}
|
|
|
|
temporary, err := os.CreateTemp(parent, ".slot-link-*")
|
|
if err != nil {
|
|
return fmt.Errorf("reserve native backend slot link path: %w", err)
|
|
}
|
|
temporaryPath := temporary.Name()
|
|
if err := temporary.Close(); err != nil {
|
|
_ = os.Remove(temporaryPath)
|
|
return fmt.Errorf("close native backend slot link reservation: %w", err)
|
|
}
|
|
if err := os.Remove(temporaryPath); err != nil {
|
|
return fmt.Errorf("remove native backend slot link reservation: %w", err)
|
|
}
|
|
committed := false
|
|
defer func() {
|
|
if !committed {
|
|
_ = os.Remove(temporaryPath)
|
|
}
|
|
}()
|
|
if err := os.Symlink(o.desiredTarget, temporaryPath); err != nil {
|
|
return fmt.Errorf("create native backend slot link: %w", err)
|
|
}
|
|
if err := os.Rename(temporaryPath, o.path); err != nil {
|
|
return fmt.Errorf("replace native backend slot link %s: %w", o.path, err)
|
|
}
|
|
committed = true
|
|
return syncDirectory(parent)
|
|
}
|
|
|
|
// Inspect 判断槽位软链接当前状态与期望是否一致,返回事务检查结果。
|
|
// 参数 ctx 未使用,仅用于满足接口签名。软链接指向 desiredTarget 时为已应用,
|
|
// 指向 previousTarget 时为未应用,其余情况为未知;错误时返回错误。
|
|
func (o *slotLinkOperation) Inspect(context.Context) (transaction.Inspection, error) {
|
|
info, err := os.Lstat(o.path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
if o.desiredTarget == "" {
|
|
if err := syncDirectory(filepath.Dir(o.path)); err != nil {
|
|
return transaction.Inspection{}, err
|
|
}
|
|
return transaction.Inspection{Status: transaction.InspectionApplied, Result: linkResult(o.path, "")}, nil
|
|
}
|
|
if o.previousTarget == "" {
|
|
return transaction.Inspection{Status: transaction.InspectionNotApplied}, nil
|
|
}
|
|
return transaction.Inspection{Status: transaction.InspectionUnknown}, nil
|
|
}
|
|
if err != nil {
|
|
return transaction.Inspection{}, err
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
return transaction.Inspection{Status: transaction.InspectionUnknown}, nil
|
|
}
|
|
target, err := os.Readlink(o.path)
|
|
if err != nil {
|
|
return transaction.Inspection{}, err
|
|
}
|
|
result := linkResult(o.path, target)
|
|
if target == o.desiredTarget {
|
|
if err := syncDirectory(filepath.Dir(o.path)); err != nil {
|
|
return transaction.Inspection{}, err
|
|
}
|
|
return transaction.Inspection{Status: transaction.InspectionApplied, Result: result}, nil
|
|
}
|
|
if target == o.previousTarget {
|
|
return transaction.Inspection{Status: transaction.InspectionNotApplied, Result: result}, nil
|
|
}
|
|
return transaction.Inspection{Status: transaction.InspectionUnknown, Result: result}, nil
|
|
}
|
|
|
|
// unitStartOperation “启动 systemd 单元”的事务操作,实现 transaction.Operation。
|
|
// 它启动指定单元,并通过 Inspect 依据单元活跃状态判断是否已生效。
|
|
type unitStartOperation struct {
|
|
units systemd.Manager
|
|
name string
|
|
}
|
|
|
|
// Apply 启动指定单元。
|
|
// ctx 用于取消。返回值是启动失败时的错误。
|
|
func (o *unitStartOperation) Apply(ctx context.Context) error {
|
|
return o.units.Start(ctx, o.name)
|
|
}
|
|
|
|
// Inspect 检查单元是否已启动:活跃为已应用,非活跃或失败为未应用,其余为未知。
|
|
// ctx 用于取消。返回值是事务检查结果;检查单元失败时返回错误。
|
|
func (o *unitStartOperation) Inspect(ctx context.Context) (transaction.Inspection, error) {
|
|
unit, err := o.units.Inspect(ctx, o.name)
|
|
if err != nil {
|
|
return transaction.Inspection{}, err
|
|
}
|
|
result := unitResult(unit)
|
|
switch unit.ActiveState {
|
|
case activeState:
|
|
return transaction.Inspection{Status: transaction.InspectionApplied, Result: result}, nil
|
|
case inactiveState, failedState:
|
|
return transaction.Inspection{Status: transaction.InspectionNotApplied, Result: result}, nil
|
|
default:
|
|
return transaction.Inspection{Status: transaction.InspectionUnknown, Result: result}, nil
|
|
}
|
|
}
|
|
|
|
// unitStopOperation “停止 systemd 单元”的事务操作,实现 transaction.Operation。
|
|
// 它停止指定单元,并通过 Inspect 依据单元活跃状态判断是否已停止。
|
|
type unitStopOperation struct {
|
|
units systemd.Manager
|
|
name string
|
|
}
|
|
|
|
// Apply 停止指定单元。
|
|
// ctx 用于取消。返回值是停止失败时的错误。
|
|
func (o *unitStopOperation) Apply(ctx context.Context) error {
|
|
return o.units.Stop(ctx, o.name)
|
|
}
|
|
|
|
// Inspect 检查单元是否已停止:非活跃或失败为已应用,活跃为未应用,其余为未知。
|
|
// ctx 用于取消。返回值是事务检查结果;检查单元失败时返回错误。
|
|
func (o *unitStopOperation) Inspect(ctx context.Context) (transaction.Inspection, error) {
|
|
unit, err := o.units.Inspect(ctx, o.name)
|
|
if err != nil {
|
|
return transaction.Inspection{}, err
|
|
}
|
|
result := unitResult(unit)
|
|
switch unit.ActiveState {
|
|
case inactiveState, failedState:
|
|
return transaction.Inspection{Status: transaction.InspectionApplied, Result: result}, nil
|
|
case activeState:
|
|
return transaction.Inspection{Status: transaction.InspectionNotApplied, Result: result}, nil
|
|
default:
|
|
return transaction.Inspection{Status: transaction.InspectionUnknown, Result: result}, nil
|
|
}
|
|
}
|
|
|
|
// healthOperation “等待后端 Actuator 健康检查”的事务操作,实现 transaction.Operation。
|
|
// 它等待健康检查就绪,并通过互斥锁缓存已确认的报告以避免重复探测。
|
|
type healthOperation struct {
|
|
units systemd.Manager
|
|
checker actuatorChecker
|
|
unitName string
|
|
endpoint string
|
|
timeout time.Duration
|
|
|
|
mu sync.Mutex
|
|
confirmedReport healthcheck.ActuatorReport
|
|
confirmed bool
|
|
}
|
|
|
|
// Apply 阻塞等待健康检查就绪,成功后缓存已确认的报告。
|
|
// ctx 用于取消与超时。返回值是等待失败时的错误。
|
|
func (o *healthOperation) Apply(ctx context.Context) error {
|
|
report, err := o.checker.Wait(ctx, o.endpoint, o.timeout, o.running)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
o.mu.Lock()
|
|
o.confirmedReport = report
|
|
o.confirmed = true
|
|
o.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// Inspect 检查健康状态:若已有确认报告则直接返回,否则执行一次即时健康检查。
|
|
// ctx 用于取消。返回值是事务检查结果;检查器错误由 healthInspection 归一化后返回。
|
|
func (o *healthOperation) Inspect(ctx context.Context) (transaction.Inspection, error) {
|
|
o.mu.Lock()
|
|
if o.confirmed {
|
|
report := o.confirmedReport
|
|
o.mu.Unlock()
|
|
return healthInspection(report, true, nil)
|
|
}
|
|
o.mu.Unlock()
|
|
report, ready, err := o.checker.Check(ctx, o.endpoint, o.running)
|
|
return healthInspection(report, ready, err)
|
|
}
|
|
|
|
// running 判断后端单元当前是否处于活跃状态,作为健康检查的探针。
|
|
// ctx 用于取消。单元不存在时返回 false 且无错误;其他检查失败时返回错误。
|
|
func (o *healthOperation) running(ctx context.Context) (bool, error) {
|
|
unit, err := o.units.Inspect(ctx, o.unitName)
|
|
if errors.Is(err, systemd.ErrUnitNotFound) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return unit.ActiveState == activeState, nil
|
|
}
|
|
|
|
// healthInspection 把健康检查结果归一化为事务检查状态。
|
|
// report 健康报告;ready 是就绪标志;err 是检查错误。工作负载停止、出错或未就绪均为未应用,否则为已应用。
|
|
func healthInspection(report healthcheck.ActuatorReport, ready bool, err error) (transaction.Inspection, error) {
|
|
result := resultJSON(report)
|
|
if errors.Is(err, healthcheck.ErrWorkloadStopped) {
|
|
return transaction.Inspection{Status: transaction.InspectionNotApplied, Result: result}, nil
|
|
}
|
|
if err != nil || !ready {
|
|
return transaction.Inspection{Status: transaction.InspectionNotApplied, Result: result}, nil
|
|
}
|
|
return transaction.Inspection{Status: transaction.InspectionApplied, Result: result}, nil
|
|
}
|
|
|
|
// linkResult 把槽位路径与目标序列化为 JSON 结果。
|
|
// path 槽位软链接路径;target 是软链接指向的目标。返回值是序列化后的 JSON。
|
|
func linkResult(path, target string) json.RawMessage {
|
|
return resultJSON(struct {
|
|
Path string `json:"path"`
|
|
Target string `json:"target"`
|
|
}{path, target})
|
|
}
|
|
|
|
// unitResult 把 systemd 单元状态序列化为 JSON 结果。
|
|
// unit 待序列化的单元。返回值是序列化后的 JSON。
|
|
func unitResult(unit systemd.Unit) json.RawMessage {
|
|
return resultJSON(struct {
|
|
Name string `json:"name"`
|
|
LoadState string `json:"loadState"`
|
|
ActiveState string `json:"activeState"`
|
|
SubState string `json:"subState"`
|
|
}{unit.Name, unit.LoadState, unit.ActiveState, unit.SubState})
|
|
}
|
|
|
|
// resultJSON 把任意值序列化为 JSON 原始消息。
|
|
// value 待序列化的值。序列化失败时直接 panic(内部结果应始终可序列化)。返回值是序列化后的 JSON。
|
|
func resultJSON(value any) json.RawMessage {
|
|
payload, err := json.Marshal(value)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("marshal internal step result: %v", err))
|
|
}
|
|
return payload
|
|
}
|
|
|
|
// syncDirectory 打开目录并同步其元数据到磁盘,确保重命名或删除持久化。
|
|
// path 待刷新的目录路径。返回值是打开、同步或关闭失败时的错误。
|
|
func syncDirectory(path string) error {
|
|
directory, err := os.Open(path)
|
|
if err != nil {
|
|
return fmt.Errorf("open directory for flush: %w", err)
|
|
}
|
|
syncErr := directory.Sync()
|
|
closeErr := directory.Close()
|
|
if err := errors.Join(syncErr, closeErr); err != nil {
|
|
return fmt.Errorf("flush directory: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 以下编译期断言确保各操作类型都实现了 transaction.Operation 接口。
|
|
var _ transaction.Operation = (*installJarOperation)(nil)
|
|
var _ transaction.Operation = (*slotLinkOperation)(nil)
|
|
var _ transaction.Operation = (*unitStartOperation)(nil)
|
|
var _ transaction.Operation = (*unitStopOperation)(nil)
|
|
var _ transaction.Operation = (*healthOperation)(nil)
|