storage: red tests, the v1.0.x upgrade seeds after a clean stop

A seam: ChunkCleanStop (the proof a start reads before its claim: HEAD a
committed, idle detach of this root's lineage at a position), upgradeSeed
(the proof, the attached HEAD, the zero chunk's hash) and
upgradeChunkRecordSeeded, which ignores the seed for now.

- TestUpgradeSeedsAfterACleanStop: with the proof at the store's synced
  position and the store's own lineage, every whole local chunk is HP with
  HEAD's hash (zero in HEAD: the zero chunk's). Fails here: U.
- TestUpgradeSeedCrashAtEachStep: a crash after the seeded pages and before
  the field rebuilds without trusting them; after the field the marked store
  never seeds; after the header the complete record is used as written.
  The first and last fail here (no seeded pages are written).
- Guards that hold here and must keep holding: no seed when the store is
  above or below the proof (an acknowledged write after the detach; the
  checkpoint position is not the store's), for another or no recorded
  lineage, without a proof, or when HEAD moved since the proof.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 06:02:28 -08:00
1 parent 1eed884fe2
commit 0442e4cfc7
2 files changed
+264

No files matched your search

@@ -0,0 +1,242 @@
package storage
import (
"context"
"crypto/sha256"
"errors"
"os"
"testing"
"github.com/seaweedfs/seaweed-block/core/chunkkernel"
)
const seedClaim = "claim-a"
// seedHead is the HEAD a clean stop left: chunks 0 and 1 named with their
// content's hash, every other chunk zero in HEAD; at position pos.
func seedHead(pos uint64) chunkHead {
return chunkHead{Gen: 4, Epoch: 3, Holder: "h", RemoteLSN: pos, Chunks: map[uint64]chunkRef{
0: {Key: "k0", SHA256: sha256.Sum256([]byte("chunk 0"))},
1: {Key: "k1", SHA256: sha256.Sum256([]byte("chunk 1"))},
}}
}
var seedZero = sha256.Sum256(make([]byte, 4*4096))
// openForSeed is openForUpgrade on a root whose layout identity is seedClaim.
func openForSeed(t *testing.T, path string) *WALStore {
t.Helper()
s := openForUpgrade(t, path)
if err := s.SetLayoutIdentity(seedClaim, false); err != nil {
t.Fatal(err)
}
return s
}
// cleanStopAt is the proof Start reads before its claim: HEAD a committed,
// idle detach of this root's lineage at pos.
func cleanStopAt(pos uint64, claim string) *upgradeSeed {
return &upgradeSeed{proof: &ChunkCleanStop{Position: pos, ClaimID: claim}, head: seedHead(pos), zero: seedZero}
}
func recordEntries(t *testing.T, s *WALStore) []chunkRecordEntry {
t.Helper()
marked, epoch := s.ChunkStateMarker()
got, err := loadChunkRecord(chunkRecordDir(s.path), 16, marked, epoch, syncedPos(s))
if err != nil || !got.Complete {
t.Fatalf("record not complete: %+v %v", got, err)
}
return got.Pages[0].Entries
}
// checkSeeded: every present chunk is hash-known and published with HEAD's
// hash (a chunk HEAD does not name is zero in HEAD); the absent and partial
// chunks are classified as without a seed.
func checkSeeded(t *testing.T, s *WALStore) {
t.Helper()
checkLocalBits(t, s)
head := seedHead(0)
for c, e := range recordEntries(t, s) {
k, err := chunkRecordKind(e.State)
if err != nil {
t.Fatal(err)
}
switch c {
case 2, 3:
if k != chunkkernel.Partial {
t.Fatalf("partial chunk %d is %s", c, k)
}
continue
case 5:
if k != chunkkernel.Absent {
t.Fatalf("absent chunk 5 is %s", k)
}
continue
}
want := seedZero
if ref, ok := head.Chunks[uint64(c)]; ok {
want = ref.SHA256
}
if k != chunkkernel.HashPublished || e.Hash != want {
t.Fatalf("present chunk %d is %s with hash %x, want HP with HEAD's %x", c, k, e.Hash[:4], want[:4])
}
}
if _, err := os.Stat(chunkPresencePath(s.path)); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("legacy presence still there: %v", err)
}
}
// After a clean stop (HEAD detached by this lineage at the store's synced
// position) the upgrade seeds every whole local chunk with HEAD's hash:
// nothing is left for the compare pass.
func TestUpgradeSeedsAfterACleanStop(t *testing.T) {
path := legacyVolume(t)
s := openForSeed(t, path)
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, cleanStopAt(syncedPos(s), seedClaim), nil); err != nil {
t.Fatal(err)
}
checkSeeded(t, s)
}
// Rows 122 and 126: L, compared with the proof's position, is the
// store's synced position after recovery (replay included), never the
// checkpoint position. A write acknowledged after the detach leaves the store
// above the proof: no seed, and the pass has every present chunk to compare.
func TestUpgradeSeedNeedsTheStoreAtTheProofPosition(t *testing.T) {
for _, tc := range []struct {
name string
pos func(s *WALStore) uint64
}{
{"a write acknowledged after the detach", func(s *WALStore) uint64 { return syncedPos(s) - 1 }},
{"the checkpoint position", func(s *WALStore) uint64 { return s.CheckpointLSN() }},
{"above the store", func(s *WALStore) uint64 { return syncedPos(s) + 1 }},
} {
t.Run(tc.name, func(t *testing.T) {
path := legacyVolume(t)
s := openForSeed(t, path)
if tc.pos(s) == syncedPos(s) {
t.Fatalf("the control needs a position other than the store's %d", syncedPos(s))
}
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, cleanStopAt(tc.pos(s), seedClaim), nil); err != nil {
t.Fatal(err)
}
checkUpgradedKinds(t, s)
})
}
}
// Row 123: no seed without the epoch compare. The proof's lineage must be the
// store's own (its layout claim id); another lineage's detach, or a root that
// never recorded one, is not this store's clean stop.
func TestUpgradeSeedNeedsTheStoresLineage(t *testing.T) {
t.Run("another lineage", func(t *testing.T) {
path := legacyVolume(t)
s := openForSeed(t, path)
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, cleanStopAt(syncedPos(s), "claim-b"), nil); err != nil {
t.Fatal(err)
}
checkUpgradedKinds(t, s)
})
t.Run("no recorded lineage", func(t *testing.T) {
path := legacyVolume(t)
s := openForUpgrade(t, path)
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, cleanStopAt(syncedPos(s), seedClaim), nil); err != nil {
t.Fatal(err)
}
checkUpgradedKinds(t, s)
})
}
// Row 122 (no seed after an unclean stop): without a proof, and when HEAD has
// moved since the proof (another cut), nothing is seeded.
func TestUpgradeSeedNeedsAProofHEADStillShows(t *testing.T) {
t.Run("no proof", func(t *testing.T) {
path := legacyVolume(t)
s := openForSeed(t, path)
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, &upgradeSeed{head: seedHead(syncedPos(s)), zero: seedZero}, nil); err != nil {
t.Fatal(err)
}
checkUpgradedKinds(t, s)
})
t.Run("HEAD moved since the proof", func(t *testing.T) {
path := legacyVolume(t)
s := openForSeed(t, path)
seed := cleanStopAt(syncedPos(s), seedClaim)
seed.head.RemoteLSN++
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, seed, nil); err != nil {
t.Fatal(err)
}
checkUpgradedKinds(t, s)
})
}
// Rows 118, 127 and 128: the seed writes through the record in the
// upgrade's order (pages, the store's field, the header, the presence file
// removed). A crash after any step, and the next open (here without the proof,
// as after a restart: the claim made HEAD live) uses the record or rebuilds,
// never trusts a partial one.
func TestUpgradeSeedCrashAtEachStep(t *testing.T) {
t.Run("after the pages, before the field", func(t *testing.T) {
path := legacyVolume(t)
s := openForSeed(t, path)
crash := errors.New("crash before the descriptor slots")
seeded := false
s.v3.testBeforeSlotWrite = func() error {
if pages, bad, err := readChunkRecordPages(chunkRecordDir(path), 16); err == nil && len(bad) == 0 && len(pages) == 1 {
if k, _ := chunkRecordKind(pages[0].Entries[0].State); k == chunkkernel.HashPublished {
seeded = true
}
}
return crash
}
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, cleanStopAt(syncedPos(s), seedClaim), nil); !errors.Is(err, crash) {
t.Fatalf("the upgrade answered %v, want the injected crash", err)
}
if !seeded {
t.Fatal("the control needs seeded pages on disk at the crash")
}
crashStore(t, s)
next := openForSeed(t, path)
if err := upgradeChunkRecordSeeded(context.Background(), next, 7, &upgradeSeed{head: seedHead(syncedPos(next)), zero: seedZero}, nil); err != nil {
t.Fatal(err)
}
checkUpgradedKinds(t, next) // the seeded pages are not trusted: rebuilt, unknown
})
t.Run("after the field, before the header", func(t *testing.T) {
path := legacyVolume(t)
s := openForSeed(t, path)
header := chunkRecordDir(path) + "/" + chunkRecordHeaderName
if err := os.MkdirAll(header, 0o755); err != nil { // the header's write fails
t.Fatal(err)
}
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, cleanStopAt(syncedPos(s), seedClaim), nil); err == nil {
t.Fatal("the control needs a failed header write")
}
crashStore(t, s)
if err := os.Remove(header); err != nil {
t.Fatal(err)
}
next := openForSeed(t, path)
// even with the proof: a marked store never seeds; its record is rebuilt from the store's bits
if err := upgradeChunkRecordSeeded(context.Background(), next, 7, cleanStopAt(syncedPos(next), seedClaim), nil); err != nil {
t.Fatal(err)
}
checkUpgradedKinds(t, next)
})
t.Run("after the header, before the presence file goes", func(t *testing.T) {
path := legacyVolume(t)
s := openForSeed(t, path)
if err := upgradeChunkRecordSeeded(context.Background(), s, 7, cleanStopAt(syncedPos(s), seedClaim), nil); err != nil {
t.Fatal(err)
}
crashStore(t, s)
if err := os.WriteFile(chunkPresencePath(path), []byte("absent 2\n"), 0o644); err != nil {
t.Fatal(err) // as if the removal had not happened
}
next := openForSeed(t, path)
if err := upgradeChunkRecordSeeded(context.Background(), next, 7, &upgradeSeed{head: seedHead(syncedPos(next)), zero: seedZero}, nil); err != nil {
t.Fatal(err)
}
checkSeeded(t, next) // the complete record is used as written
})
}
@@ -163,6 +163,28 @@ func upgradeChunkRecordWith(ctx context.Context, s *WALStore, epoch uint64, prog
return removeLegacyPresence(presence)
}
// ChunkCleanStop is the proof of a clean stop that a start reads before its
// claim: HEAD was a committed, idle detach of this root's lineage (ClaimID)
// at Position. The claim makes HEAD live, so the proof is read before it.
type ChunkCleanStop struct {
Position uint64
ClaimID string
}
// upgradeSeed is what the clean-stop seed of the v1.0.x upgrade needs from
// the attach: the proof (nil: none), the HEAD the layer attached to, and the
// hash of a zero chunk (a chunk HEAD does not name).
type upgradeSeed struct {
proof *ChunkCleanStop
head chunkHead
zero [32]byte
}
// upgradeChunkRecordSeeded is upgradeChunkRecordWith with a clean-stop seed.
func upgradeChunkRecordSeeded(ctx context.Context, s *WALStore, epoch uint64, seed *upgradeSeed, progress func(uint64)) error {
return upgradeChunkRecordWith(ctx, s, epoch, progress)
}
// writeChunkRecordPagesFrom writes every page, valid through pos, with each
// chunk's state from its coverage by the kernel's Rebuilt event.
func writeChunkRecordPagesFrom(dir string, chunks, pos uint64, r ChunkCoverageReader, progress func(uint64)) error {