1 22 23 package org.continuent.sequoia.controller.loadbalancer.raidb2; 24 25 import java.sql.SQLException ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 29 import org.continuent.sequoia.common.exceptions.NoMoreBackendException; 30 import org.continuent.sequoia.common.exceptions.UnreachableBackendException; 31 import org.continuent.sequoia.common.i18n.Translate; 32 import org.continuent.sequoia.common.xml.DatabasesXmlTags; 33 import org.continuent.sequoia.controller.backend.DatabaseBackend; 34 import org.continuent.sequoia.controller.backend.result.ControllerResultSet; 35 import org.continuent.sequoia.controller.backend.result.ExecuteResult; 36 import org.continuent.sequoia.controller.cache.metadata.MetadataCache; 37 import org.continuent.sequoia.controller.loadbalancer.WeightedBalancer; 38 import org.continuent.sequoia.controller.loadbalancer.policies.WaitForCompletionPolicy; 39 import org.continuent.sequoia.controller.loadbalancer.policies.createtable.CreateTablePolicy; 40 import org.continuent.sequoia.controller.requests.SelectRequest; 41 import org.continuent.sequoia.controller.requests.StoredProcedure; 42 import org.continuent.sequoia.controller.virtualdatabase.VirtualDatabase; 43 44 64 public class RAIDb2_WRR extends RAIDb2 65 { 66 private HashMap weights; 67 private int currentWeight; 68 69 72 73 83 public RAIDb2_WRR(VirtualDatabase vdb, 84 WaitForCompletionPolicy waitForCompletionPolicy, 85 CreateTablePolicy createTablePolicy) throws Exception 86 { 87 super(vdb, waitForCompletionPolicy, createTablePolicy); 88 currentWeight = -1; 89 } 90 91 94 95 99 public ControllerResultSet statementExecuteQuery(SelectRequest request, 100 MetadataCache metadataCache) throws SQLException 101 { 102 return null; 103 } 104 105 113 public ControllerResultSet readOnlyCallableStatementExecuteQuery( 114 StoredProcedure proc, MetadataCache metadataCache) throws SQLException 115 { 116 return (ControllerResultSet) callStoredProcedure(proc, 117 CALLABLE_STATEMENT_EXECUTE_QUERY, metadataCache); 118 } 119 120 128 public ExecuteResult readOnlyCallableStatementExecute(StoredProcedure proc, 129 MetadataCache metadataCache) throws SQLException 130 { 131 return (ExecuteResult) callStoredProcedure(proc, 132 CALLABLE_STATEMENT_EXECUTE, metadataCache); 133 } 134 135 147 private Object callStoredProcedure(StoredProcedure proc, int callType, 148 MetadataCache metadataCache) throws SQLException 149 { 150 try 152 { 153 vdb.acquireReadLockBackendLists(); 154 } 155 catch (InterruptedException e) 156 { 157 String msg = Translate.get( 158 "loadbalancer.backendlist.acquire.readlock.failed", e); 159 logger.error(msg); 160 throw new SQLException (msg); 161 } 162 163 DatabaseBackend backend = null; 164 165 try 168 { 169 ArrayList backends = vdb.getBackends(); 170 int size = backends.size(); 171 172 if (size == 0) 173 throw new NoMoreBackendException(Translate.get( 174 "loadbalancer.execute.no.backend.available", proc.getId())); 175 176 int w = 0; for (int i = 0; i < size; i++) 179 { 180 DatabaseBackend b = (DatabaseBackend) backends.get(i); 181 if (b.isReadEnabled() 182 && b.hasStoredProcedure(proc.getProcedureKey(), proc 183 .getNbOfParameters())) 184 { 185 if (backend == null) 186 backend = b; 188 Integer weight = (Integer ) weights.get(b.getName()); 190 if (weight == null) 191 logger.error("No weight defined for backend " + b.getName()); 192 else 193 { 194 int backendWeight = weight.intValue(); 195 if (backendWeight == 0) 196 continue; else 198 w += backendWeight; 199 } 200 201 if (currentWeight <= w) 203 { 204 backend = b; 205 currentWeight++; break; 207 } 208 } 209 } 210 211 if (backend == null) 212 throw new NoMoreBackendException(Translate.get( 213 "loadbalancer.execute.no.backend.enabled", proc.getId())); 214 215 if (currentWeight > w) 219 currentWeight = 1; 220 } 221 catch (RuntimeException e) 222 { 223 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 224 new String []{proc.getSqlShortForm(vdb.getSqlShortFormLength()), 225 e.getMessage()}); 226 logger.error(msg, e); 227 throw new SQLException (msg); 228 } 229 finally 230 { 231 vdb.releaseReadLockBackendLists(); 232 } 233 234 try 236 { 237 switch (callType) 238 { 239 case CALLABLE_STATEMENT_EXECUTE_QUERY : 240 return executeStoredProcedureOnBackend(proc, true, backend, 241 metadataCache); 242 case CALLABLE_STATEMENT_EXECUTE : 243 return executeStoredProcedureOnBackend(proc, false, backend, 244 metadataCache); 245 default : 246 throw new RuntimeException ("Unhandled call type " + callType 247 + " in executeRoundRobin"); 248 } 249 } 250 catch (UnreachableBackendException urbe) 251 { 252 return callStoredProcedure(proc, callType, metadataCache); 254 } 255 catch (SQLException se) 256 { 257 String msg = Translate.get("loadbalancer.something.failed", new String []{ 258 "Stored procedure ", String.valueOf(proc.getId()), se.getMessage()}); 259 if (logger.isInfoEnabled()) 260 logger.info(msg); 261 throw se; 262 } 263 catch (RuntimeException e) 264 { 265 String msg = Translate.get("loadbalancer.something.failed.on", 266 new String []{"Stored procedure ", 267 proc.getSqlShortForm(vdb.getSqlShortFormLength()), 268 backend.getName(), e.getMessage()}); 269 logger.error(msg, e); 270 throw new SQLException (msg); 271 } 272 } 273 274 277 278 282 public void setWeight(String name, int w) throws SQLException 283 { 284 throw new SQLException ("Weight is not supported with this load balancer"); 285 } 286 287 290 291 296 public String getInformation() 297 { 298 if (weights == null) 299 return "RAIDb-2 Weighted Round Robin Request load balancer: !!!Warning!!! No backend nodes found\n"; 300 else 301 return "RAIDb-2 Weighted Round Robin Request load balancer balancing over " 302 + weights.size() + " nodes\n"; 303 } 304 305 308 public String getRaidb2Xml() 309 { 310 return WeightedBalancer.getRaidbXml(weights, 311 DatabasesXmlTags.ELT_RAIDb_2_WeightedRoundRobin); 312 } 313 } | Popular Tags |