| 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. |