1 19 package org.objectweb.carol.cmi; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 25 public class RoundRobin extends StubLB { 26 private ClusterStubData csd; 27 private int len; 28 private StubData[] sd; 29 private double[] load; 30 private double minLoad; 31 32 36 public RoundRobin(ClusterStubData csd, Collection c) { 37 this.csd = csd; 38 len = c.size(); 39 sd = new StubData[len]; 40 load = new double[len]; 41 Iterator it = c.iterator(); 42 for (int i = 0; i < len; i++) { 43 StubData s = (StubData) it.next(); 44 sd[i] = s; 45 } 46 47 49 for (int i = 0; i<SecureRandom.getInt(len); i++) { 50 load[i] = sd[i].getLoadIncr(); 51 } 52 } 53 54 private synchronized void ensureCapacity(int minCapacity) { 55 int old = sd.length; 56 if (old >= minCapacity) 57 return; 58 int l = (old * 3) / 2 + 1; 59 if (l < minCapacity) 60 l = minCapacity; 61 StubData[] nsd = new StubData[l]; 62 double[] nload = new double[l]; 63 System.arraycopy(sd, 0, nsd, 0, old); 64 System.arraycopy(load, 0, nload, 0, old); 65 sd = nsd; 66 load = nload; 67 } 68 69 74 synchronized void add(StubData sd) { 75 ensureCapacity(len + 1); 76 this.sd[len] = sd; 77 load[len] = minLoad; 78 len++; 79 } 80 81 86 synchronized void removeCallback(StubData s) { 87 for (int i=0; i<len; i++) { 88 if (sd[i] == s) { 89 len--; 90 sd[i] = sd[len]; 91 sd[len] = null; 92 load[i] = load[len]; 93 return; 94 } 95 } 96 } 97 98 private static StubLBFilter emptyFilter = new StubLBFilter(); 99 100 public synchronized StubData get() throws NoMoreStubException { 101 return get(emptyFilter); 102 } 103 104 public synchronized StubData get(StubLBFilter f) throws NoMoreStubException { 105 double min = Double.MAX_VALUE; 106 double minOk = Double.MAX_VALUE; 107 int index = -1; 108 for (int i=0; i<len; i++) { 109 double l = load[i]; 110 if (l < minOk) { 111 if (!f.contains(sd[i])) { 112 minOk = l; 113 index = i; 114 } 115 if (l < min) { 116 min = l; 117 } 118 } 119 } 120 121 if (index < 0) { 122 throw new NoMoreStubException(); 123 } 124 125 if (min >= 100.0) { 127 for (int i=0; i<len; i++) { 128 load[i] -= min; 129 } 130 min = 0; 131 } 132 133 StubData s = sd[index]; 134 load[index] += s.getLoadIncr(); 135 return s; 136 } 137 138 141 public void remove(StubData s) { 142 csd.removeStubData(s); 143 } 144 } 145 | Popular Tags |