carlos / platform Public

region HA P1a Task 10: one files store builder, lease.NewFilesStore

The HA restore and the files publisher (and the claim's files) build the
files store through lease.NewFilesStore: s3compat's options and the
lease's own region rule, so a CARLOS_S3_ENDPOINT store with no region
configured is reached as the lease reaches it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev d43c27666878c7722bb71743ab99239c97cabd26 parent 1ab2a94
3 files changed, +57 −17
  • internal/host/claimfiles.go +5 −17
  • internal/host/hawake_wiring_test.go +25 −0
  • internal/lease/endpoint.go +27 −0
diff --git a/internal/host/claimfiles.go b/internal/host/claimfiles.go
index 4f5f6c1fd..6c443de1f 100644
--- a/internal/host/claimfiles.go
+++ b/internal/host/claimfiles.go
@@ -7,9 +7,6 @@ import (
"path/filepath"
"sync"
- "github.com/aws/aws-sdk-go-v2/aws"
- awsconfig "github.com/aws/aws-sdk-go-v2/config"
- awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/carlosframework/platform/internal/activator"
"github.com/carlosframework/platform/internal/hostbox"
"github.com/carlosframework/platform/internal/hostwrite"
@@ -19,8 +16,6 @@ import (
"github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
- "github.com/carlosframework/platform/internal/rootaws"
- "github.com/carlosframework/platform/internal/s3compat"
)
// ClaimProductionFilesSource is the C8 files-manifest provider's production
@@ -205,19 +200,12 @@ func (s *movesFilesSource) Open(ctx context.Context, claims release.Store, data
func (s *movesFilesSource) Close(context.Context) error { return nil }
// newProductionFilesStore is the data bucket as a release.Store for the
-// files manifests: the S3 store (create-only and compare-and-swap Put)
-// over a client bound to the bucket's region, loaded through rootaws
-// (#267), with the options release.NewS3Store sets.
+// files manifests: lease.NewFilesStore, the one builder (create-only and
+// compare-and-swap Put, the lease's region rule and CARLOS_S3_ENDPOINT),
+// shared by the claim's files and the region HA wake's restore and
+// publisher, so neither reads or writes a different store from the lease.
func newProductionFilesStore(ctx context.Context, bucket, region string) (release.Store, error) {
- cfg, err := rootaws.LoadDefaultConfig(ctx, awsconfig.WithRegion(region))
- if err != nil {
- return nil, err
- }
- client := awss3.NewFromConfig(cfg, s3compat.Options(), func(o *awss3.Options) {
- o.RequestChecksumCalculation = aws.RequestChecksumCalculationWhenRequired
- o.ResponseChecksumValidation = aws.ResponseChecksumValidationWhenRequired
- })
- return &release.S3Store{Client: client, Bucket: bucket}, nil
+ return lease.NewFilesStore(ctx, bucket, region)
}
// registerProductionClaimFiles is the agent's registration, before
diff --git a/internal/host/hawake_wiring_test.go b/internal/host/hawake_wiring_test.go
index 21d5ea957..5f8b869b9 100644
--- a/internal/host/hawake_wiring_test.go
+++ b/internal/host/hawake_wiring_test.go
@@ -9,6 +9,7 @@ import (
"time"
"github.com/carlosframework/platform/internal/domains"
+ "github.com/carlosframework/platform/internal/lease/locktest"
"github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
@@ -77,3 +78,27 @@ func TestAgentWiresTheHAWake(t *testing.T) {
}
}
}
+
+// The files store an HA wake restores from and publishes to is the one
+// builder lease shares (Task 10): it reaches a CARLOS_S3_ENDPOINT store
+// even when no region is configured anywhere, signing as the lease does,
+// and does both halves (a conditional create, then a read) there.
+func TestProductionFilesStoreHonoursTheEndpoint(t *testing.T) {
+ fx := locktest.NewLockFixture(t)
+ t.Setenv("AWS_REGION", "")
+ t.Setenv("AWS_DEFAULT_REGION", "")
+ ctx := context.Background()
+ s, err := newProductionFilesStore(ctx, fx.Bucket, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := s.Put(ctx, "shop.example.com/files/0/manifest.json", []byte(`{}`), ""); err != nil {
+ t.Fatalf("put through the endpoint: %v", err)
+ }
+ if body, _, err := s.Get(ctx, "shop.example.com/files/0/manifest.json"); err != nil || string(body) != `{}` {
+ t.Fatalf("get through the endpoint: %q, %v", body, err)
+ }
+ if got := strings.Join(fx.Requests(), ","); got != "PUT shop.example.com/files/0/manifest.json,GET shop.example.com/files/0/manifest.json" {
+ t.Fatalf("the endpoint saw %s", got)
+ }
+}
diff --git a/internal/lease/endpoint.go b/internal/lease/endpoint.go
index 4ec0156a9..25e624713 100644
--- a/internal/lease/endpoint.go
+++ b/internal/lease/endpoint.go
@@ -24,6 +24,7 @@ import (
awsconfig "github.com/aws/aws-sdk-go-v2/config"
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
lss3 "github.com/benbjohnson/litestream/s3"
+ "github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/rootaws"
"github.com/carlosframework/platform/internal/s3compat"
@@ -168,3 +169,29 @@ func ReadReplicaClient(ctx context.Context, bucket, prefix, region string) (*lss
}
return newReplicaClient(bucket, prefix, region)
}
+
+// NewFilesStore is the hibernate bucket as a release.Store for a host's
+// published files (<hibernate prefix>/files/0/): the one builder every
+// files reader and writer uses, so the files sit beside lock.json and the
+// LTX files on the same store. It resolves the region by the lease's own
+// rule (regionFor: the explicit region, else the default chain, else,
+// under a CARLOS_S3_ENDPOINT override, litestream's non-AWS default) and
+// applies s3compat's options, so a compatible store gets the endpoint,
+// path-style addressing and every quirk. Checksums are when-required on
+// every client, as release.NewS3Store sets them.
+func NewFilesStore(ctx context.Context, bucket, region string) (release.Store, error) {
+ var opts []func(*awsconfig.LoadOptions) error
+ if region != "" {
+ opts = append(opts, awsconfig.WithRegion(region))
+ }
+ cfg, err := rootaws.LoadDefaultConfig(ctx, opts...)
+ if err != nil {
+ return nil, fmt.Errorf("files store: load aws config: %w", err)
+ }
+ cfg.Region = regionFor(region, cfg.Region)
+ client := awss3.NewFromConfig(cfg, s3compat.Options(), func(o *awss3.Options) {
+ o.RequestChecksumCalculation = aws.RequestChecksumCalculationWhenRequired
+ o.ResponseChecksumValidation = aws.ResponseChecksumValidationWhenRequired
+ })
+ return &release.S3Store{Client: client, Bucket: bucket}, nil
+}