This commit wires the checker's state machine through to the VPP dataplane:
every backend state transition flows through a single code path that
recomputes the effective per-backend weight (with pool failover) and pushes
the result to VPP. Along the way several latent bugs in the state machine
and the sync path were fixed.
internal/vpp/reconciler.go (new)
- New Reconciler type subscribes to checker.Checker events and, on every
transition, calls Client.SyncLBStateVIP for the affected frontend. This
is the ONLY place in the codebase where backend state changes cause VPP
calls — the "single path" discipline requested during design.
- Defines an EventSource interface (checker.Checker satisfies it) so the
dependency direction stays vpp → checker; the checker never imports vpp.
internal/vpp/client.go
- Renamed ConfigSource → StateSource. The interface now has two methods:
Config() and BackendState(name) — the reconciler and the desired-state
builder both need live health state to compute effective weights.
- SetConfigSource → SetStateSource; internal cfgSrc field → stateSrc.
- New getStateSource() helper for internal locked access.
- lbSyncLoop still uses the state source for its periodic drift
reconciliation; it's fully idempotent and runs the same code path as
event-driven syncs.
internal/vpp/lbsync.go
- desiredAS grows a Flush bool so the mapping function can signal "on
transition to weight 0, flush existing flow-table entries".
- asFromBackend is now the single source of truth for the state →
(weight, flush) rule. Documented with a full truth table. Takes an
activePool parameter so it can distinguish "up in active pool" from
"up but standby".
- activePoolIndex(fe, states) implements priority failover: returns the
index of the first pool containing any StateUp backend. pool[0] wins
when at least one member is up; pool[1] takes over when pool[0] is
empty; and so on. Defaults to 0 (unobservable, since all backends map
to weight 0 when nothing is up).
- desiredFromFrontend snapshots backend states once, computes activePool,
then walks every backend through asFromBackend. No more filtering on
b.Enabled — disabled backends stay in the desired set so they keep
their AS entry in VPP with weight=0. The previous filter caused delAS
on disable, which destroyed the entry and broke enable afterwards.
- EffectiveWeights(fe, src) exported helper that returns the per-pool
per-backend weight map for one frontend. Used by the gRPC GetFrontend
handler and robot tests to observe failover without touching VPP.
- reconcileVIP computes flush at the weight-change call site:
flush = desired.Flush && cur.Weight > 0 && desired.Weight == 0
This ensures only the *transition* to disabled flushes sessions —
steady-state syncs with already-zero weight skip the call entirely.
- setASWeight now plumbs IsFlush into lb_as_set_weight.
internal/vpp/lbsync_test.go (new)
- TestAsFromBackend: 15 cases locking down the truth table, including
failover scenarios (up in standby pool, up promoted in pool[1]).
- TestActivePoolIndex: 8 cases covering pool[0]-has-up, pool[0]-all-down,
all-disabled, all-paused, all-unknown, nothing-up-anywhere, and
three-tier failover.
- TestDesiredFromFrontendFailover: 5 end-to-end scenarios wiring a fake
StateSource through desiredFromFrontend and asserting the final
per-IP weight map. Exercises the complete pipeline without VPP.
internal/checker/checker.go
- Added BackendState(name) (health.State, bool) — one-line method that
satisfies vpp.StateSource. The checker is otherwise unchanged.
- EnableBackend rewritten to reuse the existing worker (parallel to
ResumeBackend). The old code called startWorker which constructed a
brand-new Backend via health.New, throwing away the transition
history; the resulting 'backend-transition' log showed the bogus
from=unknown,to=unknown. Now uses w.backend.Enable() to record a
proper disabled→unknown transition and launches a fresh goroutine.
- Static (no-healthcheck) backends now fire their synthetic 'always up'
pass on the first iteration of runProbe instead of sleeping 30s
first. Previously static backends sat in StateUnknown for 30s after
startup — useless for deterministic testing and surprising for
operators. The fix is a simple first-iteration flag.
internal/health/state.go
- New Enable(maxHistory) method parallel to Disable. Transitions the
backend from whatever state it's in (typically StateDisabled) to
StateUnknown, resets the health counter to rise-1 so the expedited
resolution kicks in on the first probe result, and emits a transition
with code 'enabled'.
proto/maglev.proto
- PoolBackendInfo gains effective_weight: the state-aware weight that
would be programmed into VPP (distinct from the configured weight in
the YAML). Exposed via GetFrontend.
internal/grpcapi/server.go
- frontendToProto takes a vpp.StateSource, computes effective weights
via vpp.EffectiveWeights, and populates PoolBackendInfo.EffectiveWeight.
- GetFrontend and SetFrontendPoolBackendWeight updated to pass the
checker in.
cmd/maglevc/commands.go
- 'show frontends <name>' now renders every pool backend row as
<name> weight <cfg> effective <eff> [disabled]?
so both values are always visible. The VPP-style key/value format
avoids the ANSI-alignment pitfall we hit earlier and makes the output
regex-parseable for robot tests.
cmd/maglevd/main.go
- Construct and start the Reconciler alongside the VPP client. Two
extra lines, no other changes to startup.
tests/01-maglevd/maglevd-lab/maglev.yaml
- Two new static backends (static-primary, static-fallback) and a new
failover-vip frontend with one backend per pool. No healthcheck, so
the state machine resolves them to 'up' immediately via the synthetic
pass. Used by the failover robot tests.
tests/01-maglevd/01-healthcheck.robot
- Three new test cases exercising pool failover end-to-end:
1. primary up, secondary standby (initial state)
2. disable primary → fallback takes over (effective weight flips)
3. enable primary → fallback steps back
All run without VPP: they scrape 'maglevc show frontends <name>' and
regex-match the effective weight in the output. Deterministic and
fast (~2s total) because the static backends don't probe.
- Two helper keywords: Static Backend Should Be Up and
Effective Weight Should Be.
Net result: 16/16 robot tests pass. Backend state transitions now
flow through a single documented path (checker event → reconciler →
SyncLBStateVIP → desiredFromFrontend → asFromBackend → reconcileVIP →
setASWeight), and the pool failover / enable-after-disable / static-
backend-startup bugs are all fixed.
274 lines
8.4 KiB
Go
274 lines
8.4 KiB
Go
// Copyright (c) 2026, Pim van Pelt <pim@ipng.ch>
|
|
|
|
package vpp
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"git.ipng.ch/ipng/vpp-maglev/internal/config"
|
|
"git.ipng.ch/ipng/vpp-maglev/internal/health"
|
|
)
|
|
|
|
// TestAsFromBackend locks down the state → (weight, flush) truth table.
|
|
// This is the single source of truth for how maglevd decides what to
|
|
// program into VPP for each backend state. If this test needs updating
|
|
// the behavior has deliberately changed.
|
|
func TestAsFromBackend(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
poolIdx int
|
|
activePool int
|
|
state health.State
|
|
cfgWeight int
|
|
wantWeight uint8
|
|
wantFlush bool
|
|
}{
|
|
// up in active pool → configured weight, no flush
|
|
{"up active w100", 0, 0, health.StateUp, 100, 100, false},
|
|
{"up active w50", 0, 0, health.StateUp, 50, 50, false},
|
|
{"up active w0", 0, 0, health.StateUp, 0, 0, false},
|
|
{"up active clamp-high", 0, 0, health.StateUp, 150, 100, false},
|
|
{"up active clamp-low", 0, 0, health.StateUp, -5, 0, false},
|
|
|
|
// up in non-active pool → standby (weight 0), no flush
|
|
{"up standby pool0 active=1", 0, 1, health.StateUp, 100, 0, false},
|
|
{"up standby pool1 active=0", 1, 0, health.StateUp, 100, 0, false},
|
|
{"up standby pool2 active=0", 2, 0, health.StateUp, 100, 0, false},
|
|
|
|
// up in secondary, promoted because pool[1] is now active
|
|
{"up failover pool1 active=1", 1, 1, health.StateUp, 100, 100, false},
|
|
|
|
// unknown → off, drain
|
|
{"unknown pool0 active=0", 0, 0, health.StateUnknown, 100, 0, false},
|
|
{"unknown pool1 active=0", 1, 0, health.StateUnknown, 100, 0, false},
|
|
|
|
// down → off, drain (probe might be wrong)
|
|
{"down pool0 active=0", 0, 0, health.StateDown, 100, 0, false},
|
|
{"down pool1 active=1", 1, 1, health.StateDown, 100, 0, false},
|
|
|
|
// paused → off, drain (graceful maintenance)
|
|
{"paused pool0 active=0", 0, 0, health.StatePaused, 100, 0, false},
|
|
|
|
// disabled → off, flush (hard stop)
|
|
{"disabled pool0 active=0", 0, 0, health.StateDisabled, 100, 0, true},
|
|
{"disabled pool1 active=1", 1, 1, health.StateDisabled, 100, 0, true},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
w, f := asFromBackend(tc.poolIdx, tc.activePool, tc.state, tc.cfgWeight)
|
|
if w != tc.wantWeight {
|
|
t.Errorf("weight: got %d, want %d", w, tc.wantWeight)
|
|
}
|
|
if f != tc.wantFlush {
|
|
t.Errorf("flush: got %v, want %v", f, tc.wantFlush)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestActivePoolIndex locks down the priority-failover selector: the first
|
|
// pool containing at least one up backend is the active pool. Default 0.
|
|
func TestActivePoolIndex(t *testing.T) {
|
|
mkFE := func(pools ...[]string) config.Frontend {
|
|
out := make([]config.Pool, len(pools))
|
|
for i, p := range pools {
|
|
out[i] = config.Pool{Name: "p", Backends: map[string]config.PoolBackend{}}
|
|
for _, name := range p {
|
|
out[i].Backends[name] = config.PoolBackend{Weight: 100}
|
|
}
|
|
}
|
|
return config.Frontend{Pools: out}
|
|
}
|
|
|
|
cases := []struct {
|
|
name string
|
|
fe config.Frontend
|
|
states map[string]health.State
|
|
want int
|
|
}{
|
|
{
|
|
name: "pool0 has up, pool1 standby",
|
|
fe: mkFE([]string{"a", "b"}, []string{"c", "d"}),
|
|
states: map[string]health.State{"a": health.StateUp, "b": health.StateDown, "c": health.StateUp, "d": health.StateUp},
|
|
want: 0,
|
|
},
|
|
{
|
|
name: "pool0 all down, pool1 has up → failover",
|
|
fe: mkFE([]string{"a", "b"}, []string{"c", "d"}),
|
|
states: map[string]health.State{"a": health.StateDown, "b": health.StateDown, "c": health.StateUp, "d": health.StateUp},
|
|
want: 1,
|
|
},
|
|
{
|
|
name: "pool0 all disabled, pool1 has up → failover",
|
|
fe: mkFE([]string{"a", "b"}, []string{"c"}),
|
|
states: map[string]health.State{"a": health.StateDisabled, "b": health.StateDisabled, "c": health.StateUp},
|
|
want: 1,
|
|
},
|
|
{
|
|
name: "pool0 all paused, pool1 has up → failover",
|
|
fe: mkFE([]string{"a"}, []string{"c"}),
|
|
states: map[string]health.State{"a": health.StatePaused, "c": health.StateUp},
|
|
want: 1,
|
|
},
|
|
{
|
|
name: "pool0 all unknown (startup), pool1 up → pool1",
|
|
fe: mkFE([]string{"a"}, []string{"c"}),
|
|
states: map[string]health.State{"a": health.StateUnknown, "c": health.StateUp},
|
|
want: 1,
|
|
},
|
|
{
|
|
name: "nothing up anywhere → default 0",
|
|
fe: mkFE([]string{"a"}, []string{"c"}),
|
|
states: map[string]health.State{"a": health.StateDown, "c": health.StateDown},
|
|
want: 0,
|
|
},
|
|
{
|
|
name: "1 up in pool0 is enough",
|
|
fe: mkFE([]string{"a", "b", "c"}, []string{"d"}),
|
|
states: map[string]health.State{"a": health.StateDown, "b": health.StateDown, "c": health.StateUp, "d": health.StateUp},
|
|
want: 0,
|
|
},
|
|
{
|
|
name: "three tiers, pool0 and pool1 both empty → pool2",
|
|
fe: mkFE([]string{"a"}, []string{"b"}, []string{"c"}),
|
|
states: map[string]health.State{"a": health.StateDown, "b": health.StateDown, "c": health.StateUp},
|
|
want: 2,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := activePoolIndex(tc.fe, tc.states)
|
|
if got != tc.want {
|
|
t.Errorf("got pool %d, want pool %d", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// fakeStateSource implements StateSource from a static map.
|
|
type fakeStateSource struct {
|
|
cfg *config.Config
|
|
states map[string]health.State
|
|
}
|
|
|
|
func (f *fakeStateSource) Config() *config.Config { return f.cfg }
|
|
func (f *fakeStateSource) BackendState(name string) (health.State, bool) {
|
|
s, ok := f.states[name]
|
|
return s, ok
|
|
}
|
|
|
|
// TestDesiredFromFrontendFailover is the end-to-end integration test for
|
|
// priority-failover: given a frontend with two pools, the desired weights
|
|
// flip between pools based on which has any up backends.
|
|
func TestDesiredFromFrontendFailover(t *testing.T) {
|
|
ip := func(s string) net.IP { return net.ParseIP(s).To4() }
|
|
cfg := &config.Config{
|
|
Backends: map[string]config.Backend{
|
|
"p1": {Address: ip("10.0.0.1"), Enabled: true},
|
|
"p2": {Address: ip("10.0.0.2"), Enabled: true},
|
|
"s1": {Address: ip("10.0.0.11"), Enabled: true},
|
|
"s2": {Address: ip("10.0.0.12"), Enabled: true},
|
|
},
|
|
}
|
|
fe := config.Frontend{
|
|
Address: ip("192.0.2.1"),
|
|
Protocol: "tcp",
|
|
Port: 80,
|
|
Pools: []config.Pool{
|
|
{Name: "primary", Backends: map[string]config.PoolBackend{
|
|
"p1": {Weight: 100},
|
|
"p2": {Weight: 100},
|
|
}},
|
|
{Name: "fallback", Backends: map[string]config.PoolBackend{
|
|
"s1": {Weight: 100},
|
|
"s2": {Weight: 100},
|
|
}},
|
|
},
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
states map[string]health.State
|
|
want map[string]uint8 // backend IP → expected weight
|
|
}{
|
|
{
|
|
name: "primary all up → primary serves, secondary standby",
|
|
states: map[string]health.State{
|
|
"p1": health.StateUp, "p2": health.StateUp,
|
|
"s1": health.StateUp, "s2": health.StateUp,
|
|
},
|
|
want: map[string]uint8{
|
|
"10.0.0.1": 100, "10.0.0.2": 100,
|
|
"10.0.0.11": 0, "10.0.0.12": 0,
|
|
},
|
|
},
|
|
{
|
|
name: "primary 1 up → primary still serves",
|
|
states: map[string]health.State{
|
|
"p1": health.StateDown, "p2": health.StateUp,
|
|
"s1": health.StateUp, "s2": health.StateUp,
|
|
},
|
|
want: map[string]uint8{
|
|
"10.0.0.1": 0, "10.0.0.2": 100,
|
|
"10.0.0.11": 0, "10.0.0.12": 0,
|
|
},
|
|
},
|
|
{
|
|
name: "primary all down → failover to secondary",
|
|
states: map[string]health.State{
|
|
"p1": health.StateDown, "p2": health.StateDown,
|
|
"s1": health.StateUp, "s2": health.StateUp,
|
|
},
|
|
want: map[string]uint8{
|
|
"10.0.0.1": 0, "10.0.0.2": 0,
|
|
"10.0.0.11": 100, "10.0.0.12": 100,
|
|
},
|
|
},
|
|
{
|
|
name: "primary all disabled → failover",
|
|
states: map[string]health.State{
|
|
"p1": health.StateDisabled, "p2": health.StateDisabled,
|
|
"s1": health.StateUp, "s2": health.StateUp,
|
|
},
|
|
want: map[string]uint8{
|
|
"10.0.0.1": 0, "10.0.0.2": 0,
|
|
"10.0.0.11": 100, "10.0.0.12": 100,
|
|
},
|
|
},
|
|
{
|
|
name: "everything down → all zero, no serving",
|
|
states: map[string]health.State{
|
|
"p1": health.StateDown, "p2": health.StateDown,
|
|
"s1": health.StateDown, "s2": health.StateDown,
|
|
},
|
|
want: map[string]uint8{
|
|
"10.0.0.1": 0, "10.0.0.2": 0,
|
|
"10.0.0.11": 0, "10.0.0.12": 0,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
src := &fakeStateSource{cfg: cfg, states: tc.states}
|
|
d := desiredFromFrontend(cfg, fe, src)
|
|
for addr, wantW := range tc.want {
|
|
got, ok := d.ASes[addr]
|
|
if !ok {
|
|
t.Errorf("%s: missing from desired set", addr)
|
|
continue
|
|
}
|
|
if got.Weight != wantW {
|
|
t.Errorf("%s: weight got %d, want %d", addr, got.Weight, wantW)
|
|
}
|
|
}
|
|
if len(d.ASes) != len(tc.want) {
|
|
t.Errorf("got %d ASes, want %d", len(d.ASes), len(tc.want))
|
|
}
|
|
})
|
|
}
|
|
}
|