KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > tx > TransactionalObjectManagerTest


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

5 package com.tc.objectserver.tx;
6
7 import com.tc.net.protocol.tcm.ChannelID;
8 import com.tc.object.ObjectID;
9 import com.tc.object.dmi.DmiDescriptor;
10 import com.tc.object.dna.impl.ObjectStringSerializer;
11 import com.tc.object.lockmanager.api.LockID;
12 import com.tc.object.tx.TransactionID;
13 import com.tc.object.tx.TxnBatchID;
14 import com.tc.object.tx.TxnType;
15 import com.tc.objectserver.context.ApplyCompleteEventContext;
16 import com.tc.objectserver.context.ApplyTransactionContext;
17 import com.tc.objectserver.context.CommitTransactionContext;
18 import com.tc.objectserver.context.LookupEventContext;
19 import com.tc.objectserver.context.RecallObjectsContext;
20 import com.tc.objectserver.core.api.TestDNA;
21 import com.tc.objectserver.core.impl.TestManagedObject;
22 import com.tc.objectserver.gtx.TestGlobalTransactionManager;
23 import com.tc.objectserver.impl.TestObjectManager;
24 import com.tc.test.TCTestCase;
25 import com.tc.util.SequenceID;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37
38 public class TransactionalObjectManagerTest extends TCTestCase {
39
40   TestObjectManager objectManager;
41   TransactionSequencer sequencer;
42   TestTransactionalStageCoordinator coordinator;
43   private TransactionalObjectManagerImpl txObjectManager;
44   private TestGlobalTransactionManager gtxMgr;
45
46   public void setUp() {
47     objectManager = new TestObjectManager();
48     sequencer = new TransactionSequencer();
49     coordinator = new TestTransactionalStageCoordinator();
50     gtxMgr = new TestGlobalTransactionManager();
51     txObjectManager = new TransactionalObjectManagerImpl(objectManager, sequencer, gtxMgr, coordinator);
52
53   }
54
55   // This test is added to reproduce a failure. More test are needed for TransactionalObjectManager
56
public void testTxnObjectManagerRecallAllOnGC() throws Exception JavaDoc {
57
58     Map JavaDoc changes = new HashMap JavaDoc();
59
60     changes.put(new ObjectID(1), new TestDNA(new ObjectID(1)));
61     changes.put(new ObjectID(2), new TestDNA(new ObjectID(2)));
62
63     ServerTransaction stxn1 = new ServerTransactionImpl(new TxnBatchID(1), new TransactionID(1), new SequenceID(1),
64                                                         new LockID[0], new ChannelID(2),
65                                                         new ArrayList JavaDoc(changes.values()), new ObjectStringSerializer(),
66                                                         Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList JavaDoc(),
67                                                         DmiDescriptor.EMPTY_ARRAY);
68     List JavaDoc txns = new ArrayList JavaDoc();
69     txns.add(stxn1);
70
71     txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST);
72
73     // Lookup context should have been fired
74
LookupEventContext loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
75     assertNotNull(loc);
76     assertTrue(coordinator.lookupSink.queue.isEmpty());
77
78     txObjectManager.lookupObjectsForTransactions();
79
80     // for txn1 - ObjectID 1, 2/home/ssubbiah/2.2.1/code/base/tcbuild check_prep dso-l2 unit
81
Object JavaDoc args[] = (Object JavaDoc[]) objectManager.lookupObjectForCreateIfNecessaryContexts.take();
82     assertNotNull(args);
83
84     // Apply should not have been called as we dont have Obejct 1, 2
85
ApplyTransactionContext aoc = (ApplyTransactionContext) coordinator.applySink.queue.remove(0);
86     assertTrue(stxn1 == aoc.getTxn());
87     assertNotNull(aoc);
88     assertTrue(coordinator.applySink.queue.isEmpty());
89
90     // Next txn
91
changes.put(new ObjectID(3), new TestDNA(new ObjectID(3)));
92     changes.put(new ObjectID(4), new TestDNA(new ObjectID(4)));
93
94     ServerTransaction stxn2 = new ServerTransactionImpl(new TxnBatchID(2), new TransactionID(2), new SequenceID(1),
95                                                         new LockID[0], new ChannelID(2),
96                                                         new ArrayList JavaDoc(changes.values()), new ObjectStringSerializer(),
97                                                         Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList JavaDoc(),
98                                                         DmiDescriptor.EMPTY_ARRAY);
99
100     txns.clear();
101     txns.add(stxn2);
102
103     txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST);
104
105     // Lookup context should have been fired
106
loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
107     assertNotNull(loc);
108     assertTrue(coordinator.lookupSink.queue.isEmpty());
109
110     objectManager.makePending = true;
111     txObjectManager.lookupObjectsForTransactions();
112
113     // for txn2, ObjectID 3, 4
114
args = (Object JavaDoc[]) objectManager.lookupObjectForCreateIfNecessaryContexts.take();
115     assertNotNull(args);
116
117     // Apply and commit complete for the first transaction
118
txObjectManager.applyTransactionComplete(stxn1.getServerTransactionID());
119     ApplyCompleteEventContext acec = (ApplyCompleteEventContext) coordinator.applyCompleteSink.queue.remove(0);
120     assertNotNull(acec);
121     assertTrue(coordinator.applyCompleteSink.queue.isEmpty());
122
123     txObjectManager.processApplyComplete();
124     CommitTransactionContext ctc = (CommitTransactionContext) coordinator.commitSink.queue.remove(0);
125     assertNotNull(ctc);
126     assertTrue(coordinator.commitSink.queue.isEmpty());
127
128     txObjectManager.commitTransactionsComplete(ctc);
129     Collection JavaDoc applied = ctc.getAppliedServerTransactionIDs();
130     assertTrue(applied.size() == 1);
131     assertEquals(stxn1.getServerTransactionID(), applied.iterator().next());
132     Collection JavaDoc objects = ctc.getObjects();
133     assertTrue(objects.size() == 2);
134
135     Set JavaDoc recd = new HashSet JavaDoc();
136     TestManagedObject tmo;
137     for (Iterator JavaDoc i = objects.iterator(); i.hasNext();) {
138       tmo = (TestManagedObject) i.next();
139       recd.add(tmo.getID());
140     }
141
142     Set JavaDoc expected = new HashSet JavaDoc();
143     expected.add(new ObjectID(1));
144     expected.add(new ObjectID(2));
145
146     assertEquals(expected, recd);
147
148     // Process the request for 3, 4
149
objectManager.makePending = false;
150     objectManager.processPending(args);
151
152     // Lookup context should have been fired
153
loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
154     assertNotNull(loc);
155     assertTrue(coordinator.lookupSink.queue.isEmpty());
156
157     // Dont give out 1,2
158
objectManager.makePending = true;
159     txObjectManager.lookupObjectsForTransactions();
160
161     // for txn2, ObjectID 1, 2
162
args = (Object JavaDoc[]) objectManager.lookupObjectForCreateIfNecessaryContexts.take();
163     assertNotNull(args);
164
165     // Apply should not have been called as we dont have Obejct 1, 2
166
assertTrue(coordinator.applySink.queue.isEmpty());
167
168     // now do a recall.
169
txObjectManager.recallAllCheckedoutObject();
170     RecallObjectsContext roc = (RecallObjectsContext) coordinator.recallSink.queue.remove(0);
171     assertNotNull(roc);
172     assertTrue(coordinator.recallSink.queue.isEmpty());
173
174     // process recall
175
txObjectManager.recallCheckedoutObject(roc);
176
177     // check 2 and only 2 object are released
178
Collection JavaDoc released = (Collection JavaDoc) objectManager.releaseAllQueue.take();
179     assertNotNull(released);
180
181     System.err.println("Released = " + released);
182
183     assertEquals(2, released.size());
184
185     HashSet JavaDoc ids_expected = new HashSet JavaDoc();
186     ids_expected.add(new ObjectID(3));
187     ids_expected.add(new ObjectID(4));
188
189     HashSet JavaDoc ids_recd = new HashSet JavaDoc();
190
191     for (Iterator JavaDoc i = released.iterator(); i.hasNext();) {
192       tmo = (TestManagedObject) i.next();
193       ids_recd.add(tmo.getID());
194     }
195
196     assertEquals(ids_expected, ids_recd);
197   }
198
199   // This test is added to reproduce a failure. More test are needed for TransactionalObjectManager
200
// This test is only slightly different from the above on in that the final recall should happen for 3 objects
201
public void testTxnObjectManagerRecallAllOnGCCase2() throws Exception JavaDoc {
202
203     Map JavaDoc changes = new HashMap JavaDoc();
204
205     changes.put(new ObjectID(1), new TestDNA(new ObjectID(1)));
206     changes.put(new ObjectID(2), new TestDNA(new ObjectID(2)));
207
208     ServerTransaction stxn1 = new ServerTransactionImpl(new TxnBatchID(1), new TransactionID(1), new SequenceID(1),
209                                                         new LockID[0], new ChannelID(2),
210                                                         new ArrayList JavaDoc(changes.values()), new ObjectStringSerializer(),
211                                                         Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList JavaDoc(),
212                                                         DmiDescriptor.EMPTY_ARRAY);
213     List JavaDoc txns = new ArrayList JavaDoc();
214     txns.add(stxn1);
215
216     txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST);
217
218     // Lookup context should have been fired
219
LookupEventContext loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
220     assertNotNull(loc);
221     assertTrue(coordinator.lookupSink.queue.isEmpty());
222
223     txObjectManager.lookupObjectsForTransactions();
224
225     // for txn1 - ObjectID 1, 2
226
Object JavaDoc args[] = (Object JavaDoc[]) objectManager.lookupObjectForCreateIfNecessaryContexts.take();
227     assertNotNull(args);
228
229     // Apply should have been called as we have Obejct 1, 2
230
ApplyTransactionContext aoc = (ApplyTransactionContext) coordinator.applySink.queue.remove(0);
231     assertTrue(stxn1 == aoc.getTxn());
232     assertNotNull(aoc);
233     assertTrue(coordinator.applySink.queue.isEmpty());
234
235     // Next txn
236
changes.put(new ObjectID(3), new TestDNA(new ObjectID(3)));
237     changes.put(new ObjectID(4), new TestDNA(new ObjectID(4)));
238
239     ServerTransaction stxn2 = new ServerTransactionImpl(new TxnBatchID(2), new TransactionID(2), new SequenceID(1),
240                                                         new LockID[0], new ChannelID(2),
241                                                         new ArrayList JavaDoc(changes.values()), new ObjectStringSerializer(),
242                                                         Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList JavaDoc(),
243                                                         DmiDescriptor.EMPTY_ARRAY);
244
245     txns.clear();
246     txns.add(stxn2);
247
248     txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST);
249
250     // Lookup context should have been fired
251
loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
252     assertNotNull(loc);
253     assertTrue(coordinator.lookupSink.queue.isEmpty());
254
255     objectManager.makePending = true;
256     txObjectManager.lookupObjectsForTransactions();
257
258     // for txn2, ObjectID 3, 4
259
args = (Object JavaDoc[]) objectManager.lookupObjectForCreateIfNecessaryContexts.take();
260     assertNotNull(args);
261
262     // Apply and commit complete for the first transaction
263
txObjectManager.applyTransactionComplete(stxn1.getServerTransactionID());
264     ApplyCompleteEventContext acec = (ApplyCompleteEventContext) coordinator.applyCompleteSink.queue.remove(0);
265     assertNotNull(acec);
266     assertTrue(coordinator.applyCompleteSink.queue.isEmpty());
267
268     txObjectManager.processApplyComplete();
269     CommitTransactionContext ctc = (CommitTransactionContext) coordinator.commitSink.queue.remove(0);
270     assertNotNull(ctc);
271     assertTrue(coordinator.commitSink.queue.isEmpty());
272
273     txObjectManager.commitTransactionsComplete(ctc);
274     Collection JavaDoc applied = ctc.getAppliedServerTransactionIDs();
275     assertTrue(applied.size() == 1);
276     assertEquals(stxn1.getServerTransactionID(), applied.iterator().next());
277     Collection JavaDoc objects = ctc.getObjects();
278     assertTrue(objects.size() == 2);
279
280     Set JavaDoc recd = new HashSet JavaDoc();
281     TestManagedObject tmo;
282     for (Iterator JavaDoc i = objects.iterator(); i.hasNext();) {
283       tmo = (TestManagedObject) i.next();
284       recd.add(tmo.getID());
285     }
286
287     Set JavaDoc expected = new HashSet JavaDoc();
288     expected.add(new ObjectID(1));
289     expected.add(new ObjectID(2));
290
291     assertEquals(expected, recd);
292
293     // Process the request for 3, 4
294
objectManager.makePending = false;
295     objectManager.processPending(args);
296
297     // Lookup context should have been fired
298
loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
299     assertNotNull(loc);
300     assertTrue(coordinator.lookupSink.queue.isEmpty());
301
302     // Dont give out 1,2
303
objectManager.makePending = true;
304     txObjectManager.lookupObjectsForTransactions();
305
306     // for txn2, ObjectID 1, 2
307
args = (Object JavaDoc[]) objectManager.lookupObjectForCreateIfNecessaryContexts.take();
308     assertNotNull(args);
309
310     // Apply should not have been called as we dont have Obejct 1, 2
311
assertTrue(coordinator.applySink.queue.isEmpty());
312
313     // --------------------------------This is where its differernt from case 1-------------------------
314
// Process the request for 5 and 6
315
changes.clear();
316     changes.put(new ObjectID(5), new TestDNA(new ObjectID(5)));
317     ServerTransaction stxn3 = new ServerTransactionImpl(new TxnBatchID(3), new TransactionID(3), new SequenceID(2),
318                                                         new LockID[0], new ChannelID(2),
319                                                         new ArrayList JavaDoc(changes.values()), new ObjectStringSerializer(),
320                                                         Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList JavaDoc(),
321                                                         DmiDescriptor.EMPTY_ARRAY);
322
323     txns.clear();
324     txns.add(stxn3);
325     txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST);
326
327     // Lookup context should have been fired
328
loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
329     assertNotNull(loc);
330     assertTrue(coordinator.lookupSink.queue.isEmpty());
331
332     objectManager.makePending = false;
333     txObjectManager.lookupObjectsForTransactions();
334
335     // for txn3 - ObjectID 5
336
args = (Object JavaDoc[]) objectManager.lookupObjectForCreateIfNecessaryContexts.take();
337     assertNotNull(args);
338
339     // Apply should have been called as we have Object 5
340
aoc = (ApplyTransactionContext) coordinator.applySink.queue.remove(0);
341     assertTrue(stxn3 == aoc.getTxn());
342     assertNotNull(aoc);
343     assertTrue(coordinator.applySink.queue.isEmpty());
344
345     // Next Txn , Object 5, 6
346
changes.put(new ObjectID(6), new TestDNA(new ObjectID(6)));
347     ServerTransaction stxn4 = new ServerTransactionImpl(new TxnBatchID(4), new TransactionID(4), new SequenceID(3),
348                                                         new LockID[0], new ChannelID(2),
349                                                         new ArrayList JavaDoc(changes.values()), new ObjectStringSerializer(),
350                                                         Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList JavaDoc(),
351                                                         DmiDescriptor.EMPTY_ARRAY);
352
353     txns.clear();
354     txns.add(stxn4);
355     txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST);
356
357     // Lookup context should have been fired
358
loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
359     assertNotNull(loc);
360     assertTrue(coordinator.lookupSink.queue.isEmpty());
361
362     objectManager.makePending = true;
363     txObjectManager.lookupObjectsForTransactions();
364
365     // for txn4, ObjectID 6
366
args = (Object JavaDoc[]) objectManager.lookupObjectForCreateIfNecessaryContexts.take();
367     assertNotNull(args);
368
369     // Apply and commit complete for the 3'rd transaction
370
txObjectManager.applyTransactionComplete(stxn3.getServerTransactionID());
371     acec = (ApplyCompleteEventContext) coordinator.applyCompleteSink.queue.remove(0);
372     assertNotNull(acec);
373     assertTrue(coordinator.applyCompleteSink.queue.isEmpty());
374
375     txObjectManager.processApplyComplete();
376     ctc = (CommitTransactionContext) coordinator.commitSink.queue.remove(0);
377     assertNotNull(ctc);
378     assertTrue(coordinator.commitSink.queue.isEmpty());
379
380     txObjectManager.commitTransactionsComplete(ctc);
381     applied = ctc.getAppliedServerTransactionIDs();
382     assertTrue(applied.size() == 1);
383     assertEquals(stxn3.getServerTransactionID(), applied.iterator().next());
384     objects = ctc.getObjects();
385     assertTrue(objects.size() == 1);
386
387     recd = new HashSet JavaDoc();
388     for (Iterator JavaDoc i = objects.iterator(); i.hasNext();) {
389       tmo = (TestManagedObject) i.next();
390       recd.add(tmo.getID());
391     }
392
393     expected = new HashSet JavaDoc();
394     expected.add(new ObjectID(5));
395
396     assertEquals(expected, recd);
397
398     // Process the request for 6, still 1 and 2 is not given out
399
objectManager.makePending = false;
400     objectManager.processPending(args);
401
402     // Lookup context should have been fired
403
loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0);
404     assertNotNull(loc);
405     assertTrue(coordinator.lookupSink.queue.isEmpty());
406
407     // Now before look up thread got a chance to execute, recall gets executed.
408
objectManager.makePending = true; // Since we dont want to give out 5 when we do a recall commit
409
// -------------------------------------------------------------------------------------------------
410

