KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > utils > RecoveryHooks > FailureInducer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.jts.utils.RecoveryHooks;
30
31 import java.text.MessageFormat JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 import com.sun.jts.jtsxa.Utility;
35 import com.sun.jts.otsidl.JCoordinator;
36 import com.sun.jts.otsidl.JCoordinatorHelper;
37 import com.sun.jts.CosTransactions.GlobalTID;
38 import org.omg.CosTransactions.Coordinator;
39
40 import java.util.logging.Logger JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import com.sun.logging.LogDomains;
43 import com.sun.jts.utils.LogFormatter;
44 /**
45  * This class defines API's for the test harness to induce
46  * recovery by setting crash points (in the case of TM crashes)
47  * and wait points (in the case of RM crashes).
48  *
49  * In order to induce TM crashes, the test harness sets the failure points
50  * by calling setCrashPoint(), after transaction.begin() from each thread.
51  * Thus, multiple threads involving different transactions can
52  * set different points of failure, which will cause the TM to wait
53  * at the failure points, until crash() method is invoked.
54  * Note: Only those transactions calling setCrashPoint() will be affected.
55  *
56  * The crash() method needs to be invoked from a seperate
57  * thread. The TM would increment TestRecovery.waitCount field to
58  * indicate the total number of failure points reached.
59  * When the waitCount reaches the expected value
60  * (obtained through getWaitCount()), the test harness shall call crash().
61  *
62  * In order to test RM recovery, the
63  * test harness sets the failure points by calling setWaitPoint(),
64  * after transaction.begin() from each thread. The TM waits at predefined
65  * failure points for the stipulated time duration, during which the RM is
66  * expected to crash; after which the TM will proceed with regular
67  * completion (a human is expected to crash the RM manually during the
68  * wait duration). As in the TM recovery case, the waitCount will be
69  * incremented to indicate the total number of failure points reached.
70  *
71  * If the RM does not crash during the stipulated time
72  * duration, the TM will proceed with normal completion for the specific
73  * transaction. It does not matter if the RM
74  * comes back alive, since the TM would anyway retry completion.
75  * Note: Only those transactions calling setWaitPoint() will be affected.
76  *
77  * @author Ram Jeyaraman 04/21/1999
78  */

