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: * Copyright (C) 2006 Continuent, Inc.
007: * Contact: sequoia@continuent.org
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: * Initial developer(s): Emmanuel Cecchet.
022: * Contributor(s): Damian Arregui.
023: */package org.continuent.sequoia.controller.virtualdatabase.protocol;
024:
025: import java.io.Serializable;
026: import java.sql.SQLException;
027:
028: import org.continuent.hedera.common.Member;
029: import org.continuent.sequoia.common.exceptions.VirtualDatabaseStartingException;
030: import org.continuent.sequoia.common.log.Trace;
031: import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
032: import org.continuent.sequoia.controller.requests.AbstractRequest;
033: import org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase;
034:
035: /**
036: * <code>DistributedRequest</code> is an abstract class that defines the
037: * interface for distributed execution of a request (horizontal scalability).
038: *
039: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
040: * @author <a href="mailto:damian.arregui@continuent.com">Damian Arregui </a>
041: * @version 1.0
042: */
043: public abstract class DistributedRequest extends
044: DistributedVirtualDatabaseMessage {
045: protected AbstractRequest request;
046:
047: /**
048: * Builds a new <code>DistributedRequest</code> object.
049: *
050: * @param request request to execute
051: */
052: public DistributedRequest(AbstractRequest request) {
053: this .request = request;
054: }
055:
056: /**
057: * Returns the request value.
058: *
059: * @return Returns the request.
060: */
061: public final AbstractRequest getRequest() {
062: return request;
063: }
064:
065: /**
066: * Schedule the request. This method blocks until the request is scheduled.
067: *
068: * @param drm a distributed request manager
069: * @return the object inserted in the total order queue
070: * @throws SQLException if an error occurs.
071: */
072: public abstract Object scheduleRequest(DistributedRequestManager drm)
073: throws SQLException;
074:
075: /**
076: * Code to be executed by the distributed request manager receiving the
077: * request.
078: *
079: * @param drm a distributed request manager
080: * @return an Object to be sent back to the caller
081: * @throws SQLException if an error occurs.
082: */
083: public abstract Serializable executeScheduledRequest(
084: DistributedRequestManager drm) throws SQLException;
085:
086: /**
087: * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageSingleThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
088: * org.continuent.hedera.common.Member)
089: */
090: public Object handleMessageSingleThreaded(
091: DistributedVirtualDatabase dvdb, Member sender) {
092: if (!dvdb.isVirtualDatabaseStarted()) {
093: request = null; // prevent further logging
094: return new VirtualDatabaseStartingException();
095: }
096:
097: Trace logger = dvdb.getLogger();
098:
099: if (logger.isDebugEnabled() && (getRequest() != null))
100: logger.debug(dvdb.getControllerName()
101: + ": Scheduling distributedRequest "
102: + getRequest().getId() + " from " + sender);
103:
104: // Take current time to profile query execution time
105: if (request != null)
106: request.setStartTime(System.currentTimeMillis());
107:
108: // Distributed request logger
109: Trace distributedRequestLogger = dvdb
110: .getDistributedRequestLogger();
111: if (distributedRequestLogger.isInfoEnabled())
112: distributedRequestLogger.info(toString());
113:
114: try {
115: return scheduleRequest((DistributedRequestManager) dvdb
116: .getRequestManager());
117: } catch (SQLException e) {
118: return e;
119: }
120: }
121:
122: /**
123: * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageMultiThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
124: * org.continuent.hedera.common.Member, java.lang.Object)
125: */
126: public Serializable handleMessageMultiThreaded(
127: DistributedVirtualDatabase dvdb, Member sender,
128: Object handleMessageSingleThreadedResult) {
129: Trace logger = dvdb.getLogger();
130:
131: if (handleMessageSingleThreadedResult instanceof Exception) {
132: // The scheduling part more likely failed, report the exception back
133: if (logger.isInfoEnabled() && (getRequest() != null))
134: logger.info("Distributed query "
135: + getRequest().getSqlShortForm(
136: dvdb.getSqlShortFormLength())
137: + " failed on this controller",
138: (Throwable) handleMessageSingleThreadedResult);
139: return (Serializable) handleMessageSingleThreadedResult;
140: }
141:
142: if ((logger.isDebugEnabled()) && (getRequest() != null))
143: logger.debug(dvdb.getControllerName()
144: + ": Executing distributedRequest "
145: + getRequest().getId() + " from " + sender);
146: try {
147: return executeScheduledRequest((DistributedRequestManager) dvdb
148: .getRequestManager());
149: } catch (SQLException e) {
150: return e;
151: }
152: }
153: }
|