dcdc8d1ddd
全栈知识治理平台首次入库(P1-P10 + 发布链路模拟验证闭环): - 治理核心:源目录/变更集/人工审核/发布批次/发布执行(fail-closed 闸口) - 测试通道:WeKnora 测试 KB 上传/轮询/验收;poll 完成时写入 parser_generation/index_generation,修复 manual/seed 路径发布闸口永久拦截 - 发布适配器 v071(本地 source=测试角色 / 云端 target=生产角色 模拟验证通过): * ES 导出/导入 mapping 自适应(.keyword 子字段、可选 tenant_id) * OSS locator 支持嵌套 scheme;chunks.seq_id 由目标序列分配 * bundle 归档同身份重试原子替换;verifier 按 bundle chunk_type 计数 * 回滚 job payload 支持 prior_execution_id(strict 解码) - 三通道发布+回滚演练实测:publish 4/4 verified,检索命中, rollback 4/4 rolled_back,目标端 PG is_enabled=false、ES is_enabled=false - 前端 A·Data-console 风格全站统一,120 用例通过 - 门禁:go vet / go test 全包通过,gofmt 干净
117 lines
3.9 KiB
Go
117 lines
3.9 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"knowledge-governance/internal/platform/postgres"
|
|
)
|
|
|
|
// TestRealServicesHealthMatrix freezes docs plan P10: liveness and readiness
|
|
// are per-service and independent, worker liveness rides on queue heartbeats,
|
|
// and missing configuration is an explicit state — never "unknown".
|
|
func TestRealServicesHealthMatrix(t *testing.T) {
|
|
databaseURL := isolatedMetricsURL(t)
|
|
pool, err := pgxpool.New(context.Background(), databaseURL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer pool.Close()
|
|
ctx := context.Background()
|
|
if err := postgres.ApplyMigrations(ctx, pool); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
now := time.Date(2026, time.September, 12, 12, 0, 0, 0, time.UTC)
|
|
provider, err := NewRealProvider(RealProviderConfig{
|
|
DB: metricsPool{pool},
|
|
Now: func() time.Time { return now },
|
|
HTTPClient: &http.Client{Timeout: time.Second},
|
|
WorkerStaleAfter: 2 * time.Minute,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Before any worker claims: queue/postgres healthy, worker aggregated down.
|
|
health := provider.Services(ctx)
|
|
byKey := map[string]string{}
|
|
for _, item := range health {
|
|
byKey[item.Key+"/l"] = item.Liveness
|
|
byKey[item.Key+"/r"] = item.Readiness
|
|
}
|
|
if byKey["postgres/l"] != LiveUp || byKey["postgres/r"] != ReadReady {
|
|
t.Fatalf("postgres health = %v/%v", byKey["postgres/l"], byKey["postgres/r"])
|
|
}
|
|
if byKey["worker/l"] != LiveDown || byKey["worker/r"] != ReadBlocked {
|
|
t.Fatalf("worker health with no heartbeat = %v/%v", byKey["worker/l"], byKey["worker/r"])
|
|
}
|
|
if byKey["weknora/r"] != ReadNotConfigured || byKey["mineru/r"] != ReadNotConfigured {
|
|
t.Fatalf("unconfigured deps must be explicit: %v %v", byKey["weknora/r"], byKey["mineru/r"])
|
|
}
|
|
for _, key := range []string{"redis", "oss", "embedding", "llm"} {
|
|
if byKey[key+"/r"] != ReadNotApplicable || byKey[key+"/l"] != LiveUnknown {
|
|
t.Fatalf("not-deployed %s = %v/%v", key, byKey[key+"/l"], byKey[key+"/r"])
|
|
}
|
|
}
|
|
// Readiness must be blocked ONLY by configured+deployed failures: worker.
|
|
ok, blocked := overallReadiness(health)
|
|
if ok || len(blocked) != 1 || blocked[0] != "worker" {
|
|
t.Fatalf("overall readiness ok=%v blocked=%v", ok, blocked)
|
|
}
|
|
|
|
// A worker claim refreshes the heartbeat row and flips liveness independently.
|
|
store := postgres.NewStore(pool)
|
|
if _, err := store.Claim(ctx, "kg-test-worker", []string{"test.upload"}, now, time.Minute); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
health = provider.Services(ctx)
|
|
found := false
|
|
for _, item := range health {
|
|
if strings.HasPrefix(item.Key, "worker:") {
|
|
found = true
|
|
if item.Liveness != LiveUp || item.Readiness != ReadReady {
|
|
t.Fatalf("fresh worker health = %v/%v", item.Liveness, item.Readiness)
|
|
}
|
|
}
|
|
if item.Key == "worker" {
|
|
t.Fatalf("stale aggregate row must disappear once a worker is live")
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("live worker row missing after claim")
|
|
}
|
|
|
|
// Heartbeat older than the stale window: liveness down, and the API-side
|
|
// process itself stays independent (liveness of the API is never blocked).
|
|
if _, err := pool.Exec(ctx, `UPDATE worker_heartbeats SET last_seen_at = $1`, now.Add(-10*time.Minute)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
health = provider.Services(ctx)
|
|
staleDown := false
|
|
for _, item := range health {
|
|
if (item.Key == "worker" || strings.HasPrefix(item.Key, "worker:")) && item.Liveness == LiveDown {
|
|
staleDown = true
|
|
}
|
|
}
|
|
if !staleDown {
|
|
t.Fatal("stale heartbeat must mark worker down")
|
|
}
|
|
if ok, _ := overallReadiness(health); ok {
|
|
t.Fatal("stale worker must block readiness")
|
|
}
|
|
// Tracker history: postgres was seen successful and keeps no failures.
|
|
for _, item := range health {
|
|
if item.Key == "postgres" {
|
|
if item.LastSuccessAt == nil || item.ConsecutiveFailures != 0 {
|
|
t.Fatalf("postgres tracker history = %v failures=%d", item.LastSuccessAt, item.ConsecutiveFailures)
|
|
}
|
|
}
|
|
}
|
|
}
|