1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.policies.errorchecking; 26 27 import java.util.ArrayList ; 28 import java.util.Random ; 29 30 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 31 32 38 public class ErrorCheckingRandom extends ErrorCheckingPolicy 39 { 40 private Random random = new Random (); 41 42 47 public ErrorCheckingRandom(int numberOfNodes) 48 { 49 super(ErrorCheckingPolicy.RANDOM, numberOfNodes); 50 } 51 52 55 public ArrayList getBackends(ArrayList backends) 56 throws ErrorCheckingException 57 { 58 int size = backends.size(); 59 60 if (nbOfNodes == size) 61 return backends; 62 else if (nbOfNodes > size) 63 throw new ErrorCheckingException( 64 "Asking for more backends (" 65 + nbOfNodes 66 + ") than available (" 67 + size 68 + ")"); 69 70 ArrayList result = new ArrayList (nbOfNodes); 71 ArrayList clonedList = new ArrayList (size); 72 for (int i = 0; i < size; i++) 73 { DatabaseBackend db = (DatabaseBackend) backends.get(i); 75 if (db.isReadEnabled() || db.isWriteEnabled()) 76 clonedList.add(db); 77 } 78 79 int clonedSize = clonedList.size(); 80 81 if (nbOfNodes == clonedSize) 82 return backends; 83 else if (nbOfNodes > clonedSize) 84 throw new ErrorCheckingException( 85 "Asking for more backends (" 86 + nbOfNodes 87 + ") than available (" 88 + clonedSize 89 + ")"); 90 91 for (int i = 0; i < nbOfNodes; i++) 93 result.add(clonedList.remove(random.nextInt(clonedSize))); 94 95 return result; 96 } 97 98 101 public String getInformation() 102 { 103 return "Error checking using " + nbOfNodes + " nodes choosen randomly"; 104 } 105 } 106 | Popular Tags |