KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > impl > TransactionStoreTest


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.objectserver.impl;
5
6 import com.tc.net.protocol.tcm.ChannelID;
7 import com.tc.object.gtx.GlobalTransactionID;
8 import com.tc.object.tx.ServerTransactionID;
9 import com.tc.object.tx.TransactionID;
10 import com.tc.objectserver.gtx.GlobalTransactionDescriptor;
11 import com.tc.objectserver.persistence.api.PersistenceTransaction;
12 import com.tc.objectserver.persistence.api.TransactionPersistor;
13 import com.tc.objectserver.persistence.api.TransactionStore;
14 import com.tc.objectserver.persistence.impl.TestPersistenceTransaction;
15 import com.tc.objectserver.persistence.impl.TransactionStoreImpl;
16 import com.tc.test.TCTestCase;
17 import com.tc.util.concurrent.NoExceptionLinkedQueue;
18 import com.tc.util.sequence.Sequence;
19
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 public class TransactionStoreTest extends TCTestCase {
30
31   private TestTransactionPersistor persistor;
32   private TransactionStore store;
33   private Map JavaDoc sid2Gid;
34
35   public void testDeleteByGlobalTransactionID() throws Exception JavaDoc {
36     persistor = new TestTransactionPersistor();
37     store = new TransactionStoreImpl(persistor, persistor);
38     sid2Gid = new HashMap JavaDoc();
39     List JavaDoc gtxs = new LinkedList JavaDoc();
40     for (int i = 0; i < 100; i++) {
41       ServerTransactionID sid1 = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
42       createAndAddGIDFor(sid1);
43       GlobalTransactionDescriptor desc = store.createTransactionDescriptor(sid1);
44       store.commitTransactionDescriptor(null, desc);
45       assertNotNull(store.getTransactionDescriptor(new ServerTransactionID(desc.getChannelID(), desc
46           .getClientTransactionID())));
47       gtxs.add(desc);
48     }
49     final GlobalTransactionDescriptor originalMin = (GlobalTransactionDescriptor) gtxs.get(0);
50
51     assertEquals(getGlobalTransactionID(originalMin), store.getLeastGlobalTransactionID());
52
53     // create a set of transactions to delete
54
Collection JavaDoc toDelete = new HashSet JavaDoc();
55     Collection JavaDoc toDeleteIDs = new HashSet JavaDoc();
56     for (int i = 0; i < 10; i++) {
57
58       GlobalTransactionDescriptor o = (GlobalTransactionDescriptor) gtxs.remove(0);
59       toDelete.add(o);
60       toDeleteIDs.add(o.getServerTransactionID());
61     }
62     assertFalse(originalMin == gtxs.get(0));
63
64     // delete the set
65
store.removeAllByServerTransactionID(null, toDeleteIDs);
66
67     GlobalTransactionDescriptor currentMin = (GlobalTransactionDescriptor) gtxs.get(0);
68     // make sure the min has been adjusted properly
69
assertEquals(getGlobalTransactionID(currentMin), store.getLeastGlobalTransactionID());
70     // make sure the deleted set has actually been deleted
71
for (Iterator JavaDoc i = toDelete.iterator(); i.hasNext();) {
72       GlobalTransactionDescriptor desc = (GlobalTransactionDescriptor) i.next();
73       assertNull(store.getTransactionDescriptor(desc.getServerTransactionID()));
74     }
75
76     // make sure the persistor is told to delete them all
77
assertEquals(toDeleteIDs, persistor.deleteQueue.poll(1));
78   }
79
80   private GlobalTransactionID getGlobalTransactionID(GlobalTransactionDescriptor gtd) {
81     return (GlobalTransactionID) sid2Gid.get(gtd.getServerTransactionID());
82   }
83
84   private void createAndAddGIDFor(ServerTransactionID sid1) {
85     sid2Gid.put(sid1, store.createGlobalTransactionID(sid1));
86   }
87
88   public void testLeastGlobalTransactionID() throws Exception JavaDoc {
89
90     persistor = new TestTransactionPersistor();
91     store = new TransactionStoreImpl(persistor, persistor);
92     sid2Gid = new HashMap JavaDoc();
93
94     assertEquals(GlobalTransactionID.NULL_ID, store.getLeastGlobalTransactionID());
95
96     ServerTransactionID stx1 = new ServerTransactionID(new ChannelID(1), new TransactionID(1));
97
98     GlobalTransactionDescriptor gtx1 = store.createTransactionDescriptor(stx1);
99     assertEquals(GlobalTransactionID.NULL_ID, store.getLeastGlobalTransactionID());
100     createAndAddGIDFor(stx1);
101
102     store.commitTransactionDescriptor(null, gtx1);
103     assertEquals(getGlobalTransactionID(gtx1), store.getLeastGlobalTransactionID());
104
105     int min = 100;
106     int max = 200;
107     for (int i = min; i < max; i++) {
108       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
109       GlobalTransactionDescriptor gtxi = store.createTransactionDescriptor(stxid);
110       createAndAddGIDFor(stxid);
111       store.commitTransactionDescriptor(null, gtxi);
112     }
113
114     // Still the least Global Txn ID is the same
115
assertEquals(getGlobalTransactionID(gtx1), store.getLeastGlobalTransactionID());
116
117     // now remove some from the the middle
118
Set JavaDoc toDelete = new HashSet JavaDoc();
119     for (int i = min + 50; i < max - 50; i++) {
120       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
121       toDelete.add(stxid);
122     }
123     store.removeAllByServerTransactionID(null, toDelete);
124
125     // Still the least Global Txn ID is the same
126
assertEquals(getGlobalTransactionID(gtx1), store.getLeastGlobalTransactionID());
127
128     // now remove some from the beginning
129
toDelete.clear();
130     for (int i = min; i < min + 50; i++) {
131       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
132       toDelete.add(stxid);
133     }
134     toDelete.add(stx1);
135     store.removeAllByServerTransactionID(null, toDelete);
136
137     // the least Global Txn ID is not the same
138
assertNotEquals(getGlobalTransactionID(gtx1), store.getLeastGlobalTransactionID());
139     assertTrue(getGlobalTransactionID(gtx1).toLong() < store.getLeastGlobalTransactionID().toLong());
140
141     // RESTART scenario
142
persistor = new TestTransactionPersistor();
143     for (int i = min; i < max; i++) {
144       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
145       persistor.persisted.add(new GlobalTransactionDescriptor(stxid));
146       createAndAddGIDFor(stxid);
147     }
148     store = new TransactionStoreImpl(persistor, persistor);
149
150     // The store should be initialized with the least transaction id set to negative number
151
GlobalTransactionID least = store.getLeastGlobalTransactionID();
152     assertTrue(least.toLong() < 0);
153
154     // now remove some from the the middle
155
toDelete.clear();
156     for (int i = min + 50; i < max - 50; i++) {
157       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
158       toDelete.add(stxid);
159     }
160     store.removeAllByServerTransactionID(null, toDelete);
161
162     // Still the least Global Txn ID is the same
163
assertEquals(least, store.getLeastGlobalTransactionID());
164
165     // now remove some more
166
toDelete.clear();
167     for (int i = min; i < min + 50; i++) {
168       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
169       toDelete.add(stxid);
170     }
171     store.removeAllByServerTransactionID(null, toDelete);
172
173     // the least Global Txn ID is still negative
174
least = store.getLeastGlobalTransactionID();
175     assertTrue(least.toLong() < 0);
176
177     // now resend all the remaining txns
178
toDelete.clear();
179     for (int i = max - 50; i < max; i++) {
180       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
181       createAndAddGIDFor(stxid);
182     }
183
184     // now remove each of the remaining ones
185
for (int i = max - 50; i < max; i++) {
186       // the least Global Txn ID is not the same
187
assertNotEquals(least, store.getLeastGlobalTransactionID());
188       assertTrue(least.toLong() < store.getLeastGlobalTransactionID().toLong());
189       least = store.getLeastGlobalTransactionID();
190       assertTrue(least.toLong() > 0);
191
192       toDelete.clear();
193       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
194       toDelete.add(stxid);
195       store.removeAllByServerTransactionID(null, toDelete);
196     }
197
198     assertNotEquals(least, store.getLeastGlobalTransactionID());
199     least = store.getLeastGlobalTransactionID();
200     assertTrue(least.isNull());
201   }
202
203   public void testClientShutdown() throws Exception JavaDoc {
204     long sequence = 0;
205     int initialMin = 200;
206     int initialMax = 300;
207     int laterMax = 400;
208     persistor = new TestTransactionPersistor();
209     for (int i = initialMin; i < initialMax; i++) {
210       sequence++;
211       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i % 2), new TransactionID(i));
212       persistor.persisted.add(new GlobalTransactionDescriptor(stxid));
213     }
214     store = new TransactionStoreImpl(persistor, persistor);
215     GlobalTransactionID lowmk1 = store.getLeastGlobalTransactionID();
216
217     // create more
218
for (int i = initialMax; i < laterMax; i++) {
219       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i % 2), new TransactionID(i));
220       store.createGlobalTransactionID(stxid);
221       store.commitTransactionDescriptor(null, store.createTransactionDescriptor(stxid));
222     }
223     GlobalTransactionID lowmk2 = store.getLeastGlobalTransactionID();
224
225     assertEquals(lowmk1, lowmk2);
226
227     ChannelID channel0 = new ChannelID(0);
228     store.shutdownClient(null, channel0);
229
230     // Check if all channel1 IDs are gone
231
for (int i = initialMin; i < laterMax; i++) {
232       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i % 2), new TransactionID(i));
233       if (i % 2 == 0) {
234         assertNull(store.getTransactionDescriptor(stxid));
235       } else {
236         assertNotNull(store.getTransactionDescriptor(stxid));
237       }
238     }
239   }
240
241   public void tests() throws Exception JavaDoc {
242     long sequence = 0;
243     int initialMin = 200;
244     int initialMax = 300;
245     persistor = new TestTransactionPersistor();
246     for (int i = initialMin; i < initialMax; i++) {
247       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
248       persistor.persisted.add(new GlobalTransactionDescriptor(stxid));
249     }
250     store = new TransactionStoreImpl(persistor, persistor);
251
252     // make sure that the persisted transaction ids get loaded in the
253
// constructor.
254

255     for (int i = initialMin; i < initialMax; i++) {
256       ServerTransactionID stxid = new ServerTransactionID(new ChannelID(i), new TransactionID(i));
257       assertNotNull(store.getTransactionDescriptor(stxid));
258     }
259
260     ChannelID channel1 = new ChannelID(1);
261     ChannelID channel2 = new ChannelID(2);
262     TransactionID tx1 = new TransactionID(1);
263     TransactionID tx2 = new TransactionID(2);
264     ServerTransactionID stxid1 = new ServerTransactionID(channel1, tx1);
265     ServerTransactionID stxid2 = new ServerTransactionID(channel2, tx2);
266
267     assertNull(store.getTransactionDescriptor(stxid1));
268     store.createGlobalTransactionID(stxid1);
269     GlobalTransactionDescriptor gtx1 = store.createTransactionDescriptor(stxid1);
270     assertEquals(gtx1, store.getTransactionDescriptor(stxid1));
271
272     assertSame(gtx1, store.getTransactionDescriptor(stxid1));
273
274     assertEquals(++sequence, persistor.sequence);
275
276     assertNull(store.getTransactionDescriptor(stxid2));
277     GlobalTransactionDescriptor gtx2 = store.createTransactionDescriptor(stxid2);
278     store.createGlobalTransactionID(stxid2);
279     assertEquals(gtx2, store.getTransactionDescriptor(stxid2));
280     assertEquals(++sequence, persistor.sequence);
281
282     PersistenceTransaction ptx = new TestPersistenceTransaction();
283     store.commitTransactionDescriptor(ptx, gtx1);
284     Object JavaDoc[] args = (Object JavaDoc[]) persistor.storeQueue.poll(1);
285     assertTrue(persistor.storeQueue.isEmpty());
286     assertSame(ptx, args[0]);
287     assertSame(gtx1, args[1]);
288
289     store.commitTransactionDescriptor(ptx, gtx2);
290     args = (Object JavaDoc[]) persistor.storeQueue.poll(1);
291     assertTrue(persistor.storeQueue.isEmpty());
292     assertSame(ptx, args[0]);
293     assertSame(gtx2, args[1]);
294   }
295
296   private static final class TestTransactionPersistor implements TransactionPersistor, Sequence {
297
298     public final NoExceptionLinkedQueue deleteQueue = new NoExceptionLinkedQueue();
299     public final List JavaDoc persisted = new LinkedList JavaDoc();
300     public final NoExceptionLinkedQueue storeQueue = new NoExceptionLinkedQueue();
301     public long sequence = 0;
302
303     public Collection JavaDoc loadAllGlobalTransactionDescriptors() {
304       return persisted;
305     }
306
307     public void saveGlobalTransactionDescriptor(PersistenceTransaction tx, GlobalTransactionDescriptor gtx) {
308       storeQueue.put(new Object JavaDoc[] { tx, gtx });
309     }
310
311     public long next() {
312       return ++sequence;
313     }
314
315     public void deleteAllByServerTransactionID(PersistenceTransaction tx, Collection JavaDoc toDelete) {
316       deleteQueue.put(toDelete);
317     }
318
319   }
320 }
321
Popular Tags