demo claims: readiness names every failing gate; HA gate asks born HA
Readiness evaluates every gate and logs one throttled line naming all that fail, with verdicts unchanged. The HA gate refuses only when this claim's fresh shared-pool instance would be born HA. Paul's ruling on #527 and #421. Squashed from demo-claim-ready-gates into main.
9 files changed,
+985
−79
internal/console/demo_claim_gate_causes_test.go+321 −0internal/console/demo_claim_gates_test.go+392 −0internal/console/demo_claim_ha_gate_test.go+100 −0internal/console/demo_claim_moved.go+21 −11internal/console/demo_claim_ready.go+85 −29internal/console/demo_claim_seed.go+44 −29internal/console/demo_claim_target.go+1 −1internal/console/demo_claim_transfer.go+20 −8internal/console/server.go+1 −1
CI passed — run details
diff --git a/internal/console/demo_claim_gate_causes_test.go b/internal/console/demo_claim_gate_causes_test.go| new file mode 100644 |
| index 000000000..44e85bdb0 |
| --- /dev/null |
| +++ b/internal/console/demo_claim_gate_causes_test.go |
| @@ -0,0 +1,321 @@ |
| +package console |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "log" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/seeds" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +type claimHungIndexStore struct{ release.Store } |
| + |
| +func (s claimHungIndexStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if key == release.ChangeIndexKey { |
| + <-ctx.Done() |
| + return nil, "", ctx.Err() |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +func TestDemoClaimReadinessDeadlineCause(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + s.cfg.Releases = claimHungIndexStore{s.cfg.Releases} |
| + ctx, cancel := context.WithTimeout(t.Context(), 20*time.Millisecond) |
| + defer cancel() |
| + gates, err := s.demoClaimReadinessGates(ctx) |
| + if !errors.Is(ctx.Err(), context.DeadlineExceeded) || err == nil || err.Error() != demoClaimUnavailable { |
| + t.Fatalf("changed deadline verdict: context=%v, verdict=%v", ctx.Err(), err) |
| + } |
| + if !slices.Contains(gates, "context") || !slices.Contains(gates, "change-index") { |
| + t.Fatalf("missing deadline cause: %s", gates.names()) |
| + } |
| +} |
| + |
| +func claimGateFloor(t *testing.T, s *Server, min int, minRelease string) { |
| + t.Helper() |
| + _, tag, err := s.cfg.Releases.Get(t.Context(), seeds.FloorKey) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + raw, err := json.Marshal(seeds.Floor{Min: min, MinRelease: minRelease}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err = s.cfg.Releases.Put(t.Context(), seeds.FloorKey, raw, tag); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessSeedRefusalCauses(t *testing.T) { |
| + for _, tc := range []struct { |
| + name, gate string |
| + change func(*testing.T, *Server) |
| + }{ |
| + {"flat", "box-layout:box-a", func(t *testing.T, s *Server) { |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Layout.Mode = "flat" }) |
| + }}, |
| + {"missing-layout", "box-layout:box-a", func(t *testing.T, s *Server) { |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Layout = nil }) |
| + }}, |
| + {"claim-control", "box-claim-control:box-a", func(t *testing.T, s *Server) { |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.HostClaimEnabled = true }) |
| + }}, |
| + {"moves-state", "edge-moves-floor:box-a", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["moves_floor_state"] = "accepted" }) |
| + }}, |
| + {"moves-generation", "edge-moves-floor:box-a", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["moves_floor_generation"] = 1 }) |
| + }}, |
| + {"moves-unarmed-seeds", "edge-moves-floor:box-a", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { |
| + f["moves_floor_state"], f["seeds_floor_state"] = "accepted", "accepted" |
| + }) |
| + }}, |
| + {"floor-protocol", "seed-floor-protocol", func(t *testing.T, s *Server) { |
| + claimGateFloor(t, s, 3, "v1.0.0") |
| + }}, |
| + {"floor-release-empty", "seed-floor-release", func(t *testing.T, s *Server) { |
| + claimGateFloor(t, s, 2, "") |
| + }}, |
| + {"floor-release-malformed", "seed-floor-release", func(t *testing.T, s *Server) { |
| + claimGateFloor(t, s, 2, "invalid") |
| + }}, |
| + {"no-region-edge", "seed-region-edges", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["placement"] = "other" }) |
| + }}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + tc.change(t, s) |
| + if s.seedRegionReadyForEdges(t.Context(), s.cfg.Region, false, demoClaimEdge) == nil { |
| + t.Fatal("fixture did not reach an existing seed refusal") |
| + } |
| + var output bytes.Buffer |
| + old := log.Writer() |
| + log.SetOutput(&output) |
| + defer log.SetOutput(old) |
| + if err := s.demoClaimReady(t.Context()); err == nil || err.Error() != demoClaimUnavailable { |
| + t.Fatalf("changed refusal: %v", err) |
| + } |
| + names := strings.Split(s.demoClaimAdmission.failures, ",") |
| + if !slices.Contains(names, tc.gate) || !slices.Contains(names, "seed-region") { |
| + t.Fatalf("missing cause or umbrella: %v", names) |
| + } |
| + if !strings.Contains(output.String(), "console: demo claim readiness: refusing: "+s.demoClaimAdmission.failures+"\n") { |
| + t.Fatalf("missing diagnostic: %s", &output) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessSeedCauseChanges(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Layout.Mode = "flat" }) |
| + var output bytes.Buffer |
| + old := log.Writer() |
| + log.SetOutput(&output) |
| + defer log.SetOutput(old) |
| + for _, want := range []string{"box-layout:box-a,seed-region", "box-claim-control:box-a,seed-region"} { |
| + for range 2 { |
| + s.demoClaimAdmission.expiresAt = time.Time{} |
| + if s.demoClaimReady(t.Context()) == nil { |
| + t.Fatal("admitted refusal") |
| + } |
| + } |
| + if !strings.HasSuffix(output.String(), "console: demo claim readiness: refusing: "+want+"\n") { |
| + t.Fatalf("missing changed cause: %s", &output) |
| + } |
| + claimGateBox(t, s, func(b *status.Box) { |
| + b.Metrics.Layout.Mode, b.Metrics.HostClaimEnabled = "nested", true |
| + }) |
| + } |
| + if strings.Count(output.String(), "console: demo claim readiness: refusing:") != 2 { |
| + t.Fatalf("want two diagnostic lines: %s", &output) |
| + } |
| +} |
| + |
| +func TestDemoClaimSeedCauseChecksPreserveAcceptance(t *testing.T) { |
| + for _, mode := range []string{"higher-protocol", "moves-with-accepted-seeds", "other-region", "named-pool"} { |
| + t.Run(mode, func(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + switch mode { |
| + case "higher-protocol": |
| + claimGateFloor(t, s, 3, "v1.0.0") |
| + claimGateEdge(t, s, func(f map[string]any) { f["seeds_protocol"] = 3 }) |
| + case "moves-with-accepted-seeds": |
| + claimGateEdge(t, s, func(f map[string]any) { |
| + f["moves_floor_state"], f["seeds_floor_state"], f["seeds_floor_generation"] = "refused", "accepted", 1 |
| + }) |
| + default: |
| + fx := &consoleFixture{srv: s, rel: s.cfg.Releases} |
| + seedReadyBox(t, fx, "box-b", "other", 2, "v1.0.0", "v1.0.0", "flat", true) |
| + // Keep all shared readers capable, including a flat box outside the destination region. |
| + boxes, err := status.LoadBoxes(t.Context(), s.cfg.Releases) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, b := range boxes { |
| + if b.Label == "box-b" { |
| + b.Metrics.Capabilities = append(b.Metrics.Capabilities, seeds.DemoClaimCapability) |
| + if err := status.WriteBox(t.Context(), s.cfg.Releases, b); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + } |
| + key := "control/edges/box-b.json" |
| + raw, tag, err := s.cfg.Releases.Get(t.Context(), key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var f map[string]any |
| + if err := json.Unmarshal(raw, &f); err != nil { |
| + t.Fatal(err) |
| + } |
| + f["capabilities"] = []string{seeds.DemoClaimCapability} |
| + f["moves_floor_state"] = "accepted" |
| + if mode == "named-pool" { |
| + f["placement"], f["serves_shared_pool"] = s.cfg.Region, false |
| + } |
| + raw, err = json.Marshal(f) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := s.cfg.Releases.Put(t.Context(), key, raw, tag); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if ref := s.seedRegionReadyForEdges(t.Context(), s.cfg.Region, false, demoClaimEdge); ref != nil { |
| + t.Fatalf("existing refusal: %+v", ref) |
| + } |
| + if gates := s.demoClaimSeedGates(t.Context(), s.cfg.Region); len(gates) != 0 { |
| + t.Fatalf("new refusal: %s", gates.names()) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDemoClaimSeedHostClaimNilStore(t *testing.T) { |
| + s := &Server{cfg: Config{HostClaim: true}} |
| + gates := s.demoClaimSeedGates(t.Context(), "") |
| + if !slices.Contains(gates, "host-claim") || gates.err() == nil { |
| + t.Fatalf("missing host-claim refusal: %s", gates.names()) |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessEarlyRefusals(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + if err := s.demoClaimReady(t.Context()); err != nil { |
| + t.Fatal(err) |
| + } |
| + counted := &demoCountingStore{Store: s.cfg.Releases} |
| + s.cfg.Releases = counted |
| + var output bytes.Buffer |
| + old := log.Writer() |
| + log.SetOutput(&output) |
| + defer log.SetOutput(old) |
| + defer func() { s.store.gate = nil }() |
| + for _, want := range []string{"demo-enabled", "host-claim", "claim-gated", "claim-gated,demo-enabled,host-claim"} { |
| + s.cfg.Demo.Enabled = !strings.Contains(want, "demo-enabled") |
| + s.cfg.HostClaim = strings.Contains(want, "host-claim") |
| + s.store.gate = nil |
| + if strings.Contains(want, "claim-gated") { |
| + s.store.gate = &claimGate{} |
| + } |
| + output.Reset() |
| + for i := range 3 { |
| + if i == 2 { |
| + s.demoClaimAdmission.expiresAt = time.Time{} |
| + } |
| + if err := s.demoClaimReady(t.Context()); err == nil || err.Error() != demoClaimUnavailable { |
| + t.Fatalf("early refusal: %v", err) |
| + } |
| + } |
| + if strings.Count(output.String(), "console: demo claim readiness: refusing:") != 1 || |
| + !strings.HasSuffix(output.String(), "refusing: "+want+"\n") { |
| + t.Fatalf("early diagnostic: %s", &output) |
| + } |
| + } |
| + if counted.requests() != 0 { |
| + t.Fatalf("early refusal read deployment: %d", counted.requests()) |
| + } |
| + s.cfg.Demo.Enabled, s.cfg.HostClaim, s.store.gate = true, false, nil |
| + if err := s.demoClaimReady(t.Context()); err != nil { |
| + t.Fatalf("stale prerequisite refusal: %v", err) |
| + } |
| + if !strings.HasSuffix(output.String(), "console: demo claim readiness: ready\n") { |
| + t.Fatalf("missing recovery: %s", &output) |
| + } |
| +} |
| + |
| +func TestDemoClaimTransferCapabilityCauseOnce(t *testing.T) { |
| + for _, fault := range []string{"worker-peer", "box-layout:box-a,seed-region", "moved-edge:box-a"} { |
| + t.Run(fault, func(t *testing.T) { |
| + s, op, _ := claimTransferFixture(t) |
| + switch fault { |
| + case "worker-peer": |
| + s.demoJobs.peers["box"].ready.ClaimCapability = "" |
| + case "box-layout:box-a,seed-region": |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Layout.Mode = "flat" }) |
| + case "moved-edge:box-a": |
| + claimGateEdge(t, s, func(f map[string]any) { f["capabilities"] = []string{seeds.DemoClaimCapability} }) |
| + } |
| + var output bytes.Buffer |
| + old := log.Writer() |
| + log.SetOutput(&output) |
| + defer log.SetOutput(old) |
| + claimAdvance(t, s, op) |
| + claimAdvance(t, s, op) |
| + p, err := s.claimProgress(t.Context(), op.ID) |
| + if err != nil || p.Reason != "capability" { |
| + t.Fatalf("abort reason: %+v %v", p, err) |
| + } |
| + if strings.Count(output.String(), "console: demo claim capability:") != 1 || |
| + !strings.Contains(output.String(), "console: demo claim capability: refusing: "+fault+"\n") { |
| + t.Fatalf("abort diagnostic: %s", &output) |
| + } |
| + if strings.Contains(output.String(), op.ID) || strings.Contains(output.String(), op.Target.Host) { |
| + t.Fatalf("operation identity leaked: %s", &output) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDemoClaimTransferCapabilityCleanupRetry(t *testing.T) { |
| + s, op, _ := claimTransferFixture(t) |
| + s.demoJobs.peers["box"].ready.ClaimCapability = "" |
| + store := s.cfg.Releases |
| + s.cfg.Releases = &claimGateStore{Store: store, failGet: provision.RecordKey(op.Target.Sqid, op.Target.App, op.Target.Host)} |
| + var output bytes.Buffer |
| + old := log.Writer() |
| + log.SetOutput(&output) |
| + defer log.SetOutput(old) |
| + for range 2 { |
| + if err := s.advanceDemoClaim(t.Context(), op.ID); !errors.Is(err, errClaimGateStore) { |
| + t.Fatalf("expected cleanup failure: %v", err) |
| + } |
| + fresh, err := s.store.demoClaimOp(t.Context(), op.ID) |
| + if err != nil || fresh.Phase != "aborting" { |
| + t.Fatalf("abort transition not persisted: %+v %v", fresh, err) |
| + } |
| + } |
| + s.cfg.Releases = store |
| + claimAdvance(t, s, op) |
| + if strings.Count(output.String(), "console: demo claim capability: refusing: worker-peer\n") != 1 { |
| + t.Fatalf("cleanup retry repeated diagnostic: %s", &output) |
| + } |
| +} |
diff --git a/internal/console/demo_claim_gates_test.go b/internal/console/demo_claim_gates_test.go| new file mode 100644 |
| index 000000000..677504591 |
| --- /dev/null |
| +++ b/internal/console/demo_claim_gates_test.go |
| @@ -0,0 +1,392 @@ |
| +package console |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "log" |
| + "slices" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/democlaim" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/seeds" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +func claimGateEdge(t *testing.T, s *Server, edit func(map[string]any)) { |
| + t.Helper() |
| + key := "control/edges/box-a.json" |
| + raw, tag, err := s.cfg.Releases.Get(t.Context(), key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var f map[string]any |
| + if err := json.Unmarshal(raw, &f); err != nil { |
| + t.Fatal(err) |
| + } |
| + edit(f) |
| + raw, err = json.Marshal(f) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err = s.cfg.Releases.Put(t.Context(), key, raw, tag); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func claimGateBox(t *testing.T, s *Server, edit func(*status.Box)) { |
| + t.Helper() |
| + boxes, err := status.LoadBoxes(t.Context(), s.cfg.Releases) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, b := range boxes { |
| + if b.Label != "box-a" { |
| + continue |
| + } |
| + edit(&b) |
| + if err := status.WriteBox(t.Context(), s.cfg.Releases, b); err != nil { |
| + t.Fatal(err) |
| + } |
| + return |
| + } |
| + t.Fatal("missing fixture box") |
| +} |
| + |
| +var errClaimGateStore = errors.New("private store error must not appear in readiness diagnostics") |
| + |
| +type claimGateStore struct { |
| + release.Store |
| + failGet, failList string |
| + failListAt int32 |
| + lists atomic.Int32 |
| +} |
| + |
| +func (s *claimGateStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if key == s.failGet { |
| + return nil, "", errClaimGateStore |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| +func (s *claimGateStore) List(ctx context.Context, prefix string) ([]string, error) { |
| + if prefix == s.failList { |
| + n := s.lists.Add(1) |
| + if s.failListAt == 0 || n == s.failListAt { |
| + return nil, errClaimGateStore |
| + } |
| + } |
| + return s.Store.List(ctx, prefix) |
| +} |
| + |
| +func TestDemoClaimReadinessGateNames(t *testing.T) { |
| + cases := []struct { |
| + name, gate string |
| + change func(*testing.T, *Server) |
| + }{ |
| + {"keymail", "keymail", func(t *testing.T, s *Server) { s.km = nil }}, |
| + {"keymail-host", "keymail", func(t *testing.T, s *Server) { s.km = NewKeymail("", s.cfg.Origin) }}, |
| + {"apps-domain", "apps-domain", func(t *testing.T, s *Server) { s.cfg.AppsDomain = "" }}, |
| + {"signing-key", "signing-key", func(t *testing.T, s *Server) { s.cfg.Key = nil }}, |
| + {"release-pub", "release-pub", func(t *testing.T, s *Server) { s.cfg.ReleasePub = nil }}, |
| + {"releases", "releases", func(t *testing.T, s *Server) { s.cfg.Releases = nil }}, |
| + {"seed-data", "seed-data", func(t *testing.T, s *Server) { s.cfg.SeedData = nil }}, |
| + {"leftover-check", "leftover-check", func(t *testing.T, s *Server) { s.cfg.HibernateBucket = "" }}, |
| + {"seed-stream", "seed-stream", func(t *testing.T, s *Server) { s.cfg.SeedData = &demoCountingStore{Store: s.cfg.SeedData} }}, |
| + {"record-watch", "record-watch", func(t *testing.T, s *Server) { s.recordView = nil }}, |
| + {"change-index", "change-index", func(t *testing.T, s *Server) { |
| + s.cfg.Releases = &claimGateStore{Store: s.cfg.Releases, failGet: release.ChangeIndexKey} |
| + }}, |
| + {"worker-peer", "worker-peer", func(t *testing.T, s *Server) { s.demoJobs.peers["box"].ready.ClaimCapability = "" }}, |
| + {"publish", "publish", func(t *testing.T, s *Server) { s.cfg.Bucket = "private-bucket"; s.cfg.PolicyLease = nil }}, |
| + {"ha-region", "ha-region", func(t *testing.T, s *Server) { s.cfg.HomeRegion = "home"; s.cfg.HARegions = []string{"home"} }}, |
| + {"seed-region", "seed-region", func(t *testing.T, s *Server) { |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Layout.Mode = "flat" }) |
| + }}, |
| + {"seed-floor", "seed-floor", func(t *testing.T, s *Server) { |
| + if err := s.cfg.Releases.Delete(t.Context(), seeds.FloorKey); err != nil { |
| + t.Fatal(err) |
| + } |
| + }}, |
| + {"seed-floor-old", "seed-floor", func(t *testing.T, s *Server) { |
| + _, tag, _ := s.cfg.Releases.Get(t.Context(), seeds.FloorKey) |
| + if _, err := s.cfg.Releases.Put(t.Context(), seeds.FloorKey, []byte(`{"min":1,"min_release":"v1.0.0"}`), tag); err != nil { |
| + t.Fatal(err) |
| + } |
| + }}, |
| + {"edges-evidence", "edges-evidence", func(t *testing.T, s *Server) { |
| + s.cfg.Releases = &claimGateStore{Store: s.cfg.Releases, failList: "control/edges/"} |
| + }}, |
| + {"edge-capability", "edge-capability:box-a", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["capabilities"] = []string{democlaim.MovedCapability} }) |
| + }}, |
| + {"edge-protocol", "edge-protocol:box-a", func(t *testing.T, s *Server) { claimGateEdge(t, s, func(f map[string]any) { f["seeds_protocol"] = 1 }) }}, |
| + {"edge-floor", "edge-floor:box-a", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["seeds_floor_min_release"] = "v0.9.0" }) |
| + }}, |
| + {"edge-prev", "edge-prev:box-a", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["prev_seeds_allowed_for"] = "" }) |
| + }}, |
| + {"edge-floor-state", "edge-floor-state:box-a", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["seeds_floor_state"] = "refused"; f["seeds_floor_generation"] = 1 }) |
| + }}, |
| + {"no-shared-edges", "no-shared-edges", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["serves_shared_pool"] = false }) |
| + }}, |
| + {"box-status", "box-status:box-a", func(t *testing.T, s *Server) { |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Capabilities = []string{democlaim.MovedCapability} }) |
| + }}, |
| + {"box-status-read", "box-status", func(t *testing.T, s *Server) { |
| + s.cfg.Releases = &claimGateStore{Store: s.cfg.Releases, failList: "status/boxes/"} |
| + }}, |
| + {"box-platform", "box-platform:box-a", func(t *testing.T, s *Server) { |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Platform = "linux-amd64" }) |
| + }}, |
| + {"moved-box", "moved-box:box-a", func(t *testing.T, s *Server) { |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Capabilities = []string{seeds.DemoClaimCapability} }) |
| + }}, |
| + {"moved-edge", "moved-edge:box-a", func(t *testing.T, s *Server) { |
| + claimGateEdge(t, s, func(f map[string]any) { f["capabilities"] = []string{seeds.DemoClaimCapability} }) |
| + }}, |
| + } |
| + for _, tc := range cases { |
| + t.Run(tc.name, func(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + tc.change(t, s) |
| + gates, err := s.demoClaimReadinessGates(t.Context()) |
| + if err == nil || err.Error() != demoClaimUnavailable { |
| + t.Fatalf("caller error = %v", err) |
| + } |
| + if !slices.Contains(gates, tc.gate) { |
| + t.Fatalf("missing %q in %q", tc.gate, gates.names()) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessAllFailuresAndTransitions(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + km, key := s.km, s.cfg.Key |
| + s.km, s.cfg.Key = nil, nil |
| + claimSeedReadyFixture(t, s, 1, false, "") |
| + claimGateBox(t, s, func(b *status.Box) { b.Metrics.Platform = "linux-amd64" }) |
| + var output bytes.Buffer |
| + old := log.Writer() |
| + log.SetOutput(&output) |
| + defer log.SetOutput(old) |
| + check := func(ready bool) { |
| + t.Helper() |
| + err := s.demoClaimReady(t.Context()) |
| + if ready && err != nil || !ready && (err == nil || err.Error() != demoClaimUnavailable) { |
| + t.Fatalf("readiness = %v", err) |
| + } |
| + } |
| + check(false) |
| + want := "console: demo claim readiness: refusing: box-platform:box-a,box-status:box-a,edge-capability:box-a,edge-prev:box-a,edge-protocol:box-a,keymail,moved-box:box-a,moved-edge:box-a,seed-floor-protocol,seed-region,signing-key" |
| + if !strings.Contains(output.String(), want+"\n") { |
| + t.Fatalf("missing complete sorted failures: %s", &output) |
| + } |
| + first := output.String() |
| + check(false) |
| + s.demoClaimAdmission.expiresAt = time.Time{} |
| + check(false) |
| + if output.String() != first { |
| + t.Fatalf("repeat logged again: %s", &output) |
| + } |
| + s.km = km |
| + s.demoClaimAdmission.expiresAt = time.Time{} |
| + check(false) |
| + if strings.Count(output.String(), "console: demo claim readiness: refusing:") != 2 { |
| + t.Fatalf("changed set did not log: %s", &output) |
| + } |
| + s.cfg.Key = key |
| + claimSeedReadyFixture(t, s, 2, true, "v1.0.0") |
| + s.demoClaimAdmission.expiresAt = time.Time{} |
| + check(true) |
| + s.demoClaimAdmission.expiresAt = time.Time{} |
| + check(true) |
| + if strings.Count(output.String(), "console: demo claim readiness: ready") != 1 { |
| + t.Fatalf("recovery: %s", &output) |
| + } |
| + s.km = nil |
| + s.demoClaimAdmission.expiresAt = time.Time{} |
| + check(false) |
| + if !strings.HasSuffix(output.String(), "console: demo claim readiness: refusing: keymail\n") { |
| + t.Fatalf("regression: %s", &output) |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessOffNoReadsOrLogs(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + s.cfg.Demo.ClaimEnabled = false |
| + store := &demoCountingStore{Store: s.cfg.Releases} |
| + s.cfg.Releases = store |
| + var output bytes.Buffer |
| + old := log.Writer() |
| + log.SetOutput(&output) |
| + defer log.SetOutput(old) |
| + for range 3 { |
| + if err := s.demoClaimReady(t.Context()); err == nil || err.Error() != demoClaimUnavailable { |
| + t.Fatalf("off error: %v", err) |
| + } |
| + } |
| + if store.gets.Load() != 0 || store.lists.Load() != 0 || output.Len() != 0 { |
| + t.Fatalf("off read/log: %d/%d %s", store.gets.Load(), store.lists.Load(), &output) |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessNilStoresKeepIndependentGates(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + s.cfg.Releases, s.cfg.SeedData, s.km = nil, nil, nil |
| + s.demoJobs.peers["box"].ready.ClaimCapability = "" |
| + gates, err := s.demoClaimReadinessGates(t.Context()) |
| + if err == nil || gates.names() != "keymail,releases,seed-data,worker-peer" { |
| + t.Fatalf("nil stores: %q %v", gates.names(), err) |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessCapabilitiesCompatibility(t *testing.T) { |
| + for _, fault := range []string{"", "source", "seed", "moved", "moved-store"} { |
| + t.Run(fault, func(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + op := claimOp{SourceBox: s.demoJobs.peers["box"].box(), Target: claimTarget{Region: s.cfg.Region}} |
| + ctx := withReqMemo(t.Context()) |
| + switch fault { |
| + case "source": |
| + op.SourceBox = "missing" |
| + case "seed": |
| + claimGateEdge(t, s, func(f map[string]any) { f["seeds_protocol"] = 1 }) |
| + case "moved": |
| + claimGateEdge(t, s, func(f map[string]any) { f["capabilities"] = []string{seeds.DemoClaimCapability} }) |
| + case "moved-store": |
| + s.cfg.Releases = &claimGateStore{Store: s.cfg.Releases, failList: "status/boxes/", failListAt: 4} |
| + } |
| + err := s.demoClaimCapabilitiesReady(ctx, op) |
| + switch fault { |
| + case "": |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + case "moved-store": |
| + if !errors.Is(err, errClaimGateStore) { |
| + t.Fatalf("lost moved storage error: %v", err) |
| + } |
| + default: |
| + if err == nil || err.Error() != demoClaimUnavailable { |
| + t.Fatalf("changed refusal: %v", err) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessRequestBudget(t *testing.T) { |
| + for _, memo := range []bool{false, true} { |
| + for _, failing := range []bool{false, true} { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + if failing { |
| + s.km = nil |
| + s.cfg.Key = nil |
| + claimGateEdge(t, s, func(f map[string]any) { f["capabilities"] = []string{seeds.DemoClaimCapability} }) |
| + } |
| + counted := &demoCountingStore{Store: s.cfg.Releases} |
| + s.cfg.Releases = counted |
| + ctx := t.Context() |
| + if memo { |
| + ctx = withReqMemo(ctx) |
| + } |
| + err := s.demoClaimReady(ctx) |
| + if (err != nil) != failing { |
| + t.Fatalf("readiness: %v", err) |
| + } |
| + // Measured against the original complete pass, with one edge and one box. |
| + var gets, lists int32 = 12, 9 |
| + if memo { |
| + gets, lists = 8, 5 |
| + } |
| + if counted.gets.Load() != gets || counted.lists.Load() != lists { |
| + t.Fatalf("memo=%v failing=%v: GET/LIST=%d/%d, want %d/%d", memo, failing, counted.gets.Load(), counted.lists.Load(), gets, lists) |
| + } |
| + for range 3 { |
| + _ = s.demoClaimReady(t.Context()) |
| + } |
| + if counted.gets.Load() != gets || counted.lists.Load() != lists || counted.puts.Load() != 0 { |
| + t.Fatal("cache read or readiness write") |
| + } |
| + t.Logf("memo=%v failing=%v: %d GET, %d LIST; cache hits: zero", memo, failing, gets, lists) |
| + } |
| + } |
| +} |
| + |
| +func TestDemoClaimReadinessMultipleBoxes(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + claimGateEdge(t, s, func(f map[string]any) { f["prev_seeds_allowed_for"] = "" }) |
| + raw, _, err := s.cfg.Releases.Get(t.Context(), "control/edges/box-a.json") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var f map[string]any |
| + if err = json.Unmarshal(raw, &f); err != nil { |
| + t.Fatal(err) |
| + } |
| + f["box"] = "dennis-stenciling" |
| + f["eip"] = "192.0.2.11" |
| + raw, err = json.Marshal(f) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err = s.cfg.Releases.Put(t.Context(), "control/edges/dennis-stenciling.json", raw, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + boxes, err := status.LoadBoxes(t.Context(), s.cfg.Releases) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + b := boxes[0] |
| + b.Label = "dennis-stenciling" |
| + if err = status.WriteBox(t.Context(), s.cfg.Releases, b); err != nil { |
| + t.Fatal(err) |
| + } |
| + gates, err := s.demoClaimReadinessGates(t.Context()) |
| + want := "edge-prev:box-a,edge-prev:dennis-stenciling,seed-region" |
| + if err == nil || gates.names() != want || gates.names() != want { |
| + t.Fatalf("multiple boxes: %q %v", gates.names(), err) |
| + } |
| +} |
| + |
| +func TestDemoClaimSeedGatePrerequisites(t *testing.T) { |
| + for _, which := range []string{"host-claim", "claim-gated", "seed-region"} { |
| + t.Run(which, func(t *testing.T) { |
| + s, _, _ := claimTestServer(t) |
| + claimActivationFixture(t, s) |
| + region := s.cfg.Region |
| + switch which { |
| + case "host-claim": |
| + s.cfg.HostClaim = true |
| + case "claim-gated": |
| + s.store.gate = &claimGate{} |
| + defer func() { s.store.gate = nil }() |
| + case "seed-region": |
| + region = "other" |
| + } |
| + gates := s.demoClaimSeedGates(t.Context(), region) |
| + if !slices.Contains(gates, which) { |
| + t.Fatalf("missing %q: %q", which, gates.names()) |
| + } |
| + if err := s.demoClaimSeedReady(t.Context(), region); err == nil || err.Error() != demoClaimUnavailable { |
| + t.Fatalf("seed refusal: %v", err) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/console/demo_claim_ha_gate_test.go b/internal/console/demo_claim_ha_gate_test.go| new file mode 100644 |
| index 000000000..ff56b1bd0 |
| --- /dev/null |
| +++ b/internal/console/demo_claim_ha_gate_test.go |
| @@ -0,0 +1,100 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "testing" |
| + |
| + lsfile "github.com/benbjohnson/litestream/file" |
| +) |
| + |
| +func TestDemoClaimReservationBornHA(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + ha bool |
| + pools []string |
| + invalid, refuse bool |
| + }{ |
| + {name: "non-HA"}, |
| + {name: "region-wide", ha: true, refuse: true}, |
| + {name: "named-only", ha: true, pools: []string{"ha"}}, |
| + // The parser rejects this configuration; the predicate also refuses it |
| + // if an in-memory scope ever contains the shared pool. |
| + {name: "shared-pool", ha: true, pools: []string{""}, refuse: true}, |
| + {name: "invalid-scope", ha: true, pools: []string{"ha"}, invalid: true, refuse: true}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + s, _, token := claimTestServer(t) |
| + f, _ := claimProved(t, s, token, "shop@example.com") |
| + a := claimAccount(t, s, 501, f.Person, RoleOwner) |
| + claimActivationFixture(t, s) |
| + s.cfg.HomeRegion = "ie" |
| + if tc.ha { |
| + s.cfg.HARegions = []string{"ie"} |
| + } |
| + s.haPools = map[string][]string{"ie": tc.pools} |
| + s.haScopeInvalid = tc.invalid |
| + gates, err := s.demoClaimReadinessGates(t.Context()) |
| + if slices.Contains(gates, "ha-region") != tc.refuse || (err != nil) != tc.refuse { |
| + t.Fatalf("readiness HA gate: %q, %v; refuse=%v", gates.names(), err, tc.refuse) |
| + } |
| + if err := s.demoClaimSeedReady(t.Context(), s.cfg.Region); (err != nil) != tc.refuse { |
| + t.Fatalf("seed HA gate: %v; refuse=%v", err, tc.refuse) |
| + } else if err != nil && err.Error() != demoClaimUnavailable { |
| + t.Fatalf("seed refusal changed: %v", err) |
| + } |
| + // Reach the placement gate even when readiness would refuse admission. |
| + s.demoClaimTestEnabled = true |
| + op, err := s.reserveDemoClaim(t.Context(), f, a, regionReq{}) |
| + if tc.refuse { |
| + if !errors.Is(err, errDemoClaimPlacement) { |
| + t.Fatalf("reservation refusal: %v", err) |
| + } |
| + var count int |
| + if err = s.store.db.QueryRow(`SELECT count(*) FROM demo_claim_ops`).Scan(&count); err != nil || count != 0 { |
| + t.Fatalf("refused placement reserved an operation: %d %v", count, err) |
| + } |
| + apps, err := s.store.AppsIn(a.ID) |
| + if err != nil || len(apps) != 0 { |
| + t.Fatalf("refused placement created an app: %v %v", apps, err) |
| + } |
| + return |
| + } |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + root := t.TempDir() |
| + s.dbReplica = func(_ context.Context, prefix string) (replicaReader, error) { |
| + return lsfile.NewReplicaClient(filepath.Join(root, prefix)), nil |
| + } |
| + rec, err := s.createDemoClaimSeed(t.Context(), op) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if rec.HA || rec.Pool != "" { |
| + t.Fatalf("destination born HA or pooled: %+v", rec) |
| + } |
| + stored, _, err := s.readRecord(t.Context(), op.Target.Sqid, op.Target.App, op.Target.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if stored.HA || stored.Pool != "" { |
| + t.Fatalf("stored destination born HA or pooled: %+v", stored) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDemoClaimSeedHAPoolScopeParsing(t *testing.T) { |
| + raw := strings.Replace(haTestConfig, `"aws_region":"eu-west-1"`, `"aws_region":"eu-west-1","ha_pools":[""]`, 1) |
| + if _, err := parseHACapacityConfig(raw, []string{"ie"}, "ie"); err == nil { |
| + t.Fatal("shared pool unexpectedly representable in HA config") |
| + } |
| + raw = strings.Replace(haTestConfig, `"aws_region":"eu-west-1"`, `"aws_region":"eu-west-1","ha_pools":["ha"]`, 1) |
| + if _, err := parseHACapacityConfig(raw, []string{"ie"}, "ie"); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
diff --git a/internal/console/demo_claim_moved.go b/internal/console/demo_claim_moved.go| index a85a6296e..e803e94f1 100644 |
| --- a/internal/console/demo_claim_moved.go |
| +++ b/internal/console/demo_claim_moved.go |
| @@ -26,13 +26,23 @@ type demoMovedCache struct { |
| } |
| func (s *Server) demoClaimMovedReady(ctx context.Context) error { |
| - snap, err := s.liveEdges(ctx) |
| - if err != nil || !snap.EvidenceComplete() || len(snap.Files) == 0 { |
| - return errors.New(demoClaimUnavailable) |
| + _, err := s.demoClaimMovedGates(ctx) |
| + return err |
| +} |
| + |
| +func (s *Server) demoClaimMovedGates(ctx context.Context) (demoClaimGates, error) { |
| + var gates demoClaimGates |
| + gates.check("releases", s.cfg.Releases != nil) |
| + if s.cfg.Releases == nil { |
| + return gates, gates.err() |
| } |
| + snap, err := s.liveEdges(ctx) |
| + gates.check("edges-evidence", err == nil && snap.EvidenceComplete() && len(snap.Files) != 0) |
| + firstErr := gates.err() |
| boxes, err := status.LoadBoxes(ctx, s.cfg.Releases) |
| - if err != nil { |
| - return err |
| + gates.check("box-status", err == nil) |
| + if firstErr == nil { |
| + firstErr = err |
| } |
| capable := map[string]bool{} |
| seen := map[string]bool{} |
| @@ -50,14 +60,14 @@ func (s *Server) demoClaimMovedReady(ctx context.Context) error { |
| continue |
| } |
| counted++ |
| - if !capable[f.Box] || !slices.Contains(f.Capabilities, democlaim.MovedCapability) { |
| - return errors.New(demoClaimUnavailable) |
| - } |
| + gates.check("moved-box:"+f.Box, capable[f.Box]) |
| + gates.check("moved-edge:"+f.Box, slices.Contains(f.Capabilities, democlaim.MovedCapability)) |
| } |
| - if counted == 0 { |
| - return errors.New(demoClaimUnavailable) |
| + gates.check("no-shared-edges", counted != 0) |
| + if firstErr != nil { |
| + return gates, firstErr |
| } |
| - return nil |
| + return gates, gates.err() |
| } |
| // Pending operations reserve their eventual wire size in the same writer |
diff --git a/internal/console/demo_claim_ready.go b/internal/console/demo_claim_ready.go| index da64bf3c6..d6e10cd40 100644 |
| --- a/internal/console/demo_claim_ready.go |
| +++ b/internal/console/demo_claim_ready.go |
| @@ -4,12 +4,42 @@ import ( |
| "context" |
| "crypto/ed25519" |
| "errors" |
| + "log" |
| + "slices" |
| + "strings" |
| "time" |
| "github.com/carlosframework/platform/internal/democlaim" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| +type demoClaimAdmissionCache struct { |
| + demoAdmissionCache |
| + failures string |
| + prerequisites string |
| +} |
| + |
| +type demoClaimGates []string |
| + |
| +func (g *demoClaimGates) check(name string, ready bool) { |
| + if !ready { |
| + *g = append(*g, name) |
| + } |
| +} |
| + |
| +func (g demoClaimGates) names() string { |
| + g = slices.Clone(g) |
| + slices.Sort(g) |
| + return strings.Join(slices.Compact(g), ",") |
| +} |
| + |
| +func (g demoClaimGates) err() error { |
| + if len(g) != 0 { |
| + return errors.New(demoClaimUnavailable) |
| + } |
| + return nil |
| +} |
| + |
| // Older writers do not declare pool membership, so they still count. |
| func demoClaimEdge(f edgesFilePlacementView) bool { |
| return f.ServesSharedPool == nil || *f.ServesSharedPool |
| @@ -19,42 +49,68 @@ func demoClaimEdge(f edgesFilePlacementView) bool { |
| // Switching off bypasses the cache and performs no deployment reads. |
| func (s *Server) demoClaimReady(ctx context.Context) error { |
| refuse := func() error { return errors.New(demoClaimUnavailable) } |
| - if !s.cfg.Demo.ClaimEnabled || !s.cfg.Demo.Enabled || s.cfg.HostClaim || s.store.claimGated() { |
| + if !s.cfg.Demo.ClaimEnabled { |
| return refuse() |
| } |
| c := &s.demoClaimAdmission |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| - if c.checked && time.Now().Before(c.expiresAt) { |
| + var gates demoClaimGates |
| + gates.check("demo-enabled", s.cfg.Demo.Enabled) |
| + gates.check("host-claim", !s.cfg.HostClaim) |
| + gates.check("claim-gated", s.store == nil || !s.store.claimGated()) |
| + prerequisites := gates.names() |
| + if c.checked && prerequisites == c.prerequisites && time.Now().Before(c.expiresAt) { |
| return c.err |
| } |
| - ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) |
| - defer cancel() |
| - c.err = s.checkDemoClaimReady(ctx) |
| + err := gates.err() |
| + if err == nil { |
| + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) |
| + defer cancel() |
| + gates, err = s.demoClaimReadinessGates(ctx) |
| + } |
| + failures := gates.names() |
| + if failures != c.failures { |
| + if failures == "" { |
| + log.Printf("console: demo claim readiness: ready") |
| + } else { |
| + log.Printf("console: demo claim readiness: refusing: %s", failures) |
| + } |
| + } |
| + c.failures = failures |
| + c.prerequisites = prerequisites |
| + c.err = err |
| c.checked = true |
| c.expiresAt = time.Now().Add(demoAdmissionTTL) |
| return c.err |
| } |
| func (s *Server) checkDemoClaimReady(ctx context.Context) error { |
| - refuse := func() error { return errors.New(demoClaimUnavailable) } |
| + _, err := s.demoClaimReadinessGates(ctx) |
| + return err |
| +} |
| + |
| +func (s *Server) demoClaimReadinessGates(ctx context.Context) (demoClaimGates, error) { |
| + var gates demoClaimGates |
| // Readiness means the browser Keymail path can complete. Email-code starts |
| // are refused per request when no mailer is ready. |
| - if s.km == nil || s.km.Host() == "" || s.cfg.AppsDomain == "" || |
| - len(s.cfg.Key) != ed25519.PrivateKeySize || len(s.cfg.ReleasePub) != ed25519.PublicKeySize || |
| - s.cfg.Releases == nil || s.cfg.SeedData == nil || !s.leftoverCheckApplies(s.cfg.Region, "") { |
| - return refuse() |
| - } |
| - if _, ok := s.cfg.SeedData.(release.PutStreamer); !ok { |
| - return refuse() |
| + gates.check("keymail", s.km != nil && s.km.Host() != "") |
| + gates.check("apps-domain", s.cfg.AppsDomain != "") |
| + gates.check("signing-key", len(s.cfg.Key) == ed25519.PrivateKeySize) |
| + gates.check("release-pub", len(s.cfg.ReleasePub) == ed25519.PublicKeySize) |
| + gates.check("releases", s.cfg.Releases != nil) |
| + gates.check("seed-data", s.cfg.SeedData != nil) |
| + gates.check("leftover-check", s.leftoverCheckApplies(s.cfg.Region, "")) |
| + if s.cfg.SeedData != nil { |
| + _, ok := s.cfg.SeedData.(release.PutStreamer) |
| + gates.check("seed-stream", ok) |
| } |
| // Delivery uses the existing shared watch. Reading its index checks access |
| // without mutating control state or adding a watch per claim or per box. |
| - if s.recordView == nil || s.recordView.Watch() == nil { |
| - return refuse() |
| - } |
| - if _, tag, err := release.ReadChangeIndex(ctx, s.cfg.Releases); err != nil || tag == "" { |
| - return refuse() |
| + gates.check("record-watch", s.recordView != nil && s.recordView.Watch() != nil) |
| + if s.cfg.Releases != nil { |
| + _, tag, err := release.ReadChangeIndex(ctx, s.cfg.Releases) |
| + gates.check("change-index", err == nil && tag != "") |
| } |
| found := false |
| for _, p := range s.demoEligiblePeers() { |
| @@ -63,17 +119,17 @@ func (s *Server) checkDemoClaimReady(ctx context.Context) error { |
| break |
| } |
| } |
| - if !found { |
| - return refuse() |
| - } |
| - if err := s.demoPublishReady(ctx, ""); err != nil { |
| - return refuse() |
| + gates.check("worker-peer", found) |
| + gates.check("publish", s.demoPublishReady(ctx, "") == nil) |
| + gates = append(gates, s.demoClaimSeedGates(ctx, s.cfg.Region)...) |
| + if s.cfg.Releases != nil { |
| + moved, _ := s.demoClaimMovedGates(ctx) |
| + gates = append(gates, moved...) |
| } |
| - if err := s.demoClaimSeedReady(ctx, s.cfg.Region); err != nil { |
| - return refuse() |
| - } |
| - if err := s.demoClaimMovedReady(ctx); err != nil { |
| - return refuse() |
| + if err := gates.err(); err != nil { |
| + gates.check("context", ctx.Err() == nil) |
| + return gates, err |
| } |
| - return ctx.Err() |
| + gates.check("context", ctx.Err() == nil) |
| + return gates, ctx.Err() |
| } |
diff --git a/internal/console/demo_claim_seed.go b/internal/console/demo_claim_seed.go| index a84c09e0f..a4261e4db 100644 |
| --- a/internal/console/demo_claim_seed.go |
| +++ b/internal/console/demo_claim_seed.go |
| @@ -34,40 +34,53 @@ func (s *Server) demoClaimVerifyToken(id string) (string, error) { |
| // Admission requires every ingress reader as well as every destination. |
| // Protocol 2's existing seeds floor excludes an older rollback binary. |
| func (s *Server) demoClaimSeedReady(ctx context.Context, region string) error { |
| - refuse := func() error { return errors.New(demoClaimUnavailable) } |
| - if s.cfg.HostClaim || s.store.claimGated() || s.cfg.Releases == nil || region != s.cfg.Region || s.haRegion(region) { |
| - return refuse() |
| - } |
| - if ref := s.seedRegionReadyForEdges(ctx, region, false, demoClaimEdge); ref != nil { |
| - return refuse() |
| - } |
| + return s.demoClaimSeedGates(ctx, region).err() |
| +} |
| + |
| +func (s *Server) demoClaimSeedGates(ctx context.Context, region string) demoClaimGates { |
| + var gates demoClaimGates |
| + gates.check("host-claim", !s.cfg.HostClaim) |
| + gates.check("claim-gated", s.store == nil || !s.store.claimGated()) |
| + gates.check("releases", s.cfg.Releases != nil) |
| + gates.check("seed-region", region == s.cfg.Region) |
| + gates.check("ha-region", !s.haCreate(region, "")) |
| + if s.cfg.Releases == nil { |
| + return gates |
| + } |
| + gates.check("seed-region", s.seedRegionReadyForEdges(ctx, region, false, demoClaimEdge) == nil) |
| floor, err := seeds.ReadFloor(ctx, s.cfg.Releases) |
| - if err != nil || floor == nil || floor.Min < seeds.DemoClaimProtocol { |
| - return refuse() |
| + gates.check("seed-floor", err == nil && floor != nil && floor.Min >= seeds.DemoClaimProtocol) |
| + if floor != nil { |
| + ok, err := seeds.ReleaseAtOrAbove(floor.MinRelease, floor.MinRelease) |
| + gates.check("seed-floor-release", ok && err == nil) |
| } |
| snap, err := s.liveEdges(ctx) |
| - if err != nil || !snap.EvidenceComplete() || len(snap.Files) == 0 { |
| - return refuse() |
| - } |
| - counted := 0 |
| + gates.check("edges-evidence", err == nil && snap.EvidenceComplete() && len(snap.Files) != 0) |
| + counted, inRegion := 0, 0 |
| for _, f := range snap.Files { |
| if !demoClaimEdge(f) { |
| continue |
| } |
| counted++ |
| - if !slices.Contains(f.Capabilities, seeds.DemoClaimCapability) || f.SeedsProtocol < seeds.DemoClaimProtocol || |
| - f.SeedsFloorMinRelease != floor.MinRelease || (f.PrevRelease != "none" && f.PrevSeedsAllowedFor != floor.MinRelease) || |
| - ((f.SeedsFloorState != "" || f.SeedsFloorGeneration != 0) && (f.SeedsFloorState != "accepted" || f.SeedsFloorGeneration == 0)) { |
| - return refuse() |
| + if s.homeRegionIn(f.Placement) == s.homeRegionIn(region) { |
| + inRegion++ |
| + if floor != nil { |
| + gates.check("seed-floor-protocol", f.SeedsProtocol >= max(seeds.RequiredProtocol, floor.Min)) |
| + } |
| + gates.check("edge-moves-floor:"+f.Box, (f.MovesFloorState == "" && f.MovesFloorGeneration == 0) || (f.SeedsFloorState == "accepted" && f.SeedsFloorGeneration != 0)) |
| } |
| + gates.check("edge-capability:"+f.Box, slices.Contains(f.Capabilities, seeds.DemoClaimCapability)) |
| + gates.check("edge-protocol:"+f.Box, f.SeedsProtocol >= seeds.DemoClaimProtocol) |
| + if floor != nil { |
| + gates.check("edge-floor:"+f.Box, f.SeedsFloorMinRelease == floor.MinRelease) |
| + gates.check("edge-prev:"+f.Box, f.PrevRelease == "none" || f.PrevSeedsAllowedFor == floor.MinRelease) |
| + } |
| + gates.check("edge-floor-state:"+f.Box, (f.SeedsFloorState == "" && f.SeedsFloorGeneration == 0) || (f.SeedsFloorState == "accepted" && f.SeedsFloorGeneration != 0)) |
| } |
| - if counted == 0 { |
| - return refuse() |
| - } |
| + gates.check("no-shared-edges", counted != 0) |
| + gates.check("seed-region-edges", inRegion != 0) |
| boxes, err := status.LoadBoxes(ctx, s.cfg.Releases) |
| - if err != nil { |
| - return refuse() |
| - } |
| + gates.check("box-status", err == nil) |
| byLabel := map[string]status.Box{} |
| dup := map[string]bool{} |
| for _, b := range boxes { |
| @@ -81,14 +94,16 @@ func (s *Server) demoClaimSeedReady(ctx context.Context, region string) error { |
| continue |
| } |
| b, ok := byLabel[f.Box] |
| - if !ok || dup[f.Box] || status.BoxStale(b, time.Now(), status.SnapshotPeriod) || b.Metrics == nil || !slices.Contains(b.Metrics.Capabilities, seeds.DemoClaimCapability) { |
| - return refuse() |
| - } |
| - if s.homeRegionIn(f.Placement) == s.homeRegionIn(region) && b.Metrics.Platform != "linux-arm64" { |
| - return refuse() |
| + gates.check("box-status:"+f.Box, ok && !dup[f.Box] && !status.BoxStale(b, time.Now(), status.SnapshotPeriod) && b.Metrics != nil && slices.Contains(b.Metrics.Capabilities, seeds.DemoClaimCapability)) |
| + if s.homeRegionIn(f.Placement) == s.homeRegionIn(region) { |
| + gates.check("box-layout:"+f.Box, b.Metrics != nil && b.Metrics.Layout != nil && b.Metrics.Layout.Mode == "nested") |
| + if b.Metrics != nil { |
| + gates.check("box-claim-control:"+f.Box, !b.Metrics.HostClaimEnabled) |
| + gates.check("box-platform:"+f.Box, b.Metrics.Platform == "linux-arm64") |
| + } |
| } |
| } |
| - return nil |
| + return gates |
| } |
| // Call while holding the app lock. Stored identity, not a caller's target, |
diff --git a/internal/console/demo_claim_target.go b/internal/console/demo_claim_target.go| index ac691107b..a9f1d0ec0 100644 |
| --- a/internal/console/demo_claim_target.go |
| +++ b/internal/console/demo_claim_target.go |
| @@ -139,7 +139,7 @@ func (s *Server) reserveDemoClaim(ctx context.Context, f claimFlow, acct *Accoun |
| } |
| region, source := s.resolveNewAppRegion(ctx, rq, false) |
| // Never silently replace an incompatible normal placement with the home. |
| - if region != s.cfg.Region || s.haRegion(region) || s.cfg.HostClaim || s.cfg.AppsDomain == "" { |
| + if region != s.cfg.Region || s.haCreate(region, "") || s.cfg.HostClaim || s.cfg.AppsDomain == "" { |
| return claimOp{}, errDemoClaimPlacement |
| } |
| if err = s.roomIn(ctx, region, ""); err != nil { |
diff --git a/internal/console/demo_claim_transfer.go b/internal/console/demo_claim_transfer.go| index 5ddb19eba..e8d4074b0 100644 |
| --- a/internal/console/demo_claim_transfer.go |
| +++ b/internal/console/demo_claim_transfer.go |
| @@ -7,6 +7,7 @@ import ( |
| "encoding/hex" |
| "errors" |
| "io" |
| + "log" |
| "net/http" |
| "os" |
| "sort" |
| @@ -44,19 +45,27 @@ func (s *Server) claimBoundary(at string) error { |
| } |
| func (s *Server) demoClaimCapabilitiesReady(ctx context.Context, op claimOp) error { |
| + _, err := s.demoClaimCapabilityGates(ctx, op) |
| + return err |
| +} |
| + |
| +func (s *Server) demoClaimCapabilityGates(ctx context.Context, op claimOp) (demoClaimGates, error) { |
| + var gates demoClaimGates |
| found := false |
| for _, p := range s.demoEligiblePeers() { |
| if p.box() == op.SourceBox && p.ready.ClaimCapability == democlaim.Capability && p.ready.MovedCapability == democlaim.MovedCapability { |
| found = true |
| } |
| } |
| - if !found { |
| - return errors.New(demoClaimUnavailable) |
| + gates.check("worker-peer", found) |
| + if err := gates.err(); err != nil { |
| + return gates, err |
| } |
| - if err := s.demoClaimSeedReady(ctx, op.Target.Region); err != nil { |
| - return err |
| + gates = s.demoClaimSeedGates(ctx, op.Target.Region) |
| + if err := gates.err(); err != nil { |
| + return gates, err |
| } |
| - return s.demoClaimMovedReady(ctx) |
| + return s.demoClaimMovedGates(ctx) |
| } |
| // The operation mutex serializes pickup, upload, abort and recovery. It is |
| @@ -102,8 +111,8 @@ func (s *Server) advanceDemoClaimLocked(ctx context.Context, id string) error { |
| if !s.demoClaimEnabled() { |
| return s.beginDemoClaimAbort(ctx, op, "disabled") |
| } |
| - if err = s.demoClaimCapabilitiesReady(ctx, op); err != nil { |
| - return s.beginDemoClaimAbort(ctx, op, "capability") |
| + if gates, err := s.demoClaimCapabilityGates(ctx, op); err != nil { |
| + return s.beginDemoClaimAbort(ctx, op, "capability", gates...) |
| } |
| if err = s.pinDemoClaim(ctx, &op); err != nil { |
| return s.beginDemoClaimAbort(ctx, op, "source_unready") |
| @@ -773,7 +782,7 @@ func (s *Server) abortDemoClaimLocked(ctx context.Context, id, reason string) er |
| return s.beginDemoClaimAbort(ctx, op, reason) |
| }) |
| } |
| -func (s *Server) beginDemoClaimAbort(ctx context.Context, op claimOp, reason string) error { |
| +func (s *Server) beginDemoClaimAbort(ctx context.Context, op claimOp, reason string, gates ...string) error { |
| if op.Phase == "committed" || op.Phase == "done" { |
| return ErrDemoToken |
| } |
| @@ -797,6 +806,9 @@ func (s *Server) beginDemoClaimAbort(ctx context.Context, op claimOp, reason str |
| if err = tx.Commit(); err != nil { |
| return err |
| } |
| + if op.Phase == "reserved" && reason == "capability" { |
| + log.Printf("console: demo claim capability: refusing: %s", demoClaimGates(gates).names()) |
| + } |
| op.Phase = "aborting" |
| return s.continueDemoClaimAbort(ctx, op) |
| } |
diff --git a/internal/console/server.go b/internal/console/server.go| index e1f9f3d77..101640147 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -1007,7 +1007,7 @@ type Server struct { |
| demoTransport http.RoundTripper |
| demoAdmission demoAdmissionCache |
| - demoClaimAdmission demoAdmissionCache |
| + demoClaimAdmission demoClaimAdmissionCache |
| demoStatus demoStatusLimiter |
| demoControlMu sync.RWMutex |
| demoStopMu sync.Mutex |