replication: a failed store-learning barrier never degrades the peer

At bring-up the primary may install a peer whose replica does not listen
yet. The barrier that learns the replica's store then failed and degraded
the peer; nothing re-probes a degraded peer before a write, so its store
was never attested and a new volume never left creating. The store-learning
barrier now leaves the peer as it is on failure and retries in the
background (100 ms doubling to 2 s) until the store is learned or the peer
leaves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-25 11:43:24 -08:00
1 parent 395a2b0953
commit 9bcb6c28cd
3 files changed
+132 -11

No files matched your search

+34
View File
@@ -710,6 +710,40 @@ func (p *ReplicaPeer) Barrier(ctx context.Context, targetLSN uint64) (transport.
return ack, nil
}
// learnStore asks the peer for its store with a barrier at LSN 0 and
// records it. Unlike Barrier, a failure does not degrade the peer: a
// replica that does not answer yet (it starts after the primary installed
// it) holds nothing to be wrong about, and nothing re-probes a degraded
// peer while no write runs. It reports whether the store is now known.
func (p *ReplicaPeer) learnStore() bool {
p.mu.Lock()
if p.closed || p.state != ReplicaHealthy {
p.mu.Unlock()
return false
}
lineage := p.lineage
p.mu.Unlock()
ack, err := p.executor.Barrier(p.target.ReplicaID, lineage, 0)
if err != nil || ack.StoreID.IsZero() {
return false
}
p.mu.Lock()
defer p.mu.Unlock()
if p.closed || p.state != ReplicaHealthy {
return false
}
p.storeID = ack.StoreID
return true
}
// learningStore reports whether a store-learning retry should go on: the
// peer is open, Healthy and its store is still unknown.
func (p *ReplicaPeer) learningStore() bool {
p.mu.Lock()
defer p.mu.Unlock()
return !p.closed && p.state == ReplicaHealthy && p.storeID.IsZero()
}
// Fence issues a synchronous fence exchange against this peer using
// the caller-supplied lineage. Returns nil on success or a
// descriptive error on any failure. On error, marks the peer
@@ -0,0 +1,59 @@
package replication
import (
"net"
"path/filepath"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/storage"
"github.com/seaweedfs/seaweed-block/core/transport"
)
// Bring-up order: the primary installs its peers before a replica listens
// (the hosts start one after another). The barrier that learns the peer's
// store fails then; it must not degrade the peer (nothing re-probes a
// degraded peer while no write runs, so its store would never be attested
// and a new volume would never leave creating), and the store is learned
// once the replica answers.
func TestStoreLearnedOnceAReplicaListensAfterTheAssignment(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() // nothing listens on addr yet
v := NewReplicationVolume("v", storage.NewBlockStore(64, 4096))
defer v.Close() // before the listener's cleanup: Stop waits for the peer's connection
v.RequireAttestedStores()
if err := v.UpdateReplicaSet(1, []ReplicaTarget{{ReplicaID: "r2", DataAddr: addr, ControlAddr: addr, Epoch: 1, EndpointVersion: 1}}); err != nil {
t.Fatal(err)
}
if s := v.peers["r2"].State(); s != ReplicaHealthy {
t.Fatalf("a failed store-learning barrier degraded the peer (%s); nothing would ever attest it", s)
}
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() })
deadline := time.Now().Add(10 * time.Second)
for {
if got := v.AttestedStores()["r2"]; got == s.StoreID() {
return
}
if time.Now().After(deadline) {
t.Fatalf("the replica listens, but its store was never learned (attested %v)", v.AttestedStores())
}
time.Sleep(20 * time.Millisecond)
}
}
+39 -11
View File
@@ -550,21 +550,49 @@ func (v *ReplicationVolume) UpdateReplicaSet(generation uint64, targets []Replic
// is degraded like any failed barrier; its store is learned at the barrier
// that heals it.
func (v *ReplicationVolume) learnPeerStores(peers []*ReplicaPeer) {
var wg sync.WaitGroup
for _, peer := range peers {
wg.Add(1)
go func(peer *ReplicaPeer) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), promotionSeedBarrierTimeout)
defer cancel()
if _, err := peer.Barrier(ctx, 0); err != nil {
log.Printf("replication: volume %s store barrier peer=%s: %v", v.volumeID, peer.Target().ReplicaID, err)
}
}(peer)
go v.learnPeerStore(peer)
}
wg.Wait()
}
// learnPeerStore retries learnStore with a growing pause (up to
// storeLearnMaxPause) until the peer's store is known, or the peer is
// closed, replaced or no longer Healthy (a heal's barrier then teaches it),
// or the volume closes. Nothing waits for it.
func (v *ReplicationVolume) learnPeerStore(peer *ReplicaPeer) {
pause := storeLearnFirstPause
for attempt := 1; ; attempt++ {
if !peer.learningStore() || !v.holdsPeer(peer) {
return
}
if peer.learnStore() {
return
}
if attempt == 1 || attempt%20 == 0 {
log.Printf("replication: volume %s peer=%s store not learned yet (attempt %d); retrying", v.volumeID, peer.Target().ReplicaID, attempt)
}
time.Sleep(pause)
if pause *= 2; pause > storeLearnMaxPause {
pause = storeLearnMaxPause
}
}
}
// holdsPeer reports whether peer is still this open volume's peer.
func (v *ReplicationVolume) holdsPeer(peer *ReplicaPeer) bool {
v.mu.Lock()
defer v.mu.Unlock()
return !v.closed && v.peers[peer.Target().ReplicaID] == peer
}
// The store-learning retry pauses: short first (a replica that starts a
// moment after the primary), capped so a replica that is down costs one
// barrier attempt per cap.
const (
storeLearnFirstPause = 100 * time.Millisecond
storeLearnMaxPause = 2 * time.Second
)
func (v *ReplicationVolume) closePeerQueueLocked(replicaID string) {
if q := v.peerQueues[replicaID]; q != nil {
q.closeAndWait()