region HA G3-B10: scoped data proof and storage gate
Co-Authored-By: Astra <noreply@openai.com>
7 files changed,
+112
−3
cmd/console/main.go+18 −0internal/console/apiinstances.go+1 −0internal/console/apisidecars.go+1 −0internal/console/createleftovers.go+30 −1internal/console/hacapacity.go+49 −0internal/console/nocapacity.go+11 −2internal/console/server.go+2 −0
diff --git a/cmd/console/main.go b/cmd/console/main.go| index 013660429..ea509bdc1 100644 |
| --- a/cmd/console/main.go |
| +++ b/cmd/console/main.go |
| @@ -632,6 +632,7 @@ func serve() { |
| cfg.WakeRetryReset = console.NewProductionWakeRetryReset() |
| } |
| cfg.Perf = rec |
| + cfg.HADataStores = buildHADataStores(context.Background(), cfg, rec, release.NewS3StoreIn) |
| srv, err := console.NewServer(cfg, st) |
| if err != nil { |
| log.Fatal(err) |
| @@ -2086,3 +2087,20 @@ func startHACapacity(ctx context.Context, srv *console.Server, cfg console.Confi |
| close(done) |
| return done |
| } |
| + |
| +func buildHADataStores(ctx context.Context, cfg console.Config, rec *perf.Recorder, open func(context.Context, string, string) (*release.S3Store, error)) map[string]release.Store { |
| + regions, err := console.HADataStoreRegions(cfg) |
| + if err != nil { |
| + return nil |
| + } |
| + stores := make(map[string]release.Store) |
| + for code, coords := range regions { |
| + store, err := open(ctx, coords.DataBucket, coords.DataBucketRegion) |
| + if err != nil { |
| + log.Printf("console: HA data store for %s: %v", code, err) |
| + continue |
| + } |
| + stores[code] = console.TimeS3Store(store, rec) |
| + } |
| + return stores |
| +} |
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go| index 7d7a5c4ee..378db2db3 100644 |
| --- a/internal/console/apiinstances.go |
| +++ b/internal/console/apiinstances.go |
| @@ -2311,6 +2311,7 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request) |
| taken: host + " still holds data from a deleted instance. Choose another address, or ask an operator to purge it.", |
| unsure: "could not check whether " + host + " is free. Try again.", |
| }); ref != nil { |
| + setNoCapacityRetryAfter(w, ref) |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
diff --git a/internal/console/apisidecars.go b/internal/console/apisidecars.go| index 672a952d1..fb035de67 100644 |
| --- a/internal/console/apisidecars.go |
| +++ b/internal/console/apisidecars.go |
| @@ -444,6 +444,7 @@ func (s *Server) handleAPISidecarCreate(w http.ResponseWriter, r *http.Request) |
| taken: "the name " + name + " still holds data from a deleted sidecar. Choose another name, or ask an operator to purge it.", |
| unsure: "could not check whether the name " + name + " is free. Try again.", |
| }); ref != nil { |
| + setNoCapacityRetryAfter(w, ref) |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
diff --git a/internal/console/createleftovers.go b/internal/console/createleftovers.go| index 468e940e7..8d7ffe1c5 100644 |
| --- a/internal/console/createleftovers.go |
| +++ b/internal/console/createleftovers.go |
| @@ -164,7 +164,13 @@ func (s *Server) leftoverReplicaRefusal(parent context.Context, c leftoverCreate |
| if c.sidecar { |
| kind = "sidecar" |
| } |
| - name := s.offlineName(c.region) |
| + name := s.offlinePlace(c.region) |
| + if s.haScopeInvalid { |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: fmt.Sprintf(haCapacityUnknownRefusal, name), haCapacityRetryAfter: 60} |
| + } |
| + if len(s.haPools[s.homeRegionOut(c.region)]) > 0 { |
| + return s.scopedHALeftoverRefusal(ctx, c, kind, name) |
| + } |
| if s.homeRegionIn(c.region) != "" || c.pool != "" { |
| return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf("high availability data creation is not available yet in %s. use the console's home region without a pool.", name)} |
| } |
| @@ -198,3 +204,26 @@ func (s *Server) leftoverReplicaRefusal(parent context.Context, c leftoverCreate |
| } |
| return nil |
| } |
| + |
| +func (s *Server) scopedHALeftoverRefusal(ctx context.Context, c leftoverCreate, kind, name string) *doorRefusal { |
| + unsure := &doorRefusal{Status: http.StatusServiceUnavailable, |
| + Msg: fmt.Sprintf("could not check this %s's data in %s. nothing was created. try again. persistent failures may require an operator to check home data access.", kind, name), haCapacityRetryAfter: 60} |
| + store := s.cfg.HADataStores[s.homeRegionOut(c.region)] |
| + if s.cfg.HibernateBucket == "" || store == nil { |
| + return unsure |
| + } |
| + for i, prefix := range []string{activator.HibernatePrefix(s.cfg.HibernatePrefix, c.host) + "/", activator.MovesPrefix(c.host)} { |
| + class, err := firstHALeftover(ctx, store, prefix) |
| + if err != nil { |
| + log.Printf("console: %s: HA prefix check for %s: %v", c.logTag, c.host, err) |
| + return unsure |
| + } |
| + if class != "" { |
| + if i == 1 { |
| + class = "replica" |
| + } |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf("%s still holds %s from a deleted %s. choose another address, or ask an operator to purge it.", c.host, class, kind)} |
| + } |
| + } |
| + return nil |
| +} |
diff --git a/internal/console/hacapacity.go b/internal/console/hacapacity.go| index 0d68d17e5..b90545f5e 100644 |
| --- a/internal/console/hacapacity.go |
| +++ b/internal/console/hacapacity.go |
| @@ -186,6 +186,20 @@ func parseHACapacityConfig(raw string, regions []string, home string) (*haCapaci |
| c.universe = slices.Compact(c.universe) |
| return &c, nil |
| } |
| +func HADataStoreRegions(cfg Config) (map[string]haCoordinates, error) { |
| + c, err := parseHACapacityConfig(cfg.HACapacityConfig, cfg.HARegions, cfg.HomeRegion) |
| + if err != nil { |
| + return nil, err |
| + } |
| + regions := make(map[string]haCoordinates) |
| + for region, coords := range c.Regions { |
| + if len(coords.HAPools) > 0 { |
| + regions[region] = coords |
| + } |
| + } |
| + return regions, nil |
| +} |
| + |
| func haGroupReference(account, instance string) string { |
| sum := sha256.Sum256([]byte("carlos-box-ref-v1|" + account + "|" + instance)) |
| return "g-" + hex.EncodeToString(sum[:])[:20] |
| @@ -284,6 +298,41 @@ type HACapacityDecision struct { |
| // These storage keys belong only on operator diagnostics, never member pages. |
| type HACapacityBlocker struct{ Key, Code string } |
| +func (o *HACapacityObservation) MixedStorage(region string) (box string, mixed bool) { |
| + if o == nil || o.config == nil { |
| + return "", false |
| + } |
| + coords, ok := o.config.Regions[region] |
| + if !ok { |
| + return "", false |
| + } |
| + wantPlacement := region |
| + if region == o.config.home { |
| + wantPlacement = "" |
| + } |
| + for _, f := range o.files { |
| + if f.malformed { |
| + continue |
| + } |
| + placement, valid := haField[string](f.fields, "placement") |
| + if _, present := f.fields["placement"]; !present { |
| + valid = true |
| + } |
| + if !valid || !provision.ValidRegion(placement) || placement != wantPlacement { |
| + continue |
| + } |
| + bucket, valid := haField[string](f.fields, "data_bucket") |
| + if valid && objstore.ValidBucketName(bucket) && bucket != coords.DataBucket { |
| + return f.ref, true |
| + } |
| + bucketRegion, valid := haField[string](f.fields, "data_bucket_region") |
| + if valid && haAWSRegion.MatchString(bucketRegion) && bucketRegion != coords.DataBucketRegion { |
| + return f.ref, true |
| + } |
| + } |
| + return "", false |
| +} |
| + |
| func (o *HACapacityObservation) Regional(region string, now time.Time) HACapacityRegional { |
| n, complete, _ := o.project(region, "", false, now) |
| return HACapacityRegional{n, complete} |
diff --git a/internal/console/nocapacity.go b/internal/console/nocapacity.go| index 6f11dfaf3..c564738bc 100644 |
| --- a/internal/console/nocapacity.go |
| +++ b/internal/console/nocapacity.go |
| @@ -12,6 +12,7 @@ import ( |
| "time" |
| "github.com/carlosframework/platform/internal/alerting" |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| "github.com/carlosframework/platform/internal/hostclaim" |
| "github.com/carlosframework/platform/internal/hostops" |
| ) |
| @@ -51,8 +52,16 @@ func (s *Server) requireHACreateCapacity(ctx context.Context, region, pool strin |
| } |
| name := s.homeRegionOut(region) |
| decision := HACapacityDecision{Kind: HACapacityUnknown} |
| - if obs, available := s.HACapacity(); available { |
| - decision = obs.Decide(name, pool, s.haCapacity.Load().clock.Now(), sidecarPlacement...) |
| + if obs, available := s.HACapacity(); obs != nil { |
| + if len(s.haPools[name]) > 0 { |
| + if box, mixed := obs.MixedStorage(name); mixed { |
| + log.Printf("console: HA create in %s: mixed storage in %s", name, edgesfile.Prefix+box+".json") |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: fmt.Sprintf("%s has a box on a different data bucket (%s). Nothing was created. An operator must fix or retire that box.", s.offlinePlace(region), box), haCapacityRetryAfter: 60} |
| + } |
| + } |
| + if available { |
| + decision = obs.Decide(name, pool, s.haCapacity.Load().clock.Now(), sidecarPlacement...) |
| + } |
| } |
| switch decision.Kind { |
| case HACapacityAllow: |
diff --git a/internal/console/server.go b/internal/console/server.go| index 3b4a7ead0..14a9054da 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -558,6 +558,8 @@ type Config struct { |
| // import spools. Start-up removes the pool's own leftovers from it. |
| // Empty: the seed doors answer 507. |
| SeedScratchDir string |
| + HADataStores map[string]release.Store |
| + |
| // SeedData is the home region's data bucket, where seeds/ lives (the |
| // bucket HibernateBucket names, at its root). nil: the seed doors |
| // answer 503. |