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:
@@ -126,10 +126,17 @@ func (b RestoreMarkerBinding) Digest() (string, error) {
|
||||
}
|
||||
|
||||
// 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"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
State string `json:"state"`
|
||||
Held bool `json:"held"`
|
||||
BindingConflict bool `json:"binding_conflict,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// RestoreSendPermit 是"这一次调用、这一个 effect、这一次发送"的许可。
|
||||
|
||||
@@ -29,7 +29,24 @@ import (
|
||||
|
||||
const RestoreDispatchActivationMaxAge = 2 * time.Hour
|
||||
|
||||
const RestoreDispatchActivationProtocolVersion = 2
|
||||
// 证据文档 schema 版本与语义兼容批次是两件事:
|
||||
// - ProtocolVersion:这份 JSON 的字段形状(解析契约);
|
||||
// - ProtocolEpoch:派发协议语义批次;只有同一批次的构建才允许共享激活证据
|
||||
// (例如同一批次的兼容重建/打包),不同批次必须重新登记。
|
||||
//
|
||||
// 构建身份用完整 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)。
|
||||
@@ -41,27 +58,35 @@ type RestoreDispatchWriterDrain struct {
|
||||
|
||||
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"`
|
||||
ProtocolBuild string `json:"protocol_build"`
|
||||
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"`
|
||||
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,
|
||||
@@ -74,6 +99,14 @@ func (e RestoreDispatchActivationEvidence) Validate(now time.Time, maxAge time.D
|
||||
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")
|
||||
}
|
||||
@@ -161,6 +194,26 @@ func isHex64(value string) bool {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user