From 3bb31d2fa8b7ac01b9e67d6ed5b6f2b5b2cd8958 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 18:55:25 -0800 Subject: [PATCH] layout: a later cleaning reclaims a name a dead remover marked The removal planned only unmarked candidates, so a name marked by a cleaning that died before its delete stayed in the pool for ever. Every found candidate is now planned with its whole tag (session and mark) as read; the conditional mark replaces an abandoned mark, and the dead remover's late delete, conditional on its own mark, fails. The interrupt test now requires every orphan gone after the re-run. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/layout/remove.go | 9 +++-- .../frontend/chunkstore/layout/remove_test.go | 40 ++++++++++++++----- seaweed-block/docs/design/invariants.md | 3 +- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/layout/remove.go b/seaweed-block/core/frontend/chunkstore/layout/remove.go index 998e3d4d42..d84751bf69 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/remove.go +++ b/seaweed-block/core/frontend/chunkstore/layout/remove.go @@ -23,8 +23,11 @@ import ( // its writes: a session that names a chunk no root named at its Begin // writes that chunk's entry (create, or reuse with a new tag), so the MARK // or the DELETE fails its condition, however late it lands. No lease -// reasoning is needed. A candidate marked by another session is left -// alone. Chunks a restore target names stay live: the source's snapshot +// reasoning is needed. A candidate an earlier session marked and did not +// delete (it died in between) is planned again: its tag carries that +// mark, so the MARK replaces it, and the earlier session's late DELETE, +// conditional on its own mark, fails. Every unreachable name is therefore +// removed by the next completed cleaning. Chunks a restore target names stay live: the source's snapshot // root names them while it has dependents. It returns the names it // deleted, in index order. func RemoveUnreachable(ctx context.Context, st HeadStore, r *Reader, os ObjectStore, me HolderID, session string, shard uint64, candidates []ChunkName) ([]ChunkName, error) { @@ -42,7 +45,7 @@ func RemoveUnreachable(ctx context.Context, st HeadStore, r *Reader, os ObjectSt if err != nil { return nil, err } - if found && meta.Mark == "" { + if found { plan[c] = meta } } diff --git a/seaweed-block/core/frontend/chunkstore/layout/remove_test.go b/seaweed-block/core/frontend/chunkstore/layout/remove_test.go index 645e2391c1..a48faa2145 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/remove_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/remove_test.go @@ -219,17 +219,37 @@ func TestRemoveUnreachable_ASuccessorBeforeTheHeadViewStopsThePass(t *testing.T) } } -// A candidate already marked by another session is left alone. -func TestRemoveUnreachable_LeavesAnotherSessionsMark(t *testing.T) { +// A candidate an earlier session marked and never deleted (it died in +// between) is reclaimed by the next cleaning, and the earlier session's +// late delete, conditional on its own mark, fails once it is re-marked. +func TestRemoveUnreachable_ReclaimsAnAbandonedMark(t *testing.T) { + ctx := context.Background() o, h, _, orphans := removeFixture(t) + var p5 string for _, c := range orphans { if c.Index == 5 { - o.objs[path.Join(volRoot, c.Path())].meta.Mark = "c0" + p5 = path.Join(volRoot, c.Path()) + o.objs[p5].meta.Mark = "c0" } } removed, err := removeShard0(t, &memHead{h: h}, o, "c1", orphans) - if err != nil || len(removed) != 1 || removed[0].Index != 7 { - t.Fatalf("removed %v %v; want only chunk 7 (5 carries another session's mark)", removed, err) + if err != nil || len(removed) != 2 { + t.Fatalf("removed %v %v; want chunks 5 (abandoned mark) and 7", removed, err) + } + o2, _, _, orphans2 := removeFixture(t) + var c5 ChunkName + for _, c := range orphans2 { + if c.Index == 5 { + c5 = c + o2.objs[path.Join(volRoot, c.Path())].meta.Mark = "c0" + } + } + tag, _, _ := o2.Stat(ctx, c5.Path()) + if err := o2.Mark(ctx, c5.Path(), tag, "c1"); err != nil { + t.Fatal(err) + } + if err := o2.DeleteMarked(ctx, c5.Path(), "c0"); !errors.Is(err, ErrPrecondition) { + t.Fatalf("the dead session's late delete after the re-mark: %v; want ErrPrecondition", err) } } @@ -286,9 +306,9 @@ func cleaningPass(v *memVolume, session string, cands []ChunkName) error { // writes (BeginClean, each mark, each conditional delete, EndClean), before // the write applies and after it applied with its reply lost; at no point // does HEAD reach a missing object, and the restarted holder (a new -// session) ends idle with every orphan of shard 0 removed and every -// reachable object present. A mark left by the killed session is cleared -// by nothing but a reuse, so a marked orphan stays until then. +// session) ends idle with every orphan of shard 0 removed (also one the +// killed session marked and did not delete) and every reachable object +// present. func TestCleaningPass_InterruptAtEveryStep(t *testing.T) { fresh := func() (*memVolume, []ChunkName, []ChunkName) { o, h, live, orphans := removeFixture(t) @@ -331,8 +351,8 @@ func TestCleaningPass_InterruptAtEveryStep(t *testing.T) { for _, c := range orphans { p := path.Join(volRoot, c.Path()) _, present := v.objects[p] - if c.Index != 10001 && present && v.meta(p).Mark != "c1" { - t.Fatalf("orphan %s still present and not held by the killed session's mark", c) + if c.Index != 10001 && present { + t.Fatalf("orphan %s still present after the re-run (mark %q)", c, v.meta(p).Mark) } } for _, c := range live { diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index bb1d739f40..3403bf1914 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -545,7 +545,7 @@ stateDiagram-v2 ## Part B. Invariant ledger -343 rows: 272 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 328 new. +344 rows: 273 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 329 new. The "Machine / transition" column refers to Part A. Notes under a table record what a row does not cover and, for kept rows, what was narrowed from the internal ledger. @@ -1097,6 +1097,7 @@ Notes: | `INV-SNAPK-READ-MISSING-001` | A read resolves root, segment, chunk; an index the root does not name is zeros; a root, segment or chunk the root reaches but the store lacks is ErrMissing, never zeros; bytes that do not hash to their name are ErrCorrupt; a foreign chunk is read from its owner's pool. | Content-named layout (read path, no state transition) | `core/frontend/chunkstore/layout` `read.go` `Reader.Open`, `Image.Segment`, `Image.Chunk` | `TestRead_AbsentIndexIsZeros`, `TestRead_ForeignChunkFromSourcePool`, `TestRead_MissingObjectIsAnErrorNeverZeros`, `TestRead_FetchFailureAndCorruption`, `TestRead_SegmentOfAnotherShardRefused` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-REMOVE-MARK-001` | A pool name is removed only inside the holder's own cleaning, when no root in HEAD names it: tags read first, HEAD read and validated after, then a mark conditional on the tag and a delete conditional on the mark. | M9 clean → clean (RemoveUnreachable) | `core/frontend/chunkstore/layout` `remove.go` `RemoveUnreachable` | `TestRemoveUnreachable_RemovesOnlyWhatNoRootNames`, `TestCleaningPass_InterruptAtEveryStep`, `TestHolderFunctions_RefuseAStaleHolder` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-REMOVE-STOP-001` | A reuse of an object after the plan, or after the mark, makes the remover's mark or delete fail its condition, however late it lands; an unreadable root stops the pass before any write; an open snapshot refuses it. | M9 clean → clean (RemoveUnreachable) | `core/frontend/chunkstore/layout` `remove.go` `RemoveUnreachable` | `TestRemoveUnreachable_ASuccessorAfterTheHeadViewDefeatsTheMark`, `TestRemoveUnreachable_ASuccessorBeforeTheHeadViewStopsThePass`, `TestRemoveUnreachable_AReuseAfterThePlanDefeatsTheMark`, `TestRemoveUnreachable_AReuseDefeatsALateMarkOrDelete`, `TestRemoveUnreachable_NothingRemovedOnAPartialView`, `TestRemoveUnreachable_RefusedOutsideItsCleaning` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-REMOVE-CONVERGE-001` | A name a cleaning marked and did not delete (the remover died in between) is planned again by the next cleaning: the re-mark replaces the old mark, so the dead remover's late delete fails, and every unreachable name goes once a cleaning completes. | M9 clean → clean | `core/frontend/chunkstore/layout` `remove.go` `RemoveUnreachable` | `TestRemoveUnreachable_ReclaimsAnAbandonedMark`, `TestCleaningPass_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-REMOVE-GUARD-001` | A source scan requires DeleteMarked as RemoveUnreachable's only removal, each immediately after an if on its Mark's error that leaves: an unconditional delete, an ignored mark, a reused mark or another method fails. | M9 clean → clean (RemoveUnreachable) | `core/frontend/chunkstore/layout` (source scan of `remove.go`) | `TestGuard_WritersTransitionsAndRemovers`, `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-REUSE-001` | Writing an object for a session creates it, or reuses an existing one by a conditional update that sets the session and clears any mark; a commit names only objects its session wrote or reused, unmarked. | M9 snap → idle (Upload, Commit) | `core/frontend/chunkstore/layout` `objects.go` `EnsureObject`; `commit.go` `RunCommit` | `TestEnsureObject`, `TestRunCommit_NeedsObjectsOfItsOwnSession`, `TestSnapshotSession_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-STALE-HOLDER-001` | Every transition refuses an older epoch of the same server, the same epoch held by another server, and the same epoch and server with another claim id, in idle, snap and clean; a new transition must be added to the table. | M9 every transition | `core/frontend/chunkstore/layout` `Head.heldBy` and each transition | `TestTransitions_RefuseAStaleHolder` | — (the snapshot kernel model is not in this repository) | ACTIVE |