moves: refuse a storage-generations host before copying
Spec 2026-11-12 §6.3: v1 does not move a host whose location has head.json. lease.HasHead (one GET via ReadHead on NewFilesStore) and lease.ErrGenerationsMove are the box-side defence behind the console's HA refusals. - legacyMoveRound: preparing, ready and cutting are refused before the mirror lists or copies and before the cut sets status or fences. Aborting is not refused: it only deletes the staged copy and clears the move, the way out of a refused move. - offlineStageRound: refused before the whole-prefix mirror. - offlineTakeSeal: refused before the replica listing, the checkpoint and the tombstone. - promoteStagedReplica: once something is staged, refused when the own location has head.json or the staged copy carries one. Asked only after the existing guard listing finds staging, so a plain wake adds no request. A HasHead error refuses the step with that error. moveCfg.HasHead is wired by armMovesPass (nil skips, for tests without S3); ExecBackend.HasHead nil is lease.HasHead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+633
−2
internal/activator/activator_test.go+2 −0internal/activator/backend_exec.go+24 −0internal/activator/promote_generations_test.go+150 −0internal/host/moves_generations_test.go+315 −0internal/host/movesoffline_seal.go+6 −0internal/host/movesoffline_seal_test.go+2 −0internal/host/movesoffline_stage.go+6 −1internal/host/movespass.go+38 −1internal/lease/hashead_test.go+69 −0internal/lease/head.go+21 −0
diff --git a/internal/activator/activator_test.go b/internal/activator/activator_test.go| index 4519b8e56..3314cfe6c 100644 |
| --- a/internal/activator/activator_test.go |
| +++ b/internal/activator/activator_test.go |
| @@ -4675,6 +4675,8 @@ func testExecBackend(t *testing.T) *ExecBackend { |
| Cfg: Config{Bucket: "test-bucket", Idle: time.Minute}, |
| OptDir: t.TempDir(), |
| DataDir: t.TempDir(), |
| + // The root layout: nil would be lease.HasHead, a real S3 GET. |
| + HasHead: func(context.Context, string, string, string) (bool, error) { return false, nil }, |
| } |
| } |
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index 87dce5d19..10b6bb913 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -135,6 +135,11 @@ type ExecBackend struct { |
| // vouches for what it restored. Nil = the fallback does not exist, |
| // byte-identical to the pre-move behaviour. |
| Mirror lease.MirrorStore |
| + // HasHead answers whether a host's location has head.json, a |
| + // storage-generations host: the staging fallback refuses to promote |
| + // over one (spec 2026-11-12 region HA storage generations §6.3). Nil |
| + // is lease.HasHead; tests set a fake. |
| + HasHead func(ctx context.Context, bucket, loc, region string) (bool, error) |
| // Phase1, set by the agent from moves.Enabled, retires the staging |
| // fallback above: Restore never promotes a moves/<host>/ tree, because |
| // under Phase 1 the staged copy is the target's to read through the |
| @@ -4274,6 +4279,25 @@ func (b *ExecBackend) promoteStagedReplica(ctx context.Context, host string) (ma |
| if len(staged) == 0 { |
| return nil, nil |
| } |
| + // A storage-generations host is not moved in v1 (spec 2026-11-12 |
| + // §6.3): no staged copy is promoted over a location with head.json, |
| + // nor one that carries head.json (a generations host's replica, staged |
| + // by a source that did not refuse). Asked only once something is |
| + // staged, so a plain wake pays no request for it; an answer HasHead |
| + // could not give fails the wake, never goes on. |
| + if _, ok := staged[MovesPrefix(host)+"head.json"]; ok { |
| + return nil, fmt.Errorf("%s/%s: %w", b.Cfg.Bucket, MovesPrefix(host), lease.ErrGenerationsMove) |
| + } |
| + hasHead := b.HasHead |
| + if hasHead == nil { |
| + hasHead = lease.HasHead |
| + } |
| + own := b.Cfg.hibernatePrefix(host) |
| + if has, err := hasHead(ctx, b.Cfg.Bucket, own, b.Cfg.Region); err != nil { |
| + return nil, err |
| + } else if has { |
| + return nil, fmt.Errorf("%s/%s: %w", b.Cfg.Bucket, own, lease.ErrGenerationsMove) |
| + } |
| log.Printf("activator: %s: promoting %d staged object(s) from %s (residence move landing)", host, len(staged), MovesPrefix(host)) |
| // No receipts: a staged object is the source box's single-part copy, |
| // and an in-bucket copy of it keeps its ETag, so plain ETag equality |
diff --git a/internal/activator/promote_generations_test.go b/internal/activator/promote_generations_test.go| new file mode 100644 |
| index 000000000..547b67bda |
| --- /dev/null |
| +++ b/internal/activator/promote_generations_test.go |
| @@ -0,0 +1,150 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "path/filepath" |
| + "reflect" |
| + "sync" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// Spec 2026-11-12 region HA storage generations §6.3: the staging |
| +// fallback never promotes a staged copy over a storage-generations host's |
| +// location (head.json there) nor promotes a staged copy that carries a |
| +// head.json. It is asked only once something is staged, so a plain wake |
| +// pays no request for it. |
| + |
| +// recMirrorStore records the promotion's copies and deletes. |
| +type recMirrorStore struct { |
| + *fakeMirrorStore |
| + mu sync.Mutex |
| + ops []string |
| +} |
| + |
| +func (m *recMirrorStore) Copy(ctx context.Context, sb, sk, se, db, dk string) (string, error) { |
| + m.mu.Lock() |
| + m.ops = append(m.ops, "copy "+db+"/"+dk) |
| + m.mu.Unlock() |
| + return m.fakeMirrorStore.Copy(ctx, sb, sk, se, db, dk) |
| +} |
| + |
| +func (m *recMirrorStore) Delete(ctx context.Context, bucket, key string) error { |
| + m.mu.Lock() |
| + m.ops = append(m.ops, "delete "+bucket+"/"+key) |
| + m.mu.Unlock() |
| + return m.fakeMirrorStore.Delete(ctx, bucket, key) |
| +} |
| + |
| +type promoteGen struct { |
| + b *ExecBackend |
| + m *recMirrorStore |
| + asks []string |
| + fail error |
| +} |
| + |
| +func newPromoteGen(t *testing.T) *promoteGen { |
| + p := &promoteGen{b: testExecBackend(t), m: &recMirrorStore{fakeMirrorStore: newFakeMirrorStore()}} |
| + p.b.Cfg.Region = "eu-west-1" |
| + p.b.Mirror = p.m |
| + p.b.HasHead = func(_ context.Context, bucket, loc, region string) (bool, error) { |
| + p.asks = append(p.asks, bucket+"/"+loc+"/head.json@"+region) |
| + if p.fail != nil { |
| + return false, p.fail |
| + } |
| + _, ok := p.m.buckets[bucket][loc+"/head.json"] |
| + return ok, nil |
| + } |
| + return p |
| +} |
| + |
| +func TestPromoteGenerationsRefusedOverAHeadJSON(t *testing.T) { |
| + p := newPromoteGen(t) |
| + host := "shop.example.com" |
| + own := p.b.Cfg.hibernatePrefix(host) |
| + p.m.put(p.b.Cfg.Bucket, own+"/head.json", "h1") |
| + p.m.put(p.b.Cfg.Bucket, own+"/g-00000000000000000007/ltx/0/a.ltx", "e0") |
| + p.m.put(p.b.Cfg.Bucket, MovesPrefix(host)+"generations/a/snapshot", "e1") |
| + before := len(p.m.buckets[p.b.Cfg.Bucket]) |
| + |
| + staged, err := p.b.promoteStagedReplica(context.Background(), host) |
| + if !errors.Is(err, lease.ErrGenerationsMove) || staged != nil { |
| + t.Fatalf("promote = %v, %v; want ErrGenerationsMove", staged, err) |
| + } |
| + if want := []string{p.b.Cfg.Bucket + "/" + own + "/head.json@eu-west-1"}; !reflect.DeepEqual(p.asks, want) { |
| + t.Fatalf("head asks %v, want %v", p.asks, want) |
| + } |
| + if len(p.m.ops) != 0 || len(p.m.buckets[p.b.Cfg.Bucket]) != before { |
| + t.Fatalf("promotion wrote: %v", p.m.ops) |
| + } |
| +} |
| + |
| +// A staged copy that is a generations host's replica is not promoted |
| +// either, whatever the own location holds. |
| +func TestPromoteGenerationsRefusesAStagedHeadJSON(t *testing.T) { |
| + p := newPromoteGen(t) |
| + host := "shop.example.com" |
| + p.m.put(p.b.Cfg.Bucket, MovesPrefix(host)+"head.json", "h1") |
| + p.m.put(p.b.Cfg.Bucket, MovesPrefix(host)+"g-00000000000000000007/ltx/0/a.ltx", "e1") |
| + if _, err := p.b.promoteStagedReplica(context.Background(), host); !errors.Is(err, lease.ErrGenerationsMove) { |
| + t.Fatalf("promote = %v, want ErrGenerationsMove", err) |
| + } |
| + if len(p.m.ops) != 0 { |
| + t.Fatalf("promotion wrote: %v", p.m.ops) |
| + } |
| +} |
| + |
| +func TestPromoteGenerationsRefusesAnUnknownAnswer(t *testing.T) { |
| + p := newPromoteGen(t) |
| + host := "shop.example.com" |
| + p.m.put(p.b.Cfg.Bucket, MovesPrefix(host)+"generations/a/snapshot", "e1") |
| + p.fail = errors.New("head.json: read: injected") |
| + if _, err := p.b.promoteStagedReplica(context.Background(), host); !errors.Is(err, p.fail) { |
| + t.Fatalf("promote = %v, want the head read's error", err) |
| + } |
| + if len(p.m.ops) != 0 { |
| + t.Fatalf("promotion wrote: %v", p.m.ops) |
| + } |
| +} |
| + |
| +// Nothing staged: no head.json request. Staged over the root layout: the |
| +// promotion is today's. |
| +func TestPromoteGenerationsRootLayoutPromotes(t *testing.T) { |
| + p := newPromoteGen(t) |
| + host := "shop.example.com" |
| + if staged, err := p.b.promoteStagedReplica(context.Background(), host); err != nil || staged != nil || len(p.asks) != 0 { |
| + t.Fatalf("nothing staged: %v, %v, asks %v; want no request", staged, err, p.asks) |
| + } |
| + p.m.put(p.b.Cfg.Bucket, MovesPrefix(host)+"generations/a/snapshot", "e1") |
| + staged, err := p.b.promoteStagedReplica(context.Background(), host) |
| + if err != nil || len(staged) != 1 || len(p.asks) != 1 { |
| + t.Fatalf("promote = %v, %v, asks %v", staged, err, p.asks) |
| + } |
| + if _, ok := p.m.buckets[p.b.Cfg.Bucket][p.b.Cfg.hibernatePrefix(host)+"/generations/a/snapshot"]; !ok { |
| + t.Fatal("the staged copy was not promoted") |
| + } |
| +} |
| + |
| +// Through Restore: the refusal fails the wake, never falls through to a |
| +// fresh boot. |
| +func TestPromoteGenerationsFailsTheWake(t *testing.T) { |
| + p := newPromoteGen(t) |
| + host := "shop.example.com" |
| + p.m.put(p.b.Cfg.Bucket, p.b.Cfg.hibernatePrefix(host)+"/head.json", "h1") |
| + p.m.put(p.b.Cfg.Bucket, MovesPrefix(host)+"generations/a/snapshot", "e1") |
| + restored := false |
| + old := restoreReplica |
| + restoreReplica = func(context.Context, string, string, string, string) (bool, error) { |
| + restored = true |
| + return false, nil |
| + } |
| + t.Cleanup(func() { restoreReplica = old }) |
| + rt := registry.Route{Host: host, Kind: "instance", Socket: filepath.Join(t.TempDir(), "i.sock")} |
| + fresh, err := p.b.Restore(context.Background(), rt) |
| + if !errors.Is(err, lease.ErrGenerationsMove) || fresh || restored { |
| + t.Fatalf("restore = fresh %v, %v, restored %v; want the refusal", fresh, err, restored) |
| + } |
| +} |
diff --git a/internal/host/moves_generations_test.go b/internal/host/moves_generations_test.go| new file mode 100644 |
| index 000000000..803ebb847 |
| --- /dev/null |
| +++ b/internal/host/moves_generations_test.go |
| @@ -0,0 +1,315 @@ |
| +//go:build linux |
| + |
| +package host |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "reflect" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/activator" |
| + "github.com/carlosframework/platform/internal/hostwrite" |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/moves" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/replicagen" |
| +) |
| + |
| +// Spec 2026-11-12 region HA storage generations §6.3: v1 refuses every move |
| +// of a storage-generations host (one whose location has head.json) before |
| +// anything is listed, copied, tombstoned or deleted. A host without |
| +// head.json takes today's path. |
| + |
| +// genHeadBody is a valid head.json body. |
| +func genHeadBody(t *testing.T) []byte { |
| + t.Helper() |
| + at := time.Date(2026, 11, 17, 9, 0, 0, 0, time.UTC) |
| + b, err := json.Marshal(lease.Head{V: lease.HeadVersion, Generation: replicagen.Name(7), |
| + CreatedBy: lease.HeadCreator{LockGeneration: 7, Box: "ie-a", IID: "i-0abc", Lifetime: "inc-1", At: at}, |
| + History: []lease.HeadHistory{}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return b |
| +} |
| + |
| +// recMirror records every mirror call the legacy round makes. |
| +type recMirror struct { |
| + lease.MirrorStore |
| + mu sync.Mutex |
| + ops []string |
| +} |
| + |
| +func (m *recMirror) log(op, bucket, key string) { |
| + m.mu.Lock() |
| + m.ops = append(m.ops, op+" "+bucket+"/"+key) |
| + m.mu.Unlock() |
| +} |
| +func (m *recMirror) List(ctx context.Context, bucket, prefix string) (map[string]lease.MirrorObject, error) { |
| + m.log("list", bucket, prefix) |
| + return m.MirrorStore.List(ctx, bucket, prefix) |
| +} |
| +func (m *recMirror) Copy(ctx context.Context, sb, sk, se, db, dk string) (string, error) { |
| + m.log("copy", db, dk) |
| + return m.MirrorStore.Copy(ctx, sb, sk, se, db, dk) |
| +} |
| +func (m *recMirror) Delete(ctx context.Context, bucket, key string) error { |
| + m.log("delete", bucket, key) |
| + return m.MirrorStore.Delete(ctx, bucket, key) |
| +} |
| +func (m *recMirror) Get(ctx context.Context, bucket, key string) ([]byte, error) { |
| + m.log("get", bucket, key) |
| + return m.MirrorStore.Get(ctx, bucket, key) |
| +} |
| +func (m *recMirror) Put(ctx context.Context, bucket, key string, body []byte) error { |
| + m.log("put", bucket, key) |
| + return m.MirrorStore.Put(ctx, bucket, key, body) |
| +} |
| + |
| +func (m *recMirror) taken() []string { |
| + m.mu.Lock() |
| + defer m.mu.Unlock() |
| + return append([]string(nil), m.ops...) |
| +} |
| + |
| +// genLegacy is the legacy harness with a recording mirror and a HasHead |
| +// that reads head.json from the fake bucket, as lease.HasHead reads it |
| +// from S3, recording each GET. |
| +type genLegacy struct { |
| + *moveHarness |
| + m *recMirror |
| + asks []string |
| + fail error |
| +} |
| + |
| +func newGenLegacy(t *testing.T) *genLegacy { |
| + g := &genLegacy{moveHarness: newMoveHarness(t)} |
| + g.m = &recMirror{MirrorStore: g.f} |
| + g.cfg.Mirror = func(context.Context, string) (lease.MirrorStore, error) { return g.m, nil } |
| + g.cfg.HasHead = func(_ context.Context, bucket, loc, region string) (bool, error) { |
| + g.asks = append(g.asks, bucket+"/"+loc+"/head.json@"+region) |
| + if g.fail != nil { |
| + return false, g.fail |
| + } |
| + _, ok := g.f.buckets[bucket][loc+"/head.json"] |
| + return ok, nil |
| + } |
| + return g |
| +} |
| + |
| +func (g *genLegacy) round(t *testing.T, host string) error { |
| + t.Helper() |
| + ctx := context.Background() |
| + targets, err := moveTargets(ctx, g.s) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return legacyMoveRound(ctx, g.s, g.worker(), g.read(t, host), targets) |
| +} |
| + |
| +func TestGenerationsMoveLegacyRoundRefusedBeforeAnyRequest(t *testing.T) { |
| + now := time.Now() |
| + for _, tc := range []struct { |
| + name string |
| + steps []func(*provision.Record) error |
| + }{ |
| + {"preparing", []func(*provision.Record) error{provision.MoveRequest("uk", now)}}, |
| + {"ready", []func(*provision.Record) error{provision.MoveRequest("uk", now), provision.MovePromoteReady(now)}}, |
| + {"cutting", []func(*provision.Record) error{provision.MoveRequest("uk", now), provision.MovePromoteReady(now), provision.MoveMarkCutting()}}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + g := newGenLegacy(t) |
| + host := "shop.example.com" |
| + g.record(t, host, tc.steps...) |
| + g.f.put("data-ie", "instances/"+host+"/head.json", "h1") |
| + g.f.put("data-ie", "instances/"+host+"/"+replicagen.Name(7)+"/ltx/0/0000000000000001-0000000000000001.ltx", "e1") |
| + g.f.put("data-uk", "moves/"+host+"/stale", "s1") |
| + before := g.read(t, host) |
| + fenced := 0 |
| + g.cfg.Fence = func(context.Context, string, uint64) error { fenced++; return nil } |
| + |
| + err := g.round(t, host) |
| + if !errors.Is(err, lease.ErrGenerationsMove) { |
| + t.Fatalf("round = %v, want ErrGenerationsMove", err) |
| + } |
| + if want := []string{"data-ie/instances/" + host + "/head.json@eu-west-1"}; !reflect.DeepEqual(g.asks, want) { |
| + t.Fatalf("head asks %v, want %v", g.asks, want) |
| + } |
| + if ops := g.m.taken(); len(ops) != 0 { |
| + t.Fatalf("mirror calls %v, want none", ops) |
| + } |
| + if len(g.f.ops) != 0 || len(g.statuses) != 0 || fenced != 0 { |
| + t.Fatalf("ops %v, statuses %v, fences %d: want none", g.f.ops, g.statuses, fenced) |
| + } |
| + if after := g.read(t, host); !reflect.DeepEqual(after.Move, before.Move) || after.Region != before.Region { |
| + t.Fatalf("record moved: %+v, was %+v", after.Move, before.Move) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// An answer HasHead cannot give refuses the round with its error. |
| +func TestGenerationsMoveLegacyRoundRefusesAnUnknownAnswer(t *testing.T) { |
| + g := newGenLegacy(t) |
| + host := "shop.example.com" |
| + g.record(t, host, provision.MoveRequest("uk", time.Now())) |
| + g.f.put("data-ie", "instances/"+host+"/generations/g/snapshot", "e1") |
| + g.fail = errors.New("head.json: read: injected") |
| + if err := g.round(t, host); !errors.Is(err, g.fail) { |
| + t.Fatalf("round = %v, want the head read's error", err) |
| + } |
| + if ops := g.m.taken(); len(ops) != 0 { |
| + t.Fatalf("mirror calls %v, want none", ops) |
| + } |
| +} |
| + |
| +// Without head.json the round is today's: the staged copy is made. |
| +func TestGenerationsMoveLegacyRoundRootLayoutCopies(t *testing.T) { |
| + g := newGenLegacy(t) |
| + host := "shop.example.com" |
| + g.record(t, host, provision.MoveRequest("uk", time.Now())) |
| + g.f.put("data-ie", "instances/"+host+"/generations/g/snapshot", "e1") |
| + if err := g.round(t, host); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(g.asks) != 1 { |
| + t.Fatalf("head asks %v, want one", g.asks) |
| + } |
| + if staged, _ := g.f.List(context.Background(), "data-uk", "moves/"+host+"/"); len(staged) != 1 { |
| + t.Fatalf("staged %v, want the snapshot", staged) |
| + } |
| +} |
| + |
| +// Abort is the way out of a refused move: it deletes only the staged copy |
| +// and clears the move, and asks nothing of head.json. |
| +func TestGenerationsMoveLegacyAbortStillCompletes(t *testing.T) { |
| + g := newGenLegacy(t) |
| + host := "shop.example.com" |
| + g.record(t, host, provision.MoveRequest("uk", time.Now()), provision.MoveAbort()) |
| + g.f.put("data-ie", "instances/"+host+"/head.json", "h1") |
| + g.f.put("data-uk", "moves/"+host+"/stale", "s1") |
| + if err := g.round(t, host); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(g.asks) != 0 { |
| + t.Fatalf("abort asked %v", g.asks) |
| + } |
| + if got := g.read(t, host); got.Move != nil { |
| + t.Fatalf("abort did not complete: %+v", got.Move) |
| + } |
| + if _, ok := g.f.buckets["data-ie"]["instances/"+host+"/head.json"]; !ok { |
| + t.Fatal("abort touched the source's head.json") |
| + } |
| +} |
| + |
| +// armMovesPass wires lease.HasHead: a nil HasHead skips the refusal. |
| +func TestGenerationsMoveArmWiresHasHead(t *testing.T) { |
| + prev := armedMovesWorker.Load() |
| + t.Cleanup(func() { armedMovesWorker.Store(prev) }) |
| + armMovesPass(activator.Config{}, t.TempDir(), nil, nil, nil, hostwrite.Config{}, ClaimProviders{}, nil, nil, nil) |
| + if armedMovesWorker.Load().cfg.HasHead == nil { |
| + t.Fatal("armMovesPass left HasHead nil: the generations refusal is off") |
| + } |
| +} |
| + |
| +// genS3 records the seal world's S3 calls under the host's replica and |
| +// staging prefixes (FailOn sees list|get|copy|delete|put; a GET of an |
| +// absent key is answered before it). |
| +type genS3 struct { |
| + mu sync.Mutex |
| + ops []string |
| +} |
| + |
| +func (sw *sealWorld) recordHostS3() *genS3 { |
| + r := &genS3{} |
| + src, dst := sealPrefix+"/"+offHost+"/", activator.MovesPrefix(offHost) |
| + sw.fake.FailOn = func(op, key string) error { |
| + if strings.HasPrefix(key, src) || strings.HasPrefix(key, dst) { |
| + r.mu.Lock() |
| + r.ops = append(r.ops, op+" "+key) |
| + r.mu.Unlock() |
| + } |
| + return nil |
| + } |
| + sw.t.Cleanup(func() { sw.fake.FailOn = nil }) |
| + return r |
| +} |
| + |
| +func (r *genS3) taken() []string { |
| + r.mu.Lock() |
| + defer r.mu.Unlock() |
| + return append([]string(nil), r.ops...) |
| +} |
| + |
| +func (sw *sealWorld) putGenHead() string { |
| + key := sealPrefix + "/" + offHost + "/head.json" |
| + sw.fake.Put(sealIE, key, genHeadBody(sw.t)) |
| + return key |
| +} |
| + |
| +func TestGenerationsMoveOfflineStageRefusedBeforeAnyCopy(t *testing.T) { |
| + sw := newSealWorld(t) |
| + ie := sw.ie |
| + sw.instance(ie) |
| + head := sw.putGenHead() |
| + sw.stage() |
| + rec := sw.recordHostS3() |
| + |
| + err := sw.stagePass(ie) |
| + if !errors.Is(err, lease.ErrGenerationsMove) { |
| + t.Fatalf("stage pass = %v, want ErrGenerationsMove", err) |
| + } |
| + if got, want := rec.taken(), []string{"get " + head}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("S3 calls %v, want only %v", got, want) |
| + } |
| + if staged := sw.stagedRel(); len(staged) != 0 { |
| + t.Fatalf("staged %d objects", len(staged)) |
| + } |
| + r := sw.report(moves.OfflineRoleStager, "ie-a") |
| + if r.Phase != moves.OfflinePhaseFailed || !strings.Contains(r.StageError, lease.ErrGenerationsMove.Error()) { |
| + t.Fatalf("stager report %+v", r) |
| + } |
| +} |
| + |
| +func TestGenerationsMoveOfflineSealRefusedBeforeTheTombstone(t *testing.T) { |
| + sw := newSealWorld(t) |
| + ie := sw.ie |
| + sw.instance(ie) |
| + head := sw.putGenHead() |
| + sw.startAndSeal("ie-a", ie) |
| + owner0, expires0, gen0 := sw.lock() |
| + rec := sw.recordHostS3() |
| + |
| + err := sw.pass(ie) |
| + if !errors.Is(err, lease.ErrGenerationsMove) { |
| + t.Fatalf("seal pass = %v, want ErrGenerationsMove", err) |
| + } |
| + if got, want := rec.taken(), []string{"get " + head}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("S3 calls %v, want only %v", got, want) |
| + } |
| + if owner, expires, gen := sw.lock(); owner != owner0 || !expires.Equal(expires0) || gen != gen0 || strings.Contains(owner, moves.TombstoneOwner(offID)) { |
| + t.Fatalf("lock %q %v %d, was %q %v %d: the seal tombstoned", owner, expires, gen, owner0, expires0, gen0) |
| + } |
| + if _, _, ok := sw.sealRecord(); ok || sw.record().Move.State != provision.OfflineSealing { |
| + t.Fatal("sealed a generations host") |
| + } |
| + if staged := sw.stagedRel(); len(staged) != 0 { |
| + t.Fatalf("staged %d objects", len(staged)) |
| + } |
| +} |
| + |
| +// Without head.json the stage round is today's, through the production |
| +// HasHead (arm wires lease.HasHead as armMovesPass does). |
| +func TestGenerationsMoveOfflineStageRootLayoutCopies(t *testing.T) { |
| + sw := newSealWorld(t) |
| + ie := sw.ie |
| + sw.instance(ie) |
| + sw.stage() |
| + sw.mustStagePass(ie) |
| + sameSizes(t, sw.stagedRel(), sw.replicaRel()) |
| +} |
diff --git a/internal/host/movesoffline_seal.go b/internal/host/movesoffline_seal.go| index f98559c24..7ce35ff73 100644 |
| --- a/internal/host/movesoffline_seal.go |
| +++ b/internal/host/movesoffline_seal.go |
| @@ -365,6 +365,12 @@ func (w *movesWorker) offlineTakeSeal(ctx context.Context, s release.Store, rec |
| if err != nil { |
| return seal, err |
| } |
| + // A storage-generations host is not moved in v1 (spec 2026-11-12 |
| + // §6.3): refused before the replica is listed or checkpointed and so |
| + // before the tombstone. |
| + if err := cfg.refuseGenerationsMove(ctx, bucket, loc, region); err != nil { |
| + return seal, err |
| + } |
| srcPrefix := loc + "/" |
| dstPrefix := activator.MovesPrefix(host) |
| dbPath := rt.DBPath |
diff --git a/internal/host/movesoffline_seal_test.go b/internal/host/movesoffline_seal_test.go| index 96d4b7c47..f01c78632 100644 |
| --- a/internal/host/movesoffline_seal_test.go |
| +++ b/internal/host/movesoffline_seal_test.go |
| @@ -111,6 +111,8 @@ func (sw *sealWorld) arm(b *offBox) { |
| c.Mirror = func(ctx context.Context, dst string) (lease.MirrorStore, error) { |
| return lease.NewS3Mirror(ctx, sealIERegion, dst) |
| } |
| + // armMovesPass's head.json question (spec §6.3). |
| + c.HasHead = lease.HasHead |
| // armMovesPass's Checkpoint, as it is wired there. |
| c.Checkpoint = func(ctx context.Context, dbPath string) error { |
| host := strings.TrimSuffix(filepath.Base(dbPath), ".db") |
diff --git a/internal/host/movesoffline_stage.go b/internal/host/movesoffline_stage.go| index c3cb2767c..ffa691ec3 100644 |
| --- a/internal/host/movesoffline_stage.go |
| +++ b/internal/host/movesoffline_stage.go |
| @@ -142,7 +142,7 @@ func (w *movesWorker) offlineStageRound(ctx context.Context, s release.Store, re |
| if tgt.DataBucket != m.StageBucket || tgt.DataBucketRegion != m.StageRegion { |
| return stageProgress{}, fmt.Errorf("the target now publishes %s (%s), not the staging bucket %s (%s) the stage fixed", tgt.DataBucket, tgt.DataBucketRegion, m.StageBucket, m.StageRegion) |
| } |
| - bucket, loc, _, err := cfg.tombstoneAt(host) |
| + bucket, loc, region, err := cfg.tombstoneAt(host) |
| if err != nil { |
| return stageProgress{}, err |
| } |
| @@ -154,6 +154,11 @@ func (w *movesWorker) offlineStageRound(ctx context.Context, s release.Store, re |
| if m.StageBucket == bucket { |
| return stageProgress{}, fmt.Errorf("the staging bucket is this box's own bucket %s; a staged copy there would replace the serving replica on its next wake", bucket) |
| } |
| + // A storage-generations host is not moved in v1 (spec 2026-11-12 |
| + // §6.3): refused before the round lists or copies anything. |
| + if err := cfg.refuseGenerationsMove(ctx, bucket, loc, region); err != nil { |
| + return stageProgress{}, err |
| + } |
| mirror, err := w.offlineMirror(ctx, m.StageRegion) |
| if err != nil { |
| return stageProgress{}, err |
diff --git a/internal/host/movespass.go b/internal/host/movespass.go| index d4f2fe74b..9fbdefbcd 100644 |
| --- a/internal/host/movespass.go |
| +++ b/internal/host/movespass.go |
| @@ -306,6 +306,7 @@ func armMovesPass(hib activator.Config, dataDir string, reg *registry.Registry, |
| return nil |
| }, |
| LiftFence: svc.LiftFence, |
| + HasHead: lease.HasHead, |
| SetStatus: reg.SetStatus, |
| CarryRequest: carry, |
| OwnsRoute: func(host string) bool { |
| @@ -753,7 +754,14 @@ type moveCfg struct { |
| // safe. The cut deletes the warm cache, so it must first prove the own |
| // replica really carries the tail. Nil skips (tests without S3). |
| Checkpoint func(ctx context.Context, dbPath string) error |
| - Now func() time.Time |
| + // HasHead answers whether a host's replica location has head.json, a |
| + // storage-generations host (lease.HasHead, wired by armMovesPass). The |
| + // legacy round, the offline stage round and the seal refuse such a |
| + // host with lease.ErrGenerationsMove before they list, copy, tombstone |
| + // or delete anything (spec 2026-11-12 region HA storage generations |
| + // §6.3). Nil skips (tests without S3). |
| + HasHead func(ctx context.Context, bucket, loc, region string) (bool, error) |
| + Now func() time.Time |
| // ReadyDelta is the readiness bar (#175): a preparing round whose |
| // lease.MirrorRound.DeltaBytes (bytes copied plus bytes deferred) is at |
| // most this promotes the move to ready. Zero means zero bytes moved: a |
| @@ -1517,6 +1525,17 @@ func legacyMoveRound(ctx context.Context, s release.Store, w *movesWorker, rec p |
| // written but never read back. |
| receipts := lease.Receipts{Bucket: cfg.OwnBucket, Key: activator.MoveReceiptsKey(rec.Host)} |
| + // A storage-generations host is not moved in v1 (spec 2026-11-12 §6.3): |
| + // refused before a round lists or copies anything and before the cut |
| + // quiesces the host. Abort is not refused: it deletes only the staged |
| + // copy and clears the move, the way out of a move refused here. |
| + switch rec.Move.State { |
| + case provision.MovePreparing, provision.MoveReady, provision.MoveCutting: |
| + if err := cfg.refuseGenerationsMove(ctx, cfg.OwnBucket, srcPrefix, cfg.OwnBucketRegion); err != nil { |
| + return err |
| + } |
| + } |
| + |
| switch rec.Move.State { |
| case provision.MovePreparing, provision.MoveReady: |
| m, err := mirror() |
| @@ -1663,6 +1682,24 @@ func legacyMoveRound(ctx context.Context, s release.Store, w *movesWorker, rec p |
| return nil |
| } |
| +// refuseGenerationsMove is nil only when HasHead answers that loc has no |
| +// head.json (or HasHead is nil). A head.json refuses the step with an error |
| +// wrapping lease.ErrGenerationsMove; an answer HasHead could not give |
| +// refuses it with that error, never goes on. |
| +func (c moveCfg) refuseGenerationsMove(ctx context.Context, bucket, loc, region string) error { |
| + if c.HasHead == nil { |
| + return nil |
| + } |
| + has, err := c.HasHead(ctx, bucket, loc, region) |
| + if err != nil { |
| + return err |
| + } |
| + if has { |
| + return fmt.Errorf("%s/%s: %w", bucket, loc, lease.ErrGenerationsMove) |
| + } |
| + return nil |
| +} |
| + |
| // clearMoveReceipts deletes a finished or abandoned move's copy receipts. |
| // Best-effort, after the record write that ended the move: receipts are |
| // self-checking (a stale one can only fail to match, never vouch for the |
diff --git a/internal/lease/hashead_test.go b/internal/lease/hashead_test.go| new file mode 100644 |
| index 000000000..98fffe8da |
| --- /dev/null |
| +++ b/internal/lease/hashead_test.go |
| @@ -0,0 +1,69 @@ |
| +package lease_test |
| + |
| +import ( |
| + "encoding/json" |
| + "errors" |
| + "reflect" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/lease" |
| +) |
| + |
| +// HasHead is the moves' question (spec §6.3): does this location have |
| +// head.json? One GET, over the production store (CARLOS_S3_ENDPOINT); an |
| +// answer it cannot read is an error, never "no". |
| +func TestHasHeadOneGetAndNeverAbsentOnAnError(t *testing.T) { |
| + ctx := t.Context() |
| + fx := newRVFixture(t) |
| + key := rvLoc + "/head.json" |
| + wantReqs := []string{"GET " + key} |
| + |
| + has, err := lease.HasHead(ctx, rvBucket, rvLoc, rvRegion) |
| + if err != nil || has { |
| + t.Fatalf("HasHead(absent) = %v, %v; want false, nil", has, err) |
| + } |
| + if got := fx.requests(); !reflect.DeepEqual(got, wantReqs) { |
| + t.Fatalf("absent: requests %v, want %v", got, wantReqs) |
| + } |
| + |
| + body, err := json.Marshal(sampleHead()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.m.Put(rvBucket, key, body) |
| + fx.mu.Lock() |
| + fx.reqs = nil |
| + fx.mu.Unlock() |
| + has, err = lease.HasHead(ctx, rvBucket, rvLoc, rvRegion) |
| + if err != nil || !has { |
| + t.Fatalf("HasHead(present) = %v, %v; want true, nil", has, err) |
| + } |
| + if got := fx.requests(); !reflect.DeepEqual(got, wantReqs) { |
| + t.Fatalf("present: requests %v, want %v", got, wantReqs) |
| + } |
| + |
| + // A head.json that does not decode is not absent. |
| + fx.m.Put(rvBucket, key, []byte(`{"v":1,`)) |
| + if has, err := lease.HasHead(ctx, rvBucket, rvLoc, rvRegion); err == nil || has { |
| + t.Fatalf("HasHead(undecodable) = %v, %v; want an error", has, err) |
| + } |
| + |
| + // A failed read is not absent either. |
| + boom := errors.New("injected read failure") |
| + fx.m.FailOn = func(op, k string) error { |
| + if op == "get" && k == key { |
| + return boom |
| + } |
| + return nil |
| + } |
| + if has, err := lease.HasHead(ctx, rvBucket, rvLoc, rvRegion); err == nil || has { |
| + t.Fatalf("HasHead(read failure) = %v, %v; want an error", has, err) |
| + } |
| +} |
| + |
| +func TestErrGenerationsMoveText(t *testing.T) { |
| + const want = "moves of a storage-generations host are not supported yet (region HA storage generations, spec §6.3)" |
| + if got := lease.ErrGenerationsMove.Error(); got != want { |
| + t.Fatalf("ErrGenerationsMove = %q, want %q", got, want) |
| + } |
| +} |
diff --git a/internal/lease/head.go b/internal/lease/head.go| index 3477cec21..dd4829959 100644 |
| --- a/internal/lease/head.go |
| +++ b/internal/lease/head.go |
| @@ -115,6 +115,27 @@ func ReadHead(ctx context.Context, st release.Store, loc string) (h Head, etag s |
| return h, etag, true, nil |
| } |
| +// ErrGenerationsMove refuses a move of a storage-generations host, one |
| +// whose location has head.json: v1 moves only the root layout (spec |
| +// 2026-11-12 region HA storage generations, §6.3). Every move step that |
| +// would list, copy, tombstone or delete such a host's replica asks HasHead |
| +// first and returns an error wrapping this one. |
| +var ErrGenerationsMove = errors.New("moves of a storage-generations host are not supported yet (region HA storage generations, spec §6.3)") |
| + |
| +// HasHead reports whether s3://bucket/loc has a head.json, that is, |
| +// whether the host is a storage-generations host: one GET, ReadHead over |
| +// NewFilesStore. Absent is false and a nil error. Any other read failure, |
| +// or a head.json that does not decode or validate, is an error and never |
| +// reads as absent, so a caller never goes on from an unknown answer. |
| +func HasHead(ctx context.Context, bucket, loc, region string) (bool, error) { |
| + st, err := NewFilesStore(ctx, bucket, region) |
| + if err != nil { |
| + return false, err |
| + } |
| + _, _, found, err := ReadHead(ctx, st, loc) |
| + return found, err |
| +} |
| + |
| // PutHead writes loc's head.json and returns its new ETag. ifMatch "" is |
| // create-only (If-None-Match: *); otherwise it replaces only the head with |
| // that ETag. A lost race is release.ErrCASConflict. A head that is not |