1- use crate :: cluster_async:: { ClusterParams , ConnectionFuture } ;
1+ use crate :: cluster_async:: ConnectionFuture ;
22use crate :: cluster_routing:: { Route , SlotAddr } ;
33use crate :: cluster_slotmap:: { ReadFromReplicaStrategy , SlotMap , SlotMapValue } ;
44use crate :: cluster_topology:: TopologyHash ;
@@ -233,10 +233,10 @@ where
233233 }
234234 }
235235
236- fn round_robin_read_from_az_awareness_replica (
236+ pub ( crate ) fn round_robin_read_from_az_awareness_replica (
237237 & self ,
238238 slot_map_value : & SlotMapValue ,
239- user_az : String ,
239+ client_az : String ,
240240 ) -> Option < ConnectionAndAddress < Connection > > {
241241 let addrs = & slot_map_value. addrs ;
242242 let initial_index = slot_map_value
@@ -266,7 +266,7 @@ where
266266 // Check if this replica’s AZ matches the user’s AZ.
267267 if let Some ( connection_details) = self . connection_details_for_address ( replica. as_str ( ) )
268268 {
269- if connection_details. 1 . az . as_deref ( ) == Some ( & user_az ) {
269+ if connection_details. 1 . az . as_deref ( ) == Some ( & client_az ) {
270270 // Attempt to update `latest_used_replica` with the index of this replica.
271271 let _ = slot_map_value. latest_used_replica . compare_exchange_weak (
272272 initial_index,
@@ -280,42 +280,33 @@ where
280280 }
281281 }
282282
283- fn lookup_route (
284- & self ,
285- route : & Route ,
286- cluster_params : & Option < ClusterParams > ,
287- ) -> Option < ConnectionAndAddress < Connection > > {
283+ fn lookup_route ( & self , route : & Route ) -> Option < ConnectionAndAddress < Connection > > {
288284 let slot_map_value = self . slot_map . slot_value_for_route ( route) ?;
289285 let addrs = & slot_map_value. addrs ;
290286 if addrs. replicas . is_empty ( ) {
291287 return self . connection_for_address ( addrs. primary . as_str ( ) ) ;
292288 }
293289
294- //
295290 match route. slot_addr ( ) {
296291 // Master strategy will be in use when the command is not read_only
297292 SlotAddr :: Master => self . connection_for_address ( addrs. primary . as_str ( ) ) ,
298293 // ReplicaOptional strategy will be in use when the command is read_only
299- SlotAddr :: ReplicaOptional => match self . read_from_replica_strategy {
294+ SlotAddr :: ReplicaOptional => match & self . read_from_replica_strategy {
300295 ReadFromReplicaStrategy :: AlwaysFromPrimary => {
301296 self . connection_for_address ( addrs. primary . as_str ( ) )
302297 }
303298 ReadFromReplicaStrategy :: RoundRobin => {
304299 self . round_robin_read_from_replica ( slot_map_value)
305300 }
306- ReadFromReplicaStrategy :: AZAffinity => self
307- . round_robin_read_from_az_awareness_replica (
308- slot_map_value,
309- cluster_params. as_ref ( ) . unwrap ( ) . client_az . clone ( ) ?,
310- ) ,
301+ ReadFromReplicaStrategy :: AZAffinity ( az) => {
302+ self . round_robin_read_from_az_awareness_replica ( slot_map_value, az. to_string ( ) )
303+ }
311304 } ,
312305 // when the user strategy per command is replica_preffered
313- SlotAddr :: ReplicaRequired => match self . read_from_replica_strategy {
314- ReadFromReplicaStrategy :: AZAffinity => self
315- . round_robin_read_from_az_awareness_replica (
316- slot_map_value,
317- cluster_params. as_ref ( ) . unwrap ( ) . client_az . clone ( ) ?,
318- ) ,
306+ SlotAddr :: ReplicaRequired => match & self . read_from_replica_strategy {
307+ ReadFromReplicaStrategy :: AZAffinity ( az) => {
308+ self . round_robin_read_from_az_awareness_replica ( slot_map_value, az. to_string ( ) )
309+ }
319310 _ => self . round_robin_read_from_replica ( slot_map_value) ,
320311 } ,
321312 }
@@ -325,17 +316,9 @@ where
325316 & self ,
326317 route : & Route ,
327318 ) -> Option < ConnectionAndAddress < Connection > > {
328- self . connection_for_route_with_params ( route, None )
329- }
330-
331- pub ( crate ) fn connection_for_route_with_params (
332- & self ,
333- route : & Route ,
334- cluster_params : Option < ClusterParams > ,
335- ) -> Option < ConnectionAndAddress < Connection > > {
336- self . lookup_route ( route, & cluster_params) . or_else ( || {
319+ self . lookup_route ( route) . or_else ( || {
337320 if route. slot_addr ( ) != SlotAddr :: Master {
338- self . lookup_route ( & Route :: new ( route. slot ( ) , SlotAddr :: Master ) , & cluster_params )
321+ self . lookup_route ( & Route :: new ( route. slot ( ) , SlotAddr :: Master ) )
339322 } else {
340323 None
341324 }
@@ -513,7 +496,7 @@ mod tests {
513496 }
514497
515498 fn create_container_with_az_strategy (
516- strategy : ReadFromReplicaStrategy ,
499+ // strategy: ReadFromReplicaStrategy,
517500 use_management_connections : bool ,
518501 ) -> ConnectionsContainer < usize > {
519502 let slot_map = SlotMap :: new (
@@ -571,7 +554,7 @@ mod tests {
571554 ConnectionsContainer {
572555 slot_map,
573556 connection_map,
574- read_from_replica_strategy : strategy ,
557+ read_from_replica_strategy : ReadFromReplicaStrategy :: AZAffinity ( "use-1a" . to_string ( ) ) ,
575558 topology_hash : 0 ,
576559 }
577560 }
@@ -802,28 +785,17 @@ mod tests {
802785
803786 #[ test]
804787 fn get_connection_for_az_affinity_route ( ) {
805- let container =
806- create_container_with_az_strategy ( ReadFromReplicaStrategy :: AZAffinity , false ) ;
807- let mut cluster_params = ClusterParams :: default ( ) ;
808-
809- cluster_params. client_az = Some ( "use-1a" . to_string ( ) ) ;
788+ let container = create_container_with_az_strategy ( false ) ;
810789
811790 // slot number is not exits
812791 assert ! ( container
813- . connection_for_route_with_params(
814- & Route :: new( 1001 , SlotAddr :: ReplicaOptional ) ,
815- Some ( cluster_params. clone( ) )
816- )
792+ . connection_for_route( & Route :: new( 1001 , SlotAddr :: ReplicaOptional ) )
817793 . is_none( ) ) ;
818-
819794 // Get the replica that holds the slot 1002
820795 assert_eq ! (
821796 21 ,
822797 container
823- . connection_for_route_with_params(
824- & Route :: new( 1002 , SlotAddr :: ReplicaOptional ) ,
825- Some ( cluster_params. clone( ) )
826- )
798+ . connection_for_route( & Route :: new( 1002 , SlotAddr :: ReplicaOptional ) )
827799 . unwrap( )
828800 . 1
829801 ) ;
@@ -832,20 +804,14 @@ mod tests {
832804 assert_eq ! (
833805 2 ,
834806 container
835- . connection_for_route_with_params(
836- & Route :: new( 1500 , SlotAddr :: Master ) ,
837- Some ( cluster_params. clone( ) )
838- )
807+ . connection_for_route( & Route :: new( 1500 , SlotAddr :: Master ) )
839808 . unwrap( )
840809 . 1
841810 ) ;
842811
843812 // receive one of the replicas that holds the slot 2001 and is in the availability zone of the client ("use-1a")
844813 assert ! ( one_of(
845- container. connection_for_route_with_params(
846- & Route :: new( 2001 , SlotAddr :: ReplicaRequired ) ,
847- Some ( cluster_params. clone( ) )
848- ) ,
814+ container. connection_for_route( & Route :: new( 2001 , SlotAddr :: ReplicaRequired ) ) ,
849815 & [ 31 , 33 ] ,
850816 ) ) ;
851817
@@ -854,10 +820,7 @@ mod tests {
854820 assert_eq ! (
855821 31 ,
856822 container
857- . connection_for_route_with_params(
858- & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) ,
859- Some ( cluster_params. clone( ) )
860- )
823+ . connection_for_route( & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) )
861824 . unwrap( )
862825 . 1
863826 ) ;
@@ -867,10 +830,7 @@ mod tests {
867830 assert_eq ! (
868831 32 ,
869832 container
870- . connection_for_route_with_params(
871- & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) ,
872- Some ( cluster_params. clone( ) )
873- )
833+ . connection_for_route( & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) )
874834 . unwrap( )
875835 . 1
876836 ) ;
@@ -880,15 +840,38 @@ mod tests {
880840 assert_eq ! (
881841 3 ,
882842 container
883- . connection_for_route_with_params(
884- & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) ,
885- Some ( cluster_params. clone( ) )
886- )
843+ . connection_for_route( & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) )
887844 . unwrap( )
888845 . 1
889846 ) ;
890847 }
891848
849+ #[ test]
850+ fn get_connection_for_az_affinity_route_round_robin ( ) {
851+ let container = create_container_with_az_strategy ( false ) ;
852+
853+ let mut addresses = vec ! [
854+ container
855+ . connection_for_route( & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) )
856+ . unwrap( )
857+ . 1 ,
858+ container
859+ . connection_for_route( & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) )
860+ . unwrap( )
861+ . 1 ,
862+ container
863+ . connection_for_route( & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) )
864+ . unwrap( )
865+ . 1 ,
866+ container
867+ . connection_for_route( & Route :: new( 2001 , SlotAddr :: ReplicaOptional ) )
868+ . unwrap( )
869+ . 1 ,
870+ ] ;
871+ addresses. sort ( ) ;
872+ assert_eq ! ( addresses, vec![ 31 , 31 , 33 , 33 ] ) ;
873+ }
874+
892875 #[ test]
893876 fn get_connection_by_address ( ) {
894877 let container = create_container ( ) ;
0 commit comments