diff --git a/seaweed-block/core/storage/chunk_cut_cow.go b/seaweed-block/core/storage/chunk_cut_cow.go index 24af54495c..f5ff20f4a7 100644 --- a/seaweed-block/core/storage/chunk_cut_cow.go +++ b/seaweed-block/core/storage/chunk_cut_cow.go @@ -520,11 +520,26 @@ func (l *chunkLayerImpl) readLocalBlock(lba uint64, w *cutCOWWindow, dst []byte) // fillMissing completes a COW capture: the blocks listed missing get the cut's // base HEAD bytes. It runs after the local entry is released, bounded by // ReadMissTimeout and the owning cut's cancellation. +// +// On the v1.1 format the fetched HEAD bytes also fill the chunk (M1): they +// are installed beneath every block with a local version, the absence is +// resolved and the record gets its Fill (P to U, held until the store's +// batched mark), so the cut's Hash meets U and a later cut finds the chunk +// local and clean once committed. A fill that no longer belongs to the +// absence installs nothing and leaves the chunk P (the scratch exemption). func (l *chunkLayerImpl) fillMissing(ctx context.Context, base map[uint64]chunkRef, idx uint64, data []byte, missing []uint64) ([]byte, error) { ref, ok := base[idx] if !ok { return nil, fmt.Errorf("storage: cut chunk %d: lazily absent blocks but HEAD has no chunk", idx) } + fill := l.shadow != nil && l.shadow.v11 + var gen, version uint64 + if fill { + l.stMu.Lock() + fill = l.absent[idx] && l.head.Chunks[idx] == ref + gen, version = l.absentGen[idx], l.chunkVersion(idx) + l.stMu.Unlock() + } ctx, cancel := context.WithTimeout(ctx, l.cfg.ReadMissTimeout) defer cancel() head, err := l.remote.Get(ctx, ref.Key) @@ -538,6 +553,11 @@ func (l *chunkLayerImpl) fillMissing(ctx context.Context, base map[uint64]chunkR for _, i := range missing { copy(out[int(i)*l.blockSize:int(i+1)*l.blockSize], head[int(i)*l.blockSize:int(i+1)*l.blockSize]) } + if fill { + if _, err := l.installFill(idx, gen, version, ref, head); err != nil { + return nil, fmt.Errorf("storage: cut chunk %d fill: %w", idx, err) + } + } return out, nil } diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 5016eb5ac2..05abf3cab0 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -466,7 +466,8 @@ func (l *chunkLayerImpl) OnExtentWrite(lba uint64, lsn uint64) { if l.absent[idx] { l.localBlocks[lba] = true l.presenceVer++ - if sh := l.shadow; sh != nil && l.chunkLocalLocked(idx) { + // On v1.1 the write's own event carries the store's bits (noteMutation). + if sh := l.shadow; sh != nil && !sh.v11 && l.chunkLocalLocked(idx) { sh.landed(idx) } } @@ -552,7 +553,11 @@ func (l *chunkLayerImpl) recoveredLocked(sh *chunkRecordShadow) { continue } cov := "all" - if l.absent[idx] { + if l.absent[idx] && sh.v11 { + // The store's bits (durable, or WAL-backed), never the engine's + // view: a fill not yet marked is not local after a crash. + cov = localBitsArg(l.s.v3.mapEntries.at(int(idx)).Local) + } else if l.absent[idx] { n, total := uint64(0), uint64(0) for i := uint64(0); i < l.chunkBlocks; i++ { b := idx*l.chunkBlocks + i @@ -686,11 +691,6 @@ func (l *chunkLayerImpl) OnReadMiss(lba uint64) ([]byte, error) { // current absence (generation and HEAD ref unchanged). It reports whether // it resolved the absence. func (l *chunkLayerImpl) fillOnce(ctx context.Context, idx, gen uint64, ref chunkRef) (bool, error) { - // current reports whether this fill still belongs to the chunk's present - // absence: same generation, same HEAD ref. Caller holds stMu. - current := func() bool { - return l.absent[idx] && l.absentGen[idx] == gen && l.head.Chunks[idx] == ref - } // Read with the end check's lock: a mutation bumps the version after its // dirty-map insert, so one that lands during the fill changes it. l.stMu.Lock() @@ -703,6 +703,20 @@ func (l *chunkLayerImpl) fillOnce(ctx context.Context, idx, gen uint64, ref chun if sha256.Sum256(data) != ref.SHA256 || len(data) != int(l.chunkBlocks)*l.blockSize { return false, fmt.Errorf("storage: chunk %d fetch: %w", idx, errChunkContentMismatch) } + return l.installFill(idx, gen, version, ref, data) +} + +// installFill installs a fetched, verified chunk version: every block with +// no newer local version, as long as the fill still belongs to the chunk's +// current absence (generation and HEAD ref unchanged), and then resolves +// the absence (the record's Fill). version is the chunk's mutation version +// read before the fetch. It reports whether it resolved the absence. +func (l *chunkLayerImpl) installFill(idx, gen, version uint64, ref chunkRef, data []byte) (bool, error) { + // current reports whether this fill still belongs to the chunk's present + // absence: same generation, same HEAD ref. Caller holds stMu. + current := func() bool { + return l.absent[idx] && l.absentGen[idx] == gen && l.head.Chunks[idx] == ref + } first := idx * l.chunkBlocks for i := uint64(0); i < l.chunkBlocks; i++ { b := first + i diff --git a/seaweed-block/core/storage/chunk_record_shadow_test.go b/seaweed-block/core/storage/chunk_record_shadow_test.go index 9eaac29921..2a390ee920 100644 --- a/seaweed-block/core/storage/chunk_record_shadow_test.go +++ b/seaweed-block/core/storage/chunk_record_shadow_test.go @@ -46,9 +46,11 @@ func shadowDefects(l *chunkLayerImpl) int { // extent directly, and a flushed WAL write does not land that way on v3. // Writing every block and flushing leaves P; a cut of it is a scratch cut // (M1): no Hash, no Commit, no refusal. -// - Blocks landing directly (a BASE block) fill the bitmap: P while it is -// partial, U on the last block (never all from one block). Mutant (E1b -// removed): the chunk stays P. +// - Blocks landing directly (a BASE block) fill the bitmap: on v1.0, P +// while it is partial, U on the last block (never all from one block); +// mutant (E1b removed): the chunk stays P. On v1.1 the engine's bitmap +// is never an event's coverage (ChunkStateK3 write_engine_view): the +// chunk stays P until the BASE completion's Changed(all). func TestShadowWriteCoverageFollowsTheStoreBitmap(t *testing.T) { s, l, _ := lazyFreshVolume(t) const idx = 1 @@ -84,7 +86,7 @@ func TestShadowWriteCoverageFollowsTheStoreBitmap(t *testing.T) { for i := uint64(0); i < ctChunkBlocks; i++ { l.OnExtentWrite(base*ctChunkBlocks+i, 0) // a block landing directly want := chunkkernel.Partial - if i == ctChunkBlocks-1 { + if i == ctChunkBlocks-1 && !l.shadow.v11 { want = chunkkernel.Unknown } if k := shadowKind(l, base); k != want { diff --git a/seaweed-block/core/storage/chunk_v11_cut_fill_test.go b/seaweed-block/core/storage/chunk_v11_cut_fill_test.go new file mode 100644 index 0000000000..04d66c57bf --- /dev/null +++ b/seaweed-block/core/storage/chunk_v11_cut_fill_test.go @@ -0,0 +1,125 @@ +package storage + +import ( + "bytes" + "testing" + + "github.com/seaweedfs/seaweed-block/core/chunkkernel" +) + +// M1 on the v1.1 format (ChunkStateK3 FillInstall, MarkDurable; mutants +// hash_partial, page_before_mark): a cut of a partial chunk (one block +// written, the rest only in HEAD) also fills it. The HEAD bytes the cut +// fetched are installed beneath the local write, the absence is resolved, +// and the record's Fill makes the chunk U before the cut's Hash (never Hash +// on P), so the commit leaves it HP. Its page shows P, the state before the +// fill, until the store's batched mark. A second cut compares and uploads +// nothing more, and the chunk reads locally with its write and HEAD's bytes. +func TestV11ACutOfAPartialChunkFillsIt(t *testing.T) { + s, path, l, f := v11LazyVolume(t) + const idx = 3 + written := makeBlock(ctBlockSize, 0xD1) + if _, err := s.Write(idx*ctChunkBlocks+1, written); err != nil { + t.Fatal(err) + } + if k := shadowKind(l, idx); k != chunkkernel.Partial { + t.Fatalf("setup: %s, want P", k) + } + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + l.stMu.Lock() + absent := l.absent[idx] + l.stMu.Unlock() + l.shadow.mu.Lock() + _, scratch := l.shadow.scratchP[idx] + l.shadow.mu.Unlock() + if absent || scratch { + t.Fatalf("after the cut: absent %v, scratch %v; want the chunk filled", absent, scratch) + } + if k := shadowKind(l, idx); k != chunkkernel.HashPublished { + t.Fatalf("after the cut: %s, want HP (Fill, then Hash and Commit)", k) + } + if n := shadowDefects(l); n != 0 { + t.Fatalf("%d shell events refused", n) + } + if err := l.shadow.rec.Flush(); err != nil { + t.Fatal(err) + } + if k := pageState(t, path, l.numChunks, idx); k != chunkkernel.Partial { + t.Fatalf("page before the mark: %s, want P (the state before the fill)", k) + } + if err := l.markFills(l.shadow); err != nil { + t.Fatal(err) + } + if err := l.shadow.rec.Flush(); err != nil { + t.Fatal(err) + } + if k := pageState(t, path, l.numChunks, idx); k != chunkkernel.HashPublished { + t.Fatalf("page after the mark: %s, want HP", k) + } + + before := f.stats() + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + for i := uint32(0); i < ctChunkBlocks; i++ { + got, err := s.Read(idx*ctChunkBlocks + i) + if err != nil { + t.Fatal(err) + } + want := makeBlock(ctBlockSize, byte(0x40+idx*ctChunkBlocks+i)) + if i == 1 { + want = written + } + if !bytes.Equal(got, want) { + t.Fatalf("block %d of the filled chunk: wrong bytes", i) + } + } + if after := f.stats(); after.puts != before.puts || after.gets != before.gets { + t.Fatalf("second cut and reads: %d puts, %d gets; want none (the chunk is local and clean)", after.puts-before.puts, after.gets-before.gets) + } +} + +// The run-time Changed takes an absent chunk's coverage from the store's +// bits, never from the engine's view (ChunkStateK3 changed_engine_view: the +// engine's bitmap may say local what a crash would not keep). The engine is +// made to say every block local while the store's bits hold one written +// block: Recover's Changed must give P (some), not U. +func TestV11RunTimeChangedTakesTheStoreBits(t *testing.T) { + s, _, l, _ := v11LazyVolume(t) + const idx = 3 + if _, err := s.Write(idx*ctChunkBlocks+1, makeBlock(ctBlockSize, 0xD1)); err != nil { + t.Fatal(err) + } + l.stMu.Lock() + for i := uint64(0); i < ctChunkBlocks; i++ { + l.localBlocks[idx*ctChunkBlocks+i] = true + } + l.stMu.Unlock() + s.mu.Lock() + l.noteAllMutated() + s.mu.Unlock() + if k := shadowKind(l, idx); k != chunkkernel.Partial { + t.Fatalf("Changed over one store bit with the engine saying all: %s, want P", k) + } +} + +// A block landing in the extent of an absent chunk is not a Write with the +// engine bitmap's coverage on v1.1 (ChunkStateK3 write_engine_view): the +// write's own event already carried the store's bits. Every block of a +// chunk landing directly leaves it P; the BASE completion's Changed(all) +// is what makes it U (TestShadowBaseInstallCompletingChangesEveryChunk). +func TestV11ALandingIsNotAWriteFromTheEngineBitmap(t *testing.T) { + _, _, l, _ := v11LazyVolume(t) + const idx = 2 + for i := uint64(0); i < ctChunkBlocks; i++ { + l.OnExtentWrite(idx*ctChunkBlocks+i, 0) + } + if k := shadowKind(l, idx); k != chunkkernel.Partial { + t.Fatalf("every block landed directly: %s, want P", k) + } + if n := shadowDefects(l); n != 0 { + t.Fatalf("%d shell events refused", n) + } +}