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 44 public class ParallelDB_RR extends ParallelDB 45 { 46 47 private int index = 0; 48 49 55 public ParallelDB_RR(VirtualDatabase vdb) throws Exception 56 { 57 super(vdb); 58 } 59 60 67 public DatabaseBackend chooseBackendForReadRequest(AbstractRequest request) 68 throws SQLException 69 { 70 try 72 { 73 vdb.acquireReadLockBackendLists(); 74 } 75 catch (InterruptedException e) 76 { 77 String msg = Translate.get( 78 "loadbalancer.backendlist.acquire.readlock.failed", e); 79 logger.error(msg); 80 throw new SQLException (msg); 81 } 82 83 DatabaseBackend backend = null; 85 try 88 { 89 ArrayList backends = vdb.getBackends(); 90 int size = backends.size(); 91 92 if (size == 0) 93 throw new SQLException (Translate.get( 94 "loadbalancer.execute.no.backend.available", request.getId())); 95 96 int maxTries = size; 98 synchronized (this) 99 { 100 do 101 { 102 index = (index + 1) % size; 103 backend = (DatabaseBackend) backends.get(index); 104 maxTries--; 105 } 106 while ((!backend.isReadEnabled() && maxTries >= 0)); 107 } 108 109 if (maxTries < 0) 110 throw new SQLException (Translate.get( 111 "loadbalancer.execute.no.backend.enabled", request.getId())); 112 } 113 catch (RuntimeException e) 114 { 115 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 116 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 117 e.getMessage()}); 118 logger.error(msg, e); 119 throw new SQLException (msg); 120 } 121 finally 122 { 123 vdb.releaseReadLockBackendLists(); 124 } 125 return backend; 126 } 127 128 135 public DatabaseBackend chooseBackendForWriteRequest( 136 AbstractWriteRequest request) throws SQLException 137 { 138 try 140 { 141 vdb.acquireReadLockBackendLists(); 142 } 143 catch (InterruptedException e) 144 { 145 String msg = Translate.get( 146 "loadbalancer.backendlist.acquire.readlock.failed", e); 147 logger.error(msg); 148 throw new SQLException (msg); 149 } 150 151 DatabaseBackend backend = null; 153 try 156 { 157 ArrayList backends = vdb.getBackends(); 158 int size = backends.size(); 159 160 if (size == 0) 161 throw new SQLException (Translate.get( 162 "loadbalancer.execute.no.backend.available", request.getId())); 163 164 int maxTries = size; 166 synchronized (this) 167 { 168 do 169 { 170 index = (index + 1) % size; 171 backend = (DatabaseBackend) backends.get(index); 172 maxTries--; 173 } 174 while ((!backend.isWriteEnabled() || backend.isDisabling()) 175 && (maxTries >= 0)); 176 } 177 178 if (maxTries < 0) 179 throw new SQLException (Translate.get( 180 "loadbalancer.execute.no.backend.enabled", request.getId())); 181 } 182 catch (RuntimeException e) 183 { 184 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 185 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 186 e.getMessage()}); 187 logger.error(msg, e); 188 throw new SQLException (msg); 189 } 190 finally 191 { 192 vdb.releaseReadLockBackendLists(); 193 } 194 return backend; 195 } 196 197 200 public String getInformation() 201 { 202 int size = vdb.getBackends().size(); 204 205 if (size == 0) 206 return "ParallelDB Round-Robin Request load balancer: !!!Warning!!! No backend nodes found\n"; 207 else 208 return "ParallelDB Round-Robin Request load balancer (" + size 209 + " backends)\n"; 210 } 211 212 215 public String getParallelDBXml() 216 { 217 return "<" + DatabasesXmlTags.ELT_ParallelDB_RoundRobin + "/>"; 218 } 219 } | Popular Tags |