1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.paralleldb; 26 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 30 import org.objectweb.cjdbc.common.i18n.Translate; 31 import org.objectweb.cjdbc.common.sql.AbstractRequest; 32 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest; 33 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 34 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 35 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase; 36 37 45 public class ParallelDB_LPRF extends ParallelDB 46 { 47 48 54 public ParallelDB_LPRF(VirtualDatabase vdb) throws Exception 55 { 56 super(vdb); 57 } 58 59 62 public DatabaseBackend chooseBackendForReadRequest(AbstractRequest request) 63 throws SQLException 64 { 65 try 67 { 68 vdb.acquireReadLockBackendLists(); 69 } 70 catch (InterruptedException e) 71 { 72 String msg = Translate.get( 73 "loadbalancer.backendlist.acquire.readlock.failed", e); 74 logger.error(msg); 75 throw new SQLException (msg); 76 } 77 78 DatabaseBackend backend = null; 80 try 83 { 84 ArrayList backends = vdb.getBackends(); 85 int size = backends.size(); 86 87 if (size == 0) 88 throw new SQLException (Translate.get( 89 "loadbalancer.execute.no.backend.available", request.getId())); 90 91 int leastRequests = 0; 93 for (int i = 0; i < size; i++) 94 { 95 DatabaseBackend b = (DatabaseBackend) backends.get(i); 96 if (b.isReadEnabled()) 97 { 98 int pending = b.getPendingRequests().size(); 99 if ((backend == null) || (pending < leastRequests)) 100 { 101 backend = b; 102 if (pending == 0) 103 break; else 105 leastRequests = pending; 106 } 107 } 108 } 109 110 if (backend == null) 111 throw new SQLException (Translate.get( 112 "loadbalancer.execute.no.backend.enabled", request.getId())); 113 } 114 catch (RuntimeException e) 115 { 116 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 117 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 118 e.getMessage()}); 119 logger.error(msg, e); 120 throw new SQLException (msg); 121 } 122 finally 123 { 124 vdb.releaseReadLockBackendLists(); 125 } 126 127 return backend; 128 } 129 130 133 public DatabaseBackend chooseBackendForWriteRequest( 134 AbstractWriteRequest request) throws SQLException 135 { 136 try 138 { 139 vdb.acquireReadLockBackendLists(); 140 } 141 catch (InterruptedException e) 142 { 143 String msg = Translate.get( 144 "loadbalancer.backendlist.acquire.readlock.failed", e); 145 logger.error(msg); 146 throw new SQLException (msg); 147 } 148 149 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 leastRequests = 0; 164 for (int i = 0; i < size; i++) 165 { 166 DatabaseBackend b = (DatabaseBackend) backends.get(i); 167 if (b.isWriteEnabled() && !b.isDisabling()) 168 { 169 int pending = b.getPendingRequests().size(); 170 if ((backend == null) || (pending < leastRequests)) 171 { 172 backend = b; 173 if (pending == 0) 174 break; else 176 leastRequests = pending; 177 } 178 } 179 } 180 181 if (backend == null) 182 { 183 throw new SQLException (Translate.get( 184 "loadbalancer.execute.no.backend.enabled", request.getId())); 185 } 186 } 187 catch (RuntimeException e) 188 { 189 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 190 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 191 e.getMessage()}); 192 logger.error(msg, e); 193 throw new SQLException (msg); 194 } 195 finally 196 { 197 vdb.releaseReadLockBackendLists(); 198 } 199 200 return backend; 201 } 202 203 206 public String getInformation() 207 { 208 int size = vdb.getBackends().size(); 210 211 if (size == 0) 212 return "ParallelDB Least Pending Request First Request load balancer: !!!Warning!!! No backend nodes found\n"; 213 else 214 return "ParallelDB Least Pending Request First Request load balancer (" 215 + size + " backends)\n"; 216 } 217 218 221 public String getParallelDBXml() 222 { 223 return "<" + DatabasesXmlTags.ELT_ParallelDB_LeastPendingRequestsFirst 224 + "/>"; 225 } 226 } | Popular Tags |