1 23 24 package com.sun.appserv.naming; 25 26 import java.util.logging.Level ; 27 import com.sun.logging.LogDomains; 28 import java.util.logging.Logger ; 29 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Vector ; 33 import java.net.InetAddress ; 34 35 import java.net.MalformedURLException ; 36 import java.net.UnknownHostException ; 37 38 import com.sun.jndi.cosnaming.IiopUrl; 39 import com.sun.corba.ee.spi.folb.ClusterInstanceInfo; 40 import com.sun.corba.ee.spi.folb.SocketInfo; 41 import com.sun.enterprise.util.ORBManager; 42 43 86 87 public class RoundRobinPolicy { 88 89 private List <ClusterInstanceInfo> endpointsList = 90 new LinkedList <ClusterInstanceInfo>(); 91 92 private static Logger _logger = LogDomains.getLogger( 93 LogDomains.JNDI_LOGGER); 94 95 private static java.util.Random rand = new java.util.Random (); 96 97 private int sumOfAllWeights = 0; 98 99 private static final int default_weight = 10; 100 101 public RoundRobinPolicy(String [] list) { 103 setClusterInstanceInfo(list); 104 } 105 106 public void setClusterInstanceInfo(List <ClusterInstanceInfo> list) { 108 sumOfAllWeights = 0; 109 endpointsList = list; 110 111 String policy = System.getProperty(S1ASCtxFactory.LOAD_BALANCING_PROPERTY); 112 boolean isWeighted; 113 if (policy == null) { 114 policy = S1ASCtxFactory.IC_BASED; 116 } 117 if (policy.equals(S1ASCtxFactory.IC_BASED_WEIGHTED)) { 118 isWeighted = true; 119 } else if (policy.equals(S1ASCtxFactory.IC_BASED)) { 120 isWeighted = false; 121 } else { 122 isWeighted = false; 123 _logger.warning("loadbalancing.policy.incorrect"); 124 } 125 for (ClusterInstanceInfo endpoint : endpointsList) { 127 if (! isWeighted) { 128 endpoint.weight = default_weight; 129 } 130 sumOfAllWeights += endpoint.weight; 131 } 132 } 133 134 137 public void setClusterInstanceInfo(String [] list) { 138 String [] newList = null; 139 140 if (list != null && list.length > 0) { 153 newList = getAddressPortList(list); 154 } else { 155 newList = getEndpointForProviderURL( 156 System.getProperty(ORBManager.JNDI_PROVIDER_URL_PROPERTY)); 157 } 158 if (newList != null && newList.length > 0) { 160 String [] new_list = randomize(newList); 161 List <ClusterInstanceInfo> targetServerList = 162 new LinkedList <ClusterInstanceInfo> (); 163 for (int i = 0; i < new_list.length; i++) { 164 if (notDuplicate(new_list[i])) { 165 targetServerList.add( 166 makeClusterInstanceInfo( 167 new_list[i], 168 default_weight)); 169 } 170 } 171 if (!targetServerList.isEmpty()) { 172 targetServerList.addAll(endpointsList); 173 setClusterInstanceInfo(targetServerList); 174 } 175 } else { 176 _logger.log(Level.WARNING,"no.endpoints"); 177 } 178 } 179 180 185 private ClusterInstanceInfo makeClusterInstanceInfo(String str, int weight) { 186 String [] host_port = str.split(":"); 187 String server_identifier = ""; String type = "CLEAR_TEXT"; SocketInfo socketInfo = new SocketInfo(type, 190 host_port[0], 191 new Integer (host_port[1]).intValue()); 192 ClusterInstanceInfo instanceInfo = 193 new ClusterInstanceInfo(server_identifier, 194 weight, 195 new SocketInfo[]{socketInfo}); 196 return instanceInfo; 197 198 } 199 200 204 private boolean notDuplicate(String str) { 205 String [] host_port = str.split(":"); 206 for (ClusterInstanceInfo endpoint : endpointsList) { 207 ClusterInstanceInfo instanceInfo = endpoint; 208 for(int j = 0; j < instanceInfo.endpoints.length; j++) { 209 if (instanceInfo.endpoints[j].host.equals(host_port[0]) && 210 instanceInfo.endpoints[j].port == 211 new Integer (host_port[1]).intValue()) { 212 return false; 213 } 214 } 215 } 216 return true; 217 } 218 219 226 public String [] getEndpointForProviderURL(String providerURLString) { 227 String [] newList = null; 228 if (providerURLString != null) { 229 try { 230 IiopUrl providerURL = new IiopUrl(providerURLString); 231 newList = getAddressPortList(providerURL); 232 _logger.log(Level.WARNING, "no.endpoints.selected.provider", 233 new Object [] {providerURLString}); 234 } catch (MalformedURLException me) { 235 _logger.log(Level.WARNING, "provider.exception", 236 new Object [] {me.getMessage(), 237 providerURLString}); 238 } 239 } 240 return newList; 241 } 242 243 246 public String [] randomize(String [] list) { 247 String [] randomizedList = new String [list.length]; 249 for (int i = 0; i < list.length; i++) { 250 int random; 251 do { 252 random = rand.nextInt(list.length); 253 _logger.fine("random ==> " + random); 254 } while (list[random] == null); 255 randomizedList[i] = list[random]; 256 _logger.fine("randomisedList[" + i + "] ==> " + 257 randomizedList[i]); 258 list[random] = null; 259 } 260 return randomizedList; 261 } 262 263 266 public boolean isEmpty() { 267 if (endpointsList.size() == 0) 268 return true; 269 else return false; 270 } 271 272 273 280 public Object [] getNextRotation() { 281 int lowerLimit = 0; int random = 0; 283 while( random == 0) { 289 random = rand.nextInt(sumOfAllWeights); 290 if ( random != 0) { 291 break; 292 } 293 } 294 _logger.fine("random # = " + random + 295 " sum of all weights = " + sumOfAllWeights); 296 int i = 0; 297 for (ClusterInstanceInfo endpoint : endpointsList) { 298 int upperLimit = lowerLimit + endpoint.weight; 299 _logger.fine("upperLimit = " + upperLimit); 300 if (random > lowerLimit && random <= upperLimit) { 301 List <ClusterInstanceInfo> instanceInfo = 302 new LinkedList <ClusterInstanceInfo>(); 303 304 instanceInfo.addAll(0, 306 endpointsList.subList(i, 307 endpointsList.size())); 308 instanceInfo.addAll(endpointsList.subList(0, i)); 310 311 _logger.fine("returning the following list..." + 313 instanceInfo.toString()); 314 315 return convertIntoCorbaloc(instanceInfo); 316 } 317 lowerLimit = upperLimit; 318 _logger.fine("lowerLimit = " + lowerLimit); 319 i++; 320 } 321 _logger.warning("Could not find an endpoint to send request to!"); 322 return null; 323 } 324 325 private Object [] convertIntoCorbaloc(List <ClusterInstanceInfo> list) { 326 List host_port = new LinkedList (); 327 for (ClusterInstanceInfo endpoint : list) { 328 SocketInfo[] socketInfo = endpoint.endpoints; 329 for (int j = 0; j < socketInfo.length; j++) { 330 if (!host_port.contains(socketInfo[j].host.trim() + 331 ":" + socketInfo[j].port)) { 332 host_port.add(socketInfo[j].host.trim() + 333 ":" + socketInfo[j].port); 334 } 335 } 336 } 337 return host_port.toArray(); 338 } 339 340 341 342 343 348 private String [] getAddressPortList(String [] hostPortList) { 349 Vector addressPortVector = new Vector (); 351 for (int i=0; i<hostPortList.length; i++) { 352 try { 353 IiopUrl url = new IiopUrl("iiop://"+hostPortList[i]); 354 String [] apList = getAddressPortList(url); 355 for (int j=0; j<apList.length; j++) { 356 addressPortVector.addElement(apList[j]); 357 } 358 } catch (MalformedURLException me) { 359 _logger.log(Level.WARNING, "bad.host.port", 360 new Object [] {hostPortList[i], 361 me.getMessage()}); 362 } 363 } 364 String [] ret = new String [addressPortVector.size()]; 365 for (int i=0; i<ret.length; i++) { 366 ret[i] = (String )addressPortVector.elementAt(i); 367 } 368 return ret; 370 } 371 372 private String [] getAddressPortList(IiopUrl iiopUrl) { 373 IiopUrl.Address iiopUrlAddress = 375 (IiopUrl.Address)(iiopUrl.getAddresses().elementAt(0)); 376 String host = iiopUrlAddress.host; 377 int portNumber = iiopUrlAddress.port; 378 String port = Integer.toString(portNumber); 379 return getAddressPortList(host, port); 381 } 382 383 public String [] getAddressPortList(String host, String port) { 384 try { 386 InetAddress [] addresses = InetAddress.getAllByName(host); 387 String [] ret = new String [addresses.length]; 388 for (int i = 0; i < addresses.length; i++) { 389 ret[i] = addresses[i].getHostAddress() + ":" + port; 390 } 391 return ret; 393 } catch (UnknownHostException ukhe) { 394 _logger.log(Level.WARNING, "unknown.host", 395 new Object [] {host, ukhe.getMessage()}); 396 return null; 397 } 398 } 399 400 403 public void print() { 404 _logger.fine("List contents ==> "); 405 int i = 0; 406 for (ClusterInstanceInfo endpoint : endpointsList) { 407 ClusterInstanceInfo instanceInfo = endpoint; 408 409 _logger.fine("endpoint[" + i + "] ==> name =" + 410 instanceInfo.name + " weight = " + 411 instanceInfo.weight); 412 for (int j = 0; j < instanceInfo.endpoints.length; j++ ) { 413 _logger.fine("IP addresses = " + 414 instanceInfo.endpoints[j].host + ":" + 415 instanceInfo.endpoints[j].port + 416 " type = " + instanceInfo.endpoints[j].type); 417 } 418 i++; 419 } 420 } 421 } 422 | Popular Tags |