From c88f028cf6daa4bd74fef5b88ca90ff482313878 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 18 Nov 2026 23:17:43 -0800 Subject: [PATCH 1/6] transport: G1/G2 refusal checks by probe handshake, not a read timer #227: g2Refused gave the listener 500 ms to close a refused connection; a loaded machine can take longer and the refusal was misread as acceptance. A probe at the same lineage now follows the frame on the same connection: the listener handles frames in order, so it either closes (refused) or answers after the frame's effect (accepted). g1Ship checks the bytes once after that answer instead of polling for 2 s; g1Barrier drops its deadline. New TestG1_SlowPersistFailureIsStillARefusal: a persist failure slowed past the old window is still a refusal. Co-Authored-By: Claude Opus 5.5 --- .../core/transport/g1_durable_floor_test.go | 49 +++++++++++++------ .../core/transport/g2_self_fence_test.go | 37 ++++++++------ 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/seaweed-block/core/transport/g1_durable_floor_test.go b/seaweed-block/core/transport/g1_durable_floor_test.go index 32a45a07c0..cabcb631ca 100644 --- a/seaweed-block/core/transport/g1_durable_floor_test.go +++ b/seaweed-block/core/transport/g1_durable_floor_test.go @@ -54,32 +54,27 @@ func g1Lineage(sess, epoch uint64) RecoveryLineage { } // g1Ship sends one ship on a fresh connection and reports whether it was -// applied (bytes visible within d) or refused (connection closed). +// applied or refused (g2Refused's probe handshake: an accepted ship is applied +// before the probe is answered). func g1Ship(t *testing.T, ln *ReplicaListener, store storage.LogicalStorage, l RecoveryLineage, lsn uint64, fill byte) bool { t.Helper() want := bytes.Repeat([]byte{fill}, 4096) - if g2Refused(t, ln.Addr(), MsgShipEntry, EncodeShipEntry(ShipEntry{Lineage: l, LBA: 0, LSN: lsn, Data: want})) { - if bytes.Equal(g1Block(t, store), want) { - t.Fatalf("epoch-%d ship refused yet its bytes were applied", l.Epoch) - } - return false + refused := g2Refused(t, ln.Addr(), MsgShipEntry, EncodeShipEntry(ShipEntry{Lineage: l, LBA: 0, LSN: lsn, Data: want}), l) + applied := bytes.Equal(g1Block(t, store), want) + if refused && applied { + t.Fatalf("epoch-%d ship refused yet its bytes were applied", l.Epoch) } - deadline := time.Now().Add(2 * time.Second) - for !bytes.Equal(g1Block(t, store), want) { - if time.Now().After(deadline) { - t.Fatalf("epoch-%d ship neither refused nor applied", l.Epoch) - } - time.Sleep(5 * time.Millisecond) + if !refused && !applied { + t.Fatalf("epoch-%d ship accepted but its bytes are not visible after the probe answer", l.Epoch) } - return true + return !refused } // g1Barrier sends a barrier and reports whether it was ACKed (a barrier // response echoing the lineage) or refused (connection closed, no ACK). func g1Barrier(t *testing.T, ln *ReplicaListener, l RecoveryLineage) bool { t.Helper() - conn := g2Dial(t, ln.Addr()) - _ = conn.SetDeadline(time.Now().Add(2 * time.Second)) + conn := g2Dial(t, ln.Addr()) // no deadline: the listener answers or closes if err := WriteMsg(conn, MsgBarrierReq, EncodeLineage(l)); err != nil { t.Fatal(err) } @@ -100,7 +95,7 @@ func g1Barrier(t *testing.T, ln *ReplicaListener, l RecoveryLineage) bool { func g1Rebuild(t *testing.T, ln *ReplicaListener, store storage.LogicalStorage, l RecoveryLineage, fill byte) bool { t.Helper() data := bytes.Repeat([]byte{fill}, 4096) - refused := g2Refused(t, ln.Addr(), MsgRebuildBlock, EncodeRebuildBlock(l, 0, data)) + refused := g2Refused(t, ln.Addr(), MsgRebuildBlock, EncodeRebuildBlock(l, 0, data), l) if refused && bytes.Equal(g1Block(t, store), data) { t.Fatalf("epoch-%d rebuild refused yet its bytes were written", l.Epoch) } @@ -331,6 +326,28 @@ func TestG1_PersistFailureRefusesTheRaisingMutation(t *testing.T) { } } +// A refusal that is slow (a loaded machine; here a persist failure delayed +// past the old 500 ms read window) is still classified as a refusal: the +// probe handshake waits for the listener's answer or close, not a timer. +func TestG1_SlowPersistFailureIsStillARefusal(t *testing.T) { + store := storage.NewBlockStore(16, 4096) + floor := g1Fresh(t) + ln := g1Listener(t, store, floor) + if err := ln.AdmitAsReplica(1); err != nil { + t.Fatal(err) + } + ops := osFloorOps + ops.rename = func(string, string) error { + time.Sleep(700 * time.Millisecond) // injected slowness, not a bound + return errors.New("injected slow rename failure") + } + floorFS = ops + t.Cleanup(func() { floorFS = osFloorOps }) + if g1Ship(t, ln, store, g1Lineage(1, 2), 1, 's') { + t.Fatal("a slow refused ship was classified as applied") + } +} + // The role calls persist before they return; a persist failure is returned // and nothing changes (an unassigned listener stays unassigned). func TestG1_RoleCallsPersistBeforeReturning(t *testing.T) { diff --git a/seaweed-block/core/transport/g2_self_fence_test.go b/seaweed-block/core/transport/g2_self_fence_test.go index 903586f435..98ba71d108 100644 --- a/seaweed-block/core/transport/g2_self_fence_test.go +++ b/seaweed-block/core/transport/g2_self_fence_test.go @@ -1,9 +1,7 @@ package transport import ( - "errors" "net" - "os" "testing" "time" @@ -25,16 +23,27 @@ func g2Dial(t *testing.T, addr string) net.Conn { } // g2Refused sends one mutating frame and reports whether the listener closed -// the connection (refusal) rather than leaving it open (accepted). -func g2Refused(t *testing.T, addr string, msgType byte, payload []byte) bool { +// the connection (refusal) rather than accepting it. Handshake, no timer: a +// probe at the same lineage follows on the same connection; the listener +// handles frames in order, so it either closes the connection (refused) or +// answers the probe after the frame's effect (accepted). +func g2Refused(t *testing.T, addr string, msgType byte, payload []byte, l RecoveryLineage) bool { t.Helper() conn := g2Dial(t, addr) if err := WriteMsg(conn, msgType, payload); err != nil { t.Fatal(err) } - _ = conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond)) - _, err := conn.Read(make([]byte, 1)) - return err != nil && !errors.Is(err, os.ErrDeadlineExceeded) + if err := WriteMsg(conn, MsgProbeReq, EncodeProbeReq(ProbeRequest{Lineage: l})); err != nil { + return true // the listener already closed the connection + } + replyType, _, err := ReadMsg(conn) + if err != nil { + return true + } + if replyType != MsgProbeResp { + t.Fatalf("frame 0x%02x followed by a probe answered 0x%02x", msgType, replyType) + } + return false } func g2ShipPayload(l RecoveryLineage, lsn uint64) []byte { @@ -46,13 +55,13 @@ func TestG2_PrimaryListenerRefusesMutationsButAnswersProbes(t *testing.T) { listener.FenceAsPrimary(2) newer := RecoveryLineage{SessionID: 9, Epoch: 3, EndpointVersion: 1, TargetLSN: 1} - if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(newer, 1)) { + if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(newer, 1), newer) { t.Fatal("a primary applied an inbound ship (even at a higher epoch)") } - if !g2Refused(t, listener.Addr(), MsgRecoveryLaneStart, EncodeLineage(newer)) { + if !g2Refused(t, listener.Addr(), MsgRecoveryLaneStart, EncodeLineage(newer), newer) { t.Fatal("a primary accepted a recovery lane start") } - if !g2Refused(t, listener.Addr(), MsgRebuildBlock, EncodeRebuildBlock(newer, 0, make([]byte, 4096))) { + if !g2Refused(t, listener.Addr(), MsgRebuildBlock, EncodeRebuildBlock(newer, 0, make([]byte, 4096)), newer) { t.Fatal("a primary accepted a rebuild block") } @@ -72,15 +81,15 @@ func TestG2_FloorStaysWhenThePrimaryLaterSupportsAnother(t *testing.T) { listener.FenceAsPrimary(2) listener.AdmitAsReplica(3) // superseded, now supporting the epoch-3 primary - if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 1, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}, 1)) { + if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 1, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}, 1), RecoveryLineage{SessionID: 1, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}) { t.Fatal("a ship below the floor (epoch 2 < 3) was accepted") } - if g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 2, Epoch: 3, EndpointVersion: 1, TargetLSN: 1}, 1)) { + if g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 2, Epoch: 3, EndpointVersion: 1, TargetLSN: 1}, 1), RecoveryLineage{SessionID: 2, Epoch: 3, EndpointVersion: 1, TargetLSN: 1}) { t.Fatal("the epoch-3 primary's ship was refused by its supporting replica") } listener.AdmitAsReplica(1) // a stale, lower admission never lowers the floor - if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 3, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}, 2)) { + if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 3, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}, 2), RecoveryLineage{SessionID: 3, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}) { t.Fatal("the floor went down after a lower admission") } } @@ -180,7 +189,7 @@ func TestG2_FenceReportsAMutationStuckPastTheBound(t *testing.T) { t.Fatal("FenceAsPrimary returned nil with a mutation still applying past the bound") } newer := RecoveryLineage{SessionID: 9, Epoch: 3, EndpointVersion: 1, TargetLSN: 1} - if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(newer, 2)) { + if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(newer, 2), newer) { t.Fatal("after a timed-out fence the listener accepted a new ship") } } From b5f5076070cece44c790388a0d2f0ea0b3ba39e8 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 01:13:44 -0800 Subject: [PATCH 2/6] test: #227 part 2: progress conditions in durable restore, chunk BASE, v3 flusher tests - durable TestRestoreWaitsForFirstAssignment / TestRestoreDeferredAssignment WaitEndsOnDeadlineAndClose: the 2 s test bounds become progress waits; the 40 ms ChunkAssignmentTimeout under test is unchanged; the limit poll fails at once if the backend becomes operational without an assignment. - TestWALStoreChunk_ExplicitCutRestartsTheTimer: the final wait for the automatic cut is a progress wait; the early-cut window check (with its slow-host skip) is the timing assertion and stays. - storage TestChunkLayerV3_BASEBeginDrainsWholeCut: the held-upload, drain, settlement and resume waits are progress waits (were 5 s). - TestWALStoreV3FlusherWritesTheExtentButHoldsTheCheckpointUntilTheMap: stop the automatic flusher before writes, as walstore_test does, so the simulated crash cannot race its use of the descriptor ('volume locked'). Co-Authored-By: Claude Opus 5.5 --- .../durable/provider_assignment_test.go | 53 ++++++------------- .../durable/provider_chunk_sched_test.go | 12 ++--- .../core/storage/chunk_base_v3_test.go | 17 ++---- .../core/storage/walstore_v3_test.go | 9 ++++ 4 files changed, 32 insertions(+), 59 deletions(-) diff --git a/seaweed-block/core/frontend/durable/provider_assignment_test.go b/seaweed-block/core/frontend/durable/provider_assignment_test.go index 2bf0853c4e..baee18b66f 100644 --- a/seaweed-block/core/frontend/durable/provider_assignment_test.go +++ b/seaweed-block/core/frontend/durable/provider_assignment_test.go @@ -37,7 +37,9 @@ func (v *arrivingAssignment) Projection() frontend.Projection { } func TestRestoreWaitsForFirstAssignment(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + // No wall-clock bound (#227): every wait below is a progress condition; a + // hang is reported by the go test timeout. + ctx, cancel := context.WithCancel(context.Background()) defer cancel() v := &arrivingAssignment{seen: make(chan struct{}), assigned: make(chan struct{})} p := chunkProviderForAssignment(t, v) @@ -49,34 +51,21 @@ func TestRestoreWaitsForFirstAssignment(t *testing.T) { defer finishRecovery() done := make(chan error, 1) go func() { _, err := p.RecoverVolume(recoverCtx, "v1"); done <- err }() - select { - case <-v.seen: - case <-ctx.Done(): - t.Fatal("recovery never inspected assignment") - } - select { - case err := <-done: - if err != nil { - t.Fatalf("first assignment did not converge: %v", err) - } - case <-ctx.Done(): - t.Fatal("local recovery must settle before assignment can be applied") + <-v.seen // recovery inspected the assignment + if err := <-done; err != nil { // local recovery settles before assignment can be applied + t.Fatalf("first assignment did not converge: %v", err) } if statuses := p.DurableStatuses(); len(statuses) != 1 || statuses[0].Operational { t.Fatalf("unassigned backend became operational: %+v", statuses) } finishRecovery() // deferred attachment outlives a completed recovery caller close(v.assigned) - for { + for { // progress: the deferred attachment becomes operational statuses := p.DurableStatuses() if len(statuses) == 1 && statuses[0].Operational { break } - select { - case <-ctx.Done(): - t.Fatalf("assignment never became operational: %+v", statuses) - case <-time.After(time.Millisecond): - } + time.Sleep(time.Millisecond) } if _, err := p.CaptureVolume(ctx, "v1"); err != nil { t.Fatal(err) @@ -121,19 +110,10 @@ func TestRestoreDeferredAssignmentWaitEndsOnDeadlineAndClose(t *testing.T) { defer cancel() done := make(chan error, 1) go func() { _, err := p.RecoverVolume(ctx, "v1"); done <- err }() - select { - case <-v.seen: - case <-time.After(2 * time.Second): - t.Fatal("no assignment inspection") - } + <-v.seen // progress, no timer (#227): recovery inspected the assignment // Local recovery must return first, so the host can apply assignment. - select { - case err := <-done: - if err != nil { - t.Fatal(err) - } - case <-time.After(2 * time.Second): - t.Fatal("local recovery blocked on assignment") + if err := <-done; err != nil { + t.Fatal(err) } cancel() // recovery caller no longer owns the deferred worker if mode == "close" { @@ -144,18 +124,15 @@ func TestRestoreDeferredAssignmentWaitEndsOnDeadlineAndClose(t *testing.T) { if mode == "close" { return } - deadline := time.NewTimer(2 * time.Second) - defer deadline.Stop() - for { + for { // progress: the 40 ms assignment limit fails the wait closed status := p.DurableStatuses() if len(status) == 1 && !status[0].Operational && strings.Contains(status[0].Evidence, "deadline exceeded") { break } - select { - case <-deadline.C: - t.Fatalf("assignment wait did not fail closed: %+v", status) - case <-time.After(time.Millisecond): + if len(status) == 1 && status[0].Operational { + t.Fatalf("became operational without an assignment: %+v", status) } + time.Sleep(time.Millisecond) } }) } diff --git a/seaweed-block/core/frontend/durable/provider_chunk_sched_test.go b/seaweed-block/core/frontend/durable/provider_chunk_sched_test.go index a5f05fb8ae..6e75b9234d 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk_sched_test.go +++ b/seaweed-block/core/frontend/durable/provider_chunk_sched_test.go @@ -96,14 +96,10 @@ func TestWALStoreChunk_ExplicitCutRestartsTheTimer(t *testing.T) { if h := headGen(t, remote); h.Gen != 1 { t.Fatalf("automatic cut %s after the explicit one (HEAD gen %d): the timer did not restart", time.Since(explicit), h.Gen) } - deadline := explicit.Add(interval + 5*time.Second) - for { - if h := headGen(t, remote); h.Gen >= 2 { - return - } - if time.Now().After(deadline) { - t.Fatal("no automatic cut one interval after the explicit cut") - } + // Progress, no timer (#227): the automatic cut follows; a loaded host only + // delays it (the early-cut check above is the timing assertion, and it + // skips when the host cannot separate the two deadlines). + for headGen(t, remote).Gen < 2 { time.Sleep(50 * time.Millisecond) } } diff --git a/seaweed-block/core/storage/chunk_base_v3_test.go b/seaweed-block/core/storage/chunk_base_v3_test.go index 853c5aae41..87135ab735 100644 --- a/seaweed-block/core/storage/chunk_base_v3_test.go +++ b/seaweed-block/core/storage/chunk_base_v3_test.go @@ -52,7 +52,7 @@ func TestChunkLayerV3_BASEBeginDrainsWholeCut(t *testing.T) { } entered, release := holdPuts(t, f) cutDone := startCapture(t, l) - waitKey(t, entered, "held cut upload after barrier") + <-entered // progress, no timer (#227): the cut's upload is held after its barrier attempted := make(chan struct{}) l.testBaseTransitionBeforeLock = func() { close(attempted) } type result struct { @@ -70,26 +70,17 @@ func TestChunkLayerV3_BASEBeginDrainsWholeCut(t *testing.T) { } }) go func() { defer close(joined); h, e := s.BeginBaseInstall(7, idA); finished <- result{h, e} }() - select { - case <-attempted: - case <-time.After(ctLocalBound): - t.Fatal("BASE did not reach outer drain") - } + <-attempted // BASE reached the outer drain select { case r := <-finished: t.Fatalf("BASE begin completed before the captured upload/CAS drained: %v", r.err) case <-time.After(200 * time.Millisecond): } release() - if e := waitErr(t, cutDone, "old cut settlement"); e != nil { + if e := <-cutDone; e != nil { // the old cut settles once released t.Fatal(e) } - var r result - select { - case r = <-finished: - case <-time.After(ctLocalBound): - t.Fatal("BASE begin failed to resume after cut settled") - } + r := <-finished // BASE begin resumes after the cut settled if r.err != nil { t.Fatal(r.err) } diff --git a/seaweed-block/core/storage/walstore_v3_test.go b/seaweed-block/core/storage/walstore_v3_test.go index f47d8f1596..4e0654e6d8 100644 --- a/seaweed-block/core/storage/walstore_v3_test.go +++ b/seaweed-block/core/storage/walstore_v3_test.go @@ -357,6 +357,15 @@ func TestWALStoreV3FlusherWritesTheExtentButHoldsTheCheckpointUntilTheMap(t *tes if err != nil { t.Fatal(err) } + // The simulated crash below cannot leave the automatic flusher using the + // descriptor (its reopen raced that close: "volume locked", #227); this + // test drives the flush by hand. Stop it before writes, as walstore_test. + s.DisableAutoFlushForRecoveryTest() + select { + case <-s.flusher.doneCh: + default: + t.Fatal("automatic flusher still running") + } writeSynced(t, s, 1, 3) before := s.v3.pub.current() if err := s.flusher.flushOnceInternal(false); err != nil { From a72edc364f26fddce75382cec934633d9e8b8443 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 01:23:05 -0800 Subject: [PATCH 3/6] test: #227: host lease expiry test sets up under a 3 s lease, expires under 300 ms TestWorkspaceHostLeaseExpiryAndRevokeCloseRealProvider ran every case under a 300 ms lease, so on a loaded runner the lease could expire before the write, cut or detach steps (detach-then-revoke failed #248's CI). The setup now runs under a 3 s lease; the expiry case shortens it to 300 ms, waits until one 300 ms grant was served, and only then stalls renewals. Co-Authored-By: Claude Opus 5.5 --- .../host/volume/workspace_runtime_test.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/seaweed-block/core/host/volume/workspace_runtime_test.go b/seaweed-block/core/host/volume/workspace_runtime_test.go index c26bb0e5ae..c5cd125e79 100644 --- a/seaweed-block/core/host/volume/workspace_runtime_test.go +++ b/seaweed-block/core/host/volume/workspace_runtime_test.go @@ -77,6 +77,7 @@ type runtimeLeaseServer struct { mu sync.Mutex ttl time.Duration disabled, blocked, revoking bool + shortGrants int // grants served with ttl < 1 s acks int ackGen uint64 first chan struct{} @@ -88,6 +89,9 @@ func (s *runtimeLeaseServer) RenewWorkspaceLease(ctx context.Context, id *contro s.once.Do(func() { close(s.first) }) s.mu.Lock() ttl, disabled, blocked, revoking := s.ttl, s.disabled, s.blocked, s.revoking + if !disabled && !blocked && ttl < time.Second { + s.shortGrants++ + } s.mu.Unlock() if disabled { return nil, status.Error(codes.FailedPrecondition, "workspace RPC disabled") @@ -164,7 +168,10 @@ func TestWorkspaceHostLeaseExpiryAndRevokeCloseRealProvider(t *testing.T) { for _, kind := range []string{"expiry-stalled-renewal", "detach-then-revoke", "cancel"} { t.Run(kind, func(t *testing.T) { var stopped atomic.Bool - wire := &runtimeLeaseServer{ttl: 300 * time.Millisecond, stopped: &stopped} + // A 3 s lease while the volume is set up (#227: a 300 ms lease could + // expire under load before the detach/cut steps ran); the expiry case + // switches to 300 ms below. + wire := &runtimeLeaseServer{ttl: 3 * time.Second, stopped: &stopped} h := workspaceRuntimeHost(t, runtimeWire(t, wire), true) p, remote := runtimeStore(t, h) ctx, cancel := context.WithCancel(context.Background()) @@ -213,6 +220,16 @@ func TestWorkspaceHostLeaseExpiryAndRevokeCloseRealProvider(t *testing.T) { wire.mu.Unlock() } if kind == "expiry-stalled-renewal" { + // Shorten the lease, wait (progress) until one 300 ms grant was + // served, then stall renewals: the lease expires 300 ms later. + wire.mu.Lock() + wire.ttl = 300 * time.Millisecond + wire.mu.Unlock() + runtimeAwait(t, "a 300 ms grant", func() bool { + wire.mu.Lock() + defer wire.mu.Unlock() + return wire.shortGrants > 0 + }) wire.mu.Lock() wire.blocked = true wire.mu.Unlock() From 61d1f9bc747bf47075303f50af340cc856d50f0f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 01:30:59 -0800 Subject: [PATCH 4/6] test: #253 review fold: always-admissible probe; witnessed short grant and stalled renewal - g2Refused's probe uses a fixed lineage above every test epoch, so it is always admissible (probes are refused only below the active epoch) and its answer cannot depend on what the mutation did. The lineage argument is dropped. New TestG1_ProbeHandshakeClassifiesByEffect: an accepted ship is visible after the answer, a refused lower-epoch ship never is. - Host lease expiry case: after a 300 ms grant was served and renewals were stalled, wait until a renewal arrived blocked; serial renewals make that the witness that the client accepted the 300 ms grant and is not renewed. Co-Authored-By: Claude Opus 5.5 --- .../host/volume/workspace_runtime_test.go | 16 +++++++++-- .../core/transport/g1_durable_floor_test.go | 27 ++++++++++++++++--- .../core/transport/g2_self_fence_test.go | 27 +++++++++++-------- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/seaweed-block/core/host/volume/workspace_runtime_test.go b/seaweed-block/core/host/volume/workspace_runtime_test.go index c5cd125e79..166502d0b7 100644 --- a/seaweed-block/core/host/volume/workspace_runtime_test.go +++ b/seaweed-block/core/host/volume/workspace_runtime_test.go @@ -78,6 +78,7 @@ type runtimeLeaseServer struct { ttl time.Duration disabled, blocked, revoking bool shortGrants int // grants served with ttl < 1 s + blockedCalls int // renewals that arrived while blocked acks int ackGen uint64 first chan struct{} @@ -97,6 +98,9 @@ func (s *runtimeLeaseServer) RenewWorkspaceLease(ctx context.Context, id *contro return nil, status.Error(codes.FailedPrecondition, "workspace RPC disabled") } if blocked { + s.mu.Lock() + s.blockedCalls++ + s.mu.Unlock() <-ctx.Done() return nil, ctx.Err() } @@ -220,8 +224,11 @@ func TestWorkspaceHostLeaseExpiryAndRevokeCloseRealProvider(t *testing.T) { wire.mu.Unlock() } if kind == "expiry-stalled-renewal" { - // Shorten the lease, wait (progress) until one 300 ms grant was - // served, then stall renewals: the lease expires 300 ms later. + // Shorten the lease and wait (progress) until a 300 ms grant was + // served; then stall renewals and wait until a renewal arrived + // blocked. The guard renews serially, so that blocked call was + // issued after the client accepted the reply before it, which was + // a 300 ms grant: the client holds a short lease with no renewal. wire.mu.Lock() wire.ttl = 300 * time.Millisecond wire.mu.Unlock() @@ -233,6 +240,11 @@ func TestWorkspaceHostLeaseExpiryAndRevokeCloseRealProvider(t *testing.T) { wire.mu.Lock() wire.blocked = true wire.mu.Unlock() + runtimeAwait(t, "a renewal stalled after a 300 ms grant", func() bool { + wire.mu.Lock() + defer wire.mu.Unlock() + return wire.blockedCalls > 0 + }) } if kind == "cancel" { cancel() diff --git a/seaweed-block/core/transport/g1_durable_floor_test.go b/seaweed-block/core/transport/g1_durable_floor_test.go index cabcb631ca..f3600ba360 100644 --- a/seaweed-block/core/transport/g1_durable_floor_test.go +++ b/seaweed-block/core/transport/g1_durable_floor_test.go @@ -55,11 +55,11 @@ func g1Lineage(sess, epoch uint64) RecoveryLineage { // g1Ship sends one ship on a fresh connection and reports whether it was // applied or refused (g2Refused's probe handshake: an accepted ship is applied -// before the probe is answered). +// before the probe is answered), and checks the effect agrees. func g1Ship(t *testing.T, ln *ReplicaListener, store storage.LogicalStorage, l RecoveryLineage, lsn uint64, fill byte) bool { t.Helper() want := bytes.Repeat([]byte{fill}, 4096) - refused := g2Refused(t, ln.Addr(), MsgShipEntry, EncodeShipEntry(ShipEntry{Lineage: l, LBA: 0, LSN: lsn, Data: want}), l) + refused := g2Refused(t, ln.Addr(), MsgShipEntry, EncodeShipEntry(ShipEntry{Lineage: l, LBA: 0, LSN: lsn, Data: want})) applied := bytes.Equal(g1Block(t, store), want) if refused && applied { t.Fatalf("epoch-%d ship refused yet its bytes were applied", l.Epoch) @@ -95,7 +95,7 @@ func g1Barrier(t *testing.T, ln *ReplicaListener, l RecoveryLineage) bool { func g1Rebuild(t *testing.T, ln *ReplicaListener, store storage.LogicalStorage, l RecoveryLineage, fill byte) bool { t.Helper() data := bytes.Repeat([]byte{fill}, 4096) - refused := g2Refused(t, ln.Addr(), MsgRebuildBlock, EncodeRebuildBlock(l, 0, data), l) + refused := g2Refused(t, ln.Addr(), MsgRebuildBlock, EncodeRebuildBlock(l, 0, data)) if refused && bytes.Equal(g1Block(t, store), data) { t.Fatalf("epoch-%d rebuild refused yet its bytes were written", l.Epoch) } @@ -348,6 +348,27 @@ func TestG1_SlowPersistFailureIsStillARefusal(t *testing.T) { } } +// The probe handshake classifies by effect: an accepted ship's bytes are +// visible after the probe answer, a refused ship's never are, including a +// refused ship below an accepted higher epoch (the probe stays admissible). +func TestG1_ProbeHandshakeClassifiesByEffect(t *testing.T) { + store := storage.NewBlockStore(16, 4096) + floor := g1Fresh(t) + ln := g1Listener(t, store, floor) + if err := ln.AdmitAsReplica(3); err != nil { + t.Fatal(err) + } + if !g1Ship(t, ln, store, g1Lineage(1, 3), 1, 'a') { + t.Fatal("an epoch-3 ship at the admitted epoch was classified refused") + } + if g1Ship(t, ln, store, g1Lineage(2, 2), 2, 'b') { + t.Fatal("an epoch-2 ship below the admitted epoch was classified accepted") + } + if !bytes.Equal(g1Block(t, store), bytes.Repeat([]byte{'a'}, 4096)) { + t.Fatal("the refused ship changed the block") + } +} + // The role calls persist before they return; a persist failure is returned // and nothing changes (an unassigned listener stays unassigned). func TestG1_RoleCallsPersistBeforeReturning(t *testing.T) { diff --git a/seaweed-block/core/transport/g2_self_fence_test.go b/seaweed-block/core/transport/g2_self_fence_test.go index 98ba71d108..799924132a 100644 --- a/seaweed-block/core/transport/g2_self_fence_test.go +++ b/seaweed-block/core/transport/g2_self_fence_test.go @@ -22,18 +22,23 @@ func g2Dial(t *testing.T, addr string) net.Conn { return conn } +// g2ProbeLineage is always admissible for a probe: probes are refused only +// below the active (epoch, endpointVersion), and no test reaches this epoch. +// Probes never change listener state. +var g2ProbeLineage = RecoveryLineage{SessionID: 1, Epoch: 1 << 62, EndpointVersion: 1, TargetLSN: 1} + // g2Refused sends one mutating frame and reports whether the listener closed -// the connection (refusal) rather than accepting it. Handshake, no timer: a -// probe at the same lineage follows on the same connection; the listener +// the connection (refusal) rather than accepting it. Handshake, no timer: an +// always-admissible probe follows on the same connection; the listener // handles frames in order, so it either closes the connection (refused) or // answers the probe after the frame's effect (accepted). -func g2Refused(t *testing.T, addr string, msgType byte, payload []byte, l RecoveryLineage) bool { +func g2Refused(t *testing.T, addr string, msgType byte, payload []byte) bool { t.Helper() conn := g2Dial(t, addr) if err := WriteMsg(conn, msgType, payload); err != nil { t.Fatal(err) } - if err := WriteMsg(conn, MsgProbeReq, EncodeProbeReq(ProbeRequest{Lineage: l})); err != nil { + if err := WriteMsg(conn, MsgProbeReq, EncodeProbeReq(ProbeRequest{Lineage: g2ProbeLineage})); err != nil { return true // the listener already closed the connection } replyType, _, err := ReadMsg(conn) @@ -55,13 +60,13 @@ func TestG2_PrimaryListenerRefusesMutationsButAnswersProbes(t *testing.T) { listener.FenceAsPrimary(2) newer := RecoveryLineage{SessionID: 9, Epoch: 3, EndpointVersion: 1, TargetLSN: 1} - if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(newer, 1), newer) { + if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(newer, 1)) { t.Fatal("a primary applied an inbound ship (even at a higher epoch)") } - if !g2Refused(t, listener.Addr(), MsgRecoveryLaneStart, EncodeLineage(newer), newer) { + if !g2Refused(t, listener.Addr(), MsgRecoveryLaneStart, EncodeLineage(newer)) { t.Fatal("a primary accepted a recovery lane start") } - if !g2Refused(t, listener.Addr(), MsgRebuildBlock, EncodeRebuildBlock(newer, 0, make([]byte, 4096)), newer) { + if !g2Refused(t, listener.Addr(), MsgRebuildBlock, EncodeRebuildBlock(newer, 0, make([]byte, 4096))) { t.Fatal("a primary accepted a rebuild block") } @@ -81,15 +86,15 @@ func TestG2_FloorStaysWhenThePrimaryLaterSupportsAnother(t *testing.T) { listener.FenceAsPrimary(2) listener.AdmitAsReplica(3) // superseded, now supporting the epoch-3 primary - if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 1, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}, 1), RecoveryLineage{SessionID: 1, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}) { + if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 1, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}, 1)) { t.Fatal("a ship below the floor (epoch 2 < 3) was accepted") } - if g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 2, Epoch: 3, EndpointVersion: 1, TargetLSN: 1}, 1), RecoveryLineage{SessionID: 2, Epoch: 3, EndpointVersion: 1, TargetLSN: 1}) { + if g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 2, Epoch: 3, EndpointVersion: 1, TargetLSN: 1}, 1)) { t.Fatal("the epoch-3 primary's ship was refused by its supporting replica") } listener.AdmitAsReplica(1) // a stale, lower admission never lowers the floor - if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 3, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}, 2), RecoveryLineage{SessionID: 3, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}) { + if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(RecoveryLineage{SessionID: 3, Epoch: 2, EndpointVersion: 1, TargetLSN: 1}, 2)) { t.Fatal("the floor went down after a lower admission") } } @@ -189,7 +194,7 @@ func TestG2_FenceReportsAMutationStuckPastTheBound(t *testing.T) { t.Fatal("FenceAsPrimary returned nil with a mutation still applying past the bound") } newer := RecoveryLineage{SessionID: 9, Epoch: 3, EndpointVersion: 1, TargetLSN: 1} - if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(newer, 2), newer) { + if !g2Refused(t, listener.Addr(), MsgShipEntry, g2ShipPayload(newer, 2)) { t.Fatal("after a timed-out fence the listener accepted a new ship") } } From aab451d6897e22c93fa92833a1e6b7c27bab835c Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 01:45:06 -0800 Subject: [PATCH 5/6] test: #253 review fold: witness the guard's installed 300 ms horizon The expiry case now waits until the host's own lease guard installed a new deadline less than 1 s ahead (the 3 s grants install about 3 s ahead), read under the guard's lock, before it stalls renewals; a blocked renewal then follows. A completed renewal call is no longer taken as an accepted grant. Co-Authored-By: Claude Opus 5.5 --- .../host/volume/workspace_runtime_test.go | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/seaweed-block/core/host/volume/workspace_runtime_test.go b/seaweed-block/core/host/volume/workspace_runtime_test.go index 166502d0b7..aa23922b4e 100644 --- a/seaweed-block/core/host/volume/workspace_runtime_test.go +++ b/seaweed-block/core/host/volume/workspace_runtime_test.go @@ -77,7 +77,6 @@ type runtimeLeaseServer struct { mu sync.Mutex ttl time.Duration disabled, blocked, revoking bool - shortGrants int // grants served with ttl < 1 s blockedCalls int // renewals that arrived while blocked acks int ackGen uint64 @@ -90,9 +89,6 @@ func (s *runtimeLeaseServer) RenewWorkspaceLease(ctx context.Context, id *contro s.once.Do(func() { close(s.first) }) s.mu.Lock() ttl, disabled, blocked, revoking := s.ttl, s.disabled, s.blocked, s.revoking - if !disabled && !blocked && ttl < time.Second { - s.shortGrants++ - } s.mu.Unlock() if disabled { return nil, status.Error(codes.FailedPrecondition, "workspace RPC disabled") @@ -224,18 +220,30 @@ func TestWorkspaceHostLeaseExpiryAndRevokeCloseRealProvider(t *testing.T) { wire.mu.Unlock() } if kind == "expiry-stalled-renewal" { - // Shorten the lease and wait (progress) until a 300 ms grant was - // served; then stall renewals and wait until a renewal arrived - // blocked. The guard renews serially, so that blocked call was - // issued after the client accepted the reply before it, which was - // a 300 ms grant: the client holds a short lease with no renewal. + // Shorten the lease and wait (progress) until the client guard + // installed a 300 ms horizon; then stall renewals and wait until a + // renewal arrived blocked: the client holds a short lease that is + // not renewed. + guard := h.view.workspaceGuard.Load() + if guard == nil { + t.Fatal("no workspace lease guard") + } + guard.mu.Lock() + lastUntil := guard.until // the current 3 s horizon + guard.mu.Unlock() wire.mu.Lock() wire.ttl = 300 * time.Millisecond wire.mu.Unlock() - runtimeAwait(t, "a 300 ms grant", func() bool { - wire.mu.Lock() - defer wire.mu.Unlock() - return wire.shortGrants > 0 + // Witness on the client: the host's guard installed a 300 ms + // horizon (a newly installed deadline less than 1 s ahead; the 3 s + // grants install about 3 s ahead). + runtimeAwait(t, "the guard installed a 300 ms grant", func() bool { + guard.mu.Lock() + until, fenced := guard.until, guard.fenced + guard.mu.Unlock() + installed := !fenced && !until.Equal(lastUntil) && until.Sub(time.Now()) < time.Second + lastUntil = until + return installed }) wire.mu.Lock() wire.blocked = true From b48edab8969c2a76f2123182af4805aebf8faec0 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 19:31:35 -0800 Subject: [PATCH 6/6] test: say why a probe write/read error counts as refused in g2Refused (review nit) Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/transport/g2_self_fence_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/seaweed-block/core/transport/g2_self_fence_test.go b/seaweed-block/core/transport/g2_self_fence_test.go index 799924132a..2aa4df3edd 100644 --- a/seaweed-block/core/transport/g2_self_fence_test.go +++ b/seaweed-block/core/transport/g2_self_fence_test.go @@ -31,7 +31,9 @@ var g2ProbeLineage = RecoveryLineage{SessionID: 1, Epoch: 1 << 62, EndpointVersi // the connection (refusal) rather than accepting it. Handshake, no timer: an // always-admissible probe follows on the same connection; the listener // handles frames in order, so it either closes the connection (refused) or -// answers the probe after the frame's effect (accepted). +// answers the probe after the frame's effect (accepted). A write or read error +// on the probe counts as refused: the listener is the only side that closes +// this connection, and it closes it only on a refusal. func g2Refused(t *testing.T, addr string, msgType byte, payload []byte) bool { t.Helper() conn := g2Dial(t, addr)