serve: [batch] config; open the store, reap jobs killed by a restart, start the runner

Add the [batch] config block (Batch type + File.Batch) and ParseSize (binary
KiB/MiB/GiB byte counts) in internal/config. serve now opens the dcache store
when batch is on, marks the queued/running jobs a restart killed as failed
(never resumed) and observes them, builds the runner from the config with the
runner's defaults filling zeros, and closes the runner on both shutdown paths.
Batch is on by default; disabled = true leaves srv.Batch nil so the endpoints
answer 503.

Co-Authored-By: ornith-1.5-35b-a3b <ornith-1.5-35b-a3b@llama.cpp.invalid>
This commit is contained in:
2026-09-26 23:06:46 -07:00
co-authored by ornith-1.5-35b-a3b
parent 692a334c09
commit 484c33f986
5 changed files with 132 additions and 1 deletions
+56
View File
@@ -0,0 +1,56 @@
package config
// P6 task 08: the [batch] block and ParseSize.
import (
"os"
"path/filepath"
"testing"
)
func TestBatchBlock(t *testing.T) {
p := filepath.Join(t.TempDir(), "sift.toml")
body := "[batch]\ndisabled = false\ntargets_per_job = 16\njobs_per_caller = 2\njobs_total = 4\nworkers = 2\nttl = \"12h\"\nmax_bytes = \"32MiB\"\n"
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
f, err := Load(p)
if err != nil {
t.Fatal(err)
}
b := f.Batch
if b.Disabled || b.TargetsPerJob != 16 || b.JobsPerCaller != 2 || b.JobsTotal != 4 || b.Workers != 2 || b.TTL != "12h" || b.MaxBytes != "32MiB" {
t.Fatalf("batch block %+v", b)
}
// No block: zero values, and batch is on (the runner applies its own defaults).
empty := filepath.Join(t.TempDir(), "empty.toml")
if err := os.WriteFile(empty, []byte("listen = \":8443\"\n"), 0o600); err != nil {
t.Fatal(err)
}
if f, err := Load(empty); err != nil || f.Batch != (Batch{}) {
t.Fatalf("no [batch] block: %+v %v", f.Batch, err)
}
}
func TestParseSize(t *testing.T) {
const def = 7
for _, c := range []struct {
in string
want int64
}{
{"64MiB", 64 << 20},
{"512KiB", 512 << 10},
{"1GiB", 1 << 30},
{"1000", 1000},
{" 2MiB ", 2 << 20},
{"", def},
{"junk", def},
{"-1MiB", def},
{"0", def},
{"12MB", def}, // decimal units are not accepted: say MiB
} {
if got := ParseSize(c.in, def); got != c.want {
t.Errorf("ParseSize(%q) = %d, want %d", c.in, got, c.want)
}
}
}
+40
View File
@@ -2,6 +2,8 @@ package config
import (
"os"
"strconv"
"strings"
"time"
"github.com/pelletier/go-toml/v2"
@@ -17,6 +19,19 @@ type File struct {
Digest Digest `toml:"digest"`
Context Context `toml:"context"`
Search Search `toml:"search"`
Batch Batch `toml:"batch"`
}
// Batch is the [batch] block. Batch is on by default: no block means the runner runs with its own
// defaults. disabled = true turns it off and the endpoints answer 503.
type Batch struct {
Disabled bool `toml:"disabled"`
TargetsPerJob int `toml:"targets_per_job"`
JobsPerCaller int `toml:"jobs_per_caller"`
JobsTotal int `toml:"jobs_total"`
Workers int `toml:"workers"`
TTL string `toml:"ttl"`
MaxBytes string `toml:"max_bytes"`
}
type Dixie struct {
@@ -110,3 +125,28 @@ func ParseDuration(s string, def time.Duration) time.Duration {
}
return d
}
// ParseSize parses a positive byte count: a plain integer, or one with a binary suffix KiB, MiB or
// GiB. Anything else, zero or negative returns def.
func ParseSize(s string, def int64) int64 {
s = strings.TrimSpace(s)
if s == "" {
return def
}
var mult int64
switch {
case strings.HasSuffix(s, "KiB"):
mult, s = 1<<10, s[:len(s)-3]
case strings.HasSuffix(s, "MiB"):
mult, s = 1<<20, s[:len(s)-3]
case strings.HasSuffix(s, "GiB"):
mult, s = 1<<30, s[:len(s)-3]
default:
mult = 1
}
n, err := strconv.ParseInt(s, 10, 64)
if err != nil || n <= 0 {
return def
}
return n * mult
}