@@ -636,8 +636,7 @@ impl Default for DefaultPolicy {
636636/// # }
637637#[ derive( Clone , Debug ) ]
638638pub struct DefaultPolicyBuilder {
639- preferred_datacenter : Option < String > ,
640- preferred_rack : Option < String > ,
639+ preferences : ReplicaLocationPreference ,
641640 is_token_aware : bool ,
642641 permit_dc_failover : bool ,
643642 latency_awareness : Option < LatencyAwarenessBuilder > ,
@@ -648,8 +647,7 @@ impl DefaultPolicyBuilder {
648647 /// Creates a builder used to customise configuration of a new DefaultPolicy.
649648 pub fn new ( ) -> Self {
650649 Self {
651- preferred_datacenter : None ,
652- preferred_rack : None ,
650+ preferences : ReplicaLocationPreference :: Any ,
653651 is_token_aware : true ,
654652 permit_dc_failover : false ,
655653 latency_awareness : None ,
@@ -668,24 +666,8 @@ impl DefaultPolicyBuilder {
668666 Box :: new ( DefaultPolicy :: is_alive)
669667 } ;
670668
671- // As the case of providing preferred rack without providing datacenter is invalid, the rack is then ignored.
672- // According to the principle “Make illegal states unrepresentable”, in the next major release we will
673- // alter the `DefaultPolicyBuilder`'s API so that it is impossible for the user to create such state.
674- let preferences = match ( self . preferred_datacenter , self . preferred_rack ) {
675- ( None , None ) => ReplicaLocationPreference :: Any ,
676- ( None , Some ( _) ) => {
677- // This a is case that the user shouldn't be able to represent.
678- warn ! ( "Preferred rack has effect only if a preferred datacenter is set as well. Ignoring the preferred rack." ) ;
679- ReplicaLocationPreference :: Any
680- }
681- ( Some ( datacenter) , None ) => ReplicaLocationPreference :: Datacenter ( datacenter) ,
682- ( Some ( datacenter) , Some ( rack) ) => {
683- ReplicaLocationPreference :: DatacenterAndRack ( datacenter, rack)
684- }
685- } ;
686-
687669 Arc :: new ( DefaultPolicy {
688- preferences,
670+ preferences : self . preferences ,
689671 is_token_aware : self . is_token_aware ,
690672 permit_dc_failover : self . permit_dc_failover ,
691673 pick_predicate,
@@ -694,22 +676,28 @@ impl DefaultPolicyBuilder {
694676 } )
695677 }
696678
697- /// Sets the rack to be preferred by this policy
679+ /// Sets the datacenter to be preferred by this policy.
698680 ///
699- /// Allows the load balancing policy to prioritize nodes based on their availability zones
700- /// in the preferred datacenter.
701- /// When a preferred rack is set, the policy will first return replicas in the local rack
702- /// in the preferred datacenter, and then the other replicas in the datacenter.
681+ /// Allows the load balancing policy to prioritize nodes based on their location.
682+ /// When a preferred datacenter is set, the policy will treat nodes in that
683+ /// datacenter as "local" nodes, and nodes in other datacenters as "remote" nodes.
684+ /// This affects the order in which nodes are returned by the policy when
685+ /// selecting replicas for read or write operations. If no preferred datacenter
686+ /// is specified, the policy will treat all nodes as local nodes.
703687 ///
704- /// When a preferred datacenter is not set, setting preferred rack will not have any effect.
705- pub fn prefer_rack ( mut self , rack_name : String ) -> Self {
706- self . preferred_rack = Some ( rack_name) ;
688+ /// When datacenter failover is disabled (`permit_dc_failover` is set to false),
689+ /// the default policy will only include local nodes in load balancing plans.
690+ /// Remote nodes will be excluded, even if they are alive and available
691+ /// to serve requests.
692+ pub fn prefer_datacenter ( mut self , datacenter_name : String ) -> Self {
693+ self . preferences = ReplicaLocationPreference :: Datacenter ( datacenter_name) ;
707694 self
708695 }
709696
710- /// Sets the datacenter to be preferred by this policy.
697+ /// Sets the datacenter and rack to be preferred by this policy.
711698 ///
712- /// Allows the load balancing policy to prioritize nodes based on their location.
699+ /// Allows the load balancing policy to prioritize nodes based on their location
700+ /// as well as their availability zones in the preferred datacenter.
713701 /// When a preferred datacenter is set, the policy will treat nodes in that
714702 /// datacenter as "local" nodes, and nodes in other datacenters as "remote" nodes.
715703 /// This affects the order in which nodes are returned by the policy when
@@ -720,8 +708,15 @@ impl DefaultPolicyBuilder {
720708 /// the default policy will only include local nodes in load balancing plans.
721709 /// Remote nodes will be excluded, even if they are alive and available
722710 /// to serve requests.
723- pub fn prefer_datacenter ( mut self , datacenter_name : String ) -> Self {
724- self . preferred_datacenter = Some ( datacenter_name) ;
711+ ///
712+ /// When a preferred rack is set, the policy will first return replicas in the local rack
713+ /// in the preferred datacenter, and then the other replicas in the datacenter.
714+ pub fn prefer_datacenter_and_rack (
715+ mut self ,
716+ datacenter_name : String ,
717+ rack_name : String ,
718+ ) -> Self {
719+ self . preferences = ReplicaLocationPreference :: DatacenterAndRack ( datacenter_name, rack_name) ;
725720 self
726721 }
727722
@@ -740,7 +735,7 @@ impl DefaultPolicyBuilder {
740735 /// In the case of `DefaultPolicy`, token awareness is enabled by default,
741736 /// meaning that the policy will prefer to return alive local replicas
742737 /// if the token is available. This means that if the client is requesting data
743- /// that falls within the token range of a particular node, the policy will try\
738+ /// that falls within the token range of a particular node, the policy will try
744739 /// to route the request to that node first, assuming it is alive and responsive.
745740 ///
746741 /// Token awareness can significantly improve the performance and scalability
0 commit comments