为什么:候选环境改用真实 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 互斥。
This commit is contained in:
@@ -37,7 +37,8 @@ RETURNING id`,
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
if pgErr.ConstraintName == "library_jobs_active" {
|
||||
// 同一目标的写作业(sync/restore)共用一个唯一索引,跨 kind 互斥。
|
||||
if pgErr.ConstraintName == "library_jobs_write_target" || pgErr.ConstraintName == "library_jobs_active" {
|
||||
return governance.LibraryJob{}, false, governance.ErrLibraryJobActive
|
||||
}
|
||||
}
|
||||
@@ -246,6 +247,40 @@ ON CONFLICT (id) DO NOTHING`,
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// InterruptedLibraryJobs 找出"业务作业 running、但队列里已没有活跃作业"的作业:
|
||||
// worker 被杀/重启后它们需要被接管,而不是被当成失败丢掉已完成的预备份。
|
||||
func (s *Store) InterruptedLibraryJobs(ctx context.Context) ([]governance.LibraryJob, error) {
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT id FROM library_jobs lj
|
||||
WHERE lj.state='running' AND lj.kind IN ('sync','restore')
|
||||
AND NOT EXISTS (SELECT 1 FROM jobs j WHERE j.id=lj.queue_job_id AND j.state IN ('pending','running','retry'))
|
||||
ORDER BY lj.created_at`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list interrupted library jobs: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
ids := make([]string, 0, 4)
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jobs := make([]governance.LibraryJob, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
job, err := s.LibraryJob(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jobs = append(jobs, job)
|
||||
}
|
||||
return jobs, nil
|
||||
}
|
||||
|
||||
// ReconcileLibraryJobs 收拾"业务作业还在飞、队列里已经没有对应作业"的孤儿:
|
||||
// 它们会永久占用 (scope,kind) 唯一索引,让同一目标再也发不出作业。
|
||||
// 只标失败并写明恢复责任,不猜结果、不重放。
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
DROP INDEX IF EXISTS library_jobs_write_target;
|
||||
CREATE UNIQUE INDEX library_jobs_active ON library_jobs(scope, kind)
|
||||
WHERE state IN ('pending','running');
|
||||
@@ -0,0 +1,6 @@
|
||||
-- 同一目标的写作业(同步与恢复)必须互斥:两种 kind 同时写同一目标会互相覆盖。
|
||||
-- 原来的唯一索引是 (scope, kind),允许 sync 与 restore 同时存在,这里改成只按 scope。
|
||||
DROP INDEX IF EXISTS library_jobs_active;
|
||||
CREATE UNIQUE INDEX library_jobs_write_target
|
||||
ON library_jobs(scope)
|
||||
WHERE state IN ('pending','running') AND kind IN ('sync','restore');
|
||||
@@ -11,26 +11,25 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("loadMigrations() error = %v", err)
|
||||
}
|
||||
if len(migrations) != 68 {
|
||||
t.Fatalf("migration count = %d, want 68", len(migrations))
|
||||
if len(migrations) != 69 {
|
||||
t.Fatalf("migration count = %d, want 69", len(migrations))
|
||||
}
|
||||
if tail := migrations[len(migrations)-1]; tail.Version != 68 ||
|
||||
!strings.Contains(tail.UpSQL, "restore") {
|
||||
t.Fatalf("migration 68 = %#v", tail)
|
||||
if tail := migrations[len(migrations)-1]; tail.Version != 69 ||
|
||||
!strings.Contains(tail.UpSQL, "library_jobs_write_target") {
|
||||
t.Fatalf("migration 69 = %#v", tail)
|
||||
}
|
||||
if last := migrations[len(migrations)-2]; last.Version != 67 ||
|
||||
if restore := migrations[len(migrations)-2]; restore.Version != 68 || !strings.Contains(restore.UpSQL, "restore") {
|
||||
t.Fatalf("migration 68 = %#v", restore)
|
||||
}
|
||||
if last := migrations[len(migrations)-3]; last.Version != 67 ||
|
||||
!strings.Contains(last.UpSQL, "library_release_authorizations") ||
|
||||
!strings.Contains(last.UpSQL, "params_digest") {
|
||||
t.Fatalf("migration 67 = %#v", last)
|
||||
}
|
||||
if prev := migrations[len(migrations)-3]; prev.Version != 66 ||
|
||||
!strings.Contains(prev.UpSQL, "CREATE TABLE library_versions") ||
|
||||
!strings.Contains(prev.UpSQL, "library_jobs_active") {
|
||||
t.Fatalf("migration 66 = %#v", prev)
|
||||
}
|
||||
if auth := migrations[len(migrations)-2]; auth.Version != 67 ||
|
||||
!strings.Contains(auth.UpSQL, "library_release_authorizations") {
|
||||
t.Fatalf("migration 67 = %#v", auth)
|
||||
if flow := migrations[len(migrations)-4]; flow.Version != 66 ||
|
||||
!strings.Contains(flow.UpSQL, "CREATE TABLE library_versions") ||
|
||||
!strings.Contains(flow.UpSQL, "library_jobs_active") {
|
||||
t.Fatalf("migration 66 = %#v", flow)
|
||||
}
|
||||
if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") {
|
||||
t.Fatalf("migration 1 = %#v", migrations[0])
|
||||
|
||||
Reference in New Issue
Block a user