The proxy leases a not-yet-loaded model's size (+20%) and forces CPU-only with 4 threads on a CPU grant; loaded models, other endpoints and a down broker pass through. Spread leases let a model use both cards, as Ollama does with gemma4-12b (8.4 GB > the 1080's 8 GB). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
5.0 KiB
Go
141 lines
5.0 KiB
Go
// Package server exposes the scheduler over HTTP (see api/openapi.yaml) and as Prometheus metrics.
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.godemperordoge.org/ziadarafat/gpu-broker/internal/api"
|
|
"gitea.godemperordoge.org/ziadarafat/gpu-broker/internal/sched"
|
|
)
|
|
|
|
type handlers struct {
|
|
s *sched.Scheduler
|
|
ttl time.Duration
|
|
}
|
|
|
|
var priorities = map[api.Priority]sched.Priority{
|
|
api.Batch: sched.Batch, api.Interactive: sched.Interactive, api.Realtime: sched.Realtime,
|
|
}
|
|
|
|
var priorityNames = map[sched.Priority]api.Priority{
|
|
sched.Batch: api.Batch, sched.Interactive: api.Interactive, sched.Realtime: api.Realtime,
|
|
}
|
|
|
|
func (h handlers) Health(_ context.Context, _ api.HealthRequestObject) (api.HealthResponseObject, error) {
|
|
if len(h.s.Status().GPUs) == 0 {
|
|
return api.Health200JSONResponse{Status: api.NoSample}, nil
|
|
}
|
|
return api.Health200JSONResponse{Status: api.Ok}, nil
|
|
}
|
|
|
|
func (h handlers) AcquireLease(ctx context.Context, r api.AcquireLeaseRequestObject) (api.AcquireLeaseResponseObject, error) {
|
|
body := r.Body
|
|
priority, ok := priorities[body.Priority]
|
|
if !ok || body.VramMb < 1 || body.Client == "" {
|
|
return api.AcquireLease400JSONResponse{ProblemJSONResponse: api.ProblemJSONResponse{Error: "need client, priority and vram_mb >= 1"}}, nil
|
|
}
|
|
req := sched.Request{Client: body.Client, Priority: priority, VRAMMB: body.VramMb}
|
|
if body.MaxWaitS != nil {
|
|
req.MaxWait = time.Duration(*body.MaxWaitS) * time.Second
|
|
}
|
|
if body.CpuFallback != nil {
|
|
req.CPUFallback = *body.CpuFallback
|
|
}
|
|
if body.Prefer != nil {
|
|
req.Prefer = *body.Prefer
|
|
}
|
|
if body.Spread != nil {
|
|
req.Spread = *body.Spread
|
|
}
|
|
g, err := h.s.Acquire(ctx, req)
|
|
switch {
|
|
case errors.Is(err, sched.ErrTooBig):
|
|
return api.AcquireLease413JSONResponse{Error: err.Error()}, nil
|
|
case errors.Is(err, sched.ErrTimeout):
|
|
return api.AcquireLease503JSONResponse{Error: err.Error()}, nil
|
|
case err != nil:
|
|
return nil, err
|
|
}
|
|
if g.GPU == sched.CPU {
|
|
return api.AcquireLease200JSONResponse{Device: api.Cpu, Gpu: sched.CPU}, nil
|
|
}
|
|
return api.AcquireLease200JSONResponse{Device: api.Gpu, Gpu: g.GPU, LeaseId: &g.LeaseID, ExpiresAt: &g.ExpiresAt}, nil
|
|
}
|
|
|
|
func (h handlers) RenewLease(_ context.Context, r api.RenewLeaseRequestObject) (api.RenewLeaseResponseObject, error) {
|
|
if err := h.s.Renew(r.Id); err != nil {
|
|
return api.RenewLease404JSONResponse{ProblemJSONResponse: api.ProblemJSONResponse{Error: err.Error()}}, nil
|
|
}
|
|
return api.RenewLease204Response{}, nil
|
|
}
|
|
|
|
func (h handlers) ReleaseLease(_ context.Context, r api.ReleaseLeaseRequestObject) (api.ReleaseLeaseResponseObject, error) {
|
|
h.s.Release(r.Id)
|
|
return api.ReleaseLease204Response{}, nil
|
|
}
|
|
|
|
func (h handlers) Status(_ context.Context, _ api.StatusRequestObject) (api.StatusResponseObject, error) {
|
|
st := h.s.Status()
|
|
out := api.Status200JSONResponse{TtlS: int(h.ttl / time.Second)}
|
|
for _, g := range st.GPUs {
|
|
out.Gpus = append(out.Gpus, api.GpuStatus{Index: g.Index, TotalMb: g.TotalMB, UsedMb: g.UsedMB, ReserveMb: g.ReserveMB, FreeMb: g.FreeMB})
|
|
}
|
|
for _, l := range st.Leases {
|
|
out.Leases = append(out.Leases, api.LeaseInfo{Id: l.ID, Client: l.Client, Gpu: l.GPU, VramMb: l.VRAMMB, GrantedAt: l.Granted, ExpiresAt: l.Expires})
|
|
}
|
|
for _, w := range st.Waiting {
|
|
out.Waiting = append(out.Waiting, api.WaitInfo{Client: w.Client, Priority: priorityNames[w.Priority], VramMb: w.VRAMMB, Since: w.Since})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Handler serves the API plus /metrics.
|
|
func Handler(s *sched.Scheduler, ttl time.Duration) http.Handler {
|
|
mux := http.NewServeMux()
|
|
api.HandlerFromMux(api.NewStrictHandler(handlers{s: s, ttl: ttl}, nil), mux)
|
|
mux.HandleFunc("GET /metrics", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "text/plain; version=0.0.4")
|
|
fmt.Fprint(w, Metrics(s))
|
|
})
|
|
return mux
|
|
}
|
|
|
|
// Tick drives lease expiry and queue timeouts until ctx ends.
|
|
func Tick(ctx context.Context, s *sched.Scheduler, every time.Duration) {
|
|
t := time.NewTicker(every)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
s.Tick()
|
|
}
|
|
}
|
|
}
|
|
|
|
// Metrics renders the scheduler state in the Prometheus text format.
|
|
func Metrics(s *sched.Scheduler) string {
|
|
st := s.Status()
|
|
var b strings.Builder
|
|
b.WriteString("# HELP gpu_broker_vram_free_mb VRAM the broker can still lease (after reserve and fresh leases).\n# TYPE gpu_broker_vram_free_mb gauge\n")
|
|
gpus := st.GPUs
|
|
sort.Slice(gpus, func(i, j int) bool { return gpus[i].Index < gpus[j].Index })
|
|
for _, g := range gpus {
|
|
fmt.Fprintf(&b, "gpu_broker_vram_free_mb{gpu=\"%d\"} %d\n", g.Index, g.FreeMB)
|
|
}
|
|
b.WriteString("# HELP gpu_broker_vram_used_mb VRAM in use per nvidia-smi.\n# TYPE gpu_broker_vram_used_mb gauge\n")
|
|
for _, g := range gpus {
|
|
fmt.Fprintf(&b, "gpu_broker_vram_used_mb{gpu=\"%d\"} %d\n", g.Index, g.UsedMB)
|
|
}
|
|
fmt.Fprintf(&b, "# HELP gpu_broker_leases Active leases.\n# TYPE gpu_broker_leases gauge\ngpu_broker_leases %d\n", len(st.Leases))
|
|
fmt.Fprintf(&b, "# HELP gpu_broker_waiting Queued requests.\n# TYPE gpu_broker_waiting gauge\ngpu_broker_waiting %d\n", len(st.Waiting))
|
|
return b.String()
|
|
}
|