diff --git a/seaweed-block/core/frontend/durable/provider_chunk_lifetime_test.go b/seaweed-block/core/frontend/durable/provider_chunk_lifetime_test.go index 34513119ff..d9fa1ad44d 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk_lifetime_test.go +++ b/seaweed-block/core/frontend/durable/provider_chunk_lifetime_test.go @@ -395,3 +395,109 @@ func TestWALStoreChunk_ConcurrentRecoverAttachesOnce(t *testing.T) { t.Fatalf("capture after concurrent recovery: %v", err) } } + +// The caller's recovery deadline governs the attach's first remote call, +// with no Close and no hold release. +func TestWALStoreChunk_RecoverHonoursCallerDeadline(t *testing.T) { + g := newGated(storage.NewMemChunkRemote()) + g.holdGetHead = make(chan struct{}) + g.honourCtx = true + t.Cleanup(g.release) + p := chunkProvider(t, t.TempDir(), g, v1View(), 0) + if _, err := p.EnsureStorage("v1"); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond) + defer cancel() + recErr := make(chan error, 1) + go func() { _, err := p.RecoverVolume(ctx, "v1"); recErr <- err }() + waitEntered(t, g, "gethead") + if err := recvErr(t, recErr, "RecoverVolume past its caller deadline"); err == nil { + t.Fatal("RecoverVolume succeeded although its deadline expired during attach") + } +} + +// An attached layer outlives the recovery context: main cancels it right +// after recovery, and captures must keep working. +func TestWALStoreChunk_LayerOutlivesRecoveryContext(t *testing.T) { + remote := storage.NewMemChunkRemote() + p := chunkProvider(t, t.TempDir(), remote, v1View(), 0) + s, err := p.EnsureStorage("v1") + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + if _, err := p.RecoverVolume(ctx, "v1"); err != nil { + t.Fatal(err) + } + cancel() + if _, err := s.Write(1, block(1)); err != nil { + t.Fatal(err) + } + if _, err := p.CaptureVolume(context.Background(), "v1"); err != nil { + t.Fatalf("capture after the recovery context ended: %v", err) + } +} + +// Recovery before the master's assignment is visible does not bind the +// layer to epoch 0: once the projection carries the assignment, the +// volume publishes under it. +func TestWALStoreChunk_AttachWaitsForMasterAuthority(t *testing.T) { + remote := storage.NewMemChunkRemote() + view := newStubView(frontend.Projection{VolumeID: "v1", ReplicaID: "r1"}) // pre-assignment + p := chunkProvider(t, t.TempDir(), remote, view, 0) + s, err := p.EnsureStorage("v1") + if err != nil { + t.Fatal(err) + } + if _, err := p.RecoverVolume(context.Background(), "v1"); err != nil { + t.Fatalf("RecoverVolume before assignment: %v", err) + } + if _, err := s.Write(1, block(1)); err != nil { + t.Fatal(err) + } + // Healthy but with no master-assigned epoch is not authority either. + view.set(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: "r1"})) + time.Sleep(150 * time.Millisecond) + if _, err := p.CaptureVolume(context.Background(), "v1"); err == nil { + t.Fatal("capture succeeded under a healthy projection with epoch 0") + } + if head, _ := remote.GetHead(context.Background()); head.Gen != 0 { + t.Fatalf("published under epoch 0: HEAD gen %d epoch %d", head.Gen, head.Epoch) + } + view.set(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 4, EndpointVersion: 2})) + deadline := time.Now().Add(10 * time.Second) + for { + _, err = p.CaptureVolume(context.Background(), "v1") + if err == nil { + break + } + if time.Now().After(deadline) { + t.Fatalf("capture never succeeded under the assigned epoch: %v", err) + } + time.Sleep(20 * time.Millisecond) + } + if head, _ := remote.GetHead(context.Background()); head.Epoch != 4 { + t.Fatalf("HEAD epoch %d, want the assigned 4", head.Epoch) + } +} + +// Close while a volume still waits for authority returns and leaves no +// layer behind. +func TestWALStoreChunk_CloseWhileAwaitingAuthority(t *testing.T) { + view := newStubView(frontend.Projection{VolumeID: "v1", ReplicaID: "r1"}) + p := chunkProvider(t, t.TempDir(), storage.NewMemChunkRemote(), view, 0) + if _, err := p.EnsureStorage("v1"); err != nil { + t.Fatal(err) + } + if _, err := p.RecoverVolume(context.Background(), "v1"); err != nil { + t.Fatal(err) + } + closed := make(chan error, 1) + go func() { closed <- p.Close() }() + _ = recvErr(t, closed, "Close") + view.set(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 4, EndpointVersion: 2})) + if _, err := p.CaptureVolume(context.Background(), "v1"); !errors.Is(err, durable.ErrVolumeNotOpen) { + t.Fatalf("chunk layer reachable after Close: %v", err) + } +} diff --git a/seaweed-block/core/host/volume/chunk_bootstrap_test.go b/seaweed-block/core/host/volume/chunk_bootstrap_test.go new file mode 100644 index 0000000000..a451f269dd --- /dev/null +++ b/seaweed-block/core/host/volume/chunk_bootstrap_test.go @@ -0,0 +1,126 @@ +package volume + +// walstore-chunk in the production startup order of cmd/blockvolume: +// the host blocks local readiness, the master's assignment arrives and is +// retained, the durable provider opens and recovers the volume, readiness +// clears and SetReplicationVolume replays the assignment. Only then is +// the master-assigned epoch known; the chunk layer must publish under it. + +import ( + "bytes" + "context" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/frontend/durable" + "github.com/seaweedfs/seaweed-block/core/replication" + control "github.com/seaweedfs/seaweed-block/core/rpc/control" + "github.com/seaweedfs/seaweed-block/core/storage" +) + +func TestWALStoreChunk_ProductionStartupPublishesUnderAssignedEpoch(t *testing.T) { + h, err := New(Config{ + MasterAddr: "127.0.0.1:1", // never dialed: no Start + ServerID: "s1", VolumeID: "v1", ReplicaID: "r1", + DataAddr: "127.0.0.1:2", CtrlAddr: "127.0.0.1:3", + EnableT1Readiness: true, + Logger: newDiscardLogger(), + }) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = h.Close() }) + + // main.go: block readiness before Start; the assignment arrives and + // is retained, not applied. + h.BlockLocalReadiness("awaiting durable recovery") + h.applyFact(&control.AssignmentFact{VolumeId: "v1", ReplicaId: "r1", Epoch: 5, EndpointVersion: 2, PeerSetGeneration: 1}) + if p := h.ProjectionView().Projection(); p.Healthy || p.Epoch != 0 { + t.Fatalf("assignment applied while readiness was blocked: %+v", p) + } + + remote := storage.NewMemChunkRemote() + dp, err := durable.NewDurableProvider(durable.ProviderConfig{ + Impl: durable.ImplWALStoreChunk, + StorageRoot: t.TempDir(), + BlockSize: 4096, + NumBlocks: 256, + ChunkBlocks: 16, + ChunkCaptureInterval: 20 * time.Millisecond, + ChunkRemoteFor: func(string) (storage.ChunkRemote, error) { return remote, nil }, + }, h.ProjectionView()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = dp.Close() }) + if _, err := dp.EnsureStorage("v1"); err != nil { + t.Fatal(err) + } + recCtx, recCancel := context.WithTimeout(context.Background(), 10*time.Second) + _, recErr := dp.RecoverVolume(recCtx, "v1") + recCancel() + if recErr != nil { + t.Fatalf("RecoverVolume during blocked readiness: %v", recErr) + } + + // main.go: clear readiness, install replication; the retained + // assignment replays. + h.ClearLocalReadinessBlock() + h.SetReplicationVolume(replication.NewReplicationVolume("v1", dp.LogicalStorage("v1"))) + + openCtx, openCancel := context.WithTimeout(context.Background(), 10*time.Second) + defer openCancel() + be, err := dp.Open(openCtx, "v1") + if err != nil { + t.Fatalf("frontend open after replay: %v", err) + } + if p := h.ProjectionView().Projection(); !p.Healthy || p.Epoch != 5 || p.EndpointVersion != 2 { + t.Fatalf("projection after replay: %+v", p) + } + data := bytes.Repeat([]byte{0x5A}, 4096) + deadline := time.Now().Add(10 * time.Second) + for { + if _, err = be.Write(context.Background(), 40*4096, data); err == nil { + break + } + if time.Now().After(deadline) { + t.Fatalf("frontend write never admitted: %v", err) + } + time.Sleep(20 * time.Millisecond) + } + if err := be.Sync(context.Background()); err != nil { + t.Fatal(err) + } + + // On-demand capture under the assigned lineage. + lsn, err := dp.CaptureVolume(context.Background(), "v1") + if err != nil { + t.Fatalf("on-demand capture after startup: %v", err) + } + head, _ := remote.GetHead(context.Background()) + if head.Epoch != 5 || head.Holder != "r1" || head.RemoteLSN != lsn { + t.Fatalf("HEAD after on-demand capture: epoch %d holder %q remote_lsn %d (want 5, r1, %d)", head.Epoch, head.Holder, head.RemoteLSN, lsn) + } + + // Periodic capture under the same lineage. + if _, err := be.Write(context.Background(), 200*4096, bytes.Repeat([]byte{0xC8}, 4096)); err != nil { + t.Fatal(err) + } + if err := be.Sync(context.Background()); err != nil { + t.Fatal(err) + } + deadline = time.Now().Add(10 * time.Second) + for { + head, _ = remote.GetHead(context.Background()) + if _, ok := head.Chunks[200/16]; ok && head.RemoteLSN > lsn { + break + } + if time.Now().After(deadline) { + t.Fatalf("periodic capture never published the second write (HEAD gen %d remote_lsn %d)", head.Gen, head.RemoteLSN) + } + time.Sleep(20 * time.Millisecond) + } + if head.Epoch != 5 { + t.Fatalf("periodic HEAD epoch %d, want 5", head.Epoch) + } +}