alerts: backhaul_shed counts saturation, clears after 3 quiet minutes
Review round 1 (#1535). The rule counts only refusals at a pool's cap, not fair-share 429s (still in the sample). It clears once three minutes pass with none, so bursts on alternate minutes do not flap. The watch commits its baseline only when alertTick succeeds, so a firing transition whose publish failed fires on the next tick instead of being lost to a quiet minute. Samples carry backhaul_sheds only once a counter is non-zero; the rule still reads zero from a quiet edge.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+258
−112
internal/host/agent.go+15 −5internal/host/metrics_backhaul_test.go+153 −61internal/host/metrics_loop.go+35 −16internal/metrics/alert.go+14 −6internal/metrics/backhaul.go+16 −12internal/metrics/backhaul_test.go+25 −12
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 2e1ff3de0..498127881 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -4922,15 +4922,25 @@ var staticTreeSource atomic.Value // func() int64 |
| // edge exists, for the metrics sample and the backhaul_shed rule (#1535). |
| var backhaulShedSource atomic.Value // func() edge.BackhaulSheds |
| -// sampleBackhaulSheds reads it as a sample carries it, nil before the edge |
| -// is ready (no reading never fires and never clears). |
| -func sampleBackhaulSheds() *metrics.BackhaulShedSample { |
| +// backhaulShedCounts reads it, false before the edge is ready. |
| +func backhaulShedCounts() (metrics.BackhaulShedSample, bool) { |
| fn, _ := backhaulShedSource.Load().(func() edge.BackhaulSheds) |
| if fn == nil { |
| - return nil |
| + return metrics.BackhaulShedSample{}, false |
| } |
| c := fn() |
| - return &metrics.BackhaulShedSample{Requests: c.Requests, Streams: c.Streams, FairShare: c.FairShare} |
| + return metrics.BackhaulShedSample{Requests: c.Requests, Streams: c.Streams, FairShare: c.FairShare}, true |
| +} |
| + |
| +// sampleBackhaulSheds is the counters as a sample carries them: nil before |
| +// the edge is ready and while it has refused nothing, so a quiet box's |
| +// samples are what they were before the counters existed. |
| +func sampleBackhaulSheds() *metrics.BackhaulShedSample { |
| + c, ok := backhaulShedCounts() |
| + if !ok || c == (metrics.BackhaulShedSample{}) { |
| + return nil |
| + } |
| + return &c |
| } |
| // largestStaticTree reads it, 0 before the edge is ready. |
diff --git a/internal/host/metrics_backhaul_test.go b/internal/host/metrics_backhaul_test.go| index 34090ca9b..66cc3c5c5 100644 |
| --- a/internal/host/metrics_backhaul_test.go |
| +++ b/internal/host/metrics_backhaul_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "context" |
| "encoding/json" |
| "path/filepath" |
| + "strings" |
| "testing" |
| "time" |
| @@ -12,10 +13,9 @@ import ( |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| -// The edge's backhaul refusal counters reach the metrics loop through the |
| -// same in-process seam as the geoip code: a func the edge-ready callback |
| -// publishes. Before the edge is ready there is no reading. |
| -func TestSampleBackhaulShedsReadsTheEdge(t *testing.T) { |
| +// setBackhaulShedSource stands in for the edge-ready callback for one test. |
| +func setBackhaulShedSource(t *testing.T, fn func() edge.BackhaulSheds) { |
| + t.Helper() |
| old := backhaulShedSource.Load() |
| t.Cleanup(func() { |
| if old != nil { |
| @@ -24,21 +24,40 @@ func TestSampleBackhaulShedsReadsTheEdge(t *testing.T) { |
| backhaulShedSource.Store((func() edge.BackhaulSheds)(nil)) |
| } |
| }) |
| - backhaulShedSource.Store((func() edge.BackhaulSheds)(nil)) |
| + backhaulShedSource.Store(fn) |
| +} |
| + |
| +// The edge's backhaul refusal counters reach the metrics loop through the |
| +// same in-process seam as the geoip code: a func the edge-ready callback |
| +// publishes. Before the edge is ready there is no reading; while it has |
| +// refused nothing the sample carries none, but the counts still read zero. |
| +func TestSampleBackhaulShedsReadsTheEdge(t *testing.T) { |
| + setBackhaulShedSource(t, nil) |
| if got := sampleBackhaulSheds(); got != nil { |
| t.Fatalf("before the edge is ready: %+v, want nil", got) |
| } |
| - backhaulShedSource.Store(func() edge.BackhaulSheds { return edge.BackhaulSheds{Requests: 1, Streams: 2, FairShare: 3} }) |
| + if _, ok := backhaulShedCounts(); ok { |
| + t.Fatal("counts before the edge is ready") |
| + } |
| + counts := edge.BackhaulSheds{} |
| + setBackhaulShedSource(t, func() edge.BackhaulSheds { return counts }) |
| + if got := sampleBackhaulSheds(); got != nil { |
| + t.Fatalf("a quiet edge: %+v, want nil so samples keep their old shape", got) |
| + } |
| + if c, ok := backhaulShedCounts(); !ok || c != (metrics.BackhaulShedSample{}) { |
| + t.Fatalf("a quiet edge's counts: %+v %v, want zeros", c, ok) |
| + } |
| + counts = edge.BackhaulSheds{Requests: 1, Streams: 2, FairShare: 3} |
| got := sampleBackhaulSheds() |
| if got == nil || *got != (metrics.BackhaulShedSample{Requests: 1, Streams: 2, FairShare: 3}) { |
| t.Fatalf("sampleBackhaulSheds = %+v, want the edge's counters", got) |
| } |
| } |
| -// A sample carries the counters, and the watch turns consecutive samples |
| -// into the per-tick reading the rule wants. |
| +// A sample carries the counters when there are any and omits the key when |
| +// there are none. |
| func TestSamplerCarriesBackhaulSheds(t *testing.T) { |
| - counts := &metrics.BackhaulShedSample{} |
| + var counts *metrics.BackhaulShedSample |
| at := time.Date(2026, 12, 2, 12, 0, 0, 0, time.UTC) |
| probes := sampleProbes{ |
| procStat: func() *metrics.CPUCounters { return nil }, |
| @@ -47,78 +66,151 @@ func TestSamplerCarriesBackhaulSheds(t *testing.T) { |
| diskFrac: func() *float64 { return nil }, |
| units: func() map[string]unitRaw { return nil }, |
| now: func() time.Time { return at }, |
| - backhaulSheds: func() *metrics.BackhaulShedSample { c := *counts; return &c }, |
| + backhaulSheds: func() *metrics.BackhaulShedSample { return counts }, |
| } |
| sm := &sampler{} |
| - var w backhaulShedWatch |
| - var readings []float64 |
| - for _, c := range []metrics.BackhaulShedSample{{Requests: 5}, {Requests: 5, Streams: 20}, {Requests: 5, Streams: 20}} { |
| - *counts = c |
| - sm.tick(probes) |
| - latest := sm.buf[len(sm.buf)-1] |
| - if latest.BackhaulSheds == nil || *latest.BackhaulSheds != c { |
| - t.Fatalf("sample carries %+v, want %+v", latest.BackhaulSheds, c) |
| - } |
| - r := w.read(latest.BackhaulSheds) |
| - if r == nil { |
| - t.Fatal("no reading from a sample with counters") |
| - } |
| - readings = append(readings, *r) |
| - } |
| - if want := []float64{5, 20, 0}; readings[0] != want[0] || readings[1] != want[1] || readings[2] != want[2] { |
| - t.Fatalf("readings = %v, want %v", readings, want) |
| + sm.tick(probes) |
| + counts = &metrics.BackhaulShedSample{Requests: 5, Streams: 20} |
| + sm.tick(probes) |
| + quiet, err := json.Marshal(sm.buf[0]) |
| + if err != nil { |
| + t.Fatal(err) |
| } |
| - if r := w.read(nil); r != nil { |
| - t.Fatalf("no counters read %v, want nil", *r) |
| + if strings.Contains(string(quiet), "backhaul_sheds") { |
| + t.Fatalf("a quiet sample carries the key: %s", quiet) |
| } |
| b, err := json.Marshal(sm.buf[1]) |
| if err != nil { |
| t.Fatal(err) |
| } |
| var back metrics.Sample |
| - if err := json.Unmarshal(b, &back); err != nil || back.BackhaulSheds == nil || back.BackhaulSheds.Streams != 20 { |
| + if err := json.Unmarshal(b, &back); err != nil || back.BackhaulSheds == nil || *back.BackhaulSheds != *counts { |
| t.Fatalf("chunk round trip: %s (%v)", b, err) |
| } |
| } |
| -// backhaul_shed is published to the box's alert object like every box |
| -// rule, fires and clears there, and is not a box-sink rule: it reaches |
| -// operators through the console's alert engine. |
| -func TestAlertTickPublishesBackhaulShed(t *testing.T) { |
| - ctx := context.Background() |
| - store := release.NewDirStore(t.TempDir()) |
| - statePath := filepath.Join(t.TempDir(), "monitor", "alerts.json") |
| - box := "box-a" |
| - st := &metrics.AlertState{V: 1, Box: box, Rules: map[string]metrics.RuleState{}} |
| - var posts int |
| - sinks := webhookSinks("http://example.test/webhook", func(string, []byte) error { posts++; return nil }) |
| - at := time.Date(2026, 12, 2, 12, 0, 0, 0, time.UTC) |
| - published := func(n float64) metrics.RuleState { |
| +// The watch reads saturation refusals since the last committed tick, and |
| +// commits only when the tick it fed succeeded. |
| +func TestBackhaulShedWatch(t *testing.T) { |
| + var w backhaulShedWatch |
| + var got []any |
| + feed := func(cur *metrics.BackhaulShedSample, fail bool) { |
| t.Helper() |
| - at = at.Add(time.Minute) |
| - if err := alertTick(ctx, store, box, metrics.DefaultRules(), st, metrics.Sample{At: at}, alertReadings{backhaulSheds: &n}, statePath, sinks, nil, nil); err != nil { |
| - t.Fatal(err) |
| - } |
| - body, _, err := store.Get(ctx, metrics.AlertKey(box)) |
| - if err != nil { |
| - t.Fatalf("published state: %v", err) |
| - } |
| - var pub metrics.AlertState |
| - if err := json.Unmarshal(body, &pub); err != nil { |
| - t.Fatal(err) |
| + w.tick(cur, func(r *float64) error { |
| + if r == nil { |
| + got = append(got, nil) |
| + } else { |
| + got = append(got, *r) |
| + } |
| + if fail { |
| + return context.DeadlineExceeded |
| + } |
| + return nil |
| + }) |
| + } |
| + feed(nil, false) |
| + feed(&metrics.BackhaulShedSample{Requests: 5, FairShare: 50}, false) |
| + 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} |
| + if len(got) != len(want) { |
| + t.Fatalf("readings = %v, want %v", got, want) |
| + } |
| + for i := range want { |
| + if got[i] != want[i] { |
| + t.Fatalf("readings = %v, want %v", got, want) |
| } |
| - return pub.Rules[metrics.BackhaulShedRule] |
| } |
| - if rs := published(40); !rs.Firing || rs.Value != 40 { |
| - t.Fatalf("40 refusals in a minute: published %+v, want firing with 40", rs) |
| +} |
| + |
| +// shedRig drives the watch and alertTick one minute at a time, the way |
| +// metricsLoop does, against a store that can be made to fail. |
| +type shedRig struct { |
| + t *testing.T |
| + ctx context.Context |
| + store release.Store |
| + path string |
| + st *metrics.AlertState |
| + sinks *alertSinks |
| + posts *int |
| + watch backhaulShedWatch |
| + at time.Time |
| + counter metrics.BackhaulShedSample |
| +} |
| + |
| +func newShedRig(t *testing.T) *shedRig { |
| + posts := 0 |
| + return &shedRig{ |
| + t: t, ctx: context.Background(), |
| + store: release.NewDirStore(t.TempDir()), |
| + path: filepath.Join(t.TempDir(), "monitor", "alerts.json"), |
| + st: &metrics.AlertState{V: 1, Box: "box-a", Rules: map[string]metrics.RuleState{}}, |
| + sinks: webhookSinks("http://example.test/webhook", func(string, []byte) error { posts++; return nil }), |
| + posts: &posts, |
| + at: time.Date(2026, 12, 2, 12, 0, 0, 0, time.UTC), |
| } |
| - if rs := published(0); rs.Firing { |
| - t.Fatalf("a quiet minute: published %+v, want cleared", rs) |
| +} |
| + |
| +// tick adds n saturation refusals to the edge's counter, runs a minute on |
| +// store s, and returns the published backhaul_shed state. |
| +func (r *shedRig) tick(s release.Store, n uint64) (metrics.RuleState, error) { |
| + r.t.Helper() |
| + r.at = r.at.Add(time.Minute) |
| + r.counter.Streams += n |
| + cur := r.counter |
| + err := r.watch.tick(&cur, func(reading *float64) error { |
| + return alertTick(r.ctx, s, "box-a", metrics.DefaultRules(), r.st, metrics.Sample{At: r.at}, alertReadings{backhaulSheds: reading}, r.path, r.sinks, nil, nil) |
| + }) |
| + body, _, gerr := r.store.Get(r.ctx, metrics.AlertKey("box-a")) |
| + if gerr != nil { |
| + return metrics.RuleState{}, err |
| } |
| - if posts != 0 { |
| - t.Fatalf("the box's own sink got %d post(s), want none: backhaul_shed is not a box-sink rule", posts) |
| + var pub metrics.AlertState |
| + if jerr := json.Unmarshal(body, &pub); jerr != nil { |
| + r.t.Fatal(jerr) |
| + } |
| + return pub.Rules[metrics.BackhaulShedRule], err |
| +} |
| + |
| +// backhaul_shed is published to the box's alert object like every box |
| +// rule, fires, holds through two quiet minutes and clears on the third, and |
| +// is not a box-sink rule: it reaches operators through the console's alert |
| +// engine. |
| +func TestAlertTickPublishesBackhaulShed(t *testing.T) { |
| + r := newShedRig(t) |
| + if rs, err := r.tick(r.store, 40); err != nil || !rs.Firing || rs.Value != 40 { |
| + t.Fatalf("40 refusals in a minute: published %+v (%v), want firing with 40", rs, err) |
| + } |
| + for i := 1; i <= 2; i++ { |
| + if rs, _ := r.tick(r.store, 0); !rs.Firing { |
| + t.Fatalf("quiet minute %d: published %+v, want still firing", i, rs) |
| + } |
| + } |
| + if rs, err := r.tick(r.store, 0); err != nil || rs.Firing { |
| + t.Fatalf("third quiet minute: published %+v (%v), want cleared", rs, err) |
| + } |
| + if *r.posts != 0 { |
| + t.Fatalf("the box's own sink got %d post(s), want none: backhaul_shed is not a box-sink rule", *r.posts) |
| } |
| if boxSinkRule(metrics.BackhaulShedRule) { |
| t.Fatal("backhaul_shed is a box-sink rule") |
| } |
| } |
| + |
| +// A firing transition whose publish failed is not lost to the next quiet |
| +// minute: the refusals stay in the count until a tick publishes. |
| +func TestBackhaulShedFiresAfterAFailedPublish(t *testing.T) { |
| + r := newShedRig(t) |
| + fail := &failingPutStore{Store: release.NewDirStore(t.TempDir())} |
| + if _, err := r.tick(fail, 40); err == nil { |
| + t.Fatal("the publish against a failing store succeeded") |
| + } |
| + if r.st.Rules[metrics.BackhaulShedRule].Firing { |
| + t.Fatal("committed state fired without a publish") |
| + } |
| + rs, err := r.tick(r.store, 0) // a quiet minute, and the store is back |
| + if err != nil || !rs.Firing || rs.Value != 40 { |
| + t.Fatalf("the quiet minute after the failed publish: published %+v (%v), want the 40 refusals firing", rs, err) |
| + } |
| +} |
diff --git a/internal/host/metrics_loop.go b/internal/host/metrics_loop.go| index 7cf02bce4..df86fe3c0 100644 |
| --- a/internal/host/metrics_loop.go |
| +++ b/internal/host/metrics_loop.go |
| @@ -51,7 +51,7 @@ type sampleProbes struct { |
| now func() time.Time |
| departures func() *metrics.DepartureSample |
| // backhaulSheds is the edge's backhaul refusal counters |
| - // (sampleBackhaulSheds); nil, or nil from it, is no reading. |
| + // (sampleBackhaulSheds); nil, or nil from it, puts none in the sample. |
| backhaulSheds func() *metrics.BackhaulShedSample |
| } |
| @@ -650,24 +650,34 @@ type alertReadings struct { |
| // rule reads the higher of it and the data disk's blocks. nil on any |
| // other box. |
| demoFill *diskReading |
| - // backhaulSheds is how many hops the edge's backhaul refused since the |
| - // last tick (backhaulShedWatch); nil leaves backhaul_shed as it was. |
| + // backhaulSheds is how many hops the edge's backhaul refused at a cap |
| + // since the last committed tick (backhaulShedWatch); nil leaves |
| + // backhaul_shed as it was. |
| backhaulSheds *float64 |
| } |
| -// backhaulShedWatch turns the cumulative counters each sample carries into |
| -// the backhaul_shed rule's per-tick reading. One for the life of the loop. |
| +// 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 } |
| -// read is the refusals cur counts that the last reading did not, or nil |
| -// when cur is nil (the edge is not ready), which keeps the last reading. |
| -func (w *backhaulShedWatch) read(cur *metrics.BackhaulShedSample) *float64 { |
| - if cur == nil { |
| - return nil |
| - } |
| - v := metrics.BackhaulShedsSince(w.prev, cur) |
| - w.prev = cur |
| - return v |
| +// 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. |
| +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) |
| + } |
| + err := run(reading) |
| + if err == nil && cur != nil { |
| + w.prev = cur |
| + } |
| + return err |
| } |
| // verifyWatch reads the restore drill's local copy of this box's record |
| @@ -1094,8 +1104,17 @@ func metricsLoop(execRoot string, src unitSources, owners func() map[string]metr |
| latest := sm.buf[len(sm.buf)-1] |
| recordDiskPoint(&processDiskTrend, probes.diskBytes, latest.At) |
| extra := restartTick(latest.At) |
| - extra.backhaulSheds = sheds.read(latest.BackhaulSheds) |
| - if err := alertTick(ctx, s, box, rules, &st, latest, extra, statePath, sinks, &processDiskTrend, beat); err != nil { |
| + // The counters straight from the edge, not the sample's copy, |
| + // which is nil while they are all zero: a quiet box must still |
| + // read zero, or a fire restored from disk would never clear. |
| + var shedCounts *metrics.BackhaulShedSample |
| + if c, ok := backhaulShedCounts(); ok { |
| + shedCounts = &c |
| + } |
| + if err := sheds.tick(shedCounts, func(reading *float64) error { |
| + extra.backhaulSheds = reading |
| + return alertTick(ctx, s, box, rules, &st, latest, extra, statePath, sinks, &processDiskTrend, beat) |
| + }); err != nil { |
| log.Printf("agent: alert tick: %v", err) |
| } |
diff --git a/internal/metrics/alert.go b/internal/metrics/alert.go| index 0a8cddfa9..7962b008b 100644 |
| --- a/internal/metrics/alert.go |
| +++ b/internal/metrics/alert.go |
| @@ -33,10 +33,12 @@ type Rules struct { |
| // reporter's window before that unit's restarts rule fires. It is a |
| // count, not a fraction; see UnitRestarts for what clears it. |
| RestartCount float64 // fire >, default 3 |
| - // BackhaulShedCount is how many backhaul refusals one tick may count |
| - // before backhaul_shed fires. A count, not a fraction; the rule clears |
| - // on the first tick with none. |
| + // BackhaulShedCount is how many backhaul saturation refusals one tick |
| + // may count before backhaul_shed fires. A count, not a fraction. |
| BackhaulShedCount float64 // fire >, default 10 |
| + // BackhaulShedQuiet is how long backhaul_shed must see no refusals |
| + // before it clears, so bursts on alternate minutes do not flap. |
| + BackhaulShedQuiet time.Duration // default 3 * time.Minute |
| } |
| // DefaultRules are the thresholds shipped with the platform. |
| @@ -57,6 +59,7 @@ func DefaultRules() Rules { |
| RestartCount: 3, |
| // backhaul_shed (#1535): fixed, like the restart count. |
| BackhaulShedCount: 10, |
| + BackhaulShedQuiet: 3 * time.Minute, |
| } |
| } |
| @@ -481,8 +484,10 @@ func Evaluate(r Rules, st AlertState, obs Observation) (AlertState, []Event) { |
| } |
| // backhaul_shed: a count over the last tick, so no sustain. It fires |
| - // above BackhaulShedCount and clears on the first tick with none, so a |
| - // box still refusing a few hops keeps it up. |
| + // above BackhaulShedCount. While it is firing, Since is the last tick |
| + // that counted any refusal, and it clears once BackhaulShedQuiet has |
| + // passed since then with none (three one-minute ticks by default), so a |
| + // box still refusing a few, or refusing in bursts, keeps it up. |
| if obs.BackhaulSheds != nil { |
| val := *obs.BackhaulSheds |
| cur := out.Rules[BackhaulShedRule] |
| @@ -490,7 +495,10 @@ func Evaluate(r Rules, st AlertState, obs Observation) (AlertState, []Event) { |
| case !cur.Firing && val > r.BackhaulShedCount: |
| out.Rules[BackhaulShedRule] = RuleState{Firing: true, Since: obs.At, FiredAt: obs.At, Value: val} |
| emit(BackhaulShedRule, true, val) |
| - case cur.Firing && val == 0: |
| + case cur.Firing && val > 0: |
| + cur.Since = obs.At |
| + out.Rules[BackhaulShedRule] = cur |
| + case cur.Firing && obs.At.Sub(cur.Since) >= r.BackhaulShedQuiet: |
| out.Rules[BackhaulShedRule] = RuleState{} |
| emit(BackhaulShedRule, false, 0) |
| } |
diff --git a/internal/metrics/backhaul.go b/internal/metrics/backhaul.go| index b6eeb4c88..729a0e4de 100644 |
| --- a/internal/metrics/backhaul.go |
| +++ b/internal/metrics/backhaul.go |
| @@ -1,8 +1,11 @@ |
| package metrics |
| -// BackhaulShedRule is the box rule for hops the sending edge's backhaul |
| -// refused (#1535): a request-pool or stream-pool saturation (503) or a |
| -// fair-share refusal (429). Its value is how many in the last tick. |
| +// BackhaulShedRule is the box rule for forwarded requests the sending edge's |
| +// backhaul refused because a pool was at its cap (#1535): a request-pool or |
| +// stream-pool saturation, each a 503. Fair-share refusals (429s) are one |
| +// client being held to its share, not visitors at large being turned away, |
| +// so they are recorded in the sample but do not count toward the rule. Its |
| +// value is how many in the last tick. |
| const BackhaulShedRule = "backhaul_shed" |
| // BackhaulShedSample is the edge's refusal counters, cumulative since the |
| @@ -13,20 +16,21 @@ type BackhaulShedSample struct { |
| FairShare uint64 `json:"fair_share"` |
| } |
| -func (s BackhaulShedSample) total() uint64 { return s.Requests + s.Streams + s.FairShare } |
| +// saturated is the refusals the rule counts: both pools' saturation. |
| +func (s BackhaulShedSample) saturated() uint64 { return s.Requests + s.Streams } |
| -// BackhaulShedsSince is the backhaul_shed reading: how many refusals cur |
| -// counts that prev did not. nil cur is no reading. nil prev counts from zero, |
| -// which is right because the edge's counters start at zero in the same |
| -// process as the reader. A total lower than prev's is a new edge, counted |
| -// from zero too. |
| +// BackhaulShedsSince is the backhaul_shed reading: how many saturation |
| +// refusals cur counts that prev did not. nil cur is no reading. nil prev |
| +// counts from zero, which is right because the edge's counters start at zero |
| +// in the same process as the reader. A count lower than prev's is a new |
| +// edge, counted from zero too. |
| func BackhaulShedsSince(prev, cur *BackhaulShedSample) *float64 { |
| if cur == nil { |
| return nil |
| } |
| - n := cur.total() |
| - if prev != nil && prev.total() <= n { |
| - n -= prev.total() |
| + n := cur.saturated() |
| + if prev != nil && prev.saturated() <= n { |
| + n -= prev.saturated() |
| } |
| v := float64(n) |
| return &v |
diff --git a/internal/metrics/backhaul_test.go b/internal/metrics/backhaul_test.go| index 4e856bf83..f60602aa1 100644 |
| --- a/internal/metrics/backhaul_test.go |
| +++ b/internal/metrics/backhaul_test.go |
| @@ -6,12 +6,13 @@ import ( |
| ) |
| // The backhaul_shed rule (#1535): fires when more than BackhaulShedCount |
| -// hops were refused in the last tick, holds while any are, clears on the |
| -// first tick with none. nil is no reading and leaves it alone. |
| +// saturation refusals were counted in the last tick, holds while any are, |
| +// and clears after three quiet minutes. nil is no reading and leaves it |
| +// alone. |
| func TestEvaluateBackhaulShed(t *testing.T) { |
| r := DefaultRules() |
| - if r.BackhaulShedCount != 10 { |
| - t.Fatalf("default BackhaulShedCount = %v, want 10", r.BackhaulShedCount) |
| + if r.BackhaulShedCount != 10 || r.BackhaulShedQuiet != 3*time.Minute { |
| + t.Fatalf("defaults %v, %v, want 10 and 3m", r.BackhaulShedCount, r.BackhaulShedQuiet) |
| } |
| st := AlertState{V: 1, Box: "box-a", Rules: map[string]RuleState{}} |
| at := time.Date(2026, 12, 2, 12, 0, 0, 0, time.UTC) |
| @@ -23,23 +24,27 @@ func TestEvaluateBackhaulShed(t *testing.T) { |
| return evs |
| } |
| f := func(v float64) *float64 { return &v } |
| + firing := func() bool { return st.Rules[BackhaulShedRule].Firing } |
| - if evs := step(f(10)); len(evs) != 0 || st.Rules[BackhaulShedRule].Firing { |
| + if evs := step(f(10)); len(evs) != 0 || firing() { |
| t.Fatalf("10 sheds (at the line) = %+v, want quiet", evs) |
| } |
| evs := step(f(11)) |
| if len(evs) != 1 || evs[0].Rule != BackhaulShedRule || !evs[0].Firing || evs[0].Value != 11 || evs[0].Box != "box-a" { |
| t.Fatalf("11 sheds = %+v, want one fire with value 11", evs) |
| } |
| - if evs := step(nil); len(evs) != 0 || !st.Rules[BackhaulShedRule].Firing { |
| + if evs := step(nil); len(evs) != 0 || !firing() { |
| t.Fatalf("nil while firing = %+v, want held", evs) |
| } |
| - if evs := step(f(1)); len(evs) != 0 || !st.Rules[BackhaulShedRule].Firing { |
| - t.Fatalf("1 shed while firing = %+v, want held: only a quiet minute clears", evs) |
| + // Bursts on alternate minutes never clear it. |
| + for i, v := range []float64{5, 0, 0, 1, 0, 0, 30, 0, 0} { |
| + if evs := step(f(v)); len(evs) != 0 || !firing() { |
| + t.Fatalf("tick %d (%v sheds) while firing = %+v, want held: no three quiet minutes yet", i, v, evs) |
| + } |
| } |
| evs = step(f(0)) |
| if len(evs) != 1 || evs[0].Firing || evs[0].Value != 0 { |
| - t.Fatalf("0 sheds while firing = %+v, want one clear", evs) |
| + t.Fatalf("third quiet minute = %+v, want one clear", evs) |
| } |
| if rs, ok := st.Rules[BackhaulShedRule]; !ok || rs.Firing { |
| t.Fatalf("cleared state = %+v (present %v), want a quiet entry", rs, ok) |
| @@ -47,6 +52,13 @@ func TestEvaluateBackhaulShed(t *testing.T) { |
| if evs := step(f(0)); len(evs) != 0 { |
| t.Fatalf("0 sheds while quiet = %+v, want nothing", evs) |
| } |
| + // A nil tick inside the quiet run neither clears nor resets it. |
| + step(f(20)) |
| + step(f(0)) |
| + step(nil) |
| + if evs := step(f(0)); len(evs) != 1 || evs[0].Firing { |
| + t.Fatalf("three minutes after the last shed, across a nil tick = %+v, want the clear", evs) |
| + } |
| } |
| func TestBackhaulShedsSince(t *testing.T) { |
| @@ -59,10 +71,11 @@ func TestBackhaulShedsSince(t *testing.T) { |
| want *float64 |
| }{ |
| {"no reading", s(1, 1, 1), nil, nil}, |
| - {"first reading counts from the edge's start", nil, s(2, 3, 4), ptr(9)}, |
| - {"delta", s(2, 3, 4), s(5, 3, 10), ptr(9)}, |
| + {"first reading counts from the edge's start", nil, s(2, 3, 4), ptr(5)}, |
| + {"delta", s(2, 3, 4), s(5, 4, 10), ptr(4)}, |
| + {"fair-share refusals do not count", s(2, 3, 4), s(2, 3, 90), ptr(0)}, |
| {"none", s(2, 3, 4), s(2, 3, 4), ptr(0)}, |
| - {"a counter went back: a new edge, counted from zero", s(50, 0, 0), s(3, 1, 0), ptr(4)}, |
| + {"a counter went back: a new edge, counted from zero", s(50, 0, 0), s(3, 1, 7), ptr(4)}, |
| } { |
| got := BackhaulShedsSince(tc.prev, tc.cur) |
| if (got == nil) != (tc.want == nil) || (got != nil && *got != *tc.want) { |