region HA P1a Task 10: hostfiles SyncStats.Complete and Publisher.Gen
Complete is false for any TooLarge, NotRegular, Unreadable, Retry or Torn path (r2.8). Gen names the committed manifest as <seq>:<sha256 of its exact body>, the release marker's files_gen.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+110
−8
internal/hostfiles/hostfiles_test.go+29 −0internal/hostfiles/manifest.go+10 −4internal/hostfiles/publisher.go+22 −4internal/hostfiles/sync.go+12 −0internal/hostfiles/sync_unix_test.go+37 −0
diff --git a/internal/hostfiles/hostfiles_test.go b/internal/hostfiles/hostfiles_test.go| index 366d53543..e4e61e9c4 100644 |
| --- a/internal/hostfiles/hostfiles_test.go |
| +++ b/internal/hostfiles/hostfiles_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "context" |
| "encoding/json" |
| "errors" |
| + "fmt" |
| "os" |
| "path/filepath" |
| "reflect" |
| @@ -820,3 +821,31 @@ func TestFetchRefusesAnObjectWhoseLengthIsNotTheRecordedSize(t *testing.T) { |
| t.Fatalf("ReadPlatform err = %v, want a size mismatch", err) |
| } |
| } |
| + |
| +// Gen names the committed revision by its sequence number and the sha256 |
| +// of the exact manifest bytes in the store, after a commit and after a |
| +// reopen; with nothing committed it is "0:". |
| +func TestGenNamesTheCommittedManifest(t *testing.T) { |
| + ctx, s := context.Background(), release.NewDirStore(t.TempDir()) |
| + p := open(t, s) |
| + if g := p.Gen(); g != "0:" { |
| + t.Fatalf("no manifest: %q", g) |
| + } |
| + stage(t, p, "a.txt", "1") |
| + seq := commit(t, p) |
| + raw, _, err := s.Get(ctx, ManifestKey(pfx)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + want := fmt.Sprintf("%d:%s", seq, sum(raw)) |
| + if g := p.Gen(); g != want { |
| + t.Fatalf("after commit %q, want %q", g, want) |
| + } |
| + stage(t, p, "a.txt", "2") // staged, not committed: Gen is unchanged |
| + if g := p.Gen(); g != want { |
| + t.Fatalf("a staged change moved Gen: %q", g) |
| + } |
| + if g := open(t, s).Gen(); g != want { |
| + t.Fatalf("reopened %q, want %q", g, want) |
| + } |
| +} |
diff --git a/internal/hostfiles/manifest.go b/internal/hostfiles/manifest.go| index f451efe49..51ec0843c 100644 |
| --- a/internal/hostfiles/manifest.go |
| +++ b/internal/hostfiles/manifest.go |
| @@ -129,19 +129,25 @@ func Load(ctx context.Context, s release.Store, prefix string) (Manifest, bool, |
| } |
| func load(ctx context.Context, s release.Store, prefix string) (Manifest, string, bool, error) { |
| + m, _, etag, found, err := loadRaw(ctx, s, prefix) |
| + return m, etag, found, err |
| +} |
| + |
| +// loadRaw is load with the manifest's exact bytes, nil when there is none. |
| +func loadRaw(ctx context.Context, s release.Store, prefix string) (Manifest, []byte, string, bool, error) { |
| empty := Manifest{V: ManifestVersion, Entries: map[string]Entry{}, Platform: map[string]Entry{}} |
| raw, etag, err := s.Get(ctx, ManifestKey(prefix)) |
| if errors.Is(err, os.ErrNotExist) { |
| - return empty, "", false, nil |
| + return empty, nil, "", false, nil |
| } |
| if err != nil { |
| - return Manifest{}, "", false, err |
| + return Manifest{}, nil, "", false, err |
| } |
| m, err := decode(prefix, raw) |
| if err != nil { |
| - return Manifest{}, "", false, err |
| + return Manifest{}, nil, "", false, err |
| } |
| - return m, etag, true, nil |
| + return m, raw, etag, true, nil |
| } |
| // decode is a manifest from its exact bytes. |
diff --git a/internal/hostfiles/publisher.go b/internal/hostfiles/publisher.go| index 19f1f80ac..192fa074d 100644 |
| --- a/internal/hostfiles/publisher.go |
| +++ b/internal/hostfiles/publisher.go |
| @@ -37,13 +37,16 @@ type Publisher struct { |
| // carried. Every one of those Puts named the same etag, so at most one |
| // of them can be in storage. |
| inflight map[string]int64 |
| + // committed is the sha256 of the manifest body last committed, or read |
| + // at Open: the body behind man.Seq. "" while there is none. |
| + committed string |
| } |
| func Open(ctx context.Context, s release.Store, prefix string) (*Publisher, error) { |
| if err := checkPrefix(prefix); err != nil { |
| return nil, err |
| } |
| - m, etag, _, err := load(ctx, s, prefix) |
| + m, raw, etag, found, err := loadRaw(ctx, s, prefix) |
| if err != nil { |
| return nil, err |
| } |
| @@ -54,7 +57,11 @@ func Open(ctx context.Context, s release.Store, prefix string) (*Publisher, erro |
| if err := manifestWithinCap(m); err != nil { |
| return nil, fmt.Errorf("hostfiles: open %s: the manifest cannot be continued, an operator must replace it: %w", prefix, err) |
| } |
| - return &Publisher{s: s, prefix: prefix, man: m, etag: etag, next: m.Seq}, nil |
| + p := &Publisher{s: s, prefix: prefix, man: m, etag: etag, next: m.Seq} |
| + if found { |
| + p.committed = sum(raw) |
| + } |
| + return p, nil |
| } |
| // RestorableWithinCap is the check a caller makes before it starts work |
| @@ -102,7 +109,7 @@ func OpenAt(s release.Store, prefix string, body []byte, etag string) (*Publishe |
| if err := manifestWithinCap(m); err != nil { |
| return nil, fmt.Errorf("hostfiles: open %s: the manifest cannot be continued, an operator must replace it: %w", prefix, err) |
| } |
| - return &Publisher{s: s, prefix: prefix, man: m, etag: etag, next: m.Seq}, nil |
| + return &Publisher{s: s, prefix: prefix, man: m, etag: etag, next: m.Seq, committed: sum(body)}, nil |
| } |
| func sum(body []byte) string { |
| @@ -249,7 +256,7 @@ func (p *Publisher) Commit(ctx context.Context) (int64, error) { |
| } else if seq, ok := p.inflight[sum(cur)]; ok { |
| // An earlier attempt landed and later stages changed the body. |
| // Adopt it, then write the current body once on top of it. |
| - p.man.Seq, p.etag, p.inflight = seq, curEtag, nil |
| + p.man.Seq, p.etag, p.inflight, p.committed = seq, curEtag, nil, sum(cur) |
| etag, err = p.s.Put(ctx, ManifestKey(p.prefix), body, p.etag) |
| if errors.Is(err, release.ErrCASConflict) { |
| return 0, fmt.Errorf("%w: %s", ErrSuperseded, p.prefix) |
| @@ -268,9 +275,20 @@ func (p *Publisher) Commit(ctx context.Context) (int64, error) { |
| return 0, err |
| } |
| p.man.Seq, p.etag, p.dirty, p.inflight = out.Seq, etag, false, nil |
| + p.committed = sum(body) |
| return out.Seq, nil |
| } |
| +// Gen names the manifest revision last committed (or read at Open): |
| +// "<seq>:<sha256 hex of its exact body>". A region HA release marker |
| +// carries it as files_gen, and the keep decision compares it with the |
| +// local record's. With no manifest committed yet it is "0:". |
| +func (p *Publisher) Gen() string { |
| + p.mu.Lock() |
| + defer p.mu.Unlock() |
| + return fmt.Sprintf("%d:%s", p.man.Seq, p.committed) |
| +} |
| + |
| // SetOrigins sets the manifest's carried origin set (Manifest.Origins), |
| // sorted and deduplicated. The same set is not a change; a different one |
| // goes out with the next Commit. |
diff --git a/internal/hostfiles/sync.go b/internal/hostfiles/sync.go| index 65158cf8b..234fbf9c4 100644 |
| --- a/internal/hostfiles/sync.go |
| +++ b/internal/hostfiles/sync.go |
| @@ -39,6 +39,18 @@ type SyncStats struct { |
| Retry, Torn []string |
| } |
| +// Complete reports a pass that published the whole tree as it stood: no |
| +// file over the cap (TooLarge), nothing that is not a regular file |
| +// (NotRegular), nothing that could not be read (Unreadable), replaced |
| +// mid-look (Retry) or changed while it was read (Torn). Only a complete |
| +// final pass lets a region HA release marker say clean (r2.8): every |
| +// other one leaves something on this box that the manifest does not |
| +// faithfully hold. |
| +func (s SyncStats) Complete() bool { |
| + return len(s.TooLarge) == 0 && len(s.NotRegular) == 0 && len(s.Unreadable) == 0 && |
| + len(s.Retry) == 0 && len(s.Torn) == 0 |
| +} |
| + |
| // skipped reports whether rel, or any directory above it, is excluded. |
| func skipped(skip func(string, bool) bool, rel string, isDir bool) bool { |
| if skip == nil { |
diff --git a/internal/hostfiles/sync_unix_test.go b/internal/hostfiles/sync_unix_test.go| index 33006cc32..31060e5b5 100644 |
| --- a/internal/hostfiles/sync_unix_test.go |
| +++ b/internal/hostfiles/sync_unix_test.go |
| @@ -354,3 +354,40 @@ func TestPublishRefusesATornRead(t *testing.T) { |
| t.Fatalf("a manifest was committed: found = %v, err = %v", found, err) |
| } |
| } |
| + |
| +// Complete (region HA r2.8): a pass that left a file replaced mid-look |
| +// (Retry) or staged a torn image (Torn) is not complete, so no release |
| +// marker it feeds says clean. A clean pass is. |
| +func TestCompleteRefusesRetryAndTorn(t *testing.T) { |
| + ctx, s := context.Background(), release.NewDirStore(t.TempDir()) |
| + src := t.TempDir() |
| + write(t, src, "f", "v1") |
| + if st, err := SyncDir(ctx, open(t, s), src, nil); err != nil || !st.Complete() { |
| + t.Fatalf("a clean pass: %+v %v", st, err) |
| + } |
| + |
| + retry := t.TempDir() |
| + write(t, retry, "f", "v1") |
| + replaceAt(t, 1, retry) |
| + if st, err := SyncDir(ctx, open(t, s), retry, nil); err != nil || len(st.Retry) == 0 || st.Complete() { |
| + t.Fatalf("replaced mid-look: %+v %v", st, err) |
| + } |
| + betweenLookAndOpen = func() {} |
| + |
| + torn := t.TempDir() |
| + write(t, torn, "f", "v1") |
| + tearAt(t, torn) |
| + if st, err := SyncDir(ctx, open(t, s), torn, nil); err != nil || len(st.Torn) == 0 || st.Complete() { |
| + t.Fatalf("torn: %+v %v", st, err) |
| + } |
| + |
| + for name, st := range map[string]SyncStats{ |
| + "too large": {TooLarge: []string{"x"}}, |
| + "not regular": {NotRegular: []string{"x"}}, |
| + "unreadable": {Unreadable: []string{"x"}}, |
| + } { |
| + if st.Complete() { |
| + t.Errorf("%s: complete", name) |
| + } |
| + } |
| +} |