| index 59e2f8fce..99303d3df 100644 |
| --- a/internal/host/region_ha_g2_pool_root_linux_test.go |
| +++ b/internal/host/region_ha_g2_pool_root_linux_test.go |
| @@ -9,9 +9,11 @@ import ( |
| "crypto/hmac" |
| "crypto/rand" |
| "crypto/sha256" |
| + "crypto/x509" |
| "encoding/base64" |
| "encoding/hex" |
| "encoding/json" |
| + "encoding/pem" |
| "errors" |
| "fmt" |
| "io" |
| @@ -201,27 +203,37 @@ func g2ZeroAcquire(events []g2S3Event, ip, host string) error { |
| return nil |
| } |
| func g2NoLocal(t *testing.T, box *g2Box, storage *g2Storage, since int) { |
| + t.Helper() |
| + g2NoLocalHost(t, box, storage, since, g2Host) |
| +} |
| +func g2Stopped(t *testing.T, state g2State, host string) { |
| + t.Helper() |
| + pid, ok := state.TenantPIDs[host] |
| + if !ok || (pid != "0" && pid != "") || strings.Contains(state.TenantCgroups[host], "populated 1") { |
| + t.Fatalf("%s: host %s has live or missing process evidence: %+v", g2PoolViolation, host, state) |
| + } |
| +} |
| +func g2NoLocalHost(t *testing.T, box *g2Box, storage *g2Storage, since int, host string) { |
| t.Helper() |
| state := box.state() |
| t.Logf("A evidence %s", g2JSON(t, state)) |
| - if state.TenantPIDs[g2Host] != "0" && state.TenantPIDs[g2Host] != "" { |
| - t.Fatalf("%s: local PID %s", g2PoolViolation, state.TenantPIDs[g2Host]) |
| - } |
| - if strings.Contains(state.TenantCgroups[g2Host], "populated 1") { |
| - t.Fatalf("%s: populated local tenant cgroup", g2PoolViolation) |
| - } |
| - g2Must(t, g2ZeroAcquire(storage.snapshot()[since:], box.ip, g2Host)) |
| - view := g2Lock(t, storage.data, g2Host) |
| + g2Stopped(t, state, host) |
| + g2Must(t, g2ZeroAcquire(storage.snapshot()[since:], box.ip, host)) |
| + view := g2Lock(t, storage.data, host) |
| t.Logf("lock evidence %s", g2JSON(t, view)) |
| if owner, ok := hafresh.ParseOwner(view.Lock.Owner); ok && owner.IID == box.iid && !view.Absent { |
| t.Fatalf("%s: A holds lock", g2PoolViolation) |
| } |
| } |
| func g2WaitReleased(t *testing.T, storage *g2Storage, iid string) hafresh.View { |
| + t.Helper() |
| + return g2WaitReleasedHost(t, storage, iid, g2Host) |
| +} |
| +func g2WaitReleasedHost(t *testing.T, storage *g2Storage, iid, host string) hafresh.View { |
| t.Helper() |
| var view hafresh.View |
| g2Wait(t, "real idle sleep and release", 100*time.Second, func() bool { |
| - view = g2Lock(t, storage.data, g2Host) |
| + view = g2Lock(t, storage.data, host) |
| return view.Lock.Release != nil && view.Lock.Release.IID == iid |
| }) |
| t.Logf("release evidence %s", g2JSON(t, view)) |
| @@ -244,11 +256,38 @@ func g2WaitPeer(t *testing.T, store release.Store, ref string, pools []string) { |
| // The real edge polls its peer view; no in-process refresh seam. |
| time.Sleep(35 * time.Second) |
| } |
| +func g2PoolRecords() []provision.Record { |
| + var records []provision.Record |
| + for _, host := range []string{g2Host, g2AbsentOldHost, g2AbsentRowHost} { |
| + records = append(records, provision.Record{Schema: 1, Host: host, Slug: strings.SplitN(host, ".", 2)[0], Account: g2Account, App: g2App, Pool: "ha", Region: "ie", Channel: "stable", HA: true, CreatedAt: time.Now().Add(-time.Hour).UTC()}) |
| + } |
| + return records |
| +} |
| +func g2AbsentPrefix(ctx context.Context, store release.Store, host string, leftover bool) error { |
| + prefix := "instances/" + host + "/" |
| + keys, err := store.List(ctx, prefix) |
| + if err != nil { |
| + return err |
| + } |
| + if slices.Contains(keys, prefix+"lock.json") || (len(keys) > 0) != leftover { |
| + return fmt.Errorf("absent-lock precondition host=%s leftover=%t keys=%v", host, leftover, keys) |
| + } |
| + return nil |
| +} |
| +func g2HeldBy(t *testing.T, storage *g2Storage, host, iid string) { |
| + t.Helper() |
| + view := g2Lock(t, storage.data, host) |
| + owner, ok := hafresh.ParseOwner(view.Lock.Owner) |
| + if view.Absent || !ok || owner.IID != iid || view.Lock.Generation <= 0 || view.Lock.Release != nil { |
| + t.Fatalf("expected %s held by %s: %+v", host, iid, view) |
| + } |
| +} |
| + |
| func g2Seed(t *testing.T, storage *g2Storage) (g2Config, []byte) { |
| cert, key, ca := g2Certificates(t) |
| secret, _, err := appconfig.GenerateKey() |
| g2Must(t, err) |
| - config := g2Config{Certificate: cert, CertificateKey: key, CA: ca, SecretsKey: secret, Records: []provision.Record{{Schema: 1, Host: g2Host, Slug: "pool", Account: g2Account, App: g2App, Pool: "ha", Region: "ie", Channel: "stable", HA: true, CreatedAt: time.Now().Add(-time.Hour).UTC()}}} |
| + config := g2Config{Certificate: cert, CertificateKey: key, CA: ca, SecretsKey: secret, Records: g2PoolRecords()} |
| _, err = storage.deploy.Put(t.Context(), provision.ManifestKey(g2Account, g2App), g2JSON(t, provision.Manifest{Schema: 1, App: g2App, Domain: "g2fix.test", Pool: "ha", Channel: "stable", Regions: []string{"ie"}}), "") |
| g2Must(t, err) |
| for _, rec := range config.Records { |
| @@ -268,7 +307,7 @@ func g2Seed(t *testing.T, storage *g2Storage) (g2Config, []byte) { |
| g2Must(t, err) |
| _, err = storage.deploy.Put(t.Context(), "control/edge-mesh-key", []byte(hex.EncodeToString(mesh)), "") |
| g2Must(t, err) |
| - for _, host := range []string{g2Host, g2Alias, g2Regional} { |
| + for _, host := range []string{g2Host, g2Alias, g2Regional, g2AbsentOldHost, g2AbsentRowHost} { |
| _, err = storage.deploy.Put(t.Context(), "control/certs/"+host, []byte(key+cert), "") |
| g2Must(t, err) |
| } |
| @@ -365,7 +404,7 @@ func TestRootG2PoolIngress(t *testing.T) { |
| if livePeer.TenantPIDs[g2Host] == "0" || !strings.Contains(livePeer.TenantCgroups[g2Host], "populated 1") { |
| t.Fatal("B has no live nested tenant cgroup") |
| } |
| - for _, kind := range []string{"clean", "unclean", "absent"} { |
| + for _, kind := range []string{"clean", "unclean"} { |
| released := g2WaitReleased(t, storage, b.IID) |
| if kind == "clean" && !released.Lock.Release.Clean { |
| t.Fatal("real clean release was not clean") |
| @@ -386,9 +425,6 @@ func TestRootG2PoolIngress(t *testing.T) { |
| _, err = storage.data.Put(t.Context(), "instances/"+g2Host+"/lock.json", raw, released.ETag) |
| g2Must(t, err) |
| } |
| - if kind == "absent" { |
| - g2Must(t, storage.data.Delete(t.Context(), "instances/"+g2Host+"/lock.json")) |
| - } |
| // Restart A through its supported startup path to clear holder-cache |
| // history. Its pool remains 'other' and no membership seam is used. |
| a.pools("other") |
| @@ -396,8 +432,71 @@ func TestRootG2PoolIngress(t *testing.T) { |
| g2NoLocal(t, a, storage, start) |
| t.Logf("CASE %s/%s PASS", projection, kind) |
| } |
| + // Each projection gets its own first-wake host. No previous case can |
| + // have put a lock or replica object under this prefix. |
| + fresh := g2AbsentOldHost |
| + if projection == "row-absent" { |
| + fresh = g2AbsentRowHost |
| + } |
| + a.pools("other") |
| + if projection == "row-absent" { |
| + g2Must(t, a.reg.Delete(fresh)) |
| + g2Serial(t, storage.deploy) |
| + } |
| + g2Must(t, g2AbsentPrefix(t.Context(), storage.data, fresh, false)) |
| + g2Stopped(t, a.state(), fresh) |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), fresh) |
| + freshStart := len(storage.snapshot()) |
| + g2Served(t, client, projection+"/absent", fresh, b.Ref, func() { |
| + g2NoLocalHost(t, a, storage, freshStart, fresh) |
| + }) |
| + g2NoLocalHost(t, a, storage, freshStart, fresh) |
| + g2HeldBy(t, storage, fresh, b.IID) |
| + liveFresh := g2Peer(t, g2RPC{Action: "state"}) |
| + if liveFresh.TenantPIDs[fresh] == "" || liveFresh.TenantPIDs[fresh] == "0" || !strings.Contains(liveFresh.TenantCgroups[fresh], "populated 1") { |
| + t.Fatal("fresh host has no live B tenant", liveFresh) |
| + } |
| + t.Logf("CASE %s/absent PASS", projection) |
| + |
| + // Keep the failed run's shape as a refusal witness. Only this host's |
| + // released lock is deleted; its replica objects remain untouched. |
| + g2WaitReleasedHost(t, storage, b.IID, fresh) |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), fresh) |
| + negativeStart := len(storage.snapshot()) |
| + journalCursor := g2Peer(t, g2RPC{Action: "journal-cursor"}).AgentJournal |
| + g2Must(t, storage.data.Delete(t.Context(), "instances/"+fresh+"/lock.json")) |
| + g2Must(t, g2AbsentPrefix(t.Context(), storage.data, fresh, true)) |
| + a.pools("other") |
| + if projection == "row-absent" { |
| + g2Must(t, a.reg.Delete(fresh)) |
| + } |
| + checkNegative := func() { |
| + g2NoLocalHost(t, a, storage, negativeStart, fresh) |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), fresh) |
| + g2Must(t, g2ZeroAcquire(storage.snapshot()[negativeStart:], os.Getenv("G2_B_IP"), fresh)) |
| + g2Must(t, g2AbsentPrefix(t.Context(), storage.data, fresh, true)) |
| + } |
| + g2Outcome(t, client, projection+"/absent-leftover", fresh, "GET", "", "", func(response g2Response) error { |
| + if response.Err != nil || response.Status != 503 { |
| + return fmt.Errorf("expected leftover-data refusal: %+v", response) |
| + } |
| + journal := g2Peer(t, g2RPC{Action: "journal", Value: journalCursor}).AgentJournal |
| + if !strings.Contains(journal, "activator: wake "+fresh+": instance lease refused: leftover data") { |
| + return errors.New("B has not yet refused leftover data") |
| + } |
| + t.Logf("B leftover-data evidence host=%s journal=%s", fresh, journal) |
| + return nil |
| + }, checkNegative) |
| + checkNegative() |
| + t.Logf("CASE %s/absent-leftover PASS", projection) |
| + |
| + // Aliases still resolve the ordinary host, whose release marker was |
| + // never deleted. Establish a live B writer before testing each name. |
| + served(projection+"/alias-precondition", g2Host, b.Ref) |
| + g2HeldBy(t, storage, g2Host, b.IID) |
| served(projection+"/alias", g2Alias, b.Ref) |
| t.Logf("CASE %s/alias PASS", projection) |
| + g2HeldBy(t, storage, g2Host, b.IID) |
| served(projection+"/regional", g2Regional, b.Ref) |
| t.Logf("CASE %s/regional PASS", projection) |
| g2NoLocal(t, a, storage, start) |
| @@ -406,6 +505,8 @@ func TestRootG2PoolIngress(t *testing.T) { |
| // Every POST has a unique receipt ID; replay increments a count rather |
| // than being hidden by an application's idempotency response. |
| start = len(storage.snapshot()) |
| + served("concurrent-posts/precondition", g2Host, b.Ref) |
| + g2HeldBy(t, storage, g2Host, b.IID) |
| const count = 16 |
| responses := make([]g2Response, count) |
| var wg sync.WaitGroup |
| @@ -438,6 +539,8 @@ func TestRootG2PoolIngress(t *testing.T) { |
| // Freeze only the publisher's S3 writes while retaining its real pool |
| // evidence. The peer still serves through its real agent. |
| g2WaitReleased(t, storage, b.IID) |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) |
| + start = len(storage.snapshot()) |
| g2PeerEvidence(t, storage, b.Ref, true, false) |
| a.pools("other") |
| served("eligible-low-headroom", g2Host, b.Ref) |
| @@ -448,11 +551,17 @@ func TestRootG2PoolIngress(t *testing.T) { |
| b = g2Peer(t, g2RPC{Action: "pools", Value: "other"}) |
| g2Serial(t, storage.deploy) |
| g2WaitPeer(t, storage.deploy, b.Ref, []string{"other"}) |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) |
| + if g2Lock(t, storage.data, g2Host).Lock.Release == nil { |
| + t.Fatal("no-eligible-peer needs a released ordinary host") |
| + } |
| start = len(storage.snapshot()) |
| a.pools("other") |
| g2Refused(t, client, "no-eligible-peer", g2Host, "") |
| g2NoLocal(t, a, storage, start) |
| t.Log("CASE no-eligible-peer PASS") |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) |
| + start = len(storage.snapshot()) |
| g2PeerEvidence(t, storage, b.Ref, false, true) |
| a.pools("other") |
| g2Refused(t, client, "host-advertised-wrong-pool", g2Host, "") |
| @@ -463,6 +572,10 @@ func TestRootG2PoolIngress(t *testing.T) { |
| // Previously awake A, first clean withdrawal, then a stop whose root |
| // control channel cannot complete, then a durable owed release. |
| for _, fault := range []string{"clean", "blocked-stop", "pending-release"} { |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) |
| + if g2Lock(t, storage.data, g2Host).Lock.Release == nil { |
| + t.Fatal("withdrawal needs a released ordinary host") |
| + } |
| start = len(storage.snapshot()) |
| a.pools("ha") |
| g2Serial(t, storage.deploy) |
| @@ -742,3 +855,51 @@ func TestG2PoolReadinessEvidenceAndDeadline(t *testing.T) { |
| } |
| }) |
| } |
| + |
| +func TestG2PoolAbsentFixture(t *testing.T) { |
| + records := g2PoolRecords() |
| + if len(records) != 3 || records[0].Host != g2Host { |
| + t.Fatalf("need the ordinary host and two fresh hosts: %+v", records) |
| + } |
| + seen := map[string]bool{} |
| + for _, rec := range records { |
| + if seen[rec.Host] || rec.Pool != "ha" || !rec.HA || rec.Region != "ie" { |
| + t.Fatalf("fresh host is reused or outside the HA pool: %+v", rec) |
| + } |
| + seen[rec.Host] = true |
| + } |
| + for _, host := range []string{g2AbsentOldHost, g2AbsentRowHost} { |
| + if !seen[host] { |
| + t.Fatalf("unseeded absent host %s", host) |
| + } |
| + } |
| + cert, _, _ := g2Certificates(t) |
| + block, _ := pem.Decode([]byte(cert)) |
| + if block == nil { |
| + t.Fatal("missing fixture certificate") |
| + } |
| + leaf, err := x509.ParseCertificate(block.Bytes) |
| + g2Must(t, err) |
| + for host := range seen { |
| + g2Must(t, leaf.VerifyHostname(host)) |
| + } |
| + store := release.NewDirStore(t.TempDir()) |
| + host := records[1].Host |
| + g2Must(t, g2AbsentPrefix(t.Context(), store, host, false)) |
| + if err := g2AbsentPrefix(t.Context(), store, host, true); err == nil { |
| + t.Fatal("empty prefix accepted as leftover-data fixture") |
| + } |
| + _, err = store.Put(t.Context(), "instances/"+host+"/db/generation/replica.ltx", []byte("replica"), "") |
| + g2Must(t, err) |
| + if err := g2AbsentPrefix(t.Context(), store, host, false); err == nil { |
| + t.Fatal("replica data accepted as never-woken fixture") |
| + } |
| + g2Must(t, g2AbsentPrefix(t.Context(), store, host, true)) |
| + _, err = store.Put(t.Context(), "instances/"+host+"/lock.json", []byte("lock"), "") |
| + g2Must(t, err) |
| + for _, leftover := range []bool{false, true} { |
| + if err := g2AbsentPrefix(t.Context(), store, host, leftover); err == nil { |
| + t.Fatal("existing lock accepted as absent fixture") |
| + } |
| + } |
| +} |