KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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): Damian Arregui.
20  */

21
22 package org.continuent.sequoia.controller.virtualdatabase.protocol;
23
24 import java.io.Serializable JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 import org.continuent.hedera.common.Member;
29 import org.continuent.sequoia.common.exceptions.VirtualDatabaseException;
30 import org.continuent.sequoia.common.i18n.Translate;
31 import org.continuent.sequoia.common.log.Trace;
32 import org.continuent.sequoia.controller.scheduler.AbstractScheduler;
33 import org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase;
34
35 /**
36  * This class a SuspendActivity message which does the following:
37  * <ol>
38  * <li>Suspend new persistent connections, new transactions and new writes (in
39  * this order)
40  * <li>Wait for completion of current transactions and persistent connections.
41  * </ol>
42  * <p>
43  *
44  * @author <a HREF="mailto:ralph.hannus@continuent.com">Ralph Hannus</a>
45  * @version 1.0
46  */

47 public class SuspendActivity extends DistributedVirtualDatabaseMessage
48 {
49   private static final long serialVersionUID = -8451114082404567986L;
50
51   private transient LinkedList JavaDoc totalOrderQueue;
52
53   /**
54    * Creates a new <code>DisableBackendsAndSetCheckpoint</code> object
55    */

56   public SuspendActivity()
57   {
58   }
59
60   /**
61    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageSingleThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
62    * org.continuent.hedera.common.Member)
63    */

64   public Object JavaDoc handleMessageSingleThreaded(DistributedVirtualDatabase dvdb,
65       Member sender)
66   {
67     totalOrderQueue = dvdb.getTotalOrderQueue();
68     if (totalOrderQueue == null)
69       return new VirtualDatabaseException(Translate
70           .get("virtualdatabase.no.total.order.queue", dvdb
71               .getVirtualDatabaseName()));
72
73     synchronized (totalOrderQueue)
74     {
75       SuspendWritesMessage request = new SuspendWritesMessage(
76           "Suspend activity");
77       totalOrderQueue.addLast(request);
78       return request;
79     }
80   }
81
82   /**
83    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageMultiThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
84    * org.continuent.hedera.common.Member, java.lang.Object)
85    */

86   public Serializable JavaDoc handleMessageMultiThreaded(
87       DistributedVirtualDatabase dvdb, Member sender,
88       Object JavaDoc handleMessageSingleThreadedResult)
89   {
90     Trace logger = dvdb.getLogger();
91
92     // Wait for our turn to execute
93
boolean found = dvdb.waitForTotalOrder(handleMessageSingleThreadedResult,
94         false);
95
96     AbstractScheduler scheduler = dvdb.getRequestManager().getScheduler();
97
98     // Suspend new persistent connections
99
scheduler.suspendNewPersistentConnections();
100
101     // Suspend new transactions and writes
102
scheduler.suspendNewTransactionsAndWrites();
103
104     // Remove ourselves from the queue to allow others to complete if needed
105
if (!found)
106       logger
107           .error("Suspend activity was not found in total order queue, posting out of order");
108     else
109       synchronized (totalOrderQueue)
110       {
111         totalOrderQueue.removeFirst();
112         totalOrderQueue.notifyAll();
113       }
114
115     // Wait for writes to complete
116
try
117     {
118       scheduler.waitForSuspendedWritesToComplete();
119     }
120     catch (SQLException JavaDoc e)
121     {
122       dvdb.getLogger().error("Failed to wait for writes to complete");
123       return e;
124     }
125
126     // Wait for transactions to complete
127
try
128     {
129       scheduler.waitForSuspendedTransactionsToComplete();
130     }
131     catch (SQLException JavaDoc e)
132     {
133       dvdb.getLogger().error(
134           "Failed to wait for suspended transactions to complete");
135       return e;
136     }
137
138     // Wait for opened persistent connections to be closed
139
try
140     {
141       scheduler.waitForPersistentConnectionsToComplete();
142     }
143     catch (SQLException JavaDoc e)
144     {
145       dvdb.getLogger().error(
146           "Failed to wait for persistent connections to close");
147       return e;
148     }
149
150     if (!dvdb.hasRecoveryLog())
151     {
152       // Resume transactions, writes and persistent connections
153
scheduler.resumeWritesTransactionsAndPersistentConnections();
154       return new VirtualDatabaseException(Translate
155           .get("virtualdatabase.no.recovery.log"));
156     }
157
158     return null;
159   }
160
161   /**
162    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#cancel(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase)
163    */

164   public void cancel(DistributedVirtualDatabase dvdb)
165   {
166     if (dvdb.getLogger().isWarnEnabled())
167       dvdb
168           .getLogger()
169           .warn(
170               "Canceling SuspendActivity message: resuming writes, transactions and persistent connections");
171     AbstractScheduler scheduler = dvdb.getRequestManager().getScheduler();
172     scheduler.resumeWritesTransactionsAndPersistentConnections();
173   }
174 }
175
Popular Tags