mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
recovery: seal dual-lane writes before barrier
This commit is contained in:
1 parent
4de435c607
commit
8682e15e31
8 files changed
+351
-22
No files matched your search
@@ -34,12 +34,10 @@ type barrierEligibilityProbe interface {
|
||||
ProbeBarrierEligibility() (debtZero, liveTail, walLegOk bool, cursor, head uint64)
|
||||
}
|
||||
|
||||
// preBarrierSealer is implemented only by the bridging sink. It lets
|
||||
// Sender close the live-write acceptance window at the single point
|
||||
// where both base and WAL backlog have completed, immediately before
|
||||
// BarrierReq. Production RecoverySink does not implement this; its
|
||||
// resident WalShipper remains the live feeder through the barrier
|
||||
// window until EndSession.
|
||||
// preBarrierSealer lets Sender close the live-write acceptance window
|
||||
// at the single point where both base and WAL backlog have completed,
|
||||
// immediately before BarrierReq. Both the bridging sink and the
|
||||
// transport RecoverySink implement this atomic cut.
|
||||
type preBarrierSealer interface {
|
||||
SealBeforeBarrier() error
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ package transport
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -55,6 +56,34 @@ import (
|
||||
"github.com/seaweedfs/seaweed-block/core/storage/memorywal"
|
||||
)
|
||||
|
||||
type pillar3BlockFirstReadStore struct {
|
||||
storage.LogicalStorage
|
||||
entered chan struct{}
|
||||
release chan struct{}
|
||||
enterOnce sync.Once
|
||||
releaseOnce sync.Once
|
||||
}
|
||||
|
||||
func newPillar3BlockFirstReadStore(inner storage.LogicalStorage) *pillar3BlockFirstReadStore {
|
||||
return &pillar3BlockFirstReadStore{
|
||||
LogicalStorage: inner,
|
||||
entered: make(chan struct{}),
|
||||
release: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *pillar3BlockFirstReadStore) Read(lba uint32) ([]byte, error) {
|
||||
s.enterOnce.Do(func() {
|
||||
close(s.entered)
|
||||
<-s.release
|
||||
})
|
||||
return s.LogicalStorage.Read(lba)
|
||||
}
|
||||
|
||||
func (s *pillar3BlockFirstReadStore) unblock() {
|
||||
s.releaseOnce.Do(func() { close(s.release) })
|
||||
}
|
||||
|
||||
// TestPillar3Slice1_ReceiverConvergence_LiveOverwritesBacklog_SameLBAs —
|
||||
// the architect-suggested "pattern A then B" check, generalised to
|
||||
// multiple LBAs to give the assertion bite.
|
||||
@@ -99,6 +128,8 @@ func TestPillar3Slice1_ReceiverConvergence_LiveOverwritesBacklog_SameLBAs(t *tes
|
||||
overwriteLBAs := []uint32{3, 5, 7}
|
||||
|
||||
primary := memorywal.NewStore(numBlocks, blockSize)
|
||||
recoveryStore := newPillar3BlockFirstReadStore(primary)
|
||||
defer recoveryStore.unblock()
|
||||
replica := storage.NewBlockStore(numBlocks, blockSize)
|
||||
|
||||
// Seed: A_i pattern keyed by LBA. byte 0 = 0x40 | lba so we can
|
||||
@@ -126,7 +157,7 @@ func TestPillar3Slice1_ReceiverConvergence_LiveOverwritesBacklog_SameLBAs(t *tes
|
||||
coord := recovery.NewPeerShipCoordinator()
|
||||
const replicaID = "r1"
|
||||
exec := NewBlockExecutorWithDualLane(
|
||||
primary, "127.0.0.1:0", dualLaneAddr, coord,
|
||||
recoveryStore, "127.0.0.1:0", dualLaneAddr, coord,
|
||||
recovery.ReplicaID(replicaID),
|
||||
)
|
||||
|
||||
@@ -151,6 +182,11 @@ func TestPillar3Slice1_ReceiverConvergence_LiveOverwritesBacklog_SameLBAs(t *tes
|
||||
// will ship it as WALKindSessionLive on the wire.
|
||||
bridge := exec.dualLane.Bridge
|
||||
waitDualLaneLiveReady(t, bridge, recovery.ReplicaID(replicaID))
|
||||
select {
|
||||
case <-recoveryStore.entered:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("base lane did not enter blocked Read")
|
||||
}
|
||||
bRefs := make(map[uint32][]byte, len(overwriteLBAs))
|
||||
for _, lba := range overwriteLBAs {
|
||||
bData := make([]byte, blockSize)
|
||||
@@ -168,6 +204,7 @@ func TestPillar3Slice1_ReceiverConvergence_LiveOverwritesBacklog_SameLBAs(t *tes
|
||||
}
|
||||
bRefs[lba] = bData
|
||||
}
|
||||
recoveryStore.unblock()
|
||||
|
||||
var res adapter.SessionCloseResult
|
||||
select {
|
||||
|
||||
@@ -56,17 +56,20 @@ type RecoverySink struct {
|
||||
steadyLineage RecoveryLineage
|
||||
steadyProfile EmitProfile
|
||||
|
||||
// sealMu protects sealed and serializes NotifyAppend's check-
|
||||
// then-delegate against EndSession. After EndSession sets
|
||||
// sealed=true, NotifyAppend returns ErrSinkSealed instead of
|
||||
// delegating to WalShipper — matches senderBacklogSink's
|
||||
// post-flushAndSeal contract so callers (PrimaryBridge.PushLiveWrite)
|
||||
// get a clean "session is closing" signal during the brief
|
||||
// window between sink-end and bridge-map-removal.
|
||||
// sealMu serializes NotifyAppend against the pre-barrier cut and
|
||||
// EndSession. ended distinguishes writes retained while the
|
||||
// barrier is in flight from writes that may use restored steady
|
||||
// context after EndSession.
|
||||
sealMu sync.Mutex
|
||||
sealed bool
|
||||
ended bool
|
||||
}
|
||||
|
||||
// ErrSinkBarrierSealed is returned after the recovery sender has made
|
||||
// its atomic pre-barrier cut but before EndSession restores the steady
|
||||
// emit context. The committed WAL entry must be retained for replay.
|
||||
var ErrSinkBarrierSealed = errors.New("recovery: NotifyAppend after pre-barrier seal")
|
||||
|
||||
// ErrSinkSealed is returned by RecoverySink.NotifyAppend after the
|
||||
// session's EndSession has fired. Callers (PushLiveWrite) treat this
|
||||
// as "session is closing — fall back to steady-live or retry on a
|
||||
@@ -170,17 +173,34 @@ func (r *RecoverySink) DrainBacklog(ctx context.Context) error {
|
||||
// emit triggered by the EndSession path completes under session
|
||||
// context before the restore lands.
|
||||
func (r *RecoverySink) EndSession() {
|
||||
// Set sealed FIRST so any in-flight NotifyAppend that races
|
||||
// past the sealMu check sees the sealed=true gate before the
|
||||
// WalShipper transitions and emit context is restored. This is
|
||||
// the analogue of senderBacklogSink.flushAndSeal's atomic seal
|
||||
// boundary.
|
||||
r.sealMu.Lock()
|
||||
defer r.sealMu.Unlock()
|
||||
if r.ended {
|
||||
return
|
||||
}
|
||||
r.sealed = true
|
||||
|
||||
r.e.WalShipperFor(r.replicaID).EndSession()
|
||||
r.e.updateWalShipperEmitContext(r.replicaID, r.steadyConn, r.steadyLineage, r.steadyProfile)
|
||||
r.e.WalShipperFor(r.replicaID).endSessionWithHandoff(func() {
|
||||
r.e.updateWalShipperEmitContext(r.replicaID, r.steadyConn, r.steadyLineage, r.steadyProfile)
|
||||
})
|
||||
r.ended = true
|
||||
}
|
||||
|
||||
// SealBeforeBarrier establishes the recovery cut. NotifyAppend holds
|
||||
// the same lock through its WalShipper call, so every accepted write
|
||||
// completes before this method returns and every later write is
|
||||
// retained until a future recovery session.
|
||||
func (r *RecoverySink) SealBeforeBarrier() error {
|
||||
r.sealMu.Lock()
|
||||
defer r.sealMu.Unlock()
|
||||
if r.ended {
|
||||
return ErrSinkSealed
|
||||
}
|
||||
if err := r.e.WalShipperFor(r.replicaID).SealSession(); err != nil {
|
||||
return err
|
||||
}
|
||||
r.sealed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// NotifyAppend delegates to WalShipper.NotifyAppend during the
|
||||
@@ -199,9 +219,16 @@ func (r *RecoverySink) NotifyAppend(lba uint32, lsn uint64, data []byte) error {
|
||||
r.sealMu.Lock()
|
||||
defer r.sealMu.Unlock()
|
||||
if r.sealed {
|
||||
if !r.ended {
|
||||
return ErrSinkBarrierSealed
|
||||
}
|
||||
return ErrSinkSealed
|
||||
}
|
||||
return r.e.WalShipperFor(r.replicaID).NotifyAppend(lba, lsn, data)
|
||||
err := r.e.WalShipperFor(r.replicaID).NotifyAppend(lba, lsn, data)
|
||||
if errors.Is(err, ErrWalShipperSessionSealed) {
|
||||
return ErrSinkBarrierSealed
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteMu returns the per-replica entry's writeMu so recovery.Sender
|
||||
|
||||
@@ -521,6 +521,52 @@ func TestRecoverySink_NotifyAppendAfterEndSession_Sealed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoverySink_SealBeforeBarrier_RetainsUntilEndSession(t *testing.T) {
|
||||
primary := memorywal.NewStore(8, 4096)
|
||||
e := NewBlockExecutor(primary, "127.0.0.1:0")
|
||||
const replicaID = "r1"
|
||||
|
||||
steadyConn, _ := net.Pipe()
|
||||
defer steadyConn.Close()
|
||||
sessionConn, _ := net.Pipe()
|
||||
defer sessionConn.Close()
|
||||
|
||||
steadyLineage := RecoveryLineage{SessionID: 7, Epoch: 1, EndpointVersion: 1, TargetLSN: 1}
|
||||
sessionLineage := RecoveryLineage{SessionID: 42, Epoch: 1, EndpointVersion: 1, TargetLSN: 200}
|
||||
e.updateWalShipperEmitContext(replicaID, steadyConn, steadyLineage, EmitProfileSteadyMsgShip)
|
||||
|
||||
sink := NewRecoverySink(e, replicaID,
|
||||
sessionConn, sessionLineage,
|
||||
steadyConn, steadyLineage,
|
||||
)
|
||||
if err := sink.StartSession(0); err != nil {
|
||||
t.Fatalf("StartSession: %v", err)
|
||||
}
|
||||
if err := sink.DrainBacklog(context.Background()); err != nil {
|
||||
t.Fatalf("DrainBacklog: %v", err)
|
||||
}
|
||||
if err := sink.SealBeforeBarrier(); err != nil {
|
||||
t.Fatalf("SealBeforeBarrier: %v", err)
|
||||
}
|
||||
|
||||
if err := sink.NotifyAppend(0, 1, []byte{0x42}); !errors.Is(err, ErrSinkBarrierSealed) {
|
||||
t.Fatalf("NotifyAppend before barrier err=%v want ErrSinkBarrierSealed", err)
|
||||
}
|
||||
gotConn, gotLineage := readEntryEmitContext(t, e, replicaID)
|
||||
if gotConn != sessionConn || gotLineage != sessionLineage {
|
||||
t.Fatalf("pre-barrier seal restored steady context early: conn=%p lineage=%+v", gotConn, gotLineage)
|
||||
}
|
||||
|
||||
sink.EndSession()
|
||||
if err := sink.NotifyAppend(0, 1, []byte{0x42}); !errors.Is(err, ErrSinkSealed) {
|
||||
t.Fatalf("NotifyAppend after EndSession err=%v want ErrSinkSealed", err)
|
||||
}
|
||||
gotConn, gotLineage = readEntryEmitContext(t, e, replicaID)
|
||||
if gotConn != steadyConn || gotLineage != steadyLineage {
|
||||
t.Fatalf("EndSession did not restore steady context: conn=%p lineage=%+v", gotConn, gotLineage)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecoverySink_SatisfiesWalShipperSink — compile-time check
|
||||
// that *RecoverySink implements the recovery.WalShipperSink
|
||||
// interface by duck typing. The interface lives in `recovery`
|
||||
@@ -536,6 +582,7 @@ func TestRecoverySink_SatisfiesWalShipperSink(t *testing.T) {
|
||||
type walShipperSinkShape interface {
|
||||
StartSession(fromLSN uint64) error
|
||||
DrainBacklog(ctx context.Context) error
|
||||
SealBeforeBarrier() error
|
||||
EndSession()
|
||||
NotifyAppend(lba uint32, lsn uint64, data []byte) error
|
||||
}
|
||||
|
||||
@@ -93,6 +93,11 @@ func (e *BlockExecutor) FeedLiveWrite(replicaID string, lineage RecoveryLineage,
|
||||
replicaID, lsn)
|
||||
return LiveWriteRetained, nil
|
||||
}
|
||||
if errors.Is(err, ErrSinkBarrierSealed) {
|
||||
log.Printf("transport: live write retained replica=%s lsn=%d (recovery barrier cut established)",
|
||||
replicaID, lsn)
|
||||
return LiveWriteRetained, nil
|
||||
}
|
||||
if errors.Is(err, recovery.ErrNoActiveSession) {
|
||||
// Bridge sender-map deletion raced with our registered-session
|
||||
// snapshot. Recovery no longer owns this write; use the steady
|
||||
|
||||
@@ -801,3 +801,78 @@ func TestExecutor_Ship_ActiveSessionSealed_FallsBackToSteadyEmitter(t *testing.T
|
||||
_ = sessionServer.Close()
|
||||
close(sink.release)
|
||||
}
|
||||
|
||||
func TestExecutor_FeedLiveWrite_BarrierSealedSessionRetains(t *testing.T) {
|
||||
primary := storage.NewBlockStore(8, 4096)
|
||||
coord := recovery.NewPeerShipCoordinator()
|
||||
replicaID := "r1"
|
||||
exec := NewBlockExecutorWithDualLane(primary, "127.0.0.1:1", "127.0.0.1:2", coord, recovery.ReplicaID(replicaID))
|
||||
|
||||
steadyClient, steadyServer := net.Pipe()
|
||||
t.Cleanup(func() {
|
||||
_ = steadyClient.Close()
|
||||
_ = steadyServer.Close()
|
||||
})
|
||||
go func() {
|
||||
_, _ = io.Copy(io.Discard, steadyServer)
|
||||
}()
|
||||
|
||||
steadyLineage := RecoveryLineage{SessionID: 91, Epoch: 7, EndpointVersion: 3, TargetLSN: 100}
|
||||
session, err := exec.registerSession(steadyLineage)
|
||||
if err != nil {
|
||||
t.Fatalf("register steady session: %v", err)
|
||||
}
|
||||
session.conn = steadyClient
|
||||
shipper := exec.WalShipperFor(replicaID)
|
||||
exec.updateWalShipperEmitContext(replicaID, steadyClient, steadyLineage, EmitProfileSteadyMsgShip)
|
||||
|
||||
sessionClient, sessionServer := net.Pipe()
|
||||
t.Cleanup(func() {
|
||||
_ = sessionClient.Close()
|
||||
_ = sessionServer.Close()
|
||||
})
|
||||
go func() {
|
||||
_, _ = io.Copy(io.Discard, sessionServer)
|
||||
}()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
sink := newRecordingSessionSink()
|
||||
sink.notifyErr = ErrSinkBarrierSealed
|
||||
if err := exec.dualLane.Bridge.StartRebuildSessionWithSink(
|
||||
ctx, sessionClient, recovery.ReplicaID(replicaID), 77, 10, 10, sink,
|
||||
); err != nil {
|
||||
t.Fatalf("StartRebuildSessionWithSink: %v", err)
|
||||
}
|
||||
select {
|
||||
case <-sink.started:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("session sink did not start")
|
||||
}
|
||||
|
||||
data := make([]byte, 4096)
|
||||
data[0] = 0x6a
|
||||
disposition, err := exec.FeedLiveWrite(replicaID, steadyLineage, true, 3, 1, data)
|
||||
if err != nil {
|
||||
t.Fatalf("FeedLiveWrite with barrier-sealed session: %v", err)
|
||||
}
|
||||
if disposition != LiveWriteRetained {
|
||||
t.Fatalf("disposition=%v want LiveWriteRetained", disposition)
|
||||
}
|
||||
if got := sink.Entries(); got != 0 {
|
||||
t.Fatalf("barrier-sealed session recorded %d entries; want 0", got)
|
||||
}
|
||||
if got := shipper.Cursor(); got != 0 {
|
||||
t.Fatalf("steady shipper cursor=%d want 0; write fell through during barrier", got)
|
||||
}
|
||||
gotConn, gotLineage, gotProfile := exec.SnapshotEmitContext(replicaID)
|
||||
if gotConn != steadyClient || gotLineage != steadyLineage || gotProfile != EmitProfileSteadyMsgShip {
|
||||
t.Fatalf("barrier-sealed write mutated steady context: conn=%p lineage=%+v profile=%s",
|
||||
gotConn, gotLineage, gotProfile)
|
||||
}
|
||||
|
||||
cancel()
|
||||
_ = sessionClient.Close()
|
||||
_ = sessionServer.Close()
|
||||
close(sink.release)
|
||||
}
|
||||
@@ -117,6 +117,8 @@ const (
|
||||
ModeBacklog
|
||||
)
|
||||
|
||||
var ErrWalShipperSessionSealed = errors.New("walshipper: recovery session sealed before barrier")
|
||||
|
||||
func (m WalShipperMode) String() string {
|
||||
switch m {
|
||||
case ModeIdle:
|
||||
@@ -249,6 +251,7 @@ type WalShipper struct {
|
||||
// initial state at NewWalShipper construction; there is no path
|
||||
// from any non-Idle mode back to Idle in current API.
|
||||
sessionActive bool
|
||||
sessionSealed bool
|
||||
fromLSN uint64 // session entry watermark (post-rewind cursor anchor)
|
||||
|
||||
// Saturation observability (R2). lagSample updated on every emit
|
||||
@@ -433,6 +436,12 @@ func (s *WalShipper) drive(in driveInput) error {
|
||||
if s.mode == ModeIdle {
|
||||
return nil
|
||||
}
|
||||
if s.sessionSealed {
|
||||
if in.has {
|
||||
return ErrWalShipperSessionSealed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
head := s.head.Head()
|
||||
|
||||
@@ -644,17 +653,41 @@ func (s *WalShipper) StartSession(fromLSN uint64) error {
|
||||
s.cursor = fromLSN // the one rewind, per INV-MONOTONIC-CURSOR
|
||||
s.fromLSN = fromLSN
|
||||
s.sessionActive = true
|
||||
s.sessionSealed = false
|
||||
s.mode = ModeBacklog
|
||||
s.saturationFired.Store(false)
|
||||
s.firstLiveEmitted.Store(false) // §IV.2.1 PrimaryLiveTail reset per session
|
||||
return nil
|
||||
}
|
||||
|
||||
// SealSession establishes the atomic pre-barrier cut under the same
|
||||
// serializer used by NotifyAppend and the timer drain. It is legal only
|
||||
// after backlog drain has reached Realtime.
|
||||
func (s *WalShipper) SealSession() error {
|
||||
s.shipMu.Lock()
|
||||
defer s.shipMu.Unlock()
|
||||
if !s.sessionActive {
|
||||
return errors.New("walshipper: SealSession without active session")
|
||||
}
|
||||
if s.mode != ModeRealtime {
|
||||
return fmt.Errorf("walshipper: SealSession in mode %s (want Realtime)", s.mode)
|
||||
}
|
||||
s.sessionSealed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// EndSession returns the shipper to Realtime (post-session steady).
|
||||
// Cursor stays where it is (typically caught up to head via
|
||||
// DrainBacklog success). Called from recovery.Sender's defer or on
|
||||
// session failure.
|
||||
func (s *WalShipper) EndSession() {
|
||||
s.endSessionWithHandoff(nil)
|
||||
}
|
||||
|
||||
// endSessionWithHandoff keeps emission sealed while the owner swaps
|
||||
// session-specific state, such as the executor's wire context, back to
|
||||
// steady state.
|
||||
func (s *WalShipper) endSessionWithHandoff(handoff func()) {
|
||||
s.shipMu.Lock()
|
||||
defer s.shipMu.Unlock()
|
||||
s.sessionActive = false
|
||||
@@ -664,6 +697,10 @@ func (s *WalShipper) EndSession() {
|
||||
// fromLSN. Mode Realtime is safe because steady-state callers
|
||||
// (NotifyAppend) check shipMu + cursor independently.
|
||||
s.mode = ModeRealtime
|
||||
if handoff != nil {
|
||||
handoff()
|
||||
}
|
||||
s.sessionSealed = false
|
||||
}
|
||||
|
||||
// NotifyAppend is the §6.3 single-mailbox entry for "primary just
|
||||
|
||||
@@ -16,7 +16,9 @@ package transport
|
||||
// kickoff §10 OOS — WAL substrate, not BlockStore).
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -89,6 +91,107 @@ func (r *recordingEmit) Snapshot() []emitRecord {
|
||||
|
||||
func (r *recordingEmit) MaxInFlight() int32 { return r.maxInFlight.Load() }
|
||||
|
||||
func TestWalShipper_SealSessionStopsTimerAndAppend(t *testing.T) {
|
||||
primary := memorywal.NewStore(8, 64)
|
||||
emit := newRecordingEmit()
|
||||
cfg := DefaultWalShipperConfig()
|
||||
cfg.IdleSleep = time.Millisecond
|
||||
s := NewWalShipperWithOptions("r1", HeadSourceFromStorage(primary), primary, emit.Func(), cfg)
|
||||
defer s.Stop()
|
||||
|
||||
if err := s.StartSession(0); err != nil {
|
||||
t.Fatalf("StartSession: %v", err)
|
||||
}
|
||||
if err := s.DrainBacklog(context.Background()); err != nil {
|
||||
t.Fatalf("DrainBacklog: %v", err)
|
||||
}
|
||||
if err := s.SealSession(); err != nil {
|
||||
t.Fatalf("SealSession: %v", err)
|
||||
}
|
||||
|
||||
data := bytes.Repeat([]byte{0x5a}, 64)
|
||||
lsn, err := primary.Write(1, data)
|
||||
if err != nil {
|
||||
t.Fatalf("primary Write: %v", err)
|
||||
}
|
||||
if err := s.NotifyAppend(1, lsn, data); !errors.Is(err, ErrWalShipperSessionSealed) {
|
||||
t.Fatalf("NotifyAppend after SealSession err=%v want ErrWalShipperSessionSealed", err)
|
||||
}
|
||||
time.Sleep(10 * cfg.IdleSleep)
|
||||
if got := len(emit.Snapshot()); got != 0 {
|
||||
t.Fatalf("sealed session emitted %d records from timer/append; want 0", got)
|
||||
}
|
||||
if got := s.Cursor(); got != 0 {
|
||||
t.Fatalf("sealed session cursor=%d want 0", got)
|
||||
}
|
||||
|
||||
s.EndSession()
|
||||
if err := s.NotifyAppend(1, lsn, data); err != nil {
|
||||
t.Fatalf("NotifyAppend after EndSession: %v", err)
|
||||
}
|
||||
if got := len(emit.Snapshot()); got != 1 {
|
||||
t.Fatalf("post-EndSession steady emits=%d want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalShipper_EndSessionHandoffKeepsTimerSealed(t *testing.T) {
|
||||
primary := memorywal.NewStore(8, 64)
|
||||
emit := newRecordingEmit()
|
||||
cfg := DefaultWalShipperConfig()
|
||||
cfg.IdleSleep = time.Millisecond
|
||||
s := NewWalShipperWithOptions("r1", HeadSourceFromStorage(primary), primary, emit.Func(), cfg)
|
||||
defer s.Stop()
|
||||
|
||||
if err := s.StartSession(0); err != nil {
|
||||
t.Fatalf("StartSession: %v", err)
|
||||
}
|
||||
if err := s.DrainBacklog(context.Background()); err != nil {
|
||||
t.Fatalf("DrainBacklog: %v", err)
|
||||
}
|
||||
if err := s.SealSession(); err != nil {
|
||||
t.Fatalf("SealSession: %v", err)
|
||||
}
|
||||
if _, err := primary.Write(1, bytes.Repeat([]byte{0x6b}, 64)); err != nil {
|
||||
t.Fatalf("primary Write: %v", err)
|
||||
}
|
||||
|
||||
handoffEntered := make(chan struct{})
|
||||
handoffRelease := make(chan struct{})
|
||||
endDone := make(chan struct{})
|
||||
go func() {
|
||||
s.endSessionWithHandoff(func() {
|
||||
close(handoffEntered)
|
||||
<-handoffRelease
|
||||
})
|
||||
close(endDone)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-handoffEntered:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("EndSession handoff did not start")
|
||||
}
|
||||
time.Sleep(10 * cfg.IdleSleep)
|
||||
if got := len(emit.Snapshot()); got != 0 {
|
||||
t.Fatalf("timer emitted %d records before handoff completed; want 0", got)
|
||||
}
|
||||
|
||||
close(handoffRelease)
|
||||
select {
|
||||
case <-endDone:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("EndSession handoff did not complete")
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(100 * time.Millisecond)
|
||||
for len(emit.Snapshot()) == 0 && time.Now().Before(deadline) {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
if got := len(emit.Snapshot()); got != 1 {
|
||||
t.Fatalf("steady timer emits after handoff=%d want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
// fixedHeadSource lets the test pin head independently of substrate;
|
||||
// useful for R1 race tests where we want to control the head value
|
||||
// observed by the WalShipper independently of what the substrate
|
||||
|
||||
Reference in new issue
Block a user