provision: a scoped pass never runs the policy backstop's listing (#328)
A scoped pass (passLegs.scoped, an index-only change) runs Reconcile whole, so its policy reads went through the same PolicyCache.read as a full pass: once the 30 to 33 minute backstop fell due, whichever pass came next listed control/app-policy/ and read every app whose ETag moved. On a box with the change index armed that is often a scoped pass, which then read other apps' keys (TestScopedPassTouchesOnlyMovedApp). The listing count stayed inside spec 3a's 48 a day, since the timer gates it, but the scoped pass's reads did not stay with its app. ReconcileConfig.PolicyScope marks a scoped pass: its policy reads skip the backstop, which waits for the next full pass, and read only the moved apps under the cache's rules 2 to 4. Any other app keeps its cached read, or is unknown this pass. A nil Policies no longer builds a throwaway cache whose backstop is due every pass: a pass with no cache never lists, and reads each of its apps' policies with one GET. The scoped-pass witness now shares the armed cache as boxProvisionConfig does and runs its scoped pass after the backstop fell due.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+173
−21
internal/host/agent.go+2 −1internal/host/changeindex.go+27 −7internal/host/changeindex_test.go+25 −4internal/provision/apppolicy_cache.go+27 −6internal/provision/apppolicy_cache_test.go+71 −0internal/provision/reconcile.go+21 −3
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 3b64d07e3..7f8145b96 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -1679,7 +1679,8 @@ var reconcileMu sync.Mutex |
| // budget: one backstop listing every 30 to 33 minutes, and a GET only for a |
| // policy that may have changed. Two caches, or one per pass, would read |
| // more. RunAgent builds it after arming the change index; until then |
| -// (tests) it is nil and each pass builds a throwaway. |
| +// (tests) it is nil, and a pass with no cache never lists the policies: it |
| +// reads each app's with one GET (ReconcileConfig.Policies). |
| var armedPolicyCache atomic.Pointer[provision.PolicyCache] |
| // armPolicyCache builds the process's policy cache over the armed change |
diff --git a/internal/host/changeindex.go b/internal/host/changeindex.go| index 37e8b0f80..696411d71 100644 |
| --- a/internal/host/changeindex.go |
| +++ b/internal/host/changeindex.go |
| @@ -211,6 +211,18 @@ func movedRoutes(moved []string) func(registry.Route) bool { |
| return func(rt registry.Route) bool { return apps[[2]string{release.RouteAccount(rt), rt.App}] } |
| } |
| +// movedApps is the (account, app) pairs of moved's app scopes, for the |
| +// scoped pass's policy reads (ReconcileConfig.PolicyScope). |
| +func movedApps(moved []string) map[[2]string]bool { |
| + apps := map[[2]string]bool{} |
| + for _, scope := range moved { |
| + if a, b, ok := provision.ScopeApp(scope); ok { |
| + apps[[2]string{a, b}] = true |
| + } |
| + } |
| + return apps |
| +} |
| + |
| // passLegs is the serial-triggered pass set's inputs (fastConvergeLoop), |
| // shared by the full pass and the scoped one so the two run the same legs |
| // in the same order and cannot drift. |
| @@ -236,7 +248,7 @@ var passAfterReconcileHook func() |
| // full is the serial-triggered pass set, in spec order: mint routes |
| // (reconcile), then reconcile aliases, then stage them (adopt), then deliver |
| // their config, over every hosted route. |
| -func (l *passLegs) full(ctx context.Context) { l.run(ctx, "serial pass stages", nil) } |
| +func (l *passLegs) full(ctx context.Context) { l.run(ctx, "serial pass stages", nil, nil) } |
| // scoped is the pass an index-only change runs (spec §4.5, codex 10): the |
| // same legs, with build states, adopt and config sync restricted to the |
| @@ -244,14 +256,20 @@ func (l *passLegs) full(ctx context.Context) { l.run(ctx, "serial pass stages", |
| // plans legs, so changing one app re-reads that app only. Reconcile and the moves pass |
| // run whole, over the record view. Telemetry resolves every hosted app (its |
| // sink takes a whole map), but its reads are gated on their own scopes |
| -// (telemetryReads), so only the moved apps' files are read. |
| +// (telemetryReads), so only the moved apps' files are read. Reconcile's |
| +// app policy reads are scoped too (ReconcileConfig.PolicyScope): only the |
| +// moved apps' policies, and never the policy backstop's listing of every |
| +// app's, which the next full pass runs. |
| func (l *passLegs) scoped(ctx context.Context, moved []string) { |
| - l.run(ctx, "scoped pass stages", movedRoutes(moved)) |
| + apps := movedApps(moved) |
| + l.run(ctx, "scoped pass stages", movedRoutes(moved), func(acct, app string) bool { return apps[[2]string{acct, app}] }) |
| } |
| -// run is one pass set; keep nil is the unrestricted full pass. Each error is |
| -// one log line, never fatal: the hourly floors stand underneath every leg. |
| -func (l *passLegs) run(ctx context.Context, what string, keep func(registry.Route) bool) { |
| +// run is one pass set; keep nil is the unrestricted full pass, and |
| +// policyScope is the scoped pass's ReconcileConfig.PolicyScope (nil on a |
| +// full pass). Each error is one log line, never fatal: the hourly floors |
| +// stand underneath every leg. |
| +func (l *passLegs) run(ctx context.Context, what string, keep func(registry.Route) bool, policyScope func(acct, app string) bool) { |
| // Each stage's time, logged together when the set is slow, so the |
| // journal says WHICH stage held a promote back. |
| var took []string |
| @@ -262,7 +280,9 @@ func (l *passLegs) run(ctx context.Context, what string, keep func(registry.Rout |
| } |
| defer func(start time.Time) { logSlow(what, start, strings.Join(took, ", ")) }(time.Now()) |
| - if err := reconcileSerialized(ctx, l.store, l.reg, l.cfg()); err != nil { |
| + cfg := l.cfg() |
| + cfg.PolicyScope = policyScope |
| + if err := reconcileSerialized(ctx, l.store, l.reg, cfg); err != nil { |
| log.Printf("agent: fast converge reconcile: %v", err) |
| } |
| mark("reconcile") |
diff --git a/internal/host/changeindex_test.go b/internal/host/changeindex_test.go| index 06086694e..a8d7fc7ce 100644 |
| --- a/internal/host/changeindex_test.go |
| +++ b/internal/host/changeindex_test.go |
| @@ -14,6 +14,7 @@ import ( |
| "time" |
| "github.com/carlosframework/platform/internal/appconfig" |
| + "github.com/carlosframework/platform/internal/apppolicy" |
| "github.com/carlosframework/platform/internal/domains" |
| "github.com/carlosframework/platform/internal/provision" |
| "github.com/carlosframework/platform/internal/registry" |
| @@ -751,10 +752,18 @@ func TestBackstopEveryIsJitteredAroundTheBackstop(t *testing.T) { |
| // runs a scoped pass that reads only shop's keys (its app prefix and its |
| // build state), plus the change index, while shop's new config is |
| // delivered. Nothing under cafe's prefix, nothing for cafe's build state, |
| -// and nothing box-wide (edges files, domain claims) is read. |
| +// and nothing box-wide (edges files, domain claims, the app policy |
| +// backstop's listing of control/app-policy/) is read. The passes share the |
| +// process's policy cache as boxProvisionConfig hands it, and the scoped |
| +// pass runs after the policy backstop fell due (#328, #329): the listing |
| +// waits for the next full pass. |
| func TestScopedPassTouchesOnlyMovedApp(t *testing.T) { |
| w := newCIWorld(t) |
| armView(t, w.live) |
| + prevPolicies := armedPolicyCache.Load() |
| + t.Cleanup(func() { armedPolicyCache.Store(prevPolicies) }) |
| + armPolicyCache() |
| + passNow := time.Now() |
| armedMovesWorker.Store(newMovesWorker(moveCfg{})) |
| t.Cleanup(func() { armedMovesWorker.Store(nil) }) |
| // A sink, so the telemetry leg runs (telemetryPass returns early without |
| @@ -776,11 +785,14 @@ func TestScopedPassTouchesOnlyMovedApp(t *testing.T) { |
| bs := &buildStates{hibBucket: "hib", prefix: func(h string) string { return "inst/" + h }, |
| storeFor: storesOf(map[string]release.Store{"hib": w.live}), reg: reg, optDir: t.TempDir(), bakeDir: t.TempDir(), now: time.Now} |
| legs := &passLegs{store: w.live, reg: reg, optDir: t.TempDir(), priv: pk, dd: dd, bs: bs, |
| - cfg: func() provision.ReconcileConfig { return provision.ReconcileConfig{DataDir: dd, Layout: flatLayout} }} |
| + cfg: func() provision.ReconcileConfig { |
| + return provision.ReconcileConfig{DataDir: dd, Layout: flatLayout, Policies: armedPolicyCache.Load(), |
| + Now: func() time.Time { return passNow }} |
| + }} |
| w.live.reset() |
| legs.full(context.Background()) |
| // The fixture exercises every box-wide leg on the full pass. |
| - for _, want := range []string{"inst/cafe.example.com/", "domains/", "control/edges", |
| + for _, want := range []string{"inst/cafe.example.com/", "domains/", "control/edges", apppolicy.Prefix, |
| telemetry.AccountKey(w.acct), telemetry.AppKey(w.acct, "cafe")} { |
| if !slices.ContainsFunc(w.live.got(), func(c string) bool { _, k, _ := strings.Cut(c, " "); return strings.HasPrefix(k, want) }) { |
| t.Fatalf("fixture: the full pass read nothing under %s: %v", want, w.live.got()) |
| @@ -799,6 +811,8 @@ func TestScopedPassTouchesOnlyMovedApp(t *testing.T) { |
| if err := appconfig.SetEnv(context.Background(), w.live, w.acct, "shop", map[string]string{"APP": "shop2"}); err != nil { |
| t.Fatal(err) |
| } |
| + // Past the policy backstop's 30 to 33 minutes: a full pass would list. |
| + passNow = passNow.Add(2 * release.ChangeIndexBackstop) |
| w.live.reset() |
| legs.scoped(context.Background(), []string{w.scope("shop")}) |
| @@ -806,7 +820,7 @@ func TestScopedPassTouchesOnlyMovedApp(t *testing.T) { |
| t.Fatalf("switch maps pushed %v, want one per pass, both all off", pushed) |
| } |
| shop := "apps/" + w.acct + "/shop/" |
| - var shopReads int |
| + var shopReads, shopPolicyReads int |
| for _, c := range w.live.got() { |
| _, key, _ := strings.Cut(c, " ") |
| switch { |
| @@ -815,6 +829,10 @@ func TestScopedPassTouchesOnlyMovedApp(t *testing.T) { |
| case key == release.ChangeIndexKey, strings.HasPrefix(key, "inst/shop.example.com/"): |
| case strings.HasPrefix(key, "control/config-ack/") && strings.HasSuffix(key, "/shop.example.com.json"): |
| // shop's config acknowledgement, written for the delivery |
| + case key == apppolicy.Key(w.acct, "shop"): |
| + // shop's own policy: its generation moved (the policy cache's |
| + // rule 2), so one read |
| + shopPolicyReads++ |
| default: |
| t.Errorf("the scoped pass for shop read %s, which is not shop's", c) |
| } |
| @@ -822,6 +840,9 @@ func TestScopedPassTouchesOnlyMovedApp(t *testing.T) { |
| if shopReads == 0 { |
| t.Fatalf("the scoped pass read nothing under shop: %v", w.live.got()) |
| } |
| + if shopPolicyReads > 1 { |
| + t.Fatalf("the scoped pass read shop's policy %d times, want at most once: %v", shopPolicyReads, w.live.got()) |
| + } |
| rt, _ := reg.Get("shop.example.com") |
| if got := readConfigEnv(t, dd, *rt); !strings.Contains(got, "APP=shop2") { |
| t.Fatalf("shop's config after the scoped pass = %q, want APP=shop2", got) |
diff --git a/internal/provision/apppolicy_cache.go b/internal/provision/apppolicy_cache.go| index 9bb891b26..4d2c0388e 100644 |
| --- a/internal/provision/apppolicy_cache.go |
| +++ b/internal/provision/apppolicy_cache.go |
| @@ -27,7 +27,10 @@ import ( |
| // only for policies that changed. A pass reads an app's policy only when: |
| // |
| // 1. the backstop is due (the first pass, then 30 to 33 minutes after the |
| -// last completed one, or after a failed one). It snapshots the watch, |
| +// last completed one, or after a failed one) and the pass is a full one |
| +// (readScope.backstop). A scoped pass (the change index moved only some |
| +// apps) never lists: it reads only its own apps, under rules 2 to 4, and |
| +// leaves a due backstop to the next full pass. It snapshots the watch, |
| // lists every policy's ETag once, and GETs only the pass's apps whose |
| // listed ETag differs from the cached one. A pass app the listing does |
| // not name is known absent with no read. A refused listing (an edge |
| @@ -125,10 +128,25 @@ func (c *PolicyCache) completeBackstop(now time.Time) { |
| c.next = now.Add(release.ChangeIndexBackstop + rand.N(release.ChangeIndexBackstop/10)) |
| } |
| +// readScope is what one pass may read. |
| +type readScope struct { |
| + // backstop: the pass may run a due backstop listing (rule 1). Only a |
| + // full pass over the agent's own cache sets it; a scoped pass and a |
| + // pass with no cache (ReconcileConfig.Policies nil) never list. |
| + backstop bool |
| + // only, when set, is the apps a scoped pass may read. Any other pass |
| + // app is answered from its cache entry, with no read, or left out of |
| + // the result (unknown this pass) when it has none. |
| + only func(a [2]string) bool |
| +} |
| + |
| +func (sc readScope) in(a [2]string) bool { return sc.only == nil || sc.only(a) } |
| + |
| // read is the pass's policies for apps (each {sqid, app}), reading the store |
| -// only as the rules on PolicyCache allow. Every read is apppolicy.Get with |
| -// pub and now, so nothing unverified or expired is ever returned as known. |
| -func (c *PolicyCache) read(ctx context.Context, s release.Store, pub ed25519.PublicKey, apps [][2]string, now time.Time) map[[2]string]policyRead { |
| +// only as the rules on PolicyCache and sc allow. Every read is |
| +// apppolicy.Get with pub and now, so nothing unverified or expired is ever |
| +// returned as known. |
| +func (c *PolicyCache) read(ctx context.Context, s release.Store, pub ed25519.PublicKey, apps [][2]string, now time.Time, sc readScope) map[[2]string]policyRead { |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| out := make(map[[2]string]policyRead, len(apps)) |
| @@ -155,12 +173,12 @@ func (c *PolicyCache) read(ctx context.Context, s release.Store, pub ed25519.Pub |
| due := map[[2]string]uint64{} // app -> generation taken before its read |
| listedThisPass := false |
| - if c.backstopDue(now) { |
| + if sc.backstop && c.backstopDue(now) { |
| listedThisPass = c.backstop(ctx, s, apps, now, due) |
| } |
| if !listedThisPass { |
| for _, a := range apps { |
| - if _, ok := due[a]; ok { |
| + if _, ok := due[a]; ok || !sc.in(a) { |
| continue |
| } |
| scope := appScope(a) |
| @@ -185,6 +203,9 @@ func (c *PolicyCache) read(ctx context.Context, s release.Store, pub ed25519.Pub |
| for _, a := range apps { |
| e := c.entries[a] |
| + if e == nil && !sc.in(a) { |
| + continue // outside a scoped pass, never read: unknown |
| + } |
| if e == nil { |
| out[a] = policyRead{state: policyTransient, err: errors.New("the policy listing failed and the policy has not been read")} |
| continue |
diff --git a/internal/provision/apppolicy_cache_test.go b/internal/provision/apppolicy_cache_test.go| index 92ecf613b..7e8a03718 100644 |
| --- a/internal/provision/apppolicy_cache_test.go |
| +++ b/internal/provision/apppolicy_cache_test.go |
| @@ -423,3 +423,74 @@ func TestARecreatedAppDoesNotInheritItsPredecessorsCachedPolicy(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// A scoped pass (ReconcileConfig.PolicyScope, the host's passLegs.scoped) |
| +// never runs the backstop listing, even when it is due, and reads only its |
| +// own apps' policies; the next full pass runs the listing. Fails if a scoped |
| +// pass lists control/app-policy/ or reads another app's policy. |
| +func TestAScopedPassReadsOnlyItsAppsAndLeavesTheBackstopToAFullPass(t *testing.T) { |
| + b := newBudgetFixture(t, true, "shop", "cart", "blog") |
| + b.putPolicy("shop", policyGrant) |
| + b.putPolicy("cart", policyGrant) |
| + b.pass() // a full pass: the backstop |
| + signed := b.fx.now |
| + b.cs.take() |
| + |
| + // Past the backstop, both policies rewritten. The scoped pass for |
| + // shop reads shop's alone; cart keeps its cached read. |
| + b.fx.now = b.fx.now.Add(2 * release.ChangeIndexBackstop) |
| + grown := apppolicy.Policy{Revision: 2, EgressAllow: []string{"10.0.5.20"}, DefaultSize: "xlarge"} |
| + b.putPolicy("shop", grown) |
| + b.putPolicy("cart", grown) |
| + shopOnly := func(acct, app string) bool { return acct == b.sqid && app == "shop" } |
| + b.cfg.PolicyScope = shopOnly |
| + b.pass() |
| + c := b.cs.take() |
| + if c.listETags != 0 || c.lists != 0 || c.totalGets() != 1 || c.gets[b.key("shop")] != 1 { |
| + t.Fatalf("the scoped pass with the backstop due: %d ETag listings, %d LISTs, GETs %v; want only one GET of shop's key", c.listETags, c.lists, c.gets) |
| + } |
| + wantCols(t, b.row("shop"), "10.0.5.20", "xlarge", b.fx.now.Add(apppolicy.Lifetime).Unix()) |
| + wantCols(t, b.row("cart"), "10.0.5.20", "large", signed.Add(apppolicy.Lifetime).Unix()) |
| + b.pass() |
| + b.wantNothing(b.cs.take(), "a second scoped pass with nothing moved") |
| + |
| + // The next full pass runs the due backstop: one listing, and one GET |
| + // for cart, whose listed ETag moved. |
| + b.cfg.PolicyScope = nil |
| + b.pass() |
| + c = b.cs.take() |
| + if c.listETags != 1 || c.lists != 0 || c.totalGets() != 1 || c.gets[b.key("cart")] != 1 { |
| + t.Fatalf("the full pass after: %d ETag listings, %d LISTs, GETs %v; want one listing and one GET of cart's key", c.listETags, c.lists, c.gets) |
| + } |
| + wantCols(t, b.row("cart"), "10.0.5.20", "xlarge", b.fx.now.Add(apppolicy.Lifetime).Unix()) |
| + |
| + // A scoped pass over an empty cache (a process whose first pass is |
| + // scoped): shop, new to the cache, is read with one GET; cart and blog |
| + // are not read, and cart's row keeps its columns. |
| + b.cfg.Policies = NewPolicyCache(b.watch) |
| + b.cfg.PolicyScope = shopOnly |
| + b.fx.now = b.fx.now.Add(time.Minute) |
| + b.pass() |
| + c = b.cs.take() |
| + if c.listETags != 0 || c.lists != 0 || c.totalGets() != 1 || c.gets[b.key("shop")] != 1 { |
| + t.Fatalf("a scoped pass over an empty cache: %d ETag listings, %d LISTs, GETs %v; want only one GET of shop's key", c.listETags, c.lists, c.gets) |
| + } |
| + wantCols(t, b.row("cart"), "10.0.5.20", "xlarge", b.fx.now.Add(-time.Minute).Add(apppolicy.Lifetime).Unix()) |
| +} |
| + |
| +// A pass with no cache (ReconcileConfig.Policies nil: tests, tools) never |
| +// lists control/app-policy/; it reads each of its apps' policies with one GET. |
| +func TestAPassWithNoCacheNeverListsThePolicies(t *testing.T) { |
| + b := newBudgetFixture(t, true, "shop", "blog") |
| + b.putPolicy("shop", policyGrant) |
| + b.cfg.Policies = nil |
| + for i := range 2 { |
| + b.pass() |
| + c := b.cs.take() |
| + if c.listETags != 0 || c.lists != 0 || c.totalGets() != 2 || c.gets[b.key("shop")] != 1 || c.gets[b.key("blog")] != 1 { |
| + t.Fatalf("pass %d with no cache: %d ETag listings, %d LISTs, GETs %v; want one GET each of shop and blog", i, c.listETags, c.lists, c.gets) |
| + } |
| + } |
| + wantCols(t, b.row("shop"), "10.0.5.20", "large", b.fx.now.Add(apppolicy.Lifetime).Unix()) |
| + wantCols(t, b.row("blog"), "", "", 0) |
| +} |
diff --git a/internal/provision/reconcile.go b/internal/provision/reconcile.go| index d79c75095..848fad361 100644 |
| --- a/internal/provision/reconcile.go |
| +++ b/internal/provision/reconcile.go |
| @@ -140,11 +140,23 @@ type ReconcileConfig struct { |
| // (control/app-policy/, #328, #329; apppolicy_cache.go), which keeps the |
| // policy reads inside spec §3a's budget: no read per pass when nothing |
| // changed. Production builds one per process and hands the same pointer |
| - // to every pass. Nil (tests, `carlos` tools) builds a throwaway cache per |
| - // pass, which therefore lists and reads every pass: correct, never cheap. |
| + // to every pass (the host's boxProvisionConfig). Nil (tests, tools) |
| + // never lists control/app-policy/: the backstop listing belongs to the |
| + // agent's one cache, so a pass with none reads each of its apps' policy |
| + // with one GET instead, every pass: correct, never cheap. |
| // Each policy is verified with ReleasePub; a nil ReleasePub makes every |
| // present policy unverifiable (no allow, sizes at the build's tier). |
| Policies *PolicyCache |
| + // PolicyScope marks a scoped pass (S3 change-index design §4.5: the |
| + // index moved only some apps' scopes; the host's passLegs.scoped). Its |
| + // policy reads never run the backstop listing, which belongs to full |
| + // passes (spec §3a: at most 48 a day from those), and read only the |
| + // apps it answers true for, under the cache's rules 2 to 4: a GET only |
| + // for an app new to the cache, whose last read failed, or whose |
| + // generation moved. Any other app keeps its cached read, or is unknown |
| + // this pass when it has none (its rows keep their columns, forRow). |
| + // Nil: a full pass. |
| + PolicyScope func(acct, app string) bool |
| // PolicyChanged is called with a host after this pass changed its row's |
| // egress allow or size (never for an expiry-only change, which the |
| // console's 12-hour re-sign causes). Nil: nobody is told. |
| @@ -1255,11 +1267,17 @@ func Reconcile(ctx context.Context, s release.Store, reg *registry.Registry, cfg |
| } |
| } |
| policyCache := cfg.Policies |
| + policyScope := readScope{backstop: policyCache != nil && cfg.PolicyScope == nil} |
| if policyCache == nil { |
| + // No agent cache: an empty one for this pass alone, which never |
| + // lists (backstop false), so each app is read with one GET. |
| policyCache = NewPolicyCache(nil) |
| } |
| + if in := cfg.PolicyScope; in != nil { |
| + policyScope.only = func(a [2]string) bool { return in(a[0], a[1]) } |
| + } |
| policyNow := cfg.Now() |
| - policies := newPolicyPass(policyCache.read(ctx, freshStore(s), cfg.ReleasePub, policyApps, policyNow), policyNow) |
| + policies := newPolicyPass(policyCache.read(ctx, freshStore(s), cfg.ReleasePub, policyApps, policyNow, policyScope), policyNow) |
| seedCache := map[acctApp]string{} |
| seedKnown := map[acctApp]bool{} |