KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > gtx > ClientGlobalTransactionManagerImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.gtx;
5
6 import com.tc.net.protocol.tcm.ChannelID;
7 import com.tc.object.lockmanager.api.LockFlushCallback;
8 import com.tc.object.lockmanager.api.LockID;
9 import com.tc.object.tx.RemoteTransactionManager;
10 import com.tc.object.tx.ServerTransactionID;
11 import com.tc.object.tx.TransactionID;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.SortedMap JavaDoc;
19 import java.util.TreeMap JavaDoc;
20 import java.util.Map.Entry;
21
22 public class ClientGlobalTransactionManagerImpl implements ClientGlobalTransactionManager {
23
24   private static final int ALLOWED_LWM_DELTA = 100;
25   private final Set JavaDoc applied = new HashSet JavaDoc();
26   private final SortedMap JavaDoc globalTransactionIDs = new TreeMap JavaDoc(GlobalTransactionID.COMPARATOR);
27
28   private GlobalTransactionID lowWatermark = GlobalTransactionID.NULL_ID;
29   private final RemoteTransactionManager remoteTransactionManager;
30
31   public ClientGlobalTransactionManagerImpl(RemoteTransactionManager remoteTransactionManager) {
32     this.remoteTransactionManager = remoteTransactionManager;
33   }
34
35   // For testing
36
public synchronized int size() {
37     return applied.size();
38   }
39   
40   // For testing
41
public int getAllowedLowWaterMarkDelta() {
42     return ALLOWED_LWM_DELTA;
43   }
44
45   public synchronized boolean startApply(ChannelID committerID, TransactionID transactionID, GlobalTransactionID gtxID) {
46     if (gtxID.lessThan(getLowGlobalTransactionIDWatermark())) {
47       // formatting
48
throw new UnknownTransactionError("Attempt to apply a transaction lower than the low watermark: gtxID=" + gtxID
49                                         + ", low watermark=" + getLowGlobalTransactionIDWatermark());
50     }
51     ServerTransactionID serverTransactionID = new ServerTransactionID(committerID, transactionID);
52     globalTransactionIDs.put(gtxID, serverTransactionID);
53     return applied.add(serverTransactionID);
54   }
55
56   public synchronized GlobalTransactionID getLowGlobalTransactionIDWatermark() {
57     return lowWatermark;
58   }
59
60   public synchronized void setLowWatermark(GlobalTransactionID lowWatermark) {
61     if (this.lowWatermark.toLong() + ALLOWED_LWM_DELTA > lowWatermark.toLong()) {
62       // Dont do this every time - once in a while is ok.
63
return;
64     }
65     this.lowWatermark = lowWatermark;
66     Map JavaDoc toDelete = globalTransactionIDs.headMap(lowWatermark);
67     for (Iterator JavaDoc i = toDelete.entrySet().iterator(); i.hasNext();) {
68       Entry e = (Entry) i.next();
69       ServerTransactionID stxID = (ServerTransactionID) e.getValue();
70       i.remove();
71       applied.remove(stxID);
72     }
73   }
74
75   public void flush(LockID lockID) {
76     remoteTransactionManager.flush(lockID);
77   }
78
79   public boolean isTransactionsForLockFlushed(LockID lockID, LockFlushCallback callback) {
80     return remoteTransactionManager.isTransactionsForLockFlushed(lockID, callback);
81   }
82
83   public void unpause() {
84     remoteTransactionManager.unpause();
85   }
86
87   public void pause() {
88     remoteTransactionManager.pause();
89   }
90
91   public void resendOutstanding() {
92     remoteTransactionManager.resendOutstanding();
93   }
94
95   public Collection JavaDoc getTransactionSequenceIDs() {
96     return remoteTransactionManager.getTransactionSequenceIDs();
97   }
98
99   public Collection JavaDoc getResentTransactionIDs() {
100     return remoteTransactionManager.getResentTransactionIDs();
101   }
102
103   public void starting() {
104     remoteTransactionManager.starting();
105   }
106
107   public void resendOutstandingAndUnpause() {
108     remoteTransactionManager.resendOutstandingAndUnpause();
109   }
110
111 }
112
Popular Tags