carlos / platform Public

edge: the import reconcile acts only through domains.ReconcileImport

Every decision and delete is now domains.ReconcileImport's (entry
revision, then claim, then StaleImport, then DeleteIfMatch on that
revision); the edge's own staleImport is gone. The mirror's copies and
one ClaimCache listing are a prefilter only: ReconcileImport runs for a
name the cached view suggests is stale, so a current import costs no
reads, and the cached view never deletes. Issuer only, at most five
calls a pass, nothing on a failed listing.

Tests: a claim unreadable in the listing and the fresh read keeps the
entry; an unread listing still gets a fresh look; an import replaced
since the mirror read it, bound to the re-acquired claim, stays; a
current import costs no import reads over three passes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 10106e6036be7d46b83f762958a5fb3c874ac40e parent b84e644
2 files changed, +186 −101
  • internal/edge/certimport.go +61 −91
  • internal/edge/certimport_test.go +125 −10
diff --git a/internal/edge/certimport.go b/internal/edge/certimport.go
index e606228a6..55a99a66e 100644
--- a/internal/edge/certimport.go
+++ b/internal/edge/certimport.go
@@ -86,12 +86,6 @@ func (m *certMirror) holdsUsableImport(host string, now time.Time) bool {
return m.snapshot().usableImport(host, now) != nil
}
-// imports is the current snapshot's import map. Read-only: the snapshot is
-// immutable once published.
-func (m *certMirror) imports() map[string]importEntry {
- return m.snapshot().imports
-}
-
// wrapImported is the import layer of the handshake chain. edge.go puts it
// above the mirror's wrapGetCertificate and below the wildcard wrapper, and
// AFTER capturing perHostCert, so the cert warm (which calls perHostCert)
@@ -237,30 +231,42 @@ const (
importReconcileDeletesPerPass = 5
)
-// importReconciler deletes imports whose claim binding no longer holds: the
-// claim is gone, pending, an instance catch-all, held by another account or
-// app, or re-acquired (another created_at), or the entry carries no readable
-// binding. It is the one cleanup for every detach path. It also removes an
-// import whose leaf expired more than certSweepGrace ago: the generic cert
-// sweep skips imports, because its delete is not conditional and an import
-// can be replaced under the same key at any time. Issuer only; each
-// delete is conditional on the ETag the mirror read, so an import rewritten
-// since is never removed by a stale view, and a store that cannot delete
-// conditionally deletes nothing.
+// importReconciler is the issuer's cleanup of imports (#1429). It decides
+// and deletes nothing itself: every decision and every delete is
+// domains.ReconcileImport's, which reads the entry's revision, then the
+// claim, judges with domains.StaleImport (the binding does not parse or
+// names another host, the leaf expired more than certSweepGrace ago, or the
+// claim was read and is absent or does not match), and deletes only that
+// revision. It is the one cleanup for every detach path, and the only expiry
+// path for imports: the generic cert sweep skips them, because its delete is
+// not conditional and an import can be replaced under the same key at any
+// time.
+//
+// What this type adds is the cost bound. A ReconcileImport call is two GETs,
+// so a pass does not make one per import every minute: the mirror's copy of
+// each entry and one ClaimCache listing (made after that copy was read) are
+// a PREFILTER, and ReconcileImport runs only for a name that view suggests
+// is stale (StaleImport over the cached copies says so; a claim listed but
+// not read counts as missing there). The cached view never deletes
+// anything. Issuer only; at most
+// importReconcileDeletesPerPass ReconcileImport calls a pass; nothing on a
+// failed listing.
type importReconciler struct {
store release.Store
issuer IssuerGate
// claims is one listing of the deployment's claims, plus the ones it
// listed but could not read (domains.ClaimCache.ListWithUnread).
claims func(context.Context) ([]domains.Claim, []domains.UnreadClaim, error)
- // imports is the mirror snapshot's import map.
- imports func() map[string]importEntry
+ // snapshot is the mirror's current snapshot: each import's bytes and the
+ // ETag they were read at.
+ snapshot func() *certSnapshot
// now is the pass's clock (nil is time.Now; tests inject one).
now func() time.Time
mu sync.Mutex
- // removed is the ETag each name was deleted at, so a mirror that has not
- // listed since does not make the next pass delete and log it again.
+ // removed is the mirror's ETag for each name this box deleted, so a
+ // mirror that has not listed since does not spend another
+ // ReconcileImport call on it.
removed map[string]string
lastLog time.Time
}
@@ -286,8 +292,8 @@ func (e *Edge) newImportReconciler(mirror *certMirror) *importReconciler {
claims: func(ctx context.Context) ([]domains.Claim, []domains.UnreadClaim, error) {
return cc.ListWithUnread(ctx, store)
},
- imports: mirror.imports,
- removed: map[string]string{},
+ snapshot: mirror.snapshot,
+ removed: map[string]string{},
}
}
@@ -300,35 +306,30 @@ func (r *importReconciler) run() {
// pass is one reconcile: nothing unless this box is the issuer and the
// mirror holds an import, then one claims listing and at most
-// importReconcileDeletesPerPass conditional deletes.
+// importReconcileDeletesPerPass ReconcileImport calls.
func (r *importReconciler) pass(ctx context.Context) {
held, ok := r.issuer()
if !ok {
return
}
- // The imports are read BEFORE the claims: the console writes a claim
- // before the import bound to it, so every import seen here has its claim
- // in the listing below.
- imports := r.imports()
+ // The cached view: the mirror's entries, then one claims listing.
+ snap := r.snapshot()
+ imports := snap.imports
r.forget(imports)
if len(imports) == 0 {
return
}
cctx, cancel := context.WithTimeout(held, time.Minute)
- claims, unread, err := r.claims(cctx)
+ claims, _, err := r.claims(cctx)
cancel()
if err != nil {
- // A failed listing is not "no claims": nothing is deleted.
+ // A failed listing is not "no claims": nothing is looked at.
r.logFailure("edge: import reconcile: reading the domain claims failed: %v — no import is removed until it reads again", err)
return
}
- byHost := make(map[string]domains.Claim, len(claims))
- for _, c := range claims {
- byHost[c.Host] = c
- }
- undecided := make(map[string]bool, len(unread))
- for _, u := range unread {
- undecided[u.Host] = true
+ byHost := make(map[string]*domains.Claim, len(claims))
+ for i := range claims {
+ byHost[claims[i].Host] = &claims[i]
}
now := time.Now()
if r.now != nil {
@@ -339,80 +340,49 @@ func (r *importReconciler) pass(ctx context.Context) {
names = append(names, name)
}
slices.Sort(names)
- attempts := 0
+ calls := 0
for _, name := range names {
- ie := imports[name]
- host, reason := staleImport(name, ie, byHost, undecided, now)
- if reason == "" || ie.etag == "" || r.alreadyRemoved(name, ie.etag) {
+ host := strings.TrimSuffix(name, domains.ImportSuffix)
+ if r.alreadyRemoved(name, imports[name].etag) {
continue
}
- if attempts >= importReconcileDeletesPerPass {
+ // The prefilter. A claim the listing named but could not read is
+ // absent from claims, so it is a candidate like a missing one:
+ // ReconcileImport reads it afresh, and keeps the entry if that read
+ // fails as well.
+ if _, suspect := domains.StaleImport(host, snap.entries[name], byHost[host], false, now, certSweepGrace); !suspect {
+ continue
+ }
+ if calls >= importReconcileDeletesPerPass {
return
}
- attempts++
+ calls++
dctx, cancel := context.WithTimeout(held, certPublishTimeout)
- err := release.DeleteIfMatch(dctx, r.store, certKey(name), ie.etag)
+ deleted, reason, err := domains.ReconcileImport(dctx, r.store, host, now, certSweepGrace)
cancel()
if err != nil {
- // Unsupported, a CAS refusal (rewritten since the mirror read it),
- // or a store error: nothing was deleted; the next pass decides
- // again from a fresh view.
- r.logFailure("edge: import reconcile: removing the import for %s left it in place: %v — retried next pass", host, err)
+ // A failed read, a store that cannot delete conditionally, or a
+ // store error: nothing was deleted; the next pass looks again.
+ r.logFailure("edge: import reconcile: the import for %s was left in place: %v — retried next pass", host, err)
+ continue
+ }
+ if !deleted {
+ // The fresh read found it current, or it was replaced or removed
+ // since: the next pass judges whatever is there then.
continue
}
r.mu.Lock()
- r.removed[name] = ie.etag
+ r.removed[name] = imports[name].etag
r.mu.Unlock()
log.Printf("edge: import reconcile: removed the imported certificate for %s: %s", host, reason)
}
}
-// staleImport decides one import: the host it is for, and why it must go
-// ("" = it stays, or this pass cannot decide).
-func staleImport(name string, ie importEntry, claims map[string]domains.Claim, undecided map[string]bool, now time.Time) (host, reason string) {
- host, ok := domains.ImportHost(name)
- if !ok {
- return strings.TrimSuffix(name, domains.ImportSuffix), "the entry's name is not a hostname"
- }
- if ie.cert == nil {
- return host, "the entry has no readable binding"
- }
- b := ie.binding
- if b.Host != host {
- return host, "the binding names another host"
- }
- if leaf := ie.cert.Leaf; leaf != nil && now.Sub(leaf.NotAfter) >= certSweepGrace {
- // Long dead, whatever the claim says: the cert sweep's predicate,
- // applied here because only this delete is conditional.
- return host, "the certificate expired " + leaf.NotAfter.UTC().Format("2006-01-02")
- }
- if undecided[host] {
- // Listed but unreadable: not "gone".
- return host, ""
- }
- c, ok := claims[host]
- switch {
- case !ok:
- return host, "no claim holds the name"
- case b.Matches(c):
- return host, ""
- case c.Pending:
- return host, "the claim is pending"
- case c.Instances || strings.HasPrefix(c.Host, "*."):
- return host, "the claim is not an exact name"
- case c.Account != b.Account:
- return host, "the claim is held by another account"
- case c.App != b.App:
- return host, "the claim is held by another app"
- default:
- return host, "the claim was detached and attached again"
- }
-}
-
func (r *importReconciler) alreadyRemoved(name, etag string) bool {
r.mu.Lock()
defer r.mu.Unlock()
- return r.removed[name] == etag
+ was, ok := r.removed[name]
+ return ok && was == etag
}
// forget drops the removed memo for names the mirror no longer holds.
diff --git a/internal/edge/certimport_test.go b/internal/edge/certimport_test.go
index 53ab98ef1..546b38cae 100644
--- a/internal/edge/certimport_test.go
+++ b/internal/edge/certimport_test.go
@@ -777,16 +777,22 @@ func TestImportReconcileDeletesNothingWhenItCannotDecide(t *testing.T) {
t.Fatal("a failed claims listing was read as no claims")
}
})
- t.Run("claim listed but unreadable", func(t *testing.T) {
- s, m := setup(t)
- r := reconcilerFor(s, m, true)
- r.claims = func(context.Context) ([]domains.Claim, []domains.UnreadClaim, error) {
- return nil, []domains.UnreadClaim{{Host: importHost}}, nil
- }
- r.pass(context.Background())
+ t.Run("claim unreadable", func(t *testing.T) {
+ // The claim exists, held by another account, but every read of its
+ // body fails: in the cached listing (unread, so a candidate) and in
+ // ReconcileImport's fresh read (so the entry stays).
+ s, _ := setup(t)
+ putClaim(t, s, importHost, "zzz", importApp, importCreated, false)
+ fs := &claimGetFailStore{DirStore: s, key: domains.ClaimKey(importHost)}
+ m := newCertMirror(fs, nil)
+ refreshMirror(t, m)
+ reconcilerFor(fs, m, true).pass(context.Background())
if !importPresent(t, s, importHost) {
t.Fatal("an unreadable claim was read as gone")
}
+ if fs.claimReads() < 2 {
+ t.Fatalf("claim reads = %d; want the listing's and ReconcileImport's", fs.claimReads())
+ }
})
t.Run("store without conditional delete", func(t *testing.T) {
s, m := setup(t)
@@ -796,18 +802,106 @@ func TestImportReconcileDeletesNothingWhenItCannotDecide(t *testing.T) {
t.Fatal("a store without DeleteIfMatch deleted an import")
}
})
- t.Run("rewritten since the mirror read it", func(t *testing.T) {
+ t.Run("replaced since the mirror read it", func(t *testing.T) {
+ // The mirror holds an import bound to an earlier acquisition; the
+ // claim has been acquired again and a new import bound to it written
+ // since. The cached view suggests stale; the fresh read keeps it.
s, m := setup(t)
- fresh := goodImport(t, ca, importHost, false)
+ const again = "2026-11-25T11:00:00Z"
+ putClaim(t, s, importHost, importAccount, importApp, again, false)
+ b := importBinding(importHost)
+ b.ClaimCreated = again
+ fresh := importPEM(t, ca, b, importHost, false, time.Now().Add(-time.Hour), time.Now().Add(60*24*time.Hour))
putBucket(t, s, domains.ImportName(importHost), fresh)
reconcilerFor(s, m, true).pass(context.Background())
got, err := certStoreGet(t, s, certKey(domains.ImportName(importHost)))
if err != nil || !bytes.Equal(got, fresh) {
- t.Fatalf("a CAS refusal must leave the rewritten import in place (%v)", err)
+ t.Fatalf("an import bound to the current claim was deleted on the mirror's stale view (%v)", err)
}
})
}
+// claimGetFailStore fails every Get of one claim key and counts them. It
+// embeds the DirStore so the listing (ModLister) and the conditional delete
+// stay available.
+type claimGetFailStore struct {
+ *release.DirStore
+ key string
+ mu sync.Mutex
+ reads int
+}
+
+func (c *claimGetFailStore) Get(ctx context.Context, key string) ([]byte, string, error) {
+ if key == c.key {
+ c.mu.Lock()
+ c.reads++
+ c.mu.Unlock()
+ return nil, "", errors.New("bucket blip")
+ }
+ return c.DirStore.Get(ctx, key)
+}
+
+func (c *claimGetFailStore) claimReads() int {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return c.reads
+}
+
+// importGetCounter counts Gets of import entries: what a ReconcileImport
+// call costs beyond the pass's listing.
+type importGetCounter struct {
+ *release.DirStore
+ mu sync.Mutex
+ gets int
+}
+
+func (c *importGetCounter) Get(ctx context.Context, key string) ([]byte, string, error) {
+ if strings.HasSuffix(key, domains.ImportSuffix) {
+ c.mu.Lock()
+ c.gets++
+ c.mu.Unlock()
+ }
+ return c.DirStore.Get(ctx, key)
+}
+
+func (c *importGetCounter) count() int {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return c.gets
+}
+
+// The cached view is a prefilter: an import that matches its claim costs no
+// ReconcileImport call, pass after pass; a stale one does, and is deleted.
+func TestImportReconcilePrefilterSpendsNoReadsOnCurrentImports(t *testing.T) {
+ e2eQuietLogs(t)
+ ca := newE2ECA(t)
+ ds := release.NewDirStore(t.TempDir())
+ putClaim(t, ds, importHost, importAccount, importApp, importCreated, false)
+ putBucket(t, ds, domains.ImportName(importHost), goodImport(t, ca, importHost, false))
+ s := &importGetCounter{DirStore: ds}
+ m := newCertMirror(s, nil)
+ refreshMirror(t, m)
+ r := reconcilerFor(s, m, true)
+ before := s.count()
+ for range 3 {
+ r.pass(context.Background())
+ }
+ if n := s.count() - before; n != 0 {
+ t.Fatalf("%d import reads over three passes for a current import; want none", n)
+ }
+ if !importPresent(t, ds, importHost) {
+ t.Fatal("a current import was deleted")
+ }
+ putClaim(t, ds, importHost, "zzz", importApp, importCreated, false)
+ r.pass(context.Background())
+ if importPresent(t, ds, importHost) {
+ t.Fatal("an import whose claim moved to another account survived")
+ }
+ if n := s.count() - before; n != 1 {
+ t.Fatalf("%d import reads; want the one ReconcileImport made", n)
+ }
+}
+
// plainStore hides every optional interface of the store it wraps.
type plainStore struct{ release.Store }
@@ -1100,3 +1194,24 @@ func TestImportReconcileReplacedBetweenReadAndDeleteDeletesNothing(t *testing.T)
t.Fatalf("the replacement was deleted with its expired predecessor (%v)", err)
}
}
+
+// A claim the listing named but could not read is not a reason to keep an
+// import forever: ReconcileImport reads it afresh, and deletes when that read
+// shows the binding no longer holds.
+func TestImportReconcileUnreadListingStillGetsAFreshLook(t *testing.T) {
+ e2eQuietLogs(t)
+ ca := newE2ECA(t)
+ s := release.NewDirStore(t.TempDir())
+ putClaim(t, s, importHost, "zzz", importApp, importCreated, false)
+ putBucket(t, s, domains.ImportName(importHost), goodImport(t, ca, importHost, false))
+ m := newCertMirror(s, nil)
+ refreshMirror(t, m)
+ r := reconcilerFor(s, m, true)
+ r.claims = func(context.Context) ([]domains.Claim, []domains.UnreadClaim, error) {
+ return nil, []domains.UnreadClaim{{Host: importHost}}, nil
+ }
+ r.pass(context.Background())
+ if importPresent(t, s, importHost) {
+ t.Fatal("an import whose claim the fresh read shows held by another account survived")
+ }
+}