001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Emmanuel Cecchet.
021: * Contributor(s): Jean-Bernard van Zuylen.
022: */package org.continuent.sequoia.controller.scheduler.raidb2;
023:
024: import java.sql.SQLException;
025:
026: import org.continuent.sequoia.common.exceptions.RollbackException;
027: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
028: import org.continuent.sequoia.controller.core.ControllerConstants;
029: import org.continuent.sequoia.controller.requestmanager.RAIDbLevels;
030: import org.continuent.sequoia.controller.requests.AbstractWriteRequest;
031: import org.continuent.sequoia.controller.requests.ParsingGranularities;
032: import org.continuent.sequoia.controller.requests.SelectRequest;
033: import org.continuent.sequoia.controller.requests.StoredProcedure;
034: import org.continuent.sequoia.controller.scheduler.AbstractScheduler;
035: import org.continuent.sequoia.controller.scheduler.schema.TransactionExclusiveLock;
036:
037: /**
038: * This scheduler provides transaction level scheduling for RAIDb-2 controllers.
039: * Each write takes a lock on the whole database. All following writes are
040: * blocked until the transaction of the first write completes.
041: *
042: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
043: * @author <a href="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
044: * </a>
045: * @version 1.0
046: * @deprecated since Sequoia 2.2
047: */
048: public class RAIDb2PessimisticTransactionLevelScheduler extends
049: AbstractScheduler {
050:
051: //
052: // How the code is organized ?
053: //
054: // 1. Member variables
055: // 2. Constructor
056: // 3. Request handling
057: // 4. Transaction management
058: // 5. Debug/Monitoring
059: //
060:
061: private TransactionExclusiveLock lock = new TransactionExclusiveLock();
062:
063: //
064: // Constructor
065: //
066:
067: /**
068: * Creates a new Pessimistic Transaction Level Scheduler
069: */
070: public RAIDb2PessimisticTransactionLevelScheduler() {
071: super (RAIDbLevels.RAIDb2, ParsingGranularities.NO_PARSING);
072: }
073:
074: //
075: // Request Handling
076: //
077:
078: /**
079: * Additionally to scheduling the request, this method replaces the SQL Date
080: * macros such as now() with the current date.
081: *
082: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleNonSuspendedReadRequest(SelectRequest)
083: */
084: public final void scheduleNonSuspendedReadRequest(
085: SelectRequest request) throws SQLException {
086: }
087:
088: /**
089: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#readCompletedNotify(SelectRequest)
090: */
091: public final void readCompletedNotify(SelectRequest request) {
092: }
093:
094: /**
095: * Note that CREATE statements are not synchronized.
096: *
097: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleWriteRequest(AbstractWriteRequest)
098: */
099: public void scheduleNonSuspendedWriteRequest(
100: AbstractWriteRequest request) throws SQLException {
101: if (request.isCreate()) {
102: return;
103: }
104:
105: if (lock.acquire(request)) {
106: if (logger.isDebugEnabled())
107: logger.debug("Request " + request.getId()
108: + " scheduled for write (" + getPendingWrites()
109: + " pending writes)");
110: } else {
111: if (logger.isWarnEnabled())
112: logger
113: .warn("Request " + request.getId()
114: + " timed out (" + request.getTimeout()
115: + " s)");
116: throw new SQLException("Timeout (" + request.getTimeout()
117: + ") for request: " + request.getId());
118: }
119: }
120:
121: /**
122: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#notifyWriteCompleted(AbstractWriteRequest)
123: */
124: public final synchronized void notifyWriteCompleted(
125: AbstractWriteRequest request) {
126: // Requests outside transaction delimiters must release the lock
127: // as soon as they have executed
128: if (request.isAutoCommit() && (!request.isCreate()))
129: releaseLock(request.getTransactionId());
130: }
131:
132: /**
133: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#scheduleNonSuspendedStoredProcedure(org.continuent.sequoia.controller.requests.StoredProcedure)
134: */
135: public final void scheduleNonSuspendedStoredProcedure(
136: StoredProcedure proc) throws SQLException,
137: RollbackException {
138: if (lock.acquire(proc)) {
139: if (logger.isDebugEnabled())
140: logger.debug("Stored procedure " + proc.getId()
141: + " scheduled for write (" + getPendingWrites()
142: + " pending writes)");
143: } else {
144: if (logger.isWarnEnabled())
145: logger.warn("Stored procedure " + proc.getId()
146: + " timed out (" + proc.getTimeout() + " s)");
147: throw new SQLException(
148: "Timeout ("
149: + proc.getTimeout()
150: + ") for request: "
151: + proc
152: .getSqlShortForm(ControllerConstants.SQL_SHORT_FORM_LENGTH));
153: }
154: }
155:
156: /**
157: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#notifyStoredProcedureCompleted(org.continuent.sequoia.controller.requests.StoredProcedure)
158: */
159: public final void notifyStoredProcedureCompleted(
160: StoredProcedure proc) {
161: // Requests outside transaction delimiters must release the lock
162: // as soon as they have executed
163: if (proc.isAutoCommit() && (!proc.isCreate()))
164: releaseLock(proc.getTransactionId());
165: }
166:
167: //
168: // Transaction Management
169: //
170:
171: /**
172: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#commitTransaction(long)
173: */
174: protected final void commitTransaction(long transactionId) {
175: if (lock.isLocked())
176: releaseLock(transactionId);
177: // else this was an empty or read-only transaction
178: }
179:
180: /**
181: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#rollbackTransaction(long)
182: */
183: protected final void rollbackTransaction(long transactionId) {
184: if (lock.isLocked())
185: releaseLock(transactionId);
186: // else this was an empty or read-only transaction
187: }
188:
189: /**
190: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#rollbackTransaction(long,
191: * String)
192: */
193: protected final void rollbackTransaction(long transactionId,
194: String savepointName) {
195: }
196:
197: /**
198: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#setSavepointTransaction(long,
199: * String)
200: */
201: protected final void setSavepointTransaction(long transactionId,
202: String name) {
203: }
204:
205: /**
206: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#releaseSavepointTransaction(long,
207: * String)
208: */
209: protected final void releaseSavepointTransaction(
210: long transactionId, String name) {
211: }
212:
213: /**
214: * Release the locks we may own on the schema.
215: *
216: * @param transactionId id of the transaction that releases the lock
217: */
218: private void releaseLock(long transactionId) {
219: // Are we the lock owner ?
220: if (lock.isLocked()) {
221: if (lock.getLocker() == transactionId)
222: lock.release();
223:
224: // Note that the following warnings could be safely ignored if the
225: // transaction commiting/rollbacking (releasing the lock) has not done any
226: // conflicting write
227: else if (logger.isDebugEnabled())
228: logger
229: .debug("Transaction "
230: + transactionId
231: + " wants to release the lock held by transaction "
232: + lock.getLocker());
233: } else if (logger.isDebugEnabled())
234: logger
235: .warn("Transaction "
236: + transactionId
237: + " tries to release a lock that has not been acquired.");
238: }
239:
240: //
241: // Debug/Monitoring
242: //
243:
244: /**
245: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler#getXmlImpl()
246: */
247: public String getXmlImpl() {
248: return "<" + DatabasesXmlTags.ELT_RAIDb2Scheduler + " "
249: + DatabasesXmlTags.ATT_level + "=\""
250: + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>";
251: }
252: }
|