@@ -694,6 +694,8 @@ impl ShardReplicaSet {
694694
695695 if self . this_peer_id ( ) == peer_id {
696696 self . on_local_state_updated ( state) . await ?;
697+ } else {
698+ self . on_remote_state_updated ( peer_id, state) . await ;
697699 }
698700
699701 self . update_locally_disabled ( peer_id) ;
@@ -764,6 +766,8 @@ impl ShardReplicaSet {
764766
765767 if self . this_peer_id ( ) == peer_id {
766768 self . on_local_state_updated ( state) . await ?;
769+ } else {
770+ self . on_remote_state_updated ( peer_id, state) . await ;
767771 }
768772
769773 self . update_locally_disabled ( peer_id) ;
@@ -783,11 +787,42 @@ impl ShardReplicaSet {
783787 } else {
784788 local_shard. take_newest_clocks_snapshot ( ) . await ?;
785789 }
790+ // Reset WAL retention to normal whenever local shard changes state
791+ local_shard. set_normal_wal_retention ( ) . await ;
786792 }
787793
788794 Ok ( ( ) )
789795 }
790796
797+ /// Called when a peer state is changed (except local peer).
798+ ///
799+ async fn on_remote_state_updated ( & self , _peer_id : PeerId , _new_state : ReplicaState ) {
800+ let mut is_any_remote_dead = false ;
801+ let mut is_local_active = false ;
802+ let this_peer_id = self . this_peer_id ( ) ;
803+
804+ for ( peer_id, peer_state) in self . replica_state . read ( ) . peers ( ) . iter ( ) {
805+ if * peer_id == this_peer_id {
806+ if peer_state. is_active ( ) {
807+ is_local_active = true ;
808+ }
809+ } else if peer_state. requires_recovery ( ) {
810+ is_any_remote_dead = true ;
811+ }
812+ }
813+
814+ {
815+ let local_opt = self . local . read ( ) . await ;
816+ if let Some ( local_shard) = local_opt. as_ref ( ) {
817+ if is_local_active && is_any_remote_dead {
818+ local_shard. set_extended_wal_retention ( ) . await ;
819+ } else {
820+ local_shard. set_normal_wal_retention ( ) . await ;
821+ }
822+ }
823+ }
824+ }
825+
791826 pub async fn remove_peer ( & self , peer_id : PeerId ) -> CollectionResult < ( ) > {
792827 if self . this_peer_id ( ) == peer_id {
793828 self . remove_local ( ) . await ?;
0 commit comments