replication: learn new peers' stores outside the volume lock; convergence test

The frontier-0 store barriers run in parallel after UpdateReplicaSet
releases v.mu. A replica refused for an unrecorded store stays Healthy with
its learned store, so the next attestation records it with no operator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-25 06:53:38 -08:00
1 parent 5bf6e17ab1
commit 58b3c02386
2 files changed
+69 -18

No files matched your search

@@ -63,3 +63,43 @@ func TestUnrecordedReplicaAckDoesNotCompleteAQuorumWrite(t *testing.T) {
t.Fatalf("the flush after replica-1's store was recorded: %v", err)
}
}
// CONVERGENCE, with no operator: a replica whose store the master does not
// have (an upgraded volume: nothing recorded; a rebuilt replica: its old
// store recorded) fails sync_all flushes, but the failure leaves it Healthy
// with its barrier-learned store, so the primary's next attestation names
// that store. The master records it and re-sends the descriptors; the next
// flush counts it. The window is one attestation round (a heartbeat plus the
// re-send); the rule is not relaxed during it.
func TestUnrecordedReplicaConvergesThroughItsAttestation(t *testing.T) {
for _, c := range []struct {
name string
recorded func(real storage.StoreID) map[string]storage.StoreID
}{
{"upgraded volume (no record)", func(storage.StoreID) map[string]storage.StoreID { return nil }},
{"rebuilt replica (old store recorded)", func(storage.StoreID) map[string]storage.StoreID {
return map[string]storage.StoreID{"replica-0": {9, 9}}
}},
} {
t.Run(c.name, func(t *testing.T) {
cl := NewCluster(t, Walstore).WithReplicas(1).WithLiveShip().Start()
rv := cl.Primary().RepVol
rv.SetDurabilityMode(replication.DurabilitySyncAll)
real := storage.StoreIDOf(cl.Replica(0).Store)
rv.RequireAttestedStores()
recordStores(t, cl, c.recorded(real))
if err := attestSync(t, cl, 1, 0x11); err == nil {
t.Fatal("a sync_all flush counted a replica whose store the master does not have")
}
// What the primary attests in its next heartbeat:
att := rv.AttestedStores()
if att["replica-0"] != real {
t.Fatalf("after the refused flush the primary attests %x, want the replica's store %x (an invalidated peer would never be attested)", att["replica-0"], real)
}
recordStores(t, cl, att) // the master records the attestation and re-sends
if err := attestSync(t, cl, 2, 0x22); err != nil {
t.Fatalf("the flush after the attestation was recorded: %v", err)
}
})
}
}
+29 -18
View File
@@ -531,7 +531,7 @@ func (v *ReplicationVolume) UpdateReplicaSet(generation uint64, targets []Replic
addedPeers = append(addedPeers, peer)
}
v.seedNewPeerLiveCursorsLocked(addedPeers)
learn := v.seedNewPeerLiveCursorsLocked(addedPeers)
v.applyExpectedStoresLocked(targets)
// Advance the monotonic guard only for real (non-zero) generations.
@@ -540,23 +540,29 @@ func (v *ReplicationVolume) UpdateReplicaSet(generation uint64, targets []Replic
}
v.membershipUpdating = false
v.mu.Unlock()
v.learnPeerStores(learn)
return nil
}
// learnPeerStoresLocked runs a barrier at LSN 0 to each peer: the answer
// carries the peer's store. A peer that does not answer stays as it is (its
// store is learned at its next barrier).
func (v *ReplicationVolume) learnPeerStoresLocked(peers []*ReplicaPeer) {
// learnPeerStores runs a barrier at LSN 0 to each peer, in parallel and
// without v.mu (only the UpdateReplicaSet call waits, at most one barrier
// timeout): the answer carries the peer's store. A peer that does not answer
// 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 {
if peer == nil {
continue
}
ctx, cancel := context.WithTimeout(context.Background(), promotionSeedBarrierTimeout)
if _, err := peer.Barrier(ctx, 0); err != nil {
log.Printf("replication: volume %s store barrier peer=%s: %v", v.volumeID, peer.Target().ReplicaID, err)
}
cancel()
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)
}
wg.Wait()
}
func (v *ReplicationVolume) closePeerQueueLocked(replicaID string) {
@@ -611,24 +617,28 @@ func (v *ReplicationVolume) replaceTerminalPeerQueueLocked(id string, old *peerW
// cannot satisfy sync-quorum/sync-all write acknowledgement. A peer that
// answers above the frontier holds a divergent old-epoch tail and is
// degraded too (it needs a rebuild, never counts by length).
func (v *ReplicationVolume) seedNewPeerLiveCursorsLocked(peers []*ReplicaPeer) {
//
// It returns the peers whose store is still to be learned (a new volume
// where stores are required): the caller runs learnPeerStores on them after
// releasing v.mu.
func (v *ReplicationVolume) seedNewPeerLiveCursorsLocked(peers []*ReplicaPeer) []*ReplicaPeer {
if len(peers) == 0 {
return
return nil
}
frontier, err := v.store.Sync()
if err != nil {
log.Printf("replication: volume %s live cursor seed skipped: local sync failed: %v",
v.volumeID, err)
return
return nil
}
if frontier == 0 {
// Nothing to seed. Where stores are required, a barrier at 0 still
// teaches the primary each new peer's store, so it can attest it
// before any write (a new volume serves nothing until then).
if v.requireAttest {
v.learnPeerStoresLocked(peers)
return peers
}
return
return nil
}
for _, peer := range peers {
if peer == nil {
@@ -672,6 +682,7 @@ func (v *ReplicationVolume) seedNewPeerLiveCursorsLocked(peers []*ReplicaPeer) {
}
peer.SeedLiveShipCursor(frontier, "assignment frontier barrier")
}
return nil
}
// peerIDSet extracts the set of peer IDs from the current peers map.