From c7cff76f0018febdb19febad1d011443dde31bb2 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 11:47:43 -0800 Subject: [PATCH] replication: the store learner runs on the peer's queue worker The learning barrier used the peer's connection from its own goroutine, outside the work queue that keeps one writer per connection, so on an exposed volume it could overlap the first ship. It is now a queue job on that worker; it is queued only when there is room (it never saturates the queue, which would degrade the peer) and retried otherwise. Co-Authored-By: Claude Opus 5.5 --- .../core/replication/peer_work_queue.go | 27 +++++++++ .../replication/store_learn_bringup_test.go | 56 +++++++++++++++++++ seaweed-block/core/replication/volume.go | 31 ++++++++-- 3 files changed, 109 insertions(+), 5 deletions(-) diff --git a/seaweed-block/core/replication/peer_work_queue.go b/seaweed-block/core/replication/peer_work_queue.go index db5c55f5bd..9292c1825a 100644 --- a/seaweed-block/core/replication/peer_work_queue.go +++ b/seaweed-block/core/replication/peer_work_queue.go @@ -24,6 +24,10 @@ type peerWorkKind uint8 const ( peerWorkWrite peerWorkKind = iota + 1 peerWorkBarrier + // peerWorkLearnStore asks the peer for its store (a barrier at LSN 0 + // that never degrades it). It runs on the queue's one worker, so it + // never overlaps a ship or a data barrier on the peer's connection. + peerWorkLearnStore ) type peerWorkResult struct { @@ -43,6 +47,7 @@ type peerWork struct { type peerWorkQueueOps struct { ship func(context.Context, LocalWrite) (bool, error) barrier func(context.Context, uint64) (uint64, error) + learnStore func() bool invalidate func(string) } @@ -91,6 +96,7 @@ func newPeerWorkQueue(peer *ReplicaPeer, depth int) *peerWorkQueue { } return ack.AchievedLSN, nil }, + learnStore: peer.learnStore, invalidate: peer.Invalidate, }) } @@ -120,6 +126,23 @@ func (q *peerWorkQueue) enqueueBarrier(targetLSN uint64, result chan<- peerWorkR return q.enqueue(peerWork{kind: peerWorkBarrier, targetLSN: targetLSN, result: result}) } +// tryLearnStore queues a store-learning job when there is room, and reports +// whether it did. Unlike a write it never saturates the queue (that would +// degrade the peer): when the queue is full the caller tries again later. +func (q *peerWorkQueue) tryLearnStore(result chan<- peerWorkResult) bool { + q.mu.Lock() + defer q.mu.Unlock() + if q.closed { + return false + } + select { + case q.jobs <- peerWork{kind: peerWorkLearnStore, result: result}: + return true + default: + return false + } +} + func (q *peerWorkQueue) enqueue(work peerWork) (int, error) { q.mu.Lock() if q.closed { @@ -186,6 +209,10 @@ func (q *peerWorkQueue) run() { result.eligible, result.err = q.ops.ship(q.ctx, work.write) case peerWorkBarrier: result.ackLSN, result.err = q.ops.barrier(q.ctx, work.targetLSN) + case peerWorkLearnStore: + if q.ops.learnStore != nil { + result.eligible = q.ops.learnStore() // eligible: the store is now known + } default: result.err = fmt.Errorf("replication: peer %s unknown queue work kind %d", q.peerID, work.kind) } diff --git a/seaweed-block/core/replication/store_learn_bringup_test.go b/seaweed-block/core/replication/store_learn_bringup_test.go index 4e99dbfe1e..8e058f53c3 100644 --- a/seaweed-block/core/replication/store_learn_bringup_test.go +++ b/seaweed-block/core/replication/store_learn_bringup_test.go @@ -1,6 +1,7 @@ package replication import ( + "context" "net" "path/filepath" "testing" @@ -57,3 +58,58 @@ func TestStoreLearnedOnceAReplicaListensAfterTheAssignment(t *testing.T) { time.Sleep(20 * time.Millisecond) } } + +// The store learner and the first writes share the peer's connection. Run +// with -race: a write racing a learner must not touch the connection from +// two goroutines (the learner runs on the peer's queue worker, the single +// writer). +func TestStoreLearnerDoesNotRaceTheFirstWrites(t *testing.T) { + probe, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + addr := probe.Addr().String() + _ = probe.Close() + + local, err := storage.CreateWALStore(filepath.Join(t.TempDir(), "p.wal"), 64, 4096) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = local.Close() }) + v := NewReplicationVolume("v", local) + defer v.Close() + v.RequireAttestedStores() + if err := v.UpdateReplicaSet(1, []ReplicaTarget{{ReplicaID: "r2", DataAddr: addr, ControlAddr: addr, Epoch: 1, EndpointVersion: 1}}); err != nil { + t.Fatal(err) + } + s, err := storage.CreateWALStore(filepath.Join(t.TempDir(), "r2.wal"), 64, 4096) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = s.Close() }) + l, err := transport.NewReplicaListener(addr, s) + if err != nil { + t.Skipf("could not re-bind %s: %v", addr, err) + } + l.Serve() + t.Cleanup(func() { l.Stop() }) + + // Writes start the moment the replica listens, while the learner retries. + data := make([]byte, 4096) + for i := uint32(0); i < 40; i++ { + lsn, err := local.Write(i%64, data) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + _ = v.OnLocalWrite(ctx, LocalWrite{LBA: i % 64, Data: data, LSN: lsn}) + cancel() + } + deadline := time.Now().Add(10 * time.Second) + for v.AttestedStores()["r2"] != s.StoreID() { + if time.Now().After(deadline) { + t.Fatalf("store never learned while writes ran (attested %v)", v.AttestedStores()) + } + time.Sleep(20 * time.Millisecond) + } +} diff --git a/seaweed-block/core/replication/volume.go b/seaweed-block/core/replication/volume.go index 182c8b3a84..3eb9380622 100644 --- a/seaweed-block/core/replication/volume.go +++ b/seaweed-block/core/replication/volume.go @@ -562,10 +562,14 @@ func (v *ReplicationVolume) learnPeerStores(peers []*ReplicaPeer) { func (v *ReplicationVolume) learnPeerStore(peer *ReplicaPeer) { pause := storeLearnFirstPause for attempt := 1; ; attempt++ { - if !peer.learningStore() || !v.holdsPeer(peer) { + if !peer.learningStore() { return } - if peer.learnStore() { + q, ok := v.peerQueueFor(peer) + if !ok { + return + } + if v.learnThroughQueue(q) { return } if attempt == 1 || attempt%20 == 0 { @@ -578,11 +582,28 @@ func (v *ReplicationVolume) learnPeerStore(peer *ReplicaPeer) { } } -// holdsPeer reports whether peer is still this open volume's peer. -func (v *ReplicationVolume) holdsPeer(peer *ReplicaPeer) bool { +// peerQueueFor returns peer's work queue while peer is still this open +// volume's peer. +func (v *ReplicationVolume) peerQueueFor(peer *ReplicaPeer) (*peerWorkQueue, bool) { v.mu.Lock() defer v.mu.Unlock() - return !v.closed && v.peers[peer.Target().ReplicaID] == peer + id := peer.Target().ReplicaID + if v.closed || v.peers[id] != peer || v.peerQueues[id] == nil { + return nil, false + } + return v.peerQueues[id], true +} + +// learnThroughQueue runs one store-learning attempt on the peer's queue +// worker (the connection's single writer) and reports whether the store is +// now known. A full or closed queue is an attempt that did not run. +func (v *ReplicationVolume) learnThroughQueue(q *peerWorkQueue) bool { + result := make(chan peerWorkResult, 1) + if !q.tryLearnStore(result) { + return false + } + r := <-result + return r.err == nil && r.eligible } // The store-learning retry pauses: short first (a replica that starts a