carlos / platform Public

release, host: admit demo routes only under the lock, before reuse

Fix round 3 of demo disk slice 1. release.WithRouteAdmission asks about
every route before the pass touches it; a refused route is kept out of
recovery and adoption, so nothing of its is staged, restaged, reused or
pointed live. Root's adopt pass admits a demo route only with the adopt
pass lock held on a valid demo box that is not on the ops account. Apps
held by a non-demo row are now held like unread references: backlog,
named in the record, and their stage refused. Bound restated with R as
reserved trees covering the output, the native worst case beside gVisor.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 13328e5a61ff2606dd8063f85e0a89061481298b parent a0349c8
6 files changed, +312 −50
  • internal/host/agent.go +2 −2
  • internal/host/demoretention.go +114 −37
  • internal/host/demoretention_linux_test.go +2 −2
  • internal/host/demoretention_test.go +107 −5
  • internal/release/adopt.go +34 −4
  • internal/release/beforestage_test.go +53 −0
diff --git a/internal/host/agent.go b/internal/host/agent.go
index fe6dbe345..f3555da05 100644
--- a/internal/host/agent.go
+++ b/internal/host/agent.go
@@ -6369,8 +6369,8 @@ func runAdoptUnderRootRelease(args []string, buildClaims ClaimFactory, ensureUni
// pending-output cleanup before a demo app's new output is staged, and
// retention after a pass that adopted a demo version, or on the hourly
// pass. Both only under this pass's lock, held until the pass returns.
- passErr := pr.around(ctx, adoptPassAround(ctx, *configOnly, *restartOnly, *stageOnly, passLockErr, reg, generationPins, func(stage release.PassOption) error {
- return release.Pass(ctx, reg, store, pub, release.OptDir(), restart, append(passOpts, stage)...)
+ passErr := pr.around(ctx, adoptPassAround(ctx, *configOnly, *restartOnly, *stageOnly, passLockErr, productionDemoRetentionFor(reg, generationPins), func(demo ...release.PassOption) error {
+ return release.Pass(ctx, reg, store, pub, release.OptDir(), restart, append(passOpts, demo...)...)
}))
if passErr != nil {
log.Printf("adopt: %v", passErr)
diff --git a/internal/host/demoretention.go b/internal/host/demoretention.go
index 16a0517a6..11b1ce0f2 100644
--- a/internal/host/demoretention.go
+++ b/internal/host/demoretention.go
@@ -6,7 +6,7 @@
// until the demo's purge: up to 61 builds a demo. Retention deletes a demo
// app's outputs that nothing on the box still names, after every adoption of
// a demo version and on the hourly pass, and the pending-output cleanup
-// below keeps at most one output a demo app never adopted.
+// below keeps at most one unpinned output a demo app has not adopted.
//
// The deletion-safety contract:
// - what may be deleted is decided from root's own facts only: the box's
@@ -54,12 +54,35 @@
// would hold, and a cleanup could remove a build an agent pass is
// adopting.
//
+// Admission and holds: a demo route is adopted, reused or restaged only by
+// a pass that holds the lock on a correctly configured demo box
+// (admitRoute, release.WithRouteAdmission, before the pass touches it). An
+// app held by a registry row that is not a valid demo binding, or by
+// unread settings references, keeps every build, counts what it keeps
+// beyond the bound as backlog, is named with the reason in the record, and
+// stages nothing new until the hold clears.
+//
// The bound per demo app is (8 + G + R) x 32 MiB: six registry and
// settings pins (Version, PrevVersion, GoodVersion, the settings pin,
// LastExecuted, the resolved target), the previous link (after a failed
// adoption rolls back it names the failed build, which no row names), one
-// pending output, G generations-file pins and R root generation pins. With
-// G = R = 0 under gVisor that is 256 MiB a demo, 5.0 GiB for 20.
+// pending output, G generations-file pins for the app, and R, the reserved
+// trees on the box that cover one of the app's outputs. A root
+// generation's Tree is the directory of its executable, <ver>/<demo
+// account>, shared by every demo, and demo versions all count from 1, so
+// one native demo's reservation at demo.n keeps every demo's demo.n output:
+// R can be as large as the number of distinct reserved versions on the box.
+// - gVisor demos (no generations): G = R = 0, 256 MiB a demo, 5.0 GiB for
+// 20.
+// - native demos, one reservation each at distinct versions and G = 1:
+// R <= 20 with 20 demos, (8 + 1 + 20) x 32 MiB = 928 MiB a demo, about
+// 18.1 GiB for 20.
+//
+// Assumptions: executable bytes only (manifests, directories and
+// filesystem overhead are extra); the live link names the row's Version,
+// which is not so after a repoint whose registry stamp failed
+// (release.adoptHost): then the live link's build is one more until the
+// next stamp.
package host
import (
@@ -107,7 +130,10 @@ type demoRetentionStatus struct {
}
type demoRetention struct {
- box demo.Config
+ box demo.Config
+ // boxErr: host.env says this is a demo box (CARLOS_DEMO_BOX) but its
+ // demo settings are invalid (demo.BoxConfig).
+ boxErr error
reg routeLister
optDir string
genPath string
@@ -134,7 +160,7 @@ func productionDemoRetention(reg routeLister, rootPins release.PinSource, locked
log.Printf("adopt: demo build retention off: %v", err)
box = demo.Config{}
}
- return &demoRetention{box: box, reg: reg, optDir: release.OptDir(), genPath: release.GenerationsPath(dataDir()),
+ return &demoRetention{box: box, boxErr: err, reg: reg, optDir: release.OptDir(), genPath: release.GenerationsPath(dataDir()),
rootPins: rootPins, locked: locked, now: time.Now, remove: instancefs.RemoveAllBeneath, logf: log.Printf,
record: func() (rootstate.Store, error) { return rootstate.Open(demoRetentionDir, true, true) }}
}
@@ -147,13 +173,14 @@ type demoOutput struct {
demoAppKey
version string
bytes int64
- // pinned: a pin names it or a path above or inside it, it is the
- // version about to be staged, or a registry row runs its app as
- // something other than a valid demo binding.
+ // pinned: a pin names it or a path above or inside it, or it is the
+ // version about to be staged.
pinned bool
- // held: its app's settings references did not read in full
- // (release.IncompleteRefsError), so nothing of that app is removed.
- held bool
+ // held says why nothing of its app is removed and its app stages
+ // nothing new: a registry row runs the app as something other than a
+ // valid demo binding, or its settings references did not read in full
+ // (release.IncompleteRefsError). "" when neither.
+ held string
// inFlight: staged after every version its app's live links name.
inFlight bool
}
@@ -422,11 +449,17 @@ func (r *demoRetention) listOutputs(acct, only string) ([]demoOutput, error) {
return out, nil
}
+// Why an app is held (demoOutput.held).
+const (
+ heldByRow = "a registry row runs it as something other than a valid demo"
+ heldByRefs = "its settings references did not read in full"
+)
+
// selection is one look at the store for the demo account's apps (only,
// when set): every output present, marked. except is the version about to
-// be staged, which is kept. The IncompleteRefsError, when not nil, names
-// the apps held.
-func (r *demoRetention) selection(ctx context.Context, only, except string, strict bool) ([]demoOutput, *release.IncompleteRefsError, error) {
+// be staged, which is kept. hold says why an app is held, "" when it is
+// not, for any app, whether or not it has outputs.
+func (r *demoRetention) selection(ctx context.Context, only, except string, strict bool) (outs []demoOutput, hold func(app string) string, err error) {
if r.onPins != nil {
r.onPins()
}
@@ -458,7 +491,16 @@ func (r *demoRetention) selection(ctx context.Context, only, except string, stri
guarded[rt.App] = true
}
}
- outs, err := r.listOutputs(acct, only)
+ hold = func(app string) string {
+ switch {
+ case guarded[app]:
+ return heldByRow
+ case inc != nil && inc.Covers(acct, app):
+ return heldByRefs
+ }
+ return ""
+ }
+ outs, err = r.listOutputs(acct, only)
if err != nil {
return nil, nil, err
}
@@ -482,11 +524,11 @@ func (r *demoRetention) selection(ctx context.Context, only, except string, stri
}
for i := range outs {
o := &outs[i]
- o.pinned = o.version == except || guarded[o.app] || pinned(pins, r.optDir, *o)
- o.held = inc != nil && inc.Covers(acct, o.app)
+ o.pinned = o.version == except || pinned(pins, r.optDir, *o)
+ o.held = hold(o.app)
o.inFlight = inFlight(o.version, adopted[o.app])
}
- return outs, inc, nil
+ return outs, hold, nil
}
// planRemovals is what retention removes from outs: every output nothing
@@ -505,7 +547,7 @@ func planRemovals(outs []demoOutput) (remove []int, backlog int) {
apps = append(apps, o.app)
}
pending[o.app] = append(pending[o.app], i)
- case o.held:
+ case o.held != "":
backlog++
default:
remove = append(remove, i)
@@ -532,7 +574,7 @@ func planRemovals(outs []demoOutput) (remove []int, backlog int) {
for _, i := range idx {
switch {
case i == newest:
- case outs[i].held:
+ case outs[i].held != "":
backlog++
default:
remove = append(remove, i)
@@ -543,12 +585,12 @@ func planRemovals(outs []demoOutput) (remove []int, backlog int) {
return remove, backlog
}
-// heldApps names the apps of outs held by unread settings references.
+// heldApps says, for each app of outs that is held, which and why.
func heldApps(outs []demoOutput) []string {
var apps []string
for _, o := range outs {
- if o.held && !slices.Contains(apps, o.app) {
- apps = append(apps, o.app)
+ if why := o.app + ": " + o.held; o.held != "" && !slices.Contains(apps, why) {
+ apps = append(apps, why)
}
}
return apps
@@ -624,7 +666,7 @@ func (r *demoRetention) retain(ctx context.Context) demoRetentionResult {
}
}
if held := heldApps(outs); len(held) > 0 {
- failed = append(failed, fmt.Errorf("kept every build of %s: settings references did not read in full", strings.Join(held, ", ")))
+ failed = append(failed, fmt.Errorf("kept every build of %s", strings.Join(held, "; ")))
}
if len(failed) > 0 {
res.err = errors.Join(failed...)
@@ -716,12 +758,12 @@ func (r *demoRetention) beforeStage(ctx context.Context, path string) error {
if !r.locked {
return errors.New("demo build cleanup: the adopt pass lock is not held")
}
- outs, inc, err := r.selection(ctx, k.app, ver, true)
+ outs, hold, err := r.selection(ctx, k.app, ver, true)
if err != nil {
return fmt.Errorf("demo build cleanup: %w", err)
}
- if inc != nil && inc.Covers(k.acct, k.app) {
- return fmt.Errorf("demo build cleanup: %s: %w", k.app, inc)
+ if why := hold(k.app); why != "" {
+ return fmt.Errorf("demo build cleanup: %s: %s, so its build is not staged", k.app, why)
}
for _, o := range outs {
if o.pinned {
@@ -730,7 +772,37 @@ func (r *demoRetention) beforeStage(ctx context.Context, path string) error {
if err := r.removeOutput(o); err != nil {
return fmt.Errorf("demo build cleanup: %s: %w", o.path(r.optDir), err)
}
- r.logf("adopt: demo build cleanup: removed %s@%s, never adopted, before staging %s", o.app, o.version, ver)
+ r.logf("adopt: demo build cleanup: removed %s@%s, which nothing pins, before staging %s", o.app, o.version, ver)
+ }
+ return nil
+}
+
+// admitRoute is the demo admission, asked of every route before the
+// release pass does anything with it (release.WithRouteAdmission), so no
+// pass can stage, restage, reuse or adopt a demo output unless it holds the
+// adopt pass lock on a correctly configured demo box. Without it a pass
+// that could not take the lock could adopt an output a locked pass is
+// removing. A demo route here is a tmp-demo- app of the demo account; on a
+// box whose demo settings do not validate, any tmp-demo- app. Every other
+// route is admitted.
+func (r *demoRetention) admitRoute(rt registry.Route) error {
+ if r == nil || !strings.HasPrefix(rt.App, demo.AppPrefix) {
+ return nil
+ }
+ if r.boxErr != nil {
+ return fmt.Errorf("this box's demo settings in host.env are invalid (%v), so no demo build is adopted", r.boxErr)
+ }
+ if !r.box.BoxMode() || release.RouteAccount(rt) != r.box.Account {
+ return nil
+ }
+ if _, err := r.demoAccount(); err != nil {
+ return err
+ }
+ if !validDemoName(rt.App) {
+ return fmt.Errorf("%q is not a demo app name, so its builds are not adopted", rt.App)
+ }
+ if !r.locked {
+ return errors.New("this pass does not hold the adopt pass lock, so no demo build is adopted; the next pass tries again")
}
return nil
}
@@ -795,14 +867,18 @@ func adoptPassRetention(configOnly, restartOnly, stageOnly bool, lockErr error,
}
// adoptPassAround is runAdoptUnderRootRelease's release pass inside the
-// demo build retention its mode and lock allow (adoptPassRetention).
-func adoptPassAround(ctx context.Context, configOnly, restartOnly, stageOnly bool, lockErr error, reg routeLister, rootPins release.PinSource, pass func(stage release.PassOption) error) func() error {
- r, hourly := adoptPassRetention(configOnly, restartOnly, stageOnly, lockErr, func(locked bool) *demoRetention {
- return productionDemoRetention(reg, rootPins, locked)
- })
+// demo build retention its mode and lock allow (adoptPassRetention). build
+// makes the retention for a lock state (productionDemoRetentionFor).
+func adoptPassAround(ctx context.Context, configOnly, restartOnly, stageOnly bool, lockErr error, build func(locked bool) *demoRetention, pass func(demo ...release.PassOption) error) func() error {
+ r, hourly := adoptPassRetention(configOnly, restartOnly, stageOnly, lockErr, build)
return r.around(ctx, hourly, pass)
}
+// productionDemoRetentionFor is adoptPassAround's build on this box.
+func productionDemoRetentionFor(reg routeLister, rootPins release.PinSource) func(locked bool) *demoRetention {
+ return func(locked bool) *demoRetention { return productionDemoRetention(reg, rootPins, locked) }
+}
+
// afterPass runs retention after a pass that adopted a demo version, and
// on every hourly pass as the backstop. before is demoAdoptions from before
// the pass; nil (unknown) counts as an adoption.
@@ -826,12 +902,13 @@ func (r *demoRetention) afterPass(ctx context.Context, hourly bool, before map[s
}
// around is one release pass as RunAdopt runs it on any box: pass gets the
-// pending-output cleanup as its WithBeforeStage option, and retention runs
-// after it when it adopted a demo version, or on the hourly pass.
-func (r *demoRetention) around(ctx context.Context, hourly bool, pass func(stage release.PassOption) error) func() error {
+// demo admission (WithRouteAdmission, admitRoute) and the pending-output
+// cleanup (WithBeforeStage) as its options, and retention runs after it
+// when it adopted a demo version, or on the hourly pass.
+func (r *demoRetention) around(ctx context.Context, hourly bool, pass func(demo ...release.PassOption) error) func() error {
return func() error {
before := r.demoAdoptions()
- err := pass(release.WithBeforeStage(r.beforeStage))
+ err := pass(release.WithRouteAdmission(r.admitRoute), release.WithBeforeStage(r.beforeStage))
r.afterPass(ctx, hourly, before)
return err
}
diff --git a/internal/host/demoretention_linux_test.go b/internal/host/demoretention_linux_test.go
index 93750f074..c27a4fb45 100644
--- a/internal/host/demoretention_linux_test.go
+++ b/internal/host/demoretention_linux_test.go
@@ -135,8 +135,8 @@ func TestDemoRetentionConcurrentAdoptionIsSerialized(t *testing.T) {
defer unlock2()
second := b.retention(true)
ctx := context.Background()
- if err := second.around(ctx, false, func(stage release.PassOption) error {
- return release.Pass(ctx, b.reg, b.store, b.pub, b.opt, nil, release.BakeDir(b.data), stage)
+ if err := second.around(ctx, false, func(demo ...release.PassOption) error {
+ return release.Pass(ctx, b.reg, b.store, b.pub, b.opt, nil, append([]release.PassOption{release.BakeDir(b.data)}, demo...)...)
})(); err != nil {
t.Error(err)
}
diff --git a/internal/host/demoretention_test.go b/internal/host/demoretention_test.go
index 1f92abfc9..05ef13297 100644
--- a/internal/host/demoretention_test.go
+++ b/internal/host/demoretention_test.go
@@ -112,8 +112,8 @@ func (b *retentionBox) ship(acct, app, ver string) {
func (b *retentionBox) pass(r *demoRetention, hourly bool) {
b.t.Helper()
ctx := context.Background()
- err := r.around(ctx, hourly, func(stage release.PassOption) error {
- return release.Pass(ctx, b.reg, b.store, b.pub, b.opt, b.restart, release.BakeDir(b.data), stage)
+ err := r.around(ctx, hourly, func(demo ...release.PassOption) error {
+ return release.Pass(ctx, b.reg, b.store, b.pub, b.opt, b.restart, append([]release.PassOption{release.BakeDir(b.data)}, demo...)...)
})()
if err != nil {
b.t.Fatalf("pass: %v", err)
@@ -675,8 +675,27 @@ func TestDemoRetentionRegistryRowsOnlyAddProtection(t *testing.T) {
if c.kept && len(got) != 4 {
t.Fatalf("a registry row made builds eligible: store %v (%+v)", got, res)
}
- if !c.kept && !sameSet(got, []string{demoVer(3), demoVer(4)}) {
- t.Fatalf("control: store %v, want v3 and v4 (%+v)", got, res)
+ if !c.kept {
+ if !sameSet(got, []string{demoVer(3), demoVer(4)}) {
+ t.Fatalf("control: store %v, want v3 and v4 (%+v)", got, res)
+ }
+ return
+ }
+ // The app is held, not pinned: what it keeps beyond the bound is
+ // backlog, the record names it and why, and its next stages are
+ // refused, so successive updates do not grow the store.
+ if s := b.status(); s.Backlog != 2 || !strings.Contains(s.Reason, demoAppName("red")) || s.BacklogSince.IsZero() {
+ t.Fatalf("held app's record %+v", s)
+ }
+ for i := 5; i <= 7; i++ {
+ b.ship("shop", demoAppName("red"), demoVer(i))
+ b.pass(b.retention(true), false)
+ if v, got := b.route("red").Version, b.outputs("shop", demoAppName("red")); v != demoVer(4) || len(got) != 4 {
+ t.Fatalf("update %d of a held app: route %q, store %v (want the stage refused)", i, v, got)
+ }
+ if s := b.status(); s.Backlog != 2 || !strings.Contains(s.Reason, demoAppName("red")) {
+ t.Fatalf("update %d: record %+v", i, s)
+ }
}
})
}
@@ -731,11 +750,16 @@ func TestDemoRetentionRefusesADemoAccountOnTheOpsLayout(t *testing.T) {
t.Fatal(err)
}
b.ship(release.OpsAccount, app, demoVer(1))
+ // Already staged with the release's bytes: the pass would reuse it, not
+ // write it, so the refusal must come before reuse.
+ if err := release.StageArtifact(b.opt, release.OpsAccount, demoVer(1), app, []byte(app+" build "+demoVer(1))); err != nil {
+ t.Fatal(err)
+ }
r := b.retention(true)
r.box.Account = release.OpsAccount
b.pass(r, false)
if v := b.route("red").Version; v != "" {
- t.Fatalf("a demo build was staged on the ops layout: route at %q", v)
+ t.Fatalf("a demo build was adopted on the ops layout: route at %q", v)
}
err := r.beforeStage(context.Background(), release.VersionPath(b.opt, release.OpsAccount, demoVer(2), app))
if err == nil || !strings.Contains(err.Error(), "ops account") {
@@ -878,3 +902,81 @@ func TestAdoptPassRetentionFollowsTheModeAndTheLock(t *testing.T) {
t.Fatal("a stage without the lock was not refused")
}
}
+
+// The production orchestration (adoptPassAround around a real
+// release.Pass) gates every demo adoption on the adopt pass lock before
+// release processing reaches the output: the next build already staged
+// with the release's bytes (reuse) or with other bytes (restage) is not
+// adopted without the lock, and is with it. A -config-only or
+// -restart-only composition builds no retention, so its pass is the plain
+// release pass, as before (and production returns before it anyway).
+func TestAdoptPassAroundGatesDemoAdoptionOnTheLock(t *testing.T) {
+ app := demoAppName("red")
+ lockErr := errors.New("lock file unavailable")
+ release2 := []byte(app + " build " + demoVer(2))
+ for _, c := range []struct {
+ name string
+ staged []byte
+ configOnly, restartOnly bool
+ lockErr error
+ want string
+ }{
+ {name: "reuse without the lock", staged: release2, lockErr: lockErr, want: demoVer(1)},
+ {name: "restage without the lock", staged: []byte("other bytes"), lockErr: lockErr, want: demoVer(1)},
+ {name: "reuse with the lock", staged: release2, want: demoVer(2)},
+ {name: "restage with the lock", staged: []byte("other bytes"), want: demoVer(2)},
+ {name: "config-only", staged: release2, configOnly: true, lockErr: lockErr, want: demoVer(2)},
+ {name: "restart-only", staged: release2, restartOnly: true, lockErr: lockErr, want: demoVer(2)},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ b := newRetentionBox(t, "")
+ b.addDemo("red", true)
+ b.ship("shop", app, demoVer(1))
+ b.pass(b.retention(true), true)
+ b.ship("shop", app, demoVer(2))
+ if err := release.StageArtifact(b.opt, "shop", demoVer(2), app, c.staged); err != nil {
+ t.Fatal(err)
+ }
+ ctx := context.Background()
+ err := adoptPassAround(ctx, c.configOnly, c.restartOnly, false, c.lockErr, b.retention, func(demo ...release.PassOption) error {
+ return release.Pass(ctx, b.reg, b.store, b.pub, b.opt, b.restart, append([]release.PassOption{release.BakeDir(b.data)}, demo...)...)
+ })()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if v := b.route("red").Version; v != c.want {
+ t.Fatalf("route at %q, want %q", v, c.want)
+ }
+ if got := b.outputs("shop", app); !slices.Contains(got, demoVer(2)) {
+ t.Fatalf("v2's output is gone: %v", got)
+ }
+ })
+ }
+}
+
+// A box whose host.env says it is a demo box but whose demo settings do not
+// validate adopts no demo build at all: it cannot tell which are its demo
+// outputs, so it neither bounds nor protects them.
+func TestAdoptPassAroundRefusesDemoBuildsOnAMisconfiguredDemoBox(t *testing.T) {
+ b := newRetentionBox(t, "")
+ app := demoAppName("red")
+ b.addDemo("red", true)
+ b.ship("shop", app, demoVer(1))
+ if err := release.StageArtifact(b.opt, "shop", demoVer(1), app, []byte(app+" build "+demoVer(1))); err != nil {
+ t.Fatal(err)
+ }
+ ctx := context.Background()
+ build := func(locked bool) *demoRetention {
+ r := b.retention(locked)
+ r.box, r.boxErr = demo.Config{}, demo.ErrConfig
+ return r
+ }
+ if err := adoptPassAround(ctx, false, false, false, nil, build, func(demo ...release.PassOption) error {
+ return release.Pass(ctx, b.reg, b.store, b.pub, b.opt, b.restart, append([]release.PassOption{release.BakeDir(b.data)}, demo...)...)
+ })(); err != nil {
+ t.Fatal(err)
+ }
+ if v := b.route("red").Version; v != "" {
+ t.Fatalf("a misconfigured demo box adopted a demo build: %q", v)
+ }
+}
diff --git a/internal/release/adopt.go b/internal/release/adopt.go
index de02ebb80..3fb3eda9c 100644
--- a/internal/release/adopt.go
+++ b/internal/release/adopt.go
@@ -116,8 +116,11 @@ type passConfig struct {
// pins is WithPinSource's provider: staged copies that something beyond
// the registry rows, the live links and the generations file still
// refers to. nil adds nothing.
- pins PinSource
- beforeStage func(context.Context, string) error
+ pins PinSource
+ beforeStage func(context.Context, string) error
+ // admitRoute, set by WithRouteAdmission: false keeps a route out of the
+ // whole pass. nil admits every route.
+ admitRoute func(registry.Route) bool
generationPins PinSource
// stage is what ensureStagedChecked needs to judge a staged copy it is
@@ -501,6 +504,33 @@ func WithBeforeStage(fn func(ctx context.Context, path string) error) PassOption
return func(c *passConfig) { c.beforeStage = fn }
}
+// WithRouteAdmission asks fn about every route before the pass does
+// anything with it: an error keeps the route out of the whole pass, as if
+// it were not adoptable, so nothing of its is staged, restaged, reused,
+// recovered or pointed live this pass; the error is printed once per host.
+// Root's adopt pass sets it for the demo disk bound
+// (internal/host/demoretention.go): a demo app is adopted only by a pass
+// that holds the adopt pass lock on a correctly configured demo box, so no
+// pass can adopt an output another is removing.
+func WithRouteAdmission(fn func(registry.Route) error) PassOption {
+ refused := map[string]bool{}
+ return func(c *passConfig) {
+ c.admitRoute = func(rt registry.Route) bool {
+ err := fn(rt)
+ if err != nil && !refused[rt.Host] {
+ refused[rt.Host] = true
+ fmt.Printf(" %s: not adopted this pass: %v\n", rt.Host, err)
+ }
+ return err == nil
+ }
+ }
+}
+
+// routeAdmitted is WithRouteAdmission's answer for rt; true without one.
+func (cfg *passConfig) routeAdmitted(rt registry.Route) bool {
+ return cfg.admitRoute == nil || cfg.admitRoute(rt)
+}
+
// stageWriteCheck is the pass's check before a staged write to path: the
// generation pins (generationWriteCheck), then WithBeforeStage's callback
// for a path that holds nothing yet.
@@ -611,7 +641,7 @@ func PassFilter(ctx context.Context, reg *registry.Registry, s Store, pub ed2551
var failures []error
remaining := false
for _, rt := range routes {
- if extra != nil && !extra(rt) {
+ if extra != nil && !extra(rt) || !cfg.routeAdmitted(rt) {
continue
}
done, err := cfg.recovery(ctx, rt)
@@ -672,7 +702,7 @@ func PassFilter(ctx context.Context, reg *registry.Registry, s Store, pub ed2551
}
}
adopts := func(rt registry.Route) bool {
- return (Adoptable(rt) || SidecarAdoptable(rt)) && (extra == nil || extra(rt))
+ return (Adoptable(rt) || SidecarAdoptable(rt)) && (extra == nil || extra(rt)) && cfg.routeAdmitted(rt)
}
// WithPointerGate: a skipped group leaves the pass before anything is
// selected for it.
diff --git a/internal/release/beforestage_test.go b/internal/release/beforestage_test.go
index 0188c2c10..b4759bad3 100644
--- a/internal/release/beforestage_test.go
+++ b/internal/release/beforestage_test.go
@@ -5,6 +5,8 @@ import (
"errors"
"os"
"testing"
+
+ "github.com/carlosframework/platform/internal/registry"
)
// The demo disk bound's pending-output cleanup (plan slice 1, step 4): the
@@ -68,3 +70,54 @@ func TestPassBeforeStageRunsOnlyForANewOutputAndRefusesTheStage(t *testing.T) {
t.Fatalf("calls = %v, want none for a version already staged", calls)
}
}
+
+// WithRouteAdmission keeps a refused route out of the whole pass: a build
+// already staged with the release's bytes is not reused and the route is
+// not pointed live, while an admitted route in the same pass adopts. Once
+// admitted, the refused route adopts its staged build.
+func TestPassRouteAdmissionKeepsARefusedRouteOutOfThePass(t *testing.T) {
+ s := NewDirStore(t.TempDir())
+ ctx := context.Background()
+ priv, pub := testKeys(t)
+ opt := t.TempDir()
+ reg := testRegistry(t)
+ addRoute(t, reg, "shop.example.com", "shop", "canary/x", true)
+ addRoute(t, reg, "blog.example.com", "blog", "canary/x", true)
+ for _, app := range []string{"shop", "blog"} {
+ shipRelease(t, s, "bab", app, "v1", adoptPlatform, []byte(app+" one"))
+ if err := Promote(ctx, s, priv, "bab", app, "canary/x", "v1"); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := StageArtifact(opt, "bab", "v1", "shop", []byte("shop one")); err != nil {
+ t.Fatal(err)
+ }
+ asked := 0
+ admit := func(refuse bool) PassOption {
+ return WithRouteAdmission(func(rt registry.Route) error {
+ asked++
+ if refuse && rt.App == "shop" {
+ return errors.New("not admitted")
+ }
+ return nil
+ })
+ }
+ if err := Pass(ctx, reg, s, pub, opt, nil, admit(true)); err != nil {
+ t.Fatalf("Pass: %v", err)
+ }
+ if rt, _ := reg.Get("shop.example.com"); rt == nil || rt.Version != "" {
+ t.Fatalf("a refused route adopted its staged build: %+v", rt)
+ }
+ if rt, _ := reg.Get("blog.example.com"); rt == nil || rt.Version != "v1" {
+ t.Fatalf("an admitted route did not adopt: %+v", rt)
+ }
+ if v, _ := LiveVersion(opt, "shop.example.com"); v != "" || asked == 0 {
+ t.Fatalf("refused route's live link %q (asked %d)", v, asked)
+ }
+ if err := Pass(ctx, reg, s, pub, opt, nil, admit(false)); err != nil {
+ t.Fatalf("Pass: %v", err)
+ }
+ if rt, _ := reg.Get("shop.example.com"); rt == nil || rt.Version != "v1" {
+ t.Fatalf("admitted, the route did not adopt: %+v", rt)
+ }
+}