diff --git a/seaweed-block/core/transport/epoch_table.go b/seaweed-block/core/transport/epoch_table.go index ea9388d736..b8b3e834e5 100644 --- a/seaweed-block/core/transport/epoch_table.go +++ b/seaweed-block/core/transport/epoch_table.go @@ -106,6 +106,9 @@ func checkEpochRows(rows []EpochRow) error { // the epoch of the last row is a no-op; a lower epoch is refused (no write). // Call it before the entry at firstLSN is acknowledged. func (t *EpochTable) Note(epoch, firstLSN uint64) error { + if t == nil { + return nil // no table: nothing recorded (a store from before the table) + } if epoch == 0 || firstLSN == 0 { return errors.New("epoch table: zero epoch or LSN") } @@ -165,6 +168,10 @@ func (t *EpochTable) persistLocked(rows []EpochRow) error { return nil } +// SetEpochTable installs the volume's epoch table on the listener. Call +// before Serve. +func (r *ReplicaListener) SetEpochTable(t *EpochTable) { r.epochs = t } + // LastEpoch is the epoch of the last row; 0 = no rows. func (t *EpochTable) LastEpoch() uint64 { t.mu.Lock() @@ -175,6 +182,24 @@ func (t *EpochTable) LastEpoch() uint64 { return t.rows[len(t.rows)-1].Epoch } +// LastDataEpochAt is the epoch of the last row whose first LSN is at or +// below end (the durable end). A row is made durable BEFORE the first entry +// of its epoch, so a crash between the two leaves a row with no entry; it is +// not counted here. 0 = no such row. +func (t *EpochTable) LastDataEpochAt(end uint64) uint64 { + if t == nil { + return 0 + } + t.mu.Lock() + defer t.mu.Unlock() + for i := len(t.rows) - 1; i >= 0; i-- { + if t.rows[i].FirstLSN <= end { + return t.rows[i].Epoch + } + } + return 0 +} + // Rows is a copy of the table. func (t *EpochTable) Rows() []EpochRow { t.mu.Lock() diff --git a/seaweed-block/core/transport/epoch_table_test.go b/seaweed-block/core/transport/epoch_table_test.go index 754f8d9e68..f1c63b3680 100644 --- a/seaweed-block/core/transport/epoch_table_test.go +++ b/seaweed-block/core/transport/epoch_table_test.go @@ -74,6 +74,19 @@ func TestEpochTable_InterruptEveryPersistStep(t *testing.T) { } } +// A row made durable before its first entry, with a crash between: the row +// is not the last data epoch until the durable end reaches its first LSN. +func TestEpochTable_LastDataEpochAtTheDurableEnd(t *testing.T) { + tb, _ := LoadEpochTable("", "v1") + _ = tb.Note(1, 1) + _ = tb.Note(2, 50) + for _, c := range []struct{ end, want uint64 }{{0, 0}, {1, 1}, {49, 1}, {50, 2}, {80, 2}} { + if got := tb.LastDataEpochAt(c.end); got != c.want { + t.Errorf("LastDataEpochAt(%d) = %d; want %d", c.end, got, c.want) + } + } +} + // SharedEnd's outcomes, one per guard. func TestEpochTable_SharedEnd(t *testing.T) { tb, _ := LoadEpochTable("", "v1") diff --git a/seaweed-block/core/transport/replica.go b/seaweed-block/core/transport/replica.go index b413222c7e..796f939792 100644 --- a/seaweed-block/core/transport/replica.go +++ b/seaweed-block/core/transport/replica.go @@ -68,6 +68,10 @@ type ReplicaListener struct { stopCh chan struct{} wg sync.WaitGroup applyHook ApplyHook // optional gate plug-in; nil = direct apply + // epochs is the volume's epoch table (nil = none): a shipped entry's + // epoch is noted durably before the entry is applied, and the barrier + // reply names the last data epoch at the durable end. + epochs *EpochTable mu sync.Mutex activeLineage RecoveryLineage @@ -423,6 +427,12 @@ func (r *ReplicaListener) handleConn(conn net.Conn) { entry.Lineage.SessionID, entry.Lineage.Epoch, entry.Lineage.EndpointVersion) return true } + // The row first, then the entry: an entry whose epoch the + // table cannot name is never applied. + if err := r.epochs.Note(entry.Lineage.Epoch, entry.LSN); err != nil { + log.Printf("replica: epoch table lsn=%d epoch=%d: %v", entry.LSN, entry.Lineage.Epoch, err) + return true + } // If the apply-gate is installed, route through it. // Caller (this handler) decides lane; gate is lane-pure. // Live-lane stale entries return error here → log + @@ -543,8 +553,9 @@ func (r *ReplicaListener) handleConn(conn net.Conn) { // Echo the request's full lineage in the rebuild-done ack // so primary-side validators can consume it. resp := EncodeBarrierResp(BarrierResponse{ - Lineage: lineage, - AchievedLSN: frontier, + Lineage: lineage, + AchievedLSN: frontier, + LastDataEpoch: r.epochs.LastDataEpochAt(frontier), }) replyType, reply = MsgBarrierResp, resp return true @@ -572,8 +583,9 @@ func (r *ReplicaListener) handleConn(conn net.Conn) { // tuple against the session it is awaiting before // counting the ack toward quorum. resp := EncodeBarrierResp(BarrierResponse{ - Lineage: lineage, - AchievedLSN: frontier, + Lineage: lineage, + AchievedLSN: frontier, + LastDataEpoch: r.epochs.LastDataEpochAt(frontier), }) replyType, reply = MsgBarrierResp, resp