From 6cd951696b61e06d43c79ed2b23da5b3e3dbaecb Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 15:07:10 -0800 Subject: [PATCH] chunkstore: a late HEAD create of an abandoned restore lands after the name is reused A restore dies between its VOLUME and its HEAD; the operator releases it, the snapshot is deleted and the name is taken by a new volume. The abandoned restore's late HEAD create must be refused (the deleted HEAD in its never-reused directory) and the new volume untouched. Co-Authored-By: Claude Opus 5.5 --- .../chunkstore/catalog_name_reuse_test.go | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 seaweed-block/core/frontend/chunkstore/catalog_name_reuse_test.go diff --git a/seaweed-block/core/frontend/chunkstore/catalog_name_reuse_test.go b/seaweed-block/core/frontend/chunkstore/catalog_name_reuse_test.go new file mode 100644 index 0000000000..e90d325894 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/catalog_name_reuse_test.go @@ -0,0 +1,84 @@ +package chunkstore + +import ( + "context" + "errors" + "path" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb" + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" +) + +// headDropFiler refuses the first create of HEAD in dir: the restore dies +// after its VOLUME and before its HEAD. +type headDropFiler struct { + Filer + dir string + dropped bool +} + +func (f *headDropFiler) CreateEntry(ctx context.Context, dir string, e *filerpb.Entry, oExcl bool) error { + if !f.dropped && dir == f.dir && e.GetName() == "HEAD" { + f.dropped = true + return errors.New("injected: the restore died before its HEAD") + } + return f.Filer.CreateEntry(ctx, dir, e, oExcl) +} + +// A restore is abandoned between its VOLUME and its HEAD; the operator +// releases it (its directory gets a deleted HEAD first), the source's +// snapshot is deleted, and the name is taken by a new volume. Then the +// abandoned restore's late HEAD create lands. It must be refused: the old +// directory is never reused (the new volume has its own) and the deleted +// HEAD there stops the create, so no live target ever exists without the +// snapshot whose chunks it names, and the new volume is untouched. +func TestBCatalog_AbandonedRestoreNameReusedLateHeadCreateRefused(t *testing.T) { + ctx := context.Background() + geometry := layout.Geometry{SizeBytes: 64 * 4096, BlockSize: 4096, ChunkBytes: 4 * 4096} + src, x := newRef(t, "src"), newRef(t, "t") + f := &headDropFiler{Filer: newFakeFiler(), dir: path.Join("/blocks", x.Dir())} + c, err := NewBCatalog(f, newFakeBlobs(), "/blocks") + if err != nil { + t.Fatal(err) + } + if _, err := c.CreateVolume(ctx, src, geometry); err != nil { + t.Fatal(err) + } + commitSnapshot(t, c, src, []byte("source chunk"), "daily") + + if _, err := c.Restore(ctx, src, "daily", x); err == nil || !f.dropped { + t.Fatalf("restore: %v (dropped %t); want it to die before its HEAD", err, f.dropped) + } + if _, found, err := c.ReadHead(ctx, x); err != nil || found { + t.Fatalf("abandoned target HEAD found=%t %v; want none", found, err) + } + if _, err := c.ReleaseTarget(ctx, src, x); err != nil { + t.Fatalf("operator release of the abandoned target: %v", err) + } + if _, err := c.DeleteSnapshot(ctx, src, "daily"); err != nil { + t.Fatalf("snapshot delete after the release: %v", err) + } + y := VolumeRef{Name: x.Name, ID: newRef(t, "t").ID} // the name is reused + if _, err := c.CreateVolume(ctx, y, geometry); err != nil { + t.Fatalf("a new volume under the freed name: %v", err) + } + yh, found, err := c.ReadHead(ctx, y) + if err != nil || !found { + t.Fatalf("new volume HEAD found=%t %v", found, err) + } + + xs, err := c.Store(x) + if err != nil { + t.Fatal(err) + } + if err := xs.CreateHead(ctx, layout.Unclaimed(x.ID, nil)); !errors.Is(err, layout.ErrExists) { + t.Fatalf("the abandoned restore's late HEAD create: %v; want ErrExists (a live target without its snapshot)", err) + } + if xh, found, err := c.ReadHead(ctx, x); err != nil || !found || xh.State != layout.StateDeleted { + t.Fatalf("abandoned target HEAD %+v found=%t %v; want it deleted", xh, found, err) + } + if got, found, err := c.ReadHead(ctx, y); err != nil || !found || got.VolumeID != y.ID || got.Gen != yh.Gen || got.State != yh.State { + t.Fatalf("new volume HEAD %+v found=%t %v; want its own, unchanged (%+v)", got, found, err, yh) + } +}