carlos / platform Public

activator: refuse interrupted init; the first-boot marker is confirmed before the database is placed (r2 ruling)

Paul's ruling on the database-continuity family (2026-10-27, stopping
rule 2). A fresh rootfs first boot reads the marker, writes it
create-only, reads it back, and only then places the empty database. A
write conflict is never taken as the marker: only the read back confirms
it (census r2 P1-1). The local pending record and the warm-wake settle
are gone (census r2 P1-2, whole-branch r2 P1-1). A crash after the
confirmation leaves the instance refused until an operator deletes the
marker; the recipe is in rootfsmarker.go and
docs/superpowers/notes/rootfs-first-boot-marker.md.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev fb201af2c27c13ff771505988f9a3083918b4914 parent 4ece7e5
8 files changed, +227 −234
  • docs/superpowers/notes/rootfs-first-boot-marker.md +48 −0
  • internal/activator/activator.go +3 −9
  • internal/activator/production_backend_test.go +4 −7
  • internal/activator/rootfs_gate_test.go +17 −28
  • internal/activator/rootfs_invariants_test.go +2 −1
  • internal/activator/rootfs_service.go +2 −2
  • internal/activator/rootfsmarker.go +65 −149
  • internal/activator/rootfsmarker_test.go +86 −38
diff --git a/docs/superpowers/notes/rootfs-first-boot-marker.md b/docs/superpowers/notes/rootfs-first-boot-marker.md
new file mode 100644
index 000000000..64974ca34
--- /dev/null
+++ b/docs/superpowers/notes/rootfs-first-boot-marker.md
@@ -0,0 +1,48 @@
+# Rootfs first-boot marker: repairing an interrupted first boot
+
+A rootfs instance must never boot with an empty database after it has
+served. A box can't tell that from its own registry: a replacement box
+starts with a fresh one. So the first boot of each instance lifetime
+leaves a marker in the hibernate bucket, beside the replica:
+
+ <CARLOS_HIBERNATE_PREFIX>/<host>/rootfs-initialized/<incarnation>.json
+
+`<incarnation>` is the instance record's `incarnation` (the lifetime
+number; a deleted and recreated host gets a new one, and so a new key).
+
+## The order on a first boot
+
+On a fresh wake with no good build, no local database and no replica, the
+agent, holding the instance's lease:
+
+1. reads the marker. Present, or unreadable: refuse, nothing placed.
+2. writes it create-only, then reads it back at the key. Only that read
+ back confirms it. A write conflict on its own proves nothing.
+3. places the empty database and starts the instance.
+
+A warm wake never asks the bucket.
+
+## When an instance refuses with "no database and no replica"
+
+The log line is `lifetime N has booted before (its first-boot marker is
+there): refusing an empty boot`. There are two causes, and the repair is
+different:
+
+- **The instance served and its replica is gone.** Restore the replica.
+ Never delete the marker: that boots the instance empty over lost data.
+- **The first boot was interrupted** (the agent stopped after step 2 and
+ before the first replica upload). The instance never served. This
+ refusal is by design (Paul's ruling, 2026-10-27, "refuse interrupted
+ init"); the platform does not guess which case it is.
+
+To repair an interrupted first boot:
+
+1. Confirm the instance never served. The bucket in
+ `CARLOS_HIBERNATE_BUCKET` holds nothing under
+ `<CARLOS_HIBERNATE_PREFIX>/<host>/ltx/`. If anything is there, it is
+ the first case: restore, don't delete.
+2. Delete `<CARLOS_HIBERNATE_PREFIX>/<host>/rootfs-initialized/<incarnation>.json`
+ from that bucket.
+3. The next request wakes the instance as a first boot.
+
+The code is `internal/activator/rootfsmarker.go`.
diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index be05c789d..ffb801529 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -2581,16 +2581,10 @@ func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, erro
// registry only, so the first boot also asks the bucket (branch review
// r1 P1-1, rootfsmarker.go): the lifetime's first-boot marker, read
// under this lease, refuses when it is there or cannot be read, and is
- // written and confirmed before the start. A row that is not fresh only
- // settles a first boot this box left pending. A binary row has its seed
- // and creates its own file, and never takes this path.
+ // written and confirmed by a read back before the database is placed.
+ // A warm wake does nothing with it. A binary row has its seed and
+ // creates its own file, and never takes this path.
placed := false
- if !fresh && rt.Rootfs() {
- if err := a.settleRootfsFirstBoot(ctx, rt); err != nil {
- w.stopOutcome.leaseReleased = a.giveBack(ctx, host, lease)
- return w, err
- }
- }
if fresh && rt.Rootfs() {
if rt.GoodVersion != "" {
w.stopOutcome.leaseReleased = a.giveBack(ctx, host, lease)
diff --git a/internal/activator/production_backend_test.go b/internal/activator/production_backend_test.go
index 6d0656f41..4ecab1210 100644
--- a/internal/activator/production_backend_test.go
+++ b/internal/activator/production_backend_test.go
@@ -51,13 +51,10 @@ func TestProductionBackendImplementsRootfsSeams(t *testing.T) {
if !ok {
t.Fatal("SplitBackend is not a rootfsFirstBootMarker")
}
- if _, err := m.RootfsInitialized(context.Background(), unit); err == nil {
- return nil
+ if _, err := m.RootfsInitialized(context.Background(), unit); !errors.Is(err, errNoRootfsMarker) {
+ t.Errorf("RootfsInitialized on a unit row = %v, want errNoRootfsMarker", err)
}
- if err := m.SettleRootfsFirstBoot(context.Background(), unit); err != nil {
- t.Errorf("SettleRootfsFirstBoot on a unit row = %v, want nothing pending", err)
- }
- return m.BeginRootfsFirstBoot(unit)
+ return m.WriteRootfsMarker(context.Background(), unit)
},
"ProcessReader": func() error {
r, ok := b.(ProcessReader)
@@ -83,7 +80,7 @@ func TestProductionBackendImplementsRootfsSeams(t *testing.T) {
t.Errorf("PlaceRootfsDB on a unit row = %v, want errNoRootfsDB", err)
}
if err := rows["rootfsFirstBootMarker"](); !errors.Is(err, errNoRootfsMarker) {
- t.Errorf("BeginRootfsFirstBoot on a unit row = %v, want errNoRootfsMarker", err)
+ t.Errorf("WriteRootfsMarker on a unit row = %v, want errNoRootfsMarker", err)
}
if err := rows["ProcessReader"](); !errors.Is(err, errNoRootfsControl) || !errors.Is(err, errRootfsRefused) {
t.Errorf("Processes on a unit row = %v, want a marked errNoRootfsControl", err)
diff --git a/internal/activator/rootfs_gate_test.go b/internal/activator/rootfs_gate_test.go
index b574711a4..b8f11f950 100644
--- a/internal/activator/rootfs_gate_test.go
+++ b/internal/activator/rootfs_gate_test.go
@@ -64,12 +64,14 @@ func (b *fakeBackend) PlaceRootfsDB(context.Context, registry.Route) error {
}
// fakeMarker is fakeBackend's first-boot marker: marked holds the
-// bucket's markers and pending this box's pending first boots, each keyed
-// host@incarnation; readErr and writeErr answer the read and the write.
+// bucket's markers, keyed host@incarnation. readErr answers every read,
+// confirmErr only the read back after a write; writeErr answers the write,
+// and a write with writeLost set leaves nothing at the key.
type fakeMarker struct {
- marked, pending map[string]bool
- readErr, writeErr error
- reads, writes int
+ marked map[string]bool
+ readErr, confirmErr, writeErr error
+ writeLost bool
+ reads, writes int
}
func markerID(rt registry.Route) string {
@@ -84,37 +86,24 @@ func (b *fakeBackend) RootfsInitialized(_ context.Context, rt registry.Route) (b
if b.marker.readErr != nil {
return false, b.marker.readErr
}
- return b.marker.marked[markerID(rt)], nil
-}
-
-func (b *fakeBackend) BeginRootfsFirstBoot(rt registry.Route) error {
- b.mu.Lock()
- defer b.mu.Unlock()
- if b.marker.pending == nil {
- b.marker.pending = map[string]bool{}
+ if b.marker.writes > 0 && b.marker.confirmErr != nil {
+ return false, b.marker.confirmErr
}
- b.marker.pending[markerID(rt)] = true
- return nil
+ return b.marker.marked[markerID(rt)], nil
}
-func (b *fakeBackend) SettleRootfsFirstBoot(_ context.Context, rt registry.Route) error {
+func (b *fakeBackend) WriteRootfsMarker(_ context.Context, rt registry.Route) error {
b.mu.Lock()
defer b.mu.Unlock()
- id := markerID(rt)
- if !b.marker.pending[id] {
- return nil
- }
b.marker.writes++
b.order = append(b.order, "marker-write")
- if b.marker.writeErr != nil {
- return b.marker.writeErr
- }
- if b.marker.marked == nil {
- b.marker.marked = map[string]bool{}
+ if !b.marker.writeLost {
+ if b.marker.marked == nil {
+ b.marker.marked = map[string]bool{}
+ }
+ b.marker.marked[markerID(rt)] = true
}
- b.marker.marked[id] = true
- delete(b.marker.pending, id)
- return nil
+ return b.marker.writeErr
}
// SyncAndWait is a first upload asked of the replica, counted and logged.
diff --git a/internal/activator/rootfs_invariants_test.go b/internal/activator/rootfs_invariants_test.go
index 26c7cb441..abdf8bcfc 100644
--- a/internal/activator/rootfs_invariants_test.go
+++ b/internal/activator/rootfs_invariants_test.go
@@ -889,10 +889,11 @@ var rootfsRefusals = []struct {
{"a served row with no replica (errRootfsReplicaMissing, marked)", refuseRootfs(fmt.Errorf("x: %w", errRootfsReplicaMissing))},
{"no placeholder placer (errNoRootfsDB, marked)", refuseRootfs(fmt.Errorf("x: %w", errNoRootfsDB))},
// Branch review r1 P1-1: the first-boot marker's refusals, as
- // rootfsFirstBoot and settleRootfsFirstBoot mark them.
+ // rootfsFirstBoot marks them.
{"no first-boot marker store (errNoRootfsMarker, marked)", refuseRootfs(fmt.Errorf("x: %w", errNoRootfsMarker))},
{"a first-boot marker that cannot be read (marked)", refuseRootfs(fmt.Errorf("x: read the first-boot marker: %w", errors.New("x")))},
{"a first-boot marker that cannot be written (marked)", refuseRootfs(fmt.Errorf("x: write the first-boot marker: %w", errors.New("x")))},
+ {"a first-boot marker not confirmed by its read back (errRootfsMarkerUnconfirmed, marked)", refuseRootfs(fmt.Errorf("x: %w", errRootfsMarkerUnconfirmed))},
// Task 15 round 1: the HA containment refusal of a rootfs row, as
// StartStaged marks it.
{"an HA containment refusal of a rootfs row (marked)", refuseRootfs(refusedUncontained(errors.New("x")))},
diff --git a/internal/activator/rootfs_service.go b/internal/activator/rootfs_service.go
index 0eca710b0..5826d2f03 100644
--- a/internal/activator/rootfs_service.go
+++ b/internal/activator/rootfs_service.go
@@ -324,8 +324,8 @@ func (a *Service) placeRootfsDB(ctx context.Context, rt registry.Route) error {
// before the scheduler can record the build as good. A failure only logs:
// the replicator keeps trying on its interval, the next sleep's checkpoint
// uploads it, and if neither lands the next fresh wake on any box finds the
-// lifetime's first-boot marker, written and confirmed before the start
-// (rootfsmarker.go), and refuses (errRootfsReplicaMissing), the safe
+// lifetime's first-boot marker, written and confirmed before the
+// placement (rootfsmarker.go), and refuses (errRootfsReplicaMissing), the safe
// direction. GoodVersion alone could not say so on another box.
func syncRootfsPlaceholder(ctx context.Context, host string, rep Rep) {
s, ok := rep.(interface{ SyncAndWait(context.Context) error })
diff --git a/internal/activator/rootfsmarker.go b/internal/activator/rootfsmarker.go
index 9055bbf86..11daf09e1 100644
--- a/internal/activator/rootfsmarker.go
+++ b/internal/activator/rootfsmarker.go
@@ -1,29 +1,40 @@
// rootfsmarker.go is the cross-box half of "never boot empty after
-// serving" for a rootfs instance (branch review r1, P1-1). The local
-// good-build check (doWake step 2a, rt.GoodVersion) is registry state: a
-// replacement box with a fresh registry has none, so it cannot tell a first
-// boot from a lost database. The evidence that can is a marker in the
-// bucket the replica lives in, beside the replica, scoped to the
-// instance's lifetime:
+// serving" for a rootfs instance (branch review r1 P1-1; r2 ruling,
+// "refuse interrupted init"). The local good-build check (doWake step 2a,
+// rt.GoodVersion) is registry state: a replacement box with a fresh
+// registry has none, so it cannot tell a first boot from a lost database.
+// The evidence that can is a marker in the bucket the replica lives in,
+// beside the replica, scoped to the instance's lifetime:
//
// <hibernate prefix>/rootfs-initialized/<incarnation>.json
//
-// It is written, and its write confirmed, before a placeholder instance
-// first starts. A fresh wake (no local database, no replica) reads it under
-// the lease before placing a placeholder: present, or unreadable, refuses.
+// A fresh wake with no good build (no local database, no replica) reads it
+// under the lease. Present, or unreadable, refuses. Absent: the marker is
+// written create-only, then read back at the exact key, and only a
+// successful read back confirms it (a write conflict proves nothing). Only
+// then is the empty database placed and the instance started. A failed
+// write or read back refuses with nothing placed. A warm wake does nothing
+// with the marker.
//
-// The request budget: one GET per fresh rootfs wake, one PUT per first
-// boot, nothing periodic. A warm wake asks the bucket nothing; it only
-// looks for this box's own pending first boot on its local disk.
+// The request budget: one GET per fresh rootfs first boot, then one PUT
+// and one confirming GET. Nothing on a warm wake, nothing periodic.
//
-// The pending first boot closes the gap between the placement and the
-// marker. A placed database is a warm cache to every later wake on this
-// box, which reads no marker; so the pending record is written on this
-// box's own disk (the agent's data directory, never the tenant's instance
-// directory) before the placement and removed only once the marker is
-// confirmed, and every warm wake that finds it writes the marker before
-// anything starts. A marker write that failed, or an agent that stopped
-// between the placement and the write, therefore never serves unmarked.
+// Interrupted init is refused, by design. The marker is confirmed before
+// the database is placed, so an agent that stops after the confirmation
+// and before the first replica upload leaves a lifetime that never served
+// but reads as served: its next fresh wake, on any box, refuses with
+// errRootfsReplicaMissing. The operator's repair:
+//
+// 1. Confirm the instance never served: the hibernate bucket holds no
+// replica for the host, nothing under <hibernate prefix>/ltx/ (the
+// hibernate prefix is CARLOS_HIBERNATE_PREFIX joined with the host).
+// If a replica is there, it is not this case: restore, never delete.
+// 2. Delete the marker of the row's lifetime,
+// <hibernate prefix>/rootfs-initialized/<incarnation>.json, from the
+// bucket in CARLOS_HIBERNATE_BUCKET.
+// 3. The next request wakes it as a first boot.
+//
+// docs/superpowers/notes/rootfs-first-boot-marker.md has the same recipe.
package activator
import (
@@ -33,7 +44,6 @@ import (
"fmt"
"log"
"os"
- "path/filepath"
"strconv"
"time"
@@ -42,13 +52,9 @@ import (
)
// rootfsMarkerDir is the marker's directory under the instance's
-// hibernate prefix, beside the replica's generations and lock.json.
+// hibernate prefix, beside the replica's ltx/ and lock.json.
const rootfsMarkerDir = "rootfs-initialized"
-// rootfsPendingDir is the agent's own record of first boots whose marker
-// is not confirmed yet, under ExecBackend.DataDir.
-const rootfsPendingDir = ".rootfs-first-boot"
-
// rootfsMarkerKey is rt's marker key: the instance's hibernate prefix (the
// replica's), then the lifetime. The lifetime is the row's Incarnation,
// the record's lifetime number (provision's host incarnation slot): a
@@ -71,22 +77,17 @@ type rootfsMarker struct {
At string `json:"at"`
}
-// rootfsFirstBootMarker is the backend's half: the bucket marker and this
-// box's pending first boots. ExecBackend has it; SplitBackend forwards it.
+// rootfsFirstBootMarker is the backend's half: the bucket marker's read
+// and its create-only write. ExecBackend has it; SplitBackend forwards it.
type rootfsFirstBootMarker interface {
// RootfsInitialized reads rt's marker: true when an object is at the
// key, false only on a definite not-found. Any other answer is an
// error, and the wake refuses on it.
RootfsInitialized(ctx context.Context, rt registry.Route) (bool, error)
- // BeginRootfsFirstBoot records on this box's disk, durably, that a
- // first boot of rt's lifetime is placing a database whose marker is not
- // confirmed yet.
- BeginRootfsFirstBoot(rt registry.Route) error
- // SettleRootfsFirstBoot writes rt's marker when this box has a first
- // boot of rt's lifetime pending, and clears the pending record once the
- // write is confirmed. Nothing pending: nil, with nothing asked of the
- // bucket.
- SettleRootfsFirstBoot(ctx context.Context, rt registry.Route) error
+ // WriteRootfsMarker writes rt's marker create-only and returns the
+ // store's answer as it is (a conflict is release.ErrCASConflict). It
+ // confirms nothing: the caller reads the key back.
+ WriteRootfsMarker(ctx context.Context, rt registry.Route) error
}
// errNoRootfsMarker is a backend, or a box, that cannot read or write a
@@ -94,12 +95,17 @@ type rootfsFirstBootMarker interface {
// places anything.
var errNoRootfsMarker = errors.New("activator: this box cannot read or write a rootfs instance's first-boot marker")
+// errRootfsMarkerUnconfirmed is a marker whose read back found nothing at
+// the key after the write: the first boot refuses with nothing placed.
+var errRootfsMarkerUnconfirmed = errors.New("activator: the first-boot marker was not there when read back")
+
// rootfsFirstBoot is doWake's step 2a for a fresh rootfs row with no good
// build, under the lease: the marker read (present or unreadable refuses,
-// present as errRootfsReplicaMissing), the pending record, the placeholder
-// (D7), then the marker written and confirmed. Every failure is a rootfs
-// refusal: nothing of the build has run, so it is never the build's
-// failure. The capabilities are checked before the bucket is asked.
+// present as errRootfsReplicaMissing), the create-only write, the read
+// back that alone confirms it, and only then the placeholder (D7). Every
+// failure is a rootfs refusal with nothing placed: nothing of the build
+// has run, so it is never the build's failure. The capabilities are
+// checked before the bucket is asked.
func (a *Service) rootfsFirstBoot(ctx context.Context, rt registry.Route) error {
if _, ok := a.backend.(rootfsDBPlacer); !ok {
return refuseRootfs(fmt.Errorf("%s: %w", rt.Host, errNoRootfsDB))
@@ -116,31 +122,19 @@ func (a *Service) rootfsFirstBoot(ctx context.Context, rt registry.Route) error
log.Printf("activator: %s: no database and no replica, and lifetime %d has booted before (its first-boot marker is there): refusing an empty boot", rt.Host, rt.Incarnation)
return refuseRootfs(fmt.Errorf("%s: %w", rt.Host, errRootfsReplicaMissing))
}
- if err := m.BeginRootfsFirstBoot(rt); err != nil {
- return refuseRootfs(fmt.Errorf("%s: record the first boot: %w", rt.Host, err))
- }
- if err := a.placeRootfsDB(ctx, rt); err != nil {
- return err
- }
- if err := m.SettleRootfsFirstBoot(ctx, rt); err != nil {
+ // A conflict is let through to the read back, never taken as the
+ // marker: only an object read at the key confirms it.
+ if err := m.WriteRootfsMarker(ctx, rt); err != nil && !errors.Is(err, release.ErrCASConflict) {
return refuseRootfs(fmt.Errorf("%s: write the first-boot marker: %w", rt.Host, err))
}
- return nil
-}
-
-// settleRootfsFirstBoot is step 2a for a rootfs row that is not fresh: a
-// first boot this box left pending (its marker write failed, or the agent
-// stopped after the placement) gets its marker before anything starts. A
-// backend without the marker cannot have begun one.
-func (a *Service) settleRootfsFirstBoot(ctx context.Context, rt registry.Route) error {
- m, ok := a.backend.(rootfsFirstBootMarker)
- if !ok {
- return nil
+ confirmed, err := m.RootfsInitialized(ctx, rt)
+ if err != nil {
+ return refuseRootfs(fmt.Errorf("%s: confirm the first-boot marker: %w", rt.Host, err))
}
- if err := m.SettleRootfsFirstBoot(ctx, rt); err != nil {
- return refuseRootfs(fmt.Errorf("%s: write the first-boot marker: %w", rt.Host, err))
+ if !confirmed {
+ return refuseRootfs(fmt.Errorf("%s: %w", rt.Host, errRootfsMarkerUnconfirmed))
}
- return nil
+ return a.placeRootfsDB(ctx, rt)
}
// RootfsInitialized reads rt's marker from the hibernate bucket
@@ -159,62 +153,9 @@ func (b *ExecBackend) RootfsInitialized(ctx context.Context, rt registry.Route)
return false, err
}
-// rootfsPendingPath is rt's pending record: the agent's data directory,
-// which no tenant writes, one file per host and lifetime.
-func (b *ExecBackend) rootfsPendingPath(rt registry.Route) (string, error) {
- if b.DataDir == "" {
- return "", errNoRootfsMarker
- }
- name := rt.Host + "@" + strconv.FormatUint(rt.Incarnation, 10)
- if rt.Host == "" || filepath.Base(name) != name {
- return "", fmt.Errorf("activator: no pending first-boot record for host %q", rt.Host)
- }
- return filepath.Join(b.DataDir, rootfsPendingDir, name), nil
-}
-
-// BeginRootfsFirstBoot writes rt's pending record, synced, and its
-// directory synced, before the placement.
-func (b *ExecBackend) BeginRootfsFirstBoot(rt registry.Route) error {
- if b.FilesStore == nil {
- return errNoRootfsMarker
- }
- p, err := b.rootfsPendingPath(rt)
- if err != nil {
- return err
- }
- dir := filepath.Dir(p)
- if err := os.MkdirAll(dir, 0o700); err != nil {
- return err
- }
- f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
- if err != nil {
- return err
- }
- _, werr := f.Write([]byte(b.Cfg.rootfsMarkerKey(rt) + "\n"))
- serr := f.Sync()
- cerr := f.Close()
- if err := errors.Join(werr, serr, cerr); err != nil {
- return err
- }
- syncDirBestEffort(dir)
- return nil
-}
-
-// SettleRootfsFirstBoot writes rt's marker if this box has its first boot
-// pending: create-only, so a marker already there (this box's own earlier
-// write whose answer was lost) is kept and counts as confirmed. The
-// pending record goes only after the write is confirmed; a removal that
-// fails is logged, and costs one more PUT at the next wake.
-func (b *ExecBackend) SettleRootfsFirstBoot(ctx context.Context, rt registry.Route) error {
- p, err := b.rootfsPendingPath(rt)
- if err != nil {
- return err
- }
- if _, err := os.Lstat(p); errors.Is(err, os.ErrNotExist) {
- return nil
- } else if err != nil {
- return err
- }
+// WriteRootfsMarker writes rt's marker to the hibernate bucket,
+// create-only.
+func (b *ExecBackend) WriteRootfsMarker(ctx context.Context, rt registry.Route) error {
if b.FilesStore == nil {
return errNoRootfsMarker
}
@@ -222,30 +163,12 @@ func (b *ExecBackend) SettleRootfsFirstBoot(ctx context.Context, rt registry.Rou
if err != nil {
return err
}
- key := b.Cfg.rootfsMarkerKey(rt)
- if _, err := b.FilesStore.Put(ctx, key, body, ""); err != nil && !errors.Is(err, release.ErrCASConflict) {
- return err
- }
- if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) {
- log.Printf("activator: %s: first-boot marker %s confirmed; its pending record stays: %v", rt.Host, key, err)
- return nil
- }
- syncDirBestEffort(filepath.Dir(p))
- return nil
+ _, err = b.FilesStore.Put(ctx, b.Cfg.rootfsMarkerKey(rt), body, "")
+ return err
}
-// syncDirBestEffort syncs a directory's entries where the platform can
-// (not on Windows, which refuses a directory sync).
-func syncDirBestEffort(dir string) {
- if d, err := os.Open(dir); err == nil {
- _ = d.Sync()
- d.Close()
- }
-}
-
-// RootfsInitialized, BeginRootfsFirstBoot and SettleRootfsFirstBoot
-// forward to the exec side. A unit-backed row, or an exec side without
-// them, is errNoRootfsMarker; Settle there has nothing pending.
+// RootfsInitialized and WriteRootfsMarker forward to the exec side. A
+// unit-backed row, or an exec side without them, is errNoRootfsMarker.
func (b *SplitBackend) RootfsInitialized(ctx context.Context, rt registry.Route) (bool, error) {
if m, ok := b.execMarker(rt); ok {
return m.RootfsInitialized(ctx, rt)
@@ -253,20 +176,13 @@ func (b *SplitBackend) RootfsInitialized(ctx context.Context, rt registry.Route)
return false, fmt.Errorf("%s: %w", rt.Host, errNoRootfsMarker)
}
-func (b *SplitBackend) BeginRootfsFirstBoot(rt registry.Route) error {
+func (b *SplitBackend) WriteRootfsMarker(ctx context.Context, rt registry.Route) error {
if m, ok := b.execMarker(rt); ok {
- return m.BeginRootfsFirstBoot(rt)
+ return m.WriteRootfsMarker(ctx, rt)
}
return fmt.Errorf("%s: %w", rt.Host, errNoRootfsMarker)
}
-func (b *SplitBackend) SettleRootfsFirstBoot(ctx context.Context, rt registry.Route) error {
- if m, ok := b.execMarker(rt); ok {
- return m.SettleRootfsFirstBoot(ctx, rt)
- }
- return nil
-}
-
func (b *SplitBackend) execMarker(rt registry.Route) (rootfsFirstBootMarker, bool) {
if rt.Backing == registry.BackingUnit {
return nil, false
diff --git a/internal/activator/rootfsmarker_test.go b/internal/activator/rootfsmarker_test.go
index 50f53056a..2ad7ee551 100644
--- a/internal/activator/rootfsmarker_test.go
+++ b/internal/activator/rootfsmarker_test.go
@@ -93,7 +93,7 @@ func markerPresent(t *testing.T, s release.Store, eb *ExecBackend, rt registry.R
}
// The failure the review names, end to end: box A first-boots lifetime 1,
-// writes the marker before the start and serves; it never records a good
+// writes and confirms the marker before it places the database, and serves; it never records a good
// build anywhere box B can see, and the replica is then lost. Box B, with
// its own registry and data directory (GoodVersion empty, no database, no
// replica), reads the marker under its lease and refuses: nothing placed,
@@ -111,8 +111,8 @@ func TestRootfsReplacementBoxRefusesAfterTheReplicaIsLost(t *testing.T) {
if bbA.placements() != 1 || !markerPresent(t, bucket.Store, bbA.ExecBackend, rtA) {
t.Fatalf("box A: placements %d, marker %v; want 1, present", bbA.placements(), markerPresent(t, bucket.Store, bbA.ExecBackend, rtA))
}
- if gets, puts := bucket.counts(); gets != 1 || puts != 1 {
- t.Fatalf("box A's first boot asked the bucket %d GETs and %d PUTs for the marker, want 1 and 1", gets, puts)
+ if gets, puts := bucket.counts(); gets != 2 || puts != 1 {
+ t.Fatalf("box A's first boot asked the bucket %d GETs and %d PUTs for the marker, want 2 (read, read back) and 1", gets, puts)
}
sleepEntry(t, aA, rtA.Host)
if err := os.RemoveAll(filepath.Join(rig.dir, "replica")); err != nil {
@@ -140,8 +140,8 @@ func TestRootfsReplacementBoxRefusesAfterTheReplicaIsLost(t *testing.T) {
if aB.entryCount() != 0 {
t.Fatal("box B kept an entry for the refused wake")
}
- if gets, puts := bucket.counts(); gets != 2 || puts != 1 {
- t.Fatalf("after box B: %d marker GETs and %d PUTs, want 2 and 1 (one GET for the fresh wake, no write)", gets, puts)
+ if gets, puts := bucket.counts(); gets != 3 || puts != 1 {
+ t.Fatalf("after box B: %d marker GETs and %d PUTs, want 3 and 1 (one GET for the fresh wake, no write)", gets, puts)
}
}
@@ -185,11 +185,10 @@ func TestRootfsMarkerKeyIsBesideTheReplica(t *testing.T) {
}
}
-// A marker that cannot be written refuses the first boot before anything
-// starts, with the lease given back. The placed database is a warm cache
-// to the next wake on this box, which reads no marker: that wake writes it
-// first (the pending record), refuses while it still cannot, and starts
-// only once it is confirmed.
+// A marker that cannot be written refuses the first boot with nothing
+// placed and the lease given back, so the next wake is a fresh first boot
+// again and tries the write again; it places and starts only once the
+// write is confirmed (r2 ruling: no local pending record).
func TestRootfsUnwritableMarkerRefusesTheFirstBoot(t *testing.T) {
dir := t.TempDir()
bucket := &faultyBucket{Store: release.NewDirStore(filepath.Join(dir, "bucket"))}
@@ -204,41 +203,68 @@ func TestRootfsUnwritableMarkerRefusesTheFirstBoot(t *testing.T) {
if v := verdictOf(err); v != gateNoOwnership {
t.Errorf("wake %d: verdictOf = %d, want gateNoOwnership", i, v)
}
- if n := len(ctl.started()); n != 0 {
- t.Fatalf("wake %d: started %d with no marker confirmed", i, n)
+ if n := len(ctl.started()); n != 0 || bb.placements() != 0 {
+ t.Fatalf("wake %d: started %d, placed %d with no marker confirmed", i, n, bb.placements())
+ }
+ if _, err := os.Stat(rt.DBPath); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("wake %d wrote a database: %v", i, err)
}
}
if _, puts := bucket.counts(); puts != 2 {
- t.Fatalf("marker PUTs %d, want 2: the warm retry must write it again", puts)
- }
- if bb.placements() != 1 {
- t.Fatalf("placements %d, want 1 (the retry is warm)", bb.placements())
+ t.Fatalf("marker PUTs %d, want 2: each fresh retry writes it again", puts)
}
bucket.set(nil, nil)
if err := wakeOnce(a, rt); err != nil {
t.Fatalf("the wake once the bucket takes the write: %v; calls:\n%s", err, ctl.joined())
}
- if !markerPresent(t, bucket.Store, bb.ExecBackend, rt) || len(ctl.started()) != 1 {
- t.Fatalf("marker %v, starts %d; want present, 1", markerPresent(t, bucket.Store, bb.ExecBackend, rt), len(ctl.started()))
- }
- pend, _ := bb.rootfsPendingPath(rt)
- if _, err := os.Stat(pend); !errors.Is(err, os.ErrNotExist) {
- t.Fatalf("the pending record outlived its confirmed marker: %v", err)
+ if !markerPresent(t, bucket.Store, bb.ExecBackend, rt) || len(ctl.started()) != 1 || bb.placements() != 1 {
+ t.Fatalf("marker %v, starts %d, placements %d; want present, 1, 1", markerPresent(t, bucket.Store, bb.ExecBackend, rt), len(ctl.started()), bb.placements())
}
sleepEntry(t, a, rt.Host)
- // Settled: a warm wake asks the bucket nothing.
+ // A warm wake asks the bucket nothing.
gets, puts := bucket.counts()
if err := wakeOnce(a, rt); err != nil {
t.Fatalf("a warm wake: %v", err)
}
if g, p := bucket.counts(); g != gets || p != puts {
- t.Fatalf("a settled warm wake asked the bucket %d GETs and %d PUTs", g-gets, p-puts)
+ t.Fatalf("a warm wake asked the bucket %d GETs and %d PUTs", g-gets, p-puts)
+ }
+}
+
+// Interrupted init is refused (r2 ruling): an agent that stopped after the
+// marker was confirmed and before the database was placed leaves the
+// marker and nothing else. The next wake, on this box or any other, is a
+// fresh wake that finds the marker and refuses with nothing placed, until
+// an operator deletes the marker (rootfsmarker.go's recipe). Then the
+// next wake first-boots.
+func TestRootfsInterruptedInitRefusesUntilTheMarkerIsDeleted(t *testing.T) {
+ dir := t.TempDir()
+ bucket := release.NewDirStore(filepath.Join(dir, "bucket"))
+ a, bb, ctl, rt := bucketServiceOn(t, &bucketRig{dir: dir}, bucket)
+ if err := bb.WriteRootfsMarker(context.Background(), rt); err != nil {
+ t.Fatal(err)
+ }
+ err := wakeOnce(a, rt)
+ if !errors.Is(err, errRootfsReplicaMissing) || !errors.Is(err, errRootfsRefused) {
+ t.Fatalf("wake after an interrupted init = %v, want a refused errRootfsReplicaMissing", err)
+ }
+ if bb.placements() != 0 || len(ctl.started()) != 0 {
+ t.Fatalf("placements %d, starts %d; want 0, 0", bb.placements(), len(ctl.started()))
+ }
+ if err := bucket.Delete(context.Background(), bb.Cfg.rootfsMarkerKey(rt)); err != nil {
+ t.Fatal(err)
+ }
+ if err := wakeOnce(a, rt); err != nil {
+ t.Fatalf("the wake after the operator's repair: %v; calls:\n%s", err, ctl.joined())
+ }
+ if bb.placements() != 1 || len(ctl.started()) != 1 || !markerPresent(t, bucket, bb.ExecBackend, rt) {
+ t.Fatalf("placements %d, starts %d; want 1, 1 and the marker back", bb.placements(), len(ctl.started()))
}
}
// A marker that cannot be read refuses (fail closed): no placeholder, no
-// pending record, no start, no write.
+// start, no write.
func TestRootfsUnreadableMarkerRefuses(t *testing.T) {
dir := t.TempDir()
bucket := &faultyBucket{Store: release.NewDirStore(filepath.Join(dir, "bucket"))}
@@ -257,10 +283,6 @@ func TestRootfsUnreadableMarkerRefuses(t *testing.T) {
if _, puts := bucket.counts(); puts != 0 {
t.Fatalf("marker PUTs %d after an unreadable read", puts)
}
- pend, _ := bb.rootfsPendingPath(rt)
- if _, err := os.Stat(pend); !errors.Is(err, os.ErrNotExist) {
- t.Fatalf("a pending record was written: %v", err)
- }
}
// A box with no hibernate bucket store cannot read or write the marker:
@@ -277,8 +299,10 @@ func TestRootfsFirstBootWithoutAMarkerStoreRefuses(t *testing.T) {
}
}
-// Through fakeBackend: the order (marker read, placeholder, marker write,
-// start), and each refusal giving the lease back with nothing started.
+// Through fakeBackend: the order (marker read, marker write, the read back,
+// placeholder, start), and each refusal giving the lease back with nothing
+// placed or started. A write conflict is never taken as the marker: only
+// the read back confirms it (census r2 P1-1).
func TestRootfsFirstBootMarkerOrderAndRefusals(t *testing.T) {
t.Run("order", func(t *testing.T) {
a, b, _, _ := gateService(t)
@@ -295,15 +319,37 @@ func TestRootfsFirstBootMarkerOrderAndRefusals(t *testing.T) {
}
return -1
}
- r, p, w, s := idx("marker-read"), idx("placeholder"), idx("marker-write"), idx("start")
- if r < 0 || !(r < p && p < w && w < s) {
- t.Fatalf("order %v: want marker-read, placeholder, marker-write, start", ev)
+ r, w, p, s := idx("marker-read"), idx("marker-write"), idx("placeholder"), idx("start")
+ back := -1
+ for i := w + 1; w >= 0 && i < len(ev); i++ {
+ if ev[i] == "marker-read" {
+ back = i
+ break
+ }
+ }
+ if r < 0 || !(r < w && w < back && back < p && p < s) {
+ t.Fatalf("order %v: want marker-read, marker-write, marker-read, placeholder, start", ev)
+ }
+ })
+ t.Run("a conflict the read back confirms", func(t *testing.T) {
+ a, b, _, _ := gateService(t)
+ b.restoreFresh = true
+ b.marker.writeErr = release.ErrCASConflict
+ if err := wakeOnce(a, rootfsGateRoute("shop.example.com")); err != nil {
+ t.Fatalf("wake = %v, want admitted: the read back found the marker", err)
+ }
+ if b.placeholders != 1 || b.starts != 1 {
+ t.Fatalf("placeholders %d, starts %d; want 1, 1", b.placeholders, b.starts)
}
})
for name, set := range map[string]func(b *fakeBackend, rt registry.Route){
"present": func(b *fakeBackend, rt registry.Route) { b.marker.marked = map[string]bool{markerID(rt): true} },
"unreadable": func(b *fakeBackend, _ registry.Route) { b.marker.readErr = errors.New("unreadable") },
"unwritable": func(b *fakeBackend, _ registry.Route) { b.marker.writeErr = errors.New("unwritable") },
+ "a conflict with nothing at the key": func(b *fakeBackend, _ registry.Route) {
+ b.marker.writeErr, b.marker.writeLost = release.ErrCASConflict, true
+ },
+ "a read back that fails": func(b *fakeBackend, _ registry.Route) { b.marker.confirmErr = errors.New("slow down") },
} {
t.Run(name, func(t *testing.T) {
a, b, _, _ := gateService(t)
@@ -322,15 +368,18 @@ func TestRootfsFirstBootMarkerOrderAndRefusals(t *testing.T) {
if b.starts != 0 || b.releases != 1 {
t.Fatalf("starts %d, releases %d; want 0, 1", b.starts, b.releases)
}
- if want := map[string]int{"present": 0, "unreadable": 0, "unwritable": 1}[name]; b.placeholders != want {
- t.Fatalf("placeholders %d, want %d", b.placeholders, want)
+ if b.placeholders != 0 {
+ t.Fatalf("placeholders %d, want 0", b.placeholders)
+ }
+ if name == "a conflict with nothing at the key" && !errors.Is(err, errRootfsMarkerUnconfirmed) {
+ t.Errorf("wake = %v, want errRootfsMarkerUnconfirmed", err)
}
})
}
}
// A binary row is untouched: a fresh wake and a warm one never read or
-// write a marker, nor record a pending first boot.
+// write a marker.
func TestBinaryWakesAskNoMarker(t *testing.T) {
for _, fresh := range []bool{true, false} {
t.Run(fmt.Sprintf("fresh=%v", fresh), func(t *testing.T) {
@@ -340,7 +389,6 @@ func TestBinaryWakesAskNoMarker(t *testing.T) {
b.restoreFresh = fresh
rt := testRoute("blog.example.com")
rt.Seed = `["seed"]`
- b.marker.pending = map[string]bool{markerID(rt): true} // never settled for a binary row
if err := wakeOnce(a, rt); err != nil {
t.Fatalf("wake: %v", err)
}