Keep circuit-breaker writes in state order and flush them before the database closes.

A later update can no longer be queued behind an older one, a failed save is retried, and shutdown waits for the last cooling window.
This commit is contained in:
protonmatter
2026-11-17 22:39:53 +01:00
committed by BaaRai
parent 847e32407a
commit 1fbf2263a1
6 changed files with 301 additions and 98 deletions
+3
View File
@@ -142,6 +142,9 @@ func run() int {
if drainErr != nil {
log.Printf("task drain: %v", drainErr)
}
// The health writer flushes on ctx.Done(). Wait for it before the deferred
// store close, so the last cooling window is not written at a closed database.
srv.FlushLLMHealth()
if httpErr != nil || drainErr != nil {
return 1
}
+13 -16
View File
@@ -46,7 +46,9 @@ type Registry struct {
m map[int64]*State
// persist / forget mirror state to the DB so a cooling-off window survives a
// restart. Both may be nil (no DB); both are called off the hot path.
// restart. Both may be nil (no DB). Both run while r.mu is held, so a later
// change to the same profile cannot enqueue ahead of this one. They must not
// call back into the registry.
persist func(id int64, st State)
forget func(id int64)
@@ -118,16 +120,14 @@ func (r *Registry) IsOpen(id int64) bool { return r.Get(id).Open() }
// recovers is fully trusted again (and drops the persisted row).
func (r *Registry) Pass(id int64) {
r.mu.Lock()
defer r.mu.Unlock()
st := r.m[id]
if st == nil || (st.Fails == 0 && st.Trips == 0 && st.OpenUntil.IsZero()) {
r.mu.Unlock()
return // already clean — nothing to write
return // already clean; nothing to write
}
delete(r.m, id)
forget := r.forget
r.mu.Unlock()
if forget != nil {
forget(id)
if r.forget != nil {
r.forget(id)
}
}
@@ -137,6 +137,7 @@ func (r *Registry) Pass(id int64) {
// true when this call is what opened the breaker, so the caller can log it once.
func (r *Registry) Trip(id int64, errMsg string, hard bool) (tripped bool) {
r.mu.Lock()
defer r.mu.Unlock()
st := r.m[id]
if st == nil {
st = &State{}
@@ -152,11 +153,8 @@ func (r *Registry) Trip(id int64, errMsg string, hard bool) (tripped bool) {
st.Fails = 0 // counted into this trip; start fresh for the half-open probe
tripped = true
}
snap := *st
persist := r.persist
r.mu.Unlock()
if persist != nil {
persist(id, snap)
if r.persist != nil {
r.persist(id, *st)
}
return tripped
}
@@ -164,11 +162,10 @@ func (r *Registry) Trip(id int64, errMsg string, hard bool) (tripped bool) {
// Reset clears one profile's state — the UI's "Recover now" action.
func (r *Registry) Reset(id int64) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.m, id)
forget := r.forget
r.mu.Unlock()
if forget != nil {
forget(id)
if r.forget != nil {
r.forget(id)
}
}
+51
View File
@@ -0,0 +1,51 @@
package llmpool
import (
"sync/atomic"
"testing"
"time"
)
// persist and forget run under the registry lock. A second update on the same
// profile cannot enqueue until the first callback returns, so the queue cannot
// observe the later state first.
func TestHealthCallbacksStayInStateOrder(t *testing.T) {
var entered atomic.Int32
firstIn := make(chan struct{})
release := make(chan struct{})
block := func() {
if entered.Add(1) == 1 {
close(firstIn)
<-release
}
}
reg := NewRegistry(
func(int64, State) { block() },
func(int64) { block() },
)
go reg.Trip(1, "down", true)
select {
case <-firstIn:
case <-time.After(2 * time.Second):
t.Fatal("Trip did not reach persist")
}
passed := make(chan struct{})
go func() {
reg.Pass(1)
close(passed)
}()
select {
case <-passed:
t.Fatal("Pass enqueued while Trip's persist was still running")
case <-time.After(50 * time.Millisecond):
}
close(release)
select {
case <-passed:
case <-time.After(2 * time.Second):
t.Fatal("Pass did not finish after Trip released the lock")
}
if entered.Load() != 2 {
t.Fatalf("callbacks=%d, want Trip then Pass", entered.Load())
}
}
+136 -46
View File
@@ -1,6 +1,7 @@
package server
import (
"context"
"log"
"sync"
"time"
@@ -18,13 +19,14 @@ import (
// - Circuit-breaker state is process-wide (s.llmHealth); rebuilding the pool does not clear it.
// newLLMHealthRegistry builds the process-wide circuit-breaker registry, mirroring
// state into PG so a cooling-off window survives a restart. Writes are async and
// best-effort — the in-memory copy is authoritative.
func newLLMHealthRegistry(pg *db.DB) *llmpool.Registry {
// state into PG so a cooling-off window survives a restart. Writes are async.
// The in-memory copy is authoritative. The returned syncer is nil when there is
// no database; otherwise it flushes when ctx is cancelled.
func newLLMHealthRegistry(ctx context.Context, pg *db.DB) (*llmpool.Registry, *healthSync) {
if pg == nil {
return llmpool.NewRegistry(nil, nil)
return llmpool.NewRegistry(nil, nil), nil
}
syncer := newHealthSync(pg.SaveLLMHealth, pg.ClearLLMHealth)
syncer := newHealthSync(ctx, pg.SaveLLMHealth, pg.ClearLLMHealth)
persist := func(id int64, st llmpool.State) {
syncer.enqueue(id, healthOp{st: st})
}
@@ -44,11 +46,13 @@ func newLLMHealthRegistry(pg *db.DB) *llmpool.Registry {
log.Printf("[llmpool] restored circuit-breaker state: profile #%d cooling until %s", h.ProfileID, st.OpenUntil.Format(time.RFC3339))
}
}
return reg
return reg, syncer
}
// healthOp is the latest circuit-breaker write for one profile. clear drops the row.
// healthOp is one circuit-breaker write for a profile. clear drops the row.
// seq orders writes for that profile: a newer op wins over one still in flight.
type healthOp struct {
seq uint64
clear bool
st llmpool.State
}
@@ -56,35 +60,67 @@ type healthOp struct {
// healthSync writes circuit-breaker state from one goroutine. A newer update for
// the same profile replaces one that has not been written yet, so a burst of
// state changes becomes one database call and the request path does not wait.
// save and clear must not call enqueue.
type healthSync struct {
save func(db.LLMHealth) error
clear func(int64) error
mu sync.Mutex
pending map[int64]healthOp
wake chan struct{}
save func(db.LLMHealth) error
clear func(int64) error
retryAfter time.Duration // tests set a short delay; zero uses one second
mu sync.Mutex
pending map[int64]healthOp
applied map[int64]uint64
seq uint64
stopped bool
wake chan struct{}
done chan struct{}
}
func newHealthSync(save func(db.LLMHealth) error, clear func(int64) error) *healthSync {
func newHealthSync(ctx context.Context, save func(db.LLMHealth) error, clear func(int64) error) *healthSync {
if ctx == nil {
ctx = context.Background()
}
h := &healthSync{
save: save,
clear: clear,
pending: map[int64]healthOp{},
applied: map[int64]uint64{},
wake: make(chan struct{}, 1),
done: make(chan struct{}),
}
go h.loop()
go h.loop(ctx)
return h
}
// FlushLLMHealth waits until the circuit-breaker writer has flushed the ops
// queued before shutdown. Call it after the server context is cancelled and
// before the database closes.
func (s *Server) FlushLLMHealth() {
if s == nil || s.llmHealthSync == nil {
return
}
select {
case <-s.llmHealthSync.done:
case <-time.After(5 * time.Second):
log.Printf("[llmpool] timed out flushing circuit-breaker state")
}
}
func (h *healthSync) enqueue(id int64, op healthOp) {
h.mu.Lock()
if h.pending == nil {
h.pending = map[int64]healthOp{}
h.seq++
op.seq = h.seq
if h.stopped {
h.mu.Unlock()
if err := h.apply(id, op); err != nil {
log.Printf("[llmpool] write circuit-breaker profile #%d: %v", id, err)
}
return
}
if prev, ok := h.pending[id]; ok && prev.seq > op.seq {
h.mu.Unlock()
return
}
h.pending[id] = op
h.mu.Unlock()
if h.wake == nil {
return
}
select {
case h.wake <- struct{}{}:
default:
@@ -102,34 +138,88 @@ func (h *healthSync) take() map[int64]healthOp {
return out
}
func (h *healthSync) loop() {
for {
batch := h.take()
if len(batch) == 0 {
<-h.wake
continue
func (h *healthSync) restore(id int64, op healthOp) {
h.mu.Lock()
defer h.mu.Unlock()
if h.stopped || h.applied[id] >= op.seq {
return
}
if cur, ok := h.pending[id]; ok && cur.seq >= op.seq {
return
}
h.pending[id] = op
}
func (h *healthSync) apply(id int64, op healthOp) error {
h.mu.Lock()
defer h.mu.Unlock()
if op.seq > 0 && h.applied[id] >= op.seq {
return nil
}
var err error
if op.clear {
if h.clear != nil {
err = h.clear(id)
}
for id, op := range batch {
if op.clear {
if h.clear == nil {
continue
}
if err := h.clear(id); err != nil {
log.Printf("[llmpool] clear circuit-breaker profile #%d: %v", id, err)
}
continue
}
if h.save == nil {
continue
}
row := db.LLMHealth{ProfileID: id, Fails: op.st.Fails, Trips: op.st.Trips, LastError: op.st.LastError}
if !op.st.OpenUntil.IsZero() {
openUntil := op.st.OpenUntil
row.OpenUntil = &openUntil
}
if err := h.save(row); err != nil {
log.Printf("[llmpool] persist circuit-breaker profile #%d: %v", id, err)
}
} else if h.save != nil {
row := db.LLMHealth{ProfileID: id, Fails: op.st.Fails, Trips: op.st.Trips, LastError: op.st.LastError}
if !op.st.OpenUntil.IsZero() {
openUntil := op.st.OpenUntil
row.OpenUntil = &openUntil
}
err = h.save(row)
}
if err != nil {
return err
}
if h.applied[id] < op.seq {
h.applied[id] = op.seq
}
return nil
}
func (h *healthSync) write(batch map[int64]healthOp) (failed bool) {
for id, op := range batch {
if err := h.apply(id, op); err != nil {
log.Printf("[llmpool] write circuit-breaker profile #%d: %v", id, err)
h.restore(id, op)
failed = true
}
}
return failed
}
func (h *healthSync) finish() {
h.mu.Lock()
h.stopped = true
batch := h.pending
h.pending = map[int64]healthOp{}
h.mu.Unlock()
for id, op := range batch {
if err := h.apply(id, op); err != nil {
log.Printf("[llmpool] write circuit-breaker profile #%d: %v", id, err)
}
}
}
func (h *healthSync) loop(ctx context.Context) {
defer close(h.done)
for {
failed := h.write(h.take())
delay := h.retryAfter
if delay <= 0 {
delay = time.Second
}
var retry <-chan time.Time
if failed {
retry = time.After(delay)
}
select {
case <-ctx.Done():
h.finish()
return
case <-h.wake:
case <-retry:
}
}
}
+93 -34
View File
@@ -1,51 +1,110 @@
package server
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/artex/llmpool"
)
func TestHealthSyncCoalescesToLatest(t *testing.T) {
h := &healthSync{pending: map[int64]healthOp{}}
h.enqueue(7, healthOp{st: llmpool.State{Fails: 1}})
h.enqueue(7, healthOp{st: llmpool.State{Fails: 4}})
func TestHealthSyncLoopWritesLatest(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var mu sync.Mutex
saved := map[int64]db.LLMHealth{}
cleared := map[int64]int{}
savedCh := make(chan struct{}, 4)
clearedCh := make(chan struct{}, 4)
h := newHealthSync(ctx,
func(row db.LLMHealth) error {
mu.Lock()
saved[row.ProfileID] = row
mu.Unlock()
savedCh <- struct{}{}
return nil
},
func(id int64) error {
mu.Lock()
cleared[id]++
delete(saved, id)
mu.Unlock()
clearedCh <- struct{}{}
return nil
},
)
openUntil := time.Now().Add(time.Minute).UTC().Truncate(time.Second)
h.enqueue(8, healthOp{st: llmpool.State{Fails: 1, LastError: "first"}})
h.enqueue(8, healthOp{st: llmpool.State{Fails: 4, Trips: 1, OpenUntil: openUntil, LastError: "down"}})
deadline := time.After(2 * time.Second)
var latest db.LLMHealth
for {
mu.Lock()
latest = saved[8]
mu.Unlock()
if latest.Fails == 4 && latest.OpenUntil != nil && latest.OpenUntil.Equal(openUntil) {
break
}
select {
case <-savedCh:
case <-deadline:
t.Fatalf("loop saved=%+v, want the latest trip", latest)
}
}
h.enqueue(7, healthOp{clear: true})
h.enqueue(8, healthOp{st: llmpool.State{Fails: 2}})
batch := h.take()
if len(batch) != 2 || !batch[7].clear || batch[8].st.Fails != 2 {
t.Fatalf("batch=%+v", batch)
select {
case <-clearedCh:
case <-time.After(2 * time.Second):
t.Fatal("loop did not clear")
}
if again := h.take(); again != nil {
t.Fatalf("pending was not cleared: %+v", again)
mu.Lock()
defer mu.Unlock()
got := saved[8]
if got.Fails != 4 || got.Trips != 1 || got.LastError != "down" || got.OpenUntil == nil || !got.OpenUntil.Equal(openUntil) {
t.Fatalf("saved=%+v, want the latest trip including OpenUntil", got)
}
var saved []db.LLMHealth
var cleared []int64
worker := &healthSync{
save: func(row db.LLMHealth) error {
saved = append(saved, row)
if cleared[7] != 1 {
t.Fatalf("cleared=%v", cleared)
}
}
func TestHealthSyncRetriesFailedSave(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var mu sync.Mutex
calls := 0
done := make(chan struct{})
h := newHealthSync(ctx,
func(row db.LLMHealth) error {
mu.Lock()
calls++
n := calls
mu.Unlock()
if n == 1 {
return errors.New("database down")
}
if n == 2 {
close(done)
}
return nil
},
clear: func(id int64) error {
cleared = append(cleared, id)
return nil
},
}
worker.enqueue(7, healthOp{clear: true})
worker.enqueue(8, healthOp{st: llmpool.State{Fails: 3, LastError: "down"}})
for id, op := range worker.take() {
if op.clear {
if err := worker.clear(id); err != nil {
t.Fatal(err)
}
continue
}
if err := worker.save(db.LLMHealth{ProfileID: id, Fails: op.st.Fails, LastError: op.st.LastError}); err != nil {
t.Fatal(err)
}
func(int64) error { return nil },
)
h.retryAfter = 20 * time.Millisecond
h.enqueue(3, healthOp{st: llmpool.State{Fails: 2, LastError: "blip"}})
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("failed save was not retried")
}
if len(cleared) != 1 || cleared[0] != 7 || len(saved) != 1 || saved[0].ProfileID != 8 || saved[0].Fails != 3 {
t.Fatalf("cleared=%v saved=%+v", cleared, saved)
mu.Lock()
defer mu.Unlock()
if calls < 2 {
t.Fatalf("save calls=%d, want a retry", calls)
}
}
+5 -2
View File
@@ -117,7 +117,9 @@ type Server struct {
// It deliberately lives OUTSIDE the provider caches: rebuilding the chain
// (saving an unrelated profile, flipping a setting) must not erase what we
// learned about which backends are out of credit / rate-limited.
llmHealth *llmpool.Registry
// llmHealthSync flushes that state when ctx is cancelled, before the database closes.
llmHealth *llmpool.Registry
llmHealthSync *healthSync
// Explicit task chains use one task-scoped dynamic provider shared by goals,
// planner, workers, and the main agent. Bundles are stable; their runtime reads
@@ -163,9 +165,10 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
chatCancel: map[string]context.CancelCauseFunc{}, triggerQ: map[string][]triggeredRun{},
triggerActive: map[string]int{}, triggerCfg: map[string]triggerBehavior{},
profChatAgents: map[int64]*agent.ChatAgent{},
provByProfile: map[int64]*provEntry{}, llmHealth: newLLMHealthRegistry(m.pg),
provByProfile: map[int64]*provEntry{},
taskAgents: map[string]*taskAgentBundle{}, archiveWake: make(chan struct{}, 1),
loginLimit: newLoginLimiter(time.Now)}
s.llmHealth, s.llmHealthSync = newLLMHealthRegistry(ctx, m.pg)
for _, o := range opts {
o(s)
}