ac3b395102
build / build (push) Successful in 1m21s
为什么:候选环境改用真实 WeKnora 表结构后,暴露出多处"按猜的列名/形状"的假设, 以及若干 varchar 长度、对象定位符、空索引基线问题,导致真实链路跑不通。 - knowledge_tag_relations 无 tenant_id/KB 列 → 归属走父表;resources 作用域=绑定+chunk 引用。 - 写入顺序按真实外键;无代理键表用内容摘要核对。 - 队列作业 ID / 版本记录 ID 改固定长度摘要(列宽 varchar(36)/varchar(64))。 - 对象定位符拆 storage://<backend>/oss://<bucket>/<key>;provider 可配置。 - 空索引基线全链路一致(Current/Export/Backup/Archive),非空仍严格比对映射。 - finalize 阶段:版本登记失败不报成功;崩溃接管复用已持久化预备份。 - 迁移 000069:同一目标的 sync/restore 跨 kind 互斥。
116 lines
5.0 KiB
Go
116 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"knowledge-governance/internal/appconfig"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
v071 "knowledge-governance/internal/releaseadapter/v071"
|
|
)
|
|
|
|
// buildLibraryProductionStack 用部署注入的配置装配生产整库目标与独立预备份存储。
|
|
// 任何一项缺失都返回明确原因,调用方据此把作业失败并把缺项写进阶段,
|
|
// 不用"未配置"的占位对象假装能跑。
|
|
func buildLibraryProductionStack(ctx context.Context, config appconfig.Config, scope, snapshotID, knownMapping string) (
|
|
*v071.ProductionLibraryTarget, *v071.ProductionBackupStore, error) {
|
|
missing := make([]string, 0, 4)
|
|
if config.LibraryProductionDatabaseURL == "" {
|
|
missing = append(missing, "LIBRARY_PRODUCTION_DATABASE_URL")
|
|
}
|
|
if config.LibraryProductionElasticURL == "" {
|
|
missing = append(missing, "LIBRARY_PRODUCTION_ELASTIC_URL")
|
|
}
|
|
if config.LibraryProductionTenantID == 0 {
|
|
missing = append(missing, "LIBRARY_PRODUCTION_TENANT_ID")
|
|
}
|
|
kb := config.LibraryProductionInternalKB
|
|
if scope == "external" {
|
|
kb = config.LibraryProductionExternalKB
|
|
}
|
|
if kb == "" {
|
|
missing = append(missing, "LIBRARY_PRODUCTION_KB_"+strings.ToUpper(scope))
|
|
}
|
|
if config.LibraryProductionS3Endpoint == "" || config.LibraryProductionS3Bucket == "" ||
|
|
config.LibraryProductionS3AccessKey == "" || config.LibraryProductionS3SecretKey == "" {
|
|
missing = append(missing, "LIBRARY_PRODUCTION_S3_ENDPOINT/BUCKET/ACCESS_KEY/SECRET_KEY")
|
|
}
|
|
if len(missing) > 0 {
|
|
return nil, nil, fmt.Errorf("生产整库目标未配置:缺少 %s", strings.Join(missing, "、"))
|
|
}
|
|
secure := strings.HasPrefix(config.LibraryProductionS3Endpoint, "https://")
|
|
objects, err := v071.NewS3ObjectStore(v071.S3ObjectStoreConfig{
|
|
Provider: config.LibraryProductionS3Provider, Endpoint: config.LibraryProductionS3Endpoint,
|
|
Region: config.LibraryProductionS3Region, Bucket: config.LibraryProductionS3Bucket,
|
|
AccessKeyID: config.LibraryProductionS3AccessKey, SecretAccessKey: config.LibraryProductionS3SecretKey,
|
|
Secure: secure, ForcePathStyle: true, MaxObjectBytes: 256 * 1024 * 1024,
|
|
})
|
|
if err != nil {
|
|
return nil, nil, errors.New("生产对象存储配置无效")
|
|
}
|
|
identity := v071.ProductionScope{TenantID: config.LibraryProductionTenantID, KnowledgeBaseID: kb}
|
|
target, err := v071.NewProductionLibraryTarget(ctx, v071.ProductionLibraryConfig{
|
|
Postgres: config.LibraryProductionDatabaseURL, SearchURL: config.LibraryProductionElasticURL,
|
|
SearchIndex: config.LibraryProductionIndex, Objects: objects, ObjectsReader: objects,
|
|
ArchiveRoot: config.LibraryArchiveRoot,
|
|
MaxDocuments: 200000,
|
|
}, identity)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
// 契约以"要同步的这份快照"为准:生产必须向它看齐,而不是反过来。
|
|
// 生产为空时索引可能根本不存在,这时不能拿生产映射当契约(会永远启动不了)。
|
|
archive, err := librarysnapshot.Open(config.LibraryArchiveRoot, 256*1024*1024)
|
|
if err != nil {
|
|
target.Close()
|
|
return nil, nil, fmt.Errorf("归档不可用,无法读取快照契约: %w", err)
|
|
}
|
|
manifest, err := archive.Load(snapshotID)
|
|
archive.Close()
|
|
if err != nil {
|
|
target.Close()
|
|
return nil, nil, fmt.Errorf("读取目标快照失败: %w", err)
|
|
}
|
|
contract := manifest.Compatibility
|
|
if contract.EmbeddingDimension == 0 {
|
|
contract.EmbeddingModel = config.LibraryEmbeddingModel
|
|
contract.EmbeddingDimension = config.LibraryEmbeddingDimension
|
|
}
|
|
// 生产索引存在时必须与快照契约一致;不存在或为空基线时跳过比对
|
|
// (空索引的映射没有可比性,非空之后仍然严格比对)。
|
|
if mapping, mapErr := target.Search().MappingSHA256(ctx); mapErr == nil {
|
|
// 映射基线:优先用"上一次生产生效版本登记的实际映射",
|
|
// 它才是这份生产索引的既有事实;快照契约只管"来自哪份数据"。
|
|
baseline := knownMapping
|
|
if baseline == "" {
|
|
baseline = contract.IndexMappingSHA256
|
|
}
|
|
if baseline != "" && mapping != baseline {
|
|
current, currentErr := target.Current(ctx)
|
|
emptyBaseline := currentErr == nil && current.Counts["index"] == 0 && current.Counts["chunks"] == 0
|
|
if !emptyBaseline {
|
|
target.Close()
|
|
return nil, nil, errors.New("生产索引映射与上次生效版本不一致(索引被改动过),先对齐索引映射")
|
|
}
|
|
}
|
|
}
|
|
// 预备份描述的是"生产现在的索引",不是"快照的来源索引":
|
|
// 生产映射基线优先用上次生效版本登记的实际值。
|
|
backupContract := contract
|
|
if knownMapping != "" {
|
|
backupContract.IndexMappingSHA256 = knownMapping
|
|
}
|
|
backups, err := v071.NewProductionBackupStore(v071.ProductionBackupConfig{
|
|
Pool: target.Pool(), Search: target.Search(), Objects: target.Objects(),
|
|
ArchiveRoot: config.LibraryArchiveRoot, Scope: scope, Identity: target.Identity(),
|
|
Compatibility: backupContract, MaxDocuments: 200000, MaxObjectByte: 256 * 1024 * 1024,
|
|
})
|
|
if err != nil {
|
|
target.Close()
|
|
return nil, nil, err
|
|
}
|
|
return target, backups, nil
|
|
}
|