state

package
v0.1.24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package state 持久化 Runtime 操作状态与生命周期状态。

Index

Constants

View Source
const SchemaVersion = 1

SchemaVersion 是首版状态文件唯一支持的 schema。

Variables

View Source
var ErrClosed = errors.New("state store is closed")

ErrClosed 表示 Store 已经关闭文件能力。

View Source
var ErrCorrupt = errors.New("state file is corrupt")

ErrCorrupt 表示状态文件内容损坏或违反 v1 schema。

View Source
var ErrInvalidPID = errors.New("pid is invalid")

ErrInvalidPID 表示 PID 不能用于 Windows 进程查询。

View Source
var ErrNotFound = errors.New("state file not found")

ErrNotFound 表示固定状态文件不存在。

View Source
var ErrTransactionChanged = errors.New("state transaction changed")

ErrTransactionChanged 表示条件删除前事务文件身份或内容已经变化。

View Source
var ErrUnsupportedSchema = errors.New("state schema is unsupported")

ErrUnsupportedSchema 表示状态文件使用当前 Runtime 不支持的 schema。

Functions

func ValidateTransaction

func ValidateTransaction(kind TransactionKind, value TransactionState) error

ValidateTransaction 对内存输入和磁盘解码值执行同一语义规则。

Types

type Activity

type Activity string

Activity 标识事务状态文件与真实占用的分类结果。

const (
	// ActivityActive 表示权威 Mutex 当前被持有。
	ActivityActive Activity = "active"
	// ActivityStale 表示 Mutex free 且记录的 PID 已退出。
	ActivityStale Activity = "stale"
	// ActivityInconsistent 表示 Mutex free 但相同数字的 PID 仍存活。
	ActivityInconsistent Activity = "inconsistent"
	// ActivityUnknown 表示输入或任一必要探针无法确认。
	ActivityUnknown Activity = "unknown"
)

func (Activity) String

func (a Activity) String() string

String 返回稳定字面量。

func (Activity) Valid

func (a Activity) Valid() bool

Valid 报告 activity 是否属于冻结集合。

type BrokenEnvironment

type BrokenEnvironment struct {
	TargetVersion string         `json:"targetVersion"`
	Branch        string         `json:"branch"`
	Commit        string         `json:"commit"`
	PythonVersion string         `json:"pythonVersion"`
	UVVersion     string         `json:"uvVersion"`
	Reason        BrokenReason   `json:"reason"`
	Stage         protocol.Stage `json:"stage"`
	ExitCode      int            `json:"exitCode"`
	LogPath       string         `json:"logPath"`
}

BrokenEnvironment 保存当前 active revision 无法启动主环境的原因。

type BrokenReason

type BrokenReason string

BrokenReason 标识环境失效的稳定原因。

const (
	// ReasonRepositoryChanged 表示 active repo 已替换或受管文件正在恢复,主环境尚未重新证明可用。
	ReasonRepositoryChanged BrokenReason = "repository_changed"
	// ReasonOperationFailed 表示改变主环境后的具体叶子操作失败。
	ReasonOperationFailed BrokenReason = "operation_failed"
)

func (BrokenReason) String

func (r BrokenReason) String() string

String 返回稳定字面量。

func (BrokenReason) Valid

func (r BrokenReason) Valid() bool

Valid 报告 reason 是否属于冻结集合。

type CorruptError

type CorruptError struct {
	File  string
	Cause error
}

CorruptError 表示磁盘状态内容不满足严格 schema。

func (*CorruptError) Error

func (e *CorruptError) Error() string

func (*CorruptError) Is

func (e *CorruptError) Is(target error) bool

Is 让 CorruptError 匹配 ErrCorrupt。

func (*CorruptError) Unwrap

func (e *CorruptError) Unwrap() error

Unwrap 保留损坏分类下的稳定原因。

type EnvironmentState

type EnvironmentState struct {
	SchemaVersion  int                  `json:"schemaVersion"`
	Status         protocol.StateStatus `json:"status"`
	UpdatedAt      time.Time            `json:"updatedAt"`
	LastSuccessful Revision             `json:"lastSuccessful"`
	Broken         *BrokenEnvironment   `json:"broken"`
}

EnvironmentState 是跨 Runtime 进程持久化的稳定环境真相。

type Inspection

type Inspection struct {
	Activity       Activity
	MutexHeld      bool
	MutexRecovered bool
	PIDChecked     bool
	PIDAlive       bool
	ProbeError     error
}

Inspection 是事务文件与 Mutex/PID 事实的失败关闭分类。

func InspectTransaction

func InspectTransaction(
	ctx context.Context,
	kind TransactionKind,
	value TransactionState,
	pidProbe PIDProbe,
	mutexProbe MutexProbe,
) Inspection

InspectTransaction 先查询权威 Mutex,只在 free 时查询 PID。

func (Inspection) CanAutoClean

func (i Inspection) CanAutoClean() bool

CanAutoClean 只报告 stale 候选;调用方仍须持 Lease 重读并条件删除。

type MutexKind

type MutexKind string

MutexKind 是 state 消费的两类互斥身份。

const (
	// MutexBackend 标识 backend 命名 Mutex。
	MutexBackend MutexKind = "backend"
	// MutexMutation 标识 mutation 与 update 共用的命名 Mutex。
	MutexMutation MutexKind = "mutation"
)

func (MutexKind) String

func (k MutexKind) String() string

String 返回稳定字面量。

func (MutexKind) Valid

func (k MutexKind) Valid() bool

Valid 报告 kind 是否属于冻结集合。

type MutexProbe

type MutexProbe interface {
	Probe(ctx context.Context, kind MutexKind) (MutexProbeResult, error)
}

MutexProbe 按 state 绑定的 MutexKind 查询真实占用。

type MutexProbeResult

type MutexProbeResult struct {
	Held      bool
	Recovered bool
}

MutexProbeResult 保存零等待探测与 abandoned 恢复事实。

type NotFoundError

type NotFoundError struct {
	File string
	Path string
}

NotFoundError 表示固定逻辑状态文件不存在。

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

func (*NotFoundError) Is

func (e *NotFoundError) Is(target error) bool

Is 让 NotFoundError 匹配 ErrNotFound。

type Option

type Option func(*options) error

Option 配置 Store 的可测试依赖。

func WithClock

func WithClock(clock func() time.Time) Option

WithClock 注入每次状态构造使用一次的时钟。

type PIDProbe

type PIDProbe interface {
	Alive(ctx context.Context, pid uint32) (bool, error)
}

PIDProbe 查询一个已知 PID 是否仍存活。

type PIDProbeError

type PIDProbeError struct {
	Operation string
	PID       uint32
	Cause     error
}

PIDProbeError 表示查询指定 PID 的 Win32 操作失败。

func (*PIDProbeError) Error

func (e *PIDProbeError) Error() string

func (*PIDProbeError) Unwrap

func (e *PIDProbeError) Unwrap() error

Unwrap 保留 Win32 原因。

type ReadError

type ReadError struct {
	File  string
	Cause error
}

ReadError 表示普通状态文件读取 I/O 故障。

func (*ReadError) Error

func (e *ReadError) Error() string

func (*ReadError) Unwrap

func (e *ReadError) Unwrap() error

Unwrap 保留底层 filesystem 或 I/O 错误链。

type Revision

type Revision struct {
	Version string `json:"version"`
	Commit  string `json:"commit"`
}

Revision 标识一个已验证的产品版本和 Git Commit。

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store 线性化状态构造、文件 I/O 和关闭。

func NewStore

func NewStore(
	ctx context.Context,
	layout *config.Layout,
	options ...Option,
) (*Store, error)

NewStore 创建并拥有 T2.5 StateFiles。

func (*Store) Close

func (s *Store) Close() error

Close 幂等关闭 Store 拥有的 StateFiles,并缓存第一次结果。

func (*Store) NewBrokenEnvironment

func (s *Store) NewBrokenEnvironment(
	lastSuccessful Revision,
	broken BrokenEnvironment,
) (EnvironmentState, error)

NewBrokenEnvironment 构造保留最近成功 revision 的失效状态。

func (*Store) NewReadyEnvironment

func (s *Store) NewReadyEnvironment(
	version string,
	commit string,
) (EnvironmentState, error)

NewReadyEnvironment 构造依赖已同步成功的稳定状态。

func (*Store) NewTransaction

func (s *Store) NewTransaction(
	kind TransactionKind,
	input TransactionInput,
) (TransactionState, error)

NewTransaction 采样一次时钟并构造已完整校验的事务状态。

func (*Store) ReadEnvironment

func (s *Store) ReadEnvironment(
	ctx context.Context,
) (EnvironmentState, error)

ReadEnvironment 严格读取稳定环境状态并丢弃文件 token。

func (*Store) ReadTransaction

func (s *Store) ReadTransaction(
	ctx context.Context,
	kind TransactionKind,
) (TransactionSnapshot, error)

ReadTransaction 严格读取并保留不可重建的事务 snapshot。

func (*Store) RemoveOwnedTransaction

func (s *Store) RemoveOwnedTransaction(
	ctx context.Context,
	kind TransactionKind,
	expected TransactionState,
) error

RemoveOwnedTransaction 仅在当前事务仍与调用方持有的完整业务 snapshot 一致时条件删除。 读取后的物理文件 token 仍由 RemoveTransaction 交给 StateFiles 复验,避免删除后来操作的事务。

func (*Store) RemoveTransaction

func (s *Store) RemoveTransaction(
	ctx context.Context,
	snapshot TransactionSnapshot,
) error

RemoveTransaction 使用同一 Store 读取的原 token 条件删除事务。

func (*Store) ValidateEnvironment

func (s *Store) ValidateEnvironment(value EnvironmentState) error

ValidateEnvironment 对内存输入和磁盘解码值执行同一稳定状态规则。

func (*Store) WriteEnvironment

