1 23 24 package org.continuent.sequoia.controller.loadbalancer.policies.createtable; 25 26 import java.util.ArrayList ; 27 import java.util.Random ; 28 29 37 public class CreateTableRandom extends CreateTableRule 38 { 39 private Random random = new Random (); 40 41 44 public CreateTableRandom() 45 { 46 super(CreateTablePolicy.RANDOM); 47 } 48 49 54 public CreateTableRandom(ArrayList backendList) 55 { 56 super(CreateTablePolicy.RANDOM, backendList); 57 } 58 59 62 public ArrayList getBackends(ArrayList backends) throws CreateTableException 63 { 64 if (nbOfNodes == 0) 65 return null; 66 67 ArrayList clonedList = super.getBackends(backends); 68 69 int clonedSize = clonedList.size(); 70 71 if (nbOfNodes == clonedSize) 72 return clonedList; 73 else if (nbOfNodes > clonedSize) 74 throw new CreateTableException( 75 "Asking for more backends (" 76 + nbOfNodes 77 + ") than available (" 78 + clonedSize 79 + ")"); 80 81 ArrayList result = new ArrayList (nbOfNodes); 82 83 for (int i = 0; i < nbOfNodes; i++) 84 result.add(clonedList.remove(random.nextInt(clonedSize - i))); 85 86 return result; 87 } 88 89 92 public String getInformation() 93 { 94 String s; 95 if (tableName == null) 96 s = "Default rule create table on "; 97 else 98 s = "Rule for table " + tableName + " create table on "; 99 100 return s + nbOfNodes + " node(s) randomly from " + backendList; 101 } 102 } 103 | Popular Tags |