carlos / platform Public

region HA P1a Task 10: fix round 2 (the kept manifest in a file of its own beside the release record; the cadence limit stated)

- The manifest a release committed last is kept in
  <db>.release.files.json (read cap 32 MiB), not in the record, so the
  record stays small and a large manifest still keeps; a baseline that
  cannot be read costs one manifest GET, never the keep. setAside removes
  it. LocalRecord is back to Task 8's shape.
- Stated limit (ruling on finding 3): a platform publish may wait behind
  one app object upload in flight (Publisher.Stage, hostFiles.Run).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 212ebe2130ff80e40c4209bf213ef3270e89d7ff parent 1683ad0
11 files changed, +177 −63
  • internal/activator/backend_unit.go +2 −1
  • internal/activator/hafiles.go +72 −22
  • internal/activator/hafiles_test.go +22 −14
  • internal/activator/harecord.go +7 −1
  • internal/activator/harecord_linux.go +7 −4
  • internal/activator/harecord_other.go +2 −0
  • internal/activator/harelease.go +12 −7
  • internal/activator/harelease_keep_test.go +43 −0
  • internal/activator/hawake.go +2 −1
  • internal/hafresh/keep.go +0 −13
  • internal/hostfiles/publisher.go +8 −0

CI failed — run details

diff --git a/internal/activator/backend_unit.go b/internal/activator/backend_unit.go
index efd59cbcb..ddb1ddcd6 100644
--- a/internal/activator/backend_unit.go
+++ b/internal/activator/backend_unit.go
@@ -58,6 +58,7 @@ import (
"github.com/carlosframework/platform/internal/cgroup"
"github.com/carlosframework/platform/internal/hafresh"
+ "github.com/carlosframework/platform/internal/hostfiles"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
)
@@ -365,7 +366,7 @@ func (b *SplitBackend) haReleaseBase(rt registry.Route) hafresh.Marker {
}
return hafresh.Marker{V: 1}
}
-func (b *SplitBackend) writeHARecord(rt registry.Route, m hafresh.Marker, files *hafresh.FilesBaseline) error {
+func (b *SplitBackend) writeHARecord(rt registry.Route, m hafresh.Marker, files hostfiles.Baseline) error {
if r, ok := b.pick(rt).(haReleaser); ok {
return r.writeHARecord(rt, m, files)
}
diff --git a/internal/activator/hafiles.go b/internal/activator/hafiles.go
index 3f240f97e..c93543527 100644
--- a/internal/activator/hafiles.go
+++ b/internal/activator/hafiles.go
@@ -2,6 +2,7 @@ package activator
import (
"context"
+ "encoding/json"
"errors"
"fmt"
"io/fs"
@@ -11,7 +12,6 @@ import (
"sync"
"time"
- "github.com/carlosframework/platform/internal/hafresh"
"github.com/carlosframework/platform/internal/hostfiles"
"github.com/carlosframework/platform/internal/instancefs"
"github.com/carlosframework/platform/internal/registry"
@@ -102,23 +102,73 @@ func (b *ExecBackend) stageWakeFiles(rt registry.Route, base hostfiles.Baseline)
b.haWakes.setFiles(rt.Host, b.newHostFiles(rt, pub))
}
-// stageKeptFiles stages a kept wake's publisher on the manifest its local
-// release record carries (none: the prefix held no manifest), but only
-// when that is the revision the marker names (gen, which carries the
-// body's hash): the record is on ground the tenant can write, and the
-// marker is not. Anything else stages nothing, and the entry's open reads
-// the manifest.
-func (b *ExecBackend) stageKeptFiles(rt registry.Route, rec hafresh.LocalRecord, gen string) {
+// filesBaselinePath is where a release keeps the manifest it committed
+// last, beside its local record: <db>.release.files.json. It is its own
+// file, not part of the record, so the record stays small whatever the
+// manifest's size, and one that cannot be read costs a manifest read,
+// never a keep.
+func filesBaselinePath(p instancePaths) string { return p.dbPath + ".release.files.json" }
+
+// maxBaselineBytes bounds the baseline file's read: it is on ground the
+// tenant can write. A bigger manifest is read from the store instead.
+const maxBaselineBytes = 32 << 20
+
+// filesBaselineFile is the baseline file's form.
+type filesBaselineFile struct {
+ V int `json:"v"`
+ ETag string `json:"etag"`
+ Body []byte `json:"body"`
+}
+
+// writeFilesBaseline keeps base beside the release record, durably; with
+// no manifest (base.Found false) it removes any earlier one.
+func writeFilesBaseline(p instancePaths, base hostfiles.Baseline) error {
+ path := filesBaselinePath(p)
+ if !base.Found {
+ return removeIfThere(filepath.Dir(path), filepath.Base(path))
+ }
+ body, err := json.Marshal(filesBaselineFile{V: 1, ETag: base.ETag, Body: base.Body})
+ if err != nil {
+ return err
+ }
+ return writeFileDurably(path, body)
+}
+
+// readFilesBaseline is the baseline a release kept: Found false when it
+// kept none (the prefix held no manifest).
+func readFilesBaseline(p instancePaths) (hostfiles.Baseline, error) {
+ raw, err := readCappedFile(filesBaselinePath(p), maxBaselineBytes)
+ if errors.Is(err, fs.ErrNotExist) {
+ return hostfiles.Baseline{}, nil
+ }
+ if err != nil {
+ return hostfiles.Baseline{}, err
+ }
+ var f filesBaselineFile
+ if err := json.Unmarshal(raw, &f); err != nil || f.V != 1 {
+ return hostfiles.Baseline{}, fmt.Errorf("files baseline: not one this box wrote (%v)", err)
+ }
+ return hostfiles.Baseline{Found: true, Body: f.Body, ETag: f.ETag}, nil
+}
+
+// stageKeptFiles stages a kept wake's publisher on the manifest its
+// release kept beside the local record (none: the prefix held no
+// manifest), but only when that is the revision the marker names (gen,
+// which carries the body's hash): the file is on ground the tenant can
+// write, and the marker is not. Anything else stages nothing, and the
+// entry's open reads the manifest (one GET); the keep stands either way.
+func (b *ExecBackend) stageKeptFiles(rt registry.Route, gen string) {
if b.FilesStore == nil {
return
}
- base := hostfiles.Baseline{}
- if rec.Files != nil {
- base = hostfiles.Baseline{Found: true, Body: rec.Files.Body, ETag: rec.Files.ETag}
+ base, err := readFilesBaseline(b.pathsFor(rt))
+ if err != nil {
+ log.Printf("activator: %s: files: %v; the publisher reads the manifest again", rt.Host, err)
+ return
}
pub, err := hostfiles.OpenBaseline(b.FilesStore, b.filesPrefix(rt.Host), base)
if err != nil || pub.Gen() != gen {
- log.Printf("activator: %s: files: the release record's manifest is not the marker's (%v); the publisher reads it again", rt.Host, err)
+ log.Printf("activator: %s: files: the kept manifest is not the marker's (%v); the publisher reads it again", rt.Host, err)
return
}
b.haWakes.setFiles(rt.Host, b.newHostFiles(rt, pub))
@@ -157,7 +207,13 @@ func (b *ExecBackend) openHostFiles(ctx context.Context, rt registry.Route) (*ho
// Run publishes until ctx ends: the trio and the wake state every
// platformEvery when they changed, and the whole tree every snapshotEvery
// when its digest changed. A snapshot runs beside the platform passes,
-// one at a time, and Run waits for it before it returns. A failed pass is
+// one at a time, and Run waits for it before it returns.
+//
+// Stated limit (P1a): a platform pass can still wait behind one app object
+// upload the snapshot has in flight, since Publisher.Stage holds the
+// publisher's lock across its PUT. The delay is bounded by that one
+// upload, and the release's final publish is a complete pass, so a clean
+// marker is unaffected. A follow-up separates the uploads. A failed pass is
// logged and retried at the next tick; a superseded manifest (another
// writer) stops Run, and Final then fails, so the release is not clean.
func (f *hostFiles) Run(ctx context.Context) {
@@ -304,15 +360,9 @@ func (f *hostFiles) Final(ctx context.Context) (gen string, complete bool, err e
return f.pub.Gen(), complete, nil
}
-// baseline is the manifest the publisher last committed, as the local
-// release record keeps it: nil when there is none.
-func (f *hostFiles) baseline() *hafresh.FilesBaseline {
- b := f.pub.Baseline()
- if !b.Found {
- return nil
- }
- return &hafresh.FilesBaseline{Body: b.Body, ETag: b.ETag}
-}
+// baseline is the manifest the publisher last committed, as the release
+// keeps it beside its local record (writeFilesBaseline).
+func (f *hostFiles) baseline() hostfiles.Baseline { return f.pub.Baseline() }
// syncFiles syncs the files directory (skip as SyncDirAt takes it),
// opened beneath the instance directory without following a link: the
diff --git a/internal/activator/hafiles_test.go b/internal/activator/hafiles_test.go
index 2e7ed0996..7c79054f1 100644
--- a/internal/activator/hafiles_test.go
+++ b/internal/activator/hafiles_test.go
@@ -8,7 +8,6 @@ import (
"testing"
"time"
- "github.com/carlosframework/platform/internal/hafresh"
"github.com/carlosframework/platform/internal/hostfiles"
)
@@ -165,29 +164,38 @@ func TestPlatformPublishesDuringASnapshot(t *testing.T) {
}
}
-// A kept wake continues from the manifest its release record carries only
-// when that is the revision the marker names: a record whose bytes are not
-// (the tenant can write it) stages nothing, and the entry reads the
-// manifest instead.
+// A kept wake continues from the manifest its release kept beside the
+// record only when that is the revision the marker names: a file whose
+// bytes are not (the tenant can write it), or none at all, stages
+// nothing, and the entry reads the manifest instead.
func TestKeptFilesNeedTheMarkersManifest(t *testing.T) {
b, rt := haBackend(t, "shop.example.com")
+ p := b.pathsFor(rt)
+ if err := os.MkdirAll(p.dataDir, 0o700); err != nil {
+ t.Fatal(err)
+ }
publishManifest(t, b, rt, map[string]string{"a.txt": "1"}, "")
pub, err := hostfiles.Open(context.Background(), b.FilesStore, b.filesPrefix(rt.Host))
if err != nil {
t.Fatal(err)
}
base, gen := pub.Baseline(), pub.Gen()
- forged := append([]byte(nil), base.Body...)
- forged[len(forged)-2] = ' '
- b.stageKeptFiles(rt, hafresh.LocalRecord{Files: &hafresh.FilesBaseline{Body: forged, ETag: base.ETag}}, gen)
- if _, ok := b.wakeFiles(rt.Host); ok {
- t.Fatal("a record whose manifest is not the marker's staged a publisher")
+ forged := base
+ forged.Body = append([]byte(nil), base.Body...)
+ forged.Body[len(forged.Body)-2] = ' '
+ for name, kept := range map[string]hostfiles.Baseline{"forged": forged, "none": {}} {
+ if err := writeFilesBaseline(p, kept); err != nil {
+ t.Fatal(err)
+ }
+ b.stageKeptFiles(rt, gen)
+ if _, ok := b.wakeFiles(rt.Host); ok {
+ t.Fatalf("%s: a kept manifest that is not the marker's staged a publisher", name)
+ }
}
- b.stageKeptFiles(rt, hafresh.LocalRecord{}, gen)
- if _, ok := b.wakeFiles(rt.Host); ok {
- t.Fatal("a record with no manifest staged one for a marker that names one")
+ if err := writeFilesBaseline(p, base); err != nil {
+ t.Fatal(err)
}
- b.stageKeptFiles(rt, hafresh.LocalRecord{Files: &hafresh.FilesBaseline{Body: base.Body, ETag: base.ETag}}, gen)
+ b.stageKeptFiles(rt, gen)
if f, ok := b.wakeFiles(rt.Host); !ok || f.pub.Gen() != gen {
t.Fatal("the marker's own manifest was not staged")
}
diff --git a/internal/activator/harecord.go b/internal/activator/harecord.go
index 1a0819f04..0a1f9e5f4 100644
--- a/internal/activator/harecord.go
+++ b/internal/activator/harecord.go
@@ -41,7 +41,13 @@ func writeReleaseRecord(p instancePaths, host string, rec hafresh.LocalRecord) e
if err != nil {
return fmt.Errorf("release record: %w", err)
}
- path := recordPath(p, host)
+ return writeFileDurably(recordPath(p, host), body)
+}
+
+// writeFileDurably writes body at path as writeReleaseRecord does: a new
+// temporary file beside it (created exclusively), fsynced, renamed over
+// path, and the directory fsynced.
+func writeFileDurably(path string, body []byte) error {
dir := filepath.Dir(path)
f, err := os.CreateTemp(dir, ".release-*.tmp")
if err != nil {
diff --git a/internal/activator/harecord_linux.go b/internal/activator/harecord_linux.go
index 4edba977c..e2a037310 100644
--- a/internal/activator/harecord_linux.go
+++ b/internal/activator/harecord_linux.go
@@ -116,7 +116,10 @@ func writeField(h hash.Hash, b []byte) {
// readRecordFile reads the release record at path, never through a link
// and never blocking on a special file: it must be a regular file of at
// most maxRecordBytes. A missing record is fs.ErrNotExist.
-func readRecordFile(path string) ([]byte, error) {
+func readRecordFile(path string) ([]byte, error) { return readCappedFile(path, maxRecordBytes) }
+
+// readCappedFile reads path as readRecordFile does, with max as its cap.
+func readCappedFile(path string, max int) ([]byte, error) {
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_NOFOLLOW|unix.O_NONBLOCK|unix.O_CLOEXEC, 0)
if err != nil {
return nil, &fs.PathError{Op: "open", Path: path, Err: err}
@@ -130,12 +133,12 @@ func readRecordFile(path string) ([]byte, error) {
if !fi.Mode().IsRegular() {
return nil, fmt.Errorf("%s: not a regular file", path)
}
- b, err := io.ReadAll(io.LimitReader(f, maxRecordBytes+1))
+ b, err := io.ReadAll(io.LimitReader(f, int64(max)+1))
if err != nil {
return nil, err
}
- if len(b) > maxRecordBytes {
- return nil, fmt.Errorf("%s: larger than %d bytes", path, maxRecordBytes)
+ if len(b) > max {
+ return nil, fmt.Errorf("%s: larger than %d bytes", path, max)
}
return b, nil
}
diff --git a/internal/activator/harecord_other.go b/internal/activator/harecord_other.go
index 96c4d3221..e81b88c6c 100644
--- a/internal/activator/harecord_other.go
+++ b/internal/activator/harecord_other.go
@@ -17,3 +17,5 @@ func statFileID(string) (hafresh.FileID, error) { return hafresh.FileID{}, errNo
func treeDigest(string, string) (string, error) { return "", errNoKeepProof }
func readRecordFile(string) ([]byte, error) { return nil, errNoKeepProof }
+
+func readCappedFile(string, int) ([]byte, error) { return nil, errNoKeepProof }
diff --git a/internal/activator/harelease.go b/internal/activator/harelease.go
index 6650e8980..57725d68f 100644
--- a/internal/activator/harelease.go
+++ b/internal/activator/harelease.go
@@ -9,6 +9,7 @@ import (
"time"
"github.com/carlosframework/platform/internal/hafresh"
+ "github.com/carlosframework/platform/internal/hostfiles"
"github.com/carlosframework/platform/internal/instancectl"
"github.com/carlosframework/platform/internal/lease"
"github.com/carlosframework/platform/internal/registry"
@@ -40,7 +41,7 @@ const (
type haReleaser interface {
hostSliceEmpty(rt registry.Route) (bool, error)
haReleaseBase(rt registry.Route) hafresh.Marker
- writeHARecord(rt registry.Route, m hafresh.Marker, files *hafresh.FilesBaseline) error
+ writeHARecord(rt registry.Route, m hafresh.Marker, files hostfiles.Baseline) error
}
// hostSliceEmpty reads the host slice's cgroup.events
@@ -82,15 +83,16 @@ func (b *ExecBackend) haReleaseBase(rt registry.Route) hafresh.Marker {
}
// writeHARecord writes the host's local release record for m: the marker,
-// the database, WAL and files tree as they are now (readLocalNow), and
-// files, the manifest the release committed last (a keep continues from
-// it without reading it, stageKeptFiles). A
+// the database, WAL and files tree as they are now (readLocalNow); and,
+// beside it in a file of its own, files, the manifest the release
+// committed last (a keep continues from it without reading it,
+// stageKeptFiles; a failure there is logged and costs only that read). A
// clean release first checkpoints the WAL into the database and truncates
// it (lease.TruncateWAL; every writer is proven stopped and the replica
// closed by now), so the record can show it empty, as a keep needs. A
// checkpoint that fails is logged and the record says the WAL is not
// empty: that copy is never kept.
-func (b *ExecBackend) writeHARecord(rt registry.Route, m hafresh.Marker, files *hafresh.FilesBaseline) error {
+func (b *ExecBackend) writeHARecord(rt registry.Route, m hafresh.Marker, files hostfiles.Baseline) error {
p := b.pathsFor(rt)
if m.Clean {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
@@ -104,7 +106,10 @@ func (b *ExecBackend) writeHARecord(rt registry.Route, m hafresh.Marker, files *
if err != nil {
return err
}
- return writeReleaseRecord(p, rt.Host, hafresh.LocalRecord{Marker: m, DB: now.DB, WALEmpty: now.WALEmpty, TreeDigest: now.TreeDigest, Files: files})
+ if err := writeFilesBaseline(p, files); err != nil {
+ log.Printf("activator: %s: keep the files manifest beside the release record: %v (a keep reads it again)", rt.Host, err)
+ }
+ return writeReleaseRecord(p, rt.Host, hafresh.LocalRecord{Marker: m, DB: now.DB, WALEmpty: now.WALEmpty, TreeDigest: now.TreeDigest})
}
// haEntryRep is e's replica as an HA one, if it is.
@@ -151,7 +156,7 @@ func (a *Service) releaseHAClean(ctx context.Context, e *actEntry) error {
f := e.files
a.mu.Unlock()
gen, complete, ferr := "", false, errors.New("the entry has no files publisher")
- var files *hafresh.FilesBaseline
+ var files hostfiles.Baseline
if f != nil {
// A budget of its own: a large tree's final read must not eat the
// final sync's.
diff --git a/internal/activator/harelease_keep_test.go b/internal/activator/harelease_keep_test.go
new file mode 100644
index 000000000..400cc742b
--- /dev/null
+++ b/internal/activator/harelease_keep_test.go
@@ -0,0 +1,43 @@
+package activator
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "strings"
+ "testing"
+)
+
+// A manifest well past the release record's own read cap (64 KiB) still
+// keeps: the record stays small, the manifest is kept in a file of its
+// own beside it, and the kept wake continues from it with no read: GET
+// and PUT on the lock, nothing else.
+func TestLargeManifestReleaseThenKeep(t *testing.T) {
+ s, e := haServiceEntry(t, "shop.example.com")
+ files := map[string]string{}
+ for i := 0; i < 600; i++ {
+ files[fmt.Sprintf("upload-%04d-%s.txt", i, strings.Repeat("x", 40))] = fmt.Sprint(i)
+ }
+ writeFiles(t, s.b, s.rt, files)
+ s.stopForTest(e)
+ if v := s.lockView(t); !s.stoppedFor(e) || v.Lock.Release == nil || !v.Lock.Release.Clean {
+ t.Fatalf("want a clean release: %+v", v.Lock.Release)
+ }
+ p := s.b.pathsFor(s.rt)
+ base, err := readFilesBaseline(p)
+ if err != nil || !base.Found || len(base.Body) <= maxRecordBytes {
+ t.Fatalf("the kept manifest is %d bytes (%v); the test needs one over %d", len(base.Body), err, maxRecordBytes)
+ }
+ if fi, err := os.Stat(recordPath(p, s.rt.Host)); err != nil || fi.Size() > maxRecordBytes {
+ t.Fatalf("the release record: %v, %v", fi, err)
+ }
+ lock := s.lockKey()
+ n := len(s.fx.Requests())
+ if _, err := s.wakeForTest(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ reqs := s.fx.Requests()[n:]
+ if restoreCalls(s.b) != 0 || strings.Join(reqs, ",") != "GET "+lock+",PUT "+lock {
+ t.Fatalf("a kept wake with a large manifest made %v (%d restores)", reqs, restoreCalls(s.b))
+ }
+}
diff --git a/internal/activator/hawake.go b/internal/activator/hawake.go
index 5f52c048f..ff91867ed 100644
--- a/internal/activator/hawake.go
+++ b/internal/activator/hawake.go
@@ -436,7 +436,7 @@ func (b *ExecBackend) restoreHA(ctx context.Context, rt registry.Route, v hafres
}
if keep {
log.Printf("activator: %s: kept local", host)
- b.stageKeptFiles(rt, now.Record, v.Lock.Release.FilesGen)
+ b.stageKeptFiles(rt, v.Lock.Release.FilesGen)
return false, nil
}
if err := b.setAside(p, host); err != nil {
@@ -511,6 +511,7 @@ func (b *ExecBackend) setAside(p instancePaths, host string) error {
func() error { return renameIfThere(dbDir, dbName+"-wal", dbName+staleSuffix+"-wal") },
func() error { return removeIfThere(dbDir, dbName+"-shm") },
func() error { return removeIfThere(dbDir, filepath.Base(recordPath(p, host))) },
+ func() error { return removeIfThere(dbDir, filepath.Base(filesBaselinePath(p))) },
func() error { return removeIfThere(dbDir, dbName+SeedMarkerSuffix) },
func() error { return removeAllIfThere(p.dataDir, host+staleSuffix) },
func() error { return renameIfThere(p.dataDir, host, host+staleSuffix) },
diff --git a/internal/hafresh/keep.go b/internal/hafresh/keep.go
index a75c0e5c0..d13cc0a8c 100644
--- a/internal/hafresh/keep.go
+++ b/internal/hafresh/keep.go
@@ -20,19 +20,6 @@ type LocalRecord struct {
DB FileID `json:"db"`
WALEmpty bool `json:"wal_empty"`
TreeDigest string `json:"tree_digest"`
- // Files is the files manifest the release committed last (the one
- // Marker.FilesGen names), so a wake that keeps this copy continues
- // publishing from it without reading it again. Nil when no manifest
- // was committed. It decides nothing here: the caller trusts it only
- // when its hash is the one FilesGen carries.
- Files *FilesBaseline `json:"files,omitempty"`
-}
-
-// FilesBaseline is a files manifest's exact bytes and the ETag the store
-// answered for them.
-type FilesBaseline struct {
- Body []byte `json:"body"`
- ETag string `json:"etag"`
}
// LocalNow is the local state as it is now, read at wake: the local record
diff --git a/internal/hostfiles/publisher.go b/internal/hostfiles/publisher.go
index 1810a8b5a..77078de7e 100644
--- a/internal/hostfiles/publisher.go
+++ b/internal/hostfiles/publisher.go
@@ -177,6 +177,14 @@ func (p *Publisher) put(ctx context.Context, body []byte, mode uint32, torn bool
// Stage records path's content. Content identical to what is already
// staged or committed takes no sequence number and writes nothing.
+//
+// Stated limit (region HA P1a): Stage holds the publisher's lock across
+// its object PUT, so every other call on the publisher (a platform item's
+// stage, a commit) waits behind an upload already in flight. A region HA
+// host's one-second platform publication can therefore be late by at
+// most one app object upload. The final publish at release still makes a
+// complete pass, so a clean marker is unaffected. A follow-up separates
+// the uploads from the lock.
func (p *Publisher) Stage(ctx context.Context, path string, body []byte, mode uint32, torn bool) error {
clean, err := CleanPath(path)
if err != nil {