KickJava   Java API By Example, From Geeks To Geeks.

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


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): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.virtualdatabase.protocol;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.LinkedList JavaDoc;
26
27 import org.continuent.hedera.common.Member;
28 import org.continuent.sequoia.common.exceptions.VirtualDatabaseException;
29 import org.continuent.sequoia.common.i18n.Translate;
30 import org.continuent.sequoia.common.log.Trace;
31 import org.continuent.sequoia.controller.recoverylog.RecoveryLog;
32 import org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase;
33
34 /**
35  * This class defines a CompleteRecoveryLogResync message used to check that the
36  * recovery log resync happened properly.
37  *
38  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
39  * @version 1.0
40  */

41 public class CompleteRecoveryLogResync
42     extends DistributedVirtualDatabaseMessage
43 {
44   private static final long serialVersionUID = 5811295250708240645L;
45
46   private long commonCheckpointId;
47   private String JavaDoc commonCheckpointName;
48   private String JavaDoc nowCheckpointName;
49   private long nbOfEntriesToResync;
50
51   private transient LinkedList JavaDoc totalOrderQueue;
52
53   /**
54    * Creates a new <code>CompleteRecoveryLogResync</code> object. <br>
55    * This object will be used to check that the restore log operation did not
56    * generate any inconcistency (by checking that the number of copied log
57    * entries from commonCheckpointId to the log id corresponding to
58    * nowCheckpointName is equal to nbOfEntriesToResync)
59    *
60    * @param commonCheckpointId common checkpoint id where resync will start
61    * @param nowCheckpointName newly inserted checkpoint where resync will end
62    * @param nbOfEntriesToResync number of entries to be resynchronized between
63    * these 2 checkpoints
64    */

65   public CompleteRecoveryLogResync(long commonCheckpointId,
66       String JavaDoc nowCheckpointName, long nbOfEntriesToResync)
67   {
68     this.commonCheckpointId = commonCheckpointId;
69     this.nowCheckpointName = nowCheckpointName;
70     this.nbOfEntriesToResync = nbOfEntriesToResync;
71   }
72
73   /**
74    * Creates a new <code>CompleteRecoveryLogResync</code> object<br>
75    * It will be used there to check that the automatic resync of recovery logs
76    * worked well, by verifying that the number of log entries that has been
77    * transfered is the expected one (nbOfEntriesToResync) between the two log
78    * entries identified by commonCheckpointName and nowCheckpointName.
79    *
80    * @param commonCheckpointName common checkpoint name where resync will start
81    * @param nowCheckpointName newly inserted checkpoint where resync will end
82    * @param nbOfEntriesToResync number of entries to be resynchronized between
83    * these 2 checkpoints
84    */

85   public CompleteRecoveryLogResync(String JavaDoc commonCheckpointName,
86       String JavaDoc nowCheckpointName, long nbOfEntriesToResync)
87   {
88     this.commonCheckpointName = commonCheckpointName;
89     this.nowCheckpointName = nowCheckpointName;
90     this.nbOfEntriesToResync = nbOfEntriesToResync;
91   }
92
93   /**
94    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageSingleThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
95    * org.continuent.hedera.common.Member)
96    */

97   public Object JavaDoc handleMessageSingleThreaded(DistributedVirtualDatabase dvdb,
98       Member sender)
99   {
100     if (!dvdb.hasRecoveryLog())
101       return new VirtualDatabaseException(Translate
102           .get("virtualdatabase.no.recovery.log"));
103
104     totalOrderQueue = dvdb.getTotalOrderQueue();
105     if (totalOrderQueue == null)
106       return new VirtualDatabaseException(Translate
107           .get("virtualdatabase.no.total.order.queue", dvdb.getVirtualDatabaseName()));
108
109     synchronized (totalOrderQueue)
110     {
111       totalOrderQueue.addLast(this);
112       return this;
113     }
114   }
115
116   /**
117    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageMultiThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
118    * org.continuent.hedera.common.Member, java.lang.Object)
119    */

120   public Serializable JavaDoc handleMessageMultiThreaded(
121       DistributedVirtualDatabase dvdb, Member sender,
122       Object JavaDoc handleMessageSingleThreadedResult)
123   {
124     if (!dvdb.hasRecoveryLog())
125       return new VirtualDatabaseException(Translate
126           .get("virtualdatabase.no.recovery.log"));
127
128     Trace logger = dvdb.getLogger();
129     try
130     {
131       // Wait for our turn to execute
132
if (!dvdb.waitForTotalOrder(handleMessageSingleThreadedResult, false))
133         logger
134             .error("CompleteRecoveryLogResync was not found in total order queue, posting out of order ("
135                 + commonCheckpointName + ")");
136       else
137         synchronized (totalOrderQueue)
138         {
139           totalOrderQueue.removeFirst();
140           totalOrderQueue.notifyAll();
141         }
142
143       RecoveryLog recoveryLog = dvdb.getRequestManager().getRecoveryLog();
144       long nowCheckpointId = recoveryLog.getCheckpointLogId(nowCheckpointName);
145       if (commonCheckpointName != null)
146       {
147         commonCheckpointId = recoveryLog
148             .getCheckpointLogId(commonCheckpointName);
149       }
150       long localNbOfLogEntries = recoveryLog.getNumberOfLogEntries(
151           commonCheckpointId, nowCheckpointId);
152       long diff = localNbOfLogEntries - nbOfEntriesToResync;
153
154       if (logger.isDebugEnabled())
155       {
156         logger.debug("Recovery log is being resynchronized between "
157             + commonCheckpointName + "and " + nowCheckpointId + " ("
158             + nowCheckpointName + ")");
159         logger.debug("Recovery log has resynchronized " + localNbOfLogEntries
160             + "entries (diff with remote controller is " + diff + ")");
161       }
162
163       if (diff != 0)
164         logger
165             .error("Detected inconsistency in restore log operation, logs differ by "
166                 + diff
167                 + " entries (original was "
168                 + nbOfEntriesToResync
169                 + ", local is " + localNbOfLogEntries + ")");
170       else
171         logger.info("Recovery log re-synchronized " + nbOfEntriesToResync
172             + " entries successfully");
173
174       // Send the difference
175
return new Long JavaDoc(diff);
176     }
177     catch (Exception JavaDoc e)
178     {
179       logger.error("Unable to complete recovery log resynchronization", e);
180       return new VirtualDatabaseException(
181           "Unable to complete recovery log resynchronization", e);
182     }
183   }
184 }
185
Popular Tags