mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
T4a-1: bump transport envelope to 12B preamble
V3 clean-break replication envelope now carries magic + version + flags/reserved in every frame. Replicas refuse foreign traffic at the first byte; unknown protocol versions are rejected explicitly. Wire layout: [4B magic "SWRP"][1B version=1][1B type][1B flags=0][1B reserved=0] [4B length][payload] All existing message encode/decode bodies are unchanged — only the preamble extends. MsgShipEntry / MsgProbeResp / MsgRebuildBlock / MsgBarrierResp frames all ride the new envelope transparently. Tests: EnvelopeRoundTrip (every message type), MagicRejected_WrongMagic, VersionRejected_UnknownVersion. Existing core/transport/*_test.go continues to pass unchanged (verified). Part of Phase 15 T4a per v3-phase-15-t4a-mini-plan.md §2 T4a-1. Gate G-3 (pre-existing transport tests stay green) satisfied.
This commit is contained in:
1 parent
7ee616f94a
commit
2b54fef3f7
2 files changed
+194
-9
No files matched your search
@@ -1,8 +1,13 @@
|
||||
// Package transport provides minimal TCP block replication for the sparrow.
|
||||
// Three operations: Ship (WAL entry), Probe (handshake with R/S/H), Rebuild (full base copy).
|
||||
//
|
||||
// Wire format is intentionally simple — no framing library needed.
|
||||
// Each message: [1B type][4B length][payload...]
|
||||
// Wire format — V3 clean-break replication envelope (12 bytes):
|
||||
//
|
||||
// [4B magic "SWRP"][1B version][1B type][1B flags][1B reserved][4B length][payload]
|
||||
//
|
||||
// Magic + version let replicas refuse foreign / incompatible traffic at the
|
||||
// first byte. Flags and reserved are zero in V1 and carved out for future
|
||||
// protocol extensions without reshuffling the preamble.
|
||||
package transport
|
||||
|
||||
import (
|
||||
@@ -12,6 +17,22 @@ import (
|
||||
"net"
|
||||
)
|
||||
|
||||
// Envelope preamble (V3 T4a-1 clean-break layout).
|
||||
//
|
||||
// MagicV3Repl is ASCII "SWRP" (SeaWeed Replication Protocol). Present on every
|
||||
// frame. Replicas reject frames whose first 4 bytes do not match.
|
||||
//
|
||||
// ProtocolVersion = 1 is V1 of the V3 replication envelope. Unknown versions
|
||||
// are rejected — there is no silent fallback. Future versions may change
|
||||
// flags/reserved semantics or payload encoding.
|
||||
//
|
||||
// preambleLen is the fixed preamble byte count (4+1+1+1+1+4 = 12).
|
||||
const (
|
||||
MagicV3Repl uint32 = 0x53575250 // "SWRP"
|
||||
ProtocolVersion byte = 1
|
||||
preambleLen = 12
|
||||
)
|
||||
|
||||
// Message types
|
||||
const (
|
||||
MsgShipEntry byte = 0x01 // primary → replica: one block write
|
||||
@@ -53,10 +74,18 @@ type ProbeResponse struct {
|
||||
// --- Wire helpers ---
|
||||
|
||||
// WriteMsg sends a typed message on the connection.
|
||||
//
|
||||
// Wire preamble (12 bytes): [4B magic "SWRP"][1B version][1B type]
|
||||
// [1B flags=0][1B reserved=0][4B length][payload...].
|
||||
// Flags and reserved are always 0 in V1; future versions may define bits.
|
||||
func WriteMsg(conn net.Conn, msgType byte, payload []byte) error {
|
||||
header := make([]byte, 5)
|
||||
header[0] = msgType
|
||||
binary.BigEndian.PutUint32(header[1:5], uint32(len(payload)))
|
||||
header := make([]byte, preambleLen)
|
||||
binary.BigEndian.PutUint32(header[0:4], MagicV3Repl)
|
||||
header[4] = ProtocolVersion
|
||||
header[5] = msgType
|
||||
header[6] = 0 // flags
|
||||
header[7] = 0 // reserved
|
||||
binary.BigEndian.PutUint32(header[8:12], uint32(len(payload)))
|
||||
if _, err := conn.Write(header); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -72,14 +101,26 @@ func WriteMsg(conn net.Conn, msgType byte, payload []byte) error {
|
||||
// Prevents memory exhaustion from malicious or buggy senders.
|
||||
const MaxPayloadSize = 16 * 1024 * 1024 // 16MB
|
||||
|
||||
// ReadMsg reads a typed message from the connection.
|
||||
// ReadMsg reads a typed message from the connection. Validates magic + version
|
||||
// before accepting the frame; mismatches are rejected with a descriptive error
|
||||
// so the caller can close the conn rather than attempt to recover.
|
||||
func ReadMsg(conn net.Conn) (msgType byte, payload []byte, err error) {
|
||||
header := make([]byte, 5)
|
||||
header := make([]byte, preambleLen)
|
||||
if _, err := io.ReadFull(conn, header); err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
msgType = header[0]
|
||||
length := binary.BigEndian.Uint32(header[1:5])
|
||||
magic := binary.BigEndian.Uint32(header[0:4])
|
||||
if magic != MagicV3Repl {
|
||||
return 0, nil, fmt.Errorf("transport: bad magic: 0x%08x (want 0x%08x)", magic, MagicV3Repl)
|
||||
}
|
||||
version := header[4]
|
||||
if version != ProtocolVersion {
|
||||
return 0, nil, fmt.Errorf("transport: unsupported version: %d (want %d)", version, ProtocolVersion)
|
||||
}
|
||||
msgType = header[5]
|
||||
// header[6] (flags) and header[7] (reserved) are ignored in V1; reserved
|
||||
// for forward-compatible extension.
|
||||
length := binary.BigEndian.Uint32(header[8:12])
|
||||
if length > MaxPayloadSize {
|
||||
return 0, nil, fmt.Errorf("transport: payload too large: %d > %d", length, MaxPayloadSize)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// pipeConn returns a connected pair of in-memory TCP conns for preamble tests.
|
||||
func pipeConn(t *testing.T) (client, server net.Conn) {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln.Close()
|
||||
|
||||
done := make(chan net.Conn, 1)
|
||||
go func() {
|
||||
c, err := ln.Accept()
|
||||
if err != nil {
|
||||
done <- nil
|
||||
return
|
||||
}
|
||||
done <- c
|
||||
}()
|
||||
|
||||
client, err = net.Dial("tcp", ln.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case server = <-done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("accept timed out")
|
||||
}
|
||||
if server == nil {
|
||||
t.Fatal("listener accept failed")
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
client.Close()
|
||||
server.Close()
|
||||
})
|
||||
return client, server
|
||||
}
|
||||
|
||||
// TestProtocol_EnvelopeRoundTrip writes one frame of every defined message
|
||||
// type and verifies the reader recovers the same type + payload bytes.
|
||||
func TestProtocol_EnvelopeRoundTrip(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
msgType byte
|
||||
payload []byte
|
||||
}{
|
||||
{"MsgShipEntry", MsgShipEntry, EncodeShipEntry(ShipEntry{
|
||||
Lineage: RecoveryLineage{SessionID: 1, Epoch: 1, EndpointVersion: 1, TargetLSN: 10},
|
||||
LBA: 7,
|
||||
LSN: 10,
|
||||
Data: bytes.Repeat([]byte{0xAB}, 4096),
|
||||
})},
|
||||
{"MsgProbeReq", MsgProbeReq, nil},
|
||||
{"MsgProbeResp", MsgProbeResp, EncodeProbeResp(ProbeResponse{
|
||||
SyncedLSN: 100, WalTail: 1, WalHead: 100,
|
||||
})},
|
||||
{"MsgRebuildBlock", MsgRebuildBlock, EncodeRebuildBlock(
|
||||
RecoveryLineage{SessionID: 1, Epoch: 1, EndpointVersion: 1, TargetLSN: 5},
|
||||
3, bytes.Repeat([]byte{0xCD}, 4096))},
|
||||
{"MsgRebuildDone", MsgRebuildDone, nil},
|
||||
{"MsgBarrierReq", MsgBarrierReq, nil},
|
||||
{"MsgBarrierResp", MsgBarrierResp, make([]byte, 8)},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c, s := pipeConn(t)
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- WriteMsg(c, tc.msgType, tc.payload) }()
|
||||
|
||||
gotType, gotPayload, err := ReadMsg(s)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadMsg: %v", err)
|
||||
}
|
||||
if err := <-errCh; err != nil {
|
||||
t.Fatalf("WriteMsg: %v", err)
|
||||
}
|
||||
if gotType != tc.msgType {
|
||||
t.Fatalf("type: got 0x%02x want 0x%02x", gotType, tc.msgType)
|
||||
}
|
||||
if !bytes.Equal(gotPayload, tc.payload) {
|
||||
t.Fatalf("payload mismatch")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestProtocol_MagicRejected_WrongMagic fabricates a frame with a bogus magic
|
||||
// and expects ReadMsg to fail-closed.
|
||||
func TestProtocol_MagicRejected_WrongMagic(t *testing.T) {
|
||||
c, s := pipeConn(t)
|
||||
|
||||
// Construct a valid-looking preamble except for the magic.
|
||||
hdr := make([]byte, preambleLen)
|
||||
binary.BigEndian.PutUint32(hdr[0:4], 0xDEADBEEF)
|
||||
hdr[4] = ProtocolVersion
|
||||
hdr[5] = MsgShipEntry
|
||||
// length=0
|
||||
go func() {
|
||||
_, _ = c.Write(hdr)
|
||||
}()
|
||||
|
||||
_, _, err := ReadMsg(s)
|
||||
if err == nil {
|
||||
t.Fatal("expected error on bad magic, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "bad magic") {
|
||||
t.Fatalf("expected 'bad magic' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestProtocol_VersionRejected_UnknownVersion fabricates a frame with a
|
||||
// known magic but unsupported version byte; reader must reject.
|
||||
func TestProtocol_VersionRejected_UnknownVersion(t *testing.T) {
|
||||
c, s := pipeConn(t)
|
||||
|
||||
hdr := make([]byte, preambleLen)
|
||||
binary.BigEndian.PutUint32(hdr[0:4], MagicV3Repl)
|
||||
hdr[4] = 99 // future / unsupported
|
||||
hdr[5] = MsgShipEntry
|
||||
go func() {
|
||||
_, _ = c.Write(hdr)
|
||||
}()
|
||||
|
||||
_, _, err := ReadMsg(s)
|
||||
if err == nil {
|
||||
t.Fatal("expected error on unknown version, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unsupported version") {
|
||||
t.Fatalf("expected 'unsupported version' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user