1 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 ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.Set ; 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 public void testTxnObjectManagerRecallAllOnGC() throws Exception { 57 58 Map changes = new HashMap (); 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 (changes.values()), new ObjectStringSerializer(), 66 Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList (), 67 DmiDescriptor.EMPTY_ARRAY); 68 List txns = new ArrayList (); 69 txns.add(stxn1); 70 71 txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST); 72 73 LookupEventContext loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0); 75 assertNotNull(loc); 76 assertTrue(coordinator.lookupSink.queue.isEmpty()); 77 78 txObjectManager.lookupObjectsForTransactions(); 79 80 Object args[] = (Object []) objectManager.lookupObjectForCreateIfNecessaryContexts.take(); 82 assertNotNull(args); 83 84 ApplyTransactionContext aoc = (ApplyTransactionContext) coordinator.applySink.queue.remove(0); 86 assertTrue(stxn1 == aoc.getTxn()); 87 assertNotNull(aoc); 88 assertTrue(coordinator.applySink.queue.isEmpty()); 89 90 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 (changes.values()), new ObjectStringSerializer(), 97 Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList (), 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 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 args = (Object []) objectManager.lookupObjectForCreateIfNecessaryContexts.take(); 115 assertNotNull(args); 116 117 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 applied = ctc.getAppliedServerTransactionIDs(); 130 assertTrue(applied.size() == 1); 131 assertEquals(stxn1.getServerTransactionID(), applied.iterator().next()); 132 Collection objects = ctc.getObjects(); 133 assertTrue(objects.size() == 2); 134 135 Set recd = new HashSet (); 136 TestManagedObject tmo; 137 for (Iterator i = objects.iterator(); i.hasNext();) { 138 tmo = (TestManagedObject) i.next(); 139 recd.add(tmo.getID()); 140 } 141 142 Set expected = new HashSet (); 143 expected.add(new ObjectID(1)); 144 expected.add(new ObjectID(2)); 145 146 assertEquals(expected, recd); 147 148 objectManager.makePending = false; 150 objectManager.processPending(args); 151 152 loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0); 154 assertNotNull(loc); 155 assertTrue(coordinator.lookupSink.queue.isEmpty()); 156 157 objectManager.makePending = true; 159 txObjectManager.lookupObjectsForTransactions(); 160 161 args = (Object []) objectManager.lookupObjectForCreateIfNecessaryContexts.take(); 163 assertNotNull(args); 164 165 assertTrue(coordinator.applySink.queue.isEmpty()); 167 168 txObjectManager.recallAllCheckedoutObject(); 170 RecallObjectsContext roc = (RecallObjectsContext) coordinator.recallSink.queue.remove(0); 171 assertNotNull(roc); 172 assertTrue(coordinator.recallSink.queue.isEmpty()); 173 174 txObjectManager.recallCheckedoutObject(roc); 176 177 Collection released = (Collection ) objectManager.releaseAllQueue.take(); 179 assertNotNull(released); 180 181 System.err.println("Released = " + released); 182 183 assertEquals(2, released.size()); 184 185 HashSet ids_expected = new HashSet (); 186 ids_expected.add(new ObjectID(3)); 187 ids_expected.add(new ObjectID(4)); 188 189 HashSet ids_recd = new HashSet (); 190 191 for (Iterator 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 public void testTxnObjectManagerRecallAllOnGCCase2() throws Exception { 202 203 Map changes = new HashMap (); 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 (changes.values()), new ObjectStringSerializer(), 211 Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList (), 212 DmiDescriptor.EMPTY_ARRAY); 213 List txns = new ArrayList (); 214 txns.add(stxn1); 215 216 txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST); 217 218 LookupEventContext loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0); 220 assertNotNull(loc); 221 assertTrue(coordinator.lookupSink.queue.isEmpty()); 222 223 txObjectManager.lookupObjectsForTransactions(); 224 225 Object args[] = (Object []) objectManager.lookupObjectForCreateIfNecessaryContexts.take(); 227 assertNotNull(args); 228 229 ApplyTransactionContext aoc = (ApplyTransactionContext) coordinator.applySink.queue.remove(0); 231 assertTrue(stxn1 == aoc.getTxn()); 232 assertNotNull(aoc); 233 assertTrue(coordinator.applySink.queue.isEmpty()); 234 235 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 (changes.values()), new ObjectStringSerializer(), 242 Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList (), 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 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 args = (Object []) objectManager.lookupObjectForCreateIfNecessaryContexts.take(); 260 assertNotNull(args); 261 262 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 applied = ctc.getAppliedServerTransactionIDs(); 275 assertTrue(applied.size() == 1); 276 assertEquals(stxn1.getServerTransactionID(), applied.iterator().next()); 277 Collection objects = ctc.getObjects(); 278 assertTrue(objects.size() == 2); 279 280 Set recd = new HashSet (); 281 TestManagedObject tmo; 282 for (Iterator i = objects.iterator(); i.hasNext();) { 283 tmo = (TestManagedObject) i.next(); 284 recd.add(tmo.getID()); 285 } 286 287 Set expected = new HashSet (); 288 expected.add(new ObjectID(1)); 289 expected.add(new ObjectID(2)); 290 291 assertEquals(expected, recd); 292 293 objectManager.makePending = false; 295 objectManager.processPending(args); 296 297 loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0); 299 assertNotNull(loc); 300 assertTrue(coordinator.lookupSink.queue.isEmpty()); 301 302 objectManager.makePending = true; 304 txObjectManager.lookupObjectsForTransactions(); 305 306 args = (Object []) objectManager.lookupObjectForCreateIfNecessaryContexts.take(); 308 assertNotNull(args); 309 310 assertTrue(coordinator.applySink.queue.isEmpty()); 312 313 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 (changes.values()), new ObjectStringSerializer(), 320 Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList (), 321 DmiDescriptor.EMPTY_ARRAY); 322 323 txns.clear(); 324 txns.add(stxn3); 325 txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST); 326 327 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 args = (Object []) objectManager.lookupObjectForCreateIfNecessaryContexts.take(); 337 assertNotNull(args); 338 339 aoc = (ApplyTransactionContext) coordinator.applySink.queue.remove(0); 341 assertTrue(stxn3 == aoc.getTxn()); 342 assertNotNull(aoc); 343 assertTrue(coordinator.applySink.queue.isEmpty()); 344 345 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 (changes.values()), new ObjectStringSerializer(), 350 Collections.EMPTY_MAP, TxnType.NORMAL, new LinkedList (), 351 DmiDescriptor.EMPTY_ARRAY); 352 353 txns.clear(); 354 txns.add(stxn4); 355 txObjectManager.addTransactions(new ChannelID(2), txns, Collections.EMPTY_LIST); 356 357 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 args = (Object []) objectManager.lookupObjectForCreateIfNecessaryContexts.take(); 367 assertNotNull(args); 368 369 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 (); 388 for (Iterator i = objects.iterator(); i.hasNext();) { 389 tmo = (TestManagedObject) i.next(); 390 recd.add(tmo.getID()); 391 } 392 393 expected = new HashSet (); 394 expected.add(new ObjectID(5)); 395 396 assertEquals(expected, recd); 397 398 objectManager.makePending = false; 400 objectManager.processPending(args); 401 402 loc = (LookupEventContext) coordinator.lookupSink.queue.remove(0); 404 assertNotNull(loc); 405 assertTrue(coordinator.lookupSink.queue.isEmpty()); 406 407 objectManager.makePending = true; 411 txObjectManager.recallAllCheckedoutObject(); 413 RecallObjectsContext roc = (RecallObjectsContext) coordinator.recallSink.queue.remove(0); 414 assertNotNull(roc); 415 assertTrue(coordinator.recallSink.queue.isEmpty()); 416 417 txObjectManager.recallCheckedoutObject(roc); 419 420 Collection released = (Collection ) objectManager.releaseAllQueue.take(); 422 assertNotNull(released); 423 424 System.err.println("Released = " + released); 425 426 assertEquals(3, released.size()); 427 428 HashSet ids_expected = new HashSet (); 429 ids_expected.add(new ObjectID(3)); 430 ids_expected.add(new ObjectID(4)); 431 ids_expected.add(new ObjectID(6)); 432 433 HashSet ids_recd = new HashSet (); 434 435 for (Iterator 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 |