carlos / platform Public

edge: owner and generation changes on a warm cache (asset cache plan 3 Task 8)

Edge-side tests: a moved owner, a recreated generation and a policy that
stops catching serve no old entry, and the sweep removes it. No production
change.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 311db4bac52882e5cbde9fcb6b1a9f35c1aaf678 parent 4996b2c
1 file changed, +337 −0
  • internal/edge/asset_owner_test.go +337 −0

CI failed — run details

diff --git a/internal/edge/asset_owner_test.go b/internal/edge/asset_owner_test.go
new file mode 100644
index 000000000..b32bbe5e8
--- /dev/null
+++ b/internal/edge/asset_owner_test.go
@@ -0,0 +1,337 @@
+package edge
+
+import (
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/assetcache"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// Owner and generation changes on a warm cache (edge asset cache design §6,
+// §7; plan 3 Task 8).
+//
+// Not acceptance. The authority table itself (what the real assetPolicy
+// publishes when a host changes account, app or generation) is plan 1's
+// internal/host test; internal/host imports this package, so it cannot run
+// here. This file is the edge's half, on this package's own fixtures: given
+// that the published owner or generation changed, a warm far or home cache
+// serves none of the old entries, and the sweep removes them. Per drift D3 the
+// home's policy answers from a settled snapshot, so the home tests advance it
+// through the fake LocalAssets (the policy's own path is its setter) and, where
+// the point is the race with the edge's route snapshot, withhold Edge.Reload.
+
+const (
+ ownerOldBody = "old owner's script\n"
+ ownerNewBody = "new owner's script\n"
+)
+
+// setRoute rewrites laHost's route row with the owner and generation given
+// and reloads the edge's route snapshot.
+func (s *laScene) setRoute(t *testing.T, acct, app, gen string) {
+ t.Helper()
+ if err := s.reg.Upsert(registry.Route{Host: laHost, Kind: "instance", Addr: s.b.srv.Listener.Addr().String(),
+ Status: "ready", Account: acct, App: app, AssetGen: gen, Version: laBuild}); err != nil {
+ t.Fatal(err)
+ }
+ if err := s.e.Reload(); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// wantStoreHas checks whether the store holds k.
+func wantStoreHas(t *testing.T, st *assetcache.Store, k assetcache.Key, want bool, what string) {
+ t.Helper()
+ if _, ok := st.Get(k); ok != want {
+ t.Fatalf("%s: store holds %+v = %v, want %v", what, k, ok, want)
+ }
+}
+
+// farAnswers makes the fake home's app answer body from now on.
+func (s *farScene) farAnswers(body string) {
+ s.home.set(func(h *farHome) { h.app = assetOK(body) })
+}
+
+// wantLastFillFor checks the home was last asked to fill for the owner and
+// generation given: the assertion the far edge signed names the published
+// owner, not the one the warm entry was stored under.
+func (s *farScene) wantLastFillFor(t *testing.T, path, acct, app, gen string) {
+ t.Helper()
+ seen := s.rec.get()
+ c := assetFillClaims{Host: laHost, PathQuery: path, Account: acct, App: app, Gen: gen}
+ if _, ok := verifyAssetFill(s.meshKey, nil, c, seen.fill, time.Now()); !ok {
+ t.Fatalf("the last fill's assertion %q does not verify for %+v", seen.fill, c)
+ }
+}
+
+// A warm far entry; the host's published owner changes (the account behind it
+// is not the account that filled the entry). The next request must not be
+// answered from the old owner's entry: it misses, fills from the home under
+// the new owner, and the sweep then deletes the old entry.
+func TestHostMovedToAnotherAccountServesNoOldEntry(t *testing.T) {
+ t.Run("far", func(t *testing.T) {
+ s := newFarScene(t, assetOK(ownerOldBody))
+ armForTest(t, s.e, s.store, nil)
+ oldKey := s.key("/assets/app.js")
+ wantCache(t, s.get(t, "/assets/app.js"), "miss")
+ hit := s.get(t, "/assets/app.js")
+ wantCache(t, hit, "hit")
+ if hit.Body.String() != ownerOldBody {
+ t.Fatalf("warm hit body = %q", hit.Body.String())
+ }
+ wantStoreHas(t, s.store, oldKey, true, "warm")
+ before := s.rec.get().n
+
+ // The host moves to another account; the new owner's app answers
+ // differently, so any byte of the old entry would show.
+ s.farAnswers(ownerNewBody)
+ s.publishOwner(t, "xyz", "shop", laGen)
+
+ // The old entry is still on disk (the sweep has not run), and still
+ // unreachable.
+ wantStoreHas(t, s.store, oldKey, true, "before the sweep")
+ rec := s.get(t, "/assets/app.js")
+ wantCache(t, rec, "miss")
+ if rec.Body.String() != ownerNewBody {
+ t.Fatalf("after the move the body = %q, want the new owner's %q", rec.Body.String(), ownerNewBody)
+ }
+ if got := s.rec.get().n; got != before+1 {
+ t.Fatalf("the home saw %d requests, want %d (the move must go back to the home)", got, before+1)
+ }
+ s.wantLastFillFor(t, "/assets/app.js", "xyz", "shop", laGen)
+
+ newKey := oldKey
+ newKey.Account = "xyz"
+ wantStoreHas(t, s.store, newKey, true, "the new owner's fill")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+
+ if n := s.e.sweepAssets(time.Now()); n != 1 {
+ t.Fatalf("sweep dropped %d entries, want the old owner's 1", n)
+ }
+ wantStoreHas(t, s.store, oldKey, false, "after the sweep")
+ wantStoreHas(t, s.store, newKey, true, "after the sweep")
+ if rec := s.get(t, "/assets/app.js"); rec.Body.String() != ownerNewBody {
+ t.Fatalf("after the sweep the body = %q", rec.Body.String())
+ }
+ })
+
+ // The same move on another app under the same account: the key names both.
+ t.Run("far, another app", func(t *testing.T) {
+ s := newFarScene(t, assetOK(ownerOldBody))
+ armForTest(t, s.e, s.store, nil)
+ oldKey := s.key("/assets/app.js")
+ wantCache(t, s.get(t, "/assets/app.js"), "miss")
+ s.farAnswers(ownerNewBody)
+ s.publishOwner(t, "abc", "blog", laGen)
+ rec := s.get(t, "/assets/app.js")
+ wantCache(t, rec, "miss")
+ if rec.Body.String() != ownerNewBody {
+ t.Fatalf("body = %q, want the new app's", rec.Body.String())
+ }
+ if n := s.e.sweepAssets(time.Now()); n != 1 {
+ t.Fatalf("sweep dropped %d entries, want 1", n)
+ }
+ wantStoreHas(t, s.store, oldKey, false, "after the sweep")
+ })
+
+ // The home stops publishing the host's assets altogether (the new owner
+ // declares no asset paths): nothing is served from disk, the request is an
+ // ordinary backhaul, and the sweep deletes the entry.
+ t.Run("far, new owner catches nothing", func(t *testing.T) {
+ s := newFarScene(t, assetOK(ownerOldBody))
+ armForTest(t, s.e, s.store, nil)
+ oldKey := s.key("/assets/app.js")
+ wantCache(t, s.get(t, "/assets/app.js"), "miss")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+ s.farAnswers(ownerNewBody)
+ putJSON(t, s.m.s, "control/edges/betty.json", `{"v":1,"box":"betty","eip":"`+s.eip+`","hosts":{"`+laHost+
+ `":{"role":"home","account":"xyz","app":"shop"}}}`)
+ refreshHomeMap(t, s.m)
+ rec := s.get(t, "/assets/app.js")
+ if rec.Header().Get("Carlos-Cache") != "" || rec.Body.String() != ownerNewBody {
+ t.Fatalf("got Carlos-Cache %q, body %q; want a plain backhaul to the new owner",
+ rec.Header().Get("Carlos-Cache"), rec.Body.String())
+ }
+ if n := s.e.sweepAssets(time.Now()); n != 1 {
+ t.Fatalf("sweep dropped %d entries, want 1", n)
+ }
+ wantStoreHas(t, s.store, oldKey, false, "after the sweep")
+ })
+
+ // The home box. The policy publishes another account for the host; the
+ // edge's route snapshot has not caught up (drift D3, Edge.Reload withheld).
+ // The decision and the route disagree, so the request is an ordinary
+ // uncached one and the old entry is not served. Once the route reloads the
+ // new owner fills under its own key, and the sweep drops the old entry.
+ t.Run("home", func(t *testing.T) {
+ s := newLAScene(t, assetOK(ownerOldBody))
+ armForTest(t, s.e, s.store, s.local)
+ oldKey := s.key("/assets/app.js", "")
+ wantCache(t, s.get(t, "/assets/app.js"), "miss")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+ wantStoreHas(t, s.store, oldKey, true, "warm")
+
+ s.b.mu.Lock()
+ s.b.handler = assetOK(ownerNewBody)
+ s.b.mu.Unlock()
+ moved := laAssets()
+ moved.Account = "xyz"
+ s.local.set(laHost, moved)
+
+ before := s.b.n.Load()
+ rec := s.get(t, "/assets/app.js")
+ wantCache(t, rec, "bypass")
+ if rec.Body.String() != ownerNewBody || s.b.n.Load() != before+1 {
+ t.Fatalf("with the route behind the policy: body %q, app saw %d new requests; want the app's answer, once",
+ rec.Body.String(), s.b.n.Load()-before)
+ }
+ wantStoreHas(t, s.store, oldKey, true, "route snapshot behind: nothing stored or dropped yet")
+
+ s.setRoute(t, "xyz", "shop", laGen)
+ rec = s.get(t, "/assets/app.js")
+ wantCache(t, rec, "miss")
+ if rec.Body.String() != ownerNewBody {
+ t.Fatalf("after the reload the body = %q, want the new owner's", rec.Body.String())
+ }
+ newKey := oldKey
+ newKey.Account = "xyz"
+ wantStoreHas(t, s.store, newKey, true, "the new owner's fill")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+ if n := s.e.sweepAssets(time.Now()); n != 1 {
+ t.Fatalf("sweep dropped %d entries, want the old owner's 1", n)
+ }
+ wantStoreHas(t, s.store, oldKey, false, "after the sweep")
+ wantStoreHas(t, s.store, newKey, true, "after the sweep")
+ })
+}
+
+// A warm entry; the host is deleted and recreated, so its published generation
+// token is new. The old generation's entry is never served again, and the
+// sweep removes it.
+func TestRecreatedHostWithANewGenerationServesNoOldEntry(t *testing.T) {
+ const newGen = "fedcba9876543210"
+
+ t.Run("far", func(t *testing.T) {
+ s := newFarScene(t, assetOK(ownerOldBody))
+ armForTest(t, s.e, s.store, nil)
+ oldKey := s.key("/assets/app.js")
+ wantCache(t, s.get(t, "/assets/app.js"), "miss")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+
+ s.farAnswers(ownerNewBody)
+ s.publishOwner(t, "abc", "shop", newGen)
+
+ wantStoreHas(t, s.store, oldKey, true, "before the sweep")
+ rec := s.get(t, "/assets/app.js")
+ wantCache(t, rec, "miss")
+ if rec.Body.String() != ownerNewBody {
+ t.Fatalf("after the recreate the body = %q, want the new generation's", rec.Body.String())
+ }
+ s.wantLastFillFor(t, "/assets/app.js", "abc", "shop", newGen)
+ newKey := oldKey
+ newKey.Gen = newGen
+ wantStoreHas(t, s.store, newKey, true, "the new generation's fill")
+
+ if n := s.e.sweepAssets(time.Now()); n != 1 {
+ t.Fatalf("sweep dropped %d entries, want the old generation's 1", n)
+ }
+ wantStoreHas(t, s.store, oldKey, false, "after the sweep")
+ wantStoreHas(t, s.store, newKey, true, "after the sweep")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+ })
+
+ // The home box, with the policy ahead of the edge's route snapshot: the
+ // policy's token is new while the route row still carries the old one. The
+ // two disagree, so the request is uncached; the old entry is not served.
+ // After the route reloads the new generation fills, and the sweep drops
+ // the old one.
+ t.Run("home", func(t *testing.T) {
+ s := newLAScene(t, assetOK(ownerOldBody))
+ armForTest(t, s.e, s.store, s.local)
+ oldKey := s.key("/assets/app.js", "")
+ wantCache(t, s.get(t, "/assets/app.js"), "miss")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+
+ s.b.mu.Lock()
+ s.b.handler = assetOK(ownerNewBody)
+ s.b.mu.Unlock()
+ recreated := laAssets()
+ recreated.Gen = newGen
+ s.local.set(laHost, recreated)
+
+ rec := s.get(t, "/assets/app.js")
+ wantCache(t, rec, "bypass")
+ if rec.Body.String() != ownerNewBody {
+ t.Fatalf("with the route behind the policy the body = %q, want the app's", rec.Body.String())
+ }
+
+ s.setRoute(t, "abc", "shop", newGen)
+ rec = s.get(t, "/assets/app.js")
+ wantCache(t, rec, "miss")
+ if rec.Body.String() != ownerNewBody {
+ t.Fatalf("after the reload the body = %q", rec.Body.String())
+ }
+ newKey := oldKey
+ newKey.Gen = newGen
+ wantStoreHas(t, s.store, newKey, true, "the new generation's fill")
+ if n := s.e.sweepAssets(time.Now()); n != 1 {
+ t.Fatalf("sweep dropped %d entries, want the old generation's 1", n)
+ }
+ wantStoreHas(t, s.store, oldKey, false, "after the sweep")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+ })
+}
+
+// A warm home entry; the policy stops catching the host (LocalAssets.For turns
+// false: the app dropped its asset paths, was gated, or went away). The
+// request is forwarded to the app, never answered from disk, and the sweep
+// removes the entry.
+func TestHomeLocalCatchStopsWhenForReturnsFalse(t *testing.T) {
+ s := newLAScene(t, assetOK(laBody))
+ armForTest(t, s.e, s.store, s.local)
+ k := s.key("/assets/app.js", "")
+ wantCache(t, s.get(t, "/assets/app.js"), "miss")
+ wantCache(t, s.get(t, "/assets/app.js"), "hit")
+ wantStoreHas(t, s.store, k, true, "warm")
+ if n := s.b.n.Load(); n != 1 {
+ t.Fatalf("the app saw %d requests filling and hitting, want 1", n)
+ }
+
+ s.b.mu.Lock()
+ s.b.handler = assetOK(ownerNewBody)
+ s.b.mu.Unlock()
+ s.local.mu.Lock()
+ delete(s.local.hosts, laHost)
+ s.local.mu.Unlock()
+ if _, ok := s.local.For(laHost); ok {
+ t.Fatal("fixture: For still answers for the host")
+ }
+
+ // The ordinary path may answer a repeat from the shared LRU (the scene
+ // enables it), so only the first request must reach the app; both must
+ // carry the app's live answer and none of the disk entry's bytes.
+ for i := 0; i < 2; i++ {
+ rec := s.get(t, "/assets/app.js")
+ if rec.Code != http.StatusOK || rec.Body.String() != ownerNewBody {
+ t.Fatalf("request %d = %d %q, want the app's live answer", i, rec.Code, rec.Body.String())
+ }
+ if v := rec.Header().Values("Carlos-Cache"); len(v) != 0 {
+ t.Fatalf("request %d carries Carlos-Cache %q, want the ordinary path's none", i, v)
+ }
+ if i == 0 && s.b.n.Load() != 2 {
+ t.Fatalf("the app saw %d requests, want 2 (forwarded, not served from disk)", s.b.n.Load())
+ }
+ }
+ wantStoreHas(t, s.store, k, true, "before the sweep")
+
+ if n := s.e.sweepAssets(time.Now()); n != 1 {
+ t.Fatalf("sweep dropped %d entries, want 1", n)
+ }
+ wantStoreHas(t, s.store, k, false, "after the sweep")
+
+ // And it comes back as a miss if the policy catches again: a warm entry is
+ // never resurrected.
+ s.local.set(laHost, laAssets())
+ wantCache(t, s.get(t, "/assets/app.js"), "miss")
+}