diff --git a/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go b/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go index 3a50a51790..5042ceefe7 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go @@ -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)}) } diff --git a/seaweed-block/core/frontend/durable/provider_snapshot_position_test.go b/seaweed-block/core/frontend/durable/provider_snapshot_position_test.go new file mode 100644 index 0000000000..bd66bfd011 --- /dev/null +++ b/seaweed-block/core/frontend/durable/provider_snapshot_position_test.go @@ -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") + } +}