From e1a2cdd4e3013b5afbc67e96952ca615c1e72da5 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 22:00:55 -0800 Subject: [PATCH] fix(chunkstore): the commit check returns at once for the checked commit in its open session A re-proposal after an unknown outcome runs the commit check again before its CAS; the check forgot the kept commit first and looked up every object again. It now returns at once for exactly the checked commit in its still open session, the same match the CAS uses to reuse the preparation. The re-proposal test counts every lookup, not only those inside the CAS. Co-Authored-By: Claude Opus 5.5 --- .../layout_commit_retry_plan_test.go | 24 +++++++++++++++---- .../core/frontend/chunkstore/layout_remote.go | 18 ++++++++++++-- .../chunkstore/layout_stop_session_test.go | 8 +++---- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/layout_commit_retry_plan_test.go b/seaweed-block/core/frontend/chunkstore/layout_commit_retry_plan_test.go index 48f52de033..aa0a25d598 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_commit_retry_plan_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_commit_retry_plan_test.go @@ -49,11 +49,20 @@ func c1Layer(t *testing.T) (*layoutHost, *c1Remote, storage.ChunkLayer) { // A HEAD update whose outcome is unknown is proposed again in the same open // session; that attempt keeps the passed object check and its plan, so it -// looks up no chunk. Clearing the check before the commit made every -// re-proposal of a 1 TiB cut redo all its lookups inside the HEAD update's -// budget, so the commit never finished. +// looks up no chunk, neither in its check nor in its CAS. Clearing the check +// before the commit made every re-proposal of a 1 TiB cut redo all its +// lookups inside the HEAD update's budget, so the commit never finished; +// redoing them in the re-proposal's check wastes the same work outside it. func TestReproposalAfterAnUnknownCommitOutcomeLooksUpNothing(t *testing.T) { lh, w, l := c1Layer(t) + var total atomic.Int64 + inCAS := lh.ff.afterLookup + lh.ff.afterLookup = func(p string) { + inCAS(p) + if strings.Contains(p, "/pool/") { + total.Add(1) + } + } var lost atomic.Bool lh.ff.failUpdate = func(p string) error { // under ff.mu: the update is lost before it applies if strings.HasSuffix(p, "/HEAD") && w.inCAS.Load() && !lost.Load() { @@ -71,11 +80,18 @@ func TestReproposalAfterAnUnknownCommitOutcomeLooksUpNothing(t *testing.T) { if n := w.casGot.Load(); n != 0 { t.Fatalf("setup: the first CAS looked up %d chunks after a passed pre-check", n) } + if total.Load() == 0 { + t.Fatal("setup: the first cut's check looked up no chunk") + } + total.Store(0) if _, err := l.Cut(context.Background()); err != nil { t.Fatalf("the re-proposal after the unknown outcome: %v", err) } if n := w.casGot.Load(); n != 0 { - t.Fatalf("the re-proposal in the same session looked up %d chunks; the passed check was dropped", n) + t.Fatalf("the re-proposal in the same session looked up %d chunks in its CAS; the passed check was dropped", n) + } + if n := total.Load(); n != 0 { + t.Fatalf("the re-proposal in the same session looked up %d chunks in all; the passed check was redone", n) } if lh.head().Current == nil { t.Fatal("no commit landed") diff --git a/seaweed-block/core/frontend/chunkstore/layout_remote.go b/seaweed-block/core/frontend/chunkstore/layout_remote.go index 99cfe80faa..5a1386db67 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_remote.go +++ b/seaweed-block/core/frontend/chunkstore/layout_remote.go @@ -356,10 +356,10 @@ func (l *LayoutRemote) prepareCommitLocked(ctx context.Context, expected storage if expected.Gen != l.visible { return layout.Head{}, layout.CommitRequest{}, false, storage.ErrChunkHeadPrecondition } - if c := &l.checked; c.session != "" && c.session == l.session && c.expected == expected && reflect.DeepEqual(c.next, next) { + if l.isCheckedLocked(expected, next) { // The checked commit, in its still open session: its manifests, root // and segments are already prepared and its plan built. - return h, c.req, true, nil + return h, l.checked.req, true, nil } prev, nextM, err := l.manifests(ctx, h, next) if err != nil { @@ -387,6 +387,13 @@ func (l *LayoutRemote) prepareCommitLocked(ctx context.Context, expected storage func (l *LayoutRemote) CheckCommit(ctx context.Context, expected storage.ChunkHeadVersion, next storage.ChunkHead) error { l.mu.Lock() defer l.mu.Unlock() + l.takeAbandonLocked() + if l.isCheckedLocked(expected, next) { + // Already checked in this open session (a re-proposal after an + // unknown outcome): the check holds for every attempt of the + // session, and redoing it would look up every object again. + return nil + } l.clearCheckedLocked() h, req, _, err := l.prepareCommitLocked(ctx, expected, next) if err != nil { @@ -441,6 +448,13 @@ func (l *LayoutRemote) commitStepLocked(ctx context.Context, req layout.CommitRe return layout.RunCommit(step, l.st, l.st, l.vol.Name, l.me, req, layout.CauseCommit) } +// isCheckedLocked reports whether expected and next are exactly the checked +// commit, in its still open session. +func (l *LayoutRemote) isCheckedLocked(expected storage.ChunkHeadVersion, next storage.ChunkHead) bool { + c := &l.checked + return c.session != "" && c.session == l.session && c.expected == expected && reflect.DeepEqual(c.next, next) +} + // clearCheckedLocked forgets the checked commit. func (l *LayoutRemote) clearCheckedLocked() { l.checked.session, l.checked.expected, l.checked.next, l.checked.req = "", storage.ChunkHeadVersion{}, storage.ChunkHead{}, layout.CommitRequest{} diff --git a/seaweed-block/core/frontend/chunkstore/layout_stop_session_test.go b/seaweed-block/core/frontend/chunkstore/layout_stop_session_test.go index f7eec5eee1..f726ff44a4 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_stop_session_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_stop_session_test.go @@ -29,12 +29,12 @@ func TestLayoutRemoteSessionFieldSites(t *testing.T) { } // Begin returns the session it opened; CASHead checks that it survived // a concurrent operator HEAD write before retrying. CheckCommit records - // the checked session, prepareCommitLocked matches a call against it - // (the reuse of its preparation and the skip of the object check), and - // clearCheckedLocked forgets it. + // the checked session, isCheckedLocked matches a call against it (for + // prepareCommitLocked's reuse of its preparation and CheckCommit's + // immediate return), and clearCheckedLocked forgets it. // ProbeSnapshots skips open activity // (TestLayoutSnapshotProbeSkipsOpenActivityWithoutReadingHEAD). - if !reflect.DeepEqual(fields, map[string]int{"openSessionLocked": 2, "rollBackLocked": 1, "Begin": 1, "PutPaced": 1, "CASHead": 2, "CheckCommit": 1, "clearCheckedLocked": 1, "prepareCommitLocked": 4, "CommitPendingSnapshot": 2, "writeRootLocked": 1, "Clean": 1, "syncSessionLocked": 3, "takeAbandonLocked": 1, "ProbeSnapshots": 1}) { + if !reflect.DeepEqual(fields, map[string]int{"openSessionLocked": 2, "rollBackLocked": 1, "Begin": 1, "PutPaced": 1, "CASHead": 2, "CheckCommit": 1, "clearCheckedLocked": 1, "isCheckedLocked": 3, "prepareCommitLocked": 1, "CommitPendingSnapshot": 2, "writeRootLocked": 1, "Clean": 1, "syncSessionLocked": 3, "takeAbandonLocked": 1, "ProbeSnapshots": 1}) { t.Fatal("session field sites changed; enumerate and probe them", fields) } }