KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > cluster > ClusterServer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
45 import java.net.UnknownHostException JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48
49 /**
50  * Defines a member of the cluster.
51  *
52  * A {@link ServerConnector} obtained with {@link #getServerConnector} is used to actually
53  * communicate with this ClusterServer when it is active in another instance of
54  * Resin .
55  */

56 public class ClusterServer {
57   private static final Logger JavaDoc 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 JavaDoc _objectName;
63
64   private Cluster _cluster;
65   private String JavaDoc _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 JavaDoc<Port> _ports = new ArrayList JavaDoc<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   /**
108    * Gets the server identifier.
109    */

110   public String JavaDoc getId()
111   {
112     return _id;
113   }
114
115   /**
116    * Sets the server identifier.
117    */

118   public void setId(String JavaDoc id)
119   {
120     _id = id;
121   }
122
123   /**
124    * Returns the cluster.
125    */

126   public Cluster getCluster()
127   {
128     return _cluster;
129   }
130
131   /**
132    * Returns the server index.
133    */

134   void setIndex(int index)
135   {
136     _index = index;
137   }
138
139   /**
140    * Returns the server index.
141    */

142   public int getIndex()
143   {
144     return _index;
145   }
146
147   /**
148    * Sets the keepalive timeout.
149    */

150   public void setKeepaliveTimeout(Period timeout)
151   {
152     _keepaliveTimeout = timeout.getPeriod();
153   }
154
155   /**
156    * Gets the keepalive timeout.
157    */

158   public long getKeepaliveTimeout()
159   {
160     return _keepaliveTimeout;
161   }
162
163   /**
164    * Sets the address
165    */

166   public void setAddress(String JavaDoc address)
167     throws UnknownHostException JavaDoc
168   {
169     _clusterPort.setAddress(address);
170   }
171
172   /**
173    * Sets the loadBalance connection time.
174    */

175   public void setLoadBalanceConnectTimeout(Period period)
176   {
177     _loadBalanceConnectTimeout = period.getPeriod();
178   }
179
180   /**
181    * Gets the loadBalance connection time.
182    */

183   public long getLoadBalanceConnectTimeout()
184   {
185     return _loadBalanceConnectTimeout;
186   }
187
188   /**
189    * Sets the loadBalance max-idle-time.
190    */

191   public void setLoadBalanceIdleTime(Period period)
192   {
193     _loadBalanceIdleTime = period.getPeriod();
194   }
195
196   /**
197    * Sets the loadBalance idle-time.
198    */

199   public long getLoadBalanceIdleTime()
200   {
201     if (_loadBalanceIdleTime != DEFAULT)
202       return _loadBalanceIdleTime;
203     else
204       return _keepaliveTimeout - 2000L;
205   }
206
207   /**
208    * Sets the loadBalance fail-recover-time.
209    */

210   public void setLoadBalanceRecoverTime(Period period)
211   {
212     _loadBalanceRecoverTime = period.getPeriod();
213   }
214
215   /**
216    * Gets the loadBalance fail-recover-time.
217    */

218   public long getLoadBalanceRecoverTime()
219   {
220     return _loadBalanceRecoverTime;
221   }
222
223   /**
224    * Sets the loadBalance read/write timeout
225    */

226   public void setSocketTimeout(Period period)
227   {
228     _socketTimeout = period.getPeriod();
229   }
230
231   /**
232    * Gets the loadBalance read/write timeout
233    */

234   public long getSocketTimeout()
235   {
236     return _socketTimeout;
237   }
238
239   /**
240    * Sets the loadBalance warmup time
241    */

242   public void setLoadBalanceWarmupTime(Period period)
243   {
244     _loadBalanceWarmupTime = period.getPeriod();
245   }
246
247   /**
248    * Gets the loadBalance warmup time
249    */

250   public long getLoadBalanceWarmupTime()
251   {
252     return _loadBalanceWarmupTime;
253   }
254
255   /**
256    * Sets the loadBalance weight
257    */

258   public void setLoadBalanceWeight(int weight)
259   {
260     _loadBalanceWeight = weight;
261   }
262
263   /**
264    * Gets the loadBalance weight
265    */

266   public int getLoadBalanceWeight()
267   {
268     return _loadBalanceWeight;
269   }
270
271   /**
272    * Arguments on boot
273    */

274   public void addJvmArg(String JavaDoc args)
275   {
276   }
277
278   /**
279    * Arguments on boot
280    */

281   public void addWatchdogArg(String JavaDoc args)
282   {
283   }
284
285   /**
286    * Sets a port.
287    */

288   public void setPort(int port)
289   {
290     _clusterPort.setPort(port);
291   }
292
293   /**
294    * Adds a http.
295    */

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   /**
311    * Adds a custom-protocol port.
312    */

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   /**
324    * Pre-binding of ports.
325    */

326   public void bind(String JavaDoc address, int port, QServerSocket ss)
327     throws Exception JavaDoc
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 JavaDoc(L.l("No matching port for {0}:{1}",
345                     address, port));
346   }
347
348   /**
349    * Sets the user name.
350    */

351   public void setUserName(String JavaDoc userName)
352   {
353   }
354
355   /**
356    * Sets the group name.
357    */

358   public void setGroupName(String JavaDoc groupName)
359   {
360   }
361
362   /**
363    * Returns the ports.
364    */

365   public ArrayList JavaDoc<Port> getPorts()
366   {
367     return _ports;
368   }
369
370   /**
371    * Sets the ClusterPort.
372    */

373   public ClusterPort createClusterPort()
374   {
375     return _clusterPort;
376   }
377
378   /**
379    * Sets the ClusterPort.
380    */

381   public ClusterPort getClusterPort()
382   {
383     return _clusterPort;
384   }
385
386   /**
387    * Returns the server connector.
388    */

389   public ServerConnector getServerConnector()
390   {
391     if (_cluster.getSelfServer() != this)
392       return _serverConnector;
393     else
394       return null;
395   }
396
397   /**
398    * Adds a program.
399    */

400   public void addBuilderProgram(BuilderProgram program)
401   {
402     _serverProgram.addProgram(program);
403   }
404
405   /**
406    * Adds a program.
407    */

408   public BuilderProgram getServerProgram()
409   {
410     return _serverProgram;
411   }
412
413   /**
414    * Initialize
415    */

416   @PostConstruct
417   public void init()
418     throws Exception JavaDoc
419   {
420     _clusterPort.init();
421
422     if (_cluster != null && ! getId().equals(_cluster.getServerId()))
423       _serverConnector.init();
424   }
425
426   /**
427    * Starts the server.
428    */

429   public Server startServer()
430     throws Throwable JavaDoc
431   {
432     return _cluster.startServer(this);
433   }
434
435   /**
436    * Generate the primary, secondary, tertiary, returning the value encoded
437    * in a long.
438    */

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   /**
490    * Close any ports.
491    */

492   public void close()
493   {
494     if (_serverConnector != null)
495       _serverConnector.close();
496   }
497
498   public String JavaDoc toString()
499   {
500     return ("ClusterServer[id=" + getId() + "]");
501   }
502 }
503
Popular Tags