host: asset fix round 1: publish assets blocks only once the asset store is open
The policy carries a publication gate, closed until armEdgeAssetCache has opened the store and armed the edge. The edges file writer and its loop work with assetPolicy.active(), nil while the gate is closed, so an armed box whose store failed, and every armed box before its ready hook runs, publishes exactly as an unarmed box; a later successful open publishes on the next tick. Still one shared policy (drift D12).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+129
−26
internal/host/assetarm.go+8 −4internal/host/assetarm_test.go+77 −15internal/host/assetpolicy.go+27 −0internal/host/assetpublish_test.go+3 −0internal/host/edgesfile.go+14 −7
CI failed — run details
diff --git a/internal/host/assetarm.go b/internal/host/assetarm.go| index a26777876..35df01ca2 100644 |
| --- a/internal/host/assetarm.go |
| +++ b/internal/host/assetarm.go |
| @@ -30,10 +30,11 @@ func assetCacheBudget(getenv func(string) string, total uint64) int64 { |
| // CARLOS_ASSET_CACHE=1 it opens the store at dataDir/asset-cache and hands |
| // enable (the edge's EnableAssetCache) the store and policy, the one policy |
| // the edges file loop fills (drift D12). Unarmed, or with no policy, it does |
| -// nothing and the edge forwards as today. A Statfs or Open error is logged |
| -// and leaves the cache off; the box still publishes its assets blocks, and |
| -// fills sent to it are then answered as ordinary hops. It returns the store |
| -// it handed over, or nil. |
| +// nothing and the edge forwards as today. Only a successful open lets the |
| +// edges file writer publish the policy's assets blocks (assetPolicy.active), |
| +// so a Statfs or Open error, logged, leaves the box exactly as if it were |
| +// unarmed: no edge cache and nothing published. It returns the store it |
| +// handed over, or nil. |
| func armEdgeAssetCache(getenv func(string) string, dataDir string, fs assetcache.FS, policy *assetPolicy, enable func(*assetcache.Store, edge.LocalAssets)) *assetcache.Store { |
| if !assetCacheArmed(getenv) || policy == nil { |
| return nil |
| @@ -52,6 +53,9 @@ func armEdgeAssetCache(getenv func(string) string, dataDir string, fs assetcache |
| } |
| st := s.Stats() |
| log.Printf("agent: asset cache armed at %s (budget %d bytes, %d entries on disk)", dir, budget, st.Entries) |
| + // The edge first, then the gate: by the time a far edge reads this |
| + // box's assets blocks, its edge can answer and acknowledge the fills. |
| enable(s, policy) |
| + policy.allowPublication() |
| return s |
| } |
diff --git a/internal/host/assetarm_test.go b/internal/host/assetarm_test.go| index 7d309f057..f6ba9e535 100644 |
| --- a/internal/host/assetarm_test.go |
| +++ b/internal/host/assetarm_test.go |
| @@ -1,6 +1,7 @@ |
| package host |
| import ( |
| + "bytes" |
| "errors" |
| "os" |
| "path/filepath" |
| @@ -80,34 +81,71 @@ func TestAssetCacheArmingReadsTheEnv(t *testing.T) { |
| } |
| } |
| +// catchingWorld is an asset world with one host that catches /assets/ once |
| +// its box publishes: shipped, routed and confirmed. |
| +func catchingWorld(t *testing.T) *assetWorld { |
| + t.Helper() |
| + w := newAssetWorld(t) |
| + w.ship("v1", "/assets/") |
| + w.instance("shop.example.com", assetGen1, "v1") |
| + w.confirm() |
| + return w |
| +} |
| + |
| +// wantNothingPublished runs a production writer pass with p and asserts |
| +// the file it wrote carries no assets block at all, as on an unarmed box. |
| +func wantNothingPublished(t *testing.T, w *assetWorld, p *assetPolicy, when string) { |
| + t.Helper() |
| + w.policy = p |
| + wantNoAssets(t, w.hosts(), "shop.example.com") |
| + if raw := w.raw()["shop.example.com"]; bytes.Contains(raw, []byte(`"assets"`)) { |
| + t.Fatalf("%s: the writer published %s", when, raw) |
| + } |
| +} |
| + |
| +// wantPublished runs a production writer pass with p and asserts the host's |
| +// block is out. |
| +func wantPublished(t *testing.T, w *assetWorld, p *assetPolicy) { |
| + t.Helper() |
| + w.policy = p |
| + wantAssets(t, w.hosts(), "shop.example.com", []string{"/assets/"}, assetGen1, assetT0) |
| +} |
| + |
| // D12: the edge answers from the policy the edges file writer fills. The |
| // agent creates it before the loop has a store, so the loop binds its |
| -// manifest cache; the edge sees the writer's first settled snapshot, and |
| -// nothing before it. |
| +// manifest cache. Until the ready hook has opened the store the writer |
| +// publishes nothing (a far edge must never be told to fill from a home that |
| +// cannot acknowledge, review fix 1); after it, the edge sees the writer's |
| +// first settled snapshot. |
| func TestAssetCacheArmingSharesTheWritersPolicy(t *testing.T) { |
| t.Setenv("CARLOS_DATA_DIR", t.TempDir()) |
| t.Setenv("CARLOS_ASSET_CACHE", "1") |
| - w := newAssetWorld(t) |
| - w.ship("v1", "/assets/") |
| - w.instance("shop.example.com", assetGen1, "v1") |
| - w.confirm() |
| + w := catchingWorld(t) |
| p := armAssetPolicy(os.Getenv, w.reg, w.auth, nil, w.servingBuild) // as the agent builds it |
| + p.bindManifests(w.cache) // what edgesFileLoop does with its store |
| + // The window before the ready hook: the loop may already be running. |
| + if p.active() != nil || p.active().rewriteDue() { |
| + t.Fatal("a policy whose store is not open is active for the writer") |
| + } |
| + wantNothingPublished(t, w, p, "before arming") |
| + if p.AssetsReady() { |
| + t.Fatal("a writer pass before arming stored a snapshot") |
| + } |
| + |
| rec := &enableRecorder{} |
| armEdgeAssetCache(os.Getenv, dataDir(), roomyFS, p, rec.enable) |
| if rec.local == nil { |
| t.Fatal("the edge was handed no policy") |
| } |
| if _, ok := rec.local.For("shop.example.com"); ok { |
| - t.Fatal("the edge's policy answered before the writer's first pass") |
| + t.Fatal("the edge's policy answered before the writer's first armed pass") |
| } |
| if r, ok := rec.local.(interface{ AssetsReady() bool }); !ok || r.AssetsReady() { |
| t.Fatalf("AssetsReady before the first snapshot: implemented %v", ok) |
| } |
| - p.bindManifests(w.cache) // what edgesFileLoop does with its store |
| - w.policy = p |
| - w.pass() |
| + wantPublished(t, w, p) |
| a, ok := rec.local.For("shop.example.com") |
| if !ok || a.Gen != assetGen1 || a.Account != "abc" || a.App != "shop" { |
| t.Fatalf("after the writer's pass the edge's policy answers %+v %v", a, ok) |
| @@ -120,26 +158,50 @@ func TestAssetCacheArmingSharesTheWritersPolicy(t *testing.T) { |
| } |
| } |
| -// An Open or Statfs error leaves the cache off, with nothing handed to the |
| -// edge. |
| +// An Open or Statfs error leaves the cache off as if the box were unarmed: |
| +// nothing handed to the edge, and no assets block in the edges file (review |
| +// fix 1). A later successful open starts publishing. |
| func TestAssetCacheArmingFailureLeavesItOff(t *testing.T) { |
| t.Setenv("CARLOS_ASSET_CACHE", "1") |
| t.Run("statfs", func(t *testing.T) { |
| t.Setenv("CARLOS_DATA_DIR", t.TempDir()) |
| - if _, rec := armFromEnv(t, newAssetWorld(t), spaceFS{err: assetcache.ErrUnsupported}); rec.calls != 0 { |
| + w := catchingWorld(t) |
| + p, rec := armFromEnv(t, w, spaceFS{err: assetcache.ErrUnsupported}) |
| + if rec.calls != 0 { |
| t.Fatalf("%d EnableAssetCache call(s) after a Statfs error", rec.calls) |
| } |
| + wantNothingPublished(t, w, p, "after a Statfs error") |
| + wantNothingPublished(t, w, p, "on the next pass") |
| + |
| + armEdgeAssetCache(os.Getenv, dataDir(), roomyFS, p, rec.enable) |
| + if rec.calls != 1 { |
| + t.Fatalf("%d EnableAssetCache call(s) after the open succeeded", rec.calls) |
| + } |
| + wantPublished(t, w, p) |
| }) |
| t.Run("open", func(t *testing.T) { |
| data := t.TempDir() |
| t.Setenv("CARLOS_DATA_DIR", data) |
| // A file where the cache directory goes: Open cannot create it. |
| - if err := os.WriteFile(filepath.Join(data, "asset-cache"), nil, 0o600); err != nil { |
| + blocker := filepath.Join(data, "asset-cache") |
| + if err := os.WriteFile(blocker, nil, 0o600); err != nil { |
| t.Fatal(err) |
| } |
| - if _, rec := armFromEnv(t, newAssetWorld(t), roomyFS); rec.calls != 0 { |
| + w := catchingWorld(t) |
| + p, rec := armFromEnv(t, w, roomyFS) |
| + if rec.calls != 0 { |
| t.Fatalf("%d EnableAssetCache call(s) after an Open error", rec.calls) |
| } |
| + wantNothingPublished(t, w, p, "after an Open error") |
| + |
| + if err := os.Remove(blocker); err != nil { |
| + t.Fatal(err) |
| + } |
| + armEdgeAssetCache(os.Getenv, dataDir(), roomyFS, p, rec.enable) |
| + if rec.calls != 1 { |
| + t.Fatalf("%d EnableAssetCache call(s) after the open succeeded", rec.calls) |
| + } |
| + wantPublished(t, w, p) |
| }) |
| } |
diff --git a/internal/host/assetpolicy.go b/internal/host/assetpolicy.go| index 1769cd18f..e9bf05460 100644 |
| --- a/internal/host/assetpolicy.go |
| +++ b/internal/host/assetpolicy.go |
| @@ -121,6 +121,13 @@ type assetPolicy struct { |
| // snap is the last settled decision For and Declared answer from, stored |
| // only by the edges file writer. |
| snap atomic.Pointer[assetSnapshot] |
| + |
| + // publishing gates the whole policy for the writer (active): false until |
| + // the ready hook has opened this box's asset store and armed its edge |
| + // (armEdgeAssetCache), so an edges file never advertises a home that |
| + // cannot answer or acknowledge a fill. Set by the ready hook, read by |
| + // the writer's goroutine. |
| + publishing atomic.Bool |
| } |
| type writtenAssets struct { |
| @@ -142,6 +149,26 @@ func armAssetPolicy(getenv func(string) string, reg *registry.Registry, auth *as |
| return &assetPolicy{reg: reg, auth: auth, manifests: manifests, serving: sb, written: map[string]writtenAssets{}} |
| } |
| +// allowPublication opens the writer's gate (active): the box's asset store |
| +// is open and its edge armed, so the edges file may carry assets blocks. |
| +func (p *assetPolicy) allowPublication() { |
| + if p != nil { |
| + p.publishing.Store(true) |
| + } |
| +} |
| + |
| +// active is the policy the edges file writer works with: p once publication |
| +// is allowed, else nil, which the writer treats exactly as an unarmed box |
| +// (no assets blocks, no manifest reads, no snapshot, no rewrite due). An |
| +// armed box whose store failed to open, and every armed box in the moment |
| +// before its ready hook runs, so publishes nothing (Task 7 review fix 1). |
| +func (p *assetPolicy) active() *assetPolicy { |
| + if p == nil || !p.publishing.Load() { |
| + return nil |
| + } |
| + return p |
| +} |
| + |
| // bindManifests gives a policy built without a manifest cache the one the |
| // edges file loop reads through its own store handle. The agent builds the |
| // one policy before that loop has opened the store, and hands it to both the |
diff --git a/internal/host/assetpublish_test.go b/internal/host/assetpublish_test.go| index 3d4de1694..134836809 100644 |
| --- a/internal/host/assetpublish_test.go |
| +++ b/internal/host/assetpublish_test.go |
| @@ -156,6 +156,7 @@ func newAssetWorld(t *testing.T) *assetWorld { |
| if w.policy == nil { |
| t.Fatal("an armed environment built no policy") |
| } |
| + w.policy.allowPublication() // the store opened (armEdgeAssetCache) |
| return w |
| } |
| @@ -621,6 +622,7 @@ func TestPublishesNothingUnderAnExclusion(t *testing.T) { |
| {"no authority yet", func(w *assetWorld) { |
| w.auth = newAssetAuthority(w.clock.now) |
| w.policy = armAssetPolicy(armedAssetEnv, w.reg, w.auth, w.cache, w.servingBuild) |
| + w.policy.allowPublication() |
| w.auth.recordsPass(assetT0, provision.ReconcileReport{Complete: map[string]bool{"abc/shop": true}}) |
| }}, |
| {"a build that declares nothing", func(w *assetWorld) { |
| @@ -1053,6 +1055,7 @@ func newAuthorityBox(t *testing.T) *authorityBox { |
| reg: reg, regPath: regPath, dataDir: t.TempDir(), clock: &assetClock{t: assetT0}} |
| b.auth = newAssetAuthority(b.clock.now) |
| b.policy = armAssetPolicy(armedAssetEnv, reg, b.auth, newAssetManifestCache(base), noServingBuild) |
| + b.policy.allowPublication() |
| manifest, err := json.Marshal(map[string]any{"id": 2, "sqid": acct, "name": "fixture", "created_at": assetT0}) |
| if err != nil { |
| t.Fatal(err) |
diff --git a/internal/host/edgesfile.go b/internal/host/edgesfile.go| index 46922b7d3..f604d7822 100644 |
| --- a/internal/host/edgesfile.go |
| +++ b/internal/host/edgesfile.go |
| @@ -425,9 +425,12 @@ var errBoxAddressesUnknown = errors.New("edges file: the box's addresses could n |
| // nil publishes exactly what edgesFileWrite always has. |
| // |
| // assets is the box's asset-cache policy, nil unless CARLOS_ASSET_CACHE=1; |
| -// nil publishes no assets blocks and reads no manifests (edge asset cache |
| -// design §4.1). |
| +// nil, or a policy whose store has not opened (assetPolicy.active), |
| +// publishes no assets blocks and reads no manifests (edge asset cache design |
| +// §4.1). |
| func edgesFileWriteClaims(ctx context.Context, s release.Store, reg *registry.Registry, ident edgesFile, eip string, last *[]byte, refresh bool, claims *edgesClaims, assets *assetPolicy) error { |
| + // A policy whose box has not opened its asset store publishes nothing. |
| + assets = assets.active() |
| box := ident.Box |
| // The authority clocks are copied before any registry read (see |
| // assetClocks): the rows read below are then at least as new as the |
| @@ -952,7 +955,7 @@ func edgesFileLoop(reg *registry.Registry, assets *assetPolicy) { |
| // first pass. |
| assets.bindManifests(newAssetManifestCache(store)) |
| if assets != nil { |
| - log.Printf("agent: edges file publishes asset-cache blocks (CARLOS_ASSET_CACHE=1)") |
| + log.Printf("agent: edges file publishes asset-cache blocks once the asset store is open (CARLOS_ASSET_CACHE=1)") |
| } |
| log.Printf("agent: edges file loop armed (5min + jitter, and %s after a registry change, %s)", edgesFileWatchInterval, edgesFileKey(box)) |
| last := seedEdgesFileGate(ctx, store, box) |
| @@ -979,11 +982,15 @@ func edgesFileLoop(reg *registry.Registry, assets *assetPolicy) { |
| } else { |
| fpErrs = 0 |
| } |
| + // The policy as the writer sees it this tick: nil until the ready |
| + // hook has opened the store (assetPolicy.active). Opening it changes |
| + // the key and makes a rewrite due, so the first blocks follow at once. |
| + pub := assets.active() |
| key := fp |
| - if assets != nil && err == nil { |
| - key = edgesChangeKey(fp, assets.servingDigest()) |
| + if pub != nil && err == nil { |
| + key = edgesChangeKey(fp, pub.servingDigest()) |
| } |
| - if !state.dueWith(now, key, err == nil, assets.rewriteDue()) { |
| + if !state.dueWith(now, key, err == nil, pub.rewriteDue()) { |
| continue |
| } |
| if state.timed() { |
| @@ -994,7 +1001,7 @@ func edgesFileLoop(reg *registry.Registry, assets *assetPolicy) { |
| ident.Headroom = headroomFor(headroomCapabilities(ident.Capabilities), dataDir, reg) |
| ident = withMovesFields(ident, dataDir, optDir) |
| ident = withSeedsFields(ident, dataDir, optDir) |
| - err = edgesFileWriteClaims(ctx, store, reg, ident, eip, &last, state.timed(), claims, assets) |
| + err = edgesFileWriteClaims(ctx, store, reg, ident, eip, &last, state.timed(), claims, pub) |
| if err != nil { |
| // Throttled: a bucket that refuses every write (an IAM Deny, an |
| // outage) is retried on a backoff, and each retry must not be a |