carlos / platform Public

host: carry one tick's shed count across failed publishes, not a sum

Review round 2 (#1535). The watch carries the largest reading of the
failed ticks since the last success, so a pending fire survives a failed
publish but a trickle over a long outage never adds up to one.
backhaul_shed keeps the reading that fired as its value while firing,
as every other rule does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 83b6a65fd4856484a2a9e84fa51cc4afbc2378ae parent 0ac9f0e
3 files changed, +34 −15
  • internal/host/metrics_backhaul_test.go +8 −3
  • internal/host/metrics_loop.go +24 −12
  • internal/metrics/alert.go +2 −0
diff --git a/internal/host/metrics_backhaul_test.go b/internal/host/metrics_backhaul_test.go
index 66cc3c5c5..de778b2f9 100644
--- a/internal/host/metrics_backhaul_test.go
+++ b/internal/host/metrics_backhaul_test.go
@@ -89,8 +89,8 @@ func TestSamplerCarriesBackhaulSheds(t *testing.T) {
}
}
-// The watch reads saturation refusals since the last committed tick, and
-// commits only when the tick it fed succeeded.
+// The watch reads saturation refusals since the last tick, carries the
+// largest reading across failed ticks, and clears the carry on a success.
func TestBackhaulShedWatch(t *testing.T) {
var w backhaulShedWatch
var got []any
@@ -113,7 +113,12 @@ func TestBackhaulShedWatch(t *testing.T) {
feed(&metrics.BackhaulShedSample{Requests: 5, Streams: 20, FairShare: 90}, true)
feed(&metrics.BackhaulShedSample{Requests: 5, Streams: 20, FairShare: 90}, false)
feed(&metrics.BackhaulShedSample{Requests: 5, Streams: 20, FairShare: 99}, false)
- want := []any{nil, 5.0, 20.0, 20.0, 0.0}
+ // A long outage with a trickle: 6 a minute never adds up to a fire.
+ for i, c := range []uint64{26, 32, 38} {
+ feed(&metrics.BackhaulShedSample{Requests: 5, Streams: c}, i < 2)
+ }
+ feed(&metrics.BackhaulShedSample{Requests: 5, Streams: 38}, false)
+ want := []any{nil, 5.0, 20.0, 20.0, 0.0, 6.0, 6.0, 6.0, 0.0}
if len(got) != len(want) {
t.Fatalf("readings = %v, want %v", got, want)
}
diff --git a/internal/host/metrics_loop.go b/internal/host/metrics_loop.go
index df86fe3c0..3dc4ee270 100644
--- a/internal/host/metrics_loop.go
+++ b/internal/host/metrics_loop.go
@@ -658,24 +658,36 @@ type alertReadings struct {
// backhaulShedWatch turns the edge's cumulative counters into the
// backhaul_shed rule's per-tick reading. One for the life of the loop.
-type backhaulShedWatch struct{ prev *metrics.BackhaulShedSample }
-
-// tick gives run the refusals cur counts that the last committed reading
-// did not (nil when cur is nil: the edge is not ready, and the rule holds),
-// and commits cur only when run succeeds. run is alertTick, which keeps its
-// committed state when the publish fails, so a firing transition that did
-// not publish is evaluated again next tick with the same refusals still in
-// the count, rather than lost to a quiet minute. A failed publish for
-// another rule holds the count back the same way, so the next tick may
-// count two minutes; that errs toward firing.
+type backhaulShedWatch struct {
+ prev *metrics.BackhaulShedSample
+ // carry is the largest reading of the ticks since the last one that
+ // succeeded: what a failed publish still owes the rule.
+ carry float64
+}
+
+// tick gives run the refusals cur counts since the last tick (nil when cur
+// is nil: the edge is not ready, and the rule holds), or the carried
+// reading if that is larger. run is alertTick, which keeps its committed
+// state when the publish fails. A failed tick carries its reading, so a
+// firing transition that did not publish is evaluated again next tick with
+// the same count rather than lost to a quiet minute. The carry is a maximum
+// and never a sum: however long the store is down, the reading is never
+// more than one tick's count, so a trickle over an outage cannot add up to
+// a fire. A success clears it.
func (w *backhaulShedWatch) tick(cur *metrics.BackhaulShedSample, run func(reading *float64) error) error {
var reading *float64
if cur != nil {
- reading = metrics.BackhaulShedsSince(w.prev, cur)
+ v := max(*metrics.BackhaulShedsSince(w.prev, cur), w.carry)
+ reading = &v
}
err := run(reading)
- if err == nil && cur != nil {
+ if cur != nil {
w.prev = cur
+ if err != nil {
+ w.carry = *reading
+ } else {
+ w.carry = 0
+ }
}
return err
}
diff --git a/internal/metrics/alert.go b/internal/metrics/alert.go
index 7962b008b..a9d672cf0 100644
--- a/internal/metrics/alert.go
+++ b/internal/metrics/alert.go
@@ -496,6 +496,8 @@ func Evaluate(r Rules, st AlertState, obs Observation) (AlertState, []Event) {
out.Rules[BackhaulShedRule] = RuleState{Firing: true, Since: obs.At, FiredAt: obs.At, Value: val}
emit(BackhaulShedRule, true, val)
case cur.Firing && val > 0:
+ // Value stays the reading that fired, as every other rule's
+ // does between transitions (RuleState.Value).
cur.Since = obs.At
out.Rules[BackhaulShedRule] = cur
case cur.Firing && obs.At.Sub(cur.Since) >= r.BackhaulShedQuiet: