KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > virtualdatabase > protocol > DistributedReleaseSavepoint


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Copyright (C) 2005-2006 Continuent, Inc.
7  * Contact: sequoia@continuent.org
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Initial developer(s): Jean-Bernard van Zuylen.
22  * Contributor(s): Emmanuel Cecchet.
23  */

24
25 package org.continuent.sequoia.controller.virtualdatabase.protocol;
26
27 import java.io.Serializable JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.LinkedList JavaDoc;
30
31 import org.continuent.sequoia.common.exceptions.NoMoreBackendException;
32 import org.continuent.sequoia.common.i18n.Translate;
33 import org.continuent.sequoia.controller.loadbalancer.AllBackendsFailedException;
34 import org.continuent.sequoia.controller.requestmanager.TransactionMetaData;
35 import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
36 import org.continuent.sequoia.controller.requests.AbstractRequest;
37 import org.continuent.sequoia.controller.requests.UnknownWriteRequest;
38
39 /**
40  * Execute a distributed release savepoint
41  *
42  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
43  * </a>
44  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet
45  * </a>
46  * @version 1.0
47  */

48 public class DistributedReleaseSavepoint extends DistributedTransactionMarker
49 {
50   private static final long serialVersionUID = 3025352266902951038L;
51
52   private String JavaDoc savepointName;
53
54   /**
55    * Creates a new <code>ReleaseSavepoint</code> message
56    *
57    * @param transactionId the transaction identifier
58    * @param savepointName the savepoint name
59    */

60   public DistributedReleaseSavepoint(long transactionId, String JavaDoc savepointName)
61   {
62     super(transactionId);
63     this.savepointName = savepointName;
64   }
65
66   /**
67    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedTransactionMarker#scheduleCommand(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
68    */

69   public Object JavaDoc scheduleCommand(DistributedRequestManager drm)
70       throws SQLException JavaDoc
71   {
72     LinkedList JavaDoc totalOrderQueue = drm.getVirtualDatabase().getTotalOrderQueue();
73     if (totalOrderQueue != null)
74     {
75       synchronized (totalOrderQueue)
76       {
77         totalOrderQueue.addLast(this);
78       }
79     }
80     return this;
81   }
82
83   /**
84    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedTransactionMarker#executeCommand(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
85    */

86   public Serializable JavaDoc executeCommand(DistributedRequestManager drm)
87       throws SQLException JavaDoc
88   {
89     boolean hasBeenScheduled = false;
90
91     // Let's find the transaction marker since it will be used even for
92
// logging purposes
93
Long JavaDoc tid = new Long JavaDoc(transactionId);
94     TransactionMetaData tm;
95     try
96     {
97       tm = drm.getTransactionMetaData(tid);
98     }
99     catch (SQLException JavaDoc e)
100     {
101       // Remove the rollback from the total order queue
102
drm.getLoadBalancer().removeObjectFromAndNotifyTotalOrderQueue(this);
103       throw e;
104     }
105
106     // Check that a savepoint with given name has been set
107
if (!drm.hasSavepoint(tid, savepointName))
108       throw new SQLException JavaDoc(Translate.get("transaction.savepoint.not.found",
109           new String JavaDoc[]{savepointName, String.valueOf(transactionId)}));
110
111     try
112     {
113       // Wait for the scheduler to give us the authorization to execute
114
drm.getScheduler().releaseSavepoint(tm, savepointName, this);
115       hasBeenScheduled = true;
116
117       if (drm.getLogger().isDebugEnabled())
118         drm.getLogger().debug(
119             Translate.get("transaction.releasesavepoint", new String JavaDoc[]{
120                 savepointName, String.valueOf(transactionId)}));
121
122       // Send to load balancer
123
drm.getLoadBalancer().releaseSavepoint(tm, savepointName);
124
125       // Update recovery log
126
drm.getRecoveryLog().logRequestCompletion(tm.getLogId(), true, 0);
127
128       // Notify scheduler for completion
129
drm.getScheduler().savepointCompleted(transactionId);
130     }
131     catch (NoMoreBackendException e)
132     {
133       addFailureOnAllBackends(drm, hasBeenScheduled, tm);
134       if (drm.getLogger().isDebugEnabled())
135         drm.getLogger().debug(
136             Translate.get(
137                 "virtualdatabase.distributed.releasesavepoint.logging.only",
138                 new String JavaDoc[]{savepointName, String.valueOf(transactionId)}));
139       throw e;
140     }
141     catch (SQLException JavaDoc e)
142     {
143       addFailureOnAllBackends(drm, hasBeenScheduled, tm);
144       drm
145           .getLogger()
146           .warn(
147               Translate
148                   .get("virtualdatabase.distributed.releasesavepoint.sqlexception"),
149               e);
150       return e;
151     }
152     catch (RuntimeException JavaDoc re)
153     {
154       addFailureOnAllBackends(drm, hasBeenScheduled, tm);
155       drm.getLogger().warn(
156           Translate
157               .get("virtualdatabase.distributed.releasesavepoint.exception"),
158           re);
159       throw new SQLException JavaDoc(re.getMessage());
160     }
161     catch (AllBackendsFailedException e)
162     {
163       addFailureOnAllBackends(drm, hasBeenScheduled, tm);
164       if (drm.getLogger().isDebugEnabled())
165         drm
166             .getLogger()
167             .debug(
168                 Translate
169                     .get(
170                         "virtualdatabase.distributed.releasesavepoint.all.backends.locally.failed",
171                         new String JavaDoc[]{savepointName,
172                             String.valueOf(transactionId)}));
173       return e;
174     }
175     finally
176     {
177       // Remove savepoint for the transaction
178
drm.removeSavepoint(tid, savepointName);
179     }
180     return Boolean.TRUE;
181   }
182
183   private void addFailureOnAllBackends(DistributedRequestManager drm,
184       boolean hasBeenScheduled, TransactionMetaData tm)
185   {
186     AbstractRequest request = new UnknownWriteRequest("release "
187         + savepointName, false, 0, "\n");
188     request.setTransactionId(transactionId);
189     request.setLogId(tm.getLogId());
190     drm.addFailedOnAllBackends(request, hasBeenScheduled);
191   }
192
193   /**
194    * Returns the savepointName value.
195    *
196    * @return Returns the savepointName.
197    */

198   public String JavaDoc getSavepointName()
199   {
200     return savepointName;
201   }
202
203   /**
204    * @see java.lang.Object#equals(java.lang.Object)
205    */

206   public boolean equals(Object JavaDoc obj)
207   {
208     if (super.equals(obj))
209       return savepointName.equals(((DistributedReleaseSavepoint) obj)
210           .getSavepointName());
211     else
212       return false;
213   }
214
215   /**
216    * @see java.lang.Object#toString()
217    */

218   public String JavaDoc toString()
219   {
220     return "Release savepoint " + savepointName + " from transaction "
221         + transactionId;
222   }
223 }
224
Popular Tags