diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index d8b08b7cab..9f4d6c18f3 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1557,7 +1557,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch inPlace := byChange && fromBase // cur is the cut's base: its candidates only finishCapturedCut(cur, h, nil, nil, inPlace) // nothing new: no CAS persistCapturedMap(capturedVersions) - return chunkCutResult{H: h, Head: cutResultHead(cur, inPlace), Noop: true, FullHash: all}, nil + return chunkCutResult{H: h, Head: resultHead(cur), Noop: true, FullHash: all}, nil } expected := chunkHeadVersion{Gen: baseHead.Gen, Epoch: baseHead.Epoch} // On the v1.1 format the commit goes by change: one owned change set, @@ -1605,7 +1605,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch finishCapturedCut(next, h, changed, newKeys, changes != nil) l.shadowCommit(captured, capturedVersions) persistCapturedMap(capturedVersions) - return l.cutDone(chunkCutResult{H: h, Head: cutResultHead(next, changes != nil), FullHash: all}, detach), nil + return l.cutDone(chunkCutResult{H: h, Head: resultHead(next), FullHash: all}, detach), nil } // Precondition or unknown outcome: resolve by re-reading HEAD, never by // a blind retry. @@ -1649,7 +1649,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch finishCapturedCut(cur, h, changed, newKeys, false) // cur is HEAD as read: whole l.shadowCommit(captured, capturedVersions) persistCapturedMap(capturedVersions) - return l.cutDone(chunkCutResult{H: h, Head: copyHead(cur), FullHash: all}, detach), nil + return l.cutDone(chunkCutResult{H: h, Head: resultHead(cur), FullHash: all}, detach), nil } // HEAD moved past the CAS's base without it: it can never land, and // what it staged was compared against a HEAD that is gone. @@ -1761,14 +1761,14 @@ func (l *chunkLayerImpl) SettleDetach(ctx context.Context) (settled detachSettle l.pendingCaptured, l.pendingChanges, l.pendingMarked = nil, nil, nil switch { case proposalLanded(cur, *pending, base.Gen, changes, marked): - l.noteSettled(l.head, cur) + l.noteSettledFromHeadLocked(cur) l.setHeadLocked(copyHead(cur)) l.remoteLSN = cur.RemoteLSN l.shadowCommitCaptured(landed, true) - cut.Head = copyHead(cur) + cut.Head = resultHead(cur) l.detached = &cut r := cut - r.Head = copyHead(cur) + r.Head = resultHead(cur) return detachApplied, r, nil case l.supersededBy(cur): l.fenced = true @@ -2018,6 +2018,21 @@ func cutResultHead(h chunkHead, byChange bool) chunkHead { return out } +// resultHead is the HEAD every cut, settlement and detach outcome reports and +// keeps: identity only on the v1.1 format, on every path (a whole read's, +// a re-proposal's, a no-op's), so no result holds a chunk map; whole before. +func resultHead(h chunkHead) chunkHead { return cutResultHead(h, chunkV11Format) } + +// noteSettledFromHeadLocked records the publication from the layer's HEAD, +// whole (the ref table's image on the v1.1 format), to next, before next is +// installed. Caller holds stMu. +func (l *chunkLayerImpl) noteSettledFromHeadLocked(next chunkHead) { + if l.cfg.OnPublishSettled == nil || next.Gen == l.head.Gen { + return + } + l.noteSettled(l.wholeHeadLocked(), next) +} + // copyResultHead copies a cut's reported HEAD as it was reported: a result // that reports identity only (Chunks nil) stays so. func copyResultHead(h chunkHead) chunkHead { @@ -2150,13 +2165,13 @@ func (l *chunkLayerImpl) resolvePending(ctx context.Context) (err error) { if inPlace { l.applyCommitLocked(cur, changedIndices(changes)) } else { - l.noteSettled(l.head, cur) + l.noteSettledFromHeadLocked(cur) l.setHeadLocked(copyHead(cur)) } l.remoteLSN = cur.RemoteLSN l.shadowCommitCaptured(landed, true) if wasDetach { - cut.Head = cutResultHead(cur, inPlace) + cut.Head = resultHead(cur) l.detached = &cut return errChunkLayerDetached } @@ -2498,7 +2513,7 @@ func (l *chunkLayerImpl) finishCut(head chunkHead, h uint64, changed []uint64, n if inPlace { l.applyCommitLocked(head, changed) } else { - l.noteSettled(l.head, head) + l.noteSettledFromHeadLocked(head) l.setHeadLocked(copyHead(head)) } l.remoteLSN, l.pending = h, nil diff --git a/seaweed-block/core/storage/chunk_proposal_by_change_test.go b/seaweed-block/core/storage/chunk_proposal_by_change_test.go index d29b006275..b0c574ccdd 100644 --- a/seaweed-block/core/storage/chunk_proposal_by_change_test.go +++ b/seaweed-block/core/storage/chunk_proposal_by_change_test.go @@ -107,9 +107,19 @@ func TestV11LostCommitReplySettlesByChange(t *testing.T) { if err != nil { t.Fatalf("a landed commit by change whose reply was lost: %v", err) } - if res.Head.Chunks[9] != res.Head.Chunks[2] { + cur, err := f.GetHead(ctTimeout(t)) // HEAD as read back (the hook changed it after the install) + if err != nil { + t.Fatal(err) + } + if res.Head.Gen != cur.Gen || cur.Chunks[9] != cur.Chunks[2] { t.Fatal("the settled HEAD is not the one read back") } + l.stMu.Lock() + got9 := l.headChunk(9) + l.stMu.Unlock() + if got9 != cur.Chunks[9] { + t.Fatal("the layer did not install the HEAD it read back") + } } // On the v1.1 format a cut copies HEAD's chunks for its candidates only: its diff --git a/seaweed-block/core/storage/chunk_v11_outcomes_test.go b/seaweed-block/core/storage/chunk_v11_outcomes_test.go new file mode 100644 index 0000000000..e0d660c7c5 --- /dev/null +++ b/seaweed-block/core/storage/chunk_v11_outcomes_test.go @@ -0,0 +1,174 @@ +package storage + +import ( + "context" + "errors" + "reflect" + "sync" + "testing" +) + +// v11ReportLayer is a v1.1 layer over a fake remote with chunks 0 and 1 +// published, recording every publication report from then on. +func v11ReportLayer(t *testing.T) (*WALStore, *fakeChunkRemote, *chunkLayerImpl, *writeLog, func() [][2]chunkHead) { + t.Helper() + withV11Format(t) + s, _ := v11Store(t) + f := newFakeChunkRemote() + l := v11Attach(t, s, f) + w := newWriteLog() + w.write(t, s, 0, 0x10) + w.write(t, s, ctChunkBlocks, 0x11) + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + var mu sync.Mutex + var got [][2]chunkHead + l.cfg.OnPublishSettled = func(_ context.Context, prev, next ChunkHead) { + mu.Lock() + got = append(got, [2]chunkHead{prev, next}) + mu.Unlock() + } + return s, f, l, w, func() [][2]chunkHead { mu.Lock(); defer mu.Unlock(); return append([][2]chunkHead(nil), got...) } +} + +// checkReportedWhole: the first report after the hook went on names the +// remote's HEAD before the settled one as prev, whole, and the settled one +// as next, whole. +func checkReportedWhole(t *testing.T, f *fakeChunkRemote, reports [][2]chunkHead) { + t.Helper() + if len(reports) == 0 { + t.Fatal("the settlement reported no publication") + } + prev, next := reports[0][0], reports[0][1] + heads := f.installedHeads() + var before, settled chunkHead + for _, h := range heads { + if h.Gen == next.Gen { + settled = h + } + if h.Gen == prev.Gen { + before = h + } + } + if len(before.Chunks) == 0 || !reflect.DeepEqual(prev.Chunks, before.Chunks) { + t.Fatalf("the report's prev (gen %d) names %d chunks; the remote's HEAD then named %d", prev.Gen, len(prev.Chunks), len(before.Chunks)) + } + if !reflect.DeepEqual(next.Chunks, settled.Chunks) { + t.Fatalf("the report's next (gen %d) names %d chunks; the remote's HEAD names %d", next.Gen, len(next.Chunks), len(settled.Chunks)) + } +} + +// On the v1.1 format, where the layer's HEAD keeps no chunk map, every +// settlement that installs a HEAD read whole reports the previous image +// whole, from the ref table: a lost reply read back, a re-proposal read +// back, a detach settled. +func TestV11SettlementsReportTheWholePreviousImage(t *testing.T) { + t.Run("lost reply read back", func(t *testing.T) { + s, f, l, w, reports := v11ReportLayer(t) + w.write(t, s, 2*ctChunkBlocks, 0x22) + f.mu.Lock() + f.casModes = []fakeCASMode{casAppliedLost} + f.mu.Unlock() + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + checkReportedWhole(t, f, reports()) + }) + t.Run("re-proposal read back", func(t *testing.T) { + s, f, l, w, reports := v11ReportLayer(t) + w.write(t, s, 2*ctChunkBlocks, 0x22) + f.mu.Lock() + f.casModes = []fakeCASMode{casUnknownNotApplied, casAppliedLost} + f.mu.Unlock() + if _, err := l.Cut(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) { + t.Fatalf("cut with an unknown outcome: %v", err) + } + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatalf("settling cut: %v", err) + } + checkReportedWhole(t, f, reports()) + }) + t.Run("detach settled", func(t *testing.T) { + s, f, l, w, reports := v11ReportLayer(t) + w.write(t, s, 2*ctChunkBlocks, 0x22) + f.mu.Lock() + f.casModes = []fakeCASMode{casAppliedLost} + f.beforeCAS = func(f *fakeChunkRemote) { f.failGet = errFakeRemoteDown } + f.mu.Unlock() + if _, err := l.Detach(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) { + t.Fatalf("detach with an unresolved CAS: %v", err) + } + healRemote(f) + if settled, _, err := l.SettleDetach(ctTimeout(t)); err != nil || settled != detachApplied { + t.Fatalf("SettleDetach: %v %v, want applied", settled, err) + } + checkReportedWhole(t, f, reports()) + }) +} + +// On the v1.1 format every outcome reports and keeps HEAD's identity only: +// a no-op cut, a lost reply read back, a detach and its repeat, a settled +// detach; a detached layer holds no chunk map. +func TestV11OutcomesKeepNoChunkMap(t *testing.T) { + noMap := func(t *testing.T, what string, h chunkHead) { + t.Helper() + if h.Gen == 0 || h.Chunks != nil || h.Segments != nil { + t.Fatalf("%s: gen %d, %d chunks, %d segments; want HEAD's identity only", what, h.Gen, len(h.Chunks), len(h.Segments)) + } + } + t.Run("no-op and read back", func(t *testing.T) { + s, f, l, w, _ := v11ReportLayer(t) + r, err := l.Cut(ctTimeout(t)) + if err != nil || !r.Noop { + t.Fatalf("no-op cut: %+v %v", r, err) + } + noMap(t, "a no-op cut", r.Head) + w.write(t, s, 2*ctChunkBlocks, 0x22) + f.mu.Lock() + f.casModes = []fakeCASMode{casAppliedLost} + f.mu.Unlock() + if r, err = l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + noMap(t, "a lost reply read back", r.Head) + }) + t.Run("detach", func(t *testing.T) { + _, _, l, _, _ := v11ReportLayer(t) + r, err := l.Detach(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + noMap(t, "a detach", r.Head) + again, err := l.Detach(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + noMap(t, "a repeated detach", again.Head) + l.stMu.Lock() + kept := l.detached.Head + l.stMu.Unlock() + noMap(t, "the detached layer's kept result", kept) + }) + t.Run("detach settled", func(t *testing.T) { + s, f, l, w, _ := v11ReportLayer(t) + w.write(t, s, 2*ctChunkBlocks, 0x22) + f.mu.Lock() + f.casModes = []fakeCASMode{casAppliedLost} + f.beforeCAS = func(f *fakeChunkRemote) { f.failGet = errFakeRemoteDown } + f.mu.Unlock() + if _, err := l.Detach(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) { + t.Fatalf("detach with an unresolved CAS: %v", err) + } + healRemote(f) + _, r, err := l.SettleDetach(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + noMap(t, "a settled detach", r.Head) + l.stMu.Lock() + kept := l.detached.Head + l.stMu.Unlock() + noMap(t, "the settled layer's kept result", kept) + }) +}