- 同作业号重跑读持久日志+已封存清单按首次请求重放(source_read=false, writers_stopped=false),换范围/绑定库或清单校验失败即拒绝 - 归档用量按跨版本去重统计内容字节,清单/未引用 blob/实际分配单列, 不再用各版本 unique 之和冒充归档占用 - 冻结窗口内复核来源契约,不一致即中止;拒绝组外同镜像运行容器 - 补齐整库只读接口的 OpenAPI 契约 - 按用户确认取消 A06:文档/页面/接口不再把效果测试列为封版门槛
This commit is contained in:
@@ -15,6 +15,11 @@ type LibraryCaptureRequest struct {
|
||||
Compatibility librarysnapshot.Compatibility
|
||||
CreatedAt time.Time
|
||||
MaxDocuments int
|
||||
// VerifyContract, when set, runs inside the stopped window immediately before
|
||||
// the export. The caller re-reads the source contract and returns an error if
|
||||
// it differs from Compatibility, so a model/index change between the
|
||||
// preflight read and the maintenance window cannot be sealed silently.
|
||||
VerifyContract func(context.Context) error
|
||||
}
|
||||
|
||||
// CaptureLibrary connects maintenance to the actual PG/PIT/object archive path.
|
||||
@@ -45,6 +50,11 @@ func CaptureLibrary(ctx context.Context, freeze *libraryfreeze.Coordinator, arch
|
||||
if err := sourceReady(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if req.VerifyContract != nil {
|
||||
if err := req.VerifyContract(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
pg, err := ExportLibrary(ctx, db, req.Identity)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -2,6 +2,7 @@ package v071
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -14,6 +15,8 @@ import (
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
)
|
||||
|
||||
var errTestContractChanged = errors.New("source contract changed inside the maintenance window")
|
||||
|
||||
type captureWriter struct {
|
||||
running atomic.Bool
|
||||
db *libraryFixture
|
||||
@@ -38,6 +41,85 @@ func (c *captureWriter) Stop(context.Context, string) error {
|
||||
return nil
|
||||
}
|
||||
func (c *captureWriter) Start(context.Context, string) error { c.running.Store(true); return nil }
|
||||
|
||||
// The contract is read before the window and must be re-checked inside it: a
|
||||
// model or mapping change between those two moments must abort, restore writers
|
||||
// and seal nothing.
|
||||
func TestLibraryCaptureRechecksContractInsideFrozenWindow(t *testing.T) {
|
||||
writer := &captureWriter{db: newLibraryFixture()}
|
||||
writer.running.Store(true)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/chunks/_mapping":
|
||||
fmt.Fprint(w, `{"chunks":{"mappings":{"properties":{"knowledge_base_id":{"type":"keyword"},"knowledge_id":{"type":"keyword"},"tenant_id":{"type":"long"}}}}}`)
|
||||
case "/chunks/_pit":
|
||||
fmt.Fprint(w, `{"id":"fixed-pit"}`)
|
||||
case "/_search":
|
||||
fmt.Fprint(w, `{"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"hits":[]}}`)
|
||||
case "/_pit":
|
||||
fmt.Fprint(w, `{"succeeded":true}`)
|
||||
default:
|
||||
http.Error(w, "unexpected", 400)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
search, err := NewSearchExporter(SearchExporterConfig{BaseURL: server.URL, Index: "chunks", PageSize: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mapping, err := search.inspectMapping(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := t.TempDir()
|
||||
_ = os.Chmod(root, 0700)
|
||||
freeze, err := libraryfreeze.Open(root, "test-source", writer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer freeze.Close()
|
||||
archiveRoot := t.TempDir()
|
||||
_ = os.Chmod(archiveRoot, 0700)
|
||||
archive, err := librarysnapshot.Open(archiveRoot, 1024*1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer archive.Close()
|
||||
req := LibraryCaptureRequest{JobID: "contract-job", Scope: "internal",
|
||||
Identity: PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"},
|
||||
Compatibility: librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74,
|
||||
RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "fixture-model", EmbeddingDimension: 1024,
|
||||
IndexMappingSHA256: mapping.digest},
|
||||
CreatedAt: time.Date(2026, 10, 28, 0, 0, 0, 0, time.UTC), MaxDocuments: 100}
|
||||
called := 0
|
||||
req.VerifyContract = func(context.Context) error {
|
||||
called++
|
||||
if writer.running.Load() {
|
||||
t.Error("contract re-check ran before writers stopped")
|
||||
}
|
||||
return errTestContractChanged
|
||||
}
|
||||
ready := func(context.Context) error { return nil }
|
||||
result, err := CaptureLibrary(context.Background(), freeze, archive, dbFixture(t, writer), search, &libraryCandidateReader{}, ready, req)
|
||||
if err == nil || result.State == "succeeded" || result.SnapshotID != "" {
|
||||
t.Fatalf("contract change was sealed: %+v %v", result, err)
|
||||
}
|
||||
if called != 1 {
|
||||
t.Fatalf("contract re-check called %d times", called)
|
||||
}
|
||||
if !writer.running.Load() {
|
||||
t.Fatal("writer left stopped after a contract abort")
|
||||
}
|
||||
if usage, usageErr := archive.Usage(); usageErr != nil || len(usage) != 0 {
|
||||
t.Fatalf("aborted capture left an artifact: %v %v", usage, usageErr)
|
||||
}
|
||||
}
|
||||
|
||||
func dbFixture(t *testing.T, writer *captureWriter) *libraryFixture {
|
||||
t.Helper()
|
||||
return writer.db
|
||||
}
|
||||
|
||||
func TestLibraryCaptureMaintenanceOwnsArchiveAndRestoresOnFailure(t *testing.T) {
|
||||
for _, mode := range []string{"success", "initial-busy", "late-pending", "search-failure", "other-library-busy", "other-library-late-pending"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user