Files
knowledge-governance/internal/releaseadapter/v071/library_capture.go
T
姚章浩 bf049cee1f
build / build (push) Successful in 1m12s
fix(snapshot): 暂停前后强制检查共享源全库任务
2026-10-28 01:05:44 +08:00

77 lines
3.4 KiB
Go

package v071
import (
"context"
"errors"
"time"
"knowledge-governance/internal/libraryfreeze"
"knowledge-governance/internal/librarysnapshot"
)
type LibraryCaptureRequest struct {
JobID, Scope string
Identity PostgresIdentity
Compatibility librarysnapshot.Compatibility
CreatedAt time.Time
MaxDocuments int
}
// CaptureLibrary connects maintenance to the actual PG/PIT/object archive path.
// sourceReady is mandatory and must verify the whole writer group (both scopes,
// ingress and background jobs), not only this KB. It runs before stopping and
// again inside the stopped window. Deployment must establish that the group
// contains every source writer. This function does not certify Wiki semantics,
// approve a release, or give the worker any production credentials.
func CaptureLibrary(ctx context.Context, freeze *libraryfreeze.Coordinator, archive *librarysnapshot.Archive, db librarySnapshotDB, search *SearchExporter, objects ObjectReader, sourceReady func(context.Context) error, req LibraryCaptureRequest) (libraryfreeze.Result, error) {
if freeze == nil || archive == nil || db == nil || search == nil || objects == nil || sourceReady == nil || req.CreatedAt.IsZero() || req.MaxDocuments < 1 || req.Identity.TenantID == 0 || !validSearchIdentifier(req.Identity.KnowledgeBaseID) || req.Identity.KnowledgeID != "" || (req.Scope != "internal" && req.Scope != "external") {
return libraryfreeze.Result{}, errors.New("invalid library capture configuration")
}
result, err := freeze.RunPrepared(ctx, req.JobID, func(ctx context.Context) error {
if err := checkLibrarySourceSettlement(ctx, db); err != nil {
return err
}
if err := sourceReady(ctx); err != nil {
return err
}
// Check readiness before a maintenance window, then repeat the export inside
// that window: work can arrive between preflight and the graceful stop.
_, err := ExportLibrary(ctx, db, req.Identity)
return err
}, func(ctx context.Context) (string, error) {
if err := checkLibrarySourceSettlement(ctx, db); err != nil {
return "", err
}
if err := sourceReady(ctx); err != nil {
return "", err
}
pg, err := ExportLibrary(ctx, db, req.Identity)
if err != nil {
return "", err
}
es, err := search.ExportLibrary(ctx, SearchIdentity{TenantID: req.Identity.TenantID, KnowledgeBaseID: req.Identity.KnowledgeBaseID}, req.MaxDocuments)
if err != nil {
return "", err
}
return ArchiveLibraryCandidate(ctx, archive, req.Scope, req.Identity, req.Compatibility, pg, es, objects, req.CreatedAt)
})
if err != nil {
return result, err
}
// A successful old operation is replayable only while its exact independent
// archive still verifies, and never under another KB/scope/model contract.
saved, err := archive.Load(result.SnapshotID)
if err != nil {
return libraryfreeze.Result{}, errors.New("captured library archive no longer verifies")
}
if saved.Scope != req.Scope || saved.TenantID != req.Identity.TenantID || saved.KnowledgeBaseID != req.Identity.KnowledgeBaseID || saved.Compatibility != req.Compatibility || !saved.CreatedAt.Equal(req.CreatedAt) {
return libraryfreeze.Result{}, errors.New("library capture request conflicts with existing job")
}
for _, part := range saved.Parts {
if part.Kind == "index" && part.Count > int64(req.MaxDocuments) {
return libraryfreeze.Result{}, errors.New("replayed library exceeds requested document limit")
}
}
return result, nil
}