feat(library): 存储管理真实化——容量门禁、依赖保护与清理预览
build / build (push) Successful in 1m15s

为什么:存储页原来只有占用数字,保留/依赖保护/清理预览写"未接线";这些都不需要生产写权限,
按"未设置就不做决定"的口径实现成只读计算。

- internal/libraryretention:按保留数量/天数生成预览;生产生效、每库最近封存、每次生产预备份、
  在飞作业引用一律受保护;回收预览只算独占字节,共享 blob 不计。
- internal/libraryretention.CheckCapacity:预算未设置即不放行(无法证明装得下),超预算阻断;
  已并入 POST /library/syncs 的 gate 缺项。
- 新增 GET /api/v1/library/storage:真实归档用量 + 三类版本保护依据 + 策略 + 预览 + 容量,只读。
- 系统设置→存储管理改为读该接口:策略未设置时明确"全部保留、不生成候选",自动清理恒关闭。
- 配置项 LIBRARY_CAPACITY_BUDGET_BYTES / LIBRARY_RETENTION_VERSIONS / LIBRARY_RETENTION_DAYS(未设=0)。
This commit is contained in:
姚章浩
2026-10-28 15:04:44 +08:00
parent 12bb839d8a
commit f91359f881
13 changed files with 756 additions and 49 deletions
+22 -1
View File
@@ -72,6 +72,11 @@ type Config struct {
// LibraryArchiveRoot 是整库封存产物的只读归档根(主机私有 0700 目录的只读挂载)。
// 留空即"未接线":页面只显示原因,不显示空表冒充没有快照。
LibraryArchiveRoot string
// LibraryCapacityBudgetBytes 是整库归档容量预算;0 表示未设置(容量门禁不放行)。
LibraryCapacityBudgetBytes int64
// LibraryRetentionVersions / Days 是保留策略;都为 0 表示未设置(不生成清理候选)。
LibraryRetentionVersions int
LibraryRetentionDays int
// LibraryAgentSocket 是主机侧封存 agent 的 unix socket(只由 worker 连接)。
// 留空即"未接线":页面不显示可用的封存入口,接口返回原因而不是假装已排队。
LibraryAgentSocket string
@@ -81,6 +86,20 @@ type Config struct {
}
func Load(getenv func(string) string) (Config, error) {
// 容量与保留策略:未设置一律 0(= 不做决定),由使用方按 fail-closed 处理。
capacityBudget, err := positiveInt64OrDefault(strings.TrimSpace(getenv("LIBRARY_CAPACITY_BUDGET_BYTES")), 0)
if err != nil {
return Config{}, errors.New("LIBRARY_CAPACITY_BUDGET_BYTES must be a positive integer")
}
retentionVersions64, err := positiveInt64OrDefault(strings.TrimSpace(getenv("LIBRARY_RETENTION_VERSIONS")), 0)
if err != nil {
return Config{}, errors.New("LIBRARY_RETENTION_VERSIONS must be a positive integer")
}
retentionDays64, err := positiveInt64OrDefault(strings.TrimSpace(getenv("LIBRARY_RETENTION_DAYS")), 0)
if err != nil {
return Config{}, errors.New("LIBRARY_RETENTION_DAYS must be a positive integer")
}
retentionVersions, retentionDays := int(retentionVersions64), int(retentionDays64)
config := Config{
DatabaseURL: getenv("DATABASE_URL"),
AppAddr: valueOrDefault(getenv("APP_ADDR"), ":8080"),
@@ -124,6 +143,9 @@ func Load(getenv func(string) string) (Config, error) {
VersionHistoryAllowedOrigins: splitOrigins(getenv("VERSION_HISTORY_ALLOWED_ORIGINS")),
LibraryArchiveRoot: strings.TrimSpace(getenv("LIBRARY_ARCHIVE_ROOT")),
LibraryAgentSocket: strings.TrimSpace(getenv("LIBRARY_AGENT_SOCKET")),
LibraryCapacityBudgetBytes: capacityBudget,
LibraryRetentionVersions: retentionVersions,
LibraryRetentionDays: retentionDays,
VersionHistoryLinkOrigin: strings.TrimSpace(getenv("VERSION_HISTORY_LINK_ORIGIN")),
VersionHistoryHTTPTimeout: 15 * time.Second,
VersionHistoryMaxPages: valueOrDefaultInt(getenv("VERSION_HISTORY_MAX_PAGES"), 200),
@@ -131,7 +153,6 @@ func Load(getenv func(string) string) (Config, error) {
if config.DatabaseURL == "" {
return Config{}, errors.New("DATABASE_URL is required")
}
var err error
if config.PollInterval, err = durationOrDefault(getenv("WORKER_POLL_INTERVAL"), config.PollInterval); err != nil {
return Config{}, fmt.Errorf("WORKER_POLL_INTERVAL: %w", err)
}