1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.raidb1; 26 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 30 import org.objectweb.cjdbc.common.exceptions.NoMoreBackendException; 31 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 32 import org.objectweb.cjdbc.common.i18n.Translate; 33 import org.objectweb.cjdbc.common.sql.AbstractRequest; 34 import org.objectweb.cjdbc.common.sql.SelectRequest; 35 import org.objectweb.cjdbc.common.sql.StoredProcedure; 36 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 37 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 38 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache; 39 import org.objectweb.cjdbc.controller.loadbalancer.policies.WaitForCompletionPolicy; 40 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet; 41 import org.objectweb.cjdbc.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 execReadRequest(SelectRequest request, 94 MetadataCache metadataCache) throws SQLException 95 { 96 return executeLPRF(request, true, "Request ", metadataCache); 97 } 98 99 107 public ControllerResultSet execReadOnlyReadStoredProcedure( 108 StoredProcedure proc, MetadataCache metadataCache) throws SQLException 109 { 110 return executeLPRF(proc, false, "Stored procedure ", metadataCache); 111 } 112 113 127 private ControllerResultSet executeLPRF(AbstractRequest request, 128 boolean isSelect, String errorMsgPrefix, MetadataCache metadataCache) 129 throws SQLException 130 { 131 try 133 { 134 vdb.acquireReadLockBackendLists(); 135 } 136 catch (InterruptedException e) 137 { 138 String msg = Translate.get( 139 "loadbalancer.backendlist.acquire.readlock.failed", e); 140 logger.error(msg); 141 throw new SQLException (msg); 142 } 143 144 DatabaseBackend backend = null; 152 try 155 { 156 ArrayList backends = vdb.getBackends(); 157 int size = backends.size(); 158 159 if (size == 0) 160 throw new SQLException (Translate.get( 161 "loadbalancer.execute.no.backend.available", request.getId())); 162 163 int leastRequests = 0; 165 for (int i = 0; i < size; i++) 166 { 167 DatabaseBackend b = (DatabaseBackend) backends.get(i); 168 if (b.isReadEnabled()) 169 { 170 int pending = b.getPendingRequests().size(); 171 if ((backend == null) || (pending < leastRequests)) 172 { 173 backend = b; 174 if (pending == 0) 175 break; else 177 leastRequests = pending; 178 } 179 } 180 } 181 182 if (backend == null) 183 throw new NoMoreBackendException(Translate.get( 184 "loadbalancer.execute.no.backend.enabled", request.getId())); 185 } 186 catch (RuntimeException e) 187 { 188 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 189 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 190 e.getMessage()}); 191 logger.error(msg, e); 192 throw new SQLException (msg); 193 } 194 finally 195 { 196 vdb.releaseReadLockBackendLists(); 197 } 198 199 ControllerResultSet rs = null; 200 try 202 { 203 if (isSelect) 204 rs = executeRequestOnBackend((SelectRequest) request, backend, 205 metadataCache); 206 else 207 rs = executeStoredProcedureOnBackend((StoredProcedure) request, 208 backend, metadataCache); 209 } 210 catch (UnreachableBackendException urbe) 211 { 212 return executeLPRF(request, isSelect, errorMsgPrefix, metadataCache); 214 } 215 catch (SQLException se) 216 { 217 String msg = Translate.get("loadbalancer.something.failed", new String []{ 218 errorMsgPrefix, String.valueOf(request.getId()), se.getMessage()}); 219 if (logger.isInfoEnabled()) 220 logger.info(msg); 221 throw se; 222 } 223 catch (RuntimeException e) 224 { 225 String msg = Translate.get("loadbalancer.something.failed.on", 226 new String []{errorMsgPrefix, 227 request.getSQLShortForm(vdb.getSQLShortFormLength()), 228 backend.getName(), e.getMessage()}); 229 logger.error(msg, e); 230 throw new SQLException (msg); 231 } 232 233 return rs; 234 } 235 236 239 240 245 public String getInformation() 246 { 247 int size = vdb.getBackends().size(); 249 250 if (size == 0) 251 return "RAIDb-1 Least Pending Request First load balancer: !!!Warning!!! No backend nodes found\n"; 252 else 253 return "RAIDb-1 Least Pending Request First load balancer (" + size 254 + " backends)\n"; 255 } 256 257 260 public String getRaidb1Xml() 261 { 262 return "<" + DatabasesXmlTags.ELT_RAIDb_1_LeastPendingRequestsFirst + "/>"; 263 } 264 265 } 266 | Popular Tags |