Files
knowledge-governance/internal/libraryretention/retention.go
T
姚章浩 f91359f881
build / build (push) Successful in 1m15s
feat(library): 存储管理真实化——容量门禁、依赖保护与清理预览
为什么:存储页原来只有占用数字,保留/依赖保护/清理预览写"未接线";这些都不需要生产写权限,
按"未设置就不做决定"的口径实现成只读计算。

- 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)。
2026-10-28 15:04:44 +08:00

110 lines
4.3 KiB
Go

// Package libraryretention 计算整库归档的保留、依赖保护与清理预览。
//
// 只做计算,不做删除:所有结论都是"如果按这个策略清理,哪些版本会被保留、
// 哪些成为候选、大约能释放多少独占字节"。共享 blob 不计入回收量,因为仍有
// 其它版本引用它;没有任何策略配置时不给候选,也不拿默认值替用户拍板。
package libraryretention
import (
"fmt"
"sort"
"time"
)
// Version 是参与保留判断的一个整库版本。
type Version struct {
ID string `json:"id"`
Kind string `json:"kind"`
CreatedAt time.Time `json:"created_at"`
// Protected 由调用方依据真实引用计算(生产生效、最近封存、在飞作业),
// 这里不猜;ProtectedBy 写明是哪条依据,审计时能逐条核对。
Protected bool `json:"protected"`
ProtectedBy []string `json:"protected_by,omitempty"`
// ExclusiveBytes 是只有这个版本引用的内容字节(来自归档用量)。
ExclusiveBytes int64 `json:"exclusive_bytes"`
SharedBytes int64 `json:"shared_bytes"`
}
// Policy 是保留策略。零值表示"未设置"——这时不给任何候选。
type Policy struct {
KeepVersions int `json:"keep_versions"`
KeepDays int `json:"keep_days"`
}
func (p Policy) Configured() bool { return p.KeepVersions > 0 || p.KeepDays > 0 }
type Candidate struct {
ID string `json:"id"`
Kind string `json:"kind"`
Reason []string `json:"reason"`
// ExclusiveBytes 是上限口径:就算删掉,共享 blob 也不会释放。
ExclusiveBytes int64 `json:"exclusive_bytes"`
}
type Plan struct {
Policy Policy `json:"policy"`
PolicyConfigured bool `json:"policy_configured"`
AutomaticCleanup bool `json:"automatic_cleanup"`
VersionCount int `json:"version_count"`
TotalBytes int64 `json:"total_bytes"`
Protected []string `json:"protected"`
Keep []string `json:"keep"`
Candidates []Candidate `json:"candidates"`
// ReclaimPreviewBytes 只是候选版本独占字节之和,不是承诺。
ReclaimPreviewBytes int64 `json:"reclaim_preview_bytes"`
// Blocked 说明为什么给不出安全预览(策略未配置、时间基准异常等)。
Blocked []string `json:"blocked"`
Now time.Time `json:"now"`
}
// Build 生成清理预览。now 由调用方给,便于测试;不给真实时钟依赖。
func Build(versions []Version, totalBytes int64, policy Policy, now time.Time) Plan {
plan := Plan{Policy: policy, PolicyConfigured: policy.Configured(), AutomaticCleanup: false,
VersionCount: len(versions), TotalBytes: totalBytes, Protected: []string{}, Keep: []string{},
Candidates: []Candidate{}, Blocked: []string{}, Now: now.UTC()}
sorted := append([]Version(nil), versions...)
sort.Slice(sorted, func(i, j int) bool {
if sorted[i].CreatedAt.Equal(sorted[j].CreatedAt) {
return sorted[i].ID > sorted[j].ID
}
return sorted[i].CreatedAt.After(sorted[j].CreatedAt)
})
if !policy.Configured() {
plan.Blocked = append(plan.Blocked, "保留策略未设置(保留版本数与保留天数都没有),不生成清理候选")
}
if policy.KeepVersions < 0 || policy.KeepDays < 0 {
plan.Blocked = append(plan.Blocked, "保留策略取值非法,拒绝生成预览")
return plan
}
for rank, version := range sorted {
if version.Protected {
plan.Protected = append(plan.Protected, version.ID)
plan.Keep = append(plan.Keep, version.ID)
continue
}
if !policy.Configured() {
// 没策略时按"全部保留"处理,绝不默认清理。
plan.Keep = append(plan.Keep, version.ID)
continue
}
reasons := make([]string, 0, 2)
if policy.KeepVersions > 0 && rank >= policy.KeepVersions {
reasons = append(reasons, fmt.Sprintf("超出保留 %d 个版本", policy.KeepVersions))
}
if policy.KeepDays > 0 && !version.CreatedAt.IsZero() && version.CreatedAt.Before(now.AddDate(0, 0, -policy.KeepDays)) {
reasons = append(reasons, fmt.Sprintf("超出保留 %d 天", policy.KeepDays))
}
if len(reasons) == 0 {
plan.Keep = append(plan.Keep, version.ID)
continue
}
plan.Candidates = append(plan.Candidates, Candidate{ID: version.ID, Kind: version.Kind,
Reason: reasons, ExclusiveBytes: version.ExclusiveBytes})
plan.ReclaimPreviewBytes += version.ExclusiveBytes
}
return plan
}