mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
layout: a HEAD with a version field is refused as written by a newer version
A v1.0.x reader meeting a HEAD that carries a version field (a newer format writes it first) answers "HEAD written by a newer version: use a newer binary" (ErrHeadNewerVersion, wrapping ErrMalformed) instead of a plain malformed HEAD, so every final refusal stays final. Read side only: nothing here writes a version or ext. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f52c0960ce
commit
afae4dfa0b
6 files changed
+293
No files matched your search
@@ -0,0 +1,111 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"path"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
|
||||
)
|
||||
|
||||
// countingChunkStore counts every mutating filer call and blob put.
|
||||
type countingChunkStore struct {
|
||||
*memChunkStore
|
||||
writes atomic.Int64
|
||||
}
|
||||
|
||||
func (c *countingChunkStore) CreateEntry(ctx context.Context, dir string, e *filerpb.Entry, oExcl bool) error {
|
||||
c.writes.Add(1)
|
||||
return c.memChunkStore.CreateEntry(ctx, dir, e, oExcl)
|
||||
}
|
||||
|
||||
func (c *countingChunkStore) UpdateEntry(ctx context.Context, dir string, e *filerpb.Entry, expected map[string][]byte) error {
|
||||
c.writes.Add(1)
|
||||
return c.memChunkStore.UpdateEntry(ctx, dir, e, expected)
|
||||
}
|
||||
|
||||
func (c *countingChunkStore) DeleteEntry(ctx context.Context, dir, name string, expected map[string][]byte) error {
|
||||
c.writes.Add(1)
|
||||
return c.memChunkStore.DeleteEntry(ctx, dir, name, expected)
|
||||
}
|
||||
|
||||
func (c *countingChunkStore) Put(ctx context.Context, data []byte) (string, error) {
|
||||
c.writes.Add(1)
|
||||
return c.memChunkStore.Put(ctx, data)
|
||||
}
|
||||
|
||||
// plantNewerHead rewrites v's HEAD as a newer format writes it (a version
|
||||
// field first, a size and an ext object), in place.
|
||||
func plantNewerHead(t *testing.T, store *memChunkStore, v chunkstore.VolumeRef) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
dir := path.Join(defaultChunkPathCLI, v.Dir())
|
||||
e, err := store.LookupEntry(ctx, dir, layout.HeadName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := e.Content
|
||||
if _, err := layout.DecodeHead(b, 1, v.ID); err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
newer := append([]byte(`{"version":1,`), b[1:len(b)-1]...)
|
||||
e.Content = append(newer, []byte(`,"current_bytes":10,"ext":{"later":1}}`)...)
|
||||
if err := store.MemFiler.UpdateEntry(ctx, dir, e, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// A v1.0.x CLI meeting a HEAD written by a newer format: every chunk verb
|
||||
// that reads HEAD from the filer refuses with the newer-version text (use a
|
||||
// newer binary; never corruption, restore or recreate), and writes nothing:
|
||||
// no entry is created, updated or deleted and no blob is put. cut, snapshot
|
||||
// and detach are requests to the holder, whose Start refuses the HEAD
|
||||
// (TestFinalLayoutStartRefusalTable).
|
||||
func TestOpsChunkEveryVerbRefusesANewerHeadAndWritesNothing(t *testing.T) {
|
||||
mem := newMemChunkStore()
|
||||
store := &countingChunkStore{memChunkStore: mem}
|
||||
useMemChunkStore(t, mem)
|
||||
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) { return store, func() error { return nil }, nil }
|
||||
src := cliVolume(t, mem, "src", "daily")
|
||||
plantNewerHead(t, mem, src)
|
||||
tid, _ := layout.NewVolumeID()
|
||||
tgt := chunkstore.VolumeRef{Name: "t", ID: tid}
|
||||
lc := &fakeLifecycle{}
|
||||
addr := startFakeLifecycle(t, lc)
|
||||
verbs := map[string][]string{
|
||||
"snapshots": cliArgs("snapshots", volArgs("volume", src)),
|
||||
"show": cliArgs("show", volArgs("volume", src)),
|
||||
"usage": cliArgs("usage", volArgs("volume", src)),
|
||||
"snapshot-delete": cliArgs("snapshot-delete", volArgs("volume", src), []string{"--name", "daily"}),
|
||||
"retain": cliArgs("retain", volArgs("volume", src)),
|
||||
"retain --dry-run": cliArgs("retain", volArgs("volume", src), []string{"--dry-run"}),
|
||||
"release": cliArgs("release", volArgs("source", src), volArgs("target", tgt)),
|
||||
"verify": cliArgs("verify", volArgs("volume", src)),
|
||||
"restore": cliArgs("restore", volArgs("source", src), volArgs("target", tgt), []string{"--snapshot", "daily"}),
|
||||
"volume-delete": cliArgs("volume-delete", volArgs("volume", src), []string{"--master", addr, "--request-id", "r1"}),
|
||||
}
|
||||
for name, args := range verbs {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
before := store.writes.Load()
|
||||
var out, errb bytes.Buffer
|
||||
code := run(append([]string{"ops", "chunk"}, args...), &out, &errb)
|
||||
all := out.String() + errb.String()
|
||||
if code == 0 || !strings.Contains(all, "written by a newer version: use a newer binary") {
|
||||
t.Fatalf("exit %d, out %q err %q; want the newer-version refusal", code, out.String(), errb.String())
|
||||
}
|
||||
for _, bad := range []string{"corrupt", "recreate", "restore or"} {
|
||||
if strings.Contains(strings.ToLower(all), bad) {
|
||||
t.Fatalf("the refusal says %q: %s", bad, all)
|
||||
}
|
||||
}
|
||||
if n := store.writes.Load() - before; n != 0 {
|
||||
t.Fatalf("%d filer writes; want none", n)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -318,6 +318,12 @@ func (h Head) Encode() ([]byte, error) {
|
||||
return b, err
|
||||
}
|
||||
|
||||
// ErrHeadNewerVersion refuses a HEAD written by a newer format version: a
|
||||
// binary too old to read it, not damage. It wraps ErrMalformed, so every
|
||||
// caller that refuses a malformed HEAD for good (Start among them) refuses
|
||||
// this one the same way; its text names the cure.
|
||||
var ErrHeadNewerVersion = fmt.Errorf("%w: HEAD written by a newer version: use a newer binary", ErrMalformed)
|
||||
|
||||
// DecodeHead parses canonical HEAD bytes for the volume whose VOLUME
|
||||
// record carries volume; unknown fields and non-canonical bytes are
|
||||
// refused, and so is a HEAD of another volume id (ErrIdentity): one left
|
||||
@@ -327,6 +333,21 @@ 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")
|
||||
}
|
||||
// A version field is a later format's (v1.1 writes version 1 first):
|
||||
// read it leniently and refuse the HEAD as newer, never as damaged.
|
||||
var probe struct {
|
||||
Version json.RawMessage `json:"version"`
|
||||
}
|
||||
if err := json.Unmarshal(b, &probe); err != nil {
|
||||
return Head{}, fmt.Errorf("%w: HEAD: %v", ErrMalformed, err)
|
||||
}
|
||||
if probe.Version != nil {
|
||||
var v uint64
|
||||
if err := json.Unmarshal(probe.Version, &v); err != nil || v == 0 {
|
||||
return Head{}, fmt.Errorf("%w: HEAD version %s", ErrMalformed, probe.Version)
|
||||
}
|
||||
return Head{}, fmt.Errorf("%w (version %d; this binary reads only the v1.0 format)", ErrHeadNewerVersion, v)
|
||||
}
|
||||
var d headDoc
|
||||
dec := json.NewDecoder(bytes.NewReader(b))
|
||||
dec.DisallowUnknownFields()
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A HEAD carrying a version field was written by a newer format (v1.1
|
||||
// writes version 1 and its own fields). This reader answers that it is
|
||||
// newer (ErrHeadNewerVersion, which wraps ErrMalformed so every final
|
||||
// refusal stays final, and whose text names a newer binary, never
|
||||
// corruption); a HEAD without the field decodes as before, and a version
|
||||
// field that is not a positive number is malformed (not newer).
|
||||
func TestHeadWithAVersionIsRefusedAsNewer(t *testing.T) {
|
||||
h := idleHead()
|
||||
b, err := h.Encode()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(b, []byte(`{"volume_id"`)) {
|
||||
t.Fatalf("setup: HEAD bytes %s", b)
|
||||
}
|
||||
newer := append([]byte(`{"version":1,`), b[1:]...)
|
||||
newer = bytes.Replace(newer, []byte(`}`), []byte(`,"current_bytes":10}`), 1)
|
||||
newer = append(newer[:len(newer)-1], []byte(`,"ext":{"later":1}}`)...)
|
||||
_, err = DecodeHead(newer, 1, h.VolumeID)
|
||||
if !errors.Is(err, ErrHeadNewerVersion) || !errors.Is(err, ErrMalformed) {
|
||||
t.Fatalf("a HEAD of a newer version: %v; want ErrHeadNewerVersion, wrapping ErrMalformed", err)
|
||||
}
|
||||
if msg := err.Error(); !strings.Contains(msg, "written by a newer version: use a newer binary") || strings.Contains(msg, "corrupt") {
|
||||
t.Fatalf("the refusal's text: %q", msg)
|
||||
}
|
||||
if _, err := DecodeHead(b, 1, h.VolumeID); err != nil {
|
||||
t.Fatalf("a v1.0 HEAD: %v", err)
|
||||
}
|
||||
for _, v := range []string{`0`, `-1`, `"1"`, `1.5`} {
|
||||
bad := append([]byte(`{"version":`+v+`,`), b[1:]...)
|
||||
if _, err := DecodeHead(bad, 1, h.VolumeID); !errors.Is(err, ErrMalformed) || errors.Is(err, ErrHeadNewerVersion) {
|
||||
t.Fatalf("version %s: %v; want ErrMalformed", v, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package durable_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// writeCountingFiler counts every mutating filer call.
|
||||
type writeCountingFiler struct {
|
||||
*chunkstore.MemFiler
|
||||
writes atomic.Int64
|
||||
}
|
||||
|
||||
func (f *writeCountingFiler) CreateEntry(ctx context.Context, dir string, e *filerpb.Entry, oExcl bool) error {
|
||||
f.writes.Add(1)
|
||||
return f.MemFiler.CreateEntry(ctx, dir, e, oExcl)
|
||||
}
|
||||
|
||||
func (f *writeCountingFiler) UpdateEntry(ctx context.Context, dir string, e *filerpb.Entry, expected map[string][]byte) error {
|
||||
f.writes.Add(1)
|
||||
return f.MemFiler.UpdateEntry(ctx, dir, e, expected)
|
||||
}
|
||||
|
||||
func (f *writeCountingFiler) DeleteEntry(ctx context.Context, dir, name string, expected map[string][]byte) error {
|
||||
f.writes.Add(1)
|
||||
return f.MemFiler.DeleteEntry(ctx, dir, name, expected)
|
||||
}
|
||||
|
||||
// countingStartStore counts the HEAD reads a holder's Start makes.
|
||||
type countingStartStore struct {
|
||||
layout.StartStore
|
||||
reads *atomic.Int64
|
||||
}
|
||||
|
||||
func (s countingStartStore) ReadHead(ctx context.Context) (layout.Head, bool, error) {
|
||||
s.reads.Add(1)
|
||||
return s.StartStore.ReadHead(ctx)
|
||||
}
|
||||
|
||||
// A v1.0.x holder meeting a HEAD written by a newer format refuses it once
|
||||
// and finally: its recovery answers the newer-version refusal (use a newer
|
||||
// binary), the volume is not served, no Start is retried (the HEAD reads
|
||||
// stay at one across a wait), and nothing is written to the filer.
|
||||
func TestLayoutProviderRefusesANewerHeadOnceAndWritesNothing(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
f := &writeCountingFiler{MemFiler: chunkstore.NewMemFiler()}
|
||||
cat, err := chunkstore.NewBCatalog(f, &layoutProviderBlobs{blobs: map[string][]byte{}}, "/layout-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id, err := layout.NewVolumeID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
v := chunkstore.VolumeRef{Name: "v1", ID: id}
|
||||
if _, err = cat.CreateVolume(ctx, v, layout.Geometry{SizeBytes: 64 * 4096, BlockSize: 4096, ChunkBytes: 4 * 4096}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dir := path.Join("/layout-test", v.Dir())
|
||||
e, err := f.LookupEntry(ctx, dir, layout.HeadName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := e.Content
|
||||
e.Content = append(append([]byte(`{"version":1,`), b[1:len(b)-1]...), []byte(`,"current_bytes":10,"ext":{"later":1}}`)...)
|
||||
if err := f.MemFiler.UpdateEntry(ctx, dir, e, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writes := f.writes.Load()
|
||||
var reads atomic.Int64
|
||||
view := newStubView(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1}))
|
||||
p, err := durable.NewDurableProvider(durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 4, ChunkHolder: "s/r1",
|
||||
ChunkLayoutHeadFor: func(string) (layout.StartStore, error) {
|
||||
st, err := cat.Store(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return countingStartStore{StartStore: st, reads: &reads}, nil
|
||||
},
|
||||
ChunkLayoutRemoteFor: func(_ string, me layout.HolderID) (storage.ChunkRemote, error) {
|
||||
return chunkstore.NewLayoutRemote(cat, v, me)
|
||||
},
|
||||
}, view)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = p.Close() })
|
||||
if _, err := p.EnsureStorage("v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = p.RecoverVolume(ctx, "v1")
|
||||
if err == nil || !strings.Contains(err.Error(), "written by a newer version: use a newer binary") {
|
||||
t.Fatalf("recovery over a newer HEAD: %v; want the newer-version refusal", err)
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond) // a retry loop would read HEAD again
|
||||
if n := reads.Load(); n != 1 {
|
||||
t.Fatalf("%d HEAD reads; want one Start attempt, no retry", n)
|
||||
}
|
||||
if n := f.writes.Load() - writes; n != 0 {
|
||||
t.Fatalf("%d filer writes; want none", n)
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ func TestFinalLayoutStartRefusalTable(t *testing.T) {
|
||||
{"foreign volume", layout.ErrIdentity, true},
|
||||
{"corrupt store", layout.ErrCorrupt, true},
|
||||
{"malformed store", layout.ErrMalformed, true},
|
||||
{"HEAD of a newer version", layout.ErrHeadNewerVersion, true},
|
||||
{"missing VOLUME", layout.ErrMissing, true},
|
||||
{"oversized HEAD", layout.ErrHeadTooLarge, true},
|
||||
{"legacy store", chunkstore.ErrLegacyStore, true},
|
||||
|
||||
@@ -143,6 +143,9 @@ func TestWorkspaceLayoutHEADRefusalsAndStates(t *testing.T) {
|
||||
{name: "legacy content", alter: func(e *filerpb.Entry) {
|
||||
e.Content = []byte(`{"format":1,"gen":9,"epoch":3,"holder":"node/r","remote_lsn":42}`)
|
||||
}},
|
||||
{name: "HEAD of a newer version", alter: func(e *filerpb.Entry) {
|
||||
e.Content = append(append([]byte(`{"version":1,`), e.Content[1:len(e.Content)-1]...), []byte(`,"current_bytes":10,"ext":{"later":1}}`)...)
|
||||
}, sentinel: layout.ErrHeadNewerVersion},
|
||||
{name: "oversized inline HEAD", alter: func(e *filerpb.Entry) { e.Content = bytes.Repeat([]byte("x"), layout.MaxHeadBytes+1) }, sentinel: layout.ErrHeadTooLarge},
|
||||
{name: "not found", absent: true, sentinel: layout.ErrHeadAbsent},
|
||||
{name: "unavailable", down: true},
|
||||
|
||||
Reference in new issue
Block a user