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 53 public class RAIDb1_RR extends RAIDb1 54 { 55 59 60 private int index; 62 65 66 74 public RAIDb1_RR(VirtualDatabase vdb, 75 WaitForCompletionPolicy waitForCompletionPolicy) throws Exception 76 { 77 super(vdb, waitForCompletionPolicy); 78 index = -1; 79 } 80 81 84 85 92 public ControllerResultSet execReadRequest(SelectRequest request, 93 MetadataCache metadataCache) throws SQLException 94 { 95 return executeRoundRobinRequest(request, true, "Request ", metadataCache); 96 } 97 98 105 public ControllerResultSet execReadOnlyReadStoredProcedure( 106 StoredProcedure proc, MetadataCache metadataCache) throws SQLException 107 { 108 return executeRoundRobinRequest(proc, false, "Stored procedure ", 109 metadataCache); 110 } 111 112 126 private ControllerResultSet executeRoundRobinRequest(AbstractRequest request, 127 boolean isSelect, String errorMsgPrefix, MetadataCache metadataCache) 128 throws SQLException 129 { 130 try 132 { 133 vdb.acquireReadLockBackendLists(); 134 } 135 catch (InterruptedException e) 136 { 137 String msg = Translate.get( 138 "loadbalancer.backendlist.acquire.readlock.failed", e); 139 logger.error(msg); 140 throw new SQLException (msg); 141 } 142 143 DatabaseBackend backend = null; 151 try 154 { 155 ArrayList backends = vdb.getBackends(); 156 int size = backends.size(); 157 158 if (size == 0) 159 throw new SQLException (Translate.get( 160 "loadbalancer.execute.no.backend.available", request.getId())); 161 162 int maxTries = size; 164 synchronized (this) 165 { 166 do 167 { 168 index = (index + 1) % size; 169 backend = (DatabaseBackend) backends.get(index); 170 maxTries--; 171 } 172 while ((!backend.isReadEnabled() && maxTries >= 0)); 173 } 174 175 if (maxTries < 0) 176 throw new NoMoreBackendException(Translate.get( 177 "loadbalancer.execute.no.backend.enabled", request.getId())); 178 } 179 catch (RuntimeException e) 180 { 181 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 182 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 183 e.getMessage()}); 184 logger.error(msg, e); 185 throw new SQLException (msg); 186 } 187 finally 188 { 189 vdb.releaseReadLockBackendLists(); 190 } 191 192 ControllerResultSet rs = null; 193 try 195 { 196 if (isSelect) 197 rs = executeRequestOnBackend((SelectRequest) request, backend, 198 metadataCache); 199 else 200 rs = executeStoredProcedureOnBackend((StoredProcedure) request, 201 backend, metadataCache); 202 } 203 catch (UnreachableBackendException urbe) 204 { 205 return executeRoundRobinRequest(request, isSelect, errorMsgPrefix, 207 metadataCache); 208 } 209 catch (SQLException se) 210 { 211 String msg = Translate.get("loadbalancer.something.failed", new String []{ 212 errorMsgPrefix, String.valueOf(request.getId()), se.getMessage()}); 213 if (logger.isInfoEnabled()) 214 logger.info(msg); 215 throw se; 216 } 217 catch (RuntimeException e) 218 { 219 String msg = Translate.get("loadbalancer.something.failed.on", 220 new String []{errorMsgPrefix, 221 request.getSQLShortForm(vdb.getSQLShortFormLength()), 222 backend.getName(), e.getMessage()}); 223 logger.error(msg, e); 224 throw new SQLException (msg); 225 } 226 227 return rs; 228 } 229 230 233 234 239 public String getInformation() 240 { 241 int size = vdb.getBackends().size(); 243 244 if (size == 0) 245 return "RAIDb-1 Round-Robin Request load balancer: !!!Warning!!! No backend nodes found\n"; 246 else 247 return "RAIDb-1 Round-Robin Request load balancer (" + size 248 + " backends)\n"; 249 } 250 251 254 public String getRaidb1Xml() 255 { 256 return "<" + DatabasesXmlTags.ELT_RAIDb_1_RoundRobin + "/>"; 257 } 258 259 } | Popular Tags |