为什么:父审指出资源范围未关联外层 resources,会把别的库/租户资源算进来; Verify 只看行数与 ID,同 ID 改正文/改向量/损坏对象都能通过;集成测试还在用假对象存储。 - scopeWhere(resources)/resourcePaths 用 resource_bindings.resource_id=resources.id 精确关联, 覆盖 knowledge/chunk 绑定并限制租户;资源部件名与资源 ID 对齐。 - Verify 增加逐行内容摘要、索引文档摘要、对象字节 SHA256 校验。 - 预备份一致性改为内容指纹(数量+摘要),同数量改写也会失败。 - 新增真实 MinIO 对象存储的本机验收用例,覆盖隔离/损坏/独立恢复。
This commit is contained in:
@@ -2,6 +2,7 @@ package v071
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -63,6 +64,44 @@ func NewProductionBackupStore(config ProductionBackupConfig) (*ProductionBackupS
|
||||
maxObjectByte: config.MaxObjectByte, now: now}, nil
|
||||
}
|
||||
|
||||
// tableFingerprint 是一张表的"数量 + 内容摘要"指纹:同数量改写也会被识别。
|
||||
type tableFingerprint struct {
|
||||
Count int64
|
||||
Digest string
|
||||
}
|
||||
|
||||
// fingerprints 读取当前生产指纹;Prepare 在导出前后各取一次做一致性判断。
|
||||
func (s *ProductionBackupStore) fingerprints(ctx context.Context) (map[string]tableFingerprint, error) {
|
||||
result := map[string]tableFingerprint{}
|
||||
for _, table := range productionLibraryTables {
|
||||
rows, err := s.pool.Query(ctx, fmt.Sprintf(`SELECT to_jsonb(%s) FROM %s WHERE %s`,
|
||||
table, table, scopeWhere(table)), s.identity.TenantID, s.identity.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取生产 %s 指纹失败", table)
|
||||
}
|
||||
digests := make([]string, 0, 64)
|
||||
for rows.Next() {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := rows.Scan(&raw); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
digest, err := canonicalRowDigest(raw)
|
||||
if err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
digests = append(digests, digest)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[table] = tableFingerprint{Count: int64(len(digests)), Digest: combineRowDigests(digests)}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *ProductionBackupStore) Prepare(ctx context.Context, _ string, current libraryrelease.CurrentState) (libraryrelease.BackupRef, error) {
|
||||
archive, err := librarysnapshot.Open(s.archiveRoot, 256*1024*1024)
|
||||
if err != nil {
|
||||
@@ -70,6 +109,10 @@ func (s *ProductionBackupStore) Prepare(ctx context.Context, _ string, current l
|
||||
}
|
||||
defer archive.Close()
|
||||
createdAt := s.now().UTC()
|
||||
before, err := s.fingerprints(ctx)
|
||||
if err != nil {
|
||||
return libraryrelease.BackupRef{}, err
|
||||
}
|
||||
pg, err := ExportLibrary(ctx, s.pool, s.identity)
|
||||
if err != nil {
|
||||
return libraryrelease.BackupRef{}, fmt.Errorf("读取生产整库失败: %w", err)
|
||||
@@ -92,17 +135,23 @@ func (s *ProductionBackupStore) Prepare(ctx context.Context, _ string, current l
|
||||
if err != nil {
|
||||
return libraryrelease.BackupRef{}, fmt.Errorf("密封预备份失败: %w", err)
|
||||
}
|
||||
// 静默检查:导出期间生产不能有变化,否则这份预备份不是一致切点。
|
||||
after, err := s.rowCounts(ctx)
|
||||
// 静默检查:导出期间生产内容不能变。只比行数会被"同数量改写"骗过,
|
||||
// 所以这里比对逐行内容摘要(行数 + 身份 + 内容)。
|
||||
after, err := s.fingerprints(ctx)
|
||||
if err != nil {
|
||||
return libraryrelease.BackupRef{}, err
|
||||
}
|
||||
for table, beforeState := range before {
|
||||
if after[table] != beforeState {
|
||||
return libraryrelease.BackupRef{}, fmt.Errorf("预备份期间生产 %s 内容发生变化,切点不一致", table)
|
||||
}
|
||||
}
|
||||
for table, want := range current.Counts {
|
||||
if table == "index" {
|
||||
continue
|
||||
}
|
||||
if after[table] != want {
|
||||
return libraryrelease.BackupRef{}, fmt.Errorf("预备份期间生产 %s 行数从 %d 变为 %d,切点不一致", table, want, after[table])
|
||||
if after[table].Count != want {
|
||||
return libraryrelease.BackupRef{}, fmt.Errorf("预备份期间生产 %s 行数从 %d 变为 %d", table, want, after[table].Count)
|
||||
}
|
||||
}
|
||||
manifest, err := archive.Load(snapshotID)
|
||||
|
||||
Reference in New Issue
Block a user