Merge pull request #531 from seaweedfs/test/flat-head-invariant

docs: state flat image references and shared-space retention
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-11-23 20:26:07 -08:00
commit 4ce7ebbbfe
5 files changed
+94 -5

No files matched your search

@@ -0,0 +1,78 @@
package chunkstore
import (
"bytes"
"context"
"path"
"testing"
)
func TestForkGrandchildReadsEveryBlockWithoutAncestorImages(t *testing.T) {
f, b, c, source := catalogFixture(t)
ctx := context.Background()
if _, err := c.CreateSnapshot(ctx, "v1", SnapshotRequest{Name: "seed", RequestID: "seed"}, source); err != nil {
t.Fatal(err)
}
if _, err := c.Fork(ctx, ForkRequest{Source: "v1", Snapshot: "seed", Target: "v2", RequestID: "parent"}); err != nil {
t.Fatal(err)
}
parent, err := NewChunkRemote(f, b, catBase+"/v2")
if err != nil {
t.Fatal(err)
}
ph, err := parent.GetHead(ctx)
if err != nil {
t.Fatal(err)
}
if _, err := c.CreateSnapshot(ctx, "v2", SnapshotRequest{Name: "template", RequestID: "template"}, ph); err != nil {
t.Fatal(err)
}
if _, err := c.Fork(ctx, ForkRequest{Source: "v2", Snapshot: "template", Target: "v3", RequestID: "grandchild"}); err != nil {
t.Fatal(err)
}
// Remove only ancestor image metadata, preserving immutable object bindings.
for vol, name := range map[string]string{"v1": "seed", "v2": "template"} {
attempt := snapPath(t, f, vol, name)
for _, entry := range []string{attempt, path.Join(catBase, vol, snapNamesDir, name), path.Join(catBase, vol, headName)} {
if err := f.DeleteEntry(ctx, path.Dir(entry), path.Base(entry), nil); err != nil {
t.Fatal(err)
}
if _, err := f.LookupEntry(ctx, path.Dir(entry), path.Base(entry)); err == nil {
t.Fatalf("ancestor image metadata remains: %s", entry)
}
}
}
// Open a fresh reader: no cached parent HEAD or snapshot can supply data.
grandchild, err := NewChunkRemote(f, b, catBase+"/v3")
if err != nil {
t.Fatal(err)
}
h, err := grandchild.GetHead(ctx)
if err != nil {
t.Fatal(err)
}
if len(h.Chunks) != 2 {
t.Fatalf("grandchild lost its complete image: %d chunks", len(h.Chunks))
}
for block := uint64(0); block < uint64(catGeo.NumBlocks); block++ {
got := make([]byte, catGeo.BlockSize)
if ref, ok := h.Chunks[block/uint64(catGeo.ChunkBlocks)]; ok {
data, err := grandchild.Get(ctx, ref.Key)
if err != nil {
t.Fatalf("block %d: %v", block, err)
}
off := (block % uint64(catGeo.ChunkBlocks)) * uint64(catGeo.BlockSize)
copy(got, data[off:off+uint64(catGeo.BlockSize)])
}
var fill byte
switch block / uint64(catGeo.ChunkBlocks) {
case 0:
fill = 0x10
case 3:
fill = 0x13
}
if !bytes.Equal(got, bytes.Repeat([]byte{fill}, int(catGeo.BlockSize))) {
t.Fatalf("block %d differs after ancestor images were removed", block)
}
}
}
@@ -103,7 +103,7 @@ Per volume, under the chunk base directory in the filer (`core/frontend/chunksto
- Each HEAD is logically full: restore never chains deltas.
- A flat (format 1) HEAD is upgraded at its next cut ([chunk manifests](../chunk-manifests.md)). In a host-side proxy, the root of a 10 GiB volume shrank from 261,531 bytes (flat) to 2,172 bytes (two-level) (§13).
**How versions share data.** A HEAD, a snapshot and a fork root are each a complete map from chunk index to chunk key. There is no backing chain and no per-layer bitmap that falls through to a parent, as qcow2 backing files or overlay layers have.
**How versions share data.** A HEAD, a snapshot and a fork root are each a complete map from chunk index to chunk key. There is no backing chain and no per-layer bitmap that falls through to a parent, as qcow2 backing files or overlay layers have. This is `INV-HEAD-FLAT-001`: immutable segment indirection is part of one image map, not a parent-image chain.
- A cut uploads a new key only for a chunk whose content changed (§5, step 6) and rewrites only the segments that list such chunks. Every other index keeps its key, and every other segment its object, from the previous HEAD. A snapshot copies the root; a fork writes a new root with the same references (§6). Neither copies a chunk or a segment.
- So a chunk that was not rewritten after a snapshot is the same object in that snapshot, in every later snapshot and in HEAD. A chunk rewritten after the snapshot has an old key that only the snapshot (and any earlier version that shares it) still references.
- A read looks up one map: the root, one segment, one chunk. It never walks a chain, and deleting a version never requires merging another one.
+2 -1
View File
@@ -497,7 +497,7 @@ Tests: `TestSnapshotSchedule_SlotsAreOffsetPerVolume`, `_FailureIsTypedAndBacksO
## Part B. Invariant ledger
320 rows: 249 ACTIVE, 68 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 303 new.
321 rows: 250 ACTIVE, 68 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 306 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.
@@ -887,6 +887,7 @@ Notes:
| ID | Statement | Machine / transition | Enforced in | Test | TLA+ property | Status |
|---|---|---|---|---|---|---|
| `INV-HEAD-FLAT-001` | Every HEAD and snapshot root is a complete image map, directly or through immutable segments. Forks preserve original object owners; reads never fall through to a parent HEAD or snapshot, so successive forks add no backing-chain depth. | M6 snapshot: published → fork | `core/frontend/chunkstore/catalog.go` `Fork`; `core/frontend/chunkstore/manifest.go` `qualifyManifestKey`, `resolveManifest` | `TestForkGrandchildReadsEveryBlockWithoutAncestorImages` (`core/frontend/chunkstore/flat_head_test.go`); `TestFork_ForkOfForkKeepsOriginalOwner` (`core/frontend/chunkstore/catalog_test.go`); `TestManifest_ForkAfterParentSnapshotDelete` (`core/frontend/chunkstore/manifest_test.go`) | —; existing representation, no new transition | ACTIVE |
| `INV-CHUNK-WRITE-ONCE-001` | Chunk and segment keys are write-once: a key that exists or once existed is refused, and a volume writes only its own keys. | M6 key: Bound | `core/frontend/chunkstore/chunk_remote.go` `putObject` | `TestChunkRemote_KeysAreWriteOnce` (`core/frontend/chunkstore/chunk_remote_test.go`); `TestChunkLayer_C5_ChunksAreImmutable` (`core/storage/chunk_layer_contract_test.go`) | — | ACTIVE |
| `INV-MAINT-ONE-OWNER` | Only the current attached handle with matching replica/epoch/endpoint admits maintenance; one attached volume admits at most one pass; elapsed ticks coalesce and never enqueue catch-up work. | M8 Armed → Running | `provider_chunk.go` `chunkMaintenanceLoop` | `TestMaintenanceCoalescesWhileOnePassIsHeld`, `TestMaintenancePassHasOneDeadline`, `TestMaintenanceRejectsForeignLineageBeforeAdmission`, `TestMaintenanceSameHandleRearmsAfterJoinedStop` | `MaintenanceOwner` `OneOwner`, `CurrentHandleAtAdmission` | ACTIVE |
| `INV-MAINT-SCOPE` | Per-volume GC traces foreign roots while restricting candidate reclamation, intent expiry and copy settlement to its volume; namespace coverage is not imported. | M8 scoped GC | `gc.go` `Collector.run`; `gc_copy.go` `SweepCopies`; `gc_execution.go` `ExecuteVolumeGC` | `TestVolumeGC_ForeignIntentRootsAndLocalCandidates`, `TestVolumeGC_DoesNotImportGlobalMark`, `TestVolumeGC_ForeignEligibleCaptureIntentStaysProtected`, `TestVolumeGC_ForeignEligibleCopyIsNotSettled` | `ScopedMaintenanceGC` `ForeignRootSurvives`, `ForeignCandidatesUntouched`, `OwnCoverageOnly` | ACTIVE |
+2 -2
View File
@@ -135,10 +135,10 @@ default) can expire the host's slots; keep host clocks synchronized.
- scheduled and manual snapshots;
- deleting snapshots, and getting space back:
- GC deletes the chunks after the grace (`--grace`, 1 hour by default), so a deleted snapshot's space is
reclaimed about 2 hours after the delete;
reclaimed about 2 hours after the delete unless still referenced by a snapshot, fork or restore;
- then the asynchronous SeaweedFS vacuum compacts the volume files;
- `ops chunk verify`;
- restoring a snapshot into a new, independent volume.
- forking a snapshot into a new volume that shares immutable objects, or restoring it into a new, independent volume.
A snapshot can be refused with `host_slot_busy` / `no background slot` (retryable) while every host slot is held by
other volumes' cuts; retry with the same request id. With many volumes on a host, a snapshot may wait for a slot:
+11 -1
View File
@@ -506,7 +506,7 @@ Space comes back in two steps. Neither is immediate.
1. **GC deletes the chunks**, after the grace. The grace is `sw-block ops chunk gc --grace`, **1 hour by default**.
- A deleted snapshot keeps its chunks rooted for the grace after the delete.
- A chunk is then reclaimed only once two complete GC marks, at least the grace apart, have both seen it unreferenced.
- With the default grace, a deleted snapshot's chunks are reclaimed about **2 hours** after the delete. That needs a GC pass at least 1 hour after the delete and another at least 1 hour after that.
- With the default grace, a deleted snapshot's unreferenced chunks are reclaimed about **2 hours** after the delete. Chunks still referenced by another snapshot, fork or restore remain live. That needs a GC pass at least 1 hour after the delete and another at least 1 hour after that.
- Automatic GC is off by default (`--chunk-maintenance-gc`), so run the passes yourself:
```bash
@@ -564,6 +564,16 @@ therefore see every chunk of every volume and snapshot as unreferenced.
- Space is returned only by the two steps above (`sw-block ops chunk gc`, then a SeaweedFS vacuum). `volume.vacuum`
is safe: it compacts needles that were already deleted.
### Forking a snapshot into a new volume
A fork creates a new volume sharing the snapshot's immutable chunks and segments:
```bash
sw-block ops chunk fork --source v1 --snapshot before-upgrade --target v2 --chunk-filer FILER --request-id fork-v2
```
Keep the request id to retry an uncertain result. The new HEAD contains a complete image map; reads do not walk back through parent volumes or snapshots. A fork of a fork preserves the original object owners without adding a backing chain. Deleting the source snapshot does not delete objects still referenced by the fork. Use restore when the target must eventually own its data independently.
### Restoring a snapshot into a new volume
A restore makes a new, independent volume from a snapshot. The source volume and its snapshot can be deleted afterwards.