mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
chunkstore: keep an unchanged touched segment in the size; ext is a canonical object
A commit by change subtracts only the touched segments its new root no longer names: a shard whose segment came out unchanged stays in the size. HEAD's ext must be a JSON object in its canonical bytes, on encode and decode, so it round-trips byte for byte. The format doc states the upgrade order (version-aware readers before any v1.1 writer) and that the fullest-HEAD figure is a sizing check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e4e34c26a6
commit
b84f55ba3e
5 files changed
+103
-5
No files matched your search
@@ -225,6 +225,26 @@ func validName(field, v string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkExt refuses an ext that is not a JSON object in its canonical bytes
|
||||
// (what Encode writes: compact, HTML-escaped), or one on a v1.0 HEAD. Ext
|
||||
// is carried as raw bytes and written back unchanged, so only canonical
|
||||
// bytes round-trip byte for byte.
|
||||
func checkExt(version int, ext json.RawMessage) error {
|
||||
if ext == nil {
|
||||
return nil
|
||||
}
|
||||
if version == 0 {
|
||||
return fmt.Errorf("%w: a v1.0 HEAD carries ext", ErrMalformed)
|
||||
}
|
||||
if len(ext) == 0 || ext[0] != '{' || !json.Valid(ext) {
|
||||
return fmt.Errorf("%w: HEAD ext is not a JSON object", ErrMalformed)
|
||||
}
|
||||
if canon, err := json.Marshal(ext); err != nil || !bytes.Equal(canon, ext) {
|
||||
return fmt.Errorf("%w: HEAD ext is not in its canonical bytes", ErrMalformed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ErrNoNextPosition: a HEAD position with no room for a next LSN. No
|
||||
// commit produces it (Commit refuses); a HEAD that already carries one (an
|
||||
// older or broken writer) is still read, written by every other
|
||||
@@ -235,6 +255,9 @@ func (h Head) check() error {
|
||||
if h.VolumeID.IsZero() {
|
||||
return fmt.Errorf("%w: HEAD without a volume id", ErrMalformed)
|
||||
}
|
||||
if err := checkExt(h.Version, h.Ext); err != nil {
|
||||
return err
|
||||
}
|
||||
switch h.State {
|
||||
case StateLive, StateDetached, StateDeleting, StateDeleted:
|
||||
default:
|
||||
|
||||
@@ -117,3 +117,34 @@ func TestCommitRecordsTheManifestSize(t *testing.T) {
|
||||
t.Fatalf("a snapshot's size was rewritten: %d", *s.ManifestBytes)
|
||||
}
|
||||
}
|
||||
|
||||
// Ext is a JSON object in its canonical bytes, so it round-trips byte for
|
||||
// byte: a HEAD whose ext is not an object, or not canonical (spacing, an
|
||||
// unescaped character Encode would escape), is refused on decode and on
|
||||
// encode, never rewritten into other bytes.
|
||||
func TestHeadExtIsACanonicalObject(t *testing.T) {
|
||||
root := RootName{Hash: Sum([]byte("image"))}
|
||||
h := idleHead()
|
||||
h.Current, h.Position = &root, 9
|
||||
h.Version, h.Ext = HeadVersion1, json.RawMessage(`{"a":1}`)
|
||||
b, err := h.Encode()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, ext := range map[string]string{
|
||||
"an array": `[1]`,
|
||||
"a string": `"x"`,
|
||||
"spaced": `{ "a":1}`,
|
||||
"not escaped": `{"a":"<"}`,
|
||||
} {
|
||||
bad := bytes.Replace(b, []byte(`"ext":{"a":1}`), []byte(`"ext":`+ext), 1)
|
||||
if _, err := DecodeHead(bad, 1, h.VolumeID); !errors.Is(err, ErrMalformed) {
|
||||
t.Fatalf("decode, ext %s: %v; want ErrMalformed", name, err)
|
||||
}
|
||||
e := h
|
||||
e.Ext = json.RawMessage(ext)
|
||||
if _, err := e.Encode(); !errors.Is(err, ErrMalformed) {
|
||||
t.Fatalf("encode, ext %s: %v; want ErrMalformed", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,3 +119,30 @@ func TestACommitByChangeKeepsAnUnknownSizeUnknown(t *testing.T) {
|
||||
t.Fatalf("a commit by change over an unknown size wrote %d", *h.CurrentBytes)
|
||||
}
|
||||
}
|
||||
|
||||
// A commit by change that touches a shard but leaves its segment unchanged
|
||||
// (a replacement equal to the ref it replaces) keeps that segment: the size
|
||||
// takes out only the segments the new root no longer names. Mixed witness:
|
||||
// shard 0 touched and unchanged, shard 1 changed.
|
||||
func TestACommitByChangeKeepsAnUnchangedTouchedSegmentInTheSize(t *testing.T) {
|
||||
withManifestSizes(t, true)
|
||||
ctx, _, r, h1 := threeShardVolume(t)
|
||||
same, ok := h1.Chunks[7]
|
||||
if !ok {
|
||||
t.Fatal("setup: no chunk 7 in HEAD")
|
||||
}
|
||||
changed := putChunk(t, ctx, r, layout.ShardSize+9, "second")
|
||||
changes := map[uint64]*storage.ChunkRef{7: &same, layout.ShardSize + 9: &changed}
|
||||
exp := storage.ChunkHeadVersion{Gen: h1.Gen, Epoch: 1}
|
||||
next := storage.ChunkHead{Epoch: 1, RemoteLSN: 2}
|
||||
if err := r.CheckCommitDelta(ctx, exp, next, changes); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.CASHeadDelta(ctx, exp, next, changes); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h, want := currentSize(t, ctx, r)
|
||||
if h.CurrentBytes == nil || *h.CurrentBytes != want {
|
||||
t.Fatalf("size %v, want %d (the sum of the manifest's objects)", h.CurrentBytes, want)
|
||||
}
|
||||
}
|
||||
@@ -953,7 +953,13 @@ func deltaManifestSize(prev *uint64, d layout.CommitDelta, plan layout.CommitPla
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, seg := range d.Prev {
|
||||
for shard, seg := range d.Prev {
|
||||
// A touched shard whose segment came out unchanged is still named
|
||||
// by the new root: it stays in the size (and is not in the plan's
|
||||
// new segments either).
|
||||
if name, kept := plan.RootObject().Segments[shard]; kept && name == d.BaseRoot.Segments[shard] {
|
||||
continue
|
||||
}
|
||||
b, err := seg.Encode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -61,9 +61,20 @@ A binary refuses a HEAD with a version newer than it reads as a newer version,
|
||||
not as damage. Known top-level fields stay strict; a v1.0 record carrying a
|
||||
v1.1 field is malformed.
|
||||
|
||||
The ext contract: a v1.1 HEAD may carry one `ext` object, which every writer
|
||||
keeps and re-emits byte for byte while it changes the known fields. Ext holds
|
||||
Upgrade order: every build from this version on reads a version-1 HEAD, but
|
||||
only a data plane on the draft v1.1 store format writes one; release builds
|
||||
keep writing version 0. A v1.0.x binary decodes HEAD strictly and refuses a
|
||||
version-1 record as malformed (it fails closed, it never rewrites it), so
|
||||
every reader of a volume, the control plane included, must run a
|
||||
version-aware build before any v1.1 writer touches that volume.
|
||||
|
||||
The ext contract: a v1.1 HEAD may carry one `ext` object, in its canonical
|
||||
bytes (compact JSON, as HEAD is encoded), which every writer keeps and
|
||||
re-emits byte for byte while it changes the known fields. An ext that is not
|
||||
an object, or not canonical, makes the record malformed. Ext holds
|
||||
only additive facts that such a writer cannot make wrong. A fact an older
|
||||
writer could invalidate by changing a known field without updating it bumps
|
||||
the version instead. Ext counts toward the 64 KiB inline limit; the fullest
|
||||
v1.1 HEAD the bounds allow is about 28 KiB, leaving at least 16 KiB for ext.
|
||||
the version instead. Ext counts toward the 64 KiB inline limit, which Encode
|
||||
enforces. With the holder, claim, session and volume names at 64 bytes, the
|
||||
fullest v1.1 HEAD the other bounds allow is about 28 KiB; those names have no
|
||||
HEAD bound of their own, so this is a sizing check, not a proof.
|
||||
Reference in new issue
Block a user