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 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-19 01:09:19 -08:00
1 parent f353f605c1
commit c88f028cf6
2 files changed
+56 -30

No files matched your search

@@ -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) {
@@ -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")
}
}