why:审计 19 项(12 P1 / 7 P2)来自同一批根因,逐点试错会把协议撕成十几份:
· 意图与执行混为一谈(F01/F15):`library.sync:<scope>:<快照>` 是终身幂等键,
A→B→A 的第三次发布拿到第一次的历史成功作业,什么都不做;
· 版本行既是实体又是事件(F02/F09):主键只由 (kind,scope,快照) 决定、写入是
upsert,第二个作业覆盖 `source_job_id`/`evidence`/`created_at`;
· 接管没有代次(F13):两个 worker 都能认领同一 pending 作业,失租的旧执行还能
写业务状态、释放别人的维护锁;
· 早退与终态没有统一政策(F03/F04/F05/F06):半写窗口的前置检查失败会重新开门、
服务起不来也照样开入口、journal 终态后收尾不可重建、恢复成功不回写 current;
· 数据身份与准入各写一套(F07/F08/F14/F17):接管丢了复用语义、全库搬运不改
location_hash、通用入口可造 library.* 作业、目标模型从没被问过;
· 存储/容量/UI/CI 判据各自为政(F10/F11/F12/F16/F18/F19)。
what:按批准的一套协议集中实施(详见 handoffs/release-consolidated-implementation/
current-contract.md):
· operation(一次明确意图)/ attempt(epoch 代次)/ 不可变版本事件 / current 指针;
治理库迁移 76,只动治理库,不碰 WeKnora Schema/源码/镜像;
· 所有业务状态、阶段、版本事件、维护锁写入带 epoch 守卫(失租代次写不进去);
心跳失租立刻取消 handler;
· 半写窗口早退保持关闭;服务未全部健康绝不开入口;journal 终态可从持久事实重建
收尾;恢复成功登记 recovery 事件并推进 current;
· worker 在动维护前核对业务行/kind/scope/队列关联;通用入口拒绝 library.*;
· 全库搬运重算 location_hash 并与文档级导入共用实现;接管消费写集合的复用决定;
发布前核目标实时模型契约、容量门禁前置;
· 存储保护闭包改为全量依赖、类型只认显式登记、保护未知则不出可删候选;
· 页面核 HTTP 状态与结构、恢复纳入轮询、Wiki 差异覆盖标题/摘要/父级。
how:`make verify` rc=0(16 项必需检查全绿,含 go test 全量与前端 485 例);
`go vet ./...` rc=0;`npx tsc -p tsconfig.json --noEmit` rc=0;逐项负例见各包测试
(F03/F04/F08/F12/F13/F14/F16/F18/F19 都有先红后绿记录)。
未执行:治理库迁移到任何已有库、真实 PG/ES/S3/WeKnora 集成、故障注入、部署。
This commit is contained in:
@@ -219,6 +219,7 @@ func Run(ctx context.Context, request Request, target Target, backups BackupStor
|
||||
|
||||
// 写集合必须在第一笔写入之前落盘并登记:恢复只按"这次明确写了什么"清理,
|
||||
// 不按目标现状推断残渣(那会把历史孤儿/共享资源当成本次残留删掉)。
|
||||
existingPlan := backup.WriteSet != nil
|
||||
if backup.WriteSet == nil {
|
||||
if planner, ok := target.(WriteSetPlanner); ok {
|
||||
set, planErr := planner.PlanWriteSet(ctx, WriteSetPlan{JobID: request.JobID, Target: request.Target,
|
||||
@@ -244,6 +245,26 @@ func Run(ctx context.Context, request Request, target Target, backups BackupStor
|
||||
}
|
||||
}
|
||||
|
||||
// 计划已经存在(接管/恢复)时,执行目标必须**消费**它:把"这些资源已证明可复用"
|
||||
// 的决定装进去,写入阶段才会跳过共享对象。不能重新计划——那时目标已被写过,
|
||||
// 重新计划等于把半写现状当新基线(F07)。
|
||||
if existingPlan {
|
||||
if adopter, ok := target.(WriteSetAdopter); ok {
|
||||
if err := adopter.AdoptWriteSet(*backup.WriteSet); err != nil {
|
||||
outcome.State = "failed"
|
||||
outcome.Error = "无法接管已持久化的写集合,拒绝继续写目标:" + err.Error()
|
||||
recorder.Stage(ctx, "prebackup", "failed", outcome.Error)
|
||||
return outcome
|
||||
}
|
||||
} else if _, plans := target.(WriteSetPlanner); plans {
|
||||
// 会写资源、却读不懂计划:继续写就会重写共享行/对象。
|
||||
outcome.State = "failed"
|
||||
outcome.Error = "目标实现无法消费已持久化的写集合,拒绝在失去复用语义的情况下写目标"
|
||||
recorder.Stage(ctx, "prebackup", "failed", outcome.Error)
|
||||
return outcome
|
||||
}
|
||||
}
|
||||
|
||||
for _, stage := range Stages() {
|
||||
recorder.Stage(ctx, string(stage), "running", "")
|
||||
if err = target.Apply(ctx, stage, request.SnapshotID); err != nil {
|
||||
|
||||
@@ -259,6 +259,19 @@ type writeSetPlanTarget struct {
|
||||
events *[]string
|
||||
ledger *WriteSet
|
||||
restores []BackupRef
|
||||
// adopted 记录"接管时消费了哪份已持久化写集合"(F07):adoptErr 表达"读不懂计划"。
|
||||
adopted []WriteSet
|
||||
adoptErr error
|
||||
}
|
||||
|
||||
// AdoptWriteSet 让这个假目标具备"消费既有计划"的能力:真实实现按它把复用决定装回去。
|
||||
func (p *writeSetPlanTarget) AdoptWriteSet(set WriteSet) error {
|
||||
*p.events = append(*p.events, "adopt")
|
||||
if p.adoptErr != nil {
|
||||
return p.adoptErr
|
||||
}
|
||||
p.adopted = append(p.adopted, set)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *writeSetPlanTarget) PlanWriteSet(_ context.Context, plan WriteSetPlan) (WriteSet, error) {
|
||||
@@ -400,6 +413,13 @@ func TestRunResumeUsesPersistedWriteSet(t *testing.T) {
|
||||
if len(target.plans) != 0 {
|
||||
t.Fatalf("resume must not re-plan the write set: %+v", target.plans)
|
||||
}
|
||||
// F07:既有计划必须被执行目标消费——否则"复用"只停留在账本上,Apply 仍会重写共享对象。
|
||||
if len(target.adopted) != 1 {
|
||||
t.Fatalf("接管必须把已持久化的写集合交给执行目标:adopted=%d", len(target.adopted))
|
||||
}
|
||||
if target.adopted[0].BackupSnapshotID != "backup-snap" {
|
||||
t.Fatalf("消费的写集合绑定了错误的预备份:%+v", target.adopted[0].BackupSnapshotID)
|
||||
}
|
||||
if len(target.restores) != 1 || target.restores[0].WriteSet == nil {
|
||||
t.Fatalf("restore must carry the persisted write set: %+v", target.restores)
|
||||
}
|
||||
@@ -409,3 +429,28 @@ func TestRunResumeUsesPersistedWriteSet(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// F07 反向:会写资源、却读不懂既有计划的执行目标必须**在被写之前**失败。
|
||||
// 继续写就等于"账本说没动共享对象,实际又 upsert + 重传了一遍"。
|
||||
func TestRunRefusesPlanBlindTargetOnResume(t *testing.T) {
|
||||
_, recorder, _, request, _ := plannedRun(t)
|
||||
blind := &blindPlanTarget{fakeTarget: &fakeTarget{}}
|
||||
resumed := &fakeBackups{ref: BackupRef{ID: "backup-1", SnapshotID: "backup-snap",
|
||||
WriteSet: &WriteSet{Version: WriteSetVersion, JobID: request.JobID, Target: request.Target,
|
||||
TenantID: 10001, KnowledgeBaseID: "kb", BackupSnapshotID: "backup-snap",
|
||||
TargetSnapshotID: request.SnapshotID, CreatedAt: time.Now().UTC()}}}
|
||||
outcome := Run(context.Background(), request, blind, resumed, recorder)
|
||||
if outcome.State != "failed" {
|
||||
t.Fatalf("读不懂计划的执行目标必须失败:%+v", outcome)
|
||||
}
|
||||
if len(blind.applied) != 0 {
|
||||
t.Fatalf("失去复用语义时不许写任何阶段:%v", blind.applied)
|
||||
}
|
||||
}
|
||||
|
||||
// blindPlanTarget 会规划写集合(因此会写资源),但没有 AdoptWriteSet。
|
||||
type blindPlanTarget struct{ *fakeTarget }
|
||||
|
||||
func (b *blindPlanTarget) PlanWriteSet(context.Context, WriteSetPlan) (WriteSet, error) {
|
||||
return WriteSet{Version: WriteSetVersion}, nil
|
||||
}
|
||||
|
||||
@@ -247,6 +247,16 @@ type WriteSetPlanner interface {
|
||||
PlanWriteSet(ctx context.Context, plan WriteSetPlan) (WriteSet, error)
|
||||
}
|
||||
|
||||
// WriteSetAdopter 让"计划已经存在"的执行也能带上复用语义(F07)。
|
||||
//
|
||||
// 触发场景:首次 planner 证明某些 resources 可复用 → 写集合落盘 → 进程中断 → 接管时用
|
||||
// **新进程/新 target** 继续。写集合已存在,planner 因此被跳过;如果执行目标不消费这份
|
||||
// 计划,它会按"没有复用"重新 upsert 资源行并重传对象——账本说"共享对象没动",实际却
|
||||
// 重写了共享对象。必须显式接管计划,而不是重新按半写现状计划一遍。
|
||||
type WriteSetAdopter interface {
|
||||
AdoptWriteSet(set WriteSet) error
|
||||
}
|
||||
|
||||
// WriteSetObserver 让调用方在写第一笔之前把写集合摘要持久化到治理库。
|
||||
// 失败同样意味着"一笔都不许写":没有可追溯的写集合,恢复就只能靠推断。
|
||||
type WriteSetObserver interface {
|
||||
|
||||
Reference in New Issue
Block a user