@@ -138,8 +138,9 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
138138 private double [] curFunctionCosts ;
139139 private double [] weightsOfGenerators ;
140140
141- // Keep locality based cost function to alert them
141+ // Keep locality based picker and cost function to alert them
142142 // when new services are offered
143+ private LocalityBasedCandidateGenerator localityCandidateGenerator ;
143144 private ServerLocalityCostFunction localityCost ;
144145 private RackLocalityCostFunction rackLocalityCost ;
145146 private RegionReplicaHostCostFunction regionReplicaHostCostFunction ;
@@ -148,20 +149,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
148149 protected List <CandidateGenerator > candidateGenerators ;
149150
150151 public enum GeneratorType {
151- RANDOM (new RandomCandidateGenerator ()),
152- LOAD (new LoadCandidateGenerator ()),
153- LOCALITY (new LocalityBasedCandidateGenerator ()),
154- RACK (new RegionReplicaRackCandidateGenerator ());
155-
156- private final CandidateGenerator generator ;
157-
158- private GeneratorType (CandidateGenerator generator ) {
159- this .generator = generator ;
160- }
161-
162- public CandidateGenerator getGenerator (){
163- return generator ;
164- }
152+ RANDOM , LOAD , LOCALITY , RACK
165153 }
166154
167155 /**
@@ -209,16 +197,24 @@ private void loadCustomCostFunctions(Configuration conf) {
209197 }
210198 }
211199
200+ @ RestrictedApi (explanation = "Should only be called in tests" , link = "" ,
201+ allowedOnPath = ".*/src/test/.*" )
202+ List <CandidateGenerator > getCandidateGenerators () {
203+ return this .candidateGenerators ;
204+ }
205+
212206 @ Override
213207 protected float getDefaultSlop () {
214208 return 0.001f ;
215209 }
216210
217211 protected List <CandidateGenerator > createCandidateGenerators () {
218212 List <CandidateGenerator > candidateGenerators = new ArrayList <CandidateGenerator >(4 );
219- for (GeneratorType generatorType : GeneratorType .values ()) {
220- candidateGenerators .add (generatorType .getGenerator ());
221- }
213+ candidateGenerators .add (GeneratorType .RANDOM .ordinal (), new RandomCandidateGenerator ());
214+ candidateGenerators .add (GeneratorType .LOAD .ordinal (), new LoadCandidateGenerator ());
215+ candidateGenerators .add (GeneratorType .LOCALITY .ordinal (), localityCandidateGenerator );
216+ candidateGenerators .add (GeneratorType .RACK .ordinal (),
217+ new RegionReplicaRackCandidateGenerator ());
222218 return candidateGenerators ;
223219 }
224220
@@ -232,6 +228,7 @@ protected void loadConf(Configuration conf) {
232228
233229 numRegionLoadsToRemember = conf .getInt (KEEP_REGION_LOADS , numRegionLoadsToRemember );
234230 minCostNeedBalance = conf .getFloat (MIN_COST_NEED_BALANCE_KEY , minCostNeedBalance );
231+ localityCandidateGenerator = new LocalityBasedCandidateGenerator ();
235232 localityCost = new ServerLocalityCostFunction (conf );
236233 rackLocalityCost = new RackLocalityCostFunction (conf );
237234
0 commit comments