package v071 import ( "context" "crypto/sha256" "encoding/hex" "errors" "fmt" "strings" ) // 恢复派发只接受"实际目标"的身份:租户/KB、目标库(服务器+库名)、索引名与索引 // UUID、对象桶。它们用于识别"同名但被重建/替换"的目标,让旧绑定无法复用;它们不是 // 历史未发送的证明,也不构成 ES 端 fence。 func (t *ProductionLibraryTarget) observeRestoreTarget(ctx context.Context) (string, string, error) { if t.pool == nil || t.search == nil { return "", "", errors.New("restore dispatch requires a live target") } var database, address string var port int if err := t.pool.QueryRow(ctx, `SELECT current_database(), COALESCE(host(inet_server_addr()), ''), COALESCE(inet_server_port(), 0)`). Scan(&database, &address, &port); err != nil { return "", "", fmt.Errorf("read target database identity: %w", err) } if database == "" { return "", "", errors.New("target database identity is empty") } indexUUID, err := t.search.IndexUUID(ctx) if err != nil { return "", "", fmt.Errorf("read target index uuid: %w", err) } joined := strings.Join([]string{ "v1", fmt.Sprintf("%d", t.scope.TenantID), t.scope.KnowledgeBaseID, database, address, fmt.Sprintf("%d", port), t.config.SearchIndex, indexUUID, t.config.ObjectsBucket, }, "\x00") sum := sha256.Sum256([]byte(joined)) return hex.EncodeToString(sum[:]), indexUUID, nil } // frozenRestoreTarget 在一个 Restore 调用内冻结实际目标身份;后续每次重建绑定都 // 重新观察并与冻结值比较。目标/索引在恢复中途被替换时立即失败,而不是继续用旧绑定。 func (t *ProductionLibraryTarget) frozenRestoreTarget(ctx context.Context) (string, string, error) { fingerprint, indexUUID, err := t.observeRestoreTarget(ctx) if err != nil { return "", "", err } if t.restoreFingerprint == "" { t.restoreFingerprint, t.restoreIndexUUID = fingerprint, indexUUID return fingerprint, indexUUID, nil } if fingerprint != t.restoreFingerprint || indexUUID != t.restoreIndexUUID { return "", "", fmt.Errorf("%w: 目标/索引身份在恢复过程中发生变化", ErrProductionTargetUnconfigured) } return t.restoreFingerprint, t.restoreIndexUUID, nil }