hub v0.162.0 (built, not deployed): no repeat mail after a restart for a known-down host or an already-reported stale restore test (R-1001); the Day-0 agent list leaves out packages without the felhom-agent file, a vouch of one is refused (R-1011); an absent tunnel reads 'none set up' (R-1070)
gates / gates (push) Successful in 7m20s

This commit is contained in:
admin committed 2026-10-19 20:27:01 +02:00
1 parent a28f162c66
commit 5f31da04bc
16 files changed
+443 -17

No files matched your search

+41 -6
View File
@@ -49,16 +49,31 @@ func NewHostStalenessChecker(s *store.Store, threshold time.Duration, onEvent Ev
logger.Printf("[WARN] Host staleness checker: failed to seed states: %v", err)
return sc
}
var okCount, staleCount, downCount int
var okCount, staleCount, downCount, knownCount int
for _, row := range rows {
if s.IsCustomerBlocked(row.CustomerID) {
continue
}
sc.customerOf[row.HostID] = row.CustomerID
age := time.Since(row.LastReportAt)
// F2: seed only HEALTHY hosts. A host already stale/down at (re)start is left UNSEEDED so the
// first Check() observes oldState=="" and emits once (a box that went silent while the hub was
// down would otherwise stay silent). The dispatcher's 1h cooldown dedups the re-emit.
// F2: a host already stale/down at (re)start is left UNSEEDED so the first Check() observes oldState==""
// and emits once (a box that went silent while the hub was down would otherwise stay silent).
//
// R-1001 (hub 0.162.0): UNLESS the alarm was already raised for this silence — the newest host_stale /
// host_down event for the host is newer than its last report. Then the host is seeded with the state that
// event recorded, silently. The old comment said „the dispatcher's 1h cooldown dedups the re-emit"; it did
// not (host liveness events bypass that cooldown, `dispatcher.go` nodeLivenessEvents), so every hub restart
// re-mailed the operator about Tester 2, silent for days (measured 2026-10-13). A host seeded „stale" still
// emits host_down when it crosses downAfter — a new transition. A failed lookup leaves it unseeded (the
// alarm, never the silence). Pinned by TestHostStaleness_R1001_RestartDoesNotReMail.
if age > sc.threshold {
if known := sc.alreadyAlarmed(row); known != "" {
sc.states[row.HostID] = known
sc.downtimeStart[row.HostID] = row.LastReportAt
knownCount++
continue
}
}
switch {
case age > sc.downAfter:
downCount++
@@ -69,10 +84,30 @@ func NewHostStalenessChecker(s *store.Store, threshold time.Duration, onEvent Ev
okCount++
}
}
logger.Printf("[INFO] Host staleness checker initialized: %d ok, %d stale, %d down (stale/down left unseeded → first Check emits; host_stale after %s, host_down after %s)", okCount, staleCount, downCount, threshold, 2*threshold)
logger.Printf("[INFO] Host staleness checker initialized: %d ok, %d already alarmed (seeded silently, R-1001), %d stale, %d down (left unseeded → first Check emits; host_stale after %s, host_down after %s)", okCount, knownCount, staleCount, downCount, threshold, 2*threshold)
return sc
}
// alreadyAlarmed returns "stale" or "down" when the newest liveness event for the host is that alarm and is not older
// than the host's last report (the operator was told about THIS silence), else "".
func (sc *HostStalenessChecker) alreadyAlarmed(row store.HostStaleRow) string {
ev, err := sc.store.LatestHostLivenessEvent(row.CustomerID, row.HostID)
if err != nil {
sc.logger.Printf("[WARN] Host staleness: could not read the last alarm for %s (%v) — the first Check alarms (R-1001)", row.HostID, err)
return ""
}
if ev == nil || ev.CreatedAt.Before(row.LastReportAt) {
return ""
}
switch ev.EventType {
case "host_down":
return "down"
case "host_stale":
return "stale"
}
return ""
}
// Check evaluates all hosts and emits events on state transitions. Call every 60s.
func (sc *HostStalenessChecker) Check() {
rows, err := sc.store.GetHostStaleness()
@@ -125,7 +160,7 @@ func (sc *HostStalenessChecker) Check() {
if oldState == "" {
// F2: first observation (incl. a hub restart). Healthy seeds silently; a host already
// stale/down at first sight emits once (else a box that went silent while the hub was
// down would never alert). The dispatcher's 1h cooldown dedups a re-emit on hub restart.
// down would never alert). A host whose alarm was already raised was seeded at start (R-1001).
sc.states[row.HostID] = newState
if newState != "ok" {
sc.emitTransition(row.HostID, row.CustomerID, "unknown", newState, age)
+187
View File
@@ -0,0 +1,187 @@
package monitor
import (
"database/sql"
"io"
"log"
"path/filepath"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
_ "modernc.org/sqlite"
)
// R-1001 (measured 2026-10-13 18:33 UTC, hub v0.149.0 rollout): Tester 2, silent 216 h, got a fresh host_down and
// restore_test_stale mail on a hub restart. A restart is a NEW checker over the SAME database — that is what these
// tests build: alarm with checker 1, then a second checker on the same DB (the restarted hub) must not mail again,
// and a host that goes silent after the start must still mail once.
func r1001Store(t *testing.T) (*store.Store, *sql.DB) {
t.Helper()
path := filepath.Join(t.TempDir(), "r1001.db")
st, err := store.New(path, log.New(io.Discard, "", 0))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { st.Close() })
db, err := sql.Open("sqlite", path)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
return st, db
}
func r1001Host(t *testing.T, st *store.Store, db *sql.DB, cid, hid string, minutesAgo int) {
t.Helper()
if err := st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: cid, APIKey: "k-" + cid, RetrievalPassword: "p"}); err != nil {
t.Fatal(err)
}
if err := st.UpsertHost(&store.Host{HostID: hid, CustomerID: cid, APIKey: "hk-" + hid}); err != nil {
t.Fatal(err)
}
if err := st.SaveHostReport(hid, cid, []byte(`{}`), store.HostReportDenorm{}); err != nil {
t.Fatal(err)
}
backdate(t, db, hid, minutesAgo)
}
// COMPANION RED-PROOF (observed 2026-10-19): with the R-1001 seeding removed from NewHostStalenessChecker (the pre-fix
// F2 shape), the restart case fails with
//
// r1001_restart_test.go: the restarted hub re-sent [host_down] for a host whose silence was already alarmed
//
// Restored.
func TestHostStaleness_R1001_RestartDoesNotReMail(t *testing.T) {
st, db := r1001Store(t)
r1001Host(t, st, db, "tester-2", "Tester-2-be8404", 216*60) // down for 9 days
var got []string
on := func(_, et, _, _, _, _ string) { got = append(got, et) }
quiet := log.New(io.Discard, "", 0)
first := NewHostStalenessChecker(st, 30*time.Minute, on, quiet)
first.Check()
if len(got) != 1 || got[0] != "host_down" {
t.Fatalf("the first hub ever to see the silence must alarm once: %v", got)
}
got = nil
restarted := NewHostStalenessChecker(st, 30*time.Minute, on, quiet)
restarted.Check()
restarted.Check()
if len(got) != 0 {
t.Fatalf("the restarted hub re-sent %v for a host whose silence was already alarmed", got)
}
if s := restarted.GetState("Tester-2-be8404"); s != "down" {
t.Fatalf("the already-alarmed host must be seeded with its alarm's state, got %q", s)
}
// The box reports again → host_recovered once, and its next silence alarms again (a new run).
backdate(t, db, "Tester-2-be8404", 0)
restarted.Check()
if len(got) != 1 || got[0] != "host_recovered" {
t.Fatalf("a seeded host that reports again must recover once: %v", got)
}
got = nil
backdate(t, db, "Tester-2-be8404", 70)
again := NewHostStalenessChecker(st, 30*time.Minute, on, quiet) // a restart in the middle of a NEW silence
again.Check()
if len(got) != 1 || got[0] != "host_down" {
t.Fatalf("a new silence after a report must alarm (the old alarm is older than the report): %v", got)
}
}
// The other half of the rule: a host that goes down AFTER the start mails once; an alarm seeded „stale" still
// escalates to host_down when it crosses the down threshold (a new transition, not a repeat).
func TestHostStaleness_R1001_NewSilenceStillMails(t *testing.T) {
st, db := r1001Host2(t)
var got []string
on := func(_, et, _, _, _, _ string) { got = append(got, et) }
quiet := log.New(io.Discard, "", 0)
sc := NewHostStalenessChecker(st, 30*time.Minute, on, quiet)
sc.Check()
if len(got) != 0 {
t.Fatalf("a healthy host at start: no mail, got %v", got)
}
backdate(t, db, "h-new", 70) // down after the start
sc.Check()
if len(got) != 1 || got[0] != "host_down" {
t.Fatalf("a host down after the start must mail once: %v", got)
}
// stale alarmed, then a restart, then down → host_down (new transition), once.
got = nil
backdate(t, db, "h-new", 0)
sc.Check() // recovered
backdate(t, db, "h-new", 40)
sc.Check() // host_stale
got = nil
restarted := NewHostStalenessChecker(st, 30*time.Minute, on, quiet)
restarted.Check()
if len(got) != 0 {
t.Fatalf("restart during an alarmed stale: no mail, got %v", got)
}
backdate(t, db, "h-new", 70)
restarted.Check()
if len(got) != 1 || got[0] != "host_down" {
t.Fatalf("stale → down after the restart must mail host_down once: %v", got)
}
}
func r1001Host2(t *testing.T) (*store.Store, *sql.DB) {
st, db := r1001Store(t)
r1001Host(t, st, db, "c-new", "h-new", 0)
return st, db
}
// restore_test_stale across a restart: the same DB, a second checker. Both tiers unproven for 20 days.
//
// COMPANION RED-PROOF (observed 2026-10-19): with the staleAlreadyReported case removed from checkStaleness, it fails
// with
//
// r1001_restart_test.go: the restarted hub re-sent 2 restore_test_stale signal(s)
//
// Restored.
func TestRestoreTestStale_R1001_RestartDoesNotReMail(t *testing.T) {
st, db := r1001Store(t)
if err := st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "k", RetrievalPassword: "p"}); err != nil {
t.Fatal(err)
}
if err := st.UpsertHost(&store.Host{HostID: "h1", CustomerID: "c1", APIKey: "k1"}); err != nil {
t.Fatal(err)
}
now := time.Now().UTC()
if err := st.SaveHostReport("h1", "c1", []byte(rtReport(t, "", false, now, "")), store.HostReportDenorm{}); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`UPDATE host_reports SET received_at = datetime('now', '-20 days')`); err != nil {
t.Fatal(err)
}
var got []capturedEvent
first := rtChecker(st, &got, nil)
first.Check()
if len(got) != 2 {
t.Fatalf("two tiers unproven for 20 days: want 2 stale signals, got %v", got)
}
got = nil
restarted := rtChecker(st, &got, nil)
restarted.Check()
restarted.Check()
if len(got) != 0 {
t.Fatalf("the restarted hub re-sent %d restore_test_stale signal(s)", len(got))
}
// An earlier signal outside the read window does not hold the next one back.
if _, err := db.Exec(`UPDATE events SET created_at = datetime('now', '-15 days') WHERE event_type = ?`, EventRestoreTestStale); err != nil {
t.Fatal(err)
}
got = nil
later := rtChecker(st, &got, nil)
later.Check()
if len(got) != 2 {
t.Fatalf("a signal older than the read window must not silence the tier: got %v", got)
}
}
+24
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"strings"
"sync"
"time"
@@ -277,6 +278,9 @@ func (c *RestoreTestChecker) checkStaleness(customerID, latestJSON string, now t
c.mu.Unlock()
switch {
case state == "stale" && prev == "" && c.staleAlreadyReported(customerID, tier, proven[tier], now):
// R-1001: the first sweep after a hub restart — this tier's stale run was already reported.
c.logger.Printf("[INFO] restore-test staleness: %s %s tier still unproven, already reported before the restart — not re-sent (R-1001)", customerID, tier)
case state == "stale" && prev != "stale":
c.emit(customerID, EventRestoreTestStale, "warning", v.reason)
case v.verdict == verdictUnknown && prev == "":
@@ -287,6 +291,26 @@ func (c *RestoreTestChecker) checkStaleness(customerID, latestJSON string, now t
}
}
// staleAlreadyReported (R-1001, hub 0.162.0): the edge state lives in memory, so a hub restart forgot that a tier's
// stale signal was already sent and the first sweep re-sent it (measured 2026-10-13, Tester 2). The DB holds the
// answer: a restore_test_stale event for this tier („<tier> tier: …") inside the read window and newer than the
// tier's last proof means this run of staleness was reported. The window bound keeps it honest — a pass between that
// event and now would be inside the window and newer than it. An event older than the window, or a failed read, lets
// the signal through (the alarm, never the silence). Pinned by TestRestoreTestStale_R1001_RestartDoesNotReMail.
func (c *RestoreTestChecker) staleAlreadyReported(customerID, tier string, provenAt, now time.Time) bool {
evs, err := c.store.GetEventsByType(customerID, EventRestoreTestStale, now.Add(-restoreWindowRead))
if err != nil {
c.logger.Printf("[WARN] restore-test staleness: could not read the past signals for %s (%v) — sending (R-1001)", customerID, err)
return false
}
for _, e := range evs {
if strings.HasPrefix(e.Message, tier+" tier:") && e.CreatedAt.After(provenAt) {
return true
}
}
return false
}
// assessRestoreProven is the per-tier verdict. PURE (now and the window injected) so the policy is
// unit-tested — the property that made R-81 provable, kept deliberately.
//