KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > AdminUtil


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 package com.sun.jts.CosTransactions;
29
30 import java.util.*;
31 import org.omg.CosTransactions.*;
32
33 /**
34  * AdminUtil - Utility class for monitoring and administering jts.
35  *
36  * This class is used by admin utilities for performing monitoring and
37  * administration.
38  * @author <a HREF="mailto:ajay.kr@sun.com">Ajay Kumar</a>
39  * @version 1.0
40  */

41 public class AdminUtil
42 {
43     //freezing functionality
44

45     /**
46      * freezeAll
47      *
48      * Freeze all transactional activity.
49      *
50      * Part of freezing functions that will be used to achive a
51      * transactional quiet period before taking any action or collecting
52      * any statistics. This call returns when all the activities are frozen.
53      * All the states would be allowed to complete before freezing on. For
54      * example a transaction in preparing state would be allowed to transition
55      * to next state - the prepared state.
56      *
57      */

58     public static void freezeAll()
59     {
60         TransactionState.freezeLock.acquireWriteLock();
61     }
62
63     /**
64      * unfreezeAll
65      *
66      * Unfreeze all transactional activity frozen by a freezeAll call.
67      *
68      * Part of freezing functions that will be used to achive a
69      * transactional quiet period before taking any action or collecting
70      * any statistics. This call returns almost immediately.
71      *
72      */

73     public static void unfreezeAll()
74     {
75         TransactionState.freezeLock.releaseWriteLock();
76     }
77
78
79     /**
80      * isFrozenAll
81      *
82      * Get the current state.
83      *
84      * Part of freezing functions that will be used to achive a
85      * transactional quiet period before taking any action or collecting
86      * any statistics. This call returns almost immediately.
87      *
88      */

89     public static boolean isFrozenAll()
90     {
91         return TransactionState.freezeLock.isWriteLocked();
92     }
93
94
95
96     /* For future use
97      *
98     public static void freeze(Control ctrl)
99     {
100     }
101      */

102
103
104     /* For future use.
105      *
106     public static void unfreeze(Control ctrl)
107     {
108     }
109      */

110
111     //Sampling control
112

113     /**
114      * startSampling
115      *
116      * Start the sampling window. The sample window determines the duration
117      * in which the data is collected. Start and stop calls demarcate the
118      * the window. The sampling data could be number of transactions committed
119      * ,rolledback etc and transient data like pending etc. It also resets
120      * various counters.
121      *
122      * @see stopSampling
123      */

124     public static void startSampling()
125     {
126         if(!bSampling){
127             try
128             {
129                 statisticsLock.acquireWriteLock();
130                 lSampleEndTime = 0 ;
131                 lSampleStartTime = System.currentTimeMillis();
132                 iCommits = 0;
133                 iAborts = 0;
134                 iPending = 0;
135                 iRecCommits = 0;
136                 iRecAborts = 0;
137                 //iImmigerent = 0;
138
//iEmmigerent = 0;
139
Iterator iter = getAllTransactions().iterator();
140                 
141                 while ( iter.hasNext() )
142                 {
143                  CoordinatorImpl coord = (CoordinatorImpl)iter.next();
144                  if ( coord.get_status() ==
145                      org.omg.CosTransactions.Status.StatusPrepared )
146                     iPending++;
147                 }
148             
149                 bSampling = true;
150             }
151             finally
152             {
153                  statisticsLock.releaseWriteLock();
154             }
155         }
156     }
157
158
159     /**
160      * stopSampling
161      *
162      * Stop sampling the statitics values. This is used to indicate end of
163      * sampling window.
164      *
165      * @see startSampling
166      *
167      */

168     public static void stopSampling()
169     {
170         try
171         {
172             statisticsLock.acquireWriteLock();
173             lSampleEndTime = System.currentTimeMillis();
174             bSampling = false;
175         }
176         finally
177         {
178             statisticsLock.releaseWriteLock();
179         }
180     }
181
182     //data collection
183

184     /**
185      * Increment the count of committed transactions.
186      */

187     static void incrementCommitedTransactionCount()
188     {
189         try
190         {
191             statisticsLock.acquireReadLock();
192             synchronized ( lkCommits )
193             {
194                 iCommits++;
195             }
196         }
197         finally
198         {
199             statisticsLock.releaseReadLock();
200         }
201     }
202
203     /**
204      * Increment the count of transactions that were rolled back.
205      *
206      */

207     static void incrementAbortedTransactionCount()
208     {
209         try
210         {
211             statisticsLock.acquireReadLock();
212             synchronized ( lkAborts )
213             {
214                 iAborts++;
215             }
216         }
217         finally
218         {
219             statisticsLock.releaseReadLock();
220         }
221     }
222     
223     /**
224      * Increment the count of transactions that were rolled back and ne'er went through prepare phase.
225      *
226      */

227     static void incrementUnpreparedAbortedTransactionCount()
228     {
229         try
230         {
231             statisticsLock.acquireReadLock();
232             synchronized ( lkUAborts )
233             {
234                 iUAborts++;
235             }
236         }
237         finally
238         {
239             statisticsLock.releaseReadLock();
240         }
241     }
242
243
244     /**
245      * Increment the count of pending transactions
246      * - i.e. the transactions entering the prepared state.
247      *
248      */

249     static void incrementPendingTransactionCount()
250     {
251         try
252         {
253             statisticsLock.acquireReadLock();
254             synchronized ( lkPending )
255             {
256                 iPending++;
257             }
258         }
259         finally
260         {
261             statisticsLock.releaseReadLock();
262         }
263     }
264
265     /**
266      * Increments the number of transactions that were commited as part of
267      * the recovery process.
268      */

269     public static void incrementRecoveryCommitedTransactionCount()
270     {
271         try
272         {
273             statisticsLock.acquireReadLock();
274             synchronized ( lkRecCommits )
275             {
276                 iRecCommits++;
277             }
278         }
279         finally
280         {
281             statisticsLock.releaseReadLock();
282         }
283     }
284      
285
286     /**
287      * Increments the number of transactions that were rolled back as part
288      * of the recovery process.
289      */

290     public static void incrementRecoveryAbortedTransactionCount()
291     {
292         try
293         {
294             statisticsLock.acquireReadLock();
295             synchronized ( lkRecAborts )
296             {
297                 iRecAborts++;
298             }
299         }
300         finally
301         {
302             statisticsLock.releaseReadLock();
303         }
304     }
305     
306
307     /* For future use
308      *
309     public static void incrementImmigerentTransactionCount()
310     {
311         try
312         {
313             statisticsLock.acquireReadLock();
314             synchronized ( lkImmigerent )
315             {
316                 iImmigerent++;
317             }
318         }
319         finally
320         {
321             statisticsLock.releaseReadLock();
322         }
323     }
324      */

325
326     /* For future use
327      *
328     public static void incrementEmmigerentTransactionCount()
329     {
330         try
331         {
332             statisticsLock.acquireReadLock();
333             synchronized ( lkEmmigerent )
334             {
335                 iEmmigerent++;
336             }
337         }
338         finally
339         {
340             statisticsLock.releaseReadLock();
341         }
342     }
343      */

344
345
346     //gathering statistics
347

348     /**
349      * Return the committed transactions count.
350      *
351      */

352     public static long getCommitedTransactionCount()
353     {
354         return iCommits;
355     }
356
357     /**
358      * Return the count of rolledback transactions.
359      *
360      */

361     public static long getAbortedTransactionCount()
362     {
363         return iAborts;
364     }
365
366     /**
367      * Return the number of transactions that are active currently.
368      *
369      */

370     public static long getActiveTransactionCount()
371     {
372         return RecoveryManager.getCoordsByGlobalTID().size()
373             - getPendingTransactionCount();
374     }
375
376     /**
377      * Return the count of transactions that are prepared - but not completed.
378      * This includes the total transactions that ever entered into prepared state
379      * minus ones which got committed, rolledback, taking care of ones that were
380      * rolledback without entering prepared state.
381      *
382      */

383     public static long getPendingTransactionCount()
384     {
385         return iPending-iAborts-iCommits+iUAborts;
386     }
387
388     /**
389      * Return the count of transactions that were commited as part of the
390      * recovery process.
391      */

392     public static long getRecoveryCommitedTransactionCount()
393     {
394         return iRecCommits;
395     }
396      
397
398     /**
399      * Return the number of transactions that were rolled back as part of the
400      * recovery process.
401      */

402     public static long getRecoveryAbortedTransactionCount()
403     {
404         return iRecAborts;
405     }
406     
407
408     /**
409      *
410     public static long getImmigerentTransactionCount()
411     {
412         
413         return iImmigerent;
414     }
415      */

416
417     /**
418      *
419     public static long getEmmigerentTransactionCount()
420     {
421         return iEmmigerent;
422     }
423      */

424
425     /**
426      * Returns the time in milliseconds, since the begining of time, when the
427      * sampling window was started.
428      */

429     public static long getSamplingStartTime()
430     {
431         return lSampleStartTime;
432     }
433
434
435     /**
436      * Returns the time in millisecond , since the begining of time, when the
437      * sampling window was closed.
438      */

439     public static long getSamplingEndTime()
440     {
441         return lSampleEndTime;
442     }
443
444     /**
445      * Returns the duration for which the last sampling window was open, in milliseconds.
446      * If the sampling window is still open, then the running duration is returned.
447      */

448     public static long getSamplingDuration()
449     {
450         return ( lSampleEndTime == 0 && lSampleStartTime != 0 )
451             ? System.currentTimeMillis() - lSampleStartTime
452             : lSampleEndTime - lSampleStartTime;
453     }
454
455
456     // Enumerating...
457

458     /**
459      * Returns the collection of existing coordinators.
460      *
461      */

462     public static Collection/*<Coordinator>*/ getAllTransactions()
463     {
464         return RecoveryManager.getCoordsByGlobalTID().values();
465     }
466
467     /**
468      * Returned enumeration of globaltids of existing transactions.
469      *
470      */

471     public static Enumeration/*<GlobalTID>*/ getAllTIDs()
472     {
473         return RecoveryManager.getCoordsByGlobalTID().keys();
474     }
475
476     
477     private static RWLock statisticsLock = new RWLock() ;
478     private static long lSampleStartTime = 0 ;
479     private static long lSampleEndTime = 0 ;
480     static boolean bSampling = false ;
481     private static int iCommits = 0;
482     private static int iAborts = 0;
483     private static int iUAborts=0;
484     private static int iPending = 0;
485     static int iRecCommits = 0;
486     static int iRecAborts = 0;
487     /*
488      * for future use
489      *
490     static int iImmigerent = 0;
491     static int iEmmigerent = 0;
492      */

493     private static Object JavaDoc lkCommits = new Object JavaDoc();
494     private static Object JavaDoc lkAborts = new Object JavaDoc();
495     private static Object JavaDoc lkUAborts = new Object JavaDoc();
496     private static Object JavaDoc lkPending = new Object JavaDoc();
497     static Object JavaDoc lkRecCommits = new Object JavaDoc();
498     static Object JavaDoc lkRecAborts = new Object JavaDoc();
499
500     /*
501      * for future use
502      *
503     static Object lkImmigerent = new Object();
504     static Object lkEmmigerent = new Object();
505      */

506
507 }
508
Popular Tags