chunkstore, durable: the named snapshot is the request's own image, through SnapshotVolume too

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-29 16:32:47 -08:00
1 parent fb7fbad293
commit df9d40c7b9
2 files changed
+117

No files matched your search

@@ -85,6 +85,12 @@ func TestASnapshotNameIsNotTakenByACutPastItsBarrier(t *testing.T) {
if second.H < from || h.Position < from {
t.Fatalf("the named image is at %d (HEAD %d); want at or past the request's %d", second.H, h.Position, from)
}
// The named snapshot is this image itself: its own root and position,
// not an older root kept under the name.
snap := h.Snapshots["daily"]
if h.Current == nil || snap.Root != *h.Current || snap.Position != h.Position || snap.Position < from {
t.Fatalf("snapshot daily: root %v position %d; want HEAD's root %v and position %d (>= %d)", snap.Root, snap.Position, h.Current, h.Position, from)
}
lh.readBack("reader", map[uint32][]byte{0: block(1), lrChunkBlocks: block(2)})
}
@@ -0,0 +1,111 @@
package durable_test
import (
"context"
"sync"
"testing"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
"github.com/seaweedfs/seaweed-block/core/frontend/durable/testutil"
"github.com/seaweedfs/seaweed-block/core/storage"
)
// heldCutRemote holds the first cut at its commit check (past its barrier)
// and reports when a snapshot request has set its name.
type heldCutRemote struct {
*chunkstore.LayoutRemote
entered, release, named chan struct{}
enter, name sync.Once
}
func (r *heldCutRemote) CheckCommit(ctx context.Context, e storage.ChunkHeadVersion, n storage.ChunkHead) error {
held := false
r.enter.Do(func() { held = true })
if held {
close(r.entered)
<-r.release
}
return r.LayoutRemote.CheckCommit(ctx, e, n)
}
func (r *heldCutRemote) SetSnapshot(name string, meta layout.SnapshotMeta, from uint64) {
r.LayoutRemote.SetSnapshot(name, meta, from)
r.name.Do(func() { close(r.named) })
}
// A snapshot request that arrives through the provider while a cut is past
// its barrier gets an image holding every write acknowledged before it:
// that earlier cut does not take the name, the request's own cut does.
func TestASnapshotRequestDuringACutPastItsBarrierNamesItsOwnImage(t *testing.T) {
ctx := context.Background()
fixture := testutil.NewLayoutFixture(t, "v1", pcBlocks, pcChunkBlocks)
remote := &heldCutRemote{entered: make(chan struct{}), release: make(chan struct{}), named: make(chan struct{})}
cfg := durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, ChunkBlocks: pcChunkBlocks}
fixture.Configure(&cfg)
cfg.ChunkLayoutRemoteFor = func(_ string, me layout.HolderID) (storage.ChunkRemote, error) {
inner, err := chunkstore.NewLayoutRemote(fixture.Catalog, fixture.Ref, me)
if err != nil {
return nil, err
}
remote.LayoutRemote = inner
return remote, nil
}
p, err := durable.NewDurableProvider(cfg, v1View())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = p.Close() })
openRecovered(t, p)
b, err := p.Open(ctx, "v1")
if err != nil {
t.Fatal(err)
}
if _, err := b.Write(ctx, 0, block(1)); err != nil {
t.Fatal(err)
}
type cutOut struct {
r storage.ChunkCutResult
err error
}
earlier := make(chan cutOut, 1)
go func() {
r, err := p.CutVolume(ctx, "v1", 0)
earlier <- cutOut{r, err}
}()
<-remote.entered // that cut is past its barrier
// A write acknowledged before the request, then the request.
if _, err := b.Write(ctx, int64(4096*pcChunkBlocks), block(2)); err != nil {
t.Fatal(err)
}
type snapOut struct {
h layout.Head
err error
}
request := make(chan snapOut, 1)
go func() {
h, err := p.SnapshotVolume(ctx, "v1", "daily", layout.SnapshotMeta{CreatedNs: 1})
request <- snapOut{h, err}
}()
<-remote.named
close(remote.release)
first := <-earlier
if first.err != nil {
t.Fatal(first.err)
}
got := <-request
if got.err != nil {
t.Fatalf("the snapshot request: %v", got.err)
}
snap, ok := got.h.Snapshots["daily"]
if !ok {
t.Fatalf("the snapshot is not in HEAD: %v", got.h.Snapshots)
}
if snap.Position <= first.r.H {
t.Fatalf("snapshot daily is at %d, the earlier cut's position (%d): that cut, past its barrier when the request came, took the name", snap.Position, first.r.H)
}
if !bytesEqual(layoutHeadBlock(t, fixture, uint32(pcChunkBlocks)), block(2)) || got.h.Current == nil || snap.Root != *got.h.Current {
t.Fatal("the named image does not hold the write acknowledged before the request")
}
}