diff --git a/seaweed-block/core/frontend/chunkstore/layout/commit.go b/seaweed-block/core/frontend/chunkstore/layout/commit.go index 797adbc682..b06773b57c 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/commit.go +++ b/seaweed-block/core/frontend/chunkstore/layout/commit.go @@ -23,6 +23,11 @@ var ( ErrSnapshotExists = errors.New("layout: snapshot name taken") // ErrTooManySnapshots: HEAD already holds MaxSnapshots. ErrTooManySnapshots = errors.New("layout: snapshot limit reached") + // ErrForeignRefAdded: the new manifest holds another volume's chunk at + // an index where the previous manifest did not hold that same chunk. A + // restore target keeps or drops its source's references, never gains + // one, so a dependency is never deeper than one step. + ErrForeignRefAdded = errors.New("layout: commit adds a reference to another volume's chunk") ) // CommitRequest is what a snapshot session commits: the image at @@ -65,6 +70,20 @@ func newOwn(vol string, prev, next map[uint64]Ref) map[uint64]Ref { return out } +// addedForeign returns an index of next holding another volume's chunk +// that prev does not hold at that index (the smallest, for a stable +// refusal). +func addedForeign(vol string, prev, next map[uint64]Ref) (uint64, bool) { + var found bool + var at uint64 + for i, r := range next { + if r.Owner != vol && prev[i] != r && (!found || i < at) { + found, at = true, i + } + } + return at, found +} + // Commit is transition 4 as a pure step on HEAD: for the holder at HEAD's // epoch with snap(session) open, current = root, position, the optional // snapshot added, activity idle, and state live or detached. Detached is the @@ -165,6 +184,9 @@ func RunCommit(ctx context.Context, st HeadStore, os ObjectStore, vol string, me return Head{}, false, ErrStaleBase } } + if i, ok := addedForeign(vol, req.Prev, req.Next); ok { + return Head{}, false, fmt.Errorf("%w: index %d names %s's chunk", ErrForeignRefAdded, i, req.Next[i].Owner) + } var rels []string chunks := newOwn(vol, req.Prev, req.Next) idx := make([]uint64, 0, len(chunks)) diff --git a/seaweed-block/core/frontend/chunkstore/layout/commit_test.go b/seaweed-block/core/frontend/chunkstore/layout/commit_test.go index 875f97dede..382c23ac48 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/commit_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/commit_test.go @@ -92,7 +92,7 @@ func TestCommit_Closure(t *testing.T) { func commitFixture(t *testing.T) (*memObjects, Head, CommitRequest) { t.Helper() f := newMemObjects(volRoot) - prev := manifest(ref("v1", 0, "a"), ref("v1", 3, "b")) + prev := manifest(ref("v1", 0, "a"), ref("v1", 3, "b"), ref("src", 7, "d")) // a restore target: it holds a source chunk prevRoot := prepared(t, f, prev, "s0") next := manifest(ref("v1", 0, "a"), ref("v1", 3, "b2"), ref("v1", 10004, "c"), ref("src", 7, "d")) prepared(t, f, next, "s1") @@ -288,3 +288,38 @@ func TestRunCommit_NeedsObjectsOfItsOwnSession(t *testing.T) { } } } + +// A restore target keeps or drops its source's references and never gains +// one: a commit whose manifest holds another volume's chunk at an index +// where the previous manifest did not hold that same chunk is refused, and +// HEAD is not written. Keeping one (the fixture) and dropping one commit. +func TestRunCommit_RefusesAnAddedForeignReference(t *testing.T) { + for name, add := range map[string]func(next map[uint64]Ref){ + "at a fresh index": func(next map[uint64]Ref) { next[20] = ref("src", 20, "e") }, + "another chunk at a held index": func(next map[uint64]Ref) { next[7] = ref("src", 7, "d2") }, + "a third volume at a held index": func(next map[uint64]Ref) { next[7] = ref("other", 7, "d") }, + "over an own chunk at that index": func(next map[uint64]Ref) { next[3] = ref("src", 3, "b") }, + } { + t.Run(name, func(t *testing.T) { + f, h, req := commitFixture(t) + add(req.Next) + prepared(t, f, req.Next, "s1") + st := &memHead{h: &h} + if _, err := runCommit(st, f, req); !errors.Is(err, ErrForeignRefAdded) { + t.Fatalf("commit adding a foreign reference %s: %v; want ErrForeignRefAdded", name, err) + } + if st.updates != 0 || st.h.Activity.Kind != ActSnap { + t.Fatalf("HEAD written by a refused commit: %+v", st.h) + } + }) + } + t.Run("dropping a foreign reference", func(t *testing.T) { + f, h, req := commitFixture(t) + delete(req.Next, 7) + prepared(t, f, req.Next, "s1") + st := &memHead{h: &h} + if _, err := runCommit(st, f, req); err != nil { + t.Fatalf("commit dropping a source reference: %v", err) + } + }) +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/session_test.go b/seaweed-block/core/frontend/chunkstore/layout/session_test.go index a486a271a3..0dc96ac2a4 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/session_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/session_test.go @@ -226,7 +226,7 @@ func mustRoot(m map[uint64]Ref) RootName { func sessionFixture(t *testing.T) (func() *memVolume, map[uint64][]byte, CommitRequest) { t.Helper() data := map[uint64][]byte{0: []byte("a"), 3: []byte("b2"), 10004: []byte("c")} - prev := manifest(ref("v1", 0, "a"), ref("v1", 3, "b")) + prev := manifest(ref("v1", 0, "a"), ref("v1", 3, "b"), ref("src", 7, "d")) // a restore target: it holds a source chunk next := manifest(ref("v1", 0, "a"), ref("v1", 3, "b2"), ref("v1", 10004, "c"), ref("src", 7, "d")) prevData := map[uint64][]byte{0: []byte("a"), 3: []byte("b")} fresh := func() *memVolume { diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 2dcf8b3964..aec2ce8e51 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -926,6 +926,7 @@ Notes: | `INV-RESTORE-HYDRATE-001` | The background hydrate walker fetches a chunk only through that chunk's shared fill flight: at most one fetch per chunk absence, and a fetched block is never installed over a block written locally. | M4 Holder (background hydrate) | `core/storage/chunk_hydrate_all.go` `HydrateAll`, `HydrateAllAt`, `startWalkerFill`, `nextHydrateTarget`, `admitFill`; `core/storage/chunk_hydration.go` `hydrationFlightLocked`; `core/storage/chunk_layer.go` `fillOnce` | `TestHydrateAll_FillsEveryChunkOnceAndReadsTheHeadImage`, `TestHydrateAll_YieldsToGuestReadsAndNeverFetchesTwice`, `TestHydrateAll_NeverOverwritesALocalWrite`, `TestHydrateAll_AdmitsEachFillThroughTheLimiterWithTheCallersClass`, `TestHydrateAll_RealBackgroundFillLimiterAdmitsEveryFill`, `TestHydrateAll_PermitIsHeldUntilTheOwnedFetchSettles`, `TestHydrateAll_RevocationStopsTheWalker`, `TestHydrateAll_CancelReturnsWhileTheFetchIsHeld`, `TestHydrateAll_PacedByByteRate` (`core/storage/chunk_hydrate_all_test.go`) | — | ACTIVE | | `INV-RESTORE-OWN-BATCH-001` | A batch cut re-uploads at most N local chunks whose HEAD keys name another volume under new own keys, even when their bytes equal HEAD's, in one ordinary cut (one intent, one HEAD CAS); it never fetches an absent chunk. | M6.Restore.Owning>Owning | `core/storage/chunk_own.go` `OwnBatch`; `core/storage/chunk_cut_payload.go` `streamCutPayloads` | `TestOwnBatch_BoundedBatchesReplaceEveryForeignKey`, `TestOwnBatch_SkipsChunksNotLocal`, `TestOwnBatch_CarriesGuestWrites` (`core/storage/chunk_own_test.go`) | `RestoreIndependence` `NoGap` | ACTIVE | | `INV-RESTORE-READ-REF-001` | A restore target reads each chunk its HEAD names from the pool of the reference's owner, by the reference alone (no other copy, no name-built path); a chunk missing there is an error on a fresh host, never zeros. | M6 restore target read (no transition) | `core/frontend/chunkstore/layout/read.go` `Reader.ReadRef`; `core/frontend/chunkstore/layout_remote.go` `LayoutRemote.Get` | `TestLayoutRemote_RestoreTargetReadsItsSource` (`core/frontend/chunkstore/layout_restore_read_test.go`) | — | ACTIVE | +| `INV-RESTORE-REF-NO-GAIN-001` | A commit never adds a reference to another volume's chunk: at each index the new manifest holds another volume's chunk only if the previous one held that same chunk, so a restore target keeps or drops its source's references and a dependency is one step deep. | M6 Commit (transition 4) | `core/frontend/chunkstore/layout/commit.go` `RunCommit`, `addedForeign` | `TestRunCommit_RefusesAnAddedForeignReference` (`core/frontend/chunkstore/layout/commit_test.go`) | — | ACTIVE | | `INV-SHARE-ACTIVATION-001` | With a `ShareGuard` installed, a snapshot or fork activates (snapshot entry, child HEAD) only after `Finish` accepts the share; a refused `Begin` or `Finish` aborts before activation and drops the new intent. With no guard, nothing is called or checked. | M6 snapshot: Pending → Published; fork: child HEAD create | `core/frontend/chunkstore/share_guard.go` `beginShare`, `finishShare`; `core/frontend/chunkstore/catalog.go` `CreateSnapshot`, `Fork`; `core/host/volume/chunk_control.go` `chunkControl.Snapshot`, `sealSnapshot` (explicit and scheduled snapshots) | none (legacy share guard removed) | — | ARCHIVED | | `INV-PUBLISH-SETTLED-001` | With `OnPublishSettled` set, each confirmed publication (a new HEAD generation) is delivered once, in publication order, one at a time, outside the capture and state locks. After Close begins, no report is dequeued. | M5 CAS → Settled; M6 HEAD: published | `core/storage/chunk_publish_settled.go` `noteSettled`, `unlockCap`, `runPublishSettled`; `core/storage/chunk_periodic_cut.go` `CutUnlessCutSince` | `TestChunkPublishSettled_ReportsEachPublication`, `TestChunkPublishSettled_ReportsALatePublication`, `TestChunkPublishSettled_ClosedAndUnset`, `TestChunkPublishSettled_OverlappingPublishersStayOrdered`, `TestChunkPublishSettled_CloseDropsUnstartedReports`, `TestChunkPublishSettled_HookThatPublishes`, `TestChunkPublishSettled_PeriodicCutReports` (`core/storage/chunk_publish_settled_test.go`); `TestWALStoreChunk_PublishSettledHook` (`core/frontend/durable/provider_chunk_sched_test.go`) | — | ACTIVE |