This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
)
|
||||
|
||||
func TestLibraryReleaseRejectsAnotherTargetBeforeMaintenance(t *testing.T) {
|
||||
legacy, pool := openLedgerStore(t)
|
||||
config := appconfig.Config{LibraryTargetInstance: "new-cloud-target"}
|
||||
current := legacy.WithLibraryTarget(config.LibraryTargetIdentity())
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
job, _, err := legacy.CreateLibraryJob(ctx, governance.LibraryJob{ID: "old-target-job", Scope: "internal", Kind: governance.LibraryJobSync, IdempotencyKey: "old-target-job", RequestDigest: "digest", QueueJobID: "queue-old", TargetSnapshotID: "snapshot"}, governance.SyncStages(), now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload, _ := json.Marshal(libraryReleasePayload{LibraryJobID: job.ID, Scope: "internal", TargetSnapshotID: "snapshot"})
|
||||
err = runLibraryRelease(ctx, current, config, jobs.Job{ID: "queue-old", Payload: payload}, governance.LibraryJobSync)
|
||||
if err == nil || !strings.Contains(err.Error(), "拒绝在任何维护动作前继续") {
|
||||
t.Fatalf("must reject at admission, not after opening a target: %v", err)
|
||||
}
|
||||
var attempts, locks int
|
||||
if err := pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM library_attempts), (SELECT count(*) FROM library_instance_maintenance)`).Scan(&attempts, &locks); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if attempts != 0 || locks != 0 {
|
||||
t.Fatalf("foreign request caused side effects: attempts=%d locks=%d", attempts, locks)
|
||||
}
|
||||
got, err := legacy.LibraryJob(ctx, job.ID)
|
||||
if err != nil || got.State != governance.LibraryJobPending {
|
||||
t.Fatalf("old job was modified: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -142,7 +142,8 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
} else if len(pending) > 0 {
|
||||
return fmt.Errorf("治理库 schema 落后(待应用迁移 %v):本角色不拥有 DDL,先由控制面进程完成迁移", pending)
|
||||
}
|
||||
store := postgresstore.NewStore(pool)
|
||||
store := postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity())
|
||||
log.Printf("library target evidence identity=%s", config.LibraryTargetIdentity())
|
||||
// 启动先接管被中断的写作业(复用已持久化的预备份),再把剩下的孤儿判失败:
|
||||
// 顺序反了会把可接管的作业先标失败,丢掉已完成的备份。
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user