@@ -41,10 +41,11 @@ use crate::{
4141 error:: { ConnectionError , InternalError } ,
4242 Credentials , DriverOptions , Error , Result ,
4343} ;
44+ use crate :: connection:: server_replica:: { AvailableServerReplica , Replica } ;
4445
4546pub ( crate ) struct ServerManager {
4647 configured_addresses : Addresses ,
47- replicas : RwLock < HashSet < ServerReplica > > ,
48+ replicas : RwLock < HashSet < AvailableServerReplica > > ,
4849 replica_connections : RwLock < HashMap < Address , ServerConnection > > ,
4950 address_translation : RwLock < AddressTranslation > ,
5051
@@ -87,11 +88,10 @@ impl ServerManager {
8788 )
8889 . await ?;
8990 let address_translation = addresses. address_translation ( ) ;
90-
9191 println ! ( "INIT REPLICA CONNECTIONS: {:?}" , source_connections) ;
9292 let server_manager = Self {
9393 configured_addresses : addresses,
94- replicas : RwLock :: new ( replicas) ,
94+ replicas : RwLock :: new ( Self :: filter_unavailable_replicas ( replicas) ) ,
9595 replica_connections : RwLock :: new ( source_connections) ,
9696 address_translation : RwLock :: new ( address_translation) ,
9797 background_runtime,
@@ -207,10 +207,6 @@ impl ServerManager {
207207 self . replica_connections . write ( ) . expect ( "Expected server connections write access" )
208208 }
209209
210- fn read_replicas ( & self ) -> RwLockReadGuard < ' _ , HashSet < ServerReplica > > {
211- self . replicas . read ( ) . expect ( "Expected a read replica lock" )
212- }
213-
214210 fn read_address_translation ( & self ) -> RwLockReadGuard < ' _ , AddressTranslation > {
215211 self . address_translation . read ( ) . expect ( "Expected address translation read access" )
216212 }
@@ -219,16 +215,6 @@ impl ServerManager {
219215 self . read_replica_connections ( ) . values ( ) . map ( ServerConnection :: force_close) . try_collect ( ) . map_err ( Into :: into)
220216 }
221217
222- fn replicas ( & self ) -> HashSet < ServerReplica > {
223- let res = self . read_replicas ( ) . iter ( ) . cloned ( ) . collect ( ) ;
224- println ! ( "Current replicas: {res:?}" ) ;
225- res
226- }
227-
228- pub ( crate ) fn primary_replica ( & self ) -> Option < ServerReplica > {
229- self . read_replicas ( ) . iter ( ) . filter ( |replica| replica. is_primary ( ) ) . max_by_key ( |replica| replica. term ( ) ) . cloned ( )
230- }
231-
232218 pub ( crate ) fn username ( & self ) -> Result < String > {
233219 match self . read_replica_connections ( ) . iter ( ) . next ( ) {
234220 Some ( ( _, replica_connection) ) => Ok ( replica_connection. username ( ) . to_string ( ) ) ,
@@ -271,9 +257,12 @@ impl ServerManager {
271257 P : Future < Output = Result < R > > ,
272258 {
273259 println ! ( "STRONG" ) ;
274- let mut primary_replica = match self . primary_replica ( ) {
260+ let mut primary_replica = match self . read_primary_replica ( ) {
275261 Some ( replica) => replica,
276- None => self . seek_primary_replica_in ( self . replicas ( ) ) . await ?,
262+ None => {
263+ let replicas: HashSet < _ > = self . read_replicas ( ) . iter ( ) . cloned ( ) . collect ( ) ;
264+ self . seek_primary_replica_in ( replicas) . await ?
265+ } ,
277266 } ;
278267
279268 let retries = self . driver_options . primary_failover_retries ;
@@ -283,7 +272,8 @@ impl ServerManager {
283272 let private_address = primary_replica. private_address ( ) . clone ( ) ;
284273 match self . execute_on ( primary_replica. address ( ) , & private_address, & task) . await {
285274 Err ( Error :: Connection ( connection_error) ) => {
286- let replicas_without_old_primary = self . replicas ( ) . into_iter ( ) . filter ( |replica| {
275+ let replicas: HashSet < _ > = self . read_replicas ( ) . iter ( ) . cloned ( ) . collect ( ) ;
276+ let replicas_without_old_primary = replicas. into_iter ( ) . filter ( |replica| {
287277 println ! ( "REPLICAS: Filter out? {private_address:?} vs mine {:?}" , replica. private_address( ) ) ;
288278 replica. private_address ( ) != & private_address
289279 } ) ;
@@ -326,12 +316,12 @@ impl ServerManager {
326316 F : Fn ( ServerConnection ) -> P ,
327317 P : Future < Output = Result < R > > ,
328318 {
329- let replicas = self . replicas ( ) ;
319+ let replicas: HashSet < _ > = self . read_replicas ( ) . iter ( ) . cloned ( ) . collect ( ) ;
330320 self . execute_on_any ( replicas, task) . await
331321 }
332322
333323 #[ cfg_attr( feature = "sync" , maybe_async:: must_be_sync) ]
334- async fn execute_on_any < F , P , R > ( & self , replicas : impl IntoIterator < Item = ServerReplica > , task : F ) -> Result < R >
324+ async fn execute_on_any < F , P , R > ( & self , replicas : impl IntoIterator < Item = AvailableServerReplica > , task : F ) -> Result < R >
335325 where
336326 F : Fn ( ServerConnection ) -> P ,
337327 P : Future < Output = Result < R > > ,
@@ -379,8 +369,8 @@ impl ServerManager {
379369 #[ cfg_attr( feature = "sync" , maybe_async:: must_be_sync) ]
380370 async fn seek_primary_replica_in (
381371 & self ,
382- source_replicas : impl IntoIterator < Item = ServerReplica > ,
383- ) -> Result < ServerReplica > {
372+ source_replicas : impl IntoIterator < Item = AvailableServerReplica > ,
373+ ) -> Result < AvailableServerReplica > {
384374 // TODO: Add retries with sleeps in between? Maybe replica_discovery_attempts should work / be used differently
385375 println ! ( "SEEK" ) ;
386376 self . execute_on_any ( source_replicas, |replica_connection| async {
@@ -390,13 +380,13 @@ impl ServerManager {
390380 }
391381
392382 #[ cfg_attr( feature = "sync" , maybe_async:: must_be_sync) ]
393- async fn seek_primary_replica ( & self , replica_connection : ServerConnection ) -> Result < ServerReplica > {
383+ async fn seek_primary_replica ( & self , replica_connection : ServerConnection ) -> Result < AvailableServerReplica > {
394384 let address_translation = self . read_address_translation ( ) . clone ( ) ;
395385 println ! ( "SEEK PRIMARY REPLICA" ) ;
396386 let replicas = Self :: fetch_replicas_from_connection ( & replica_connection, & address_translation) . await ?;
397387 println ! ( "WRITE NEW PRIMARY" ) ;
398- * self . replicas . write ( ) . expect ( "Expected replicas write lock" ) = replicas;
399- if let Some ( replica) = self . primary_replica ( ) {
388+ * self . replicas . write ( ) . expect ( "Expected replicas write lock" ) = Self :: filter_unavailable_replicas ( replicas) ;
389+ if let Some ( replica) = self . read_primary_replica ( ) {
400390 self . update_replica_connections ( ) . await ?;
401391 Ok ( replica)
402392 } else {
@@ -438,7 +428,7 @@ impl ServerManager {
438428 return Ok ( ( source_connections, translated_replicas) ) ;
439429 } else {
440430 if let Some ( target_replica) =
441- translated_replicas. into_iter ( ) . find ( |replica| replica. address ( ) == address)
431+ translated_replicas. into_iter ( ) . find ( |replica| replica. address ( ) == Some ( address) )
442432 {
443433 let source_connections = HashMap :: from ( [ ( address. clone ( ) , replica_connection) ] ) ;
444434 return Ok ( ( source_connections, HashSet :: from ( [ target_replica] ) ) ) ;
@@ -478,7 +468,7 @@ impl ServerManager {
478468 async move { Self :: fetch_replicas_from_connection ( & replica_connection, & address_translation) . await }
479469 } )
480470 . await ?;
481- * self . replicas . write ( ) . expect ( "Expected replicas write lock" ) = replicas. clone ( ) ;
471+ * self . replicas . write ( ) . expect ( "Expected replicas write lock" ) = Self :: filter_unavailable_replicas ( replicas. clone ( ) ) ;
482472 Ok ( replicas)
483473 }
484474
@@ -497,6 +487,31 @@ impl ServerManager {
497487 replicas. into_iter ( ) . map ( |replica| replica. translated ( address_translation) ) . collect ( )
498488 }
499489
490+ fn filter_unavailable_replicas ( replicas : impl IntoIterator < Item = ServerReplica > ) -> HashSet < AvailableServerReplica > {
491+ replicas. into_iter ( ) . filter_map ( |replica| match replica {
492+ ServerReplica :: Available ( available) => Some ( available) ,
493+ ServerReplica :: Unavailable { .. } => None ,
494+ } ) . collect ( )
495+ }
496+
497+ #[ cfg_attr( feature = "sync" , maybe_async:: must_be_sync) ]
498+ pub ( crate ) async fn fetch_primary_replica ( & self ) -> Result < Option < ServerReplica > > {
499+ let replicas = self . fetch_replicas ( ) . await ?;
500+ Ok ( Self :: filter_primary_replica ( replicas. iter ( ) ) )
501+ }
502+
503+ fn read_replicas ( & self ) -> RwLockReadGuard < ' _ , HashSet < AvailableServerReplica > > {
504+ self . replicas . read ( ) . expect ( "Expected a read replica lock" )
505+ }
506+
507+ fn read_primary_replica ( & self ) -> Option < AvailableServerReplica > {
508+ Self :: filter_primary_replica ( self . read_replicas ( ) . iter ( ) )
509+ }
510+
511+ fn filter_primary_replica < ' a , R : Replica + ' a > ( replicas : impl IntoIterator < Item = & ' a R > ) -> Option < R > {
512+ replicas. into_iter ( ) . filter ( |replica| replica. is_primary ( ) ) . max_by_key ( |replica| replica. term ( ) . unwrap_or_default ( ) ) . cloned ( )
513+ }
514+
500515 fn server_connection_failed_err ( & self , errors : HashMap < Address , Error > ) -> Error {
501516 let accessed_addresses =
502517 Addresses :: from_addresses ( self . read_replicas ( ) . iter ( ) . map ( |replica| replica. address ( ) . clone ( ) ) ) ;
0 commit comments