domains: write only changed path rows; path cost benchmarks
The reconcile pass now upserts a route_paths row only when it differs from the stored one, so an unchanged republish writes nothing (spec §6). The write block moves into writePaths, which keeps ReconcileAliasesReport under its code-health record (lowered 364 -> 349). Adds the three cost benchmarks later tasks re-run, at 1, 5 and 10 hosts x 10,000 prefixes, with a local workload until Task 3 flips placement: BenchmarkReconcilePathSection (unchanged and initial), BenchmarkFingerprintPaths and BenchmarkReloadPaths.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+443
−9
.amadan/code-health.tsv+1 −1internal/domains/aliases.go+42 −8internal/domains/paths_cost_test.go+231 −0internal/edge/path_reload_bench_test.go+98 −0internal/registry/registry_paths_bench_test.go+71 −0
diff --git a/.amadan/code-health.tsv b/.amadan/code-health.tsv| index d4d748c8f..b5dfee73e 100644 |
| --- a/.amadan/code-health.tsv |
| +++ b/.amadan/code-health.tsv |
| @@ -2186,7 +2186,7 @@ func-lines internal/console (*Store).migrate - 335 |
| func-lines internal/console TestDemoProtectionsSourceToLimitAndTakedown - 412 |
| func-lines internal/console TestDemoSourceToLiveAndUpdate - 383 |
| func-lines internal/console TestHAP2ProductionDoors - 304 |
| -func-lines internal/domains ReconcileAliasesReport - 364 |
| +func-lines internal/domains ReconcileAliasesReport - 349 |
| func-lines internal/edge (*Edge).ServeHTTP - 421 |
| func-lines internal/edge TestDemoAPIAliasBoundary - 360 |
| func-lines internal/edge TestRegionalNameNeverLeavesItsRegion - 330 |
diff --git a/internal/domains/aliases.go b/internal/domains/aliases.go| index 849b1e4e3..d7d50b45d 100644 |
| --- a/internal/domains/aliases.go |
| +++ b/internal/domains/aliases.go |
| @@ -959,18 +959,50 @@ func ReconcileAliasesReport(ctx context.Context, s release.Store, reg *registry. |
| // prefix removed from a republished map disappears here with no delete |
| // verb anywhere. That is exactly how a per-account rollback reaches the |
| // edge. |
| - existingPaths, err := reg.ListPaths() |
| + pathsIncomplete, err := writePaths(reg, desiredPaths, noRemoval) |
| if err != nil { |
| return err |
| } |
| - for _, p := range desiredPaths { |
| + if pathsIncomplete { |
| + incomplete = true |
| + } |
| + if rep != nil { |
| + rep.Complete = !incomplete |
| + } |
| + return nil |
| +} |
| + |
| +// writePaths brings route_paths to desired (keyed host + "\x00" + prefix). |
| +// Only a row that differs is written (spec §6): the pass runs every few |
| +// seconds on every box, and an unchanged republish of a 10,000-prefix map |
| +// must write nothing. A RoutePath is all its columns, so equality is |
| +// "already stored exactly so". A row no longer desired is deleted, unless |
| +// noRemoval (the startup pass) leaves it for the next ordinary pass. It |
| +// reports whether the result is incomplete: a write failed, or a row was |
| +// left. The error is the listing's, which aborts the pass. |
| +func writePaths(reg *registry.Registry, desired map[string]registry.RoutePath, noRemoval bool) (incomplete bool, err error) { |
| + existing, err := reg.ListPaths() |
| + if err != nil { |
| + return false, err |
| + } |
| + existingByKey := make(map[string]registry.RoutePath, len(existing)) |
| + for _, p := range existing { |
| + existingByKey[p.Host+"\x00"+p.Prefix] = p |
| + } |
| + for k, p := range desired { |
| + if cur, ok := existingByKey[k]; ok && cur == p { |
| + continue |
| + } |
| + if upsertPathHook != nil { |
| + upsertPathHook(p) |
| + } |
| if err := reg.UpsertPath(p); err != nil { |
| log.Printf("domains: upsert path %s/%s: %v", p.Host, p.Prefix, err) |
| incomplete = true |
| } |
| } |
| - for _, p := range existingPaths { |
| - if _, ok := desiredPaths[p.Host+"\x00"+p.Prefix]; !ok { |
| + for _, p := range existing { |
| + if _, ok := desired[p.Host+"\x00"+p.Prefix]; !ok { |
| if noRemoval { |
| incomplete = true // left for the next ordinary pass |
| continue |
| @@ -981,12 +1013,14 @@ func ReconcileAliasesReport(ctx context.Context, s release.Store, reg *registry. |
| } |
| } |
| } |
| - if rep != nil { |
| - rep.Complete = !incomplete |
| - } |
| - return nil |
| + return incomplete, nil |
| } |
| +// upsertPathHook, when set, is called just before each route_paths upsert |
| +// the reconcile pass makes. nil in production; the cost tests count writes |
| +// with it. |
| +var upsertPathHook func(registry.RoutePath) |
| + |
| // deriveRegional adds a regional claim's desired regional_domains rows (#447, |
| // spec §7.2) for a box in boxRegion: the claim's host and, with Wildcard and |
| // Catchall, "*.<host>", each naming the claim's placement for boxRegion when |
diff --git a/internal/domains/paths_cost_test.go b/internal/domains/paths_cost_test.go| new file mode 100644 |
| index 000000000..30acb389c |
| --- /dev/null |
| +++ b/internal/domains/paths_cost_test.go |
| @@ -0,0 +1,231 @@ |
| +package domains |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "fmt" |
| + "io" |
| + "log" |
| + "path/filepath" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// TestReconcileUnchangedPathsWritesNothing is the cost budget's write half |
| +// (spec §6): a republish that changes no path row writes no path row. The |
| +// pass runs every few seconds on every box, so rewriting 10,000 identical |
| +// rows each time would be the whole cost of the section. |
| +func TestReconcileUnchangedPathsWritesNothing(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + reg := aliasRegWith(t) |
| + addRoute(t, reg, "ti.to", "tito", "classic") |
| + addRoute(t, reg, "go.tito.io", "tito", "go") |
| + addRoute(t, reg, "beta.tito.io", "tito", "go") |
| + putClaim(t, s, Claim{ |
| + Host: "ti.to", Account: "tito", App: "classic", Route: "ti.to", |
| + CreatedAt: "2026-09-03T00:00:00Z", |
| + Paths: map[string]string{ |
| + "paulca": "go.tito.io", |
| + "bigcorp": "go.tito.io", |
| + "canary": "beta.tito.io", |
| + }, |
| + }) |
| + if err := ReconcileAliases(ctx, s, reg, aliasNow(), nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + calls := 0 |
| + upsertPathHook = func(registry.RoutePath) { calls++ } |
| + t.Cleanup(func() { upsertPathHook = nil }) |
| + if err := ReconcileAliases(ctx, s, reg, aliasNow(), nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if calls != 0 { |
| + t.Fatalf("second pass over an unchanged claim upserted %d path rows, want 0", calls) |
| + } |
| + got := pathsByPrefix(t, reg, "ti.to") |
| + want := map[string]string{"paulca": "go.tito.io", "bigcorp": "go.tito.io", "canary": "beta.tito.io"} |
| + if len(got) != len(want) { |
| + t.Fatalf("route_paths = %v, want %v", got, want) |
| + } |
| + for p, tgt := range want { |
| + if got[p] != tgt { |
| + t.Fatalf("route_paths = %v, want %v", got, want) |
| + } |
| + } |
| +} |
| + |
| +// TestReconcileRepointedPathWritesOnlyThatRow is the other half of the diff: |
| +// skipping unchanged rows must never skip a changed one. Repointing one |
| +// prefix of three writes exactly that row, with its new target. |
| +func TestReconcileRepointedPathWritesOnlyThatRow(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + reg := aliasRegWith(t) |
| + addRoute(t, reg, "ti.to", "tito", "classic") |
| + addRoute(t, reg, "go.tito.io", "tito", "go") |
| + addRoute(t, reg, "beta.tito.io", "tito", "go") |
| + putClaim(t, s, Claim{ |
| + Host: "ti.to", Account: "tito", App: "classic", Route: "ti.to", |
| + CreatedAt: "2026-09-03T00:00:00Z", |
| + Paths: map[string]string{"paulca": "go.tito.io", "bigcorp": "go.tito.io", "canary": "go.tito.io"}, |
| + }) |
| + if err := ReconcileAliases(ctx, s, reg, aliasNow(), nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := SetClaimPaths(ctx, s, "ti.to", "tito", "classic", |
| + map[string]string{"paulca": "go.tito.io", "bigcorp": "go.tito.io", "canary": "beta.tito.io"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + var written []registry.RoutePath |
| + upsertPathHook = func(p registry.RoutePath) { written = append(written, p) } |
| + t.Cleanup(func() { upsertPathHook = nil }) |
| + if err := ReconcileAliases(ctx, s, reg, aliasNow(), nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(written) != 1 || written[0].Prefix != "canary" || written[0].Target != "beta.tito.io" { |
| + t.Fatalf("pass after repointing canary upserted %+v, want only canary -> beta.tito.io", written) |
| + } |
| + if got := pathsByPrefix(t, reg, "ti.to"); got["canary"] != "beta.tito.io" || got["paulca"] != "go.tito.io" || len(got) != 3 { |
| + t.Fatalf("route_paths = %v, want canary -> beta.tito.io and the other two unchanged", got) |
| + } |
| +} |
| + |
| +// pathCostPrefixes is the per-host prefix count of the cost budget (spec |
| +// §6): every benchmark below runs hosts × this many path rows. |
| +const pathCostPrefixes = 10_000 |
| + |
| +// pathCostTargets is how many target routes each front host's prefixes are |
| +// spread over, round-robin. |
| +const pathCostTargets = 10 |
| + |
| +// pathCostLocal is the placement the benchmarks run with. Until Task 3 the |
| +// reconcile pass derives rows only for a front route and ready targets on |
| +// this box, so the workload must be local to survive the code under test; |
| +// Task 3 flips this to false. |
| +const pathCostLocal = true |
| + |
| +func pathCostFront(i int) string { return fmt.Sprintf("front-%d.example.com", i) } |
| +func pathCostTarget(i, j int) string { return fmt.Sprintf("target-%d-%d.example.com", i, j) } |
| + |
| +// pathCostRoutes writes the route rows of a hosts-wide workload into reg. |
| +// With local false it writes none: the fronts and targets are homed on other |
| +// boxes, and the task that switches the workload supplies whatever marks them |
| +// served elsewhere. |
| +func pathCostRoutes(tb testing.TB, reg *registry.Registry, hosts int, local bool) { |
| + tb.Helper() |
| + if !local { |
| + return |
| + } |
| + for i := 0; i < hosts; i++ { |
| + rows := []registry.Route{{Host: pathCostFront(i), Account: "shop", App: "front", Kind: "instance", Status: "ready"}} |
| + for j := 0; j < pathCostTargets; j++ { |
| + rows = append(rows, registry.Route{Host: pathCostTarget(i, j), Account: "shop", App: "back", Kind: "instance", Status: "ready"}) |
| + } |
| + for _, rt := range rows { |
| + if err := reg.Upsert(rt); err != nil { |
| + tb.Fatal(err) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// pathCostClaims publishes one claim per front host, each with |
| +// pathCostPrefixes prefixes spread over the host's targets. |
| +func pathCostClaims(tb testing.TB, s release.Store, hosts int) { |
| + tb.Helper() |
| + for i := 0; i < hosts; i++ { |
| + paths := make(map[string]string, pathCostPrefixes) |
| + for p := 0; p < pathCostPrefixes; p++ { |
| + paths[fmt.Sprintf("p%05d", p)] = pathCostTarget(i, p%pathCostTargets) |
| + } |
| + c := Claim{ |
| + Schema: 1, Host: pathCostFront(i), Account: "shop", App: "front", Route: pathCostFront(i), |
| + CreatedAt: "2026-09-03T00:00:00Z", Paths: paths, |
| + } |
| + body, err := json.Marshal(c) |
| + if err != nil { |
| + tb.Fatal(err) |
| + } |
| + if _, err := s.Put(context.Background(), ClaimKey(c.Host), body, ""); err != nil { |
| + tb.Fatal(err) |
| + } |
| + } |
| +} |
| + |
| +func pathCostRegistry(tb testing.TB, hosts int, local bool) *registry.Registry { |
| + tb.Helper() |
| + reg, err := registry.Open(filepath.Join(tb.TempDir(), "registry.db")) |
| + if err != nil { |
| + tb.Fatal(err) |
| + } |
| + tb.Cleanup(func() { reg.Close() }) |
| + pathCostRoutes(tb, reg, hosts, local) |
| + return reg |
| +} |
| + |
| +func pathCostWantRows(b *testing.B, reg *registry.Registry, hosts int) { |
| + b.Helper() |
| + rows, err := reg.ListPaths() |
| + if err != nil { |
| + b.Fatal(err) |
| + } |
| + if want := hosts * pathCostPrefixes; len(rows) != want { |
| + b.Fatalf("route_paths holds %d rows, want %d: the workload did not survive the pass", len(rows), want) |
| + } |
| +} |
| + |
| +// BenchmarkReconcilePathSection is the cost budget's reconcile half (spec |
| +// §6): one ReconcileAliases pass at 1, 5 and 10 hosts × 10,000 prefixes. |
| +// "unchanged" is the steady state every box runs every few seconds, after |
| +// the first derivation; "initial" is that first derivation into an empty |
| +// table. |
| +func BenchmarkReconcilePathSection(b *testing.B) { |
| + ctx := context.Background() |
| + // The pass logs one line per front host per pass ("a real route owns |
| + // that host"); at five passes per run that is noise, not signal. |
| + prev := log.Writer() |
| + log.SetOutput(io.Discard) |
| + b.Cleanup(func() { log.SetOutput(prev) }) |
| + for _, hosts := range []int{1, 5, 10} { |
| + b.Run(fmt.Sprintf("hosts=%d", hosts), func(b *testing.B) { |
| + s := release.NewDirStore(b.TempDir()) |
| + pathCostClaims(b, s, hosts) |
| + |
| + b.Run("unchanged", func(b *testing.B) { |
| + reg := pathCostRegistry(b, hosts, pathCostLocal) |
| + cache := &ClaimCache{} |
| + if err := ReconcileAliases(ctx, s, reg, aliasNow(), nil, cache); err != nil { |
| + b.Fatal(err) |
| + } |
| + b.ResetTimer() |
| + for i := 0; i < b.N; i++ { |
| + if err := ReconcileAliases(ctx, s, reg, aliasNow(), nil, cache); err != nil { |
| + b.Fatal(err) |
| + } |
| + } |
| + b.StopTimer() |
| + pathCostWantRows(b, reg, hosts) |
| + }) |
| + |
| + b.Run("initial", func(b *testing.B) { |
| + var reg *registry.Registry |
| + for i := 0; i < b.N; i++ { |
| + b.StopTimer() |
| + reg = pathCostRegistry(b, hosts, pathCostLocal) |
| + cache := &ClaimCache{} |
| + b.StartTimer() |
| + if err := ReconcileAliases(ctx, s, reg, aliasNow(), nil, cache); err != nil { |
| + b.Fatal(err) |
| + } |
| + } |
| + b.StopTimer() |
| + pathCostWantRows(b, reg, hosts) |
| + }) |
| + }) |
| + } |
| +} |
diff --git a/internal/edge/path_reload_bench_test.go b/internal/edge/path_reload_bench_test.go| new file mode 100644 |
| index 000000000..f7303a021 |
| --- /dev/null |
| +++ b/internal/edge/path_reload_bench_test.go |
| @@ -0,0 +1,98 @@ |
| +package edge |
| + |
| +import ( |
| + "fmt" |
| + "path/filepath" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// pathReloadPrefixes and pathReloadTargets are the cost budget's workload |
| +// (spec §6), the same shape as the domains package's |
| +// BenchmarkReconcilePathSection: hosts × 10,000 prefixes, each host's |
| +// prefixes spread round-robin over ten target routes. |
| +const ( |
| + pathReloadPrefixes = 10_000 |
| + pathReloadTargets = 10 |
| +) |
| + |
| +// pathReloadLocal is the placement the benchmark runs with. Today Reload |
| +// attaches a path row only to a front route this box serves, so the |
| +// workload must be local to survive the code under test; Task 5 flips this |
| +// to false (unserved hosts, whose rows land in the remote map). |
| +const pathReloadLocal = true |
| + |
| +// pathReloadRegistry opens a registry holding hosts × pathReloadPrefixes |
| +// route_paths rows, and, with local, the fronts' and targets' route rows. |
| +func pathReloadRegistry(tb testing.TB, hosts int, local bool) *registry.Registry { |
| + tb.Helper() |
| + reg, err := registry.Open(filepath.Join(tb.TempDir(), "registry.db")) |
| + if err != nil { |
| + tb.Fatal(err) |
| + } |
| + tb.Cleanup(func() { reg.Close() }) |
| + for i := 0; i < hosts; i++ { |
| + front := fmt.Sprintf("front-%d.example.com", i) |
| + target := func(j int) string { return fmt.Sprintf("target-%d-%d.example.com", i, j) } |
| + if local { |
| + // An address nothing listens on: Reload builds a proxy for it |
| + // and never dials. |
| + rows := []registry.Route{{Host: front, Account: "shop", App: "front", Kind: "instance", Status: "ready", Addr: "127.0.0.1:1"}} |
| + for j := 0; j < pathReloadTargets; j++ { |
| + rows = append(rows, registry.Route{Host: target(j), Account: "shop", App: "back", Kind: "instance", Status: "ready", Addr: "127.0.0.1:1"}) |
| + } |
| + for _, rt := range rows { |
| + if err := reg.Upsert(rt); err != nil { |
| + tb.Fatal(err) |
| + } |
| + } |
| + } |
| + for p := 0; p < pathReloadPrefixes; p++ { |
| + if err := reg.UpsertPath(registry.RoutePath{ |
| + Host: front, Prefix: fmt.Sprintf("p%05d", p), Target: target(p % pathReloadTargets), |
| + Account: "shop", App: "front", |
| + }); err != nil { |
| + tb.Fatal(err) |
| + } |
| + } |
| + } |
| + return reg |
| +} |
| + |
| +// pathReloadLive counts the path entries the published map holds. |
| +func pathReloadLive(e *Edge) int { |
| + n := 0 |
| + for _, up := range e.current() { |
| + if up.alias { |
| + continue // an alias carries its primary's map, not rows of its own |
| + } |
| + n += len(up.paths) |
| + } |
| + return n |
| +} |
| + |
| +// BenchmarkReloadPaths is the cost budget's edge half (spec §6): one Reload |
| +// over a registry holding 1, 5 and 10 hosts × 10,000 path rows. After the |
| +// timing it checks the live map holds every row, so a workload the code |
| +// under test dropped cannot pass as cheap. |
| +func BenchmarkReloadPaths(b *testing.B) { |
| + for _, hosts := range []int{1, 5, 10} { |
| + reg := pathReloadRegistry(b, hosts, pathReloadLocal) |
| + e, err := New(reg, nil) |
| + if err != nil { |
| + b.Fatal(err) |
| + } |
| + b.Run(fmt.Sprintf("hosts=%d", hosts), func(b *testing.B) { |
| + for i := 0; i < b.N; i++ { |
| + if err := e.Reload(); err != nil { |
| + b.Fatal(err) |
| + } |
| + } |
| + b.StopTimer() |
| + if got, want := pathReloadLive(e), hosts*pathReloadPrefixes; got != want { |
| + b.Fatalf("live map holds %d path entries, want %d", got, want) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/registry/registry_paths_bench_test.go b/internal/registry/registry_paths_bench_test.go| new file mode 100644 |
| index 000000000..231a89a64 |
| --- /dev/null |
| +++ b/internal/registry/registry_paths_bench_test.go |
| @@ -0,0 +1,71 @@ |
| +package registry |
| + |
| +import ( |
| + "fmt" |
| + "path/filepath" |
| + "testing" |
| +) |
| + |
| +// pathBenchPrefixes and pathBenchTargets are the cost budget's workload |
| +// (spec §6), the same shape as the domains package's |
| +// BenchmarkReconcilePathSection: hosts × 10,000 prefixes, each host's |
| +// prefixes spread round-robin over ten target routes. |
| +const ( |
| + pathBenchPrefixes = 10_000 |
| + pathBenchTargets = 10 |
| +) |
| + |
| +// pathBenchLocal is the placement the benchmark runs with: true writes the |
| +// front and target route rows on this box as well as the path rows. Later |
| +// tasks flip it to false (rows for hosts and targets homed elsewhere). |
| +const pathBenchLocal = true |
| + |
| +// pathBenchRegistry opens a registry holding hosts × pathBenchPrefixes |
| +// route_paths rows, and, with local, the fronts' and targets' route rows. |
| +func pathBenchRegistry(tb testing.TB, hosts int, local bool) *Registry { |
| + tb.Helper() |
| + reg, err := Open(filepath.Join(tb.TempDir(), "registry.db")) |
| + if err != nil { |
| + tb.Fatal(err) |
| + } |
| + tb.Cleanup(func() { reg.Close() }) |
| + for i := 0; i < hosts; i++ { |
| + front := fmt.Sprintf("front-%d.example.com", i) |
| + target := func(j int) string { return fmt.Sprintf("target-%d-%d.example.com", i, j) } |
| + if local { |
| + if err := reg.Upsert(Route{Host: front, Account: "shop", App: "front", Kind: "instance", Status: "ready"}); err != nil { |
| + tb.Fatal(err) |
| + } |
| + for j := 0; j < pathBenchTargets; j++ { |
| + if err := reg.Upsert(Route{Host: target(j), Account: "shop", App: "back", Kind: "instance", Status: "ready"}); err != nil { |
| + tb.Fatal(err) |
| + } |
| + } |
| + } |
| + for p := 0; p < pathBenchPrefixes; p++ { |
| + if err := reg.UpsertPath(RoutePath{ |
| + Host: front, Prefix: fmt.Sprintf("p%05d", p), Target: target(p % pathBenchTargets), |
| + Account: "shop", App: "front", |
| + }); err != nil { |
| + tb.Fatal(err) |
| + } |
| + } |
| + } |
| + return reg |
| +} |
| + |
| +// BenchmarkFingerprintPaths is the cost budget's poll half (spec §6): one |
| +// Fingerprint over a registry holding 1, 5 and 10 hosts × 10,000 path rows. |
| +// Watch runs it every two seconds on every box. |
| +func BenchmarkFingerprintPaths(b *testing.B) { |
| + for _, hosts := range []int{1, 5, 10} { |
| + reg := pathBenchRegistry(b, hosts, pathBenchLocal) |
| + b.Run(fmt.Sprintf("hosts=%d", hosts), func(b *testing.B) { |
| + for i := 0; i < b.N; i++ { |
| + if _, err := reg.Fingerprint(); err != nil { |
| + b.Fatal(err) |
| + } |
| + } |
| + }) |
| + } |
| +} |