dcdc8d1ddd
全栈知识治理平台首次入库(P1-P10 + 发布链路模拟验证闭环): - 治理核心:源目录/变更集/人工审核/发布批次/发布执行(fail-closed 闸口) - 测试通道:WeKnora 测试 KB 上传/轮询/验收;poll 完成时写入 parser_generation/index_generation,修复 manual/seed 路径发布闸口永久拦截 - 发布适配器 v071(本地 source=测试角色 / 云端 target=生产角色 模拟验证通过): * ES 导出/导入 mapping 自适应(.keyword 子字段、可选 tenant_id) * OSS locator 支持嵌套 scheme;chunks.seq_id 由目标序列分配 * bundle 归档同身份重试原子替换;verifier 按 bundle chunk_type 计数 * 回滚 job payload 支持 prior_execution_id(strict 解码) - 三通道发布+回滚演练实测:publish 4/4 verified,检索命中, rollback 4/4 rolled_back,目标端 PG is_enabled=false、ES is_enabled=false - 前端 A·Data-console 风格全站统一,120 用例通过 - 门禁:go vet / go test 全包通过,gofmt 干净
150 lines
6.2 KiB
Go
150 lines
6.2 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/releasebundle"
|
|
releaseworkflow "knowledge-governance/internal/workflows/release"
|
|
)
|
|
|
|
type postgresSnapshotExporter interface {
|
|
Export(ctx context.Context, identity PostgresIdentity) (PostgresSnapshot, error)
|
|
}
|
|
|
|
type searchSnapshotExporter interface {
|
|
Export(ctx context.Context, identity SearchIdentity) (SearchSnapshot, error)
|
|
}
|
|
|
|
type objectSnapshotExporter interface {
|
|
Export(ctx context.Context, snapshot PostgresSnapshot) (ObjectSnapshot, error)
|
|
}
|
|
|
|
type DocumentExporterConfig struct {
|
|
Contract targetContractInspector
|
|
Postgres postgresSnapshotExporter
|
|
Search searchSnapshotExporter
|
|
Objects objectSnapshotExporter
|
|
TenantID uint64
|
|
EmbeddingModel string
|
|
Now func() time.Time
|
|
}
|
|
|
|
type DocumentExporter struct {
|
|
config DocumentExporterConfig
|
|
}
|
|
|
|
type DocumentBundle struct {
|
|
Manifest releasebundle.Manifest
|
|
Postgres PostgresSnapshot
|
|
Search SearchSnapshot
|
|
Objects ObjectSnapshot
|
|
}
|
|
|
|
func (bundle *DocumentBundle) ReleaseManifest() releasebundle.Manifest {
|
|
if bundle == nil {
|
|
return releasebundle.Manifest{}
|
|
}
|
|
return bundle.Manifest
|
|
}
|
|
|
|
func NewDocumentExporter(config DocumentExporterConfig) (*DocumentExporter, error) {
|
|
if config.Contract == nil || config.Postgres == nil || config.Search == nil || config.Objects == nil || config.TenantID == 0 ||
|
|
!validSearchIdentifier(config.EmbeddingModel) || config.Now == nil {
|
|
return nil, errors.New("invalid v0.7.1 document exporter configuration")
|
|
}
|
|
return &DocumentExporter{config: config}, nil
|
|
}
|
|
|
|
func (exporter *DocumentExporter) Export(ctx context.Context, item releaseworkflow.PlanItem) (releaseworkflow.ExportedBundle, error) {
|
|
if !validDocumentPlan(item) {
|
|
return nil, errors.New("invalid document release plan")
|
|
}
|
|
sourceContract, err := exporter.config.Contract.Contract(ctx, item)
|
|
if err != nil || sourceContract.WeKnoraVersion != releasebundle.WeKnoraVersionV071 ||
|
|
sourceContract.MigrationVersion != releasebundle.WeKnoraMigrationV071 || sourceContract.TenantID != exporter.config.TenantID ||
|
|
sourceContract.KnowledgeBaseID != item.TestKnowledgeBaseID || sourceContract.RetrieverEngine != "elasticsearch_v8" ||
|
|
sourceContract.EmbeddingModel != exporter.config.EmbeddingModel || sourceContract.EmbeddingDimension != releasebundle.EmbeddingDimensionV071 {
|
|
return nil, errors.New("source WeKnora release contract mismatch")
|
|
}
|
|
postgresSnapshot, err := exporter.config.Postgres.Export(ctx, PostgresIdentity{
|
|
TenantID: exporter.config.TenantID, KnowledgeBaseID: item.TestKnowledgeBaseID, KnowledgeID: item.KnowledgeID,
|
|
})
|
|
if err != nil {
|
|
return nil, errors.New("export PostgreSQL document snapshot")
|
|
}
|
|
searchSnapshot, err := exporter.config.Search.Export(ctx, SearchIdentity{
|
|
TenantID: exporter.config.TenantID, KnowledgeBaseID: item.TestKnowledgeBaseID, KnowledgeID: item.KnowledgeID,
|
|
})
|
|
if err != nil {
|
|
return nil, errors.New("export Elasticsearch document snapshot")
|
|
}
|
|
if len(searchSnapshot.Documents) == 0 || !validLowerSHA256(searchSnapshot.MappingSHA256) || !validLowerSHA256(searchSnapshot.DocumentsSHA256) {
|
|
return nil, errors.New("Elasticsearch document snapshot is incomplete")
|
|
}
|
|
if searchSnapshot.MappingSHA256 != sourceContract.ElasticsearchMappingSHA256 {
|
|
return nil, errors.New("source Elasticsearch mapping contract mismatch")
|
|
}
|
|
objectSnapshot, err := exporter.config.Objects.Export(ctx, postgresSnapshot)
|
|
if err != nil {
|
|
return nil, errors.New("export object document snapshot")
|
|
}
|
|
if !validLowerSHA256(objectSnapshot.SHA256) {
|
|
return nil, errors.New("object document snapshot is incomplete")
|
|
}
|
|
tables := make([]releasebundle.TableDigest, 0, len(postgresSnapshot.Tables))
|
|
for _, table := range postgresSnapshot.Tables {
|
|
if !validReleaseTable(table.Name) || table.RowCount != len(table.Rows) || !validLowerSHA256(table.SHA256) {
|
|
return nil, errors.New("PostgreSQL document snapshot is incomplete")
|
|
}
|
|
tables = append(tables, releasebundle.TableDigest{Name: table.Name, RowCount: table.RowCount, SHA256: table.SHA256})
|
|
}
|
|
manifest, err := releasebundle.Seal(releasebundle.Manifest{
|
|
FormatVersion: releasebundle.FormatVersionV1, ExecutionID: item.ReleaseExecutionID,
|
|
PublicationID: item.PublicationID, SourceItemVersionID: item.SourceItemVersionID,
|
|
SourceRevision: item.SourceRevision, ParseRevision: item.ParseRevision, ReleaseRevision: item.ReleaseRevision,
|
|
CreatedAt: exporter.config.Now().UTC(),
|
|
Contract: releasebundle.Contract{
|
|
WeKnoraVersion: sourceContract.WeKnoraVersion, MigrationVersion: sourceContract.MigrationVersion,
|
|
TenantID: sourceContract.TenantID, KnowledgeBaseID: sourceContract.KnowledgeBaseID, KnowledgeID: item.KnowledgeID,
|
|
RetrieverEngine: sourceContract.RetrieverEngine, EmbeddingModel: sourceContract.EmbeddingModel,
|
|
EmbeddingDimension: sourceContract.EmbeddingDimension, ElasticsearchMappingSHA256: sourceContract.ElasticsearchMappingSHA256,
|
|
},
|
|
Tables: tables,
|
|
Search: releasebundle.SearchDigest{DocumentCount: len(searchSnapshot.Documents), SHA256: searchSnapshot.DocumentsSHA256},
|
|
Assets: releasebundle.AssetDigest{ObjectCount: len(objectSnapshot.Objects), TotalBytes: objectSnapshot.TotalBytes, SHA256: objectSnapshot.SHA256},
|
|
})
|
|
if err != nil {
|
|
return nil, errors.New("seal v0.7.1 document release bundle")
|
|
}
|
|
return &DocumentBundle{Manifest: manifest, Postgres: postgresSnapshot, Search: searchSnapshot, Objects: objectSnapshot}, nil
|
|
}
|
|
|
|
func validDocumentPlan(item releaseworkflow.PlanItem) bool {
|
|
for _, value := range []string{item.ReleaseExecutionID, item.PublicationID, item.SourceItemVersionID,
|
|
item.TestKnowledgeBaseID, item.KnowledgeID} {
|
|
if !validSearchIdentifier(value) {
|
|
return false
|
|
}
|
|
}
|
|
for _, value := range []string{item.SourceRevision, item.ParseRevision, item.ReleaseRevision} {
|
|
if value == "" || len(value) > 128 || value != strings.TrimSpace(value) || strings.ContainsAny(value, "\x00\r\n\t/?#\\") {
|
|
return false
|
|
}
|
|
}
|
|
return item.DecidedVisibility == "internal" || item.DecidedVisibility == "external"
|
|
}
|
|
|
|
func validReleaseTable(name string) bool {
|
|
switch name {
|
|
case "knowledges", "chunks", "knowledge_tags", "knowledge_tag_relations", "resources", "resource_bindings":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
var _ releaseworkflow.BundleExporter = (*DocumentExporter)(nil)
|