feat(backup): 只读盘点真实批次、子库路径与容量
为什么:28189 验收里历史批次/子库路径/容量都没有真实接口,页面只能占位; 用户要求修实际接口而不是继续改文案,同时不得写主机或触发备份。 怎么改:新增 GET /api/v1/backup-schedule/inventory,只读盘点 statfs 实测容量、 st_blocks 实际占盘、全部批次与 MANIFEST 状态、每个子库完整主机路径。目录必须 只读挂载;用设备号区分挂载文件系统与容器根盘,同设备时不给主机容量。清单 missing/invalid、未接线、不可读分别展示。SSH 计划读回与盘点独立加载。 前端消费该接口:整盘色带、批次列表、子库路径复制、清单失败与容器根盘提示。 怎么验:Go 单测覆盖清单/占盘/符号链接/容器根盘;handler 契约;appconfig 接线校验; vitest 627 项、tsc、vite build、prettier 通过;本地页面夹具核对 8 批与路径复制, 1440/1024/768 无横向溢出。未连接真实主机、未执行备份、未改真实计划。
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"knowledge-governance/internal/backupinventory"
|
||||
)
|
||||
|
||||
// BackupInventoryReader 只读盘点备份主机目录。它与 SSH 主机计划读回完全独立:
|
||||
// 挂载没接线不应拖垮计划读回,计划读回失败也不应把历史批次说成"没有"。
|
||||
type BackupInventoryReader interface {
|
||||
Available() bool
|
||||
Snapshot(context.Context) backupinventory.Snapshot
|
||||
}
|
||||
|
||||
// handleBackupInventory 返回真实批次、子库路径与实测容量。
|
||||
// 未接线/目录不可读/清单损坏都作为 200 的字段返回,由页面区分原因;
|
||||
// 只有投影本身失败才 500。
|
||||
func (config Config) handleBackupInventory(w http.ResponseWriter, r *http.Request) {
|
||||
snapshot := config.BackupInventory.Snapshot(r.Context())
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": snapshot})
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/backupinventory"
|
||||
)
|
||||
|
||||
type fakeBackupInventory struct {
|
||||
snapshot backupinventory.Snapshot
|
||||
}
|
||||
|
||||
func (fake fakeBackupInventory) Available() bool { return fake.snapshot.Wired }
|
||||
|
||||
func (fake fakeBackupInventory) Snapshot(context.Context) backupinventory.Snapshot {
|
||||
return fake.snapshot
|
||||
}
|
||||
|
||||
func TestBackupInventoryProjectionPassesRealFields(t *testing.T) {
|
||||
config := Config{BackupInventory: fakeBackupInventory{snapshot: backupinventory.Snapshot{
|
||||
Wired: true,
|
||||
Root: "/var/lib/knowledge-governance/backup-host/rds-logical",
|
||||
HostRoot: "/srv/kb-backups/rds-logical",
|
||||
Source: "weknora-test:/srv/kb-backups/rds-logical",
|
||||
Mount: backupinventory.MountInfo{
|
||||
MountVerified: true,
|
||||
ReadOnly: true,
|
||||
Evidence: "挂载设备 20 与容器根盘 10 不同",
|
||||
},
|
||||
Capacity: backupinventory.Capacity{Measured: true, TotalBytes: 100, FreeBytes: 40, AvailableBytes: 35, UsedBytes: 60},
|
||||
Batches: []backupinventory.Batch{{
|
||||
Stamp: "20261118T193001Z",
|
||||
HostPath: "/srv/kb-backups/rds-logical/20261118T193001Z",
|
||||
ManifestState: backupinventory.ManifestOK,
|
||||
ActualBytes: 1024,
|
||||
Databases: []backupinventory.Database{{
|
||||
Name: "weknora_v090prod",
|
||||
HostPath: "/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir",
|
||||
Present: true,
|
||||
ActualBytes: 512,
|
||||
}},
|
||||
}},
|
||||
Totals: backupinventory.Totals{BatchCount: 1, LatestStamp: "20261118T193001Z"},
|
||||
}}}
|
||||
recorder := httptest.NewRecorder()
|
||||
config.handleBackupInventory(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/inventory", nil))
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", recorder.Code)
|
||||
}
|
||||
var payload struct {
|
||||
Data backupinventory.Snapshot `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !payload.Data.Wired || !payload.Data.Mount.MountVerified || !payload.Data.Capacity.Measured {
|
||||
t.Fatalf("接线/挂载/容量字段丢失: %+v", payload.Data)
|
||||
}
|
||||
if len(payload.Data.Batches) != 1 ||
|
||||
payload.Data.Batches[0].Databases[0].HostPath != "/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir" {
|
||||
t.Fatalf("批次/子库真实路径丢失: %+v", payload.Data.Batches)
|
||||
}
|
||||
if payload.Data.Batches[0].ActualBytes != 1024 {
|
||||
t.Fatalf("实际占盘字段丢失: %+v", payload.Data.Batches[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupInventoryUnconfiguredIsNotAnError(t *testing.T) {
|
||||
config := Config{BackupInventory: backupinventory.New(backupinventory.Config{})}
|
||||
recorder := httptest.NewRecorder()
|
||||
config.handleBackupInventory(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/inventory", nil))
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("未接线不应 500: %d", recorder.Code)
|
||||
}
|
||||
var payload struct {
|
||||
Data backupinventory.Snapshot `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.Data.Wired || payload.Data.Error == "" || payload.Data.Batches == nil {
|
||||
t.Fatalf("未接线必须给原因且批次为空数组: %+v", payload.Data)
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/backuphost"
|
||||
"knowledge-governance/internal/backupinventory"
|
||||
"knowledge-governance/internal/dingtalkdiscovery"
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
@@ -198,10 +199,13 @@ type Config struct {
|
||||
// 它不是运行时开关:生产部署不设置它,那次探测仍然直连目标库逐表计数。
|
||||
LibraryScopeProbe func(ctx context.Context, scope string) (bool, map[string]int64, error)
|
||||
// LibraryRetentionVersions/Days 是保留策略;都为 0 = 未设置(不生成清理候选)。
|
||||
LibraryRetentionVersions int
|
||||
LibraryRetentionDays int
|
||||
BackupSchedule BackupScheduleStore
|
||||
BackupHost BackupHost
|
||||
LibraryRetentionVersions int
|
||||
LibraryRetentionDays int
|
||||
BackupSchedule BackupScheduleStore
|
||||
BackupHost BackupHost
|
||||
// BackupInventory 读的是只读挂载的 rds-logical 目录(批次/子库/实测容量)。
|
||||
// 与 BackupHost 的 SSH 计划读回分离,任一侧失败都不能影响另一侧。
|
||||
BackupInventory BackupInventoryReader
|
||||
BackupRuns *BackupRunTracker
|
||||
ProductionBindings ProductionBindingStore
|
||||
ProductionKnowledgeBases ProductionKnowledgeBaseLister
|
||||
@@ -251,6 +255,10 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
// 未挂载主机凭据:Available()=false,页面显示"未接线"而不是 500。
|
||||
config.BackupHost = backuphost.New(backuphost.Config{})
|
||||
}
|
||||
if config.BackupInventory == nil {
|
||||
// 未挂载只读备份目录:批次/容量显示"未接线",独立于 SSH 计划读回。
|
||||
config.BackupInventory = backupinventory.New(backupinventory.Config{})
|
||||
}
|
||||
for _, knowledgeBaseID := range []string{config.InternalRoutingKnowledgeBaseID, config.ExternalRoutingKnowledgeBaseID} {
|
||||
if knowledgeBaseID != "" && !validCatalogID(knowledgeBaseID) {
|
||||
return nil, errors.New("routing knowledge base ID is invalid")
|
||||
@@ -431,6 +439,9 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
}
|
||||
config.handleBackupScheduleHost(w, r)
|
||||
})
|
||||
mux.HandleFunc("GET /api/v1/backup-schedule/inventory", func(w http.ResponseWriter, r *http.Request) {
|
||||
config.handleBackupInventory(w, r)
|
||||
})
|
||||
mux.HandleFunc("PUT /api/v1/backup-schedule", func(w http.ResponseWriter, r *http.Request) {
|
||||
if config.BackupSchedule == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "backup schedule storage unavailable"})
|
||||
|
||||
Reference in New Issue
Block a user