fix(worker): 写集合预检经包装层转发,缺失即拒写

orchestrator 用 target.(WriteSetPlanner) 决定要不要在写第一笔之前算写集合,
而 contentTrackedTarget 只转发了 Current/Apply/Verify/Restore,断言恒为假,
台账既不落盘也不登记。真实现场:10-29 两次真作业的预备份证据里没有
writeset_digest,归档根下没有 writesets/,崩溃接管必然无台账可读。

现在包装层显式转发;内层不支持预检时返回硬错误,不退回"没有台账也照写"。
用例断言包装后仍实现 WriteSetPlanner,并覆盖"内层不支持"的硬错误。
This commit is contained in:
姚章浩
2026-10-30 01:33:40 +08:00
parent a0a7a26639
commit f0f5571518
2 changed files with 65 additions and 0 deletions
+16
View File
@@ -56,6 +56,22 @@ func (t *contentTrackedTarget) Restore(ctx context.Context, backup libraryreleas
return t.inner.Restore(ctx, backup)
}
// PlanWriteSet 必须显式转发:orchestrator 是按 `target.(WriteSetPlanner)` 这个
// 类型断言决定要不要在写第一笔之前算写集合的,包一层之后断言就失败了——于是
// 台账既不落盘也不登记,恢复/接管时根本没有"这次明确写了什么"可读。
// 真实后果:2026-10-29 的两次真作业的预备份证据里都没有 writeset_digest,
// 归档根下也没有 writesets/;一旦 worker 崩在写入中间,接管必然失败。
//
// 内层不支持预检时返回硬错误,不退回"没有台账也照写":那会让恢复只能按目标
// 现状推断残渣,从而误删历史孤儿资源。
func (t *contentTrackedTarget) PlanWriteSet(ctx context.Context, plan libraryrelease.WriteSetPlan) (libraryrelease.WriteSet, error) {
planner, ok := t.inner.(libraryrelease.WriteSetPlanner)
if !ok {
return libraryrelease.WriteSet{}, errors.New("目标实现不支持写集合预检:拒绝在没有可追溯写集合的情况下写生产")
}
return planner.PlanWriteSet(ctx, plan)
}
// libraryReleasePayload 是同步与恢复共用的队列载荷形状。
type libraryReleasePayload struct {
LibraryJobID string `json:"library_job_id"`
+49
View File
@@ -5,6 +5,7 @@ import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
@@ -147,3 +148,51 @@ func TestLedgerBackupStoreRefusesWithoutWriteSet(t *testing.T) {
t.Fatalf("接管必须带上持久化的写集合:%+v", ref)
}
}
// 包装层必须把"写集合预检"这件事透出去:orchestrator 用的是类型断言,
// 少一个方法就等于静默跳过台账(真实后果:真作业没有 writeset_digest、也没有
// writesets/ 文件,崩溃接管无台账可读)。
type plainTarget struct{ applied int }
func (t *plainTarget) Current(context.Context) (libraryrelease.CurrentState, error) {
return libraryrelease.CurrentState{}, nil
}
func (t *plainTarget) Apply(context.Context, libraryrelease.Stage, string) error {
t.applied++
return nil
}
func (t *plainTarget) Verify(context.Context, libraryrelease.Expectation) error { return nil }
func (t *plainTarget) Restore(context.Context, libraryrelease.BackupRef) error { return nil }
type planningTarget struct {
plainTarget
planned int
}
func (t *planningTarget) PlanWriteSet(context.Context, libraryrelease.WriteSetPlan) (libraryrelease.WriteSet, error) {
t.planned++
return libraryrelease.WriteSet{Version: libraryrelease.WriteSetVersion, JobID: "job-1", Target: "internal"}, nil
}
func TestContentTrackedTargetForwardsWriteSetPlanning(t *testing.T) {
inner := &planningTarget{}
wrapped := &contentTrackedTarget{inner: inner, onApply: func() {}}
planner, ok := libraryrelease.Target(wrapped).(libraryrelease.WriteSetPlanner)
if !ok {
t.Fatal("包装后的目标必须仍然实现 WriteSetPlanner,否则写集合会被静默跳过")
}
if _, err := planner.PlanWriteSet(context.Background(), libraryrelease.WriteSetPlan{JobID: "job-1", Target: "internal"}); err != nil {
t.Fatalf("plan: %v", err)
}
if inner.planned != 1 {
t.Fatalf("预检必须落到内层目标,planned=%d", inner.planned)
}
// 内层不支持预检 ⇒ 硬错误,不许"没有台账也照写"。
strict := &contentTrackedTarget{inner: &plainTarget{}, onApply: func() {}}
if _, err := strict.PlanWriteSet(context.Background(), libraryrelease.WriteSetPlan{}); err == nil {
t.Fatal("内层不支持写集合预检时必须报错")
} else if !strings.Contains(err.Error(), "写集合") {
t.Fatalf("错误必须点名写集合:%v", err)
}
}