@@ -137,6 +137,7 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
137137  private  double  curOverallCost  = 0d ;
138138  private  double [] tempFunctionCosts ;
139139  private  double [] curFunctionCosts ;
140+   private  double [] weightOfGenerator ;
140141
141142  // Keep locality based picker and cost function to alert them 
142143  // when new services are offered 
@@ -146,6 +147,10 @@ public class StochasticLoadBalancer extends BaseLoadBalancer {
146147  private  RegionReplicaHostCostFunction  regionReplicaHostCostFunction ;
147148  private  RegionReplicaRackCostFunction  regionReplicaRackCostFunction ;
148149
150+   public  enum  GeneratorType  {
151+     RANDOM , LOAD , LOCALITY , RACK ,
152+   }
153+ 
149154  /** 
150155   * The constructor that pass a MetricsStochasticBalancer to BaseLoadBalancer to replace its 
151156   * default MetricsBalancer 
@@ -204,10 +209,11 @@ protected float getDefaultSlop() {
204209
205210  protected  List <CandidateGenerator > createCandidateGenerators () {
206211    List <CandidateGenerator > candidateGenerators  = new  ArrayList <CandidateGenerator >(4 );
207-     candidateGenerators .add (new  RandomCandidateGenerator ());
208-     candidateGenerators .add (new  LoadCandidateGenerator ());
209-     candidateGenerators .add (localityCandidateGenerator );
210-     candidateGenerators .add (new  RegionReplicaRackCandidateGenerator ());
212+     candidateGenerators .add (GeneratorType .RANDOM .ordinal (), new  RandomCandidateGenerator ());
213+     candidateGenerators .add (GeneratorType .LOAD .ordinal (), new  LoadCandidateGenerator ());
214+     candidateGenerators .add (GeneratorType .LOCALITY .ordinal (), localityCandidateGenerator );
215+     candidateGenerators .add (GeneratorType .RACK .ordinal (),
216+       new  RegionReplicaRackCandidateGenerator ());
211217    return  candidateGenerators ;
212218  }
213219
@@ -393,6 +399,27 @@ BalanceAction nextAction(BalancerClusterState cluster) {
393399      .generate (cluster );
394400  }
395401
402+   private  CandidateGenerator  getRandomGenerate () {
403+     double  sum  = 0 ;
404+     for  (int  i  = 0 ; i  < weightOfGenerator .length ; i ++) {
405+       sum  += weightOfGenerator [i ];
406+       weightOfGenerator [i ] = sum ;
407+     }
408+     if  (sum  == 0 ) {
409+       return  candidateGenerators .get (0 );
410+     }
411+     for  (int  i  = 0 ; i  < weightOfGenerator .length ; i ++) {
412+       weightOfGenerator [i ] /= sum ;
413+     }
414+     double  rand  = ThreadLocalRandom .current ().nextDouble ();
415+     for  (int  i  = 0 ; i  < weightOfGenerator .length ; i ++) {
416+       if  (rand  <= weightOfGenerator [i ]) {
417+         return  candidateGenerators .get (i );
418+       }
419+     }
420+     return  candidateGenerators .get (candidateGenerators .size () - 1 );
421+   }
422+ 
396423  @ RestrictedApi (explanation  = "Should only be called in tests" , link  = "" ,
397424    allowedOnPath  = ".*/src/test/.*" )
398425  void  setRackManager (RackManager  rackManager ) {
@@ -476,6 +503,8 @@ protected List<RegionPlan> balanceTable(TableName tableName, Map<ServerName,
476503    long  step ;
477504
478505    for  (step  = 0 ; step  < computedMaxSteps ; step ++) {
506+       // Initialize the weights of generator every time 
507+       weightOfGenerator  = new  double [this .candidateGenerators .size ()];
479508      BalanceAction  action  = nextAction (cluster );
480509
481510      if  (action .getType () == BalanceAction .Type .NULL ) {
0 commit comments