From f288f3c4238799f890ba403c60e1751a844bdf1f Mon Sep 17 00:00:00 2001
From: Ilker Manap
Date: Wed, 30 Sep 2026 22:40:45 +0200
Subject: [PATCH] Set capacity quotas, class, owner and reservation from the
admin pages
The quotas page gains a capacity section over the same API methods the CLI
uses; a model's card sets its class, owner, memory and cores, applying only
what changed. Headroom counts the replicas a model could get by preempting
what its class outranks, and the card and ctl model target use it.
Co-Authored-By: Claude Opus 5.5 (1M context)
---
CHANGELOG.md | 22 ++++
internal/cli/quota.go | 4 +
internal/control/adminapi/api.go | 7 +-
internal/control/adminapi/quotas.go | 84 +++++++++---
internal/control/adminapi/target.go | 10 +-
internal/control/reconciler/headroom.go | 34 ++++-
internal/control/reconciler/preempt_test.go | 19 +++
internal/router/handlers.go | 5 +
internal/web/account.go | 2 +
internal/web/admin_capacity.go | 138 ++++++++++++++++++++
internal/web/admin_capacity_test.go | 88 +++++++++++++
internal/web/admin_mirror.go | 13 +-
internal/web/admin_quotas.go | 37 +++++-
internal/web/admin_serve.go | 5 +
internal/web/admin_serve_test.go | 19 +++
internal/web/mirror_detail.go | 85 +++++++++++-
internal/web/mirror_detail_test.go | 94 ++++++++++++-
internal/web/static/base.css | 3 +
internal/web/templates/admin_mirror.html | 30 ++++-
internal/web/templates/admin_quotas.html | 54 ++++++++
20 files changed, 709 insertions(+), 44 deletions(-)
create mode 100644 internal/web/admin_capacity.go
create mode 100644 internal/web/admin_capacity_test.go
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c1749fe..40b8222 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,28 @@ is only released once its **exit criterion** is met, not when its code lands.
Until v1.0.0 the minor version is the breaking one: configuration formats and Go
APIs change between minors without a deprecation period.
+## Unreleased
+
+Changes since v0.10.5, published as release candidates (v0.10.6-rc.N) until
+there are enough for v0.10.6.
+
+### Capacity from the admin pages
+
+What v0.10.5 made settable from the command line is now on the pages too:
+
+- **Quotas** has a Capacity section: every scope's guaranteed and most GPUs,
+ cores and memory beside what it holds, with a form to set, change or remove
+ one. It goes through the same checks as `llmgw ctl quota`.
+- **A model's card** on the mirror page sets its class, its owner (when there
+ are quotas to count against), and the memory and cores a replica reserves,
+ beside its replica counts. Only what changed is applied, and each change is
+ said.
+- **Room counts what a class may take.** The card, `llmgw ctl model target` and
+ the headroom API count the replicas a model could get by stopping those its
+ class outranks, separately from free room, and name whose they would be. A
+ critical model's card no longer refuses a replica that preemption would make
+ room for.
+
## v0.10.5 — 2026-09-30
Published first as release candidates v0.10.5-rc.1 to rc.14; rc.5 failed its
diff --git a/internal/cli/quota.go b/internal/cli/quota.go
index 2d376dd..1fcd4a7 100644
--- a/internal/cli/quota.go
+++ b/internal/cli/quota.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"strconv"
+ "strings"
"github.com/spf13/cobra"
@@ -192,6 +193,9 @@ func newTargetCmd(client func() *ctlClient, asJSON *bool) *cobra.Command {
switch {
case v.More == 0:
fmt.Fprintln(out, "It already has them.")
+ case v.Fits && len(v.Preempts) > 0:
+ fmt.Fprintf(out, "%d more; there is room once replicas of %s are stopped, which its class may do.\n",
+ v.More, strings.Join(v.Preempts, ", "))
case v.Fits:
fmt.Fprintf(out, "%d more, and there is room for them.\n", v.More)
default:
diff --git a/internal/control/adminapi/api.go b/internal/control/adminapi/api.go
index ed7bd05..1a89f1c 100644
--- a/internal/control/adminapi/api.go
+++ b/internal/control/adminapi/api.go
@@ -408,6 +408,10 @@ type HeadroomView struct {
Blocked []string `json:"blocked,omitempty"`
// TokPerSec is what one more replica was measured to serve; 0 unmeasured.
TokPerSec float64 `json:"tok_per_sec,omitempty"`
+ // ByPreempting is how many more would fit by stopping replicas this
+ // model's class may take; Preempts the models those belong to.
+ ByPreempting int `json:"by_preempting,omitempty"`
+ Preempts []string `json:"preempts,omitempty"`
}
// RejectionView is one rejected candidate.
@@ -967,7 +971,8 @@ func (a *API) Headroom(model string, limit int) (HeadroomView, bool) {
return HeadroomView{}, false
}
return HeadroomView{Model: model, Current: h.Current, More: h.More, Nodes: h.Nodes,
- Limited: h.Limited, Blocked: h.Blocked, TokPerSec: h.TokPerSec}, true
+ Limited: h.Limited, Blocked: h.Blocked, TokPerSec: h.TokPerSec,
+ ByPreempting: h.ByPreempting, Preempts: h.Preempts}, true
}
func (a *API) Explain(model string) ExplainView {
diff --git a/internal/control/adminapi/quotas.go b/internal/control/adminapi/quotas.go
index af5bf44..3810bcd 100644
--- a/internal/control/adminapi/quotas.go
+++ b/internal/control/adminapi/quotas.go
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
+ "sort"
"strings"
"time"
@@ -72,38 +73,24 @@ func (a *API) registerQuotas(mux *http.ServeMux) {
writeError(w, http.StatusBadRequest, "the request is not valid JSON: "+err.Error())
return
}
- scope, id, _, err := a.resolveScope(r.Context(), req.Scope)
+ key, err := a.SetQuota(r.Context(), req, actorOf(r))
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
- if !req.Guarantee.Any() && !req.Limit.Any() {
- writeError(w, http.StatusBadRequest, "a quota needs a guarantee or a limit; to remove one, DELETE it")
- return
- }
- q := store.CapacityQuota{Scope: scope, ScopeID: id, Guarantee: req.Guarantee, Limit: req.Limit,
- UpdatedBy: actorOf(r)}
- if err := a.quotas.PutCapacityQuota(r.Context(), q); err != nil {
- writeError(w, http.StatusBadRequest, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"scope": quota.Key(scope, id)})
+ writeJSON(w, http.StatusOK, map[string]string{"scope": key})
})
mux.HandleFunc("DELETE /api/v1/quotas/{scope}", func(w http.ResponseWriter, r *http.Request) {
if a.quotas == nil {
writeError(w, http.StatusNotImplemented, "this control plane has no store")
return
}
- scope, id, _, err := a.resolveScope(r.Context(), r.PathValue("scope"))
+ key, err := a.ClearQuota(r.Context(), r.PathValue("scope"))
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
- if err := a.quotas.DeleteCapacityQuota(r.Context(), scope, id); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{"removed": quota.Key(scope, id)})
+ writeJSON(w, http.StatusOK, map[string]string{"removed": key})
})
mux.HandleFunc("POST /api/v1/models/{name}/owner", func(w http.ResponseWriter, r *http.Request) {
if a.models == nil || a.quotas == nil {
@@ -137,6 +124,9 @@ func (a *API) registerQuotas(mux *http.ServeMux) {
// QuotaViews lists every quota with what its scope holds now.
func (a *API) QuotaViews(ctx context.Context) ([]QuotaView, error) {
+ if a.quotas == nil {
+ return nil, nil
+ }
qs, err := a.quotas.CapacityQuotas(ctx)
if err != nil {
return nil, err
@@ -235,3 +225,61 @@ func (a *API) resolveScope(ctx context.Context, s string) (store.Scope, string,
}
return "", "", "", fmt.Errorf("%q could be any of %d %ss; give its ID", what, len(found), kind)
}
+
+// SetQuota sets a scope's capacity quota, named by path or ID, and returns
+// the scope as org:ID.
+func (a *API) SetQuota(ctx context.Context, req QuotaRequest, by string) (string, error) {
+ if a.quotas == nil {
+ return "", fmt.Errorf("this control plane has no store")
+ }
+ scope, id, _, err := a.resolveScope(ctx, req.Scope)
+ if err != nil {
+ return "", err
+ }
+ if !req.Guarantee.Any() && !req.Limit.Any() {
+ return "", fmt.Errorf("a quota needs a guarantee or a limit; to remove one, clear it")
+ }
+ q := store.CapacityQuota{Scope: scope, ScopeID: id, Guarantee: req.Guarantee, Limit: req.Limit, UpdatedBy: by}
+ if err := a.quotas.PutCapacityQuota(ctx, q); err != nil {
+ return "", err
+ }
+ return quota.Key(scope, id), nil
+}
+
+// ClearQuota removes a scope's capacity quota.
+func (a *API) ClearQuota(ctx context.Context, scope string) (string, error) {
+ if a.quotas == nil {
+ return "", fmt.Errorf("this control plane has no store")
+ }
+ s, id, _, err := a.resolveScope(ctx, scope)
+ if err != nil {
+ return "", err
+ }
+ if err := a.quotas.DeleteCapacityQuota(ctx, s, id); err != nil {
+ return "", err
+ }
+ return quota.Key(s, id), nil
+}
+
+// ScopeName is a part of the tenancy tree: org:ID and its path.
+type ScopeName struct {
+ Key string `json:"scope"`
+ Name string `json:"name"`
+}
+
+// Scopes lists every organization, team and project, by path.
+func (a *API) Scopes(ctx context.Context) ([]ScopeName, error) {
+ if a.quotas == nil {
+ return nil, nil
+ }
+ names, err := a.scopeNames(ctx)
+ if err != nil {
+ return nil, err
+ }
+ out := make([]ScopeName, 0, len(names))
+ for k, n := range names {
+ out = append(out, ScopeName{Key: k, Name: n})
+ }
+ sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
+ return out, nil
+}
diff --git a/internal/control/adminapi/target.go b/internal/control/adminapi/target.go
index cebd1d0..fba0358 100644
--- a/internal/control/adminapi/target.go
+++ b/internal/control/adminapi/target.go
@@ -24,6 +24,9 @@ type TargetView struct {
Room int `json:"room"`
Fits bool `json:"fits"`
Blocked []string `json:"blocked,omitempty"`
+ // Preempts is whose replicas would be stopped to make the room, when
+ // free room alone is not enough; Room counts that room too.
+ Preempts []string `json:"preempts,omitempty"`
}
// Target sizes a model for a throughput.
@@ -40,8 +43,11 @@ func (a *API) Target(model string, tokPerSec float64) (TargetView, error) {
}
v.Current = h.Current
v.More = max(v.Needed-h.Current, 0)
- v.Room = h.More
- v.Fits = v.More <= h.More || h.Limited
+ v.Room = h.More + h.ByPreempting
+ v.Fits = v.More <= v.Room || h.Limited
+ if v.More > h.More {
+ v.Preempts = h.Preempts
+ }
if !v.Fits {
v.Blocked = h.Blocked
}
diff --git a/internal/control/reconciler/headroom.go b/internal/control/reconciler/headroom.go
index 9d05df6..efa82e1 100644
--- a/internal/control/reconciler/headroom.go
+++ b/internal/control/reconciler/headroom.go
@@ -28,6 +28,11 @@ type Headroom struct {
// TokPerSec is what one more replica was measured to serve, on the
// profile placement would give it; zero when it was never measured.
TokPerSec float64
+ // ByPreempting is how many more, beyond More, would fit by stopping
+ // replicas the model's class may take, and Preempts the models those
+ // replicas belong to: what "room" means for a model that outranks others.
+ ByPreempting int
+ Preempts []string
}
// Headroom tries placing up to limit more replicas of a model on a copy of
@@ -61,12 +66,29 @@ func (r *Reconciler) Headroom(model string, limit int) (Headroom, bool) {
if pol != nil {
usage = pol.Usage(r.Holdings())
}
- for h.More < limit {
+ place := func() placement.Result {
res := sched.Place(r.orderedProfiles(m), avoid)
if res.Placement == nil && len(avoid) > 0 {
// As scaleUp does: spreading is a preference, not a rule.
res = sched.Place(r.orderedProfiles(m), nil)
}
+ return res
+ }
+ // What the model's class may take, in the order preemption would, each
+ // released on the copy only when free room has run out.
+ victims, nextVictim, preempting := r.victims(m), 0, false
+ for h.More+h.ByPreempting < limit {
+ res := place()
+ for res.Placement == nil && nextVictim < len(victims) {
+ v := victims[nextVictim]
+ nextVictim++
+ inv.ReleaseReplica(v.ID)
+ if !slices.Contains(h.Preempts, v.Model) {
+ h.Preempts = append(h.Preempts, v.Model)
+ }
+ preempting = true
+ res = place()
+ }
if res.Placement == nil {
for _, rej := range res.Rejections {
h.Blocked = append(h.Blocked, rej.String())
@@ -103,8 +125,14 @@ func (r *Reconciler) Headroom(model string, limit int) (Headroom, bool) {
nodes = append(nodes, a.NodeID)
}
avoid = append(avoid, nodes...)
- h.Nodes = append(h.Nodes, nodes)
- h.More++
+ // Once anything was stopped, what fits after is counted as fitting
+ // by preempting: it may be sitting in the room that was freed.
+ if preempting {
+ h.ByPreempting++
+ } else {
+ h.Nodes = append(h.Nodes, nodes)
+ h.More++
+ }
}
h.Limited = true
return h, true
diff --git a/internal/control/reconciler/preempt_test.go b/internal/control/reconciler/preempt_test.go
index f095480..87559dd 100644
--- a/internal/control/reconciler/preempt_test.go
+++ b/internal/control/reconciler/preempt_test.go
@@ -312,3 +312,22 @@ func TestOfTwoThatWouldEachMakeRoomTheSmallerIsStopped(t *testing.T) {
}
}
}
+
+func TestHeadroomCountsWhatAHigherClassCouldTake(t *testing.T) {
+ h := fillThenWant(t, gpuModel("vital", catalog.ClassCritical, 1), gpuModel("chat", "", 1), gpuNode("1", 8))
+ got, _ := h.r.Headroom("vital", 3)
+ if got.More != 0 || got.ByPreempting != 1 || len(got.Preempts) != 1 || got.Preempts[0] != "chat" {
+ t.Errorf("a critical model should see one more by stopping chat: %+v", got)
+ }
+ // Tried on a copy: nothing was stopped.
+ if replicasOf(h, "chat") != 1 {
+ t.Fatal("asking about room stopped a replica")
+ }
+ // A model of chat's own class may not take its minimum, so has no such room.
+ peer := newFleetHarness(t, []catalog.Model{gpuModel("chat", "", 1), withMin(gpuModel("peer", "", 1), 0)}, gpuNode("1", 8))
+ peer.settle(t, 3)
+ peer.advance(peer.r.cfg.MinLifetime + time.Minute)
+ if got, _ := peer.r.Headroom("peer", 3); got.ByPreempting != 0 {
+ t.Errorf("a standard model counted room in another's minimum: %+v", got)
+ }
+}
diff --git a/internal/router/handlers.go b/internal/router/handlers.go
index be87b30..6ff1592 100644
--- a/internal/router/handlers.go
+++ b/internal/router/handlers.go
@@ -185,6 +185,11 @@ func (r *Router) Handler() http.Handler {
pages.RegisterAdminUsers(mux, r.store)
// Quotas are written on the same tree the accounts page builds, so
// they are mounted together.
+ // Capacity quotas are the control plane's, reached through the
+ // same admin API the CLI uses.
+ if c, ok := r.clusterView.(web.CapacityAdmin); ok {
+ pages.SetCapacity(c)
+ }
pages.RegisterAdminQuotas(mux, r.store, r.acct)
// And the report of what that tree actually used.
pages.RegisterAdminUsage(mux, r.store, r.acct)
diff --git a/internal/web/account.go b/internal/web/account.go
index eefe394..e24a6e8 100644
--- a/internal/web/account.go
+++ b/internal/web/account.go
@@ -61,6 +61,8 @@ type AccountPages struct {
hasMirror bool
// hasQuotas is set when /admin/quotas is registered.
hasQuotas bool
+ // capacity sets capacity quotas, from the quotas page; nil leaves them off.
+ capacity CapacityAdmin
// personalOff refuses personal keys; see SetPersonalOff.
personalOff bool
// personalDefaults is what personal use falls under by default.
diff --git a/internal/web/admin_capacity.go b/internal/web/admin_capacity.go
new file mode 100644
index 0000000..a97d066
--- /dev/null
+++ b/internal/web/admin_capacity.go
@@ -0,0 +1,138 @@
+package web
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "strconv"
+ "strings"
+
+ "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
+ "gitea.mynodes.xyz/ilker/llmgw/internal/store"
+)
+
+// CapacityAdmin sets what parts of the tenancy tree are guaranteed of the
+// cluster and may hold. The admin API implements it; the page and the CLI go
+// through the same checks.
+type CapacityAdmin interface {
+ QuotaViews(ctx context.Context) ([]adminapi.QuotaView, error)
+ SetQuota(ctx context.Context, req adminapi.QuotaRequest, by string) (string, error)
+ ClearQuota(ctx context.Context, scope string) (string, error)
+ Scopes(ctx context.Context) ([]adminapi.ScopeName, error)
+}
+
+// SetCapacity gives the quotas page capacity quotas to show and set. Without
+// it the section is left off.
+func (p *AccountPages) SetCapacity(c CapacityAdmin) { p.capacity = c }
+
+// capacityRow is one scope's capacity quota as the page shows it.
+type capacityRow struct {
+ adminapi.QuotaView
+ GPUs, Cores, Memory string
+}
+
+// capacityForm is what the form starts with: a scope's quota to change, or
+// nothing for a new one.
+type capacityForm struct {
+ Scope string
+ GPUs, Cores, Memory string
+ LGPUs, LCores, LMem string
+}
+
+func (p *AccountPages) capacitySection(ctx context.Context, d *adminQuotasData, editing string) {
+ if p.capacity == nil {
+ return
+ }
+ d.HasCapacity = true
+ views, err := p.capacity.QuotaViews(ctx)
+ if err != nil {
+ d.Error = err.Error()
+ return
+ }
+ for _, v := range views {
+ d.Capacity = append(d.Capacity, capacityRow{QuotaView: v,
+ GPUs: capCell(v.Holds.GPUs, v.Guarantee.GPUs, v.Limit.GPUs),
+ Cores: capCell(v.Holds.CPUCores, v.Guarantee.CPUCores, v.Limit.CPUCores),
+ Memory: capCell(v.Holds.MemoryGB, v.Guarantee.MemoryGB, v.Limit.MemoryGB)})
+ if v.Scope == editing {
+ d.CapForm = capacityForm{Scope: v.Scope,
+ GPUs: intStr(v.Guarantee.GPUs), Cores: intStr(v.Guarantee.CPUCores), Memory: intStr(v.Guarantee.MemoryGB),
+ LGPUs: intStr(v.Limit.GPUs), LCores: intStr(v.Limit.CPUCores), LMem: intStr(v.Limit.MemoryGB)}
+ }
+ }
+ if d.Scopes, err = p.capacity.Scopes(ctx); err != nil {
+ d.Error = err.Error()
+ }
+}
+
+// capCell is "6 of 8 (at most 16)": held, guaranteed, limit.
+func capCell(holds int, g, l *int) string {
+ if g == nil && l == nil {
+ return ""
+ }
+ out := strconv.Itoa(holds)
+ if g != nil {
+ out += " of " + strconv.Itoa(*g)
+ }
+ if l != nil {
+ out += fmt.Sprintf(" (at most %d)", *l)
+ }
+ return out
+}
+
+func intStr(v *int) string {
+ if v == nil {
+ return ""
+ }
+ return strconv.Itoa(*v)
+}
+
+// applyCapacity sets or clears one scope's capacity quota from the form.
+func (p *AccountPages) applyCapacity(r *http.Request, admin store.User) (string, error) {
+ scope := strings.TrimSpace(r.FormValue("scope"))
+ if scope == "" {
+ return "", fmt.Errorf("choose where the quota applies")
+ }
+ if r.FormValue("clear") != "" {
+ key, err := p.capacity.ClearQuota(r.Context(), scope)
+ if err != nil {
+ return "", err
+ }
+ return "capacity quota removed from " + key, nil
+ }
+ field := func(name, what string) (*int, error) {
+ v := strings.TrimSpace(r.FormValue(name))
+ if v == "" {
+ return nil, nil
+ }
+ n, err := strconv.Atoi(v)
+ if err != nil || n < 0 {
+ return nil, fmt.Errorf("%s must be a whole number, or blank for none", what)
+ }
+ return &n, nil
+ }
+ var req adminapi.QuotaRequest
+ req.Scope = scope
+ var err error
+ for _, f := range []struct {
+ dst **int
+ name string
+ what string
+ }{
+ {&req.Guarantee.GPUs, "gpus", "guaranteed GPUs"},
+ {&req.Guarantee.CPUCores, "cores", "guaranteed cores"},
+ {&req.Guarantee.MemoryGB, "memory", "guaranteed memory"},
+ {&req.Limit.GPUs, "limit_gpus", "the GPU limit"},
+ {&req.Limit.CPUCores, "limit_cores", "the core limit"},
+ {&req.Limit.MemoryGB, "limit_memory", "the memory limit"},
+ } {
+ if *f.dst, err = field(f.name, f.what); err != nil {
+ return "", err
+ }
+ }
+ key, err := p.capacity.SetQuota(r.Context(), req, admin.Email)
+ if err != nil {
+ return "", err
+ }
+ return "capacity quota set on " + key + "; it takes effect within half a minute", nil
+}
diff --git a/internal/web/admin_capacity_test.go b/internal/web/admin_capacity_test.go
new file mode 100644
index 0000000..8eb4072
--- /dev/null
+++ b/internal/web/admin_capacity_test.go
@@ -0,0 +1,88 @@
+package web
+
+import (
+ "context"
+ "errors"
+ "log/slog"
+ "net/http"
+ "net/url"
+ "strings"
+ "testing"
+
+ "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
+ "gitea.mynodes.xyz/ilker/llmgw/internal/control/quota"
+ "gitea.mynodes.xyz/ilker/llmgw/internal/store"
+)
+
+type fakeCapacity struct {
+ views []adminapi.QuotaView
+ set []adminapi.QuotaRequest
+ cleared []string
+}
+
+func (f *fakeCapacity) QuotaViews(context.Context) ([]adminapi.QuotaView, error) { return f.views, nil }
+func (f *fakeCapacity) SetQuota(_ context.Context, r adminapi.QuotaRequest, _ string) (string, error) {
+ if r.Scope == "team:nope" {
+ return "", errors.New(`there is no team "nope"`)
+ }
+ f.set = append(f.set, r)
+ return r.Scope, nil
+}
+func (f *fakeCapacity) ClearQuota(_ context.Context, s string) (string, error) {
+ f.cleared = append(f.cleared, s)
+ return s, nil
+}
+func (f *fakeCapacity) Scopes(context.Context) ([]adminapi.ScopeName, error) {
+ return []adminapi.ScopeName{{Key: "org:o1", Name: "Acme"}, {Key: "team:t1", Name: "Acme / ml"}}, nil
+}
+
+func TestCapacityQuotasAreShownAndSetFromTheQuotasPage(t *testing.T) {
+ eight, sixteen := 8, 16
+ fc := &fakeCapacity{views: []adminapi.QuotaView{{Scope: "team:t1", Name: "Acme / ml",
+ Guarantee: store.Resources{GPUs: &eight}, Limit: store.Resources{GPUs: &sixteen},
+ Holds: quota.Amount{GPUs: 6}, UpdatedBy: "ops@acme"}}}
+ a := fakeAccounts{user: store.User{ID: "u1", Email: "a@example.com", IsAdmin: true}}
+ p := NewAccountPages(a, nil, nil, "v0", slog.New(slog.DiscardHandler))
+ p.SetCapacity(fc)
+ mux := http.NewServeMux()
+ p.RegisterAdminQuotas(mux, &fakeQuotas{}, pricedYes{})
+
+ body := mirrorGet(t, mux, "/admin/quotas").Body.String()
+ for _, want := range []string{`id="capacity"`, "Acme / ml", "6 of 8 (at most 16)", "ops@acme",
+ ``} {
+ if !strings.Contains(body, want) {
+ t.Errorf("the page lacks %q", want)
+ }
+ }
+ // "change" fills the form with the scope's figures.
+ edit := mirrorGet(t, mux, "/admin/quotas?cap=team:t1").Body.String()
+ if !strings.Contains(edit, ``,
+ `name="owner"`, ``, `name="memory" value="12"`, `name="cores" value="4"`} {
+ if !strings.Contains(card, want) {
+ t.Errorf("the card lacks %q", want)
+ }
+ }
+ post := func(v url.Values) string {
+ v.Set("csrf", "tok")
+ v.Set("model", "coder")
+ v.Set("open", "coder:v1")
+ return mirrorPost(t, mux, "/admin/mirror/replicas", v).Body.String()
+ }
+ same := url.Values{"min": {"1"}, "max": {"1"}, "class": {""}, "owner": {""}, "memory": {"12"}, "cores": {"4"}}
+ if body := post(same); !strings.Contains(body, "coder: nothing changed") ||
+ len(cat.classes)+len(cat.owners)+len(cat.resources)+len(cat.scaled) != 0 {
+ t.Errorf("an unchanged form changed something: %v %v %v %v", cat.classes, cat.owners, cat.resources, cat.scaled)
+ }
+ changed := url.Values{"min": {"1"}, "max": {"1"}, "class": {"critical"}, "owner": {"team:t1"}, "memory": {"14"}, "cores": {"4"}}
+ body := post(changed)
+ if strings.Join(cat.resources, ",") != "coder=14/4" || strings.Join(cat.classes, ",") != "coder=critical" ||
+ strings.Join(cat.owners, ",") != "coder=team:t1" || len(cat.scaled) != 0 {
+ t.Errorf("applied %v %v %v %v", cat.resources, cat.classes, cat.owners, cat.scaled)
+ }
+ if !strings.Contains(body, "a replica reserves 14 GB and 4 cores") || !strings.Contains(body, "counts against team:t1") {
+ t.Errorf("the change was not said: %s", body)
+ }
+
+ // Without capacity quotas there is no owner to choose, and none is sent.
+ plain := mirrorGet(t, settingsPage(t, coderCatalog(), false), "/admin/mirror?open=coder:v1").Body.String()
+ if strings.Contains(plain, `name="owner"`) {
+ t.Error("an owner choice with no quotas to count against")
+ }
+}
+
+func TestRoomAHigherClassWouldMakeIsOfferedAndSaid(t *testing.T) {
+ fleet := &roomyFleet{more: 0, preempt: 1, preempts: []string{"chat"}, blocked: []string{"n1: full"}}
+ cat := coderCatalog()
+ mux := mirrorPageDeps(t, MirrorDeps{Mirror: &fakeMirror{models: []mirror.Status{
+ {Model: "coder", Version: "v1", File: "c.gguf", State: mirror.Present, BytesDone: 1},
+ }}, Catalog: cat, Fleet: fleet}, true)
+
+ card := mirrorGet(t, mux, "/admin/mirror?open=coder:v1").Body.String()
+ if !strings.Contains(card, "No free room for another replica now.") ||
+ !strings.Contains(card, "1 by stopping replicas its class outranks: chat.") ||
+ !strings.Contains(card, `name="min" value="1" min="0" max="2"`) {
+ t.Errorf("the card should offer the room preemption makes, and say whose")
+ }
+ body := mirrorPost(t, mux, "/admin/mirror/replicas", url.Values{"csrf": {"tok"},
+ "model": {"coder"}, "min": {"2"}, "max": {"2"}, "open": {"coder:v1"}}).Body.String()
+ if len(cat.scaled) != 1 || cat.lastScale.Min != 2 {
+ t.Fatalf("two were refused though preemption makes room: %s", body)
+ }
+ if !strings.Contains(body, "replicas of chat will be stopped") {
+ t.Errorf("stopping somebody else's replica should be said: %s", body)
+ }
+ body = mirrorPost(t, mux, "/admin/mirror/replicas", url.Values{"csrf": {"tok"},
+ "model": {"coder"}, "min": {"3"}, "max": {"3"}, "open": {"coder:v1"}}).Body.String()
+ if len(cat.scaled) != 1 || !strings.Contains(body, "at most 2") {
+ t.Errorf("three is past even what preemption makes: %s", body)
+ }
+}
diff --git a/internal/web/static/base.css b/internal/web/static/base.css
index 489bc2e..878d835 100644
--- a/internal/web/static/base.css
+++ b/internal/web/static/base.css
@@ -855,3 +855,6 @@ table.explore .from { margin-left: .5rem; font-size: .78rem; color: var(--muted)
.clip-inline { display: inline-block; max-width: 24rem; overflow: hidden; text-overflow: ellipsis;
vertical-align: bottom; font-size: .8rem; color: var(--muted); margin-left: .4rem; }
table.rows td.warn { color: var(--warn); cursor: help; }
+.held-card .replicas-form select, .capacity-form select { display: block; margin-top: .25rem; }
+.held-card .replicas-form .field-row { flex-wrap: wrap; gap: .75rem 1rem; }
+.quota-tree td.q-act form.inline { margin-left: .75rem; }
diff --git a/internal/web/templates/admin_mirror.html b/internal/web/templates/admin_mirror.html
index 42aba77..091e312 100644
--- a/internal/web/templates/admin_mirror.html
+++ b/internal/web/templates/admin_mirror.html
@@ -443,7 +443,7 @@ computes once it has it.
{{end}}
{{/* The card beneath a held version: what runs it, where, and how many. */}}
-{{define "mirror_held_detail"}}{{$row := .Row}}{{$csrf := .CSRF}}
+{{define "mirror_held_detail"}}{{$row := .Row}}{{$csrf := .CSRF}}{{$scopes := .D.Scopes}}
{{if gt $row.Files 1}}{{$row.Files}} files{{else}}{{$row.File}}{{end}}
@@ -482,7 +482,8 @@ computes once it has it.
{{if .Limited}}Room for at least {{.More}} more replicas.
{{else if .More}}Room for {{.More}} more now{{if .Nodes}}, on {{range $i, $n := .Nodes}}{{if $i}}, {{end}}{{join $n}}{{end}}{{end}}.
-{{else}}No room for another replica now.{{end}}
+{{else}}No free room for another replica now.{{end}}
+{{if .ByPreempting}} {{if .More}}And {{end}}{{.ByPreempting}} {{if .More}}more {{end}}by stopping replicas its class outranks: {{join .Preempts}}.{{end}}
{{if and (not .Limited) .Blocked}}Why not {{if .More}}more{{else}}another{{end}}
{{range .Blocked}}
{{.}}
{{end}}
{{end}}
@@ -500,11 +501,30 @@ computes once it has it.
-
+
+ {{if $scopes}}{{$owner := .Owner}}{{end}}
-
{{if .MostNow}}At most {{.MostNow}} can always run with what is free now. {{end}}0 always running with a
+
+
+
+
+
+
{{if .MostNow}}At most {{.MostNow}} can always run now{{if .Headroom}}{{if .Headroom.ByPreempting}}, counting what its class may stop to make room{{end}}{{end}}. {{end}}0 always running with a
higher limit loads it when asked for; 0 and 0 stops it. A replica is added under load when requests wait, and
- taken away after ten quiet minutes.
+ taken away after ten quiet minutes. A higher class may stop replicas of lower ones to make room; memory and
+ cores apply to replicas started after the change.
{{end}}
diff --git a/internal/web/templates/admin_quotas.html b/internal/web/templates/admin_quotas.html
index 8eb195e..67f85dd 100644
--- a/internal/web/templates/admin_quotas.html
+++ b/internal/web/templates/admin_quotas.html
@@ -147,6 +147,60 @@ keys, projects and the chat page. {{.Personal}}
{{else}}
Nobody has been approved yet.
{{end}}
+{{if .HasCapacity}}
+
Capacity
+
What a part of the tree is guaranteed of the cluster, and the most
+it may hold: GPUs, CPU cores and memory, counted from the replicas of the models
+it owns and of those beneath it. What it holds within its guarantee is never taken
+for someone else's model; beyond it, it borrows free room and gives it back when
+asked. A model's owner is set on its card on the mirror page.