A cadence cap on the local-API whole-guest backup route (R-973, unreleased)
gates / gates (push) Successful in 49s
gates / gates (push) Successful in 49s
Each successful run prunes its tier, so a compromised controller calling POST /backup in a loop pushed out every pre-compromise restore point. A tier with a cadence of at most 24 h now allows 3 successful runs per rolling 24 h (night, a press, the next night); a longer tier 1 per max(cadence - 24 h, cadence/2), 6 days for the weekly off-site tier. Only successes count. A refusal is a 429, a WARN, saved, and for 24 h a critical host-report status that the hub turns into its existing operator-only agent_capability_degraded event. The success history and the refusal are kept in the R-894 state file, so a restart does not reset the cap. No sudoers change. Tests TestBackupCap_* and the state, status and wiring tests, red-proved.
This commit is contained in:
1 parent
0963bae3c3
commit
f17e1979bd
10 files changed
+722
-8
No files matched your search
@@ -39,13 +39,25 @@ type savedSuccess struct {
|
||||
target string
|
||||
vmid int
|
||||
at time.Time
|
||||
// recent (R-973) holds the start times of the newest successes (at most maxRecentSuccesses, newest
|
||||
// last) — what the local-API backup cadence cap counts, so an agent restart does not reset the cap.
|
||||
recent []time.Time
|
||||
// refusedAt (R-973) is the newest time the cap refused a backup of this guest on this tier.
|
||||
refusedAt time.Time
|
||||
}
|
||||
|
||||
// backupSuccessJSON is one entry on disk.
|
||||
// maxRecentSuccesses bounds the per-tier success history kept for the cadence cap (R-973). The cap
|
||||
// allows at most 3 runs in a window, so 8 is ample.
|
||||
const maxRecentSuccesses = 8
|
||||
|
||||
// backupSuccessJSON is one entry on disk. Recent and LastRefusedAt are additive (R-973): a file written
|
||||
// before them reads as "no history", and an older agent ignores them.
|
||||
type backupSuccessJSON struct {
|
||||
Target string `json:"target"`
|
||||
VMID int `json:"vmid"`
|
||||
StartedAt string `json:"started_at"`
|
||||
Target string `json:"target"`
|
||||
VMID int `json:"vmid"`
|
||||
StartedAt string `json:"started_at"`
|
||||
Recent []string `json:"recent,omitempty"`
|
||||
LastRefusedAt string `json:"last_refused_at,omitempty"`
|
||||
}
|
||||
|
||||
func backupStateKey(target string, vmid int) string { return target + "/" + strconv.Itoa(vmid) }
|
||||
@@ -67,7 +79,19 @@ func NewBackupSuccessState(path string) *BackupSuccessState {
|
||||
if perr != nil {
|
||||
continue // one unreadable entry must not lose the others
|
||||
}
|
||||
s.last[backupStateKey(e.Target, e.VMID)] = savedSuccess{target: e.Target, vmid: e.VMID, at: t.UTC()}
|
||||
ss := savedSuccess{target: e.Target, vmid: e.VMID, at: t.UTC()}
|
||||
for _, r := range e.Recent {
|
||||
if rt, rerr := time.Parse(time.RFC3339, r); rerr == nil {
|
||||
ss.recent = append(ss.recent, rt.UTC())
|
||||
}
|
||||
}
|
||||
if len(ss.recent) == 0 {
|
||||
ss.recent = []time.Time{ss.at} // a pre-R-973 file: its one known success counts
|
||||
}
|
||||
if rt, rerr := time.Parse(time.RFC3339, e.LastRefusedAt); rerr == nil {
|
||||
ss.refusedAt = rt.UTC()
|
||||
}
|
||||
s.last[backupStateKey(e.Target, e.VMID)] = ss
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -85,13 +109,72 @@ func (s *BackupSuccessState) RecordBackupSuccess(target string, b hub.Backup) er
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
k := backupStateKey(target, b.VMID)
|
||||
if old, ok := s.last[k]; ok && !t.After(old.at) {
|
||||
old, had := s.last[k]
|
||||
if had && !t.After(old.at) {
|
||||
return nil
|
||||
}
|
||||
s.last[k] = savedSuccess{target: target, vmid: b.VMID, at: t.UTC()}
|
||||
ss := savedSuccess{target: target, vmid: b.VMID, at: t.UTC(), refusedAt: old.refusedAt}
|
||||
ss.recent = append(append([]time.Time(nil), old.recent...), t.UTC())
|
||||
if len(ss.recent) > maxRecentSuccesses {
|
||||
ss.recent = ss.recent[len(ss.recent)-maxRecentSuccesses:]
|
||||
}
|
||||
s.last[k] = ss
|
||||
return s.saveLocked()
|
||||
}
|
||||
|
||||
// RecentBackupSuccesses returns the saved success start times for this tier and guest that are not
|
||||
// older than since (R-973 — the cadence cap's restart-proof count).
|
||||
func (s *BackupSuccessState) RecentBackupSuccesses(target string, vmid int, since time.Time) []time.Time {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
var out []time.Time
|
||||
for _, t := range s.last[backupStateKey(target, vmid)].recent {
|
||||
if !t.Before(since) {
|
||||
out = append(out, t)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// RecordBackupRefusal saves that the cadence cap refused a backup of this guest on this tier at at
|
||||
// (R-973), so the operator signal it raises survives an agent restart.
|
||||
func (s *BackupSuccessState) RecordBackupRefusal(target string, vmid int, at time.Time) error {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
k := backupStateKey(target, vmid)
|
||||
ss, ok := s.last[k]
|
||||
if !ok {
|
||||
return nil // nothing to cap against without a success; the cap never refuses then
|
||||
}
|
||||
if !at.After(ss.refusedAt) {
|
||||
return nil
|
||||
}
|
||||
ss.refusedAt = at.UTC()
|
||||
s.last[k] = ss
|
||||
return s.saveLocked()
|
||||
}
|
||||
|
||||
// LastBackupRefusal returns the newest saved cadence-cap refusal across every tier and guest.
|
||||
func (s *BackupSuccessState) LastBackupRefusal() (target string, vmid int, at time.Time, ok bool) {
|
||||
if s == nil {
|
||||
return "", 0, time.Time{}, false
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, e := range s.last {
|
||||
if !e.refusedAt.IsZero() && e.refusedAt.After(at) {
|
||||
target, vmid, at, ok = e.target, e.vmid, e.refusedAt, true
|
||||
}
|
||||
}
|
||||
return target, vmid, at, ok
|
||||
}
|
||||
|
||||
// LastKnownSuccess returns the newest saved success for this tier and guest (ok=false = none on file).
|
||||
func (s *BackupSuccessState) LastKnownSuccess(target string, vmid int) (time.Time, bool) {
|
||||
if s == nil {
|
||||
@@ -129,7 +212,14 @@ func (s *BackupSuccessState) KnownBackupSuccesses() []hub.Backup {
|
||||
func (s *BackupSuccessState) saveLocked() error {
|
||||
entries := make([]backupSuccessJSON, 0, len(s.last))
|
||||
for _, e := range s.last {
|
||||
entries = append(entries, backupSuccessJSON{Target: e.target, VMID: e.vmid, StartedAt: e.at.Format(time.RFC3339)})
|
||||
j := backupSuccessJSON{Target: e.target, VMID: e.vmid, StartedAt: e.at.Format(time.RFC3339)}
|
||||
for _, r := range e.recent {
|
||||
j.Recent = append(j.Recent, r.Format(time.RFC3339))
|
||||
}
|
||||
if !e.refusedAt.IsZero() {
|
||||
j.LastRefusedAt = e.refusedAt.Format(time.RFC3339)
|
||||
}
|
||||
entries = append(entries, j)
|
||||
}
|
||||
// Deterministic file content (Go's map order is random).
|
||||
sort.Slice(entries, func(i, j int) bool {
|
||||
|
||||
@@ -74,3 +74,40 @@ func TestBackupSuccessState_KnownBackupSuccessesSurviveAReopen(t *testing.T) {
|
||||
t.Fatal("uncovered_volumes must marshal as [], not null")
|
||||
}
|
||||
}
|
||||
|
||||
// R-973: the cadence cap's history and refusals survive a reopen; the history is bounded; a file
|
||||
// written before R-973 counts its one known success (never "no history" for a tier that has a copy).
|
||||
func TestBackupSuccessState_R973_RecentHistoryAndRefusalSurviveAReopen(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "backup-success-state.json")
|
||||
s := NewBackupSuccessState(path)
|
||||
base := time.Date(2026, 10, 18, 2, 30, 0, 0, time.UTC)
|
||||
for i := 0; i < 10; i++ {
|
||||
at := base.Add(time.Duration(i) * time.Hour)
|
||||
if err := s.RecordBackupSuccess("local", hub.Backup{VMID: 9201, Success: true, StartedAt: at.Format(time.RFC3339)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
refused := base.Add(11 * time.Hour)
|
||||
if err := s.RecordBackupRefusal("local", 9201, refused); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewBackupSuccessState(path)
|
||||
all := r.RecentBackupSuccesses("local", 9201, time.Time{})
|
||||
if len(all) != maxRecentSuccesses || !all[len(all)-1].Equal(base.Add(9*time.Hour)) {
|
||||
t.Fatalf("recent history after a reopen: %v (want the newest %d, ending at +9h)", all, maxRecentSuccesses)
|
||||
}
|
||||
if got := r.RecentBackupSuccesses("local", 9201, base.Add(7*time.Hour)); len(got) != 3 {
|
||||
t.Fatalf("successes since +7h: %d, want 3", len(got))
|
||||
}
|
||||
if tg, vm, at, ok := r.LastBackupRefusal(); !ok || tg != "local" || vm != 9201 || !at.Equal(refused) {
|
||||
t.Fatalf("refusal after a reopen: %q %d %v %v", tg, vm, at, ok)
|
||||
}
|
||||
|
||||
old := filepath.Join(t.TempDir(), "old.json")
|
||||
if err := os.WriteFile(old, []byte(`[{"target":"felhom-pbs","vmid":9201,"started_at":"2026-10-17T03:00:00Z"}]`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := NewBackupSuccessState(old).RecentBackupSuccesses("felhom-pbs", 9201, time.Time{}); len(got) != 1 {
|
||||
t.Fatalf("a pre-R-973 file must count its one success: %v", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user