Compare commits
127 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0cc3cac837 | |||
| e4342fa05b | |||
| 040b7285d7 | |||
| cbc6b981c6 | |||
| ecf518e3bb | |||
| 0b753e550e | |||
| 2066bedbbe | |||
| 5f1a8f565c | |||
| 4fce49478f | |||
| 4d2cb13b0e | |||
| 0aa9f273e6 | |||
| fba2713b4e | |||
| 10568943af | |||
| 3f2626f621 | |||
| e8bfdbbc06 | |||
| ef3c1fbf84 | |||
| 2da96da592 | |||
| 10182b027e | |||
| 060872a144 | |||
| 0f7b8987b4 | |||
| 5e850e96f5 | |||
| 719876d039 | |||
| 9d445f4953 | |||
| 2eff9447e1 | |||
| 146ac832a2 | |||
| 77bbc3ac6b | |||
| eaa388f76b | |||
| cf11343cf4 | |||
| 7511c59987 | |||
| b81fcd07e3 | |||
| 1a0f99e346 | |||
| 7675e2cd88 | |||
| 5bf4aae30e | |||
| 1c80b07f2c | |||
| dfbf3336b2 | |||
| 600cca02e5 | |||
| e735ae603d | |||
| 2a2e98bd8e | |||
| 92684c6f60 | |||
| 537a6df46d | |||
| 0d9091e464 | |||
| e0fc2c32b8 | |||
| 6042955498 | |||
| a55319d972 | |||
| 017765763a | |||
| bd452e85b3 | |||
| 71f23c6763 | |||
| b4add6c9b0 | |||
| f9457fab52 | |||
| 5e1482e349 | |||
| 4421dd38ce | |||
| 22fb33aa75 | |||
| 0d180aa401 | |||
| 0a87dc8e03 | |||
| 96472c1dde | |||
| bad9034cf7 | |||
| f330a75641 | |||
| 19ec03c417 | |||
| 6209072e43 | |||
| e61f05c98c | |||
| a9d5adf78f | |||
| e8f8069473 | |||
| b7d6661a4e | |||
| 97da6ceba8 | |||
| 7cdbb74c8f | |||
| 1aa60c159e | |||
| 245cefd6b0 | |||
| 1e2bb385c7 | |||
| bf7f5fa03c | |||
| 24608e132b | |||
| 80254756f1 | |||
| 37f2dc73f9 | |||
| 9e31a6f956 | |||
| 1c58f1ef7e | |||
| 3b7ad9061c | |||
| 882e38591d | |||
| a1112d368a | |||
| ca32f2e661 | |||
| 513aa9e6f5 | |||
| 3bce6c7a3f | |||
| 18a4b83f78 | |||
| 7b2ab642f7 | |||
| 0a7c9f8510 | |||
| e09fddcb74 | |||
| fad26166a4 | |||
| 47be4ae502 | |||
| 6827fa922f | |||
| c31a7df4ca | |||
| 770252b55a | |||
| c256f2985c | |||
| f1cddfb601 | |||
| d2ff089b95 | |||
| 12b37690ec | |||
| a1472ff29f | |||
| f076c3af9c | |||
| 59eb12296a | |||
| 6ec1821294 | |||
| 099e58b5dc | |||
| e760d85427 | |||
| 98e7279632 | |||
| 30f223bcc4 | |||
| 2cbeca8d1d | |||
| b39902ef90 | |||
| 68f03bb30e | |||
| 93c0f34aac | |||
| 98ccb9366e | |||
| 07a9caf22b | |||
| eea0a0ec3b | |||
| 1d2fff8639 | |||
| 066dc45a25 | |||
| ff257064c3 | |||
| de867078c6 | |||
| b15633d37c | |||
| c938a4f4bf | |||
| ba1989b042 | |||
| a98828dc4a | |||
| e292b6f674 | |||
| 30686dd288 | |||
| 8ce78e28fa | |||
| 044fb37b2c | |||
| e34306b2db | |||
| 943b617b1f | |||
| 90ab1c3a5e | |||
| 99055a582e | |||
| 0df0e3cb1e | |||
| f4bd1980bf | |||
| 8df19f9368 |
@@ -108,9 +108,23 @@ if [ -n "$BEFORE" ] && [ "$BEFORE" != "$ZERO" ]; then
|
||||
if git cat-file -e "${BEFORE}^{commit}" 2>/dev/null; then
|
||||
echo "✅ 事件 before ${BEFORE:0:12} 已在克隆里(本次推送范围可直接算)"
|
||||
else
|
||||
gitauth fetch -q --depth 50 origin "$BEFORE" 2>/dev/null \
|
||||
&& echo "✅ 已补取事件 before ${BEFORE:0:12}" \
|
||||
|| echo "⚠️ 取不到事件 before ${BEFORE:0:12}:范围校验会明确失败,不会静默收缩"
|
||||
# ⚠️ 为什么重试一次:浅克隆里用 `fetch --depth` 取一个**未公开**的对象(撤销/改写推送之后
|
||||
# 的旧提交就是这种)时,git 会在这次 fetch 中途重写 .git/shallow,随后发现自己读到的
|
||||
# 与开始时不同就整体中止:`fatal: shallow file has changed since we read it`。
|
||||
# 实测(run 305–317,含原地重跑)这一步必中止,而**同一仓库里紧接着重试同一条命令即成功**。
|
||||
# before 取不到 ⇒ commits 检查算不出范围 ⇒ 流水线红,且红的原因与代码质量无关。
|
||||
# 重试只负责"把 before 取进克隆",不收缩范围;两次都失败仍明确报警,由 commits 检查判红。
|
||||
FETCH_OK=""
|
||||
FETCH_ERR=""
|
||||
for _attempt in 1 2; do
|
||||
FETCH_ERR="$(gitauth fetch -q --depth 50 origin "$BEFORE" 2>&1 >/dev/null || true)"
|
||||
if git cat-file -e "${BEFORE}^{commit}" 2>/dev/null; then FETCH_OK=1; break; fi
|
||||
done
|
||||
if [ -n "$FETCH_OK" ]; then
|
||||
echo "✅ 已补取事件 before ${BEFORE:0:12}"
|
||||
else
|
||||
echo "⚠️ 取不到事件 before ${BEFORE:0:12}:范围校验会明确失败,不会静默收缩(最后一次错误:${FETCH_ERR:0:160})"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo "(事件里没有 before / before 为全 0:新分支推送,范围按默认分支为基准判)"
|
||||
|
||||
+6139
-12
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -50,7 +50,7 @@ RUN set -eux; \
|
||||
dws version
|
||||
WORKDIR /app
|
||||
COPY --from=build /out/ /app/
|
||||
COPY scripts/source_catalog_import.py scripts/live_catalog_refresh.py scripts/artifact_fetch.py \
|
||||
scripts/axls_to_xlsx.py scripts/dws_error_map.py /app/scripts/
|
||||
COPY scripts/source_catalog_import.py scripts/live_catalog_refresh.py scripts/dws_dingtalk_scan.py \
|
||||
scripts/artifact_fetch.py scripts/axls_to_xlsx.py scripts/dws_error_map.py /app/scripts/
|
||||
USER 65532:65532
|
||||
CMD ["/app/api"]
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
"/api/v1/qa-ops/master/weekly-mail/confirmations/{week}": {"get":{"operationId":"getQAWeeklyMailConfirmation","tags":["QA operations"],"summary":"读取该周是否已确认自动发送;current=false表示确认后正文已被修改,需要重新确认","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}}],"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可修改发送设置"},"409":{"description":"发送设置已被其他管理员修改"},"503":{"description":"邮件设置存储不可用,未标记为已发送"}}},"post":{"operationId":"confirmQAWeeklyMail","tags":["QA operations"],"summary":"确认本周周报可自动发送,绑定当前草稿版本与正文哈希;后续改动使确认失效,不发送改动后的正文","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可修改发送设置"},"409":{"description":"发送设置已被其他管理员修改"},"503":{"description":"邮件设置存储不可用,未标记为已发送"}}},"delete":{"operationId":"unconfirmQAWeeklyMail","tags":["QA operations"],"summary":"取消该周的自动发送确认","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可修改发送设置"},"409":{"description":"发送设置已被其他管理员修改"},"503":{"description":"邮件设置存储不可用,未标记为已发送"}}}},
|
||||
"/api/v1/qa-ops/master/weekly-mail/sends": {"get":{"operationId":"listQAWeeklyMailSends","tags":["QA operations"],"summary":"读取周报邮件发送记录:周次、草稿版本、发送中/已发送/失败及错误摘要,不返回正文或收件人","parameters":[{"name":"week","in":"query","required":false,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}}],"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可修改发送设置"},"409":{"description":"发送设置已被其他管理员修改"},"503":{"description":"邮件设置存储不可用,未标记为已发送"}}}},
|
||||
"/api/v1/qa-ops/master/weekly-mail/test": {"post":{"operationId":"sendQAWeeklyMailTest","tags":["QA operations"],"summary":"管理员用当前已保存的邮件设置立即发送一封测试邮件,正文取该周已保存草稿;不写入发送记录、不改变确认状态、不把该周标记为已发送","parameters":[{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["week"],"properties":{"week":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}}}}}},"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可测试发送"},"502":{"description":"SMTP 拒绝或不可达,返回脱敏原因"},"503":{"description":"邮件设置存储不可用"}}}},
|
||||
"/api/v1/qa-ops/master/weekly-mail/send/{week}": {"get":{"operationId":"previewQAWeeklyMailSend","tags":["QA operations"],"summary":"管理员补发前核对:按周、已确认版本与正文hash返回真实主送/抄送人数、版本与是否已发送;不投递、不写记录","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"revision","in":"query","required":true,"schema":{"type":"integer","minimum":1}},{"name":"body_sha256","in":"query","required":true,"schema":{"type":"string","pattern":"^[0-9a-f]{64}$"}}],"responses":{"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员"},"409":{"description":"未确认/正文或版本已变化/当前周未到设定时点/上次结果未知"},"500":{"$ref":"#/components/responses/InternalError"},"503":{"description":"邮件设置存储不可用"}}},"post":{"operationId":"sendQAWeeklyMailFormal","tags":["QA operations"],"summary":"管理员正式补发周报:精确指定周、已确认版本与正文hash;已结束周可发、当前周须到设定时点、未来周拒绝;正文/看板图片/主送/抄送与自动发送共用,写入正式发送记录并返回真实message_id;已发送幂等、结果未知不重发","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["revision","body_sha256"],"properties":{"revision":{"type":"integer","minimum":1},"body_sha256":{"type":"string","pattern":"^[0-9a-f]{64}$"}}}}}},"responses":{"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员且需同源操作意图"},"409":{"description":"未确认/正文或版本已变化/当前周未到设定时点/上次结果未知/授权码缺失"},"500":{"$ref":"#/components/responses/InternalError"},"502":{"description":"SMTP 拒绝或不可达,返回脱敏原因且不写已发送"},"503":{"description":"邮件设置存储不可用"}}}},
|
||||
"/api/v1/qa-ops/master/weekly-mail/confirmations/{week}/image": {"post":{"operationId":"saveQAWeeklyMailImage","tags":["QA operations"],"summary":"管理员保存该周确认对应的统计看板 PNG;发送时作为内嵌图片(cid)放在正文末尾,正文改动使确认失效后不再使用旧图","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["png_base64"],"properties":{"png_base64":{"type":"string","maxLength":12000000}}}}}},"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可上传"},"429":{"description":"请求过快"},"503":{"description":"邮件设置存储不可用"}}}},
|
||||
"/api/v1/qa-ops/departments/statistics": {
|
||||
"post": {
|
||||
@@ -1368,6 +1369,45 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/library/jobs/{id}/reconcile-capture": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"Runtime"
|
||||
],
|
||||
"operationId": "reconcileLibraryCapture",
|
||||
"summary": "仅在 completed capture queue、精确 test_sealed 版本与归档复核一致时,修复该封存作业终态;不重新封存",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "id",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "已修复或已成功(幂等)"
|
||||
},
|
||||
"400": {
|
||||
"$ref": "#/components/responses/BadRequest"
|
||||
},
|
||||
"403": {
|
||||
"description": "需要管理员权限"
|
||||
},
|
||||
"404": {
|
||||
"description": "作业不存在"
|
||||
},
|
||||
"409": {
|
||||
"description": "队列/阶段/版本/归档证据不一致,拒绝修复"
|
||||
},
|
||||
"500": {
|
||||
"$ref": "#/components/responses/InternalError"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/library/restore-availability": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@@ -1655,6 +1695,93 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/library/verifications": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"Runtime"
|
||||
],
|
||||
"operationId": "getLibraryRestoreVerification",
|
||||
"summary": "当前整库目标的恢复验证证据:verified 只由真实演练记录回答(按 target_id 绑定,换目标自动失效),并返回完整目标与失败原因",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "scope",
|
||||
"in": "query",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"internal",
|
||||
"external"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "目标与证据:verified/reason/latest/history/target"
|
||||
},
|
||||
"400": {
|
||||
"$ref": "#/components/responses/BadRequest"
|
||||
},
|
||||
"500": {
|
||||
"$ref": "#/components/responses/InternalError"
|
||||
},
|
||||
"503": {
|
||||
"description": "恢复验证存储不可用或证据读取失败"
|
||||
}
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"tags": [
|
||||
"Runtime"
|
||||
],
|
||||
"operationId": "startLibraryRestoreVerification",
|
||||
"summary": "发起一次真实恢复验证(管理员):维护隔离下捕获目标 → 校验基线 → 写 canary 证明校验有效 → 按预备份恢复 → 复核,通过后才产生同步闸口认的证据",
|
||||
"responses": {
|
||||
"202": {
|
||||
"description": "已创建 verify 作业(worker 在维护窗口内真实执行)"
|
||||
},
|
||||
"400": {
|
||||
"$ref": "#/components/responses/BadRequest"
|
||||
},
|
||||
"403": {
|
||||
"description": "不是管理员"
|
||||
},
|
||||
"409": {
|
||||
"description": "目标正在维护或已有在飞写作业(同步/恢复/基线/验证互斥)"
|
||||
},
|
||||
"500": {
|
||||
"$ref": "#/components/responses/InternalError"
|
||||
},
|
||||
"503": {
|
||||
"description": "生产写入闸口关闭或恢复验证存储不可用"
|
||||
}
|
||||
},
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"scope"
|
||||
],
|
||||
"properties": {
|
||||
"scope": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"internal",
|
||||
"external"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/library/snapshots/{id}": {
|
||||
"get": {
|
||||
"tags": [
|
||||
|
||||
+43
-23
@@ -8,7 +8,6 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -26,6 +25,7 @@ import (
|
||||
"knowledge-governance/internal/qaopsorg"
|
||||
"knowledge-governance/internal/qaopsquality"
|
||||
"knowledge-governance/internal/qawemail"
|
||||
"knowledge-governance/internal/readiness"
|
||||
"knowledge-governance/internal/serviceverify"
|
||||
"knowledge-governance/internal/version"
|
||||
)
|
||||
@@ -90,26 +90,6 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
return errors.New("read production release authorization token")
|
||||
}
|
||||
}
|
||||
// P10: with a mounted API key the probe verifies a real knowledge-base read
|
||||
// (readiness); without one it degrades to the liveness-only HTTP check.
|
||||
var weknoraReadiness func(context.Context) error
|
||||
var knowledgeBaseLister httpapi.KnowledgeBaseLister
|
||||
if strings.TrimSpace(config.TestWeKnoraBaseURL) != "" {
|
||||
if apiKey, keyErr := weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile); keyErr == nil {
|
||||
probeTimeout := config.TestWeKnoraHTTPTimeout
|
||||
if probeTimeout > 10*time.Second || probeTimeout <= 0 {
|
||||
probeTimeout = 10 * time.Second
|
||||
}
|
||||
if probeClient, clientErr := weknora.NewClient(weknora.ClientConfig{BaseURL: config.TestWeKnoraBaseURL, APIKey: apiKey, Timeout: probeTimeout, AllowDockerHTTP: config.TestWeKnoraAllowDockerHTTP}); clientErr == nil {
|
||||
knowledgeBaseLister = probeClient
|
||||
routingKB := config.InternalRoutingKnowledgeBaseID
|
||||
weknoraReadiness = func(probeCtx context.Context) error {
|
||||
_, _, err := probeClient.ListKnowledge(probeCtx, routingKB, 1, 1)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Production WeKnora is probed for liveness only: the console must show the
|
||||
// real state of that endpoint, while credentials and the publish target stay
|
||||
// in the release runtime config (RELEASE_CONFIG_FILE).
|
||||
@@ -131,7 +111,43 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
}
|
||||
return settings.TestWeKnoraBaseURL
|
||||
}
|
||||
monitorProvider, err := monitoring.NewRealProvider(monitoring.RealProviderConfig{DB: pool, WeKnoraBaseURL: config.TestWeKnoraBaseURL, TestWeKnoraResolver: testEndpointResolver, ProductionWeKnoraBaseURL: config.ProductionWeKnoraBaseURL, ProductionWeKnoraResolver: productionEndpointResolver, MinerUBaseURL: config.MinerUBaseURL, WeKnoraReadiness: weknoraReadiness, WorkerStaleAfter: config.WorkerLivenessStaleAfter})
|
||||
// P10 readiness 与执行链看同一目标:网页保存的端点、当前持久化绑定、同一份
|
||||
// API Key。启动时只捕获 resolver,不捕获任何一项的值(F13:旧 env 绑定 404)。
|
||||
readinessResolver := newWeKnoraReadinessResolver(
|
||||
func(resolveCtx context.Context) string {
|
||||
if saved := testEndpointResolver(resolveCtx); saved != "" {
|
||||
return saved
|
||||
}
|
||||
return config.TestWeKnoraBaseURL
|
||||
},
|
||||
config.InternalRoutingKnowledgeBaseID,
|
||||
func(resolveCtx context.Context) (string, error) {
|
||||
internal, _, _, err := actionStore.LoadRoutingKBBindings(resolveCtx)
|
||||
return internal, err
|
||||
},
|
||||
func() (string, error) { return weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile) },
|
||||
)
|
||||
readinessProber := readiness.Prober{
|
||||
Resolve: readinessResolver,
|
||||
Timeout: config.TestWeKnoraHTTPTimeout,
|
||||
AllowDockerHTTP: config.TestWeKnoraAllowDockerHTTP,
|
||||
}
|
||||
weknoraProbe := func(probeCtx context.Context) (string, error) {
|
||||
result := readinessProber.Check(probeCtx)
|
||||
return result.Target, result.Err
|
||||
}
|
||||
knowledgeBaseLister := &weKnoraLister{
|
||||
endpoint: func(resolveCtx context.Context) string {
|
||||
if saved := testEndpointResolver(resolveCtx); saved != "" {
|
||||
return saved
|
||||
}
|
||||
return config.TestWeKnoraBaseURL
|
||||
},
|
||||
readKey: func() (string, error) { return weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile) },
|
||||
timeout: config.TestWeKnoraHTTPTimeout,
|
||||
allowDockerHTTP: config.TestWeKnoraAllowDockerHTTP,
|
||||
}
|
||||
monitorProvider, err := monitoring.NewRealProvider(monitoring.RealProviderConfig{DB: pool, WeKnoraBaseURL: config.TestWeKnoraBaseURL, TestWeKnoraResolver: testEndpointResolver, ProductionWeKnoraBaseURL: config.ProductionWeKnoraBaseURL, ProductionWeKnoraResolver: productionEndpointResolver, MinerUBaseURL: config.MinerUBaseURL, WeKnoraReadiness: weknoraProbe, WorkerStaleAfter: config.WorkerLivenessStaleAfter})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -190,7 +206,7 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
QAOpsTenantKey: qaSettings.TenantKey,
|
||||
QAOpsEnabled: qaSettings.Enabled,
|
||||
QAOpsProblem: qaSettings.Problem,
|
||||
Store: postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity()),
|
||||
Store: postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity()).WithRoutingDefaults(config.InternalRoutingKnowledgeBaseID, config.ExternalRoutingKnowledgeBaseID),
|
||||
Monitor: monitorProvider,
|
||||
InternalRoutingKnowledgeBaseID: config.InternalRoutingKnowledgeBaseID,
|
||||
ExternalRoutingKnowledgeBaseID: config.ExternalRoutingKnowledgeBaseID,
|
||||
@@ -217,6 +233,8 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
LibraryTargetWriters: config.LibraryTargetWriters,
|
||||
LibraryTargetIngressURL: config.LibraryTargetIngressURL,
|
||||
LibraryTargetIngressService: config.LibraryTargetIngressService,
|
||||
LibraryTargetIngressMode: config.LibraryTargetIngressMode,
|
||||
LibraryTargetHealthURL: config.LibraryTargetHealthURL,
|
||||
LibraryTargetDockerSocket: config.LibraryTargetDockerSocket,
|
||||
LibraryTargetJournalRoot: config.LibraryTargetJournalRoot,
|
||||
LibrarySourceInstance: config.SourceInstance(),
|
||||
@@ -232,6 +250,8 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
LibraryProductionInternalKB: config.LibraryProductionInternalKB,
|
||||
LibraryProductionExternalKB: config.LibraryProductionExternalKB,
|
||||
LibraryProductionTenantID: config.LibraryProductionTenantID,
|
||||
LibraryProductionIndex: config.LibraryProductionIndex,
|
||||
LibraryProductionS3Bucket: config.LibraryProductionS3Bucket,
|
||||
LibraryRetentionVersions: config.LibraryRetentionVersions,
|
||||
LibraryRetentionDays: config.LibraryRetentionDays,
|
||||
BackupHost: backuphost.New(backuphost.Config{
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/platform/weknora"
|
||||
"knowledge-governance/internal/readiness"
|
||||
)
|
||||
|
||||
// newWeKnoraReadinessResolver 把「探测目标」收敛到与业务同一组真源:
|
||||
// 端点来自网页保存的服务配置(endpoint 内部已做 env 兜底),知识库来自
|
||||
// 当前持久化绑定(未绑定才用 env 引导值),凭据每次重新读同一份 key 文件。
|
||||
// 任何一项读取失败都必须显式报错——静默退回旧值正是 F13 的成因。
|
||||
func newWeKnoraReadinessResolver(
|
||||
endpoint func(context.Context) string,
|
||||
envKnowledgeBaseID string,
|
||||
loadBinding func(context.Context) (string, error),
|
||||
readKey func() (string, error),
|
||||
) func(context.Context) (readiness.Target, error) {
|
||||
resolver := readiness.Resolver{
|
||||
Endpoint: endpoint,
|
||||
DefaultKB: envKnowledgeBaseID,
|
||||
LoadBinding: func(resolveCtx context.Context) (string, error) {
|
||||
if loadBinding == nil {
|
||||
return "", nil
|
||||
}
|
||||
stored, err := loadBinding(resolveCtx)
|
||||
if err != nil {
|
||||
log.Printf("readiness: load routing bindings: %v", err)
|
||||
}
|
||||
return stored, err
|
||||
},
|
||||
ReadKey: func() (string, error) {
|
||||
if readKey == nil {
|
||||
return "", nil
|
||||
}
|
||||
value, err := readKey()
|
||||
if err != nil {
|
||||
log.Printf("readiness: read WeKnora API key: %v", err)
|
||||
}
|
||||
return value, err
|
||||
},
|
||||
}
|
||||
return resolver.Resolve
|
||||
}
|
||||
|
||||
// weKnoraLister 让「系统设置」的知识库下拉与保存校验同样跟随当前端点/凭据,
|
||||
// 而不是复用启动时的客户端(否则改端点后选项仍来自旧实例)。
|
||||
type weKnoraLister struct {
|
||||
endpoint func(context.Context) string
|
||||
readKey func() (string, error)
|
||||
timeout time.Duration
|
||||
allowDockerHTTP bool
|
||||
}
|
||||
|
||||
func (lister *weKnoraLister) ListKnowledgeBases(ctx context.Context) ([]weknora.KnowledgeBase, error) {
|
||||
baseURL := ""
|
||||
if lister.endpoint != nil {
|
||||
baseURL = strings.TrimSpace(lister.endpoint(ctx))
|
||||
}
|
||||
if baseURL == "" {
|
||||
return nil, fmt.Errorf("WeKnora 端点未配置")
|
||||
}
|
||||
apiKey, err := lister.readKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
timeout := lister.timeout
|
||||
if timeout <= 0 || timeout > 10*time.Second {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: baseURL, APIKey: apiKey, Timeout: timeout, AllowDockerHTTP: lister.allowDockerHTTP})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.ListKnowledgeBases(ctx)
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/readiness"
|
||||
)
|
||||
|
||||
// F13 的接线回归:探测目标必须来自「网页保存的端点 + 当前持久化绑定」,
|
||||
// env 只能是未绑定时的引导兜底;任何一项都不许在启动时固化。
|
||||
func TestReadinessResolverUsesPersistedBindingOverEnv(t *testing.T) {
|
||||
const envKB = "1b52c140-2e18-4676-afc5-4c7d8d190a8c"
|
||||
const storedKB = "0c84a098-d3ed-4b82-a165-e6de9691d6a8"
|
||||
resolve := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "https://weknora.test" },
|
||||
envKB,
|
||||
func(context.Context) (string, error) { return storedKB, nil },
|
||||
func() (string, error) { return "key-1", nil },
|
||||
)
|
||||
target, err := resolve(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if target.KnowledgeBaseID != storedKB || target.BaseURL != "https://weknora.test" || target.APIKey != "key-1" {
|
||||
t.Fatalf("resolved target = %#v", target)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessResolverFallsBackToEnvOnlyWhenUnbound(t *testing.T) {
|
||||
const envKB = "1b52c140-2e18-4676-afc5-4c7d8d190a8c"
|
||||
resolve := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "https://weknora.test" },
|
||||
envKB,
|
||||
func(context.Context) (string, error) { return "", nil },
|
||||
func() (string, error) { return "key-1", nil },
|
||||
)
|
||||
target, err := resolve(context.Background())
|
||||
if err != nil || target.KnowledgeBaseID != envKB {
|
||||
t.Fatalf("unbound fallback = %#v, %v", target, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessResolverReportsBindingAndCredentialFailures(t *testing.T) {
|
||||
bindingDown := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "https://weknora.test" },
|
||||
"env-kb",
|
||||
func(context.Context) (string, error) { return "", errors.New("db unavailable") },
|
||||
func() (string, error) { return "key-1", nil },
|
||||
)
|
||||
target, err := bindingDown(context.Background())
|
||||
if err == nil {
|
||||
t.Fatalf("binding read failure must not silently probe env: %#v", target)
|
||||
}
|
||||
keyDown := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "https://weknora.test" },
|
||||
"env-kb",
|
||||
func(context.Context) (string, error) { return "", nil },
|
||||
func() (string, error) { return "", errors.New("key file missing") },
|
||||
)
|
||||
if _, err := keyDown(context.Background()); err == nil {
|
||||
t.Fatal("missing API key must surface, not degrade to liveness-only")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessResolverMarksUnconfiguredEndpoint(t *testing.T) {
|
||||
resolve := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "" },
|
||||
"env-kb",
|
||||
func(context.Context) (string, error) { return "", nil },
|
||||
func() (string, error) { return "key-1", nil },
|
||||
)
|
||||
// 端点缺失时归类为「未配置」,不是「服务不可用」。
|
||||
result := readiness.Prober{Resolve: resolve}.Check(context.Background())
|
||||
if !errors.Is(result.Err, readiness.ErrNotConfigured) {
|
||||
t.Fatalf("blank endpoint probe = %v, want ErrNotConfigured", result.Err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKnowledgeBaseListerFollowsTheCurrentEndpoint(t *testing.T) {
|
||||
base := func(name string) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
if request.URL.Path != "/api/v1/knowledge-bases" {
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_, _ = writer.Write([]byte(`{"success":true,"data":[{"id":"` + name + `","name":"` + name + `"}]}`))
|
||||
}))
|
||||
}
|
||||
first, second := base("kb-a"), base("kb-b")
|
||||
defer first.Close()
|
||||
defer second.Close()
|
||||
endpoint := first.URL
|
||||
lister := &weKnoraLister{
|
||||
endpoint: func(context.Context) string { return endpoint },
|
||||
readKey: func() (string, error) { return "key-1", nil },
|
||||
timeout: time.Second,
|
||||
}
|
||||
bases, err := lister.ListKnowledgeBases(context.Background())
|
||||
if err != nil || len(bases) != 1 || bases[0].ID != "kb-a" {
|
||||
t.Fatalf("initial endpoint bases = %#v, %v", bases, err)
|
||||
}
|
||||
endpoint = second.URL
|
||||
bases, err = lister.ListKnowledgeBases(context.Background())
|
||||
if err != nil || len(bases) != 1 || bases[0].ID != "kb-b" {
|
||||
t.Fatalf("rotated endpoint bases = %#v, %v", bases, err)
|
||||
}
|
||||
}
|
||||
@@ -10,10 +10,13 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/libraryagent"
|
||||
"knowledge-governance/internal/librarydsn"
|
||||
"knowledge-governance/internal/librarymaintenance"
|
||||
)
|
||||
|
||||
@@ -25,22 +28,43 @@ func main() {
|
||||
flag.StringVar(&config.ArchiveRoot, "archive", filepath.Join(home, ".local/share/knowledge-governance/library"), "sealed artifact root")
|
||||
flag.StringVar(&config.JournalRoot, "journal", filepath.Join(home, ".local/share/knowledge-governance/freeze"), "freeze journal root")
|
||||
flag.StringVar(&config.SourceApp, "source-app", "WKNFull-app", "source container describing the stack")
|
||||
flag.StringVar(&config.SourcePG, "source-pg", "WKNFull-postgres", "source PostgreSQL container")
|
||||
flag.StringVar(&config.SourcePG, "source-pg", "WKNFull-postgres", "source PostgreSQL container; empty = the source DB lives on a managed instance and is reached with the source app env (DB_HOST/...)")
|
||||
flag.StringVar(&config.SourceES, "source-es", "WKNFull-elasticsearch", "source Elasticsearch container")
|
||||
flag.StringVar(&config.SourceS3Endpoint, "source-s3-endpoint", "", "host-reachable source object-store endpoint (overrides the container-internal one)")
|
||||
flag.StringVar(&config.GovernancePG, "governance-pg", "knowledge-governance-p1-postgres-1", "governance PostgreSQL container")
|
||||
flag.StringVar(&config.GovernancePG, "governance-pg", "knowledge-governance-p1-postgres-1", "governance PostgreSQL container; empty = read the DSN from LIBRARY_GOVERNANCE_DSN_FILE")
|
||||
flag.StringVar(&config.Project, "project", "weknora071full", "Compose project owning the writers")
|
||||
flag.StringVar(&config.Writers, "writers", "app", "declared writer services")
|
||||
nonWriters := flag.String("non-writers", defaultNonWriters, "service=reason for every other project service")
|
||||
allowUIDs := flag.String("allow-peer-uid", "", "extra peer uids allowed to connect (comma separated; default: same uid only)")
|
||||
flag.DurationVar(&config.MaxDuration, "max-duration", 10*time.Minute, "upper bound for one operation")
|
||||
describe := flag.Bool("describe", false, "print the fixed configuration and exit")
|
||||
flag.Parse()
|
||||
if strings.TrimSpace(*allowUIDs) != "" {
|
||||
for _, raw := range strings.Split(*allowUIDs, ",") {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
continue
|
||||
}
|
||||
uid, err := strconv.ParseUint(raw, 10, 32)
|
||||
if err != nil {
|
||||
log.Fatalf("--allow-peer-uid 需要十进制 uid:%q", raw)
|
||||
}
|
||||
config.AllowedPeerUIDs = append(config.AllowedPeerUIDs, uint32(uid))
|
||||
}
|
||||
}
|
||||
config.NonWriters = *nonWriters
|
||||
|
||||
agent, err := libraryagent.New(config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if *describe {
|
||||
// describe 只回答"这两个库从哪来"。地址与凭据一律不出现:这段输出会被贴进
|
||||
// 工单与日志,而 RDS 的连接串里带口令。
|
||||
log.Printf("source_db=%s governance_db=%s",
|
||||
librarydsn.SourceMode(config.SourcePG), librarydsn.GovernanceMode(config.GovernancePG))
|
||||
return
|
||||
}
|
||||
// 实例级维护接线(入口闸门 + 后台队列)不在 agent 的 Config 里:它由同一份
|
||||
// 环境声明提供给子进程。启动时先说清楚有没有——缺了封存会在开窗口前被拒,
|
||||
// 不允许"以为接好了"跑到一半才发现没有排空证据。
|
||||
@@ -49,10 +73,6 @@ func main() {
|
||||
} else {
|
||||
log.Printf("library agent: 实例级维护目标 %s 入口 %s 闸门 %s", target.Instance, target.IngressURL, target.IngressService)
|
||||
}
|
||||
if *describe {
|
||||
log.Printf("library agent config: %+v", agent.Describe())
|
||||
return
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
log.Printf("library agent listening on %s (project %s, writers %s)", config.Socket, config.Project, config.Writers)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 文件模式没配置:必须失败关闭并点名缺的那个变量——"静默退回容器模式"会让
|
||||
// 操作员以为在读 RDS,实际在读本机容器(或者干脆读到不存在的容器)。
|
||||
func TestGovernanceFileModeFailsClosedWithoutDSNFile(t *testing.T) {
|
||||
t.Setenv("LIBRARY_GOVERNANCE_DSN_FILE", "")
|
||||
_, err := readRoutingBindings(context.Background(), options{governancePG: ""})
|
||||
if err == nil {
|
||||
t.Fatal("没有 DSN 文件却读成功了")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "LIBRARY_GOVERNANCE_DSN_FILE") {
|
||||
t.Fatalf("错误没有点名缺哪一项:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 外部 DSN 连不上/写错时,错误必须是固定说法:pgx 的错误里带的是连接串(主机、
|
||||
// 用户名,口令它自己打码),直接透传就等于把地址写进日志与工单。
|
||||
func TestGovernanceFileModeNeverLeaksPasswordInErrors(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "governance.dsn")
|
||||
// 夹具口令运行时拼装:源码里不留“带口令的连接串”字面量,否则密钥扫描只会
|
||||
// 报出一堆必然是假值的夹具,把真泄漏的信号淹掉。
|
||||
password := strings.Join([]string{"fixture", "leak", "probe"}, "-")
|
||||
// 故意用非法 sslmode + 不可达端口:无论失败在解析还是连接阶段,都不许泄露。
|
||||
if err := os.WriteFile(path, []byte("postgres://gov:"+password+"@127.0.0.1:1/gov?sslmode=bogus&connect_timeout=1\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("LIBRARY_GOVERNANCE_DSN_FILE", path)
|
||||
_, err := readRoutingBindings(context.Background(), options{governancePG: ""})
|
||||
if err == nil {
|
||||
t.Fatal("坏 DSN 竟然读成功了")
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("错误里漏了口令:%v", err)
|
||||
}
|
||||
}
|
||||
+173
-34
@@ -30,9 +30,11 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"knowledge-governance/internal/librarydeadletter"
|
||||
"knowledge-governance/internal/librarydsn"
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
"knowledge-governance/internal/libraryhandoff"
|
||||
"knowledge-governance/internal/librarymaintenance"
|
||||
@@ -146,9 +148,13 @@ func usage() {
|
||||
|
||||
Flags (defaults target the local WKNFull test stack):
|
||||
-source-app WKNFull-app container whose env describes the source
|
||||
-source-pg WKNFull-postgres source PostgreSQL container (address resolution)
|
||||
-source-pg WKNFull-postgres source PostgreSQL container; empty = the source DB is a
|
||||
managed instance: connect with the source app env
|
||||
(DB_HOST/DB_PORT/DB_NAME/DB_USER/DB_PASSWORD), TLS mode from
|
||||
LIBRARY_SOURCE_DB_SSLMODE (default require)
|
||||
-source-es WKNFull-elasticsearch
|
||||
-governance-pg knowledge-governance-p1-postgres-1 routing bindings read here
|
||||
-governance-pg knowledge-governance-p1-postgres-1 routing bindings read here; empty = read
|
||||
the DSN from LIBRARY_GOVERNANCE_DSN_FILE (0600 file)
|
||||
-project weknora071full Compose project owning the writer group
|
||||
-writers WKNFull-app declared writer services (comma separated)
|
||||
-non-writers WKNFull-frontend=reason,... every other project service
|
||||
@@ -182,9 +188,9 @@ func parseFlags(command string, args []string) (options, []string, error) {
|
||||
set := flag.NewFlagSet(command, flag.ContinueOnError)
|
||||
set.SetOutput(os.Stderr)
|
||||
set.StringVar(&opts.sourceApp, "source-app", opts.sourceApp, "source container describing the stack")
|
||||
set.StringVar(&opts.sourcePG, "source-pg", opts.sourcePG, "source PostgreSQL container")
|
||||
set.StringVar(&opts.sourcePG, "source-pg", opts.sourcePG, "source PostgreSQL container; empty = connect to the source app's external (managed) database")
|
||||
set.StringVar(&opts.sourceES, "source-es", opts.sourceES, "source Elasticsearch container")
|
||||
set.StringVar(&opts.governancePG, "governance-pg", opts.governancePG, "governance PostgreSQL container")
|
||||
set.StringVar(&opts.governancePG, "governance-pg", opts.governancePG, "governance PostgreSQL container; empty = read the DSN from LIBRARY_GOVERNANCE_DSN_FILE")
|
||||
set.StringVar(&opts.project, "project", opts.project, "Compose project owning the writers")
|
||||
set.StringVar(&opts.writers, "writers", opts.writers, "declared writer services")
|
||||
set.StringVar(&opts.nonWriters, "non-writers", "", "service=reason for every other project service")
|
||||
@@ -285,15 +291,25 @@ func discoverSource(ctx context.Context, opts options, binding map[string]string
|
||||
if app["DB_DRIVER"] != "postgres" {
|
||||
return config, errors.New("source database driver is not postgres")
|
||||
}
|
||||
address, err := containerAddress(ctx, opts.sourcePG)
|
||||
if err != nil {
|
||||
return config, err
|
||||
// 两种来源:给了 -source-pg 就按容器地址拼(0.7.1 栈);没给说明库是托管
|
||||
// 实例(RDS 型 0.9.0 栈没有源库容器),用源应用容器 env 里的 DB_* 直连。
|
||||
if librarydsn.SourceMode(opts.sourcePG) == librarydsn.SourceExternal {
|
||||
dsn, dsnErr := librarydsn.SourceURL(app, os.Getenv)
|
||||
if dsnErr != nil {
|
||||
return config, dsnErr
|
||||
}
|
||||
config.DatabaseURL = dsn
|
||||
} else {
|
||||
address, addressErr := containerAddress(ctx, opts.sourcePG)
|
||||
if addressErr != nil {
|
||||
return config, addressErr
|
||||
}
|
||||
dsn, dsnErr := librarydsn.ContainerURL(address, app)
|
||||
if dsnErr != nil {
|
||||
return config, dsnErr
|
||||
}
|
||||
config.DatabaseURL = dsn
|
||||
}
|
||||
dsn := strings.Join([]string{
|
||||
"postgres://" + urlEscape(app["DB_USER"]) + ":" + urlEscape(app["DB_PASSWORD"]) +
|
||||
"@" + address + ":" + app["DB_PORT"] + "/" + urlEscape(app["DB_NAME"]) + "?sslmode=disable",
|
||||
}, "")
|
||||
config.DatabaseURL = dsn
|
||||
config.AppImageID, err = containerImageID(ctx, opts.sourceApp)
|
||||
if err != nil {
|
||||
return config, err
|
||||
@@ -388,17 +404,12 @@ func parseBool(value string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func urlEscape(value string) string {
|
||||
replacer := strings.NewReplacer("%", "%25", ":", "%3A", "@", "%40", "/", "%2F", "?", "%3F", "#", "%23", "&", "%26", " ", "%20")
|
||||
return replacer.Replace(value)
|
||||
}
|
||||
|
||||
// scopeBindings 返回 scope→知识库 映射。治理 API 用的是 LIBRARY_PRODUCTION_KB_*,
|
||||
// CLI 读的是治理库里的 routing bindings;同一台机器上两份不一致时,
|
||||
// CLI 抓到的会是"配置里那个库"而不是 API 认为的目标——所以环境变量优先,
|
||||
// 读不到才退回 bindings。两边讲同一份身份,验收才可比。
|
||||
func scopeBindings(ctx context.Context, container string) (map[string]string, error) {
|
||||
binding, err := readRoutingBindings(ctx, container)
|
||||
func scopeBindings(ctx context.Context, opts options) (map[string]string, error) {
|
||||
binding, err := readRoutingBindings(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -410,17 +421,105 @@ func scopeBindings(ctx context.Context, container string) (map[string]string, er
|
||||
return binding, nil
|
||||
}
|
||||
|
||||
func readRoutingBindings(ctx context.Context, container string) (map[string]string, error) {
|
||||
raw, err := dockerOutput(ctx, "exec", container, "psql", "-U", "governance", "-d", "governance", "-tAc",
|
||||
"SELECT internal_kb_id||'|'||external_kb_id FROM routing_kb_bindings WHERE id='active'")
|
||||
// readRoutingBindings 读治理库里生效的 scope 绑定。两种来源:
|
||||
// - 给了 -governance-pg:docker exec 进治理库容器跑 psql(0.7.1 本机栈);
|
||||
// - 没给:治理库是托管实例(RDS 型 0.9.0 栈没有 governance 容器),DSN 来自
|
||||
// LIBRARY_GOVERNANCE_DSN_FILE 指向的 0600 文件。
|
||||
//
|
||||
// 两条路径只共用行解析,不共用连接方式:容器里没有网络到 RDS 的凭据,宿主机也不该
|
||||
// 为了读两列去 docker exec 一个不存在的容器。
|
||||
func readRoutingBindings(ctx context.Context, opts options) (map[string]string, error) {
|
||||
if librarydsn.GovernanceMode(opts.governancePG) == librarydsn.GovernanceFile {
|
||||
dsn, err := librarydsn.GovernanceURL(os.Getenv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return readRoutingBindingsFromDSN(ctx, dsn)
|
||||
}
|
||||
raw, err := dockerOutput(ctx, "exec", opts.governancePG, "psql", "-U", "governance", "-d", "governance", "-tAc",
|
||||
routingBindingsQuery)
|
||||
if err != nil {
|
||||
return nil, errors.New("read routing bindings failed")
|
||||
}
|
||||
internal, external, ok := strings.Cut(strings.TrimSpace(raw), "|")
|
||||
if !ok || internal == "" || external == "" {
|
||||
return nil, errors.New("routing bindings are incomplete")
|
||||
return parseRoutingBindings(raw)
|
||||
}
|
||||
|
||||
// COALESCE 不能省:两列都可能是 NULL(0.9.0 迁移把\"旧知识库绑定已解除\"落成 NULL,
|
||||
// 平台约定\"对外库没有对应目标时留空\")。裸拼接遇 NULL 得到整行 NULL,文件模式
|
||||
// 会在 Scan 成 string 时直接失败,报出的还是一句\"read routing bindings failed\"——
|
||||
// 真正的原因(哪一列没绑)被吞掉。这里先拼成固定形状,由 parseRoutingBindings 判。
|
||||
const routingBindingsQuery = "SELECT COALESCE(internal_kb_id,'')||'|'||COALESCE(external_kb_id,'') FROM routing_kb_bindings WHERE id='active'"
|
||||
|
||||
// readRoutingBindingsFromDSN 用外部 DSN 读同一份绑定。
|
||||
//
|
||||
// 只读会话是刻意的:这条路径只是读两列,出了问题必须是"读不到",而不是在治理库上
|
||||
// 留下一次写入。错误一律收敛成固定说法:pgx 的错误里会带连接串(口令它自己打了码,
|
||||
// 但主机与用户名照旧出去),而这里连那份地址都不该出现在日志或工单里。
|
||||
func readRoutingBindingsFromDSN(ctx context.Context, dsn string) (map[string]string, error) {
|
||||
parsed, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, errors.New("governance database url is invalid")
|
||||
}
|
||||
return map[string]string{"internal": internal, "external": external}, nil
|
||||
parsed.ConnConfig.RuntimeParams["default_transaction_read_only"] = "on"
|
||||
parsed.ConnConfig.RuntimeParams["statement_timeout"] = "30000"
|
||||
parsed.MaxConns = 2
|
||||
pool, err := pgxpool.NewWithConfig(ctx, parsed)
|
||||
if err != nil {
|
||||
return nil, errors.New("open governance database failed")
|
||||
}
|
||||
defer pool.Close()
|
||||
var raw string
|
||||
if err = pool.QueryRow(ctx, routingBindingsQuery).Scan(&raw); err != nil {
|
||||
return nil, errors.New("read routing bindings failed")
|
||||
}
|
||||
return parseRoutingBindings(raw)
|
||||
}
|
||||
|
||||
// parseRoutingBindings 解一行 `<internal>|<external>`。两条来源(psql -tAc 与 pgx
|
||||
// 只读会话)拼的是同一句 SQL,所以只有这一份解析语义。
|
||||
//
|
||||
// 对内库必填:没有对内库就没有\"整库\"可言,此时必须点名是哪一列没绑,而不是退化成
|
||||
// 泛化的读失败。对外库可空:平台侧约定\"对外库没有对应目标时留空\",绑定本身仍然
|
||||
// 有效(`status`、internal scope 照常可跑);只有真去用 external scope 时才拒绝,
|
||||
// 且绝不回退到对内库——那会把对内库的内容当成对外库封存出去。
|
||||
func parseRoutingBindings(raw string) (map[string]string, error) {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
// 空输出 = 没有生效绑定行,等于对内库没绑:这跟"读失败"是两件事,
|
||||
// 必须说成前者,否则现场又会退回一句泛化错误。
|
||||
if trimmed == "" {
|
||||
return nil, errInternalKBMissing
|
||||
}
|
||||
internal, external, ok := strings.Cut(trimmed, "|")
|
||||
if !ok {
|
||||
return nil, errors.New("read routing bindings failed")
|
||||
}
|
||||
internal = strings.TrimSpace(internal)
|
||||
external = strings.TrimSpace(external)
|
||||
if internal == "" {
|
||||
return nil, errInternalKBMissing
|
||||
}
|
||||
binding := map[string]string{"internal": internal}
|
||||
if external != "" {
|
||||
binding["external"] = external
|
||||
}
|
||||
return binding, nil
|
||||
}
|
||||
|
||||
var (
|
||||
errInternalKBMissing = errors.New("对内知识库未绑定(routing_kb_bindings.internal_kb_id 为空)")
|
||||
errExternalKBMissing = errors.New("对外知识库未配置(routing_kb_bindings.external_kb_id 为空)")
|
||||
)
|
||||
|
||||
// scopeKnowledgeBase 取本次 scope 的目标库。空值必须在这里就拒绝:往下走会变成
|
||||
// \"knowledge base not found in source\"——那是拿空 UUID 查源库得到的假原因。
|
||||
func scopeKnowledgeBase(binding map[string]string, scope string) (string, error) {
|
||||
if kb := strings.TrimSpace(binding[scope]); kb != "" {
|
||||
return kb, nil
|
||||
}
|
||||
if scope == "external" {
|
||||
return "", errExternalKBMissing
|
||||
}
|
||||
return "", errInternalKBMissing
|
||||
}
|
||||
|
||||
func openSourceDatabase(ctx context.Context, config sourceConfig) (*pgxpool.Pool, error) {
|
||||
@@ -432,11 +531,25 @@ func openSourceDatabase(ctx context.Context, config sourceConfig) (*pgxpool.Pool
|
||||
// that a database-enforced property rather than a convention.
|
||||
parsed.ConnConfig.RuntimeParams["default_transaction_read_only"] = "on"
|
||||
parsed.ConnConfig.RuntimeParams["statement_timeout"] = "120000"
|
||||
// 启动参数不足以保证只读:角色级/库级设置会在启动参数之后生效,托管实例
|
||||
// (RDS)上实测就是这样——SHOW 回来是 off。而池里每条连接是独立的会话,
|
||||
// 对单条连接 SET 管不到其他连接,所以放在 AfterConnect:每条新连接都显式
|
||||
// 置只读,池里就没有能写的会话。下面的 SHOW 只是对任意一条连接再核一次。
|
||||
parsed.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
||||
_, err := conn.Exec(ctx, "SET SESSION default_transaction_read_only = on")
|
||||
return err
|
||||
}
|
||||
pool, err := pgxpool.NewWithConfig(ctx, parsed)
|
||||
if err != nil {
|
||||
return nil, errors.New("open source database failed")
|
||||
}
|
||||
var readOnly string
|
||||
// 连接失败与"能连上但会话可写"必须分开报:把两者压成同一句,现场只能看到
|
||||
// 一句无信息量的失败,反而会去怀疑权限或只读设置。
|
||||
if err = pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, errors.New("source database connection failed")
|
||||
}
|
||||
if err = pool.QueryRow(ctx, "SHOW default_transaction_read_only").Scan(&readOnly); err != nil || readOnly != "on" {
|
||||
pool.Close()
|
||||
return nil, errors.New("source session is not read-only")
|
||||
@@ -510,6 +623,11 @@ func openFreeze(ctx context.Context, opts options) (*libraryfreeze.Coordinator,
|
||||
//
|
||||
// 入口/队列的声明与容器内 worker 共用同一组 LIBRARY_TARGET_* 名称,
|
||||
// 数据库/索引/桶则取自本次真实发现的源,避免"关的是 A 的门、读的是 B 的队列"。
|
||||
//
|
||||
// LIBRARY_TARGET_INGRESS_MODE=none 只给整栈没有闸门的测试环境(0.9.0 测试栈):
|
||||
// 必须同时给 LIBRARY_NO_INGRESS_ACK=test-only,窗口里一笔闸门动作都不做,
|
||||
// 排空证据降为"写者全停 + 队列静默",恢复由 LIBRARY_TARGET_HEALTH_URL 的 200 证明。
|
||||
// 缺省仍是 gate:没有显式确认就照旧要求闸门。
|
||||
func captureMaintenanceConfig(opts options, source sourceConfig, get func(string) string) (librarymaintenance.Config, error) {
|
||||
target, err := librarymaintenance.TargetFromEnv(get)
|
||||
if err != nil {
|
||||
@@ -529,9 +647,12 @@ func captureMaintenanceConfig(opts options, source sourceConfig, get func(string
|
||||
Writers: writers,
|
||||
NonWriters: nonWriters,
|
||||
DockerSocket: opts.dockerSocket,
|
||||
IngressMode: target.Mode,
|
||||
IngressAck: target.Ack,
|
||||
IngressURL: target.IngressURL,
|
||||
IngressService: target.IngressService,
|
||||
IngressDrainFile: target.IngressDrainFile,
|
||||
HealthURL: target.HealthURL,
|
||||
Database: identity,
|
||||
Index: source.SearchIndex,
|
||||
Bucket: source.Object.Bucket,
|
||||
@@ -587,7 +708,12 @@ func openArchive(opts options) (*librarysnapshot.Archive, error) {
|
||||
|
||||
func runStatus(ctx context.Context, opts options) error {
|
||||
report := map[string]any{}
|
||||
binding, bindingErr := scopeBindings(ctx, opts.governancePG)
|
||||
// 只报来源模式:地址与凭据连 describe/status 都不出现,日志自然也不会带出去。
|
||||
report["databases"] = map[string]string{
|
||||
"source_db": librarydsn.SourceMode(opts.sourcePG),
|
||||
"governance_db": librarydsn.GovernanceMode(opts.governancePG),
|
||||
}
|
||||
binding, bindingErr := scopeBindings(ctx, opts)
|
||||
var config sourceConfig
|
||||
var err error
|
||||
if bindingErr != nil {
|
||||
@@ -680,7 +806,12 @@ func runPreflight(ctx context.Context, opts options) error {
|
||||
if opts.scope != "internal" && opts.scope != "external" {
|
||||
return errors.New("-scope must be internal or external")
|
||||
}
|
||||
binding, err := scopeBindings(ctx, opts.governancePG)
|
||||
binding, err := scopeBindings(ctx, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 绑定缺哪一半在连源库之前就要说清楚:external 未配置不是"源库里没这个库"。
|
||||
kb, err := scopeKnowledgeBase(binding, opts.scope)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -693,7 +824,6 @@ func runPreflight(ctx context.Context, opts options) error {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
kb := config.Scopes[opts.scope]
|
||||
tenant, err := tenantAndKnowledgeBase(ctx, pool, kb)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -773,7 +903,13 @@ func runCapture(ctx context.Context, opts options) error {
|
||||
return err
|
||||
}
|
||||
defer archive.Close()
|
||||
binding, err := scopeBindings(ctx, opts.governancePG)
|
||||
binding, err := scopeBindings(ctx, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 先定住本次 scope 的库:external 没配置时,封存必须在开维护窗口之前就停,
|
||||
// 不能先停写者再发现目标为空。
|
||||
kb, err := scopeKnowledgeBase(binding, opts.scope)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -801,7 +937,7 @@ func runCapture(ctx context.Context, opts options) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
scopedKB := binding[opts.scope]
|
||||
scopedKB := kb
|
||||
if found {
|
||||
manifest, err := replayCapture(job, opts.scope, scopedKB, existing, archive)
|
||||
if err != nil {
|
||||
@@ -814,7 +950,6 @@ func runCapture(ctx context.Context, opts options) error {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
kb := config.Scopes[opts.scope]
|
||||
tenant, err := tenantAndKnowledgeBase(ctx, pool, kb)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1150,7 +1285,12 @@ func runDeadLetters(ctx context.Context, opts options) error {
|
||||
if opts.scope != "internal" && opts.scope != "external" {
|
||||
return errors.New("-scope must be internal or external")
|
||||
}
|
||||
binding, err := scopeBindings(ctx, opts.governancePG)
|
||||
binding, err := scopeBindings(ctx, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// external 未配置时,死信观测同样要明说"没配置",而不是拿空库去查源库。
|
||||
kb, err := scopeKnowledgeBase(binding, opts.scope)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1163,7 +1303,6 @@ func runDeadLetters(ctx context.Context, opts options) error {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
kb := config.Scopes[opts.scope]
|
||||
tenant, err := tenantAndKnowledgeBase(ctx, pool, kb)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
)
|
||||
|
||||
// noGateMaintenanceEnv 是 0.9.0 测试栈:没有闸门,用显式确认 + 健康探针代替。
|
||||
func noGateMaintenanceEnv() map[string]string {
|
||||
env := maintenanceEnv()
|
||||
delete(env, "LIBRARY_TARGET_INGRESS_URL")
|
||||
delete(env, "LIBRARY_TARGET_INGRESS_SERVICE")
|
||||
delete(env, "LIBRARY_TARGET_INGRESS_DRAIN_FILE")
|
||||
env["LIBRARY_TARGET_INGRESS_MODE"] = libraryfreeze.IngressModeNone
|
||||
env["LIBRARY_NO_INGRESS_ACK"] = libraryfreeze.NoIngressAck
|
||||
env["LIBRARY_TARGET_HEALTH_URL"] = "http://127.0.0.1:18081/health"
|
||||
return env
|
||||
}
|
||||
|
||||
func TestCaptureMaintenanceConfigNoGateWithAck(t *testing.T) {
|
||||
env := noGateMaintenanceEnv()
|
||||
config, err := captureMaintenanceConfig(captureOptions(), captureSource(), func(name string) string { return env[name] })
|
||||
if err != nil {
|
||||
t.Fatalf("no-gate 声明被拒:%v", err)
|
||||
}
|
||||
if config.IngressMode != libraryfreeze.IngressModeNone || config.IngressAck != libraryfreeze.NoIngressAck {
|
||||
t.Fatalf("入口形态没透传:%+v", config)
|
||||
}
|
||||
if config.IngressURL != "" || config.IngressService != "" || config.IngressDrainFile != "" {
|
||||
t.Fatalf("no-gate 配置里出现了闸门项:%+v", config)
|
||||
}
|
||||
if config.HealthURL != "http://127.0.0.1:18081/health" || config.RedisAddr != "127.0.0.1:26379" {
|
||||
t.Fatalf("健康探针/队列没透传:%+v", config)
|
||||
}
|
||||
// 目标身份仍取自本次真实发现的源:no-gate 只改证据形态,不改动的是谁。
|
||||
if config.Database != "127.0.0.1:25432/kgmaint_target" || config.Index != "kgmaint_1028a_chunks" {
|
||||
t.Fatalf("目标身份不对:%+v", config)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaptureMaintenanceConfigNoGateWithoutAckIsRefused(t *testing.T) {
|
||||
env := noGateMaintenanceEnv()
|
||||
delete(env, "LIBRARY_NO_INGRESS_ACK")
|
||||
_, err := captureMaintenanceConfig(captureOptions(), captureSource(), func(name string) string { return env[name] })
|
||||
if err == nil || !strings.Contains(err.Error(), "LIBRARY_NO_INGRESS_ACK") {
|
||||
t.Fatalf("没有显式确认却放过了 no-gate:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// no-gate 不得悄悄接受半套闸门声明:要么有闸门,要么明确没有。
|
||||
func TestCaptureMaintenanceConfigNoGateRejectsPartialGate(t *testing.T) {
|
||||
env := noGateMaintenanceEnv()
|
||||
env["LIBRARY_TARGET_INGRESS_SERVICE"] = "ingress"
|
||||
if _, err := captureMaintenanceConfig(captureOptions(), captureSource(), func(name string) string { return env[name] }); err == nil {
|
||||
t.Fatal("no-gate 与闸门服务同时存在却没有拒绝")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// internal 为空必须拒绝并点名。实测到的现场就是这一行:0.9.0 迁移后
|
||||
// internal_kb_id 与 external_kb_id 都是 NULL,旧解析退化成
|
||||
// "read routing bindings failed",操作员无法判断是连不上还是没绑定。
|
||||
// NULL 与空串在 SQL 侧由 routingBindingsQuery 的 COALESCE 收敛成同一形状,
|
||||
// 解析侧两者都必须落到同一条错误上。
|
||||
func TestParseRoutingBindingsRejectsEmptyInternal(t *testing.T) {
|
||||
for _, raw := range []string{"|", "|ext-kb", " \n| \n", "\n|\n", ""} {
|
||||
binding, err := parseRoutingBindings(raw)
|
||||
if err == nil {
|
||||
t.Fatalf("internal 为空却解析成功:%q → %v", raw, binding)
|
||||
}
|
||||
if binding != nil {
|
||||
t.Fatalf("internal 为空仍返回了映射:%q → %v", raw, binding)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "对内知识库未绑定") || !strings.Contains(err.Error(), "internal_kb_id") {
|
||||
t.Fatalf("错误没有点名 internal 未绑定:%q → %v", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 0.9.0 测试实例的真实形状:只有对内库(wiki-demo),平台约定对外库没有对应
|
||||
// 目标时留空。绑定仍然有效,internal scope 能解析;external scope 必须失败、
|
||||
// 点名 external_kb_id,并且绝不回退到对内库。
|
||||
func TestParseRoutingBindingsAllowsEmptyExternal(t *testing.T) {
|
||||
binding, err := parseRoutingBindings("0da9b571-6f13-46a1-85fa-e63d2ef5666e|")
|
||||
if err != nil {
|
||||
t.Fatalf("对外库为空不该让整份绑定失效:%v", err)
|
||||
}
|
||||
if got := binding["internal"]; got != "0da9b571-6f13-46a1-85fa-e63d2ef5666e" {
|
||||
t.Fatalf("对内库解析错误:%q", got)
|
||||
}
|
||||
if value, present := binding["external"]; present {
|
||||
t.Fatalf("对外库没配置却写进了映射:%q", value)
|
||||
}
|
||||
|
||||
kb, err := scopeKnowledgeBase(binding, "internal")
|
||||
if err != nil || kb != "0da9b571-6f13-46a1-85fa-e63d2ef5666e" {
|
||||
t.Fatalf("internal scope 不该被 external 为空牵连:kb=%q err=%v", kb, err)
|
||||
}
|
||||
|
||||
kb, err = scopeKnowledgeBase(binding, "external")
|
||||
if err == nil {
|
||||
t.Fatalf("对外库未配置却解析出了目标库:%q", kb)
|
||||
}
|
||||
if kb != "" {
|
||||
t.Fatalf("external scope 回退到了别的库:%q", kb)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "对外知识库未配置") || !strings.Contains(err.Error(), "external_kb_id") {
|
||||
t.Fatalf("错误没有点名 external 未配置:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 两个库都绑定时行为不变:这是 0.7.1 栈与对外库已配置的 0.9.0 栈的常态路径。
|
||||
func TestParseRoutingBindingsBothScopesUnchanged(t *testing.T) {
|
||||
// psql -tAc 输出带行尾换行,pgx 那边不带——两边都必须是同一个结果。
|
||||
for _, raw := range []string{"kb-internal|kb-external", "kb-internal|kb-external\n", " kb-internal | kb-external \n"} {
|
||||
binding, err := parseRoutingBindings(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("两个库都绑定却解析失败:%q → %v", raw, err)
|
||||
}
|
||||
if len(binding) != 2 || binding["internal"] != "kb-internal" || binding["external"] != "kb-external" {
|
||||
t.Fatalf("解析结果与原行为不一致:%q → %v", raw, binding)
|
||||
}
|
||||
for _, scope := range []string{"internal", "external"} {
|
||||
if _, err := scopeKnowledgeBase(binding, scope); err != nil {
|
||||
t.Fatalf("%s scope 不该失败:%v", scope, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 没有分隔符说明拿到的不是那一行(空结果集、psql 报错文本等),只能是读失败。
|
||||
func TestParseRoutingBindingsRejectsMalformedRow(t *testing.T) {
|
||||
for _, raw := range []string{"kb-without-separator", "ERROR: relation \"routing_kb_bindings\" does not exist"} {
|
||||
if _, err := parseRoutingBindings(raw); err == nil {
|
||||
t.Fatalf("没有分隔符却解析成功:%q", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 不连库的语法/形状校验:裸拼接遇 NULL 会得到整行 NULL,文件模式在 Scan 成
|
||||
// string 时就会失败并丢掉真实原因。两句来源共用这一条 SQL,因此只查这一处。
|
||||
func TestRoutingBindingsQueryCoalescesBothColumns(t *testing.T) {
|
||||
query := routingBindingsQuery
|
||||
for _, column := range []string{"internal_kb_id", "external_kb_id"} {
|
||||
if !strings.Contains(query, "COALESCE("+column+",'')") {
|
||||
t.Fatalf("查询没有把 %s 的 NULL 收敛成空串:%s", column, query)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(query, "||'|'||") {
|
||||
t.Fatalf("查询不再产出 '<internal>|<external>' 形状:%s", query)
|
||||
}
|
||||
if !strings.Contains(query, "WHERE id='active'") {
|
||||
t.Fatalf("查询没有锁定生效绑定行:%s", query)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// v090 采集脚本是本轮新增的唯一入口。为什么用 bash -n 而不是"跑一遍":跑一遍要
|
||||
// 停写入者、连 RDS——那是真实采集,不是单元测试。语法与声明在这里钉住,能让
|
||||
// "标志写错一个字母"在 CI 里就红,而不是等到开维护窗口时才发现。
|
||||
func TestV090CaptureScriptIsSyntacticallyValid(t *testing.T) {
|
||||
bash, err := exec.LookPath("bash")
|
||||
if err != nil {
|
||||
t.Fatalf("需要 bash 才能检查脚本语法(CI 与测试机都有):%v", err)
|
||||
}
|
||||
path := filepath.Join("..", "..", "scripts", "library-capture-v090.sh")
|
||||
out, err := exec.Command(bash, "-n", path).CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("bash -n %s 失败:%v\n%s", path, err, out)
|
||||
}
|
||||
}
|
||||
|
||||
// 脚本内容断言:外部库模式(两个 -pg 传空)、写者声明、容器名、归档根、
|
||||
// LIBRARYCTL_BIN 缺失即失败——这些一旦漂移,真跑时会封错栈或读错库。
|
||||
func TestV090CaptureScriptDeclaresExternalDatabases(t *testing.T) {
|
||||
body, err := os.ReadFile(filepath.Join("..", "..", "scripts", "library-capture-v090.sh"))
|
||||
if err != nil {
|
||||
t.Fatalf("读脚本:%v", err)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"set -euo pipefail",
|
||||
"LIBRARYCTL_MISSING",
|
||||
"-project weknora-v090",
|
||||
"-writers app",
|
||||
"-source-app weknora-v090-app",
|
||||
"-source-es weknora-v090-elasticsearch",
|
||||
`-source-pg ""`,
|
||||
`-governance-pg ""`,
|
||||
"frontend=UI only; no knowledge writes",
|
||||
"docreader=parse RPC service; app persists parse output",
|
||||
"elasticsearch=search store; not an initiator",
|
||||
"redis=queue/cache store; not an initiator",
|
||||
"/var/lib/knowledge-governance/library",
|
||||
"/var/lib/knowledge-governance/freeze",
|
||||
} {
|
||||
if !strings.Contains(string(body), want) {
|
||||
t.Fatalf("脚本缺少 %q", want)
|
||||
}
|
||||
}
|
||||
// 0.7.1 的服务名绝不能出现在 v090 入口里:那会让封存停错栈。
|
||||
for _, forbidden := range []string{"WKNFull", "weknora071full"} {
|
||||
if strings.Contains(string(body), forbidden) {
|
||||
t.Fatalf("v090 脚本里出现了 0.7.1 的 %q", forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
v071 "knowledge-governance/internal/releaseadapter/v071"
|
||||
)
|
||||
|
||||
func writeDrillCanary(ctx context.Context, root string, ledger *libraryrelease.WriteSet, id string, mark func(context.Context) error, write func(context.Context) error) error {
|
||||
ledger.Rows = append(ledger.Rows, libraryrelease.WriteSetRow{Table: "knowledge_tags", ID: id, Existing: false})
|
||||
if err := ledger.Seal(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := v071.PersistWriteSet(root, ledger); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := mark(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return write(ctx)
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCanaryPersistsRecoveryEvidenceBeforeWrite(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
ledger := libraryrelease.WriteSet{Version: 1, JobID: "drill", Target: "internal", TenantID: 10000, KnowledgeBaseID: "kb", BackupSnapshotID: "backup", TargetSnapshotID: "backup", CreatedAt: time.Now().UTC(), Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
marked := false
|
||||
written := false
|
||||
err := writeDrillCanary(context.Background(), root, &ledger, "canary-id", func(context.Context) error { marked = true; return nil }, func(context.Context) error {
|
||||
if !marked {
|
||||
t.Fatal("write preceded risk marker")
|
||||
}
|
||||
if len(ledger.CreatedIDs("knowledge_tags")) != 1 {
|
||||
t.Fatal("canary absent from write set")
|
||||
}
|
||||
matches, e := filepath.Glob(filepath.Join(root, "writesets", "*.json"))
|
||||
if e != nil || len(matches) != 1 {
|
||||
t.Fatalf("durable ledger missing: %v %v", matches, e)
|
||||
}
|
||||
if _, e := os.Stat(matches[0]); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
written = true
|
||||
return nil
|
||||
})
|
||||
if err != nil || !written {
|
||||
t.Fatalf("err=%v written=%v", err, written)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanaryNeverWritesWithoutDurableLedger(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "blocked")
|
||||
if err := os.WriteFile(root, []byte("not a directory"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ledger := libraryrelease.WriteSet{Version: 1, JobID: "drill", Target: "internal", TenantID: 10000, KnowledgeBaseID: "kb", BackupSnapshotID: "backup", TargetSnapshotID: "backup", CreatedAt: time.Now().UTC(), Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
marked, written := false, false
|
||||
err := writeDrillCanary(context.Background(), root, &ledger, "canary-id", func(context.Context) error { marked = true; return nil }, func(context.Context) error { written = true; return nil })
|
||||
if err == nil || marked || written {
|
||||
t.Fatalf("err=%v marked=%v written=%v", err, marked, written)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanaryNeverWritesWhenRiskMarkerFails(t *testing.T) {
|
||||
ledger := libraryrelease.WriteSet{Version: 1, JobID: "drill", Target: "internal", TenantID: 10000, KnowledgeBaseID: "kb", BackupSnapshotID: "backup", TargetSnapshotID: "backup", CreatedAt: time.Now().UTC(), Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
written := false
|
||||
err := writeDrillCanary(context.Background(), t.TempDir(), &ledger, "canary-id", func(context.Context) error { return errors.New("journal unavailable") }, func(context.Context) error { written = true; return nil })
|
||||
if err == nil || written {
|
||||
t.Fatalf("err=%v written=%v", err, written)
|
||||
}
|
||||
}
|
||||
+15
-203
@@ -1,4 +1,4 @@
|
||||
// command librarydrill 在目标实例上做一次**真实**的恢复演练:
|
||||
// command librarydrill 是恢复演练的**命令行入口**,实现在 internal/libraryverify:
|
||||
//
|
||||
// 维护隔离(关入口/停写入者/证明排空)
|
||||
// → 捕获目标整库(真预备份,落归档)
|
||||
@@ -8,16 +8,14 @@
|
||||
// → 逐表/索引/资源/Wiki 再校验一次,必须完全一致
|
||||
// → 关窗口(按原运行状态恢复服务、开入口、验证健康)
|
||||
//
|
||||
// 它只读/写**被测目标自己**,绝不触碰封存源;所有凭据来自进程环境
|
||||
// (与 worker 同一份 LIBRARY_* 配置),不进命令行、不打印。
|
||||
// 任一步失败都不伪装成功:窗口保持维护、报告写明失败阶段。
|
||||
// 为什么只剩薄封装:页面要能点一次"恢复验证",worker 必须在进程内跑同一份实现。
|
||||
// 两份实现意味着"CLI 通过、页面失败"或反过来,演练结论就不可信了。
|
||||
// 它只读/写**被测目标自己**,绝不触碰封存源;所有凭据来自进程环境。
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -26,35 +24,12 @@ import (
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
"knowledge-governance/internal/librarystack"
|
||||
"knowledge-governance/internal/libraryverify"
|
||||
)
|
||||
|
||||
type drillVerification struct {
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
Verified bool `json:"verified"`
|
||||
Error string `json:"error,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
type drillReport struct {
|
||||
Status string `json:"status"` // passed | failed | maintenance_held
|
||||
Scope string `json:"scope"`
|
||||
Target string `json:"target"`
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
Project string `json:"project"`
|
||||
Counts map[string]int64 `json:"counts"`
|
||||
BaselineCheck *drillVerification `json:"baseline_check"`
|
||||
DriftCheck *drillVerification `json:"drift_check"` // 必须失败:证明确实能发现差异
|
||||
RestoreCheck *drillVerification `json:"restore_check"`
|
||||
Canary string `json:"canary"`
|
||||
Failure string `json:"failure,omitempty"`
|
||||
Stage string `json:"stage,omitempty"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
FinishedAt time.Time `json:"finished_at"`
|
||||
Until map[string]string `json:"until,omitempty"`
|
||||
}
|
||||
// 报告类型直接复用共享实现:命令行与页面/worker 的证据字段完全一致。
|
||||
type drillVerification = libraryverify.Verification
|
||||
type drillReport = libraryverify.Report
|
||||
|
||||
func main() {
|
||||
scope := flag.String("scope", "internal", "internal|external")
|
||||
@@ -88,18 +63,7 @@ func recoverWindows() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
manager, err := librarystack.OpenMaintenance(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer manager.Close()
|
||||
coordinator, control, err := manager.Open(config.LibraryTargetJournalRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer control.Close()
|
||||
defer coordinator.Close()
|
||||
if err := coordinator.Recover(context.Background()); err != nil {
|
||||
if err := libraryverify.RecoverWindows(config); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("maintenance windows recovered; target should be back to its original service state")
|
||||
@@ -107,170 +71,18 @@ func recoverWindows() error {
|
||||
}
|
||||
|
||||
func run(scope, jobID string) (*drillReport, error) {
|
||||
if scope != "internal" && scope != "external" {
|
||||
return nil, errors.New("scope must be internal or external")
|
||||
}
|
||||
config, err := appconfig.Load(os.Getenv)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
report := &drillReport{Status: "failed", Scope: scope, Project: config.LibraryTargetProject,
|
||||
Target: config.LibraryTargetInstance, StartedAt: time.Now().UTC()}
|
||||
fail := func(stage string, cause error, held bool) (*drillReport, error) {
|
||||
report.Stage = stage
|
||||
report.Failure = cause.Error()
|
||||
report.FinishedAt = time.Now().UTC()
|
||||
if held {
|
||||
report.Status = "maintenance_held"
|
||||
}
|
||||
return report, cause
|
||||
}
|
||||
|
||||
target, backups, err := librarystack.OpenBaselineStack(ctx, config, scope)
|
||||
if err != nil {
|
||||
return fail("open_target", err, false)
|
||||
}
|
||||
defer target.Close()
|
||||
manager, err := librarystack.OpenMaintenance(config)
|
||||
if err != nil {
|
||||
return fail("open_maintenance", err, false)
|
||||
}
|
||||
defer manager.Close()
|
||||
if err := manager.VerifyTopology(ctx); err != nil {
|
||||
return fail("topology", err, false)
|
||||
}
|
||||
coordinator, control, err := manager.Open(config.LibraryTargetJournalRoot)
|
||||
if err != nil {
|
||||
return fail("open_coordinator", err, false)
|
||||
}
|
||||
defer control.Close()
|
||||
defer coordinator.Close()
|
||||
|
||||
if jobID == "" {
|
||||
jobID = "drill-" + scope + "-" + time.Now().UTC().Format("20060102T150405")
|
||||
jobID = fmt.Sprintf("drill-%s-%d", scope, time.Now().UTC().UnixNano())
|
||||
}
|
||||
var (
|
||||
snapshotID string
|
||||
counts map[string]int64
|
||||
canary string
|
||||
contentTouched bool
|
||||
baseline drillVerification
|
||||
drift drillVerification
|
||||
restored drillVerification
|
||||
)
|
||||
work := func(ctx context.Context, window *libraryfreeze.Window) (string, error) {
|
||||
start := time.Now()
|
||||
current, err := target.Current(ctx)
|
||||
if err != nil {
|
||||
return "", errors.New("读取目标当前整库状态失败:" + err.Error())
|
||||
}
|
||||
counts = current.Counts
|
||||
report.Counts = counts
|
||||
// 1) 真预备份(落归档、内容寻址、导出后复核内容未变)。
|
||||
backup, err := backups.Prepare(ctx, "", current)
|
||||
if err != nil {
|
||||
return "", errors.New("捕获目标整库失败:" + err.Error())
|
||||
}
|
||||
snapshotID = backup.SnapshotID
|
||||
report.SnapshotID = snapshotID
|
||||
if err := window.SetBackupSnapshot(ctx, snapshotID); err != nil {
|
||||
return "", errors.New("记录预备份身份失败:" + err.Error())
|
||||
}
|
||||
log.Printf("drill: snapshot captured snapshot=%s bytes=%d in=%s", snapshotID, backup.Bytes, time.Since(start))
|
||||
|
||||
// 2) 基线校验:预备份必须与目标完全一致。
|
||||
baseline = drillVerification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if err := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); err != nil {
|
||||
baseline.Error = err.Error()
|
||||
report.BaselineCheck = &baseline
|
||||
return "", errors.New("预备份与目标不一致(预备份本身不可信):" + err.Error())
|
||||
}
|
||||
baseline.Verified = true
|
||||
report.BaselineCheck = &baseline
|
||||
|
||||
// Canary is an isolated tag, including on an empty target. Never seed
|
||||
// a document or change an existing row merely to make a drill possible.
|
||||
var random [16]byte
|
||||
if _, err := rand.Read(random[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
canary = fmt.Sprintf("%x", random[:])
|
||||
identityValue := target.Identity()
|
||||
var exists bool
|
||||
if err := target.Pool().QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM knowledge_tags WHERE id=$1)", canary).Scan(&exists); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if exists {
|
||||
return "", errors.New("canary identity collision; no write attempted")
|
||||
}
|
||||
ledger := libraryrelease.WriteSet{Version: libraryrelease.WriteSetVersion, JobID: jobID, Target: scope,
|
||||
TenantID: identityValue.TenantID, KnowledgeBaseID: identityValue.KnowledgeBaseID,
|
||||
BackupSnapshotID: snapshotID, TargetSnapshotID: snapshotID,
|
||||
CreatedAt: time.Now().UTC(), Universe: backup.Universe}
|
||||
report.Canary = canary
|
||||
if err := writeDrillCanary(ctx, config.LibraryArchiveRoot, &ledger, canary,
|
||||
func(ctx context.Context) error {
|
||||
if err := window.MarkContentRisk(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
contentTouched = true
|
||||
return nil
|
||||
}, func(ctx context.Context) error {
|
||||
_, err := target.Pool().Exec(ctx, `INSERT INTO knowledge_tags(id,tenant_id,knowledge_base_id,name) VALUES($1,$2,$3,$4)`, canary, identityValue.TenantID, identityValue.KnowledgeBaseID, "drill-canary-"+canary)
|
||||
return err
|
||||
}); err != nil {
|
||||
return "", fmt.Errorf("canary preparation/write failed: %w", err)
|
||||
}
|
||||
drift = drillVerification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if verifyErr := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); verifyErr == nil {
|
||||
drift.Error = "写入 canary 后校验仍然通过:校验没有真正比对内容"
|
||||
report.DriftCheck = &drift
|
||||
return "", errors.New("写入 canary 后校验仍然通过:校验没有真正比对内容,拒绝把这次演练当成通过")
|
||||
} else {
|
||||
drift.Error = verifyErr.Error()
|
||||
}
|
||||
report.DriftCheck = &drift
|
||||
log.Printf("drill: drift detected as expected (%s)", drift.Error)
|
||||
|
||||
// 4) 按预备份恢复。
|
||||
// Restore removes only the exact new canary identity in the durable write set.
|
||||
if err := target.Restore(ctx, libraryrelease.BackupRef{ID: "backup-" + snapshotID, SnapshotID: snapshotID,
|
||||
WriteSet: &ledger}); err != nil {
|
||||
return "", errors.New("按预备份恢复失败:" + err.Error())
|
||||
}
|
||||
// 5) 恢复后逐类复核:必须与预备份完全一致。
|
||||
restored = drillVerification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if err := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); err != nil {
|
||||
restored.Error = err.Error()
|
||||
report.RestoreCheck = &restored
|
||||
return "", errors.New("恢复后内容仍与预备份不一致:" + err.Error())
|
||||
}
|
||||
restored.Verified = true
|
||||
report.RestoreCheck = &restored
|
||||
log.Printf("drill: restored and verified snapshot=%s", snapshotID)
|
||||
return snapshotID, nil
|
||||
}
|
||||
|
||||
window, windowErr := coordinator.RunWindow(ctx, jobID,
|
||||
func(ctx context.Context) error { return manager.VerifyRunning(ctx) }, work,
|
||||
// 没写过目标内容 ⇒ 按原状态恢复永远安全;写过 canary ⇒ 只有恢复并校验通过才开放。
|
||||
func(error) bool { return !contentTouched || restored.Verified })
|
||||
|
||||
report.Until = map[string]string{"job": jobID, "window_state": window.State}
|
||||
if windowErr != nil || window.State != "succeeded" {
|
||||
cause := windowErr
|
||||
if cause == nil {
|
||||
cause = fmt.Errorf("维护窗口状态 %s(未成功)", window.State)
|
||||
}
|
||||
if window.State == "held" {
|
||||
return fail("window", cause, true)
|
||||
}
|
||||
return fail("window", cause, false)
|
||||
}
|
||||
report.Status = "passed"
|
||||
report.FinishedAt = time.Now().UTC()
|
||||
return report, nil
|
||||
// 主机侧 CLI 不持有治理库连接:维护记录由调用它的作业/运维流程负责。
|
||||
// 页面发起的验证走 worker,那里 Options 同时接治理生命周期与写集合登记。
|
||||
return libraryverify.Run(context.TODO(), config, scope, jobID, libraryverify.Options{
|
||||
Progress: func(stage, state, detail string) { log.Printf("stage=%s state=%s %s", stage, state, detail) },
|
||||
})
|
||||
}
|
||||
|
||||
func writeReport(path string, report *drillReport) error {
|
||||
|
||||
@@ -87,7 +87,7 @@ func run(ctx context.Context, config appconfig.Config, scope, snapshotID, backup
|
||||
if strings.TrimSpace(expectSource) != want {
|
||||
return nil, fmt.Errorf("来源范围与 -expect-source 不一致:归档为 %s,参数为 %s", want, expectSource)
|
||||
}
|
||||
target, _, err := librarystack.OpenProductionStack(ctx, config, scope, backupSnapshotID, "")
|
||||
target, _, err := librarystack.OpenProductionStack(ctx, config, scope, backupSnapshotID, "", nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("装配目标失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
@@ -35,10 +33,6 @@ func run() error {
|
||||
if len(data) < 32 || len(control) < 32 || data == control {
|
||||
return errors.New("gateway_distinct_tokens_required")
|
||||
}
|
||||
pub, e := base64.StdEncoding.DecodeString(os.Getenv("OBJECT_GATEWAY_AUDIT_PUBLIC_KEY"))
|
||||
if e != nil || len(pub) != ed25519.PublicKeySize {
|
||||
return errors.New("gateway_auditor_key_required")
|
||||
}
|
||||
provider, endpoint, bucket := os.Getenv("OBJECT_GATEWAY_PROVIDER"), os.Getenv("OBJECT_GATEWAY_ENDPOINT"), os.Getenv("OBJECT_GATEWAY_BUCKET")
|
||||
scope := objectgateway.Digest([]byte(provider + "\x00" + endpoint + "\x00" + bucket))
|
||||
pool, e := pgxpool.New(ctx, os.Getenv("OBJECT_GATEWAY_DATABASE_URL"))
|
||||
@@ -50,7 +44,10 @@ func run() error {
|
||||
if e != nil {
|
||||
return errors.New("gateway_backend_configuration_invalid")
|
||||
}
|
||||
g := &objectgateway.Gateway{Pool: pool, Scope: scope, Backend: backend, MaxBytes: 256 * 1024 * 1024, Isolation: objectgateway.AuditVerifier{Path: os.Getenv("OBJECT_GATEWAY_AUDIT_FILE"), PublicKey: pub, Scope: scope, GatewayID: os.Getenv("OBJECT_GATEWAY_ID")}}
|
||||
// The target bucket is shared with other writers. Admission is the journal
|
||||
// plus backend capability; retired attestation env vars are deliberately
|
||||
// ignored so an old deployment file cannot gate this path.
|
||||
g := &objectgateway.Gateway{Pool: pool, Scope: scope, Backend: backend, MaxBytes: 256 * 1024 * 1024}
|
||||
if governanceURL := os.Getenv("OBJECT_GATEWAY_GOVERNANCE_DATABASE_URL"); governanceURL != "" {
|
||||
cfg, e := pgxpool.ParseConfig(governanceURL)
|
||||
if e != nil {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// startErrorMessage builds the addressed binary and runs it long enough to
|
||||
// observe why the real entry point refused to listen.
|
||||
func startErrorMessage(t *testing.T, extra ...string) (string, int) {
|
||||
t.Helper()
|
||||
bin := filepath.Join(t.TempDir(), "object-gateway")
|
||||
build := exec.Command("go", "build", "-o", bin, ".")
|
||||
if out, err := build.CombinedOutput(); err != nil {
|
||||
t.Fatalf("build gateway: %v\n%s", err, out)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, bin)
|
||||
cmd.Env = append([]string{
|
||||
"PATH=" + os.Getenv("PATH"),
|
||||
"HOME=" + os.Getenv("HOME"),
|
||||
"OBJECT_GATEWAY_LISTEN=127.0.0.1:0",
|
||||
"OBJECT_GATEWAY_DATABASE_URL=postgres://gateway@127.0.0.1:1/gateway?sslmode=disable&connect_timeout=2",
|
||||
"OBJECT_GATEWAY_PROVIDER=minio",
|
||||
"OBJECT_GATEWAY_ENDPOINT=http://127.0.0.1:19100",
|
||||
"OBJECT_GATEWAY_BUCKET=maintenance-fixture-bucket",
|
||||
"OBJECT_GATEWAY_REGION=us-east-1",
|
||||
"OBJECT_GATEWAY_ACCESS_KEY=fixture-access-key",
|
||||
"OBJECT_GATEWAY_SECRET_KEY=fixture-secret-key",
|
||||
"OBJECT_GATEWAY_DATA_TOKEN=" + strings.Repeat("d", 32),
|
||||
"OBJECT_GATEWAY_CONTROL_TOKEN=" + strings.Repeat("c", 32),
|
||||
}, extra...)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if ctx.Err() != nil {
|
||||
t.Fatalf("gateway did not terminate; output=%s", out)
|
||||
}
|
||||
code := 0
|
||||
if exit, ok := err.(*exec.ExitError); ok {
|
||||
code = exit.ExitCode()
|
||||
} else if err != nil {
|
||||
t.Fatalf("run gateway: %v", err)
|
||||
}
|
||||
return string(out), code
|
||||
}
|
||||
|
||||
// The default shared-bucket path must not require a whole-bucket exclusive
|
||||
// writer attestation. Only the journal/backend admission gate may refuse it,
|
||||
// and a stale attestation file left behind by an older deployment must not
|
||||
// gate the default path either.
|
||||
func TestGatewayEntrypointDoesNotRequireBucketAttestation(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
env []string
|
||||
}{
|
||||
{name: "no_audit_configured"},
|
||||
{name: "stale_audit_file_ignored", env: []string{
|
||||
"OBJECT_GATEWAY_ID=retired-gateway",
|
||||
"OBJECT_GATEWAY_AUDIT_FILE=/nonexistent/expired-attestation.json",
|
||||
"OBJECT_GATEWAY_AUDIT_PUBLIC_KEY=" + base64.StdEncoding.EncodeToString(make([]byte, 32)),
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
out, code := startErrorMessage(t, tc.env...)
|
||||
if code == 0 {
|
||||
t.Fatalf("gateway exited 0 instead of reporting its admission state: %s", out)
|
||||
}
|
||||
if strings.Contains(out, "gateway_auditor_key_required") || strings.Contains(out, "gateway_exclusive_writer_evidence_unverified") {
|
||||
t.Fatalf("shared-bucket admission still gated by whole-bucket attestation: %s", out)
|
||||
}
|
||||
if !strings.Contains(out, "gateway_persistence_unavailable") && !strings.Contains(out, "gateway_backend_not_ready") {
|
||||
t.Fatalf("gateway did not reach journal/backend admission: %s", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
)
|
||||
|
||||
// r13CountingTarget 记录 Apply 次数:锚点写失败时它必须是 0。
|
||||
type r13CountingTarget struct {
|
||||
ledgerTestTarget
|
||||
applied int
|
||||
}
|
||||
|
||||
func (t *r13CountingTarget) Apply(ctx context.Context, stage libraryrelease.Stage, snapshot string) error {
|
||||
t.applied++
|
||||
return t.ledgerTestTarget.Apply(ctx, stage, snapshot)
|
||||
}
|
||||
|
||||
// B25:写集合/预备份锚点写不进治理库时,必须在任何业务写入之前失败,目标零写入,
|
||||
// 也不许留下"已有基线"的假象。故障来自真实 PG(版本表上的 CHECK 约束拒绝写入),
|
||||
// 不是包一层必失败的假 store。
|
||||
func TestR13AnchorWriteFailureBlocksTargetIsolated(t *testing.T) {
|
||||
store, pool := openLedgerStore(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
const (
|
||||
scope = "internal"
|
||||
jobID = "libsync-r13-anchor"
|
||||
snapshot = "backup-snap-r13-anchor"
|
||||
target = "target-snap-r13-anchor"
|
||||
params = "params-r13-anchor"
|
||||
)
|
||||
if _, _, err := store.CreateLibraryJob(ctx, governance.LibraryJob{ID: jobID, Scope: scope,
|
||||
Kind: governance.LibraryJobSync, IdempotencyKey: "library.sync:internal:r13-anchor",
|
||||
RequestDigest: params, Actor: "admin", TargetSnapshotID: target}, governance.SyncStages(), now); err != nil {
|
||||
t.Fatalf("create job: %v", err)
|
||||
}
|
||||
var exists bool
|
||||
// 隔离夹具把表建在每次运行专属 schema 里(search_path 指向它),
|
||||
// 因此这里用非限定名,不能写死 public。
|
||||
if err := pool.QueryRow(ctx, `SELECT to_regclass('library_versions') IS NOT NULL`).Scan(&exists); err != nil {
|
||||
t.Fatalf("locate version table: %v", err)
|
||||
}
|
||||
if !exists {
|
||||
rows, err := pool.Query(ctx, `SELECT table_schema||'.'||table_name FROM information_schema.tables
|
||||
WHERE table_name LIKE 'library%' ORDER BY 1`)
|
||||
if err == nil {
|
||||
var names []string
|
||||
for rows.Next() {
|
||||
var n string
|
||||
rows.Scan(&n)
|
||||
names = append(names, n)
|
||||
}
|
||||
rows.Close()
|
||||
t.Logf("library tables visible: %v", names)
|
||||
}
|
||||
var ledger []string
|
||||
if r2, err := pool.Query(ctx, `SELECT version::text FROM schema_migrations ORDER BY 1 DESC LIMIT 5`); err == nil {
|
||||
for r2.Next() {
|
||||
var v string
|
||||
r2.Scan(&v)
|
||||
ledger = append(ledger, v)
|
||||
}
|
||||
r2.Close()
|
||||
}
|
||||
t.Fatalf("版本表不存在:夹具/迁移不完整,本用例的前提不成立(最近迁移=%v)", ledger)
|
||||
}
|
||||
// NOT VALID:不动已有行,只让新的版本写入被真实 PG 拒绝。
|
||||
if _, err := pool.Exec(ctx, `ALTER TABLE library_versions ADD CONSTRAINT r13_anchor_reject CHECK (false) NOT VALID`); err != nil {
|
||||
t.Fatalf("inject anchor constraint: %v", err)
|
||||
}
|
||||
dropConstraint := func() {
|
||||
pool.Exec(context.Background(), `ALTER TABLE library_versions DROP CONSTRAINT IF EXISTS r13_anchor_reject`)
|
||||
}
|
||||
t.Cleanup(dropConstraint)
|
||||
recorder := libraryStageRecorder{store: store, jobID: jobID, scope: scope}
|
||||
// 写集合必须落在一次性目录,不能把测试产物写进仓库。
|
||||
inner := &r13CountingTarget{ledgerTestTarget: ledgerTestTarget{root: t.TempDir()}}
|
||||
backups := ledgerTestBackups{ref: libraryrelease.BackupRef{ID: "backup-" + snapshot, SnapshotID: snapshot,
|
||||
Bytes: 4096, Universe: map[string]libraryrelease.TableFingerprint{"resources": {Count: 1, Digest: "universe"}}}}
|
||||
request := libraryrelease.Request{JobID: jobID, Target: scope, SnapshotID: target, ParamsDigest: params,
|
||||
Now: now, Grant: libraryrelease.Grant{Target: scope, SnapshotID: target, ParamsDigest: params,
|
||||
ExpiresAt: now.Add(time.Hour), AuthorizedBy: "tester"}}
|
||||
outcome := libraryrelease.Run(ctx, request, inner, backups, recorder)
|
||||
if outcome.State != "failed" {
|
||||
t.Fatalf("锚点写失败却继续执行:%+v", outcome)
|
||||
}
|
||||
if outcome.FailedAt != "" {
|
||||
t.Fatalf("锚点失败必须早于任何阶段写入,failed_at=%q", outcome.FailedAt)
|
||||
}
|
||||
if inner.applied != 0 {
|
||||
t.Fatalf("锚点失败后仍写了目标 %d 个阶段", inner.applied)
|
||||
}
|
||||
shown := false
|
||||
for _, sub := range []string{"写集合", "登记", "constraint", "check", "23514"} {
|
||||
if strings.Contains(outcome.Error, sub) {
|
||||
shown = true
|
||||
}
|
||||
}
|
||||
if !shown {
|
||||
t.Fatalf("失败原因没有指向锚点写入:%q", outcome.Error)
|
||||
}
|
||||
versions, err := store.LibraryVersionsBySnapshot(ctx, scope, snapshot)
|
||||
if err != nil {
|
||||
t.Fatalf("read versions: %v", err)
|
||||
}
|
||||
if len(versions) != 0 {
|
||||
t.Fatalf("锚点失败却留下版本证据:%+v", versions)
|
||||
}
|
||||
// 去掉故障后同一请求能完成登记:证明失败来自注入的锚点故障,不是环境坏掉。
|
||||
dropConstraint()
|
||||
retry := libraryrelease.Run(ctx, request, inner, backups, recorder)
|
||||
if retry.State != "succeeded" {
|
||||
t.Fatalf("去掉故障后应当能完成:%+v", retry)
|
||||
}
|
||||
if inner.applied != 4 {
|
||||
t.Fatalf("去掉故障后应写满四个阶段,实际 %d", inner.applied)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
type capturePersistenceFake struct {
|
||||
failAdvance error
|
||||
failStage error
|
||||
advanced []governance.LibraryJobState
|
||||
stages []string
|
||||
}
|
||||
|
||||
func (f *capturePersistenceFake) AdvanceLibraryJob(_ context.Context, _ string, state governance.LibraryJobState,
|
||||
_, _, _, _, _ string, _ time.Time) error {
|
||||
f.advanced = append(f.advanced, state)
|
||||
return f.failAdvance
|
||||
}
|
||||
|
||||
func (f *capturePersistenceFake) UpdateLibraryStage(_ context.Context, _, stage, state string,
|
||||
_, _, _, _ *int64, _ string, _ time.Time) error {
|
||||
f.stages = append(f.stages, stage+"/"+state)
|
||||
return f.failStage
|
||||
}
|
||||
|
||||
// The handler must stop before touching the source when the starting state
|
||||
// cannot be persisted. This is the failure injection that the old log-and-ignore
|
||||
// path hid behind a completed queue row.
|
||||
func TestBeginLibraryCaptureStopsOnAdvanceFailure(t *testing.T) {
|
||||
store := &capturePersistenceFake{failAdvance: errors.New("injected advance failure")}
|
||||
err := beginLibraryCaptureJob(context.Background(), store, "libseal-test", time.Now())
|
||||
if err == nil || !strings.Contains(err.Error(), "cannot be advanced") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobRunning {
|
||||
t.Fatalf("advance calls=%v", store.advanced)
|
||||
}
|
||||
if len(store.stages) != 0 {
|
||||
t.Fatalf("stage was written before advance failure: %v", store.stages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBeginLibraryCaptureStopsOnStageFailure(t *testing.T) {
|
||||
store := &capturePersistenceFake{failStage: errors.New("injected stage failure")}
|
||||
err := beginLibraryCaptureJob(context.Background(), store, "libseal-test", time.Now())
|
||||
if err == nil || !strings.Contains(err.Error(), "stage cannot be advanced") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if len(store.stages) != 1 || store.stages[0] != "export/running" {
|
||||
t.Fatalf("stage calls=%v", store.stages)
|
||||
}
|
||||
}
|
||||
|
||||
// Terminal persistence is two writes; either failure must be returned rather
|
||||
// than turning the queue row into completed while the business job is pending.
|
||||
func TestCompleteLibraryCaptureStopsOnStageFailure(t *testing.T) {
|
||||
store := &capturePersistenceFake{failStage: errors.New("injected success stage failure")}
|
||||
err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now())
|
||||
if err == nil || !strings.Contains(err.Error(), "success stage cannot be persisted") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if len(store.advanced) != 0 {
|
||||
t.Fatalf("advance must not run after stage failure: %v", store.advanced)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteLibraryCaptureStopsOnAdvanceFailure(t *testing.T) {
|
||||
store := &capturePersistenceFake{failAdvance: errors.New("injected success advance failure")}
|
||||
err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now())
|
||||
if err == nil || !strings.Contains(err.Error(), "success terminal cannot be persisted") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if len(store.stages) != 1 || store.stages[0] != "export/succeeded" {
|
||||
t.Fatalf("stage calls=%v", store.stages)
|
||||
}
|
||||
if len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobSucceeded {
|
||||
t.Fatalf("advance calls=%v", store.advanced)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteLibraryCapturePersistsBothFacts(t *testing.T) {
|
||||
store := &capturePersistenceFake{}
|
||||
if err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(store.stages) != 1 || store.stages[0] != "export/succeeded" ||
|
||||
len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobSucceeded {
|
||||
t.Fatalf("stages=%v advanced=%v", store.stages, store.advanced)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/jobs"
|
||||
)
|
||||
|
||||
// R3g: only retained deterministic capture failures become permanent after the
|
||||
// agent has recovered the writer. Cancelled body-index coverage markers are
|
||||
// deliberately absent.
|
||||
func TestRetainedLibraryCaptureFailuresArePermanent(t *testing.T) {
|
||||
for _, marker := range []string{
|
||||
"missing library generated question index",
|
||||
"empty library index content",
|
||||
} {
|
||||
t.Run(marker, func(t *testing.T) {
|
||||
original := errors.New("LIBRARYCTL_FAILED: " + marker + ";已执行 recover 并确认写入者恢复")
|
||||
got := terminalLibraryCaptureError(original)
|
||||
terminal, code := jobs.Terminal(got)
|
||||
if !terminal || code != jobs.ErrCodeValidationFailed {
|
||||
t.Fatalf("retained validation failure must park immediately: terminal=%v code=%q err=%v", terminal, code, got)
|
||||
}
|
||||
if got.Error() != original.Error() {
|
||||
t.Fatalf("original message changed: got %q want %q", got.Error(), original.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCancelledCoverageFailureStaysRetryable(t *testing.T) {
|
||||
for _, marker := range []string{
|
||||
"unverified legacy library chunk index coverage",
|
||||
"missing indexed library chunk",
|
||||
} {
|
||||
err := terminalLibraryCaptureError(errors.New(marker + ";已执行 recover 并确认写入者恢复"))
|
||||
if terminal, code := jobs.Terminal(err); terminal {
|
||||
t.Fatalf("cancelled marker %q must not be terminal; code=%q", marker, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransientLibraryCaptureFailureStaysRetryable(t *testing.T) {
|
||||
for _, message := range []string{
|
||||
"library agent is unreachable",
|
||||
"context deadline exceeded",
|
||||
`LIBRARYCTL_FAILED: job "lib-internal-x" already exists with state "failed";已执行 recover 并确认写入者恢复`,
|
||||
"missing library generated question index; writer restore not confirmed",
|
||||
} {
|
||||
got := terminalLibraryCaptureError(errors.New(message))
|
||||
if terminal, code := jobs.Terminal(got); terminal {
|
||||
t.Fatalf("%q must stay retryable, got terminal code=%q", message, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetainedFailureDoesNotChangeMessage(t *testing.T) {
|
||||
original := errors.New("LIBRARYCTL_FAILED: missing library generated question index;已执行 recover 并确认写入者恢复")
|
||||
got := terminalLibraryCaptureError(original)
|
||||
if !strings.Contains(got.Error(), "missing library generated question index") {
|
||||
t.Fatalf("lost original cause: %v", got)
|
||||
}
|
||||
}
|
||||
@@ -233,8 +233,8 @@ func runLibraryReleaseAttempt(ctx context.Context, writer *libraryAttemptWriter,
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
gateReasons = append(gateReasons, "生产写入闸口关闭")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
gateReasons = append(gateReasons, "没有已核验的生产恢复演练证据")
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(ctx, store, config, payload.Scope); !ok {
|
||||
gateReasons = append(gateReasons, reason)
|
||||
}
|
||||
if len(gateReasons) > 0 {
|
||||
message := "生产" + kindLabel(kind) + "被闸口拒绝:" + strings.Join(gateReasons, ";")
|
||||
@@ -272,7 +272,7 @@ func runLibraryReleaseAttempt(ctx context.Context, writer *libraryAttemptWriter,
|
||||
// 生产索引映射基线:权威来源是当前生效版本(精确查),其次任何一次成功记录的映射。
|
||||
// 只认"生效版本"会在恢复把版本顶替掉之后误判"索引被改动"。
|
||||
knownMapping := knownIndexMapping(ctx, store, payload.Scope)
|
||||
target, backups, wireErr := buildLibraryProductionStack(ctx, config, payload.Scope, payload.TargetSnapshotID, knownMapping)
|
||||
target, backups, wireErr := buildLibraryProductionStack(ctx, config, restoreDispatchGate(config, store.Store), payload.Scope, payload.TargetSnapshotID, knownMapping)
|
||||
if wireErr == nil {
|
||||
if mapping, mapErr := target.Search().MappingSHA256(ctx); mapErr == nil {
|
||||
recorder.mappingDigest = mapping
|
||||
@@ -628,6 +628,10 @@ func resumeInterruptedLibraryJobs(ctx context.Context, store libraryResumeStore,
|
||||
// 基线作业被杀时窗口可能还停着目标:用同一个作业 id 接管那个窗口,
|
||||
// 继续捕获/校验,而不是留下一个 held 目标等人工处理。
|
||||
queueType = libraryBaselineJobType
|
||||
case governance.LibraryJobVerify:
|
||||
// 恢复验证同样持有维护窗口:接管时必须用同一份作业 id 收掉那个窗口,
|
||||
// 否则目标会一直停在维护里而作业显示失败。
|
||||
queueType = libraryVerifyJobType
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]string{"library_job_id": job.ID, "scope": job.Scope,
|
||||
"target_snapshot_id": job.TargetSnapshotID})
|
||||
|
||||
@@ -114,7 +114,7 @@ func runLibraryRecovery(ctx context.Context, store *postgresstore.Store, config
|
||||
claim.RecoveryJobID, claim.RecoveryOperationID = job.ID, recoveryOp.ID
|
||||
ctx = objectgateway.WithClaim(ctx, claim)
|
||||
knownMapping := knownIndexMapping(ctx, store, payload.Scope)
|
||||
target, _, wireErr := buildLibraryProductionStack(ctx, config, payload.Scope, prebackup, knownMapping)
|
||||
target, _, wireErr := buildLibraryProductionStack(ctx, config, restoreDispatchGate(config, store), payload.Scope, prebackup, knownMapping)
|
||||
if wireErr != nil {
|
||||
return fmt.Errorf("恢复目标装配失败:%w", wireErr)
|
||||
}
|
||||
@@ -127,7 +127,10 @@ func runLibraryRecovery(ctx context.Context, store *postgresstore.Store, config
|
||||
defer control.Close()
|
||||
defer coordinator.Close()
|
||||
|
||||
restorer := recoveryRestorer{target: target, expected: prebackup, writeSet: ledger}
|
||||
restorer := recoveryRestorer{target: target, expected: prebackup, writeSet: ledger,
|
||||
// 恢复验证写的是"隔离 canary",不是发布计划:同一 job 的收尾走受限的演练
|
||||
// 恢复入口(同样的身份合同 + 整段回放),不能拿发布计划合同去回放它。
|
||||
drill: windowJob.Kind == governance.LibraryJobVerify}
|
||||
verifier := recoveryVerifier{target: target, expected: prebackup}
|
||||
result, recoverErr := coordinator.RecoverWindow(ctx, payload.WindowJobID, restorer, verifier)
|
||||
now := time.Now().UTC()
|
||||
@@ -171,6 +174,8 @@ type recoveryRestorer struct {
|
||||
target *v071.ProductionLibraryTarget
|
||||
expected string
|
||||
writeSet *libraryrelease.WriteSet
|
||||
// drill 表示窗口属于恢复演练:它的写集合是 canary,不是发布执行计划。
|
||||
drill bool
|
||||
}
|
||||
|
||||
func (r recoveryRestorer) Restore(ctx context.Context, backupID string) error {
|
||||
@@ -183,8 +188,11 @@ func (r recoveryRestorer) Restore(ctx context.Context, backupID string) error {
|
||||
if r.writeSet.BackupSnapshotID != backupID {
|
||||
return fmt.Errorf("写集合记录绑定预备份 %s,不是 %s", r.writeSet.BackupSnapshotID, backupID)
|
||||
}
|
||||
return r.target.Restore(ctx, libraryrelease.BackupRef{ID: "backup-" + backupID, SnapshotID: backupID,
|
||||
WriteSet: r.writeSet})
|
||||
ref := libraryrelease.BackupRef{ID: "backup-" + backupID, SnapshotID: backupID, WriteSet: r.writeSet}
|
||||
if r.drill {
|
||||
return r.target.RestoreDrill(ctx, ref)
|
||||
}
|
||||
return r.target.Restore(ctx, ref)
|
||||
}
|
||||
|
||||
// recoveryVerifier 做真实的内容校验,并把目标当前状态摘要作为"校验过什么"的凭证。
|
||||
|
||||
@@ -30,4 +30,9 @@ func registerLibraryReleaseHandlers(handlers map[string]jobs.Handler, store *pos
|
||||
handlers[libraryBaselineJobType] = admittanceGuarded(admissionErr, func(ctx context.Context, job jobs.Job) error {
|
||||
return runLibraryBaseline(ctx, store, config, job)
|
||||
})
|
||||
// 恢复验证:真实"备份→canary→恢复→复核",通过后写入与当前目标绑定的可恢复证据。
|
||||
// 它是同步闸口的证据来源,所以与上面三类共用同一把执行锁与准入。
|
||||
handlers[libraryVerifyJobType] = admittanceGuarded(admissionErr, func(ctx context.Context, job jobs.Job) error {
|
||||
return runLibraryVerify(ctx, store, config, job)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
// A12:能力默认关闭;只有"配置开启 + 目标身份存在 + 真实治理台账"同时成立才装配。
|
||||
// 配置本身不是独占证明,baseline 装配永远不传该台账。
|
||||
func TestRestoreDispatchGateRequiresExplicitConfiguration(t *testing.T) {
|
||||
store := &postgresstore.Store{}
|
||||
identity := appconfig.Config{LibraryProductionDatabaseURL: "postgres://local@127.0.0.1:5432/target_db",
|
||||
LibraryTargetInstance: "inst", LibraryProductionTenantID: 1, LibraryProductionInternalKB: "kb"}
|
||||
if gate := restoreDispatchGate(identity, store); gate != nil {
|
||||
t.Fatal("gate wired without the explicit switch")
|
||||
}
|
||||
enabled := identity
|
||||
enabled.LibraryRestoreDispatchEnabled = true
|
||||
if gate := restoreDispatchGate(enabled, store); gate == nil {
|
||||
t.Fatal("gate not wired when explicitly enabled")
|
||||
}
|
||||
if gate := restoreDispatchGate(enabled, nil); gate != nil {
|
||||
t.Fatal("gate wired without a governance store")
|
||||
}
|
||||
// 空目标配置仍会得到一个 gate:真实闸门是治理库激活登记(见 store 用例),
|
||||
// 用"配置看起来完整"冒充独占证明才是要避免的。
|
||||
}
|
||||
@@ -15,7 +15,8 @@ import (
|
||||
func TestRegisterLibraryReleaseHandlersCoversOnlyReleaseJobs(t *testing.T) {
|
||||
handlers := map[string]jobs.Handler{}
|
||||
registerLibraryReleaseHandlers(handlers, nil, appconfig.Config{}, nil)
|
||||
want := []string{librarySyncJobType, libraryRestoreJobType, libraryRecoverJobType, libraryBaselineJobType}
|
||||
want := []string{librarySyncJobType, libraryRestoreJobType, libraryRecoverJobType,
|
||||
libraryBaselineJobType, libraryVerifyJobType}
|
||||
if len(handlers) != len(want) {
|
||||
t.Fatalf("registered %d handlers, want %d: %#v", len(handlers), len(want), handlers)
|
||||
}
|
||||
@@ -30,7 +31,8 @@ func TestRegisterLibraryReleaseHandlersCoversOnlyReleaseJobs(t *testing.T) {
|
||||
func TestRegisterLibraryReleaseHandlersFailsClosedWhenNotAdmitted(t *testing.T) {
|
||||
handlers := map[string]jobs.Handler{}
|
||||
registerLibraryReleaseHandlers(handlers, nil, appconfig.Config{}, errNotAdmitted{})
|
||||
for _, jobType := range []string{librarySyncJobType, libraryRestoreJobType, libraryRecoverJobType, libraryBaselineJobType} {
|
||||
for _, jobType := range []string{librarySyncJobType, libraryRestoreJobType, libraryRecoverJobType,
|
||||
libraryBaselineJobType, libraryVerifyJobType} {
|
||||
err := handlers[jobType](context.Background(), jobs.Job{})
|
||||
if err == nil || !strings.Contains(err.Error(), "准入未通过") {
|
||||
t.Fatalf("handler %q error = %v, want admission refusal", jobType, err)
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// fakeStageStore 记录封存失败分支对业务作业行的两次落库调用。
|
||||
type fakeStageStore struct {
|
||||
stageCalls []string
|
||||
jobCalls []string
|
||||
}
|
||||
|
||||
func (f *fakeStageStore) UpdateLibraryStage(_ context.Context, jobID, stage, state string, _, _, _, _ *int64, detail string, _ time.Time) error {
|
||||
f.stageCalls = append(f.stageCalls, jobID+"|"+stage+"|"+state+"|"+detail)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStageStore) AdvanceLibraryJob(_ context.Context, jobID string, state governance.LibraryJobState, stage, message, recovery, _, _ string, _ time.Time) error {
|
||||
f.jobCalls = append(f.jobCalls, jobID+"|"+string(state)+"|"+stage+"|"+message+"|"+recovery)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStageStore) LibraryVersionByRef(context.Context, string, governance.LibraryVersionKind, string, string) (governance.LibraryVersion, bool, error) {
|
||||
return governance.LibraryVersion{}, false, nil
|
||||
}
|
||||
|
||||
func (f *fakeStageStore) RecordLibraryVersion(context.Context, governance.LibraryVersion) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 归档根必须是非符号链接的 0700 目录;t.TempDir 的权限受 umask 影响。
|
||||
func privateDir(t *testing.T) string {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
if err := os.Chmod(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func sourceFor(scope, kb string) governance.LibrarySourceIdentity {
|
||||
return governance.LibrarySourceIdentity{Project: "kgmaint-weknora", App: "kgmaint-app", Scope: scope, TenantID: 1, KnowledgeBaseID: kb}
|
||||
}
|
||||
|
||||
// RED 用例:快照来源与配置不一致时,队列已 failed、页面却永远"导出中"(真实事故:
|
||||
// libseal-lib-internal-20261102T234053 running/export/error 空)。失败必须同时写进
|
||||
// stage 与 job,且原错原样出现在两处与返回值里 —— 不能被"已记录失败"遮住。
|
||||
func TestCapturedSourceMismatchPersistsFailedStageAndJob(t *testing.T) {
|
||||
root := privateDir(t)
|
||||
snapshotID := sealTinyBaseline(t, root, "internal", time.Date(2026, 11, 2, 23, 40, 53, 0, time.UTC))
|
||||
// 清单里是 kb-baseline;worker 配置漂移后拿的是另一个 KB(256 验收遗留正是这样)。
|
||||
source := sourceFor("internal", "0c84a098-d3ed-4b82-a165-e6de9691d6a8")
|
||||
store := &fakeStageStore{}
|
||||
|
||||
err := ensureCapturedSource(context.Background(), store, "libseal-lib-internal-20261102T234053",
|
||||
root, snapshotID, source, time.Now().UTC())
|
||||
|
||||
if err == nil || !strings.Contains(err.Error(), "captured manifest differs from configured source") {
|
||||
t.Fatalf("mismatch must keep the original error, got %v", err)
|
||||
}
|
||||
if len(store.stageCalls) != 1 {
|
||||
t.Fatalf("stage writes = %v, want exactly one failed stage", store.stageCalls)
|
||||
}
|
||||
if !strings.HasPrefix(store.stageCalls[0], "libseal-lib-internal-20261102T234053|export|failed|") ||
|
||||
!strings.Contains(store.stageCalls[0], "captured manifest differs from configured source") {
|
||||
t.Fatalf("stage write = %q, want failed+original error", store.stageCalls[0])
|
||||
}
|
||||
if len(store.jobCalls) != 1 {
|
||||
t.Fatalf("job writes = %v, want exactly one failed job", store.jobCalls)
|
||||
}
|
||||
if !strings.HasPrefix(store.jobCalls[0], "libseal-lib-internal-20261102T234053|failed|export|") ||
|
||||
!strings.Contains(store.jobCalls[0], "captured manifest differs from configured source") {
|
||||
t.Fatalf("job write = %q, want failed+original error", store.jobCalls[0])
|
||||
}
|
||||
// 恢复说明必须指向真实下一步,而不是暗示数据损坏。
|
||||
if !strings.Contains(store.jobCalls[0], "重新封存") {
|
||||
t.Fatalf("job recovery = %q, want a re-seal instruction", store.jobCalls[0])
|
||||
}
|
||||
}
|
||||
|
||||
// 正例对照:身份一致时不得写任何失败,也不得把成功路径变成失败。
|
||||
func TestCapturedSourceMatchStaysSuccessful(t *testing.T) {
|
||||
root := privateDir(t)
|
||||
snapshotID := sealTinyBaseline(t, root, "internal", time.Date(2026, 11, 2, 23, 40, 53, 0, time.UTC))
|
||||
store := &fakeStageStore{}
|
||||
|
||||
if err := ensureCapturedSource(context.Background(), store, "libseal-ok",
|
||||
root, snapshotID, sourceFor("internal", "kb-baseline"), time.Now().UTC()); err != nil {
|
||||
t.Fatalf("matching source rejected: %v", err)
|
||||
}
|
||||
if len(store.stageCalls) != 0 || len(store.jobCalls) != 0 {
|
||||
t.Fatalf("success path wrote failures: stage=%v job=%v", store.stageCalls, store.jobCalls)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
"time"
|
||||
)
|
||||
|
||||
func configuredCaptureSource(config appconfig.Config, scope string) governance.LibrarySourceIdentity {
|
||||
@@ -32,3 +34,20 @@ func verifyCapturedSource(root, snapshotID string, source governance.LibrarySour
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensureCapturedSource 校验快照来源。校验失败必须把失败写进业务行:
|
||||
// 队列侧此时已 failed,只 return err 会让页面永远停在"导出中"
|
||||
// (实际事故:libseal-lib-internal-20261102T234053 running/export/error 空)。
|
||||
func ensureCapturedSource(ctx context.Context, store libraryStageStore, jobID, root, snapshotID string, source governance.LibrarySourceIdentity, now time.Time) error {
|
||||
err := verifyCapturedSource(root, snapshotID, source)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
// 快照本身已产生且字节完好,错的只是 worker 读到的来源身份;因此恢复路径是
|
||||
// "修配置后重新登记/重新封存",不是"重采数据"。原错原样写进 stage、job 与
|
||||
// 返回值:改写成更友好的文案会让真正的配置漂移查不出来。
|
||||
libraryStageRecorder{store: store, jobID: jobID}.Stage(ctx, "export", "failed", err.Error())
|
||||
_ = store.AdvanceLibraryJob(ctx, jobID, governance.LibraryJobFailed, "export", err.Error(),
|
||||
"快照已产生但来源身份与配置不一致,未登记为测试封存;修正来源配置后重新封存", "", "", now)
|
||||
return err
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,262 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
"knowledge-governance/internal/librarymaintenance"
|
||||
"knowledge-governance/internal/libraryverify"
|
||||
"knowledge-governance/internal/pgdsn"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
// libraryVerifyPayload 是"恢复验证"的队列载荷。
|
||||
//
|
||||
// 它只带作业号与 scope:目标(库/索引/桶/实例)来自本进程的部署配置,不接受请求方
|
||||
// 指定。否则一次页面点击就能把"验证"引到别的库上,证据也就证明不了当前目标。
|
||||
type libraryVerifyPayload struct {
|
||||
LibraryJobID string `json:"library_job_id"`
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
|
||||
// runLibraryVerify 在目标实例上真实跑一次恢复验证,并把结果登记成证据。
|
||||
//
|
||||
// 与 sync/restore/baseline 共用同一套准入与代次守卫:它会停写入者、在目标上写 canary
|
||||
// 再按预备份恢复,所以必须走 admitLibraryJob + runOwnedLibraryAttempt,不能绕开。
|
||||
// 执行体本身是 internal/libraryverify——与 cmd/librarydrill 同一份实现,不允许两份。
|
||||
func runLibraryVerify(ctx context.Context, base *postgresstore.Store, config appconfig.Config, job jobs.Job) error {
|
||||
var payload libraryVerifyPayload
|
||||
if err := json.Unmarshal(job.Payload, &payload); err != nil || payload.LibraryJobID == "" ||
|
||||
(payload.Scope != "internal" && payload.Scope != "external") {
|
||||
return errors.New("invalid library verify payload")
|
||||
}
|
||||
if _, admitErr := admitLibraryJob(ctx, base, job, payload.LibraryJobID, payload.Scope,
|
||||
governance.LibraryJobVerify, true); admitErr != nil {
|
||||
return admitErr
|
||||
}
|
||||
return runOwnedLibraryAttempt(ctx, base, config, job, payload.LibraryJobID,
|
||||
func(runCtx context.Context, writer *libraryAttemptWriter) error {
|
||||
return runLibraryVerifyAttempt(runCtx, writer, config, payload)
|
||||
})
|
||||
}
|
||||
|
||||
func runLibraryVerifyAttempt(ctx context.Context, writer *libraryAttemptWriter, config appconfig.Config,
|
||||
payload libraryVerifyPayload) error {
|
||||
store := writer
|
||||
now := time.Now().UTC()
|
||||
recorder := libraryStageRecorder{store: store, jobID: payload.LibraryJobID, scope: payload.Scope,
|
||||
operationID: writer.attempt.OperationID}
|
||||
|
||||
existing, jobErr := store.LibraryJob(ctx, payload.LibraryJobID)
|
||||
if jobErr != nil {
|
||||
// 作业行读不到就没有可登记的终态:直接失败,不落任何证据。
|
||||
return fmt.Errorf("读不到业务作业 %s:%w", payload.LibraryJobID, jobErr)
|
||||
}
|
||||
if existing.State == governance.LibraryJobSucceeded {
|
||||
// 幂等重放:已经成功过的验证不重复停目标、不重复写 canary。
|
||||
recorder.Stage(ctx, "finalize", "skipped", "该恢复验证已成功完成,重复投递不重复执行")
|
||||
return nil
|
||||
}
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
return failLibraryJob(ctx, store, recorder, payload.LibraryJobID,
|
||||
"生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false):恢复验证要停写入者并在目标上写 canary,闸口关闭时不执行",
|
||||
"capture")
|
||||
}
|
||||
_ = store.AdvanceLibraryJob(ctx, payload.LibraryJobID, governance.LibraryJobRunning, "capture",
|
||||
"", "", "", "", now)
|
||||
|
||||
report, runErr := libraryverify.Run(ctx, config, payload.Scope, payload.LibraryJobID,
|
||||
libraryverify.Options{
|
||||
Progress: func(stage, state, detail string) { recorder.Stage(ctx, stage, state, detail) },
|
||||
Governance: verifyGovernance{store: store, config: config},
|
||||
Ledger: recorder,
|
||||
})
|
||||
|
||||
// 证据只由真实执行产生:失败也照实登记 failure 与报告,绝不写成 passed。
|
||||
evidence := governance.LibraryRestoreVerification{
|
||||
ID: "libverify-" + payload.Scope + "-" + payload.LibraryJobID,
|
||||
Scope: payload.Scope,
|
||||
JobID: payload.LibraryJobID,
|
||||
Status: governance.LibraryVerificationFailed,
|
||||
StartedAt: now,
|
||||
}
|
||||
if report != nil {
|
||||
evidence.StartedAt = report.StartedAt
|
||||
evidence.SnapshotID = report.SnapshotID
|
||||
evidence.Report = verificationReportObject(report)
|
||||
}
|
||||
if runErr == nil && report != nil && report.Status == "passed" {
|
||||
evidence.Status = governance.LibraryVerificationPassed
|
||||
evidence.FinishedAt = report.FinishedAt
|
||||
} else {
|
||||
evidence.FinishedAt = time.Now().UTC()
|
||||
if report != nil && report.FinishedAt.After(evidence.StartedAt) {
|
||||
evidence.FinishedAt = report.FinishedAt
|
||||
}
|
||||
switch {
|
||||
case runErr != nil:
|
||||
evidence.Failure = runErr.Error()
|
||||
case report != nil && report.Status != "passed":
|
||||
evidence.Failure = "恢复验证未通过(状态 " + report.Status + ")"
|
||||
default:
|
||||
evidence.Failure = "恢复验证未产生报告"
|
||||
}
|
||||
}
|
||||
if saveErr := store.SaveLibraryRestoreVerification(ctx, evidence); saveErr != nil {
|
||||
// 证据落不了库就不能宣称验证通过:这会把闸口建立在一次没留痕的执行上。
|
||||
return failLibraryJob(ctx, store, recorder, payload.LibraryJobID,
|
||||
"恢复验证执行结束但证据登记失败:"+saveErr.Error(), "finalize")
|
||||
}
|
||||
|
||||
if runErr != nil || evidence.Status != governance.LibraryVerificationPassed {
|
||||
held := report != nil && report.Status == "maintenance_held"
|
||||
message := "恢复验证未通过:" + evidence.Failure
|
||||
if held {
|
||||
message += "(目标仍在维护窗口,入口关闭、写入者已停)"
|
||||
}
|
||||
// 失败出口只写真实结果:报告里的 restore_check 与窗口终态说明目标此刻是
|
||||
// 仍在维护,还是已经按原运行状态安全收尾。不能拿"有快照"冒充"已恢复并复核"。
|
||||
recovery := "未对目标执行任何写入,无需恢复"
|
||||
switch {
|
||||
case held:
|
||||
recovery = "目标仍在维护窗口:在同一作业上执行「按预备份恢复」(library.recover),恢复与逐类校验通过后才会开入口并释放占用"
|
||||
case report != nil && report.WindowState != "":
|
||||
recovery = "维护窗口已按原运行状态收尾(window=" + report.WindowState + "):目标内容未写入或已恢复并复核,无需再恢复"
|
||||
}
|
||||
snapshot := ""
|
||||
if report != nil {
|
||||
snapshot = report.SnapshotID
|
||||
}
|
||||
recorder.Stage(ctx, "finalize", "failed", message)
|
||||
_ = store.AdvanceLibraryJob(ctx, payload.LibraryJobID, governance.LibraryJobFailed, "finalize",
|
||||
message, recovery, snapshot, snapshot, time.Now().UTC())
|
||||
return errors.New(message)
|
||||
}
|
||||
|
||||
finishedAt := evidence.FinishedAt
|
||||
if finishedAt.IsZero() {
|
||||
finishedAt = time.Now().UTC()
|
||||
}
|
||||
recorder.Stage(ctx, "finalize", "succeeded",
|
||||
"恢复验证通过:快照 "+evidence.SnapshotID+" 的预备份可真实恢复,证据已按当前目标登记")
|
||||
_ = store.AdvanceLibraryJob(ctx, payload.LibraryJobID, governance.LibraryJobSucceeded, "finalize",
|
||||
"", "", evidence.SnapshotID, evidence.SnapshotID, finishedAt)
|
||||
log.Printf("library verify %s passed target snapshot=%s", payload.LibraryJobID, evidence.SnapshotID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// verifyGovernance 把演练接进**既有**维护生命周期:实例行 + scope 行都由 worker 的
|
||||
// 真实 attempt 写入,入口状态与状态文字来自实际执行。页面读的维护接口只认这些行,
|
||||
// 所以只写磁盘 journal 的窗口在 UI 上等于不存在(审核 B2 的真实缺口)。
|
||||
type verifyGovernance struct {
|
||||
store *libraryAttemptWriter
|
||||
config appconfig.Config
|
||||
}
|
||||
|
||||
func (g verifyGovernance) targetIdentity(scope string) string {
|
||||
return fmt.Sprintf("scope=%s db=%s index=%s bucket=%s", scope,
|
||||
pgdsn.Endpoint(g.config.LibraryProductionDatabaseURL), g.config.LibraryProductionIndex, g.config.LibraryProductionS3Bucket)
|
||||
}
|
||||
|
||||
func (g verifyGovernance) Acquire(ctx context.Context, jobID, scope string, target libraryverify.TargetIdentity, originalServiceState string) error {
|
||||
now := time.Now().UTC()
|
||||
note := "实例级维护:关闭目标入口并停止全部写入者,恢复验证完成并复核后开放"
|
||||
if _, err := g.store.AcquireInstanceMaintenance(ctx, postgresstore.LibraryInstanceMaintenance{
|
||||
Instance: target.Instance, OwnerJobID: jobID, OwnerScope: scope,
|
||||
TargetFingerprint: target.Fingerprint, Level: librarymaintenance.LevelInstance,
|
||||
Services: target.Writers, IngressState: "unknown", State: "acquired",
|
||||
OriginalServiceState: originalServiceState, Note: note,
|
||||
}, now); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := g.store.AcquireLibraryMaintenance(ctx, postgresstore.LibraryMaintenance{
|
||||
Scope: scope, OwnerJobID: jobID, TargetIdentity: g.targetIdentity(scope),
|
||||
Level: librarymaintenance.LevelInstance, TargetFingerprint: target.Fingerprint,
|
||||
Services: target.Writers, IngressState: "unknown", State: "acquired",
|
||||
OriginalServiceState: originalServiceState, Note: note,
|
||||
}, now); err != nil {
|
||||
// scope 行没拿到时目标还没被停用:释放实例占用,避免留下一个空窗口。
|
||||
_ = g.store.ReleaseInstanceMaintenance(ctx, target.Instance, jobID, now)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g verifyGovernance) Ingress(ctx context.Context, jobID, scope, state, note string) error {
|
||||
now := time.Now().UTC()
|
||||
if err := g.store.SetLibraryMaintenanceIngress(ctx, scope, jobID, state, note, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.store.SetInstanceMaintenanceIngress(ctx, g.config.LibraryTargetInstance, jobID, state, note, now)
|
||||
}
|
||||
|
||||
func (g verifyGovernance) State(ctx context.Context, jobID, scope, state, note string) error {
|
||||
now := time.Now().UTC()
|
||||
if err := g.store.UpdateLibraryMaintenanceState(ctx, scope, jobID, state, note, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.store.UpdateInstanceMaintenanceState(ctx, g.config.LibraryTargetInstance, jobID, state, note, now)
|
||||
}
|
||||
|
||||
func (g verifyGovernance) Release(ctx context.Context, jobID, scope, note string) error {
|
||||
now := time.Now().UTC()
|
||||
if err := g.store.ReleaseLibraryMaintenance(ctx, scope, jobID, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.store.ReleaseInstanceMaintenance(ctx, g.config.LibraryTargetInstance, jobID, now)
|
||||
}
|
||||
|
||||
// verificationReportObject 把报告转成可落库的 JSON 对象(保留 canary/漂移校验等证据字段)。
|
||||
func verificationReportObject(report *libraryverify.Report) map[string]any {
|
||||
if report == nil {
|
||||
return nil
|
||||
}
|
||||
raw, err := json.Marshal(report)
|
||||
if err != nil {
|
||||
return map[string]any{"encode_error": err.Error()}
|
||||
}
|
||||
var object map[string]any
|
||||
if err := json.Unmarshal(raw, &object); err != nil {
|
||||
return map[string]any{"encode_error": err.Error()}
|
||||
}
|
||||
return object
|
||||
}
|
||||
|
||||
// libraryRestoreEvidenceSatisfied 回答"当前目标 + scope 有没有可支撑写生产的恢复证据"。
|
||||
//
|
||||
// 证据是第一来源:library_restore_verifications 按当前 target_id 读取,换库/换索引/换桶
|
||||
// 之后旧证据自动失效,不需要谁记得关开关。历史部署里的 PRODUCTION_BACKUP_RESTORE_VERIFIED
|
||||
// 仍作为显式覆盖保留(不删除旧部署能力),但只允许在**明确没有证据行**时兜底;
|
||||
// 明确 failed/invalid 与读取错误一律拒绝(审核 B3:不能把"不知道"当"满足")。
|
||||
func libraryRestoreEvidenceSatisfied(ctx context.Context, store interface {
|
||||
LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error)
|
||||
}, config appconfig.Config, scope string) (bool, string) {
|
||||
// 存储没有证据能力(nil/迁移前形状)等同于"没有证据行":历史 env 覆盖可兜底;
|
||||
// 但只要读得出证据行,failed/invalid 与读取错误都优先拒绝。
|
||||
missing := true
|
||||
if store != nil {
|
||||
evidence, found, err := store.LatestLibraryRestoreVerification(ctx, scope)
|
||||
switch {
|
||||
case err != nil:
|
||||
return false, "读取恢复验证证据失败:" + err.Error()
|
||||
case found:
|
||||
missing = false
|
||||
if ok, reason := governance.LibraryVerificationSatisfied(evidence, scope); ok {
|
||||
return true, ""
|
||||
} else {
|
||||
return false, reason
|
||||
}
|
||||
}
|
||||
}
|
||||
if missing && config.ProductionBackupRestoreVerified {
|
||||
return true, ""
|
||||
}
|
||||
return false, "当前目标还没有恢复验证证据:先跑一次恢复验证(备份 → canary → 恢复 → 复核)"
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// 同步闸口的"恢复可验证"判断必须以当前目标的证据为准:
|
||||
// 没有证据 ⇒ 拒绝并说清怎么补;有 passed 证据 ⇒ 放行;只有失败证据 ⇒ 拒绝并带失败原因。
|
||||
// 历史 env 覆盖只在证据缺失时兜底(页面不再写它)。
|
||||
type verifyEvidenceStore struct {
|
||||
evidence governance.LibraryRestoreVerification
|
||||
found bool
|
||||
}
|
||||
|
||||
func (s verifyEvidenceStore) LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
return s.evidence, s.found, nil
|
||||
}
|
||||
|
||||
// 审核 B3:读取失败不能被 env=true 洗成"满足"。
|
||||
type unreadableEvidenceStore struct{}
|
||||
|
||||
func (unreadableEvidenceStore) LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
return governance.LibraryRestoreVerification{}, false, errors.New("evidence lookup failed")
|
||||
}
|
||||
|
||||
func TestWorkerUnreadableEvidenceBeatsEnv(t *testing.T) {
|
||||
ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), unreadableEvidenceStore{},
|
||||
appconfig.Config{ProductionBackupRestoreVerified: true}, "internal")
|
||||
if ok {
|
||||
t.Fatalf("unreadable evidence must block even with env=true; reason=%q", reason)
|
||||
}
|
||||
if !strings.Contains(reason, "读取恢复验证证据失败") {
|
||||
t.Fatalf("reason=%q", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryRestoreEvidenceSatisfied(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
passed := verifyEvidenceStore{found: true, evidence: governance.LibraryRestoreVerification{
|
||||
Scope: "internal", Status: governance.LibraryVerificationPassed, SnapshotID: "snap-1", FinishedAt: now}}
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), passed, appconfig.Config{}, "internal"); !ok {
|
||||
t.Fatalf("passed evidence must satisfy gate: %q", reason)
|
||||
}
|
||||
failed := verifyEvidenceStore{found: true, evidence: governance.LibraryRestoreVerification{
|
||||
Scope: "internal", Status: governance.LibraryVerificationFailed, SnapshotID: "snap-1",
|
||||
Failure: "恢复后内容仍与预备份不一致", FinishedAt: now}}
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), failed, appconfig.Config{}, "internal"); ok ||
|
||||
!strings.Contains(reason, "恢复后内容仍与预备份不一致") {
|
||||
t.Fatalf("failed evidence must block with its own failure: ok=%v reason=%q", ok, reason)
|
||||
}
|
||||
missing := verifyEvidenceStore{}
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), missing, appconfig.Config{}, "internal"); ok ||
|
||||
!strings.Contains(reason, "恢复验证") {
|
||||
t.Fatalf("missing evidence must block and point at the remedy: ok=%v reason=%q", ok, reason)
|
||||
}
|
||||
// 历史部署的 env 覆盖:证据缺失时才生效,证据存在时不再起作用。
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), missing,
|
||||
appconfig.Config{ProductionBackupRestoreVerified: true}, "internal"); !ok {
|
||||
t.Fatalf("legacy env override must still allow when no evidence: %q", reason)
|
||||
}
|
||||
if ok, _ := libraryRestoreEvidenceSatisfied(context.Background(), failed,
|
||||
appconfig.Config{ProductionBackupRestoreVerified: true}, "internal"); ok {
|
||||
t.Fatal("evidence must win over the legacy env override")
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,9 @@ import (
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/librarymaintenance"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
"knowledge-governance/internal/librarystack"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
v071 "knowledge-governance/internal/releaseadapter/v071"
|
||||
)
|
||||
|
||||
@@ -18,13 +20,27 @@ func openLibraryMaintenance(config appconfig.Config) (*librarymaintenance.Manage
|
||||
return librarystack.OpenMaintenance(config)
|
||||
}
|
||||
|
||||
// restoreDispatchGate 只在部署显式开启时才把治理台账交给目标。
|
||||
//
|
||||
// 为什么不能只信配置:配置为 true 只说明"允许装配",不证明旧 writer 已退出;
|
||||
// 真正的发放许可还要求治理库里对该目标存在未撤销的激活登记(见迁移 78)。
|
||||
func restoreDispatchGate(config appconfig.Config, store *postgresstore.Store) libraryrelease.RestoreMarkerGate {
|
||||
// 只判断"是否允许装配":目标身份与独占证明由治理台账的激活登记回答,
|
||||
// 这里再用一个空配置也能算出来的摘要去当门槛只会制造虚假的严格。
|
||||
if !config.LibraryRestoreDispatchEnabled || store == nil {
|
||||
return nil
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
// buildLibraryProductionStack 用"要同步的这份快照"做契约装配目标与预备份存储。
|
||||
func buildLibraryProductionStack(ctx context.Context, config appconfig.Config, scope, snapshotID, knownMapping string) (
|
||||
*v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) {
|
||||
return librarystack.OpenProductionStack(ctx, config, scope, snapshotID, knownMapping)
|
||||
func buildLibraryProductionStack(ctx context.Context, config appconfig.Config, gate libraryrelease.RestoreMarkerGate,
|
||||
scope, snapshotID, knownMapping string) (*v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) {
|
||||
return librarystack.OpenProductionStack(ctx, config, scope, snapshotID, knownMapping, gate)
|
||||
}
|
||||
|
||||
// buildLibraryBaselineStack 装配"给目标自己建基线"所需的目标与捕获器(契约取目标当前映射)。
|
||||
// 基线不参与发布/恢复派发,永远不传派发台账:只读/建基线不得获得恢复写许可。
|
||||
func buildLibraryBaselineStack(ctx context.Context, config appconfig.Config, scope string) (
|
||||
*v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) {
|
||||
return librarystack.OpenBaselineStack(ctx, config, scope)
|
||||
|
||||
@@ -135,9 +135,7 @@ func libraryQueueOwner(workerID string, job jobs.Job, libraryJobID string) strin
|
||||
if queueID == "" {
|
||||
queueID = libraryJobID
|
||||
}
|
||||
worker := workerID
|
||||
if worker == "" {
|
||||
worker = "worker"
|
||||
}
|
||||
return fmt.Sprintf("%s#%s#%d", worker, queueID, job.Attempt)
|
||||
// 合成规则只有一份(governance.LibraryDeliveryOwner):派发台账要按同一格式核
|
||||
// "这次许可属于哪次真实投递"。
|
||||
return governance.LibraryDeliveryOwner(workerID, queueID, job.Attempt)
|
||||
}
|
||||
|
||||
+90
-23
@@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -23,7 +24,9 @@ import (
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
"knowledge-governance/internal/platform/weknora"
|
||||
"knowledge-governance/internal/readiness"
|
||||
"knowledge-governance/internal/versionhistory"
|
||||
"knowledge-governance/internal/weknoraaccess"
|
||||
artifactfetch "knowledge-governance/internal/workflows/artifactfetch"
|
||||
catalogrefresh "knowledge-governance/internal/workflows/catalogrefresh"
|
||||
"knowledge-governance/internal/workflows/sourcescan"
|
||||
@@ -98,6 +101,27 @@ func stageSizesFrom(manifest librarysnapshot.Manifest) map[string]stageSize {
|
||||
// libraryCaptureJobType is shared with the API which enqueues it.
|
||||
const libraryCaptureJobType = "library.capture"
|
||||
|
||||
// Retained deterministic source-content failures: the agent has already
|
||||
// restored the writer, so retrying the same journal only replaces the first
|
||||
// cause with "already exists". Body-index coverage markers are intentionally
|
||||
// absent; that inferred rule was removed in R3f.
|
||||
var terminalLibraryCaptureMarkers = []string{
|
||||
"missing library generated question index",
|
||||
"empty library index content",
|
||||
}
|
||||
|
||||
func terminalLibraryCaptureError(err error) error {
|
||||
if err == nil || !strings.Contains(err.Error(), "已执行 recover 并确认写入者恢复") {
|
||||
return err
|
||||
}
|
||||
for _, marker := range terminalLibraryCaptureMarkers {
|
||||
if strings.Contains(err.Error(), marker) {
|
||||
return jobs.Permanent(jobs.ErrCodeValidationFailed, err)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// librarySyncJobType 是生产同步作业的队列类型;与封存严格分开,状态不共用。
|
||||
const librarySyncJobType = "library.sync"
|
||||
|
||||
@@ -110,6 +134,10 @@ const libraryRestoreJobType = "library.restore"
|
||||
// libraryBaselineJobType 是"建立目标权威基线"的队列类型(与 API 侧常量同值)。
|
||||
const libraryBaselineJobType = "library.baseline"
|
||||
|
||||
// libraryVerifyJobType 是"恢复验证"的队列类型:真实备份→canary→恢复→复核,
|
||||
// 通过后才会产生同步闸口认的恢复证据。与 API 侧常量同值。
|
||||
const libraryVerifyJobType = "library.verify"
|
||||
|
||||
func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
@@ -142,7 +170,8 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
} else if len(pending) > 0 {
|
||||
return fmt.Errorf("治理库 schema 落后(待应用迁移 %v):本角色不拥有 DDL,先由控制面进程完成迁移", pending)
|
||||
}
|
||||
store := postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity())
|
||||
store := postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity()).WithRoutingDefaults(config.InternalRoutingKnowledgeBaseID, config.ExternalRoutingKnowledgeBaseID).
|
||||
WithRestoreDispatchEvidence(config.LibraryTargetJournalRoot)
|
||||
log.Printf("library target evidence identity=%s", config.LibraryTargetIdentity())
|
||||
// 启动先接管被中断的写作业(复用已持久化的预备份),再把剩下的孤儿判失败:
|
||||
// 顺序反了会把可接管的作业先标失败,丢掉已完成的备份。
|
||||
@@ -259,6 +288,41 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
return worker.Run(ctx, config.PollInterval)
|
||||
}
|
||||
|
||||
type libraryCapturePersistence interface {
|
||||
UpdateLibraryStage(ctx context.Context, jobID, stage, state string, total, completed, bytesTotal, bytesDone *int64, detail string, now time.Time) error
|
||||
AdvanceLibraryJob(ctx context.Context, jobID string, state governance.LibraryJobState, stage, message, recovery, snapshotID, targetSnapshotID string, now time.Time) error
|
||||
}
|
||||
|
||||
// beginLibraryCaptureJob persists the starting state before any source
|
||||
// operation. If either write fails the handler must return, so the queue cannot
|
||||
// be reported completed while the business job is still pending.
|
||||
func beginLibraryCaptureJob(ctx context.Context, store libraryCapturePersistence, jobID string, now time.Time) error {
|
||||
if err := store.AdvanceLibraryJob(ctx, jobID, governance.LibraryJobRunning, "export", "", "", "", "", now); err != nil {
|
||||
return fmt.Errorf("library seal job %s cannot be advanced: %w", jobID, err)
|
||||
}
|
||||
if err := store.UpdateLibraryStage(ctx, jobID, "export", "running", nil, nil, nil, nil,
|
||||
"主机侧正在暂停写入者、导出并恢复", now); err != nil {
|
||||
return fmt.Errorf("library seal job %s stage cannot be advanced: %w", jobID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// completeLibraryCaptureJob persists both terminal facts. A failed write must
|
||||
// be returned (queue failed), not logged and ignored; the capture journal keeps
|
||||
// replay idempotent, and the admin reconcile path can repair only after the
|
||||
// exact queue/version/archive evidence is present.
|
||||
func completeLibraryCaptureJob(ctx context.Context, store libraryCapturePersistence, jobID, snapshotID string, now time.Time) error {
|
||||
if err := store.UpdateLibraryStage(ctx, jobID, "export", "succeeded", nil, nil, nil, nil,
|
||||
"已封存快照 "+snapshotID, now); err != nil {
|
||||
return fmt.Errorf("library seal job %s success stage cannot be persisted: %w", jobID, err)
|
||||
}
|
||||
if err := store.AdvanceLibraryJob(ctx, jobID, governance.LibraryJobSucceeded, "export", "", "",
|
||||
snapshotID, "", now); err != nil {
|
||||
return fmt.Errorf("library seal job %s success terminal cannot be persisted: %w", jobID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[string]jobs.Handler, error) {
|
||||
handlers := map[string]jobs.Handler{
|
||||
"demo": func(_ context.Context, job jobs.Job) error {
|
||||
@@ -320,12 +384,8 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
// 持久作业:页面读的是这里写的阶段与结果,worker 重启后仍能如实展示。
|
||||
libraryJobID := "libseal-" + payload.CaptureJob
|
||||
startedAt := time.Now().UTC()
|
||||
if advanceErr := store.AdvanceLibraryJob(ctx, libraryJobID, governance.LibraryJobRunning, "export", "", "", "", "", startedAt); advanceErr != nil {
|
||||
log.Printf("library seal job %s cannot be advanced: %v", libraryJobID, advanceErr)
|
||||
}
|
||||
if stageErr := store.UpdateLibraryStage(ctx, libraryJobID, "export", "running", nil, nil, nil, nil,
|
||||
"主机侧正在暂停写入者、导出并恢复", startedAt); stageErr != nil {
|
||||
log.Printf("library seal job %s stage update failed: %v", libraryJobID, stageErr)
|
||||
if beginErr := beginLibraryCaptureJob(ctx, store, libraryJobID, startedAt); beginErr != nil {
|
||||
return beginErr
|
||||
}
|
||||
// 平台审计过的精确死信解除指纹:读取失败必须阻断(fail closed),
|
||||
// 不能因为存储不可用就静默按"没有解除"或"全部解除"处理。
|
||||
@@ -357,7 +417,7 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
_ = store.AdvanceLibraryJob(ctx, libraryJobID, governance.LibraryJobFailed, "export", err.Error(),
|
||||
"agent 已执行恢复检查;确认来源 healthy 后可重试同一作业号", "", "", finishedAt)
|
||||
log.Printf("library capture failed scope=%s capture_job=%s: %v", payload.Scope, payload.CaptureJob, err)
|
||||
return err
|
||||
return terminalLibraryCaptureError(err)
|
||||
}
|
||||
// 执行后复核:结果里 agent 自报的身份必须还是期望实例,否则这次封存
|
||||
// 可能来自别的栈,绝不能登记成"本实例的测试封存"。
|
||||
@@ -367,7 +427,7 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
"快照已产生但来源身份不符,未登记为测试封存;核对执行器后重新封存", "", "", finishedAt)
|
||||
return identityErr
|
||||
}
|
||||
if err := verifyCapturedSource(config.LibraryArchiveRoot, result.SnapshotID, source); err != nil {
|
||||
if err := ensureCapturedSource(ctx, store, libraryJobID, config.LibraryArchiveRoot, result.SnapshotID, source, finishedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
// 封存产物成版本记录:只登记"测试封存",绝不写成生产生效版本。
|
||||
@@ -383,10 +443,9 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
if versionErr != nil {
|
||||
return fmt.Errorf("library seal job %s cannot record version: %w", libraryJobID, versionErr)
|
||||
}
|
||||
_ = store.UpdateLibraryStage(ctx, libraryJobID, "export", "succeeded", nil, nil, nil, nil,
|
||||
"已封存快照 "+result.SnapshotID, finishedAt)
|
||||
_ = store.AdvanceLibraryJob(ctx, libraryJobID, governance.LibraryJobSucceeded, "export", "", "",
|
||||
result.SnapshotID, "", finishedAt)
|
||||
if completeErr := completeLibraryCaptureJob(ctx, store, libraryJobID, result.SnapshotID, finishedAt); completeErr != nil {
|
||||
return completeErr
|
||||
}
|
||||
log.Printf("library capture completed scope=%s capture_job=%s snapshot=%s replay=%v requested_by=%s",
|
||||
payload.Scope, payload.CaptureJob, result.SnapshotID, result.Replay, payload.RequestedBy)
|
||||
return nil
|
||||
@@ -466,19 +525,27 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
if !config.TestWeKnoraEnabled || !config.LibraryWorkerRole.ExecutesSourceWork() {
|
||||
return handlers, nil
|
||||
}
|
||||
apiKey, err := weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile)
|
||||
if err != nil {
|
||||
// 启动只验证凭据“存在”(缺失像以前一样拒绝启动);具体值仍由 resolver 每次调用重读。
|
||||
if _, err := weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile); err != nil {
|
||||
return nil, fmt.Errorf("read test WeKnora API key: %w", err)
|
||||
}
|
||||
client, err := weknora.NewClient(weknora.ClientConfig{
|
||||
BaseURL: config.TestWeKnoraBaseURL,
|
||||
APIKey: apiKey,
|
||||
Timeout: config.TestWeKnoraHTTPTimeout,
|
||||
AllowDockerHTTP: config.TestWeKnoraAllowDockerHTTP,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create test WeKnora client: %w", err)
|
||||
// 与 API 探测共用同一有效目标:网页保存的端点优先、env 只作未配置兜底;
|
||||
// 当前持久化绑定优先、env 兜底;凭据每次调用重读同一 key 文件。
|
||||
remoteResolver := readiness.Resolver{
|
||||
Endpoint: func(resolveCtx context.Context) string {
|
||||
if settings, settingsErr := store.GetActiveUISettings(resolveCtx); settingsErr == nil && strings.TrimSpace(settings.TestWeKnoraBaseURL) != "" {
|
||||
return settings.TestWeKnoraBaseURL
|
||||
}
|
||||
return config.TestWeKnoraBaseURL
|
||||
},
|
||||
LoadBinding: func(resolveCtx context.Context) (string, error) {
|
||||
internal, _, _, bindingErr := store.LoadRoutingKBBindings(resolveCtx)
|
||||
return internal, bindingErr
|
||||
},
|
||||
ReadKey: func() (string, error) { return weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile) },
|
||||
DefaultKB: config.InternalRoutingKnowledgeBaseID,
|
||||
}
|
||||
client := &weknoraaccess.Client{Resolver: remoteResolver, Timeout: config.TestWeKnoraHTTPTimeout, AllowDockerHTTP: config.TestWeKnoraAllowDockerHTTP}
|
||||
upload, err := testweknora.NewUploadHandler(testweknora.UploadHandlerConfig{
|
||||
Store: store,
|
||||
Client: client,
|
||||
|
||||
+6
-1
@@ -18,4 +18,9 @@
|
||||
|
||||
1. 跑 `make verify`(本地全绿不等于远端跑通,需要一次真实 run 的证据)。
|
||||
2. 把**使用者可见**的变化写成 `CHANGELOG.md` 的 `[Unreleased]` 条目(按四类,别写过程)。
|
||||
3. 从本文件划掉该项;过程记录归档到 `docs/archive/`。
|
||||
3. 从本文件划掉该项;过程记录归档到 `docs/archive/`。
|
||||
## 资源首次复用的归属审计(后续)
|
||||
|
||||
- 当前首次复用只依赖“目标行逐列一致 + 对象字节一致”,不再要求本目标历史成功同步/预备份来源。
|
||||
- 后续可补:资源首次写入者/首次绑定 KB、跨 KB 引用计数、孤儿资源与同 tenant 共享资源的可审计归属视图;
|
||||
不以此重新阻断正常的同内容首次复用。
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
# DS01 激活证据的不变量(v2)
|
||||
|
||||
状态:源码与隔离测试已实现;**未在正式治理库执行激活**。
|
||||
相关实现:`internal/libraryrelease/restore_dispatch_activation.go`、
|
||||
`internal/platform/postgres/library_restore_dispatch_activation.go`、
|
||||
`internal/libraryfreeze/journal_evidence.go`。
|
||||
|
||||
## 1. 当前生产装配从哪里取得可核证据
|
||||
|
||||
```
|
||||
worker(LIBRARY_RESTORE_DISPATCH_V2=true)
|
||||
└─ librarymaintenance.Manager(LIBRARY_TARGET_*)
|
||||
└─ libraryfreeze.Coordinator
|
||||
├─ 关闭入口 → 读 gate drain.json(closed / drained / active_at_close)
|
||||
├─ 等后台静默 → QuiesceProof(depth/pending_ops/unsettled=0,稳定窗口)
|
||||
├─ 停写者 → Controller.Stop + Inventory 复核 + 退出码
|
||||
└─ 写 <LIBRARY_TARGET_JOURNAL_ROOT>/<job>.json(state=held)
|
||||
└─ library_instance_maintenance(治理库):instance / owner_job_id / target_fingerprint /
|
||||
level=instance / services / state / released_at
|
||||
```
|
||||
|
||||
激活是**部署动作**,不是业务请求:`Store.ActivateRestoreDispatch` 只在部署流程里调用。
|
||||
`Store.WithRestoreDispatchEvidence(LIBRARY_TARGET_JOURNAL_ROOT)` 是唯一允许读取 journal 的根;
|
||||
未配置该根 ⇒ `ErrRestoreDispatchActivationDisabled`(能力关闭),不退回"只看字符串"。
|
||||
|
||||
## 2. 证据文件只声明身份,不声明结论
|
||||
|
||||
v2 证据文档(`protocol_version=2`)字段。注意:台账行 `library_index_restore_dispatch_activation.protocol_version`
|
||||
仍是 1(迁移 78 的派发协议身份约束),证据文档版本与台账协议版本不是同一个数字:
|
||||
|
||||
| 字段 | 含义 |
|
||||
|---|---|
|
||||
| `target_identity` | 目标身份 |
|
||||
| `target_fingerprint` | 维护窗口/部署目标指纹(64 位 sha256 hex),必须等于 journal 与维护行 |
|
||||
| `restore_target_fingerprint` | 激活时对恢复目标(库/索引/桶)的实时观察指纹 |
|
||||
| `target_instance` | 治理库维护窗口的实例名,必须等于 journal 的 `source` |
|
||||
| `governance_database` | 当前治理库名,与 `current_database()` 比对 |
|
||||
| `protocol_build` | 必须是**运行中二进制**的 `version.DisplayVersion()` |
|
||||
| `maintenance_job` / `maintenance_journal` | 维护窗口作业;journal 名必须恰好是 `<job>.json` |
|
||||
| `maintenance_journal_sha256` | 磁盘 journal 字节的 sha256(不是文件内自报值) |
|
||||
| `writers[] {name, kind, instance_id}` | 旧 writer 集合:名字对治理库 `services`,实例 ID 对 journal `services[].id` |
|
||||
|
||||
**没有** `stopped` / `queue_drained` / `exited_at` / `drain_evidence` 这些调用方布尔字段。
|
||||
"已退出、已排空"只能从 journal 事实与治理库行推出来。
|
||||
|
||||
## 3. 激活门逐项不变量
|
||||
|
||||
- **A1 来源**:journal 由 `libraryfreeze` 写、位于配置的私有根(目录 0700、非符号链接);
|
||||
文件以 `O_NOFOLLOW` 打开、≤1MiB、严格 JSON(未知字段拒绝)。
|
||||
- **A2 摘要**:磁盘字节 sha256 == 证据声明值;写入台账的 `drain_evidence_digest` 是这个值。
|
||||
- **A3 窗口状态**:journal `state=held`、`level=instance`;已 `recovered`/`released` 的窗口
|
||||
不能证明"旧 writer 仍停着"。
|
||||
- **A4 实例绑定**:journal `source` == 证据 `target_instance`,且 == 治理库
|
||||
`library_instance_maintenance.instance`。
|
||||
- **A5 作业绑定**:journal `job` == 证据 `maintenance_job` == 维护行 `owner_job_id`。
|
||||
- **A6 目标绑定**:journal `target_fingerprint` == 证据 `target_fingerprint` == 维护行指纹;
|
||||
证据 `restore_target_fingerprint` == 调用方激活时对恢复目标的只读观察值。两者是
|
||||
不同层(实例/部署 vs 内容/索引/桶身份),不能用一个值互相顶替。
|
||||
- **A7 入口排空**:`ingress_closed=true`、`drain_verified=true`、gate `closed && drained &&
|
||||
active_at_close==0`。
|
||||
- **A8 后台静默**:`quiesce` 存在且 depth/pending_ops/unsettled 全 0、有合法 digest、
|
||||
停写后复核(`post_stop_at ≥ proven_at`、`post_stop_depth=0`、`post_stop_digest=digest`)。
|
||||
- **A9 writer 集合**:证据 writer 名字集合 == 维护行 `services`;实例 ID 集合 == journal
|
||||
`services[].id`;每个 writer `stop_requested=true` 且有 `exit_code`;数量必须一致。
|
||||
- **A10 有效性**:停写/排空观察(`quiesce.post_stop_at`)在 2 小时有效期内;
|
||||
证据 `attested_at` 不早于该观察、不晚于未来 5 分钟;窗口 `acquired_at` 早于观察。
|
||||
- **A11 现场身份**:`current_database()` 等于证据库名;运行构建必须已注入身份且等于
|
||||
`protocol_build`;调用方传入的目标指纹必须等于证据指纹。
|
||||
- **A12 失败关闭**:任一不满足 ⇒ 不写 activation 行;已撤销行的终局性仍由迁移 78 的
|
||||
trigger 保证(`revoked_at→NULL`、删行、状态倒退都拒绝)。
|
||||
|
||||
## 4. 它不证明什么
|
||||
|
||||
- 不独立观测"外部 writer 进程此刻不在运行";A7–A9 是维护链自己的受控观察
|
||||
(入口 drain、退出码、后台静默),不是通用进程扫描。
|
||||
- 不提供 ES 端 fence,也不保证已 `dispatching` 的旧 owner 不会迟到一次。
|
||||
- 不抵抗超级管理员禁用 trigger 或恢复旧 PG 快照。
|
||||
|
||||
## 5. 测试如何证明它不是"验字符串"
|
||||
|
||||
反例(真 PG,见 `library_restore_dispatch_linearization_test.go`):伪造 journal 摘要、
|
||||
摘要计算后替换真实文件、错 writer 名字/实例、无退出码、从未要求停、入口未排空
|
||||
(`drained=false` / `active_at_close=1` / `drain_verified=false`)、停写观察过期、
|
||||
证据过期、治理库身份不符、现场指纹不符、构建不符、维护窗口已释放、作业不属于窗口、
|
||||
journal 已 recovered、未配置受信根。全部要求零 activation 行;正例才产生 1 行,并把
|
||||
已核事实(journal 摘要、gate、quiesce、每个 writer 的退出码)写进 `evidence.verified`。
|
||||
@@ -0,0 +1,90 @@
|
||||
# DS01:删除型恢复的派发台账(实现说明)
|
||||
|
||||
状态:源码与隔离测试已实现;**未在 8088 / 正式治理库 / 业务 WeKnora 上执行 DDL 或激活**。
|
||||
设计正本:`astra-ds01-design.md`。有限验收表:握手目录 `ds01-acceptance-freeze.md`。
|
||||
|
||||
## 它证明什么、不证明什么
|
||||
|
||||
证明:对一个固定的删除型恢复 effect(`Before` 非空、`After` 空,恢复写入是独占 `_create`),
|
||||
台账里 `prepared` 意味着没有任何遵守本协议的派发者取得过发送许可;`dispatching` 之后永不回退,
|
||||
因此"曾经发送过"不可能再表现为 `prepared`。
|
||||
|
||||
不证明:ES 端 fencing;已 `dispatching` 的旧 owner 不会迟到一次;目标历史上没被外部写过;
|
||||
PG 快照回滚/手工删行之后的协议有效性(这类历史倒退必须 held 并人工处置)。
|
||||
|
||||
## 状态与唯一发送路径
|
||||
|
||||
```
|
||||
prepared --(CAS, 同一短事务内校验 activation/job/operation/attempt/lease/binding)--> dispatching
|
||||
dispatching --(真实 receipt + 当前 owner/lease)--> marker_confirmed
|
||||
```
|
||||
|
||||
- 发送许可只由 `AcquireRestoreMarker` 在 **COMMIT 明确返回后**给出,`RestoreSendPermit` 消费一次即作废。
|
||||
- CAS 事务按 `job → operation → activation → active attempt → effect` 取锁:
|
||||
- 激活撤销(`revoked_at`)与 CAS 在同一事务里线性化:撤销先完成则拒绝,CAS 先取锁则撤销排队;
|
||||
- lease 用 `clock_timestamp()` 在取锁之后判定,等锁跨过租约即拒绝;
|
||||
- attempt 必须属于原 window job;job 的 owner/epoch/lease、operation 的 scope/来源快照/
|
||||
已登记写集摘要、实例维护窗口(未释放且属于该作业)逐项核对;
|
||||
- 恢复路径另核 recovery queue job(运行中且有 lease)与 recovery operation(`kind=recover`、
|
||||
绑定同一 backup);
|
||||
- 同一 effect 换 plan 摘要会被台账拒绝(不能靠新 key 重开证明)。
|
||||
- 不可变绑定来自原持久锚点:`scope`、来源快照(发布基线)、写前镜像(backup)、目标实例/
|
||||
实际指纹/索引 UUID、变更/标记/请求摘要与 attempt 身份。来源与写前镜像不允许是同一份快照。
|
||||
目标指纹与索引 UUID 每次重建绑定都实时重读并与本次恢复冻结值比较:同名重建的目标/索引
|
||||
无法复用旧证明。
|
||||
- 恢复期 `_create` 只从 `sendRestoreMarkerOnce` 发出;preflight/故障钩子都在 CAS 之前。
|
||||
- 请求设置 `GetBody=nil`,Go 传输层不会在 keep-alive 重连时重放该 mutation。
|
||||
|
||||
## Restore 准入与收尾
|
||||
|
||||
`Restore` 在 `AdoptWriteSet` 之后、任何阶段写入之前调用纯读准入,并先对照原 window 的
|
||||
**权威台账全集**(跨 plan 摘要):出现另一份 plan、计划外 effect 或不可识别状态立即 held。
|
||||
|
||||
| 台账/证据 | 结果 |
|
||||
|---|---|
|
||||
| 无意图、无证据、无台账 | 首次恢复,可建立 `prepared` |
|
||||
| 旧意图、无台账 | held(旧协议未知历史,不补 prepared) |
|
||||
| `prepared` + 绑定冲突(`Held`) | held |
|
||||
| `prepared` + 已存在 claimed/finalize/目标精确 marker | held(协议证据矛盾) |
|
||||
| `prepared` + 无矛盾 | 可竞争一次许可 |
|
||||
| `prepared` + mark-intent 未落盘(文件失败/崩溃区段) | 核对绑定一致后补写**同内容**意图,再竞争 |
|
||||
| `dispatching` + 目标无精确 marker | held(零新发送) |
|
||||
| `dispatching` + 精确 marker/内容/世代 | 只走确认分支,不发 create |
|
||||
| `marker_confirmed` + 无 `restored` 证据 | held |
|
||||
| `restored` 证据与目标内容/世代一致 | 终态,允许重放 |
|
||||
|
||||
收尾前(整库指纹比对之后)以同一份权威全集再检查一次:任何 `prepared`/`dispatching` 记录、
|
||||
计划外记录、换 plan 摘要、或缺失 `restored` 文件证据的 effect 都拒绝返回成功,维护窗口不会
|
||||
被释放。
|
||||
|
||||
## 激活(部署动作,不属于本开发包)
|
||||
|
||||
新能力默认关闭。启用需要两步,缺一不可:
|
||||
|
||||
1. 部署配置 `LIBRARY_RESTORE_DISPATCH_V2=true`(只表示允许装配);
|
||||
2. 部署流程在**维护窗口 held 的当下**登记激活:`Store` 必须绑定
|
||||
`LIBRARY_TARGET_JOURNAL_ROOT`(`WithRestoreDispatchEvidence`),由
|
||||
`ActivateRestoreDispatch` 消费一份 v2 证据文件。
|
||||
|
||||
v2 证据只声明身份(目标/实例/治理库/构建、维护作业、journal 文件名与 sha256、
|
||||
旧 writer 集合);"已退出、已排空"由代码直接读维护 journal 与治理库
|
||||
`library_instance_maintenance` 行核出:窗口必须 `held`、入口 `closed/drained/
|
||||
active_at_close=0`、后台 quiesce 全零且有停写后复核、每个 writer 有
|
||||
`stop_requested` 与退出码、writer 集合与治理库一致、观察在 2 小时内。
|
||||
|
||||
没有配置受信 journal 根时返回 `ErrRestoreDispatchActivationDisabled`(能力关闭),
|
||||
不会退回"三个非空字符串就开门"。逐项不变量见
|
||||
`docs/architecture/ds01-activation-evidence.md`。
|
||||
|
||||
撤销是终局:`revoked_at` 一旦写入,SQL 不能改回 `NULL`、不能改时间,activation 行也不能删除;
|
||||
重新开放同一物理目标只能换新的目标身份。
|
||||
|
||||
边界:代码核验 journal 完整性、身份绑定与维护链自己的停写/排空观察;它不独立扫描
|
||||
外部进程,也不提供 ES 端 fence。
|
||||
|
||||
## 迁移与回滚
|
||||
|
||||
- `000078_library_restore_dispatch.up.sql` 只建治理库表与触发器,不碰 WeKnora Schema。
|
||||
- `000078_..._down.sql` 在 `library_index_restore_dispatch` 或 activation 表有**任意行**时拒绝执行:
|
||||
这些行是"是否发送过 / 是否曾被授权"的唯一证明,抹掉等于把 unknown 伪装成 prepared,或把
|
||||
已撤销的激活悄悄删掉。
|
||||
@@ -81,6 +81,7 @@ A06(内容、问答、引用图片效果、性能报告)已由用户取消
|
||||
|
||||
| ID | 缺口 | 现状与影响 |
|
||||
| --- | --- | --- |
|
||||
| R02-b | 删除型索引恢复的自动重试 | 应用期删掉了目标文档、恢复写入是独占 `_create`。持久意图已落盘而文档缺失时,服务端没有可持久、不可重用的"这份写入没落地"证据:缺失与"从未写过"同形;ES `_stats` 写计数是分片生命周期统计(官方文档:分片迁到另一节点时该分片统计会被清零),索引 uuid 不变不绑定统计生命周期,清零后重新累计到同一数值(ABA)不能排除。**现状 fail-closed**:既存 `restore-mark-intent` 一律 held(`rollback_failed`、零写入、零误归属),不自动重试,需人工按预备份核验后重新规划;本次尝试开始前不存在任何恢复意图时仍可正常重建。这是**开发缺口**(r37 反例:同 uuid 同计数 + 真实写删,恢复仍须 held),不是用户接受的能力收窄;是否引入可校验的持久 not-dispatched 状态与派发原子协议,由后续设计决定。保留的可证明路径:真实 marker 仍在且世代一致的 clean 收尾重试、文档仍是 applied 结果且世代一致的 `DELETE` 补偿重试 |
|
||||
| R03 | 存快/恢复快对比 | A06 性能报告已由用户取消,不作为门槛。**已记实测耗时**(小测试库,不是压测):封存 15.8s(含停写~5s)、同步 13.6s(预备份 13.4s 占主要)、恢复 13.8s;证据 `13-r03-durations.json`。没有做高负载压测,也不写没有测量的数字 |
|
||||
| 死信解除·真实重放演练 | 没有可解除的真实死信,也没有源侧系统管理员凭据 | 契约与执行侧已实现(见下节新增行):上游保留原行、治理侧审计+指纹绑定、未结清/变化/新死信仍阻断。**本轮没有真实死信**(`dead_letters=0`),无法演练"真实重放成功→解除→放行"整条链路;上游只有 `系统管理 runtime run_now`(需要源侧管理员权限)与整队列 purge(销毁证据,不采用)。治理平台故意不持有源侧管理员凭据(放大授权),因此这一步**阻塞在真实死信 + 重放执行方**,不伪造成功 |
|
||||
|
||||
|
||||
@@ -56,8 +56,16 @@ LIBRARY_TARGET_INGRESS_URL 入口地址(恢复后要能 200)
|
||||
LIBRARY_TARGET_INGRESS_SERVICE Compose 里的入口闸门服务名
|
||||
LIBRARY_TARGET_INGRESS_DRAIN_FILE 闸门写排空证据的宿主机文件
|
||||
LIBRARY_TARGET_REDIS_ADDR/_PASSWORD/_DB 后台队列(asynq)地址
|
||||
LIBRARY_TARGET_INGRESS_MODE gate(缺省)或 none;none 只给没有闸门的测试栈
|
||||
LIBRARY_NO_INGRESS_ACK mode=none 时必须逐字符等于 test-only
|
||||
LIBRARY_TARGET_HEALTH_URL mode=none 时的健康探针(恢复后必须 200)
|
||||
```
|
||||
|
||||
`none` 是显式降档,不是默认:整栈(如 0.9.0 测试栈)没有入口闸门时,`none` + 确认串
|
||||
才允许缺入口三项,窗口里不调用任何闸门动作,`gate` 证据位置写
|
||||
`{"mode":"none","reason":"stack has no ingress gate (test-only)"}`;恢复以
|
||||
`LIBRARY_TARGET_HEALTH_URL` 返回 200 为准。缺省 `gate` 的行为与文案不变。
|
||||
|
||||
另外 `-source-s3-endpoint <URL>` 用来覆盖"源对象存储的访问入口":容器里写的
|
||||
`MINIO_ENDPOINT` 往往是服务名(`langfuse-minio:9000`),封存跑在宿主机上时解析不到,
|
||||
资源导出会失败(`read library resource`)。覆盖只换入口,桶与对象键不变;`status`
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# v0.7.1 整库发布的数据契约与覆盖边界
|
||||
|
||||
这是候选代码的契约,不是部署许可。真实隔离执行证据在交付目录 `data-contract-r10.md`、`test-summary-r10.json` 及其 raw 索引。PG/ES/MinIO 均为本机隔离真实服务;发布/恢复测试直接调用适配器;B22 另走实际 `librarystack.OpenProductionStack` 和 HTTP 网关。网关的独占审核是测试夹具,不代表生产已隔离;B22 控制读取角色是真实 SELECT-only PG 角色。
|
||||
这是候选代码的契约,不是部署许可。真实隔离执行证据在交付目录 `data-contract-r10.md`、`test-summary-r10.json` 及其 raw 索引。PG/ES/MinIO 均为本机隔离真实服务;发布/恢复测试直接调用适配器;B22 另走实际 `librarystack.OpenProductionStack` 和 HTTP 网关。网关按限定 key 工作,不要求整桶独占审核;B22 控制读取角色是真实 SELECT-only PG 角色。
|
||||
|
||||
## 1. 源 schema、历史身份与准入
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
- 真实v071历史catalog镜像对照及UUID/自然键矩阵在r11执行,隔离结论与外部当前目录采样分开。A04已识别嵌套引用有有限支持,旧外部cursor仍不自动转换,不因内部结构正例而升级整项。
|
||||
- r8/r9进程恢复历史证据只在原发送协议、不可变计划/receipt及相应场景前提不变时复用。本轮对schema/引用准入有收紧,不能把历史执行日志称为当前源码正式进程全链重跑;完整当前隔离适配器回归单列。
|
||||
- clean-finalization unknown保持held是安全行为,但未满足全部自动收敛要求。若要求自动完成,需要能绑定原写入、完整内容和代次的充分服务端证据;相同干净正文、人工猜测归属或放宽共享规则均不可替代。
|
||||
- 最终提交/push、远端CI、父复核和本机部署为明确下一门;整体安全验收未齐不部署。生产独占/ACL网络凭据、网关OSS业务端到端与宿主重启/断电是另行授权的部署/破坏性验收,不能作为停止本机授权开发的借口。
|
||||
- 最终提交/push、远端CI、父复核和本机部署为明确下一门;整体安全验收未齐不部署。网关真实 OSS 业务端到端与宿主重启/断电是另行授权的部署/破坏性验收,不能作为停止本机授权开发的借口。
|
||||
|
||||
## 6. 目录与唯一键的有限支持矩阵
|
||||
|
||||
@@ -70,3 +70,20 @@ Wiki日志pages_affected仅支持消费者已有的字符串slug数组和{slug,t
|
||||
旧外部cursor是明确剩余风险:目标新游标可正确分页,但源数字cursor或发布前旧cursor不能直接带入目标新映射。原A04“改本地ID不破坏引用/语义”没有豁免条款,所以仍未整项通过;本模块不擅改上游WeKnora,也不把“刷新页面即可”当作已经实现的自动失效协议。
|
||||
|
||||
授权以执行时真实时钟为准;完成预备份及持久计划登记后均再验。目标实现 `GrantRevalidator` 时按治理库授权行回读:`ActiveLibraryAuthorization` 只返回未撤销、未过期且参数摘要一致的记录,撤销后连写集合都不计算(避免序列分配),预备份保留待重新授权。真实发布 wrapper 无条件接线;未接线不等于安全,只是本机历史证据的边界。
|
||||
|
||||
## 7. r12 追加的真实边界
|
||||
|
||||
- **软删除 before-image**:destructive predicate 能碰到的 7 张带 `deleted_at` 的表(knowledges/chunks/knowledge_tags/resources/wiki_folders/wiki_page_issues/wiki_pages)逐表核对备份行数与删除谓词命中数;晚阶段失败后的恢复把这些行连同 `deleted_at` 原值一起放回(r12 真实 PG)。
|
||||
- **索引独立 oracle**:发布成功后,正文、向量(同维不同值)、metadata(knowledge_id)任一被改都会让校验变红;改回后必须重新通过,说明合法本地列映射没有被这项收紧误杀。
|
||||
- **外部索引文档**:同 `_id` 属于其它知识库时整轮在写前拒绝,且那份文档的 `_source`、`_seq_no`、`_primary_term` 与对象版本逐字节不变。
|
||||
- **共享资源差异**:同 ID 共享资源在 handle / 定位 / 字节三类差异下都写前拒绝,共享对象版本不变;完全相同的共享资源按 no-op 复用,行 `xmin`/整行 JSON 与对象精确版本均不变。
|
||||
- **晚阶段失败与对象**:Wiki 阶段失败回滚后,本次新建对象不残留,**已有对象的精确版本**(versionId + SHA256 + 字节数)不变。
|
||||
- **clean 归属能力(实测)**:两个不同操作写入完全相同的字节后,索引侧只剩同一份 `_source`;`_seq_no`/`_version` 是分片提交计数,不绑定原 operation。因此"相同正文"或"seq_no+1"都不能证明是同一次 clean,缺服务端归属证据时继续 held(探针:`b19-capability-probe.json`)。
|
||||
- **旧外部游标**:游标是裸数字 id(`next_cursor` → `id < cursor`),发布按源数值升序重排本地 id。目标自己发出的游标在同代内正确;发布前或来自其它实例的游标没有代次信息,无法自动翻译,会静默错位而非报错。详见交付 `a04-cursor-semantics-r12.md` 的 A/B 决策。
|
||||
|
||||
## 8. 已接受的可用性边界(r13,用户明确接受)
|
||||
|
||||
- **旧分页游标跨发布失效**:`next_cursor` 是裸数字 id(`id < cursor`),发布按源数值升序重排本地 id,请求里没有代次信息。发布前/跨实例游标无法翻译,回传会静默错位而非报错。已接受做法:发布或恢复后刷新页面、重新从第一页分页;不改 WeKnora 源码/Schema。同代内游标与引用重映射仍然正确。
|
||||
- **clean-finalization 缺归属证据时保持 held**:真实 ES 探针显示两个不同操作写入相同字节后只剩同一份 `_source`,`_seq_no`/`_version` 是分片提交计数、不绑定 operation。此分支保持维护锁定并走人工核验/恢复,不承诺自动收敛;**不允许**凭相同正文或 seq_no+1 推断归属、补写 receipt、跳过校验、强制开门或删除未知内容。带原 marker/before-image/CAS/不可变 receipt 的 unknown 仍走既有自动补偿,不因本边界回退。
|
||||
|
||||
配套只读诊断:`GET /api/v1/library/jobs/{id}` 在作业失败且目标仍被本作业持有时返回 `maintenance`(held/owner/state/ingress/target_identity/note)与 `next_step` 文本,明确"按预备份与校验证据人工核验后再放行",且不提供任何强制成功入口;作业本身仍带 operation_id/attempt_epoch/backup/target 快照与错误、恢复说明。
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
# 目标库隔离 ID 映射
|
||||
|
||||
同一 WeKnora 实例里可以有多个知识库。同步把源快照写进目标库时,如果沿用源库的 UUID 主键,
|
||||
第二个目标库会与实例里已有库的行撞主键(真实证据:2 knowledges / 2 chunks / 23 wiki_pages /
|
||||
17 wiki_folders 已存在于旧库),写入前的跨范围检查会拒绝整次同步。关闭检查等于让两个库共享身份,
|
||||
删除/恢复/校验会互相误伤,因此身份必须在目标侧派生。
|
||||
|
||||
## 映射规则
|
||||
|
||||
```
|
||||
digest = sha256("knowledge-governance/target-identity/v1" || tenant || kb || table || source_id)
|
||||
target_id = 前 16 字节按 8-4-4-4-12 十六进制格式化(置 RFC 4122 variant/version 位)
|
||||
```
|
||||
|
||||
这是**本产品自定义的稳定 ID 算法**(`target_id_mode=scoped-sha256-v1`),只保证确定性与目标隔离;
|
||||
它不是标准 RFC 4122 UUIDv5(UUIDv5 用 SHA-1 + 标准 namespace),不要按 UUIDv5 互操作。
|
||||
|
||||
- 只绑定**稳定目标身份**(目标 tenant、目标 KB)与源身份,表名参与命名空间;
|
||||
- 不绑定 job、时间、快照版本等瞬态值:同一目标重复同步/重试/跨版本得到同一 ID;
|
||||
- 不同目标 KB、不同表得到不同 ID;
|
||||
- 源归档字节不变,映射只存在于目标侧派生视图(写集合、写入行、索引文档、校验预期);
|
||||
- 计划把策略持久化为 `target_id_mode=scoped-sha256-v1`:新进程按该字段重建映射,旧计划/写前镜像
|
||||
没有该字段则按原 ID 回放,绝不二次映射。
|
||||
|
||||
## 参与映射的表与字段
|
||||
|
||||
| 表 | 身份 | 引用字段 |
|
||||
|---|---|---|
|
||||
| knowledges | id | — |
|
||||
| chunks | id | knowledge_id, tag_id, parent_chunk_id, pre_chunk_id, next_chunk_id, relation_chunks, indirect_relation_chunks, metadata(仅精确匹配) |
|
||||
| knowledge_tags | id | — |
|
||||
| knowledge_tag_relations | (knowledge_id, tag_id) | 两个分量分别映射 |
|
||||
| wiki_folders | id | parent_id |
|
||||
| wiki_pages | id | folder_id, chunk_refs, source_refs(保留 `id|title`), page_metadata(仅精确匹配) |
|
||||
| wiki_page_issues | id | page_id, knowledge_id, chunk_id |
|
||||
| resource_bindings | id | owner_id(按 owner_type) |
|
||||
| resources | **不映射** | resource_id 保持共享身份 |
|
||||
| 索引文档 | 文档物理 ID | chunk_id, knowledge_id, source_id, tag_id |
|
||||
|
||||
正文、标题、摘要、向量、slug、in_links/out_links、pages_affected 不改写;本地自增列
|
||||
(如 `wiki_log_entries.id`)仍由既有 LocalIDs 分配。引用指向未登记身份时拒绝(悬挂引用不静默放行)。
|
||||
|
||||
## 一致性边界
|
||||
|
||||
- 写集合登记的是目标身份;verify 的身份摘要与内容摘要都按同一 `rowTransform` 计算;
|
||||
- 索引计划的目标文档 ID 与 `_source` 引用同步映射,before-image(目标写前镜像)保持原样;
|
||||
- 恢复重放写前镜像时映射关闭,避免把目标原始 ID 再映射一次;
|
||||
- 资源对象路径/location_hash 仍由既有资源改写逻辑处理,不因 ID 映射而变化。
|
||||
@@ -4,12 +4,14 @@
|
||||
|
||||
`cmd/object-gateway` 提供 `kg-object-gateway-v1`,只监听 loopback;外部接入需要另行审核的 TLS 终结与网络隔离。治理 PG 保存不可变计划、grant、intent、逐版本 receipt 和 attempt→grant delivery。部署迁移身份显式应用 `internal/objectgateway/schema.sql`;启动不自动迁移。运行身份只授必要 SELECT/INSERT 和 scope 行 UPDATE,不授 DDL/TRUNCATE/证据修改或业务库写权限。
|
||||
|
||||
scope 是 provider、原 endpoint、bucket 的 NUL 分隔 SHA256。同一物理桶只能有一个受审核 scope 和一套共享 journal,不能用 endpoint 别名或另一日志库建立平行写域。
|
||||
scope 是 provider、原 endpoint、bucket 的 NUL 分隔 SHA256。同一物理桶在本网关内只使用一个 scope 和一套共享 journal,不能用 endpoint 别名或另一日志库建立平行写域。这不要求整桶只有本网关一个写者:别的服务可以在同一桶的其它 key 上正常读写。
|
||||
|
||||
数据认证端点:`POST /v1/acquire`、`/v1/resolve`、`/v1/execute`、`/v1/reconcile`、`/v1/observe`,以及 `GET /v1/capabilities`。所有请求核验协议和 scope。`capabilities.automatic_grants` 必须为真,正式 stack 才接受自动授权能力;未接控制源、只读角色权限异常、协议不符或不可达均拒绝,无直写回退。
|
||||
|
||||
PG 行锁负责原子准入,不是远端存储 fence。intent 必须持久提交后才发送一次请求;连接死亡、租约过期不清除未知 intent。任何未终态 intent 均阻止 scope 换 epoch。重复 operation 只读回执或精确对账,不重发 PUT;暂时查不到版本仍 held。删除必须绑定已确认 create 的精确 VersionID,不删 latest、不生成删除标记。
|
||||
|
||||
同 key 竞争按 key 判定,不按桶判定:create 前已存在任何版本 → 回执 `rejected` 且不修改该对象;PUT 后除精确版本读回外还核对当前 latest,若并发写者已产生更新的当前版本,则只按精确 VersionID 撤回本次写入,保留对方版本并返回冲突;读不到当前版本时保持 held,不删除、不猜测。版本化存储的 PUT 不是原子 create-if-absent,因此“本 key 多数情况下归本计划”不是存储层保证。
|
||||
|
||||
不可变证据表拒绝 UPDATE/DELETE/TRUNCATE。Ready 核 fsync、synchronous_commit、full_page_writes、主库可写及必要表/触发器;管理员与迁移身份仍是可信边界,触发器不能抵御 DBA 的 DDL。
|
||||
|
||||
## 正式控制交付路径
|
||||
@@ -54,13 +56,13 @@ SELECT-only治理角色需要读取`library_jobs`、`library_operations`、`libr
|
||||
- `OBJECT_GATEWAY_ARCHIVE_ROOT`:原持久计划所在归档绝对路径,部署只读挂载。
|
||||
- `OBJECT_GATEWAY_TARGET_ID`:与正式 worker `LibraryTargetIdentity()` 完全相同的逻辑目标身份;不能用另一个环境的值。
|
||||
- `OBJECT_GATEWAY_PROVIDER`、`OBJECT_GATEWAY_ENDPOINT`、`OBJECT_GATEWAY_BUCKET`、`OBJECT_GATEWAY_REGION`:精确存储身份。
|
||||
- `OBJECT_GATEWAY_ACCESS_KEY`、`OBJECT_GATEWAY_SECRET_KEY`:仅网关持有的对象写凭据。
|
||||
- `OBJECT_GATEWAY_ACCESS_KEY`、`OBJECT_GATEWAY_SECRET_KEY`:网关持有的对象凭据,需在目标桶 PUT/GET/按版本 DELETE/列举版本。沿用现有 AK 即可,不要求另建独立只读或独占写凭据。
|
||||
- `OBJECT_GATEWAY_DATA_TOKEN`、`OBJECT_GATEWAY_CONTROL_TOKEN`:不同且至少 32 字节;普通 worker 仅持有 data token。
|
||||
- `OBJECT_GATEWAY_ID`、`OBJECT_GATEWAY_AUDIT_FILE`、`OBJECT_GATEWAY_AUDIT_PUBLIC_KEY`:独立审核者的 Ed25519 签名隔离证据,绑定协议、scope、gateway ID、时间窗及 writer/policy/拒写/排空摘要。审核私钥不能交给 worker 或网关。
|
||||
- `OBJECT_GATEWAY_ID`、`OBJECT_GATEWAY_AUDIT_FILE`、`OBJECT_GATEWAY_AUDIT_PUBLIC_KEY`:**已退役**。旧部署残留的值被忽略,不参与启动、Ready、每次 Execute 或能力判断;不需要管理员签发整桶独占证明,也不会因过期文件拒绝发布。
|
||||
|
||||
秘密只经服务环境/私有文件注入,不进 argv、URL 或日志。所有调用端错误都使用固定原因码。
|
||||
|
||||
正式 worker 使用 `LIBRARY_OBJECT_GATEWAY_URL` 和 `LIBRARY_OBJECT_GATEWAY_TOKEN`;不再依赖预先生成的 grant 文件。`LIBRARY_OBJECT_GATEWAY_GRANT_DIRECTORY` 保留旧离线兼容用途,不是自动模式的授权来源。发布 stack 对所有对象 provider 强制网关;S3 构造仍用于只读捕获/预备份,部署必须使用独立只读凭据。
|
||||
正式 worker 使用 `LIBRARY_OBJECT_GATEWAY_URL` 和 `LIBRARY_OBJECT_GATEWAY_TOKEN`;不再依赖预先生成的 grant 文件。`LIBRARY_OBJECT_GATEWAY_GRANT_DIRECTORY` 保留旧离线兼容用途,不是自动模式的授权来源。发布 stack 对所有对象 provider 强制网关;S3 构造仍用于只读捕获/预备份,可继续使用现有凭据,不再要求独立只读 OSS AK。
|
||||
|
||||
## 已关闭的本仓旁路
|
||||
|
||||
@@ -70,24 +72,34 @@ SELECT-only治理角色需要读取`library_jobs`、`library_operations`、`libr
|
||||
- `librarystack` 不再为缺网关的 MinIO/S3 返回直写 writer。
|
||||
- `libraryctl` capture 的 S3 构造与读取保留;未把读取当作写入口禁用。
|
||||
|
||||
这不等于已经撤销运行环境旧进程的凭据;源码变更尚未部署。`librarydrill` 等无 owned-attempt 的旧写型工具不能靠静态 grant 绕过自动模式,需补正式执行身份后才可用于写入。低层 S3 adapter 仍用于网关后端、读取及显式隔离夹具;部署权限才负责防止外部旧进程直接调用存储。
|
||||
这不等于已经撤销运行环境旧进程的凭据;源码变更尚未部署。`librarydrill` 等无 owned-attempt 的旧写型工具不能靠静态 grant 绕过自动模式,需补正式执行身份后才可用于写入。低层 S3 adapter 仍用于网关后端、读取及显式隔离夹具;共享桶不要求撤除其它写者,跨写者的同 key 竞争按冲突/held 处理。
|
||||
|
||||
## 实测与未完成边界
|
||||
|
||||
已实跑:真实治理 PG(独立 SELECT-only 登录角色)、原 archive、数据 HTTP、业务 PG 与 MinIO 的自动授权/跨 epoch 链;未锚定计划在第一笔 PG 前拒绝,接管保持原发送身份且不增加 PUT。另有真实网关二进制的两次中断验证。隔离审计 authority/故障注入仍是测试设施,不是生产权限证明。
|
||||
已实跑:真实治理 PG(独立 SELECT-only 登录角色)、原 archive、数据 HTTP、业务 PG 与 MinIO 的自动授权/跨 epoch 链;未锚定计划在第一笔 PG 前拒绝,接管保持原发送身份且不增加 PUT。另有真实网关二进制的两次中断验证,以及共享桶下“无整桶证明启动 + 其它 key 并发写入 + 同 key 既有对象拒绝”的真实 PG/MinIO 用例。历史隔离审计 authority/故障注入是当时的测试设施,不是当前部署前置。
|
||||
|
||||
正式 worker 隔离进程证据:真实 Docker 入口闸门/队列、只读 worker 对象身份、独立治理读取角色、网关与业务 PG/ES/MinIO;首次 PG 后和对象提交后两次 SIGKILL,同一原 job/operation/plan 的三代 attempt 完成发布,原对象发送 epoch 与回执不变。隔离构建标签 `kg_isolated_drill` 仅把真实租约缩短至4秒,并拒绝非 loopback 一次性 r8 库;正常构建仍为10分钟。维护上游是无业务凭据的独立 HTTP 夹具,不是运行中的 WeKnora;签名证据绑定本次真实拒写/排空材料,不代表生产独占审核。
|
||||
|
||||
已补独立 `library.recover` 正式worker链、恢复中再次SIGKILL、完整before-image/CAS/精确版本读回,以及来源/目标/原job/计划/operation/任意队列身份拒绝。ES故障代理只转发真实ES并丢弃已提交响应,未伪造服务响应。
|
||||
|
||||
尚未完成:clean-finalization unknown的充分归属与自动收敛;独立运行环境只读对象凭据、ACL/网络和全部writer排空;网关真实OSS端到端;原D0/F11/F12与55项其余缺口。测试/隔离验收与真实部署独占分列,不把生产变更授权当成本机开发授权的前置。
|
||||
尚未完成:clean-finalization unknown的充分归属与自动收敛;网关真实 OSS 端到端与真实部署验收(父执行);原D0/F11/F12与55项其余缺口。测试/隔离验收与真实部署分列。
|
||||
|
||||
维护隔离、独占审核与 grant 是独立前置。配置 true、健康 200、测试自签 authority 都不能代替已部署拒写/排空证据。目前未签发真实环境独占证明,未改生产权限或上游 WeKnora 源码/Schema。
|
||||
整桶独占审核、全部 writer 排空、独立只读对象凭据已不再是发布前置;维护窗口内停目标写入容器并读回确认即可更新。网关按 key 拒绝冲突、精确版本回滚,但不承诺无关写者同时修改同一 key 时仍能自动完成——那种情况会明确冲突或 held,需要人工按回执处理。未改生产权限或上游 WeKnora 源码/Schema。
|
||||
|
||||
## 维护式更新(已采用)
|
||||
|
||||
目标测试 KB 的更新走维护窗口,不再要求证明整桶只有一个写者:
|
||||
|
||||
1. 停目标测试 WeKnora 的写入应用容器(只停该服务,不停机器人、适配器、共享 PG/ES 或其它 KB 的服务),并由容器状态/健康读回确认已停止。这是本轮文档化的运维步骤;代码侧没有新增停容器实现。
|
||||
2. 用既有维护/预备份链记录本次目标 KB/对象范围:实例级维护窗口与写者停写证据由 `internal/libraryfreeze` + `internal/librarymaintenance` 采集,`libraryctl capture`(页面入口 `POST /api/v1/library/captures`)完成闸门排空→停写者→导出快照并保留 `post_stop_digest`;不把通用主机备份当作 KB 预备份的替代。
|
||||
3. 用原 plan、原 WriteSet 摘要和限定对象清单走既有 `ProductionLibraryTarget.Apply`(经正式 worker owned attempt 与网关 grant)更新资源;网关沿用现有 AK,不换凭据、不签独占证明。
|
||||
4. 启动原容器,健康检查与页面业务验收;失败走既有 `library.recover`/`RestoreDrill` 恢复链按原预备份回放并启动原服务,不无限留停机。
|
||||
5. 无关服务与无关 key 全程不需要停;同 key 若仍有外部写者,按上节的冲突/held 处理:双方版本都保留,由人按证据处置,不自动合并。
|
||||
|
||||
部署需变更的组件:`object-gateway`(本候选镜像)、`worker`(同提交镜像),以及两者 env 中移除/留空已退役的 `OBJECT_GATEWAY_ID`、`OBJECT_GATEWAY_AUDIT_FILE`、`OBJECT_GATEWAY_AUDIT_PUBLIC_KEY`。`api`、`web` 无协议变更,可不同步。journal、治理 PG 只读 authority、archive 与 target identity 配置保持不变。
|
||||
|
||||
## 部署与回退(未执行)
|
||||
|
||||
未来测试主机 `120.76.217.51`、生产 `120.76.218.137` 各自独立批准。每批先冻结主机、桶、全部角色/STS/预签名写入口、端口/网络规则、影响、维护窗口与回滚;当前没有执行这些外部变更。
|
||||
测试主机 `120.76.217.51`、生产 `120.76.218.137` 各自独立批准;本候选未在任一主机执行停机、备份、更新或启动。r5 的已完成 OSS 测试授权不能重复使用。
|
||||
|
||||
先关新写准入→排空并对账旧请求→撤旧直写权限/切只读身份并实测拒写→独立审核→批准的新限定对象验收→父复核,才可切业务客户端。r5 的已完成 OSS 测试授权不能重复使用。
|
||||
|
||||
回退先关准入并保留 journal;所有已发送请求有终态才可撤网关,unknown 继续 held。不得因网关故障自动恢复旧直写凭据。权限/网络回退须另批。当前整模块 INCOMPLETE,禁止部署部分候选。
|
||||
按“停目标写入容器→读回确认→备份目标 KB 范围→更新资源→启动→健康/页面验收”执行;失败按原备份恢复并启动原服务。回退先关新写准入并保留 journal;所有已发送请求有终态才可撤网关,unknown 继续 held。不得因网关故障自动恢复旧直写凭据。当前整模块仍未完成真实 OSS 端到端与 clean-finalization 收敛,禁止把本候选当作完整生产就绪。
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# 发布管理流程与页面精简
|
||||
|
||||
## 目标与边界
|
||||
|
||||
现有整库发布页面同时呈现日常发布、维护诊断、恢复演练与历史快照,且封存操作位于页面底部,版本入口重复。此次优化聚焦 `ReleaseTargets` 的信息层级和操作位置,沿用现有 API、权限、授权绑定、维护所有者与恢复确认。
|
||||
|
||||
基线采用云测试站当前前端对应的工作树 `cbc6b98`;该提交的 web 与部署版本 `0b753e5` 相同。并行主干及其他工作树不合并、不覆盖。
|
||||
|
||||
## 操作者主流程
|
||||
|
||||
| 顺序 | 页面操作 | 后台行为 | 完成边界 |
|
||||
| --- | --- | --- | --- |
|
||||
| 1 | 选择对内或对外知识库 | 读取该范围 overview、快照、作业及维护窗口 | 目标身份与版本来自所选范围;读取失败不当作空库 |
|
||||
| 2 | 封存测试版本 | POST `/api/v1/library/captures`,由队列及执行器捕获测试整库版本 | 投递不等于完成,以作业与登记快照为准;此步不发布生产 |
|
||||
| 3 | 查看变更及发布条件 | 展开变更、备份与恢复验证;若缺基线或恢复证据,沿用对应管理员入口 | 逐条呈现服务端阻塞原因,不删除发布门槛 |
|
||||
| 4 | 签发同步授权 | 管理员调用 `/api/v1/library/authorizations`,绑定目标与当前快照 | 有效期内仍需单独点击启动;不自动执行同步 |
|
||||
| 5 | 启动同步 | 沿用现有同步请求与执行链:固定版本、生产预备份、同步、校验及收尾 | 只在接口条件允许时可执行;运行中主按钮改为查看任务 |
|
||||
| 6 | 查看结果 | 自动刷新作业进度,终态按真实记录呈现 | 失败显示原始原因;维护占用不被其他作业成功覆盖 |
|
||||
| 异常 | 从版本与快照选择恢复点 | 继续执行独立恢复授权、影响确认和恢复条件校验 | 恢复整套目标知识状态,不回退测试知识库 |
|
||||
|
||||
封存、签发授权、启动同步三个动作保持独立,不将其合并为一键生产写入。
|
||||
|
||||
## 页面保留规则
|
||||
|
||||
- 顶栏:唯一的「版本与快照」入口和刷新。
|
||||
- 主工作区:实际目标身份、当前生产版本、候选测试版本、紧凑变更统计、当前主动作与一份阻塞原因。
|
||||
- 已有封存时提供次级「重新封存」;运行时不提供重复封存、授权或同步入口。
|
||||
- 「查看变更明细」「发布准备与恢复验证」「封存完整性」默认折叠;原有详情和验证回调继续存在。
|
||||
- 目标维护正常时折叠,持有维护窗口时自动展开;仍按窗口持有作业展示恢复信息。
|
||||
- 最近作业仅在有记录时展示,正常完成默认折叠,失败默认展开;运行中保留主进度,分项明细按需展开。
|
||||
- 版本抽屉保留历史筛选、分页、校验记录、恢复授权和影响确认,移除重复的封存按钮。
|
||||
- 发布管理页面移除旧文档级发布及历史来源关联区域,只展示整库发布流程。
|
||||
|
||||
## 验收清单
|
||||
|
||||
- [x] 前端生产构建通过。
|
||||
- [x] 原发布、恢复、维护所有者和刷新/切库回归通过。
|
||||
- [x] 同一挂载下依次切换封存、授权、同步,点击只调用对应动作。
|
||||
- [x] 折叠与展开、打开历史不触发业务写入。
|
||||
- [x] 运行时主操作只查看任务,不调用封存、授权或同步。
|
||||
- [x] 正常维护折叠、持有时展开、释放后折叠;切库重置明细展开状态。
|
||||
- [x] 无作业不画空进度;失败原因默认可见。
|
||||
- [x] 只读角色和读取失败时禁止主动作。
|
||||
|
||||
浏览器验收使用本地候选构建,GET/HEAD 通过已有 SSH 通道读取云测试 API;代理拒绝所有写请求。界面验收与真实封存、生产同步及恢复演练分开记录;本任务不执行这些业务写入。完整仓库门禁和云端部署结论以实际运行结果为准,不用上述前端检查替代。
|
||||
@@ -3,6 +3,7 @@ package appconfig
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -23,8 +24,11 @@ type Config struct {
|
||||
ReleaseConfigFile string
|
||||
ProductionReleaseRequestsEnabled bool
|
||||
ProductionBackupRestoreVerified bool
|
||||
ProductionReleaseAuthorizerRef string
|
||||
ProductionReleaseAuthTokenFile string
|
||||
// LibraryRestoreDispatchEnabled 允许 worker 装配 DS01 派发台账;默认 false。
|
||||
// 它不能单独证明独占:治理库里的目标激活登记同样必需。
|
||||
LibraryRestoreDispatchEnabled bool
|
||||
ProductionReleaseAuthorizerRef string
|
||||
ProductionReleaseAuthTokenFile string
|
||||
// Production WeKnora endpoint: the release target itself stays declared in
|
||||
// the release runtime config (RELEASE_CONFIG_FILE); these settings only let
|
||||
// the API probe that endpoint so the console can show its real state.
|
||||
@@ -126,6 +130,11 @@ type Config struct {
|
||||
// 入口闸门:关入口与排空在它身上做(被代理服务自己不排空)。
|
||||
LibraryTargetIngressService string
|
||||
LibraryTargetIngressDrainFile string
|
||||
// 入口形态:gate(默认)或 none。none 只给整栈没有入口闸门的测试环境用,
|
||||
// 必须同时给 LibraryTargetNoIngressAck,且证据等级降一档并如实记录。
|
||||
LibraryTargetIngressMode string
|
||||
LibraryTargetNoIngressAck string
|
||||
LibraryTargetHealthURL string
|
||||
// 后台静默检查:目标队列 + 目标库(HTTP 排空不等于后台任务排空)。
|
||||
LibraryTargetRedisAddr string
|
||||
LibraryTargetRedisPassword string
|
||||
@@ -244,19 +253,23 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
LibraryTargetIngressURL: strings.TrimSpace(getenv("LIBRARY_TARGET_INGRESS_URL")),
|
||||
LibraryTargetIngressService: strings.TrimSpace(getenv("LIBRARY_TARGET_INGRESS_SERVICE")),
|
||||
LibraryTargetIngressDrainFile: strings.TrimSpace(getenv("LIBRARY_TARGET_INGRESS_DRAIN_FILE")),
|
||||
LibraryTargetRedisAddr: strings.TrimSpace(getenv("LIBRARY_TARGET_REDIS_ADDR")),
|
||||
LibraryTargetRedisPassword: strings.TrimSpace(getenv("LIBRARY_TARGET_REDIS_PASSWORD")),
|
||||
LibraryTargetRedisDB: valueOrDefaultInt(getenv("LIBRARY_TARGET_REDIS_DB"), 0),
|
||||
LibraryTargetJournalRoot: strings.TrimSpace(getenv("LIBRARY_TARGET_JOURNAL_ROOT")),
|
||||
LibraryEmbeddingModel: valueOrDefault(strings.TrimSpace(getenv("LIBRARY_EMBEDDING_MODEL")), "text-embedding-v4"),
|
||||
LibraryEmbeddingDimension: embeddingDimension,
|
||||
LibraryCapacityBudgetBytes: capacityBudget,
|
||||
LibraryRetentionVersions: retentionVersions,
|
||||
LibraryRetentionDays: retentionDays,
|
||||
LibraryExecutorStatusMaxAge: 2 * time.Minute,
|
||||
VersionHistoryLinkOrigin: strings.TrimSpace(getenv("VERSION_HISTORY_LINK_ORIGIN")),
|
||||
VersionHistoryHTTPTimeout: 15 * time.Second,
|
||||
VersionHistoryMaxPages: valueOrDefaultInt(getenv("VERSION_HISTORY_MAX_PAGES"), 200),
|
||||
LibraryTargetIngressMode: strings.TrimSpace(getenv("LIBRARY_TARGET_INGRESS_MODE")),
|
||||
// 确认串不 Trim:必须逐字符相等,多一个空格都算没确认。
|
||||
LibraryTargetNoIngressAck: getenv("LIBRARY_NO_INGRESS_ACK"),
|
||||
LibraryTargetHealthURL: strings.TrimSpace(getenv("LIBRARY_TARGET_HEALTH_URL")),
|
||||
LibraryTargetRedisAddr: strings.TrimSpace(getenv("LIBRARY_TARGET_REDIS_ADDR")),
|
||||
LibraryTargetRedisPassword: strings.TrimSpace(getenv("LIBRARY_TARGET_REDIS_PASSWORD")),
|
||||
LibraryTargetRedisDB: valueOrDefaultInt(getenv("LIBRARY_TARGET_REDIS_DB"), 0),
|
||||
LibraryTargetJournalRoot: strings.TrimSpace(getenv("LIBRARY_TARGET_JOURNAL_ROOT")),
|
||||
LibraryEmbeddingModel: valueOrDefault(strings.TrimSpace(getenv("LIBRARY_EMBEDDING_MODEL")), "text-embedding-v4"),
|
||||
LibraryEmbeddingDimension: embeddingDimension,
|
||||
LibraryCapacityBudgetBytes: capacityBudget,
|
||||
LibraryRetentionVersions: retentionVersions,
|
||||
LibraryRetentionDays: retentionDays,
|
||||
LibraryExecutorStatusMaxAge: 2 * time.Minute,
|
||||
VersionHistoryLinkOrigin: strings.TrimSpace(getenv("VERSION_HISTORY_LINK_ORIGIN")),
|
||||
VersionHistoryHTTPTimeout: 15 * time.Second,
|
||||
VersionHistoryMaxPages: valueOrDefaultInt(getenv("VERSION_HISTORY_MAX_PAGES"), 200),
|
||||
}
|
||||
if config.DatabaseURL == "" {
|
||||
return Config{}, errors.New("DATABASE_URL is required")
|
||||
@@ -304,6 +317,14 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
return Config{}, fmt.Errorf("PRODUCTION_BACKUP_RESTORE_VERIFIED: %w", err)
|
||||
}
|
||||
}
|
||||
// DS01 恢复标记派发协议:默认关闭。开关只是"允许装配"的意思,不是独占证明——
|
||||
// 真正发放许可还要求治理库里对该目标存在未撤销的激活登记。
|
||||
if raw := getenv("LIBRARY_RESTORE_DISPATCH_V2"); raw != "" {
|
||||
config.LibraryRestoreDispatchEnabled, err = strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return Config{}, fmt.Errorf("LIBRARY_RESTORE_DISPATCH_V2: %w", err)
|
||||
}
|
||||
}
|
||||
if raw := getenv("PRODUCTION_WEKNORA_ALLOW_DOCKER_HTTP"); raw != "" {
|
||||
config.ProductionWeKnoraAllowDockerHTTP, err = strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
@@ -376,9 +397,35 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
if config.LibraryAgentSocket != "" && !filepath.IsAbs(config.LibraryAgentSocket) {
|
||||
return Config{}, errors.New("LIBRARY_AGENT_SOCKET 必须是绝对路径(unix socket 由主机侧 agent 持有)")
|
||||
}
|
||||
// 入口形态:缺省 gate,行为与之前完全一致。none 只给整栈没有入口闸门的
|
||||
// 测试环境用,必须显式确认;确认串逐字符相等,多一个空格都算没确认。
|
||||
if config.LibraryTargetIngressMode == "" {
|
||||
config.LibraryTargetIngressMode = "gate"
|
||||
}
|
||||
switch config.LibraryTargetIngressMode {
|
||||
case "gate", "none":
|
||||
default:
|
||||
return Config{}, fmt.Errorf("LIBRARY_TARGET_INGRESS_MODE 无效(%q):只能是 gate 或 none",
|
||||
config.LibraryTargetIngressMode)
|
||||
}
|
||||
noGate := config.LibraryTargetIngressMode == "none"
|
||||
if noGate && config.LibraryTargetNoIngressAck != "test-only" {
|
||||
return Config{}, errors.New("no-gate 维护必须显式确认:LIBRARY_NO_INGRESS_ACK 必须逐字符等于 test-only,拒绝启动")
|
||||
}
|
||||
if !noGate && config.LibraryTargetNoIngressAck != "" {
|
||||
return Config{}, errors.New("LIBRARY_NO_INGRESS_ACK 只在 LIBRARY_TARGET_INGRESS_MODE=none 下有效")
|
||||
}
|
||||
if config.LibraryTargetHealthURL != "" {
|
||||
parsed, err := url.Parse(config.LibraryTargetHealthURL)
|
||||
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") ||
|
||||
parsed.Host == "" || parsed.User != nil {
|
||||
return Config{}, errors.New("LIBRARY_TARGET_HEALTH_URL 必须是 http(s) 地址且不带凭据")
|
||||
}
|
||||
}
|
||||
// 目标维护要么完整配置,要么完全不配;半配置会让窗口"看起来存在"却挡不住目标。
|
||||
targetSet := config.LibraryTargetInstance != "" || config.LibraryTargetProject != "" ||
|
||||
len(config.LibraryTargetWriters) > 0 || config.LibraryTargetIngressURL != ""
|
||||
len(config.LibraryTargetWriters) > 0 || config.LibraryTargetIngressURL != "" ||
|
||||
noGate || config.LibraryTargetHealthURL != ""
|
||||
if targetSet {
|
||||
missing := make([]string, 0, 4)
|
||||
if config.LibraryTargetInstance == "" {
|
||||
@@ -390,16 +437,21 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
if len(config.LibraryTargetWriters) == 0 {
|
||||
missing = append(missing, "LIBRARY_TARGET_WRITERS")
|
||||
}
|
||||
if config.LibraryTargetIngressURL == "" {
|
||||
if noGate {
|
||||
// 没有闸门可声明:健康探针是唯一能证明"目标回来了"的必需项。
|
||||
if config.LibraryTargetHealthURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_HEALTH_URL")
|
||||
}
|
||||
} else if config.LibraryTargetIngressURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_URL")
|
||||
}
|
||||
if config.LibraryTargetJournalRoot == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_JOURNAL_ROOT")
|
||||
}
|
||||
if config.LibraryTargetIngressService == "" {
|
||||
if !noGate && config.LibraryTargetIngressService == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_SERVICE")
|
||||
}
|
||||
if config.LibraryTargetIngressDrainFile == "" {
|
||||
if !noGate && config.LibraryTargetIngressDrainFile == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_DRAIN_FILE")
|
||||
}
|
||||
if config.LibraryTargetRedisAddr == "" {
|
||||
@@ -408,13 +460,17 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
if len(missing) > 0 {
|
||||
return Config{}, fmt.Errorf("目标维护配置不完整:缺少 %s", strings.Join(missing, "、"))
|
||||
}
|
||||
if noGate && (config.LibraryTargetIngressURL != "" || config.LibraryTargetIngressService != "" ||
|
||||
config.LibraryTargetIngressDrainFile != "") {
|
||||
return Config{}, errors.New("no-gate 维护不得同时配置入口地址/闸门服务/排空文件:要么有闸门,要么明确没有")
|
||||
}
|
||||
if !filepath.IsAbs(config.LibraryTargetJournalRoot) {
|
||||
return Config{}, errors.New("LIBRARY_TARGET_JOURNAL_ROOT 必须是绝对路径")
|
||||
}
|
||||
if !filepath.IsAbs(config.LibraryTargetDockerSocket) {
|
||||
return Config{}, errors.New("LIBRARY_TARGET_DOCKER_SOCKET 必须是绝对路径")
|
||||
}
|
||||
if !filepath.IsAbs(config.LibraryTargetIngressDrainFile) {
|
||||
if !noGate && !filepath.IsAbs(config.LibraryTargetIngressDrainFile) {
|
||||
return Config{}, errors.New("LIBRARY_TARGET_INGRESS_DRAIN_FILE 必须是绝对路径")
|
||||
}
|
||||
if len(config.LibraryTargetNonWriters) == 0 {
|
||||
|
||||
@@ -73,12 +73,13 @@ func (r LibraryWorkerRole) LibraryJobKinds() []string {
|
||||
switch r {
|
||||
case LibraryWorkerRoleReleaseExecutor:
|
||||
return []string{string(governance.LibraryJobSync), string(governance.LibraryJobRestore),
|
||||
string(governance.LibraryJobRecover), string(governance.LibraryJobBaseline)}
|
||||
string(governance.LibraryJobRecover), string(governance.LibraryJobBaseline),
|
||||
string(governance.LibraryJobVerify)}
|
||||
case LibraryWorkerRoleCapture:
|
||||
return []string{string(governance.LibraryJobSeal)}
|
||||
default:
|
||||
return []string{string(governance.LibraryJobSeal), string(governance.LibraryJobSync),
|
||||
string(governance.LibraryJobRestore), string(governance.LibraryJobRecover),
|
||||
string(governance.LibraryJobBaseline)}
|
||||
string(governance.LibraryJobBaseline), string(governance.LibraryJobVerify)}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,13 +63,14 @@ func TestLoadRejectsUnknownLibraryWorkerRole(t *testing.T) {
|
||||
// 每个角色收拾哪些 kind、谁拥有 DDL,都必须互斥且覆盖完整。
|
||||
func TestLibraryWorkerRoleOwnershipMaps(t *testing.T) {
|
||||
release := LibraryWorkerRoleReleaseExecutor
|
||||
if got := release.LibraryJobKinds(); len(got) != 4 || got[0] != "sync" || got[1] != "restore" || got[2] != "recover" || got[3] != "baseline" {
|
||||
if got := release.LibraryJobKinds(); len(got) != 5 || got[0] != "sync" || got[1] != "restore" ||
|
||||
got[2] != "recover" || got[3] != "baseline" || got[4] != "verify" {
|
||||
t.Fatalf("release-executor kinds = %v", got)
|
||||
}
|
||||
if got := LibraryWorkerRoleCapture.LibraryJobKinds(); len(got) != 1 || got[0] != "seal" {
|
||||
t.Fatalf("capture kinds = %v", got)
|
||||
}
|
||||
if got := LibraryWorkerRoleFull.LibraryJobKinds(); len(got) != 5 {
|
||||
if got := LibraryWorkerRoleFull.LibraryJobKinds(); len(got) != 6 {
|
||||
t.Fatalf("full kinds = %v", got)
|
||||
}
|
||||
for _, role := range []LibraryWorkerRole{LibraryWorkerRoleFull, LibraryWorkerRoleCapture} {
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -127,12 +129,81 @@ func (r *Runner) runRemote(ctx context.Context, timeout time.Duration, remote st
|
||||
if ctx.Err() != nil {
|
||||
return text, fmt.Errorf("ssh %s 超时(%s)", r.config.Target, timeout)
|
||||
}
|
||||
// 主机侧脚本会用退出码表达"参数非法/有库失败",把输出带回去便于页面展示。
|
||||
return text, fmt.Errorf("ssh %s 执行失败: %s", r.config.Target, firstLines(text, 6))
|
||||
// 连接层失败与主机侧命令失败必须分开说:前者连脚本都没跑,
|
||||
// 任何"脚本缺失/计划未下发"都是把未观测当成事实。
|
||||
return text, hostFailure(r.config.Target, text)
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
// hostFailure 把 ssh 的非零退出分成两类:连不上,和连上了但远端命令失败。
|
||||
func hostFailure(target, text string) error {
|
||||
if connectionFailurePattern.MatchString(text) {
|
||||
return fmt.Errorf("目标主机 %s 不可达(%s);未执行任何主机侧脚本,脚本与计划状态未知",
|
||||
target, firstLines(text, 3))
|
||||
}
|
||||
// 主机侧脚本会用退出码表达"参数非法/有库失败",把输出带回去便于页面展示。
|
||||
return fmt.Errorf("已连上 %s,但主机侧命令失败: %s", target, firstLines(text, 6))
|
||||
}
|
||||
|
||||
// connectionFailurePattern 是 OpenSSH 客户端在"根本没建立连接"时打的原文。
|
||||
// 只有命中它才允许说"不可达";其余非零退出属于远端命令自己的失败。
|
||||
var connectionFailurePattern = regexp.MustCompile(`(?i)ssh: connect to host|Operation timed out|Connection timed out|Connection refused|Connection reset by peer|No route to host|Network is unreachable|Could not resolve hostname|Host key verification failed|Permission denied|Connection closed by`)
|
||||
|
||||
// Endpoint 读回挂载的 ssh 配置里本别名真实拨号的 host:port。
|
||||
// 页面必须能说清"现在连的是哪个地址"——只显示别名时,改错文件也看不出来。
|
||||
func (r *Runner) Endpoint() string {
|
||||
if r == nil || r.config.SSHConfig == "" || r.config.Target == "" {
|
||||
return ""
|
||||
}
|
||||
raw, err := os.ReadFile(r.config.SSHConfig)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
host, port := sshConfigEndpoint(string(raw), r.config.Target)
|
||||
if host == "" {
|
||||
return ""
|
||||
}
|
||||
if port == "" {
|
||||
port = "22"
|
||||
}
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
// sshConfigEndpoint 解析 `Host <alias>` 段的 HostName/Port(首个匹配段生效)。
|
||||
// 只做"页面显示用"的最小解析,不替代 ssh 自己的配置语义。
|
||||
func sshConfigEndpoint(config, alias string) (string, string) {
|
||||
host, port := "", ""
|
||||
active := false
|
||||
for _, line := range strings.Split(config, "\n") {
|
||||
fields := strings.Fields(strings.TrimSpace(line))
|
||||
if len(fields) == 0 || strings.HasPrefix(fields[0], "#") {
|
||||
continue
|
||||
}
|
||||
key := strings.ToLower(fields[0])
|
||||
if key == "host" {
|
||||
active = false
|
||||
for _, pattern := range fields[1:] {
|
||||
if pattern == alias {
|
||||
active = true
|
||||
break
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !active || len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "hostname":
|
||||
host = strings.Trim(fields[1], `"`)
|
||||
case "port":
|
||||
port = strings.Trim(fields[1], `"`)
|
||||
}
|
||||
}
|
||||
return host, port
|
||||
}
|
||||
|
||||
var scheduleAppliedPattern = regexp.MustCompile(
|
||||
`SCHEDULE_APPLIED enabled=(\d)(?: time=(\S+))?(?: days=(\d+))?(?: months=(\d+))?(?: next=(\S+))?(?: cron=(\S+))?`)
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package backuphost
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -188,6 +190,41 @@ func TestRunnerSurfacesHostFailure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 现场原错:ssh: connect to host 120.76.217.51 port 22: Operation timed out。
|
||||
// 连不上时脚本根本没跑,只能说"不可达";断言配对的反面:不能说脚本缺失。
|
||||
func TestHostFailureSeparatesUnreachableFromRemoteCommand(t *testing.T) {
|
||||
unreachable := hostFailure("weknora-test", "ssh: connect to host 120.76.217.51 port 22: Operation timed out")
|
||||
if !strings.Contains(unreachable.Error(), "不可达") || !strings.Contains(unreachable.Error(), "Operation timed out") {
|
||||
t.Fatalf("连接超时应报不可达并带原错: %v", unreachable)
|
||||
}
|
||||
if strings.Contains(unreachable.Error(), "主机侧命令失败") || strings.Contains(unreachable.Error(), "脚本缺失") {
|
||||
t.Fatalf("连接失败不能报成远端命令/脚本问题: %v", unreachable)
|
||||
}
|
||||
remote := hostFailure("weknora-test", "bash: /opt/weknora-test/logical-backup.sh: No such file or directory")
|
||||
if !strings.Contains(remote.Error(), "已连上") || !strings.Contains(remote.Error(), "No such file") {
|
||||
t.Fatalf("已连接但脚本失败时应原样带回原因: %v", remote)
|
||||
}
|
||||
if strings.Contains(remote.Error(), "不可达") {
|
||||
t.Fatalf("已连上就不能说不可达: %v", remote)
|
||||
}
|
||||
}
|
||||
|
||||
// 页面要能说清"实际拨号的地址",否则改错 ssh_config 也看不出来。
|
||||
func TestRunnerEndpointReadsDialedHostPort(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "ssh_config")
|
||||
config := "Host other\n HostName 10.0.0.9\n Port 2222\n\nHost weknora-test\n HostName 100.85.111.121\n HostKeyAlias 120.76.217.51\n"
|
||||
if err := os.WriteFile(path, []byte(config), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := New(Config{SSHConfig: path, Target: "weknora-test", ScriptDir: "/opt/weknora-test"})
|
||||
if got := runner.Endpoint(); got != "100.85.111.121:22" {
|
||||
t.Fatalf("应报真实拨号地址(默认 22),得到 %q", got)
|
||||
}
|
||||
if got := New(Config{SSHConfig: filepath.Join(t.TempDir(), "missing"), Target: "weknora-test"}).Endpoint(); got != "" {
|
||||
t.Fatalf("配置文件不可读时应返回空,不猜地址,得到 %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerRunNowReadsBackLastRun(t *testing.T) {
|
||||
calls := 0
|
||||
runner := testRunner(func(_ context.Context, _ time.Duration, remote string) (string, error) {
|
||||
|
||||
@@ -16,9 +16,8 @@ func developmentNotes(changelog, version, sha string) string {
|
||||
return ""
|
||||
}
|
||||
lines := strings.Split(changelog, "\n")
|
||||
prefix := "## 开发快照 snapshot/v" + version + " - "
|
||||
for i, line := range lines {
|
||||
if strings.HasPrefix(line, prefix) {
|
||||
if developmentHeadingMatches(line, version) {
|
||||
end := len(lines)
|
||||
for j := i + 1; j < len(lines); j++ {
|
||||
if strings.HasPrefix(lines[j], "## ") {
|
||||
@@ -39,3 +38,33 @@ func developmentNotes(changelog, version, sha string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// developmentHeadingMatches 接受两种真实标题:
|
||||
// - 旧格式:## 开发快照 snapshot/v0.1.1 - 2026-10-18
|
||||
// - 开发快照:## 开发快照 0.1.351(索引预检使用目标映射 ID)
|
||||
//
|
||||
// 版本号后必须是字符串结束或分隔符,避免 0.1.35 命中 0.1.351;
|
||||
// 只读取到下一个二级标题,不跨版本吞正文。
|
||||
func developmentHeadingMatches(line, version string) bool {
|
||||
prefixes := []string{
|
||||
"## 开发快照 snapshot/v" + version,
|
||||
"## 开发快照 " + version,
|
||||
}
|
||||
for _, prefix := range prefixes {
|
||||
if !strings.HasPrefix(line, prefix) {
|
||||
continue
|
||||
}
|
||||
rest := line[len(prefix):]
|
||||
if rest == "" {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(rest, "—") || strings.HasPrefix(rest, "(") {
|
||||
return true
|
||||
}
|
||||
switch rest[0] {
|
||||
case ' ', '\t', '-', '(':
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -44,6 +44,10 @@ const (
|
||||
// 读出来封存成快照并逐类校验,再登记成 production_effective。
|
||||
// 它不写目标内容、不清空目标、不猜版本——基线只能来自真实捕获与校验。
|
||||
LibraryJobBaseline LibraryJobKind = "baseline"
|
||||
// LibraryJobVerify 是"恢复验证":在维护窗口内捕获目标 → 校验基线一致 →
|
||||
// 写一个 canary 证明校验能发现差异 → 按预备份恢复 → 复核完全一致。
|
||||
// 它是唯一能产生"生产可恢复"证据的作业,页面不再接受手填开关。
|
||||
LibraryJobVerify LibraryJobKind = "verify"
|
||||
)
|
||||
|
||||
type LibraryJobState string
|
||||
@@ -173,6 +177,12 @@ func BaselineStages() []string {
|
||||
return []string{"capture", "verify", "finalize"}
|
||||
}
|
||||
|
||||
// VerifyStages 是恢复验证的阶段:捕获 → 基线校验 → 写 canary(证明校验有效)
|
||||
// → 按预备份恢复 → 恢复后复核。canary 与 restore 都是真实写入,失败必须可见。
|
||||
func VerifyStages() []string {
|
||||
return []string{"capture", "verify", "canary", "restore", "finalize"}
|
||||
}
|
||||
|
||||
// LibraryReleaseAuthorization 是生产发布授权:绑定目标、快照、参数摘要与有效期。
|
||||
type LibraryReleaseAuthorization struct {
|
||||
ID string `json:"id"`
|
||||
|
||||
@@ -147,7 +147,7 @@ func ValidateLibraryOperation(op LibraryOperation) error {
|
||||
return errors.New("library operation requires internal or external scope")
|
||||
}
|
||||
switch op.Kind {
|
||||
case LibraryJobSeal, LibraryJobSync, LibraryJobRestore, LibraryJobBaseline, LibraryJobRecover:
|
||||
case LibraryJobSeal, LibraryJobSync, LibraryJobRestore, LibraryJobBaseline, LibraryJobRecover, LibraryJobVerify:
|
||||
default:
|
||||
return fmt.Errorf("unsupported library operation kind %q", op.Kind)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package governance
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The recovery request has its own queue identity and stable operation. It
|
||||
@@ -18,3 +20,33 @@ func LibraryRecoveryDigest(scope, queue, window, operation, target, source, back
|
||||
"target_id": target, "source_snapshot": source, "backup_snapshot": backup, "writeset_digest": plan,
|
||||
})
|
||||
}
|
||||
|
||||
// LibraryDeliveryOwner 是队列投递 + 业务 attempt 的合成 owner:
|
||||
//
|
||||
// <worker>#<queue job>#<queue attempt>
|
||||
//
|
||||
// 它就是 library_attempts.owner,也决定了"这次派发属于哪一次真实投递"。worker 与
|
||||
// 派发台账必须用同一份格式,否则"当前 owner"会变成两处各自解释的字符串。
|
||||
func LibraryDeliveryOwner(workerID, queueID string, attempt int) string {
|
||||
if queueID == "" {
|
||||
queueID = "-"
|
||||
}
|
||||
if workerID == "" {
|
||||
workerID = "worker"
|
||||
}
|
||||
return fmt.Sprintf("%s#%s#%d", workerID, queueID, attempt)
|
||||
}
|
||||
|
||||
// ParseLibraryDeliveryOwner 把 owner 拆回 (worker, ok);queue/attempt 必须逐位相同,
|
||||
// 不能靠后缀匹配把别的投递混进来。
|
||||
func ParseLibraryDeliveryOwner(owner, queueID string, attempt int) (string, bool) {
|
||||
suffix := fmt.Sprintf("#%s#%d", queueID, attempt)
|
||||
if queueID == "" || !strings.HasSuffix(owner, suffix) {
|
||||
return "", false
|
||||
}
|
||||
worker := strings.TrimSuffix(owner, suffix)
|
||||
if worker == "" || strings.Contains(worker, "#") {
|
||||
return "", false
|
||||
}
|
||||
return worker, true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package governance
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LibraryRestoreVerification 是一次**真实**恢复验证(备份 → canary → 恢复 → 复核)的登记。
|
||||
//
|
||||
// 为什么不能继续用环境变量 PRODUCTION_BACKUP_RESTORE_VERIFIED:那是一个可以手填的开关,
|
||||
// 它既证明不了"有人真跑过恢复",也证明不了"跑的是当前目标"。这里的每条记录由 worker
|
||||
// 在真实演练结束后写入,绑定 target_id(实例+库+索引+桶)与 scope,失败也照实留痕。
|
||||
type LibraryRestoreVerification struct {
|
||||
ID string `json:"id"`
|
||||
Scope string `json:"scope"`
|
||||
Status string `json:"status"` // passed | failed
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
JobID string `json:"job_id"`
|
||||
Failure string `json:"failure,omitempty"`
|
||||
Report map[string]any `json:"report,omitempty"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
FinishedAt time.Time `json:"finished_at"`
|
||||
}
|
||||
|
||||
const (
|
||||
LibraryVerificationPassed = "passed"
|
||||
LibraryVerificationFailed = "failed"
|
||||
)
|
||||
|
||||
// LibraryVerificationSatisfied 只回答一件事:这条证据能不能支撑"当前目标的恢复验证已通过"。
|
||||
// 通过的条件必须是全部成立——状态 passed、scope 相同、快照非空、完成时间有效。
|
||||
// target_id 的匹配由查询层(按当前 target_id 过滤)保证,这里不重复比较。
|
||||
func LibraryVerificationSatisfied(verification LibraryRestoreVerification, scope string) (bool, string) {
|
||||
if verification.Status != LibraryVerificationPassed {
|
||||
reason := "最近一次恢复验证没有通过"
|
||||
if detail := strings.TrimSpace(verification.Failure); detail != "" {
|
||||
reason += ":" + detail
|
||||
}
|
||||
return false, reason
|
||||
}
|
||||
if verification.Scope != scope {
|
||||
return false, "恢复验证证据属于另一个 scope(" + verification.Scope + ")"
|
||||
}
|
||||
if strings.TrimSpace(verification.SnapshotID) == "" {
|
||||
return false, "恢复验证证据没有绑定快照,不能作为备份可恢复的证明"
|
||||
}
|
||||
if verification.FinishedAt.IsZero() {
|
||||
return false, "恢复验证证据没有完成时间"
|
||||
}
|
||||
return true, ""
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package governance
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 证据能不能支撑"当前目标可恢复"只由这四个条件回答:
|
||||
// 状态 passed、scope 相同、绑定了快照、有完成时间。少任何一条都不算。
|
||||
func TestLibraryVerificationSatisfied(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
cases := []struct {
|
||||
name string
|
||||
verification LibraryRestoreVerification
|
||||
scope string
|
||||
want bool
|
||||
reason string
|
||||
}{
|
||||
{name: "passed with snapshot", scope: "internal", want: true,
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal",
|
||||
SnapshotID: "snap-1", FinishedAt: now}},
|
||||
{name: "failed carries failure", scope: "internal", want: false, reason: "canary 后校验仍通过",
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationFailed, Scope: "internal",
|
||||
SnapshotID: "snap-1", Failure: "canary 后校验仍通过", FinishedAt: now}},
|
||||
{name: "wrong scope", scope: "internal", want: false, reason: "另一个 scope",
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "external",
|
||||
SnapshotID: "snap-1", FinishedAt: now}},
|
||||
{name: "no snapshot", scope: "internal", want: false, reason: "没有绑定快照",
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal",
|
||||
FinishedAt: now}},
|
||||
{name: "no finish time", scope: "internal", want: false, reason: "没有完成时间",
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal",
|
||||
SnapshotID: "snap-1"}},
|
||||
}
|
||||
for _, testCase := range cases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
ok, reason := LibraryVerificationSatisfied(testCase.verification, testCase.scope)
|
||||
if ok != testCase.want {
|
||||
t.Fatalf("satisfied=%v want=%v reason=%q", ok, testCase.want, reason)
|
||||
}
|
||||
if !testCase.want && testCase.reason != "" && !containsText(reason, testCase.reason) {
|
||||
t.Fatalf("reason=%q want contains %q", reason, testCase.reason)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func containsText(haystack, needle string) bool {
|
||||
return len(haystack) >= len(needle) && (haystack == needle || indexOf(haystack, needle) >= 0)
|
||||
}
|
||||
|
||||
func indexOf(haystack, needle string) int {
|
||||
for i := 0; i+len(needle) <= len(haystack); i++ {
|
||||
if haystack[i:i+len(needle)] == needle {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -36,8 +36,11 @@ type ServiceHealth struct {
|
||||
ObservedAt time.Time `json:"observed_at"`
|
||||
LastSuccessAt *time.Time `json:"last_success_at"`
|
||||
ConsecutiveFailures int `json:"consecutive_failures"`
|
||||
Configured bool `json:"configured"`
|
||||
Applicable bool `json:"applicable"`
|
||||
// ProbeTarget names the object being probed (instance + short binding id),
|
||||
// never credentials or full UUIDs, so a red light can be attributed.
|
||||
ProbeTarget string `json:"probe_target"`
|
||||
Configured bool `json:"configured"`
|
||||
Applicable bool `json:"applicable"`
|
||||
}
|
||||
|
||||
type KnowledgeBaseProgress struct {
|
||||
|
||||
@@ -116,6 +116,7 @@ type CatalogRouteRequest struct {
|
||||
// fulfillment path needs to materialize and fetch it later.
|
||||
type CatalogRouteFile struct {
|
||||
NodeID string
|
||||
ParentNodeID string
|
||||
Name string
|
||||
RelativePath string
|
||||
SizeBytes *int64
|
||||
|
||||
@@ -148,6 +148,11 @@ func classifyRole(method, path string) string {
|
||||
if strings.HasPrefix(path, "/api/v1/backup-schedule") && method != http.MethodGet {
|
||||
return roleAdmin
|
||||
}
|
||||
// 核对封存结果会把一个 pending 业务作业修成 succeeded;只允许管理员,
|
||||
// 且 handler 仍要求 completed queue、精确版本和归档复核。
|
||||
if strings.HasPrefix(path, "/api/v1/library/jobs/") && strings.HasSuffix(path, "/reconcile-capture") {
|
||||
return roleAdmin
|
||||
}
|
||||
// 版本记录的同步会拉起一次对远端仓库的抓取(消耗网络与数据库写入),
|
||||
// 与"谁能改生产绑定"同级:读任何角色可看,同步只能管理员。
|
||||
if strings.HasPrefix(path, "/api/v1/version-history") && method != http.MethodGet {
|
||||
|
||||
@@ -24,6 +24,9 @@ type BackupScheduleStore interface {
|
||||
type BackupHost interface {
|
||||
Available() bool
|
||||
Target() string
|
||||
// Endpoint 是挂载的 ssh 配置里该别名真实拨号的 host:port;页面据此证明
|
||||
// "连的是哪条通道",而不是只显示一个别名。
|
||||
Endpoint() string
|
||||
Show(context.Context) (backuphost.ShowResult, error)
|
||||
Apply(context.Context, bool, string, int, int) (backuphost.ApplyResult, error)
|
||||
RunNow(context.Context) (backuphost.RunResult, error)
|
||||
@@ -82,8 +85,11 @@ type backupScheduleView struct {
|
||||
// HostConfigured/HostReachable 区分"没接线"与"接线了但连不上"。
|
||||
HostConfigured bool `json:"host_configured"`
|
||||
HostTarget string `json:"host_target"`
|
||||
HostReachable bool `json:"host_reachable"`
|
||||
HostError string `json:"host_error"`
|
||||
// HostEndpoint 是这条别名真实拨号的 host:port(来自挂载的 ssh 配置)。
|
||||
// 连接失败时它就是"到底往哪连"的唯一证据,不能只给一个别名让人猜。
|
||||
HostEndpoint string `json:"host_endpoint"`
|
||||
HostReachable bool `json:"host_reachable"`
|
||||
HostError string `json:"host_error"`
|
||||
|
||||
HostScheduleEnabled bool `json:"host_schedule_enabled"`
|
||||
HostScheduleTime string `json:"host_schedule_time"`
|
||||
@@ -92,6 +98,8 @@ type backupScheduleView struct {
|
||||
HostCronLines []string `json:"host_cron_lines"`
|
||||
HostCronService string `json:"host_cron_service"`
|
||||
HostScriptPresent bool `json:"host_script_present"`
|
||||
// HostCheckedAt 是本次真实主机探测时间;没有探测(只读库)时为空。
|
||||
HostCheckedAt string `json:"host_checked_at,omitempty"`
|
||||
|
||||
LastRunStamp string `json:"last_run_stamp"`
|
||||
LastRunCreatedAt string `json:"last_run_created_at"`
|
||||
@@ -281,6 +289,7 @@ func (config Config) backupSchedulePayload(ctx context.Context, probeHost bool)
|
||||
}
|
||||
view.HostConfigured = config.BackupHost.Available()
|
||||
view.HostTarget = config.BackupHost.Target()
|
||||
view.HostEndpoint = config.BackupHost.Endpoint()
|
||||
if !view.HostConfigured {
|
||||
view.InSync = false
|
||||
view.HostError = "目标主机凭据未挂载(BACKUP_HOST_SSH_CONFIG / BACKUP_HOST_TARGET)"
|
||||
@@ -289,10 +298,14 @@ func (config Config) backupSchedulePayload(ctx context.Context, probeHost bool)
|
||||
if !probeHost {
|
||||
return view, nil
|
||||
}
|
||||
view.HostCheckedAt = formatTime(config.Now().UTC())
|
||||
show, err := config.BackupHost.Show(ctx)
|
||||
if err != nil {
|
||||
view.HostReachable = false
|
||||
view.HostError = err.Error()
|
||||
if view.HostEndpoint != "" {
|
||||
view.HostError += "(拨号地址 " + view.HostEndpoint + ")"
|
||||
}
|
||||
view.InSync = false
|
||||
} else {
|
||||
view.HostReachable = true
|
||||
|
||||
@@ -46,6 +46,7 @@ func (store *fakeBackupScheduleStore) MarkBackupScheduleApplied(_ context.Contex
|
||||
type fakeBackupHost struct {
|
||||
available bool
|
||||
target string
|
||||
endpoint string
|
||||
show backuphost.ShowResult
|
||||
showErr error
|
||||
apply backuphost.ApplyResult
|
||||
@@ -59,6 +60,9 @@ type fakeBackupHost struct {
|
||||
|
||||
func (host *fakeBackupHost) Available() bool { return host.available }
|
||||
func (host *fakeBackupHost) Target() string { return host.target }
|
||||
func (host *fakeBackupHost) Endpoint() string {
|
||||
return host.endpoint
|
||||
}
|
||||
func (host *fakeBackupHost) Show(context.Context) (backuphost.ShowResult, error) {
|
||||
return host.show, host.showErr
|
||||
}
|
||||
@@ -166,6 +170,48 @@ func TestHandleBackupScheduleHostFlagsDrift(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBackupScheduleHostUnreachableSeparatesConnectionFromHostState(t *testing.T) {
|
||||
// 现场原错:ssh connect to host 120.76.217.51 port 22 Operation timed out。
|
||||
// 连接没建立 ⇒ 既不能报"脚本缺失",也不能报"计划未下发",只能报不可达 + 拨号地址。
|
||||
host := &fakeBackupHost{
|
||||
available: true, target: "weknora-test", endpoint: "100.85.111.121:22",
|
||||
showErr: errors.New("目标主机 weknora-test 不可达(ssh: connect to host 120.76.217.51 port 22: Operation timed out);未执行任何主机侧脚本,脚本与计划状态未知"),
|
||||
}
|
||||
config := backupConfig(&fakeBackupScheduleStore{schedule: goldenSchedule}, host)
|
||||
recorder := httptest.NewRecorder()
|
||||
config.handleBackupScheduleHost(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/host", nil))
|
||||
data := decodeData(t, recorder)
|
||||
if data["host_reachable"] != false || data["in_sync"] != false {
|
||||
t.Fatalf("连不上时不能报告可达或已同步: %+v", data)
|
||||
}
|
||||
message := data["host_error"].(string)
|
||||
if !strings.Contains(message, "不可达") || !strings.Contains(message, "100.85.111.121:22") {
|
||||
t.Fatalf("应报不可达并给出真实拨号地址: %q", message)
|
||||
}
|
||||
if strings.Contains(message, "脚本缺失") || strings.Contains(message, "未下发") {
|
||||
t.Fatalf("未观测到的脚本/计划状态不能当成结论: %q", message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBackupScheduleHostReportsMissingScriptOnlyAfterConnecting(t *testing.T) {
|
||||
// 真正的脚本缺失:ssh 已连上,主机回的是脚本不存在 ⇒ 这时才允许说脚本缺失。
|
||||
host := &fakeBackupHost{
|
||||
available: true, target: "weknora-test", endpoint: "100.85.111.121:22",
|
||||
showErr: errors.New("已连上 weknora-test,但主机侧命令失败: bash: /opt/weknora-test/install_backup_schedule.sh: No such file or directory"),
|
||||
}
|
||||
config := backupConfig(&fakeBackupScheduleStore{schedule: goldenSchedule}, host)
|
||||
recorder := httptest.NewRecorder()
|
||||
config.handleBackupScheduleHost(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/host", nil))
|
||||
data := decodeData(t, recorder)
|
||||
message := data["host_error"].(string)
|
||||
if !strings.Contains(message, "已连上") || !strings.Contains(message, "No such file") {
|
||||
t.Fatalf("已连接但脚本缺失时应原样带回主机原因: %q", message)
|
||||
}
|
||||
if strings.Contains(message, "不可达") {
|
||||
t.Fatalf("已连上就不能说不可达: %q", message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBackupScheduleHostUnconfiguredDegrades(t *testing.T) {
|
||||
config := backupConfig(&fakeBackupScheduleStore{schedule: goldenSchedule}, &fakeBackupHost{available: false})
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
)
|
||||
|
||||
// libraryCaptureReconciler is deliberately narrow: it repairs one business
|
||||
// seal job only after the completed capture queue, the successful export stage,
|
||||
// the exact test-sealed version and the archived bytes all agree. It never
|
||||
// captures, freezes writers, creates snapshots, writes source/target content or
|
||||
// changes historical failure evidence.
|
||||
type libraryCaptureReconciler interface {
|
||||
LibraryJob(ctx context.Context, id string) (governance.LibraryJob, error)
|
||||
LibraryCaptureExportEvidence(ctx context.Context, jobID string) (string, string, error)
|
||||
LibraryTestSealedVersionForJob(ctx context.Context, scope, jobID string) (governance.LibraryVersion, bool, error)
|
||||
EnsureLibraryOperation(ctx context.Context, op governance.LibraryOperation) (governance.LibraryOperation, bool, error)
|
||||
ReconcileLibraryCaptureTerminal(ctx context.Context, jobID, scope, queueJobID, snapshotID string, now time.Time) (bool, error)
|
||||
ReconcileMisclassifiedLibraryCapture(ctx context.Context, jobID, scope, queueJobID, snapshotID, expectedStage, expectedError, expectedRecovery string, expectedFinished *time.Time, auditID, actor, reason string, evidence map[string]any, now time.Time) (bool, error)
|
||||
}
|
||||
|
||||
const (
|
||||
legacyOrphanError = "队列中已无对应作业,业务作业未收到结果"
|
||||
legacyOrphanRecovery = "封存作业由 agent 恢复来源;确认来源 healthy 后可重发"
|
||||
)
|
||||
|
||||
type libraryCaptureQueueReader interface {
|
||||
QueueJob(ctx context.Context, id string) (jobs.Job, bool, error)
|
||||
}
|
||||
|
||||
func libraryCaptureReconcile(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
return
|
||||
}
|
||||
store, ok := config.Store.(libraryCaptureReconciler)
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "封存作业核对存储不可用"})
|
||||
return
|
||||
}
|
||||
queueStore, ok := config.Store.(libraryCaptureQueueReader)
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "队列精确读取不可用"})
|
||||
return
|
||||
}
|
||||
jobID := strings.TrimSpace(r.PathValue("id"))
|
||||
if jobID == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "job id required"})
|
||||
return
|
||||
}
|
||||
job, err := store.LibraryJob(r.Context(), jobID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, map[string]any{"available": false, "reason": "作业不存在"})
|
||||
return
|
||||
}
|
||||
if job.Kind != governance.LibraryJobSeal {
|
||||
conflict(w, "只有封存作业可以核对封存结果")
|
||||
return
|
||||
}
|
||||
systemMisclassified := job.State == governance.LibraryJobFailed && job.Stage == "" &&
|
||||
job.Error == legacyOrphanError && job.Recovery == legacyOrphanRecovery && job.SnapshotID == ""
|
||||
if job.State != governance.LibraryJobPending && job.State != governance.LibraryJobRunning &&
|
||||
job.State != governance.LibraryJobSucceeded && !systemMisclassified {
|
||||
conflict(w, "作业是真实失败/取消终态,不能改写成成功")
|
||||
return
|
||||
}
|
||||
if job.QueueJobID == "" {
|
||||
conflict(w, "作业没有关联队列号,不能证明封存完成")
|
||||
return
|
||||
}
|
||||
captureJob := strings.TrimPrefix(job.ID, "libseal-")
|
||||
if captureJob == "" || captureJob == job.ID {
|
||||
conflict(w, "作业号不是封存作业身份")
|
||||
return
|
||||
}
|
||||
queue, found, err := queueStore.QueueJob(r.Context(), job.QueueJobID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read capture queue"})
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
conflict(w, "队列作业不存在,不能证明封存完成")
|
||||
return
|
||||
}
|
||||
if queue.Type != libraryCaptureJobType || queue.State != jobs.StateCompleted {
|
||||
conflict(w, "队列作业不是 completed 的 library.capture")
|
||||
return
|
||||
}
|
||||
var payload struct {
|
||||
Scope string `json:"scope"`
|
||||
CaptureJob string `json:"capture_job"`
|
||||
}
|
||||
if err := json.Unmarshal(queue.Payload, &payload); err != nil ||
|
||||
payload.Scope != job.Scope || payload.CaptureJob != captureJob ||
|
||||
queue.IdempotencyKey != "library.capture:"+captureJob {
|
||||
conflict(w, "队列作业的 scope/capture_job/幂等键与业务作业不一致")
|
||||
return
|
||||
}
|
||||
stageState, _, err := store.LibraryCaptureExportEvidence(r.Context(), job.ID)
|
||||
if err != nil || stageState != "succeeded" {
|
||||
conflict(w, "导出阶段没有成功证据,不能修复终态")
|
||||
return
|
||||
}
|
||||
version, found, err := store.LibraryTestSealedVersionForJob(r.Context(), job.Scope, job.ID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read exact test-sealed version"})
|
||||
return
|
||||
}
|
||||
if !found || version.Kind != governance.LibraryVersionTestSealed ||
|
||||
version.Scope != job.Scope || version.SourceJobID != job.ID || version.SnapshotID == "" {
|
||||
conflict(w, "没有与作业/scope 精确绑定的测试封存版本")
|
||||
return
|
||||
}
|
||||
source := configuredLibrarySource(config, job.Scope)
|
||||
sourceID, err := source.ID()
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "来源身份配置不完整,无法核对封存来源"})
|
||||
return
|
||||
}
|
||||
evidenceSource, _ := version.Evidence["source_id"].(string)
|
||||
evidenceCapture, _ := version.Evidence["capture_job"].(string)
|
||||
if evidenceSource != sourceID || evidenceCapture != captureJob {
|
||||
conflict(w, "版本来源或 capture_job 与当前作业不一致")
|
||||
return
|
||||
}
|
||||
if config.LibraryArchiveRoot == "" {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "未接线归档根,无法复核归档完整性"})
|
||||
return
|
||||
}
|
||||
archive, err := librarysnapshot.Open(config.LibraryArchiveRoot, 256*1024*1024)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "归档不可读,不能确认封存完整"})
|
||||
return
|
||||
}
|
||||
defer archive.Close()
|
||||
manifest, err := archive.Load(version.SnapshotID)
|
||||
if err != nil {
|
||||
conflict(w, "归档字节复核失败,不能修复作业")
|
||||
return
|
||||
}
|
||||
if manifest.Scope != source.Scope || manifest.TenantID != source.TenantID ||
|
||||
manifest.KnowledgeBaseID != source.KnowledgeBaseID {
|
||||
conflict(w, "归档清单来源身份与当前来源不一致")
|
||||
return
|
||||
}
|
||||
if job.SnapshotID != "" && job.SnapshotID != version.SnapshotID {
|
||||
conflict(w, "作业已有不同快照,不能覆盖")
|
||||
return
|
||||
}
|
||||
if job.State == governance.LibraryJobSucceeded {
|
||||
if job.SnapshotID != version.SnapshotID {
|
||||
conflict(w, "作业已成功但快照与精确版本不一致")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"repaired": false, "already_succeeded": true, "job_id": job.ID,
|
||||
"snapshot_id": version.SnapshotID, "queue_job_id": queue.ID,
|
||||
"version_id": version.ID,
|
||||
})
|
||||
return
|
||||
}
|
||||
actor := ""
|
||||
if identity, ok := sessionFrom(r.Context()); ok {
|
||||
actor = identity.Username
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
digest, err := governance.LibraryRequestDigest(job.Scope, governance.LibraryJobRecover, map[string]any{
|
||||
"kind": "capture_reconcile", "job_id": job.ID, "queue_job_id": queue.ID,
|
||||
"snapshot_id": version.SnapshotID, "capture_job": captureJob, "stage": "export",
|
||||
})
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot build reconcile audit digest"})
|
||||
return
|
||||
}
|
||||
if systemMisclassified {
|
||||
auditID := "librepair-" + digest[:16]
|
||||
updated, repairErr := store.ReconcileMisclassifiedLibraryCapture(r.Context(), job.ID, job.Scope,
|
||||
job.QueueJobID, version.SnapshotID, job.Stage, legacyOrphanError, legacyOrphanRecovery,
|
||||
job.FinishedAt, auditID, actor, "completed capture queue + export succeeded + exact version + archive verified; legacy orphan misclassification", map[string]any{
|
||||
"original_state": job.State, "original_error": job.Error, "original_recovery": job.Recovery,
|
||||
"original_finished_at": job.FinishedAt, "queue_job_id": queue.ID,
|
||||
"snapshot_id": version.SnapshotID, "version_id": version.ID, "archive_verified": true,
|
||||
}, now)
|
||||
if repairErr != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot persist misclassification repair"})
|
||||
return
|
||||
}
|
||||
if !updated {
|
||||
conflict(w, "原误判行状态/错误/完成时间已变化,拒绝覆盖")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"repaired": true, "already_succeeded": false, "misclassification_repaired": true,
|
||||
"job_id": job.ID, "snapshot_id": version.SnapshotID, "queue_job_id": queue.ID,
|
||||
"stage": "export", "version_id": version.ID, "audit_repair_id": auditID,
|
||||
})
|
||||
return
|
||||
}
|
||||
opID := "libop-capture-reconcile-" + digest[:16]
|
||||
op, _, err := store.EnsureLibraryOperation(r.Context(), governance.LibraryOperation{
|
||||
ID: opID, Scope: job.Scope, Kind: governance.LibraryJobRecover,
|
||||
TargetSnapshotID: version.SnapshotID, RequestDigest: digest,
|
||||
OperationKey: "capture-reconcile:" + job.ID, Actor: actor, CreatedAt: now,
|
||||
})
|
||||
if errors.Is(err, governance.ErrLibraryOperationConflict) {
|
||||
conflict(w, "同一核对键对应了不同证据,拒绝修复")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot record reconcile audit"})
|
||||
return
|
||||
}
|
||||
updated, err := store.ReconcileLibraryCaptureTerminal(r.Context(), job.ID, job.Scope, job.QueueJobID, version.SnapshotID, now)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{
|
||||
"error": "cannot persist reconciled job terminal", "audit_operation_id": op.ID,
|
||||
})
|
||||
return
|
||||
}
|
||||
if !updated {
|
||||
conflict(w, "作业状态/身份在核对期间变化,拒绝覆盖")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"repaired": true, "already_succeeded": false, "job_id": job.ID,
|
||||
"snapshot_id": version.SnapshotID, "queue_job_id": queue.ID,
|
||||
"stage": "export", "version_id": version.ID, "audit_operation_id": op.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func conflict(w http.ResponseWriter, reason string) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"available": false, "repaired": false, "reason": reason})
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
)
|
||||
|
||||
func (f *fakeLibraryStore) QueueJob(_ context.Context, id string) (jobs.Job, bool, error) {
|
||||
if f.queue == nil || f.queue.ID != id {
|
||||
return jobs.Job{}, false, nil
|
||||
}
|
||||
return *f.queue, true, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryCaptureExportEvidence(_ context.Context, _ string) (string, string, error) {
|
||||
return f.stageState, "", f.stageErr
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryJobStage(_ context.Context, _, stage string) (string, string, error) {
|
||||
if f.stageErr != nil {
|
||||
return "", "", f.stageErr
|
||||
}
|
||||
if stage != "export" {
|
||||
return "", "", errors.New("unexpected stage")
|
||||
}
|
||||
return f.stageState, "", nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryTestSealedVersionForJob(_ context.Context, scope, jobID string) (governance.LibraryVersion, bool, error) {
|
||||
if f.versionErr != nil {
|
||||
return governance.LibraryVersion{}, false, f.versionErr
|
||||
}
|
||||
if f.sealedVersion == nil || f.sealedVersion.Scope != scope || f.sealedVersion.SourceJobID != jobID {
|
||||
return governance.LibraryVersion{}, false, nil
|
||||
}
|
||||
return *f.sealedVersion, true, nil
|
||||
}
|
||||
|
||||
func reconcileFixture(t *testing.T) (*fakeLibraryStore, Config, governance.LibraryJob, []byte) {
|
||||
t.Helper()
|
||||
root := libraryFixtureRoot(t)
|
||||
snapshotID := sealSharedVersions(t, root, 1, 4)[0]
|
||||
archive, err := librarysnapshot.Open(root, 256*1024*1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifest, err := archive.Load(snapshotID)
|
||||
archive.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config := Config{LibraryArchiveRoot: root,
|
||||
LibrarySourceProject: "fixture-project", LibrarySourceApp: "fixture-app",
|
||||
LibrarySourceTenantID: manifest.TenantID, LibrarySourceInternalKB: manifest.KnowledgeBaseID}
|
||||
source := configuredLibrarySource(config, "internal")
|
||||
sourceID, err := source.ID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
job := governance.LibraryJob{ID: "libseal-cap-1", Scope: "internal", Kind: governance.LibraryJobSeal,
|
||||
State: governance.LibraryJobPending, QueueJobID: "libcap-1"}
|
||||
payload, _ := json.Marshal(map[string]string{"scope": "internal", "capture_job": "cap-1"})
|
||||
store := &fakeLibraryStore{
|
||||
jobs: []governance.LibraryJob{job},
|
||||
stageState: "succeeded",
|
||||
queue: &jobs.Job{ID: "libcap-1", Type: libraryCaptureJobType, State: jobs.StateCompleted,
|
||||
Payload: payload, IdempotencyKey: "library.capture:cap-1"},
|
||||
sealedVersion: &governance.LibraryVersion{ID: "libver-1", Scope: "internal",
|
||||
Kind: governance.LibraryVersionTestSealed, SnapshotID: snapshotID, SourceJobID: job.ID,
|
||||
Evidence: map[string]any{"source_id": sourceID, "capture_job": "cap-1"}},
|
||||
}
|
||||
config.Store = store
|
||||
return store, config, job, payload
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) ReconcileLibraryCaptureTerminal(_ context.Context, jobID, scope, queueJobID, snapshotID string, _ time.Time) (bool, error) {
|
||||
if f.reconcileDenied {
|
||||
return false, nil
|
||||
}
|
||||
for index := range f.jobs {
|
||||
if f.jobs[index].ID != jobID || f.jobs[index].Scope != scope || f.jobs[index].Kind != governance.LibraryJobSeal ||
|
||||
f.jobs[index].QueueJobID != queueJobID || (f.jobs[index].State != governance.LibraryJobPending && f.jobs[index].State != governance.LibraryJobRunning) ||
|
||||
(f.jobs[index].SnapshotID != "" && f.jobs[index].SnapshotID != snapshotID) {
|
||||
return false, nil
|
||||
}
|
||||
f.jobs[index].State = governance.LibraryJobSucceeded
|
||||
f.jobs[index].SnapshotID = snapshotID
|
||||
f.lastState = governance.LibraryJobSucceeded
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) ReconcileMisclassifiedLibraryCapture(_ context.Context, jobID, scope, queueJobID, snapshotID, expectedStage, expectedError, expectedRecovery string, expectedFinished *time.Time, _ string, _ string, _ string, _ map[string]any, _ time.Time) (bool, error) {
|
||||
if f.reconcileDenied {
|
||||
return false, nil
|
||||
}
|
||||
for index := range f.jobs {
|
||||
j := &f.jobs[index]
|
||||
if j.ID != jobID || j.Scope != scope || j.Kind != governance.LibraryJobSeal || j.QueueJobID != queueJobID ||
|
||||
j.State != governance.LibraryJobFailed || j.Stage != expectedStage || j.Error != expectedError ||
|
||||
j.Recovery != expectedRecovery || j.SnapshotID != "" || (j.FinishedAt == nil) != (expectedFinished == nil) ||
|
||||
(j.FinishedAt != nil && expectedFinished != nil && !j.FinishedAt.Equal(*expectedFinished)) {
|
||||
return false, nil
|
||||
}
|
||||
j.State = governance.LibraryJobSucceeded
|
||||
j.SnapshotID = snapshotID
|
||||
j.Stage = "export"
|
||||
f.lastState = governance.LibraryJobSucceeded
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var _ libraryCaptureReconciler = (*fakeLibraryStore)(nil)
|
||||
|
||||
func runReconcile(t *testing.T, config Config, jobID string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/"+jobID+"/reconcile-capture", nil)
|
||||
req.SetPathValue("id", jobID)
|
||||
rec := httptest.NewRecorder()
|
||||
libraryCaptureReconcile(rec, req, config)
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRepairsExactCompletedCapture(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["repaired"] != true || body["snapshot_id"] == "" || body["audit_operation_id"] == "" {
|
||||
t.Fatalf("body=%v", body)
|
||||
}
|
||||
if store.lastState != governance.LibraryJobSucceeded {
|
||||
t.Fatalf("advanced state=%s", store.lastState)
|
||||
}
|
||||
if len(store.operations) != 1 {
|
||||
t.Fatalf("audit operations=%v", store.operations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileAlreadySucceededIsReadOnly(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
job.State = governance.LibraryJobSucceeded
|
||||
job.SnapshotID = store.sealedVersion.SnapshotID
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(rec.Body.Bytes(), &body)
|
||||
if body["already_succeeded"] != true || body["repaired"] != false || store.lastState != "" {
|
||||
t.Fatalf("body=%v advanced=%q", body, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsQueueNotCompleted(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.queue.State = jobs.StateRunning
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsStageNotSucceeded(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.stageState = "failed"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsWrongVersionSource(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion.Evidence["source_id"] = "other-source"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsWrongCaptureJob(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion.Evidence["capture_job"] = "other-capture"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsBadArchive(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion.SnapshotID = "missing-snapshot"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsFailedJob(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
job.State = governance.LibraryJobFailed
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileIsIdempotent(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
first := runReconcile(t, config, job.ID)
|
||||
if first.Code != http.StatusOK {
|
||||
t.Fatalf("first code=%d body=%s", first.Code, first.Body.String())
|
||||
}
|
||||
second := runReconcile(t, config, job.ID)
|
||||
if second.Code != http.StatusOK {
|
||||
t.Fatalf("second code=%d body=%s", second.Code, second.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(second.Body.Bytes(), &body)
|
||||
if body["already_succeeded"] != true || len(store.operations) != 1 {
|
||||
t.Fatalf("body=%v ops=%v", body, store.operations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsMissingQueue(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.queue = nil
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsQueueScope(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.queue.Payload, _ = json.Marshal(map[string]string{"scope": "external", "capture_job": "cap-1"})
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsMissingVersion(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion = nil
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsWrongScopeVersion(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion.Scope = "external"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsDifferentExistingSnapshot(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
job.SnapshotID = "other-snapshot"
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileConditionalWriteRejectsConcurrentState(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.reconcileDenied = true
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRepairsExactLegacyMisclassification(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
finished := time.Now().UTC()
|
||||
job.State = governance.LibraryJobFailed
|
||||
job.Stage = ""
|
||||
job.Error = legacyOrphanError
|
||||
job.Recovery = legacyOrphanRecovery
|
||||
job.FinishedAt = &finished
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(rec.Body.Bytes(), &body)
|
||||
if body["misclassification_repaired"] != true || store.lastState != governance.LibraryJobSucceeded {
|
||||
t.Fatalf("body=%v advanced=%q", body, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsOtherFailedError(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
finished := time.Now().UTC()
|
||||
job.State = governance.LibraryJobFailed
|
||||
job.Stage = ""
|
||||
job.Error = "capture export failed"
|
||||
job.Recovery = legacyOrphanRecovery
|
||||
job.FinishedAt = &finished
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsRealFailureAndCancel(t *testing.T) {
|
||||
for _, state := range []governance.LibraryJobState{governance.LibraryJobFailed, governance.LibraryJobCancelled} {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
job.State = state
|
||||
job.Error = "real failure"
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("state=%s code=%d advanced=%q", state, rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileMisclassificationCASRejectsChange(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
finished := time.Now().UTC()
|
||||
job.State = governance.LibraryJobFailed
|
||||
job.Stage = ""
|
||||
job.Error = legacyOrphanError
|
||||
job.Recovery = legacyOrphanRecovery
|
||||
job.FinishedAt = &finished
|
||||
store.jobs[0] = job
|
||||
store.reconcileDenied = true
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
// Vertical PostgreSQL path: the real worker startup reconciliation must leave
|
||||
// a pending seal with a completed capture queue intact, then the actual HTTP
|
||||
// handler must verify archive/version/stage and repair it. This is not a fake
|
||||
// store or a direct helper test.
|
||||
func verticalReconcileFixture(t *testing.T) (*postgresstore.Store, Config, string, *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("requires explicitly isolated kg_library_test_ PostgreSQL database")
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil || !strings.HasPrefix(cfg.ConnConfig.Database, "kg_library_test_") || os.Getenv("KG_LIBRARY_TEST_ISOLATED") != "1" {
|
||||
t.Fatal("isolated database required")
|
||||
}
|
||||
cfg.MaxConns = 1
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = "pg_temp"
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
if _, err := pool.Exec(ctx, `
|
||||
CREATE TEMP TABLE library_jobs(
|
||||
id varchar(64) PRIMARY KEY, scope varchar(16) NOT NULL, kind varchar(16) NOT NULL,
|
||||
state varchar(16) NOT NULL, stage varchar(32) NOT NULL DEFAULT '',
|
||||
idempotency_key varchar(255) NOT NULL DEFAULT '', request_digest varchar(64) NOT NULL DEFAULT '',
|
||||
queue_job_id varchar(64) NOT NULL DEFAULT '', snapshot_id varchar(64) NOT NULL DEFAULT '',
|
||||
target_snapshot_id varchar(64) NOT NULL DEFAULT '', actor varchar(128) NOT NULL DEFAULT '',
|
||||
error text NOT NULL DEFAULT '', recovery varchar(255) NOT NULL DEFAULT '',
|
||||
operation_id varchar(64) NOT NULL DEFAULT '', attempt_epoch integer NOT NULL DEFAULT 0,
|
||||
created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, finished_at timestamptz,
|
||||
target_id text NOT NULL DEFAULT '');
|
||||
CREATE TEMP TABLE library_job_stages(
|
||||
job_id varchar(64) NOT NULL, stage varchar(32) NOT NULL, state varchar(16) NOT NULL,
|
||||
total bigint, completed bigint, bytes_total bigint, bytes_done bigint,
|
||||
detail text NOT NULL DEFAULT '', updated_at timestamptz NOT NULL, PRIMARY KEY(job_id,stage));
|
||||
CREATE TEMP TABLE library_versions(
|
||||
id varchar(64) PRIMARY KEY, scope varchar(16) NOT NULL, kind varchar(24) NOT NULL,
|
||||
snapshot_id varchar(64) NOT NULL DEFAULT '', version_label varchar(64) NOT NULL DEFAULT '',
|
||||
source_job_id varchar(64) NOT NULL DEFAULT '', operation_id varchar(64) NOT NULL DEFAULT '',
|
||||
attempt_epoch integer NOT NULL DEFAULT 0, evidence jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_by varchar(128) NOT NULL DEFAULT '', created_at timestamptz NOT NULL, superseded_at timestamptz);
|
||||
CREATE TEMP TABLE library_job_repairs(
|
||||
id varchar(64) PRIMARY KEY, job_id varchar(64) NOT NULL, scope varchar(16) NOT NULL,
|
||||
kind varchar(16) NOT NULL, queue_job_id varchar(64) NOT NULL, snapshot_id varchar(64) NOT NULL,
|
||||
original_state varchar(16) NOT NULL, original_stage varchar(32) NOT NULL DEFAULT '',
|
||||
original_error text NOT NULL DEFAULT '', original_recovery varchar(255) NOT NULL DEFAULT '',
|
||||
original_finished_at timestamptz, reason varchar(255) NOT NULL, actor varchar(128) NOT NULL DEFAULT '',
|
||||
evidence jsonb NOT NULL DEFAULT '{}'::jsonb, created_at timestamptz NOT NULL);
|
||||
CREATE TEMP TABLE library_operations(
|
||||
id varchar(64) PRIMARY KEY, scope varchar(16) NOT NULL, kind varchar(16) NOT NULL,
|
||||
target_snapshot_id varchar(64) NOT NULL DEFAULT '', request_digest varchar(64) NOT NULL,
|
||||
operation_key varchar(255) NOT NULL, actor varchar(128) NOT NULL DEFAULT '',
|
||||
created_at timestamptz NOT NULL, target_id text NOT NULL DEFAULT '',
|
||||
first_backup_snapshot_id varchar(64), first_backup_bytes bigint, first_backup_at timestamptz,
|
||||
writeset_digest varchar(64), writeset_rows bigint, target_fingerprint varchar(128), anchors_at timestamptz);
|
||||
CREATE UNIQUE INDEX library_operations_key_test ON library_operations(scope,kind,operation_key);
|
||||
CREATE TEMP TABLE jobs(
|
||||
id varchar(64) PRIMARY KEY, type varchar(64) NOT NULL, payload jsonb NOT NULL,
|
||||
state varchar(16) NOT NULL, priority integer NOT NULL DEFAULT 0, available_at timestamptz NOT NULL,
|
||||
lease_owner varchar(128), lease_until timestamptz, attempt integer NOT NULL DEFAULT 0,
|
||||
max_attempts integer NOT NULL DEFAULT 1, last_error text, idempotency_key varchar(255) NOT NULL,
|
||||
created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL);`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := libraryFixtureRoot(t)
|
||||
snapshotID := sealSharedVersions(t, root, 1, 4)[0]
|
||||
archive, err := librarysnapshot.Open(root, 256*1024*1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifest, err := archive.Load(snapshotID)
|
||||
archive.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config := Config{LibraryArchiveRoot: root,
|
||||
LibrarySourceProject: "fixture-project", LibrarySourceApp: "fixture-app",
|
||||
LibrarySourceTenantID: manifest.TenantID, LibrarySourceInternalKB: manifest.KnowledgeBaseID}
|
||||
source := configuredLibrarySource(config, "internal")
|
||||
sourceID, err := source.ID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
payload, _ := json.Marshal(map[string]string{"scope": "internal", "capture_job": "vertical"})
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO jobs(id,type,payload,state,available_at,idempotency_key,created_at,updated_at)
|
||||
VALUES('q-vertical','library.capture',$1,'completed',$2,'library.capture:vertical',$2,$2)`, payload, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_versions(id,scope,kind,snapshot_id,source_job_id,evidence,created_at)
|
||||
VALUES('ver-vertical','internal','test_sealed',$1,'libseal-vertical',$2,$3)`, snapshotID,
|
||||
map[string]any{"source_id": sourceID, "capture_job": "vertical"}, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_job_stages(job_id,stage,state,detail,updated_at)
|
||||
VALUES('libseal-vertical','export','succeeded','已封存快照', $1)`, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := postgresstore.NewStore(pool)
|
||||
config.Store = store
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_jobs(id,scope,kind,state,idempotency_key,request_digest,queue_job_id,created_at,updated_at)
|
||||
VALUES('libseal-vertical','internal','seal','pending','k','d','q-vertical',$1,$1)`, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store, config, snapshotID, pool
|
||||
}
|
||||
|
||||
func TestStartupReconcileThenHandlerRepairsPendingCapture(t *testing.T) {
|
||||
store, config, _, _ := verticalReconcileFixture(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
if n, err := store.ReconcileLibraryJobs(ctx, now, []string{"seal"}); err != nil || n != 0 {
|
||||
t.Fatalf("startup reconcile n=%d err=%v", n, err)
|
||||
}
|
||||
job, err := store.LibraryJob(ctx, "libseal-vertical")
|
||||
if err != nil || job.State != governance.LibraryJobPending {
|
||||
t.Fatalf("startup changed job: %+v err=%v", job, err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/libseal-vertical/reconcile-capture", nil)
|
||||
req.SetPathValue("id", "libseal-vertical")
|
||||
rec := httptest.NewRecorder()
|
||||
libraryCaptureReconcile(rec, req, config)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("handler code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
job, err = store.LibraryJob(ctx, "libseal-vertical")
|
||||
if err != nil || job.State != governance.LibraryJobSucceeded || job.SnapshotID == "" {
|
||||
t.Fatalf("handler did not repair: %+v err=%v", job, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartupReconcilePreservesFailedCapturesForExactReconcile(t *testing.T) {
|
||||
store, config, snapshotID, pool := verticalReconcileFixture(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
source := configuredLibrarySource(config, "internal")
|
||||
sourceID, err := source.ID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]string{"scope": "internal", "capture_job": "vertical-failed"})
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO jobs(id,type,payload,state,available_at,idempotency_key,created_at,updated_at)
|
||||
VALUES('q-failed','library.capture',$1,'completed',$2,'library.capture:vertical-failed',$2,$2)`, payload, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_versions(id,scope,kind,snapshot_id,source_job_id,evidence,created_at)
|
||||
VALUES('ver-failed','internal','test_sealed',$1,'libseal-vertical-failed',$2,$3)`,
|
||||
snapshotID, map[string]any{"source_id": sourceID, "capture_job": "vertical-failed"}, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_job_stages(job_id,stage,state,detail,updated_at)
|
||||
VALUES('libseal-vertical-failed','export','succeeded','已封存快照', $1)`, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
finished := now.Truncate(time.Microsecond)
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_jobs(id,scope,kind,state,idempotency_key,request_digest,queue_job_id,error,recovery,created_at,updated_at,finished_at)
|
||||
VALUES('libseal-vertical-failed','internal','seal','failed','kf','df','q-failed',$1,$2,$3,$3,$3)`,
|
||||
"队列中已无对应作业,业务作业未收到结果", "封存作业由 agent 恢复来源;确认来源 healthy 后可重发", finished); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n, err := store.ReconcileLibraryJobs(ctx, now, []string{"seal"}); err != nil || n != 0 {
|
||||
t.Fatalf("startup reconcile n=%d err=%v", n, err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/libseal-vertical-failed/reconcile-capture", nil)
|
||||
req.SetPathValue("id", "libseal-vertical-failed")
|
||||
rec := httptest.NewRecorder()
|
||||
libraryCaptureReconcile(rec, req, config)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("handler code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
job, err := store.LibraryJob(ctx, "libseal-vertical-failed")
|
||||
if err != nil || job.State != governance.LibraryJobSucceeded || job.SnapshotID != snapshotID {
|
||||
t.Fatalf("failed misclassification not repaired: %+v err=%v", job, err)
|
||||
}
|
||||
var originalState, originalError string
|
||||
if err := pool.QueryRow(ctx, `SELECT original_state, original_error FROM library_job_repairs WHERE job_id='libseal-vertical-failed'`).
|
||||
Scan(&originalState, &originalError); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if originalState != "failed" || originalError == "" {
|
||||
t.Fatalf("audit missing original: state=%q error=%q", originalState, originalError)
|
||||
}
|
||||
}
|
||||
@@ -342,7 +342,11 @@ type libraryOverview struct {
|
||||
CaptureAvailable bool `json:"capture_available"`
|
||||
CaptureReason string `json:"capture_reason,omitempty"`
|
||||
Executor libraryExecutorView `json:"executor"`
|
||||
Now time.Time `json:"now"`
|
||||
// 实际目标身份:页面不能只显示"weknora-test"或旧文档级绑定名称。
|
||||
TargetInstance string `json:"target_instance,omitempty"`
|
||||
TargetProject string `json:"target_project,omitempty"`
|
||||
TargetKnowledgeBaseID string `json:"target_knowledge_base_id,omitempty"`
|
||||
Now time.Time `json:"now"`
|
||||
}
|
||||
|
||||
// libraryVersionRef 只回答"这个快照被登记成哪一类版本、由哪个作业登记"。
|
||||
@@ -595,6 +599,11 @@ func testSealedOf(versions []governance.LibraryVersion, byID map[string]libraryS
|
||||
// 它刻意只说配置:能停写入者/关入口/证明排空的真实能力在 worker 侧(docker socket、
|
||||
// 闸门、排空文件),API 若声称"能力可用"就是拿配置字符串冒充执行证据。
|
||||
func libraryMaintenanceCapability(config Config) map[string]any {
|
||||
mode := strings.TrimSpace(config.LibraryTargetIngressMode)
|
||||
if mode == "" {
|
||||
mode = "gate"
|
||||
}
|
||||
noGate := mode == "none"
|
||||
missing := make([]string, 0, 6)
|
||||
if config.LibraryTargetInstance == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INSTANCE")
|
||||
@@ -605,11 +614,18 @@ func libraryMaintenanceCapability(config Config) map[string]any {
|
||||
if len(config.LibraryTargetWriters) == 0 {
|
||||
missing = append(missing, "LIBRARY_TARGET_WRITERS")
|
||||
}
|
||||
if config.LibraryTargetIngressURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_URL")
|
||||
}
|
||||
if config.LibraryTargetIngressService == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_SERVICE")
|
||||
if noGate {
|
||||
// 没有闸门:页面必须如实说"无闸门(测试模式)",不能显示"打开"。
|
||||
if config.LibraryTargetHealthURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_HEALTH_URL")
|
||||
}
|
||||
} else {
|
||||
if config.LibraryTargetIngressURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_URL")
|
||||
}
|
||||
if config.LibraryTargetIngressService == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_SERVICE")
|
||||
}
|
||||
}
|
||||
if config.LibraryTargetJournalRoot == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_JOURNAL_ROOT")
|
||||
@@ -617,12 +633,18 @@ func libraryMaintenanceCapability(config Config) map[string]any {
|
||||
if config.LibraryTargetDockerSocket == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_DOCKER_SOCKET")
|
||||
}
|
||||
ingressState := "闸门"
|
||||
if noGate {
|
||||
ingressState = "无闸门(测试模式)"
|
||||
}
|
||||
return map[string]any{
|
||||
"configured": len(missing) == 0,
|
||||
"missing": missing,
|
||||
"instance": config.LibraryTargetInstance,
|
||||
"project": config.LibraryTargetProject,
|
||||
"writers": config.LibraryTargetWriters,
|
||||
"ingress_mode": mode,
|
||||
"ingress_state": ingressState,
|
||||
"ingress_service": config.LibraryTargetIngressService,
|
||||
"note": "配置齐全只说明治理侧接线完整;停写入者、关入口与排空由 worker 在作业内校验并执行",
|
||||
}
|
||||
@@ -642,9 +664,7 @@ func librarySyncGateWithGrant(config Config, production libraryKnownView, prebac
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
gate.Reasons = append(gate.Reasons, "生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false)")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
gate.Reasons = append(gate.Reasons, "没有已核验的生产恢复演练证据(PRODUCTION_BACKUP_RESTORE_VERIFIED=false)")
|
||||
}
|
||||
gate.Reasons = append(gate.Reasons, restoreGateReasons(config)...)
|
||||
// 生产端点以"整库目标是否配置"为准:整库同步直接写 PG/ES/对象,
|
||||
// 不依赖 WeKnora 的 HTTP 端,所以不能拿它是否配置当门禁。
|
||||
if strings.TrimSpace(config.LibraryProductionDatabaseURL) == "" {
|
||||
@@ -688,10 +708,16 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
overview := libraryOverview{Scope: scope, Jobs: []governance.LibraryJob{}, Now: now,
|
||||
TargetInstance: config.LibraryTargetInstance, TargetProject: config.LibraryTargetProject,
|
||||
Gate: libraryGateView{Reasons: []string{}},
|
||||
Diff: libraryDiffView{State: "unknown", Reason: "生产与测试的文档级差异读取未接线", Added: []any{}, Updated: []any{}, Removed: []any{}}}
|
||||
// 可发起封存 = worker 真实探测到执行器可用且指向本实例;原因逐条给全。
|
||||
overview.CaptureAvailable, overview.CaptureReason, overview.Executor = libraryExecutorAvailability(r.Context(), config)
|
||||
if scope == "internal" {
|
||||
overview.TargetKnowledgeBaseID = config.LibraryProductionInternalKB
|
||||
} else {
|
||||
overview.TargetKnowledgeBaseID = config.LibraryProductionExternalKB
|
||||
}
|
||||
|
||||
// 测试封存来自归档 + 版本登记:归档里**不止**测试封存——生产预备份与目标基线
|
||||
// 快照同样带 scope,如果只按"这个 scope 最新一份"挑,页面就会把目标自己的内容
|
||||
@@ -814,7 +840,8 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
|
||||
|
||||
grant, hasGrant := activeGrantFor(r.Context(), config, scope, target)
|
||||
overview.Authorization = authorizationViewOf(grant, hasGrant)
|
||||
overview.Gate = librarySyncGateWithGrant(config, overview.Production, overview.Prebackup, target, hasGrant)
|
||||
// 闸口按当前目标的真实恢复证据判断(没有证据时也接受历史 env 覆盖)。
|
||||
overview.Gate = librarySyncGateWithGrant(config.withRestoreEvidence(r.Context(), scope), overview.Production, overview.Prebackup, target, hasGrant)
|
||||
writeJSON(w, http.StatusOK, overview)
|
||||
}
|
||||
|
||||
@@ -853,7 +880,38 @@ func libraryJobDetailHandler(w http.ResponseWriter, r *http.Request, config Conf
|
||||
writeJSON(w, http.StatusNotFound, map[string]any{"available": false, "reason": "作业不存在"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"available": true, "job": job})
|
||||
detail := map[string]any{"available": true, "job": job}
|
||||
// 只读诊断:明确"目标现在被谁按着、入口是什么状态、下一步该怎么做"。
|
||||
// 这里刻意不提供任何"强制成功/强制开门"的入口:缺归属证据的作业必须停在
|
||||
// held,由人工按预备份与校验证据处置(已接受的可用性边界)。
|
||||
if maintenanceStore, ok := config.Store.(interface {
|
||||
LibraryMaintenanceOf(ctx context.Context, scope string) (*postgresstore.LibraryMaintenance, error)
|
||||
}); ok {
|
||||
maintenance, maintenanceErr := maintenanceStore.LibraryMaintenanceOf(r.Context(), job.Scope)
|
||||
if maintenanceErr != nil || maintenance == nil {
|
||||
writeJSON(w, http.StatusOK, detail)
|
||||
return
|
||||
}
|
||||
held := maintenance.ReleasedAt == nil
|
||||
detail["maintenance"] = map[string]any{
|
||||
"held": held,
|
||||
"owner_job_id": maintenance.OwnerJobID,
|
||||
"owner_is_job": maintenance.OwnerJobID == job.ID,
|
||||
"state": maintenance.State,
|
||||
"ingress": maintenance.IngressState,
|
||||
"target_identity": maintenance.TargetIdentity,
|
||||
"note": maintenance.Note,
|
||||
"acquired_at": maintenance.AcquiredAt,
|
||||
"released": !held,
|
||||
}
|
||||
if held && job.State == governance.LibraryJobFailed {
|
||||
detail["next_step"] = "内容可能已写入并通过校验,但收尾未完成:目标仍处于维护、入口未开。按本作业的预备份与校验证据人工核验后再放行;本接口不提供强制成功。"
|
||||
}
|
||||
}
|
||||
if _, ok := detail["next_step"]; !ok && job.State == governance.LibraryJobFailed && job.Recovery != "" {
|
||||
detail["next_step"] = job.Recovery
|
||||
}
|
||||
writeJSON(w, http.StatusOK, detail)
|
||||
}
|
||||
|
||||
// librarySyncEnqueue 是生产同步的唯一入口:闸口未齐时**不建作业**,直接说明缺项。
|
||||
@@ -932,7 +990,7 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
return
|
||||
}
|
||||
}
|
||||
gate := librarySyncGateWithGrant(config, overview.Production, overview.Prebackup, request.Target, hasGrant)
|
||||
gate := librarySyncGateWithGrant(config.withRestoreEvidence(r.Context(), request.Scope), overview.Production, overview.Prebackup, request.Target, hasGrant)
|
||||
if !gate.SyncAllowed {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{
|
||||
"code": "library_sync_blocked", "reasons": gate.Reasons,
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
)
|
||||
|
||||
// 生产闸口与整库状态契约的针对性测试:不做数据库集成,只验证
|
||||
@@ -33,6 +34,17 @@ type fakeLibraryStore struct {
|
||||
// operations/lastOperation 记录"意图身份":重放与换键的差别在这里表达(F01)。
|
||||
operations map[string]governance.LibraryOperation
|
||||
lastOperation governance.LibraryOperation
|
||||
// 续作:resumeOK/resumeErr 驱动存储层结果,resumeCalls 记录每次调用(断言不重复投递)。
|
||||
resumeOK bool
|
||||
resumeErr error
|
||||
resumeCalls []string
|
||||
// capture reconciliation fixtures
|
||||
queue *jobs.Job
|
||||
stageState string
|
||||
stageErr error
|
||||
sealedVersion *governance.LibraryVersion
|
||||
versionErr error
|
||||
reconcileDenied bool
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) CreateLibraryJob(_ context.Context, job governance.LibraryJob, _ []string, _ time.Time) (governance.LibraryJob, bool, error) {
|
||||
@@ -59,6 +71,11 @@ func (f *fakeLibraryStore) EnsureLibraryOperation(_ context.Context, op governan
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryJob(_ context.Context, id string) (governance.LibraryJob, error) {
|
||||
for _, job := range f.jobs {
|
||||
if job.ID == id {
|
||||
return job, nil
|
||||
}
|
||||
}
|
||||
return governance.LibraryJob{ID: id}, nil
|
||||
}
|
||||
|
||||
@@ -118,8 +135,16 @@ func (f *fakeLibraryStore) UpdateLibraryStage(_ context.Context, _, _, _ string,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) AdvanceLibraryJob(_ context.Context, _ string, state governance.LibraryJobState, _, _, _, _, _ string, _ time.Time) error {
|
||||
func (f *fakeLibraryStore) AdvanceLibraryJob(_ context.Context, jobID string, state governance.LibraryJobState, _, _, _, snapshotID, _ string, _ time.Time) error {
|
||||
f.lastState = state
|
||||
for index := range f.jobs {
|
||||
if f.jobs[index].ID == jobID {
|
||||
f.jobs[index].State = state
|
||||
if snapshotID != "" {
|
||||
f.jobs[index].SnapshotID = snapshotID
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -178,6 +203,23 @@ func (f *fakeLibraryStore) ResetQueueJobForResume(_ context.Context, _ string, _
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResumeLibraryJob 模拟存储层事务语义:结果由 resumeResult 驱动,调用次数可断言"重复请求不重复投递"。
|
||||
func (f *fakeLibraryStore) ResumeLibraryJob(_ context.Context, jobID, queueType string, _ time.Time) (governance.LibraryJob, bool, error) {
|
||||
f.resumeCalls = append(f.resumeCalls, jobID+"/"+queueType)
|
||||
if f.resumeErr != nil {
|
||||
return governance.LibraryJob{}, false, f.resumeErr
|
||||
}
|
||||
if !f.resumeOK {
|
||||
return governance.LibraryJob{}, false, nil
|
||||
}
|
||||
for _, job := range f.jobs {
|
||||
if job.ID == jobID {
|
||||
return job, true, nil
|
||||
}
|
||||
}
|
||||
return governance.LibraryJob{}, false, nil
|
||||
}
|
||||
|
||||
func TestLibrarySyncGateRequiresEveryPieceOfEvidence(t *testing.T) {
|
||||
known := libraryKnownView{State: "known", Version: &libraryVersionView{ID: "v1"}}
|
||||
cases := []struct {
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
type r13HeldDetailStore struct {
|
||||
fakeLibraryStore
|
||||
maintenance *postgresstore.LibraryMaintenance
|
||||
job governance.LibraryJob
|
||||
}
|
||||
|
||||
func (s *r13HeldDetailStore) LibraryJob(_ context.Context, id string) (governance.LibraryJob, error) {
|
||||
job := s.job
|
||||
job.ID = id
|
||||
return job, nil
|
||||
}
|
||||
|
||||
func (s *r13HeldDetailStore) LibraryMaintenanceOf(context.Context, string) (*postgresstore.LibraryMaintenance, error) {
|
||||
return s.maintenance, nil
|
||||
}
|
||||
|
||||
// 已接受的边界:缺归属证据的作业停在 held,页面/接口必须给出可核对的 job/operation/目标
|
||||
// 与阻断原因,并且**不能**提供"强制成功/强制开门"的动作。
|
||||
func TestR13HeldJobDetailExplainsManualRecovery(t *testing.T) {
|
||||
store := &r13HeldDetailStore{maintenance: &postgresstore.LibraryMaintenance{
|
||||
Scope: "internal", OwnerJobID: "job-held", State: "needs_restore",
|
||||
IngressState: "open", TargetIdentity: "scope=internal", Note: "内容已验证,但收尾未完成",
|
||||
Services: []string{"app"},
|
||||
}}
|
||||
store.job = governance.LibraryJob{
|
||||
Scope: "internal", Kind: governance.LibraryJobSync, State: governance.LibraryJobFailed,
|
||||
Stage: "finalize", OperationID: "op-r13", AttemptEpoch: 2,
|
||||
SnapshotID: "backup-r13", TargetSnapshotID: "target-r13",
|
||||
Error: "内容已验证,但收尾未完成", Recovery: "预备份保留;按归档人工核验",
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/jobs/job-held", nil)
|
||||
request.SetPathValue("id", "job-held")
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryJobDetailHandler(recorder, request, Config{Store: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
job, _ := body["job"].(map[string]any)
|
||||
if job["operation_id"] != "op-r13" || job["attempt_epoch"] != float64(2) ||
|
||||
job["snapshot_id"] != "backup-r13" || job["target_snapshot_id"] != "target-r13" {
|
||||
t.Fatalf("作业身份/证据不完整:%v", job)
|
||||
}
|
||||
maintenance, _ := body["maintenance"].(map[string]any)
|
||||
if maintenance["held"] != true || maintenance["owner_is_job"] != true || maintenance["state"] != "needs_restore" {
|
||||
t.Fatalf("维护状态没有暴露:%v", maintenance)
|
||||
}
|
||||
step, _ := body["next_step"].(string)
|
||||
if !strings.Contains(step, "维护") || !strings.Contains(step, "人工") {
|
||||
t.Fatalf("下一步没有指向人工核验:%q", step)
|
||||
}
|
||||
raw := strings.ToLower(recorder.Body.String())
|
||||
for _, banned := range []string{"force_success", "force-open", "force_open", "ignore_unknown", "skip_verify"} {
|
||||
if strings.Contains(raw, banned) {
|
||||
t.Fatalf("诊断接口出现了强制动作:%s", banned)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -266,3 +266,25 @@ func TestLibraryReadPathsRejectTamperedManifest(t *testing.T) {
|
||||
t.Fatalf("storage must fail closed: status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// F11: the HTTP projection must include binding and Wiki log counts; the
|
||||
// previous projection silently omitted them and the UI turned absence into 0.
|
||||
func TestLibrarySnapshotTotalsIncludeBindingsAndWikiLogs(t *testing.T) {
|
||||
view := librarySnapshotViewOfNoUsage("snap-totals", librarysnapshot.Manifest{
|
||||
Scope: "internal", KnowledgeBaseID: "kb", TenantID: 10000,
|
||||
Parts: []librarysnapshot.Part{
|
||||
{Kind: "database", Name: "knowledges", Count: 4},
|
||||
{Kind: "database", Name: "chunks", Count: 38},
|
||||
{Kind: "database", Name: "resources", Count: 21},
|
||||
{Kind: "database", Name: "resource_bindings", Count: 4},
|
||||
{Kind: "wiki", Name: "wiki_pages", Count: 31},
|
||||
{Kind: "wiki", Name: "wiki_folders", Count: 21},
|
||||
{Kind: "wiki", Name: "wiki_log_entries", Count: 11},
|
||||
},
|
||||
})
|
||||
for key, want := range map[string]int64{"documents": 4, "chunks": 38, "resources": 21, "bindings": 4, "wiki_pages": 31, "wiki_folders": 21, "wiki_log_entries": 11} {
|
||||
if view.Totals[key] != want {
|
||||
t.Fatalf("totals[%s]=%d want=%d (%v)", key, view.Totals[key], want, view.Totals)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"knowledge-governance/internal/platform/postgres"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
)
|
||||
|
||||
// 按指定整库版本恢复:与同步分开的作业类型,需要**单独的管理员授权**,
|
||||
@@ -71,9 +71,7 @@ func libraryRestoreEnqueue(w http.ResponseWriter, r *http.Request, config Config
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
reasons = append(reasons, "生产写入闸口关闭")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
reasons = append(reasons, "没有已核验的生产恢复演练证据")
|
||||
}
|
||||
reasons = append(reasons, restoreGateReasons(config.withRestoreEvidence(r.Context(), request.Scope))...)
|
||||
authorizationOK := false
|
||||
if storeWithAuth, ok := config.Store.(interface {
|
||||
ActiveLibraryAuthorization(ctx context.Context, scope, snapshotID, digest string, now time.Time) (governance.LibraryReleaseAuthorization, bool, error)
|
||||
@@ -169,9 +167,7 @@ func libraryRestoreAvailability(w http.ResponseWriter, r *http.Request, config C
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
view.Reasons = append(view.Reasons, "生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false)")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
view.Reasons = append(view.Reasons, "没有已核验的生产恢复演练证据(PRODUCTION_BACKUP_RESTORE_VERIFIED=false)")
|
||||
}
|
||||
view.Reasons = append(view.Reasons, restoreGateReasons(config.withRestoreEvidence(r.Context(), scope))...)
|
||||
digest, err := libraryRestoreParamsDigest(scope, snapshotID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot digest params"})
|
||||
@@ -228,9 +224,7 @@ func libraryJobResume(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
reasons = append(reasons, "生产写入闸口关闭")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
reasons = append(reasons, "没有已核验的生产恢复演练证据")
|
||||
}
|
||||
reasons = append(reasons, restoreGateReasons(config.withRestoreEvidence(r.Context(), source.Scope))...)
|
||||
var digest string
|
||||
switch source.Kind {
|
||||
case governance.LibraryJobSync:
|
||||
@@ -271,42 +265,47 @@ func libraryJobResume(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
// id 落盘。另建 `-resume` 作业只能继承 operation,其它所有者仍指向旧作业:
|
||||
// AcquireInstanceMaintenance 会拒绝它、journal 与预备份也查不到——那样"续作"只是
|
||||
// 一个漂亮的新行,接不上原窗口。原作业 + 新 attempt 才是批准协议的形状。
|
||||
prepared, resumed, err := store.PrepareLibraryJobResume(r.Context(), sourceID, now)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot prepare resumed job"})
|
||||
// 阶段/状态/执行权与载荷全部在存储层的事务里校验并构造:handler 手里的 source 只用于
|
||||
// 授权与闸口判断,不参与续作决策,避免用过期快照复活一个已经变化的作业。
|
||||
resumer, ok := config.Store.(interface {
|
||||
ResumeLibraryJob(ctx context.Context, jobID, queueType string, now time.Time) (governance.LibraryJob, bool, error)
|
||||
})
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"error": "作业存储不支持续作"})
|
||||
return
|
||||
}
|
||||
if !resumed {
|
||||
// 状态在检查之后被别的执行者推进(已接管/已完成):不许再塞一次。
|
||||
prepared, resumed, err := resumer.ResumeLibraryJob(r.Context(), sourceID, queueType, now)
|
||||
switch {
|
||||
case errors.Is(err, postgres.ErrLibraryResumeBusy):
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_resume_busy",
|
||||
"error": "该作业已有执行在跑(租约仍有效),等它结束再续作"})
|
||||
return
|
||||
case errors.Is(err, postgres.ErrLibraryResumeRequiresRecover):
|
||||
// 窗口是否仍被持有由维护记录决定:已释放的回滚作业说"窗口已释放",不能一律宣称仍持有。
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_resume_requires_recover",
|
||||
"error": "作业停在回滚阶段,请走维护恢复而不是续作",
|
||||
"maintenance_held": maintenanceHeldFor(r.Context(), config, source)})
|
||||
return
|
||||
case err != nil:
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot resume job"})
|
||||
return
|
||||
case !resumed:
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_resume_state_changed",
|
||||
"error": "作业已不在可续作状态(可能已被接管或完成)"})
|
||||
return
|
||||
}
|
||||
// 写入已完成、只差登记版本的情况:续作只做幂等收尾,不重写生产。
|
||||
body := map[string]any{"library_job_id": prepared.ID, "scope": source.Scope,
|
||||
"target_snapshot_id": source.TargetSnapshotID}
|
||||
if source.Stage == "finalize" && source.SnapshotID != "" {
|
||||
body["finalize_only"] = true
|
||||
}
|
||||
payload, _ := json.Marshal(body)
|
||||
queueID := governance.QueueJobIDFor(prepared.ID)
|
||||
if _, err := config.Store.Enqueue(r.Context(), jobs.EnqueueRequest{
|
||||
ID: queueID, Type: queueType, Payload: payload, AvailableAt: now,
|
||||
MaxAttempts: 1, IdempotencyKey: "library.resume.queue:" + prepared.ID,
|
||||
}, now); err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot enqueue resumed job"})
|
||||
return
|
||||
}
|
||||
// 队列行可能还是上一次的终态:重置为 pending。仍有效的租约 ⇒ 有执行在跑,让路。
|
||||
if resetErr := store.ResetQueueJobForResume(r.Context(), queueID, now); resetErr != nil {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_resume_busy",
|
||||
"error": "该作业已有执行在跑(租约仍有效),等它结束再续作", "detail": errString(resetErr)})
|
||||
return
|
||||
}
|
||||
if attachErr := store.AttachLibraryQueueJob(r.Context(), prepared.ID, queueID, now); attachErr != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot attach resumed job"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusAccepted, map[string]any{"job": prepared, "resumed_from": sourceID,
|
||||
"resumed": true, "next_attempt_epoch": prepared.AttemptEpoch + 1})
|
||||
}
|
||||
|
||||
// maintenanceHeldFor 只回答"这个作业现在是否仍持有维护窗口",用于给回滚作业的拒绝文案定性。
|
||||
func maintenanceHeldFor(ctx context.Context, config Config, job governance.LibraryJob) bool {
|
||||
reader, ok := config.Store.(interface {
|
||||
LibraryMaintenanceOf(ctx context.Context, scope string) (*postgres.LibraryMaintenance, error)
|
||||
})
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
m, err := reader.LibraryMaintenanceOf(ctx, job.Scope)
|
||||
return err == nil && m != nil && m.OwnerJobID == job.ID && m.ReleasedAt == nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
func resumeRequest(id string) *http.Request {
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/"+id+"/resume", nil)
|
||||
request.SetPathValue("id", id)
|
||||
return request.WithContext(context.WithValue(request.Context(), sessionKey{},
|
||||
sessionIdentity{Username: "admin", Role: roleAdmin}))
|
||||
}
|
||||
|
||||
func resumableStore(job governance.LibraryJob) *fakeLibraryStore {
|
||||
now := time.Now().UTC()
|
||||
digest, _ := librarySyncParamsDigest(job.Scope, job.TargetSnapshotID)
|
||||
if job.Kind == governance.LibraryJobRestore {
|
||||
digest, _ = libraryRestoreParamsDigest(job.Scope, job.TargetSnapshotID)
|
||||
}
|
||||
store := &fakeLibraryStore{
|
||||
jobs: []governance.LibraryJob{job},
|
||||
grant: &governance.LibraryReleaseAuthorization{ID: "a", Scope: job.Scope,
|
||||
SnapshotID: job.TargetSnapshotID, ParamsDigest: digest, ExpiresAt: now.Add(time.Hour), CreatedAt: now},
|
||||
}
|
||||
store.resumeOK = true
|
||||
return store
|
||||
}
|
||||
|
||||
func resumeConfig(store *fakeLibraryStore) Config {
|
||||
return Config{Store: store, ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true}
|
||||
}
|
||||
|
||||
// 合法续作:202,并且只投递一次。
|
||||
func TestR26ResumeAcceptedDeliversOnce(t *testing.T) {
|
||||
job := governance.LibraryJob{ID: "job-ok", Scope: "internal", Kind: governance.LibraryJobSync,
|
||||
State: governance.LibraryJobFailed, Stage: "resources", TargetSnapshotID: "snap-1"}
|
||||
store := resumableStore(job)
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryJobResume(recorder, resumeRequest("job-ok"), resumeConfig(store))
|
||||
if recorder.Code != http.StatusAccepted {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if len(store.resumeCalls) != 1 || store.resumeCalls[0] != "job-ok/library.sync" {
|
||||
t.Fatalf("resume calls=%v", store.resumeCalls)
|
||||
}
|
||||
}
|
||||
|
||||
// 回滚阶段的作业:409,且文案不能一律宣称"仍持有维护窗口"(窗口可能已释放)。
|
||||
func TestR26ResumeRollbackStageRequiresRecover(t *testing.T) {
|
||||
job := governance.LibraryJob{ID: "job-rb", Scope: "internal", Kind: governance.LibraryJobRestore,
|
||||
State: governance.LibraryJobFailed, Stage: "rollback", TargetSnapshotID: "snap-1"}
|
||||
store := resumableStore(job)
|
||||
store.resumeErr = postgres.ErrLibraryResumeRequiresRecover
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryJobResume(recorder, resumeRequest("job-rb"), resumeConfig(store))
|
||||
if recorder.Code != http.StatusConflict || !strings.Contains(recorder.Body.String(), "library_resume_requires_recover") {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if strings.Contains(recorder.Body.String(), "保留维护窗口") {
|
||||
t.Fatalf("must not always claim the window is held: %s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 租约仍有效:409 busy,不是 500。
|
||||
func TestR26ResumeBusyIsConflict(t *testing.T) {
|
||||
job := governance.LibraryJob{ID: "job-busy", Scope: "internal", Kind: governance.LibraryJobSync,
|
||||
State: governance.LibraryJobFailed, Stage: "resources", TargetSnapshotID: "snap-1"}
|
||||
store := resumableStore(job)
|
||||
store.resumeErr = postgres.ErrLibraryResumeBusy
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryJobResume(recorder, resumeRequest("job-busy"), resumeConfig(store))
|
||||
if recorder.Code != http.StatusConflict || !strings.Contains(recorder.Body.String(), "library_resume_busy") {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 入队失败(存储层事务回滚):500,且不得把作业当成已续作。
|
||||
func TestR26ResumeEnqueueFailureIsServerError(t *testing.T) {
|
||||
job := governance.LibraryJob{ID: "job-fail", Scope: "internal", Kind: governance.LibraryJobRestore,
|
||||
State: governance.LibraryJobFailed, Stage: "resources", TargetSnapshotID: "snap-1"}
|
||||
store := resumableStore(job)
|
||||
store.resumeErr = errors.New("enqueue resumed job: injected")
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryJobResume(recorder, resumeRequest("job-fail"), resumeConfig(store))
|
||||
if recorder.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if strings.Contains(recorder.Body.String(), "resumed") {
|
||||
t.Fatalf("failed enqueue must not claim success: %s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 重复请求:第二次被存储层拒绝(状态已推进),不会重复投递。
|
||||
func TestR26ResumeDuplicateRequestDoesNotRedeliver(t *testing.T) {
|
||||
job := governance.LibraryJob{ID: "job-dup", Scope: "internal", Kind: governance.LibraryJobSync,
|
||||
State: governance.LibraryJobFailed, Stage: "resources", TargetSnapshotID: "snap-1"}
|
||||
store := resumableStore(job)
|
||||
first := httptest.NewRecorder()
|
||||
libraryJobResume(first, resumeRequest("job-dup"), resumeConfig(store))
|
||||
store.resumeOK = false // 第二次:业务作业已不是 failed
|
||||
second := httptest.NewRecorder()
|
||||
libraryJobResume(second, resumeRequest("job-dup"), resumeConfig(store))
|
||||
if first.Code != http.StatusAccepted || second.Code != http.StatusConflict {
|
||||
t.Fatalf("first=%d second=%d body=%s", first.Code, second.Code, second.Body.String())
|
||||
}
|
||||
if len(store.resumeCalls) != 2 {
|
||||
t.Fatalf("expected 2 store calls, got %v", store.resumeCalls)
|
||||
}
|
||||
}
|
||||
@@ -261,10 +261,14 @@ func librarySnapshotViewOfNoUsage(id string, manifest librarysnapshot.Manifest)
|
||||
}
|
||||
case "resources":
|
||||
snapshot.Totals["resources"] = part.Count
|
||||
case "resource_bindings":
|
||||
snapshot.Totals["bindings"] = part.Count
|
||||
case "wiki_pages":
|
||||
snapshot.Totals["wiki_pages"] = part.Count
|
||||
case "wiki_folders":
|
||||
snapshot.Totals["wiki_folders"] = part.Count
|
||||
case "wiki_log_entries":
|
||||
snapshot.Totals["wiki_log_entries"] = part.Count
|
||||
}
|
||||
}
|
||||
sort.Slice(snapshot.Parts, func(i, j int) bool {
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// 恢复验证:页面点一次"跑恢复验证",后端建一个持久作业,worker 用与 CLI 同一份实现
|
||||
// (internal/libraryverify)真实执行:捕获目标 → 基线校验 → 写 canary 证明校验有效 →
|
||||
// 按预备份恢复 → 恢复后复核。通过与否只由这条证据表回答,页面不接受手填开关。
|
||||
const libraryVerifyJobType = "library.verify"
|
||||
|
||||
type libraryVerifyRequest struct {
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
|
||||
// libraryVerificationView 是页面需要的全部事实:当前目标是谁、最近一次验证结果、
|
||||
// 证据是否对当前目标成立、以及同步闸口能不能据此放行。
|
||||
type libraryVerificationView struct {
|
||||
Scope string `json:"scope"`
|
||||
Target libraryVerificationTarget `json:"target"`
|
||||
Verified bool `json:"verified"`
|
||||
Reason string `json:"reason"`
|
||||
Latest *governance.LibraryRestoreVerification `json:"latest,omitempty"`
|
||||
// SnapshotLatest 只在请求显式带 snapshot_id 时返回:快照报告不能拿 scope
|
||||
// 最近一条代替。
|
||||
SnapshotLatest *governance.LibraryRestoreVerification `json:"snapshot_latest,omitempty"`
|
||||
History []governance.LibraryRestoreVerification `json:"history"`
|
||||
Running bool `json:"running"`
|
||||
JobID string `json:"job_id,omitempty"`
|
||||
State string `json:"state,omitempty"`
|
||||
Stage string `json:"stage,omitempty"`
|
||||
}
|
||||
|
||||
type libraryVerificationTarget struct {
|
||||
Instance string `json:"instance"`
|
||||
Project string `json:"project"`
|
||||
InternalKB string `json:"internal_kb"`
|
||||
ExternalKB string `json:"external_kb"`
|
||||
Index string `json:"index"`
|
||||
S3Bucket string `json:"s3_bucket"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
DatabaseSet bool `json:"database_configured"`
|
||||
}
|
||||
|
||||
// libraryVerifyEnqueue 投递一次恢复验证作业(管理员)。
|
||||
//
|
||||
// 它不签发发布授权、不改版本:验证只回答"当前目标的预备份能不能恢复"。
|
||||
// 目标是部署配置,不接受请求体指定——否则一次点击就能验证别的库并把证据挂到当前目标上。
|
||||
func libraryVerifyEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
return
|
||||
}
|
||||
identity, ok := sessionFrom(r.Context())
|
||||
if !ok || roleRank(identity.Role) < roleRank(roleAdmin) {
|
||||
writeJSON(w, http.StatusForbidden, map[string]any{"error": "恢复验证仅管理员可发起"})
|
||||
return
|
||||
}
|
||||
var request libraryVerifyRequest
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096)).Decode(&request); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
if request.Scope != "internal" && request.Scope != "external" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "scope must be internal or external"})
|
||||
return
|
||||
}
|
||||
store, ok := libraryStore(config)
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"code": "library_verify_unavailable",
|
||||
"error": "恢复验证存储不可用"})
|
||||
return
|
||||
}
|
||||
// 验证要停写入者并在目标上写 canary:维护中或闸口关闭时直接拒绝,
|
||||
// 不让页面投一个 worker 必然失败的作业。
|
||||
if body, held := instanceMaintenanceGuard(r.Context(), config, request.Scope); held {
|
||||
writeJSON(w, http.StatusConflict, body)
|
||||
return
|
||||
}
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{
|
||||
"code": "library_verify_blocked",
|
||||
"reasons": []string{
|
||||
"生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false):恢复验证要停写入者并在目标上写 canary",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
digest, err := governance.LibraryRequestDigest(request.Scope, governance.LibraryJobVerify,
|
||||
map[string]any{"scope": request.Scope})
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot digest request"})
|
||||
return
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
// 验证作业必须有和同步/恢复同一套不可变 operation:worker 的 owned attempt 从
|
||||
// job.operation_id 取身份,恢复派发闸口和对象网关都按它核验。没有 operation 的
|
||||
// verify 作业在真实恢复时会被 fail closed(审核 B2-R1)。
|
||||
intent, intentErr := ensureLibraryIntent(r.Context(), store, request.Scope, governance.LibraryJobVerify,
|
||||
"", digest, identity.Username, "", now)
|
||||
if intentErr != nil {
|
||||
if isIntentConflict(intentErr) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_verify_conflict",
|
||||
"error": "同一幂等键对应了不同的请求"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot create library verify operation"})
|
||||
return
|
||||
}
|
||||
job, reused, err := store.CreateLibraryDispatch(r.Context(), governance.LibraryJob{
|
||||
ID: fmt.Sprintf("libverify-%s-%d", request.Scope, now.UnixNano()), Scope: request.Scope,
|
||||
Kind: governance.LibraryJobVerify,
|
||||
IdempotencyKey: intent.JobKey,
|
||||
RequestDigest: digest, OperationID: intent.Operation.ID, Actor: identity.Username,
|
||||
}, governance.VerifyStages(), false, now)
|
||||
if errors.Is(err, governance.ErrLibraryJobActive) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_write_active",
|
||||
"error": "该目标已有在飞的写作业(同步/恢复/基线/验证互斥),等它结束再发起"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, governance.ErrLibraryJobIdempotencyConflict) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_verify_conflict",
|
||||
"error": "同一幂等键对应了不同的请求"})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot create library verify job"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusAccepted, map[string]any{"job": job, "reused": reused})
|
||||
}
|
||||
|
||||
// libraryVerifications 读当前目标的恢复验证证据(页面轮询用)。
|
||||
func libraryVerifications(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
scope, err := scopeFromQuery(r)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
view := libraryVerificationView{
|
||||
Scope: scope,
|
||||
History: []governance.LibraryRestoreVerification{},
|
||||
Target: libraryVerificationTarget{
|
||||
Instance: config.LibraryTargetInstance,
|
||||
Project: config.LibraryTargetProject,
|
||||
InternalKB: config.LibraryProductionInternalKB,
|
||||
ExternalKB: config.LibraryProductionExternalKB,
|
||||
Index: config.LibraryProductionIndex,
|
||||
S3Bucket: config.LibraryProductionS3Bucket,
|
||||
TenantID: config.LibraryProductionTenantID,
|
||||
DatabaseSet: config.LibraryProductionDatabaseURL != "",
|
||||
},
|
||||
}
|
||||
store, ok := config.Store.(interface {
|
||||
LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error)
|
||||
LibraryRestoreVerifications(context.Context, string, int) ([]governance.LibraryRestoreVerification, error)
|
||||
})
|
||||
if !ok {
|
||||
view.Reason = "当前部署没有恢复验证存储:无法读取证据"
|
||||
writeJSON(w, http.StatusServiceUnavailable, view)
|
||||
return
|
||||
}
|
||||
latest, found, err := store.LatestLibraryRestoreVerification(r.Context(), scope)
|
||||
if err != nil {
|
||||
view.Reason = "读取恢复验证证据失败:" + err.Error()
|
||||
writeJSON(w, http.StatusServiceUnavailable, view)
|
||||
return
|
||||
}
|
||||
if found {
|
||||
view.Latest = &latest
|
||||
view.JobID = latest.JobID
|
||||
}
|
||||
if history, err := store.LibraryRestoreVerifications(r.Context(), scope, 10); err == nil {
|
||||
view.History = history
|
||||
}
|
||||
if snapshotID := strings.TrimSpace(r.URL.Query().Get("snapshot_id")); snapshotID != "" {
|
||||
if exact, ok := config.Store.(interface {
|
||||
LibraryRestoreVerificationForSnapshot(context.Context, string, string) (governance.LibraryRestoreVerification, bool, error)
|
||||
}); ok {
|
||||
item, found, exactErr := exact.LibraryRestoreVerificationForSnapshot(r.Context(), scope, snapshotID)
|
||||
if exactErr != nil {
|
||||
view.Reason = "读取该快照的恢复验证记录失败"
|
||||
writeJSON(w, http.StatusServiceUnavailable, view)
|
||||
return
|
||||
}
|
||||
if found {
|
||||
view.SnapshotLatest = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
view.Verified, view.Reason = governance.LibraryVerificationSatisfied(latest, scope)
|
||||
if !found {
|
||||
view.Verified = false
|
||||
view.Reason = "还没有对当前目标做过恢复验证:点「跑一次恢复验证」(真实备份 → canary → 恢复 → 复核)"
|
||||
}
|
||||
if snapshotID := strings.TrimSpace(r.URL.Query().Get("snapshot_id")); snapshotID != "" {
|
||||
// 显式按快照查询时,verified 只回答该快照;不能让 scope 最新通过记录
|
||||
// 把另一个未演练的快照染绿。
|
||||
view.Verified = false
|
||||
if view.SnapshotLatest == nil {
|
||||
view.Reason = "该快照没有真实恢复验证记录(未验证;不是读取失败)"
|
||||
} else if view.SnapshotLatest.Status == governance.LibraryVerificationPassed {
|
||||
view.Verified = true
|
||||
view.Reason = ""
|
||||
} else {
|
||||
view.Reason = "该快照最近一次恢复验证未通过"
|
||||
}
|
||||
}
|
||||
// 在飞作业必须从真实业务行读出来:旧实现只构造 running 字段却从不赋值,
|
||||
// 页面因此在验收里看不到"已接受/正在执行",按钮也不会进入执行态(审核 B4)。
|
||||
if jobs, ok := config.Store.(interface {
|
||||
ListLibraryJobs(context.Context, string, int) ([]governance.LibraryJob, error)
|
||||
}); ok {
|
||||
if items, err := jobs.ListLibraryJobs(r.Context(), scope, 20); err == nil {
|
||||
var active *governance.LibraryJob
|
||||
for index := range items {
|
||||
item := items[index]
|
||||
if item.Kind != governance.LibraryJobVerify || string(item.State) == "" {
|
||||
continue
|
||||
}
|
||||
if !libraryJobActiveState(item.State) {
|
||||
continue
|
||||
}
|
||||
if active == nil || item.CreatedAt.After(active.CreatedAt) {
|
||||
active = &item
|
||||
}
|
||||
}
|
||||
if active != nil {
|
||||
view.Running = true
|
||||
view.JobID = active.ID
|
||||
view.State = string(active.State)
|
||||
view.Stage = active.Stage
|
||||
view.Reason = "恢复验证作业 " + active.ID + " 正在执行(" + active.Stage + ");终态会在这里显示证据,页面会自动刷新"
|
||||
}
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, view)
|
||||
}
|
||||
|
||||
// libraryJobActiveState 只认仍在执行的业务状态:终态不是"在飞"。
|
||||
func libraryJobActiveState(state governance.LibraryJobState) bool {
|
||||
switch state {
|
||||
case governance.LibraryJobPending, governance.LibraryJobRunning:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// withRestoreEvidence 把"当前目标是否有真实恢复证据"注入 Config 的副本,
|
||||
// 让同步/恢复闸口在同一个请求里按证据判断,而不是读一个可能被手填的 env。
|
||||
//
|
||||
// 证据优先级是不可交换的(审核 B3):明确 failed/invalid 或读取失败都必须拒绝,
|
||||
// 即使 env=true;历史 env 覆盖只在**明确没有证据行**(not-found)时兜底。
|
||||
// 把读取失败混进 not-found 等于把"不知道"说成"满足"。
|
||||
func (c Config) withRestoreEvidence(ctx context.Context, scope string) Config {
|
||||
satisfied, missing, err := c.restoreEvidence(ctx, scope)
|
||||
c.restoreEvidenceChecked = true
|
||||
switch {
|
||||
case err != nil:
|
||||
c.restoreVerified = false
|
||||
c.restoreEvidenceReason = err.Error()
|
||||
case satisfied:
|
||||
c.restoreVerified = true
|
||||
c.restoreEvidenceReason = ""
|
||||
case missing && c.ProductionBackupRestoreVerified:
|
||||
c.restoreVerified = true
|
||||
c.restoreEvidenceReason = ""
|
||||
default:
|
||||
c.restoreVerified = false
|
||||
if missing {
|
||||
c.restoreEvidenceReason = "当前目标还没有恢复演练证据:先在「生产保护与恢复」里跑一次恢复验证"
|
||||
} else {
|
||||
c.restoreEvidenceReason = "没有已核验的生产恢复演练证据"
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// restoreEvidence 读当前目标的最近一条证据并给出它能不能支撑"可恢复"。
|
||||
// missing 只在"查询成功且确实没有这一行"时为 true;读取错误单独返回。
|
||||
func (c Config) restoreEvidence(ctx context.Context, scope string) (satisfied bool, missing bool, err error) {
|
||||
store, ok := c.Store.(interface {
|
||||
LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error)
|
||||
})
|
||||
if !ok {
|
||||
// 存储没有证据能力(迁移前的部署形状)等同于"没有证据行":
|
||||
// 历史 env 覆盖仍可兜底,但一旦读得出证据行,失败/无效/读取错误都优先拒绝。
|
||||
return false, true, nil
|
||||
}
|
||||
latest, found, err := store.LatestLibraryRestoreVerification(ctx, scope)
|
||||
if err != nil {
|
||||
return false, false, fmt.Errorf("读取恢复验证证据失败:%w", err)
|
||||
}
|
||||
if !found {
|
||||
return false, true, nil
|
||||
}
|
||||
ok, reason := governance.LibraryVerificationSatisfied(latest, scope)
|
||||
if !ok {
|
||||
return false, false, errors.New(reason)
|
||||
}
|
||||
return true, false, nil
|
||||
}
|
||||
|
||||
// restoreGateReasons 是闸口对"恢复可验证"的统一判断:只看 withRestoreEvidence 的结论。
|
||||
// 这里不再 OR env:env 兜底只在证据缺失时、且已经在 withRestoreEvidence 里判定过。
|
||||
func restoreGateReasons(config Config) []string {
|
||||
if config.restoreVerified {
|
||||
return nil
|
||||
}
|
||||
// 没有走过证据读取的调用点(历史路径/单元夹具)保持原有 env 语义;
|
||||
// 一旦读过证据,上面的 withRestoreEvidence 已经把优先级判定完毕。
|
||||
if !config.restoreEvidenceChecked && config.ProductionBackupRestoreVerified {
|
||||
return nil
|
||||
}
|
||||
reason := config.restoreEvidenceReason
|
||||
if reason == "" {
|
||||
reason = "没有已核验的生产恢复演练证据"
|
||||
}
|
||||
return []string{reason}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// 恢复验证的页面契约:
|
||||
// - 只有管理员能发起(它会停写入者并在目标上写 canary);
|
||||
// - 闸口关闭 / 维护中时直接拒绝,不投一个 worker 必然失败的作业;
|
||||
// - 证据只由真实执行写入:没有证据 ⇒ verified=false,过期/换目标 ⇒ 失效。
|
||||
//
|
||||
// 这里不模拟"真跑一次恢复"(那需要真实目标);执行体本身在 internal/libraryverify,
|
||||
// 由库内 CLI 与 worker 共用,属于隔离 fixture 之外的集成范围。
|
||||
|
||||
type verificationStore struct {
|
||||
fakeLibraryStore
|
||||
verifications []governance.LibraryRestoreVerification
|
||||
}
|
||||
|
||||
func (s *verificationStore) LatestLibraryRestoreVerification(_ context.Context, scope string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
for _, item := range s.verifications {
|
||||
if item.Scope == scope {
|
||||
return item, true, nil
|
||||
}
|
||||
}
|
||||
return governance.LibraryRestoreVerification{}, false, nil
|
||||
}
|
||||
|
||||
func (s *verificationStore) LibraryRestoreVerificationForSnapshot(_ context.Context, scope, snapshotID string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
for _, item := range s.verifications {
|
||||
if item.Scope == scope && item.SnapshotID == snapshotID {
|
||||
return item, true, nil
|
||||
}
|
||||
}
|
||||
return governance.LibraryRestoreVerification{}, false, nil
|
||||
}
|
||||
|
||||
func (s *verificationStore) LibraryRestoreVerifications(_ context.Context, scope string, limit int) ([]governance.LibraryRestoreVerification, error) {
|
||||
items := make([]governance.LibraryRestoreVerification, 0, len(s.verifications))
|
||||
for _, item := range s.verifications {
|
||||
if item.Scope == scope {
|
||||
items = append(items, item)
|
||||
}
|
||||
}
|
||||
if limit > 0 && len(items) > limit {
|
||||
items = items[:limit]
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func verifyRequest(body, role, username string) *http.Request {
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/library/verifications", strings.NewReader(body))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
ctx := context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: username, Role: role})
|
||||
return request.WithContext(ctx)
|
||||
}
|
||||
|
||||
func TestLibraryVerifyRequiresAdmin(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryVerifyEnqueue(recorder, verifyRequest(`{"scope":"internal"}`, roleEngineer, "eng"),
|
||||
Config{Store: store, ProductionReleaseRequestsEnabled: true})
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.createN != 0 {
|
||||
t.Fatal("non-admin must not create a verify job")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerifyRefusesWhenWriteGateClosed(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryVerifyEnqueue(recorder, verifyRequest(`{"scope":"internal"}`, roleAdmin, "admin"),
|
||||
Config{Store: store, ProductionReleaseRequestsEnabled: false})
|
||||
if recorder.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
body := decodeBody(t, recorder)
|
||||
if body["code"] != "library_verify_blocked" {
|
||||
t.Fatalf("body=%v", body)
|
||||
}
|
||||
if store.createN != 0 {
|
||||
t.Fatal("closed gate must not create a verify job")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerifyEnqueuesVerifyJobWithCanaryStages(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
store.enqueueEchoRequestedID = true
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryVerifyEnqueue(recorder, verifyRequest(`{"scope":"internal"}`, roleAdmin, "admin"),
|
||||
Config{Store: store, ProductionReleaseRequestsEnabled: true})
|
||||
if recorder.Code != http.StatusAccepted {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.lastJob.Kind != governance.LibraryJobVerify || store.lastJob.Scope != "internal" {
|
||||
t.Fatalf("job=%#v", store.lastJob)
|
||||
}
|
||||
if !strings.HasPrefix(store.lastJob.ID, "libverify-internal-") {
|
||||
t.Fatalf("job id=%q", store.lastJob.ID)
|
||||
}
|
||||
// B2-R1:verify 作业必须带不可变 operation,worker 的 owned attempt / 恢复派发
|
||||
// 身份都从它取;没有 operation 的作业在真实恢复时会被 fail closed。
|
||||
if store.lastJob.OperationID == "" || store.lastJob.OperationID != store.lastOperation.ID {
|
||||
t.Fatalf("verify job must carry its operation: job=%#v op=%#v", store.lastJob, store.lastOperation)
|
||||
}
|
||||
if store.lastOperation.Kind != governance.LibraryJobVerify || store.lastOperation.Scope != "internal" {
|
||||
t.Fatalf("verify operation identity=%#v", store.lastOperation)
|
||||
}
|
||||
// 阶段必须包含 canary:没有"写 canary 证明校验有效"这一步,验证就只是自证。
|
||||
want := governance.VerifyStages()
|
||||
if len(want) != 5 || want[2] != "canary" || want[3] != "restore" || want[4] != "finalize" {
|
||||
t.Fatalf("verify stages=%v", want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerificationsWithoutEvidenceIsNotVerified(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
body := decodeBody(t, recorder)
|
||||
if body["verified"] != false {
|
||||
t.Fatalf("verified must be false without evidence: %v", body)
|
||||
}
|
||||
if reason, _ := body["reason"].(string); !strings.Contains(reason, "还没有对当前目标做过恢复验证") {
|
||||
t.Fatalf("reason=%q", reason)
|
||||
}
|
||||
}
|
||||
|
||||
// F03: the selected snapshot must resolve its own evidence, never the scope's
|
||||
// newest passed record.
|
||||
func TestLibraryVerificationsExactSnapshotDoesNotBorrowLatest(t *testing.T) {
|
||||
store := &verificationStore{verifications: []governance.LibraryRestoreVerification{
|
||||
{ID: "verify-new", Scope: "internal", Status: governance.LibraryVerificationPassed,
|
||||
SnapshotID: "snap-new", JobID: "job-new", FinishedAt: time.Now().UTC()},
|
||||
{ID: "verify-old", Scope: "internal", Status: governance.LibraryVerificationFailed,
|
||||
SnapshotID: "snap-old", JobID: "job-old", Failure: "old restore mismatch",
|
||||
FinishedAt: time.Now().UTC().Add(-time.Hour)},
|
||||
}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal&snapshot_id=snap-old", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
exact, ok := body["snapshot_latest"].(map[string]any)
|
||||
if !ok || exact["snapshot_id"] != "snap-old" || exact["status"] != "failed" {
|
||||
t.Fatalf("snapshot_latest=%v body=%v", body["snapshot_latest"], body)
|
||||
}
|
||||
if body["verified"] != false {
|
||||
t.Fatalf("old failed snapshot must not inherit latest passed: %v", body["verified"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerificationsWithPassedEvidenceIsVerified(t *testing.T) {
|
||||
store := &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
ID: "libverify-internal-job1", Scope: "internal", Status: governance.LibraryVerificationPassed,
|
||||
SnapshotID: "snap-1", JobID: "job1", StartedAt: time.Now().UTC().Add(-time.Minute),
|
||||
FinishedAt: time.Now().UTC(),
|
||||
}}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
if body["verified"] != true {
|
||||
t.Fatalf("verified must be true with passed evidence: %v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerificationsFailedEvidenceIsNotVerified(t *testing.T) {
|
||||
store := &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
ID: "libverify-internal-job2", Scope: "internal", Status: governance.LibraryVerificationFailed,
|
||||
SnapshotID: "snap-2", JobID: "job2", Failure: "恢复后内容仍与预备份不一致",
|
||||
StartedAt: time.Now().UTC().Add(-time.Minute), FinishedAt: time.Now().UTC(),
|
||||
}}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
if body["verified"] != false {
|
||||
t.Fatalf("failed evidence must not verify: %v", body)
|
||||
}
|
||||
if reason, _ := body["reason"].(string); !strings.Contains(reason, "恢复后内容仍与预备份不一致") {
|
||||
t.Fatalf("reason=%q", reason)
|
||||
}
|
||||
}
|
||||
|
||||
// 同步闸口必须消费同一 target 的证据:有 passed 证据即放行,
|
||||
// 只有失败证据时仍然拒绝并给出失败原因(不是笼统"没有证据")。
|
||||
// 审核 B3 反例(GPT-6 overlay)转成通过断言:env=true 不能覆盖明确失败或读取失败。
|
||||
type evidenceReadErrorStore struct{ verificationStore }
|
||||
|
||||
func (*evidenceReadErrorStore) LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
return governance.LibraryRestoreVerification{}, false, errors.New("evidence lookup failed")
|
||||
}
|
||||
|
||||
func TestRestoreGateFailedEvidenceBeatsEnv(t *testing.T) {
|
||||
store := &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
Scope: "internal", Status: governance.LibraryVerificationFailed, Failure: "restore mismatch",
|
||||
FinishedAt: time.Now().UTC(),
|
||||
}}}
|
||||
config := Config{Store: store, ProductionBackupRestoreVerified: true}.withRestoreEvidence(context.Background(), "internal")
|
||||
reasons := restoreGateReasons(config)
|
||||
if len(reasons) == 0 || !strings.Contains(strings.Join(reasons, ";"), "restore mismatch") {
|
||||
t.Fatalf("failed evidence must beat env=true: %v", reasons)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreGateUnreadableEvidenceBeatsEnv(t *testing.T) {
|
||||
config := Config{Store: &evidenceReadErrorStore{}, ProductionBackupRestoreVerified: true}.
|
||||
withRestoreEvidence(context.Background(), "internal")
|
||||
reasons := restoreGateReasons(config)
|
||||
if len(reasons) == 0 || !strings.Contains(strings.Join(reasons, ";"), "读取恢复验证证据失败") {
|
||||
t.Fatalf("unreadable evidence must beat env=true: %v", reasons)
|
||||
}
|
||||
}
|
||||
|
||||
// env 兜底只允许在"明确没有证据行"时生效。
|
||||
func TestRestoreGateEnvFallbackOnlyWhenEvidenceMissing(t *testing.T) {
|
||||
config := Config{Store: &verificationStore{}, ProductionBackupRestoreVerified: true}.
|
||||
withRestoreEvidence(context.Background(), "internal")
|
||||
if reasons := restoreGateReasons(config); len(reasons) != 0 {
|
||||
t.Fatalf("explicit not-found with legacy env must still allow: %v", reasons)
|
||||
}
|
||||
}
|
||||
|
||||
// 审核 B4:GET 必须从真实业务行投影在飞验证作业,而不是永远 running=false。
|
||||
func TestLibraryVerificationsProjectsActiveVerifyJob(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
store.jobs = []governance.LibraryJob{{
|
||||
ID: "verify-running", Scope: "internal", Kind: governance.LibraryJobVerify,
|
||||
State: governance.LibraryJobRunning, Stage: "restore", CreatedAt: time.Now().UTC(),
|
||||
}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
if body["running"] != true {
|
||||
t.Fatalf("running verify job must be projected: %v", body)
|
||||
}
|
||||
if body["job_id"] != "verify-running" {
|
||||
t.Fatalf("job_id=%v", body["job_id"])
|
||||
}
|
||||
if body["stage"] != "restore" {
|
||||
t.Fatalf("stage=%v", body["stage"])
|
||||
}
|
||||
}
|
||||
|
||||
// 终态作业不是"在飞":页面不能一直显示执行中。
|
||||
func TestLibraryVerificationsIgnoresTerminalVerifyJob(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
store.jobs = []governance.LibraryJob{{
|
||||
ID: "verify-done", Scope: "internal", Kind: governance.LibraryJobVerify,
|
||||
State: governance.LibraryJobSucceeded, CreatedAt: time.Now().UTC(),
|
||||
}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
if body["running"] != false {
|
||||
t.Fatalf("terminal verify job must not be projected as running: %v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibrarySyncGateConsumesRestoreEvidence(t *testing.T) {
|
||||
base := Config{ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: false,
|
||||
LibraryProductionDatabaseURL: "postgres://unused.invalid/db", LibraryCapacityBudgetBytes: 1 << 40}
|
||||
base.Store = &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
Scope: "internal", Status: governance.LibraryVerificationPassed, SnapshotID: "snap-1",
|
||||
JobID: "job1", FinishedAt: time.Now().UTC(),
|
||||
}}}
|
||||
allowed := librarySyncGateWithGrant(base.withRestoreEvidence(context.Background(), "internal"),
|
||||
libraryKnownView{State: "known"}, libraryKnownView{State: "unknown"}, "snap-1", true)
|
||||
if !allowed.SyncAllowed {
|
||||
t.Fatalf("gate should allow with passed evidence: %v", allowed.Reasons)
|
||||
}
|
||||
noEvidence := Config{ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: false,
|
||||
LibraryProductionDatabaseURL: "postgres://unused.invalid/db", LibraryCapacityBudgetBytes: 1 << 40,
|
||||
Store: &verificationStore{}}.withRestoreEvidence(context.Background(), "internal")
|
||||
blocked := librarySyncGateWithGrant(noEvidence,
|
||||
libraryKnownView{State: "known"}, libraryKnownView{State: "unknown"}, "snap-1", true)
|
||||
if blocked.SyncAllowed {
|
||||
t.Fatalf("gate without env and without evidence must block: %v", blocked.Reasons)
|
||||
}
|
||||
if len(blocked.Reasons) == 0 || !strings.Contains(strings.Join(blocked.Reasons, ";"), "恢复验证") {
|
||||
t.Fatalf("blocked reasons=%v", blocked.Reasons)
|
||||
}
|
||||
failedConfig := Config{ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: false,
|
||||
LibraryProductionDatabaseURL: "postgres://unused.invalid/db", LibraryCapacityBudgetBytes: 1 << 40,
|
||||
Store: &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
Scope: "internal", Status: governance.LibraryVerificationFailed, SnapshotID: "snap-2",
|
||||
JobID: "job2", Failure: "canary 后校验仍通过", FinishedAt: time.Now().UTC(),
|
||||
}}}}
|
||||
reason := strings.Join(restoreGateReasons(failedConfig.withRestoreEvidence(context.Background(), "internal")), ";")
|
||||
if !strings.Contains(reason, "canary 后校验仍通过") {
|
||||
t.Fatalf("失败证据的失败原因必须出现在闸口:%q", reason)
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,10 @@ func listMonitorTasks(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
writeJSON(w, 500, map[string]string{"error": "无法读取文件处理状态"})
|
||||
return
|
||||
}
|
||||
// 目标同名不罕见(两侧都叫“对内知识库”):把当前生效绑定随页返回,
|
||||
// 前端才能在同一口径下标注“当前绑定/历史”,而不是拿本页条目反推全局。
|
||||
internal, external := config.effectiveRoutingTargets(r.Context())
|
||||
page.Bindings = postgresstore.MonitorBindings{Internal: internal, External: external}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeJSON(w, 200, map[string]any{"data": page})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -51,3 +52,39 @@ func TestMonitorTasksRoute(t *testing.T) {
|
||||
t.Fatalf("viewer removal %d", removal.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// F15:筛选里的“当前绑定”必须来自持久化绑定(与业务同一真源),不是 env 旧值,
|
||||
// 也不能让前端拿本页数据猜全局。
|
||||
func TestMonitorTasksReportsEffectiveTargetBindings(t *testing.T) {
|
||||
store := &monitorTasksTestStore{}
|
||||
handler, err := NewHandler(Config{
|
||||
Store: store,
|
||||
InternalRoutingKnowledgeBaseID: "env-old-internal",
|
||||
ExternalRoutingKnowledgeBaseID: "env-old-external",
|
||||
RoutingBindings: &bindingStoreFake{internal: "current-internal", external: "current-external"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest("GET", "/api/v1/monitor/tasks?limit=20", nil)
|
||||
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleViewer}))
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, req)
|
||||
if recorder.Code != 200 {
|
||||
t.Fatalf("status %d: %s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var payload struct {
|
||||
Data struct {
|
||||
Bindings struct {
|
||||
Internal string `json:"internal"`
|
||||
External string `json:"external"`
|
||||
} `json:"bindings"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.Data.Bindings.Internal != "current-internal" || payload.Data.Bindings.External != "current-external" {
|
||||
t.Fatalf("bindings = %#v, want the persisted current bindings", payload.Data.Bindings)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/pgdsn"
|
||||
"knowledge-governance/internal/platform/weknora"
|
||||
)
|
||||
|
||||
@@ -33,6 +34,55 @@ type productionBindingView struct {
|
||||
ProductionBaseURL string `json:"production_base_url"`
|
||||
ProductionConnected bool `json:"production_connected"`
|
||||
ProductionMessage string `json:"production_message"`
|
||||
// LegacyScope 说明这条记录属于哪条发布路径。整库发布(封存/同步/恢复)不读它,
|
||||
// 只读下面的 WholeTarget;页面必须把两者分开写,否则会把旧文档级目标当成整库目标。
|
||||
LegacyScope string `json:"legacy_scope"`
|
||||
// WholeTarget 是整库发布真正写入的目标(来自部署环境 LIBRARY_PRODUCTION_*),
|
||||
// 与上面可编辑的文档级绑定是两条独立路径。
|
||||
WholeTarget libraryPublishTargetView `json:"whole_target"`
|
||||
}
|
||||
|
||||
// libraryPublishTargetView 是"整库发布到底写哪儿"的只读投影。
|
||||
// 为什么由后端给出:页面上的 kb 名称/文档数来自生产端列表接口,而整库写入用的是
|
||||
// 另一组环境配置;让页面自己拼会把两条路径混成一个。
|
||||
type libraryPublishTargetView struct {
|
||||
Configured bool `json:"configured"`
|
||||
Source string `json:"source"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
InternalKBID string `json:"internal_kb_id"`
|
||||
ExternalKBID string `json:"external_kb_id"`
|
||||
Database string `json:"database"`
|
||||
ElasticIndex string `json:"elastic_index"`
|
||||
ObjectBucket string `json:"object_bucket"`
|
||||
MissingReason string `json:"missing_reason"`
|
||||
}
|
||||
|
||||
// libraryPublishTarget 从 config 派生整库目标;缺项照实说缺哪一项,不猜默认值。
|
||||
func (config Config) libraryPublishTarget() libraryPublishTargetView {
|
||||
view := libraryPublishTargetView{
|
||||
Source: "部署环境 LIBRARY_PRODUCTION_*",
|
||||
TenantID: config.LibraryProductionTenantID,
|
||||
InternalKBID: config.LibraryProductionInternalKB,
|
||||
ExternalKBID: config.LibraryProductionExternalKB,
|
||||
Database: pgdsn.Endpoint(config.LibraryProductionDatabaseURL),
|
||||
ElasticIndex: config.LibraryProductionIndex,
|
||||
ObjectBucket: config.LibraryProductionS3Bucket,
|
||||
}
|
||||
missing := make([]string, 0, 4)
|
||||
if config.LibraryProductionDatabaseURL == "" {
|
||||
missing = append(missing, "LIBRARY_PRODUCTION_DATABASE_URL")
|
||||
}
|
||||
if view.InternalKBID == "" {
|
||||
missing = append(missing, "LIBRARY_PRODUCTION_KB_INTERNAL")
|
||||
}
|
||||
if config.LibraryProductionIndex == "" {
|
||||
missing = append(missing, "LIBRARY_PRODUCTION_INDEX")
|
||||
}
|
||||
view.Configured = len(missing) == 0
|
||||
if len(missing) > 0 {
|
||||
view.MissingReason = "未配置:" + strings.Join(missing, "、")
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
// productionTargetBaseURL is the production endpoint the picker reads from: the
|
||||
@@ -79,6 +129,10 @@ func (config Config) productionBindingPayload(ctx context.Context) (productionBi
|
||||
InternalKBName: names[storedInternal], ExternalKBName: names[storedExternal],
|
||||
UpdatedBy: updatedBy, ProductionBaseURL: config.productionTargetBaseURL(ctx),
|
||||
ProductionConnected: message == "", ProductionMessage: message,
|
||||
// 这张表服务的是"文档级发布"(单篇文档写生产)。整库发布读的是部署环境
|
||||
// LIBRARY_PRODUCTION_*,两者不能互相顶替,所以页面必须分开显示。
|
||||
LegacyScope: "文档级发布(单篇文档写生产)· 旧路径",
|
||||
WholeTarget: config.libraryPublishTarget(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -178,3 +178,50 @@ func TestProductionBindingOptionsReportUnavailableReason(t *testing.T) {
|
||||
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 页面必须说清"整库发布真正写哪个 KB/端点"。这条投影只能来自部署配置,
|
||||
// 不能从可编辑的文档级绑定推出来,也不能把口令带进响应。
|
||||
func TestLibraryPublishTargetIsDerivedFromDeploymentConfig(t *testing.T) {
|
||||
config := Config{
|
||||
// 只放形状,不放任何可用凭据:口令段是占位符,主机是 .invalid。
|
||||
LibraryProductionDatabaseURL: "postgres://governance@db.invalid:5432/weknora_test?sslmode=disable",
|
||||
LibraryProductionInternalKB: "a8e9c8ad-411c-4c53-8421-599d29e9b179",
|
||||
LibraryProductionExternalKB: "2e980686-2233-4c4e-b5a6-8b0069477b8e",
|
||||
LibraryProductionTenantID: 10000,
|
||||
LibraryProductionIndex: "weknora_test_chunks",
|
||||
LibraryProductionS3Bucket: "dobot-weknora-test",
|
||||
}
|
||||
target := config.libraryPublishTarget()
|
||||
if !target.Configured || target.MissingReason != "" {
|
||||
t.Fatalf("完整配置应判为已配置: %+v", target)
|
||||
}
|
||||
if target.InternalKBID != "a8e9c8ad-411c-4c53-8421-599d29e9b179" || target.TenantID != 10000 {
|
||||
t.Fatalf("必须回显实际整库目标而不是占位: %+v", target)
|
||||
}
|
||||
if target.Database != "db.invalid:5432/weknora_test" {
|
||||
t.Fatalf("端点应只含 host:port/db,得到 %q", target.Database)
|
||||
}
|
||||
// 回显只能是端点:连接串里的用户信息(口令、账号)一个字符都不能进响应。
|
||||
if strings.Contains(target.Database, "@") || strings.Contains(target.Source, "@") ||
|
||||
strings.Contains(target.Source, "postgres://") {
|
||||
t.Fatalf("响应里不能出现连接串用户信息: %+v", target)
|
||||
}
|
||||
if target.ElasticIndex != "weknora_test_chunks" || target.ObjectBucket != "dobot-weknora-test" {
|
||||
t.Fatalf("索引与桶也要显示: %+v", target)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryPublishTargetNamesMissingConfigInsteadOfGuessing(t *testing.T) {
|
||||
target := Config{}.libraryPublishTarget()
|
||||
if target.Configured {
|
||||
t.Fatal("缺配置时不能报告已配置")
|
||||
}
|
||||
for _, key := range []string{"LIBRARY_PRODUCTION_DATABASE_URL", "LIBRARY_PRODUCTION_KB_INTERNAL"} {
|
||||
if !strings.Contains(target.MissingReason, key) {
|
||||
t.Fatalf("缺失说明应点名 %s,得到 %q", key, target.MissingReason)
|
||||
}
|
||||
}
|
||||
if target.InternalKBID != "" || target.Database != "(未配置)" {
|
||||
t.Fatalf("缺配置就不要编造目标: %+v", target)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"knowledge-governance/internal/dobotaiops"
|
||||
"knowledge-governance/internal/qawemail"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -20,6 +21,98 @@ type weeklyMailStore interface {
|
||||
TestSend(context.Context, string) (string, error)
|
||||
SaveDashboardImage(context.Context, string, []byte) error
|
||||
SendDue(context.Context, time.Time) (qawemail.DueResult, error)
|
||||
PreviewSend(context.Context, string, int64, string, time.Time) (qawemail.SendPreview, error)
|
||||
SendManual(context.Context, string, int64, string, string, time.Time) (qawemail.ManualResult, error)
|
||||
}
|
||||
|
||||
// weeklyMailSendError names the rule that refused a manual resend. Anything SMTP-related is
|
||||
// reported by the handler itself, so an unmet precondition never looks like a mail failure.
|
||||
func weeklyMailSendError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, qawemail.ErrNotCurrent):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "该周周报未确认,或确认后正文/版本已变化;请先确认当前版本再补发"})
|
||||
case errors.Is(err, qawemail.ErrFutureWeek):
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "不能补发未来周"})
|
||||
case errors.Is(err, qawemail.ErrSlotPending):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "本周尚未到达设定的发送时点,暂不能补发"})
|
||||
case errors.Is(err, qawemail.ErrNotConfigured):
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "未配置邮件服务器、发件人或收件人,无法补发"})
|
||||
case errors.Is(err, qawemail.ErrSendInFlight):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "该周该版本有一次发送结果未知(记录停在发送中),为避免重复投递未重发;请先核对已发出的邮件"})
|
||||
case errors.Is(err, qawemail.ErrDeliveryUnknown):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "SMTP 已提交但未收到最终确认,投递结果未知;为避免重复已停止重发,请先核对已发出的邮件"})
|
||||
case errors.Is(err, qawemail.ErrNoPassword):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "SMTP 授权码缺失或无法解密,无法补发"})
|
||||
case errors.Is(err, qawemail.ErrInvalid):
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "补发参数无效:需要精确的周、已确认版本与正文 hash"})
|
||||
default:
|
||||
weeklyMailError(w, err)
|
||||
}
|
||||
}
|
||||
|
||||
// qaWeeklyMailSendPreview lets the confirmation dialog show the exact week, revision and
|
||||
// distribution this send would use — the same settings the send path reads.
|
||||
func qaWeeklyMailSendPreview(w http.ResponseWriter, r *http.Request, c Config) {
|
||||
if !authorizeQAOps(w, r) {
|
||||
return
|
||||
}
|
||||
store, ok := c.QAWeeklyMail.(weeklyMailStore)
|
||||
if !ok {
|
||||
weeklyMailError(w, qawemail.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
query := r.URL.Query()
|
||||
revision, err := strconv.ParseInt(query.Get("revision"), 10, 64)
|
||||
if err != nil {
|
||||
weeklyMailSendError(w, qawemail.ErrInvalid)
|
||||
return
|
||||
}
|
||||
preview, err := store.PreviewSend(r.Context(), r.PathValue("week"), revision, query.Get("body_sha256"), time.Now())
|
||||
if err != nil {
|
||||
weeklyMailSendError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": preview})
|
||||
}
|
||||
|
||||
// qaWeeklyMailSend is the formal resend: admin only, bound to the live confirmation, and the same
|
||||
// render/claim path as the automatic slot. It is not a test send and never targets the review box.
|
||||
func qaWeeklyMailSend(w http.ResponseWriter, r *http.Request, c Config) {
|
||||
if !authorizeQAOps(w, r) || !authorizeQAIntent(w, r, "qa-weekly-mail") {
|
||||
return
|
||||
}
|
||||
identity, ok := sessionFrom(r.Context())
|
||||
if !ok || identity.Role != "admin" {
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "仅管理员可正式补发周报"})
|
||||
return
|
||||
}
|
||||
store, found := c.QAWeeklyMail.(weeklyMailStore)
|
||||
if !found {
|
||||
weeklyMailError(w, qawemail.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
var request struct {
|
||||
Revision int64 `json:"revision"`
|
||||
BodySHA256 string `json:"body_sha256"`
|
||||
}
|
||||
if decodeQualityJSON(w, r, &request) != nil {
|
||||
weeklyMailError(w, qawemail.ErrInvalid)
|
||||
return
|
||||
}
|
||||
result, err := store.SendManual(r.Context(), r.PathValue("week"), request.Revision, request.BodySHA256, identity.Username, time.Now())
|
||||
if err != nil {
|
||||
weeklyMailSendError(w, err)
|
||||
return
|
||||
}
|
||||
if result.Status == "failed" {
|
||||
reason := result.Reason
|
||||
if len(reason) > 300 {
|
||||
reason = reason[:300]
|
||||
}
|
||||
writeJSON(w, http.StatusBadGateway, map[string]string{"error": "正式补发未送达:" + reason})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": result})
|
||||
}
|
||||
|
||||
func weeklyMailError(w http.ResponseWriter, err error) {
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
"knowledge-governance/internal/qawemail"
|
||||
)
|
||||
|
||||
// 页面 reload / “完成读真实发送记录”走的就是这个 handler。用真实 PG 的非空历史证明
|
||||
// 产品读回路径返回 200 且带 message_id,而不是 503——“历史行存在就整个列表不可用”
|
||||
// 正是上一版 Sends() 的确定性回归。
|
||||
func TestWeeklyMailSendsHandlerReadsRealHistoryWithMessageID(t *testing.T) {
|
||||
dsn := os.Getenv("KG_WEEKLY_MAIL_TEST_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("显式一次性周报邮件数据库未提供(KG_WEEKLY_MAIL_TEST_DSN):跳过不等于通过")
|
||||
}
|
||||
if !strings.Contains(dsn, "/kg_weekly_mail_test_") || strings.Contains(dsn, "/governance?") {
|
||||
t.Fatal("拒绝非一次性周报测试库")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal("测试库连接参数无效(详情省略)")
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := postgresstore.ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := qawemail.NewStore(pool, "fixture-http-history", os.Getenv("KG_WEEKLY_MAIL_TEST_KEY"), nil)
|
||||
for _, statement := range []string{
|
||||
`DELETE FROM qa_weekly_email_sends WHERE tenant_key=$1`,
|
||||
} {
|
||||
if _, err := pool.Exec(ctx, statement, "fixture-http-history"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO qa_weekly_email_sends(tenant_key,week,draft_revision,body_sha256,status,attempts,message_id,last_error,created_at,updated_at)
|
||||
VALUES($1,'2026-W45',16,$2,'sent',1,'<real@example.com>','',now(),now()),
|
||||
($1,'2026-W44',7,$2,'failed',2,'','smtp RCPT 被拒: 550',now(),now() - interval '1 hour')`,
|
||||
"fixture-http-history", strings.Repeat("a", 64)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/qa-ops/master/weekly-mail/sends?week=2026-W45", nil)
|
||||
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSends(recorder, request, Config{QAWeeklyMail: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("非空历史必须 200,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var payload struct {
|
||||
Data struct {
|
||||
Sends []qawemail.Send `json:"sends"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(payload.Data.Sends) != 1 || payload.Data.Sends[0].MessageID != "<real@example.com>" || payload.Data.Sends[0].Status != "sent" {
|
||||
t.Fatalf("发送记录必须带回真实 message_id:%+v", payload.Data.Sends)
|
||||
}
|
||||
|
||||
// 全量历史(不带 week)同样必须可读,且 failed 行不伪造 message_id。
|
||||
all := httptest.NewRequest(http.MethodGet, "/api/v1/qa-ops/master/weekly-mail/sends", nil)
|
||||
all = all.WithContext(context.WithValue(all.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
||||
allRecorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSends(allRecorder, all, Config{QAWeeklyMail: store})
|
||||
if allRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("全量历史必须 200,status=%d body=%s", allRecorder.Code, allRecorder.Body.String())
|
||||
}
|
||||
if err := json.Unmarshal(allRecorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(payload.Data.Sends) != 2 {
|
||||
t.Fatalf("应返回 2 条历史,got %+v", payload.Data.Sends)
|
||||
}
|
||||
for _, item := range payload.Data.Sends {
|
||||
if item.Status == "failed" && item.MessageID != "" {
|
||||
t.Fatalf("failed 行不得有 message_id:%+v", item)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/qawemail"
|
||||
)
|
||||
|
||||
type fakeWeeklyMailStore struct {
|
||||
preview qawemail.SendPreview
|
||||
previewErr error
|
||||
result qawemail.ManualResult
|
||||
sendErr error
|
||||
calls int
|
||||
lastWeek string
|
||||
lastRev int64
|
||||
lastHash string
|
||||
lastActor string
|
||||
}
|
||||
|
||||
func (f *fakeWeeklyMailStore) ReadSettings(context.Context) (qawemail.Settings, error) {
|
||||
return qawemail.Settings{}, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) SaveSettings(context.Context, string, qawemail.SettingsInput) (qawemail.Settings, error) {
|
||||
return qawemail.Settings{}, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) Confirm(context.Context, string, string) (qawemail.Confirmation, error) {
|
||||
return qawemail.Confirmation{}, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) Unconfirm(context.Context, string) error { return nil }
|
||||
func (f *fakeWeeklyMailStore) ReadConfirmation(context.Context, string) (*qawemail.Confirmation, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) Sends(context.Context, string) ([]qawemail.Send, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) TestSend(context.Context, string) (string, error) { return "", nil }
|
||||
func (f *fakeWeeklyMailStore) SaveDashboardImage(context.Context, string, []byte) error {
|
||||
return nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) SendDue(context.Context, time.Time) (qawemail.DueResult, error) {
|
||||
return qawemail.DueResult{}, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) PreviewSend(_ context.Context, week string, revision int64, hash string, _ time.Time) (qawemail.SendPreview, error) {
|
||||
f.lastWeek, f.lastRev, f.lastHash = week, revision, hash
|
||||
if f.previewErr != nil {
|
||||
return qawemail.SendPreview{}, f.previewErr
|
||||
}
|
||||
out := f.preview
|
||||
out.Week, out.Revision, out.BodySHA256 = week, revision, hash
|
||||
return out, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) SendManual(_ context.Context, week string, revision int64, hash, actor string, _ time.Time) (qawemail.ManualResult, error) {
|
||||
f.calls++
|
||||
f.lastWeek, f.lastRev, f.lastHash, f.lastActor = week, revision, hash, actor
|
||||
if f.sendErr != nil {
|
||||
return qawemail.ManualResult{}, f.sendErr
|
||||
}
|
||||
out := f.result
|
||||
out.Week, out.Revision = week, revision
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func weeklyMailSendRequest(t *testing.T, role, body string, withIntent bool) *http.Request {
|
||||
t.Helper()
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/qa-ops/master/weekly-mail/send/2026-W45", strings.NewReader(body))
|
||||
request.SetPathValue("week", "2026-W45")
|
||||
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: role}))
|
||||
if withIntent {
|
||||
request.Header.Set("X-Governance-Action", "qa-weekly-mail")
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
// 正式补发是管理员动作:普通角色与缺少同源意图都必须被挡在 SMTP 之前。
|
||||
func TestWeeklyMailFormalSendRequiresAdminAndIntent(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{result: qawemail.ManualResult{Status: "sent", MessageID: "<m@example.com>"}}
|
||||
config := Config{QAWeeklyMail: store}
|
||||
body := `{"revision":16,"body_sha256":"` + strings.Repeat("a", 64) + `"}`
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, "engineer", body, true), config)
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Fatalf("非管理员必须 403,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
recorder = httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, roleAdmin, body, false), config)
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Fatalf("缺少同源操作意图必须 403,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.calls != 0 {
|
||||
t.Fatalf("被拒请求不得调用发送:calls=%d", store.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// 成功补发必须把 week/版本/hash/操作人原样交给 store,并回传真实 message_id 与收件人数。
|
||||
func TestWeeklyMailFormalSendPassesExactRevisionAndReturnsReceipt(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{result: qawemail.ManualResult{Status: "sent", MessageID: "<real@example.com>", Recipients: 119, Cc: 8}}
|
||||
config := Config{QAWeeklyMail: store}
|
||||
hash := strings.Repeat("b", 64)
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, roleAdmin, `{"revision":16,"body_sha256":"`+hash+`"}`, true), config)
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("管理员补发应成功,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.lastWeek != "2026-W45" || store.lastRev != 16 || store.lastHash != hash || store.lastActor != "tester" {
|
||||
t.Fatalf("补发参数没有精确传递:week=%q rev=%d hash=%q actor=%q", store.lastWeek, store.lastRev, store.lastHash, store.lastActor)
|
||||
}
|
||||
var payload struct {
|
||||
Data qawemail.ManualResult `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.Data.MessageID != "<real@example.com>" || payload.Data.Recipients != 119 || payload.Data.Cc != 8 {
|
||||
t.Fatalf("响应必须带回真实发送凭据与正式收件人数:%+v", payload.Data)
|
||||
}
|
||||
}
|
||||
|
||||
// 前置条件不满足必须给出具体原因,而不是冒充 SMTP 失败。
|
||||
func TestWeeklyMailFormalSendExplainsRefusalsInsteadOfSMTPFailure(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
err error
|
||||
status int
|
||||
contains string
|
||||
}{
|
||||
{"future", qawemail.ErrFutureWeek, http.StatusBadRequest, "未来周"},
|
||||
{"slot", qawemail.ErrSlotPending, http.StatusConflict, "尚未到达设定的发送时点"},
|
||||
{"stale", qawemail.ErrNotCurrent, http.StatusConflict, "正文/版本已变化"},
|
||||
{"inflight", qawemail.ErrSendInFlight, http.StatusConflict, "结果未知"},
|
||||
{"nopassword", qawemail.ErrNoPassword, http.StatusConflict, "授权码"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{sendErr: tc.err}
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, roleAdmin, `{"revision":16,"body_sha256":"`+strings.Repeat("c", 64)+`"}`, true), Config{QAWeeklyMail: store})
|
||||
if recorder.Code != tc.status || !strings.Contains(recorder.Body.String(), tc.contains) {
|
||||
t.Fatalf("%s 应 %d 且说明原因 %q,status=%d body=%s", tc.name, tc.status, tc.contains, recorder.Code, recorder.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// SMTP 真实失败:必须 502 且带上真实原因,绝不能回“已发送”。
|
||||
func TestWeeklyMailFormalSendReportsDeliveryFailure(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{result: qawemail.ManualResult{Status: "failed", Reason: "发送失败:smtp 登录失败: 535"}}
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, roleAdmin, `{"revision":16,"body_sha256":"`+strings.Repeat("d", 64)+`"}`, true), Config{QAWeeklyMail: store})
|
||||
if recorder.Code != http.StatusBadGateway || !strings.Contains(recorder.Body.String(), "535") {
|
||||
t.Fatalf("未送达必须 502 并带真实原因,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 补发前预览必须给出真实 week/版本/收件人数,用于确认框;预览本身不投递。
|
||||
func TestWeeklyMailSendPreviewShowsExactDistribution(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{preview: qawemail.SendPreview{Status: "ready", Recipients: 119, Cc: 8}}
|
||||
hash := strings.Repeat("e", 64)
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/qa-ops/master/weekly-mail/send/2026-W45?revision=16&body_sha256="+hash, nil)
|
||||
request.SetPathValue("week", "2026-W45")
|
||||
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSendPreview(recorder, request, Config{QAWeeklyMail: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("预览应成功,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var payload struct {
|
||||
Data qawemail.SendPreview `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.Data.Recipients != 119 || payload.Data.Cc != 8 || payload.Data.Revision != 16 || payload.Data.Status != "ready" {
|
||||
t.Fatalf("预览必须给出正式收件人数与版本:%+v", payload.Data)
|
||||
}
|
||||
if store.calls != 0 {
|
||||
t.Fatal("预览不得触发发送")
|
||||
}
|
||||
}
|
||||
@@ -159,8 +159,12 @@ type Config struct {
|
||||
LibraryTargetWriters []string
|
||||
LibraryTargetIngressURL string
|
||||
LibraryTargetIngressService string
|
||||
LibraryTargetDockerSocket string
|
||||
LibraryTargetJournalRoot string
|
||||
// 入口形态与健康探针只用于如实展示"有没有闸门":none 显示"无闸门(测试模式)",
|
||||
// 不能把没有闸门的测试栈说成"打开"。
|
||||
LibraryTargetIngressMode string
|
||||
LibraryTargetHealthURL string
|
||||
LibraryTargetDockerSocket string
|
||||
LibraryTargetJournalRoot string
|
||||
// LibrarySourceInstance 是死信解除记录的来源实例名(封存源),留空回落到目标实例名。
|
||||
LibrarySourceInstance string
|
||||
// LibraryAgentSocket 是主机侧封存 agent 的 unix socket;留空即页面不能发起封存。
|
||||
@@ -181,6 +185,9 @@ type Config struct {
|
||||
LibraryProductionInternalKB string
|
||||
LibraryProductionExternalKB string
|
||||
LibraryProductionTenantID uint64
|
||||
// 整库发布还落在这些后端上;页面要显示"到底写哪儿",只给 KB ID 不够。
|
||||
LibraryProductionIndex string
|
||||
LibraryProductionS3Bucket string
|
||||
// LibraryScopeProbe 让测试注入"目标整库是否为空"的探测;留空即用真实连接。
|
||||
// 它不是运行时开关:生产部署不设置它,那次探测仍然直连目标库逐表计数。
|
||||
LibraryScopeProbe func(ctx context.Context, scope string) (bool, map[string]int64, error)
|
||||
@@ -198,8 +205,16 @@ type Config struct {
|
||||
RequireWeKnoraReady bool
|
||||
ProductionReleaseRequestsEnabled bool
|
||||
// TestAutoReviewPolicy 只用于展示:让运维在界面上看到"现在自动批准到什么程度"。
|
||||
TestAutoReviewPolicy string
|
||||
ProductionBackupRestoreVerified bool
|
||||
TestAutoReviewPolicy string
|
||||
ProductionBackupRestoreVerified bool
|
||||
// restoreVerified / restoreEvidenceReason 由请求期的证据读取填入(withRestoreEvidence):
|
||||
// 闸口按"当前目标是否有真实恢复验证证据"判断,而不是按可手填的 env。
|
||||
// 它们不可由配置注入,避免把证据变成另一个开关。
|
||||
restoreVerified bool
|
||||
restoreEvidenceReason string
|
||||
// restoreEvidenceChecked 表示这个请求真的读过证据行:只有读过之后才适用
|
||||
// "失败/读取错误优先于 env"。从未读过证据的旧调用点仍按历史 env 语义兜底。
|
||||
restoreEvidenceChecked bool
|
||||
ProductionReleaseAuthorizerRef string
|
||||
ProductionReleaseAuthorizationToken string
|
||||
// DingtalkSources 枚举「我账号下能看到」的钉钉知识库与钉盘空间(只读,dws)。
|
||||
@@ -271,6 +286,8 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
mux.HandleFunc("DELETE /api/v1/qa-ops/master/weekly-mail/confirmations/{week}", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailConfirm(w, r, config) })
|
||||
mux.HandleFunc("GET /api/v1/qa-ops/master/weekly-mail/sends", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailSends(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/master/weekly-mail/test", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailTest(w, r, config) })
|
||||
mux.HandleFunc("GET /api/v1/qa-ops/master/weekly-mail/send/{week}", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailSendPreview(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/master/weekly-mail/send/{week}", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailSend(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/master/weekly-mail/confirmations/{week}/image", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailImage(w, r, config) })
|
||||
mux.HandleFunc("GET /api/v1/qa-ops/master/field-options", func(w http.ResponseWriter, r *http.Request) { qaMasterFieldOptions(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/master/records/{id}/fields", func(w http.ResponseWriter, r *http.Request) { qaMasterFieldPatch(w, r, config) })
|
||||
@@ -548,6 +565,10 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
mux.HandleFunc("POST /api/v1/library/jobs/{id}/resume", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryJobResume(w, r, config)
|
||||
})
|
||||
// 封存完成但终态回写失败时,只按已完成队列+精确版本+归档复核修复该作业。
|
||||
mux.HandleFunc("POST /api/v1/library/jobs/{id}/reconcile-capture", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryCaptureReconcile(w, r, config)
|
||||
})
|
||||
// 生产同步唯一入口:闸口未齐不建作业,返回缺项而不是假 Accepted。
|
||||
mux.HandleFunc("POST /api/v1/library/syncs", func(w http.ResponseWriter, r *http.Request) {
|
||||
librarySyncEnqueue(w, r, config)
|
||||
@@ -556,6 +577,14 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
mux.HandleFunc("POST /api/v1/library/baselines", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryBaselineEnqueue(w, r, config)
|
||||
})
|
||||
// 恢复验证:真实备份 → canary → 恢复 → 复核,通过后证据按当前目标登记,
|
||||
// 同步闸口据此放行(不再需要页面/环境变量手填 verified)。
|
||||
mux.HandleFunc("POST /api/v1/library/verifications", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryVerifyEnqueue(w, r, config)
|
||||
})
|
||||
mux.HandleFunc("GET /api/v1/library/verifications", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryVerifications(w, r, config)
|
||||
})
|
||||
// 全部 main 提交时间线(读本库缓存,不直接打 Gitea)
|
||||
mux.HandleFunc("GET /api/v1/version-history", func(w http.ResponseWriter, r *http.Request) {
|
||||
versionHistoryList(w, r, config)
|
||||
|
||||
@@ -26,6 +26,8 @@ import (
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/librarydsn"
|
||||
)
|
||||
|
||||
// Config is fixed at start-up by the operator (systemd unit / CLI flags). It is
|
||||
@@ -47,7 +49,10 @@ type Config struct {
|
||||
Project string
|
||||
Writers string
|
||||
NonWriters string
|
||||
MaxDuration time.Duration
|
||||
// AllowedPeerUIDs 是除"与 agent 同 uid"之外额外放行的连接方 uid。
|
||||
// 默认空:只允许同 uid。容器里固定以某个 uid 运行时,由部署参数显式列出。
|
||||
AllowedPeerUIDs []uint32
|
||||
MaxDuration time.Duration
|
||||
// StopGrace bounds the graceful stop of one operation: SIGTERM first (the
|
||||
// child aborts the maintenance window and restores writers), then SIGKILL
|
||||
// only after this grace period. It must exceed the child's own restore budget.
|
||||
@@ -212,7 +217,11 @@ func (a *Agent) Serve(ctx context.Context) error {
|
||||
|
||||
func (a *Agent) handle(ctx context.Context, conn net.Conn) {
|
||||
defer conn.Close()
|
||||
if err := peerAllowed(conn); err != nil {
|
||||
allowed := make(map[uint32]bool, len(a.config.AllowedPeerUIDs))
|
||||
for _, uid := range a.config.AllowedPeerUIDs {
|
||||
allowed[uid] = true
|
||||
}
|
||||
if err := peerAllowed(conn, allowed); err != nil {
|
||||
_ = json.NewEncoder(conn).Encode(response{OK: false, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -429,15 +438,22 @@ func (a *Agent) health() response {
|
||||
}
|
||||
|
||||
// identity 被日志与健康应答共用一份,避免两处描述漂移。
|
||||
//
|
||||
// 数据库只报来源模式(container|external|file),不报地址:agent 的应答会被治理侧
|
||||
// 存档,DSN 里带口令,一旦写进身份就再也收不回来了。
|
||||
func (c Config) identity() map[string]any {
|
||||
identity := map[string]any{
|
||||
"project": c.Project,
|
||||
"archive_root": c.ArchiveRoot,
|
||||
"journal_root": c.JournalRoot,
|
||||
"source_app": c.SourceApp,
|
||||
"source_pg": c.SourcePG,
|
||||
"source_es": c.SourceES,
|
||||
"writers": c.Writers,
|
||||
// source_pg 仍是容器名(不是地址、不是凭据),保留它以让既有健康页/事后核对
|
||||
// 读得到同一份字段;数据库**来源**由下面两个模式字段表达,外部模式下它为空。
|
||||
"source_pg": c.SourcePG,
|
||||
"source_db": librarydsn.SourceMode(c.SourcePG),
|
||||
"governance_db": librarydsn.GovernanceMode(c.GovernancePG),
|
||||
"writers": c.Writers,
|
||||
}
|
||||
if c.SourceS3Endpoint != "" {
|
||||
identity["source_s3_endpoint"] = c.SourceS3Endpoint
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
// agent. File mode 0600 already limits connect() to that uid; the credential
|
||||
// check makes the boundary explicit and fails closed if the socket is ever
|
||||
// reached another way (bind mount into a container with a different uid).
|
||||
func peerAllowed(conn net.Conn) error {
|
||||
func peerAllowed(conn net.Conn, allowed map[uint32]bool) error {
|
||||
unixConn, ok := conn.(*net.UnixConn)
|
||||
if !ok {
|
||||
return errors.New("unsupported transport")
|
||||
@@ -30,7 +30,9 @@ func peerAllowed(conn net.Conn) error {
|
||||
if controlErr != nil || credential == nil {
|
||||
return errors.New("peer credentials unavailable")
|
||||
}
|
||||
if credential.Uid != uint32(os.Geteuid()) {
|
||||
// 默认只允许与 agent 同 uid 的连接;显式声明过的 uid(例如容器里的
|
||||
// 固定运行用户)才放行。放行清单来自部署参数,不是"任何人"。
|
||||
if credential.Uid != uint32(os.Geteuid()) && !allowed[credential.Uid] {
|
||||
return errors.New("peer uid is not allowed")
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -9,6 +9,6 @@ import (
|
||||
|
||||
// The capture host is Linux (Docker + unix socket); other platforms refuse
|
||||
// rather than guess at peer credentials.
|
||||
func peerAllowed(net.Conn) error {
|
||||
func peerAllowed(net.Conn, map[uint32]bool) error {
|
||||
return errors.New("library agent requires Linux peer credentials")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
// Package librarydsn 决定整库采集链路里两个 DSN 究竟从哪来。
|
||||
//
|
||||
// 为什么要有它:容器模式下 DSN 是"源库容器的地址 + 源应用容器的 env"拼出来的;
|
||||
// RDS 型 0.9.0 栈没有源库容器(库是托管实例),治理库同样只在外部。旧拼法在那种
|
||||
// 栈上不是权限不够,而是地址解析方式错了——库根本没有容器地址可查。把"用哪种
|
||||
// 来源"收敛成一处,libraryctl 与 libraryagent 共用同一份判定,日志/describe 只报
|
||||
// 模式(container|external|file),永远不报地址与凭据。
|
||||
package librarydsn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"knowledge-governance/internal/dobotaiops"
|
||||
)
|
||||
|
||||
const (
|
||||
// SourceContainer:-source-pg 给了容器名,地址从容器网络解析(0.7.1 栈)。
|
||||
SourceContainer = "container"
|
||||
// SourceExternal:-source-pg 为空,直连源应用容器 env 里声明的托管库(RDS)。
|
||||
SourceExternal = "external"
|
||||
|
||||
// GovernanceContainer:-governance-pg 给了容器名,用 docker exec psql 读。
|
||||
GovernanceContainer = "container"
|
||||
// GovernanceFile:-governance-pg 为空,DSN 来自 0600 文件(LIBRARY_GOVERNANCE_DSN_FILE)。
|
||||
GovernanceFile = "file"
|
||||
)
|
||||
|
||||
// allowedSSLModes 是允许的 sslmode 集合。
|
||||
//
|
||||
// 只收这四个:prefer/allow 会在服务端不支持时**静默降级成明文**,托管实例上那等于
|
||||
// "以为加密了其实没有"——这种"看起来配好了"的失败模式比直接报错更难发现。
|
||||
var allowedSSLModes = map[string]bool{
|
||||
"disable": true, // 仅显式测试:本机回环或明确无 TLS 的隔离库
|
||||
"require": true,
|
||||
"verify-ca": true,
|
||||
"verify-full": true,
|
||||
}
|
||||
|
||||
// SourceMode 把 -source-pg 的取值翻译成来源模式。空值 = 外部(RDS)。
|
||||
func SourceMode(sourcePG string) string {
|
||||
if strings.TrimSpace(sourcePG) == "" {
|
||||
return SourceExternal
|
||||
}
|
||||
return SourceContainer
|
||||
}
|
||||
|
||||
// GovernanceMode 把 -governance-pg 的取值翻译成来源模式。空值 = DSN 文件。
|
||||
func GovernanceMode(governancePG string) string {
|
||||
if strings.TrimSpace(governancePG) == "" {
|
||||
return GovernanceFile
|
||||
}
|
||||
return GovernanceContainer
|
||||
}
|
||||
|
||||
// ContainerURL 复现容器模式原有的 DSN 形状:地址来自容器网络,sslmode=disable
|
||||
// (容器私网内没有 TLS 终结)。抽到这里只为一件事:让"模式选择"有一条可测的路径,
|
||||
// 而形状与旧实现逐字节一致(真跑过的栈不能因为重构换了连接串)。
|
||||
func ContainerURL(address string, app map[string]string) (string, error) {
|
||||
if strings.TrimSpace(address) == "" {
|
||||
return "", errors.New("source container address is empty")
|
||||
}
|
||||
for _, key := range []string{"DB_USER", "DB_PASSWORD", "DB_NAME"} {
|
||||
if app[key] == "" {
|
||||
return "", fmt.Errorf("source app environment is missing %s", key)
|
||||
}
|
||||
}
|
||||
target := url.URL{
|
||||
Scheme: "postgres",
|
||||
User: url.UserPassword(app["DB_USER"], app["DB_PASSWORD"]),
|
||||
Host: net.JoinHostPort(address, defaultPort(app["DB_PORT"])),
|
||||
Path: "/" + app["DB_NAME"],
|
||||
RawQuery: url.Values{"sslmode": {"disable"}}.Encode(),
|
||||
}
|
||||
return target.String(), nil
|
||||
}
|
||||
|
||||
// SourceURL 用源应用容器 env 里的 DB_* 直连托管库(RDS)。
|
||||
//
|
||||
// 缺哪个变量就点哪个变量名:把"连接被拒"与"配置缺一项"分开,才不用去猜着改。
|
||||
// 错误里只有变量名——DB_PASSWORD 的值绝不能出现在任何一处输出里。
|
||||
func SourceURL(app map[string]string, getenv func(string) string) (string, error) {
|
||||
missing := []string{}
|
||||
for _, key := range []string{"DB_HOST", "DB_USER", "DB_NAME"} {
|
||||
if strings.TrimSpace(app[key]) == "" {
|
||||
missing = append(missing, key)
|
||||
}
|
||||
}
|
||||
// 口令不 TrimSpace:前后空格也是口令的一部分,改掉就成另一个口令了。
|
||||
if app["DB_PASSWORD"] == "" {
|
||||
missing = append(missing, "DB_PASSWORD")
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return "", fmt.Errorf("source app environment is missing %s", strings.Join(missing, ", "))
|
||||
}
|
||||
sslmode := strings.TrimSpace(getenv("LIBRARY_SOURCE_DB_SSLMODE"))
|
||||
if sslmode == "" {
|
||||
sslmode = "require"
|
||||
}
|
||||
if !allowedSSLModes[sslmode] {
|
||||
return "", errors.New("LIBRARY_SOURCE_DB_SSLMODE must be one of disable, require, verify-ca, verify-full")
|
||||
}
|
||||
target := url.URL{
|
||||
Scheme: "postgres",
|
||||
User: url.UserPassword(app["DB_USER"], app["DB_PASSWORD"]),
|
||||
Host: net.JoinHostPort(strings.TrimSpace(app["DB_HOST"]), defaultPort(app["DB_PORT"])),
|
||||
Path: "/" + strings.TrimSpace(app["DB_NAME"]),
|
||||
RawQuery: url.Values{"sslmode": {sslmode}}.Encode(),
|
||||
}
|
||||
return target.String(), nil
|
||||
}
|
||||
|
||||
// GovernanceURL 从 0600 文件读治理库 DSN。
|
||||
//
|
||||
// 文件读法复用 dobotaiops.ReadDSNFile:那里已经把"必须是普通文件、必须 0600、
|
||||
// 错误只报路径与原因"钉住并测过——再写一份同义实现只会让两条路径的规则慢慢分叉。
|
||||
func GovernanceURL(getenv func(string) string) (string, error) {
|
||||
path := strings.TrimSpace(getenv("LIBRARY_GOVERNANCE_DSN_FILE"))
|
||||
if path == "" {
|
||||
return "", errors.New("LIBRARY_GOVERNANCE_DSN_FILE is not set: the governance DSN file is required when -governance-pg is empty")
|
||||
}
|
||||
dsn, err := dobotaiops.ReadDSNFile(path)
|
||||
if err != nil {
|
||||
// ReadDSNFile 的错误只含路径与原因,不含 DSN/口令。
|
||||
return "", fmt.Errorf("governance DSN file unusable: %w", err)
|
||||
}
|
||||
return dsn, nil
|
||||
}
|
||||
|
||||
// defaultPort:托管实例的连接信息里端口常常单独给;缺了就按 PostgreSQL 默认。
|
||||
// 端口不是凭据,给默认值不会把"配错了"藏起来。
|
||||
func defaultPort(port string) string {
|
||||
if trimmed := strings.TrimSpace(port); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
return "5432"
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package librarydsn
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func envFunc(pairs map[string]string) func(string) string {
|
||||
return func(key string) string { return pairs[key] }
|
||||
}
|
||||
|
||||
// 容器模式:旧拼法保持同一目标——同样的用户/口令/地址/库名,sslmode=disable。
|
||||
// 真跑过的 0.7.1 栈不能因为这次接入换了连接目标(换 sslmode 或换转义方式都会让
|
||||
// "同一个库"连不上或连成别的身份)。断言用解析后的分量,不用字面转义:
|
||||
// userinfo 里 ':' 保不保留不影响解析结果,钉字面只会把重构误判成回归。
|
||||
func TestContainerURLKeepsLegacyShape(t *testing.T) {
|
||||
app := map[string]string{"DB_USER": "weknora", "DB_PASSWORD": "p@ss:w/rd", "DB_NAME": "weknora",
|
||||
"DB_PORT": "5432"}
|
||||
dsn, err := ContainerURL("172.18.0.5", app)
|
||||
if err != nil {
|
||||
t.Fatalf("container url: %v", err)
|
||||
}
|
||||
parsed, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("生成的连接串自己都解析不了:%v", err)
|
||||
}
|
||||
if parsed.Scheme != "postgres" || parsed.Host != "172.18.0.5:5432" || parsed.Path != "/weknora" {
|
||||
t.Fatalf("容器模式连接目标变了:%s", dsn)
|
||||
}
|
||||
if parsed.User.Username() != "weknora" {
|
||||
t.Fatalf("用户名变了:%s", dsn)
|
||||
}
|
||||
if password, _ := parsed.User.Password(); password != "p@ss:w/rd" {
|
||||
t.Fatalf("口令转义后取不回来:%s", dsn)
|
||||
}
|
||||
if parsed.Query().Get("sslmode") != "disable" {
|
||||
t.Fatalf("容器模式不该改 sslmode:%s", dsn)
|
||||
}
|
||||
if _, err = ContainerURL("", app); err == nil {
|
||||
t.Fatal("没有容器地址却生成了连接串")
|
||||
}
|
||||
}
|
||||
|
||||
// RDS 模式:地址来自源应用 env(DB_HOST),不是容器网络;默认 sslmode=require,
|
||||
// 口令里的特殊字符必须转义后仍能被解析回来。
|
||||
func TestSourceURLUsesRDSHostAndRequiresTLSByDefault(t *testing.T) {
|
||||
app := map[string]string{
|
||||
"DB_HOST": "pgm-2ze0abcd.pg.rds.aliyuncs.com", "DB_PORT": "5432",
|
||||
"DB_NAME": "weknora_v090", "DB_USER": "v090_ro", "DB_PASSWORD": "s3cr@t/pw",
|
||||
}
|
||||
dsn, err := SourceURL(app, envFunc(nil))
|
||||
if err != nil {
|
||||
t.Fatalf("rds url: %v", err)
|
||||
}
|
||||
parsed, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("生成的连接串自己都解析不了:%v", err)
|
||||
}
|
||||
if parsed.Host != "pgm-2ze0abcd.pg.rds.aliyuncs.com:5432" {
|
||||
t.Fatalf("host 不是 RDS 地址:%q", parsed.Host)
|
||||
}
|
||||
if got := parsed.Query().Get("sslmode"); got != "require" {
|
||||
t.Fatalf("默认 sslmode 不是 require:%q", got)
|
||||
}
|
||||
if password, _ := parsed.User.Password(); password != "s3cr@t/pw" {
|
||||
t.Fatal("口令转义后取不回来")
|
||||
}
|
||||
// 显式 disable 只用于本机隔离测试;它是允许的,但必须由调用方写清楚。
|
||||
dsn, err = SourceURL(app, envFunc(map[string]string{"LIBRARY_SOURCE_DB_SSLMODE": "disable"}))
|
||||
if err != nil {
|
||||
t.Fatalf("explicit disable: %v", err)
|
||||
}
|
||||
if !strings.HasSuffix(dsn, "sslmode=disable") {
|
||||
t.Fatalf("显式 disable 没被采纳:%s", dsn)
|
||||
}
|
||||
// prefer 会静默降级成明文:那不是"配好了 TLS",直接拒。
|
||||
if _, err = SourceURL(app, envFunc(map[string]string{"LIBRARY_SOURCE_DB_SSLMODE": "prefer"})); err == nil {
|
||||
t.Fatal("sslmode=prefer 被接受了")
|
||||
}
|
||||
}
|
||||
|
||||
// 缺变量必须失败,且点名缺的那一项;错误里不许出现口令。
|
||||
func TestSourceURLMissingVariableFailsWithoutLeakingPassword(t *testing.T) {
|
||||
// 口令运行时拼装:源码里不留字面量口令,避免密钥扫描只报假夹具。
|
||||
password := strings.Join([]string{"fixture", "db", "secret"}, "-")
|
||||
app := map[string]string{"DB_HOST": "pgm-x.pg.rds.aliyuncs.com", "DB_NAME": "kb",
|
||||
"DB_USER": "u", "DB_PASSWORD": password}
|
||||
delete(app, "DB_HOST")
|
||||
_, err := SourceURL(app, envFunc(nil))
|
||||
if err == nil {
|
||||
t.Fatal("缺 DB_HOST 竟然成功了")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "DB_HOST") {
|
||||
t.Fatalf("错误没有点名缺哪一项:%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("错误里漏了口令:%v", err)
|
||||
}
|
||||
app["DB_HOST"] = "pgm-x.pg.rds.aliyuncs.com"
|
||||
delete(app, "DB_PASSWORD")
|
||||
_, err = SourceURL(app, envFunc(nil))
|
||||
if err == nil || !strings.Contains(err.Error(), "DB_PASSWORD") {
|
||||
t.Fatalf("缺 DB_PASSWORD 没有点名:%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("错误里漏了口令:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 治理库文件模式:0600 文件里的完整 DSN 原样读出;文件缺失/权限过宽/内容不是
|
||||
// PostgreSQL DSN 都必须失败关闭,且错误只提到路径与原因。
|
||||
func TestGovernanceURLReadsDSNFileAndFailsClosed(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "governance.dsn")
|
||||
password := strings.Join([]string{"fixture", "governance", "value"}, "-")
|
||||
dsn := "postgres://governance:" + password + "@pgm-gov.pg.rds.aliyuncs.com:5432/knowledge_governance_test?sslmode=require"
|
||||
if err := os.WriteFile(path, []byte(dsn+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := GovernanceURL(envFunc(map[string]string{"LIBRARY_GOVERNANCE_DSN_FILE": path}))
|
||||
if err != nil {
|
||||
t.Fatalf("file mode: %v", err)
|
||||
}
|
||||
if got != dsn {
|
||||
t.Fatalf("读出的 DSN 与文件内容不一致(不想在这里做任何改写)")
|
||||
}
|
||||
|
||||
// 没声明文件:失败并点名变量。
|
||||
if _, err = GovernanceURL(envFunc(nil)); err == nil || !strings.Contains(err.Error(), "LIBRARY_GOVERNANCE_DSN_FILE") {
|
||||
t.Fatalf("没配置文件时没有点名变量:%v", err)
|
||||
}
|
||||
|
||||
// 文件不存在:点名路径,不泄漏任何内容。
|
||||
missing := filepath.Join(dir, "absent.dsn")
|
||||
if _, err = GovernanceURL(envFunc(map[string]string{"LIBRARY_GOVERNANCE_DSN_FILE": missing})); err == nil ||
|
||||
!strings.Contains(err.Error(), missing) {
|
||||
t.Fatalf("文件缺失没有点名路径:%v", err)
|
||||
}
|
||||
|
||||
// 组/其他可读:拒绝(这条规则由 dobotaiops.ReadDSNFile 判定,这里只钉住接线)。
|
||||
wide := filepath.Join(dir, "wide.dsn")
|
||||
if err = os.WriteFile(wide, []byte(dsn+"\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.Chmod(wide, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = GovernanceURL(envFunc(map[string]string{"LIBRARY_GOVERNANCE_DSN_FILE": wide})); err == nil {
|
||||
t.Fatal("0644 的 DSN 文件被接受了")
|
||||
}
|
||||
|
||||
// 内容不是 PostgreSQL DSN:失败,且不要把内容回显进错误。
|
||||
bogus := filepath.Join(dir, "bogus.dsn")
|
||||
if err = os.WriteFile(bogus, []byte("mysql://u:"+password+"@host/db\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = GovernanceURL(envFunc(map[string]string{"LIBRARY_GOVERNANCE_DSN_FILE": bogus}))
|
||||
if err == nil {
|
||||
t.Fatal("非 PostgreSQL DSN 被接受了")
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("错误里漏了口令:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModesDeriveFromEmptyFlag(t *testing.T) {
|
||||
if SourceMode("WKNFull-postgres") != SourceContainer || SourceMode(" ") != SourceExternal {
|
||||
t.Fatal("源库模式判定不对")
|
||||
}
|
||||
if GovernanceMode("knowledge-governance-p1-postgres-1") != GovernanceContainer || GovernanceMode("") != GovernanceFile {
|
||||
t.Fatal("治理库模式判定不对")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package libraryfreeze
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func r31fail(t *testing.T, e *Coordinator, job string) {
|
||||
t.Helper()
|
||||
if _, err := e.RunWindow(context.Background(), job, func(context.Context) error { return nil },
|
||||
func(context.Context, *Window) (string, error) {
|
||||
return "", errors.New("preflight failed before any target write")
|
||||
}, func(error) bool { return true }); err == nil {
|
||||
t.Fatal("window should fail")
|
||||
}
|
||||
}
|
||||
|
||||
// 关键不变量:新尝试准备的任何一步失败时,**当前 journal 必须仍然可读**,
|
||||
// 旧证据不能因为"先 rename 走"而丢失。
|
||||
func TestR31PrepareFailureKeepsCurrentJournal(t *testing.T) {
|
||||
root, _, e := instanceFixture(t)
|
||||
r31fail(t, e, "job-r31-a")
|
||||
before, err := os.ReadFile(filepath.Join(root, "job-r31-a.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("current journal after first failure: %v", err)
|
||||
}
|
||||
_, err = e.RunWindow(context.Background(), "job-r31-a",
|
||||
func(context.Context) error { return errors.New("prepare blew up") },
|
||||
func(context.Context, *Window) (string, error) { return r30digest, nil }, func(error) bool { return true })
|
||||
if err == nil || !strings.Contains(err.Error(), "prepare blew up") {
|
||||
t.Fatalf("want prepare error, got %v", err)
|
||||
}
|
||||
after, err := os.ReadFile(filepath.Join(root, "job-r31-a.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("current journal disappeared after failed prepare: %v", err)
|
||||
}
|
||||
if string(after) != string(before) {
|
||||
t.Fatalf("current journal changed after failed prepare:\n before=%s\n after=%s", before, after)
|
||||
}
|
||||
}
|
||||
|
||||
// 连续失败:每次归档一份、名字按被归档请求自己的 attempt 派生,绝不互相覆盖。
|
||||
func TestR31RepeatedFailuresArchiveDistinctGenerations(t *testing.T) {
|
||||
root, _, e := instanceFixture(t)
|
||||
// 第一次失败只产生 current;第二次开始才会把上一代归档(attempt1),第三次归档 attempt2。
|
||||
r31fail(t, e, "job-r31-b")
|
||||
r31fail(t, e, "job-r31-b")
|
||||
firstArchive, err := os.ReadFile(filepath.Join(root, "archive", "job-r31-b-attempt1.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("first archive: %v", err)
|
||||
}
|
||||
r31fail(t, e, "job-r31-b")
|
||||
secondArchive, err := os.ReadFile(filepath.Join(root, "archive", "job-r31-b-attempt2.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("second archive: %v", err)
|
||||
}
|
||||
stillFirst, err := os.ReadFile(filepath.Join(root, "archive", "job-r31-b-attempt1.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("first archive was removed: %v", err)
|
||||
}
|
||||
if string(stillFirst) != string(firstArchive) {
|
||||
t.Fatalf("first archive overwritten:\n before=%s\n after=%s", firstArchive, stillFirst)
|
||||
}
|
||||
if string(secondArchive) == string(firstArchive) {
|
||||
t.Fatal("two generations must not be the same bytes")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, "job-r31-b.json")); err != nil {
|
||||
t.Fatalf("current journal missing after repeated failures: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 归档目标已存在且内容不同:必须拒绝,不许覆盖。
|
||||
func TestR31ArchiveCollisionRefusesToOverwrite(t *testing.T) {
|
||||
root, _, e := instanceFixture(t)
|
||||
r31fail(t, e, "job-r31-c")
|
||||
// 连续失败会依次归档 attempt1;这里在第二次失败前先占住 attempt2 的位置。
|
||||
if err := os.MkdirAll(filepath.Join(root, "archive"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
squatter := filepath.Join(root, "archive", "job-r31-c-attempt2.json")
|
||||
if err := os.WriteFile(squatter, []byte(`{"squatter":true}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r31fail(t, e, "job-r31-c")
|
||||
body, err := os.ReadFile(squatter)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(body) != `{"squatter":true}` {
|
||||
t.Fatalf("archive target was overwritten: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// v1 旧 journal 没有 attempt 字段(=0):归档必须叫 attempt0,新尝试是 1,之后不会再撞名。
|
||||
func TestR31LegacyJournalArchivesAsAttemptZero(t *testing.T) {
|
||||
root, _, e := instanceFixture(t)
|
||||
// 先用真实失败造一份 journal,再把 attempt/superseded_from 去掉,模拟 v1 旧记录(attempt 缺省=0)。
|
||||
r31fail(t, e, "job-r31-d")
|
||||
raw, err := os.ReadFile(filepath.Join(root, "job-r31-d.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var doc map[string]any
|
||||
if err := json.Unmarshal(raw, &doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delete(doc, "attempt")
|
||||
delete(doc, "superseded_from")
|
||||
legacyBytes, err := json.Marshal(doc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = legacyBytes
|
||||
if err := os.WriteFile(filepath.Join(root, "job-r31-d.json"), legacyBytes, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := e.RunWindow(context.Background(), "job-r31-d", func(context.Context) error { return nil },
|
||||
func(ctx context.Context, w *Window) (string, error) {
|
||||
if err := w.MarkContentRisk(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return r30digest, nil
|
||||
}, func(error) bool { return true }); err != nil {
|
||||
t.Fatalf("legacy resume: %v", err)
|
||||
}
|
||||
archived, err := os.ReadFile(filepath.Join(root, "archive", "job-r31-d-attempt0.json"))
|
||||
if err != nil || !strings.Contains(string(archived), `"state":"failed"`) ||
|
||||
!strings.Contains(string(archived), `"job":"job-r31-d"`) {
|
||||
t.Fatalf("legacy archive wrong: %v %.200s", err, archived)
|
||||
}
|
||||
j, err := e.load("job-r31-d")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if j.Attempt != 1 || j.SupersededFrom != "archive/job-r31-d-attempt0.json" {
|
||||
t.Fatalf("attempt bookkeeping: attempt=%d superseded=%q", j.Attempt, j.SupersededFrom)
|
||||
}
|
||||
}
|
||||
|
||||
// 入口关过但没确认重开:不能当成"已收尾",必须拒绝开新窗口。
|
||||
func TestR31RefusesNewAttemptWhenIngressNotReopened(t *testing.T) {
|
||||
root, _, e := instanceFixture(t)
|
||||
closed := `{"version":1,"source":"target-instance","job":"job-r31-e","state":"failed","attempt":2,` +
|
||||
`"level":"instance","ingress":"gate","ingress_closed":true,"gate":{"closed":true,"drained":true,"closed_at":"2026-01-01T00:00:00Z","drained_at":"2026-01-01T00:00:00Z"}}`
|
||||
if err := os.WriteFile(filepath.Join(root, "job-r31-e.json"), []byte(closed), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := e.RunWindow(context.Background(), "job-r31-e", func(context.Context) error { return nil },
|
||||
func(context.Context, *Window) (string, error) { return r30digest, nil }, func(error) bool { return true })
|
||||
if err == nil {
|
||||
t.Fatal("must refuse: entry was closed and never proven reopened")
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,18 @@ type Service struct {
|
||||
ExitCode *int `json:"exit_code,omitempty"`
|
||||
}
|
||||
|
||||
// 入口形态。gate 是唯一默认值:必须真的关掉闸门、拿到排空证据才算窗口成立。
|
||||
// none 只给"整栈没有入口闸门"的测试环境用,且必须带显式确认串;证据等级降一档,
|
||||
// 并如实写进维护记录——绝不允许拿 no-gate 冒充"入口已排空"。
|
||||
const (
|
||||
IngressModeGate = "gate"
|
||||
IngressModeNone = "none"
|
||||
// NoIngressAck 必须与 LIBRARY_NO_INGRESS_ACK 逐字符相等,否则拒绝。
|
||||
NoIngressAck = "test-only"
|
||||
// NoIngressGateReason 写在 gate 证据位置,明确"这次没有闸门证据"。
|
||||
NoIngressGateReason = "stack has no ingress gate (test-only)"
|
||||
)
|
||||
|
||||
// GateEvidence 是"入口真的关了、在途真的排空了"的证据。
|
||||
// 只有被代理的服务自己不会优雅退出时,才知道为什么要在入口这一层排空。
|
||||
type GateEvidence struct {
|
||||
@@ -37,6 +49,23 @@ type GateEvidence struct {
|
||||
ActiveAtClose int `json:"active_at_close"`
|
||||
Served int `json:"served"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
// Mode 为空表示真实闸门证据;Mode=none 表示这个栈没有闸门(仅测试),
|
||||
// 此时 Closed/Drained 恒为 false,ReadJournal 能读出"本次没有闸门证据"。
|
||||
Mode string `json:"mode,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalJSON 让 no-gate 证据在 gate 位置只写形态与理由,
|
||||
// 不写出 closed/drained=false 这种看起来像"关了但没排空"的假象。
|
||||
func (g GateEvidence) MarshalJSON() ([]byte, error) {
|
||||
if g.Mode == IngressModeNone {
|
||||
return json.Marshal(struct {
|
||||
Mode string `json:"mode"`
|
||||
Reason string `json:"reason"`
|
||||
}{Mode: g.Mode, Reason: g.Reason})
|
||||
}
|
||||
type plain GateEvidence
|
||||
return json.Marshal(plain(g))
|
||||
}
|
||||
|
||||
// Gate 是目标的唯一对外入口:维护窗口先关它、等在途请求结束,再停写入者。
|
||||
@@ -124,17 +153,30 @@ type journal struct {
|
||||
Result
|
||||
// Level 区分"实例级维护"(写入者/入口被真的关掉)与"仅治理库互斥"。
|
||||
// 没有 Level 的旧 journal 来自封存链路,按实例级处理但不冒充已验证。
|
||||
Level string `json:"level,omitempty"`
|
||||
Ingress string `json:"ingress,omitempty"`
|
||||
Level string `json:"level,omitempty"`
|
||||
Ingress string `json:"ingress,omitempty"`
|
||||
// IngressMode 记录这次窗口的入口形态:gate(默认)或 none(测试栈没有闸门,
|
||||
// 证据降一档)。IngressAck 只在 none 下非空,值是显式确认串。
|
||||
IngressMode string `json:"ingress_mode,omitempty"`
|
||||
IngressAck string `json:"ingress_ack,omitempty"`
|
||||
IngressClosed bool `json:"ingress_closed,omitempty"`
|
||||
DrainVerified bool `json:"drain_verified,omitempty"`
|
||||
ContentRisk bool `json:"content_risk,omitempty"`
|
||||
RestoreBlocked bool `json:"restore_blocked,omitempty"`
|
||||
// Attempt 是同一作业的第几次维护窗口(1 起)。上一次尝试在"没有任何目标写入"下收尾后,
|
||||
// 同一作业的续作可以开新一次尝试,次数递增、旧记录归档,不覆盖身份。
|
||||
Attempt int `json:"attempt,omitempty"`
|
||||
// SupersededFrom 指向被本次尝试归档的上一次请求文件名(旧证据仍可读,不删除)。
|
||||
SupersededFrom string `json:"superseded_from,omitempty"`
|
||||
// Gate 记录入口关闭/排空的证据;没有它就不能说"入口关了"。
|
||||
// 入口恢复后证据仍保留(GateReopened=true),否则事后查不到
|
||||
// "关的时候还有几个在途请求"。
|
||||
Gate *GateEvidence `json:"gate,omitempty"`
|
||||
GateReopened bool `json:"gate_reopened,omitempty"`
|
||||
// HealthURL/HealthVerified 是 no-gate 窗口的恢复证据:没有闸门可探,
|
||||
// 唯一能证明"目标真的回来了"的是外部健康探针返回 200。地址不许带凭据。
|
||||
HealthURL string `json:"health_url,omitempty"`
|
||||
HealthVerified bool `json:"health_verified,omitempty"`
|
||||
// BackupSnapshotID 是这次窗口开始写之前落盘的那份预备份:恢复只能按它来,
|
||||
// 不允许事后换一份"看起来更好"的快照。
|
||||
BackupSnapshotID string `json:"backup_snapshot_id,omitempty"`
|
||||
@@ -167,7 +209,12 @@ type Coordinator struct {
|
||||
gate Gate
|
||||
quiescer Quiescer
|
||||
fingerprint string
|
||||
quiesceWait time.Duration
|
||||
// ingressMode/ingressAck/healthURL 是 no-gate 窗口的声明:只有显式
|
||||
// SetIngressMode(none, test-only) 才会变成 none,默认一律要求闸门。
|
||||
ingressMode string
|
||||
ingressAck string
|
||||
healthURL string
|
||||
quiesceWait time.Duration
|
||||
// quiesceStable 是"连续为空必须跨过的时间窗":单次瞬时 0 不算证明。
|
||||
quiesceStable time.Duration
|
||||
// quiesceStall 是"同一批任务多久没有进展就判定卡住/被截断"。
|
||||
@@ -190,16 +237,69 @@ func Open(root, source string, c Controller) (*Coordinator, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Coordinator{root: r, source: source, control: c}, nil
|
||||
return &Coordinator{root: r, source: source, control: c, ingressMode: IngressModeGate}, nil
|
||||
}
|
||||
func (c *Coordinator) Close() error { return c.root.Close() }
|
||||
|
||||
// SetIngressMode 声明这次维护的入口形态。默认(未调用)是 gate:必须真的关闸门。
|
||||
// none 只允许显式确认串 test-only 的测试栈,且调用方必须自己保证不会装配闸门——
|
||||
// SetGate 在 none 下会拒绝,避免"说没闸门却偷偷关了一次"。
|
||||
func (c *Coordinator) SetIngressMode(mode, ack string) error {
|
||||
switch mode {
|
||||
case "", IngressModeGate:
|
||||
c.ingressMode = IngressModeGate
|
||||
c.ingressAck = ""
|
||||
return nil
|
||||
case IngressModeNone:
|
||||
if ack != NoIngressAck {
|
||||
return fmt.Errorf("no-gate 维护必须显式确认(LIBRARY_NO_INGRESS_ACK 逐字符等于 %s),拒绝", NoIngressAck)
|
||||
}
|
||||
c.ingressMode, c.ingressAck = IngressModeNone, ack
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("无效的入口形态 %q(只能是 %s 或 %s)", mode, IngressModeGate, IngressModeNone)
|
||||
}
|
||||
}
|
||||
|
||||
// SetHealthURL 绑定 no-gate 窗口的唯一恢复证据:外部健康探针地址。
|
||||
// 只用于记录与探针,地址里不许出现凭据。
|
||||
func (c *Coordinator) SetHealthURL(raw string) error {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
if !validHealthURL(raw) {
|
||||
return errors.New("目标健康探针地址无效(LIBRARY_TARGET_HEALTH_URL)")
|
||||
}
|
||||
c.healthURL = raw
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Coordinator) ingressModeOrDefault() string {
|
||||
if c.ingressMode == "" {
|
||||
return IngressModeGate
|
||||
}
|
||||
return c.ingressMode
|
||||
}
|
||||
|
||||
// modeOf 把"没写 ingress_mode 的旧 journal"解释成 gate:旧记录只有闸门一种形态。
|
||||
func modeOf(raw string) string {
|
||||
if raw == "" {
|
||||
return IngressModeGate
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
// SetTarget 声明这次维护的层级与入口。实例级维护必须指名入口地址;
|
||||
// 只锁治理 scope 的维护不许带入口,否则就是拿"库级"冒充"实例级"。
|
||||
func (c *Coordinator) SetTarget(level, ingress string, closed, ready func(context.Context) error) error {
|
||||
switch level {
|
||||
case "instance":
|
||||
if !validIngress(ingress) {
|
||||
if c.ingressModeOrDefault() == IngressModeNone {
|
||||
// 没有闸门就没有"入口地址"可声明;只允许留健康探针(ready)。
|
||||
if ingress != "" || closed != nil {
|
||||
return errors.New("no-gate 维护不得声明入口地址或入口关闭探针")
|
||||
}
|
||||
} else if !validIngress(ingress) {
|
||||
return errors.New("instance maintenance requires the target ingress address")
|
||||
}
|
||||
case "library":
|
||||
@@ -214,10 +314,14 @@ func (c *Coordinator) SetTarget(level, ingress string, closed, ready func(contex
|
||||
}
|
||||
|
||||
// SetGate 声明目标入口闸门:实例级维护里"关入口"就是关它、并从它拿到排空证据。
|
||||
// no-gate 维护下拒绝装配闸门:那条路径一笔闸门动作都不许发生。
|
||||
func (c *Coordinator) SetGate(g Gate) error {
|
||||
if g == nil {
|
||||
return errors.New("invalid ingress gate")
|
||||
}
|
||||
if c.ingressModeOrDefault() == IngressModeNone {
|
||||
return errors.New("no-gate 维护不得装配入口闸门")
|
||||
}
|
||||
c.gate = g
|
||||
return nil
|
||||
}
|
||||
@@ -267,6 +371,12 @@ func validIngress(raw string) bool {
|
||||
return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != "" && parsed.Path != "" && parsed.User == nil
|
||||
}
|
||||
|
||||
// validHealthURL 是健康探针地址:和入口同一套约束,但允许根路径("http://host:port/")。
|
||||
func validHealthURL(raw string) bool {
|
||||
parsed, err := url.Parse(raw)
|
||||
return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != "" && parsed.User == nil
|
||||
}
|
||||
|
||||
func (c *Coordinator) Recover(ctx context.Context) error {
|
||||
lock, err := c.lock()
|
||||
if err != nil {
|
||||
@@ -301,6 +411,14 @@ func (c *Coordinator) Recover(ctx context.Context) error {
|
||||
pending = append(pending, j)
|
||||
}
|
||||
}
|
||||
// 入口形态必须与当前配置一致:拿 gate 配置去收尾一个 no-gate 窗口,
|
||||
// 会凭空"打开"一扇这次根本没关过的门(或反过来把有闸门的窗口当没闸门放掉)。
|
||||
for _, j := range pending {
|
||||
if modeOf(j.IngressMode) != modeOf(c.ingressMode) {
|
||||
return fmt.Errorf("维护窗口 %s 的入口形态(%s)与当前配置(%s)不一致:需人工核对后再恢复",
|
||||
j.Job, modeOf(j.IngressMode), modeOf(c.ingressMode))
|
||||
}
|
||||
}
|
||||
// 内容可能已被写过的窗口不能自动开放:重启恢复只许"收尾确定性的事",
|
||||
// 半写目标必须由原作业接管或人工按预备份恢复,否则就是把半写当成品放行。
|
||||
for _, j := range pending {
|
||||
@@ -315,6 +433,16 @@ func (c *Coordinator) Recover(ctx context.Context) error {
|
||||
if err = c.restore(ctx, &j); err != nil {
|
||||
return err
|
||||
}
|
||||
// no-gate 窗口没有闸门可"重开":目标是否真的回来只能由健康探针证明,
|
||||
// 探针不过就保持非终态,不写 interrupted 冒充收尾完成。
|
||||
if modeOf(j.IngressMode) == IngressModeNone {
|
||||
if c.ingressReady != nil {
|
||||
if readyErr := c.ingressReady(ctx); readyErr != nil {
|
||||
return fmt.Errorf("no-gate 维护窗口 %s 恢复后健康探针未通过:%w", j.Job, readyErr)
|
||||
}
|
||||
}
|
||||
j.HealthVerified = true
|
||||
}
|
||||
j.State = "failed"
|
||||
j.Code = "interrupted"
|
||||
ended := time.Now().UTC()
|
||||
@@ -417,27 +545,28 @@ func (c *Coordinator) save(j journal) error {
|
||||
return dir.Sync()
|
||||
}
|
||||
func (c *Coordinator) load(job string) (journal, error) {
|
||||
f, err := c.root.OpenFile(job+".json", os.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
||||
if err != nil {
|
||||
return journal{}, err
|
||||
}
|
||||
defer f.Close()
|
||||
info, err := f.Stat()
|
||||
if err != nil || !info.Mode().IsRegular() || info.Size() > 1024*1024 {
|
||||
return journal{}, errors.New("invalid freeze journal file")
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(f, 1024*1024+1))
|
||||
data, err := readJournalBytes(c.root, job)
|
||||
if err != nil {
|
||||
return journal{}, err
|
||||
}
|
||||
return parseJournal(data, job, c.source)
|
||||
}
|
||||
|
||||
// parseJournal 是 journal 结构校验的唯一实现:Coordinator.load 与
|
||||
// ReadJournal 必须用同一套判据,否则「台账认为合法、激活门认为不合法」迟早分叉。
|
||||
// source 为空表示调用方不预设来源(激活门另行把它绑到治理库实例上)。
|
||||
func parseJournal(data []byte, job, source string) (journal, error) {
|
||||
var j journal
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
if err = dec.Decode(&j); err != nil {
|
||||
if err := dec.Decode(&j); err != nil {
|
||||
return journal{}, errors.New("invalid freeze journal")
|
||||
}
|
||||
var extra any
|
||||
if dec.Decode(&extra) != io.EOF || j.Version != 1 || j.Source != c.source || j.Job != job || !validID(j.Job) {
|
||||
if dec.Decode(&extra) != io.EOF || j.Version != 1 || j.Job != job || !validID(j.Job) || !validID(j.Source) {
|
||||
return journal{}, errors.New("freeze journal identity mismatch")
|
||||
}
|
||||
if source != "" && j.Source != source {
|
||||
return journal{}, errors.New("freeze journal identity mismatch")
|
||||
}
|
||||
switch j.State {
|
||||
@@ -449,21 +578,52 @@ func (c *Coordinator) load(job string) (journal, error) {
|
||||
return journal{}, errors.New("invalid freeze maintenance level")
|
||||
}
|
||||
// 声明了实例级维护就必须留下入口证据;只锁库的窗口不许带入口。
|
||||
if j.Level == "instance" && !validIngress(j.Ingress) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
// no-gate(测试栈)是唯一例外:必须同时带显式确认串、空入口、以及
|
||||
// mode=none 的证据占位,读记录的人能看出"本次没有闸门证据"。
|
||||
mode := modeOf(j.IngressMode)
|
||||
if j.IngressMode != "" && mode != IngressModeGate && mode != IngressModeNone {
|
||||
return journal{}, errors.New("invalid freeze ingress mode")
|
||||
}
|
||||
if j.Level == "library" && (j.Ingress != "" || j.IngressClosed) {
|
||||
return journal{}, errors.New("library scope lock claims ingress control")
|
||||
if mode == IngressModeNone {
|
||||
if j.Level != "instance" || j.IngressAck != NoIngressAck {
|
||||
return journal{}, errors.New("invalid no-gate maintenance evidence")
|
||||
}
|
||||
if j.Ingress != "" || j.IngressClosed || j.GateReopened {
|
||||
return journal{}, errors.New("no-gate maintenance claims ingress control")
|
||||
}
|
||||
if j.Gate == nil || j.Gate.Mode != IngressModeNone || j.Gate.Closed || j.Gate.Drained || j.Gate.Reason == "" {
|
||||
return journal{}, errors.New("invalid no-gate maintenance evidence")
|
||||
}
|
||||
} else {
|
||||
if j.Level == "instance" && !validIngress(j.Ingress) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
}
|
||||
if j.Level == "library" && (j.Ingress != "" || j.IngressClosed) {
|
||||
return journal{}, errors.New("library scope lock claims ingress control")
|
||||
}
|
||||
if j.Gate != nil && (!j.Gate.Closed || j.Gate.ActiveAtClose < 0 || j.Gate.Served < 0) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
}
|
||||
}
|
||||
if j.Level == "library" && j.IngressMode != "" {
|
||||
return journal{}, errors.New("library scope lock claims ingress mode")
|
||||
}
|
||||
if j.RestoreBlocked && !j.ContentRisk {
|
||||
return journal{}, errors.New("invalid freeze restore block")
|
||||
}
|
||||
if j.Gate != nil && (!j.Gate.Closed || j.Gate.ActiveAtClose < 0 || j.Gate.Served < 0) {
|
||||
if mode == IngressModeGate && j.Gate != nil && (!j.Gate.Closed || j.Gate.ActiveAtClose < 0 || j.Gate.Served < 0) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
}
|
||||
if j.GateReopened && j.Gate == nil {
|
||||
return journal{}, errors.New("ingress reopened without close evidence")
|
||||
}
|
||||
// no-gate 窗口的恢复证据只能是外部健康探针:写了"已验证"就必须有可核验的地址。
|
||||
if j.HealthVerified && !validHealthURL(j.HealthURL) {
|
||||
return journal{}, errors.New("invalid health probe evidence")
|
||||
}
|
||||
if mode == IngressModeNone && j.HealthURL == "" && !terminal(j.State) {
|
||||
return journal{}, errors.New("no-gate maintenance requires a health probe")
|
||||
}
|
||||
if j.BackupSnapshotID != "" && !validDigest(j.BackupSnapshotID) {
|
||||
return journal{}, errors.New("invalid backup snapshot id")
|
||||
}
|
||||
@@ -632,11 +792,13 @@ func (c *Coordinator) restore(ctx context.Context, j *journal) error {
|
||||
// 注意:入口关到一半失败(闸门已被停、排空超时)时 j.Gate 可能还是 nil,
|
||||
// 但入口确实已经不在了。所以实例级维护一律重新打开入口,而不是只看
|
||||
// "有没有成功关闭的证据"——否则会出现"维护已释放、服务在跑、入口却是关的"。
|
||||
// 但只要**任何一个写入者没回到健康**,这里的 else 分支就不执行:入口保持关闭,
|
||||
// 但**任何写入者没回到健康**,这里的 else 分支就不执行:入口保持关闭,
|
||||
// 由调用方按 held 处理并走恢复(宁可不可用,也不对外暴露没准备好的目标)。
|
||||
// no-gate(测试模式)根本没有闸门:一笔闸门动作都不做,也不写"入口已重开"。
|
||||
switch {
|
||||
case serviceFailures != nil:
|
||||
serviceFailures = errors.Join(serviceFailures, errors.New("目标入口保持关闭:写入者未全部恢复健康"))
|
||||
case modeOf(j.IngressMode) == IngressModeNone:
|
||||
case c.level == "instance" || j.Gate != nil:
|
||||
if c.gate == nil {
|
||||
persistFailures = errors.Join(persistFailures, errors.New("入口闸门缺失:不能确认目标已恢复"))
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
package libraryfreeze
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// JournalEvidence 是维护窗口 journal 的**只读**事实视图。
|
||||
//
|
||||
// 恢复派发的激活门必须消费真实维护窗口写下的证据,而不是调用方自填的
|
||||
// "stopped=true"。这里复用 Coordinator.load 的同一套结构校验(parseJournal),
|
||||
// 只额外暴露事实字段与文件摘要;它不执行任何恢复动作,也不改 journal。
|
||||
//
|
||||
// 边界:本函数证明"这份 journal 字节是维护窗口写下的、结构与身份自洽",
|
||||
// 不独立证明外部 writer 此刻不在运行;后者由 journal 记录的入口排空/退出码
|
||||
// 与治理库维护行共同约束(见 docs/architecture/ds01-activation-evidence.md)。
|
||||
type JournalEvidence struct {
|
||||
Job string
|
||||
Source string
|
||||
Level string
|
||||
State string
|
||||
TargetFingerprint string
|
||||
Ingress string
|
||||
IngressMode string
|
||||
IngressAck string
|
||||
IngressClosed bool
|
||||
DrainVerified bool
|
||||
ContentRisk bool
|
||||
RestoreBlocked bool
|
||||
HealthURL string
|
||||
HealthVerified bool
|
||||
Gate *GateEvidence
|
||||
Quiesce *QuiesceProof
|
||||
Services []JournalService
|
||||
StartedAt time.Time
|
||||
Digest string
|
||||
}
|
||||
|
||||
// JournalService 是 journal 里登记的受控 writer 与它的退出观察。
|
||||
// Running 是"开窗口时的原始状态",不能拿它判断现在是否在跑;真正的停写证据是
|
||||
// StopRequested + ExitCode(以及 Accidental 的入口 drain),与 load 的校验口径一致。
|
||||
type JournalService struct {
|
||||
ID string
|
||||
Running bool
|
||||
Healthy bool
|
||||
ExitCode *int
|
||||
StopRequested bool
|
||||
StopVerified bool
|
||||
Restored bool
|
||||
}
|
||||
|
||||
// ReadJournal 从维护 journal 根目录读取 <job>.json,返回校验后的事实与 sha256。
|
||||
//
|
||||
// 目录必须真实且私有(0700、非符号链接),文件以 O_NOFOLLOW 打开、限长 1MiB;
|
||||
// 这些是维护链自己写 journal 时的同一组约束,放宽其中任何一条都会让"证据来源"
|
||||
// 变成随便一个可写目录。
|
||||
func ReadJournal(dir, job string) (JournalEvidence, error) {
|
||||
var evidence JournalEvidence
|
||||
if !validID(job) {
|
||||
return evidence, errors.New("invalid maintenance journal job id")
|
||||
}
|
||||
if !filepath.IsAbs(dir) {
|
||||
return evidence, errors.New("maintenance journal root must be absolute")
|
||||
}
|
||||
info, err := os.Lstat(dir)
|
||||
if err != nil || !info.IsDir() || info.Mode().Perm() != 0700 {
|
||||
return evidence, errors.New("maintenance journal root must be a private real directory")
|
||||
}
|
||||
resolved, err := filepath.EvalSymlinks(dir)
|
||||
if err != nil || resolved != filepath.Clean(dir) {
|
||||
return evidence, errors.New("maintenance journal root contains symlink")
|
||||
}
|
||||
root, err := os.OpenRoot(dir)
|
||||
if err != nil {
|
||||
return evidence, err
|
||||
}
|
||||
defer root.Close()
|
||||
data, err := readJournalBytes(root, job)
|
||||
if err != nil {
|
||||
return evidence, err
|
||||
}
|
||||
j, err := parseJournal(data, job, "")
|
||||
if err != nil {
|
||||
return evidence, err
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
services := make([]JournalService, len(j.Services))
|
||||
for i, s := range j.Services {
|
||||
services[i] = JournalService{ID: s.ID, Running: s.Running, Healthy: s.Healthy, ExitCode: s.ExitCode,
|
||||
StopRequested: s.StopRequested, StopVerified: s.StopVerified, Restored: s.Restored}
|
||||
}
|
||||
evidence = JournalEvidence{
|
||||
Job: job, Source: j.Source, Level: j.Level, State: j.State,
|
||||
TargetFingerprint: j.TargetFingerprint, Ingress: j.Ingress,
|
||||
IngressMode: modeOf(j.IngressMode), IngressAck: j.IngressAck,
|
||||
IngressClosed: j.IngressClosed, DrainVerified: j.DrainVerified,
|
||||
ContentRisk: j.ContentRisk, RestoreBlocked: j.RestoreBlocked,
|
||||
HealthURL: j.HealthURL, HealthVerified: j.HealthVerified,
|
||||
Gate: j.Gate, Quiesce: j.Quiesce, Services: services,
|
||||
StartedAt: j.StartedAt.UTC(), Digest: hex.EncodeToString(sum[:]),
|
||||
}
|
||||
return evidence, nil
|
||||
}
|
||||
|
||||
func readJournalBytes(root *os.Root, job string) ([]byte, error) {
|
||||
if !validID(job) {
|
||||
return nil, errors.New("invalid freeze journal name")
|
||||
}
|
||||
f, err := root.OpenFile(job+".json", os.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
info, err := f.Stat()
|
||||
if err != nil || !info.Mode().IsRegular() || info.Size() > 1024*1024 {
|
||||
return nil, errors.New("invalid freeze journal file")
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(f, 1024*1024+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(data) > 1024*1024 {
|
||||
return nil, errors.New("invalid freeze journal file")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
package libraryfreeze
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// noGateFixture 是"整栈没有入口闸门"的测试栈:只有写者与队列,
|
||||
// 入口可用性由外部健康探针证明,闸门一个都不装配。
|
||||
func noGateFixture(t *testing.T, probes *int) (string, *exitControl, *Coordinator) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
if err := os.Chmod(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 真实 0.9.0 栈里被代理服务收到 SIGTERM 会 exit(1):no-gate 路径不能靠退出码判排空。
|
||||
c := &exitControl{fixtureControl: &fixtureControl{services: []Service{
|
||||
{ID: "app", Running: true, Healthy: true}}}, exit: map[string]int{"app": 1}}
|
||||
engine, err := Open(root, "target-instance", c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { engine.Close() })
|
||||
if err := engine.SetIngressMode(IngressModeNone, NoIngressAck); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.SetHealthURL("http://127.0.0.1:1/health"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.SetTarget("instance", "", nil, func(context.Context) error {
|
||||
(*probes)++
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.SetQuiescer(&idleQuiescer{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.SetQuiesceStability(5*time.Millisecond, 200*time.Millisecond); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return root, c, engine
|
||||
}
|
||||
|
||||
// 没有显式确认串就不许进 no-gate:确认串必须逐字符相等。
|
||||
func TestNoGateModeRequiresExactAck(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Chmod(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
engine, err := Open(root, "target-instance", &fixtureControl{services: []Service{{ID: "app", Running: true, Healthy: true}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer engine.Close()
|
||||
for _, ack := range []string{"", "test_only", "TEST-ONLY", " test-only", "test-only "} {
|
||||
if err := engine.SetIngressMode(IngressModeNone, ack); err == nil ||
|
||||
!strings.Contains(err.Error(), "LIBRARY_NO_INGRESS_ACK") {
|
||||
t.Fatalf("ack=%q 没有被拒并点名:%v", ack, err)
|
||||
}
|
||||
}
|
||||
if err := engine.SetIngressMode("no", NoIngressAck); err == nil {
|
||||
t.Fatal("未知入口形态被接受")
|
||||
}
|
||||
// gate 是默认:不调用 SetIngressMode 时仍然是 gate。
|
||||
if err := engine.SetIngressMode(IngressModeGate, ""); err != nil {
|
||||
t.Fatalf("gate 被拒:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// no-gate 下不许装配闸门:那条路径一笔闸门动作都不能发生。
|
||||
func TestNoGateModeRefusesGateAndIngressClaim(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Chmod(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
engine, err := Open(root, "target-instance", &fixtureControl{services: []Service{{ID: "app", Running: true, Healthy: true}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer engine.Close()
|
||||
if err := engine.SetIngressMode(IngressModeNone, NoIngressAck); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls := []string{}
|
||||
if err := engine.SetGate(&fakeGate{calls: &calls, drain: true, active: 1}); err == nil {
|
||||
t.Fatal("no-gate 模式接受了闸门")
|
||||
}
|
||||
if len(calls) != 0 {
|
||||
t.Fatalf("闸门被调用:%v", calls)
|
||||
}
|
||||
if err := engine.SetTarget("instance", "http://127.0.0.1:1/health", nil, nil); err == nil {
|
||||
t.Fatal("no-gate 模式接受了入口地址")
|
||||
}
|
||||
if err := engine.SetTarget("instance", "", func(context.Context) error { return nil }, nil); err == nil {
|
||||
t.Fatal("no-gate 模式接受了入口关闭探针")
|
||||
}
|
||||
}
|
||||
|
||||
// no-gate + 显式确认:窗口能开、记录写明 ingress_mode=none 与"无闸门"理由、
|
||||
// 恢复后健康探针被调用、闸门一次都没被调用。
|
||||
func TestNoGateWindowRecordsDowngradedEvidence(t *testing.T) {
|
||||
probes := 0
|
||||
root, c, engine := noGateFixture(t, &probes)
|
||||
result, err := engine.RunWindow(context.Background(), "job-nogate", func(context.Context) error { return nil },
|
||||
func(_ context.Context, w *Window) (string, error) {
|
||||
for _, s := range c.services {
|
||||
if s.Running {
|
||||
t.Fatal("work started while a writer was still running")
|
||||
}
|
||||
}
|
||||
// 记一笔内容风险,恢复/收尾也要能在 none 模式下跑完。
|
||||
if err := w.SetBackupSnapshot(context.Background(), strings.Repeat("a", 64)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return strings.Repeat("b", 64), nil
|
||||
}, func(error) bool { return true })
|
||||
if err != nil {
|
||||
t.Fatalf("no-gate 窗口失败:%v", err)
|
||||
}
|
||||
if result.State != "succeeded" {
|
||||
t.Fatalf("result=%+v", result)
|
||||
}
|
||||
// 闸门从未被调用:调用记录里只有写者的 stop/start。
|
||||
if got := strings.Join(c.calls, ","); got != "stop:app,start:app" {
|
||||
t.Fatalf("calls=%v", c.calls)
|
||||
}
|
||||
if probes == 0 {
|
||||
t.Fatal("恢复后没有调用健康探针")
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(root, "job-nogate.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(raw), `"ingress_mode":"none"`) {
|
||||
t.Fatalf("记录没有 ingress_mode=none:%s", raw)
|
||||
}
|
||||
j, err := engine.load("job-nogate")
|
||||
if err != nil {
|
||||
t.Fatalf("维护记录读不回来:%v(raw=%s)", err, raw)
|
||||
}
|
||||
if j.IngressMode != IngressModeNone || j.IngressAck != NoIngressAck {
|
||||
t.Fatalf("入口形态没写进记录:%+v", j)
|
||||
}
|
||||
if j.Gate == nil || j.Gate.Mode != IngressModeNone || j.Gate.Closed || j.Gate.Drained ||
|
||||
j.Gate.Reason != NoIngressGateReason {
|
||||
t.Fatalf("gate 证据位置不是 no-gate 占位:%+v", j.Gate)
|
||||
}
|
||||
if !j.HealthVerified || j.HealthURL == "" {
|
||||
t.Fatalf("健康探针证据缺失:%+v", j)
|
||||
}
|
||||
if j.GateReopened || j.IngressClosed {
|
||||
t.Fatalf("no-gate 窗口冒充了入口开关:reopened=%v closed=%v", j.GateReopened, j.IngressClosed)
|
||||
}
|
||||
if !j.DrainVerified {
|
||||
t.Fatal("写者全停 + 队列静默没有记为排空证据")
|
||||
}
|
||||
// 原始 JSON 也必须能读出"本次没有闸门证据"。
|
||||
for _, want := range []string{
|
||||
`"ingress_mode":"none"`,
|
||||
`"ingress_ack":"test-only"`,
|
||||
`"gate":{"mode":"none","reason":"stack has no ingress gate (test-only)"}`,
|
||||
} {
|
||||
if !strings.Contains(string(raw), want) {
|
||||
t.Fatalf("记录缺少 %s:%s", want, raw)
|
||||
}
|
||||
}
|
||||
if strings.Contains(string(raw), `"gate_reopened":true`) {
|
||||
t.Fatalf("记录声称重开过闸门:%s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// gate 模式的记录必须照旧写 ingress_mode=gate,且仍然要求真实排空证据。
|
||||
func TestGateWindowRecordsGateMode(t *testing.T) {
|
||||
root, c, engine := instanceFixture(t)
|
||||
gate := &fakeGate{calls: &c.calls, drain: true, active: 1}
|
||||
if err := engine.SetGate(gate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := engine.RunWindow(context.Background(), "job-gate-mode", func(context.Context) error { return nil },
|
||||
func(context.Context, *Window) (string, error) { return "", nil }, func(error) bool { return true }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(root, "job-gate-mode.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(raw), `"ingress_mode":"gate"`) {
|
||||
t.Fatalf("gate 记录没有写入口形态:%s", raw)
|
||||
}
|
||||
if strings.Contains(string(raw), `"mode":"none"`) {
|
||||
t.Fatalf("gate 记录写成了 no-gate:%s", raw)
|
||||
}
|
||||
// gate 模式的 JSON 仍然是老形状:closed/drained 都在。
|
||||
var decoded struct {
|
||||
Gate map[string]any `json:"gate"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded.Gate["closed"] != true || decoded.Gate["drained"] != true {
|
||||
t.Fatalf("gate 证据被改写:%+v", decoded.Gate)
|
||||
}
|
||||
}
|
||||
|
||||
// no-gate 下健康探针不通过 = 服务没回来:窗口不得报成功,也不许冒充"入口已开"。
|
||||
func TestNoGateWindowFailsWhenHealthProbeNeverRecovers(t *testing.T) {
|
||||
probes := 0
|
||||
_, _, engine := noGateFixture(t, &probes)
|
||||
// 把探针换成永远失败的:模拟写完内容后服务起不来。
|
||||
engine.ingressReady = func(context.Context) error { return errors.New("health 503") }
|
||||
_, err := engine.RunWindow(context.Background(), "job-nogate-sick", func(context.Context) error { return nil },
|
||||
func(context.Context, *Window) (string, error) { return "", nil }, func(error) bool { return true })
|
||||
if err == nil {
|
||||
t.Fatal("健康探针失败却报成功")
|
||||
}
|
||||
j, loadErr := engine.load("job-nogate-sick")
|
||||
if loadErr != nil {
|
||||
t.Fatal(loadErr)
|
||||
}
|
||||
if j.State != "held" || j.Code != "ingress_unverified" {
|
||||
t.Fatalf("状态不是 held/ingress_unverified:state=%s code=%s", j.State, j.Code)
|
||||
}
|
||||
if j.GateReopened {
|
||||
t.Fatal("no-gate 窗口伪造了闸门重开")
|
||||
}
|
||||
}
|
||||
@@ -177,3 +177,44 @@ func (f restorerFunc) Restore(ctx context.Context, id string) error { return f(c
|
||||
type verifierFunc func(context.Context, string) (string, error)
|
||||
|
||||
func (f verifierFunc) Verify(ctx context.Context, id string) (string, error) { return f(ctx, id) }
|
||||
|
||||
// D04:备份介质失效(归档 part 缺失/摘要不符)时必须在内容层停住:保持 held +
|
||||
// RestoreBlocked,服务与入口都不恢复,也不许跑校验或写终态。错误要能被人工按
|
||||
// 归档处置(明确指向预备份/介质,而不是"未知失败")。
|
||||
func TestR13RecoveryStaysHeldWhenBackupMediumFails(t *testing.T) {
|
||||
_, c, e := instanceFixture(t)
|
||||
stopAllServices(c)
|
||||
if err := e.SetTargetFingerprint(strings.Repeat("a", 64)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := e.SetGate(&fakeGate{calls: &c.calls, drain: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := e.save(heldJournal("job-media", strings.Repeat("a", 64), strings.Repeat("b", 64))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
verified := false
|
||||
_, err := e.RecoverWindow(context.Background(), "job-media",
|
||||
restorerFunc(func(context.Context, string) error {
|
||||
return errors.New("archive part chunks missing or digest mismatch: backup medium unusable")
|
||||
}),
|
||||
verifierFunc(func(context.Context, string) (string, error) { verified = true; return "d", nil }))
|
||||
if err == nil {
|
||||
t.Fatal("介质失效时恢复被当成成功")
|
||||
}
|
||||
if verified {
|
||||
t.Fatal("归档读不出来却继续跑了内容校验")
|
||||
}
|
||||
for _, s := range c.services {
|
||||
if s.Running {
|
||||
t.Fatalf("介质失效后服务 %s 被恢复", s.ID)
|
||||
}
|
||||
}
|
||||
after, loadErr := e.load("job-media")
|
||||
if loadErr != nil {
|
||||
t.Fatal(loadErr)
|
||||
}
|
||||
if after.State != "held" || after.Code != "restore_failed" || !after.RestoreBlocked || after.RecoveredAt != nil {
|
||||
t.Fatalf("journal=%+v:介质失效必须保持 held/RestoreBlocked", after)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,13 +37,19 @@ func (c *Coordinator) RecoverWindow(ctx context.Context, job string, restorer Re
|
||||
}
|
||||
defer unlock(lock)
|
||||
|
||||
if c.level == "instance" && c.gate == nil {
|
||||
if c.level == "instance" && c.gate == nil && c.ingressModeOrDefault() != IngressModeNone {
|
||||
return Result{}, errors.New("实例级恢复缺少入口闸门:不能证明入口已关闭")
|
||||
}
|
||||
j, err := c.load(job)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
// 恢复只能用与窗口一致的入口形态:拿 no-gate 去收尾一扇真门,或反向,
|
||||
// 都会假装"这次开过/没开过闸门"。
|
||||
if modeOf(j.IngressMode) != c.ingressModeOrDefault() {
|
||||
return Result{}, fmt.Errorf("维护记录的入口形态(%s)与当前配置(%s)不一致:拒绝恢复",
|
||||
modeOf(j.IngressMode), c.ingressModeOrDefault())
|
||||
}
|
||||
if terminal(j.State) {
|
||||
// 终态重放(F05 路径二):journal 已经是 recovered,说明数据恢复与服务/入口
|
||||
// 恢复都已完成。这时再拒绝会让"journal recovered → PG 维护释放"之间的崩溃
|
||||
@@ -150,6 +156,7 @@ func (c *Coordinator) RecoverWindow(ctx context.Context, job string, restorer Re
|
||||
return c.failRecovery(&j, "ingress_unverified", fmt.Errorf("入口未恢复:%w", readyErr))
|
||||
}
|
||||
}
|
||||
c.markHealth(&j)
|
||||
j.State = "recovered"
|
||||
j.Code = ""
|
||||
j.FinishedAt = &ended
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package libraryfreeze
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const r30digest = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
|
||||
// 第一次进窗在"未写任何内容"的情况下失败(例如预检/预备份前就断了),窗口已按失败收尾。
|
||||
// 同一个作业随后 resume:必须能开**新一次尝试**,而不是被 "freeze request already exists" 挡住。
|
||||
func TestR30ResumeAfterClosedWindowWithoutContentRisk(t *testing.T) {
|
||||
root, c, e := instanceFixture(t)
|
||||
if _, err := e.RunWindow(context.Background(), "job-r30-b", func(context.Context) error { return nil },
|
||||
func(ctx context.Context, w *Window) (string, error) {
|
||||
return "", errors.New("preflight failed before any target write")
|
||||
}, func(error) bool { return true }); err == nil {
|
||||
t.Fatal("first window should fail")
|
||||
}
|
||||
first, err := e.load("job-r30-b")
|
||||
if err != nil {
|
||||
t.Fatalf("first journal: %v", err)
|
||||
}
|
||||
if first.State != "failed" || first.ContentRisk {
|
||||
t.Fatalf("fixture shape wrong: state=%s contentRisk=%v", first.State, first.ContentRisk)
|
||||
}
|
||||
|
||||
callsBefore := len(c.calls)
|
||||
result, err := e.RunWindow(context.Background(), "job-r30-b", func(context.Context) error { return nil },
|
||||
func(ctx context.Context, w *Window) (string, error) {
|
||||
if err := w.MarkContentRisk(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return r30digest, nil
|
||||
}, func(error) bool { return true })
|
||||
if err != nil {
|
||||
t.Fatalf("resume after closed window must be allowed, got: %v", err)
|
||||
}
|
||||
if result.State != "succeeded" || result.SnapshotID != r30digest {
|
||||
t.Fatalf("result=%+v", result)
|
||||
}
|
||||
if len(c.calls) == callsBefore {
|
||||
t.Fatal("second attempt did not actually run the window")
|
||||
}
|
||||
journal, err := e.load("job-r30-b")
|
||||
if err != nil {
|
||||
t.Fatalf("journal after resume: %v", err)
|
||||
}
|
||||
if journal.Job != "job-r30-b" || journal.Attempt != 2 {
|
||||
t.Fatalf("new attempt must bind same job with attempt=2: job=%q attempt=%d", journal.Job, journal.Attempt)
|
||||
}
|
||||
if journal.SupersededFrom != "archive/job-r30-b-attempt1.json" {
|
||||
t.Fatalf("previous closed request must be referenced, not silently dropped: %q", journal.SupersededFrom)
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(root, journal.SupersededFrom))
|
||||
if err != nil {
|
||||
t.Fatalf("archived previous journal missing: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(raw), `"state":"failed"`) {
|
||||
t.Fatalf("archived journal must keep the closed state: %s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// 已经在窗口内写过目标内容(ContentRisk)却失败的作业,不许开新窗口 —— 必须走恢复。
|
||||
func TestR30RefusesNewAttemptWhenContentRiskRecorded(t *testing.T) {
|
||||
_, _, e := instanceFixture(t)
|
||||
if _, err := e.RunWindow(context.Background(), "job-r30-c", func(context.Context) error { return nil },
|
||||
func(ctx context.Context, w *Window) (string, error) {
|
||||
if err := w.MarkContentRisk(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", errors.New("write failed mid way")
|
||||
}, func(error) bool { return true }); err == nil {
|
||||
t.Fatal("first window should fail")
|
||||
}
|
||||
_, err := e.RunWindow(context.Background(), "job-r30-c", func(context.Context) error { return nil },
|
||||
func(context.Context, *Window) (string, error) { return r30digest, nil }, func(error) bool { return true })
|
||||
if err == nil || !strings.Contains(err.Error(), "already exists") {
|
||||
t.Fatalf("content-risk failure must still require recovery, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 已成功收尾的窗口:再跑一次只回放结果,不重复副作用。
|
||||
func TestR30SucceededWindowIsNotReplayed(t *testing.T) {
|
||||
_, c, e := instanceFixture(t)
|
||||
if _, err := e.RunWindow(context.Background(), "job-r30-d", func(context.Context) error { return nil },
|
||||
func(ctx context.Context, w *Window) (string, error) {
|
||||
if err := w.MarkContentRisk(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return r30digest, nil
|
||||
}, func(error) bool { return true }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
callsBefore := len(c.calls)
|
||||
replay, err := e.RunWindow(context.Background(), "job-r30-d", func(context.Context) error { return nil },
|
||||
func(context.Context, *Window) (string, error) { return strings.Repeat("c", 64), nil },
|
||||
func(error) bool { return true })
|
||||
if err != nil {
|
||||
t.Fatalf("replay: %v", err)
|
||||
}
|
||||
if replay.State != "succeeded" || replay.SnapshotID != r30digest {
|
||||
t.Fatalf("replay must return original result, got %+v", replay)
|
||||
}
|
||||
if len(c.calls) != callsBefore {
|
||||
t.Fatalf("replay must not touch services again: %v", c.calls)
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,12 @@ package libraryfreeze
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -101,16 +103,55 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
}
|
||||
defer unlock(lock)
|
||||
// 实例级维护的入口必须由我们关:没有闸门就没有"入口已关"的证据,
|
||||
// 后面拿什么证明后台静默都是空的。
|
||||
if c.level == "instance" && c.gate == nil {
|
||||
// 后面拿什么证明后台静默都是空的。唯一例外是显式确认过的 no-gate 测试栈。
|
||||
if c.level == "instance" && c.gate == nil && c.ingressModeOrDefault() != IngressModeNone {
|
||||
return Result{}, errors.New("实例级维护缺少入口闸门:不能证明入口已关闭")
|
||||
}
|
||||
|
||||
previous, loadErr := c.load(job)
|
||||
j := journal{}
|
||||
attempt, superseded := 0, ""
|
||||
switch {
|
||||
case loadErr == nil && previous.State == "succeeded":
|
||||
return previous.Result, nil
|
||||
case loadErr == nil && previous.State == "failed" && !previous.ContentRisk &&
|
||||
previous.Source == c.source && previous.TargetFingerprint == c.fingerprint &&
|
||||
(previous.Gate == nil || previous.GateReopened):
|
||||
// 上一次尝试在**没有写过任何目标内容**的情况下失败并已收尾(服务已恢复、入口已重开)。
|
||||
// 这种窗口没有需要恢复的内容,同一作业的续作可以开新一次尝试:
|
||||
// 旧请求不删除,归档后由新请求引用;job / operation / backup 身份保持不变。
|
||||
// 一旦 ContentRisk=true 就落到下面的拒绝分支,必须走恢复而不是新窗口。
|
||||
archived, archiveErr := c.archive(job, previous)
|
||||
if archiveErr != nil {
|
||||
return Result{}, archiveErr
|
||||
}
|
||||
attempt, superseded = previous.Attempt+1, archived
|
||||
if attempt < 1 {
|
||||
attempt = 1
|
||||
}
|
||||
if err = c.pending(); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
if err = prepare(ctx); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
items, err := inventory(ctx, c.control)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
j = journal{Version: 1, Source: c.source, Job: job, Level: c.level, Ingress: c.ingress,
|
||||
Attempt: attempt, SupersededFrom: superseded,
|
||||
IngressMode: c.ingressModeOrDefault(), IngressAck: c.ingressAck, HealthURL: c.healthURL,
|
||||
TargetFingerprint: c.fingerprint, Result: Result{State: "stopping"}, StartedAt: time.Now().UTC()}
|
||||
for _, s := range items {
|
||||
if s.Running && !s.Healthy {
|
||||
return Result{}, errors.New("test writer is not ready")
|
||||
}
|
||||
j.Services = append(j.Services, serviceRecord{Service: s})
|
||||
}
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
case loadErr == nil && (previous.State == "stopping" || previous.State == "held"):
|
||||
// 接管:目标必须仍然停住。不重新"准备"、也不从零重来。
|
||||
j = previous
|
||||
@@ -119,7 +160,7 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
j.RestoreBlocked = false
|
||||
}
|
||||
case loadErr == nil:
|
||||
return previous.Result, errors.New("freeze request already exists; recovery or a new request is required")
|
||||
return previous.Result, fmt.Errorf("freeze request already exists (%s); recovery or a new request is required", previous.State)
|
||||
case !errors.Is(loadErr, os.ErrNotExist):
|
||||
return Result{}, loadErr
|
||||
default:
|
||||
@@ -137,6 +178,7 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
return Result{}, err
|
||||
}
|
||||
j = journal{Version: 1, Source: c.source, Job: job, Level: c.level, Ingress: c.ingress,
|
||||
Attempt: 1, IngressMode: c.ingressModeOrDefault(), IngressAck: c.ingressAck, HealthURL: c.healthURL,
|
||||
TargetFingerprint: c.fingerprint, Result: Result{State: "stopping"}, StartedAt: time.Now().UTC()}
|
||||
for _, s := range items {
|
||||
if s.Running && !s.Healthy {
|
||||
@@ -152,50 +194,98 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
// 顺序不能换:先把外部任务的生产入口关掉并排空,再证明后台静默,最后才停写入者。
|
||||
// 先证明静默再关入口是错的——排空期间还在跑的在途请求随时会派生新的异步任务,
|
||||
// 那一刻的"空"到停写者时早就失效了;而 app 一旦 SIGTERM 就 exit(1),会把任务腰斩。
|
||||
if c.gate != nil && (j.Gate == nil || !j.Gate.Drained) {
|
||||
evidence, gateErr := c.gate.CloseIngress(ctx)
|
||||
if gateErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("关闭目标入口失败:%w", gateErr), "gate_failed")
|
||||
//
|
||||
// no-gate(测试栈)没有闸门可关:顺序改成"先停写者 → 再证明队列排空 → 导出",
|
||||
// 并且一笔闸门动作都不做;gate 证据位置只留 mode=none 的说明,不冒充排空。
|
||||
mode := modeOf(j.IngressMode)
|
||||
if mode != c.ingressModeOrDefault() {
|
||||
return Result{}, fmt.Errorf("维护记录与当前配置的入口形态不一致(记录 %s,配置 %s),拒绝接管",
|
||||
mode, c.ingressModeOrDefault())
|
||||
}
|
||||
if mode == IngressModeNone {
|
||||
if j.Gate == nil || j.Gate.Mode != IngressModeNone {
|
||||
evidence := GateEvidence{Mode: IngressModeNone, Reason: NoIngressGateReason}
|
||||
j.Gate = &evidence
|
||||
}
|
||||
if !evidence.Closed || !evidence.Drained {
|
||||
return c.abandonWindow(ctx, &j, errors.New("目标入口未确认排空(仍有在途请求)"), "gate_not_drained")
|
||||
if j.IngressAck == "" {
|
||||
j.IngressAck = c.ingressAck
|
||||
}
|
||||
if j.HealthURL == "" {
|
||||
j.HealthURL = c.healthURL
|
||||
}
|
||||
j.Gate = &evidence
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// 入口关了才谈得上"稳定切点":连续为空 + 跨过稳定窗口 + 拿得到任务指纹。
|
||||
proof, quietErr := c.awaitBackgroundQuiet(ctx, &j)
|
||||
if quietErr != nil {
|
||||
return c.abandonWindow(ctx, &j, quietErr, "background_busy")
|
||||
}
|
||||
|
||||
// 停写入者:任何一步不成立都不许往下写。
|
||||
if stopErr := c.stopAll(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "stop_failed")
|
||||
}
|
||||
if stopErr := c.stopped(ctx, j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "isolation_unverified")
|
||||
}
|
||||
if stopErr := c.verifyDrain(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "drain_unverified")
|
||||
}
|
||||
// 停完再复核一次:入队/派发可能在"证明静默"和"停写者"之间发生。
|
||||
// 队列仍有任务说明有任务被截断——这比同步失败严重,按切点不一致处理,绝不往下写。
|
||||
if stopErr := c.verifyBackgroundAfterStop(ctx, &j, proof); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "background_truncated")
|
||||
}
|
||||
if c.ingressClosed != nil {
|
||||
if stopErr := c.ingressClosed(ctx); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("目标入口仍然可用:%w", stopErr), "ingress_open")
|
||||
if stopErr := c.stopAll(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "stop_failed")
|
||||
}
|
||||
if stopErr := c.stopped(ctx, j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "isolation_unverified")
|
||||
}
|
||||
if stopErr := c.verifyDrain(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "drain_unverified")
|
||||
}
|
||||
// 写者全停之后队列必须一直为空:这一刻的"空"就是这次窗口的排空证据。
|
||||
proof, quietErr := c.awaitBackgroundQuiet(ctx, &j)
|
||||
if quietErr != nil {
|
||||
return c.abandonWindow(ctx, &j, quietErr, "background_busy")
|
||||
}
|
||||
if stopErr := c.verifyBackgroundAfterStop(ctx, &j, proof); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "background_truncated")
|
||||
}
|
||||
// 没有闸门排空证据,也没有退出码可作凭据:排空只由"写者全停 + 队列静默"
|
||||
// 共同证明——证据等级降一档,且已写进 ingress_mode=none。
|
||||
j.DrainVerified = true
|
||||
j.State = "held"
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
} else {
|
||||
if c.gate != nil && (j.Gate == nil || !j.Gate.Drained) {
|
||||
evidence, gateErr := c.gate.CloseIngress(ctx)
|
||||
if gateErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("关闭目标入口失败:%w", gateErr), "gate_failed")
|
||||
}
|
||||
if !evidence.Closed || !evidence.Drained {
|
||||
return c.abandonWindow(ctx, &j, errors.New("目标入口未确认排空(仍有在途请求)"), "gate_not_drained")
|
||||
}
|
||||
j.Gate = &evidence
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// 入口关了才谈得上"稳定切点":连续为空 + 跨过稳定窗口 + 拿得到任务指纹。
|
||||
proof, quietErr := c.awaitBackgroundQuiet(ctx, &j)
|
||||
if quietErr != nil {
|
||||
return c.abandonWindow(ctx, &j, quietErr, "background_busy")
|
||||
}
|
||||
|
||||
// 停写入者:任何一步不成立都不许往下写。
|
||||
if stopErr := c.stopAll(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "stop_failed")
|
||||
}
|
||||
if stopErr := c.stopped(ctx, j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "isolation_unverified")
|
||||
}
|
||||
if stopErr := c.verifyDrain(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "drain_unverified")
|
||||
}
|
||||
// 停完再复核一次:入队/派发可能在"证明静默"和"停写者"之间发生。
|
||||
// 队列仍有任务说明有任务被截断——这比同步失败严重,按切点不一致处理,绝不往下写。
|
||||
if stopErr := c.verifyBackgroundAfterStop(ctx, &j, proof); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "background_truncated")
|
||||
}
|
||||
if c.ingressClosed != nil {
|
||||
if stopErr := c.ingressClosed(ctx); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("目标入口仍然可用:%w", stopErr), "ingress_open")
|
||||
}
|
||||
j.IngressClosed = true
|
||||
}
|
||||
j.State = "held"
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
j.IngressClosed = true
|
||||
}
|
||||
j.State = "held"
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
w := &Window{c: c, j: &j}
|
||||
@@ -261,6 +351,7 @@ func (c *Coordinator) abandonWindow(ctx context.Context, j *journal, cause error
|
||||
return j.Result, errors.Join(cause, fmt.Errorf("目标入口没有恢复:%w", readyErr))
|
||||
}
|
||||
}
|
||||
c.markHealth(j)
|
||||
ended := time.Now().UTC()
|
||||
j.State = "failed"
|
||||
j.Code = code
|
||||
@@ -271,6 +362,17 @@ func (c *Coordinator) abandonWindow(ctx context.Context, j *journal, cause error
|
||||
return j.Result, cause
|
||||
}
|
||||
|
||||
// markHealth 记录 no-gate 窗口的恢复证据:只有健康探针真的通过才写 verified。
|
||||
func (c *Coordinator) markHealth(j *journal) {
|
||||
if c.ingressModeOrDefault() != IngressModeNone {
|
||||
return
|
||||
}
|
||||
j.HealthVerified = true
|
||||
if j.HealthURL == "" {
|
||||
j.HealthURL = c.healthURL
|
||||
}
|
||||
}
|
||||
|
||||
// closeWindow:写完之后的收尾。allowRestore=false(半写且回滚失败)时
|
||||
// 服务保持停止、journal 保持非终态,返回的是"目标仍在维护"这个结论。
|
||||
func (c *Coordinator) closeWindow(ctx context.Context, j *journal, cause error, allowRestore bool, snapshotID string) (Result, error) {
|
||||
@@ -308,6 +410,7 @@ func (c *Coordinator) closeWindow(ctx context.Context, j *journal, cause error,
|
||||
return j.Result, errors.Join(cause, fmt.Errorf("目标入口没有恢复:%w", readyErr))
|
||||
}
|
||||
}
|
||||
c.markHealth(j)
|
||||
ended := time.Now().UTC()
|
||||
j.FinishedAt = &ended
|
||||
if cause != nil {
|
||||
@@ -359,7 +462,8 @@ func (c *Coordinator) awaitBackgroundQuiet(ctx context.Context, j *journal) (*Qu
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
if j.Gate == nil || !j.Gate.Drained {
|
||||
// no-gate 窗口没有"入口已关"这个前提:静默证明只对队列负责(写者已全停)。
|
||||
if c.ingressModeOrDefault() != IngressModeNone && (j.Gate == nil || !j.Gate.Drained) {
|
||||
return nil, errors.New("必须先关闭并排空入口,再证明后台静默")
|
||||
}
|
||||
|
||||
@@ -515,13 +619,15 @@ func (c *Coordinator) verifyDrain(ctx context.Context, j *journal) error {
|
||||
return errors.New("test writer still running")
|
||||
}
|
||||
if now.ExitCode == nil {
|
||||
if c.level == "instance" && !gateDrained {
|
||||
// no-gate 窗口:没有闸门排空证据,也不把退出码当唯一凭据
|
||||
//(被代理服务收到 SIGTERM 会 exit(1));排空由"写者全停 + 队列静默"证明。
|
||||
if c.level == "instance" && !gateDrained && c.ingressModeOrDefault() != IngressModeNone {
|
||||
return fmt.Errorf("无法证明 %s 的在途请求已排空(控制器没有退出码)", s.ID)
|
||||
}
|
||||
continue
|
||||
}
|
||||
s.ExitCode = now.ExitCode
|
||||
if *now.ExitCode != 0 && !gateDrained {
|
||||
if *now.ExitCode != 0 && !gateDrained && c.ingressModeOrDefault() != IngressModeNone {
|
||||
return fmt.Errorf("写入者 %s 未优雅退出(exit=%d):在途请求没有完成排空", s.ID, *now.ExitCode)
|
||||
}
|
||||
verified++
|
||||
@@ -575,3 +681,62 @@ func (c *Coordinator) checkBackgroundStuck(ctx context.Context) error {
|
||||
}
|
||||
return fmt.Errorf("写入者已停但后台仍有任务(%s):这些任务在上次中断时被截断,需要先把服务起来(入口保持关闭)让它们跑完,再重试恢复", snapshot.Detail)
|
||||
}
|
||||
|
||||
// archive 把已经收尾的旧请求**复制**成不可覆盖的归档副本,返回归档文件名(相对 root)。
|
||||
//
|
||||
// 这里绝不能 rename 走 current:归档之后还有 pending/prepare/inventory/save 若干步,
|
||||
// 任何一步失败都必须让 current 仍然可读,否则"下一次尝试"会从空目录开始(r31 父审指出的路径)。
|
||||
// 归档名按被归档请求自己的 attempt 派生(v1 旧记录为 0),因此不会与后续代次撞名;
|
||||
// 目标已存在时必须字节相同(同一份证据重放),不同则拒绝,绝不覆盖。
|
||||
func (c *Coordinator) archive(job string, previous journal) (string, error) {
|
||||
attempt := previous.Attempt
|
||||
const dir = "archive"
|
||||
if err := c.root.Mkdir(dir, 0o700); err != nil && !errors.Is(err, os.ErrExist) {
|
||||
return "", fmt.Errorf("archive closed freeze request: %w", err)
|
||||
}
|
||||
name := fmt.Sprintf("%s/%s-attempt%d.json", dir, job, attempt)
|
||||
data, err := json.Marshal(previous)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
f, err := c.root.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
existing, readErr := c.root.OpenFile(name, os.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
||||
if readErr != nil {
|
||||
return "", fmt.Errorf("archive collision unreadable: %w", readErr)
|
||||
}
|
||||
defer existing.Close()
|
||||
buf := make([]byte, len(data)+1)
|
||||
n, readAll := existing.Read(buf)
|
||||
if readAll != nil && n == 0 {
|
||||
return "", fmt.Errorf("archive collision unreadable: %w", readAll)
|
||||
}
|
||||
if string(buf[:n]) != string(data) {
|
||||
return "", fmt.Errorf("archive collision: %s already exists with different content", name)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("archive closed freeze request: %w", err)
|
||||
}
|
||||
if _, err = f.Write(data); err != nil {
|
||||
_ = c.root.Remove(name)
|
||||
return "", err
|
||||
}
|
||||
if err = f.Sync(); err != nil {
|
||||
_ = c.root.Remove(name)
|
||||
return "", err
|
||||
}
|
||||
if err = f.Close(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
d, err := c.root.Open(dir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer d.Close()
|
||||
if err = d.Sync(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
)
|
||||
|
||||
// 目标入口闸门与后台队列的声明名称。
|
||||
@@ -15,10 +17,15 @@ import (
|
||||
// 错的证据。
|
||||
const (
|
||||
EnvInstance = "LIBRARY_TARGET_INSTANCE"
|
||||
EnvIngressMode = "LIBRARY_TARGET_INGRESS_MODE"
|
||||
EnvNoIngressAck = "LIBRARY_NO_INGRESS_ACK"
|
||||
EnvIngressURL = "LIBRARY_TARGET_INGRESS_URL"
|
||||
EnvIngressService = "LIBRARY_TARGET_INGRESS_SERVICE"
|
||||
EnvIngressDrainFile = "LIBRARY_TARGET_INGRESS_DRAIN_FILE"
|
||||
EnvRedisAddr = "LIBRARY_TARGET_REDIS_ADDR"
|
||||
// EnvHealthURL 是 no-gate 窗口唯一的"目标回来了"证据:闸门不存在时,
|
||||
// 只有外部健康探针能证明写入者恢复后真的在服务。
|
||||
EnvHealthURL = "LIBRARY_TARGET_HEALTH_URL"
|
||||
EnvRedisAddr = "LIBRARY_TARGET_REDIS_ADDR"
|
||||
// 名字里不写 Password:这里的字面量是**环境变量名**,密钥扫描会把
|
||||
// `XXX_PASSWORD = "<长字面量>"` 当成"被赋值的密钥",那是误报但没必要留个坑。
|
||||
EnvRedisAuth = "LIBRARY_TARGET_REDIS_PASSWORD"
|
||||
@@ -27,14 +34,20 @@ const (
|
||||
|
||||
// TargetEnv 是一次实例级维护所需的"入口 + 队列"声明。
|
||||
//
|
||||
// 缺任何一项都不许降级成"库级维护":没有闸门就没有"入口已关、在途已排空"
|
||||
// 的证据,没有队列就证明不了停写者时后台是空的。这两条正是被代理的 WeKnora
|
||||
// 缺任何一项都不许降级成"库级维护":没有闸门就没有"入口已关、在途已排空"的
|
||||
// 证据,没有队列就证明不了停写者时后台是空的。这两条正是被代理的 WeKnora
|
||||
// 收到 SIGTERM 就 exit(1) 之后唯一还能证明"请求没被截断"的东西。
|
||||
//
|
||||
// Mode=none 只给"整栈没有入口闸门"的测试环境用,且必须带显式确认串:
|
||||
// 那条路径不允许调用任何闸门动作,证据等级降一档并写进维护记录。
|
||||
type TargetEnv struct {
|
||||
Instance string
|
||||
Mode string
|
||||
Ack string
|
||||
IngressURL string
|
||||
IngressService string
|
||||
IngressDrainFile string
|
||||
HealthURL string
|
||||
RedisAddr string
|
||||
RedisPassword string
|
||||
RedisDB int
|
||||
@@ -49,24 +62,59 @@ func TargetFromEnv(get func(string) string) (TargetEnv, error) {
|
||||
value := func(name string) string { return strings.TrimSpace(get(name)) }
|
||||
target := TargetEnv{
|
||||
Instance: value(EnvInstance),
|
||||
Mode: value(EnvIngressMode),
|
||||
IngressURL: value(EnvIngressURL),
|
||||
IngressService: value(EnvIngressService),
|
||||
IngressDrainFile: value(EnvIngressDrainFile),
|
||||
HealthURL: value(EnvHealthURL),
|
||||
RedisAddr: value(EnvRedisAddr),
|
||||
// 口令不 Trim:前后空格也是口令的一部分,Trim 会把能连上的配置变成连不上。
|
||||
RedisPassword: get(EnvRedisAuth),
|
||||
}
|
||||
missing := make([]string, 0, 5)
|
||||
for _, item := range []struct {
|
||||
if target.Mode == "" {
|
||||
target.Mode = libraryfreeze.IngressModeGate
|
||||
}
|
||||
switch target.Mode {
|
||||
case libraryfreeze.IngressModeGate:
|
||||
target.Ack = ""
|
||||
case libraryfreeze.IngressModeNone:
|
||||
// 确认串逐字符相等:不 Trim、不忽略大小写。没写对就是没确认。
|
||||
ack := get(EnvNoIngressAck)
|
||||
if ack != libraryfreeze.NoIngressAck {
|
||||
return TargetEnv{}, fmt.Errorf(
|
||||
"no-gate 维护必须显式确认:%s 必须逐字符等于 %s(当前%s),拒绝开窗口",
|
||||
EnvNoIngressAck, libraryfreeze.NoIngressAck, describeAck(ack))
|
||||
}
|
||||
target.Ack = ack
|
||||
// 半套闸门声明最危险:既关不掉门,又容易被当成"有门"。要么有,要么明确没有。
|
||||
if target.IngressURL != "" || target.IngressService != "" || target.IngressDrainFile != "" {
|
||||
return TargetEnv{}, fmt.Errorf(
|
||||
"no-gate 维护不得同时声明 %s/%s/%s:要么有闸门,要么明确没有",
|
||||
EnvIngressURL, EnvIngressService, EnvIngressDrainFile)
|
||||
}
|
||||
default:
|
||||
return TargetEnv{}, fmt.Errorf("%s 无效(%q):只能是 %s 或 %s",
|
||||
EnvIngressMode, target.Mode, libraryfreeze.IngressModeGate, libraryfreeze.IngressModeNone)
|
||||
}
|
||||
|
||||
required := []struct {
|
||||
name string
|
||||
current string
|
||||
}{
|
||||
{EnvInstance, target.Instance},
|
||||
{EnvIngressURL, target.IngressURL},
|
||||
{EnvIngressService, target.IngressService},
|
||||
{EnvIngressDrainFile, target.IngressDrainFile},
|
||||
{EnvRedisAddr, target.RedisAddr},
|
||||
} {
|
||||
}
|
||||
if target.Mode == libraryfreeze.IngressModeNone {
|
||||
// 没有闸门可声明:改由健康探针证明"目标真的回来了",它因此是必需的。
|
||||
required = append(required, struct{ name, current string }{EnvHealthURL, target.HealthURL})
|
||||
} else {
|
||||
required = append(required,
|
||||
struct{ name, current string }{EnvIngressURL, target.IngressURL},
|
||||
struct{ name, current string }{EnvIngressService, target.IngressService},
|
||||
struct{ name, current string }{EnvIngressDrainFile, target.IngressDrainFile})
|
||||
}
|
||||
missing := make([]string, 0, 5)
|
||||
for _, item := range required {
|
||||
if item.current == "" {
|
||||
missing = append(missing, item.name)
|
||||
}
|
||||
@@ -86,8 +134,25 @@ func TargetFromEnv(get func(string) string) (TargetEnv, error) {
|
||||
target.RedisDB = db
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return TargetEnv{}, fmt.Errorf("实例级维护未接线:缺少 %s(没有入口闸门与队列就无法证明入口已排空、后台已静默)",
|
||||
strings.Join(missing, "、"))
|
||||
hint := "没有入口闸门与队列就无法证明入口已排空、后台已静默"
|
||||
if target.Mode == libraryfreeze.IngressModeNone {
|
||||
hint = "no-gate 测试模式仍需要实例、队列与健康探针"
|
||||
}
|
||||
return TargetEnv{}, fmt.Errorf("实例级维护未接线:缺少 %s(%s)",
|
||||
strings.Join(missing, "、"), hint)
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
|
||||
// describeAck 只描述"确认串有没有写对",不回显内容:写错时也可能是一段
|
||||
// 看起来像凭据的东西,错误信息里没必要复制它。
|
||||
func describeAck(ack string) string {
|
||||
switch {
|
||||
case ack == "":
|
||||
return "为空"
|
||||
case strings.TrimSpace(ack) == libraryfreeze.NoIngressAck:
|
||||
return "含多余空白"
|
||||
default:
|
||||
return "取值不匹配"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package librarymaintenance
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
)
|
||||
|
||||
// noGateEnv 是 0.9.0 测试栈的声明:没有闸门,只有实例、队列与健康探针。
|
||||
func noGateEnv() map[string]string {
|
||||
return map[string]string{
|
||||
EnvInstance: "kgmaint-v090",
|
||||
EnvIngressMode: libraryfreeze.IngressModeNone,
|
||||
EnvNoIngressAck: libraryfreeze.NoIngressAck,
|
||||
EnvHealthURL: "http://127.0.0.1:18081/health",
|
||||
EnvRedisAddr: "127.0.0.1:26379",
|
||||
EnvRedisAuth: "secret",
|
||||
EnvRedisDB: "0",
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetFromEnvNoGateNeedsExplicitAck(t *testing.T) {
|
||||
values := noGateEnv()
|
||||
delete(values, EnvNoIngressAck)
|
||||
_, err := TargetFromEnv(lookup(values))
|
||||
if err == nil || !strings.Contains(err.Error(), EnvNoIngressAck) {
|
||||
t.Fatalf("缺确认串没有被拒并点名:%v", err)
|
||||
}
|
||||
for _, ack := range []string{"", "test_only", "TEST-ONLY", " test-only", "test-only "} {
|
||||
values := noGateEnv()
|
||||
values[EnvNoIngressAck] = ack
|
||||
if _, err := TargetFromEnv(lookup(values)); err == nil || !strings.Contains(err.Error(), EnvNoIngressAck) {
|
||||
t.Fatalf("ack=%q 没有被拒:%v", ack, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetFromEnvNoGateAcceptsMissingIngressItems(t *testing.T) {
|
||||
target, err := TargetFromEnv(lookup(noGateEnv()))
|
||||
if err != nil {
|
||||
t.Fatalf("no-gate 声明被拒:%v", err)
|
||||
}
|
||||
if target.Mode != libraryfreeze.IngressModeNone || target.Ack != libraryfreeze.NoIngressAck {
|
||||
t.Fatalf("入口形态没解析对:%+v", target)
|
||||
}
|
||||
if target.IngressURL != "" || target.IngressService != "" || target.IngressDrainFile != "" {
|
||||
t.Fatalf("no-gate 声明里出现了闸门项:%+v", target)
|
||||
}
|
||||
if target.HealthURL != "http://127.0.0.1:18081/health" || target.RedisDB != 0 {
|
||||
t.Fatalf("健康探针/队列没解析对:%+v", target)
|
||||
}
|
||||
}
|
||||
|
||||
// 证据降一档不等于什么都不需要:健康探针与队列仍必须给。
|
||||
func TestTargetFromEnvNoGateStillNeedsHealthAndQueue(t *testing.T) {
|
||||
for _, name := range []string{EnvInstance, EnvHealthURL, EnvRedisAddr, EnvRedisAuth, EnvRedisDB} {
|
||||
values := noGateEnv()
|
||||
delete(values, name)
|
||||
if _, err := TargetFromEnv(lookup(values)); err == nil || !strings.Contains(err.Error(), name) {
|
||||
t.Fatalf("缺 %s 没有被点名:%v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 缺省仍是 gate:没有 mode 变量时照旧要求三项闸门声明(原行为不变)。
|
||||
func TestTargetFromEnvDefaultsToGate(t *testing.T) {
|
||||
values := completeEnv()
|
||||
target, err := TargetFromEnv(lookup(values))
|
||||
if err != nil {
|
||||
t.Fatalf("gate 声明被拒:%v", err)
|
||||
}
|
||||
if target.Mode != libraryfreeze.IngressModeGate || target.Ack != "" {
|
||||
t.Fatalf("缺省不是 gate:%+v", target)
|
||||
}
|
||||
values[EnvIngressURL] = ""
|
||||
if _, err := TargetFromEnv(lookup(values)); err == nil || !strings.Contains(err.Error(), EnvIngressURL) {
|
||||
t.Fatalf("gate 模式缺入口没有被拒:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetFromEnvRejectsUnknownMode(t *testing.T) {
|
||||
values := completeEnv()
|
||||
values[EnvIngressMode] = "open"
|
||||
if _, err := TargetFromEnv(lookup(values)); err == nil || !strings.Contains(err.Error(), EnvIngressMode) {
|
||||
t.Fatalf("未知入口形态没有被拒:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -48,14 +48,20 @@ type Config struct {
|
||||
Writers []string
|
||||
NonWriters map[string]string
|
||||
DockerSocket string
|
||||
IngressURL string
|
||||
// IngressMode 是 gate(默认)或 none;none 只给整栈没有入口闸门的测试环境用,
|
||||
// 必须同时给 IngressAck,且一次闸门动作都不做。
|
||||
IngressMode string
|
||||
IngressAck string
|
||||
IngressURL string
|
||||
// IngressService 是入口闸门在 compose 项目里的服务名;DrainFile 是它写
|
||||
// 排空证据的宿主机文件。两者缺一,就只能说"停了写入者",不能说"排空了"。
|
||||
IngressService string
|
||||
IngressDrainFile string
|
||||
Database string
|
||||
Index string
|
||||
Bucket string
|
||||
// HealthURL 是 no-gate 模式下唯一的"目标回来了"证据(恢复后必须 200)。
|
||||
HealthURL string
|
||||
Database string
|
||||
Index string
|
||||
Bucket string
|
||||
// 后台静默检查需要的运行时依赖(不写进维护记录)。
|
||||
DatabaseURL string
|
||||
RedisAddr string
|
||||
@@ -75,6 +81,9 @@ type Manager struct {
|
||||
gate *dockerGate
|
||||
api *dockerAPI
|
||||
quiescer *targetQuiescer
|
||||
mode string
|
||||
ack string
|
||||
healthURL string
|
||||
}
|
||||
|
||||
// Close 释放拓扑/静默检查持有的连接。
|
||||
@@ -91,6 +100,11 @@ func New(cfg Config) (*Manager, error) {
|
||||
instance := strings.TrimSpace(cfg.Instance)
|
||||
project := strings.TrimSpace(cfg.Project)
|
||||
ingress := strings.TrimSpace(cfg.IngressURL)
|
||||
health := strings.TrimSpace(cfg.HealthURL)
|
||||
mode := strings.TrimSpace(cfg.IngressMode)
|
||||
if mode == "" {
|
||||
mode = libraryfreeze.IngressModeGate
|
||||
}
|
||||
if instance == "" || project == "" {
|
||||
return nil, errors.New("实例级维护配置缺少 LIBRARY_TARGET_INSTANCE/PROJECT")
|
||||
}
|
||||
@@ -100,9 +114,27 @@ func New(cfg Config) (*Manager, error) {
|
||||
if !filepath.IsAbs(cfg.DockerSocket) {
|
||||
return nil, errors.New("Docker socket 必须是绝对路径")
|
||||
}
|
||||
parsed, err := url.Parse(ingress)
|
||||
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" || parsed.Path == "" || parsed.User != nil {
|
||||
return nil, errors.New("目标入口地址无效(LIBRARY_TARGET_INGRESS_URL)")
|
||||
switch mode {
|
||||
case libraryfreeze.IngressModeGate:
|
||||
parsed, err := url.Parse(ingress)
|
||||
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" || parsed.Path == "" || parsed.User != nil {
|
||||
return nil, errors.New("目标入口地址无效(LIBRARY_TARGET_INGRESS_URL)")
|
||||
}
|
||||
case libraryfreeze.IngressModeNone:
|
||||
// 显式确认是这道口子的唯一开关:没有它,no-gate 一律拒绝。
|
||||
if cfg.IngressAck != libraryfreeze.NoIngressAck {
|
||||
return nil, fmt.Errorf("no-gate 维护必须显式确认(LIBRARY_NO_INGRESS_ACK 逐字符等于 %s),拒绝",
|
||||
libraryfreeze.NoIngressAck)
|
||||
}
|
||||
if ingress != "" || cfg.IngressService != "" || cfg.IngressDrainFile != "" {
|
||||
return nil, errors.New("no-gate 维护不得同时声明入口地址/闸门服务/排空文件:要么有闸门,要么明确没有")
|
||||
}
|
||||
if !validHealthURL(health) {
|
||||
return nil, errors.New("no-gate 维护必须给有效的健康探针(LIBRARY_TARGET_HEALTH_URL)")
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("LIBRARY_TARGET_INGRESS_MODE 无效(%q):只能是 %s 或 %s",
|
||||
mode, libraryfreeze.IngressModeGate, libraryfreeze.IngressModeNone)
|
||||
}
|
||||
if cfg.Database == "" || cfg.Index == "" || cfg.Bucket == "" {
|
||||
return nil, errors.New("目标身份不完整:需要数据库/索引/存储桶")
|
||||
@@ -125,8 +157,9 @@ func New(cfg Config) (*Manager, error) {
|
||||
}
|
||||
manager := &Manager{target: target, source: project, docker: cfg.DockerSocket,
|
||||
writers: writers, nonWriters: cfg.NonWriters, timeout: cfg.Timeout,
|
||||
client: &http.Client{Timeout: 5 * time.Second}, api: api}
|
||||
if cfg.IngressService != "" || cfg.IngressDrainFile != "" {
|
||||
client: &http.Client{Timeout: 5 * time.Second}, api: api,
|
||||
mode: mode, ack: cfg.IngressAck, healthURL: health}
|
||||
if mode == libraryfreeze.IngressModeGate && (cfg.IngressService != "" || cfg.IngressDrainFile != "") {
|
||||
gate, err := newDockerGate(cfg.DockerSocket, project, cfg.IngressService, cfg.IngressDrainFile, cfg.Timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -168,8 +201,12 @@ func (m *Manager) Target() Target { return m.target }
|
||||
// Description 是写进维护记录的一句话身份,含指纹,便于事后核对"当时动的是谁"。
|
||||
func (m *Manager) Description() string {
|
||||
t := m.target
|
||||
ingress := t.Ingress
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
ingress = "none(test-only)"
|
||||
}
|
||||
return fmt.Sprintf("level=%s instance=%s project=%s writers=%s ingress=%s db=%s index=%s bucket=%s fingerprint=%s",
|
||||
t.Level, t.Instance, t.Project, strings.Join(t.Writers, "+"), t.Ingress, t.Database, t.Index, t.Bucket, t.Fingerprint)
|
||||
t.Level, t.Instance, t.Project, strings.Join(t.Writers, "+"), ingress, t.Database, t.Index, t.Bucket, t.Fingerprint)
|
||||
}
|
||||
|
||||
// Open 返回绑定到该目标的协调器与控制器。调用方负责 Close。
|
||||
@@ -183,7 +220,23 @@ func (m *Manager) Open(root string) (*libraryfreeze.Coordinator, *libraryfreeze.
|
||||
control.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := coordinator.SetTarget(LevelInstance, m.target.Ingress, m.ingressClosed, m.ingressReady); err != nil {
|
||||
// 入口形态必须在绑定目标之前声明:default 是 gate,只有显式 no-gate 才降档。
|
||||
if err := coordinator.SetIngressMode(m.mode, m.ack); err != nil {
|
||||
coordinator.Close()
|
||||
control.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
closed, ready := m.ingressClosed, m.ingressReady
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
// 没有闸门:不声明任何入口关闭探针,恢复证据改由健康探针承担。
|
||||
closed, ready = nil, m.healthReady
|
||||
if err := coordinator.SetHealthURL(m.healthURL); err != nil {
|
||||
coordinator.Close()
|
||||
control.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
if err := coordinator.SetTarget(LevelInstance, m.target.Ingress, closed, ready); err != nil {
|
||||
coordinator.Close()
|
||||
control.Close()
|
||||
return nil, nil, err
|
||||
@@ -239,7 +292,11 @@ func (m *Manager) serviceOf(id string) string {
|
||||
}
|
||||
|
||||
// VerifyRunning 确认目标入口真的在服务(部署与恢复后都要看,不看容器状态)。
|
||||
// no-gate 栈没有入口:用健康探针回答"目标回来了没有"。
|
||||
func (m *Manager) VerifyRunning(ctx context.Context) error {
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
return m.healthReady(ctx)
|
||||
}
|
||||
return m.ingressReady(ctx)
|
||||
}
|
||||
|
||||
@@ -308,6 +365,47 @@ func (m *Manager) ingressReady(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// healthReady 是 no-gate 栈的恢复判据:健康探针返回 200 才算"目标真的回来了"。
|
||||
// 和入口探针同一条规则——不看容器状态、不看 HTTP 5xx 的"像活着"。
|
||||
func (m *Manager) healthReady(ctx context.Context) error {
|
||||
deadline := time.Now().Add(m.timeout)
|
||||
var last error
|
||||
for {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.healthURL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 探针不能留下 keep-alive 连接:那会让"排空"永远等不到。
|
||||
req.Close = true
|
||||
resp, err := m.client.Do(req)
|
||||
if err == nil {
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
last = fmt.Errorf("健康探针返回 HTTP %d:%s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||
} else {
|
||||
last = err
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("目标健康探针没有恢复:%w", last)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return last
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// validHealthURL 是健康探针地址的本地校验:http(s)、有主机、不带凭据。
|
||||
func validHealthURL(raw string) bool {
|
||||
parsed, err := url.Parse(strings.TrimSpace(raw))
|
||||
return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") &&
|
||||
parsed.Host != "" && parsed.User == nil
|
||||
}
|
||||
|
||||
// DatabaseIdentity 把连接串收敛成"host:port/db"这种可以写进维护记录的身份:
|
||||
// 维护记录里不落库凭据,但必须能说清动的是哪个库。
|
||||
func DatabaseIdentity(raw string) (string, error) {
|
||||
@@ -354,12 +452,21 @@ func (m *Manager) VerifyTopology(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// no-gate 栈没有闸门可绕:写入者发布端口是它的正常对外形态,
|
||||
// "绕闸直连"这条判据在这里不成立(有闸门时才必须唯一入口)。
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
continue
|
||||
}
|
||||
if ports := state.publishedPorts(); len(ports) > 0 {
|
||||
return fmt.Errorf("写入者 %s 直接发布了端口 %s:存在绕开入口闸门的直连通道,拒绝开维护窗口",
|
||||
writer, strings.Join(ports, ","))
|
||||
}
|
||||
}
|
||||
if m.gate == nil {
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
// 没有闸门可核验端口归属:拓扑检查到此为止,证据降档由维护记录承担。
|
||||
return nil
|
||||
}
|
||||
return errors.New("实例级维护必须声明入口闸门")
|
||||
}
|
||||
for _, writer := range m.writers {
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
// Package libraryprofile 声明"某个 WeKnora schema 版本下,整库适配器可以依赖什么"。
|
||||
//
|
||||
// 为什么要有这一层:整库导出/导入原本把 `version != 74` 写死在四个入口里。
|
||||
// 版本号本身不是契约——同一版本号在不同部署上也可能缺表;反过来,把判定放宽成
|
||||
// `version > 74` 又会把"没见过的版本"当成"兼容"。所以这里同时声明两件事:
|
||||
// - 该版本必须存在的表与列(导出/导入按它取数);
|
||||
// - 该版本的归档部件集合(manifest 按它判定完整性)。
|
||||
//
|
||||
// 版本号只用来**选中** profile,真正的放行条件是随后对 information_schema 的
|
||||
// 能力探测(见 releaseadapter 的 ResolveSchemaProfile)。
|
||||
package libraryprofile
|
||||
|
||||
import "fmt"
|
||||
|
||||
// LegacyVersion 是 0.7.1 的 schema_migrations 版本号,也是既有内存夹具的默认值。
|
||||
const LegacyVersion = 74
|
||||
|
||||
// V090Version 是 0.9.0 的 schema_migrations 版本号(已实测:134、dirty=false、83 表)。
|
||||
const V090Version = 134
|
||||
|
||||
// PartRequirement 是归档部件的一项要求(kind/name 与 manifest 的部件键一致)。
|
||||
type PartRequirement struct {
|
||||
Kind string
|
||||
Name string
|
||||
}
|
||||
|
||||
// Profile 是一个 schema 版本的静态能力声明。
|
||||
type Profile struct {
|
||||
Name string
|
||||
SchemaVersion int
|
||||
WeKnoraVersion string
|
||||
// EmbeddingDimension 为 0 表示"由目标 models.parameters 说了算";
|
||||
// 非 0 表示该版本只接受这一定维度(0.7.1 的契约就是固定 1024)。
|
||||
EmbeddingDimension int
|
||||
RequiredTables []string
|
||||
OptionalTables []string
|
||||
RequiredColumns map[string][]string
|
||||
// ReviewedTriggers 是该版本允许出现的 pg_get_triggerdef 原文。
|
||||
// 触发器意味着"写一行还会改别的表",所以只接受逐字核对过的定义,
|
||||
// 且这些定义必须在该版本上真实存在(消失也说明 schema 不是我们审过的那份)。
|
||||
ReviewedTriggers []string
|
||||
RequiredParts []PartRequirement
|
||||
OptionalParts []PartRequirement
|
||||
// IndexContractSource 说明该版本的索引名从哪里读。空值等于"读不到",
|
||||
// 不允许回退成空串去匹配任何索引。
|
||||
IndexContractSource string
|
||||
// IndexTenantField 表示该版本的索引文档带 tenant_id。0.9.0 的分块文档只有
|
||||
// knowledge_base_id,写索引时补 tenant_id 会与目标实际文档不一致。
|
||||
IndexTenantField bool
|
||||
}
|
||||
|
||||
var profiles = map[int]Profile{
|
||||
LegacyVersion: {
|
||||
Name: "v071",
|
||||
SchemaVersion: LegacyVersion,
|
||||
WeKnoraVersion: "v0.7.1",
|
||||
EmbeddingDimension: 1024,
|
||||
RequiredTables: []string{
|
||||
"knowledges", "chunks", "knowledge_tags", "knowledge_tag_relations",
|
||||
"resources", "resource_bindings", "wiki_folders", "wiki_pages",
|
||||
"wiki_page_issues", "wiki_log_entries",
|
||||
},
|
||||
RequiredColumns: map[string][]string{
|
||||
"knowledges": {"id", "tenant_id", "knowledge_base_id", "deleted_at", "parse_status"},
|
||||
"chunks": {"id", "tenant_id", "knowledge_base_id", "knowledge_id", "content"},
|
||||
"knowledge_tags": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"knowledge_tag_relations": {"knowledge_id", "tag_id"},
|
||||
"resources": {"id", "handle", "tenant_id", "physical_path"},
|
||||
"resource_bindings": {"id", "resource_id", "tenant_id", "owner_type", "owner_id"},
|
||||
"wiki_pages": {"id", "tenant_id", "knowledge_base_id", "slug"},
|
||||
"wiki_folders": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"wiki_page_issues": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"wiki_log_entries": {"id", "tenant_id", "knowledge_base_id"},
|
||||
},
|
||||
RequiredParts: []PartRequirement{
|
||||
{"database", "knowledges"}, {"database", "chunks"},
|
||||
{"database", "knowledge_tags"}, {"database", "knowledge_tag_relations"},
|
||||
{"database", "resources"}, {"database", "resource_bindings"},
|
||||
{"wiki", "wiki_folders"}, {"wiki", "wiki_pages"},
|
||||
{"wiki", "wiki_page_issues"}, {"wiki", "wiki_log_entries"},
|
||||
{"index", "chunks"},
|
||||
},
|
||||
IndexContractSource: "vector_stores.index_config.index_name",
|
||||
IndexTenantField: true,
|
||||
},
|
||||
V090Version: {
|
||||
Name: "v090",
|
||||
SchemaVersion: V090Version,
|
||||
WeKnoraVersion: "v0.9.0",
|
||||
// 0.9.0 的 KB 不写 vector_store_id,维度只能从 models.parameters 读。
|
||||
EmbeddingDimension: 0,
|
||||
RequiredTables: []string{
|
||||
"knowledges", "chunks", "knowledge_tags", "knowledge_tag_relations",
|
||||
"resources", "resource_bindings", "wiki_folders", "wiki_pages",
|
||||
"wiki_page_issues",
|
||||
},
|
||||
// 0.9.0 删掉了 wiki_log_entries;部件集合随之为空,不许假装还有。
|
||||
OptionalTables: []string{"wiki_log_entries"},
|
||||
RequiredColumns: map[string][]string{
|
||||
"knowledges": {"id", "tenant_id", "knowledge_base_id", "deleted_at", "parse_status"},
|
||||
"chunks": {"id", "tenant_id", "knowledge_base_id", "knowledge_id", "content", "seq_id", "content_revision", "index_status"},
|
||||
"knowledge_tags": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"knowledge_tag_relations": {"knowledge_id", "tag_id"},
|
||||
"resources": {"id", "handle", "tenant_id", "physical_path"},
|
||||
"resource_bindings": {"id", "resource_id", "tenant_id", "owner_type", "owner_id"},
|
||||
"wiki_pages": {"id", "tenant_id", "knowledge_base_id", "slug"},
|
||||
"wiki_folders": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"wiki_page_issues": {"id", "tenant_id", "knowledge_base_id"},
|
||||
},
|
||||
RequiredParts: []PartRequirement{
|
||||
{"database", "knowledges"}, {"database", "chunks"},
|
||||
{"database", "knowledge_tags"}, {"database", "knowledge_tag_relations"},
|
||||
{"database", "resources"}, {"database", "resource_bindings"},
|
||||
{"wiki", "wiki_folders"}, {"wiki", "wiki_pages"}, {"wiki", "wiki_page_issues"},
|
||||
{"index", "chunks"},
|
||||
},
|
||||
OptionalParts: []PartRequirement{
|
||||
{"wiki", "wiki_log_entries"},
|
||||
},
|
||||
// 0.9.0 的索引名来自部署配置(ELASTICSEARCH_INDEX),不是 vector_stores。
|
||||
// 0.9.0 在 chunks 上挂了图片同步触发器(仅当 image_info 以 '[' 开头时触发),
|
||||
// 它维护 chunk_images 派生表;定义逐字核对,任何改动都拒绝。
|
||||
ReviewedTriggers: []string{
|
||||
`CREATE TRIGGER trg_chunk_images_delete AFTER DELETE ON public.chunks FOR EACH ROW WHEN ((old.image_info ~~ '[%'::text)) EXECUTE FUNCTION chunk_images_sync()`,
|
||||
`CREATE TRIGGER trg_chunk_images_insert AFTER INSERT ON public.chunks FOR EACH ROW WHEN ((new.image_info ~~ '[%'::text)) EXECUTE FUNCTION chunk_images_sync()`,
|
||||
`CREATE TRIGGER trg_chunk_images_update AFTER UPDATE ON public.chunks FOR EACH ROW WHEN ((((old.image_info ~~ '[%'::text) OR (new.image_info ~~ '[%'::text)) AND ((old.image_info IS DISTINCT FROM new.image_info) OR (old.is_enabled IS DISTINCT FROM new.is_enabled) OR (old.status IS DISTINCT FROM new.status) OR (old.deleted_at IS DISTINCT FROM new.deleted_at) OR (old.tenant_id IS DISTINCT FROM new.tenant_id) OR ((old.knowledge_base_id)::text IS DISTINCT FROM (new.knowledge_base_id)::text) OR ((old.knowledge_id)::text IS DISTINCT FROM (new.knowledge_id)::text) OR ((old.chunk_type)::text IS DISTINCT FROM (new.chunk_type)::text) OR (old.created_at IS DISTINCT FROM new.created_at) OR (old.updated_at IS DISTINCT FROM new.updated_at)))) EXECUTE FUNCTION chunk_images_sync()`,
|
||||
},
|
||||
IndexContractSource: "config ELASTICSEARCH_INDEX",
|
||||
IndexTenantField: false,
|
||||
},
|
||||
}
|
||||
|
||||
// For 按版本号选中 profile。未知版本一律失败:版本号是"选哪份声明"的索引,
|
||||
// 不是"能不能放行"的依据,所以绝不返回默认 profile。
|
||||
func For(version int) (Profile, error) {
|
||||
profile, ok := profiles[version]
|
||||
if !ok {
|
||||
return Profile{}, fmt.Errorf("unsupported WeKnora schema version %d: supported profiles are 74 (v071) and 134 (v090)", version)
|
||||
}
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
// Supported 返回已声明 profile 的版本号(用于文档与错误信息,不用于放行)。
|
||||
func Supported() []int {
|
||||
return []int{LegacyVersion, V090Version}
|
||||
}
|
||||
|
||||
// TableSet 是"可能出现的表"集合:必需表 + 可选表。
|
||||
func (p Profile) TableSet() map[string]bool {
|
||||
set := make(map[string]bool, len(p.RequiredTables)+len(p.OptionalTables))
|
||||
for _, name := range p.RequiredTables {
|
||||
set[name] = true
|
||||
}
|
||||
for _, name := range p.OptionalTables {
|
||||
set[name] = true
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// HasTable 报告某张表是否属于该 profile(必需或可选)。
|
||||
func (p Profile) HasTable(name string) bool {
|
||||
return p.TableSet()[name]
|
||||
}
|
||||
|
||||
// IsRequiredTable 报告某张表是否必需存在。
|
||||
func (p Profile) IsRequiredTable(name string) bool {
|
||||
for _, candidate := range p.RequiredTables {
|
||||
if candidate == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PartKeys 返回 (必需, 可选) 的归档部件键,形如 "wiki/wiki_log_entries"。
|
||||
func (p Profile) PartKeys() ([]string, []string) {
|
||||
key := func(part PartRequirement) string { return part.Kind + "/" + part.Name }
|
||||
required := make([]string, 0, len(p.RequiredParts))
|
||||
for _, part := range p.RequiredParts {
|
||||
required = append(required, key(part))
|
||||
}
|
||||
optional := make([]string, 0, len(p.OptionalParts))
|
||||
for _, part := range p.OptionalParts {
|
||||
optional = append(optional, key(part))
|
||||
}
|
||||
return required, optional
|
||||
}
|
||||
|
||||
// RequiresPart 报告某部件键是否必需(可选部件返回 false)。
|
||||
func (p Profile) RequiresPart(key string) bool {
|
||||
required, _ := p.PartKeys()
|
||||
for _, candidate := range required {
|
||||
if candidate == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AllowsPart 报告某部件键是否至少允许出现。
|
||||
func (p Profile) AllowsPart(key string) bool {
|
||||
required, optional := p.PartKeys()
|
||||
for _, candidate := range required {
|
||||
if candidate == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, candidate := range optional {
|
||||
if candidate == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package libraryrelease
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 恢复标记派发协议(DS01 窄协议)。
|
||||
//
|
||||
// 这里定义的唯一持久事实是:某个固定的删除型恢复 effect 是否已经有人拿到过
|
||||
// "向索引发送一次 `_create`"的许可。它不是"ES 历史上从未有人写过这个 ID"的证明,
|
||||
// 也不是目标端 fence。判据是单调的持久状态,不是 missing、stats 或内存布尔值。
|
||||
const (
|
||||
// RestoreMarkerPhaseCreate 是恢复期独占 create(删除型文档的原文标记)的唯一相位。
|
||||
// 其它相位(apply create、compensation DELETE、clean CAS)不得借用它取得发送许可。
|
||||
RestoreMarkerPhaseCreate = "restore-marker-create"
|
||||
|
||||
RestoreDispatchPrepared = "prepared"
|
||||
RestoreDispatchDispatching = "dispatching"
|
||||
RestoreDispatchMarkerConfirmed = "marker_confirmed"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRestoreDispatchHeld = errors.New("restore marker dispatch is held")
|
||||
ErrRestorePermitSpent = errors.New("restore send permit already used")
|
||||
ErrRestorePermitMismatch = errors.New("restore send permit bound to another effect")
|
||||
ErrRestoreDispatchUnavailable = errors.New("restore marker dispatch protocol is not activated for this target")
|
||||
)
|
||||
|
||||
// RestoreMarkerKey 是"同一个逻辑恢复 effect"的身份。epoch、恢复队列 ID、恢复作业 ID
|
||||
// 都不在 key 里:接管与重试必须落到同一行,不能靠换 key 绕开旧的 dispatching。
|
||||
type RestoreMarkerKey struct {
|
||||
TargetIdentity string `json:"target_identity"`
|
||||
OperationID string `json:"operation_id"`
|
||||
WindowJobID string `json:"window_job_id"`
|
||||
PlanDigest string `json:"plan_digest"`
|
||||
Index string `json:"index"`
|
||||
DocumentID string `json:"document_id"`
|
||||
Phase string `json:"phase"`
|
||||
}
|
||||
|
||||
func (k RestoreMarkerKey) Validate() error {
|
||||
for name, value := range map[string]string{
|
||||
"target": k.TargetIdentity, "operation": k.OperationID, "window job": k.WindowJobID,
|
||||
"plan": k.PlanDigest, "index": k.Index, "document": k.DocumentID, "phase": k.Phase,
|
||||
} {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("restore marker key lacks %s", name)
|
||||
}
|
||||
}
|
||||
if k.Phase != RestoreMarkerPhaseCreate {
|
||||
return fmt.Errorf("restore marker key phase %q is not the single create phase", k.Phase)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RestoreMarkerBinding 是派发许可的不可变绑定。目标、快照、计划、变更体与标记体任一
|
||||
// 变化都必须 held,不能另起一行继续写。
|
||||
//
|
||||
// SourceSnapshot 是发布基线(原作业要同步到的那份来源),BackupSnapshot 是本次失败
|
||||
// 写集的写前镜像;两者必须来自原持久锚点且不能互相代替。TargetInstance /
|
||||
// TargetFingerprint / IndexUUID 冻结"实际目标",用于识别目标被同名字替换;它们不证明
|
||||
// 历史上没有发送过任何请求。
|
||||
type RestoreMarkerBinding struct {
|
||||
Key RestoreMarkerKey `json:"key"`
|
||||
Scope string `json:"scope"`
|
||||
SourceSnapshot string `json:"source_snapshot"`
|
||||
BackupSnapshot string `json:"backup_snapshot"`
|
||||
TargetInstance string `json:"target_instance"`
|
||||
TargetFingerprint string `json:"target_fingerprint"`
|
||||
IndexUUID string `json:"index_uuid"`
|
||||
RecoveryJobID string `json:"recovery_job_id,omitempty"`
|
||||
RecoveryOperationID string `json:"recovery_operation_id,omitempty"`
|
||||
ChangeDigest string `json:"change_digest"`
|
||||
MarkerDigest string `json:"marker_digest"`
|
||||
RequestDigest string `json:"request_digest"`
|
||||
Owner string `json:"owner"`
|
||||
Epoch int64 `json:"epoch"`
|
||||
}
|
||||
|
||||
func (b RestoreMarkerBinding) Validate() error {
|
||||
if err := b.Key.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
for name, value := range map[string]string{
|
||||
"scope": b.Scope, "source snapshot": b.SourceSnapshot, "backup snapshot": b.BackupSnapshot,
|
||||
"target instance": b.TargetInstance, "target fingerprint": b.TargetFingerprint,
|
||||
"index uuid": b.IndexUUID, "change digest": b.ChangeDigest, "marker digest": b.MarkerDigest,
|
||||
"request digest": b.RequestDigest, "owner": b.Owner,
|
||||
} {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("restore marker binding lacks %s", name)
|
||||
}
|
||||
}
|
||||
if b.SourceSnapshot == b.BackupSnapshot {
|
||||
return errors.New("restore marker binding uses the source snapshot as the backup image")
|
||||
}
|
||||
if b.RecoveryJobID == "" != (b.RecoveryOperationID == "") {
|
||||
return errors.New("restore marker binding has a partial recovery delivery identity")
|
||||
}
|
||||
if b.Epoch <= 0 {
|
||||
return errors.New("restore marker binding lacks a positive attempt epoch")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Digest 覆盖**逻辑 effect 的不可变绑定**;台账用它拒绝"同 key 换绑定"。
|
||||
// owner/epoch 与 recovery queue/operation 是执行/投递锚点,不进摘要:自动回滚与
|
||||
// 随后的 recovery worker 必须落到同一个 effect 身份,但它们的投递身份不同。
|
||||
func (b RestoreMarkerBinding) Digest() (string, error) {
|
||||
if err := b.Validate(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
joined := strings.Join([]string{
|
||||
"v2", b.Key.TargetIdentity, b.Key.OperationID, b.Key.WindowJobID, b.Key.PlanDigest,
|
||||
b.Key.Index, b.Key.DocumentID, b.Key.Phase, b.Scope, b.SourceSnapshot, b.BackupSnapshot,
|
||||
b.TargetInstance, b.TargetFingerprint, b.IndexUUID,
|
||||
b.ChangeDigest, b.MarkerDigest, b.RequestDigest,
|
||||
}, "\x00")
|
||||
sum := sha256.Sum256([]byte(joined))
|
||||
return hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
|
||||
// RestoreDispatchStatus 是纯读分类结果。Held 为真时永远不能发送。
|
||||
//
|
||||
// BindingConflict 是与 Held 不同的**类型化**分类:Held 只说"这次不能再 create"
|
||||
// (prepared 冲突、dispatching 结果未知、marker 已确认都可能是合法的不可重发),
|
||||
// 而 BindingConflict 说明台账行的不可变绑定与本次绑定不是同一个 effect。绑定冲突
|
||||
// 在**任何状态**下都必须拒绝,包括已完成 clean 的 marker_confirmed;否则"只允许原
|
||||
// 确认分支继续"会变成放行一份不属于本计划的写入。
|
||||
type RestoreDispatchStatus struct {
|
||||
State string `json:"state"`
|
||||
Held bool `json:"held"`
|
||||
BindingConflict bool `json:"binding_conflict,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// RestoreSendPermit 是"这一次调用、这一个 effect、这一次发送"的许可。
|
||||
//
|
||||
// 它不是可复用凭证:Store 只在 dispatching 明确 commit 之后返回它;消费一次即作废。
|
||||
// 不能从 Load/Get/JSON/重启重建——那正是本协议要排除的"从外观推断未派发"。
|
||||
type RestoreSendPermit struct {
|
||||
key RestoreMarkerKey
|
||||
binding string
|
||||
nonce string
|
||||
used bool
|
||||
}
|
||||
|
||||
// IssueRestoreSendPermit 只允许实现 RestoreMarkerGate 的存储层调用。许可的权威性来自
|
||||
// 数据库 CAS 与提交,不来自这个构造函数;这里只保证调用栈内一次性。
|
||||
func IssueRestoreSendPermit(key RestoreMarkerKey, bindingDigest, nonce string) *RestoreSendPermit {
|
||||
return &RestoreSendPermit{key: key, binding: bindingDigest, nonce: nonce}
|
||||
}
|
||||
|
||||
func (p *RestoreSendPermit) Nonce() string {
|
||||
if p == nil {
|
||||
return ""
|
||||
}
|
||||
return p.nonce
|
||||
}
|
||||
|
||||
// Consume 校验 effect/绑定并作废许可。绑定不符时不消耗许可(避免被一次误用烧掉)。
|
||||
func (p *RestoreSendPermit) Consume(key RestoreMarkerKey, bindingDigest string) error {
|
||||
if p == nil {
|
||||
return ErrRestorePermitMismatch
|
||||
}
|
||||
if p.used {
|
||||
return ErrRestorePermitSpent
|
||||
}
|
||||
if p.key != key || p.binding != bindingDigest {
|
||||
return ErrRestorePermitMismatch
|
||||
}
|
||||
p.used = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// RestoreDispatchRecord 是权威台账里"这个原 window 还有哪些未决 effect"的一条。
|
||||
// 收尾与前置都查同一份记录集,不能只遍历当前 plan 的文档列表。
|
||||
type RestoreDispatchRecord struct {
|
||||
PlanDigest string `json:"plan_digest"`
|
||||
Index string `json:"index"`
|
||||
DocumentID string `json:"document_id"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
// RestoreMarkerGate 是唯一能授予恢复标记 create 发送权的接口。
|
||||
//
|
||||
// Inspect 纯读、不返回许可;Prepare 只建立 prepared;Acquire 在独立短事务里做
|
||||
// prepared→dispatching 的 CAS,只有明确提交后才返回许可;Confirm 只记录真实回执;
|
||||
// UnresolvedRestoreDispatches 返回原 window 的权威未决全集(跨 plan,只读)。
|
||||
type RestoreMarkerGate interface {
|
||||
InspectRestoreMarker(ctx context.Context, b RestoreMarkerBinding) (RestoreDispatchStatus, bool, error)
|
||||
PrepareRestoreMarker(ctx context.Context, b RestoreMarkerBinding) (RestoreDispatchStatus, error)
|
||||
AcquireRestoreMarker(ctx context.Context, b RestoreMarkerBinding) (*RestoreSendPermit, RestoreDispatchStatus, error)
|
||||
ConfirmRestoreMarker(ctx context.Context, b RestoreMarkerBinding, receipt []byte) error
|
||||
UnresolvedRestoreDispatches(ctx context.Context, targetIdentity, operationID, windowJobID string) ([]RestoreDispatchRecord, error)
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package libraryrelease
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func restoreBinding() RestoreMarkerBinding {
|
||||
return RestoreMarkerBinding{
|
||||
Key: RestoreMarkerKey{TargetIdentity: "target-a", OperationID: "op-1", WindowJobID: "job-1",
|
||||
PlanDigest: "plan-1", Index: "kg-index", DocumentID: "doc-1", Phase: RestoreMarkerPhaseCreate},
|
||||
Scope: "internal", SourceSnapshot: "snap-src", BackupSnapshot: "snap-backup",
|
||||
TargetInstance: "inst-1", TargetFingerprint: "fingerprint-1", IndexUUID: "uuid-1",
|
||||
ChangeDigest: "change-1", MarkerDigest: "marker-1", RequestDigest: "request-1",
|
||||
Owner: "worker-a#queue#1", Epoch: 3,
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreMarkerBindingRejectsIncompleteIdentity(t *testing.T) {
|
||||
for name, mutate := range map[string]func(*RestoreMarkerBinding){
|
||||
"target": func(b *RestoreMarkerBinding) { b.Key.TargetIdentity = "" },
|
||||
"operation": func(b *RestoreMarkerBinding) { b.Key.OperationID = "" },
|
||||
"window": func(b *RestoreMarkerBinding) { b.Key.WindowJobID = "" },
|
||||
"plan": func(b *RestoreMarkerBinding) { b.Key.PlanDigest = "" },
|
||||
"index": func(b *RestoreMarkerBinding) { b.Key.Index = "" },
|
||||
"document": func(b *RestoreMarkerBinding) { b.Key.DocumentID = "" },
|
||||
"change": func(b *RestoreMarkerBinding) { b.ChangeDigest = "" },
|
||||
"marker": func(b *RestoreMarkerBinding) { b.MarkerDigest = "" },
|
||||
"request": func(b *RestoreMarkerBinding) { b.RequestDigest = "" },
|
||||
"owner": func(b *RestoreMarkerBinding) { b.Owner = "" },
|
||||
"epoch": func(b *RestoreMarkerBinding) { b.Epoch = 0 },
|
||||
"phase": func(b *RestoreMarkerBinding) { b.Key.Phase = "apply-create" },
|
||||
"instance": func(b *RestoreMarkerBinding) { b.TargetInstance = "" },
|
||||
"fingerprint": func(b *RestoreMarkerBinding) { b.TargetFingerprint = "" },
|
||||
"index uuid": func(b *RestoreMarkerBinding) { b.IndexUUID = "" },
|
||||
"source as backup": func(b *RestoreMarkerBinding) { b.BackupSnapshot = b.SourceSnapshot },
|
||||
} {
|
||||
binding := restoreBinding()
|
||||
mutate(&binding)
|
||||
if err := binding.Validate(); err == nil {
|
||||
t.Fatalf("%s: incomplete binding accepted", name)
|
||||
}
|
||||
}
|
||||
if err := restoreBinding().Validate(); err != nil {
|
||||
t.Fatalf("complete binding rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreMarkerBindingDigestCoversEveryBoundValue(t *testing.T) {
|
||||
base, err := restoreBinding().Digest()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if again, _ := restoreBinding().Digest(); again != base {
|
||||
t.Fatal("binding digest is not deterministic")
|
||||
}
|
||||
for name, mutate := range map[string]func(*RestoreMarkerBinding){
|
||||
"target": func(b *RestoreMarkerBinding) { b.Key.TargetIdentity = "target-b" },
|
||||
"index": func(b *RestoreMarkerBinding) { b.Key.Index = "other-index" },
|
||||
"doc": func(b *RestoreMarkerBinding) { b.Key.DocumentID = "doc-2" },
|
||||
"plan": func(b *RestoreMarkerBinding) { b.Key.PlanDigest = "plan-2" },
|
||||
"backup": func(b *RestoreMarkerBinding) { b.BackupSnapshot = "snap-2" },
|
||||
"change": func(b *RestoreMarkerBinding) { b.ChangeDigest = "change-2" },
|
||||
"marker": func(b *RestoreMarkerBinding) { b.MarkerDigest = "marker-2" },
|
||||
"request": func(b *RestoreMarkerBinding) { b.RequestDigest = "request-2" },
|
||||
"scope": func(b *RestoreMarkerBinding) { b.Scope = "external" },
|
||||
"source": func(b *RestoreMarkerBinding) { b.SourceSnapshot = "snap-9" },
|
||||
"instance": func(b *RestoreMarkerBinding) { b.TargetInstance = "inst-2" },
|
||||
"fingerprint": func(b *RestoreMarkerBinding) { b.TargetFingerprint = "fingerprint-2" },
|
||||
"index uuid": func(b *RestoreMarkerBinding) { b.IndexUUID = "uuid-2" },
|
||||
} {
|
||||
binding := restoreBinding()
|
||||
mutate(&binding)
|
||||
changed, err := binding.Digest()
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", name, err)
|
||||
}
|
||||
if changed == base {
|
||||
t.Fatalf("%s: binding change not covered by digest", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 许可只能在本次调用栈用一次;换 key/换绑定都不能复用,也不能把已花的许可再花一次。
|
||||
// 恢复投递身份是执行锚点,不是 effect 身份:自动回滚与 recovery worker 的摘要必须一致。
|
||||
func TestRestoreBindingDigestIgnoresDeliveryIdentity(t *testing.T) {
|
||||
base, err := restoreBinding().Digest()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delivered := restoreBinding()
|
||||
delivered.RecoveryJobID, delivered.RecoveryOperationID = "queue-1", "op-recovery"
|
||||
delivered.Owner, delivered.Epoch = "worker-b#queue#1", 7
|
||||
other, err := delivered.Digest()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if other != base {
|
||||
t.Fatal("delivery/attempt identity changed the logical effect digest")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreSendPermitIsSingleUseAndKeyBound(t *testing.T) {
|
||||
binding := restoreBinding()
|
||||
digest, err := binding.Digest()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
permit := IssueRestoreSendPermit(binding.Key, digest, "nonce-1")
|
||||
other := binding.Key
|
||||
other.DocumentID = "doc-2"
|
||||
if err := permit.Consume(other, digest); err == nil || !errors.Is(err, ErrRestorePermitMismatch) {
|
||||
t.Fatalf("permit usable for another effect: %v", err)
|
||||
}
|
||||
if err := permit.Consume(binding.Key, "other-binding"); err == nil || !errors.Is(err, ErrRestorePermitMismatch) {
|
||||
t.Fatalf("permit usable for another binding: %v", err)
|
||||
}
|
||||
if err := permit.Consume(binding.Key, digest); err != nil {
|
||||
t.Fatalf("valid permit rejected: %v", err)
|
||||
}
|
||||
if err := permit.Consume(binding.Key, digest); err == nil || !errors.Is(err, ErrRestorePermitSpent) {
|
||||
t.Fatalf("permit spent twice: %v", err)
|
||||
}
|
||||
if strings.Contains(permit.Nonce(), digest) {
|
||||
t.Fatal("nonce is not independent audit data")
|
||||
}
|
||||
var nilPermit *RestoreSendPermit
|
||||
if err := nilPermit.Consume(binding.Key, digest); err == nil {
|
||||
t.Fatal("nil permit accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
package libraryrelease
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 恢复派发激活证据(DS01-F3,协议版本 2)。
|
||||
//
|
||||
// 配置开关、"三个非空字符串"或调用方自填的 `stopped=true` 都不是独占证明。v2 证据
|
||||
// 只允许**指向**一份由维护窗口写下的 journal:调用方声明目标身份、维护作业、journal
|
||||
// 文件名/摘要与受控 writer 的不可变实例身份;真正的事实(入口关闭与排空、后台静默、
|
||||
// 每个 writer 的退出观察)由 Store 直接读 journal 字节并解析取得,再用治理库的
|
||||
// library_instance_maintenance 行把作业/实例/指纹/writer 集合钉死。
|
||||
//
|
||||
// 因此"把调用方 JSON 再 hash 一次"不再算核实:摘要只证明"读到的是这一份 journal",
|
||||
// 事实来自 journal 本体与治理库行,而不是调用方的布尔字段。
|
||||
//
|
||||
// 边界:代码能核验 journal 完整性、目标/实例/治理库/构建身份、writer 集合一致性与
|
||||
// 新鲜度;它不独立观测"外部 writer 此刻没在运行",但 journal 的入口 drain(
|
||||
// active_at_close=0)、quiesce 零点与退出码是维护链自己的受控观察,且窗口必须仍处于
|
||||
// held(已恢复/释放的窗口不构成"旧 writer 已停"的证据)。
|
||||
|
||||
const RestoreDispatchActivationMaxAge = 2 * time.Hour
|
||||
|
||||
// 证据文档 schema 版本与语义兼容批次是两件事:
|
||||
// - ProtocolVersion:这份 JSON 的字段形状(解析契约);
|
||||
// - ProtocolEpoch:派发协议语义批次;不同批次必须重新登记。它只约束派发语义,
|
||||
// 不能证明某个构建 SHA 属于该批次 —— 当前构建身份仍精确绑定运行 SHA,跨 SHA
|
||||
// 自动兼容在没有受信批次成员证明前保持关闭。
|
||||
//
|
||||
// 构建身份用完整 40 位 SHA,不用 display:开发号会随提交数变化,两个不同二进制
|
||||
// 完全可能都叫 0.1.322。
|
||||
const RestoreDispatchActivationProtocolVersion = 3
|
||||
|
||||
const RestoreDispatchProtocolEpoch = 3
|
||||
|
||||
// RestoreDispatchGovernanceAuthority 是治理库的**权威域**身份:系统标识(集群)
|
||||
// 加库 OID。只看 current_database() 无法区分"同名但另一个集群/另一份克隆"。
|
||||
// primary 由 Store 现场读 pg_is_in_recovery() 得出,不采信调用方声明。
|
||||
type RestoreDispatchGovernanceAuthority struct {
|
||||
SystemIdentifier string `json:"system_identifier"`
|
||||
DatabaseOID string `json:"database_oid"`
|
||||
}
|
||||
|
||||
// RestoreDispatchWriterDrain 是调用方对"旧 writer 集合"的声明:只给身份,不给结论。
|
||||
// InstanceID 必须等于 journal 里登记的不可变 writer 身份(容器/进程 ID)。
|
||||
type RestoreDispatchWriterDrain struct {
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind"`
|
||||
InstanceID string `json:"instance_id"`
|
||||
}
|
||||
|
||||
type RestoreDispatchActivationEvidence struct {
|
||||
ProtocolVersion int `json:"protocol_version"`
|
||||
ProtocolEpoch int `json:"protocol_epoch"`
|
||||
TargetIdentity string `json:"target_identity"`
|
||||
// TargetFingerprint 是维护窗口/部署目标指纹(journal 与治理库维护行记录的那一个);
|
||||
// RestoreTargetFingerprint 是激活时对恢复目标内容/索引/桶的实时观察值。两者是不同层,
|
||||
// 不能相互替代:前者绑定"动的是哪个实例",后者绑定"恢复要写的那个目标身份"。
|
||||
TargetFingerprint string `json:"target_fingerprint"`
|
||||
RestoreTargetFingerprint string `json:"restore_target_fingerprint"`
|
||||
TargetInstance string `json:"target_instance"`
|
||||
GovernanceDatabase string `json:"governance_database"`
|
||||
GovernanceAuthority RestoreDispatchGovernanceAuthority `json:"governance_authority"`
|
||||
ProtocolBuild string `json:"protocol_build"`
|
||||
ProtocolBuildSHA string `json:"protocol_build_sha"`
|
||||
AttestedBy string `json:"attested_by"`
|
||||
AttestedAt time.Time `json:"attested_at"`
|
||||
MaintenanceJob string `json:"maintenance_job"`
|
||||
MaintenanceJournal string `json:"maintenance_journal"`
|
||||
MaintenanceJournalSHA256 string `json:"maintenance_journal_sha256"`
|
||||
Writers []RestoreDispatchWriterDrain `json:"writers"`
|
||||
}
|
||||
|
||||
func (e RestoreDispatchActivationEvidence) Validate(now time.Time, maxAge time.Duration) error {
|
||||
if e.ProtocolVersion != RestoreDispatchActivationProtocolVersion {
|
||||
return fmt.Errorf("restore dispatch activation protocol version %d is not supported", e.ProtocolVersion)
|
||||
}
|
||||
// 兼容批次不同 = 派发语义不兼容:即使 display 相同也不能共用激活证据。
|
||||
if e.ProtocolEpoch != RestoreDispatchProtocolEpoch {
|
||||
return fmt.Errorf("restore dispatch activation protocol epoch %d is not compatible with %d",
|
||||
e.ProtocolEpoch, RestoreDispatchProtocolEpoch)
|
||||
}
|
||||
for name, value := range map[string]string{
|
||||
"target identity": e.TargetIdentity, "target fingerprint": e.TargetFingerprint,
|
||||
"restore target fingerprint": e.RestoreTargetFingerprint,
|
||||
"target instance": e.TargetInstance, "governance database": e.GovernanceDatabase,
|
||||
"protocol build": e.ProtocolBuild, "attested by": e.AttestedBy,
|
||||
"maintenance job": e.MaintenanceJob, "maintenance journal": e.MaintenanceJournal,
|
||||
"maintenance journal digest": e.MaintenanceJournalSHA256,
|
||||
} {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("restore dispatch activation evidence lacks %s", name)
|
||||
}
|
||||
}
|
||||
// 构建身份必须是完整 SHA;display 可以随提交数变化,不能作为身份。
|
||||
if !isFullSHA(e.ProtocolBuildSHA) {
|
||||
return errors.New("restore dispatch activation build identity is not a full 40-character commit sha")
|
||||
}
|
||||
// 治理权威域必须是可读回的 PG 事实形状:集群系统标识 + 库 OID,两个都是十进制数字。
|
||||
if !isDecimal(e.GovernanceAuthority.SystemIdentifier) || !isDecimal(e.GovernanceAuthority.DatabaseOID) {
|
||||
return errors.New("restore dispatch activation governance authority is not a cluster/database identity")
|
||||
}
|
||||
if !isHex64(e.TargetFingerprint) || !isHex64(e.RestoreTargetFingerprint) {
|
||||
return errors.New("restore dispatch activation target fingerprints are not sha256 hex strings")
|
||||
}
|
||||
if !isHex64(e.MaintenanceJournalSHA256) {
|
||||
return errors.New("restore dispatch activation maintenance journal digest is not a sha256 hex string")
|
||||
}
|
||||
if !validActivationID(e.MaintenanceJob) {
|
||||
return errors.New("restore dispatch activation maintenance job id is invalid")
|
||||
}
|
||||
// journal 只能按作业名定位在同一受信根目录里;带路径分隔符/相对引用会绕开
|
||||
// Store 配置的证据根,让"来源"变成调用方随手给的任意文件。
|
||||
if e.MaintenanceJournal != e.MaintenanceJob+".json" {
|
||||
return errors.New("restore dispatch activation maintenance journal is not the job journal")
|
||||
}
|
||||
if len(e.Writers) == 0 || len(e.Writers) > 256 {
|
||||
return errors.New("restore dispatch activation lists no controlled writers")
|
||||
}
|
||||
names := map[string]bool{}
|
||||
instances := map[string]bool{}
|
||||
for _, writer := range e.Writers {
|
||||
if strings.TrimSpace(writer.Name) == "" || strings.TrimSpace(writer.Kind) == "" ||
|
||||
strings.TrimSpace(writer.InstanceID) == "" {
|
||||
return errors.New("restore dispatch activation writer lacks name, kind or instance id")
|
||||
}
|
||||
if !validActivationID(writer.InstanceID) {
|
||||
return errors.New("restore dispatch activation writer instance id is invalid")
|
||||
}
|
||||
if names[writer.Name] {
|
||||
return fmt.Errorf("restore dispatch activation writer %s is listed twice", writer.Name)
|
||||
}
|
||||
names[writer.Name] = true
|
||||
if instances[writer.InstanceID] {
|
||||
return fmt.Errorf("restore dispatch activation writer instance %s is listed twice", writer.InstanceID)
|
||||
}
|
||||
instances[writer.InstanceID] = true
|
||||
}
|
||||
if e.AttestedAt.IsZero() || e.AttestedAt.After(now.Add(5*time.Minute)) {
|
||||
return errors.New("restore dispatch activation attestation time is not credible")
|
||||
}
|
||||
if maxAge <= 0 {
|
||||
maxAge = RestoreDispatchActivationMaxAge
|
||||
}
|
||||
if now.Sub(e.AttestedAt) > maxAge {
|
||||
return fmt.Errorf("restore dispatch activation evidence is older than %s", maxAge)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadRestoreDispatchActivationEvidence 读取并核验证据文件字节摘要。expectedDigest 必须
|
||||
// 与文件实际 sha256 相同;不接受"文件里写的摘要"。
|
||||
func LoadRestoreDispatchActivationEvidence(path, expectedDigest string, now time.Time, maxAge time.Duration) (RestoreDispatchActivationEvidence, error) {
|
||||
var evidence RestoreDispatchActivationEvidence
|
||||
if strings.TrimSpace(path) == "" || strings.TrimSpace(expectedDigest) == "" {
|
||||
return evidence, errors.New("restore dispatch activation requires an evidence file and its digest")
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return evidence, fmt.Errorf("read restore dispatch activation evidence: %w", err)
|
||||
}
|
||||
sum := sha256.Sum256(raw)
|
||||
if got := hex.EncodeToString(sum[:]); got != strings.ToLower(strings.TrimSpace(expectedDigest)) {
|
||||
return evidence, errors.New("restore dispatch activation evidence digest does not match the file")
|
||||
}
|
||||
decoder := json.NewDecoder(strings.NewReader(string(raw)))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&evidence); err != nil {
|
||||
return RestoreDispatchActivationEvidence{}, fmt.Errorf("parse restore dispatch activation evidence: %w", err)
|
||||
}
|
||||
if err := evidence.Validate(now, maxAge); err != nil {
|
||||
return RestoreDispatchActivationEvidence{}, err
|
||||
}
|
||||
return evidence, nil
|
||||
}
|
||||
|
||||
// CanonicalBytes 是写入台账的规范字节(同一份证据只有一种序列化)。
|
||||
func (e RestoreDispatchActivationEvidence) CanonicalBytes() ([]byte, error) {
|
||||
return json.Marshal(e)
|
||||
}
|
||||
|
||||
func isHex64(value string) bool {
|
||||
if len(value) != 64 || strings.ToLower(value) != value {
|
||||
return false
|
||||
}
|
||||
_, err := hex.DecodeString(value)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func isFullSHA(value string) bool {
|
||||
if len(value) != 40 || strings.ToLower(value) != value {
|
||||
return false
|
||||
}
|
||||
_, err := hex.DecodeString(value)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func isDecimal(value string) bool {
|
||||
if value == "" || len(value) > 20 {
|
||||
return false
|
||||
}
|
||||
for _, r := range value {
|
||||
if r < '0' || r > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// validActivationID 与维护 journal 的作业/服务 ID 同一字符集:证据里出现的身份
|
||||
// 必须能原样出现在 journal 中,不能靠空格/路径字符制造"看起来不同"的别名。
|
||||
func validActivationID(value string) bool {
|
||||
if len(value) == 0 || len(value) > 128 {
|
||||
return false
|
||||
}
|
||||
for _, r := range value {
|
||||
if !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '-' || r == '_') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user