carlos / platform Public

region HA P3b-2: three-minute apply gate for group maintenance

claude agent, operated by paul@keymail.dev 5867ae636a4f1f47113b6c02de06216b8e31e692 parent 1cc9907
2 files changed, +176 −6
  • internal/host/systemapply.go +16 −6
  • internal/host/systemapply_groupgate_test.go +160 −0
diff --git a/internal/host/systemapply.go b/internal/host/systemapply.go
index 1bd5ad522..9ccb2d87e 100644
--- a/internal/host/systemapply.go
+++ b/internal/host/systemapply.go
@@ -65,11 +65,16 @@ const (
// deployment and grows with the deployment's app count. A fixed 20 s
// rolled every update back, and its rollback too. The longer window
// comes with fail-fast (gateFailFast), so a build that crashes still
- // rolls back in seconds. Claims-off boxes keep systemGateWait exactly.
+ // rolls back in seconds. Fixed claims-off boxes keep systemGateWait.
systemClaimsGateWait = 120 * time.Second
- systemGateSoak = 15 * time.Second
- systemVersionWait = 30 * time.Second
- systemBootstrapWait = 120 * time.Second
+ // systemGroupGateWait covers group maintenance startup: the listener
+ // bound at 40 s with S3 and SSM dropped. A member in Standby carries
+ // no traffic, so waiting costs no traffic and a shared-store outage
+ // must not read as a bad build. Failure still rolls back at the deadline.
+ systemGroupGateWait = 3 * time.Minute
+ systemGateSoak = 15 * time.Second
+ systemVersionWait = 30 * time.Second
+ systemBootstrapWait = 120 * time.Second
// How long --recover waits for the apply lock, and how often it asks.
// See waitForLock.
@@ -279,7 +284,7 @@ type systemApplier struct {
// unit's environment (carlos-system-update.service and
// carlos-system-recover.service both load host.env). It changes only the
// gate: the claims window and fail-fast (systemGateWaitFor,
- // gateFailFast). Off, the gate is exactly the 20 s one.
+ // gateFailFast). Group maintenance selects its own window from the marker.
claims bool
// store is the deploy store verifyPointer opened, kept for armFloor.
// Nil on --recover, which reads no store.
@@ -1659,6 +1664,9 @@ func (a *systemApplier) gate(ctx context.Context, target string) error {
wait, soak := a.gateWait, a.soak
if wait <= 0 {
wait = systemGateWaitFor(a.claims)
+ if groupMarkerPresent(a.etcDir) {
+ wait = systemGroupGateWait
+ }
}
if soak <= 0 {
soak = systemGateSoak
@@ -1715,7 +1723,9 @@ func systemApplyGate(getenv func(string) string) (bool, time.Duration) {
// the way production does. CARLOS_REGION_HA is not read here (region HA
// P1a): it no longer changes how an update begins.
func (a *systemApplier) readEnvironment(getenv func(string) string) {
- a.claims, a.gateWait = systemApplyGate(getenv)
+ // Resolve the default window in gate, where the group marker is known.
+ // Leave an explicit gateWait intact for callers testing a shorter window.
+ a.claims, _ = systemApplyGate(getenv)
}
// systemGateWaitFor is the gate's window: systemClaimsGateWait with
diff --git a/internal/host/systemapply_groupgate_test.go b/internal/host/systemapply_groupgate_test.go
new file mode 100644
index 000000000..5c615fa83
--- /dev/null
+++ b/internal/host/systemapply_groupgate_test.go
@@ -0,0 +1,160 @@
+package host
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "errors"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestSystemApplyGroupGateWindow(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ group bool
+ claims bool
+ override time.Duration
+ want time.Duration
+ }{
+ {name: "fixed", want: 20 * time.Second},
+ {name: "claims", claims: true, want: 120 * time.Second},
+ {name: "group", group: true, want: 3 * time.Minute},
+ {name: "group-with-claims", group: true, claims: true, want: 3 * time.Minute},
+ {name: "group-override", group: true, override: 7 * time.Second, want: 7 * time.Second},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f := newApplyFixture(t, "stable", "v1")
+ if tc.group {
+ dir := filepath.Join(f.a.etcDir, "carlos")
+ if err := os.MkdirAll(dir, 0700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "group.json"), []byte(`{}`), 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ f.a.gateWait = tc.override
+ f.a.readEnvironment(func(k string) string {
+ if k == "CARLOS_HOST_CLAIM" && tc.claims {
+ return "1"
+ }
+ if k == "CARLOS_GROUP_ADMISSION" || k == "CARLOS_REGION_HA" {
+ return "1"
+ }
+ return ""
+ })
+ f.probeErr = errors.New("connection refused")
+ start := f.now
+ if err := f.a.gate(t.Context(), "v1"); err == nil {
+ t.Fatal("gate accepted a listener that never answers")
+ }
+ if got := f.now.Sub(start); got != tc.want {
+ t.Fatalf("gate window = %s, want %s", got, tc.want)
+ }
+ })
+ }
+}
+
+func groupGateFixture(t *testing.T) (*applyFixture, systemRequest) {
+ t.Helper()
+ m, p := maintenanceFixture(t)
+ f, req := applyHappy(t)
+ f.a.etcDir = m.a.etcDir
+ f.a.groupMaintenance = m.a.groupMaintenance
+ for path, target := range map[string]*string{
+ systemBinaryPath(f.a.optDir): &p.OldSHA256,
+ systemCachePath(f.a.dataDir, req.Version): &p.NewSHA256,
+ } {
+ body, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ sum := sha256.Sum256(body)
+ *target = hex.EncodeToString(sum[:])
+ }
+ p.AllowRollback = true
+ writeMaintenancePermit(t, f, p)
+ f.a.readEnvironment(func(string) string { return "" })
+ return f, req
+}
+
+func TestSystemApplyGroupGatePassesListenerAt40s(t *testing.T) {
+ f, req := groupGateFixture(t)
+ restarts := slowListener(f, 40*time.Second)
+ if err := f.a.Run(t.Context(), false); err != nil {
+ t.Fatalf("group gate failed the listener at 40 s: %v", err)
+ }
+ if r := f.result(); r == nil || r.Outcome != systemOutcomeApplied || !r.matches("stable", "v2", req.PromotedAt) {
+ t.Fatalf("result = %+v, want applied", r)
+ }
+ if len(*restarts) != 1 {
+ t.Fatalf("edge restarted %d times, want 1 with no rollback", len(*restarts))
+ }
+ if elapsed := f.now.Sub((*restarts)[0]); elapsed != 40*time.Second+systemGateSoak {
+ t.Fatalf("gate took %s, want 40 s plus the soak", elapsed)
+ }
+}
+
+func TestSystemApplyGroupGateNeverAnswersRollsBack(t *testing.T) {
+ for _, restoredAnswers := range []bool{false, true} {
+ t.Run(map[bool]string{false: "both-fail", true: "restored-answers-at-40s"}[restoredAnswers], func(t *testing.T) {
+ f, req := groupGateFixture(t)
+ restarts := slowListener(f, 40*time.Second)
+ probe := f.a.ops.probeEdge
+ f.a.ops.probeEdge = func() error {
+ if !restoredAnswers || len(*restarts) < 2 {
+ return errors.New("connection refused")
+ }
+ return probe()
+ }
+ if err := f.a.Run(t.Context(), false); err == nil {
+ t.Fatal("gate accepted a candidate that never answers")
+ }
+ want := systemOutcomeRollbackFailed
+ if restoredAnswers {
+ want = systemOutcomeRolledBack
+ }
+ if r := f.result(); r == nil || r.Outcome != want || !r.matches("stable", "v2", req.PromotedAt) {
+ t.Fatalf("result = %+v, want %s", r, want)
+ }
+ if len(*restarts) != 2 {
+ t.Fatalf("edge restarted %d times, want apply and rollback", len(*restarts))
+ }
+ if waited := (*restarts)[1].Sub((*restarts)[0]); waited != 3*time.Minute {
+ t.Fatalf("rollback began after %s, want 3 min", waited)
+ }
+ wantWait := 3 * time.Minute
+ if restoredAnswers {
+ wantWait = 40*time.Second + systemGateSoak
+ }
+ if waited := f.now.Sub((*restarts)[1]); waited != wantWait {
+ t.Fatalf("rollback gate took %s, want %s", waited, wantWait)
+ }
+ body, err := os.ReadFile(systemBinaryPath(f.a.optDir))
+ if err != nil || !strings.Contains(string(body), "carlos v1") {
+ t.Fatalf("previous binary was not restored: %q %v", body, err)
+ }
+ })
+ }
+}
+
+func TestSystemApplyGroupGateKeepsFailFastClaimsOnly(t *testing.T) {
+ f, _ := maintenanceFixture(t)
+ f.edgeUp = false
+ f.nrestarts = "3"
+ start := f.now
+ if err := f.a.gate(t.Context(), "v1.0.0"); err == nil {
+ t.Fatal("gate accepted a failed edge")
+ }
+ if waited := f.now.Sub(start); waited != 3*time.Minute {
+ t.Fatalf("group gate failed after %s, want 3 min", waited)
+ }
+ for _, call := range f.calls {
+ if strings.HasPrefix(call, "systemctl show") {
+ t.Fatalf("group gate invoked claims fail-fast: %s", call)
+ }
+ }
+}