transport: the barrier reply carries the replica's last data epoch (48 bytes; 40 from an older replica = no table)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-24 22:26:48 -08:00
1 parent 869673d9a8
commit b8556ab5bf
4 files changed
+46 -17

No files matched your search

@@ -345,9 +345,10 @@ func TestT4b6_BarrierWire_FullLineageEcho_StillEnforced(t *testing.T) {
},
AchievedLSN: 99,
}
sample.LastDataEpoch = 7
encoded := transport.EncodeBarrierResp(sample)
if len(encoded) != 40 {
t.Fatalf("BarrierResp encoded length = %d; T4b-1 wire shape (40B = 32B lineage + 8B achievedLSN) regressed",
if len(encoded) != 48 {
t.Fatalf("BarrierResp encoded length = %d; wire shape (48B = 32B lineage + 8B achievedLSN + 8B lastDataEpoch) regressed",
len(encoded))
}
@@ -361,8 +362,16 @@ func TestT4b6_BarrierWire_FullLineageEcho_StillEnforced(t *testing.T) {
if got.Lineage != sample.Lineage {
t.Fatalf("lineage drift: got %+v want %+v", got.Lineage, sample.Lineage)
}
if got.AchievedLSN != sample.AchievedLSN {
t.Fatalf("AchievedLSN drift: got %d want %d", got.AchievedLSN, sample.AchievedLSN)
if got.AchievedLSN != sample.AchievedLSN || got.LastDataEpoch != sample.LastDataEpoch {
t.Fatalf("AchievedLSN/LastDataEpoch drift: got %d/%d want %d/%d", got.AchievedLSN, got.LastDataEpoch, sample.AchievedLSN, sample.LastDataEpoch)
}
// A 40-byte response from a replica without the epoch table decodes with
// LastDataEpoch 0 ("no table"); lengths between are malformed.
if old, err := transport.DecodeBarrierResp(encoded[:40]); err != nil || old.LastDataEpoch != 0 || old.AchievedLSN != sample.AchievedLSN {
t.Fatalf("40B response: %+v %v; want AchievedLSN kept and no table", old, err)
}
if _, err := transport.DecodeBarrierResp(encoded[:44]); err == nil {
t.Fatal("DecodeBarrierResp on a 44B payload must reject")
}
// And: a 39-byte payload must fail decode (would happen if
+25 -10
View File
@@ -17,8 +17,9 @@ import (
// Reason is populated by higher-layer aggregation (DurabilityCoordinator)
// and is empty on successful Barrier returns.
type BarrierAck struct {
Lineage RecoveryLineage
AchievedLSN uint64
Lineage RecoveryLineage
AchievedLSN uint64
LastDataEpoch uint64 // the replica's last epoch-table row; 0 = no table (see BarrierResponse)
Success bool
Reason string
}
@@ -40,29 +41,39 @@ var ErrBarrierLineageMismatch = errors.New("transport: barrier: response lineage
// on cached primary-side assumptions (architect round-21 uniform
// rule, INV-REPL-LINEAGE-BORNE-ON-BARRIER-ACK).
//
// Wire (post-T4b-1): [32B lineage][8B achievedLSN] — 40 bytes.
// Wire: [32B lineage][8B achievedLSN][8B lastDataEpoch] — 48 bytes. A
// 40-byte response (a replica from before the epoch table) decodes with
// LastDataEpoch 0, which the primary reads as "no table".
// Lineage field order inside the 32B slab is strictly
// SessionID, Epoch, EndpointVersion, frontierHint (matches
// EncodeLineage). Any deviation MUST be rejected at decode.
type BarrierResponse struct {
Lineage RecoveryLineage
AchievedLSN uint64
// LastDataEpoch is the epoch of the replica's last epoch-table row (the
// epoch of its entries at AchievedLSN); 0 = no table. An entry's
// identity is (epoch, LSN): a length alone says nothing.
LastDataEpoch uint64
}
// barrierRespSize is the exact byte length of a valid encoded
// BarrierResponse. Short, over-long, or field-misordered payloads
// are rejected by DecodeBarrierResp and MUST NOT contribute to
// correctness decisions (round-21 rule).
const barrierRespSize = 40
const barrierRespSize = 48
// barrierRespV0Size is the response of a replica from before the epoch table.
const barrierRespV0Size = 40
// EncodeBarrierResp serializes a barrier response.
// Wire: [32B lineage (SessionID, Epoch, EndpointVersion, frontierHint)]
//
// [8B achievedLSN]
// [8B achievedLSN][8B lastDataEpoch]
func EncodeBarrierResp(r BarrierResponse) []byte {
buf := make([]byte, barrierRespSize)
copy(buf[0:32], EncodeLineage(r.Lineage))
binary.BigEndian.PutUint64(buf[32:40], r.AchievedLSN)
binary.BigEndian.PutUint64(buf[40:48], r.LastDataEpoch)
return buf
}
@@ -77,9 +88,9 @@ func EncodeBarrierResp(r BarrierResponse) []byte {
// Callers MUST treat any error return as "no valid ack" and MUST NOT
// fabricate a frontier or count the ack toward durability.
func DecodeBarrierResp(buf []byte) (BarrierResponse, error) {
if len(buf) < barrierRespSize {
return BarrierResponse{}, fmt.Errorf("transport: short barrier response: %d bytes, want %d",
len(buf), barrierRespSize)
if len(buf) < barrierRespV0Size || (len(buf) > barrierRespV0Size && len(buf) < barrierRespSize) {
return BarrierResponse{}, fmt.Errorf("transport: short barrier response: %d bytes, want %d (or %d from a replica without the epoch table)",
len(buf), barrierRespSize, barrierRespV0Size)
}
lineage, err := DecodeLineage(buf[0:32])
if err != nil {
@@ -94,10 +105,14 @@ func DecodeBarrierResp(buf []byte) (BarrierResponse, error) {
return BarrierResponse{}, fmt.Errorf("transport: barrier response has zero-valued lineage field (sessionID=%d epoch=%d endpointVersion=%d frontierHint=%d)",
lineage.SessionID, lineage.Epoch, lineage.EndpointVersion, lineage.EffectiveFrontierHint())
}
return BarrierResponse{
resp := BarrierResponse{
Lineage: lineage,
AchievedLSN: binary.BigEndian.Uint64(buf[32:40]),
}, nil
}
if len(buf) >= barrierRespSize {
resp.LastDataEpoch = binary.BigEndian.Uint64(buf[40:48])
}
return resp, nil
}
// sendBarrierReq writes a MsgBarrierReq frame on conn carrying the
+5 -1
View File
@@ -84,8 +84,12 @@ func TestBarrierResp_FullLineageEcho_RoundTrip(t *testing.T) {
//
// Required test per mini-plan §2 T4b-1.
func TestBarrierResp_ShortPayload_Rejected(t *testing.T) {
// Loop covers every short length from 0 through 39 (barrierRespSize-1).
// Every length below the full response except the 40-byte response of
// a replica from before the epoch table (TestT4b6 decodes that one).
for n := 0; n < barrierRespSize; n++ {
if n == barrierRespV0Size {
continue
}
t.Run("", func(t *testing.T) {
buf := make([]byte, n)
// Populate with a valid-looking prefix so the test fails
+3 -2
View File
@@ -567,8 +567,9 @@ func (e *BlockExecutor) Barrier(replicaID string, lineage RecoveryLineage, front
return BarrierAck{
Lineage: resp.Lineage,
AchievedLSN: resp.AchievedLSN,
Success: true,
AchievedLSN: resp.AchievedLSN,
LastDataEpoch: resp.LastDataEpoch,
Success: true,
}, nil
}