为什么:存储页原来只有占用数字,保留/依赖保护/清理预览写"未接线";这些都不需要生产写权限, 按"未设置就不做决定"的口径实现成只读计算。 - 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:
@@ -0,0 +1,35 @@
|
||||
package libraryretention
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Capacity 是生产写入前的容量门禁:预算未设置就不放行(无法证明"装得下"),
|
||||
// 预算已知但不足也阻断。它不复用清理预览的回收量——清理未授权,不能拿它抵账。
|
||||
type Capacity struct {
|
||||
BudgetBytes int64 `json:"budget_bytes"`
|
||||
CurrentBytes int64 `json:"current_bytes"`
|
||||
RequiredBytes int64 `json:"required_bytes"`
|
||||
ProjectedByte int64 `json:"projected_bytes"`
|
||||
Configured bool `json:"configured"`
|
||||
OK bool `json:"ok"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
func CheckCapacity(currentBytes, requiredBytes, budgetBytes int64) Capacity {
|
||||
result := Capacity{BudgetBytes: budgetBytes, CurrentBytes: currentBytes, RequiredBytes: requiredBytes,
|
||||
ProjectedByte: currentBytes + requiredBytes}
|
||||
if budgetBytes <= 0 {
|
||||
result.Reason = "容量预算未设置(LIBRARY_CAPACITY_BUDGET_BYTES),无法证明装得下"
|
||||
return result
|
||||
}
|
||||
result.Configured = true
|
||||
if requiredBytes < 0 || currentBytes < 0 {
|
||||
result.Reason = "容量口径异常(负数),拒绝放行"
|
||||
return result
|
||||
}
|
||||
if result.ProjectedByte > budgetBytes {
|
||||
result.Reason = fmt.Sprintf("写入后预计占用 %d 字节,超过预算 %d 字节", result.ProjectedByte, budgetBytes)
|
||||
return result
|
||||
}
|
||||
result.OK = true
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package libraryretention
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func day(n int) time.Time { return time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC).AddDate(0, 0, n) }
|
||||
|
||||
func fixture() []Version {
|
||||
return []Version{
|
||||
{ID: "v-new", Kind: "test_sealed", CreatedAt: day(5), ExclusiveBytes: 100, SharedBytes: 50},
|
||||
{ID: "v-mid", Kind: "test_sealed", CreatedAt: day(3), ExclusiveBytes: 200},
|
||||
{ID: "v-old", Kind: "test_sealed", CreatedAt: day(1), ExclusiveBytes: 300},
|
||||
{ID: "v-prod", Kind: "production_effective", CreatedAt: day(0), Protected: true, ProtectedBy: []string{"生产生效版本"}},
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildWithoutPolicyKeepsEverything(t *testing.T) {
|
||||
plan := Build(fixture(), 1000, Policy{}, day(6))
|
||||
if plan.PolicyConfigured {
|
||||
t.Fatal("zero policy must not look configured")
|
||||
}
|
||||
if len(plan.Candidates) != 0 {
|
||||
t.Fatalf("no policy must not produce candidates: %+v", plan.Candidates)
|
||||
}
|
||||
if len(plan.Blocked) == 0 {
|
||||
t.Fatal("no policy must explain why no preview is available")
|
||||
}
|
||||
if plan.AutomaticCleanup {
|
||||
t.Fatal("automatic cleanup must stay off")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildProtectsVersionRegardlessOfAge(t *testing.T) {
|
||||
plan := Build(fixture(), 1000, Policy{KeepVersions: 1}, day(6))
|
||||
for _, candidate := range plan.Candidates {
|
||||
if candidate.ID == "v-prod" {
|
||||
t.Fatal("protected version must never be a candidate")
|
||||
}
|
||||
}
|
||||
found := false
|
||||
for _, id := range plan.Protected {
|
||||
if id == "v-prod" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("protected list must include v-prod: %+v", plan.Protected)
|
||||
}
|
||||
// 保留 1 个版本(最近)之后,除受保护版本外都应成为候选
|
||||
if len(plan.Candidates) != 2 {
|
||||
t.Fatalf("candidates = %+v, want v-mid and v-old", plan.Candidates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildKeepsRecentPlusProtected(t *testing.T) {
|
||||
plan := Build(fixture(), 1000, Policy{KeepVersions: 2}, day(6))
|
||||
// 保留最近 2 个(v-new、v-mid)+ 受保护的 v-prod;只有 v-old 成为候选。
|
||||
if len(plan.Keep) != 3 {
|
||||
t.Fatalf("keep = %+v, want 3 versions", plan.Keep)
|
||||
}
|
||||
if len(plan.Candidates) != 1 || plan.Candidates[0].ID != "v-old" {
|
||||
t.Fatalf("candidates = %+v, want only v-old", plan.Candidates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReclaimPreviewCountsOnlyExclusiveBytes(t *testing.T) {
|
||||
plan := Build([]Version{
|
||||
{ID: "a", CreatedAt: day(0), ExclusiveBytes: 100, SharedBytes: 900},
|
||||
{ID: "b", CreatedAt: day(1), ExclusiveBytes: 20},
|
||||
}, 1020, Policy{KeepVersions: 1}, day(2))
|
||||
if plan.ReclaimPreviewBytes != 100 {
|
||||
t.Fatalf("reclaim preview = %d, want 100 (shared bytes must not count)", plan.ReclaimPreviewBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRejectsNegativePolicy(t *testing.T) {
|
||||
plan := Build(fixture(), 1, Policy{KeepVersions: -1}, day(6))
|
||||
if len(plan.Blocked) == 0 || len(plan.Candidates) != 0 {
|
||||
t.Fatalf("invalid policy must block preview: %+v", plan)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckCapacityFailsClosed(t *testing.T) {
|
||||
if result := CheckCapacity(100, 100, 0); result.OK || result.Configured {
|
||||
t.Fatalf("unset budget must fail closed: %+v", result)
|
||||
}
|
||||
if result := CheckCapacity(100, 100, 150); result.OK {
|
||||
t.Fatalf("over budget must fail: %+v", result)
|
||||
}
|
||||
if result := CheckCapacity(100, 20, 150); !result.OK || !result.Configured {
|
||||
t.Fatalf("within budget must pass: %+v", result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user