carlos / platform Public

ha: require stop and release proof before pool row removal

Fence withdrawn pool rows, poll normal shutdown and clean release evidence, and compare the unchanged row before deletion. Retain unsupported ownership and local duties, suppress withdrawn home advertisements, and preserve ingress forwarding.
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
claude agent, operated by paul@keymail.dev 662e4defbecb8f02f7a66bea20042b608c5fefd3 parent 8d129eb
15 files changed, +1,031 −1
  • internal/activator/activator.go +1 −0
  • internal/activator/poolwithdrawal.go +215 −0
  • internal/activator/poolwithdrawal_test.go +244 −0
  • internal/edge/forwardha_export_test.go +14 −1
  • internal/edge/poolwithdrawal_test.go +40 −0
  • internal/host/agent.go +4 −0
  • internal/host/edgesfile.go +4 −0
  • internal/host/poolwithdrawal.go +20 −0
  • internal/host/poolwithdrawal_test.go +74 −0
  • internal/provision/poolwithdrawal.go +141 −0
  • internal/provision/poolwithdrawal_test.go +153 −0
  • internal/provision/reconcile.go +3 −0
  • internal/provision/sidecar_reconcile.go +1 −0
  • internal/registry/poolwithdrawal.go +51 −0
  • internal/registry/poolwithdrawal_test.go +66 −0
diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index 1fc28acec..1a57b0161 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -1420,6 +1420,7 @@ type Service struct {
fenceOwners map[string]string
tokenPreparations map[string]chan struct{}
stopHistory map[string]*stopObservation
+ poolWithdrawals map[string]*poolWithdrawal
// straySidecars are legacy sidecars whose termination is unproven, per
// host (zero-downtime spec §7.4). Task 7 tracks into it; a stop keeps
// its lease while its host has one (releasable). Under a.mu.
diff --git a/internal/activator/poolwithdrawal.go b/internal/activator/poolwithdrawal.go
new file mode 100644
index 000000000..1a0099d5e
--- /dev/null
+++ b/internal/activator/poolwithdrawal.go
@@ -0,0 +1,215 @@
+package activator
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "io/fs"
+ "log"
+
+ "time"
+
+ "github.com/carlosframework/platform/internal/hafresh"
+ "github.com/carlosframework/platform/internal/instancectl"
+ "github.com/carlosframework/platform/internal/lease"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+type poolWithdrawal struct {
+ route registry.Route
+ owner string
+ done bool
+ err error
+ retryAt time.Time
+}
+
+func (a *Service) PreparePoolWithdrawal(ctx context.Context, rt registry.Route) error {
+ pending := provision.PoolWithdrawalPending
+ if ctx.Err() != nil {
+ return ctx.Err()
+ }
+ b := adoptionBackend(a.backend, rt)
+ if b == nil || b.Registry == nil || a.Routes == nil || !rt.PoolWithdrawalSupported() {
+ return pending("ownership")
+ }
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ current, err := a.Routes.Get(rt.Host)
+ if err != nil || current == nil || *current != withdrawalRow(rt) || a.LocalEligible(rt) {
+ return pending("changed")
+ }
+ if a.shuttingDown {
+ return pending("shutdown")
+ }
+ if e := a.entries[rt.Host]; e != nil && (!samePoolRoute(e, rt) || e.route.Kind != rt.Kind || e.route.DBPath != rt.DBPath || e.route.ClaimOwnership != rt.ClaimOwnership) {
+ return pending("changed")
+ }
+ owner := "pool-withdrawal/" + rt.Host
+ if a.seedExclusions[rt.Host] != nil || a.tokenPreparations[rt.Host] != nil || (a.fences[rt.Host] != 0 && (a.fences[rt.Host] != 1 || a.fenceOwners[rt.Host] != owner)) {
+ return pending("fence")
+ }
+ if a.fences == nil {
+ a.fences = map[string]uint64{}
+ }
+ if a.fenceOwners == nil {
+ a.fenceOwners = map[string]string{}
+ }
+ a.fences[rt.Host], a.fenceOwners[rt.Host] = 1, owner
+ if a.poolWithdrawals == nil {
+ a.poolWithdrawals = map[string]*poolWithdrawal{}
+ }
+ if old := a.poolWithdrawals[rt.Host]; old != nil {
+ if !old.done {
+ return pending("stopping")
+ }
+ if old.route == rt {
+ if old.err == nil {
+ return nil
+ }
+ if time.Now().Before(old.retryAt) {
+ return old.err
+ }
+ }
+ }
+ job := &poolWithdrawal{route: rt, owner: owner}
+ a.poolWithdrawals[rt.Host] = job
+ go a.provePoolWithdrawal(b, job)
+ return pending("stopping")
+}
+
+func (a *Service) provePoolWithdrawal(b *ExecBackend, job *poolWithdrawal) {
+ ctx, cancel := context.WithTimeout(a.life(), 5*time.Minute)
+ defer cancel()
+ err := a.poolWithdrawalProof(ctx, b, job)
+ a.mu.Lock()
+ job.err, job.done = err, true
+ job.retryAt = time.Now().Add(time.Second)
+ a.mu.Unlock()
+ if err != nil {
+ log.Printf("%v host=%s", err, job.route.Host)
+ }
+}
+
+func (a *Service) poolWithdrawalProof(ctx context.Context, b *ExecBackend, job *poolWithdrawal) error {
+ rt := job.route
+ pending := provision.PoolWithdrawalPending
+ stopCtx, stopCancel := context.WithTimeout(ctx, 10*time.Millisecond)
+ evidence, err := a.FenceAndStopAs(stopCtx, rt.Host, 1, job.owner)
+ stopCancel()
+ if err != nil && !errors.Is(err, ErrStopUnproven) {
+ return pending("stop")
+ }
+ a.mu.Lock()
+ busy := a.entries[rt.Host] != nil || a.unprovenWakes[rt.Host] != nil || len(a.straySidecars[rt.Host]) != 0 || a.releaseOps[rt.Host] != nil
+ for owed := range a.owedReleases {
+ busy = busy || owed.host == rt.Host
+ }
+ history := a.stopHistory[rt.Host]
+ a.mu.Unlock()
+ if busy || (history != nil && !history.complete()) {
+ return pending("stop")
+ }
+ if evidence == nil || history == nil || history.noStart {
+ if b.StartupSwept == nil || !b.StartupSwept() || b.sameBox.acquiredHere(rt.Host) {
+ return pending("startup")
+ }
+ }
+ if err := b.poolWithdrawalEmpty(rt); err != nil {
+ return pending("cgroup")
+ }
+ if rt.Kind == "static" && (history != nil || rt.Hibernate || rt.Socket != "" || rt.Addr != "" || rt.Addrs != "" || rt.DBPath != "" || rt.Runtime != "" || rt.Args != "") {
+ return pending("ownership")
+ }
+ if (b.Cfg.Record == nil || rt.WakeIncarnation == "") && rt.Kind != "static" {
+ return pending("record")
+ }
+ ha := rt.HA
+ life := ""
+ var view hafresh.View
+ if rt.Kind != "static" {
+ facts, err := b.Cfg.Record(ctx, rt)
+ if err != nil {
+ return pending("record")
+ }
+ ha = ha || facts.HA
+ if b.Lifetimes != nil {
+ if known, ok := b.Lifetimes.Lookup(rt.Host); ok {
+ ha = ha || known.HA
+ }
+ }
+ view, err = b.readHolderView(ctx, rt)
+ if err != nil {
+ return pending("release")
+ }
+ ha = ha || view.HAOn()
+ if !ha && evidence == nil && !view.Absent {
+ return pending("release")
+ }
+ life = facts.ID
+ if life != rt.WakeIncarnation {
+ return pending("lifetime")
+ }
+ }
+ for _, o := range lease.PendingObligations() {
+ if o.Bucket == b.Cfg.Bucket && o.Key == b.Cfg.hibernatePrefix(rt.Host)+"/lock.json" {
+ return pending("release")
+ }
+ }
+ if ha {
+ if err := b.haContained(rt); err != nil {
+ return pending("cgroup")
+ }
+ if life == "" {
+ return pending("lifetime")
+ }
+ body, rerr := readRecordFile(recordPath(b.pathsFor(rt), rt.Host))
+ var rec hafresh.LocalRecord
+ if rerr == nil {
+ rerr = json.Unmarshal(body, &rec)
+ }
+ if errors.Is(rerr, fs.ErrNotExist) && view.Absent && !b.sameBox.acquiredHere(rt.Host) {
+ if _, err := statFileID(b.pathsFor(rt).dbPath); !errors.Is(err, fs.ErrNotExist) {
+ return pending("release")
+ }
+ } else {
+ if rerr != nil || view.Absent || view.Lock.Release == nil {
+ return pending("release")
+ }
+ m := view.Lock.Release
+ if m.FilesGen == "" || !m.Clean || m.V != 1 || m.Box != b.self().Ref || m.IID != b.self().IID || m.Lifetime != life || *m != rec.Marker {
+ return pending("release")
+ }
+ }
+ }
+ timers, err := b.Registry.TimersFor(rt.Host)
+ if err != nil {
+ return pending("duties")
+ }
+ current, err := b.Registry.Get(rt.Host)
+ if err != nil || current == nil || *current != withdrawalRow(rt) || b.poolEligible(rt) {
+ return pending("changed")
+ }
+ if current.NextDue != 0 || len(timers) != 0 {
+ return pending("duties")
+ }
+ return nil
+}
+
+func withdrawalRow(rt registry.Route) registry.Route { rt.WakeIncarnation = ""; return rt }
+
+func (b *ExecBackend) poolWithdrawalEmpty(rt registry.Route) error {
+ events := b.CgroupEvents
+ if events == nil {
+ events = cgroupEventsUnder(b.cgroupRoot())
+ }
+ exists, populated, err := events(instancectl.SliceName(rt.Host), "")
+ if err != nil || (exists && populated) {
+ return ErrStopUnproven
+ }
+ exists, populated, err = b.flatHostEvents(rt.Host)
+ if err != nil || (exists && populated) {
+ return ErrStopUnproven
+ }
+ return nil
+}
diff --git a/internal/activator/poolwithdrawal_test.go b/internal/activator/poolwithdrawal_test.go
new file mode 100644
index 000000000..dfe34e5cf
--- /dev/null
+++ b/internal/activator/poolwithdrawal_test.go
@@ -0,0 +1,244 @@
+package activator
+
+import (
+ "context"
+ "errors"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/edgesfile"
+ "github.com/carlosframework/platform/internal/hafresh"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+type withdrawalSyncFailure struct {
+ Rep
+ haRep
+}
+
+func (r withdrawalSyncFailure) Finish(ctx context.Context) (uint64, error) {
+ n, err := r.haRep.Finish(ctx)
+ return n, errors.Join(err, errors.New("injected sync failure"))
+}
+
+func withdrawalFixture(t *testing.T, s *haSvc) registry.Route {
+ t.Helper()
+ s.a.Routes = s.b.Registry
+ s.b.sameBox.flatEvents = noFlatCgroup
+ s.b.ServedPools = func() edgesfile.PoolEvidence { return edgesfile.PoolEvidence(`["other"]`) }
+ rt, err := s.b.Registry.Get(s.rt.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ facts, err := s.b.Cfg.Record(context.Background(), *rt)
+ if err != nil {
+ t.Fatal(err)
+ }
+ rt.WakeIncarnation = facts.ID
+ return *rt
+}
+
+func awaitWithdrawal(t *testing.T, s *haSvc, rt registry.Route, want bool) error {
+ t.Helper()
+ var err error
+ deadline := time.Now().Add(4 * time.Second)
+ for time.Now().Before(deadline) {
+ err = s.a.PreparePoolWithdrawal(context.Background(), rt)
+ if err == nil {
+ return nil
+ }
+ s.a.mu.Lock()
+ j := s.a.poolWithdrawals[rt.Host]
+ finished := j != nil && j.done
+ s.a.mu.Unlock()
+ if !want && finished && !strings.Contains(err.Error(), "code=stopping") {
+ return err
+ }
+ time.Sleep(20 * time.Millisecond)
+ }
+ return err
+}
+
+func TestPoolWithdrawalRequiresCompleteProof(t *testing.T) {
+ for _, mode := range []string{"awake", "asleep", "populated", "failed-sync", "failed-publication", "owed", "duties", "record-read", "changed-lifetime", "changed-pool", "restart", "restart-dirty", "successor"} {
+ t.Run(mode, func(t *testing.T) {
+ s, e := haServiceEntry(t, "shop.example.com")
+ facts, err := s.b.Cfg.Record(context.Background(), s.rt)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if mode == "asleep" || mode == "restart" || mode == "successor" || mode == "changed-lifetime" || mode == "record-read" {
+ s.stopForTest(e)
+ }
+ switch mode {
+ case "populated":
+ s.unit.set(func(u *haUnit) { u.sliceHeld = true })
+ case "owed":
+ s.failMarkerPuts(10)
+ case "failed-sync":
+ s.a.mu.Lock()
+ e.rep = withdrawalSyncFailure{Rep: e.rep, haRep: e.rep.(haRep)}
+ s.a.mu.Unlock()
+ case "failed-publication":
+ s.a.stopFiles(e)
+ s.fx.FailNextMatching("PUT", 403, func(k string, _ []byte) bool { return strings.Contains(k, "/files/") })
+ case "restart", "restart-dirty":
+ s = s.restart(t)
+ }
+ row := withdrawalFixture(t, s)
+ if mode == "duties" {
+ if err := s.b.Registry.UpsertTimer(registry.Timer{Host: row.Host, Name: "reminder", Kind: "once", Spec: "at 123", NextAt: 123}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if mode == "record-read" {
+ s.b.Cfg.Record = func(context.Context, registry.Route) (RecordFacts, error) {
+ return RecordFacts{}, errors.New("injected record failure")
+ }
+ }
+ if mode == "changed-lifetime" {
+ row.WakeIncarnation = facts.ID + "-old"
+ }
+ if mode == "changed-pool" {
+ row.Pool = "changed"
+ }
+ if mode == "successor" {
+ body, err := hafresh.MarkerBody(99, hafresh.Marker{V: 1, Box: "successor", IID: "i-successor", Lifetime: facts.ID, Clean: true})
+ if err != nil {
+ t.Fatal(err)
+ }
+ s.fx.PutLock(s.b.Cfg.hibernatePrefix(row.Host), body)
+ }
+ writes := lockWrites(t, s.b, s.rt)
+ if err := s.a.PreparePoolWithdrawal(context.Background(), row); err == nil {
+ t.Fatal("proof returned synchronously before inspection")
+ }
+ success := mode == "awake" || mode == "asleep" || mode == "restart"
+ err = awaitWithdrawal(t, s, row, success)
+ if (err == nil) != success {
+ t.Fatalf("mode=%s err=%v", mode, err)
+ }
+ if got, _ := s.b.Registry.Get(row.Host); got == nil {
+ t.Fatal("preparation deleted registry row")
+ }
+ if success {
+ v := s.lockView(t)
+ if v.Lock.Release == nil || !v.Lock.Release.Clean {
+ t.Fatalf("no clean release: %+v", v)
+ }
+ if !s.stoppedFor(e) {
+ t.Fatal("release before stop")
+ }
+ if deleted, err := s.b.Registry.DeleteWithdrawnRoute(context.Background(), row); err != nil || !deleted {
+ t.Fatalf("delete after proof: %v %v", deleted, err)
+ }
+ }
+ if mode == "duties" {
+ timers, _ := s.b.Registry.TimersFor(row.Host)
+ if len(timers) != 1 {
+ t.Fatal("pending timer deleted")
+ }
+ }
+ if mode == "successor" && lockWrites(t, s.b, s.rt) != writes {
+ t.Fatal("successor lease was written")
+ }
+ if mode == "populated" {
+ s.unit.set(func(u *haUnit) { u.sliceHeld = false; u.slicePop = false })
+ s.stopForTest(e)
+ }
+ })
+ }
+}
+
+func TestPoolWithdrawalInFlightWake(t *testing.T) {
+ s := haService(t, "shop.example.com")
+ rt := withdrawalFixture(t, s)
+ var cancelled atomic.Bool
+ ready := make(chan struct{})
+ e := &actEntry{host: rt.Host, route: withdrawalRow(rt), state: actWaking, ready: ready, wakeCancel: func() { cancelled.Store(true) }}
+ s.a.mu.Lock()
+ s.a.entries[rt.Host] = e
+ s.a.mu.Unlock()
+ t.Cleanup(func() { s.a.mu.Lock(); delete(s.a.entries, rt.Host); s.a.mu.Unlock(); close(ready) })
+ if err := awaitWithdrawal(t, s, rt, false); err == nil {
+ t.Fatal("in-flight wake authorized deletion")
+ }
+ if !cancelled.Load() || !s.a.Fenced(rt.Host) {
+ t.Fatal("wake not cancelled and fenced")
+ }
+ if lockWrites(t, s.b, s.rt) != 0 {
+ t.Fatal("withdrawal acquired a lease")
+ }
+}
+
+func TestPoolWithdrawalNeverHeld(t *testing.T) {
+ s := haService(t, "shop.example.com")
+ row := withdrawalFixture(t, s)
+ if err := awaitWithdrawal(t, s, row, true); err != nil {
+ t.Fatal(err)
+ }
+ if lockWrites(t, s.b, s.rt) != 0 {
+ t.Fatal("cleanup acquired a lease")
+ }
+}
+
+func TestPoolWithdrawalProofSurvivesSuccessor(t *testing.T) {
+ s, e := haServiceEntry(t, "shop.example.com")
+ rt := withdrawalFixture(t, s)
+ if err := awaitWithdrawal(t, s, rt, true); err != nil {
+ t.Fatal(err)
+ }
+ if !s.stoppedFor(e) {
+ t.Fatal("stop incomplete")
+ }
+ body, _ := hafresh.MarkerBody(99, hafresh.Marker{V: 1, Box: "successor", IID: "i-successor", Lifetime: rt.WakeIncarnation, Clean: true})
+ s.fx.PutLock(s.b.Cfg.hibernatePrefix(rt.Host), body)
+ writes := lockWrites(t, s.b, s.rt)
+ if err := s.a.PreparePoolWithdrawal(context.Background(), rt); err != nil {
+ t.Fatal(err)
+ }
+ if lockWrites(t, s.b, s.rt) != writes {
+ t.Fatal("successor lease changed")
+ }
+}
+
+func TestPoolWithdrawalOrdinaryAndStatic(t *testing.T) {
+ for _, kind := range []string{"ordinary", "static", "static-attached"} {
+ t.Run(kind, func(t *testing.T) {
+ s := haService(t, "shop.example.com")
+ if kind == "ordinary" {
+ h := haFixOf(s.b)
+ h.mu.Lock()
+ rec := h.records[s.rt.Host]
+ rec.HA = false
+ h.records[s.rt.Host] = rec
+ h.mu.Unlock()
+ if _, err := s.wakeForTest(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ } else {
+ row, err := s.b.Registry.Get(s.rt.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ row.Kind = "static"
+ row.Socket = ""
+ row.DBPath = ""
+ row.Hibernate = false
+ if kind == "static-attached" {
+ row.Addr = "127.0.0.1:8080"
+ }
+ if err := s.b.Registry.Upsert(*row); err != nil {
+ t.Fatal(err)
+ }
+ }
+ rt := withdrawalFixture(t, s)
+ err := awaitWithdrawal(t, s, rt, kind != "static-attached")
+ if (err == nil) != (kind != "static-attached") {
+ t.Fatalf("%s: %v", kind, err)
+ }
+ })
+ }
+}
diff --git a/internal/edge/forwardha_export_test.go b/internal/edge/forwardha_export_test.go
index 3d57f183f..694a95a2c 100644
--- a/internal/edge/forwardha_export_test.go
+++ b/internal/edge/forwardha_export_test.go
@@ -249,7 +249,20 @@ func (s *HAForwardScene) Ingress(t *testing.T, lookup func(string) (registry.Rou
if err != nil {
t.Fatal(err)
}
- if mode == "missing" {
+ if mode == "withdrawn" {
+ rt.Pool, rt.Account, rt.App, rt.Kind, rt.Status = "ha", "bab", "shop", "instance", "ready"
+ rt.Provisioned, rt.Hibernate = true, true
+ if err := s.x.a.reg.Upsert(*rt); err != nil {
+ t.Fatal(err)
+ }
+ expected, err := s.x.a.reg.Get(fwHost)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if deleted, err := s.x.a.reg.DeleteWithdrawnRoute(t.Context(), *expected); err != nil || !deleted {
+ t.Fatalf("withdrawal deletion: %v %v", deleted, err)
+ }
+ } else if mode == "missing" {
if err := s.x.a.reg.Delete(fwHost); err != nil {
t.Fatal(err)
}
diff --git a/internal/edge/poolwithdrawal_test.go b/internal/edge/poolwithdrawal_test.go
new file mode 100644
index 000000000..a62a49159
--- /dev/null
+++ b/internal/edge/poolwithdrawal_test.go
@@ -0,0 +1,40 @@
+package edge_test
+
+import (
+ "context"
+ "net/http"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/activator"
+ "github.com/carlosframework/platform/internal/edge"
+ "github.com/carlosframework/platform/internal/hafresh"
+ "github.com/carlosframework/platform/internal/lease/locktest"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+func TestPoolWithdrawalScopeAndIngress(t *testing.T) {
+ fx := locktest.NewLockFixture(t)
+ cfg := activator.Config{Bucket: fx.Bucket, Idle: time.Hour, Self: func() hafresh.Self { return hafresh.Self{Ref: "annie", IID: "i-a"} }}
+ svc := activator.New(cfg, &activator.ExecBackend{ServedPools: (provision.ReconcileConfig{}).ServedPools, Cfg: cfg}, nil)
+ t.Cleanup(func() { svc.Shutdown(context.Background()) })
+ scene := edge.NewHAForwardScene(t, svc)
+ scene.IngressPools(t, "absent")
+ rt := registry.Route{Host: edge.HAForwardHost, Account: "bab", App: "shop", Pool: "ha", HA: true}
+ scene.Ingress(t, func(host string) (registry.Route, bool) { return rt, host == rt.Host }, "withdrawn")
+ w := scene.IngressRequest(t, http.MethodPost, rt.Host, "/", "once", false)
+ if w.Code != 200 || !strings.HasPrefix(w.Body.String(), "betty|") {
+ t.Fatalf("after withdrawal: %d %s", w.Code, w.Body.String())
+ }
+ local, peer := scene.IngressCounts()
+ if local != 0 || peer != 1 {
+ t.Fatalf("deliveries local=%d peer=%d", local, peer)
+ }
+ for _, request := range fx.Requests() {
+ if strings.HasPrefix(request, "PUT ") || strings.HasPrefix(request, "DELETE ") {
+ t.Fatalf("ingress acquired: %s", request)
+ }
+ }
+}
diff --git a/internal/host/agent.go b/internal/host/agent.go
index 19d0bc3ae..95c6c392c 100644
--- a/internal/host/agent.go
+++ b/internal/host/agent.go
@@ -665,6 +665,7 @@ func runAgent(args []string, buildClaims ClaimFactory) {
// (design §6.8). The registry satisfies RouteLookup with its own
// Get, exactly as DueStore above.
svc.Routes = reg
+ armedPoolWithdrawal.Store(svc)
// The box's fleet state for a labelled wake's check under the lease
// (managed fleets 1b, fleetWakeFor): a fleet box checks the record,
// a box on a fleet with an invalid identity refuses every labelled
@@ -2030,6 +2031,9 @@ func reconcileSerializedWith(ctx context.Context, s release.Store, reg *registry
func reconcileSerializedAuth(ctx context.Context, s release.Store, reg *registry.Registry, cfg provision.ReconcileConfig, w *movesWorker, auth *assetAuthority) error {
reconcileMu.Lock()
defer reconcileMu.Unlock()
+ if cfg.WithdrawPool == nil {
+ cfg.WithdrawPool = preparePoolWithdrawal
+ }
start := auth.passBegin()
defer auth.passEnd() // after the sidecar pass's writes too
// Claims off, the pass judges every record by the box's judge and its
diff --git a/internal/host/edgesfile.go b/internal/host/edgesfile.go
index 4281d3a75..d464289e4 100644
--- a/internal/host/edgesfile.go
+++ b/internal/host/edgesfile.go
@@ -597,6 +597,7 @@ func edgesFileWriteClaims(ctx context.Context, s release.Store, reg *registry.Re
withheld[host] = true
}
forwards := map[string]bool{}
+ pools := provisionTickConfig(os.Getenv).ServedPools()
for _, r := range routes {
// edge.Reload (internal/edge/edge.go) is the deployment's own
// definition of "serves": it skips any row whose Status != "ready"
@@ -631,6 +632,9 @@ func edgesFileWriteClaims(ctx context.Context, s release.Store, reg *registry.Re
if r.IsSidecar() {
continue
}
+ if _, alias := platformSources[r.Host]; !alias && !r.IsForward() && !pools.Serves(r.Pool) {
+ continue
+ }
if withheld[r.Host] {
continue
}
diff --git a/internal/host/poolwithdrawal.go b/internal/host/poolwithdrawal.go
new file mode 100644
index 000000000..58575c862
--- /dev/null
+++ b/internal/host/poolwithdrawal.go
@@ -0,0 +1,20 @@
+package host
+
+import (
+ "context"
+ "sync/atomic"
+
+ "github.com/carlosframework/platform/internal/activator"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+var armedPoolWithdrawal atomic.Pointer[activator.Service]
+
+func preparePoolWithdrawal(ctx context.Context, rt registry.Route) error {
+ svc := armedPoolWithdrawal.Load()
+ if svc == nil {
+ return provision.PoolWithdrawalPending("hook")
+ }
+ return svc.PreparePoolWithdrawal(ctx, rt)
+}
diff --git a/internal/host/poolwithdrawal_test.go b/internal/host/poolwithdrawal_test.go
new file mode 100644
index 000000000..6b821b5dc
--- /dev/null
+++ b/internal/host/poolwithdrawal_test.go
@@ -0,0 +1,74 @@
+package host
+
+import (
+ "context"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+func TestPoolWithdrawalScopeAndIngress(t *testing.T) {
+ t.Setenv("CARLOS_POOLS", "new")
+ reg := edgesTestRegistry(t)
+ rt := registry.Route{Host: "shop.example.com", Account: "bbw", App: "shop", Kind: "instance", Status: "ready", Provisioned: true, Hibernate: true, Pool: "old"}
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.UpsertAlias(registry.RouteAlias{Alias: "www.example.com", Host: rt.Host, Account: rt.Account, App: rt.App, DNSStatus: "pointing"}); err != nil {
+ t.Fatal(err)
+ }
+ for _, row := range []registry.Route{rt, {Host: "eligible.example.com", Kind: "static", Status: "ready", Pool: "new"}} {
+ if err := reg.Upsert(row); err != nil {
+ t.Fatal(err)
+ }
+ }
+ hosts := publishHosts(t, reg)
+ for _, host := range []string{rt.Host, "www.example.com"} {
+ if _, ok := hosts[host]; ok {
+ t.Fatalf("withdrawn home published: %s", host)
+ }
+ }
+ if _, ok := hosts["eligible.example.com"]; !ok {
+ t.Fatal("eligible home missing")
+ }
+ row, err := reg.Get(rt.Host)
+ if err != nil || row == nil {
+ t.Fatalf("pending row lost: %v", err)
+ }
+ if deleted, err := reg.DeleteWithdrawnRoute(context.Background(), *row); err != nil || !deleted {
+ t.Fatalf("delete: %v %v", deleted, err)
+ }
+ if len(publishHosts(t, reg)) != 1 {
+ t.Fatal("publication did not follow deletion")
+ }
+}
+
+func TestPoolWithdrawalMissingService(t *testing.T) {
+ old := armedPoolWithdrawal.Swap(nil)
+ defer armedPoolWithdrawal.Store(old)
+ if preparePoolWithdrawal(context.Background(), registry.Route{}) == nil {
+ t.Fatal("missing service granted deletion")
+ }
+}
+
+func TestPoolWithdrawalPlatformAlias(t *testing.T) {
+ t.Setenv("CARLOS_POOLS", "new")
+ for _, pool := range []string{"old", "new"} {
+ t.Run(pool, func(t *testing.T) {
+ reg := edgesTestRegistry(t)
+ primary := registry.Route{Host: "primary.example.com", Socket: "/run/primary.sock", Account: "bbw", App: "shop", Kind: "instance", Status: "ready", Provisioned: true, Hibernate: true, Pool: pool}
+ alias := registry.Route{Host: "alias.example.com", Socket: primary.Socket, Account: primary.Account, App: primary.App, Kind: "instance", Status: "ready", Platform: true}
+ for _, rt := range []registry.Route{primary, alias} {
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ }
+ hosts := publishHosts(t, reg)
+ for _, name := range []string{primary.Host, alias.Host} {
+ if _, ok := hosts[name]; ok != (pool == "new") {
+ t.Fatalf("pool %s: publication of %s=%v", pool, name, ok)
+ }
+ }
+ })
+ }
+}
diff --git a/internal/provision/poolwithdrawal.go b/internal/provision/poolwithdrawal.go
new file mode 100644
index 000000000..5e1558c76
--- /dev/null
+++ b/internal/provision/poolwithdrawal.go
@@ -0,0 +1,141 @@
+package provision
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "log"
+ "strings"
+
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+func (cfg ReconcileConfig) poolWithdrawalScope(rt registry.Route, sidecars bool) bool {
+ if (cfg.PolicyScope != nil && !cfg.PolicyScope(rt.Account, rt.App)) || !rt.Provisioned || rt.IsSidecar() != sidecars || rt.IsForward() || cfg.ServedPools().Serves(rt.Pool) {
+ return false
+ }
+ if rt.Region == cfg.Region {
+ if _, fleet := registry.FleetPlacement(cfg.Region); fleet && rt.FleetBox != "" && rt.FleetBox != cfg.FleetBox {
+ return false
+ }
+ return true
+ }
+ if sidecars || !everywhereRoute(rt) {
+ return false
+ }
+ _, rf := registry.FleetPlacement(rt.Region)
+ _, bf := registry.FleetPlacement(cfg.Region)
+ return !rf && !bf
+}
+
+func (cfg ReconcileConfig) poolWithdrawalCandidate(rt registry.Route, sidecars bool) bool {
+ return cfg.poolWithdrawalScope(rt, sidecars) && !cfg.ClaimEnabled && rt.PoolWithdrawalSupported() && (cfg.KeepParticipant == nil || !cfg.KeepParticipant(rt.Host))
+}
+
+func (cfg ReconcileConfig) withdrawalRecord(ctx context.Context, s release.Store, rt *registry.Route) error {
+ if rt.IsSidecar() {
+ if !ValidSidecarHost(rt.Host, rt.App, rt.Account) {
+ return registry.ErrHAChanged
+ }
+ name, _, _ := strings.Cut(rt.Host, ".")
+ rec, err := GetSidecarRecord(ctx, s, rt.Account, rt.App, name)
+ if err != nil {
+ return err
+ }
+ if ValidateSidecarShape(*rec) != nil || rec.Host != rt.Host || rec.Region != rt.Region || rec.Pool != rt.Pool || rt.Incarnation != 0 || rt.ServingSeq != 0 {
+ return registry.ErrHAChanged
+ }
+ life, err := SidecarLifetimeEntryOf(*rec)
+ if err != nil {
+ return err
+ }
+ if rt.WakeIncarnation != "" && rt.WakeIncarnation != life.ID {
+ return registry.ErrHAChanged
+ }
+ rt.WakeIncarnation = life.ID
+ return nil
+ }
+ obs := cfg.Judge.Observe(ctx, s, rt.Account, rt.App, rt.Host)
+ if obs.Err != nil {
+ return obs.Err
+ }
+ if !obs.Present || obs.Verdict != LifetimeValid || obs.Rec == nil {
+ return registry.ErrHAChanged
+ }
+ rec := obs.Rec
+ if cfg.Hold != nil {
+ if _, _, held := cfg.Hold(*rec); held {
+ return registry.ErrHAChanged
+ }
+ }
+ if ValidateShape(*rec) != nil || rec.Retired || rec.KindOrDefault() != rt.Kind || rec.Region != rt.Region || rec.Pool != rt.Pool || rec.Incarnation != rt.Incarnation || rec.MoveSeq != rt.ServingSeq || !cfg.ownsRecord(*rec) || rec.ReplicaAssignment != nil || rec.Role == "replica" || rec.Move != nil || len(rec.Maintenance) != 0 || rec.ActiveRun != nil || rec.Interactive != nil || rec.Demo != nil || rec.DemoClaim != nil || rec.Seed.Unfinished() || rec.Seed.BlocksRun() {
+ return registry.ErrHAChanged
+ }
+ life, err := LifetimeEntryOf(*rec)
+ if err != nil {
+ return err
+ }
+ if rt.WakeIncarnation != "" && rt.WakeIncarnation != life.ID {
+ return registry.ErrHAChanged
+ }
+ rt.WakeIncarnation = life.ID
+ return nil
+}
+
+func (cfg ReconcileConfig) withdrawPools(ctx context.Context, s release.Store, reg *registry.Registry, sidecars bool) {
+ routes, err := reg.List()
+ if err != nil {
+ log.Printf("ha_pool_withdrawal_pending code=registry err=%v", err)
+ return
+ }
+ for _, rt := range routes {
+ if !cfg.poolWithdrawalScope(rt, sidecars) {
+ continue
+ }
+ code := ""
+ switch {
+ case !cfg.poolWithdrawalCandidate(rt, sidecars):
+ code = "ownership"
+ case cfg.DryRun:
+ continue
+ case cfg.WithdrawPool == nil:
+ code = "hook"
+ default:
+ err = cfg.withdrawalRecord(ctx, freshStore(s), &rt)
+ if err != nil {
+ code = "record"
+ break
+ }
+ journal, jerr := reg.ImportJournal(rt.Host)
+ if jerr != nil || journal != nil {
+ code = "ownership"
+ break
+ }
+ if err = cfg.WithdrawPool(ctx, rt); err != nil {
+ code = "proof"
+ break
+ }
+ if err = cfg.withdrawalRecord(ctx, freshStore(s), &rt); err != nil {
+ code = "record"
+ break
+ }
+ var deleted bool
+ deleted, err = reg.DeleteWithdrawnRoute(ctx, rt)
+ if err != nil || !deleted {
+ code = "changed"
+ break
+ }
+ log.Printf("ha_pool_withdrawn host=%s", rt.Host)
+ }
+ if code != "" {
+ log.Printf("ha_pool_withdrawal_pending code=%s host=%s", code, rt.Host)
+ }
+ }
+}
+
+var errPoolWithdrawalPending = errors.New("ha_pool_withdrawal_pending")
+
+func PoolWithdrawalPending(code string) error {
+ return fmt.Errorf("%w code=%s", errPoolWithdrawalPending, code)
+}
diff --git a/internal/provision/poolwithdrawal_test.go b/internal/provision/poolwithdrawal_test.go
new file mode 100644
index 000000000..381833057
--- /dev/null
+++ b/internal/provision/poolwithdrawal_test.go
@@ -0,0 +1,153 @@
+package provision
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+func TestPoolWithdrawalRequiresCompleteProof(t *testing.T) {
+ for _, test := range []string{"success", "missing-hook", "pending", "changed-pool", "record-failure", "changed-record-lifetime", "changed-record-pool", "keep-participant", "held-record", "scoped-out"} {
+ t.Run(test, 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"}
+ reg := regWith(t, rt)
+ defer reg.Close()
+ s := dirStore(t)
+ rec := Record{Host: rt.Host, Account: rt.Account, App: rt.App, Pool: rt.Pool, Region: rt.Region, Channel: "stable"}
+ if err := CreateRecord(ctx, s, rec); err != nil {
+ t.Fatal(err)
+ }
+ before, _, err := s.Get(ctx, RecordKey(rt.Account, rt.App, rt.Host))
+ if err != nil {
+ t.Fatal(err)
+ }
+ cfg := ReconcileConfig{Region: "ie", Pools: []string{"new"}}
+ cfg.WithdrawPool = func(context.Context, registry.Route) error {
+ if test == "changed-record-lifetime" || test == "changed-record-pool" {
+ changed, err := GetRecord(ctx, s, rt.Account, rt.App, rt.Host)
+ if err != nil {
+ return err
+ }
+ if test == "changed-record-lifetime" {
+ changed.CreatedAt = changed.CreatedAt.Add(time.Hour)
+ } else {
+ changed.Pool = "new"
+ }
+ body, err := json.Marshal(changed)
+ if err != nil {
+ return err
+ }
+ putReplace(t, s, RecordKey(rt.Account, rt.App, rt.Host), body)
+ }
+ if test == "pending" {
+ return errors.New("pending")
+ }
+ if test == "changed-pool" {
+ changed := *mustGet(t, reg, rt.Host)
+ changed.Pool = "new"
+ return reg.Upsert(changed)
+ }
+ return nil
+ }
+ if test == "keep-participant" {
+ cfg.KeepParticipant = func(string) bool { return true }
+ }
+ if test == "held-record" {
+ cfg.Hold = func(Record) (string, string, bool) { return "moving", "move", true }
+ }
+ if test == "scoped-out" {
+ cfg.PolicyScope = func(string, string) bool { return false }
+ }
+ if test == "missing-hook" {
+ cfg.WithdrawPool = nil
+ }
+ if test == "record-failure" {
+ putReplace(t, s, RecordKey(rt.Account, rt.App, rt.Host), []byte("{"))
+ }
+ cfg.withdrawPools(ctx, s, reg, false)
+ got, err := reg.Get(rt.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if (got == nil) != (test == "success") {
+ t.Fatalf("row=%+v for %s", got, test)
+ }
+ if test != "record-failure" && !strings.HasPrefix(test, "changed-record-") {
+ after, _, err := s.Get(ctx, RecordKey(rt.Account, rt.App, rt.Host))
+ if err != nil || string(after) != string(before) {
+ t.Fatalf("instance record changed: %v", err)
+ }
+ }
+ })
+ }
+}
+
+func TestPoolWithdrawalScopeAndIngress(t *testing.T) {
+ for _, shape := range []string{"ordinary", "sidecar", "manual", "foreign-region", "foreign-fleet", "platform", "canary", "claim", "replica", "eligible"} {
+ t.Run(shape, 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"}
+ switch shape {
+ case "sidecar":
+ rt.Kind = SidecarKind
+ case "manual":
+ rt.Provisioned = false
+ case "foreign-region":
+ rt.Region = "us"
+ case "foreign-fleet":
+ rt.Region = "fleet/other"
+ case "platform":
+ rt.Platform = true
+ case "canary":
+ rt.Canary = true
+ case "claim":
+ rt.ClaimOwnership.ClaimOwner = "other"
+ case "replica":
+ rt.Replica = true
+ case "eligible":
+ rt.Pool = "new"
+ }
+ cfg := ReconcileConfig{Region: "ie", Pools: []string{"new"}}
+ if got := cfg.poolWithdrawalCandidate(rt, false); got != (shape == "ordinary") {
+ t.Fatalf("candidate=%v for %s", got, shape)
+ }
+ })
+ }
+}
+
+func TestPoolWithdrawalSidecarOwnKind(t *testing.T) {
+ s := dirStore(t)
+ rec := SidecarRecord{Schema: 1, Name: "worker", App: "shop", Account: "bbw", Pool: "old", Region: "ie", Channel: "stable", CreatedAt: time.Now()}
+ rec.Host = SidecarHost(rec.Name, rec.App, rec.Account)
+ if err := CreateSidecarRecord(ctx, s, rec); err != nil {
+ t.Fatal(err)
+ }
+ sidecar := registry.Route{Host: rec.Host, Account: rec.Account, App: rec.App, Kind: SidecarKind, Status: "ready", Provisioned: true, Hibernate: true, Pool: rec.Pool, Region: rec.Region}
+ instance := sidecar
+ instance.Host = "shop.example.com"
+ instance.Kind = "instance"
+ reg := regWith(t, sidecar, instance)
+ defer reg.Close()
+ calls := 0
+ cfg := ReconcileConfig{Region: "ie", Pools: []string{"new"}, WithdrawPool: func(_ context.Context, rt registry.Route) error {
+ calls++
+ if !rt.IsSidecar() {
+ t.Fatal("sidecar pass touched sibling")
+ }
+ return nil
+ }}
+ cfg.withdrawPools(ctx, s, reg, true)
+ if rt, err := reg.Get(sidecar.Host); err != nil || rt != nil {
+ t.Fatalf("sidecar not removed: %+v %v", rt, err)
+ }
+ if rt, err := reg.Get(instance.Host); err != nil || rt == nil {
+ t.Fatalf("sibling removed: %+v %v", rt, err)
+ }
+ if calls != 1 {
+ t.Fatalf("proof calls=%d", calls)
+ }
+}
diff --git a/internal/provision/reconcile.go b/internal/provision/reconcile.go
index 2fa56958d..379997814 100644
--- a/internal/provision/reconcile.go
+++ b/internal/provision/reconcile.go
@@ -36,6 +36,8 @@ 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
+
// HAIngress receives the complete pre-pool record scan; errors revoke its evidence.
HAIngress func([]Record, error)
@@ -1121,6 +1123,7 @@ func convergeOwned(reg *registry.Registry, existing registry.Route, rec Record,
// never mass-retire a fleet. Minting is additive and always safe, so it
// proceeds regardless.
func Reconcile(ctx context.Context, s release.Store, reg *registry.Registry, cfg ReconcileConfig) error {
+ cfg.withdrawPools(ctx, s, reg, false)
cfg = withDemoClaimHolds(cfg)
if cfg.Now == nil {
cfg.Now = time.Now
diff --git a/internal/provision/sidecar_reconcile.go b/internal/provision/sidecar_reconcile.go
index a48e37306..30ad79b7a 100644
--- a/internal/provision/sidecar_reconcile.go
+++ b/internal/provision/sidecar_reconcile.go
@@ -193,6 +193,7 @@ func coSpawnStaged(dataDir, host string) bool {
// - the app's sidecars manifest is absent or unreadable (no argv to run,
// and no ceilings to stamp).
func ReconcileSidecars(ctx context.Context, s release.Store, reg *registry.Registry, cfg ReconcileConfig) error {
+ cfg.withdrawPools(ctx, s, reg, true)
if cfg.Now == nil {
cfg.Now = time.Now
}
diff --git a/internal/registry/poolwithdrawal.go b/internal/registry/poolwithdrawal.go
new file mode 100644
index 000000000..05296ebe7
--- /dev/null
+++ b/internal/registry/poolwithdrawal.go
@@ -0,0 +1,51 @@
+package registry
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+)
+
+func (rt Route) PoolWithdrawalSupported() bool {
+ if !rt.Provisioned || rt.Platform || rt.Canary || rt.IsDemo() || rt.ClaimOwnership != (ClaimOwnership{}) || rt.IsForward() || rt.Stateless || rt.Replica || rt.ReplicaRevision != 0 || rt.ReplicaIncarnation != "" || rt.FleetBoxes != "" || rt.Copies != 0 || rt.HoldOp != "" || rt.ImportGate != "" || rt.SeedSource != "" || rt.Tunnel != "" || rt.Unit != "" || rt.Backing != "" || rt.Account == "" || rt.App == "" || rt.Status != "ready" {
+ return false
+ }
+ return rt.Kind == "static" || ((rt.Kind == "instance" || rt.IsSidecar()) && rt.Hibernate)
+}
+
+func (r *Registry) DeleteWithdrawnRoute(ctx context.Context, expected Route) (bool, error) {
+ if r.remote != nil {
+ return false, errRemoteMethod
+ }
+ if !expected.PoolWithdrawalSupported() {
+ return false, nil
+ }
+ tx, err := r.sql.BeginTx(ctx, nil)
+ if err != nil {
+ return false, err
+ }
+ defer tx.Rollback()
+ current, err := scanRoute(tx.QueryRowContext(ctx, `SELECT `+routeCols+` FROM routes WHERE host=?`, expected.Host).Scan)
+ if errors.Is(err, sql.ErrNoRows) {
+ return false, nil
+ }
+ if err != nil {
+ return false, err
+ }
+ expected.WakeIncarnation = ""
+ if current != expected || current.NextDue != 0 {
+ return false, nil
+ }
+ var protected bool
+ err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM timers WHERE host=?) OR EXISTS(SELECT 1 FROM import_journal WHERE host=?)`, expected.Host, expected.Host).Scan(&protected)
+ if err != nil || protected {
+ return false, err
+ }
+ if _, err = tx.ExecContext(ctx, `DELETE FROM routes WHERE host=?`, expected.Host); err != nil {
+ return false, err
+ }
+ if err = tx.Commit(); err != nil {
+ return false, err
+ }
+ return true, nil
+}
diff --git a/internal/registry/poolwithdrawal_test.go b/internal/registry/poolwithdrawal_test.go
new file mode 100644
index 000000000..0b8aaab4b
--- /dev/null
+++ b/internal/registry/poolwithdrawal_test.go
@@ -0,0 +1,66 @@
+package registry
+
+import (
+ "context"
+ "path/filepath"
+ "testing"
+)
+
+func TestPoolWithdrawalRequiresCompleteProof(t *testing.T) {
+ for _, change := range []string{"none", "pool", "lifetime", "owner", "timer", "due", "hold", "region", "account", "app", "kind", "sequence", "journal"} {
+ t.Run(change, func(t *testing.T) {
+ r, err := Open(filepath.Join(t.TempDir(), "registry.db"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer r.Close()
+ rt := Route{Host: "shop.example.com", Account: "bbw", App: "shop", Kind: "instance", Status: "ready", Provisioned: true, Hibernate: true, Pool: "old", Region: "ie", Incarnation: 1}
+ if err := r.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ old, _ := r.Get(rt.Host)
+ switch change {
+ case "region":
+ _, err = r.sql.Exec("UPDATE routes SET region='us' WHERE host=?", rt.Host)
+ case "account":
+ _, err = r.sql.Exec("UPDATE routes SET account='other' WHERE host=?", rt.Host)
+ case "app":
+ _, err = r.sql.Exec("UPDATE routes SET app='other' WHERE host=?", rt.Host)
+ case "kind":
+ _, err = r.sql.Exec("UPDATE routes SET kind='sidecar' WHERE host=?", rt.Host)
+ case "sequence":
+ _, err = r.sql.Exec("UPDATE routes SET serving_seq=2 WHERE host=?", rt.Host)
+ case "journal":
+ _, err = r.sql.Exec("INSERT INTO import_journal(host,seed_id,record_key,original,state,fence_gen,at) VALUES(?, 'seed', 'key', '{}', 'cleanup-pending',0,0)", rt.Host)
+ case "pool":
+ _, err = r.sql.Exec("UPDATE routes SET pool='new' WHERE host=?", rt.Host)
+ case "lifetime":
+ _, err = r.sql.Exec("UPDATE routes SET incarnation=2 WHERE host=?", rt.Host)
+ case "owner":
+ _, err = r.sql.Exec("UPDATE routes SET claim_owner='other' WHERE host=?", rt.Host)
+ case "hold":
+ _, err = r.sql.Exec("UPDATE routes SET hold_op='move' WHERE host=?", rt.Host)
+ case "timer":
+ err = r.UpsertTimer(Timer{Host: rt.Host, Name: "reminder", Kind: "once", Spec: "at 123", NextAt: 123})
+ case "due":
+ err = r.SetNextDue(rt.Host, 123)
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ deleted, err := r.DeleteWithdrawnRoute(context.Background(), *old)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if deleted != (change == "none") {
+ t.Fatalf("deleted=%v for %s", deleted, change)
+ }
+ if change == "timer" {
+ timers, err := r.TimersFor(rt.Host)
+ if err != nil || len(timers) != 1 {
+ t.Fatalf("timer lost: %v %v", timers, err)
+ }
+ }
+ })
+ }
+}