carlos / platform Public

replica placement: log failed steps once per backoff step (#1246)

The placement worker dropped its retire, acquire and publish errors, so a
placement that never succeeded retried with no trace of why. Each failure
is now logged with the host, the step and the error.

A step is one wait of the backoff. A record that moves while an attempt is
out leaves the backoff where it was and the next tick tries again at once,
so those attempts share one line rather than one each. A new revision
starts a new backoff and may log again. A fenced retirement is finished,
and a worker stopping is not a failure, so neither is logged.

The retry decision and the backoff are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev cef01808a12b24559205d423bb60e17e0216c40c parent 0e0dec7
5 files changed, +261 −4
  • docs/superpowers/notes/census-proposal-small-fixes-1246-1248.md +19 −0
  • internal/host/replicaplacement.go +33 −4
  • internal/host/replicaplacement_log_test.go +203 −0
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −0
  • internal/provision/testdata/plan14-root-reads-small-fixes-1246-1248.tsv +5 −0
diff --git a/docs/superpowers/notes/census-proposal-small-fixes-1246-1248.md b/docs/superpowers/notes/census-proposal-small-fixes-1246-1248.md
new file mode 100644
index 000000000..1089067c6
--- /dev/null
+++ b/docs/superpowers/notes/census-proposal-small-fixes-1246-1248.md
@@ -0,0 +1,19 @@
+# Census approval: small-fixes-1246-1248
+
+Ruling `census-ruling-small-fixes-1246-1248`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26).
+
+| unit | change | read sites | what changed |
+|---|---|---|---|
+| `internal/host/replicaplacement.go` `replicaPlacementWorker.run` | body | unchanged (6) | Logs the retire, acquire and publish failures it used to drop, at most once per backoff step, outside the worker lock. The retry decision, backoff arithmetic, deadlines and read sites are unchanged (6); no read added. |
+
+Envelopes re-pinned: `internal/host`.
+
+Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner.
+
+Recorded dispositions, which each change must still satisfy:
+
+- `internal/host/replicaplacement.go` `replicaPlacementWorker.run`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=census-ruling-stateless-replicas-2;basis=Final fix I3: retain completed lifetime/revision observations and skip their work; only confirmed current binding publication after coordinator acquisition, or existing successful/fenced retirement ACK, completes work. Existing local registry read, authority and retirement checks, and 30-second request deadline remain. No new reads or polling.;rev3=census-ruling-staticcheck-sa4006;basis=SA4006 dead store: publishCurrent's error is discarded explicitly (done, _ =) with a comment; it was assigned to err and never read, and publishCurrent returns false with every error, so done and the retry backoff are exactly as before. No read added, read sites unchanged (6).
+
+Seal: `ec3c40098ec0a652c735d54925f334b9b9680c9e2b6cd0c3d9497b8a615fb86f` → `f4994de79e61619e4c6def7782e458518a5466a75ec484cdb921d85934653bb4`.
+
+Overlay: `internal/provision/testdata/plan14-root-reads-small-fixes-1246-1248.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/internal/host/replicaplacement.go b/internal/host/replicaplacement.go
index ad40559be..06f16797f 100644
--- a/internal/host/replicaplacement.go
+++ b/internal/host/replicaplacement.go
@@ -3,6 +3,7 @@ package host
import (
"context"
"errors"
+ "log"
"net/http"
"slices"
"strings"
@@ -21,6 +22,9 @@ type replicaPlacementWork struct {
retry time.Time
delay time.Duration
done bool
+ // logged: this backoff step has reported its failure. A step ends when
+ // the backoff advances; attempts a moved record repeats at once share it.
+ logged bool
}
type replicaPlacementWorker struct {
@@ -57,6 +61,7 @@ func (w *replicaPlacementWorker) observe(rec provision.Record) {
old.retry = time.Time{}
old.delay = 0
old.done = false
+ old.logged = false
}
old.rec = rec
w.pending[rec.Host] = old
@@ -108,6 +113,8 @@ func (w *replicaPlacementWorker) run(ctx context.Context, now time.Time) {
id := replicas.Identity{Account: rec.Account, App: rec.App, Host: rec.Host, Incarnation: inc}
rctx, cancel := context.WithTimeout(ctx, 30*time.Second)
done := false
+ var step string
+ var failure error
// Serialize local removal/proof against reconcile minting a new lifetime.
reconcileMu.Lock()
rt, err := w.reg.Get(rec.Host)
@@ -120,28 +127,50 @@ func (w *replicaPlacementWorker) run(ctx context.Context, now time.Time) {
if proven && w.retired != nil {
err = w.retired(rctx, id, a.Revision)
done = err == nil || errors.Is(err, replicas.ErrFenced)
+ if !done {
+ step, failure = "retire", err
+ }
} else if err == nil && a.Contains(w.box) && rt != nil && rt.ReplicaIncarnation == inc && rt.ReplicaRevision == a.Revision && provision.ReplicaCoordinatorBox(&rec, now) == w.box && w.coordinator != nil {
- if _, err = w.coordinator.Acquire(rctx, id); err == nil && w.publisher != nil {
+ if _, err = w.coordinator.Acquire(rctx, id); err != nil {
+ step, failure = "acquire", err
+ } else if w.publisher != nil {
// Acquire's new term reaches the publisher through the next record pass.
// publishCurrent answers false with every error, so done alone
- // decides the retry. Like Acquire's, the error is not logged.
- done, _ = w.publisher.publishCurrent(rctx, rec, *rt)
+ // decides the retry.
+ if done, err = w.publisher.publishCurrent(rctx, rec, *rt); err != nil {
+ step, failure = "publish", err
+ }
}
}
cancel()
w.mu.Lock()
current := w.pending[rec.Host]
- if current.rec.RecordSeq == rec.RecordSeq && current.rec.CreatedAt == rec.CreatedAt && current.rec.ReplicaAssignment != nil && current.rec.ReplicaAssignment.Revision == a.Revision {
+ sameAssignment := current.rec.CreatedAt == rec.CreatedAt && current.rec.ReplicaAssignment != nil && current.rec.ReplicaAssignment.Revision == a.Revision
+ // An attempt the assignment has moved past answers for its own step.
+ logged := p.logged
+ if sameAssignment {
+ logged = current.logged
+ }
+ // A worker stopping fails what it was doing; that is not news.
+ report := failure != nil && !logged && ctx.Err() == nil
+ if sameAssignment && current.rec.RecordSeq == rec.RecordSeq {
if done {
current.done = true
w.pending[rec.Host] = current
} else {
p.delay = min(max(p.delay*2, time.Second), time.Minute)
p.retry = now.Add(p.delay)
+ p.logged = false
w.pending[rec.Host] = p
}
+ } else if sameAssignment && report {
+ current.logged = true
+ w.pending[rec.Host] = current
}
w.mu.Unlock()
+ if report {
+ log.Printf("agent: replica placement %s: %s failed, will retry: %v", rec.Host, step, failure)
+ }
}
}
diff --git a/internal/host/replicaplacement_log_test.go b/internal/host/replicaplacement_log_test.go
new file mode 100644
index 000000000..a48d081b4
--- /dev/null
+++ b/internal/host/replicaplacement_log_test.go
@@ -0,0 +1,203 @@
+package host
+
+import (
+ "context"
+ "errors"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/activator"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/replicas"
+)
+
+const replicaPlacementLogPrefix = "agent: replica placement "
+
+// replicaRetiringWorker is a worker whose only work is to report rec's host
+// retired from box a, through retired.
+func replicaRetiringWorker(t *testing.T, retired func(context.Context, replicas.Identity, uint64) error) (*replicaPlacementWorker, provision.Record) {
+ t.Helper()
+ rec := provision.Record{Account: "bab", App: "shop", Host: "shop.example.com", Region: "fleet/pi", CreatedAt: time.Unix(1, 0), RecordSeq: 1, ReplicaAssignment: &replicas.Assignment{Revision: 3, Desired: 1, Boxes: []string{"b"}, Retiring: []string{"a"}}}
+ w := &replicaPlacementWorker{reg: testRegistry(t), box: "a", quiet: func(string) bool { return true }, empty: func(registry.Route) error { return nil }, retired: retired}
+ w.observe(rec)
+ return w, rec
+}
+
+// A placement that keeps failing says so once for each wait of its backoff,
+// with the host, the step and the error.
+func TestReplicaPlacementLogsOncePerBackoffStep(t *testing.T) {
+ logs := captureLogBuffer(t)
+ ctx := context.Background()
+ attempts := 0
+ w, rec := replicaRetiringWorker(t, func(context.Context, replicas.Identity, uint64) error {
+ attempts++
+ return errors.New("console said no")
+ })
+ now := time.Now()
+ for _, c := range []struct {
+ at time.Duration
+ attempts int
+ }{
+ {0, 1},
+ {0, 1}, // inside the first second's wait
+ {time.Second, 2},
+ {2 * time.Second, 2}, // the wait is two seconds now
+ {3 * time.Second, 3},
+ {6 * time.Second, 3},
+ {7 * time.Second, 4},
+ } {
+ w.run(ctx, now.Add(c.at))
+ if attempts != c.attempts {
+ t.Fatalf("at +%s: %d attempts, want %d: the backoff changed", c.at, attempts, c.attempts)
+ }
+ if got := logs.count(replicaPlacementLogPrefix); got != c.attempts {
+ t.Fatalf("at +%s: %d lines for %d backoff steps:\n%s", c.at, got, c.attempts, logs.String())
+ }
+ }
+ line, _, _ := strings.Cut(logs.String(), "\n")
+ for _, want := range []string{rec.Host, "retire", "console said no"} {
+ if !strings.Contains(line, want) {
+ t.Errorf("log line %q does not name %q", line, want)
+ }
+ }
+}
+
+// A record that moves while an attempt is out leaves the backoff where it
+// was, so the next tick tries again at once. Those attempts share one step
+// and one line, however many there are.
+func TestReplicaPlacementTightRetryLoopLogsOnce(t *testing.T) {
+ logs := captureLogBuffer(t)
+ ctx := context.Background()
+ attempts := 0
+ var moved func()
+ w, rec := replicaRetiringWorker(t, func(context.Context, replicas.Identity, uint64) error {
+ attempts++
+ if moved != nil {
+ moved()
+ }
+ return errors.New("console said no")
+ })
+ moved = func() {
+ rec.RecordSeq++
+ w.observe(rec)
+ }
+ now := time.Now()
+ for i := 1; i <= 20; i++ {
+ w.run(ctx, now)
+ if attempts != i {
+ t.Fatalf("pass %d: %d attempts: a moved record must still retry at once", i, attempts)
+ }
+ }
+ if got := logs.count(replicaPlacementLogPrefix); got != 1 {
+ t.Fatalf("%d lines for 20 attempts in one backoff step, want 1:\n%s", got, logs.String())
+ }
+
+ // With the record still, this attempt ends the step: it has been logged
+ // already, and the next step gets its own line.
+ moved = nil
+ w.run(ctx, now)
+ if p := w.pending[rec.Host]; p.delay != time.Second || !p.retry.Equal(now.Add(time.Second)) {
+ t.Fatalf("backoff after the step: delay %s, retry %s", p.delay, p.retry.Sub(now))
+ }
+ if got := logs.count(replicaPlacementLogPrefix); got != 1 {
+ t.Fatalf("%d lines after the step ended, want 1:\n%s", got, logs.String())
+ }
+ w.run(ctx, now.Add(time.Second))
+ if got := logs.count(replicaPlacementLogPrefix); got != 2 {
+ t.Fatalf("%d lines in the second step, want 2:\n%s", got, logs.String())
+ }
+
+ // A new revision starts its backoff again, and its first failure is news.
+ next := *rec.ReplicaAssignment
+ next.Revision++
+ rec.ReplicaAssignment = &next
+ rec.RecordSeq++
+ w.observe(rec)
+ w.run(ctx, now.Add(time.Second))
+ if got := logs.count(replicaPlacementLogPrefix); got != 3 {
+ t.Fatalf("%d lines after a new revision, want 3:\n%s", got, logs.String())
+ }
+}
+
+// What is not a failure to retry is not logged as one.
+func TestReplicaPlacementQuietWhenNothingFailed(t *testing.T) {
+ t.Run("a fenced retirement is finished", func(t *testing.T) {
+ logs := captureLogBuffer(t)
+ w, rec := replicaRetiringWorker(t, func(context.Context, replicas.Identity, uint64) error { return replicas.ErrFenced })
+ w.run(context.Background(), time.Now())
+ if !w.pending[rec.Host].done || logs.count(replicaPlacementLogPrefix) != 0 {
+ t.Fatalf("done %v, log:\n%s", w.pending[rec.Host].done, logs.String())
+ }
+ })
+ t.Run("the worker stopping", func(t *testing.T) {
+ logs := captureLogBuffer(t)
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ w, _ := replicaRetiringWorker(t, func(ctx context.Context, _ replicas.Identity, _ uint64) error { return ctx.Err() })
+ w.run(ctx, time.Now())
+ if logs.count(replicaPlacementLogPrefix) != 0 {
+ t.Fatalf("shutdown logged as a failure:\n%s", logs.String())
+ }
+ })
+}
+
+// placementPutRefusal refuses every write.
+type placementPutRefusal struct {
+ release.Store
+ err error
+}
+
+func (s *placementPutRefusal) Put(context.Context, string, []byte, string) (string, error) {
+ return "", s.err
+}
+
+// The coordinator's two steps name themselves.
+func TestReplicaPlacementLogsCoordinatorSteps(t *testing.T) {
+ now := time.Now()
+ lease := replicas.Coordinator{Term: "term", Box: "a", BootID: "boot", ExpiresAt: now.Add(time.Hour)}
+ for _, c := range []struct {
+ name, step, cause string
+ acquire error
+ put error
+ }{
+ {name: "acquire", step: "acquire", cause: "no coordinator today", acquire: errors.New("no coordinator today")},
+ {name: "publish", step: "publish", cause: "bucket refused", put: errors.New("bucket refused")},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ logs := captureLogBuffer(t)
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ reg := testRegistry(t)
+ rec := provision.Record{Account: "bab", App: "shop", Host: "shop.example.com", Region: "fleet/pi", FleetBox: "a", CreatedAt: time.Unix(1, 0), Hibernate: new(bool), RecordSeq: 1, Coordinator: &lease, ReplicaAssignment: &replicas.Assignment{Revision: 1, Desired: 2, Boxes: []string{"a", "b"}}}
+ client := activator.NewCoordinatorClient("a", "boot", func(context.Context, replicas.Identity, replicas.CoordinatorChange) (replicas.CoordinatorReply, error) {
+ if c.acquire != nil {
+ return replicas.CoordinatorReply{}, c.acquire
+ }
+ return replicas.CoordinatorReply{Lease: lease, RemainingMS: replicas.CoordinatorTTL.Milliseconds()}, nil
+ }, nil)
+ defer client.Close(ctx)
+ store := &placementPutRefusal{Store: release.NewDirStore(t.TempDir()), err: c.put}
+ w := &replicaPlacementWorker{reg: reg, box: "a", coordinator: client, publisher: &statelessBindingPublisher{store: store, fleet: "pi", box: "a", coordinator: client}}
+ if err := reg.Upsert(provision.RouteFor(rec, t.TempDir(), "", false, nil)); err != nil {
+ t.Fatal(err)
+ }
+ w.observe(rec)
+ w.run(ctx, now)
+ w.run(ctx, now) // inside the wait
+ if got := logs.count(replicaPlacementLogPrefix); got != 1 {
+ t.Fatalf("%d lines, want 1:\n%s", got, logs.String())
+ }
+ for _, want := range []string{rec.Host, c.step, c.cause} {
+ if !strings.Contains(logs.String(), want) {
+ t.Errorf("log %q does not name %q", logs.String(), want)
+ }
+ }
+ if w.pending[rec.Host].done {
+ t.Fatal("a failed step counted as done")
+ }
+ })
+ }
+}
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index 8229c3b70..75c2a6702 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -248,3 +248,4 @@ plan14-root-reads-mail-submission-followups.tsv c9cd3c8fdf31240b83affa0b2798422f
plan14-root-reads-staticcheck-sa4006.tsv 33e5eaaa9b2d80f6f8f5de8d04e908e39ecfc820c5fe7b44c870e671565e1063
plan14-root-reads-aws-followups-1143.tsv 33e5eaaa9b2d80f6f8f5de8d04e908e39ecfc820c5fe7b44c870e671565e1063
plan14-root-reads-fix-dbforward-reuse-1032.tsv ec3c40098ec0a652c735d54925f334b9b9680c9e2b6cd0c3d9497b8a615fb86f
+plan14-root-reads-small-fixes-1246-1248.tsv f4994de79e61619e4c6def7782e458518a5466a75ec484cdb921d85934653bb4
diff --git a/internal/provision/testdata/plan14-root-reads-small-fixes-1246-1248.tsv b/internal/provision/testdata/plan14-root-reads-small-fixes-1246-1248.tsv
new file mode 100644
index 000000000..b1c9971b3
--- /dev/null
+++ b/internal/provision/testdata/plan14-root-reads-small-fixes-1246-1248.tsv
@@ -0,0 +1,5 @@
+# Census overlay small-fixes-1246-1248, written by census-propose (#369). census-ruling-small-fixes-1246-1248, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-small-fixes-1246-1248.md.
+# 1 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
+@legacy a21f3817e4d7592b66b0229a58c87f6f7a9c59ad30d6e65fe6c1dd84dfe34ce3 internal/host/replicaplacement.go replicaPlacementWorker.run legacy accepted-legacy accepted-legacy;rev3=census-ruling-stateless-replicas-2;basis=Final fix I3: retain completed lifetime/revision observations and skip their work; only confirmed current binding publication after coordinator acquisition, or existing successful/fenced retirement ACK, completes work. Existing local registry read, authority and retirement checks, and 30-second request deadline remain. No new reads or polling.;rev3=census-ruling-staticcheck-sa4006;basis=SA4006 dead store: publishCurrent's error is discarded explicitly (done, _ =) with a comment; it was assigned to err and never read, and publishCurrent returns false with every error, so done and the retry backoff are exactly as before. No read added, read sites unchanged (6).;rev3=census-ruling-small-fixes-1246-1248;basis=Logs the retire, acquire and publish failures it used to drop, at most once per backoff step, outside the worker lock. The retry decision, backoff arithmetic, deadlines and read sites are unchanged (6); no read added. registry:ReplicaIncarnation@1337,registry:ReplicaRevision@1369,whole:Route@1049,whole:Route@1324,whole:Route@1843,whole:Route@897 8d4b85a98c41b8ad7736163024cd0cef3b0f0687e1e6f76fdee509cd56a27459
+internal/host/replicaplacement.go replicaPlacementWorker.run legacy accepted-legacy accepted-legacy;rev3=census-ruling-stateless-replicas-2;basis=Final fix I3: retain completed lifetime/revision observations and skip their work; only confirmed current binding publication after coordinator acquisition, or existing successful/fenced retirement ACK, completes work. Existing local registry read, authority and retirement checks, and 30-second request deadline remain. No new reads or polling.;rev3=census-ruling-staticcheck-sa4006;basis=SA4006 dead store: publishCurrent's error is discarded explicitly (done, _ =) with a comment; it was assigned to err and never read, and publishCurrent returns false with every error, so done and the retry backoff are exactly as before. No read added, read sites unchanged (6).;rev3=census-ruling-small-fixes-1246-1248;basis=Logs the retire, acquire and publish failures it used to drop, at most once per backoff step, outside the worker lock. The retry decision, backoff arithmetic, deadlines and read sites are unchanged (6); no read added. registry:ReplicaIncarnation@1337,registry:ReplicaRevision@1369,whole:Route@1049,whole:Route@1324,whole:Route@1843,whole:Route@897 8d4b85a98c41b8ad7736163024cd0cef3b0f0687e1e6f76fdee509cd56a27459
+@coverage internal/host 97ba6b1d1eb103f39aca148b55efad35e3aebec68614ac5462ea38fbff41391e