Files
ilkerandClaude Opus 5 9ad16290bf hpcod: oversubscription is a policy you can change, not a line in a file
It was enabled - 168 cores offered from 28, 655 GiB from 504 - but only
because I edited sites.yaml on the server and restarted the service. So
changing it needed a shell, an editor and a restart, which means it needed
whoever had those rather than whoever had the judgement. It is a judgement
about how much of the hardware to promise, it moves with load, and it
belongs where the person making it can reach it.

It is now a row per site, set from the Sites page by an administrator, and
it takes effect on the next discovery sweep rather than the next restart.

The ratio is applied after discovery instead of inside the provider, which
is what makes that possible - and because it recomputes from the physical
core and memory counts rather than from whatever Cores already held,
applying it twice gives the same answer as applying it once. Without that
the provider's own default would compound with it.

The file stays as the default. A site with no row shows what the catalogue
gave it, read back from the numbers rather than claimed, and says so: "from
the site catalogue - nobody has set this" against "set by ilker". One
authority at a time, and the page says which.

The rules the config loader already enforced are enforced here too, in the
database rather than in the handler: a policy that reads as off cannot
still multiply, and turning it off resets the ratios instead of leaving
numbers behind that would take effect again for whoever turns it back on.
Memory is capped at 8x where CPU is capped at 64x, because oversubscribed
cores make jobs slow and oversubscribed memory makes them die.

Also fixed a test that was crying wolf: the public-page leak check matched
bare substrings, so adding a "label" rule to the stylesheet looked like the
tenant name "lab" appearing on the front door. It matches word boundaries
now - a test that raises false alarms gets weakened until it stops catching
anything.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 12:54:24 +02:00

160 lines
4.5 KiB
Go

package httpapi
import (
"log"
"net/http"
"strconv"
"strings"
"github.com/ilkermanap/hpcod/internal/model"
)
// The oversubscription control.
//
// The policy was a line in a YAML file on the server, which meant changing
// it needed a shell, an editor and a restart - so in practice it needed
// whoever had those, rather than whoever had the judgement. It is a
// judgement about how much of the hardware to promise, it changes with
// load, and it belongs where the person making it can reach it.
// sitePolicyView is one site's policy, with where it came from.
type sitePolicyView struct {
Site string
Enabled bool
CPU float64
Mem float64
UpdatedBy string
FromFile bool
// What the policy currently amounts to, so the page can show the
// decision and its effect together instead of making someone multiply.
PhysicalCores int
PhysicalMemGB int64
OfferedCores int
OfferedMemGB int64
}
func (s *Server) sitePolicies(r *http.Request) []sitePolicyView {
sites, _ := s.cache.get()
recorded := map[string]model.Overcommit{}
if s.store != nil {
if got, err := s.store.Policies(r.Context()); err != nil {
log.Printf("site policy: %v", err)
} else {
recorded = got
}
}
out := make([]sitePolicyView, 0, len(sites))
for _, site := range sites {
v := sitePolicyView{Site: site.ID, CPU: 1, Mem: 1, FromFile: true}
if p, ok := recorded[site.ID]; ok {
v.Enabled, v.CPU, v.Mem = p.Enabled, p.CPU, p.Mem
v.UpdatedBy, v.FromFile = p.UpdatedBy, false
}
for _, n := range site.Nodes {
if !n.Online {
continue
}
v.PhysicalCores += n.PhysicalCores
v.PhysicalMemGB += n.PhysicalMemBytes / (1 << 30)
v.OfferedCores += n.Cores
v.OfferedMemGB += n.MemBytes / (1 << 30)
}
// With no row, what is in effect is whatever the catalogue said,
// which the numbers already show. Read it back rather than
// claiming 1.0 the page cannot support.
if v.FromFile && v.PhysicalCores > 0 {
v.CPU = float64(v.OfferedCores) / float64(v.PhysicalCores)
if v.PhysicalMemGB > 0 {
v.Mem = float64(v.OfferedMemGB) / float64(v.PhysicalMemGB)
}
v.Enabled = v.CPU > 1.001 || v.Mem > 1.001
}
out = append(out, v)
}
return out
}
// setSitePolicy is the POST behind the form. Admin only: it changes how
// much of the estate every tenant can be promised.
func (s *Server) setSitePolicy(w http.ResponseWriter, r *http.Request) {
if !isAdmin(r.Context()) {
http.NotFound(w, r)
return
}
if s.store == nil {
http.Error(w, "no database configured, so a policy cannot be recorded", http.StatusServiceUnavailable)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
who := userFromContext(r.Context())
if who == "" {
http.Error(w, "an unattributed policy change is not a decision", http.StatusForbidden)
return
}
site := strings.TrimSpace(r.PostFormValue("site"))
if site == "" {
http.Error(w, "which site?", http.StatusBadRequest)
return
}
// Refuse a site nobody has heard of, so a typo records a policy that
// silently applies to nothing.
known := false
for _, have := range s.knownSites() {
if have == site {
known = true
break
}
}
if !known {
http.Error(w, "no such site", http.StatusBadRequest)
return
}
o := model.Overcommit{Site: site, Enabled: r.PostFormValue("enabled") == "on", CPU: 1, Mem: 1}
if o.Enabled {
cpu, err1 := strconv.ParseFloat(strings.TrimSpace(r.PostFormValue("cpu")), 64)
mem, err2 := strconv.ParseFloat(strings.TrimSpace(r.PostFormValue("mem")), 64)
if err1 != nil || err2 != nil {
http.Error(w, "the ratios have to be numbers", http.StatusBadRequest)
return
}
o.CPU, o.Mem = cpu, mem
}
got, err := s.store.SetPolicy(r.Context(), o, who)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Printf("site %s: oversubscribe=%v cpu=%.2f mem=%.2f by %s",
got.Site, got.Enabled, got.CPU, got.Mem, who)
// Apply it to the cached inventory now. Otherwise the page that
// redisplays would show the old numbers until the next sweep, and an
// operator would reasonably conclude the change had not taken.
sites, at := s.cache.get()
policies, err := s.store.Policies(r.Context())
if err == nil {
model.ApplyOvercommit(sites, policies)
s.cache.setAt(sites, at)
}
http.Redirect(w, r, "/sites", http.StatusSeeOther)
}
func (s *Server) knownSites() []string {
sites, _ := s.cache.get()
out := make([]string, 0, len(sites))
for _, x := range sites {
out = append(out, x.ID)
}
return out
}