411     // now do a recall.
412
txObjectManager.recallAllCheckedoutObject();
413     RecallObjectsContext roc = (RecallObjectsContext) coordinator.recallSink.queue.remove(0);
414     assertNotNull(roc);
415     assertTrue(coordinator.recallSink.queue.isEmpty());
416
417     // process recall
418
txObjectManager.recallCheckedoutObject(roc);
419
420     // check that all 3 objects (3,4,6) are released -- different from case 1
421
Collection JavaDoc released = (Collection JavaDoc) objectManager.releaseAllQueue.take();
422     assertNotNull(released);
423
424     System.err.println("Released = " + released);
425
426     assertEquals(3, released.size());
427
428     HashSet JavaDoc ids_expected = new HashSet JavaDoc();
429     ids_expected.add(new ObjectID(3));
430     ids_expected.add(new ObjectID(4));
431     ids_expected.add(new ObjectID(6));
432
433     HashSet JavaDoc ids_recd = new HashSet JavaDoc();
434
435     for (Iterator JavaDoc i = released.iterator(); i.hasNext();) {
436       tmo = (TestManagedObject) i.next();
437       ids_recd.add(tmo.getID());
438     }
439
440     assertEquals(ids_expected, ids_recd);
441   }
442
443 }
444
Popular Tags