| index 11b1ce0f2..e33b540cf 100644 |
| --- a/internal/host/demoretention.go |
| +++ b/internal/host/demoretention.go |
| @@ -58,9 +58,15 @@ |
| // 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. |
| +// unread settings references, keeps every build, is named with the reason |
| +// in the record and in box status, and stages nothing new until the hold |
| +// clears. |
| +// |
| +// The backlog is an accounting invariant (demoBacklog), not a by-product of |
| +// what retention may delete: for each app, the larger of the outputs it |
| +// keeps above its bound (8 plus those a generation reserves), however they |
| +// are pinned or held, and the outputs retention should have removed and |
| +// did not. |
| // |
| // The bound per demo app is (8 + G + R) x 32 MiB: six registry and |
| // settings pins (Version, PrevVersion, GoodVersion, the settings pin, |
| @@ -127,6 +133,9 @@ type demoRetentionStatus struct { |
| Backlog int `json:"backlog"` |
| BacklogSince time.Time `json:"backlog_since,omitzero"` |
| Reason string `json:"reason,omitempty"` |
| + // Held is every held app as "<app>: <why>": it keeps every build and |
| + // its updates are refused until the hold is cleared. |
| + Held []string `json:"held,omitempty"` |
| } |
| |
| type demoRetention struct { |
| @@ -183,8 +192,16 @@ type demoOutput struct { |
| held string |
| // inFlight: staged after every version its app's live links name. |
| inFlight bool |
| + // reserved: a generations-file or root generation pin covers it, so it |
| + // counts toward its app's G + R rather than against the bound. |
| + reserved bool |
| } |
| |
| +// demoOutputBound is the outputs a demo app may keep besides those a |
| +// generation reserves: six registry and settings pins, the previous link |
| +// and one pending output (the 8 of 8 + G + R). |
| +const demoOutputBound = 8 |
| + |
| func (o demoOutput) path(optDir string) string { |
| return release.VersionPath(optDir, o.acct, o.version, o.app) |
| } |
| @@ -283,8 +300,7 @@ func inFlight(v string, adopted []string) bool { |
| // pins are complete for every app it does not cover and which is returned |
| // for the caller to hold those apps. Otherwise (the backlog estimate) a |
| // failed source adds nothing. |
| -func (r *demoRetention) pinSet(ctx context.Context, routes []registry.Route, strict bool) ([]string, *release.IncompleteRefsError, error) { |
| - var out []string |
| +func (r *demoRetention) pinSet(ctx context.Context, routes []registry.Route, strict bool) (out, reserved []string, _ *release.IncompleteRefsError, _ error) { |
| add := func(name string, paths release.StagedPaths, err error) error { |
| if err != nil { |
| if strict { |
| @@ -297,6 +313,15 @@ func (r *demoRetention) pinSet(ctx context.Context, routes []registry.Route, str |
| } |
| return nil |
| } |
| + // The generation sources' pins are also kept apart: they are G and R. |
| + addReserved := func(name string, paths release.StagedPaths, err error) error { |
| + if err == nil { |
| + for p := range paths { |
| + reserved = append(reserved, filepath.Clean(p)) |
| + } |
| + } |
| + return add(name, paths, err) |
| + } |
| box, err := release.BoxPins(r.optDir, routes) |
| var inc *release.IncompleteRefsError |
| if errors.As(err, &inc) && box != nil { |
| @@ -305,7 +330,7 @@ func (r *demoRetention) pinSet(ctx context.Context, routes []registry.Route, str |
| inc = nil |
| } |
| if err := add("live links and registry rows", box, err); err != nil { |
| - return nil, nil, err |
| + return nil, nil, nil, err |
| } |
| if !strict { |
| // BoxPins answers nothing when one live link does not read; the |
| @@ -321,21 +346,21 @@ func (r *demoRetention) pinSet(ctx context.Context, routes []registry.Route, str |
| } |
| prev, err := linkTargets(filepath.Join(r.optDir, "previous")) |
| if err := add("previous links", prev, err); err != nil { |
| - return nil, nil, err |
| + return nil, nil, nil, err |
| } |
| gen, err := release.GenerationPins(r.genPath, func() ([]registry.Route, error) { return routes, nil }).StagedPins(ctx, r.optDir) |
| - if err := add("generations file", gen, err); err != nil { |
| - return nil, nil, err |
| + if err := addReserved("generations file", gen, err); err != nil { |
| + return nil, nil, nil, err |
| } |
| var root release.StagedPaths |
| err = os.ErrPermission |
| if r.rootPins != nil { |
| root, err = r.rootPins.StagedPins(ctx, r.optDir) |
| } |
| - if err := add("root generation pins", root, err); err != nil { |
| - return nil, nil, err |
| + if err := addReserved("root generation pins", root, err); err != nil { |
| + return nil, nil, nil, err |
| } |
| - return out, inc, nil |
| + return out, reserved, inc, nil |
| } |
| |
| // linkTargets is every link's target in dir (<opt>/previous or <opt>/live, |
| @@ -455,33 +480,43 @@ const ( |
| heldByRefs = "its settings references did not read in full" |
| ) |
| |
| +// demoSelection is one look at the store for the demo account's apps. |
| +type demoSelection struct { |
| + // outs is every output present, marked. |
| + outs []demoOutput |
| + // hold says why an app is held, "" when it is not, for any app, |
| + // whether or not it has outputs. |
| + hold func(app string) string |
| + // held is every held app of the demo account, outputs or not, as |
| + // "<app>: <why>". |
| + held []string |
| +} |
| + |
| // 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. 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) { |
| +// when set). except is the version about to be staged, which is kept. |
| +func (r *demoRetention) selection(ctx context.Context, only, except string, strict bool) (sel demoSelection, err error) { |
| if r.onPins != nil { |
| r.onPins() |
| } |
| acct, err := r.demoAccount() |
| if err != nil { |
| - return nil, nil, err |
| + return sel, err |
| } |
| if r.reg == nil { |
| - return nil, nil, os.ErrPermission |
| + return sel, os.ErrPermission |
| } |
| routes, err := r.reg.List() |
| if err != nil { |
| - return nil, nil, fmt.Errorf("list routes: %w", err) |
| + return sel, fmt.Errorf("list routes: %w", err) |
| } |
| - pins, inc, err := r.pinSet(ctx, routes, strict) |
| + pins, reserved, inc, err := r.pinSet(ctx, routes, strict) |
| if err != nil { |
| - return nil, nil, err |
| + return sel, err |
| } |
| live, liveErr := linkTargets(filepath.Join(r.optDir, "live")) |
| if liveErr != nil { |
| if strict { |
| - return nil, nil, fmt.Errorf("live links: %w", liveErr) |
| + return sel, fmt.Errorf("live links: %w", liveErr) |
| } |
| live = release.StagedPaths{} |
| } |
| @@ -491,7 +526,7 @@ func (r *demoRetention) selection(ctx context.Context, only, except string, stri |
| guarded[rt.App] = true |
| } |
| } |
| - hold = func(app string) string { |
| + hold := func(app string) string { |
| switch { |
| case guarded[app]: |
| return heldByRow |
| @@ -500,9 +535,9 @@ func (r *demoRetention) selection(ctx context.Context, only, except string, stri |
| } |
| return "" |
| } |
| - outs, err = r.listOutputs(acct, only) |
| + outs, err := r.listOutputs(acct, only) |
| if err != nil { |
| - return nil, nil, err |
| + return sel, err |
| } |
| adopted := map[string][]string{} |
| for _, o := range outs { |
| @@ -527,16 +562,33 @@ func (r *demoRetention) selection(ctx context.Context, only, except string, stri |
| o.pinned = o.version == except || pinned(pins, r.optDir, *o) |
| o.held = hold(o.app) |
| o.inFlight = inFlight(o.version, adopted[o.app]) |
| + o.reserved = pinned(reserved, r.optDir, *o) |
| } |
| - return outs, hold, nil |
| + var held []string |
| + note := func(app string) { |
| + if why := hold(app); why != "" && !slices.Contains(held, app+": "+why) { |
| + held = append(held, app+": "+why) |
| + } |
| + } |
| + for _, rt := range routes { |
| + if release.RouteAccount(rt) == acct && validDemoName(rt.App) && (only == "" || rt.App == only) { |
| + note(rt.App) |
| + } |
| + } |
| + for _, o := range outs { |
| + note(o.app) |
| + } |
| + sort.Strings(held) |
| + return demoSelection{outs: outs, hold: hold, held: held}, nil |
| } |
| |
| // planRemovals is what retention removes from outs: every output nothing |
| // pins that is not its app's newest pending output, and nothing of a held |
| -// app. backlog counts what it keeps beyond the bound: a held app's |
| -// outputs it would otherwise remove, and pending outputs beyond one where |
| -// the names cannot be ordered. |
| -func planRemovals(outs []demoOutput) (remove []int, backlog int) { |
| +// app. unremoved counts, per app, what it should remove and keeps: a held |
| +// app's outputs it would otherwise remove, and pending outputs beyond one |
| +// where the names cannot be ordered. |
| +func planRemovals(outs []demoOutput) (remove []int, unremoved map[string]int) { |
| + unremoved = map[string]int{} |
| pending := map[string][]int{} |
| var apps []string |
| for i, o := range outs { |
| @@ -548,7 +600,7 @@ func planRemovals(outs []demoOutput) (remove []int, backlog int) { |
| } |
| pending[o.app] = append(pending[o.app], i) |
| case o.held != "": |
| - backlog++ |
| + unremoved[o.app]++ |
| default: |
| remove = append(remove, i) |
| } |
| @@ -568,32 +620,46 @@ func planRemovals(outs []demoOutput) (remove []int, backlog int) { |
| } |
| } |
| if !ordered { |
| - backlog += len(idx) - 1 |
| + unremoved[app] += len(idx) - 1 |
| continue |
| } |
| for _, i := range idx { |
| switch { |
| case i == newest: |
| case outs[i].held != "": |
| - backlog++ |
| + unremoved[app]++ |
| default: |
| remove = append(remove, i) |
| } |
| } |
| } |
| sort.Ints(remove) |
| - return remove, backlog |
| + return remove, unremoved |
| } |
| |
| -// 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 why := o.app + ": " + o.held; o.held != "" && !slices.Contains(apps, why) { |
| - apps = append(apps, why) |
| +// demoBacklog is the accounting invariant behind the backlog: for each app, |
| +// the larger of the outputs it keeps above its bound (demoOutputBound plus |
| +// those a generation reserves), however they are pinned or held, and the |
| +// outputs retention should have removed and did not. So an app above its |
| +// bound always shows, whatever kept its outputs. |
| +func demoBacklog(kept []demoOutput, unremoved map[string]int) int { |
| + count, reserved := map[string]int{}, map[string]int{} |
| + for _, o := range kept { |
| + count[o.app]++ |
| + if o.reserved { |
| + reserved[o.app]++ |
| + } |
| + } |
| + for app := range unremoved { |
| + if _, ok := count[app]; !ok { |
| + count[app] = 0 |
| } |
| } |
| - return apps |
| + total := 0 |
| + for app, n := range count { |
| + total += max(n-demoOutputBound-reserved[app], unremoved[app], 0) |
| + } |
| + return total |
| } |
| |
| // removeOutput deletes one output beneath its version directory, the |
| @@ -617,56 +683,64 @@ type demoRetentionResult struct { |
| outputs int |
| outputBytes int64 |
| backlog int |
| - err error |
| + // held is every held app, as "<app>: <why>" (demoSelection.held). |
| + held []string |
| + err error |
| } |
| |
| // retain is one retention run, deleting only under the lock and on a |
| // complete pin set. |
| func (r *demoRetention) retain(ctx context.Context) demoRetentionResult { |
| var res demoRetentionResult |
| - var outs []demoOutput |
| + var sel demoSelection |
| if !r.locked { |
| res.err = errors.New("the adopt pass lock is not held") |
| } else { |
| - outs, _, res.err = r.selection(ctx, "", "", true) |
| + sel, res.err = r.selection(ctx, "", "", true) |
| } |
| if res.err != nil { |
| // Deletes nothing. The backlog is estimated on what does read. |
| - est, _, err := r.selection(ctx, "", "", false) |
| + est, err := r.selection(ctx, "", "", false) |
| if err != nil { |
| res.backlog = -1 |
| return res |
| } |
| - remove, backlog := planRemovals(est) |
| - res.backlog = len(remove) + backlog |
| - for _, o := range est { |
| + remove, unremoved := planRemovals(est.outs) |
| + for _, i := range remove { |
| + unremoved[est.outs[i].app]++ |
| + } |
| + res.backlog, res.held = demoBacklog(est.outs, unremoved), est.held |
| + for _, o := range est.outs { |
| res.outputs++ |
| res.outputBytes += o.bytes |
| } |
| return res |
| } |
| - remove, backlog := planRemovals(outs) |
| - res.backlog = backlog |
| + outs := sel.outs |
| + remove, unremoved := planRemovals(outs) |
| gone := map[int]bool{} |
| var failed []error |
| for _, i := range remove { |
| o := outs[i] |
| if err := r.removeOutput(o); err != nil { |
| failed = append(failed, fmt.Errorf("%s: %w", o.path(r.optDir), err)) |
| - res.backlog++ |
| + unremoved[o.app]++ |
| continue |
| } |
| gone[i] = true |
| res.removed = append(res.removed, o.app+"@"+o.version) |
| } |
| + var kept []demoOutput |
| for i, o := range outs { |
| if !gone[i] { |
| + kept = append(kept, o) |
| res.outputs++ |
| res.outputBytes += o.bytes |
| } |
| } |
| - if held := heldApps(outs); len(held) > 0 { |
| - failed = append(failed, fmt.Errorf("kept every build of %s", strings.Join(held, "; "))) |
| + res.backlog, res.held = demoBacklog(kept, unremoved), sel.held |
| + if len(sel.held) > 0 { |
| + failed = append(failed, fmt.Errorf("kept every build of %s", strings.Join(sel.held, "; "))) |
| } |
| if len(failed) > 0 { |
| res.err = errors.Join(failed...) |
| @@ -707,10 +781,10 @@ func (r *demoRetention) saveStatus(res demoRetentionResult) { |
| if raw, err := s.Read(demoRetentionRecord); err == nil { |
| _ = json.Unmarshal(raw, &prev) |
| } |
| - next := demoRetentionStatus{Schema: 1, At: now, Outputs: res.outputs, OutputBytes: res.outputBytes, Backlog: res.backlog} |
| + next := demoRetentionStatus{Schema: 1, At: now, Outputs: res.outputs, OutputBytes: res.outputBytes, Backlog: res.backlog, Held: res.held} |
| if res.backlog < 0 { |
| // Nothing read at all: keep the last known numbers. |
| - next.Outputs, next.OutputBytes, next.Backlog = prev.Outputs, prev.OutputBytes, max(prev.Backlog, 1) |
| + next.Outputs, next.OutputBytes, next.Backlog, next.Held = prev.Outputs, prev.OutputBytes, max(prev.Backlog, 1), prev.Held |
| } |
| if res.err != nil { |
| next.Reason = res.err.Error() |
| @@ -758,11 +832,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, hold, err := r.selection(ctx, k.app, ver, true) |
| + sel, err := r.selection(ctx, k.app, ver, true) |
| if err != nil { |
| return fmt.Errorf("demo build cleanup: %w", err) |
| } |
| - if why := hold(k.app); why != "" { |
| + outs := sel.outs |
| + if why := sel.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 { |
| @@ -790,7 +865,7 @@ func (r *demoRetention) admitRoute(rt registry.Route) error { |
| 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) |
| + return fmt.Errorf("host.env sets CARLOS_DEMO_BOX, but the demo settings (CARLOS_DEMO_BOX, CARLOS_DEMO_ACCOUNT, CARLOS_DEMO_FLEET, CARLOS_DEMO_DOMAIN) are invalid (%v), so no demo build is adopted", r.boxErr) |
| } |
| if !r.box.BoxMode() || release.RouteAccount(rt) != r.box.Account { |
| return nil |