1 23 24 package org.continuent.sequoia.controller.loadbalancer.raidb1; 25 26 import java.sql.SQLException ; 27 import java.util.ArrayList ; 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.policies.WaitForCompletionPolicy; 38 import org.continuent.sequoia.controller.requests.AbstractRequest; 39 import org.continuent.sequoia.controller.requests.SelectRequest; 40 import org.continuent.sequoia.controller.requests.StoredProcedure; 41 import org.continuent.sequoia.controller.virtualdatabase.VirtualDatabase; 42 43 54 public class RAIDb1_LPRF extends RAIDb1 55 { 56 63 66 67 75 public RAIDb1_LPRF(VirtualDatabase vdb, 76 WaitForCompletionPolicy waitForCompletionPolicy) throws Exception 77 { 78 super(vdb, waitForCompletionPolicy); 79 } 80 81 84 85 93 public ControllerResultSet execSingleBackendReadRequest( 94 SelectRequest request, MetadataCache metadataCache) throws SQLException 95 { 96 return (ControllerResultSet) executeLPRF(request, STATEMENT_EXECUTE_QUERY, 97 "Request ", metadataCache); 98 } 99 100 108 public ControllerResultSet readOnlyCallableStatementExecuteQuery( 109 StoredProcedure proc, MetadataCache metadataCache) throws SQLException 110 { 111 return (ControllerResultSet) executeLPRF(proc, 112 CALLABLE_STATEMENT_EXECUTE_QUERY, "Stored procedure ", metadataCache); 113 } 114 115 123 public ExecuteResult readOnlyCallableStatementExecute(StoredProcedure proc, 124 MetadataCache metadataCache) throws SQLException 125 { 126 return (ExecuteResult) executeLPRF(proc, CALLABLE_STATEMENT_EXECUTE, 127 "Stored procedure ", metadataCache); 128 } 129 130 145 private Object executeLPRF(AbstractRequest request, int callType, 146 String errorMsgPrefix, MetadataCache metadataCache) throws SQLException 147 { 148 try 150 { 151 vdb.acquireReadLockBackendLists(); 152 } 153 catch (InterruptedException e) 154 { 155 String msg = Translate.get( 156 "loadbalancer.backendlist.acquire.readlock.failed", e); 157 logger.error(msg); 158 throw new SQLException (msg); 159 } 160 161 164 DatabaseBackend backend = null; 165 166 try 169 { 170 ArrayList backends = vdb.getBackends(); 171 int size = backends.size(); 172 173 int leastRequests = 0; 175 for (int i = 0; i < size; i++) 176 { 177 DatabaseBackend b = (DatabaseBackend) backends.get(i); 178 if (b.isReadEnabled()) 179 { 180 int pending = b.getPendingRequests().size(); 181 if ((backend == null) || (pending < leastRequests)) 182 { 183 backend = b; 184 if (pending == 0) 185 break; else 187 leastRequests = pending; 188 } 189 } 190 } 191 192 } 193 catch (Throwable e) 194 { 195 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 196 new String []{request.getSqlShortForm(vdb.getSqlShortFormLength()), 197 e.getMessage()}); 198 logger.error(msg, e); 199 throw new SQLException (msg); 200 } 201 finally 202 { 203 vdb.releaseReadLockBackendLists(); 204 } 205 206 if (backend == null) 207 throw new NoMoreBackendException(Translate.get( 208 "loadbalancer.execute.no.backend.enabled", request.getId())); 209 210 try 212 { 213 switch (callType) 214 { 215 case STATEMENT_EXECUTE_QUERY : 216 return executeRequestOnBackend((SelectRequest) request, backend, 217 metadataCache); 218 case CALLABLE_STATEMENT_EXECUTE_QUERY : 219 return executeStoredProcedureOnBackend((StoredProcedure) request, 220 true, backend, metadataCache); 221 case CALLABLE_STATEMENT_EXECUTE : 222 return executeStoredProcedureOnBackend((StoredProcedure) request, 223 false, backend, metadataCache); 224 default : 225 throw new RuntimeException ("Unhandled call type " + callType 226 + " in executeLPRF"); 227 } 228 } 229 catch (UnreachableBackendException urbe) 230 { 231 return executeLPRF(request, callType, errorMsgPrefix, metadataCache); 233 } 234 catch (SQLException se) 235 { 236 String msg = Translate.get("loadbalancer.something.failed", new String []{ 237 errorMsgPrefix, String.valueOf(request.getId()), se.getMessage()}); 238 if (logger.isInfoEnabled()) 239 logger.info(msg); 240 throw se; 241 } 242 catch (RuntimeException e) 243 { 244 String msg = Translate.get("loadbalancer.something.failed.on", 245 new String []{errorMsgPrefix, 246 request.getSqlShortForm(vdb.getSqlShortFormLength()), 247 backend.getName(), e.getMessage()}); 248 logger.error(msg, e); 249 throw new SQLException (msg); 250 } 251 } 252 253 256 257 262 public String getInformation() 263 { 264 int size = vdb.getBackends().size(); 266 267 if (size == 0) 268 return "RAIDb-1 Least Pending Request First load balancer: !!!Warning!!! No backend nodes found\n"; 269 else 270 return "RAIDb-1 Least Pending Request First load balancer (" + size 271 + " backends)\n"; 272 } 273 274 277 public String getRaidb1Xml() 278 { 279 return "<" + DatabasesXmlTags.ELT_RAIDb_1_LeastPendingRequestsFirst + "/>"; 280 } 281 282 } 283 | Popular Tags |