47eb23cf68
页面上的"可发起封存"原来只判 LIBRARY_AGENT_SOCKET 非空:正式栈的 API 根本没挂
那个 socket,而 agent 曾顶着正式单元名冻结候选实例——配置非空、页面可点、真发起
却封错库。
现在:agent 新增 health 动作(不占封存操作锁、不跑子进程,只回答身份/降级/忙),
worker 每 30 秒探测并写入迁移 074 的 library_executor_status;overview、快照列表与
POST /library/captures 共用同一份判定(过期、不可达、实例不符、降级、正忙都拒绝并
给原因)。封存结果由 agent 附上自报身份,worker 在停写入者之前再探测一次、执行后
用结果复核,不一致就不登记测试封存——心跳只能证明"刚才"。
期望身份用 LIBRARY_SOURCE_*(封存源),不是 LIBRARY_TARGET_*(同步目标),否则
正确的执行器会被判成不匹配。另加 GET /library/restore-availability:恢复能否执行由
双闸、该快照的恢复授权与实例维护状态决定,页面不再写死 disabled。
真跑(候选隔离栈,两处 socket 隔离不变):overview 返回 executor
{kgmaint-weknora/kgmaint-app};停 agent 后转不可达并 503 带原因;重启后恢复。
90 lines
3.3 KiB
Go
90 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/libraryagent"
|
|
)
|
|
|
|
// 封存执行的身份核对分两次:执行前一次、执行后一次。
|
|
//
|
|
// 为什么心跳不够:心跳说明"上一次探测时"那个 socket 后面是哪个实例,而从心跳到
|
|
// 真正停写入者之间,socket 可能被换成另一个 agent(换挂载、换单元、误启正式单元)。
|
|
// 执行前探测把窗口缩到最小;执行后复核 agent 自报的身份,则把"换过 socket"这件事
|
|
// 变成一次显式的作业失败,而不是悄悄封错了库还登记成测试封存。
|
|
|
|
// captureExecutorPrecheck 在动源写入者之前确认执行器就是期望的那个实例。
|
|
func captureExecutorPrecheck(ctx context.Context, client libraryagent.ClientConfig, expectedProject string, probeTimeout time.Duration) error {
|
|
if strings.TrimSpace(expectedProject) == "" {
|
|
return errors.New("未配置封存来源实例身份(LIBRARY_SOURCE_PROJECT),拒绝在无法核对执行器的情况下停写入者")
|
|
}
|
|
if probeTimeout <= 0 {
|
|
probeTimeout = 3 * time.Second
|
|
}
|
|
bounded, cancel := context.WithTimeout(ctx, probeTimeout)
|
|
defer cancel()
|
|
result, err := libraryagent.Health(bounded, client)
|
|
if err != nil {
|
|
return fmt.Errorf("封存执行器不可达,拒绝停写入者:%w", err)
|
|
}
|
|
identity, err := agentIdentityOf(result.Payload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if identity.Project != expectedProject {
|
|
return fmt.Errorf("封存执行器指向的实例不匹配(agent=%q,期望=%q),拒绝停写入者",
|
|
identity.Project, expectedProject)
|
|
}
|
|
if identity.Degraded != "" {
|
|
return fmt.Errorf("封存执行器处于降级状态(%s),拒绝停写入者", identity.Degraded)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// captureExecutorPostcheck 用封存结果里 agent 自报的身份复核:不一致就当作
|
|
// "封存可能来自别的实例",调用方不得把它登记成测试封存。
|
|
func captureExecutorPostcheck(payload json.RawMessage, expectedProject string) error {
|
|
identity, err := agentIdentityOf(payload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if identity.Project == "" {
|
|
return errors.New("封存结果里没有执行器身份,无法证明这次封存来自期望实例")
|
|
}
|
|
if identity.Project != expectedProject {
|
|
return fmt.Errorf("封存结果来自另一个实例(agent=%q,期望=%q),该快照不能当作本实例的测试封存",
|
|
identity.Project, expectedProject)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// agentIdentity 是 agent 在 health/封存结果里自报的身份(无凭据)。
|
|
type agentIdentity struct {
|
|
Project string `json:"project"`
|
|
SourceApp string `json:"source_app"`
|
|
Degraded string `json:"degraded"`
|
|
}
|
|
|
|
func agentIdentityOf(payload json.RawMessage) (agentIdentity, error) {
|
|
if len(payload) == 0 {
|
|
return agentIdentity{}, errors.New("执行器应答没有身份信息")
|
|
}
|
|
var document struct {
|
|
Identity agentIdentity `json:"agent_identity"`
|
|
agentIdentity
|
|
}
|
|
if err := json.Unmarshal(payload, &document); err != nil {
|
|
return agentIdentity{}, errors.New("执行器身份无法解析")
|
|
}
|
|
// health 把身份摊平在顶层;封存结果把它放在 agent_identity 下。
|
|
if document.Identity.Project != "" {
|
|
return document.Identity, nil
|
|
}
|
|
return document.agentIdentity, nil
|
|
}
|