fix(library): 0.9.0 索引写入/归属校验不补 tenant_id,冲突可定位
build / build (push) Successful in 1m56s

- SearchIdentity 增加 NoTenantField,按目标 profile 决定是否注入 tenant_id
  (写入、归属预检、恢复搬运三条路径)
- 归属冲突与写集合不匹配写明具体字段,排查不用再猜
- 网关侧 acquire 冲突打印 plan/claim/owner 三项
This commit is contained in:
dev
2026-11-17 11:59:45 +08:00
parent ecf518e3bb
commit cbc6b981c6
9 changed files with 29 additions and 7 deletions
+4
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"github.com/jackc/pgx/v5"
"log"
"math"
)
@@ -39,6 +40,9 @@ func (g *Gateway) Acquire(ctx context.Context, c Claim) (Grant, error) {
return Grant{}, e
}
if plan.Digest != c.PlanDigest || plan.Owner != Digest([]byte(c.JobID)) {
// 冲突原因要能在服务端日志里定位:是谁的 plan、对不上哪一项。
log.Printf("gateway acquire conflict job=%s op=%s plan=%s claim=%s owner_ok=%t",
c.JobID, c.OperationID, plan.Digest, c.PlanDigest, plan.Owner == Digest([]byte(c.JobID)))
return Grant{}, ErrConflict
}
if e = g.InstallPlan(ctx, plan); e != nil {
@@ -246,7 +246,9 @@ func (target *DocumentTarget) restoreBundle(ctx context.Context, item releasewor
// bundle. The bundle's own contract keeps the SOURCE base it was exported from.
func targetIdentities(item releaseworkflow.PlanItem, manifest releasebundle.Manifest) (PostgresIdentity, SearchIdentity) {
postgresIdentity := PostgresIdentity{TenantID: manifest.Contract.TenantID, KnowledgeBaseID: item.TargetBase(), KnowledgeID: manifest.Contract.KnowledgeID}
return postgresIdentity, SearchIdentity(postgresIdentity)
// 单文档发布路径沿用 0.7.1 契约(索引带 tenant_id)。
return postgresIdentity, SearchIdentity{TenantID: postgresIdentity.TenantID,
KnowledgeBaseID: postgresIdentity.KnowledgeBaseID, KnowledgeID: postgresIdentity.KnowledgeID}
}
var _ releaseworkflow.SidecarTarget = (*DocumentTarget)(nil)
@@ -8,6 +8,7 @@ import (
"knowledge-governance/internal/governance"
"knowledge-governance/internal/libraryrelease"
"knowledge-governance/internal/objectgateway"
"log"
"path/filepath"
)
@@ -73,13 +74,18 @@ func (a *GatewayGovernanceAuthority) Load(ctx context.Context, c objectgateway.C
return objectgateway.Plan{}, objectgateway.ErrPersistence
}
if matches != 1 {
log.Printf("gateway authority: claim matches=%d job=%s op=%s epoch=%d", matches, c.JobID, c.OperationID, c.AttemptEpoch)
return objectgateway.Plan{}, objectgateway.ErrConflict
}
set, e := LoadWriteSet(a.ArchiveRoot, c.PlanDigest)
if e != nil || set == nil {
log.Printf("gateway authority: writeset %s unavailable: %v", c.PlanDigest, e)
return objectgateway.Plan{}, objectgateway.ErrConflict
}
if set.Version != libraryrelease.MigrationWriteSetVersion || set.JobID != c.JobID || set.Target != scope || set.BackupSnapshotID != backup || set.TargetSnapshotID != snapshot {
log.Printf("gateway authority: writeset mismatch version=%d/%d job=%s/%s target=%s/%s backup=%s/%s target_snap=%s/%s",
set.Version, libraryrelease.MigrationWriteSetVersion, set.JobID, c.JobID, set.Target, scope,
set.BackupSnapshotID, backup, set.TargetSnapshotID, snapshot)
return objectgateway.Plan{}, objectgateway.ErrConflict
}
for _, obj := range set.Objects {
@@ -86,7 +86,7 @@ func (t *ProductionLibraryTarget) loadIndexEffect(c libraryrelease.IndexWrite, p
return &e, nil
}
func (t *ProductionLibraryTarget) observePlannedIndex(ctx context.Context, c libraryrelease.IndexWrite) (indexWriteGuard, error) {
guards, err := t.search.indexWriteGuards(ctx, []librarysnapshot.IndexDocument{{ID: c.ID}}, SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID})
guards, err := t.search.indexWriteGuards(ctx, []librarysnapshot.IndexDocument{{ID: c.ID}}, SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID, NoTenantField: !t.schema.IndexTenantField})
if err != nil {
return indexWriteGuard{}, err
}
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"sort"
@@ -64,7 +65,10 @@ func (e *SearchExporter) indexWriteGuards(ctx context.Context, documents []libra
Tenant uint64 `json:"tenant_id"`
KB string `json:"knowledge_base_id"`
}
if json.Unmarshal(doc.Source, &owner) != nil || owner.Tenant != identity.TenantID || owner.KB != identity.KnowledgeBaseID {
if json.Unmarshal(doc.Source, &owner) != nil || owner.KB != identity.KnowledgeBaseID ||
(!identity.NoTenantField && owner.Tenant != identity.TenantID) {
log.Printf("index guard conflict id=%s owner_kb=%q want_kb=%q owner_tenant=%d want_tenant=%d no_tenant_field=%t",
doc.ID, owner.KB, identity.KnowledgeBaseID, owner.Tenant, identity.TenantID, identity.NoTenantField)
return nil, fmt.Errorf("index-level ID conflict: %s is not owned by target tenant/KB", doc.ID)
}
if doc.SeqNo == nil || doc.PrimaryTerm == nil {
@@ -115,7 +119,7 @@ func (t *ProductionLibraryTarget) preflightArchiveIndex(ctx context.Context, arc
docs = append(docs, librarysnapshot.IndexDocument{ID: id, Source: mapped[id]})
}
}
if _, err := t.search.indexWriteGuards(ctx, docs, SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID}); err != nil {
if _, err := t.search.indexWriteGuards(ctx, docs, SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID, NoTenantField: !t.schema.IndexTenantField}); err != nil {
return err
}
}
@@ -95,7 +95,7 @@ func (t *ProductionLibraryTarget) planIndex(ctx context.Context, archive *librar
for _, id := range sorted {
docs = append(docs, librarysnapshot.IndexDocument{ID: id})
}
guards, err := t.search.indexWriteGuards(ctx, docs, SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID})
guards, err := t.search.indexWriteGuards(ctx, docs, SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID, NoTenantField: !t.schema.IndexTenantField})
if err != nil {
return err
}
@@ -809,7 +809,7 @@ func (t *ProductionLibraryTarget) importIndex(ctx context.Context, archive *libr
}
}
// 直接搬运原始文档与向量:不解析、不重新 Embedding。
identity := SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID}
identity := SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID, NoTenantField: !t.schema.IndexTenantField}
write := t.search.BulkIndex
if t.activePlan != nil {
write = t.search.BulkIndexGuarded
+3
View File
@@ -22,6 +22,9 @@ type SearchIdentity struct {
TenantID uint64
KnowledgeBaseID string
KnowledgeID string
// NoTenantField 表示该版本的索引文档不带 tenant_id(0.9.0 就是这样)。
// 归属校验只能比 knowledge_base_id,否则"本来就是自己的文档"会被判冲突。
NoTenantField bool
}
type SearchDocument struct {
+4 -1
View File
@@ -143,7 +143,10 @@ func (exporter *SearchExporter) bulkIndex(ctx context.Context, documents []libra
if err := json.Unmarshal(document.Source, &source); err != nil {
return errors.New("archived index document is not an object")
}
source["tenant_id"], _ = json.Marshal(identity.TenantID)
// 0.9.0 的索引文档没有 tenant_id:补上去会让恢复后的文档与目标契约不一致。
if !identity.NoTenantField {
source["tenant_id"], _ = json.Marshal(identity.TenantID)
}
source["knowledge_base_id"], _ = json.Marshal(identity.KnowledgeBaseID)
encoded, err := json.Marshal(source)
if err != nil {