layout: HEAD format version 1 with a verbatim ext; manifest sizes as v1.1 fields

- version first (absent: v1.0, strict as before); every known version is
  strict at the top level, later fields live in ext, kept raw and written
  back verbatim, so the canonical-bytes check holds
- a HEAD of a newer version is refused as ErrHeadNewerVersion, not
  ErrMalformed; a v1.0 HEAD carrying v1.1 fields is malformed
- CurrentBytes (Current's manifest size) and a snapshot's ManifestBytes,
  written by the commit that knows the size (which makes HEAD v1.1); a
  snapshot's size is written once
- the HEAD write census gains version, current_bytes and ext

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-30 23:30:43 -08:00
1 parent 13d0627043
commit ac85a9dc2b
5 files changed
+249 -19

No files matched your search

@@ -58,6 +58,10 @@ type CommitRequest struct {
// Delta, when set, is the commit by change: Prev and Next are unused,
// and the plan builds and checks only the changed shards and the root.
Delta *CommitDelta
// ManifestBytes, when set, is the size of the committed root's manifest
// (the root and every segment it names): HEAD's CurrentBytes after the
// commit, and the named snapshot's size. nil writes neither (unknown).
ManifestBytes *uint64
}
// CommitDelta is a commit by change (cost table K3): HEAD's current root,
@@ -447,7 +451,27 @@ func commitChecked(ctx context.Context, h Head, os ObjectStore, vol string, me H
return Head{}, false, err
}
}
return h.Commit(me, req.Session, plan.root, req.Position, req.Snapshot, req.Meta, req.State)
next, changed, err := h.Commit(me, req.Session, plan.root, req.Position, req.Snapshot, req.Meta, req.State)
if err == nil && changed {
next = next.withManifestBytes(req.Snapshot, req.ManifestBytes)
}
return next, changed, err
}
// withManifestBytes records the committed root's manifest size: HEAD's
// CurrentBytes (nil: unknown, also when an earlier commit knew it), and the
// size of the snapshot this commit named, written once.
func (h Head) withManifestBytes(snapshot string, size *uint64) Head {
h = h.clone() // the snapshot map is shared with the HEAD it came from
if size != nil && h.Version < HeadVersion1 {
h.Version = HeadVersion1 // the sizes are v1.1 fields
}
h.CurrentBytes = cloneSize(size)
if s, ok := h.Snapshots[snapshot]; snapshot != "" && ok && s.ManifestBytes == nil {
s.ManifestBytes = cloneSize(size)
h.Snapshots[snapshot] = s
}
return h
}
// commitVerifyParallel bounds the object lookups one commit runs at once.
@@ -15,8 +15,11 @@
"current": "Head.Current (including nil)",
"position": "Head.Position",
"state": "Head.State",
"snapshots": "Snapshot name membership and each entry Root, Position, Meta; excludes Dependents of retained entries",
"snapshots[].Dependents": "Dependents of retained snapshot entries, including target name, target id and root; excludes snapshot membership and other snapshot fields"
"snapshots": "Snapshot name membership and each entry Root, Position, Meta, ManifestBytes (written once, when the entry is named); excludes Dependents of retained entries",
"snapshots[].Dependents": "Dependents of retained snapshot entries, including target name, target id and root; excludes snapshot membership and other snapshot fields",
"version": "Head.Version (0 absent: the v1.0 format; 1: v1.1)",
"current_bytes": "Head.CurrentBytes, the manifest size of Current (including nil: unknown)",
"ext": "Head.Ext, the v1.1 extension object, written back verbatim by every writer that does not own an entry in it"
},
"notes": [
"snapshots removal includes removal of its empty dependent list; snapshots with dependents cannot be removed.",
@@ -40,7 +43,10 @@
"position",
"state",
"snapshots",
"snapshots[].Dependents"
"snapshots[].Dependents",
"version",
"current_bytes",
"ext"
]
},
{
@@ -90,7 +96,9 @@
"position",
"activity",
"state",
"snapshots"
"snapshots",
"version",
"current_bytes"
]
},
{
@@ -99,7 +107,9 @@
"may_change": [
"current",
"activity",
"snapshots"
"snapshots",
"version",
"current_bytes"
]
},
{
@@ -178,7 +188,10 @@
"position",
"state",
"snapshots",
"snapshots[].Dependents"
"snapshots[].Dependents",
"version",
"current_bytes",
"ext"
]
}
]
@@ -49,6 +49,11 @@ type Snapshot struct {
Position uint64
Meta SnapshotMeta
Dependents []Dependent
// ManifestBytes is the size of the snapshot's manifest (its root and
// every segment the root names), written once by the commit that named
// it. nil: unknown (a snapshot named before the field, or by a commit
// that did not know the size); never reported as 0.
ManifestBytes *uint64
}
// The retention classes of a snapshot.
@@ -151,9 +156,35 @@ type Head struct {
Position uint64
Snapshots map[string]Snapshot
State string // StateLive, StateDetached, StateDeleting or StateDeleted
// CurrentBytes is the manifest size of Current (its root and every
// segment it names), kept by each commit (nil: unknown). A commit by
// change updates it from the objects it replaces, with no lookups.
CurrentBytes *uint64
// Version is HEAD's format version: 0 (absent in the bytes) is the v1.0
// format, decoded strictly; HeadVersion1 is v1.1, whose own fields are
// CurrentBytes and snapshots' ManifestBytes, and whose Ext carries every
// later field. A writer keeps the version and Ext it read.
Version int
// Ext is a v1.1 HEAD's extension object, kept as raw bytes and written
// back verbatim (the ext contract: only additive facts that a writer
// keeping them verbatim while it changes the known fields cannot make
// wrong; anything an older writer could break bumps Version instead).
Ext json.RawMessage
}
// HeadVersion1 is the v1.1 HEAD format; MaxHeadVersion the newest this
// binary reads.
const (
HeadVersion1 = 1
MaxHeadVersion = HeadVersion1
)
// ErrHeadNewerVersion refuses a HEAD written by a newer format version:
// not damage, a binary too old to read it.
var ErrHeadNewerVersion = errors.New("layout: HEAD written by a newer version")
type headDoc struct {
Version int `json:"version,omitempty"`
VolumeID string `json:"volume_id"`
Epoch uint64 `json:"epoch"`
Holder string `json:"holder"`
@@ -161,8 +192,10 @@ type headDoc struct {
Activity Activity `json:"activity"`
Current string `json:"current,omitempty"`
Position uint64 `json:"position"`
Snapshots map[string]snapDoc `json:"snapshots,omitempty"`
State string `json:"state,omitempty"`
Snapshots map[string]snapDoc `json:"snapshots,omitempty"`
State string `json:"state,omitempty"`
CurrentBytes *uint64 `json:"current_bytes,omitempty"`
Ext json.RawMessage `json:"ext,omitempty"`
}
type snapDoc struct {
@@ -172,7 +205,8 @@ type snapDoc struct {
Class string `json:"class,omitempty"`
ExpiresNs int64 `json:"expires_ns,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Dependents []depDoc `json:"dependents,omitempty"`
Dependents []depDoc `json:"dependents,omitempty"`
ManifestBytes *uint64 `json:"manifest_bytes,omitempty"`
}
type depDoc struct {
@@ -294,14 +328,14 @@ func (h Head) Encode() ([]byte, error) {
if err := h.check(); err != nil {
return nil, err
}
d := headDoc{VolumeID: h.VolumeID.String(), Epoch: h.Epoch, Holder: h.Holder, ClaimID: h.ClaimID, Activity: h.Activity, Position: h.Position, State: h.State}
d := headDoc{Version: h.Version, VolumeID: h.VolumeID.String(), Epoch: h.Epoch, Holder: h.Holder, ClaimID: h.ClaimID, Activity: h.Activity, Position: h.Position, State: h.State, CurrentBytes: h.CurrentBytes, Ext: h.Ext}
if h.Current != nil {
d.Current = h.Current.String()
}
if len(h.Snapshots) > 0 {
d.Snapshots = map[string]snapDoc{}
for n, s := range h.Snapshots {
sd := snapDoc{Root: s.Root.String(), Position: s.Position, CreatedNs: s.Meta.CreatedNs, Class: s.Meta.Class, ExpiresNs: s.Meta.ExpiresNs}
sd := snapDoc{Root: s.Root.String(), Position: s.Position, CreatedNs: s.Meta.CreatedNs, Class: s.Meta.Class, ExpiresNs: s.Meta.ExpiresNs, ManifestBytes: s.ManifestBytes}
if len(s.Meta.Labels) > 0 {
sd.Labels = s.Meta.Labels
}
@@ -327,17 +361,36 @@ func DecodeHead(b []byte, gen uint64, volume VolumeID) (Head, error) {
if volume.IsZero() {
return Head{}, errors.New("layout: DecodeHead needs the volume's id from its VOLUME")
}
// The version first, read leniently: a newer version is refused as
// newer, never as malformed. Every known version is then decoded
// strictly (unknown top-level fields refused; v1.1's later fields live
// in ext, kept raw).
var probe struct {
Version int `json:"version"`
}
if err := json.Unmarshal(b, &probe); err != nil {
return Head{}, fmt.Errorf("%w: HEAD: %v", ErrMalformed, err)
}
if probe.Version > MaxHeadVersion {
return Head{}, fmt.Errorf("%w: version %d, this binary reads up to %d", ErrHeadNewerVersion, probe.Version, MaxHeadVersion)
}
if probe.Version < 0 {
return Head{}, fmt.Errorf("%w: HEAD version %d", ErrMalformed, probe.Version)
}
var d headDoc
dec := json.NewDecoder(bytes.NewReader(b))
dec.DisallowUnknownFields()
if err := dec.Decode(&d); err != nil {
return Head{}, fmt.Errorf("%w: HEAD: %v", ErrMalformed, err)
}
if d.Version == 0 && (d.CurrentBytes != nil || d.Ext != nil || snapshotsSized(d.Snapshots)) {
return Head{}, fmt.Errorf("%w: HEAD of the v1.0 format carries v1.1 fields", ErrMalformed)
}
vid, err := ParseVolumeID(d.VolumeID)
if err != nil {
return Head{}, err
}
h := Head{Gen: gen, VolumeID: vid, Epoch: d.Epoch, Holder: d.Holder, ClaimID: d.ClaimID, Activity: d.Activity, Position: d.Position, State: d.State}
h := Head{Gen: gen, VolumeID: vid, Epoch: d.Epoch, Holder: d.Holder, ClaimID: d.ClaimID, Activity: d.Activity, Position: d.Position, State: d.State, CurrentBytes: d.CurrentBytes, Version: d.Version, Ext: d.Ext}
if d.Current != "" {
rn, err := ParseRootName(d.Current)
if err != nil {
@@ -352,7 +405,7 @@ func DecodeHead(b []byte, gen uint64, volume VolumeID) (Head, error) {
if err != nil {
return Head{}, err
}
snap := Snapshot{Root: rn, Position: s.Position, Meta: SnapshotMeta{CreatedNs: s.CreatedNs, Class: s.Class, ExpiresNs: s.ExpiresNs, Labels: s.Labels}}
snap := Snapshot{Root: rn, Position: s.Position, Meta: SnapshotMeta{CreatedNs: s.CreatedNs, Class: s.Class, ExpiresNs: s.ExpiresNs, Labels: s.Labels}, ManifestBytes: s.ManifestBytes}
for _, dep := range s.Dependents {
tid, err := ParseVolumeID(dep.TargetID)
if err != nil {
@@ -453,15 +506,36 @@ func (h Head) clone() Head {
rn := *h.Current
c.Current = &rn
}
c.CurrentBytes = cloneSize(h.CurrentBytes)
if h.Ext != nil {
c.Ext = append(json.RawMessage(nil), h.Ext...)
}
if h.Snapshots != nil {
c.Snapshots = make(map[string]Snapshot, len(h.Snapshots))
for n, s := range h.Snapshots {
c.Snapshots[n] = Snapshot{Root: s.Root, Position: s.Position, Meta: s.Meta.clone(), Dependents: append([]Dependent(nil), s.Dependents...)}
c.Snapshots[n] = Snapshot{Root: s.Root, Position: s.Position, Meta: s.Meta.clone(), Dependents: append([]Dependent(nil), s.Dependents...), ManifestBytes: cloneSize(s.ManifestBytes)}
}
}
return c
}
func snapshotsSized(snaps map[string]snapDoc) bool {
for _, s := range snaps {
if s.ManifestBytes != nil {
return true
}
}
return false
}
func cloneSize(p *uint64) *uint64 {
if p == nil {
return nil
}
v := *p
return &v
}
// ErrPrecondition is returned by HeadStore.Update when HEAD is not at the
// expected generation.
var ErrPrecondition = errors.New("layout: HEAD generation moved")
@@ -31,13 +31,13 @@ func TestMachineReadableHeadCensusNamesExactlyTheTypedCauses(t *testing.T) {
if census.SchemaVersion != 1 {
t.Fatalf("schema=%d", census.SchemaVersion)
}
observable := map[string]bool{"volume_id": true, "epoch": true, "holder": true, "claim_id": true, "activity": true, "current": true, "position": true, "state": true, "snapshots": true, "snapshots[].Dependents": true}
observable := map[string]bool{"volume_id": true, "epoch": true, "holder": true, "claim_id": true, "activity": true, "current": true, "position": true, "state": true, "snapshots": true, "snapshots[].Dependents": true, "version": true, "current_bytes": true, "ext": true}
for field := range census.FieldGroups {
if !observable[field] {
t.Fatalf("unobservable census field %s", field)
}
}
headFields := map[string]bool{"Gen": true, "VolumeID": true, "Epoch": true, "Holder": true, "ClaimID": true, "Activity": true, "Current": true, "Position": true, "Snapshots": true, "State": true}
headFields := map[string]bool{"Gen": true, "VolumeID": true, "Epoch": true, "Holder": true, "ClaimID": true, "Activity": true, "Current": true, "Position": true, "Snapshots": true, "State": true, "Version": true, "CurrentBytes": true, "Ext": true}
typ := reflect.TypeOf(Head{})
for n := 0; n < typ.NumField(); n++ {
if !headFields[typ.Field(n).Name] {
@@ -83,8 +83,8 @@ func TestMachineReadableHeadCensusNamesExactlyTheTypedCauses(t *testing.T) {
func changedCensusGroups(before, after Head) []string {
var out []string
a, b := reflect.ValueOf(before), reflect.ValueOf(after)
jsonKeys := map[string]string{"VolumeID": "volume_id", "Epoch": "epoch", "Holder": "holder", "ClaimID": "claim_id", "Activity": "activity", "Current": "current", "Position": "position", "State": "state"}
for _, name := range []string{"VolumeID", "Epoch", "Holder", "ClaimID", "Activity", "Current", "Position", "State"} {
jsonKeys := map[string]string{"VolumeID": "volume_id", "Epoch": "epoch", "Holder": "holder", "ClaimID": "claim_id", "Activity": "activity", "Current": "current", "Position": "position", "State": "state", "Version": "version", "CurrentBytes": "current_bytes", "Ext": "ext"}
for _, name := range []string{"VolumeID", "Epoch", "Holder", "ClaimID", "Activity", "Current", "Position", "State", "Version", "CurrentBytes", "Ext"} {
if !reflect.DeepEqual(a.FieldByName(name).Interface(), b.FieldByName(name).Interface()) {
out = append(out, jsonKeys[name])
}
@@ -0,0 +1,119 @@
package layout
import (
"bytes"
"encoding/json"
"errors"
"strings"
"testing"
)
func u64(v uint64) *uint64 { return &v }
// A v1.1 HEAD (version 1) carries its sizes and an ext object kept as raw
// bytes: it decodes, and encodes back to the same bytes (the canonical
// check holds with ext re-emitted verbatim). A v1.0 HEAD's bytes are
// unchanged: no version field is written when the version is 0.
func TestHeadVersion1RoundTripsWithItsExtVerbatim(t *testing.T) {
root := RootName{Hash: Sum([]byte("image"))}
h := idleHead()
h.Current, h.Position = &root, 9
h.Version, h.CurrentBytes = HeadVersion1, u64(4096)
h.Ext = json.RawMessage(`{"later":{"a":1}}`)
h.Snapshots = map[string]Snapshot{"s1": {Root: root, Position: 9, ManifestBytes: u64(4096)}}
b, err := h.Encode()
if err != nil {
t.Fatal(err)
}
if !bytes.HasPrefix(b, []byte(`{"version":1,`)) {
t.Fatalf("a v1.1 HEAD does not start with its version: %s", b)
}
back, err := DecodeHead(b, h.Gen, h.VolumeID)
if err != nil {
t.Fatal(err)
}
if back.Version != HeadVersion1 || back.CurrentBytes == nil || *back.CurrentBytes != 4096 || string(back.Ext) != string(h.Ext) {
t.Fatalf("decoded %+v", back)
}
if s := back.Snapshots["s1"]; s.ManifestBytes == nil || *s.ManifestBytes != 4096 {
t.Fatalf("snapshot size lost: %+v", s)
}
again, err := back.Encode()
if err != nil || !bytes.Equal(again, b) {
t.Fatalf("v1.1 HEAD not canonical after a round trip: %s / %s (%v)", again, b, err)
}
v0 := idleHead()
b0, err := v0.Encode()
if err != nil {
t.Fatal(err)
}
if bytes.Contains(b0, []byte(`"version"`)) || bytes.Contains(b0, []byte(`"ext"`)) || bytes.Contains(b0, []byte(`bytes"`)) {
t.Fatalf("a v1.0 HEAD's bytes changed: %s", b0)
}
}
// Decoding by version: a HEAD of a newer version is refused as newer
// (ErrHeadNewerVersion), never as damage; a v1.0 HEAD (no version) that
// carries a v1.1 field is malformed; a known version still refuses an
// unknown top-level field (later fields go in ext).
func TestHeadDecodeByVersion(t *testing.T) {
root := RootName{Hash: Sum([]byte("image"))}
h := idleHead()
h.Current, h.Position = &root, 9
h.Version, h.CurrentBytes = HeadVersion1, u64(10)
b, err := h.Encode()
if err != nil {
t.Fatal(err)
}
newer := bytes.Replace(b, []byte(`{"version":1,`), []byte(`{"version":2,`), 1)
if _, err := DecodeHead(newer, 1, h.VolumeID); !errors.Is(err, ErrHeadNewerVersion) || errors.Is(err, ErrMalformed) {
t.Fatalf("a newer HEAD: %v; want ErrHeadNewerVersion, not ErrMalformed", err)
}
v0WithSize := bytes.Replace(b, []byte(`{"version":1,`), []byte(`{`), 1)
if _, err := DecodeHead(v0WithSize, 1, h.VolumeID); !errors.Is(err, ErrMalformed) {
t.Fatalf("a v1.0 HEAD with a v1.1 field: %v; want ErrMalformed", err)
}
unknownTop := bytes.Replace(b, []byte(`{"version":1,`), []byte(`{"version":1,"surprise":1,`), 1)
if _, err := DecodeHead(unknownTop, 1, h.VolumeID); !errors.Is(err, ErrMalformed) {
t.Fatalf("an unknown top-level field in a v1.1 HEAD: %v; want ErrMalformed (later fields live in ext)", err)
}
if !strings.Contains(ErrHeadNewerVersion.Error(), "newer version") {
t.Fatal("the newer-version refusal does not say so")
}
}
// A commit that knows its manifest size writes it as HEAD's CurrentBytes and
// as the size of the snapshot it names, and makes HEAD v1.1. A commit that
// does not know it writes neither and leaves a v1.0 HEAD at v1.0. A replay
// changes nothing.
func TestCommitRecordsTheManifestSize(t *testing.T) {
root := RootName{Hash: Sum([]byte("image"))}
open := func() Head {
h := idleHead()
h.State = StateLive
h.Activity = Activity{Kind: ActSnap, Session: "s"}
return h
}
me := HolderID{Holder: "srv-a", Epoch: 3, ClaimID: "k-a"}
next, changed, err := open().Commit(me, "s", root, 9, "snap", SnapshotMeta{}, StateLive)
if err != nil || !changed {
t.Fatal(err)
}
sized := next.withManifestBytes("snap", u64(777))
if sized.Version != HeadVersion1 || sized.CurrentBytes == nil || *sized.CurrentBytes != 777 {
t.Fatalf("sized commit: %+v", sized)
}
if s := sized.Snapshots["snap"]; s.ManifestBytes == nil || *s.ManifestBytes != 777 {
t.Fatalf("the named snapshot's size: %+v", s)
}
unsized := next.withManifestBytes("snap", nil)
if unsized.Version != 0 || unsized.CurrentBytes != nil || unsized.Snapshots["snap"].ManifestBytes != nil {
t.Fatalf("a commit that does not know the size: %+v", unsized)
}
// Written once: a later size never rewrites a snapshot's.
again := sized.withManifestBytes("snap", u64(999))
if s := again.Snapshots["snap"]; *s.ManifestBytes != 777 {
t.Fatalf("a snapshot's size was rewritten: %d", *s.ManifestBytes)
}
}