diff --git a/seaweed-block/core/host/master/restore_refusal_test.go b/seaweed-block/core/host/master/restore_refusal_test.go new file mode 100644 index 0000000000..5e1b9b5798 --- /dev/null +++ b/seaweed-block/core/host/master/restore_refusal_test.go @@ -0,0 +1,106 @@ +package master + +import ( + "context" + "fmt" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" + "github.com/seaweedfs/seaweed-block/core/lifecycle" + control "github.com/seaweedfs/seaweed-block/core/rpc/control" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// restoreRefusalMaster is a master with a store-ready source holding the +// snapshot "s1", whose target creation answers refuse. +func restoreRefusalMaster(t *testing.T, refuse error) (*Host, *services, *int) { + t.Helper() + h := newTestMaster(t, t.TempDir()) + t.Cleanup(func() { closeTestMaster(t, h) }) + svc := newServices(h) + h.cfg.LayoutCreateVolume = func(context.Context, lifecycle.VolumeSpec) error { return nil } + if _, err := svc.CreateVolume(context.Background(), &control.CreateVolumeRequest{VolumeId: "source", SizeBytes: 1 << 20, ReplicationFactor: 1}); err != nil { + t.Fatal(err) + } + g := layout.Geometry{SizeBytes: 1 << 20, BlockSize: 4096, ChunkBytes: 4 << 20} + h.cfg.LayoutReadVolume = func(_ context.Context, source lifecycle.VolumeSpec) (layout.Volume, error) { + id, err := layout.ParseVolumeID(source.LayoutID) + return layout.Volume{ID: id, Name: source.VolumeID, Geometry: g, Format: layout.VolumeFormat}, err + } + h.cfg.LayoutReadHead = func(_ context.Context, source lifecycle.VolumeSpec) (layout.Head, error) { + id, _ := layout.ParseVolumeID(source.LayoutID) + return layout.Head{VolumeID: id, State: layout.StateLive, Snapshots: map[string]layout.Snapshot{"s1": {}}}, nil + } + calls := new(int) + h.cfg.LayoutCreateTarget = func(context.Context, lifecycle.VolumeSpec, lifecycle.VolumeSpec, string) error { + *calls++ + return refuse + } + return h, svc, calls +} + +// A restore its target creation refuses for good (here a snapshot of a +// restore target, which names another volume's chunks: the refusal the +// catalog's restore gives, before it writes anything) changes nothing: no +// lifecycle record is left under the target name, a replay is refused the +// same way, and the name stays free for a restore that is admitted. +func TestARefusedRestoreLeavesNoTargetRecord(t *testing.T) { + foreign := fmt.Errorf("%w: %q", layout.ErrForeignSource, "s1") + h, svc, calls := restoreRefusalMaster(t, foreign) + req := &control.CreateVolumeRequest{VolumeId: "target", SourceVolume: "source", SourceSnapshot: "s1", CreateRequestId: "restore-1"} + for i := 0; i < 2; i++ { + _, err := svc.CreateVolume(context.Background(), req) + if status.Code(err) != codes.FailedPrecondition { + t.Fatalf("attempt %d: refused restore answered %v; want FailedPrecondition", i, err) + } + if rec, ok := h.Lifecycle().Volumes.GetVolume("target"); ok { + t.Fatalf("attempt %d: a refused restore left the target record %+v", i, rec) + } + } + if *calls != 2 { + t.Fatalf("target creation ran %d times; want once per attempt", *calls) + } + h.cfg.LayoutCreateTarget = func(context.Context, lifecycle.VolumeSpec, lifecycle.VolumeSpec, string) error { return nil } + resp, err := svc.CreateVolume(context.Background(), &control.CreateVolumeRequest{VolumeId: "target", SourceVolume: "source", SourceSnapshot: "s1", CreateRequestId: "restore-2"}) + if err != nil || !resp.GetStoreReady() { + t.Fatalf("an admitted restore under the refused name: %+v %v", resp, err) + } +} + +// Control: an interrupted target creation (any other error) keeps its +// not-ready record for the replay to heal, as before. +func TestAnInterruptedRestoreKeepsItsRecordForTheReplay(t *testing.T) { + h, svc, _ := restoreRefusalMaster(t, fmt.Errorf("filer unavailable")) + req := &control.CreateVolumeRequest{VolumeId: "target", SourceVolume: "source", SourceSnapshot: "s1", CreateRequestId: "restore-1"} + if _, err := svc.CreateVolume(context.Background(), req); status.Code(err) != codes.Unavailable { + t.Fatalf("interrupted restore: %v; want Unavailable (retry)", err) + } + if rec, ok := h.Lifecycle().Volumes.GetVolume("target"); !ok || rec.StoreReady { + t.Fatalf("interrupted restore: record %+v present=%t; want a not-ready record", rec, ok) + } +} + +// workspace show of a name whose store never became ready and whose +// deletion is admitted says it is being deleted, not to retry a +// CreateVolume that is refused for a deleted name. +func TestWorkspaceShowOfADeletedNotReadyNameSaysDeleted(t *testing.T) { + h := newTestMaster(t, t.TempDir()) + t.Cleanup(func() { closeTestMaster(t, h) }) + h.cfg.WorkspaceRPC = true + svc := newServices(h) + h.cfg.LayoutCreateVolume = func(context.Context, lifecycle.VolumeSpec) error { return fmt.Errorf("filer unavailable") } + if _, err := svc.CreateVolume(context.Background(), &control.CreateVolumeRequest{VolumeId: "x", SizeBytes: 1 << 20, ReplicationFactor: 1}); status.Code(err) != codes.Unavailable { + t.Fatalf("setup: not-ready create: %v", err) + } + q := &control.WorkspaceVolume{VolumeId: "x"} + if _, err := svc.QueryWorkspace(context.Background(), q); status.Convert(err).Message() != "workspace: created, store not ready; retry CreateVolume" { + t.Fatalf("control: a not-ready name not being deleted: %v; want the retry hint", err) + } + if _, err := h.lifecycle.Deletions.Admit("x", "del-x"); err != nil { + t.Fatal(err) + } + if _, err := svc.QueryWorkspace(context.Background(), q); status.Convert(err).Message() != "workspace: volume is being deleted" { + t.Fatalf("a deleted not-ready name: %v; want 'workspace: volume is being deleted'", err) + } +} diff --git a/seaweed-block/core/host/master/services.go b/seaweed-block/core/host/master/services.go index 5f7b838e6a..c2c95442bf 100644 --- a/seaweed-block/core/host/master/services.go +++ b/seaweed-block/core/host/master/services.go @@ -162,6 +162,16 @@ func (s *services) CreateVolume(ctx context.Context, req *control.CreateVolumeRe var errLayoutStoreNotReady = errors.New("layout store not ready") +// restoreRefusedForGood: target creation refused the restore before any +// write (RunAddDependent's checks, ahead of its update), for a reason no +// replay changes: the snapshot names another volume's chunks, or it is +// absent (a recorded dependent keeps a snapshot from being deleted). Other +// refusals, such as a geometry mismatch with an existing VOLUME, can follow +// the dependent's write and keep the record for the replay. +func restoreRefusedForGood(err error) bool { + return errors.Is(err, layout.ErrForeignSource) || errors.Is(err, layout.ErrNoSnapshot) +} + // createLifecycleVolume is called under workspaceMu. A failed filer create // leaves the durable record not-ready; replay uses its same identity and // completes VOLUME/HEAD before publishing the one-way placement hint. @@ -189,6 +199,15 @@ func (h *Host) createLifecycleVolume(ctx context.Context, spec lifecycle.VolumeS return lifecycle.VolumeRecord{}, fmt.Errorf("%w: restore source or target creator is unavailable", errLayoutStoreNotReady) } createErr = h.cfg.LayoutCreateTarget(ctx, source.Spec, rec.Spec, rec.Spec.SourceSnapshot) + if restoreRefusedForGood(createErr) { + // Refused before it wrote anything, and a replay is refused the + // same way (a snapshot's root never changes): a refused restore + // changes nothing, so its not-ready record goes too. + if err := h.lifecycle.Volumes.DeleteVolume(rec.Spec.VolumeID); err != nil { + return lifecycle.VolumeRecord{}, fmt.Errorf("%w: %w (undo of its record: %v)", errLayoutStoreNotReady, createErr, err) + } + return lifecycle.VolumeRecord{}, createErr + } } else { createErr = h.cfg.LayoutCreateVolume(ctx, rec.Spec) } diff --git a/seaweed-block/core/host/master/workspace_service.go b/seaweed-block/core/host/master/workspace_service.go index e2524cd762..de55d43470 100644 --- a/seaweed-block/core/host/master/workspace_service.go +++ b/seaweed-block/core/host/master/workspace_service.go @@ -26,12 +26,14 @@ func (s *services) workspaceVolume(volume string) error { if !ok || r.Spec.ReplicationFactor != 1 { return status.Error(codes.FailedPrecondition, "workspace move requires an existing RF1 volume") } - if !s.host.layoutStoreReady(volume) { - return status.Error(codes.FailedPrecondition, "workspace: created, store not ready; retry CreateVolume") - } + // Deletion first: a deleted name's CreateVolume is refused, so it must + // never be told to retry it. if s.host.lifecycle.Deletions.Reserved(volume) { return status.Error(codes.FailedPrecondition, "workspace: volume is being deleted") } + if !s.host.layoutStoreReady(volume) { + return status.Error(codes.FailedPrecondition, "workspace: created, store not ready; retry CreateVolume") + } return nil } @@ -52,15 +54,15 @@ func (s *services) workspaceVolumeForRenewal(volume string) error { if !ok || r.Spec.ReplicationFactor != 1 { return status.Error(codes.FailedPrecondition, "workspace move requires an existing RF1 volume") } - if !s.host.layoutStoreReady(volume) { - return status.Error(codes.FailedPrecondition, "workspace: created, store not ready; retry CreateVolume") - } if s.host.lifecycle.Deletions.ReservedSnapshot(volume) { if s.host.lifecycle.Deletions.CommittedSnapshot(volume) { return leaseRefusal("workspace: volume is being deleted", control.WorkspaceLeaseRefusal_DELETED, 0) } return status.Error(codes.FailedPrecondition, "workspace: volume is being deleted") } + if !s.host.layoutStoreReady(volume) { + return status.Error(codes.FailedPrecondition, "workspace: created, store not ready; retry CreateVolume") + } return nil }