appconfig: read the default, environment and host layers together
A wake's config admission (CanProvideConfig, before the lease) read the app's default bundle, its environment and the host's overlay one after another: about 83 ms on a dev wake. They are independent objects, so they are now read concurrently; the answers are taken in the order the reads used to run, so every error and etag is the one it was.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+123
−5
internal/appconfig/environments.go+23 −3internal/appconfig/instances.go+17 −2internal/appconfig/loadconcurrent_test.go+83 −0
diff --git a/internal/appconfig/environments.go b/internal/appconfig/environments.go| index d7da25498..1ddc56e13 100644 |
| --- a/internal/appconfig/environments.go |
| +++ b/internal/appconfig/environments.go |
| @@ -16,6 +16,7 @@ import ( |
| "regexp" |
| "sort" |
| "strings" |
| + "sync" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -182,7 +183,27 @@ func loadMergedETags(ctx context.Context, s release.Store, acct, app, envName st |
| if !release.ValidApp(app) { |
| return nil, "", "", fmt.Errorf("config: invalid app %q", app) |
| } |
| + // The environment is read beside the default bundle, not after it: two |
| + // independent objects, and a wake's config admission waits on both |
| + // before its lease (wake latency, pass 2). The answers are taken in the |
| + // order the reads used to run, so every error is the one it was. |
| + var ( |
| + env *Manifest |
| + envETag string |
| + envFound bool |
| + envErr error |
| + envRead = envName != "" && ValidEnvironmentName(envName) |
| + wg sync.WaitGroup |
| + ) |
| + if envRead { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + env, envETag, envFound, envErr = getIfExistsAtETag(ctx, s, EnvironmentKey(acct, app, envName)) |
| + }() |
| + } |
| def, defETag, defFound, err := getIfExistsAtETag(ctx, s, Key(acct, app)) |
| + wg.Wait() |
| if err != nil { |
| return nil, "", "", err |
| } |
| @@ -192,11 +213,10 @@ func loadMergedETags(ctx context.Context, s release.Store, acct, app, envName st |
| } |
| return def, defETag, "", nil |
| } |
| - if !ValidEnvironmentName(envName) { |
| + if !envRead { |
| return nil, "", "", fmt.Errorf("config: invalid environment name %q", envName) |
| } |
| - env, envETag, envFound, err := getIfExistsAtETag(ctx, s, EnvironmentKey(acct, app, envName)) |
| - if err != nil { |
| + if err := envErr; err != nil { |
| return nil, "", "", err |
| } |
| if !envFound { |
diff --git a/internal/appconfig/instances.go b/internal/appconfig/instances.go| index 022ee8ebd..1cc6eaf87 100644 |
| --- a/internal/appconfig/instances.go |
| +++ b/internal/appconfig/instances.go |
| @@ -29,6 +29,7 @@ import ( |
| "fmt" |
| "regexp" |
| "strings" |
| + "sync" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -223,13 +224,27 @@ func LoadMergedHostVersions(ctx context.Context, s release.Store, acct, app, env |
| if !ValidInstanceHost(host) { |
| return nil, MergedVersions{}, fmt.Errorf("config: invalid instance host %q", host) |
| } |
| + // The host's overlay is read beside the app's layers (loadMergedETags), |
| + // not after them; the answers are taken in the old order. |
| + var ( |
| + inst *Manifest |
| + etag string |
| + found bool |
| + instErr error |
| + wg sync.WaitGroup |
| + ) |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + inst, etag, found, instErr = LoadInstanceConfig(ctx, s, acct, app, host) |
| + }() |
| base, defETag, envETag, err := loadMergedETags(ctx, s, acct, app, envName) |
| + wg.Wait() |
| if err != nil { |
| return nil, MergedVersions{}, err |
| } |
| v := MergedVersions{Default: defETag, Environment: envETag} |
| - inst, etag, found, err := LoadInstanceConfig(ctx, s, acct, app, host) |
| - if err != nil { |
| + if err := instErr; err != nil { |
| return nil, MergedVersions{}, err |
| } |
| if !found { |
diff --git a/internal/appconfig/loadconcurrent_test.go b/internal/appconfig/loadconcurrent_test.go| new file mode 100644 |
| index 000000000..978512187 |
| --- /dev/null |
| +++ b/internal/appconfig/loadconcurrent_test.go |
| @@ -0,0 +1,83 @@ |
| +package appconfig |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// gatedStore holds every Get until want of them are in flight at once, or |
| +// a deadline passes. A loader that reads its layers one after another |
| +// waits out the deadline on each; one that reads them together does not. |
| +type gatedStore struct { |
| + release.Store |
| + want int |
| + mu sync.Mutex |
| + in int |
| + open chan struct{} |
| +} |
| + |
| +func (g *gatedStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + g.mu.Lock() |
| + g.in++ |
| + if g.in == g.want { |
| + close(g.open) |
| + } |
| + g.mu.Unlock() |
| + select { |
| + case <-g.open: |
| + case <-time.After(300 * time.Millisecond): |
| + } |
| + return g.Store.Get(ctx, key) |
| +} |
| + |
| +func putManifest(t *testing.T, s release.Store, key string, m *Manifest) { |
| + t.Helper() |
| + raw, err := json.Marshal(m) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := s.Put(context.Background(), key, raw, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// A wake's config admission reads three layers (the app's default bundle, |
| +// its environment, the host's overlay). They are independent objects, so |
| +// they are read together: one round trip, not three, before the lease. |
| +func TestLoadMergedHostVersionsReadsItsLayersTogether(t *testing.T) { |
| + dir := release.NewDirStore(t.TempDir()) |
| + putManifest(t, dir, Key("bab", "shop"), &Manifest{Schema: 1, App: "shop", Vars: map[string]Var{"A": {Kind: "env", Value: "default"}, "B": {Kind: "env", Value: "default"}}}) |
| + putManifest(t, dir, EnvironmentKey("bab", "shop", "prod"), &Manifest{Schema: 1, App: "shop", Vars: map[string]Var{"B": {Kind: "env", Value: "env"}, "C": {Kind: "env", Value: "env"}}}) |
| + putManifest(t, dir, InstanceConfigKey("bab", "shop", "one.example.com"), &Manifest{Schema: 1, App: "shop", Vars: map[string]Var{"C": {Kind: "env", Value: "host"}}}) |
| + g := &gatedStore{Store: dir, want: 3, open: make(chan struct{})} |
| + start := time.Now() |
| + m, v, err := LoadMergedHostVersions(context.Background(), g, "bab", "shop", "prod", "one.example.com") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if took := time.Since(start); took > 250*time.Millisecond { |
| + t.Errorf("took %s: the three layers were not read together", took) |
| + } |
| + if m.Vars["A"].Value != "default" || m.Vars["B"].Value != "env" || m.Vars["C"].Value != "host" { |
| + t.Errorf("merge = %+v, want default < environment < host", m.Vars) |
| + } |
| + if v.Default == "" || v.Environment == "" || v.Overlay == "" { |
| + t.Errorf("versions = %+v, want each layer's etag", v) |
| + } |
| +} |
| + |
| +// Reading together changes no answer: an environment the route is bound to |
| +// but nobody created is still the error, whatever the other reads found. |
| +func TestLoadMergedHostVersionsStillRefusesAMissingEnvironment(t *testing.T) { |
| + dir := release.NewDirStore(t.TempDir()) |
| + putManifest(t, dir, Key("bab", "shop"), &Manifest{Schema: 1, App: "shop", Vars: map[string]Var{}}) |
| + putManifest(t, dir, InstanceConfigKey("bab", "shop", "one.example.com"), &Manifest{Schema: 1, App: "shop", Vars: map[string]Var{}}) |
| + if _, _, err := LoadMergedHostVersions(context.Background(), dir, "bab", "shop", "prod", "one.example.com"); err == nil { |
| + t.Fatal("a missing environment was accepted") |
| + } |
| +} |