diff --git a/seaweed-block/core/frontend/chunkstore/layout_remote.go b/seaweed-block/core/frontend/chunkstore/layout_remote.go index 9601cbf8ff..6b4d4fc098 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_remote.go +++ b/seaweed-block/core/frontend/chunkstore/layout_remote.go @@ -524,8 +524,11 @@ func (l *LayoutRemote) CommitPendingSnapshot(ctx context.Context) error { } // The image is the one the engine already holds (same root and // position): an own write that leaves the engine's view where it is. + // On a volume never cut it is HEAD's first root (the empty image): seen + // takes it too, so a later write by anyone else that leaves this image + // in place does not move visible under the engine's base. l.session, l.pending, l.lastCommit = "", nil, got - l.ownGen = got.Gen + l.ownGen, l.seen = got.Gen, imageOf(got) return nil } diff --git a/seaweed-block/core/frontend/durable/provider_layout_empty_snapshot_test.go b/seaweed-block/core/frontend/durable/provider_layout_empty_snapshot_test.go new file mode 100644 index 0000000000..eac7c39e89 --- /dev/null +++ b/seaweed-block/core/frontend/durable/provider_layout_empty_snapshot_test.go @@ -0,0 +1,90 @@ +package durable_test + +import ( + "bytes" + "context" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend" + "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/storage" +) + +// The first snapshot of a volume never written names its empty image with +// a commit of its own, which gives HEAD its first root. A later HEAD write +// by someone else (here the store-direct snapshot delete) must not make the +// holder's next cut lose its commit: the next snapshot, with data, is taken +// on its first attempt, and its data is in the image it names. +func TestLayoutProviderSnapshotAfterAnEmptySnapshotAndAnOutsideWrite(t *testing.T) { + ctx := context.Background() + cat, err := chunkstore.NewBCatalog(chunkstore.NewMemFiler(), &layoutProviderBlobs{blobs: map[string][]byte{}}, "/layout-test") + if err != nil { + t.Fatal(err) + } + id, err := layout.NewVolumeID() + if err != nil { + t.Fatal(err) + } + v := chunkstore.VolumeRef{Name: "v1", ID: id} + if _, err = cat.CreateVolume(ctx, v, layout.Geometry{SizeBytes: 64 * 4096, BlockSize: 4096, ChunkBytes: 4 * 4096}); err != nil { + t.Fatal(err) + } + view := newStubView(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1})) + p, err := durable.NewDurableProvider(durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 4, ChunkHolder: "s/r1", + ChunkLayoutHeadFor: func(string) (layout.StartStore, error) { return cat.Store(v) }, + ChunkLayoutRemoteFor: func(_ string, me layout.HolderID) (storage.ChunkRemote, error) { + return chunkstore.NewLayoutRemote(cat, v, me) + }, + }, view) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = p.Close() }) + s, err := p.EnsureStorage("v1") + if err != nil { + t.Fatal(err) + } + ws := s.(*storage.WALStore) + if _, err = p.RecoverVolume(ctx, "v1"); err != nil { + t.Fatal(err) + } + head := func() layout.Head { + h, _, err := cat.ReadHead(ctx, v) + if err != nil { + t.Fatal(err) + } + return h + } + if h := head(); h.Current != nil { + t.Fatalf("setup: a never-written volume's HEAD names an image: %+v", h) + } + if _, err := p.SnapshotVolume(ctx, "v1", "empty", layout.SnapshotMeta{CreatedNs: 1}); err != nil { + t.Fatalf("snapshot of the empty volume: %v", err) + } + if h := head(); h.Current == nil { + t.Fatalf("setup: the empty snapshot's commit gave HEAD no root: %+v", h) + } + if _, err := cat.DeleteSnapshot(ctx, v, "empty"); err != nil { // a HEAD write by someone else + t.Fatalf("store-direct snapshot delete: %v", err) + } + data := bytes.Repeat([]byte("after-the-empty!"), 4096/16) + if _, err = ws.Write(1, data); err != nil { + t.Fatal(err) + } + if _, err = ws.Sync(); err != nil { + t.Fatal(err) + } + hd, err := p.SnapshotVolume(ctx, "v1", "with-data", layout.SnapshotMeta{CreatedNs: 2}) + if err != nil { + t.Fatalf("snapshot with data after the empty snapshot and an outside HEAD write: %v; want it taken on the first attempt", err) + } + sn, ok := hd.Snapshots["with-data"] + if !ok || hd.Current == nil || sn.Root != *hd.Current || sn.Position == 0 { + t.Fatalf("snapshot with-data: %+v (HEAD current %v); want it naming the image with the write", sn, hd.Current) + } + if got, err := ws.Read(1); err != nil || !bytes.Equal(got, data) { + t.Fatalf("block 1 after the snapshot: %v", err) + } +}