Files
knowledge-governance/internal/libraryretention/capacity.go
T
姚章浩 281699a431
build / build (push) Successful in 1m21s
feat(library): 生产整库真实读写/校验/恢复与本机真实服务验收
为什么:生产目标此前是"无条件失败"的占位,编排再完整也不能落地;本轮把它做成真实实现,
并在本机 WeKnora 测试栈的真实 PG/ES 上跑通导入、撤销、预备份与故障恢复。

- ProductionLibraryTarget:Current 读行数+身份摘要;Apply 按阶段真实写入(database/wiki
  同事务先删本库再插、resources 传对象、index 搬运原始文档与向量);Verify 比对身份摘要;
  Restore 按预备份快照回放。
- 撤销真正生效:索引阶段写入后 PruneScope 删除快照里没有的文档。
- ProductionBackupStore:导出前独立完整预备份,导出前后行数不一致即失败。
- worker 用注入配置装配目标与预备份;缺项把变量名写进阶段并失败,不再有占位对象。
- capacity 安全加法防溢出;保留策略组合语义明确(各维度都超限才候选)。
- 新增本机真实服务集成测试(一次性 kg_library_test_* 库 + 随机 ES 索引),
  覆盖导入/校验/撤销/预备份/故障注入/恢复。
2026-10-28 15:54:01 +08:00

56 lines
1.8 KiB
Go

package libraryretention
import (
"fmt"
"math"
)
// 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"`
}
// addCapacity 安全相加:int64 溢出时必须拒绝,而不是绕回负数让门禁通过。
func addCapacity(current, required int64) (int64, bool) {
if current < 0 || required < 0 {
return 0, false
}
if current > math.MaxInt64-required {
return 0, false
}
return current + required, true
}
func CheckCapacity(currentBytes, requiredBytes, budgetBytes int64) Capacity {
result := Capacity{BudgetBytes: budgetBytes, CurrentBytes: currentBytes, RequiredBytes: requiredBytes}
if budgetBytes <= 0 {
result.Reason = "容量预算未设置(LIBRARY_CAPACITY_BUDGET_BYTES),无法证明装得下"
return result
}
result.Configured = true
if currentBytes < 0 || requiredBytes < 0 {
result.Reason = "容量口径异常(负数),拒绝放行"
return result
}
projected, ok := addCapacity(currentBytes, requiredBytes)
if !ok {
// 溢出说明真实需求已经大得离谱:不能算出一个负数后"通过"。
result.Reason = "容量相加溢出 int64,拒绝放行"
return result
}
result.ProjectedByte = projected
if projected > budgetBytes {
result.Reason = fmt.Sprintf("写入后预计占用 %d 字节,超过预算 %d 字节", projected, budgetBytes)
return result
}
result.OK = true
return result
}