KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import org.continuent.hedera.common.Member;
27 import org.continuent.sequoia.common.exceptions.VirtualDatabaseException;
28 import org.continuent.sequoia.common.i18n.Translate;
29 import org.continuent.sequoia.common.log.Trace;
30 import org.continuent.sequoia.controller.recoverylog.RecoveryLog;
31 import org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase;
32
33 /**
34  * This class defines a InitiateRecoveryLogResync message used to send
35  * parameters needed to initiate an automated recovery log synchronization. The
36  * message sends back the delta to apply to entries id to be inserted in the
37  * local recovery log.
38  *
39  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
40  * @version 1.0
41  */

42 public class InitiateRecoveryLogResync
43     extends DistributedVirtualDatabaseMessage
44 {
45   private static final long serialVersionUID = -6799114439172152L;
46
47   private long originCommonCheckpointId;
48   private String JavaDoc commonCheckpointName;
49   private String JavaDoc nowCheckpointName;
50   private long nbOfEntriesToResync;
51
52   /**
53    * Creates a new <code>InitiateRecoveryLogResync</code> object
54    *
55    * @param commonCheckpointName common checkpoint name where resync will start
56    * @param commonCheckpointId common checkpoint id on the original node (the
57    * one that is up-to-date)
58    * @param nowCheckpointName newly inserted checkpoint where resync will end
59    * @param nbOfEntriesToResync number of entries to be resynchronized between
60    * these 2 checkpoints
61    */

62   public InitiateRecoveryLogResync(String JavaDoc commonCheckpointName,
63       long commonCheckpointId, String JavaDoc nowCheckpointName,
64       long nbOfEntriesToResync)
65   {
66     this.commonCheckpointName = commonCheckpointName;
67     this.originCommonCheckpointId = commonCheckpointId;
68     this.nowCheckpointName = nowCheckpointName;
69     this.nbOfEntriesToResync = nbOfEntriesToResync;
70   }
71
72   /**
73    * Returns the commonCheckpointId value.
74    *
75    * @return Returns the commonCheckpointId.
76    */

77   public final long getOriginCommonCheckpointId()
78   {
79     return originCommonCheckpointId;
80   }
81
82   /**
83    * Returns the commonCheckpointName value.
84    *
85    * @return Returns the commonCheckpointName.
86    */

87   public final String JavaDoc getCommonCheckpointName()
88   {
89     return commonCheckpointName;
90   }
91
92   /**
93    * Returns the nbOfEntriesToResync value.
94    *
95    * @return Returns the nbOfEntriesToResync.
96    */

97   public final long getNbOfEntriesToResync()
98   {
99     return nbOfEntriesToResync;
100   }
101
102   /**
103    * Returns the nowCheckpointName value.
104    *
105    * @return Returns the nowCheckpointName.
106    */

107   public final String JavaDoc getNowCheckpointName()
108   {
109     return nowCheckpointName;
110   }
111
112   /**
113    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageSingleThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
114    * org.continuent.hedera.common.Member)
115    */

116   public Object JavaDoc handleMessageSingleThreaded(DistributedVirtualDatabase dvdb,
117       Member sender)
118   {
119     if (!dvdb.hasRecoveryLog())
120       return new VirtualDatabaseException(Translate
121           .get("virtualdatabase.no.recovery.log"));
122     
123     return this;
124   }
125
126   /**
127    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageMultiThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
128    * org.continuent.hedera.common.Member, java.lang.Object)
129    */

130   public Serializable JavaDoc handleMessageMultiThreaded(
131       DistributedVirtualDatabase dvdb, Member sender,
132       Object JavaDoc handleMessageSingleThreadedResult)
133   {
134     if (!dvdb.hasRecoveryLog())
135       return new VirtualDatabaseException(Translate
136           .get("virtualdatabase.no.recovery.log"));
137
138     Trace logger = dvdb.getLogger();
139     try
140     {
141       RecoveryLog recoveryLog = dvdb.getRequestManager().getRecoveryLog();
142       long commonCheckpointId = recoveryLog
143           .getCheckpointLogId(getCommonCheckpointName());
144       long nowCheckpointId = recoveryLog
145           .getCheckpointLogId(getNowCheckpointName());
146
147       // Compute how much additional space is needed to resync the log
148
long shift = getNbOfEntriesToResync()
149           - (nowCheckpointId - commonCheckpointId);
150
151       // Shift entries only if we do not have enough space
152
if (shift > 0)
153         recoveryLog.moveEntries(nowCheckpointId, shift);
154
155       // Cleanup entries in the log hole that will be resynced
156
recoveryLog.deleteLogEntriesAndCheckpointBetween(commonCheckpointId,
157           nowCheckpointId + shift);
158
159       // Update now checkpoint
160
recoveryLog.removeCheckpoint(nowCheckpointName);
161       recoveryLog.storeCheckpoint(nowCheckpointName, nowCheckpointId + shift);
162
163       return new Long JavaDoc(commonCheckpointId - getOriginCommonCheckpointId());
164     }
165     catch (Exception JavaDoc e)
166     {
167       logger.error("Unable to initialize recovery log resynchronization", e);
168       return new VirtualDatabaseException(
169           "Unable to initialize recovery log resynchronization", e);
170     }
171   }
172
173 }
174
Popular Tags