carlos / platform Public

edgesfile: cache each body under its Get's ETag; hold mapped addrs as IPv4 (#328)

Review fix round 1 for Task 8. ReadAddrsCached filed a fetched body
under the listing's ETag, so a write between LIST and GET cached the
newer body under the older version: one needless GET, or, if the object
returned to that version, the newer inventory kept for good. It now
keeps the ETag the Get returned with the body.

An IPv4-mapped published entry (::ffff:10.0.1.5, or a mapped /96+
prefix) is now held as IPv4, as Holder holds a query, so it can match.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 4fe52f877c57f7316ab6c9d06bb20a6b83f727ef parent baabbbb
2 files changed, +94 −2
  • internal/edgesfile/addrs.go +10 −2
  • internal/edgesfile/addrs_test.go +84 −0
diff --git a/internal/edgesfile/addrs.go b/internal/edgesfile/addrs.go
index 150e17ae3..667549cc9 100644
--- a/internal/edgesfile/addrs.go
+++ b/internal/edgesfile/addrs.go
@@ -76,18 +76,23 @@ func parseAddrEntry(body []byte) addrEntry {
}
// parseAddr reads a bare address as its full-length prefix, or a prefix.
+// An IPv4-mapped entry is held as IPv4, as Holder holds a query.
func parseAddr(s string) (netip.Prefix, error) {
if strings.Contains(s, "/") {
p, err := netip.ParsePrefix(s)
if err != nil {
return netip.Prefix{}, err
}
+ if p.Addr().Is4In6() && p.Bits() >= 96 {
+ p = netip.PrefixFrom(p.Addr().Unmap(), p.Bits()-96)
+ }
return p.Masked(), nil
}
a, err := netip.ParseAddr(s)
if err != nil || a.Zone() != "" {
return netip.Prefix{}, fmt.Errorf("edgesfile: %q is not an address", s)
}
+ a = a.Unmap()
return netip.PrefixFrom(a, a.BitLen()), nil
}
@@ -197,13 +202,16 @@ func ReadAddrsCached(ctx context.Context, s release.Store, c *AddrCache) (AddrIn
b.add(ke.Key, prev.entry)
continue
}
- body, _, err := s.Get(ctx, ke.Key)
+ body, etag, err := s.Get(ctx, ke.Key)
if err != nil {
b.add(ke.Key, addrEntry{unreadable: true})
continue
}
e := parseAddrEntry(body)
- next[ke.Key] = cachedAddrs{etag: ke.ETag, entry: e}
+ // The body's own ETag, not the listing's: a write between the two
+ // would otherwise file the newer body under the older version, and
+ // the object returning to that version would never be read again.
+ next[ke.Key] = cachedAddrs{etag: etag, entry: e}
b.add(ke.Key, e)
}
c.entries = next
diff --git a/internal/edgesfile/addrs_test.go b/internal/edgesfile/addrs_test.go
index 4251ab780..fcec2c191 100644
--- a/internal/edgesfile/addrs_test.go
+++ b/internal/edgesfile/addrs_test.go
@@ -259,3 +259,87 @@ func (e *etagForwarding) ListETags(ctx context.Context, prefix string) ([]releas
e.listETags++
return e.lister.ListETags(ctx, prefix)
}
+
+// A published IPv4-mapped entry is held as IPv4, as a query is, so the
+// published address and its range find their box either way.
+func TestReadAddrsHolderMatchesMappedPublications(t *testing.T) {
+ t.Parallel()
+ dir := release.NewDirStore(t.TempDir())
+ putEdgesBody(t, dir, "annie", `{"v":1,"box":"annie","eip":"","addrs":["::ffff:10.0.1.5","::ffff:10.0.2.0/120"],"hosts":{}}`)
+ inv, err := ReadAddrs(context.Background(), dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := fmt.Sprint(inv.Boxes["annie"]); got != "[10.0.1.5/32 10.0.2.0/24]" {
+ t.Errorf("annie = %s, want the entries held as IPv4", got)
+ }
+ for addr, want := range map[string]string{
+ "10.0.1.5": "annie",
+ "::ffff:10.0.1.5": "annie",
+ "10.0.2.7": "annie",
+ "::ffff:10.0.2.7": "annie",
+ "10.0.1.6": "",
+ } {
+ box, ok := inv.Holder(netip.MustParseAddr(addr))
+ if box != want || ok != (want != "") {
+ t.Errorf("Holder(%s) = %q, %v; want %q", addr, box, ok, want)
+ }
+ }
+}
+
+// rewriteBeforeGet lists with ETags and, once armed, replaces the object
+// with next just before the Get answers: a box's write landing between the
+// listing and the fetch.
+type rewriteBeforeGet struct {
+ *release.DirStore
+ t *testing.T
+ box, next string
+ gets int
+}
+
+func (r *rewriteBeforeGet) Get(ctx context.Context, key string) ([]byte, string, error) {
+ r.gets++
+ if r.next != "" && key == Key(r.box) {
+ replaceEdgesBody(r.t, r.DirStore, r.box, r.next)
+ r.next = ""
+ }
+ return r.DirStore.Get(ctx, key)
+}
+
+// The cache keeps the body under the ETag the Get returned with it, not
+// the listing's: a body that changed between the two is never filed under
+// the older version, so the object returning to that version is read again.
+func TestReadAddrsCachedKeepsTheETagItsBodyCameWith(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+ const (
+ bodyA = `{"v":1,"box":"annie","eip":"","addrs":["10.0.1.5"],"hosts":{}}`
+ bodyB = `{"v":1,"box":"annie","eip":"","addrs":["10.0.1.6"],"hosts":{}}`
+ )
+ dir := release.NewDirStore(t.TempDir())
+ putEdgesBody(t, dir, "annie", bodyA)
+ s := &rewriteBeforeGet{DirStore: dir, t: t, box: "annie", next: bodyB}
+ var c AddrCache
+ read := func(step string, wantGets int, want string) {
+ t.Helper()
+ s.gets = 0
+ inv, err := ReadAddrsCached(ctx, s, &c)
+ if err != nil {
+ t.Fatalf("%s: %v", step, err)
+ }
+ if s.gets != wantGets || fmt.Sprint(inv.Boxes["annie"]) != want {
+ t.Fatalf("%s: %d Get, annie = %v; want %d, %s", step, s.gets, inv.Boxes["annie"], wantGets, want)
+ }
+ }
+ // Listed as A, fetched as B.
+ read("rewritten between list and get", 1, "[10.0.1.6/32]")
+ // Listed as B: the cache already holds B.
+ read("listed as the fetched version", 0, "[10.0.1.6/32]")
+ // Again listed as A, fetched as B; then the object returns to A.
+ replaceEdgesBody(t, dir, "annie", bodyA)
+ s.next = bodyB
+ read("rewritten again", 1, "[10.0.1.6/32]")
+ replaceEdgesBody(t, dir, "annie", bodyA)
+ read("returned to A", 1, "[10.0.1.5/32]")
+ read("A unchanged", 0, "[10.0.1.5/32]")
+}