feat: publish stateless bindings from the coordinator reconcile
Skip stateless rows in startup and wake writers. Signal the existing full reconcile after a stateless wake, and publish through its applied-record observation with held coordination authority. Reuse the existing key, read pass and conditional write; suppress unchanged binding work. Targeted binding and reconcile witnesses and darwin/windows/linux-arm64 builds pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+313
−2
internal/host/agent.go+8 −1internal/host/fleetbind.go+92 −1internal/host/stateless_binding_test.go+141 −0internal/provision/reconcile.go+8 −0internal/provision/stateless_binding_test.go+64 −0
Intent
Attributed to this commit: 4.57 g CO₂e (521,910 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on stateless-replicas-1-runtime, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 7605eeaf2..06ac55e6b 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -669,6 +669,10 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| // goroutine, which reads svc.OnWake on every successful wake from |
| // then on — setting it any later is a data race. |
| svc.OnWake = fleetBindingHook(fleetStore, identity.Fleet, identity.Label) |
| + if coordinator != nil { |
| + armedStatelessBindings.Store(&statelessBindingPublisher{store: fleetStore, fleet: identity.Fleet, box: identity.Label, coordinator: coordinator}) |
| + defer armedStatelessBindings.Store(nil) |
| + } |
| } |
| if logSink != nil { |
| svc.Logs = logSink |
| @@ -3241,7 +3245,7 @@ func fastConvergeLoop(reg *registry.Registry, svc *activator.Service, sd *schedu |
| // observer (observeRestart): a failed restart holds its host out of the |
| // sweep instead of the sweep re-cycling the service every tick. The |
| // remaining fields are filled in right below. |
| - d := &fastDeps{reg: reg, store: store, pub: pub, optDir: optDir, serialNotify: serialNotify} |
| + d := &fastDeps{reg: reg, store: store, pub: pub, optDir: optDir, serialNotify: serialNotify, fullDue: &backstopFullDue} |
| if svc != nil { |
| d.coordinator, _ = svc.Coordinator.(interface{ Tick(context.Context) }) |
| } |
| @@ -3652,6 +3656,9 @@ func BoxProvisionConfig(reg *registry.Registry, getenv func(string) string, judg |
| // The process's one lifetime index, which the activator reads before |
| // every wake of a provisioned instance (region HA P1a). |
| cfg.Lifetimes = boxLifetimes |
| + if publisher := armedStatelessBindings.Load(); publisher != nil { |
| + cfg.StatelessBinding = publisher.publish |
| + } |
| if hubArmed(getenv) { |
| cfg.FleetOwners = hubFleetOwners(reg) |
| } |
diff --git a/internal/host/fleetbind.go b/internal/host/fleetbind.go| index 924655ea8..988fe7969 100644 |
| --- a/internal/host/fleetbind.go |
| +++ b/internal/host/fleetbind.go |
| @@ -7,6 +7,12 @@ import ( |
| "log" |
| "os" |
| "path" |
| + "sync" |
| + "sync/atomic" |
| + |
| + "github.com/carlosframework/platform/internal/activator" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/replicas" |
| "time" |
| "github.com/carlosframework/platform/internal/registry" |
| @@ -139,6 +145,10 @@ func getExistingBinding(ctx context.Context, s release.Store, key string) (*Flee |
| // startup, so a bare create-only Put (ifMatch "") would fail every write |
| // after the first with ErrCASConflict. |
| func WriteFleetBinding(ctx context.Context, s release.Store, b FleetBinding, takeover bool) error { |
| + return writeFleetBinding(ctx, s, b, takeover, nil) |
| +} |
| + |
| +func writeFleetBinding(ctx context.Context, s release.Store, b FleetBinding, takeover bool, authority replicas.Authority) error { |
| body, err := json.Marshal(b) |
| if err != nil { |
| return err |
| @@ -153,6 +163,16 @@ func WriteFleetBinding(ctx context.Context, s release.Store, b FleetBinding, tak |
| return nil |
| } |
| + if authority != nil { |
| + if err := authority.Err(); err != nil { |
| + return err |
| + } |
| + select { |
| + case <-authority.Lost(): |
| + return replicas.ErrFenced |
| + default: |
| + } |
| + } |
| if _, err := s.Put(ctx, key, body, etag); err != nil { |
| if !errors.Is(err, release.ErrCASConflict) { |
| return err |
| @@ -168,6 +188,16 @@ func WriteFleetBinding(ctx context.Context, s release.Store, b FleetBinding, tak |
| if bindingBlocked(existing, b, takeover) { |
| return nil |
| } |
| + if authority != nil { |
| + if err := authority.Err(); err != nil { |
| + return err |
| + } |
| + select { |
| + case <-authority.Lost(): |
| + return replicas.ErrFenced |
| + default: |
| + } |
| + } |
| if _, err := s.Put(ctx, key, body, etag); err != nil { |
| return err |
| } |
| @@ -335,6 +365,10 @@ func ReconcileFleetBindings(ctx context.Context, s release.Store, reg *registry. |
| // fetching, so the refusal is now total and depends on no I/O at all. |
| func fleetBindingHook(store release.Store, fleet, box string) func(rt registry.Route) { |
| return func(rt registry.Route) { |
| + if rt.Stateless { |
| + backstopFullDue.Store(true) |
| + return |
| + } |
| if rt.IsSidecar() { |
| return |
| } |
| @@ -376,6 +410,12 @@ func publishResidentFleetBindings(store release.Store, fleet, box string, reg *r |
| ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| defer cancel() |
| now := time.Now().UnixMilli() |
| + for _, rt := range routes { |
| + if rt.Stateless { |
| + backstopFullDue.Store(true) |
| + break |
| + } |
| + } |
| for _, host := range residentBindingHosts(routes) { |
| b := FleetBinding{Host: host, Fleet: fleet, Box: box, BoundAt: now} |
| if err := WriteFleetBinding(ctx, store, b, false); err != nil { |
| @@ -398,10 +438,61 @@ func publishResidentFleetBindings(store release.Store, fleet, box string, reg *r |
| func residentBindingHosts(routes []registry.Route) []string { |
| var out []string |
| for _, rt := range routes { |
| - if rt.Host == "" || rt.IsSidecar() { |
| + if rt.Host == "" || rt.IsSidecar() || rt.Stateless { |
| continue |
| } |
| out = append(out, rt.Host) |
| } |
| return out |
| } |
| + |
| +// statelessBindingPublisher consumes the existing instance reconcile read. |
| +// Its memory suppresses identical binding work across ticks and renewals. |
| +// Publication failure leaves the state unacknowledged for the next pass. |
| +type statelessBindingPublisher struct { |
| + mu sync.Mutex |
| + store release.Store |
| + fleet, box string |
| + coordinator *activator.CoordinationClient |
| + published map[string]string |
| +} |
| + |
| +var armedStatelessBindings atomic.Pointer[statelessBindingPublisher] |
| + |
| +func (p *statelessBindingPublisher) publish(ctx context.Context, rec provision.Record, rt registry.Route) error { |
| + if p == nil || p.coordinator == nil || !rt.Stateless || rt.IsSidecar() || rt.IsForward() || !rt.Provisioned || rt.Status != "ready" || rt.Host != rec.Host || rt.Account != rec.Account || rt.App != rec.App || rt.Assigned != rec.FleetBoxAt { |
| + return nil |
| + } |
| + fleet, ok := registry.FleetPlacement(rec.Region) |
| + if !ok || fleet != p.fleet || rec.FleetBox != p.box || rec.Coordinator == nil || rec.Coordinator.Box != p.box { |
| + return nil |
| + } |
| + incarnation, err := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| + if err != nil { |
| + return err |
| + } |
| + id := replicas.Identity{Account: rec.Account, App: rec.App, Host: rec.Host, Incarnation: incarnation} |
| + a := p.coordinator.Held(id) |
| + if a == nil { |
| + return nil |
| + } |
| + state, _ := json.Marshal(struct { |
| + Identity replicas.Identity |
| + Term, Box, Version string |
| + Deploy *replicas.Deploy |
| + }{id, rec.Coordinator.Term, p.box, rt.ServingVersion, rec.ReplicaDeploy}) |
| + p.mu.Lock() |
| + defer p.mu.Unlock() |
| + if p.published[rec.Host] == string(state) { |
| + return nil |
| + } |
| + binding := FleetBinding{Host: rec.Host, Fleet: p.fleet, Box: p.box, BoundAt: time.Now().UnixMilli()} |
| + if err := writeFleetBinding(ctx, p.store, binding, true, a); err != nil { |
| + return err |
| + } |
| + if p.published == nil { |
| + p.published = make(map[string]string) |
| + } |
| + p.published[rec.Host] = string(state) |
| + return nil |
| +} |
diff --git a/internal/host/stateless_binding_test.go b/internal/host/stateless_binding_test.go| new file mode 100644 |
| index 000000000..c1d9d520b |
| --- /dev/null |
| +++ b/internal/host/stateless_binding_test.go |
| @@ -0,0 +1,141 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "github.com/carlosframework/platform/internal/activator" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/replicas" |
| + "os" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +func TestStatelessBindingOnlyCoordinator(t *testing.T) { |
| + priorDue := backstopFullDue.Load() |
| + t.Cleanup(func() { backstopFullDue.Store(priorDue) }) |
| + ctx := context.Background() |
| + store := release.NewDirStore(t.TempDir()) |
| + reg := testRegistry(t) |
| + rt := registry.Route{Host: "shop.example.com", Kind: "instance", Stateless: true} |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + fleetBindingHook(store, "fleet", "follower")(rt) |
| + publishResidentFleetBindings(store, "fleet", "follower", reg) |
| + if _, _, err := store.Get(ctx, fleetBindingKey("fleet", rt.Host)); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("follower wrote a binding: %v", err) |
| + } |
| + if !backstopFullDue.Load() { |
| + t.Fatal("stateless wake did not signal reconcile") |
| + } |
| + rec := provision.Record{Account: "bab", App: "shop", Host: rt.Host, Region: "fleet/fleet", FleetBox: "coordinator", CreatedAt: time.Unix(1, 0), Coordinator: &replicas.Coordinator{Term: "term", Box: "coordinator", BootID: "boot"}} |
| + incarnation, err := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + id := replicas.Identity{Account: rec.Account, App: rec.App, Host: rec.Host, Incarnation: incarnation} |
| + coordinator := activator.NewCoordinatorClient("coordinator", "boot", func(context.Context, replicas.Identity, replicas.CoordinatorChange) (replicas.CoordinatorReply, error) { |
| + return replicas.CoordinatorReply{Lease: *rec.Coordinator, RemainingMS: replicas.CoordinatorTTL.Milliseconds()}, nil |
| + }, nil) |
| + t.Cleanup(func() { coordinator.Close(ctx) }) |
| + count := &statelessBindingStore{Store: store} |
| + publisher := &statelessBindingPublisher{store: count, fleet: "fleet", box: "coordinator", coordinator: coordinator} |
| + rt.Account, rt.App, rt.Provisioned, rt.Status, rt.ServingVersion = rec.Account, rec.App, true, "ready", "v1" |
| + if err := publisher.publish(ctx, rec, rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + if count.gets != 0 || count.puts != 0 { |
| + t.Fatal("non-coordinator attempted binding I/O") |
| + } |
| + if _, err := coordinator.Acquire(ctx, id); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := publisher.publish(ctx, rec, rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + if count.gets != 1 || count.puts != 1 { |
| + t.Fatalf("coordinator requests: get=%d put=%d", count.gets, count.puts) |
| + } |
| + raw, _, err := store.Get(ctx, fleetBindingKey("fleet", rt.Host)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var binding FleetBinding |
| + if err := json.Unmarshal(raw, &binding); err != nil || binding.Box != "coordinator" { |
| + t.Fatalf("binding: %+v, %v", binding, err) |
| + } |
| + rec.Coordinator.ExpiresAt = time.Now().Add(time.Hour) |
| + if err := publisher.publish(ctx, rec, rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + if count.gets != 1 || count.puts != 1 { |
| + t.Fatal("unchanged/renewed state republished") |
| + } |
| + rec.ReplicaDeploy = &replicas.Deploy{ID: "deploy", Phase: "released", Revision: 2} |
| + if err := publisher.publish(ctx, rec, rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + if count.puts != 2 { |
| + t.Fatal("deploy change did not publish") |
| + } |
| + // A read that outlives authority cannot reach either CAS write. |
| + count.afterGet = func() { coordinator.Close(ctx) } |
| + rec.ReplicaDeploy.Revision++ |
| + if err := publisher.publish(ctx, rec, rt); err == nil { |
| + t.Fatal("lost coordinator wrote after the binding read") |
| + } |
| + if count.puts != 2 { |
| + t.Fatal("publication after authority loss") |
| + } |
| + if err := publisher.publish(ctx, rec, rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + if count.gets != 3 { |
| + t.Fatal("follower kept polling binding") |
| + } |
| +} |
| + |
| +type statelessBindingStore struct { |
| + release.Store |
| + gets, puts int |
| + afterGet func() |
| +} |
| + |
| +func (s *statelessBindingStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + s.gets++ |
| + body, etag, err := s.Store.Get(ctx, key) |
| + if s.afterGet != nil { |
| + s.afterGet() |
| + } |
| + return body, etag, err |
| +} |
| +func (s *statelessBindingStore) Put(ctx context.Context, key string, body []byte, etag string) (string, error) { |
| + s.puts++ |
| + return s.Store.Put(ctx, key, body, etag) |
| +} |
| + |
| +func TestStatelessBindingSignalsExistingTick(t *testing.T) { |
| + prior := backstopFullDue.Load() |
| + t.Cleanup(func() { backstopFullDue.Store(prior) }) |
| + backstopFullDue.Store(false) |
| + f := newFastFixture(t) |
| + passes := 0 |
| + d := f.deps(func() { passes++ }) |
| + d.fullDue = &backstopFullDue |
| + now := time.Unix(1754640000, 0) |
| + fastTick(context.Background(), d, now) |
| + fleetBindingHook(f.store, "fleet", "box")(registry.Route{Host: "shop.example.com", Stateless: true}) |
| + fastTick(context.Background(), d, now.Add(10*time.Second)) |
| + if passes != 1 || backstopFullDue.Load() { |
| + t.Fatal("wake did not run one existing reconcile pass") |
| + } |
| + fastTick(context.Background(), d, now.Add(20*time.Second)) |
| + if passes != 1 { |
| + t.Fatal("wake added recurring binding work") |
| + } |
| +} |
diff --git a/internal/provision/reconcile.go b/internal/provision/reconcile.go| index 7f3d81da0..f95d4b22a 100644 |
| --- a/internal/provision/reconcile.go |
| +++ b/internal/provision/reconcile.go |
| @@ -145,6 +145,9 @@ type ReconcileConfig struct { |
| // an untrashed app that carries an interactive-session marker (rootfs |
| // apps spec §5.10). It must not block. Nil: no one is told. |
| InteractiveSeen func(rec Record) |
| + // StatelessBinding consumes an applied instance record and its serving |
| + // projection. The host supplies the coordinator-gated existing-key writer. |
| + StatelessBinding func(context.Context, Record, registry.Route) error |
| // Seeds is the seeded-instances wiring (seedreconcile.go). Nil: every |
| // seeded record is left alone. |
| Seeds *SeedHooks |
| @@ -2099,6 +2102,11 @@ func Reconcile(ctx context.Context, s release.Store, reg *registry.Registry, cfg |
| want.Socket, existing.Socket, want.DBPath, existing.DBPath, rec.Host) |
| } |
| } |
| + if applied && existing.Stateless && !cfg.DryRun && cfg.StatelessBinding != nil { |
| + if err := cfg.StatelessBinding(ctx, rec, existing); err != nil { |
| + return err |
| + } |
| + } |
| pr.applied[rec.Host] = applied |
| default: |
| // A route exists that this pass did not mint. If it is the same |
diff --git a/internal/provision/stateless_binding_test.go b/internal/provision/stateless_binding_test.go| new file mode 100644 |
| index 000000000..fea472da3 |
| --- /dev/null |
| +++ b/internal/provision/stateless_binding_test.go |
| @@ -0,0 +1,64 @@ |
| +package provision |
| + |
| +import ( |
| + "context" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +func TestStatelessBindingUsesExistingReconcile(t *testing.T) { |
| + s := dirStore(t) |
| + acct := writeAccountManifest(t, s, acctID, "Example") |
| + if err := PutManifest(ctx, s, acct, "shop", Manifest{Schema: 1, App: "shop", Domain: "example.com", Pool: "shop"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + rec := Record{Schema: 1, Host: "shop.example.com", Slug: "shop", App: "shop", Account: acct, Pool: "shop", Region: "ie", Channel: "stable", CreatedAt: time.Unix(1, 0)} |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := regWith(t) |
| + defer reg.Close() |
| + cfg := ReconcileConfig{Region: "ie", Pools: []string{"shop"}, DataDir: t.TempDir()} |
| + seen := 0 |
| + cfg.StatelessBinding = func(_ context.Context, r Record, rt registry.Route) error { |
| + seen++ |
| + if r.Host != rec.Host || r.CreatedAt != rec.CreatedAt || !rt.Stateless || rt.ServingVersion != "v1" { |
| + t.Fatalf("wrong binding observation: %+v %+v", r, rt) |
| + } |
| + return nil |
| + } |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if seen != 0 { |
| + t.Fatal("mint published before verified serving mode") |
| + } |
| + if err := reg.SetServingMode(rec.Host, "v1", true); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if seen != 1 { |
| + t.Fatalf("binding callback count=%d", seen) |
| + } |
| + cfg.DryRun = true |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if seen != 1 { |
| + t.Fatal("dry run published binding") |
| + } |
| + cfg.DryRun = false |
| + if err := reg.SetServingMode(rec.Host, "v2", false); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if seen != 1 { |
| + t.Fatal("stateful row used coordinator publication") |
| + } |
| +} |