fix: answer transient coordinator store errors with 503
Return 503 for transient coordinator and release store failures while preserving conflict refusals and identical authentication failures. Exercise the real console handler and fleet adapter through renewal failure, recovery and fencing, including release store failure.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/console/replicacoordinator.go+12 −2internal/host/replicacoordinator_test.go+133 −0
CI failed — run details
Intent
<task-notification> <task-id>b8fl3opi4</task-id> <tool-use-id>toolu_01CCvTNW98XYsaTDDrZ3jZHg</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/412bbef6-9468-4e8d-b9fe-5343bad21de8/tasks/b8fl3opi4.output</output-file> <status>completed</status> <summary>Background command "Wait for Task 3a implementer" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 16.6 g CO₂e (2,065,655 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/console/replicacoordinator.go b/internal/console/replicacoordinator.go| index 030f3e155..95a0b45e9 100644 |
| --- a/internal/console/replicacoordinator.go |
| +++ b/internal/console/replicacoordinator.go |
| @@ -90,7 +90,12 @@ func (s *Server) handleReplicaCoordinator(w http.ResponseWriter, r *http.Request |
| return |
| } |
| if err != nil { |
| - jsonError(w, http.StatusConflict, replicaCoordinatorChanged) |
| + status := http.StatusServiceUnavailable |
| + var held *replicas.CoordinatorHeldError |
| + if errors.Is(err, replicas.ErrFenced) || errors.Is(err, replicas.ErrInvalid) || errors.As(err, &held) { |
| + status = http.StatusConflict |
| + } |
| + jsonError(w, status, replicaCoordinatorChanged) |
| return |
| } |
| if req.Op != "renew" { |
| @@ -150,7 +155,12 @@ func (s *Server) handleReplicaRelease(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| if err != nil { |
| - jsonError(w, http.StatusConflict, replicaReleaseChanged) |
| + status := http.StatusServiceUnavailable |
| + var held *replicas.CoordinatorHeldError |
| + if errors.Is(err, replicas.ErrFenced) || errors.Is(err, replicas.ErrInvalid) || errors.As(err, &held) { |
| + status = http.StatusConflict |
| + } |
| + jsonError(w, status, replicaReleaseChanged) |
| return |
| } |
| if changed { |
diff --git a/internal/host/replicacoordinator_test.go b/internal/host/replicacoordinator_test.go| index 941487a88..cf42cea5b 100644 |
| --- a/internal/host/replicacoordinator_test.go |
| +++ b/internal/host/replicacoordinator_test.go |
| @@ -9,7 +9,10 @@ import ( |
| "net/http" |
| "net/http/httptest" |
| "path/filepath" |
| + "strings" |
| + "sync/atomic" |
| "testing" |
| + "testing/synctest" |
| "time" |
| "github.com/carlosframework/platform/internal/activator" |
| @@ -337,3 +340,133 @@ func TestCoordinatorAPITransientStatus(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +type replicaTransientStore struct { |
| + release.Store |
| + fail atomic.Bool |
| + attempts atomic.Int32 |
| +} |
| + |
| +func (s *replicaTransientStore) Put(ctx context.Context, key string, body []byte, etag string) (string, error) { |
| + if s.fail.Load() && strings.HasSuffix(key, "/shop.example.com.json") { |
| + s.attempts.Add(1) |
| + return "", context.DeadlineExceeded |
| + } |
| + return s.Store.Put(ctx, key, body, etag) |
| +} |
| + |
| +type replicaHandlerTransport struct { |
| + handler http.Handler |
| + status int |
| +} |
| + |
| +func (tr *replicaHandlerTransport) RoundTrip(r *http.Request) (*http.Response, error) { |
| + w := httptest.NewRecorder() |
| + tr.handler.ServeHTTP(w, r) |
| + tr.status = w.Code |
| + return w.Result(), nil |
| +} |
| + |
| +func TestCoordinatorFleetTransientRenewal(t *testing.T) { |
| + ctx := context.Background() |
| + root := t.TempDir() |
| + path := filepath.Join(root, "registry.db") |
| + reg, err := registry.Open(path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { reg.Close() }) |
| + if ok, err := reg.CreateFleet(registry.Fleet{Name: "home", Account: "acme", Bucket: "home-data"}); err != nil || !ok { |
| + t.Fatalf("fleet: %v", err) |
| + } |
| + token := "replica-test-token" |
| + hash := sha256.Sum256([]byte(token)) |
| + if err := reg.AddFleetBox(registry.FleetBox{Fleet: "home", Label: "b1", TokenHash: hex.EncodeToString(hash[:])}); err != nil { |
| + t.Fatal(err) |
| + } |
| + store := &replicaTransientStore{Store: release.NewDirStore(filepath.Join(root, "records"))} |
| + rec := provision.Record{Schema: 1, Host: "shop.example.com", Slug: "shop", App: "shop", Account: "acme", Pool: "shop", Region: "fleet/home", Channel: "stable", CreatedAt: time.Now(), CreatedBy: "test"} |
| + if err := provision.CreateRecord(ctx, store, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := provision.SetRecordFleetBox(ctx, store, rec.Account, rec.App, rec.Host, provision.FleetBoxExpect{Region: rec.Region, CreatedAt: rec.CreatedAt}, "b1", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + db, err := console.Open(filepath.Join(root, "console.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { db.Close() }) |
| + srv, err := console.NewServer(console.Config{Registry: path, Releases: store}, db) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + transport := &replicaHandlerTransport{handler: srv} |
| + api := &replicaCoordinatorAPI{base: "http://console.example.com", fleet: "home", box: "b1", token: token, client: &http.Client{Transport: transport}} |
| + _, err = api.coordinator(ctx, replicas.Identity{Account: rec.Account, App: rec.App, Host: rec.Host}, replicas.CoordinatorChange{Op: "renew"}) |
| + if !errors.Is(err, replicas.ErrFenced) { |
| + t.Fatalf("preflight: %v", err) |
| + } |
| + synctest.Test(t, func(t *testing.T) { |
| + client := activator.NewCoordinatorClient("b1", "boot", api.coordinator, api.releaseState) |
| + defer client.Close(ctx) |
| + inc, 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: inc} |
| + authority, err := client.Acquire(ctx, id) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + until, err := authority.SafeUntil() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + time.Sleep(replicas.CoordinatorRenewAfter) |
| + store.fail.Store(true) |
| + client.Tick(ctx) |
| + if transport.status != http.StatusServiceUnavailable || store.attempts.Load() == 0 { |
| + t.Fatalf("renew status %d; failed writes %d", transport.status, store.attempts.Load()) |
| + } |
| + after, err := authority.SafeUntil() |
| + if err != nil || !after.Equal(until) { |
| + t.Fatalf("transient renewal changed authority: %v, %v -> %v", err, until, after) |
| + } |
| + select { |
| + case <-authority.Lost(): |
| + t.Fatal("transient renewal closed authority") |
| + default: |
| + } |
| + _, err = client.ReleaseState(ctx, id, replicas.ReleaseChange{Op: "begin", ID: "deploy", Target: replicas.Build{Version: "v1", Digest: "canonical"}}) |
| + if err == nil || errors.Is(err, replicas.ErrFenced) || transport.status != http.StatusServiceUnavailable { |
| + t.Fatalf("release store failure: HTTP %d, %v", transport.status, err) |
| + } |
| + store.fail.Store(false) |
| + time.Sleep(time.Second) |
| + client.Tick(ctx) |
| + after, err = authority.SafeUntil() |
| + if transport.status != http.StatusOK || err != nil || !after.After(until) { |
| + t.Fatalf("renewal recovery: HTTP %d, %v, %v", transport.status, err, after) |
| + } |
| + current, err := provision.GetRecord(ctx, store, id.Account, id.App, id.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + _, err = provision.SetRecordCoordinator(ctx, store, id, replicas.CoordinatorChange{Op: "release", Term: current.Coordinator.Term, Box: "b1", BootID: "boot"}, time.Now()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + time.Sleep(replicas.CoordinatorRenewAfter) |
| + client.Tick(ctx) |
| + if transport.status != http.StatusConflict || !errors.Is(authority.Err(), replicas.ErrFenced) { |
| + t.Fatalf("fenced renewal: HTTP %d, %v", transport.status, authority.Err()) |
| + } |
| + select { |
| + case <-authority.Lost(): |
| + default: |
| + t.Fatal("fence kept authority open") |
| + } |
| + }) |
| +} |