carlos / platform Public

fix: back off pending pool withdrawal before fresh reads

Co-Authored-By: GPT-6 Astra <noreply@openai.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 25cfd94d2f2c921da59494bd6e18ecae2ab9bfe5 parent 596127d
5 files changed, +152 −2
  • internal/host/agent.go +3 −0
  • internal/host/poolwithdrawal.go +1 −0
  • internal/provision/poolwithdrawal.go +75 −0
  • internal/provision/poolwithdrawal_test.go +70 −0
  • internal/provision/reconcile.go +3 −2
diff --git a/internal/host/agent.go b/internal/host/agent.go
index 08a1c5a67..3190cf693 100644
--- a/internal/host/agent.go
+++ b/internal/host/agent.go
@@ -2063,6 +2063,9 @@ func reconcileSerializedAuth(ctx context.Context, s release.Store, reg *registry
if cfg.WithdrawPool == nil {
cfg.WithdrawPool = preparePoolWithdrawal
cfg.PoolWithdrawn = finishPoolWithdrawal
+ if cfg.WithdrawalBackoff == nil {
+ cfg.WithdrawalBackoff = &poolWithdrawalBackoff
+ }
}
start := auth.passBegin()
defer auth.passEnd() // after the sidecar pass's writes too
diff --git a/internal/host/poolwithdrawal.go b/internal/host/poolwithdrawal.go
index c3d2c9ff8..f0b48cd5c 100644
--- a/internal/host/poolwithdrawal.go
+++ b/internal/host/poolwithdrawal.go
@@ -10,6 +10,7 @@ import (
)
var armedPoolWithdrawal atomic.Pointer[activator.Service]
+var poolWithdrawalBackoff provision.PoolWithdrawalBackoff
func preparePoolWithdrawal(ctx context.Context, rt registry.Route) error {
svc := armedPoolWithdrawal.Load()
diff --git a/internal/provision/poolwithdrawal.go b/internal/provision/poolwithdrawal.go
index df6f199a0..55a82adfe 100644
--- a/internal/provision/poolwithdrawal.go
+++ b/internal/provision/poolwithdrawal.go
@@ -2,10 +2,13 @@ package provision
import (
"context"
+ "encoding/json"
"errors"
"fmt"
"log"
"strings"
+ "sync"
+ "time"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
@@ -89,10 +92,20 @@ func (cfg ReconcileConfig) withdrawPools(ctx context.Context, s release.Store, r
log.Printf("ha_pool_withdrawal_pending code=registry err=%v", err)
return
}
+ now := time.Now()
+ if cfg.Now != nil {
+ now = cfg.Now()
+ }
+ pools := string(cfg.ServedPools())
+ cfg.WithdrawalBackoff.prune(routes, pools)
for _, rt := range routes {
if !cfg.poolWithdrawalScope(rt, sidecars) {
continue
}
+ if cfg.WithdrawalBackoff.deferred(rt, pools, now) {
+ continue
+ }
+ row := rt
code := ""
var err error
switch {
@@ -138,6 +151,7 @@ func (cfg ReconcileConfig) withdrawPools(ctx context.Context, s release.Store, r
log.Printf("ha_pool_withdrawn host=%s", rt.Host)
}
if code != "" {
+ cfg.WithdrawalBackoff.pending(row, pools, now)
if err == nil {
err = PoolWithdrawalPending(code)
}
@@ -159,3 +173,64 @@ func (e *poolWithdrawalPendingError) Unwrap() error { return errPoolWithdrawalPe
func PoolWithdrawalPending(code string) error {
return &poolWithdrawalPendingError{code: code}
}
+
+type PoolWithdrawalBackoff struct {
+ mu sync.Mutex
+ hosts map[string]poolWithdrawalRetry
+}
+
+type poolWithdrawalRetry struct {
+ row registry.Route
+ pools string
+ after time.Time
+}
+
+func poolWithdrawalRetryRow(rt registry.Route) registry.Route {
+ var state map[string]json.RawMessage
+ if json.Unmarshal([]byte(rt.BuildState), &state) == nil && state != nil {
+ delete(state, "read_at")
+ if body, err := json.Marshal(state); err == nil {
+ rt.BuildState = string(body)
+ }
+ }
+ return rt
+}
+
+func (b *PoolWithdrawalBackoff) prune(routes []registry.Route, pools string) {
+ if b == nil {
+ return
+ }
+ rows := make(map[string]registry.Route, len(routes))
+ for _, rt := range routes {
+ rows[rt.Host] = poolWithdrawalRetryRow(rt)
+ }
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ for host, retry := range b.hosts {
+ if row, ok := rows[host]; !ok || row != retry.row || pools != retry.pools {
+ delete(b.hosts, host)
+ }
+ }
+}
+
+func (b *PoolWithdrawalBackoff) deferred(rt registry.Route, pools string, now time.Time) bool {
+ if b == nil {
+ return false
+ }
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ r, ok := b.hosts[rt.Host]
+ return ok && r.row == poolWithdrawalRetryRow(rt) && r.pools == pools && now.Before(r.after)
+}
+
+func (b *PoolWithdrawalBackoff) pending(rt registry.Route, pools string, now time.Time) {
+ if b == nil {
+ return
+ }
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ if b.hosts == nil {
+ b.hosts = make(map[string]poolWithdrawalRetry)
+ }
+ b.hosts[rt.Host] = poolWithdrawalRetry{row: poolWithdrawalRetryRow(rt), pools: pools, after: now.Add(5 * time.Minute)}
+}
diff --git a/internal/provision/poolwithdrawal_test.go b/internal/provision/poolwithdrawal_test.go
index 77ba06e34..79e1bb888 100644
--- a/internal/provision/poolwithdrawal_test.go
+++ b/internal/provision/poolwithdrawal_test.go
@@ -10,6 +10,7 @@ import (
"time"
"github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
)
func TestPoolWithdrawalRequiresCompleteProof(t *testing.T) {
@@ -184,3 +185,72 @@ func TestPoolWithdrawalLogsCause(t *testing.T) {
})
}
}
+
+func TestPoolWithdrawalPendingBackoffBeforeReads(t *testing.T) {
+ for _, code := range []string{"duties", "ownership", "release", "stopping"} {
+ t.Run(code, func(t *testing.T) {
+ rt := registry.Route{Host: "shop.example.com", Account: "bbw", App: "shop", Kind: "instance", Status: "ready", Provisioned: true, Hibernate: true, Pool: "old", Region: "ie", BuildState: `{"read_at":"before","version":"v1"}`}
+ reg := regWith(t, rt)
+ defer reg.Close()
+ store := dirStore(t)
+ if err := CreateRecord(ctx, store, Record{Host: rt.Host, Account: rt.Account, App: rt.App, Pool: rt.Pool, Region: rt.Region, Channel: "stable"}); err != nil {
+ t.Fatal(err)
+ }
+ s := &withdrawalCountingStore{Store: store}
+ now := time.Now()
+ proofs := 0
+ cfg := ReconcileConfig{Region: "ie", Pools: []string{"new"}, Now: func() time.Time { return now }, WithdrawalBackoff: &PoolWithdrawalBackoff{}, WithdrawPool: func(context.Context, registry.Route) error { proofs++; return PoolWithdrawalPending(code) }}
+ cfg.withdrawPools(ctx, s, reg, false)
+ before := s.reads
+ if before == 0 || proofs != 1 {
+ t.Fatalf("reads=%d proofs=%d", before, proofs)
+ }
+ for i := 0; i < 4; i++ {
+ now = now.Add(time.Minute)
+ rt.BuildState = fmt.Sprintf(`{"read_at":"%d","version":"v1"}`, i)
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ cfg.withdrawPools(ctx, s, reg, false)
+ }
+ if s.reads != before || proofs != 1 {
+ t.Fatalf("backoff read S3: reads=%d -> %d proofs=%d", before, s.reads, proofs)
+ }
+ now = now.Add(time.Minute)
+ cfg.withdrawPools(ctx, s, reg, false)
+ if proofs != 2 {
+ t.Fatalf("expiry proofs=%d", proofs)
+ }
+ rt.NextDue = 1
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ cfg.withdrawPools(ctx, s, reg, false)
+ if proofs != 3 {
+ t.Fatalf("row change proofs=%d", proofs)
+ }
+ cfg.Pools = []string{"another"}
+ cfg.withdrawPools(ctx, s, reg, false)
+ if proofs != 4 {
+ t.Fatalf("membership change proofs=%d", proofs)
+ }
+ cfg.Pools = []string{"old"}
+ cfg.withdrawPools(ctx, s, reg, false)
+ cfg.Pools = []string{"another"}
+ cfg.withdrawPools(ctx, s, reg, false)
+ if proofs != 5 {
+ t.Fatalf("eligibility reset proofs=%d", proofs)
+ }
+ })
+ }
+}
+
+type withdrawalCountingStore struct {
+ release.Store
+ reads int
+}
+
+func (s *withdrawalCountingStore) Get(ctx context.Context, key string) ([]byte, string, error) {
+ s.reads++
+ return s.Store.Get(ctx, key)
+}
diff --git a/internal/provision/reconcile.go b/internal/provision/reconcile.go
index e7e4799d0..45970398e 100644
--- a/internal/provision/reconcile.go
+++ b/internal/provision/reconcile.go
@@ -36,8 +36,9 @@ import (
// injection-seam shape domains.Config uses (tests supply fixtures, the host
// agent supplies production values).
type ReconcileConfig struct {
- WithdrawPool func(context.Context, registry.Route) error
- PoolWithdrawn func()
+ WithdrawPool func(context.Context, registry.Route) error
+ PoolWithdrawn func()
+ WithdrawalBackoff *PoolWithdrawalBackoff
// HAIngress receives the complete pre-pool record scan; errors revoke its evidence.
HAIngress func([]Record, error)