@@ -30,9 +30,9 @@ macro_rules! count_connections {
3030pub struct ConnectionDetails < Connection > {
3131 /// The actual connection
3232 pub conn : Connection ,
33- /// The IP and AZ associated with the connection
33+ /// The IP associated with the connection
3434 pub ip : Option < IpAddr > ,
35- /// The AZ associated with the connection
35+ /// The availability zone associated with the connection
3636 pub az : Option < String > ,
3737}
3838
@@ -207,6 +207,13 @@ where
207207 Telemetry :: incr_total_connections ( conn_count_after. saturating_sub ( conn_count_before) ) ;
208208 }
209209
210+ /// Returns the availability zone associated with the connection in address
211+ pub ( crate ) fn az_for_address ( & self , address : & str ) -> Option < String > {
212+ self . connection_map
213+ . get ( address)
214+ . map ( |item| item. value ( ) . user_connection . az . clone ( ) ) ?
215+ }
216+
210217 /// Returns true if the address represents a known primary node.
211218 pub ( crate ) fn is_primary ( & self , address : & String ) -> bool {
212219 self . connection_for_address ( address) . is_some ( ) && self . slot_map . is_primary ( address)
@@ -240,7 +247,9 @@ where
240247 }
241248 }
242249
243- pub ( crate ) fn round_robin_read_from_az_awareness_replica (
250+ /// Returns the node's connection in the same availability zone as `client_az` in round robin strategy if exits,
251+ /// if not, will fall back to any available replica or primary.
252+ pub ( crate ) fn round_robin_read_from_replica_with_az_awareness (
244253 & self ,
245254 slot_map_value : & SlotMapValue ,
246255 client_az : String ,
@@ -251,27 +260,21 @@ where
251260
252261 loop {
253262 retries = retries. saturating_add ( 1 ) ;
254- // Looped through all replicas; no connected replica found in the same AZ .
263+ // Looped through all replicas; no connected replica found in the same availability zone .
255264 if retries > addrs. replicas ( ) . len ( ) {
256- // Attempt a fallback to any available replica in other AZs.
257- for replica in & addrs. replicas ( ) {
258- if let Some ( connection) = self . connection_for_address ( replica. as_str ( ) ) {
259- return Some ( connection) ;
260- }
261- }
262- // Fallback to the primary if no replica is connected.
263- return self . connection_for_address ( addrs. primary ( ) . as_str ( ) ) ;
265+ // Attempt a fallback to any available replica or primary if needed.
266+ return self . round_robin_read_from_replica ( slot_map_value) ;
264267 }
265268
266269 // Calculate index based on initial index and check count.
267270 let index = ( initial_index + retries) % addrs. replicas ( ) . len ( ) ;
268271 let replica = & addrs. replicas ( ) [ index] ;
269272
270- // Check if this replica’s AZ matches the user’s AZ .
273+ // Check if this replica’s availability zone matches the user’s availability zone .
271274 if let Some ( ( address, connection_details) ) =
272275 self . connection_details_for_address ( replica. as_str ( ) )
273276 {
274- if connection_details . az . as_deref ( ) == Some ( & client_az) {
277+ if self . az_for_address ( & address ) == Some ( client_az. clone ( ) ) {
275278 // Attempt to update `latest_used_replica` with the index of this replica.
276279 let _ = slot_map_value. last_used_replica . compare_exchange_weak (
277280 initial_index,
@@ -303,15 +306,19 @@ where
303306 ReadFromReplicaStrategy :: RoundRobin => {
304307 self . round_robin_read_from_replica ( slot_map_value)
305308 }
306- ReadFromReplicaStrategy :: AZAffinity ( az) => {
307- self . round_robin_read_from_az_awareness_replica ( slot_map_value, az. to_string ( ) )
308- }
309+ ReadFromReplicaStrategy :: AZAffinity ( az) => self
310+ . round_robin_read_from_replica_with_az_awareness (
311+ slot_map_value,
312+ az. to_string ( ) ,
313+ ) ,
309314 } ,
310315 // when the user strategy per command is replica_preffered
311316 SlotAddr :: ReplicaRequired => match & self . read_from_replica_strategy {
312- ReadFromReplicaStrategy :: AZAffinity ( az) => {
313- self . round_robin_read_from_az_awareness_replica ( slot_map_value, az. to_string ( ) )
314- }
317+ ReadFromReplicaStrategy :: AZAffinity ( az) => self
318+ . round_robin_read_from_replica_with_az_awareness (
319+ slot_map_value,
320+ az. to_string ( ) ,
321+ ) ,
315322 _ => self . round_robin_read_from_replica ( slot_map_value) ,
316323 } ,
317324 }
@@ -502,7 +509,6 @@ mod tests {
502509 }
503510
504511 fn create_container_with_az_strategy (
505- // strategy: ReadFromReplicaStrategy,
506512 use_management_connections : bool ,
507513 ) -> ConnectionsContainer < usize > {
508514 let slot_map = SlotMap :: new (
0 commit comments