From 79b3ee78d59204e7712b118fc884fabe6bf09fa2 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 21:50:32 -0800 Subject: [PATCH] test: qualify replication exchanges and admitted-operation withdrawal --- .../replication_ingress_binary_test.go | 149 +++++++- .../replication_ingress_withdrawal_test.go | 356 ++++++++++++++++++ 2 files changed, 498 insertions(+), 7 deletions(-) create mode 100644 seaweed-block/cmd/blockvolume/replication_ingress_withdrawal_test.go diff --git a/seaweed-block/cmd/blockvolume/replication_ingress_binary_test.go b/seaweed-block/cmd/blockvolume/replication_ingress_binary_test.go index d9edda999b..ab3d0422d3 100644 --- a/seaweed-block/cmd/blockvolume/replication_ingress_binary_test.go +++ b/seaweed-block/cmd/blockvolume/replication_ingress_binary_test.go @@ -3,9 +3,12 @@ package main_test import ( + "bytes" "context" + "encoding/binary" "encoding/json" "errors" + "io" "net" "net/http" "os" @@ -15,7 +18,9 @@ import ( "testing" "time" + "github.com/seaweedfs/seaweed-block/core/recovery" control "github.com/seaweedfs/seaweed-block/core/rpc/control" + "github.com/seaweedfs/seaweed-block/core/storage" "github.com/seaweedfs/seaweed-block/core/transport" "google.golang.org/grpc" ) @@ -119,16 +124,19 @@ func TestBinaryReplicationIngressFollowsAssignedSlots(t *testing.T) { } done := make(chan error, 1) go func() { done <- child.Wait() }() + exited := false t.Cleanup(func() { - child.Process.Signal(syscall.SIGTERM) - select { - case <-done: - case <-time.After(5 * time.Second): - child.Process.Kill() + if !exited { + child.Process.Signal(syscall.SIGTERM) select { case <-done: - case <-time.After(3 * time.Second): - t.Error("child reap timeout") + case <-time.After(5 * time.Second): + child.Process.Kill() + select { + case <-done: + case <-time.After(3 * time.Second): + t.Error("child reap timeout") + } } } log.Close() @@ -214,6 +222,7 @@ func TestBinaryReplicationIngressFollowsAssignedSlots(t *testing.T) { } c.Close() } + want := ingressProtocolPositive(t, data, dual, mode) excluded := fact(5, nil) excluded.ReplicaId = "r2" m.facts <- excluded @@ -225,6 +234,132 @@ func TestBinaryReplicationIngressFollowsAssignedSlots(t *testing.T) { return errors.Is(e, syscall.ECONNREFUSED) }) ingressRefused(t, dual) + // Reopen through the public recovery API only after the binary has + // fully stopped. Exact bytes must survive the wire barrier and close. + _ = child.Process.Signal(syscall.SIGTERM) + select { + case err := <-done: + exited = true + if err != nil { + t.Fatal(err) + } + case <-time.After(8 * time.Second): + t.Fatal("binary close did not settle") + } + store, err := storage.OpenWALStore(filepath.Join(root, "v1.bin")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if _, err = store.Recover(); err != nil { + t.Fatal(err) + } + for lba, payload := range want { + got, err := store.Read(lba) + if err != nil || !bytes.Equal(got, payload) { + t.Fatalf("recovered LBA%d bytes differ: %v", lba, err) + } + } + t.Logf("%s supporting protocol/barrier and exact recovered bytes PASS", mode) }) } } + +func ingressProtocolPositive(t *testing.T, data, dual, mode string) map[uint32][]byte { + t.Helper() + a := bytes.Repeat([]byte{0xA7}, 4096) + want := map[uint32][]byte{0: a, 1: make([]byte, 4096), 2: make([]byte, 4096)} + c, err := net.DialTimeout("tcp", data, time.Second) + if err != nil { + t.Fatal(err) + } + defer c.Close() + _ = c.SetDeadline(time.Now().Add(5 * time.Second)) + lineage := transport.RecoveryLineage{Epoch: 4, SessionID: 1, EndpointVersion: 4, FrontierHint: 1} + if err = transport.WriteMsg(c, transport.MsgShipEntry, transport.EncodeShipEntry(transport.ShipEntry{Lineage: lineage, LBA: 0, LSN: 1, Data: a})); err != nil { + t.Fatal(err) + } + if err = transport.WriteMsg(c, transport.MsgBarrierReq, transport.EncodeLineage(lineage)); err != nil { + t.Fatal(err) + } + kind, payload, err := transport.ReadMsg(c) + if err != nil || kind != transport.MsgBarrierResp { + t.Fatalf("legacy barrier kind=%d err=%v", kind, err) + } + ack, err := transport.DecodeBarrierResp(payload) + if err != nil || !ack.Lineage.Equivalent(lineage) || ack.AchievedLSN != 1 { + t.Fatalf("legacy barrier %+v %v", ack, err) + } + if mode != "dual-lane" { + return want + } + d, err := net.DialTimeout("tcp", dual, time.Second) + if err != nil { + t.Fatal(err) + } + defer d.Close() + _ = d.SetDeadline(time.Now().Add(5 * time.Second)) + start := make([]byte, 36) + binary.BigEndian.PutUint64(start, 2) + binary.BigEndian.PutUint64(start[8:], 1) + binary.BigEndian.PutUint64(start[16:], 2) + binary.BigEndian.PutUint32(start[24:], 64) + binary.BigEndian.PutUint64(start[28:], 4) + ingressDualWrite(t, d, 1, start) + want[1] = bytes.Repeat([]byte{0xB8}, 4096) + want[2] = bytes.Repeat([]byte{0xC9}, 4096) + for lba := uint32(0); lba < 64; lba++ { + block := make([]byte, 4096) + if lba == 0 { + copy(block, a) + } + if lba == 1 { + copy(block, want[1]) + } + p := make([]byte, 4+4096) + binary.BigEndian.PutUint32(p, lba) + copy(p[4:], block) + ingressDualWrite(t, d, 2, p) + } + ingressDualWrite(t, d, 3, nil) + if err = recovery.WriteWALEntryFrame(d, recovery.WALKindSessionLive, 2, 2, want[2]); err != nil { + t.Fatal(err) + } + ingressDualWrite(t, d, 5, nil) + for i := 0; i < 4; i++ { + var hdr [5]byte + if _, err = io.ReadFull(d, hdr[:]); err != nil { + t.Fatal(err) + } + n := binary.BigEndian.Uint32(hdr[1:]) + if n > 1024 { + t.Fatal("oversize recovery response") + } + p := make([]byte, n) + if _, err = io.ReadFull(d, p); err != nil { + t.Fatal(err) + } + if hdr[0] == 7 { + if len(p) != 20 || binary.BigEndian.Uint64(p) != 2 { + t.Fatal("invalid base batch ack") + } + continue + } + if hdr[0] != 6 || len(p) != 8 || binary.BigEndian.Uint64(p) != 2 { + t.Fatalf("invalid recovery completion %d %x", hdr[0], p) + } + return want + } + t.Fatal("no recovery completion") + return nil +} +func ingressDualWrite(t *testing.T, c net.Conn, kind byte, p []byte) { + t.Helper() + b := make([]byte, 5+len(p)) + b[0] = kind + binary.BigEndian.PutUint32(b[1:], uint32(len(p))) + copy(b[5:], p) + if _, err := c.Write(b); err != nil { + t.Fatal(err) + } +} diff --git a/seaweed-block/cmd/blockvolume/replication_ingress_withdrawal_test.go b/seaweed-block/cmd/blockvolume/replication_ingress_withdrawal_test.go new file mode 100644 index 0000000000..dc412c1342 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/replication_ingress_withdrawal_test.go @@ -0,0 +1,356 @@ +package main + +import ( + "bytes" + "encoding/binary" + "errors" + "fmt" + "io" + "log" + "net" + "strings" + "sync" + "sync/atomic" + "syscall" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/storage" + "github.com/seaweedfs/seaweed-block/core/transport" +) + +// The storage wrapper parks an actual wire-dispatched effect AFTER admission. +// It embeds the real storage implementation; no listener/gate is mocked. +type ingressParkedStore struct { + storage.LogicalStorage + operation string + entered, resume, finished chan struct{} + once, release sync.Once + active atomic.Int32 + closed atomic.Bool +} + +func (s *ingressParkedStore) effect(op string) func() { + if s.closed.Load() { + panic("receiver used released store") + } + s.active.Add(1) + parked := false + if op == s.operation { + s.once.Do(func() { parked = true; close(s.entered); <-s.resume }) + } + return func() { + s.active.Add(-1) + if parked { + close(s.finished) + } + } +} +func (s *ingressParkedStore) ApplyEntry(lba uint32, p []byte, lsn uint64) error { + done := s.effect("apply") + defer done() + return s.LogicalStorage.ApplyEntry(lba, p, lsn) +} +func (s *ingressParkedStore) WriteExtentDirect(lba uint32, p []byte) error { + done := s.effect("base") + defer done() + return s.LogicalStorage.WriteExtentDirect(lba, p) +} +func (s *ingressParkedStore) Sync() (uint64, error) { + done := s.effect("sync") + defer done() + return s.LogicalStorage.Sync() +} +func (s *ingressParkedStore) unblock() { s.release.Do(func() { close(s.resume) }) } +func (s *ingressParkedStore) Close() error { + if s.active.Load() != 0 { + return fmt.Errorf("store still has %d active effects", s.active.Load()) + } + s.closed.Store(true) + return s.LogicalStorage.Close() +} + +// The inherited dual bridge does not expose a join handle. Its caller logs +// AFTER Serve returns; observe that exact connection's exit before releasing +// our store. This is an observed test-owner join, not a production guarantee. +type ingressExitWitness struct { + io.Writer + peer string + once sync.Once + done chan struct{} +} + +func (w *ingressExitWitness) Write(p []byte) (int, error) { + n, e := w.Writer.Write(p) + if strings.Contains(string(p), "replica Serve exit remote="+w.peer+" ") { + w.once.Do(func() { close(w.done) }) + } + return n, e +} +func ingressAwait(t *testing.T, what string, c <-chan struct{}) { + t.Helper() + select { + case <-c: + case <-time.After(4 * time.Second): + t.Fatal(what) + } +} +func ingressPair(t *testing.T) (string, string) { + t.Helper() + for i := 0; i < 100; i++ { + a, e := net.Listen("tcp", "127.0.0.1:0") + if e != nil { + t.Fatal(e) + } + x := a.Addr().String() + y, e := deriveDualLaneAddr(x) + if e != nil { + a.Close() + continue + } + b, e := net.Listen("tcp", y) + a.Close() + if e == nil { + b.Close() + return x, y + } + } + t.Fatal("no available port pair") + return "", "" +} +func ingressFrame(t *testing.T, c net.Conn, k byte, p []byte) { + t.Helper() + b := make([]byte, 5+len(p)) + b[0] = k + binary.BigEndian.PutUint32(b[1:], uint32(len(p))) + copy(b[5:], p) + if _, e := c.Write(b); e != nil { + t.Fatal(e) + } +} +func ingressStart(t *testing.T, c net.Conn) { + t.Helper() + p := make([]byte, 36) + binary.BigEndian.PutUint64(p, 1) + binary.BigEndian.PutUint64(p[8:], 1) + binary.BigEndian.PutUint64(p[16:], 1) + binary.BigEndian.PutUint32(p[24:], 4) + binary.BigEndian.PutUint64(p[28:], 1) + ingressFrame(t, c, 1, p) +} +func ingressBase(t *testing.T, c net.Conn, p []byte) { + t.Helper() + b := make([]byte, 4+len(p)) + copy(b[4:], p) + ingressFrame(t, c, 2, b) +} +func ingressWireClosed(t *testing.T, c net.Conn) { + t.Helper() + _ = c.SetReadDeadline(time.Now().Add(2 * time.Second)) + var b [1]byte + _, err := c.Read(b[:]) + if err == nil { + t.Fatal("unexpected successful wire outcome after withdrawal") + } + if n, ok := err.(net.Error); ok && n.Timeout() { + t.Fatal("wire did not settle") + } + t.Logf("explicit wire failure after withdrawal: %v", err) +} +func ingressClosedPort(t *testing.T, addr string) { + t.Helper() + c, e := net.DialTimeout("tcp", addr, time.Second) + if c != nil { + c.Close() + } + if !errors.Is(e, syscall.ECONNREFUSED) { + t.Fatalf("withdrawn endpoint %s accepted connection: %v", addr, e) + } +} + +func TestReplicationIngressWithdrawalDrainsAdmittedEffects(t *testing.T) { + for _, lane := range []string{"legacy", "dual"} { + t.Run(lane, func(t *testing.T) { + op := "apply" + if lane == "dual" { + op = "base" + } + s := &ingressParkedStore{LogicalStorage: storage.NewBlockStore(4, 4096), operation: op, entered: make(chan struct{}), resume: make(chan struct{}), finished: make(chan struct{})} + addr, dual := ingressPair(t) + r := &replicationIngress{addr: addr, store: s, dualLane: true} + if err := r.AdmitAsReplica(1); err != nil { + t.Fatal(err) + } + target := addr + if lane == "dual" { + target = dual + } + c, err := net.DialTimeout("tcp", target, time.Second) + if err != nil { + t.Fatal(err) + } + _ = c.SetDeadline(time.Now().Add(8 * time.Second)) + witness := &ingressExitWitness{Writer: log.Writer(), peer: c.LocalAddr().String(), done: make(chan struct{})} + if lane == "dual" { + log.SetOutput(witness) + defer log.SetOutput(witness.Writer) + } + defer func() { + s.unblock() + c.Close() + r.Stop() + if !s.closed.Load() { + _ = s.Close() + } + }() + payload := bytes.Repeat([]byte{0xD3}, 4096) + listener := r.listener + if lane == "legacy" { + l := transport.RecoveryLineage{Epoch: 1, SessionID: 1, EndpointVersion: 1, FrontierHint: 1} + if err = transport.WriteMsg(c, transport.MsgShipEntry, transport.EncodeShipEntry(transport.ShipEntry{Lineage: l, LBA: 0, LSN: 1, Data: payload})); err != nil { + t.Fatal(err) + } + } else { + ingressStart(t, c) + ingressBase(t, c, payload) + } + ingressAwait(t, "storage operation was not admitted", s.entered) + // Queue a second mutation on the already-open connection. It must + // never reach storage after the first admitted effect drains. + later := bytes.Repeat([]byte{0xF5}, 4096) + if lane == "legacy" { + l := transport.RecoveryLineage{Epoch: 1, SessionID: 1, EndpointVersion: 1, FrontierHint: 1} + if err = transport.WriteMsg(c, transport.MsgShipEntry, transport.EncodeShipEntry(transport.ShipEntry{Lineage: l, LBA: 1, LSN: 2, Data: later})); err != nil { + t.Fatal(err) + } + } else { + p := make([]byte, 4+len(later)) + binary.BigEndian.PutUint32(p, 1) + copy(p[4:], later) + ingressFrame(t, c, 2, p) + } + + withdrawn := make(chan error, 1) + go func() { withdrawn <- r.FenceAsPrimaryWithPeers(2, false) }() + // Public gate refusal is the fence-arrival witness. This cannot be a + // scheduler-only negative: the new role is installed while our effect lives. + end := time.Now().Add(3 * time.Second) + for { + release, err := listener.EnterDualLane(1) + if err != nil { + break + } + release() + if time.Now().After(end) { + t.Fatal("withdrawal never closed admission") + } + time.Sleep(time.Millisecond) + } + select { + case e := <-withdrawn: + t.Fatalf("withdrawal completed before effect release: %v", e) + default: + } + if s.active.Load() != 1 { + t.Fatal("effect no longer parked") + } + s.unblock() + ingressAwait(t, "effect did not return", s.finished) + select { + case err = <-withdrawn: + if err != nil { + t.Fatal(err) + } + case <-time.After(4 * time.Second): + t.Fatal("withdrawal did not settle") + } + ingressWireClosed(t, c) + if lane == "dual" { + ingressAwait(t, "dual receiver did not exit after cancellation", witness.done) + } + ingressClosedPort(t, addr) + ingressClosedPort(t, dual) + if release, e := listener.EnterDualLane(1); e == nil { + release() + t.Fatal("old gate accepted another frame") + } + got, err := s.Read(0) + if err != nil || !bytes.Equal(got, payload) { + t.Fatal("admitted bytes differ", err) + } + untouched, e := s.Read(1) + if e != nil || !bytes.Equal(untouched, make([]byte, 4096)) { + t.Fatal("queued post-withdrawal mutation reached storage", e) + } + if err = s.Close(); err != nil { + t.Fatal(err) + } + t.Log("admitted exact effect drained; wire failed explicitly; worker settled before store release") + }) + } +} + +// This bounded reproducer records the inherited distinction: sendAck's Sync +// is outside the counted frame. The test owner must await the receiver exit; +// ingress withdrawal alone is NOT proof that this Sync has stopped. +func TestReplicationIngressDualAckSyncNeedsOwnerSettlement(t *testing.T) { + s := &ingressParkedStore{LogicalStorage: storage.NewBlockStore(4, 4096), operation: "sync", entered: make(chan struct{}), resume: make(chan struct{}), finished: make(chan struct{})} + addr, dual := ingressPair(t) + r := &replicationIngress{addr: addr, store: s, dualLane: true} + if err := r.AdmitAsReplica(1); err != nil { + t.Fatal(err) + } + c, err := net.DialTimeout("tcp", dual, time.Second) + if err != nil { + t.Fatal(err) + } + _ = c.SetDeadline(time.Now().Add(8 * time.Second)) + witness := &ingressExitWitness{Writer: log.Writer(), peer: c.LocalAddr().String(), done: make(chan struct{})} + log.SetOutput(witness) + defer log.SetOutput(witness.Writer) + defer func() { + s.unblock() + c.Close() + r.Stop() + if !s.closed.Load() { + _ = s.Close() + } + }() + payload := bytes.Repeat([]byte{0xE4}, 4096) + ingressStart(t, c) + ingressBase(t, c, payload) + ingressFrame(t, c, 3, nil) + ingressAwait(t, "uncounted acknowledgement Sync did not start", s.entered) + withdrawn := make(chan error, 1) + go func() { withdrawn <- r.FenceAsPrimaryWithPeers(2, false) }() + select { + case err = <-withdrawn: + if err != nil { + t.Fatal(err) + } + case <-time.After(3 * time.Second): + t.Fatal("inherited uncounted Sync became a counted drain") + } + if s.active.Load() != 1 { + t.Fatal("no outstanding Sync at withdrawal return") + } + select { + case <-witness.done: + t.Fatal("receiver exited while Sync held") + default: + } + ingressWireClosed(t, c) + // Explicit owner disposition: release the parked Sync and wait for the + // caller's actual Serve-exit witness before releasing the store. + s.unblock() + ingressAwait(t, "Sync did not return", s.finished) + ingressAwait(t, "receiver did not settle", witness.done) + got, err := s.Read(0) + if err != nil || !bytes.Equal(got, payload) { + t.Fatal("base bytes differ", err) + } + if err = s.Close(); err != nil { + t.Fatal(err) + } + t.Log("INHERITED LIMIT reproduced: withdrawal returned with sendAck Sync live; test owner waited for receiver exit before store release") +}