From 455984013429f99d8b5b9107e036df2485cfe86b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 18:33:59 -0800 Subject: [PATCH] layout: volume directories never reused; a tombstone for a target never created A volume's directory is -, created exclusively and never reused; the reader reads a foreign chunk from the directory its reference's owner name and id give, after checking that directory's VOLUME id. For a restore target never created, the operator's remove-dependent writes a deleted HEAD exclusively in the target's directory (a tombstone, which needs no holder) and only then releases the dependent, so the restore can never create the target afterwards; if the target's HEAD appears first, the release is refused. Only that command creates a tombstone (source guard). Co-Authored-By: Claude Opus 5.5 --- .../frontend/chunkstore/head_readers_test.go | 20 +++++-- .../core/frontend/chunkstore/layout/delete.go | 41 ++++++++++---- .../frontend/chunkstore/layout/delete_test.go | 40 +++++++++++-- .../frontend/chunkstore/layout/guard_test.go | 14 +++-- .../core/frontend/chunkstore/layout/head.go | 15 +++-- .../frontend/chunkstore/layout/head_test.go | 23 ++++++++ .../core/frontend/chunkstore/layout/read.go | 10 ++-- .../frontend/chunkstore/layout/read_test.go | 56 ++++++++++--------- .../core/frontend/chunkstore/layout/volume.go | 8 +++ seaweed-block/docs/design/invariants.md | 8 ++- 10 files changed, 173 insertions(+), 62 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/head_readers_test.go b/seaweed-block/core/frontend/chunkstore/head_readers_test.go index dc152c6de3..7443284957 100644 --- a/seaweed-block/core/frontend/chunkstore/head_readers_test.go +++ b/seaweed-block/core/frontend/chunkstore/head_readers_test.go @@ -40,7 +40,7 @@ var headReaders = map[string]string{ "core/frontend/chunkstore/share_guard.go beginShare -> ReadHead": "refuses outcome_unknown: the HEAD to share is no longer current", "core/frontend/chunkstore/usage.go Usage -> ReadHead": "GAP: reports an empty image, no error (probe Usage)", "core/frontend/chunkstore/verify.go readHead -> LookupEntry": "finding ROOT_INVALID (probe Verify)", - "core/frontend/chunkstore/layout/delete.go OperatorReleaseDependent -> ReadHead": "reads the source's HEAD (absent: refuses ErrHeadAbsent) and the target's: a target with no HEAD and no VOLUME was never created and is refused, one with VOLUME and no HEAD is being created and is refused; nothing is written (probe LayoutOperatorRelease)", + "core/frontend/chunkstore/layout/delete.go OperatorReleaseDependent -> ReadHead": "reads the source's HEAD (absent: refuses ErrHeadAbsent) and the target's: with no HEAD and no VOLUME the target was never created and gets a deleted HEAD written exclusively before the release; with VOLUME and no HEAD it is being created and is refused (probe LayoutOperatorRelease)", "core/frontend/chunkstore/layout/remove.go RemoveUnreachable -> ReadHead": "refuses ErrHeadAbsent and writes nothing to the objects (probe LayoutRemoval)", "core/frontend/chunkstore/layout/head.go runTransition -> ReadHead": "refuses ErrHeadAbsent and writes nothing; no layout transition creates a HEAD (probe LayoutTransition)", "core/frontend/chunkstore/verify.go run -> LookupEntry": "finding ROOT_INVALID (probe Verify)", @@ -298,8 +298,9 @@ func TestHeadReaders_AbsentHeadProbes(t *testing.T) { t.Fatalf("operator release of a target with VOLUME and no HEAD: %v, %d writes; want ErrTargetLive and none", err, src.updates) } none, _ := layout.NewReader("/swblock/chunks", "t", func(context.Context, string) ([]byte, bool, error) { return nil, false, nil }) - if _, err := layout.OperatorReleaseDependent(ctx, src, &absentLayoutHead{}, none, tid); !errors.Is(err, layout.ErrTargetNeverCreated) || src.updates != 0 { - t.Fatalf("operator release of a target never created: %v, %d writes; want ErrTargetNeverCreated and none", err, src.updates) + tomb := &absentLayoutHead{} + if _, err := layout.OperatorReleaseDependent(ctx, src, tomb, none, tid); err != nil || tomb.created == nil || tomb.created.State != layout.StateDeleted || tomb.created.VolumeID != tid { + t.Fatalf("operator release of a target never created: %v, tombstone %+v; want a deleted HEAD of the target's id written before the release", err, tomb.created) } absent := &absentLayoutHead{} if _, err := layout.OperatorReleaseDependent(ctx, absent, &absentLayoutHead{}, r, tid); !errors.Is(err, layout.ErrHeadAbsent) || absent.updates != 0 { @@ -328,8 +329,17 @@ func TestHeadReaders_AbsentHeadProbes(t *testing.T) { }) } -// absentLayoutHead is a layout HEAD store whose HEAD is absent. -type absentLayoutHead struct{ updates int } +// absentLayoutHead is a layout HEAD store whose HEAD is absent; it records +// a tombstone created in it. +type absentLayoutHead struct { + updates int + created *layout.Head +} + +func (a *absentLayoutHead) CreateDeletedHead(_ context.Context, h layout.Head) error { + a.created = &h + return nil +} func (a *absentLayoutHead) ReadHead(context.Context) (layout.Head, bool, error) { return layout.Head{}, false, nil diff --git a/seaweed-block/core/frontend/chunkstore/layout/delete.go b/seaweed-block/core/frontend/chunkstore/layout/delete.go index ab8f860f7a..642423b43f 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/delete.go +++ b/seaweed-block/core/frontend/chunkstore/layout/delete.go @@ -26,11 +26,17 @@ var ( // ErrTargetLive: the operator asked to release a dependent whose // target still exists and may still name the source's chunks. ErrTargetLive = errors.New("layout: the dependent's target is live") - // ErrTargetNeverCreated: the dependent's target has neither VOLUME nor - // HEAD; the restore that registered it could still create it. - ErrTargetNeverCreated = errors.New("layout: the dependent's target was never created") ) +// TargetStore is a restore target's HEAD entry as the operator's +// remove-dependent sees it: read, and one exclusive create of a deleted +// HEAD (a tombstone) in the target's never-reused directory; ErrExists if +// a HEAD is already there. +type TargetStore interface { + ReadHead(ctx context.Context) (h Head, found bool, err error) + CreateDeletedHead(ctx context.Context, h Head) error +} + // HolderGone is the deletion's premise: it returns nil only when no block // holds the volume (no line or lease the master grants). DeleteVolume calls // it immediately before marking HEAD deleting. @@ -158,14 +164,15 @@ func DeleteVolume(ctx context.Context, st HeadStore, gone HolderGone, id VolumeI // OperatorReleaseDependent is the operator's remove-dependent command: it // releases the target from the source's dependents only when the target // exists and is deleted or no longer names the source, read first: its -// HEAD is deleting or deleted; its name now holds another volume (another -// VOLUME id); or its HEAD is live and no root in it (current or snapshot) -// names a chunk of the source. A live target that still names the source, -// or one being created (VOLUME without HEAD), is refused (ErrTargetLive). -// A target never created (no VOLUME, no HEAD) is refused too -// (ErrTargetNeverCreated): the restore could still create it after the -// release. The source is not written when refused. -func OperatorReleaseDependent(ctx context.Context, source, target HeadStore, r *Reader, targetID VolumeID) (Head, error) { +// HEAD is deleting or deleted; its directory's VOLUME carries another id; +// or its HEAD is live and no root in it (current or snapshot) names a +// chunk of the source. A target never created (no VOLUME, no HEAD) first +// gets a deleted HEAD written exclusively in its never-reused directory, +// so the restore that registered it can never create it later, and is +// then released. A live target that still names the source, one being +// created (VOLUME without HEAD), and one whose HEAD appeared before the +// tombstone are refused (ErrTargetLive); the source is not written then. +func OperatorReleaseDependent(ctx context.Context, source HeadStore, target TargetStore, r *Reader, targetID VolumeID) (Head, error) { sh, found, err := source.ReadHead(ctx) if err != nil { return Head{}, err @@ -186,7 +193,17 @@ func OperatorReleaseDependent(ctx context.Context, source, target HeadStore, r * case hasHead && (th.State == StateDeleting || th.State == StateDeleted): case hasVolume && id != targetID: case !hasVolume && !hasHead: - return Head{}, fmt.Errorf("%w: %s", ErrTargetNeverCreated, r.vol) + // Never created: a deleted HEAD written exclusively in the target's + // never-reused directory makes sure the restore can never create it + // later; only then is the dependent released. If a HEAD appeared + // first, the restore got there: refused, re-run decides. + tomb := Head{VolumeID: targetID, Activity: Activity{Kind: ActIdle}, State: StateDeleted} + if err := target.CreateDeletedHead(ctx, tomb); err != nil { + if errors.Is(err, ErrExists) { + return Head{}, fmt.Errorf("%w: %s appeared while being tombstoned; run again", ErrTargetLive, r.vol) + } + return Head{}, err + } case hasVolume && hasHead: names, err := namesVolume(ctx, r, th, sh.VolumeID) if err != nil { diff --git a/seaweed-block/core/frontend/chunkstore/layout/delete_test.go b/seaweed-block/core/frontend/chunkstore/layout/delete_test.go index b675f4d0ff..94b56516ed 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/delete_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/delete_test.go @@ -315,9 +315,11 @@ func operatorCase(t *testing.T, tgtHead *Head, tgtVolume *VolumeID, namesSource // exists and is deleted or no longer names the source: its HEAD deleting // or deleted, its name holding another volume (another VOLUME id), or a // live HEAD none of whose roots names a chunk of the source. A live target -// that still names the source, one being created (VOLUME without HEAD) and -// one never created (no VOLUME, no HEAD: the restore could still create -// it) are refused and the source is unchanged. +// that still names the source and one being created (VOLUME without HEAD) +// are refused and the source is unchanged. A target never created (no +// VOLUME, no HEAD) gets a deleted HEAD written exclusively in its +// never-reused directory, so the restore can never create it later, and +// only then is released. func TestOperatorReleaseDependent(t *testing.T) { ctx := context.Background() live := idleHead() @@ -335,7 +337,7 @@ func TestOperatorReleaseDependent(t *testing.T) { "live target naming the source": {&live, &tid, true, false, ErrTargetLive}, "live target no longer naming it": {&live, &tid, false, true, nil}, "target being created": {nil, &tid, false, false, ErrTargetLive}, - "target never created": {nil, nil, false, false, ErrTargetNeverCreated}, + "target never created": {nil, nil, false, true, nil}, "target deleting": {&deleting, &tid, true, true, nil}, "name holds another volume": {nil, &other, false, true, nil}, } { @@ -343,6 +345,9 @@ func TestOperatorReleaseDependent(t *testing.T) { before := src.updates _, err := OperatorReleaseDependent(ctx, src, tgt, r, tid) released := len(src.h.Snapshots["daily"].Dependents) == 0 + if tc.head == nil && tc.volume == nil && (tgt.h == nil || tgt.h.State != StateDeleted || tgt.h.VolumeID != tid) { + t.Errorf("%s: target HEAD %+v; want a deleted HEAD (tombstone) of the target's id written before the release", name, tgt.h) + } if tc.release && (err != nil || !released) { t.Errorf("%s: %v, released %t; want released", name, err, released) } @@ -351,3 +356,30 @@ func TestOperatorReleaseDependent(t *testing.T) { } } } + +// If the restore creates the target's HEAD between the operator's read and +// its tombstone, the exclusive create fails and nothing is released. +func TestOperatorReleaseDependent_TombstoneLosesToTheRestore(t *testing.T) { + src, tgt, r := operatorCase(t, nil, nil, false) + live := idleHead() + live.VolumeID = idFor("t") + created := &raceTarget{memHead: tgt, appear: &live} + before := src.updates + if _, err := OperatorReleaseDependent(context.Background(), src, created, r, idFor("t")); !errors.Is(err, ErrTargetLive) || src.updates != before { + t.Fatalf("tombstone after the restore created the HEAD: %v (%d source writes); want ErrTargetLive and none", err, src.updates-before) + } +} + +// raceTarget makes a HEAD appear right before the tombstone's create. +type raceTarget struct { + *memHead + appear *Head +} + +func (r *raceTarget) CreateDeletedHead(ctx context.Context, h Head) error { + if r.appear != nil { + n := r.appear.clone() + r.memHead.h, r.appear = &n, nil + } + return r.memHead.CreateDeletedHead(ctx, h) +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/guard_test.go b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go index afbe14bdc3..38e9210b1e 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/guard_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go @@ -171,6 +171,11 @@ func guardFindings(code map[string]map[string]bool, decl, tests map[string]bool) bad = append(bad, fn+" is declared and does not go through runTransition") } } + for _, fn := range owners(code["CreateDeletedHead"]) { + if fn != "OperatorReleaseDependent" { + bad = append(bad, fn+" uses CreateDeletedHead: only the operator's release writes a tombstone, for a target never created") + } + } for _, fn := range owners(code["RunReleaseDependent"]) { if fn != "DeleteVolume" && fn != "OperatorReleaseDependent" { bad = append(bad, fn+" uses RunReleaseDependent: only DeleteVolume (target deleting) and OperatorReleaseDependent (target checked) release") @@ -449,10 +454,11 @@ func DeleteVolume(ctx context.Context, st HeadStore, id VolumeID, srcs []HeadSto "operator release without reading the target": {file: "delete.go", src: `func OperatorReleaseDependent(ctx context.Context, source HeadStore, id VolumeID) (Head, error) { return RunReleaseDependent(ctx, source, id) }`, want: "does not read the target's HEAD"}, - "unconditional delete": {file: "remove.go", src: removeVariant(t, "unconditional"), want: "its only removal is DeleteMarked"}, - "ignored mark": {file: "remove.go", src: removeVariant(t, "ignored"), want: "without a mark gate immediately before it"}, - "one mark, two deletes": {file: "remove.go", src: removeVariant(t, "second"), want: "without a mark gate immediately before it"}, - "unlisted removal method": {file: "remove.go", src: removeVariant(t, "other"), want: "without a mark gate immediately before it"}, + "unconditional delete": {file: "remove.go", src: removeVariant(t, "unconditional"), want: "its only removal is DeleteMarked"}, + "ignored mark": {file: "remove.go", src: removeVariant(t, "ignored"), want: "without a mark gate immediately before it"}, + "one mark, two deletes": {file: "remove.go", src: removeVariant(t, "second"), want: "without a mark gate immediately before it"}, + "unlisted removal method": {file: "remove.go", src: removeVariant(t, "other"), want: "without a mark gate immediately before it"}, + "tombstone outside the operator's release": {src: `func w(ctx context.Context, t TargetStore, h Head) { _ = t.CreateDeletedHead(ctx, h) }`, want: "only the operator's release writes a tombstone"}, "remover method value": {src: `type p interface{ RemoveName(string) } func w(x p) { f := x.RemoveName; f("o") }`, want: "only RemoveUnreachable (mark-gated) and DeleteVolume"}, } { diff --git a/seaweed-block/core/frontend/chunkstore/layout/head.go b/seaweed-block/core/frontend/chunkstore/layout/head.go index b89dbec9a5..48ab207aa9 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/head.go +++ b/seaweed-block/core/frontend/chunkstore/layout/head.go @@ -125,11 +125,16 @@ func (h Head) check() error { default: return fmt.Errorf("%w: activity %q", ErrMalformed, h.Activity.Kind) } - if h.Epoch == 0 || h.Holder == "" { - return fmt.Errorf("%w: HEAD without epoch or holder", ErrMalformed) - } - if h.ClaimID == "" { - return fmt.Errorf("%w: HEAD holder %q without a claim id", ErrMalformed, h.Holder) + // A deleted HEAD written as a tombstone for a restore target never + // created has no holder; every other HEAD has an epoch, a holder and + // the claim id of its claim. + if h.State != StateDeleted || h.Holder != "" { + if h.Epoch == 0 || h.Holder == "" { + return fmt.Errorf("%w: HEAD without epoch or holder", ErrMalformed) + } + if h.ClaimID == "" { + return fmt.Errorf("%w: HEAD holder %q without a claim id", ErrMalformed, h.Holder) + } } if len(h.Snapshots) > MaxSnapshots { return fmt.Errorf("%w: %d snapshots, at most %d", ErrMalformed, len(h.Snapshots), MaxSnapshots) diff --git a/seaweed-block/core/frontend/chunkstore/layout/head_test.go b/seaweed-block/core/frontend/chunkstore/layout/head_test.go index ca166d531c..0f101ffb87 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/head_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/head_test.go @@ -207,6 +207,29 @@ func (m *memHead) UpdateHead(_ context.Context, gen uint64, next Head) error { return nil } +// CreateDeletedHead writes a deleted HEAD exclusively (a tombstone). +func (m *memHead) CreateDeletedHead(_ context.Context, h Head) error { + m.mu.Lock() + defer m.mu.Unlock() + if m.dead { + return errKilled + } + if m.h != nil { + return ErrExists + } + if h.State != StateDeleted { + return errors.New("only a deleted HEAD is created") + } + if _, err := h.Encode(); err != nil { + return err + } + n := h.clone() + n.Gen = 1 + m.h = &n + m.updates++ + return nil +} + func (m *memHead) restart() { m.mu.Lock(); m.dead = false; m.killAt = 0; m.mu.Unlock() } func TestRunBegin(t *testing.T) { diff --git a/seaweed-block/core/frontend/chunkstore/layout/read.go b/seaweed-block/core/frontend/chunkstore/layout/read.go index 3fa7de05ff..b05cfd6c84 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/read.go +++ b/seaweed-block/core/frontend/chunkstore/layout/read.go @@ -147,8 +147,9 @@ func (img *Image) Resolve(ctx context.Context, index uint64) (ref Ref, zero bool } // Chunk resolves index and reads its bytes from the pool of the volume -// that owns it, after checking that volume's VOLUME id is the one the -// reference carries (ErrIdentity otherwise). A chunk the root names but the +// that owns it, in that volume's directory (VolumeDir of the reference's +// owner name and id), after checking the directory's VOLUME id is the one +// the reference carries (ErrIdentity otherwise). A chunk the root names but the // owner's pool lacks is ErrMissing. func (img *Image) Chunk(ctx context.Context, index uint64) (Chunk, error) { ref, zero, err := img.Resolve(ctx, index) @@ -158,14 +159,15 @@ func (img *Image) Chunk(ctx context.Context, index uint64) (Chunk, error) { if zero { return Chunk{Zero: true}, nil } - id, err := img.r.VolumeID(ctx, ref.Owner) + dir := VolumeDir(ref.Owner, ref.OwnerID) + id, err := img.r.VolumeID(ctx, dir) if err != nil { return Chunk{}, err } if id != ref.OwnerID { return Chunk{}, fmt.Errorf("%w: chunk %d names %s id %s, VOLUME has %s", ErrIdentity, index, ref.Owner, ref.OwnerID, id) } - b, err := img.r.get(ctx, ref.Owner, ref.Chunk.Path(), ref.Chunk.Hash) + b, err := img.r.get(ctx, dir, ref.Chunk.Path(), ref.Chunk.Hash) if err != nil { return Chunk{}, err } diff --git a/seaweed-block/core/frontend/chunkstore/layout/read_test.go b/seaweed-block/core/frontend/chunkstore/layout/read_test.go index 67de891f2f..be87aa5bb8 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/read_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/read_test.go @@ -27,6 +27,12 @@ func (s *objStore) fetch(_ context.Context, p string) ([]byte, bool, error) { const base = "/swblock/chunks" +// The test volumes' directories: -. +var ( + tDir = VolumeDir("t", idFor("t")) + srcDir = VolumeDir("src", idFor("src")) +) + // readFixture: volume "t" (a restore target) whose root names chunk 3 // ("own") in its own pool, chunk 10002 ("src data") in the pool of "src", // and nothing else; every object is written. @@ -36,29 +42,29 @@ func readFixture(t *testing.T) (*objStore, RootName, map[uint64]Ref) { put := func(vol, rel string, b []byte) { s.obj[path.Join(base, vol, rel)] = b } own := NameChunk(3, []byte("own")) foreign := NameChunk(10002, []byte("src data")) - put("t", own.Path(), []byte("own")) - put("src", foreign.Path(), []byte("src data")) + put(tDir, own.Path(), []byte("own")) + put(srcDir, foreign.Path(), []byte("src data")) m := map[uint64]Ref{3: {Owner: "t", OwnerID: idFor("t"), Chunk: own}, 10002: {Owner: "src", OwnerID: idFor("src"), Chunk: foreign}} - putVolume(t, put, "t") - putVolume(t, put, "src") + putVolume(t, put, "t", tDir) + putVolume(t, put, "src", srcDir) root, segs, err := Build(m) if err != nil { t.Fatal(err) } for n, b := range segs { - put("t", n.Path(), b) + put(tDir, n.Path(), b) } rn, rb, err := NameRoot(root) if err != nil { t.Fatal(err) } - put("t", rn.Path(), rb) + put(tDir, rn.Path(), rb) return s, rn, m } func newReader(t *testing.T, s *objStore) *Reader { t.Helper() - r, err := NewReader(base, "t", s.fetch) + r, err := NewReader(base, tDir, s.fetch) if err != nil { t.Fatal(err) } @@ -106,7 +112,7 @@ func TestRead_AbsentIndexIsZeros(t *testing.T) { func TestRead_ForeignChunkFromSourcePool(t *testing.T) { s, rn, m := readFixture(t) // A same-named object in t's own pool must not be what is read. - s.obj[path.Join(base, "t", m[10002].Chunk.Path())] = []byte("decoy") + s.obj[path.Join(base, tDir, m[10002].Chunk.Path())] = []byte("decoy") r := newReader(t, s) img, err := r.Open(context.Background(), rn) if err != nil { @@ -117,7 +123,7 @@ func TestRead_ForeignChunkFromSourcePool(t *testing.T) { if err != nil || c.Zero || !bytes.Equal(c.Data, []byte("src data")) || c.Ref.Owner != "src" { t.Fatalf("chunk 10002: %+v %v", c, err) } - want := path.Join(base, "src", m[10002].Chunk.Path()) + want := path.Join(base, srcDir, m[10002].Chunk.Path()) if got := s.asked[len(s.asked)-1]; got != want { t.Fatalf("read %s; want the source pool path %s", got, want) } @@ -129,7 +135,7 @@ func TestRead_MissingObjectIsAnErrorNeverZeros(t *testing.T) { ctx := context.Background() t.Run("root", func(t *testing.T) { s, rn, _ := readFixture(t) - delete(s.obj, path.Join(base, "t", rn.Path())) + delete(s.obj, path.Join(base, tDir, rn.Path())) if _, err := newReader(t, s).Open(ctx, rn); !errors.Is(err, ErrMissing) { t.Fatalf("open of a missing root: %v; want ErrMissing", err) } @@ -140,7 +146,7 @@ func TestRead_MissingObjectIsAnErrorNeverZeros(t *testing.T) { if err != nil { t.Fatal(err) } - delete(s.obj, path.Join(base, "t", img.root.Segments[1].Path())) + delete(s.obj, path.Join(base, tDir, img.root.Segments[1].Path())) for _, idx := range []uint64{10002, 10003} { // named, and not named, in that shard c, err := img.Chunk(ctx, idx) if !errors.Is(err, ErrMissing) || c.Zero { @@ -150,7 +156,7 @@ func TestRead_MissingObjectIsAnErrorNeverZeros(t *testing.T) { }) t.Run("own chunk", func(t *testing.T) { s, rn, m := readFixture(t) - delete(s.obj, path.Join(base, "t", m[3].Chunk.Path())) + delete(s.obj, path.Join(base, tDir, m[3].Chunk.Path())) img, err := newReader(t, s).Open(ctx, rn) if err != nil { t.Fatal(err) @@ -161,7 +167,7 @@ func TestRead_MissingObjectIsAnErrorNeverZeros(t *testing.T) { }) t.Run("foreign chunk", func(t *testing.T) { s, rn, m := readFixture(t) - delete(s.obj, path.Join(base, "src", m[10002].Chunk.Path())) + delete(s.obj, path.Join(base, srcDir, m[10002].Chunk.Path())) img, err := newReader(t, s).Open(ctx, rn) if err != nil { t.Fatal(err) @@ -182,14 +188,14 @@ func TestRead_FetchFailureAndCorruption(t *testing.T) { if err != nil { t.Fatal(err) } - s.fail = path.Join(base, "t", m[3].Chunk.Path()) + s.fail = path.Join(base, tDir, m[3].Chunk.Path()) if _, err := img.Chunk(ctx, 3); err == nil || errors.Is(err, ErrMissing) { t.Fatalf("failed fetch: %v; want an error that is not ErrMissing", err) } s.fail = "" for name, p := range map[string]string{ - "chunk": path.Join(base, "t", m[3].Chunk.Path()), - "segment": path.Join(base, "t", img.root.Segments[0].Path()), + "chunk": path.Join(base, tDir, m[3].Chunk.Path()), + "segment": path.Join(base, tDir, img.root.Segments[0].Path()), } { s2, rn2, _ := readFixture(t) s2.obj[p] = append(append([]byte(nil), s2.obj[p]...), 'x') @@ -202,7 +208,7 @@ func TestRead_FetchFailureAndCorruption(t *testing.T) { } } s3, rn3, _ := readFixture(t) - rp := path.Join(base, "t", rn3.Path()) + rp := path.Join(base, tDir, rn3.Path()) s3.obj[rp] = append(append([]byte(nil), s3.obj[rp]...), 0) if _, err := newReader(t, s3).Open(ctx, rn3); !errors.Is(err, ErrCorrupt) { t.Errorf("root altered: %v; want ErrCorrupt", err) @@ -220,13 +226,13 @@ func TestRead_SegmentOfAnotherShardRefused(t *testing.T) { t.Fatal(err) } lie := SegName{Shard: 0, Hash: sn.Hash} - s.obj[path.Join(base, "t", lie.Path())] = sb + s.obj[path.Join(base, tDir, lie.Path())] = sb root := Root{Segments: map[uint64]SegName{0: lie}} rn, rb, err := NameRoot(root) if err != nil { t.Fatal(err) } - s.obj[path.Join(base, "t", rn.Path())] = rb + s.obj[path.Join(base, tDir, rn.Path())] = rb img, err := newReader(t, s).Open(ctx, rn) if err != nil { t.Fatal(err) @@ -247,13 +253,13 @@ func TestNewReader_RefusesBadInput(t *testing.T) { } // putVolume writes vol's VOLUME record with its test id. -func putVolume(t *testing.T, put func(vol, rel string, b []byte), vol string) { +func putVolume(t *testing.T, put func(dir, rel string, b []byte), name, dir string) { t.Helper() - b, err := Volume{ID: idFor(vol), Name: vol, Format: VolumeFormat}.Encode() + b, err := Volume{ID: idFor(name), Name: name, Format: VolumeFormat}.Encode() if err != nil { t.Fatal(err) } - put(vol, VolumeName, b) + put(dir, VolumeName, b) } // A foreign chunk is read only if its owner's VOLUME carries the id the @@ -267,7 +273,7 @@ func TestRead_ForeignChunkNeedsTheOwnersVolumeID(t *testing.T) { if err != nil { t.Fatal(err) } - s.obj[path.Join(base, "src", VolumeName)] = b + s.obj[path.Join(base, srcDir, VolumeName)] = b img, err := newReader(t, s).Open(ctx, rn) if err != nil { t.Fatal(err) @@ -277,11 +283,11 @@ func TestRead_ForeignChunkNeedsTheOwnersVolumeID(t *testing.T) { t.Fatalf("chunk of a re-created source: %v; want ErrIdentity", err) } for _, p := range s.asked { - if p == path.Join(base, "src", m[10002].Chunk.Path()) { + if p == path.Join(base, srcDir, m[10002].Chunk.Path()) { t.Fatal("the chunk was fetched from a source whose id does not match") } } - delete(s.obj, path.Join(base, "src", VolumeName)) + delete(s.obj, path.Join(base, srcDir, VolumeName)) img, _ = newReader(t, s).Open(ctx, rn) if _, err := img.Chunk(ctx, 10002); !errors.Is(err, ErrMissing) { t.Fatalf("chunk of a source without VOLUME: %v; want ErrMissing", err) diff --git a/seaweed-block/core/frontend/chunkstore/layout/volume.go b/seaweed-block/core/frontend/chunkstore/layout/volume.go index a15efbca4b..3da1a6fbd9 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/volume.go +++ b/seaweed-block/core/frontend/chunkstore/layout/volume.go @@ -47,6 +47,14 @@ func ParseVolumeID(s string) (VolumeID, error) { return id, nil } +// VolumeDir is the directory of a volume under the store's base: +// -. It is created exclusively and +// never reused, so a volume created again under a name gets another +// directory; it is resolved only from the master's volume record. +func VolumeDir(name string, id VolumeID) string { + return name + "-" + id.String()[:8] +} + // VolumeFormat is the layout format a VOLUME record declares. const VolumeFormat = 1 diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 0d288861d9..73b6ab0889 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -549,7 +549,7 @@ stateDiagram-v2 ## Part B. Invariant ledger -356 rows: 285 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 341 new. +358 rows: 287 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 343 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. @@ -1112,10 +1112,12 @@ Notes: | `INV-SNAPK-DELETE-OWNED-001` | Every object entry carries the id of its volume; a volume delete removes an object only if it carries that id, so a late delete of a finished run never removes the object of a volume created again under the name. | M9 deleting → deleted | `core/frontend/chunkstore/layout` `delete.go` `DeleteVolume`; `objects.go` `ObjectStore.DeleteOfVolume` | `TestDeleteVolume_ALateDeleteSparesTheNameReused`, `TestDeleteVolume` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-DELETE-GATE-001` | HEAD becomes deleting only with no holder (checked immediately before), no open activity and no snapshot with dependents; from deleting on, no holder transition, claim or removal applies. | M9 idle → deleting | `core/frontend/chunkstore/layout` `delete.go` `Head.MarkDeleting`; `head.go` `holderErr` | `TestMarkDeleting_Closure`, `TestMarkDeleted_Closure`, `TestDeleting_RefusesEveryHolderStep`, `TestDeleteVolume` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-DELETE-GUARD-001` | A source scan requires DeleteVolume to run no-holder check, deleting, release, object deletes and deleted in that order, and allows it as the only remover besides RemoveUnreachable. | M9 idle → deleting → deleted | `core/frontend/chunkstore/layout` (source scan of `delete.go`) | `TestGuard_WritersTransitionsAndRemovers`, `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-VOLUME-DIR-001` | A volume lives in the directory -, created exclusively and never reused; a foreign chunk is read from the directory its reference's owner name and id give, after that directory's VOLUME id is checked. | Content-named layout | `core/frontend/chunkstore/layout` `volume.go` `VolumeDir`; `read.go` `Image.Chunk` | `TestRead_ForeignChunkFromSourcePool`, `TestRead_ForeignChunkNeedsTheOwnersVolumeID` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-TOMBSTONE-001` | For a restore target never created the operator writes a deleted HEAD exclusively in its directory before releasing the dependent, so the restore cannot create it later; only that command creates a tombstone (source scan). | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent`, `TargetStore.CreateDeletedHead` | `TestOperatorReleaseDependent`, `TestOperatorReleaseDependent_TombstoneLosesToTheRestore`, `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-RELEASE-001` | A target is released from a source's dependents by its volume id, from every snapshot, in one conditional update; a volume created again under its name releases nothing; an absent target writes nothing. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `Head.ReleaseDependent`, `RunReleaseDependent` | `TestReleaseDependent_Closure`, `TestRunReleaseDependent_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-RELEASE-CALLERS-001` | Only the target's own delete (its HEAD already deleting) and the operator's command release a dependent; a source scan restricts the callers and requires the operator's read of the target. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` (source scan of `delete.go`) | `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | -| `INV-SNAPK-RELEASE-OPERATOR-001` | The operator's command releases only a target that exists and is deleted (HEAD deleting or deleted, or its name holding another volume id) or whose live roots name no chunk of the source. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE | -| `INV-SNAPK-RELEASE-REFUSE-001` | The operator's command refuses a live target still naming the source, a target being created and a target never created, and then writes nothing to the source. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-RELEASE-OPERATOR-001` | The operator's command releases a target that is deleted (HEAD deleting or deleted, or its directory's VOLUME of another id), one whose live roots name no chunk of the source, or one never created, after its tombstone. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE | +| `INV-SNAPK-RELEASE-REFUSE-001` | The operator's command refuses a live target still naming the source, a target being created (VOLUME without HEAD) and one whose HEAD appeared before its tombstone, and then writes nothing to the source. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (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 | Notes: