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>
85 lines
2.8 KiB
Go
85 lines
2.8 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/ilkermanap/hpcod/internal/model"
|
|
)
|
|
|
|
// Policies returns the recorded oversubscription policy for every site
|
|
// that has one. A site missing from the map is still on whatever the site
|
|
// catalogue said.
|
|
func (s *Store) Policies(ctx context.Context) (map[string]model.Overcommit, error) {
|
|
rows, err := s.pool.Query(ctx, `
|
|
SELECT site, oversubscribe, cpu_overcommit::float8, mem_overcommit::float8, updated_by
|
|
FROM site_policy`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := map[string]model.Overcommit{}
|
|
for rows.Next() {
|
|
var o model.Overcommit
|
|
if err := rows.Scan(&o.Site, &o.Enabled, &o.CPU, &o.Mem, &o.UpdatedBy); err != nil {
|
|
return nil, err
|
|
}
|
|
out[o.Site] = o
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// SetPolicy records a policy, replacing whatever was there.
|
|
//
|
|
// The same contradictions the config loader refuses are refused here, and
|
|
// with the same wording, so an operator who has seen one error recognises
|
|
// the other. The database refuses them too; this is so the message names
|
|
// the alternatives instead of quoting a constraint.
|
|
func (s *Store) SetPolicy(ctx context.Context, o model.Overcommit, by string) (*model.Overcommit, error) {
|
|
if by == "" {
|
|
return nil, errors.New("a policy change has to say who made it")
|
|
}
|
|
if o.Site == "" {
|
|
return nil, errors.New("which site?")
|
|
}
|
|
cpu, mem := o.CPU, o.Mem
|
|
if !o.Enabled {
|
|
// Turning it off resets the ratios rather than leaving numbers
|
|
// behind that would take effect again the moment it is turned back
|
|
// on by someone who did not know they were there.
|
|
cpu, mem = 1, 1
|
|
}
|
|
switch {
|
|
case cpu < 1 || cpu > 64:
|
|
return nil, fmt.Errorf("cpu_overcommit must be between 1.0 and 64.0, got %g", cpu)
|
|
case mem < 1 || mem > 8:
|
|
return nil, fmt.Errorf("mem_overcommit must be between 1.0 and 8.0, got %g - memory is the one that kills jobs rather than slowing them", mem)
|
|
case o.Enabled && cpu == 1 && mem == 1:
|
|
return nil, errors.New("oversubscription is on but both ratios are 1.0, so it would do nothing")
|
|
}
|
|
|
|
var got model.Overcommit
|
|
err := s.pool.QueryRow(ctx, `
|
|
INSERT INTO site_policy (site, oversubscribe, cpu_overcommit, mem_overcommit, updated_by)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
ON CONFLICT (site) DO UPDATE
|
|
SET oversubscribe = EXCLUDED.oversubscribe,
|
|
cpu_overcommit = EXCLUDED.cpu_overcommit,
|
|
mem_overcommit = EXCLUDED.mem_overcommit,
|
|
updated_at = now(), updated_by = EXCLUDED.updated_by
|
|
RETURNING site, oversubscribe, cpu_overcommit::float8, mem_overcommit::float8, updated_by`,
|
|
o.Site, o.Enabled, cpu, mem, by).Scan(
|
|
&got.Site, &got.Enabled, &got.CPU, &got.Mem, &got.UpdatedBy)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, errors.New("the policy was not recorded")
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &got, nil
|
|
}
|