func (s *Store) WriteEnvironment(
	ctx context.Context,
	value EnvironmentState,
) error

WriteEnvironment 校验并原子发布稳定环境状态。

func (*Store) WriteTransaction

func (s *Store) WriteTransaction(
	ctx context.Context,
	kind TransactionKind,
	value TransactionState,
) error

WriteTransaction 校验并原子发布一个完整事务状态。

type TransactionInput

type TransactionInput struct {
	TargetCommit  string
	BaseCommit    string
	OperationID   string
	Command       string
	PID           uint32
	TargetVersion string
	Stage         protocol.Stage
}

TransactionInput 是创建新事务时由应用服务提供的业务身份。

type TransactionKind

type TransactionKind string

TransactionKind 标识三个互不替代的事务文件。

const (
	// TransactionBackend 标识 backend supervision 事务。
	TransactionBackend TransactionKind = "backend"
	// TransactionMutation 标识顶层受管变更事务。
	TransactionMutation TransactionKind = "mutation"
	// TransactionUpdate 标识 mutation 内的 Git 更新事务。
	TransactionUpdate TransactionKind = "update"
)

func (TransactionKind) String

func (k TransactionKind) String() string

String 返回稳定字面量。

func (TransactionKind) Valid

func (k TransactionKind) Valid() bool

Valid 报告 kind 是否属于冻结集合。

type TransactionSnapshot

type TransactionSnapshot struct {
	// contains filtered or unexported fields
}

TransactionSnapshot 把已校验事务值绑定到 T2.5 原始条件删除 token。

func (TransactionSnapshot) Kind

Kind 返回 snapshot 对应的事务类型。

func (TransactionSnapshot) State

State 返回已校验事务值的副本。

type TransactionState

type TransactionState struct {
	SchemaVersion int            `json:"schemaVersion"`
	OperationID   string         `json:"operationId"`
	Command       string         `json:"command"`
	PID           uint32         `json:"pid"`
	StartedAt     time.Time      `json:"startedAt"`
	TargetVersion string         `json:"targetVersion"`
	Stage         protocol.Stage `json:"stage"`
	TargetCommit  string         `json:"targetCommit,omitempty"`
	BaseCommit    string         `json:"baseCommit,omitempty"`
}

TransactionState 是三类事务文件共享的完整 JSON schema。

type UnsupportedSchemaError

type UnsupportedSchemaError struct {
	File string
	Got  int
}

UnsupportedSchemaError 表示 schemaVersion 是合法整数但当前版本不支持。

func (*UnsupportedSchemaError) Error

func (e *UnsupportedSchemaError) Error() string

func (*UnsupportedSchemaError) Is

func (e *UnsupportedSchemaError) Is(target error) bool

Is 让 UnsupportedSchemaError 匹配 ErrUnsupportedSchema。

type ValidationError

type ValidationError struct {
	Field string
	Cause error
}

ValidationError 表示调用值或已解码字段违反稳定语义。

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error 仅报告稳定字段名,不回显调用值或磁盘内容。

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Unwrap 保留稳定校验原因。

type WriteError

type WriteError struct {
	File             string
	Phase            WritePhase
	MutationApplied  bool
	RecoveryRequired bool
	Cause            error
	CleanupError     error
}

WriteError 表示持久化写入或条件删除故障。

func (*WriteError) Code

func (e *WriteError) Code() protocol.Code

Code 返回冻结的持久化写错误码。

func (*WriteError) Error

func (e *WriteError) Error() string

func (*WriteError) Unwrap

func (e *WriteError) Unwrap() []error

Unwrap 同时保留主错误和清理错误。

type WritePhase

type WritePhase string

WritePhase 标识持久化写失败发生的稳定阶段。

const (
	// WritePhaseEncode 表示稳定 JSON 编码阶段。
	WritePhaseEncode WritePhase = "encode"
	// WritePhaseRecover 表示 guard 取得、preflight 或既有 intent 恢复阶段。
	WritePhaseRecover WritePhase = "recover"
	// WritePhaseCreate 表示临时状态文件创建阶段。
	WritePhaseCreate WritePhase = "create"
	// WritePhaseWrite 表示完整 payload 写入阶段。
	WritePhaseWrite WritePhase = "write"
	// WritePhaseSync 表示临时文件刷盘阶段。
	WritePhaseSync WritePhase = "sync"
	// WritePhaseRename 表示原子发布阶段。
	WritePhaseRename WritePhase = "rename"
	// WritePhaseFinalize 表示 rollback 或事务 leaf POSIX unlink 阶段。
	WritePhaseFinalize WritePhase = "finalize"
	// WritePhaseClose 表示已打开 handle 的收口阶段。
	WritePhaseClose WritePhase = "close"
	// WritePhaseRemove 表示事务条件删除阶段。
	WritePhaseRemove WritePhase = "remove"
)

func (WritePhase) String

func (p WritePhase) String() string

String 返回稳定字面量。

func (WritePhase) Valid

func (p WritePhase) Valid() bool

Valid 报告 phase 是否属于冻结集合。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL