src/org/objectweb/cjdbc/controller/loadbalancer/raidb1/RAIDb1_RR.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.controller.loadbalancer.raidb1; 00026 00027 import java.sql.SQLException; 00028 import java.util.ArrayList; 00029 00030 import org.objectweb.cjdbc.common.exceptions.NoMoreBackendException; 00031 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 00032 import org.objectweb.cjdbc.common.i18n.Translate; 00033 import org.objectweb.cjdbc.common.sql.AbstractRequest; 00034 import org.objectweb.cjdbc.common.sql.SelectRequest; 00035 import org.objectweb.cjdbc.common.sql.StoredProcedure; 00036 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 00037 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 00038 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache; 00039 import org.objectweb.cjdbc.controller.loadbalancer.policies.WaitForCompletionPolicy; 00040 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet; 00041 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase; 00042 00053 public class RAIDb1_RR extends RAIDb1 00054 { 00055 /* 00056 * How the code is organized ? 1. Member variables 2. Constructor(s) 3. 00057 * Request handling 4. Debug/Monitoring 00058 */ 00059 00060 private int index; // index in the backend vector the Round-Robin 00061 00062 /* 00063 * Constructors 00064 */ 00065 00074 public RAIDb1_RR(VirtualDatabase vdb, 00075 WaitForCompletionPolicy waitForCompletionPolicy) throws SQLException 00076 { 00077 super(vdb, waitForCompletionPolicy); 00078 index = -1; 00079 } 00080 00081 /* 00082 * Request Handling 00083 */ 00084 00092 public ControllerResultSet execReadRequest(SelectRequest request, 00093 MetadataCache metadataCache) throws SQLException 00094 { 00095 return executeRoundRobinRequest(request, true, "Request ", metadataCache); 00096 } 00097 00105 public ControllerResultSet execReadOnlyReadStoredProcedure( 00106 StoredProcedure proc, MetadataCache metadataCache) throws SQLException 00107 { 00108 return executeRoundRobinRequest(proc, false, "Stored procedure ", 00109 metadataCache); 00110 } 00111 00126 private ControllerResultSet executeRoundRobinRequest(AbstractRequest request, 00127 boolean isSelect, String errorMsgPrefix, MetadataCache metadataCache) 00128 throws SQLException 00129 { 00130 // Choose a backend 00131 try 00132 { 00133 vdb.acquireReadLockBackendLists(); 00134 } 00135 catch (InterruptedException e) 00136 { 00137 String msg = Translate.get( 00138 "loadbalancer.backendlist.acquire.readlock.failed", e); 00139 logger.error(msg); 00140 throw new SQLException(msg); 00141 } 00142 00143 DatabaseBackend backend = null; // The backend that will execute the query 00144 00145 // Note that vdb lock is released in the finally clause of this try/catch 00146 // block 00147 try 00148 { 00149 ArrayList backends = vdb.getBackends(); 00150 int size = backends.size(); 00151 00152 if (size == 0) 00153 throw new SQLException(Translate.get( 00154 "loadbalancer.execute.no.backend.available", request.getId())); 00155 00156 // Take the next backend 00157 int maxTries = size; 00158 synchronized (this) 00159 { 00160 do 00161 { 00162 index = (index + 1) % size; 00163 backend = (DatabaseBackend) backends.get(index); 00164 maxTries--; 00165 } 00166 while ((!backend.isReadEnabled() && maxTries >= 0)); 00167 } 00168 00169 if (maxTries < 0) 00170 throw new NoMoreBackendException(Translate.get( 00171 "loadbalancer.execute.no.backend.enabled", request.getId())); 00172 } 00173 catch (RuntimeException e) 00174 { 00175 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 00176 new String[]{request.getSQLShortForm(vdb.getSQLShortFormLength()), 00177 e.getMessage()}); 00178 logger.error(msg, e); 00179 throw new SQLException(msg); 00180 } 00181 finally 00182 { 00183 vdb.releaseReadLockBackendLists(); 00184 } 00185 00186 ControllerResultSet rs = null; 00187 // Execute the request on the chosen backend 00188 try 00189 { 00190 if (isSelect) 00191 rs = executeRequestOnBackend((SelectRequest) request, backend, 00192 metadataCache); 00193 else 00194 rs = executeStoredProcedureOnBackend((StoredProcedure) request, 00195 backend, metadataCache); 00196 } 00197 catch (UnreachableBackendException urbe) 00198 { 00199 // Try to execute query on different backend 00200 return executeRoundRobinRequest(request, isSelect, errorMsgPrefix, 00201 metadataCache); 00202 } 00203 catch (SQLException se) 00204 { 00205 String msg = Translate.get("loadbalancer.something.failed", new String[]{ 00206 errorMsgPrefix, String.valueOf(request.getId()), se.getMessage()}); 00207 if (logger.isInfoEnabled()) 00208 logger.info(msg); 00209 throw new SQLException(msg); 00210 } 00211 catch (RuntimeException e) 00212 { 00213 String msg = Translate.get("loadbalancer.something.failed.on", 00214 new String[]{errorMsgPrefix, 00215 request.getSQLShortForm(vdb.getSQLShortFormLength()), 00216 backend.getName(), e.getMessage()}); 00217 logger.error(msg, e); 00218 throw new SQLException(msg); 00219 } 00220 00221 return rs; 00222 } 00223 00224 /* 00225 * Debug/Monitoring 00226 */ 00227 00233 public String getInformation() 00234 { 00235 // We don't lock since we don't need a top accurate value 00236 int size = vdb.getBackends().size(); 00237 00238 if (size == 0) 00239 return "RAIDb-1 Round-Robin Request load balancer: !!!Warning!!! No backend nodes found\n"; 00240 else 00241 return "RAIDb-1 Round-Robin Request load balancer (" + size 00242 + " backends)\n"; 00243 } 00244 00248 public String getRaidb1Xml() 00249 { 00250 return "<" + DatabasesXmlTags.ELT_RAIDb_1_RoundRobin + "/>"; 00251 } 00252 00253 }

CJDBCversion1.0.4に対してTue Oct 12 15:16:02 2004に生成されました。 doxygen 1.3.8