diff --git a/internal/objectgateway/acquire.go b/internal/objectgateway/acquire.go index 8dc959c..5de60b6 100644 --- a/internal/objectgateway/acquire.go +++ b/internal/objectgateway/acquire.go @@ -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 { diff --git a/internal/releaseadapter/v071/document_target.go b/internal/releaseadapter/v071/document_target.go index e390273..d08ad93 100644 --- a/internal/releaseadapter/v071/document_target.go +++ b/internal/releaseadapter/v071/document_target.go @@ -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) diff --git a/internal/releaseadapter/v071/gateway_authority.go b/internal/releaseadapter/v071/gateway_authority.go index 4175765..b3d41e1 100644 --- a/internal/releaseadapter/v071/gateway_authority.go +++ b/internal/releaseadapter/v071/gateway_authority.go @@ -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 { diff --git a/internal/releaseadapter/v071/library_index_effects.go b/internal/releaseadapter/v071/library_index_effects.go index 0f09628..9daeeab 100644 --- a/internal/releaseadapter/v071/library_index_effects.go +++ b/internal/releaseadapter/v071/library_index_effects.go @@ -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 } diff --git a/internal/releaseadapter/v071/library_index_guard.go b/internal/releaseadapter/v071/library_index_guard.go index 11139ad..e09f4ad 100644 --- a/internal/releaseadapter/v071/library_index_guard.go +++ b/internal/releaseadapter/v071/library_index_guard.go @@ -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 } } diff --git a/internal/releaseadapter/v071/library_index_plan.go b/internal/releaseadapter/v071/library_index_plan.go index 8b4c390..781517f 100644 --- a/internal/releaseadapter/v071/library_index_plan.go +++ b/internal/releaseadapter/v071/library_index_plan.go @@ -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 } diff --git a/internal/releaseadapter/v071/library_production_target.go b/internal/releaseadapter/v071/library_production_target.go index 7d2e8df..ae079a7 100644 --- a/internal/releaseadapter/v071/library_production_target.go +++ b/internal/releaseadapter/v071/library_production_target.go @@ -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 diff --git a/internal/releaseadapter/v071/search.go b/internal/releaseadapter/v071/search.go index a407fe2..68ac369 100644 --- a/internal/releaseadapter/v071/search.go +++ b/internal/releaseadapter/v071/search.go @@ -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 { diff --git a/internal/releaseadapter/v071/search_write.go b/internal/releaseadapter/v071/search_write.go index df9bdff..13bd404 100644 --- a/internal/releaseadapter/v071/search_write.go +++ b/internal/releaseadapter/v071/search_write.go @@ -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 {