diff --git a/seaweed-block/core/replication/component/attest_count_test.go b/seaweed-block/core/replication/component/attest_count_test.go index c66bc945e5..017ae6cac6 100644 --- a/seaweed-block/core/replication/component/attest_count_test.go +++ b/seaweed-block/core/replication/component/attest_count_test.go @@ -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) + } + }) + } +} diff --git a/seaweed-block/core/replication/volume.go b/seaweed-block/core/replication/volume.go index dbaa9841e1..a44468f0b0 100644 --- a/seaweed-block/core/replication/volume.go +++ b/seaweed-block/core/replication/volume.go @@ -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.