79 public class FailureInducer {
80
81     // static finals
82
public static final Integer JavaDoc ACTIVE = new Integer JavaDoc(0);
83     public static final Integer JavaDoc PREPARING = new Integer JavaDoc(1);
84     public static final Integer JavaDoc PREPARED = new Integer JavaDoc(2);
85     public static final Integer JavaDoc COMPLETING = new Integer JavaDoc(3);
86     public static final Integer JavaDoc COMPLETED = new Integer JavaDoc(4);
87
88     // static fields
89

90     private static boolean failureInducerIsActive = false;
91     private static boolean crash = false;
92     private static int waitPeriod = 0;
93     private static int waitCount = 0;
94     private static Hashtable JavaDoc crashList = new Hashtable JavaDoc();
95     private static Hashtable JavaDoc waitList = new Hashtable JavaDoc();
96     private static Hashtable JavaDoc waitTime = new Hashtable JavaDoc();
97     private static ResourceBundle JavaDoc messages = ResourceBundle.
98         getBundle("com.sun.jts.utils.RecoveryHooks.Messages"/*#Frozen*/);
99     /*
100         Logger to log transaction messages
101     */

102         static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
103     // static initializer
104

105     static {
106
107
108     }
109
110     // static methods
111

112     /**
113      * This activates the FailureInducer. An application needs to activate
114      * the failure inducer first, before setting the fail or wait points.
115      */

116     public static void activateFailureInducer() {
117         failureInducerIsActive = true;
118     }
119
120     /**
121      * This deactivates the FailureInducer. An application deactivate the
122      * failure inducer, to temporarily stop failure inducement. The fail
123      * or wait points are not forgotten during the dormant state.
124      */

125     public static void deactivateFailureInducer() {
126         failureInducerIsActive = false;
127     }
128
129     /**
130      * @return the current state (active or inactive) of failure inducer.
131      */

132     public static boolean isFailureInducerActive() {
133         return failureInducerIsActive;
134     }
135
136     /**
137      * Setting a crash point will cause the TM to wait at the
138      * failure point, until crash() is called.
139      *
140      * @param crashPoint pre-defined failure points
141      * (PREPARING, PREPARED, COMMITTING, COMMITTED).
142      */

143     public static void setCrashPoint(Integer JavaDoc crashPoint) {
144         // sanity check
145
if (crashPoint == null) {
146             _logger.log(Level.SEVERE,"jts.invalid_crash_point");
147             return;
148         }
149
150         Coordinator coord = Utility.getCoordinator(Utility.getControl());
151         JCoordinator jcoord = JCoordinatorHelper.narrow(coord);
152         if (jcoord != null) {
153             GlobalTID gtid = new GlobalTID(jcoord.getGlobalTID());
154             crashList.put(gtid, crashPoint);
155         }
156     }
157
158     /**
159      * Setting a wait point will cause the TM to wait at the
160      * failure point, for the stipulated wait duration.
161      *
162      * @param crashPoint pre-defined failure points
163      * (PREPARING, PREPARED, COMMITTING, COMMITTED).
164      * @param waitDuration time duration (seconds) for RM failure to happen.
165      */

166     public static void setWaitPoint(Integer JavaDoc waitPoint, int waitDuration) {
167         // sanity check
168
if (waitPoint == null) {
169             _logger.log(Level.SEVERE,"jts.invalid_wait_point");
170             return;
171         }
172
173         Coordinator coord = Utility.getCoordinator(Utility.getControl());
174         JCoordinator jcoord = JCoordinatorHelper.narrow(coord);
175         if (jcoord != null) {
176             GlobalTID gtid = new GlobalTID(jcoord.getGlobalTID());
177             waitList.put(gtid, waitPoint);
178             waitTime.put(gtid, new Integer JavaDoc(waitDuration));
179         }
180     }
181
182     /**
183      * Forces the TM to crash.
184      */

185     public static void crash() {
186         crash = true;
187     }
188
189     /**
190      * Increments the wait count (called only by TM).
191      */

192     private static void incrementWaitCount() {
193         waitCount++;
194     }
195
196     /**
197      * @return the total number of failure points reached.
198      */

199     public static int getWaitCount() {
200         return waitCount;
201     }
202
203     /**
204      * This method is called by the coordinator at every valid
205      * failure point. If the crash point or the wait point set for
206      * the current transaction matches the current failure point,
207      * an appropriate action (crash or wait) is taken.
208      * <em>Note:</em> Crash action takes precedence over wait actions for the
209      * same failure point.
210      *
211      * @param coord the coordinator object (which represents the transaction.
212      * @param failPoint indicates the current failure point in coordinator code.
213      */

214     public static void waitForFailure(GlobalTID gtid, Integer JavaDoc failPoint) {
215
216         // sanity check
217
if (gtid == null)
218             return;
219
220         Integer JavaDoc crashPoint = (Integer JavaDoc) crashList.get(gtid);
221         Integer JavaDoc waitPoint = (Integer JavaDoc) waitList.get(gtid);
222
223         // no crash point or wait point has been set for the transaction
224
if (crashPoint == null && waitPoint == null) {
225             return;
226         }
227
228         _logger.log(Level.WARNING,"jts.failpoint",failPoint);
229         // increment wait count and wait for the crash flag to be set
230
if (crashPoint != null && crashPoint.equals(failPoint)) {
231             incrementWaitCount();
232             while (crash == false) {
233                 try {
234                     Thread.sleep(3000);
235                 } catch (Exception JavaDoc e) {}
236             }
237             System.exit(0);
238         }
239
240         // wait for the wait duration and continue
241
if (waitPoint != null && waitPoint.equals(failPoint)) {
242             Integer JavaDoc waitDuration = (Integer JavaDoc) waitTime.get(gtid);
243
244             // wait duration has not be set or is invalid
245
if (waitDuration == null || waitDuration.intValue() < 0)
246                 return;
247
248             // wait for the stipulated duration
249
try {
250                 Thread.sleep(waitDuration.intValue() * 1000);
251             } catch (Exception JavaDoc e) {}
252         }
253     }
254
255     // Utility methods for fetching i18n/l10n messages
256

257     private static String JavaDoc getMessage(String JavaDoc key) {
258         return getMessage(key, null);
259     }
260
261     private static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc[] inserts) {
262         if (messages == null || key == null) {
263             return null;
264         }
265
266         String JavaDoc msg = messages.getString(key);
267         if (msg == null) {
268             return null;
269         }
270
271         if (inserts == null) {
272             return msg;
273         } else {
274             return MessageFormat.format(msg, inserts);
275         }
276     }
277 }
278
Popular Tags