storage, chunkstore: a fresh volume starts with an initial HEAD and an empty bitmap

Integration ruling F1(a):
- Catalog.EnsureVolume (the attach check) gives a created volume its
  initial HEAD with the VOLUME entry: gen 1, epoch 0, no holder,
  remote_lsn 0, no chunks (all zero), by exclusive create. An existing HEAD
  is kept (a volume published before VOLUME entries), and a missing one is
  created on the next attach.
- A fresh local volume (FreshFromHead) without a saved bitmap starts with
  an empty bitmap bound to HEAD instead of hash mode: it took no write
  before its first attach, so its image is HEAD's. Its first cut uploads
  only the chunks written. Hash mode stays for an existing volume whose
  bitmap is lost (F1(b)'s background reconciliation is separate work).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-15 07:44:09 -08:00
1 parent 4c25191694
commit fb939771f6
5 files changed
+158 -5

No files matched your search

@@ -51,8 +51,10 @@ func TestOpsChunk_Show(t *testing.T) {
fork.Head.ChunkCount != 1 || fork.Head.DetachedBy != nil {
t.Fatalf("show v2 HEAD: %+v", fork.Head)
}
if src := show("v1"); src.Volume == nil || src.Volume.Origin != chunkstore.OriginCreate || src.Head != nil {
t.Fatalf("show v1 (VOLUME, no HEAD yet): %+v %+v", src.Volume, src.Head)
// A created volume carries its initial HEAD (gen 1, epoch 0, no chunks).
if src := show("v1"); src.Volume == nil || src.Volume.Origin != chunkstore.OriginCreate || src.Head == nil || src.Head.Gen != 1 ||
src.Head.Epoch != 0 || src.Head.RemoteLSN != 0 || src.Head.ChunkCount != 0 || src.Head.State != "active" {
t.Fatalf("show v1 (VOLUME and its initial HEAD): %+v %+v", src.Volume, src.Head)
}
if none := show("v9"); none.Volume != nil || none.Head != nil || none.VolumeID != "v9" {
t.Fatalf("show of a volume that does not exist: %+v", none)
@@ -410,6 +410,25 @@ func (c *Catalog) headExists(ctx context.Context, root string) (bool, error) {
return true, nil
}
// ensureInitialHead creates vol's initial empty HEAD unless a HEAD exists.
func (c *Catalog) ensureInitialHead(ctx context.Context, vol, root string) error {
has, err := c.headExists(ctx, root)
if err != nil {
return remoteErr(err, "read HEAD of %s", vol)
}
if has {
return nil
}
he, err := headEntry(storage.ChunkHead{Gen: 1, Chunks: map[uint64]storage.ChunkRef{}, State: storage.ChunkHeadActive})
if err != nil {
return err
}
if err := c.f.CreateEntry(ctx, root, he, true); err != nil && !errors.Is(err, ErrExists) {
return remoteErr(err, "create the initial HEAD of %s", vol)
}
return nil
}
// ReadHead returns vol's HEAD, and false when no HEAD was ever published.
func (c *Catalog) ReadHead(ctx context.Context, vol string) (storage.ChunkHead, bool, error) {
root, err := c.volRoot(vol)
@@ -462,6 +481,15 @@ func (c *Catalog) EnsureVolume(ctx context.Context, vol string, g Geometry) (Vol
if v.Geometry != g {
return VolumeInfo{}, catErr(CodeGeometryMismatch, false, nil, "volume %s: VOLUME geometry %+v, this host serves %+v", vol, v.Geometry, g)
}
if v.Origin == OriginCreate {
// A created volume gets its initial HEAD with VOLUME (integration
// ruling F1): gen 1, epoch 0, no holder, remote_lsn 0, no chunks
// (all zero). An exclusive create: an existing HEAD (a volume made
// before VOLUME entries, or an earlier attach) is kept.
if err := c.ensureInitialHead(ctx, vol, root); err != nil {
return VolumeInfo{}, err
}
}
if v.Origin == OriginFork {
has, err := c.headExists(ctx, root)
if err != nil {
@@ -19,8 +19,9 @@ const catBase = "/swblock/chunks"
var catGeo = Geometry{NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 8}
// publishSource writes chunks for vol and installs its first HEAD (epoch 1,
// remote_lsn 42) through the real ChunkRemote; it returns the HEAD.
// publishSource writes chunks for vol and installs a HEAD (epoch 1,
// remote_lsn 42) over the current one (the initial HEAD the VOLUME entry
// came with, if any) through the real ChunkRemote; it returns the HEAD.
func publishSource(t *testing.T, f *fakeFiler, b *fakeBlobs, vol string, chunks map[uint64][]byte) storage.ChunkHead {
t.Helper()
ctx := context.Background()
@@ -36,7 +37,11 @@ func publishSource(t *testing.T, f *fakeFiler, b *fakeBlobs, vol string, chunks
}
h.Chunks[idx] = storage.ChunkRef{Key: key, SHA256: sha256.Sum256(data)}
}
gen, err := r.CASHead(ctx, storage.ChunkHeadVersion{}, h)
cur, err := r.GetHead(ctx)
if err != nil {
t.Fatal(err)
}
gen, err := r.CASHead(ctx, storage.ChunkHeadVersion{Gen: cur.Gen, Epoch: cur.Epoch}, h)
if err != nil {
t.Fatal(err)
}
@@ -580,3 +585,45 @@ func TestDetach_ProductionSerializedHead(t *testing.T) {
t.Fatalf("HEAD entry extended gen %q, filer chunks %d", e.Extended[extGen], len(e.Chunks))
}
}
// A created volume gets its initial HEAD with the VOLUME entry (integration
// ruling F1): gen 1, epoch 0, no holder, remote_lsn 0, no chunks. An existing
// HEAD is kept, and a missing one is created again on the next attach.
func TestCatalog_EnsureVolumeCreatesTheInitialHead(t *testing.T) {
ctx := context.Background()
f, b := newFakeFiler(), newFakeBlobs()
c := newCat(t, f)
if _, err := c.EnsureVolume(ctx, "v1", catGeo); err != nil {
t.Fatal(err)
}
h, ok, err := c.ReadHead(ctx, "v1")
if err != nil || !ok || h.Gen != 1 || h.Epoch != 0 || h.Holder != "" || h.RemoteLSN != 0 || len(h.Chunks) != 0 || h.State != storage.ChunkHeadActive {
t.Fatalf("initial HEAD: %+v ok=%v %v", h, ok, err)
}
if _, err := c.EnsureVolume(ctx, "v1", catGeo); err != nil {
t.Fatal(err)
}
if again, _, _ := c.ReadHead(ctx, "v1"); again.Gen != 1 {
t.Fatalf("a repeated attach replaced the HEAD: %+v", again)
}
// A volume published before VOLUME entries existed keeps its HEAD.
pub := publishSource(t, f, b, "v2", map[uint64][]byte{0: chunkBytes(0x10)})
if _, err := c.EnsureVolume(ctx, "v2", catGeo); err != nil {
t.Fatal(err)
}
if kept, _, _ := c.ReadHead(ctx, "v2"); kept.Gen != pub.Gen || len(kept.Chunks) != 1 {
t.Fatalf("an existing HEAD was replaced by the initial one: %+v, published %+v", kept, pub)
}
// VOLUME written, HEAD lost before it (an interrupted first attach).
f.mu.Lock()
delete(f.entries, catBase+"/v1/HEAD")
f.mu.Unlock()
if _, err := c.EnsureVolume(ctx, "v1", catGeo); err != nil {
t.Fatal(err)
}
if h, ok, _ := c.ReadHead(ctx, "v1"); !ok || h.Gen != 1 {
t.Fatalf("the initial HEAD was not created again: %+v ok=%v", h, ok)
}
}
+11
View File
@@ -153,6 +153,17 @@ func attachChunkLayerImpl(s *WALStore, remote chunkRemote, cfg chunkLayerConfig)
for _, idx := range saved {
l.dirty[idx] = true
}
case !ok && cfg.FreshFromHead:
// A fresh local volume has taken no write yet (I/O waits for its
// first attach): its image is HEAD's, so nothing is dirty. Start with
// an empty bitmap bound to HEAD instead of hash mode, which stays for
// an existing volume whose bitmap is lost (integration ruling F1).
fresh := chunkBitmapHeader{BaseLSN: head.RemoteLSN, HeadGen: head.Gen, Epoch: head.Epoch, Base: chunkBaseNone}
if err := cfg.Bitmap.WriteHeader(fresh); err != nil {
l.hashMode = true // safe: compare every chunk on the first cut
} else {
l.hdr = fresh
}
default:
if ok {
l.hdr = hdr
@@ -0,0 +1,65 @@
package storage
import (
"testing"
)
// A fresh local volume starts with an empty bitmap bound to HEAD, not in
// hash mode: its first cut uploads only the chunks actually written
// (integration ruling F1). Hash mode stays for an existing volume whose
// bitmap is lost.
func TestChunkLayer_FreshVolumeStartsWithEmptyBitmap(t *testing.T) {
for _, tc := range []struct {
name string
initialHead bool // the empty HEAD the VOLUME entry comes with
}{
{"no HEAD yet", false},
{"initial empty HEAD", true},
} {
t.Run(tc.name, func(t *testing.T) {
f := newFakeChunkRemote()
if tc.initialHead {
f.mu.Lock()
f.installLocked(chunkHead{Chunks: map[uint64]chunkRef{}, State: chunkHeadActive})
f.mu.Unlock()
}
s, _ := newChunkTestStore(t)
cfg := ctConfig(1)
cfg.FreshFromHead = true
layer := mustAttach(t, s, f, cfg)
w := newWriteLog()
w.write(t, s, 3, 0x33) // one chunk written
before := f.stats()
r, err := layer.Cut(ctTimeout(t))
if err != nil {
t.Fatal(err)
}
if r.FullHash {
t.Fatal("the first cut of a fresh volume compared every chunk by hash")
}
if n := f.stats().puts - before.puts; n != 1 {
t.Fatalf("the first cut uploaded %d chunks, want only the one written", n)
}
assertEveryHeadIsPrefixImage(t, f, w)
})
}
}
// Control: an existing volume (not fresh) without a bitmap compares every
// chunk on its first cut.
func TestChunkLayer_LostBitmapStillHashes(t *testing.T) {
src := make([]byte, ctNumBlocks*ctBlockSize)
copy(src, makeBlock(ctBlockSize, 0x44))
f := newFakeChunkRemote()
f.seedHead(t, src, 0, 0) // an older epoch: this holder may publish over it
s, _ := newChunkTestStore(t)
layer := mustAttach(t, s, f, ctConfig(1))
newWriteLog().write(t, s, 3, 0x33)
r, err := layer.Cut(ctTimeout(t))
if err != nil {
t.Fatal(err)
}
if !r.FullHash {
t.Fatal("an existing volume without a bitmap did not compare every chunk")
}
}