system apply: a claims-on box's update gate waits 120 s and fails fast
With CARLOS_HOST_CLAIM on, the agent finishes its whole claim startup before the edge's listener binds, about 22 s after the restart. The fixed 20 s gate rolled every update back, and the rollback failed its gate too, so the result stuck at rollback_failed. On a claims-on box (read with hostwrite.Enabled, as the agent does) the gate's window is now 120 s, for the install's gate and the rollback's. Inside the window it fails fast as soon as the edge is failed or inactive, or NRestarts is above 0, from the is-active and systemctl show reads the gate already makes, so a crashing build still rolls back in seconds. Claims-off boxes keep exactly the 20 s gate and never read show inside the wait. The real fix, making a claims-on edge listen before its claim startup (the #281 shape), changes census-reviewed claim wiring and is left for its own issue.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+333
−17
internal/host/systemapply.go+93 −17internal/host/systemapply_claimsgate_test.go+240 −0
diff --git a/internal/host/systemapply.go b/internal/host/systemapply.go| index 9a959dadf..8cbcf2a2c 100644 |
| --- a/internal/host/systemapply.go |
| +++ b/internal/host/systemapply.go |
| @@ -24,6 +24,7 @@ import ( |
| "github.com/carlosframework/platform/internal/activator" |
| "github.com/carlosframework/platform/internal/cliflag" |
| "github.com/carlosframework/platform/internal/hostfloor" |
| + "github.com/carlosframework/platform/internal/hostwrite" |
| "github.com/carlosframework/platform/internal/layout" |
| "github.com/carlosframework/platform/internal/moves" |
| "github.com/carlosframework/platform/internal/provision" |
| @@ -52,13 +53,22 @@ const ( |
| // design §5). It execs /opt/carlos/carlos and stays up for weeks once |
| // the first wake activated it, so every install that changes the |
| // binary restarts it (restartInstanceControl; discussion #256). |
| - systemControlUnit = "carlos-instance-control.service" |
| - systemLockFile = "lock" |
| - systemMaxRequest = 4 << 10 |
| - systemGateWait = 20 * time.Second |
| - systemGateSoak = 15 * time.Second |
| - systemVersionWait = 30 * time.Second |
| - systemBootstrapWait = 120 * time.Second |
| + systemControlUnit = "carlos-instance-control.service" |
| + systemLockFile = "lock" |
| + systemMaxRequest = 4 << 10 |
| + systemGateWait = 20 * time.Second |
| + // systemClaimsGateWait is the gate's window on a box running with |
| + // CARLOS_HOST_CLAIM on. There the agent finishes its whole claim |
| + // startup (assembly, wiring, the inline activator and duties) before |
| + // the edge's listener binds, which takes about 22 s on a small |
| + // 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. |
| + systemClaimsGateWait = 120 * time.Second |
| + 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. |
| @@ -116,11 +126,12 @@ func RunSystemApply(args []string) { |
| if invalidChannel != "" { |
| log.Printf("system apply: %s=%q is not a channel; this box follows %s", systemChannelEnv, invalidChannel, channel) |
| } |
| + claims, gateWait := systemApplyGate(os.Getenv) |
| a := &systemApplier{ |
| rootMoves: rootMovesFloorIdentity, ops: productionSysOps(), openStore: openSystemStore, channel: channel, |
| optDir: release.OptDir(), dataDir: dataDir(), etcDir: "/etc", build: version.Build, |
| - platform: runtime.GOOS + "-" + runtime.GOARCH, gateWait: systemGateWait, soak: systemGateSoak, |
| - regionHA: regionHAEnabled(os.Getenv), |
| + platform: runtime.GOOS + "-" + runtime.GOARCH, gateWait: gateWait, soak: systemGateSoak, |
| + regionHA: regionHAEnabled(os.Getenv), claims: claims, |
| } |
| a.floorHook = installFloorHook // decided under the apply lock, in Run |
| runErr := a.runWithRelease(ctx, *recover) |
| @@ -249,6 +260,12 @@ type systemApplier struct { |
| // refuses an update while this box takes part in a claim operation |
| // (region HA spec 1 §5.10; systemparticipation.go). |
| regionHA bool |
| + // claims is CARLOS_HOST_CLAIM, read with hostwrite.Enabled from the |
| + // 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. |
| + claims bool |
| // store is the deploy store verifyPointer opened, kept for armFloor. |
| // Nil on --recover, which reads no store. |
| store release.Store |
| @@ -1479,7 +1496,7 @@ func (a *systemApplier) restartRelay() { |
| func (a *systemApplier) gate(ctx context.Context, target string) error { |
| wait, soak := a.gateWait, a.soak |
| if wait <= 0 { |
| - wait = systemGateWait |
| + wait = systemGateWaitFor(a.claims) |
| } |
| if soak <= 0 { |
| soak = systemGateSoak |
| @@ -1487,10 +1504,16 @@ func (a *systemApplier) gate(ctx context.Context, target string) error { |
| deadline := a.ops.now().Add(wait) |
| var lastErr error |
| for { |
| - lastErr = a.gateOnce(ctx, target) |
| + var state string |
| + state, lastErr = a.gateCheck(ctx, target) |
| if lastErr == nil { |
| break |
| } |
| + if a.claims { |
| + if err := a.gateFailFast(state, lastErr); err != nil { |
| + return err |
| + } |
| + } |
| if !a.ops.now().Before(deadline) { |
| return lastErr |
| } |
| @@ -1517,6 +1540,49 @@ func (a *systemApplier) gate(ctx context.Context, target string) error { |
| return a.gateOnce(ctx, target) |
| } |
| +// systemApplyGate is how RunSystemApply reads claims mode for the gate: the |
| +// unit's CARLOS_HOST_CLAIM through hostwrite.Enabled, the test the agent |
| +// and the claim wiring make, and the window that follows from it. |
| +func systemApplyGate(getenv func(string) string) (bool, time.Duration) { |
| + claims := hostwrite.Enabled(getenv) |
| + return claims, systemGateWaitFor(claims) |
| +} |
| + |
| +// systemGateWaitFor is the gate's window: systemClaimsGateWait with |
| +// CARLOS_HOST_CLAIM on, systemGateWait without. The install's gate and the |
| +// rollback's gate both use it, so a claims-on box can roll back to a |
| +// previous build that also needs more than 20 s to listen. |
| +func systemGateWaitFor(claims bool) time.Duration { |
| + if claims { |
| + return systemClaimsGateWait |
| + } |
| + return systemGateWait |
| +} |
| + |
| +// gateFailFast is what makes the claims window "wait while the same process |
| +// is still starting" rather than "wait 120 s for anything". After a failed |
| +// check it returns an error, ending the gate before its deadline, when the |
| +// edge unit reports a failed or inactive state (from the is-active the |
| +// check just made) or NRestarts above 0 (from the same systemctl show the |
| +// soak reads). A build that crashes rolls back in seconds; one that is |
| +// still starting, active with no restart, gets the whole window. A show |
| +// that cannot be read says nothing either way, so the wait goes on to the |
| +// deadline. Claims-off boxes never call it. |
| +func (a *systemApplier) gateFailFast(state string, cause error) error { |
| + switch state { |
| + case "failed", "inactive": |
| + return fmt.Errorf("%w (failing fast: %s is %s)", cause, systemEdgeUnit, state) |
| + } |
| + rs, err := a.edgeRunState() |
| + if err != nil { |
| + return nil |
| + } |
| + if err := rs.steady(); err != nil { |
| + return fmt.Errorf("%w (failing fast: %v)", cause, err) |
| + } |
| + return nil |
| +} |
| + |
| // edgeRunState is the edge's NRestarts and ActiveEnterTimestampMonotonic, |
| // in that order — the order `show -p` is asked for them. |
| type edgeRunState struct{ restarts, activeEnter string } |
| @@ -1542,19 +1608,29 @@ func (a *systemApplier) edgeRunState() (edgeRunState, error) { |
| } |
| func (a *systemApplier) gateOnce(ctx context.Context, target string) error { |
| - if out, err := a.ops.systemctl("is-active", systemEdgeUnit); err != nil || strings.TrimSpace(out) != "active" { |
| + _, err := a.gateCheck(ctx, target) |
| + return err |
| +} |
| + |
| +// gateCheck is gateOnce that also returns what is-active said about the |
| +// edge, trimmed, for gateFailFast. The checks, their order and their |
| +// errors are gateOnce's. |
| +func (a *systemApplier) gateCheck(ctx context.Context, target string) (string, error) { |
| + out, err := a.ops.systemctl("is-active", systemEdgeUnit) |
| + state := strings.TrimSpace(out) |
| + if err != nil || state != "active" { |
| if err != nil { |
| - return fmt.Errorf("%s is %s: %w", systemEdgeUnit, strings.TrimSpace(out), err) |
| + return state, fmt.Errorf("%s is %s: %w", systemEdgeUnit, state, err) |
| } |
| - return fmt.Errorf("%s is %s", systemEdgeUnit, strings.TrimSpace(out)) |
| + return state, fmt.Errorf("%s is %s", systemEdgeUnit, state) |
| } |
| if err := a.ops.probeEdge(); err != nil { |
| - return fmt.Errorf("edge probe: %w", err) |
| + return state, fmt.Errorf("edge probe: %w", err) |
| } |
| if got := a.onDiskBuild(ctx); got != target { |
| - return fmt.Errorf("installed binary reports %q, not %s", got, target) |
| + return state, fmt.Errorf("installed binary reports %q, not %s", got, target) |
| } |
| - return nil |
| + return state, nil |
| } |
| // arm enables every unit in the plan and starts the ones the plan says to |
diff --git a/internal/host/systemapply_claimsgate_test.go b/internal/host/systemapply_claimsgate_test.go| new file mode 100644 |
| index 000000000..e4de9a444 |
| --- /dev/null |
| +++ b/internal/host/systemapply_claimsgate_test.go |
| @@ -0,0 +1,240 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "os" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// The update gate on a claims-on box (CARLOS_HOST_CLAIM). There the edge's |
| +// listener binds only after the agent's whole claim startup, about 22 s |
| +// after the restart, so a fixed 20 s window rolled every update back and |
| +// the rollback too. With claims on the window is 120 s and the gate fails |
| +// fast on a crash; with claims off it is exactly the 20 s gate it was. |
| + |
| +// slowListener makes the fake edge refuse the probe for listenAfter after |
| +// every restart, as a claims-on edge does while the claim assembly runs. |
| +// It returns the times at which each restart was issued. |
| +func slowListener(f *applyFixture, listenAfter time.Duration) *[]time.Time { |
| + var restarts []time.Time |
| + orig := f.a.ops.systemctl |
| + f.a.ops.systemctl = func(args ...string) (string, error) { |
| + if args[0] == "restart" && len(args) > 1 && args[1] == systemEdgeUnit { |
| + restarts = append(restarts, f.now) |
| + } |
| + return orig(args...) |
| + } |
| + f.a.ops.probeEdge = func() error { |
| + if len(restarts) == 0 || f.now.Sub(restarts[len(restarts)-1]) < listenAfter { |
| + return errors.New("dial tcp 127.0.0.1:80: connect: connection refused") |
| + } |
| + return nil |
| + } |
| + return &restarts |
| +} |
| + |
| +func TestSystemGateWaitIsKeyedOnClaimsMode(t *testing.T) { |
| + if got := systemGateWaitFor(false); got != 20*time.Second { |
| + t.Fatalf("claims-off gate window = %s, want the 20 s it always was", got) |
| + } |
| + if got := systemGateWaitFor(true); got != 120*time.Second { |
| + t.Fatalf("claims-on gate window = %s, want 120 s", got) |
| + } |
| +} |
| + |
| +func TestSystemApplyReadsClaimsModeFromTheUnitEnvironment(t *testing.T) { |
| + for _, tc := range []struct { |
| + value string |
| + claims bool |
| + wait time.Duration |
| + }{{"", false, 20 * time.Second}, {" ", false, 20 * time.Second}, {"1", true, 120 * time.Second}, {"on", true, 120 * time.Second}} { |
| + claims, wait := systemApplyGate(func(k string) string { |
| + if k == "CARLOS_HOST_CLAIM" { |
| + return tc.value |
| + } |
| + return "" |
| + }) |
| + if claims != tc.claims || wait != tc.wait { |
| + t.Fatalf("CARLOS_HOST_CLAIM=%q: claims=%t wait=%s, want %t %s", tc.value, claims, wait, tc.claims, tc.wait) |
| + } |
| + } |
| +} |
| + |
| +func TestClaimsOffGateIsUnchangedByAnEdgeThatListensAt22s(t *testing.T) { |
| + // Ruling 6: a claims-off box sees no change. The same 22 s edge rolls |
| + // back, and its 22 s previous build fails its gate too, as on main. |
| + f, req := applyHappy(t) |
| + slowListener(f, 22*time.Second) |
| + if err := f.a.Run(context.Background(), false); err == nil { |
| + t.Fatal("a claims-off 20 s gate passed an edge that listened at 22 s") |
| + } |
| + r := f.result() |
| + if r == nil || r.Outcome != systemOutcomeRollbackFailed || !r.matches("stable", "v2", req.PromotedAt) { |
| + t.Fatalf("result = %+v, want rollback-failed as on main", r) |
| + } |
| +} |
| + |
| +func TestClaimsOffGateWaitsItsFullWindowThroughACrash(t *testing.T) { |
| + // Claims off never fails fast: a crash-restarting edge still gets the |
| + // full 20 s, and the gate's systemctl calls are only is-active. |
| + f := newApplyFixture(t, "stable", "v1") |
| + f.edgeUp = false |
| + f.nrestarts = "3" |
| + start := f.now |
| + if err := f.a.gate(context.Background(), "v1"); err == nil { |
| + t.Fatal("gate passed a failed edge") |
| + } |
| + if waited := f.now.Sub(start); waited != 20*time.Second { |
| + t.Fatalf("claims-off gate gave up after %s, want exactly 20 s", waited) |
| + } |
| + for _, c := range f.calls { |
| + if strings.HasPrefix(c, "systemctl show") { |
| + t.Fatalf("claims-off gate read %q inside its wait; it must not", c) |
| + } |
| + } |
| +} |
| + |
| +func TestClaimsOnGatePassesAnEdgeThatListensAt22s(t *testing.T) { |
| + f, req := applyHappy(t) |
| + f.a.claims = true |
| + restarts := slowListener(f, 22*time.Second) |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatalf("claims-on gate failed a still-starting edge: %v", err) |
| + } |
| + r := f.result() |
| + if r == nil || r.Outcome != systemOutcomeApplied || !r.matches("stable", "v2", req.PromotedAt) { |
| + t.Fatalf("result = %+v", r) |
| + } |
| + if len(*restarts) != 1 { |
| + t.Fatalf("edge restarted %d times, want 1 (no rollback)", len(*restarts)) |
| + } |
| +} |
| + |
| +func TestClaimsOnGateWaitsUpTo120s(t *testing.T) { |
| + f := newApplyFixture(t, "stable", "v1") |
| + f.a.claims = true |
| + f.probeErr = errors.New("connection refused") |
| + start := f.now |
| + err := f.a.gate(context.Background(), "v1") |
| + if err == nil || !strings.Contains(err.Error(), "connection refused") { |
| + t.Fatalf("gate = %v, want the probe's error at the deadline", err) |
| + } |
| + if waited := f.now.Sub(start); waited != 120*time.Second { |
| + t.Fatalf("claims-on gate gave up after %s, want 120 s", waited) |
| + } |
| +} |
| + |
| +func TestClaimsOnGateFailsFastOnACrashRestart(t *testing.T) { |
| + f := newApplyFixture(t, "stable", "v1") |
| + f.a.claims = true |
| + f.probeErr = errors.New("connection refused") |
| + f.nrestarts = "1" |
| + start := f.now |
| + err := f.a.gate(context.Background(), "v1") |
| + if err == nil || !strings.Contains(err.Error(), "NRestarts=1") { |
| + t.Fatalf("gate = %v, want a fail-fast on NRestarts", err) |
| + } |
| + if waited := f.now.Sub(start); waited != 0 { |
| + t.Fatalf("claims-on gate waited %s on a crash-restarted edge, want no wait", waited) |
| + } |
| +} |
| + |
| +func TestClaimsOnGateFailsFastOnAFailedOrInactiveEdge(t *testing.T) { |
| + for _, state := range []string{"failed", "inactive"} { |
| + t.Run(state, func(t *testing.T) { |
| + f := newApplyFixture(t, "stable", "v1") |
| + f.a.claims = true |
| + orig := f.a.ops.systemctl |
| + f.a.ops.systemctl = func(args ...string) (string, error) { |
| + if args[0] == "is-active" { |
| + return state + "\n", errors.New("exit status 3") |
| + } |
| + return orig(args...) |
| + } |
| + start := f.now |
| + err := f.a.gate(context.Background(), "v1") |
| + if err == nil || !strings.Contains(err.Error(), "failing fast: carlos-edge is "+state) { |
| + t.Fatalf("gate = %v, want a fail-fast on %s", err, state) |
| + } |
| + if waited := f.now.Sub(start); waited != 0 { |
| + t.Fatalf("claims-on gate waited %s on a %s edge", waited, state) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestClaimsOnGateKeepsWaitingWhileActivating(t *testing.T) { |
| + // "activating" with no restart is a process still starting, not a |
| + // crash: no fail-fast, the gate passes once it answers. |
| + f := newApplyFixture(t, "stable", "v1") |
| + f.a.claims = true |
| + start := f.now |
| + orig := f.a.ops.systemctl |
| + f.a.ops.systemctl = func(args ...string) (string, error) { |
| + if args[0] == "is-active" && f.now.Sub(start) < 30*time.Second { |
| + return "activating\n", errors.New("exit status 3") |
| + } |
| + return orig(args...) |
| + } |
| + if err := f.a.gate(context.Background(), "v1"); err != nil { |
| + t.Fatalf("gate = %v, want a pass once the edge is active", err) |
| + } |
| +} |
| + |
| +func TestClaimsOnRollbackGateUsesTheSameRule(t *testing.T) { |
| + // v2 crashes on start; v1, the previous build, needs 22 s to listen. |
| + // The install's gate fails fast, and the rollback's gate gives v1 the |
| + // claims window, so the rollback succeeds instead of rollback-failed. |
| + f, req := applyHappy(t) |
| + f.a.claims = true |
| + restarts := slowListener(f, 22*time.Second) |
| + orig := f.a.ops.systemctl |
| + f.a.ops.systemctl = func(args ...string) (string, error) { |
| + if args[0] == "is-active" { |
| + if b, _ := os.ReadFile(systemBinaryPath(f.a.optDir)); strings.Contains(string(b), "carlos v2") { |
| + return "failed\n", errors.New("exit status 3") |
| + } |
| + } |
| + return orig(args...) |
| + } |
| + err := f.a.Run(context.Background(), false) |
| + if err == nil { |
| + t.Fatal("a failed gate must exit non-zero") |
| + } |
| + r := f.result() |
| + if r == nil || r.Outcome != systemOutcomeRolledBack || !r.matches("stable", "v2", req.PromotedAt) { |
| + t.Fatalf("result = %+v, want rolled-back", r) |
| + } |
| + if !strings.Contains(r.Reason, "failing fast") { |
| + t.Fatalf("reason = %q, want the install gate's fail-fast", r.Reason) |
| + } |
| + if len(*restarts) != 2 { |
| + t.Fatalf("edge restarted %d times, want install + rollback", len(*restarts)) |
| + } |
| + if d := (*restarts)[1].Sub((*restarts)[0]); d > 5*time.Second { |
| + t.Fatalf("install gate took %s to give up on a failed edge, want fail-fast", d) |
| + } |
| +} |
| + |
| +func TestClaimsOnRollbackGateFailsFastToo(t *testing.T) { |
| + // Both builds crash: the rollback gate fails fast as the install's did, |
| + // rather than waiting 120 s on a dead previous build. |
| + f, req := applyHappy(t) |
| + f.a.claims = true |
| + f.nrestarts = "2" |
| + f.probeErr = errors.New("connection refused") |
| + start := f.now |
| + if err := f.a.Run(context.Background(), false); err == nil { |
| + t.Fatal("a failed gate must exit non-zero") |
| + } |
| + r := f.result() |
| + if r == nil || r.Outcome != systemOutcomeRollbackFailed || !r.matches("stable", "v2", req.PromotedAt) { |
| + t.Fatalf("result = %+v, want rollback-failed", r) |
| + } |
| + if waited := f.now.Sub(start); waited >= 20*time.Second { |
| + t.Fatalf("install and rollback gates took %s between them, want fail-fast on both", waited) |
| + } |
| +} |