01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2005 Emic Networks.
04: * Contact: sequoia@continuent.org
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: * Initial developer(s): Emmanuel Cecchet.
19: * Contributor(s): ______________________.
20: */package org.continuent.sequoia.driver.connectpolicy;
21:
22: import java.util.Random;
23:
24: import org.continuent.sequoia.common.exceptions.NoMoreControllerException;
25: import org.continuent.sequoia.driver.ControllerInfo;
26: import org.continuent.sequoia.driver.SequoiaUrl;
27:
28: /**
29: * This class defines a RandomConnectPolicy used when the Sequoia URL has the
30: * following form:
31: * jdbc:sequoia://node1,node2,node3/myDB?preferredController=random
32: *
33: * @author <a href="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet
34: * </a>
35: * @version 1.0
36: */
37: public class RandomConnectPolicy extends
38: AbstractControllerConnectPolicy {
39: private Random rand;
40:
41: /**
42: * Creates a new <code>RandomConnectPolicy</code> object
43: *
44: * @param controllerList the controller list on which the policy applies
45: * @param pingDelayInMs Interval in milliseconds between two pings of a
46: * controller
47: * @param controllerTimeoutInMs timeout in milliseconds after which a
48: * controller is considered as dead if it did not respond to pings
49: * @param debugLevel the debug level to use
50: * @see org.continuent.sequoia.driver.SequoiaUrl#DEBUG_LEVEL_OFF
51: */
52: public RandomConnectPolicy(ControllerInfo[] controllerList,
53: int pingDelayInMs, int controllerTimeoutInMs, int debugLevel) {
54: super (controllerList, pingDelayInMs, controllerTimeoutInMs,
55: debugLevel);
56: rand = new Random(System.currentTimeMillis());
57: }
58:
59: /**
60: * @see org.continuent.sequoia.driver.connectpolicy.AbstractControllerConnectPolicy#getController()
61: */
62: public synchronized ControllerInfo getController()
63: throws NoMoreControllerException {
64: int size = aliveControllers.size();
65: if (size == 0)
66: throw new NoMoreControllerException();
67:
68: // we ask for a controller with random number. If not available, take the
69: // next one in a round robin style
70: ControllerInfo controllerInfo = super .getControllerByNum(rand
71: .nextInt(size));
72: if (debugLevel == SequoiaUrl.DEBUG_LEVEL_DEBUG)
73: System.out.println("Selected controller " + controllerInfo);
74: return controllerInfo;
75: }
76: }
|