fix(library): 收紧 DS01 派发租约/投递/治理身份与绑定冲突
F2.a:Acquire/Confirm 在取得 effect 行锁后用数据库时钟重核 job/attempt/ 恢复投递租约,等锁跨过到期零许可且状态保持 prepared。 F2.b:同一 CAS 事务锁 recovery queue 与 operation,复核 window/scope、 idempotency、owner/attempt/lease,并按原 window 锚点重算恢复请求摘要。 F3.identity:激活证据 v3 携带 PG 集群 system_identifier + 库 OID、完整构建 SHA 与协议批次;激活与使用点都现场重读并拒绝同名异域/副本/unknown/不兼容 SHA。 F4.binding:区分绑定冲突与仅禁止重发的 Held,准入/确认/续作/收尾在所有状态 拒绝冲突,合法 dispatching + 精确 marker 仍可确认。
This commit is contained in:
@@ -434,9 +434,16 @@ FROM library_instance_maintenance WHERE instance=$1`, cfg.LibraryTargetInstance)
|
||||
if expected := os.Getenv("KG_R38_VERSION"); expected != "" && build != expected {
|
||||
return "", nil, fmt.Errorf("test binary build %q != runner identity %q", build, expected)
|
||||
}
|
||||
var database string
|
||||
if err := gov.QueryRow(ctx, `SELECT current_database()`).Scan(&database); err != nil {
|
||||
return "", nil, fmt.Errorf("read governance database: %w", err)
|
||||
var database, systemIdentifier, databaseOID string
|
||||
if err := gov.QueryRow(ctx, `
|
||||
SELECT current_database(),
|
||||
(SELECT system_identifier::text FROM pg_control_system()),
|
||||
(SELECT oid::text FROM pg_database WHERE datname = current_database())`).Scan(
|
||||
&database, &systemIdentifier, &databaseOID); err != nil {
|
||||
return "", nil, fmt.Errorf("read governance authority: %w", err)
|
||||
}
|
||||
if expected := os.Getenv("KG_R38_COMMIT"); expected != "" && version.CommitSHA != expected {
|
||||
return "", nil, fmt.Errorf("test binary commit %q != runner identity %q", version.CommitSHA, expected)
|
||||
}
|
||||
journalSum := sha256.Sum256(rawJournal)
|
||||
// 激活后同一窗口还会继续写 journal(recovered 等);把激活那一刻的字节原样留档,
|
||||
@@ -447,8 +454,12 @@ FROM library_instance_maintenance WHERE instance=$1`, cfg.LibraryTargetInstance)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
evidence := libraryrelease.RestoreDispatchActivationEvidence{
|
||||
ProtocolVersion: 2, TargetIdentity: cfg.LibraryTargetIdentity(), TargetFingerprint: windowFingerprint,
|
||||
RestoreTargetFingerprint: fingerprint, TargetInstance: cfg.LibraryTargetInstance, GovernanceDatabase: database, ProtocolBuild: build,
|
||||
ProtocolVersion: libraryrelease.RestoreDispatchActivationProtocolVersion,
|
||||
ProtocolEpoch: libraryrelease.RestoreDispatchProtocolEpoch,
|
||||
TargetIdentity: cfg.LibraryTargetIdentity(), TargetFingerprint: windowFingerprint,
|
||||
RestoreTargetFingerprint: fingerprint, TargetInstance: cfg.LibraryTargetInstance, GovernanceDatabase: database,
|
||||
GovernanceAuthority: libraryrelease.RestoreDispatchGovernanceAuthority{SystemIdentifier: systemIdentifier, DatabaseOID: databaseOID},
|
||||
ProtocolBuild: build, ProtocolBuildSHA: version.CommitSHA,
|
||||
AttestedBy: "r38-runner", AttestedAt: now,
|
||||
MaintenanceJob: job.ID, MaintenanceJournal: job.ID + ".json",
|
||||
MaintenanceJournalSHA256: hex.EncodeToString(journalSum[:]), Writers: writers,
|
||||
@@ -480,7 +491,10 @@ FROM library_index_restore_dispatch_activation WHERE target_identity=$1`, cfg.Li
|
||||
facts := map[string]any{
|
||||
"target": target, "fingerprint": fingerprint, "window_fingerprint": windowFingerprint,
|
||||
"governance_database": database,
|
||||
"maintenance_job": job.ID, "protocol_build": build,
|
||||
"governance_authority": map[string]string{"system_identifier": systemIdentifier, "database_oid": databaseOID,
|
||||
"primary": "true"},
|
||||
"protocol_build_sha": version.CommitSHA,
|
||||
"maintenance_job": job.ID, "protocol_build": build,
|
||||
"journal_sha256": drainDigest, "journal_snapshot": filepath.Base(journalSnapshot),
|
||||
"activation_evidence_sha256": hex.EncodeToString(evidenceSum[:]),
|
||||
"journal_state": journal["state"], "gate": gate, "quiesce": quiesce,
|
||||
|
||||
@@ -16,6 +16,7 @@ type ds01StubGate struct {
|
||||
inspectStatus libraryrelease.RestoreDispatchStatus
|
||||
inspectFound bool
|
||||
records []libraryrelease.RestoreDispatchRecord
|
||||
confirmed int
|
||||
}
|
||||
|
||||
func (g *ds01StubGate) InspectRestoreMarker(context.Context, libraryrelease.RestoreMarkerBinding) (libraryrelease.RestoreDispatchStatus, bool, error) {
|
||||
@@ -28,6 +29,7 @@ func (g *ds01StubGate) AcquireRestoreMarker(context.Context, libraryrelease.Rest
|
||||
return nil, g.inspectStatus, libraryrelease.ErrRestoreDispatchHeld
|
||||
}
|
||||
func (g *ds01StubGate) ConfirmRestoreMarker(context.Context, libraryrelease.RestoreMarkerBinding, []byte) error {
|
||||
g.confirmed++
|
||||
return nil
|
||||
}
|
||||
func (g *ds01StubGate) UnresolvedRestoreDispatches(context.Context, string, string, string) ([]libraryrelease.RestoreDispatchRecord, error) {
|
||||
@@ -127,6 +129,102 @@ func TestDS01BindingConflictHeldBeforeAnyWrite(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// DS01-F4.binding:绑定冲突是**类型化**分类——prepared/dispatching/marker_confirmed/
|
||||
// done 任一状态、准入/确认/收尾任一入口都必须拒绝;合法 dispatching + 精确 marker
|
||||
// (不冲突)仍允许确认,不能用"拒绝全部 Held"掩盖。
|
||||
func TestDS01BindingConflictRejectedInEveryState(t *testing.T) {
|
||||
fx, snapshot, target, backups, wire := ds01Fixture(t)
|
||||
authority := newDS01Authority(t, requestFor(snapshot).JobID, snapshot)
|
||||
authority.attach(target)
|
||||
outcome := ds01RunToBoundary(t, fx, snapshot, target, backups, wire)
|
||||
ledger := outcome.Backup.WriteSet
|
||||
if err := target.AdoptWriteSet(*ledger); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ds01FreezeBindingInputs(t, target, outcome.Backup)
|
||||
change := indexChangeOf(t, ledger, "chunk-target")
|
||||
body, err := target.restoreIndexClaimBody(change)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 在目标上写入精确 marker:这样 dispatching 状态本来是可确认的,冲突检查必须
|
||||
// 独立地拒绝它,而不是靠"marker 不在"。
|
||||
applied, err := target.loadIndexEffect(change, "applied")
|
||||
if err != nil || applied == nil || applied.Receipt == nil {
|
||||
t.Fatalf("applied receipt missing: %v", err)
|
||||
}
|
||||
method, path, payload, err := target.indexMutation(change, body, applied.Receipt.SeqNo, applied.Receipt.PrimaryTerm, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var marker indexReceipt
|
||||
if err := target.search.requestJSON(fx.ctx, method, path, payload, &marker); err != nil {
|
||||
t.Fatalf("exact marker write: %v", err)
|
||||
}
|
||||
observed, err := target.observePlannedIndex(fx.ctx, change)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conflict := func(state string) *ds01StubGate {
|
||||
return &ds01StubGate{inspectFound: true, inspectStatus: libraryrelease.RestoreDispatchStatus{
|
||||
State: state, Held: true, BindingConflict: true, Reason: "binding differs"},
|
||||
records: []libraryrelease.RestoreDispatchRecord{{PlanDigest: ledger.Digest, Index: change.Index,
|
||||
DocumentID: change.ID, State: libraryrelease.RestoreDispatchMarkerConfirmed}}}
|
||||
}
|
||||
for _, state := range []string{
|
||||
libraryrelease.RestoreDispatchPrepared,
|
||||
libraryrelease.RestoreDispatchDispatching,
|
||||
libraryrelease.RestoreDispatchMarkerConfirmed,
|
||||
} {
|
||||
gate := conflict(state)
|
||||
target.config.RestoreDispatch = gate
|
||||
if err := target.admitRestorePlan(fx.ctx); err == nil || !strings.Contains(err.Error(), "冲突") {
|
||||
t.Fatalf("admission accepted a binding conflict in %s: %v", state, err)
|
||||
}
|
||||
if err := target.confirmDispatchedMarker(fx.ctx, change, body, observed); err == nil || !strings.Contains(err.Error(), "冲突") {
|
||||
t.Fatalf("confirmation accepted a binding conflict in %s: %v", state, err)
|
||||
}
|
||||
if gate.confirmed != 0 {
|
||||
t.Fatalf("binding conflict in %s still recorded a confirmation", state)
|
||||
}
|
||||
}
|
||||
|
||||
// done 状态:restored 文件已在,准入仍必须因绑定冲突拒绝(旧实现只看状态)。
|
||||
receipt := indexReceipt{Index: change.Index, ID: change.ID, Result: "created",
|
||||
SeqNo: marker.SeqNo, PrimaryTerm: marker.PrimaryTerm}
|
||||
if err := target.saveIndexEffect(change, "restored", &receipt); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target.config.RestoreDispatch = conflict(libraryrelease.RestoreDispatchMarkerConfirmed)
|
||||
if err := target.admitRestorePlan(fx.ctx); err == nil || !strings.Contains(err.Error(), "冲突") {
|
||||
t.Fatalf("admission accepted a binding conflict on a completed effect: %v", err)
|
||||
}
|
||||
// 收尾:记录状态看起来是 marker_confirmed、restored 也存在,但绑定冲突必须拒绝。
|
||||
gate := conflict(libraryrelease.RestoreDispatchMarkerConfirmed)
|
||||
target.config.RestoreDispatch = gate
|
||||
if err := target.verifyRestoreEffectsClosed(fx.ctx); err == nil || !strings.Contains(err.Error(), "冲突") {
|
||||
t.Fatalf("closeout accepted a binding conflict: %v", err)
|
||||
}
|
||||
if gate.confirmed != 0 {
|
||||
t.Fatal("closeout recorded a confirmation for a conflicting binding")
|
||||
}
|
||||
|
||||
// 正例:合法 dispatching + 精确 marker(不冲突)仍可确认,且只确认一次。
|
||||
okGate := &ds01StubGate{inspectFound: true,
|
||||
inspectStatus: libraryrelease.RestoreDispatchStatus{State: libraryrelease.RestoreDispatchDispatching, Held: true,
|
||||
Reason: "dispatch outcome unknown: no further create is permitted"}}
|
||||
target.config.RestoreDispatch = okGate
|
||||
if err := target.confirmDispatchedMarker(fx.ctx, change, body, observed); err != nil {
|
||||
t.Fatalf("legitimate dispatching marker confirmation: %v", err)
|
||||
}
|
||||
if okGate.confirmed != 1 {
|
||||
t.Fatalf("legitimate marker confirmation count = %d", okGate.confirmed)
|
||||
}
|
||||
if wire.dispatch != 0 {
|
||||
t.Fatalf("confirmation path forwarded %d marker writes", wire.dispatch)
|
||||
}
|
||||
}
|
||||
|
||||
// DS01-F4:prepared 台账却已在目标上出现精确 marker —— 证据矛盾,必须 held;
|
||||
// 旧实现会把它当成"已存在的标记"认领并 clean。
|
||||
func TestDS01PreparedWithMarkerIsContradictionHeld(t *testing.T) {
|
||||
|
||||
@@ -549,8 +549,12 @@ func TestDS01RestoreHelper(t *testing.T) {
|
||||
// 测试替身只负责造这两个真实来源,不替产品做判断。
|
||||
func activateDS01(ctx context.Context, store *postgresstore.Store, pool *pgxpool.Pool,
|
||||
targetIdentity, fingerprint string) error {
|
||||
var database, job string
|
||||
if err := pool.QueryRow(ctx, `SELECT current_database()`).Scan(&database); err != nil {
|
||||
var database, systemIdentifier, databaseOID, job string
|
||||
if err := pool.QueryRow(ctx, `
|
||||
SELECT current_database(),
|
||||
(SELECT system_identifier::text FROM pg_control_system()),
|
||||
(SELECT oid::text FROM pg_database WHERE datname = current_database())`).Scan(
|
||||
&database, &systemIdentifier, &databaseOID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT owner_job_id FROM library_instance_maintenance WHERE instance=$1`,
|
||||
@@ -599,8 +603,12 @@ WHERE instance=$1`, ds01TargetInstance, `["worker-ds01"]`, fingerprint); err !=
|
||||
return err
|
||||
}
|
||||
evidence := libraryrelease.RestoreDispatchActivationEvidence{
|
||||
ProtocolVersion: 2, TargetIdentity: targetIdentity, TargetFingerprint: fingerprint,
|
||||
RestoreTargetFingerprint: fingerprint, TargetInstance: ds01TargetInstance, GovernanceDatabase: database, ProtocolBuild: version.DisplayVersion(),
|
||||
ProtocolVersion: libraryrelease.RestoreDispatchActivationProtocolVersion,
|
||||
ProtocolEpoch: libraryrelease.RestoreDispatchProtocolEpoch,
|
||||
TargetIdentity: targetIdentity, TargetFingerprint: fingerprint,
|
||||
RestoreTargetFingerprint: fingerprint, TargetInstance: ds01TargetInstance, GovernanceDatabase: database,
|
||||
GovernanceAuthority: libraryrelease.RestoreDispatchGovernanceAuthority{SystemIdentifier: systemIdentifier, DatabaseOID: databaseOID},
|
||||
ProtocolBuild: version.DisplayVersion(), ProtocolBuildSHA: version.CommitSHA,
|
||||
AttestedBy: "ds01-test", AttestedAt: now,
|
||||
MaintenanceJob: job, MaintenanceJournal: job + ".json",
|
||||
MaintenanceJournalSHA256: hex.EncodeToString(journalSum[:]),
|
||||
|
||||
@@ -137,6 +137,9 @@ func (t *ProductionLibraryTarget) restoreClaimedIndexBefore(ctx context.Context,
|
||||
if !found {
|
||||
return fmt.Errorf("%w: 既存意图没有派发台账(旧协议未知历史)", libraryrelease.ErrRestoreDispatchHeld)
|
||||
}
|
||||
if status.BindingConflict {
|
||||
return heldRestoreMarker(status, "既存意图的派发台账绑定与本次计划冲突")
|
||||
}
|
||||
if status.State == libraryrelease.RestoreDispatchPrepared && claimed != nil {
|
||||
return heldRestoreMarker(status, "prepared 台账却已有 claimed 证据:协议证据矛盾")
|
||||
}
|
||||
|
||||
@@ -161,6 +161,12 @@ func (t *ProductionLibraryTarget) admitRestorePlan(ctx context.Context) error {
|
||||
return fmt.Errorf("恢复准入读不到派发台账:%w", inspErr)
|
||||
}
|
||||
if found {
|
||||
// 绑定冲突是类型化分类:任何状态(含已完成 clean 的终态)都必须拒绝。
|
||||
// 它不能与"仅禁止第二次 create"的 dispatching-held 混为一谈——后者在
|
||||
// 精确 marker 仍在时是允许确认的。
|
||||
if status.BindingConflict {
|
||||
return heldRestoreMarker(status, "派发台账的不可变绑定与本次计划冲突")
|
||||
}
|
||||
if done != nil {
|
||||
if status.State != libraryrelease.RestoreDispatchMarkerConfirmed {
|
||||
return heldRestoreMarker(status, "已完成 clean 的 effect 台账却不是 marker_confirmed")
|
||||
@@ -241,6 +247,9 @@ func (t *ProductionLibraryTarget) confirmDispatchedMarker(ctx context.Context, c
|
||||
if !found {
|
||||
return fmt.Errorf("%w: 删除型恢复缺少派发台账,拒绝按外观继续", libraryrelease.ErrRestoreDispatchHeld)
|
||||
}
|
||||
if status.BindingConflict {
|
||||
return heldRestoreMarker(status, "派发台账的不可变绑定与本次计划冲突")
|
||||
}
|
||||
if status.Held && status.State == libraryrelease.RestoreDispatchPrepared {
|
||||
return heldRestoreMarker(status, "派发绑定与不可变计划冲突")
|
||||
}
|
||||
@@ -370,6 +379,28 @@ func (t *ProductionLibraryTarget) verifyRestoreEffectsClosed(ctx context.Context
|
||||
if done == nil {
|
||||
return fmt.Errorf("%w: 收尾前仍有未 clean 的 effect %s", libraryrelease.ErrRestoreDispatchHeld, c.ID)
|
||||
}
|
||||
// 收尾同样是**使用点**:绑定冲突即使状态看起来是 marker_confirmed 也必须拒绝。
|
||||
body, err := t.restoreIndexClaimBody(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
binding, err := t.restoreMarkerBinding(ctx, c, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
status, found, err := t.config.RestoreDispatch.InspectRestoreMarker(ctx, binding)
|
||||
if err != nil {
|
||||
return fmt.Errorf("恢复收尾读不到派发台账:%w", err)
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf("%w: 收尾时 effect %s 的派发台账消失了", libraryrelease.ErrRestoreDispatchHeld, c.ID)
|
||||
}
|
||||
if status.BindingConflict {
|
||||
return heldRestoreMarker(status, "收尾时派发台账的不可变绑定与本次计划冲突")
|
||||
}
|
||||
if status.State != libraryrelease.RestoreDispatchMarkerConfirmed {
|
||||
return fmt.Errorf("%w: 收尾时 effect %s 的派发状态是 %s", libraryrelease.ErrRestoreDispatchHeld, c.ID, status.State)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, c := range t.activePlan.Index {
|
||||
|
||||
Reference in New Issue
Block a user