1 29 30 package com.caucho.server.cluster; 31 32 import com.caucho.config.BuilderProgram; 33 import com.caucho.config.BuilderProgramContainer; 34 import com.caucho.config.ConfigException; 35 import com.caucho.config.types.Period; 36 import com.caucho.log.Log; 37 import com.caucho.server.http.HttpProtocol; 38 import com.caucho.server.port.Port; 39 import com.caucho.util.L10N; 40 import com.caucho.util.RandomUtil; 41 import com.caucho.vfs.QServerSocket; 42 43 import javax.annotation.PostConstruct; 44 import javax.management.ObjectName ; 45 import java.net.UnknownHostException ; 46 import java.util.ArrayList ; 47 import java.util.logging.Logger ; 48 49 56 public class ClusterServer { 57 private static final Logger log = Log.open(ClusterServer.class); 58 private static final L10N L = new L10N(ClusterServer.class); 59 60 private static final long DEFAULT = 0xcafebabe; 61 62 private ObjectName _objectName; 63 64 private Cluster _cluster; 65 private String _id = ""; 66 67 private int _index; 68 69 private ClusterPort _clusterPort; 70 private ServerConnector _serverConnector; 71 72 private long _socketTimeout = 65000L; 73 private long _keepaliveTimeout = 15000L; 74 75 private long _loadBalanceIdleTime = DEFAULT; 76 private long _loadBalanceRecoverTime = 15000L; 77 private long _loadBalanceWarmupTime = 60000L; 78 79 private long _loadBalanceConnectTimeout = 5000L; 80 81 private int _loadBalanceWeight = 100; 82 83 private BuilderProgramContainer _serverProgram 84 = new BuilderProgramContainer(); 85 86 private ArrayList <Port> _ports = new ArrayList <Port>(); 87 88 public ClusterServer(Cluster cluster) 89 { 90 _cluster = cluster; 91 92 _clusterPort = new ClusterPort(this); 93 _ports.add(_clusterPort); 94 95 _serverConnector = new ServerConnector(this); 96 } 97 98 public ClusterServer(Cluster cluster, boolean isTest) 99 { 100 _cluster = cluster; 101 102 _clusterPort = new ClusterPort(this); 103 104 _serverConnector = new ServerConnector(this); 105 } 106 107 110 public String getId() 111 { 112 return _id; 113 } 114 115 118 public void setId(String id) 119 { 120 _id = id; 121 } 122 123 126 public Cluster getCluster() 127 { 128 return _cluster; 129 } 130 131 134 void setIndex(int index) 135 { 136 _index = index; 137 } 138 139 142 public int getIndex() 143 { 144 return _index; 145 } 146 147 150 public void setKeepaliveTimeout(Period timeout) 151 { 152 _keepaliveTimeout = timeout.getPeriod(); 153 } 154 155 158 public long getKeepaliveTimeout() 159 { 160 return _keepaliveTimeout; 161 } 162 163 166 public void setAddress(String address) 167 throws UnknownHostException 168 { 169 _clusterPort.setAddress(address); 170 } 171 172 175 public void setLoadBalanceConnectTimeout(Period period) 176 { 177 _loadBalanceConnectTimeout = period.getPeriod(); 178 } 179 180 183 public long getLoadBalanceConnectTimeout() 184 { 185 return _loadBalanceConnectTimeout; 186 } 187 188 191 public void setLoadBalanceIdleTime(Period period) 192 { 193 _loadBalanceIdleTime = period.getPeriod(); 194 } 195 196 199 public long getLoadBalanceIdleTime() 200 { 201 if (_loadBalanceIdleTime != DEFAULT) 202 return _loadBalanceIdleTime; 203 else 204 return _keepaliveTimeout - 2000L; 205 } 206 207 210 public void setLoadBalanceRecoverTime(Period period) 211 { 212 _loadBalanceRecoverTime = period.getPeriod(); 213 } 214 215 218 public long getLoadBalanceRecoverTime() 219 { 220 return _loadBalanceRecoverTime; 221 } 222 223 226 public void setSocketTimeout(Period period) 227 { 228 _socketTimeout = period.getPeriod(); 229 } 230 231 234 public long getSocketTimeout() 235 { 236 return _socketTimeout; 237 } 238 239 242 public void setLoadBalanceWarmupTime(Period period) 243 { 244 _loadBalanceWarmupTime = period.getPeriod(); 245 } 246 247 250 public long getLoadBalanceWarmupTime() 251 { 252 return _loadBalanceWarmupTime; 253 } 254 255 258 public void setLoadBalanceWeight(int weight) 259 { 260 _loadBalanceWeight = weight; 261 } 262 263 266 public int getLoadBalanceWeight() 267 { 268 return _loadBalanceWeight; 269 } 270 271 274 public void addJvmArg(String args) 275 { 276 } 277 278 281 public void addWatchdogArg(String args) 282 { 283 } 284 285 288 public void setPort(int port) 289 { 290 _clusterPort.setPort(port); 291 } 292 293 296 public Port createHttp() 297 throws ConfigException 298 { 299 Port port = new Port(this); 300 301 HttpProtocol protocol = new HttpProtocol(); 302 protocol.setParent(port); 303 port.setProtocol(protocol); 304 305 _ports.add(port); 306 307 return port; 308 } 309 310 313 public Port createProtocol() 314 throws ConfigException 315 { 316 Port port = new Port(this); 317 318 _ports.add(port); 319 320 return port; 321 } 322 323 326 public void bind(String address, int port, QServerSocket ss) 327 throws Exception 328 { 329 for (int i = 0; i < _ports.size(); i++) { 330 Port serverPort = _ports.get(i); 331 332 if (port != serverPort.getPort()) 333 continue; 334 335 if ((address == null) != (serverPort.getAddress() == null)) 336 continue; 337 else if (address == null || address.equals(serverPort.getAddress())) { 338 serverPort.bind(ss); 339 340 return; 341 } 342 } 343 344 throw new IllegalStateException (L.l("No matching port for {0}:{1}", 345 address, port)); 346 } 347 348 351 public void setUserName(String userName) 352 { 353 } 354 355 358 public void setGroupName(String groupName) 359 { 360 } 361 362 365 public ArrayList <Port> getPorts() 366 { 367 return _ports; 368 } 369 370 373 public ClusterPort createClusterPort() 374 { 375 return _clusterPort; 376 } 377 378 381 public ClusterPort getClusterPort() 382 { 383 return _clusterPort; 384 } 385 386 389 public ServerConnector getServerConnector() 390 { 391 if (_cluster.getSelfServer() != this) 392 return _serverConnector; 393 else 394 return null; 395 } 396 397 400 public void addBuilderProgram(BuilderProgram program) 401 { 402 _serverProgram.addProgram(program); 403 } 404 405 408 public BuilderProgram getServerProgram() 409 { 410 return _serverProgram; 411 } 412 413 416 @PostConstruct 417 public void init() 418 throws Exception 419 { 420 _clusterPort.init(); 421 422 if (_cluster != null && ! getId().equals(_cluster.getServerId())) 423 _serverConnector.init(); 424 } 425 426 429 public Server startServer() 430 throws Throwable 431 { 432 return _cluster.startServer(this); 433 } 434 435 439 public long generateBackupCode() 440 { 441 ClusterServer []srunList = getCluster().getServerList(); 442 int srunLength = srunList.length; 443 444 long index = _index; 445 long backupCode = index; 446 447 long backupLength = srunLength; 448 if (backupLength < 3) 449 backupLength = 3; 450 int backup; 451 452 if (srunLength <= 1) { 453 backup = 0; 454 backupCode |= 1L << 16; 455 } 456 else if (srunLength == 2) { 457 backup = 0; 458 459 backupCode |= ((index + 1L) % 2) << 16; 460 } 461 else { 462 int sublen = srunLength - 1; 463 if (sublen > 7) 464 sublen = 7; 465 466 backup = RandomUtil.nextInt(sublen); 467 468 backupCode |= ((index + backup + 1L) % backupLength) << 16; 469 } 470 471 if (srunLength <= 2) 472 backupCode |= 2L << 32; 473 else { 474 int sublen = srunLength - 2; 475 if (sublen > 6) 476 sublen = 6; 477 478 int third = RandomUtil.nextInt(sublen); 479 480 if (backup <= third) 481 third += 1; 482 483 backupCode |= ((index + third + 1) % backupLength) << 32; 484 } 485 486 return backupCode; 487 } 488 489 492 public void close() 493 { 494 if (_serverConnector != null) 495 _serverConnector.close(); 496 } 497 498 public String toString() 499 { 500 return ("ClusterServer[id=" + getId() + "]"); 501 } 502 } 503 | Popular Tags |