001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): ______________________________________.
021: */package org.continuent.sequoia.controller.loadbalancer.policies;
022:
023: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
024:
025: /**
026: * Defines the policy to adopt before returning a result to the client.
027: *
028: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
029: * @version 1.0
030: */
031: public class WaitForCompletionPolicy {
032: /** Return as soon as one node has completed the request. */
033: public static final int FIRST = 0;
034:
035: /**
036: * Return as soon as a majority (n/2+1) of nodes has completed the request.
037: */
038: public static final int MAJORITY = 1;
039:
040: /** Wait for all nodes to complete the request before returning the result. */
041: public static final int ALL = 2;
042:
043: /** Policy (default is {@link #FIRST}). */
044: private int policy = FIRST;
045:
046: /** True if strict table based locking must be enforced */
047: private boolean enforceTableLocking;
048:
049: /** Deadlock detection timeout in ms */
050: private long deadlockTimeoutInMs;
051:
052: /**
053: * Creates a new <code>WaitForCompletionPolicy</code> object
054: *
055: * @param policy the default policy to use
056: * @param enforceTableLocking true if strict table based locking must be
057: * enforced
058: * @param deadlockTimeoutInMs deadlock detection timeout in ms
059: */
060: public WaitForCompletionPolicy(int policy,
061: boolean enforceTableLocking, long deadlockTimeoutInMs) {
062: this .policy = policy;
063: this .enforceTableLocking = enforceTableLocking;
064: this .deadlockTimeoutInMs = deadlockTimeoutInMs;
065: }
066:
067: /**
068: * Returns the deadlockTimeoutInMs value.
069: *
070: * @return Returns the deadlockTimeoutInMs.
071: */
072: public final long getDeadlockTimeoutInMs() {
073: return deadlockTimeoutInMs;
074: }
075:
076: /**
077: * Sets the deadlockTimeoutInMs value.
078: *
079: * @param deadlockTimeoutInMs The deadlockTimeoutInMs to set.
080: */
081: public final void setDeadlockTimeoutInMs(long deadlockTimeoutInMs) {
082: this .deadlockTimeoutInMs = deadlockTimeoutInMs;
083: }
084:
085: /**
086: * Returns the enforceTableLocking value.
087: *
088: * @return Returns the enforceTableLocking.
089: */
090: public boolean isEnforceTableLocking() {
091: return enforceTableLocking;
092: }
093:
094: /**
095: * Returns the policy.
096: *
097: * @return an <code>int</code> value
098: */
099: public int getPolicy() {
100: return policy;
101: }
102:
103: /**
104: * Sets the policy.
105: *
106: * @param policy the policy to set
107: */
108: public void setPolicy(int policy) {
109: this .policy = policy;
110: }
111:
112: /**
113: * Gives information about the current policy.
114: *
115: * @return a <code>String</code> value
116: */
117: public String getInformation() {
118: switch (policy) {
119: case FIRST:
120: return "return when first node completes";
121: case MAJORITY:
122: return "return when a majority of nodes completes";
123: case ALL:
124: return "return when all nodes have completed";
125: default:
126: return "unknown policy";
127: }
128: }
129:
130: /**
131: * Returns this wait policy in xml format.
132: *
133: * @return xml formatted string
134: */
135: public String getXml() {
136: StringBuffer info = new StringBuffer();
137: info.append("<" + DatabasesXmlTags.ELT_WaitForCompletion + " "
138: + DatabasesXmlTags.ATT_policy + "=\"");
139: switch (policy) {
140: case FIRST:
141: info.append(DatabasesXmlTags.VAL_first);
142: break;
143: case ALL:
144: info.append(DatabasesXmlTags.VAL_all);
145: break;
146: case MAJORITY:
147: info.append(DatabasesXmlTags.VAL_majority);
148: break;
149: default:
150: }
151: info.append("\" " + DatabasesXmlTags.ATT_enforceTableLocking
152: + "=\"" + enforceTableLocking + "\" "
153: + DatabasesXmlTags.ATT_deadlockTimeoutInMs + "=\""
154: + deadlockTimeoutInMs + "\"/>");
155: return info.toString();
156: }
157: }
|