mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
storage: typed error for a pool chunk off its HEAD ref; read-verify controls
Every read of a pool object already verifies its bytes against the name twice: the layout reader (ErrCorrupt -> ErrRemoteDamaged) and the engine's fill (sha256 and size against the HEAD ref). The engine's refusal was an untyped string; it is now ErrChunkContentMismatch, on the read-miss fill and on the cut COW fill alike. Controls: - storage: a seeded chunk whose bytes are flipped, truncated or extended is refused with ErrChunkContentMismatch on a guest read miss and on HydrateAll; nothing is served or installed, and once the remote serves the named bytes the same reads return HEAD's data. - durable, end to end over the real layout remote: a chunk captured to the pool, its blob damaged under the filer entry, a fresh replica attached from HEAD: the guest read fails with a typed error and never returns the stored bytes; restored, it reads HEAD's data. - chunkstore (real filer, chunkstore_integration): a child uploading pool objects through EnsureObject is SIGKILLed at random points over many rounds; each object it began is absent or holds exactly the bytes its name hashes, and a new session completes it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e5bb90c4e2
commit
8ddb61001e
7 files changed
+405
-2
No files matched your search
@@ -0,0 +1,185 @@
|
||||
//go:build chunkstore_integration
|
||||
|
||||
package chunkstore
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
|
||||
)
|
||||
|
||||
// On a real filer (stock weed): a pool upload cut short leaves no entry or a
|
||||
// complete one. A child process uploads pool objects through the production
|
||||
// path (layout.EnsureObject over LayoutStore: blob upload, then the exclusive
|
||||
// entry create) and is killed (SIGKILL) at a random point of a random
|
||||
// upload, round after round. After every kill each object the child began is
|
||||
// either absent or present with exactly the bytes its name hashes, and a new
|
||||
// session's EnsureObject then completes it. An entry naming partial bytes
|
||||
// would be reused by every later upload of that chunk (EnsureObject reuses
|
||||
// an existing entry), so this is checked at the store, before any reader.
|
||||
//
|
||||
// CHUNKSTORE_WEED=/path/to/weed go test -tags chunkstore_integration -run CutUpload -v ./core/frontend/chunkstore/
|
||||
//
|
||||
// CHUNKSTORE_CUT_ROUNDS (default 40) sets the number of kills.
|
||||
|
||||
const cutHelperEnv = "CHUNKSTORE_CUT_HELPER"
|
||||
|
||||
// cutObject is upload i of a round: 2 MiB of bytes fixed by (seed, i).
|
||||
func cutObject(seed int64, i int) (string, []byte) {
|
||||
data := make([]byte, 2<<20)
|
||||
rand.New(rand.NewSource(seed*1000003 + int64(i))).Read(data)
|
||||
return layout.NameChunk(uint64(i), data).Path(), data
|
||||
}
|
||||
|
||||
// TestIntegration_CutUploadHelper is the child: it uploads objects until it
|
||||
// is killed, announcing each before it starts. It does nothing unless run as
|
||||
// the child.
|
||||
func TestIntegration_CutUploadHelper(t *testing.T) {
|
||||
spec := os.Getenv(cutHelperEnv)
|
||||
if spec == "" {
|
||||
t.Skip("child of TestIntegration_CutUploadLeavesNoEntryOrACompleteOne only")
|
||||
}
|
||||
parts := strings.Split(spec, "|")
|
||||
if len(parts) != 5 {
|
||||
t.Fatalf("bad %s", cutHelperEnv)
|
||||
}
|
||||
filerGRPC, root, session := parts[0], parts[1], parts[4]
|
||||
id, err := layout.ParseVolumeID(parts[2])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seed, err := strconv.ParseInt(parts[3], 10, 64)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cl, err := DialSWFS(SWFSConfig{FilerGRPC: filerGRPC, Collection: "swblock", AssignPath: root + "/"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := NewLayoutStore(cl, cl, root, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := bufio.NewWriter(os.Stdout)
|
||||
for i := 0; ; i++ {
|
||||
rel, data := cutObject(seed, i)
|
||||
fmt.Fprintf(out, "CUT-BEGIN %d\n", i)
|
||||
out.Flush()
|
||||
if err := layout.EnsureObject(context.Background(), f, rel, data, session); err != nil {
|
||||
fmt.Fprintf(out, "CUT-ERR %d %v\n", i, err)
|
||||
out.Flush()
|
||||
os.Exit(3)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegration_CutUploadLeavesNoEntryOrACompleteOne(t *testing.T) {
|
||||
c := startWeed(t)
|
||||
rounds := 40
|
||||
if v, err := strconv.Atoi(os.Getenv("CHUNKSTORE_CUT_ROUNDS")); err == nil && v > 0 {
|
||||
rounds = v
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(rounds)*30*time.Second+2*time.Minute)
|
||||
defer cancel()
|
||||
id, err := layout.NewVolumeID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := "/swblock/objs/" + layout.VolumeDir("v1", id)
|
||||
cl := c.client(t, root+"/")
|
||||
f, err := NewLayoutStore(cl, cl, root, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pick := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
var absent, complete int
|
||||
for round := 0; round < rounds; round++ {
|
||||
seed := int64(round + 1)
|
||||
target := pick.Intn(4) // the upload to cut
|
||||
delay := time.Duration(pick.Intn(40)) * time.Millisecond // into that upload
|
||||
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestIntegration_CutUploadHelper$", "-test.v")
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s|%s|%s|%d|child-%d", cutHelperEnv, c.filerGRPC, root, id, seed, round))
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
began := -1
|
||||
sc := bufio.NewScanner(stdout)
|
||||
for sc.Scan() {
|
||||
line := sc.Text()
|
||||
if strings.HasPrefix(line, "CUT-ERR") {
|
||||
_ = cmd.Process.Kill()
|
||||
_ = cmd.Wait()
|
||||
t.Fatalf("round %d: the child's upload failed before the cut: %s", round, line)
|
||||
}
|
||||
if n, ok := strings.CutPrefix(line, "CUT-BEGIN "); ok {
|
||||
began, _ = strconv.Atoi(n)
|
||||
if began == target {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
time.Sleep(delay)
|
||||
_ = cmd.Process.Kill()
|
||||
_ = cmd.Wait()
|
||||
if began < 0 {
|
||||
t.Fatalf("round %d: the child began no upload: %s", round, stderr.String())
|
||||
}
|
||||
// Every object the child began: absent, or exactly the bytes its name hashes.
|
||||
for i := 0; i <= began; i++ {
|
||||
rel, data := cutObject(seed, i)
|
||||
_, found, err := f.Stat(ctx, rel)
|
||||
if err != nil {
|
||||
t.Fatalf("round %d object %d: stat after the cut: %v", round, i, err)
|
||||
}
|
||||
if !found {
|
||||
if i < began {
|
||||
t.Fatalf("round %d object %d: a completed upload left no entry", round, i)
|
||||
}
|
||||
absent++
|
||||
continue
|
||||
}
|
||||
raw, ok, err := f.Fetch(ctx, path.Join(root, rel))
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("round %d object %d: entry present but its bytes do not read: %v %v", round, i, ok, err)
|
||||
}
|
||||
if !bytes.Equal(raw, data) || layout.Sum(raw) != layout.NameChunk(uint64(i), data).Hash {
|
||||
t.Fatalf("round %d object %d: the cut left an entry with %d bytes that do not hash to its name (want %d)", round, i, len(raw), len(data))
|
||||
}
|
||||
if i == began {
|
||||
complete++
|
||||
}
|
||||
}
|
||||
// A new session completes whatever the cut left.
|
||||
for i := 0; i <= began; i++ {
|
||||
rel, data := cutObject(seed, i)
|
||||
if err := layout.EnsureObject(ctx, f, rel, data, fmt.Sprintf("recover-%d", round)); err != nil {
|
||||
t.Fatalf("round %d object %d: recovery upload: %v", round, i, err)
|
||||
}
|
||||
raw, ok, err := f.Fetch(ctx, path.Join(root, rel))
|
||||
if err != nil || !ok || !bytes.Equal(raw, data) {
|
||||
t.Fatalf("round %d object %d: after recovery the entry does not read back its bytes: %v %v", round, i, ok, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Logf("%d cuts: the cut upload left no entry %d times and a complete entry %d times", rounds, absent, complete)
|
||||
if absent == 0 {
|
||||
t.Error("no cut landed before the entry was created: the cut points did not reach mid-upload")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package durable
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// Every pool read verifies the content against the name, end to end: a
|
||||
// volume captured to the layout pool, the pooled chunk's bytes then damaged
|
||||
// in the blob store under the filer entry, and a fresh replica attached from
|
||||
// HEAD. Its guest read of a block in that chunk fails with a typed error and
|
||||
// never returns the stored bytes; the refusal installs nothing, so once the
|
||||
// pool holds the named bytes again the same read returns HEAD's data.
|
||||
//
|
||||
// Two checks stand on this path: the layout reader (bytes vs the object's
|
||||
// name, chunkstore.ErrRemoteDamaged) and the engine's fill (bytes vs the
|
||||
// HEAD ref, storage.ErrChunkContentMismatch). Either alone refuses; the
|
||||
// assertion accepts either typed error, and the unit controls in
|
||||
// layout/read_test.go and storage/chunk_read_verify_test.go pin each one.
|
||||
func TestPoolReadVerify_FreshReplicaNeverServesADamagedPoolChunk(t *testing.T) {
|
||||
for name, damage := range map[string]func([]byte) []byte{
|
||||
"flipped byte in the read block": func(b []byte) []byte { b[11] ^= 0xff; return b },
|
||||
"flipped byte elsewhere": func(b []byte) []byte { b[len(b)-1] ^= 0xff; return b },
|
||||
"truncated": func(b []byte) []byte { return b[:len(b)/2] },
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
remote := newKZLayout(t)
|
||||
src := kzProvider(t, t.TempDir(), remote)
|
||||
if _, err := src.EnsureStorage("v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := src.RecoverVolume(ctx, "v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := bytes.Repeat([]byte{0x6d}, 4096)
|
||||
const chunk = 5
|
||||
if _, err := src.Backend("v1").Write(ctx, chunk*16*4096, want); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := src.Backend("v1").Sync(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := src.CaptureVolume(ctx, "v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := src.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The pooled chunk: the one blob holding a whole chunk that starts with want.
|
||||
remote.blobs.Lock()
|
||||
var id string
|
||||
for k, v := range remote.blobs.b {
|
||||
if len(v) == 16*4096 && bytes.Equal(v[:4096], want) {
|
||||
if id != "" {
|
||||
remote.blobs.Unlock()
|
||||
t.Fatal("more than one blob holds the written chunk")
|
||||
}
|
||||
id = k
|
||||
}
|
||||
}
|
||||
if id == "" {
|
||||
remote.blobs.Unlock()
|
||||
t.Fatal("the captured chunk is not in the blob store")
|
||||
}
|
||||
orig := remote.blobs.b[id]
|
||||
damaged := damage(append([]byte(nil), orig...))
|
||||
remote.blobs.b[id] = damaged
|
||||
remote.blobs.Unlock()
|
||||
|
||||
p := kzProviderAt(t, t.TempDir(), remote, "r2", 4)
|
||||
if _, err := p.EnsureStorage("v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := p.RecoverVolume(ctx, "v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := make([]byte, 4096)
|
||||
rctx, rcancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
n, err := p.Backend("v1").Read(rctx, chunk*16*4096, got)
|
||||
rcancel()
|
||||
if err == nil {
|
||||
t.Fatalf("a fresh replica read a damaged pool chunk without error (%d bytes, equal to HEAD %v)", n, bytes.Equal(got, want))
|
||||
}
|
||||
if !errors.Is(err, chunkstore.ErrRemoteDamaged) && !errors.Is(err, storage.ErrChunkContentMismatch) {
|
||||
t.Fatalf("read of a damaged pool chunk: %v; want ErrRemoteDamaged or ErrChunkContentMismatch", err)
|
||||
}
|
||||
if len(damaged) >= 4096 && !bytes.Equal(damaged[:4096], want) && bytes.Equal(got, damaged[:4096]) {
|
||||
t.Fatal("the refused read handed out the damaged bytes")
|
||||
}
|
||||
|
||||
remote.blobs.Lock()
|
||||
remote.blobs.b[id] = orig
|
||||
remote.blobs.Unlock()
|
||||
got = make([]byte, 4096)
|
||||
if n, err := p.Backend("v1").Read(ctx, chunk*16*4096, got); err != nil || n != len(got) || !bytes.Equal(got, want) {
|
||||
t.Fatalf("read once the pool holds the named bytes: %d %v, equal %v; want HEAD's data (nothing of the refused copy kept)", n, err, bytes.Equal(got, want))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -532,7 +532,7 @@ func (l *chunkLayerImpl) fillMissing(ctx context.Context, base map[uint64]chunkR
|
||||
return nil, fmt.Errorf("storage: cut chunk %d HEAD fetch: %w", idx, err)
|
||||
}
|
||||
if sha256.Sum256(head) != ref.SHA256 || len(head) != int(l.chunkBlocks)*l.blockSize {
|
||||
return nil, fmt.Errorf("storage: cut chunk %d HEAD fetch: content does not match HEAD", idx)
|
||||
return nil, fmt.Errorf("storage: cut chunk %d HEAD fetch: %w", idx, errChunkContentMismatch)
|
||||
}
|
||||
out := append([]byte(nil), data...)
|
||||
for _, i := range missing {
|
||||
|
||||
@@ -478,7 +478,7 @@ func (l *chunkLayerImpl) fillOnce(ctx context.Context, idx, gen uint64, ref chun
|
||||
return false, fmt.Errorf("storage: chunk %d fetch: %w", idx, err)
|
||||
}
|
||||
if sha256.Sum256(data) != ref.SHA256 || len(data) != int(l.chunkBlocks)*l.blockSize {
|
||||
return false, fmt.Errorf("storage: chunk %d fetch: content does not match HEAD", idx)
|
||||
return false, fmt.Errorf("storage: chunk %d fetch: %w", idx, errChunkContentMismatch)
|
||||
}
|
||||
first := idx * l.chunkBlocks
|
||||
for i := uint64(0); i < l.chunkBlocks; i++ {
|
||||
|
||||
@@ -60,6 +60,7 @@ var (
|
||||
ErrChunkLayerClosed = errChunkLayerClosed
|
||||
ErrChunkBaseInstalling = errChunkBaseInstalling
|
||||
ErrChunkRemoteUnavailable = errChunkRemoteUnavailable
|
||||
ErrChunkContentMismatch = errChunkContentMismatch
|
||||
ErrChunkLSNBehind = errChunkLSNBehind
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The engine's own check on every pool read, independent of the remote's:
|
||||
// a chunk whose bytes do not hash (or size) to the HEAD ref that names it is
|
||||
// refused with ErrChunkContentMismatch on a guest read miss and on a
|
||||
// background hydration, and none of its blocks is served or installed. Once
|
||||
// the remote serves the named bytes again, the same reads return HEAD's data:
|
||||
// nothing of the refused copy was kept.
|
||||
|
||||
// damageSeed replaces the stored bytes of seeded chunk idx and returns a
|
||||
// restore function.
|
||||
func damageSeed(t *testing.T, r *fakeChunkRemote, idx int, damage func([]byte) []byte) func() {
|
||||
t.Helper()
|
||||
key := fmt.Sprintf("seed/%d", idx)
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
orig, ok := r.objs[key]
|
||||
if !ok {
|
||||
t.Fatalf("no seeded chunk %d", idx)
|
||||
}
|
||||
r.objs[key] = damage(append([]byte(nil), orig...))
|
||||
return func() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.objs[key] = orig
|
||||
}
|
||||
}
|
||||
|
||||
var readVerifyDamages = map[string]func([]byte) []byte{
|
||||
"flipped byte in the read block": func(b []byte) []byte { b[7] ^= 0xff; return b },
|
||||
"flipped byte elsewhere": func(b []byte) []byte { b[len(b)-1] ^= 0xff; return b },
|
||||
"truncated": func(b []byte) []byte { return b[:len(b)-ctBlockSize] },
|
||||
"extended": func(b []byte) []byte { return append(b, 0) },
|
||||
}
|
||||
|
||||
func TestPoolReadVerify_GuestReadMissRefusesAChunkOffItsName(t *testing.T) {
|
||||
for name, damage := range readVerifyDamages {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
s, layer, remote, _ := hydrationFixture(t, 2, 64)
|
||||
restore := damageSeed(t, remote, 1, damage)
|
||||
lba := uint32(1 * ctChunkBlocks) // first block of chunk 1
|
||||
got, err := s.ReadContext(boundedCtx(t, context.Background()), lba)
|
||||
if !errors.Is(err, ErrChunkContentMismatch) {
|
||||
t.Fatalf("read of a damaged chunk: %v; want ErrChunkContentMismatch", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("a refused read returned %d bytes", len(got))
|
||||
}
|
||||
layer.stMu.Lock()
|
||||
absent := layer.absent[1]
|
||||
installed := false
|
||||
for i := uint64(0); i < ctChunkBlocks; i++ {
|
||||
installed = installed || layer.localBlocks[uint64(lba)+i]
|
||||
}
|
||||
layer.stMu.Unlock()
|
||||
if !absent || installed {
|
||||
t.Fatalf("after the refusal: chunk absent %v, a block installed %v; want absent, none", absent, installed)
|
||||
}
|
||||
restore()
|
||||
got, err = s.ReadContext(boundedCtx(t, context.Background()), lba)
|
||||
if err != nil || !bytes.Equal(got, makeBlock(ctBlockSize, byte(0x40+lba))) {
|
||||
t.Fatalf("read once the named bytes are back: %v; want HEAD's data", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPoolReadVerify_HydrationRefusesAChunkOffItsName(t *testing.T) {
|
||||
fastWalker(t)
|
||||
for name, damage := range readVerifyDamages {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
s, layer, remote, _ := hydrationFixture(t, 2, 64)
|
||||
restore := damageSeed(t, remote, 2, damage)
|
||||
_, err := layer.HydrateAll(boundedCtx(t, bgFill()))
|
||||
if !errors.Is(err, ErrChunkContentMismatch) {
|
||||
t.Fatalf("hydration over a damaged chunk: %v; want ErrChunkContentMismatch", err)
|
||||
}
|
||||
layer.stMu.Lock()
|
||||
absent := layer.absent[2]
|
||||
layer.stMu.Unlock()
|
||||
if !absent {
|
||||
t.Fatal("hydration installed a chunk whose bytes do not match HEAD")
|
||||
}
|
||||
restore()
|
||||
if p, err := layer.HydrateAll(boundedCtx(t, bgFill())); err != nil || p.Remaining != 0 {
|
||||
t.Fatalf("hydration once the named bytes are back: %+v %v", p, err)
|
||||
}
|
||||
for b := uint32(2 * ctChunkBlocks); b < 3*ctChunkBlocks; b++ {
|
||||
got, err := s.Read(b)
|
||||
if err != nil || !bytes.Equal(got, makeBlock(ctBlockSize, byte(0x40+b))) {
|
||||
t.Fatalf("block %d after hydration: %v; want HEAD's data", b, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -583,6 +583,10 @@ var (
|
||||
errChunkLayerClosed = errors.New("storage: chunk layer closed")
|
||||
errChunkBaseInstalling = errors.New("storage: chunk BASE install in progress")
|
||||
errChunkRemoteUnavailable = errors.New("storage: chunk remote unavailable")
|
||||
// errChunkContentMismatch: a chunk read from the remote does not hash
|
||||
// (or size) to the HEAD ref that names it. Its bytes are never served
|
||||
// or installed.
|
||||
errChunkContentMismatch = errors.New("storage: chunk content does not match HEAD")
|
||||
// errChunkLSNBehind: this host's log has not reached the HEAD's
|
||||
// remote_lsn, so a cut would move remote_lsn backwards (B9 W2 appendix;
|
||||
// LSN continuity across hosts is W4b's).
|
||||
|
||||
Reference in new issue
Block a user