feat: runtime bootstrap snapshot cache with backoff

This commit is contained in:
Noel Sabia committed 2026-11-14 06:08:53 +01:00
1 parent 08f60a14d1
commit aa6bb23e24
19 files changed
+1400 -384

No files matched your search

+357 -32
View File
@@ -10,18 +10,77 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"time"
"pulsecast/api/internal/cache"
"pulsecast/api/internal/config"
"pulsecast/api/internal/ml"
"pulsecast/api/internal/model"
"pulsecast/api/internal/sse"
"pulsecast/api/internal/store"
)
const fixtureDir = "../../testdata/data"
const fixtureBootstrap = "../../testdata/data/bootstrap.json"
type stubFetcher struct {
mu sync.RWMutex
payload model.Bootstrap
err error
refreshFn func(context.Context) (model.Bootstrap, error)
}
func (s *stubFetcher) Bootstrap(ctx context.Context) (model.Bootstrap, error) {
s.mu.RLock()
fn := s.refreshFn
payload, err := s.payload, s.err
s.mu.RUnlock()
if fn != nil {
return fn(ctx)
}
if err != nil {
return model.Bootstrap{}, err
}
return payload, nil
}
func (s *stubFetcher) fail(err error) {
s.mu.Lock()
s.err = err
s.mu.Unlock()
}
func (s *stubFetcher) recover(payload model.Bootstrap) {
s.mu.Lock()
s.payload, s.err = payload, nil
s.mu.Unlock()
}
func loadFixtureBootstrap(t *testing.T) model.Bootstrap {
t.Helper()
raw, err := os.ReadFile(fixtureBootstrap)
if err != nil {
t.Fatalf("read fixture: %v", err)
}
var payload model.Bootstrap
if err := json.Unmarshal(raw, &payload); err != nil {
t.Fatalf("decode fixture: %v", err)
}
return payload
}
func newFixtureCache(t *testing.T) (*cache.Cache, *stubFetcher) {
t.Helper()
payload := loadFixtureBootstrap(t)
fetcher := &stubFetcher{payload: payload}
snapshots := cache.New(fetcher, cache.Options{RefreshInterval: time.Hour, Timeout: time.Second})
if err := snapshots.Refresh(context.Background()); err != nil {
t.Fatalf("prime cache: %v", err)
}
return snapshots, fetcher
}
type stubML struct {
predict func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error)
@@ -58,20 +117,16 @@ func (s *stubML) TrainStatus(ctx context.Context, jobID string) (int, []byte, er
return s.trainStatus(ctx, jobID)
}
func loadFixtureStore(t *testing.T) *store.Store {
func newTestServer(t *testing.T, client MLClient, mutate func(*config.Config)) (*Server, *httptest.Server) {
t.Helper()
st, err := store.Load(fixtureDir)
if err != nil {
t.Fatalf("load fixtures: %v", err)
}
return st
snapshots, _ := newFixtureCache(t)
return newTestServerWithCache(t, snapshots, client, mutate)
}
func newTestServer(t *testing.T, client MLClient, mutate func(*config.Config)) (*Server, *httptest.Server) {
func newTestServerWithCache(t *testing.T, snapshots *cache.Cache, client MLClient, mutate func(*config.Config)) (*Server, *httptest.Server) {
t.Helper()
cfg := config.Config{
Addr: ":0",
DataDir: fixtureDir,
FastAPIURL: "http://127.0.0.1:1",
Heartbeat: time.Hour,
MLTimeout: 100 * time.Millisecond,
@@ -85,12 +140,47 @@ func newTestServer(t *testing.T, client MLClient, mutate func(*config.Config)) (
t.Cleanup(cancel)
go hub.Run(ctx)
srv := New(cfg, loadFixtureStore(t), client, hub)
srv := New(cfg, snapshots, client, hub)
ts := httptest.NewServer(srv.Handler())
t.Cleanup(ts.Close)
return srv, ts
}
func forecastPoints(n int) []model.ForecastPoint {
start := time.Date(2026, 11, 16, 9, 0, 0, 0, time.UTC)
points := make([]model.ForecastPoint, n)
for i := range points {
points[i] = model.ForecastPoint{
T: start.Add(time.Duration(i) * time.Hour),
V: float64(10 + i),
Lo: float64(5 + i),
Hi: float64(15 + i),
}
}
return points
}
// weekForecast builds seven complete store-hour days (10:00-20:00 Berlin),
// which is what the staffing planner consumes.
func weekForecast(t *testing.T) []model.ForecastPoint {
t.Helper()
loc, err := time.LoadLocation(model.StoreHours.TZ)
if err != nil {
t.Fatal(err)
}
start := time.Date(2026, 11, 16, model.StoreHours.Open, 0, 0, 0, loc)
var points []model.ForecastPoint
for day := 0; day < 7; day++ {
for hour := 0; hour < model.StoreHours.Close-model.StoreHours.Open; hour++ {
points = append(points, model.ForecastPoint{
T: start.AddDate(0, 0, day).Add(time.Duration(hour) * time.Hour).UTC(),
V: 25,
})
}
}
return points
}
func getJSON(t *testing.T, url string, dst any) *http.Response {
t.Helper()
resp, err := http.Get(url)
@@ -125,13 +215,11 @@ func TestHealthAndReady(t *testing.T) {
}
}
func TestReadyzWithoutData(t *testing.T) {
cfg := config.Config{Addr: ":0", DataDir: t.TempDir(), FastAPIURL: "http://127.0.0.1:1", Heartbeat: time.Hour, MLTimeout: 50 * time.Millisecond}
st, err := store.Load(cfg.DataDir)
if err != nil {
t.Logf("expected load error: %v", err)
}
srv := New(cfg, st, &stubML{}, sse.NewHub(time.Hour))
func TestReadyzWithoutSnapshot(t *testing.T) {
cfg := config.Config{Addr: ":0", FastAPIURL: "http://127.0.0.1:1", Heartbeat: time.Hour, MLTimeout: 50 * time.Millisecond}
fetcher := &stubFetcher{err: errors.New("fastapi unavailable")}
snapshots := cache.New(fetcher, cache.Options{RefreshInterval: time.Hour, Timeout: time.Second})
srv := New(cfg, snapshots, &stubML{}, sse.NewHub(time.Hour))
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
@@ -143,6 +231,70 @@ func TestReadyzWithoutData(t *testing.T) {
if ready["ok"] || ready["data"] || ready["model"] {
t.Fatalf("readyz body=%v, want all false", ready)
}
// One failed bootstrap attempt must not make the cache ready.
if err := snapshots.Refresh(context.Background()); err == nil {
t.Fatal("expected the bootstrap refresh to fail")
}
resp = getJSON(t, ts.URL+"/api/readyz", &ready)
if resp.StatusCode != http.StatusServiceUnavailable || ready["ok"] {
t.Fatalf("readyz after failed refresh: status=%d body=%v", resp.StatusCode, ready)
}
}
func TestReadyzSurvivesFastAPIFailureAfterFirstSnapshot(t *testing.T) {
snapshots, fetcher := newFixtureCache(t)
cfg := config.Config{Addr: ":0", FastAPIURL: "http://127.0.0.1:1", Heartbeat: time.Hour, MLTimeout: 50 * time.Millisecond}
srv := New(cfg, snapshots, &stubML{}, sse.NewHub(time.Hour))
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
var ready map[string]bool
resp := getJSON(t, ts.URL+"/api/readyz", &ready)
if resp.StatusCode != http.StatusOK || !ready["ok"] || !ready["data"] || !ready["model"] {
t.Fatalf("readyz status=%d body=%v", resp.StatusCode, ready)
}
fetcher.fail(errors.New("connection refused"))
if err := snapshots.Refresh(context.Background()); err == nil {
t.Fatal("expected the refresh to fail")
}
resp = getJSON(t, ts.URL+"/api/readyz", &ready)
if resp.StatusCode != http.StatusOK || !ready["ok"] || !ready["data"] || !ready["model"] {
t.Fatalf("readyz after FastAPI failure: status=%d body=%v, want 200 with the last snapshot", resp.StatusCode, ready)
}
var meta model.Meta
getJSON(t, ts.URL+"/api/meta", &meta)
if meta.Cache.Failures != 1 || meta.Cache.LastError == nil ||
!strings.Contains(*meta.Cache.LastError, "connection refused") {
t.Fatalf("cache status=%+v", meta.Cache)
}
if meta.Cache.LastSuccessAt == nil || meta.Cache.NextRetryAt == nil {
t.Fatalf("cache timestamps missing: %+v", meta.Cache)
}
// The context endpoint comes from the snapshot; it is now stale.
var ctx model.ContextResponse
getJSON(t, ts.URL+"/api/context", &ctx)
if !ctx.Cached {
t.Error("context cached=false after a failed refresh, want true")
}
// A later success clears the failure state.
fetcher.recover(loadFixtureBootstrap(t))
if err := snapshots.Refresh(context.Background()); err != nil {
t.Fatalf("recovery refresh: %v", err)
}
getJSON(t, ts.URL+"/api/meta", &meta)
if meta.Cache.Failures != 0 || meta.Cache.LastError != nil || meta.Cache.NextRetryAt != nil {
t.Fatalf("cache status after recovery=%+v", meta.Cache)
}
getJSON(t, ts.URL+"/api/context", &ctx)
if ctx.Cached {
t.Error("context cached=true after a successful refresh, want false")
}
}
func TestMetaSensorsAndVersionHeaders(t *testing.T) {
@@ -156,6 +308,10 @@ func TestMetaSensorsAndVersionHeaders(t *testing.T) {
if meta.Coverage.Sensors != 8 || meta.ModelVersion == "" || meta.ContextVersion == "" {
t.Fatalf("meta=%+v", meta)
}
if meta.Cache.Source != "fastapi" || meta.Cache.LastSuccessAt == nil ||
meta.Cache.LastError != nil || meta.Cache.NextRetryAt != nil || meta.Cache.Failures != 0 {
t.Fatalf("meta cache=%+v", meta.Cache)
}
for _, header := range []string{"X-Data-Version", "X-Model-Version", "X-Context-Version"} {
if resp.Header.Get(header) == "" {
t.Errorf("missing %s header", header)
@@ -180,6 +336,58 @@ func TestMetaSensorsAndVersionHeaders(t *testing.T) {
}
}
func TestDataEndpointsFollowSnapshotRefresh(t *testing.T) {
snapshots, fetcher := newFixtureCache(t)
_, ts := newTestServerWithCache(t, snapshots, &stubML{}, nil)
var sensors []model.Sensor
getJSON(t, ts.URL+"/api/sensors", &sensors)
if len(sensors) != 8 {
t.Fatalf("sensors=%d, want 8", len(sensors))
}
updated := loadFixtureBootstrap(t)
updated.DataVersion = "2026-11-14T05:00:00Z"
updated.ContextVersion = "2026-11-14T05:00:00Z"
updated.Sensors = updated.Sensors[:1]
updated.Series = map[string][]model.Point{
updated.Sensors[0].ID: {{T: time.Date(2026, 11, 1, 10, 0, 0, 0, time.UTC), V: 42}},
}
fetcher.recover(updated)
if err := snapshots.Refresh(context.Background()); err != nil {
t.Fatalf("refresh: %v", err)
}
sensors = nil
resp := getJSON(t, ts.URL+"/api/sensors", &sensors)
if len(sensors) != 1 || sensors[0].ID != updated.Sensors[0].ID {
t.Fatalf("sensors after refresh=%+v", sensors)
}
if got := resp.Header.Get("X-Data-Version"); got != updated.DataVersion {
t.Errorf("X-Data-Version=%q, want %q", got, updated.DataVersion)
}
var series struct {
Points []model.Point `json:"points"`
}
getJSON(t, ts.URL+"/api/series?sensor="+updated.Sensors[0].ID+"&step=15m", &series)
if len(series.Points) != 1 || series.Points[0].V != 42 {
t.Fatalf("series after refresh=%+v", series.Points)
}
var ctx model.ContextResponse
getJSON(t, ts.URL+"/api/context", &ctx)
if ctx.UpdatedAt.IsZero() || ctx.Cached {
t.Fatalf("context after refresh=%+v", ctx)
}
var meta model.Meta
getJSON(t, ts.URL+"/api/meta", &meta)
if meta.DataVersion != updated.DataVersion || meta.Cache.Failures != 0 {
t.Fatalf("meta after refresh=%+v", meta)
}
}
func TestSeriesRangeStepAndErrors(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
@@ -241,37 +449,96 @@ func TestContextEndpoint(t *testing.T) {
if ctx.UpdatedAt.IsZero() {
t.Error("expected updated_at")
}
if ctx.Cached {
t.Error("cached=true while the last snapshot refresh succeeded, want false")
}
}
func TestForecastFallsBackWhenMLIsDown(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
func TestForecastServesLastGoodWhenMLIsDown(t *testing.T) {
live := forecastPoints(168)
var down bool
var mu sync.Mutex
client := &stubML{predict: func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error) {
mu.Lock()
defer mu.Unlock()
if down {
return nil, errors.New("connection refused")
}
return live, nil
}}
_, ts := newTestServer(t, client, nil)
var forecast model.ForecastResponse
resp := getJSON(t, ts.URL+"/api/forecast?sensor=hn-bahnhof-01&horizon=7d", &forecast)
if resp.StatusCode != http.StatusOK {
t.Fatalf("forecast status=%d", resp.StatusCode)
t.Fatalf("live forecast status=%d", resp.StatusCode)
}
if !forecast.Cached {
if forecast.Cached || len(forecast.Points) != 168 {
t.Fatalf("live forecast cached=%v points=%d, want false/168", forecast.Cached, len(forecast.Points))
}
mu.Lock()
down = true
mu.Unlock()
var fallback model.ForecastResponse
resp = getJSON(t, ts.URL+"/api/forecast?sensor=hn-bahnhof-01&horizon=7d", &fallback)
if resp.StatusCode != http.StatusOK {
t.Fatalf("fallback status=%d", resp.StatusCode)
}
if !fallback.Cached {
t.Error("cached=false, want true when FastAPI is down")
}
if len(forecast.Points) != 168 {
t.Fatalf("points=%d, want 168", len(forecast.Points))
if len(fallback.Points) != 168 || fallback.Points[0].T != forecast.Points[0].T ||
fallback.Points[0].V != forecast.Points[0].V {
t.Fatalf("fallback points do not match the last good forecast: %+v", fallback.Points[0])
}
if forecast.Scenario != "base" || forecast.DataVersion == "" || forecast.ModelVersion == "" {
t.Fatalf("forecast=%+v", forecast)
if fallback.Scenario != "base" || fallback.DataVersion == "" || fallback.ModelVersion == "" {
t.Fatalf("forecast=%+v", fallback)
}
}
func TestForecastTimeoutFallsBack(t *testing.T) {
func TestForecastWithoutLastGoodFails503(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
var errBody model.ErrorBody
resp := getJSON(t, ts.URL+"/api/forecast?sensor=hn-bahnhof-01&horizon=7d", &errBody)
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("status=%d, want 503", resp.StatusCode)
}
if errBody.Error.Code != "forecast_unavailable" {
t.Fatalf("error=%+v", errBody)
}
}
func TestForecastTimeoutServesLastGood(t *testing.T) {
live := forecastPoints(48)
var mu sync.Mutex
slow := false
client := &stubML{predict: func(ctx context.Context, _ ml.PredictRequest) ([]model.ForecastPoint, error) {
<-ctx.Done()
return nil, ctx.Err()
mu.Lock()
block := slow
mu.Unlock()
if block {
<-ctx.Done()
return nil, ctx.Err()
}
return live, nil
}}
_, ts := newTestServer(t, client, func(cfg *config.Config) { cfg.MLTimeout = 20 * time.Millisecond })
var primed model.ForecastResponse
resp := getJSON(t, ts.URL+"/api/forecast?sensor=hn-stadtgalerie-01&horizon=48h&scenario=sunny-saturday-market", &primed)
if resp.StatusCode != http.StatusOK || primed.Cached {
t.Fatalf("prime forecast status=%d cached=%v", resp.StatusCode, primed.Cached)
}
mu.Lock()
slow = true
mu.Unlock()
start := time.Now()
var forecast model.ForecastResponse
resp := getJSON(t, ts.URL+"/api/forecast?sensor=hn-stadtgalerie-01&horizon=48h&scenario=sunny-saturday-market", &forecast)
resp = getJSON(t, ts.URL+"/api/forecast?sensor=hn-stadtgalerie-01&horizon=48h&scenario=sunny-saturday-market", &forecast)
if resp.StatusCode != http.StatusOK {
t.Fatalf("forecast status=%d", resp.StatusCode)
}
@@ -327,7 +594,11 @@ func TestForecastValidation(t *testing.T) {
}
func TestStaffingPlan(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
points := weekForecast(t)
client := &stubML{predict: func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error) {
return points, nil
}}
_, ts := newTestServer(t, client, nil)
start := time.Now()
var plan model.StaffingResponse
@@ -350,9 +621,12 @@ func TestStaffingPlan(t *testing.T) {
if plan.Summary.CoveragePct != 100 {
t.Errorf("coverage=%v, want 100", plan.Summary.CoveragePct)
}
if plan.Summary.CostEUR <= 0 || plan.Summary.FlatScheduleCostEUR <= 0 {
if plan.Summary.CostEUR != 2240 || plan.Summary.FlatScheduleCostEUR != 2240 {
t.Errorf("summary=%+v", plan.Summary)
}
if plan.Cached {
t.Error("cached=true, want false for a live plan")
}
if len(plan.Why) == 0 {
t.Error("expected a non-empty why list")
}
@@ -363,6 +637,57 @@ func TestStaffingPlan(t *testing.T) {
}
}
func TestStaffingFallsBackToLastGoodForecast(t *testing.T) {
points := weekForecast(t)
var mu sync.Mutex
down := false
client := &stubML{predict: func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error) {
mu.Lock()
defer mu.Unlock()
if down {
return nil, errors.New("connection refused")
}
return points, nil
}}
_, ts := newTestServer(t, client, nil)
var live model.StaffingResponse
resp := getJSON(t, ts.URL+"/api/staffing?sensor=hn-bahnhof-01&scenario=base&standard=standard", &live)
if resp.StatusCode != http.StatusOK || live.Cached {
t.Fatalf("live staffing status=%d cached=%v", resp.StatusCode, live.Cached)
}
mu.Lock()
down = true
mu.Unlock()
// Different standard -> different plan key, so the handler must rebuild
// from the last good 7d forecast.
var fallback model.StaffingResponse
resp = getJSON(t, ts.URL+"/api/staffing?sensor=hn-bahnhof-01&scenario=base&standard=premium", &fallback)
if resp.StatusCode != http.StatusOK {
t.Fatalf("fallback staffing status=%d", resp.StatusCode)
}
if !fallback.Cached {
t.Error("cached=false, want true when built from the last good forecast")
}
if len(fallback.Hours) != 70 || len(fallback.Shifts) != 21 {
t.Fatalf("fallback plan hours=%d shifts=%d", len(fallback.Hours), len(fallback.Shifts))
}
}
func TestStaffingUnavailableWithoutForecast(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
var errBody model.ErrorBody
resp := getJSON(t, ts.URL+"/api/staffing?sensor=hn-bahnhof-01&scenario=base&standard=standard", &errBody)
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("status=%d, want 503", resp.StatusCode)
}
if errBody.Error.Code != "staffing_unavailable" {
t.Fatalf("error=%+v", errBody)
}
}
func TestStaffingValidation(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
for _, tc := range []struct {