edge: purge the LRU only for hosts the purging route still owns
Ledger R10 (refuse up front): both purge channels called PurgeHost, PurgePath or PurgeURL on the original hostname unconditionally, so a response or control listener that outlived its hostname's move to another instance could delete that instance's primary-host LRU entries. purgeOwns now judges every LRU purge in one routing snapshot: the host resolves to the owner's instance, or path-routes to it while the owner still holds its primary. The header channel filters its hosts through it; the socket asks the bridge (ownsHost), and its "all" purges exactly the instance hosts of one snapshot. Page clears are unchanged (already pinned to the owner's scope). Both replacement tests now assert B's primary-host LRU entry survives A's all, * and path purges.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+147
−44
internal/edge/cache.go+3 −0internal/edge/pagecache.go+5 −0internal/edge/pageclear_test.go+79 −33internal/edge/purge.go+18 −8internal/edge/purge_header.go+42 −3
diff --git a/internal/edge/cache.go b/internal/edge/cache.go| index d49f1bb47..d31139252 100644 |
| --- a/internal/edge/cache.go |
| +++ b/internal/edge/cache.go |
| @@ -118,6 +118,9 @@ type purgeBridge interface { |
| // cached that the table serves as that instance (a name served through |
| // a wildcard alias). |
| instanceHosts(owner registry.Route, cached []string) []string |
| + // ownsHost reports whether owner still owns host in the live table, so |
| + // a purge acting for owner may touch host's LRU entries (purgeOwns). |
| + ownsHost(owner registry.Route, host string) bool |
| } |
| type purgeBridgeRef struct{ b purgeBridge } |
diff --git a/internal/edge/pagecache.go b/internal/edge/pagecache.go| index f59fcd550..4091c6d1a 100644 |
| --- a/internal/edge/pagecache.go |
| +++ b/internal/edge/pagecache.go |
| @@ -155,6 +155,11 @@ func (e *Edge) instanceHosts(owner registry.Route, cached []string) []string { |
| return instanceHostsIn(e.current(), owner, cached) |
| } |
| +// ownsHost is purgeOwns over one snapshot of the live table (purgeBridge). |
| +func (e *Edge) ownsHost(owner registry.Route, host string) bool { |
| + return purgeOwns(e.current(), owner, host) |
| +} |
| + |
| // instanceHostsIn is every host m serves as owner's instance, sorted: each |
| // key whose entry is that instance (the primary and every alias), and each |
| // host in cached that m resolves (lookupIn) to that instance. The second |
diff --git a/internal/edge/pageclear_test.go b/internal/edge/pageclear_test.go| index 47020c5e6..3e52a0e79 100644 |
| --- a/internal/edge/pageclear_test.go |
| +++ b/internal/edge/pageclear_test.go |
| @@ -470,41 +470,49 @@ func TestPurgeCannotReachAReplacementInstance(t *testing.T) { |
| }) |
| } |
| - // The LRU half: B is not opted in, and A's all must not expand to B's |
| - // alias. (A's own host is purged as "*" always purged it.) |
| - t.Run("lru", func(t *testing.T) { |
| - entered, release := make(chan struct{}), make(chan struct{}) |
| - s := newPCScene(t, func(w http.ResponseWriter, r *http.Request) { |
| - if r.Method == http.MethodPost { |
| - entered <- struct{}{} |
| - <-release |
| - w.Header().Set(PurgeHeader, "all") |
| - w.WriteHeader(http.StatusNoContent) |
| - return |
| + // The LRU half: B is not opted in. Whatever A's response declares, it |
| + // purges no LRU entry of B's: not B's alias (A's all must not expand to |
| + // it), and not B's primary host either, which A no longer owns in the |
| + // routing snapshot the purge is judged against (ledger R10). |
| + for _, value := range []string{"all", "*", "/l"} { |
| + t.Run("lru "+value, func(t *testing.T) { |
| + entered, release := make(chan struct{}), make(chan struct{}) |
| + s := newPCScene(t, func(w http.ResponseWriter, r *http.Request) { |
| + if r.Method == http.MethodPost { |
| + entered <- struct{}{} |
| + <-release |
| + w.Header().Set(PurgeHeader, value) |
| + w.WriteHeader(http.StatusNoContent) |
| + return |
| + } |
| + w.Header().Set("Content-Type", "text/html") |
| + w.Header().Set("Cache-Control", "public, max-age=600") |
| + io.WriteString(w, "plain") |
| + }) |
| + s.e.EnablePageCache(nil) |
| + upsertAlias(t, s.reg, pcAlias, pcHost) |
| + if err := s.e.Reload(); err != nil { |
| + t.Fatal(err) |
| + } |
| + done := make(chan struct{}) |
| + go func() { |
| + defer close(done) |
| + s.e.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "http://"+pcHost+"/admin", nil)) |
| + }() |
| + <-entered |
| + s.setRoute(func(r *registry.Route) { r.App = "shop" }) |
| + s.do(http.MethodGet, "http://"+pcHost+"/l") |
| + s.do(http.MethodGet, "http://"+pcAlias+"/l") |
| + close(release) |
| + <-done |
| + if _, ok := s.e.cache.get(CacheKey{Host: pcAlias, Method: "GET", Path: "/l"}); !ok { |
| + t.Fatal("A's purge purged B's alias from the LRU") |
| + } |
| + if _, ok := s.e.cache.get(CacheKey{Host: pcHost, Method: "GET", Path: "/l"}); !ok { |
| + t.Fatal("A's purge purged B's primary host from the LRU") |
| } |
| - w.Header().Set("Content-Type", "text/html") |
| - w.Header().Set("Cache-Control", "public, max-age=600") |
| - io.WriteString(w, "plain") |
| }) |
| - s.e.EnablePageCache(nil) |
| - upsertAlias(t, s.reg, pcAlias, pcHost) |
| - if err := s.e.Reload(); err != nil { |
| - t.Fatal(err) |
| - } |
| - done := make(chan struct{}) |
| - go func() { |
| - defer close(done) |
| - s.e.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "http://"+pcHost+"/admin", nil)) |
| - }() |
| - <-entered |
| - s.setRoute(func(r *registry.Route) { r.App = "shop" }) |
| - s.do(http.MethodGet, "http://"+pcAlias+"/l") |
| - close(release) |
| - <-done |
| - if _, ok := s.e.cache.get(CacheKey{Host: pcAlias, Method: "GET", Path: "/l"}); !ok { |
| - t.Fatal("A's all purged B's alias from the LRU") |
| - } |
| - }) |
| + } |
| } |
| // TestControlSocketCannotReachAReplacementInstance: the listener built for |
| @@ -539,6 +547,44 @@ func TestControlSocketCannotReachAReplacementInstance(t *testing.T) { |
| s.wantKept("B's alias page", pcAlias, "/p") |
| }) |
| } |
| + |
| + // The LRU half: B is not opted in, and A's listener purges no LRU entry |
| + // of B's, its primary host included: A no longer owns that hostname in |
| + // the routing snapshot the purge is judged against (ledger R10). |
| + for _, path := range []string{"all", "*", "/l"} { |
| + t.Run("lru "+path, func(t *testing.T) { |
| + s := newPCScene(t, func(w http.ResponseWriter, r *http.Request) { |
| + w.Header().Set("Content-Type", "text/html") |
| + w.Header().Set("Cache-Control", "public, max-age=600") |
| + io.WriteString(w, "plain") |
| + }) |
| + s.e.EnablePageCache(nil) |
| + upsertAlias(t, s.reg, pcAlias, pcHost) |
| + if err := s.e.Reload(); err != nil { |
| + t.Fatal(err) |
| + } |
| + sock := filepath.Join(shortPurgeSocketDir(t), "c.sock") |
| + ctl, err := ListenControl(sock, s.rt, s.e.cache, nil) |
| + if err != nil { |
| + t.Fatalf("ListenControl: %v", err) |
| + } |
| + t.Cleanup(func() { ctl.Close() }) |
| + c := dialPurgeSocket(t, sock) |
| + |
| + s.setRoute(func(r *registry.Route) { r.App = "shop" }) |
| + s.do(http.MethodGet, "http://"+pcHost+"/l") |
| + s.do(http.MethodGet, "http://"+pcAlias+"/l") |
| + if code := postSocketPurge(t, c, map[string]string{"path": path}); code != http.StatusOK { |
| + t.Fatalf("purge status = %d", code) |
| + } |
| + if _, ok := s.e.cache.get(CacheKey{Host: pcAlias, Method: "GET", Path: "/l"}); !ok { |
| + t.Fatal("A's listener purged B's alias from the LRU") |
| + } |
| + if _, ok := s.e.cache.get(CacheKey{Host: pcHost, Method: "GET", Path: "/l"}); !ok { |
| + t.Fatal("A's listener purged B's primary host from the LRU") |
| + } |
| + }) |
| + } |
| } |
| // TestPurgeAllClearsWildcardServedHosts: all purges the LRU entries cached |
diff --git a/internal/edge/purge.go b/internal/edge/purge.go| index 3cdd41e82..df9927d74 100644 |
| --- a/internal/edge/purge.go |
| +++ b/internal/edge/purge.go |
| @@ -167,6 +167,12 @@ func ListenControl(socketPath string, rt registry.Route, cache *Cache, mount fun |
| bridge.clearPages(rt, host, c) |
| } |
| } |
| + // owned says whether rt still owns host in the edge's live |
| + // table (ledger R10): a listener that outlived its hostname's |
| + // move to another instance purges none of that instance's LRU |
| + // entries. With no bridge there is no table to ask, and the |
| + // LRU is the only cache. |
| + owned := func() bool { return bridge == nil || bridge.ownsHost(rt, host) } |
| switch req.Path { |
| case purgeAllPath: |
| if req.Query != "" { |
| @@ -175,7 +181,9 @@ func ListenControl(socketPath string, rt registry.Route, cache *Cache, mount fun |
| } |
| // host is still the one this listener was built for: "*" widens |
| // the purge to every path of that host, never to another host. |
| - cache.PurgeHost(host) |
| + if owned() { |
| + cache.PurgeHost(host) |
| + } |
| clearPages(pageindex.Clear{Targets: []pageindex.Target{{Kind: pageindex.TargetHost}}}) |
| case purgeInstanceTarget: |
| if req.Query != "" { |
| @@ -183,18 +191,20 @@ func ListenControl(socketPath string, rt registry.Route, cache *Cache, mount fun |
| return |
| } |
| // Every host the edge's live route table serves as rt's |
| - // instance, never anything named by the body. |
| - cache.PurgeHost(host) |
| - if bridge != nil { |
| + // instance, never anything named by the body. One snapshot |
| + // answers both: host is among them only while rt owns it. |
| + if bridge == nil { |
| + cache.PurgeHost(host) |
| + } else { |
| for _, h := range bridge.instanceHosts(rt, cache.cachedHosts()) { |
| - if h != host { |
| - cache.PurgeHost(h) |
| - } |
| + cache.PurgeHost(h) |
| } |
| } |
| clearPages(pageindex.Clear{}) |
| default: |
| - cache.PurgeURL(host, req.Path, req.Query) |
| + if owned() { |
| + cache.PurgeURL(host, req.Path, req.Query) |
| + } |
| clearPages(pageindex.Clear{Targets: []pageindex.Target{{Kind: pageindex.TargetURL, Path: req.Path, RawQuery: req.Query}}}) |
| } |
| w.WriteHeader(http.StatusOK) |
diff --git a/internal/edge/purge_header.go b/internal/edge/purge_header.go| index 1289964ee..97e41ecf8 100644 |
| --- a/internal/edge/purge_header.go |
| +++ b/internal/edge/purge_header.go |
| @@ -707,6 +707,12 @@ func validPurgeOverrideHost(h string) bool { |
| // construction: degrading to the SERVING host instead would be the worst of |
| // both, wiping a host nobody asked about while leaving the stale pages the app |
| // was trying to invalidate. |
| +// |
| +// Every LRU purge is of a host its owner still owns in cur (purgeOwns, ledger |
| +// R10): a response whose hostname moved to another instance while it was in |
| +// flight purges nothing of that instance's from the LRU, its primary host |
| +// included. Page clears need no such test: they are pinned to the owner's |
| +// own scope (clearPages). |
| func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pageHosts []purgeHost, joined string) { |
| if len(hosts) == 0 { |
| return |
| @@ -716,8 +722,15 @@ func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pa |
| e.clearPages(p.owner, p.host, pageindex.Clear{Targets: targets}) |
| } |
| } |
| + // lru is the hosts whose LRU entries this declaration may purge. |
| + lru := make([]purgeHost, 0, len(hosts)) |
| + for _, p := range hosts { |
| + if purgeOwns(cur, p.owner, p.host) { |
| + lru = append(lru, p) |
| + } |
| + } |
| purgeHosts := func() { |
| - for _, p := range hosts { |
| + for _, p := range lru { |
| e.cache.PurgeHost(p.host) |
| } |
| clearPages([]pageindex.Target{{Kind: pageindex.TargetHost}}) |
| @@ -754,7 +767,9 @@ func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pa |
| if slices.Contains(targets, purgeInstanceTarget) { |
| cached := e.cache.cachedHosts() |
| for _, p := range hosts { |
| - e.cache.PurgeHost(p.host) |
| + if purgeOwns(cur, p.owner, p.host) { |
| + e.cache.PurgeHost(p.host) |
| + } |
| for _, h := range instanceHostsIn(cur, p.owner, cached) { |
| if h != p.host { |
| e.cache.PurgeHost(h) |
| @@ -789,7 +804,7 @@ func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pa |
| } |
| keys = append(keys, key{path, rawQuery, exact}) |
| } |
| - for _, p := range hosts { |
| + for _, p := range lru { |
| for _, k := range keys { |
| if k.exact { |
| e.cache.PurgeURL(p.host, k.path, k.rawQuery) |
| @@ -820,6 +835,30 @@ func (e *Edge) applyPurgeTargets(cur map[string]upstream, from string, hosts, pa |
| } |
| } |
| +// purgeOwns reports whether a purge acting for owner may touch the LRU |
| +// entries cached under host, judged in the one routing snapshot m (ledger |
| +// R10, purge authority): m resolves host (lookupIn) to owner's instance, or |
| +// host path-routes to owner while m still serves owner's primary host as |
| +// owner's instance (the path-routing exception: the pages owner serves |
| +// under host are its own). A hostname that has moved to another instance is |
| +// that instance's, so a purge still on its way from the old one, by a |
| +// response or a control listener built before the move, leaves its LRU |
| +// entries alone. |
| +func purgeOwns(m map[string]upstream, owner registry.Route, host string) bool { |
| + up, ok := lookupIn(m, host) |
| + if !ok { |
| + return false |
| + } |
| + if sameInstance(up.route, owner) { |
| + return true |
| + } |
| + if !pathRoutesTo(up, owner.Host) { |
| + return false |
| + } |
| + primary, ok := m[owner.Host] |
| + return ok && sameInstance(primary.route, owner) |
| +} |
| + |
| // logPurgeIssue rate-limits one class of purge complaint for one host, using |
| // the same first-then-every-300th shape as the registry watcher in edge.go. |
| // |