1 22 23 package org.continuent.sequoia.controller.loadbalancer.paralleldb; 24 25 import java.sql.SQLException ; 26 import java.util.ArrayList ; 27 28 import org.continuent.sequoia.common.exceptions.NoMoreBackendException; 29 import org.continuent.sequoia.common.i18n.Translate; 30 import org.continuent.sequoia.common.xml.DatabasesXmlTags; 31 import org.continuent.sequoia.controller.backend.DatabaseBackend; 32 import org.continuent.sequoia.controller.requests.AbstractRequest; 33 import org.continuent.sequoia.controller.requests.AbstractWriteRequest; 34 import org.continuent.sequoia.controller.virtualdatabase.VirtualDatabase; 35 36 43 public class ParallelDB_RR extends ParallelDB 44 { 45 46 private int index = 0; 47 48 54 public ParallelDB_RR(VirtualDatabase vdb) throws Exception 55 { 56 super(vdb); 57 } 58 59 66 public DatabaseBackend chooseBackendForReadRequest(AbstractRequest request) 67 throws SQLException 68 { 69 try 71 { 72 vdb.acquireReadLockBackendLists(); 73 } 74 catch (InterruptedException e) 75 { 76 String msg = Translate.get( 77 "loadbalancer.backendlist.acquire.readlock.failed", e); 78 logger.error(msg); 79 throw new SQLException (msg); 80 } 81 82 DatabaseBackend backend = null; 84 try 87 { 88 ArrayList backends = vdb.getBackends(); 89 int size = backends.size(); 90 91 if (size == 0) 92 throw new NoMoreBackendException(Translate.get( 93 "loadbalancer.execute.no.backend.available", request.getId())); 94 95 int maxTries = size; 97 synchronized (this) 98 { 99 do 100 { 101 index = (index + 1) % size; 102 backend = (DatabaseBackend) backends.get(index); 103 maxTries--; 104 } 105 while ((!backend.isReadEnabled() && maxTries >= 0)); 106 } 107 108 if (maxTries < 0) 109 throw new SQLException (Translate.get( 110 "loadbalancer.execute.no.backend.enabled", request.getId())); 111 } 112 catch (RuntimeException e) 113 { 114 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 115 new String []{request.getSqlShortForm(vdb.getSqlShortFormLength()), 116 e.getMessage()}); 117 logger.error(msg, e); 118 throw new SQLException (msg); 119 } 120 finally 121 { 122 vdb.releaseReadLockBackendLists(); 123 } 124 return backend; 125 } 126 127 134 public DatabaseBackend chooseBackendForWriteRequest( 135 AbstractWriteRequest request) throws SQLException 136 { 137 try 139 { 140 vdb.acquireReadLockBackendLists(); 141 } 142 catch (InterruptedException e) 143 { 144 String msg = Translate.get( 145 "loadbalancer.backendlist.acquire.readlock.failed", e); 146 logger.error(msg); 147 throw new SQLException (msg); 148 } 149 150 DatabaseBackend backend = null; 152 try 155 { 156 ArrayList backends = vdb.getBackends(); 157 int size = backends.size(); 158 159 if (size == 0) 160 throw new NoMoreBackendException(Translate.get( 161 "loadbalancer.execute.no.backend.available", request.getId())); 162 163 int maxTries = size; 165 synchronized (this) 166 { 167 do 168 { 169 index = (index + 1) % size; 170 backend = (DatabaseBackend) backends.get(index); 171 maxTries--; 172 } 173 while ((!backend.isWriteEnabled() || backend.isDisabling()) 174 && (maxTries >= 0)); 175 } 176 177 if (maxTries < 0) 178 throw new SQLException (Translate.get( 179 "loadbalancer.execute.no.backend.enabled", request.getId())); 180 } 181 catch (RuntimeException e) 182 { 183 String msg = Translate.get("loadbalancer.execute.find.backend.failed", 184 new String []{request.getSqlShortForm(vdb.getSqlShortFormLength()), 185 e.getMessage()}); 186 logger.error(msg, e); 187 throw new SQLException (msg); 188 } 189 finally 190 { 191 vdb.releaseReadLockBackendLists(); 192 } 193 return backend; 194 } 195 196 199 public String getInformation() 200 { 201 int size = vdb.getBackends().size(); 203 204 if (size == 0) 205 return "ParallelDB Round-Robin Request load balancer: !!!Warning!!! No backend nodes found\n"; 206 else 207 return "ParallelDB Round-Robin Request load balancer (" + size 208 + " backends)\n"; 209 } 210 211 214 public String getParallelDBXml() 215 { 216 return "<" + DatabasesXmlTags.ELT_ParallelDB_RoundRobin + "/>"; 217 } 218 } | Popular Tags |