1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.raidb1; 26 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 31 import org.objectweb.cjdbc.common.exceptions.NoMoreBackendException; 32 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 33 import org.objectweb.cjdbc.common.i18n.Translate; 34 import org.objectweb.cjdbc.common.sql.AbstractRequest; 35 import org.objectweb.cjdbc.common.sql.SelectRequest; 36 import org.objectweb.cjdbc.common.sql.StoredProcedure; 37 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 38 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 39 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache; 40 import org.objectweb.cjdbc.controller.loadbalancer.WeightedBalancer; 41 import org.objectweb.cjdbc.controller.loadbalancer.policies.WaitForCompletionPolicy; 42 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet; 43 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase; 44 45 57 public class RAIDb1_WRR extends RAIDb1 58 { 59 63 64 private HashMap weights = new HashMap (); 65 private int index; 68 70 73 74 82 public RAIDb1_WRR(VirtualDatabase vdb, 83 WaitForCompletionPolicy waitForCompletionPolicy) throws Exception 84 { 85 super(vdb, waitForCompletionPolicy); 86 index = -1; 87 } 88 89 92 93 100 public ControllerResultSet execReadRequest(SelectRequest request, 101 MetadataCache metadataCache) throws SQLException 102 { 103 return executeWRR(request, true, "Request ", metadataCache); 104 } 105 106 114 public ControllerResultSet execReadOnlyReadStoredProcedure( 115 StoredProcedure proc, MetadataCache metadataCache) throws SQLException 116 { 117 return executeWRR(proc, false, "Stored procedure ", metadataCache); 118 } 119 120 134 private ControllerResultSet executeWRR(AbstractRequest request, 135 boolean isSelect, String errorMsgPrefix, MetadataCache metadataCache) 136 throws SQLException 137 { 138 try 140 { 141 vdb.acquireReadLockBackendLists(); 142 } 143 catch (InterruptedException e) 144 { 145 String msg = Translate.get( 146 "loadbalancer.backendlist.acquire.readlock.failed", e); 147 logger.error(msg); 148 throw new SQLException (msg); 149 } 150 151 DatabaseBackend backend = null; 152 153 try 156 { 157 ArrayList backends = vdb.getBackends(); 158 int size = backends.size(); 159 160 if (size == 0) 161 throw new SQLException (Translate.get( 162 "loadbalancer.execute.no.backend.available", request.getId())); 163 164 int w = 0; for (int i = 0; i < size; i++) 167 { 168 DatabaseBackend b = (DatabaseBackend) backends.get(index); 169 if (b.isReadEnabled()) 170 { 171 if (backend == null) 172 backend = b; 174 Integer weight = (Integer ) weights.get(b.getName()); 176 if (weight == null) 177 logger.error("No weight defined for backend " + b.getName()); 178 else 179 w += weight.intValue(); 180 181 if (index <= w) 183 { 184 backend = b; 185 index++; break; 187 } 188 } 189 } 190 191 if (backend == null) 192 throw new NoMoreBackendException(Translate.get( 193 "loadbalancer.execute.no.backend.enabled", request.getId())); 194 195 if (index > w) 199 index = 1; 200 } 201 catch (RuntimeException e) 202 { 203 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 204 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 205 e.getMessage()}); 206 logger.error(msg, e); 207 throw new SQLException (msg); 208 } 209 finally 210 { 211 vdb.releaseReadLockBackendLists(); 212 } 213 214 ControllerResultSet rs = null; 215 try 217 { 218 if (isSelect) 219 rs = executeRequestOnBackend((SelectRequest) request, backend, 220 metadataCache); 221 else 222 rs = executeStoredProcedureOnBackend((StoredProcedure) request, 223 backend, metadataCache); 224 } 225 catch (UnreachableBackendException urbe) 226 { 227 return executeWRR(request, isSelect, errorMsgPrefix, metadataCache); 229 } 230 catch (SQLException se) 231 { 232 String msg = Translate.get("loadbalancer.something.failed", new String []{ 233 errorMsgPrefix, String.valueOf(request.getId()), se.getMessage()}); 234 if (logger.isInfoEnabled()) 235 logger.info(msg); 236 throw se; 237 } 238 catch (RuntimeException e) 239 { 240 String msg = Translate.get("loadbalancer.something.failed.on", 241 new String []{errorMsgPrefix, 242 request.getSQLShortForm(vdb.getSQLShortFormLength()), 243 backend.getName(), e.getMessage()}); 244 logger.error(msg, e); 245 throw new SQLException (msg); 246 } 247 248 return rs; 249 } 250 251 254 255 259 public void setWeight(String name, int w) throws SQLException 260 { 261 if (logger.isDebugEnabled()) 262 logger.debug(Translate.get("loadbalancer.weight.set", new String []{ 263 String.valueOf(w), name})); 264 265 weights.put(name, new Integer (w)); 266 } 267 268 271 272 277 public String getInformation() 278 { 279 int size = vdb.getBackends().size(); 281 282 if (size == 0) 283 return "RAIDb-1 with Weighted Round Robin Request load balancer: !!!Warning!!! No backend nodes found\n"; 284 else 285 return "RAIDb-1 Weighted Round-Robin Request load balancer (" + size 286 + " backends)\n"; 287 } 288 289 292 public String getRaidb1Xml() 293 { 294 return WeightedBalancer.getRaidbXml(weights, 295 DatabasesXmlTags.ELT_RAIDb_1_WeightedRoundRobin); 296 } 297 298 } 299 | Popular Tags |