1 7 package org.jboss.cache.optimistic; 8 9 import org.jboss.cache.CacheImpl; 10 import org.jboss.cache.GlobalTransaction; 11 import org.jboss.cache.OptimisticTransactionEntry; 12 import org.jboss.cache.TransactionTable; 13 import org.jboss.cache.config.Configuration; 14 import org.jboss.cache.loader.SamplePojo; 15 import org.jboss.cache.marshall.MethodCall; 16 import org.jboss.cache.marshall.MethodCallFactory; 17 import org.jboss.cache.marshall.MethodDeclarations; 18 import org.jboss.cache.transaction.DummyTransactionManager; 19 import org.jgroups.Address; 20 21 import javax.transaction.RollbackException ; 22 import javax.transaction.Transaction ; 23 import java.io.DataInputStream ; 24 import java.io.DataOutputStream ; 25 import java.io.ObjectInput ; 26 import java.io.ObjectOutput ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 30 33 public class OptimisticReplicationInterceptorTest extends AbstractOptimisticTestCase 34 { 35 private CacheImpl cache; 36 37 40 public OptimisticReplicationInterceptorTest(String name) 41 { 42 super(name); 43 } 44 45 protected void setUp() throws Exception 46 { 47 super.setUp(); 48 cache = createCache(); 49 } 50 51 protected void tearDown() 52 { 53 super.tearDown(); 54 destroyCache(cache); 55 } 56 57 public void testLocalTransaction() throws Exception 58 { 59 MockInterceptor dummy = new MockInterceptor(); 60 dummy.setCache(cache); 61 62 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 63 64 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 65 assertNull(mgr.getTransaction()); 66 67 mgr.begin(); 68 69 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 70 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 71 72 SamplePojo pojo = new SamplePojo(21, "test"); 73 74 cache.put("/one/two", "key1", pojo); 75 76 mgr.commit(); 77 78 assertNull(mgr.getTransaction()); 79 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 80 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 81 82 84 List calls = dummy.getAllCalled(); 85 86 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 87 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 88 } 89 90 public void testRollbackTransaction() throws Exception 91 { 92 MockInterceptor dummy = new MockInterceptor(); 93 dummy.setCache(cache); 94 95 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 96 97 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 98 assertNull(mgr.getTransaction()); 99 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 100 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 101 102 SamplePojo pojo = new SamplePojo(21, "test"); 103 mgr.begin(); 104 cache.put("/one/two", "key1", pojo); 105 mgr.rollback(); 106 assertNull(mgr.getTransaction()); 107 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 108 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 109 110 112 List calls = dummy.getAllCalled(); 113 114 assertEquals(1, calls.size()); 115 assertEquals(MethodDeclarations.rollbackMethod, calls.get(0)); 116 } 117 118 public void testRemotePrepareTransaction() throws Exception 119 { 120 MockInterceptor dummy = new MockInterceptor(); 121 dummy.setCache(cache); 122 123 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 124 125 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 126 127 mgr.begin(); 129 Transaction tx = mgr.getTransaction(); 130 131 cache.getCurrentTransaction(tx); 133 134 SamplePojo pojo = new SamplePojo(21, "test"); 135 136 cache.put("/one/two", "key1", pojo); 137 138 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 139 TransactionTable table = cache.getTransactionTable(); 140 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 141 assertNotNull(mgr.getTransaction()); 142 mgr.commit(); 143 144 145 GlobalTransaction remoteGtx = new GlobalTransaction(); 146 147 remoteGtx.setAddress(new TestAddress()); 148 MethodCall meth = entry.getModifications().get(0); 150 151 meth.getArgs()[0] = remoteGtx; 152 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), false); 154 try 155 { 156 cache._replicate(prepareMethod); 157 } 158 catch (Throwable t) 159 { 160 fail(); 161 } 162 163 assertNull(mgr.getTransaction()); 165 166 assertNotNull(table.get(remoteGtx)); 168 assertNotNull(table.getLocalTransaction(remoteGtx)); 169 assertEquals(1, table.get(remoteGtx).getModifications().size()); 171 172 OptimisticTransactionEntry opEntry = (OptimisticTransactionEntry) table.get(gtx); 174 175 assertEquals(3, entry.getTransactionWorkSpace().getNodes().size()); 176 assertEquals(1, entry.getModifications().size()); 177 List calls = dummy.getAllCalled(); 178 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 179 180 181 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions()); 182 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions()); 183 184 } 185 186 187 public void testRemoteRollbackTransaction() throws Exception 188 { 189 MockInterceptor dummy = new MockInterceptor(); 190 dummy.setCache(cache); 191 192 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 193 194 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 195 196 mgr.begin(); 198 Transaction tx = mgr.getTransaction(); 199 200 cache.getCurrentTransaction(tx); 202 203 SamplePojo pojo = new SamplePojo(21, "test"); 204 205 cache.put("/one/two", "key1", pojo); 206 207 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 208 TransactionTable table = cache.getTransactionTable(); 209 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 210 assertNotNull(mgr.getTransaction()); 211 mgr.commit(); 212 213 214 GlobalTransaction remoteGtx = new GlobalTransaction(); 215 216 remoteGtx.setAddress(new TestAddress()); 217 MethodCall meth = entry.getModifications().get(0); 219 220 meth.getArgs()[0] = remoteGtx; 221 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), false); 223 try 224 { 225 cache._replicate(prepareMethod); 226 } 227 catch (Throwable t) 228 { 229 fail(); 230 } 231 232 assertNull(mgr.getTransaction()); 234 235 assertNotNull(table.get(remoteGtx)); 237 assertNotNull(table.getLocalTransaction(remoteGtx)); 238 assertEquals(1, table.get(remoteGtx).getModifications().size()); 240 241 OptimisticTransactionEntry opEntry = (OptimisticTransactionEntry) table.get(gtx); 243 244 assertEquals(3, entry.getTransactionWorkSpace().getNodes().size()); 245 assertEquals(1, entry.getModifications().size()); 246 List calls = dummy.getAllCalled(); 247 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 248 249 250 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions()); 251 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions()); 252 253 MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, remoteGtx); 255 try 256 { 257 cache._replicate(rollbackMethod); 258 } 259 catch (Throwable t) 260 { 261 fail(); 262 } 263 assertNull(mgr.getTransaction()); 265 assertEquals(MethodDeclarations.rollbackMethod, calls.get(3)); 266 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 267 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 268 } 269 270 271 public void testRemoteCommitNoPrepareTransaction() throws Exception 272 { 273 MockInterceptor dummy = new MockInterceptor(); 274 dummy.setCache(cache); 275 276 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 277 278 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 279 280 mgr.begin(); 282 Transaction tx = mgr.getTransaction(); 283 284 cache.getCurrentTransaction(tx); 286 287 SamplePojo pojo = new SamplePojo(21, "test"); 288 289 cache.put("/one/two", "key1", pojo); 290 291 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 292 TransactionTable table = cache.getTransactionTable(); 293 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 294 assertNotNull(mgr.getTransaction()); 295 mgr.commit(); 296 297 298 GlobalTransaction remoteGtx = new GlobalTransaction(); 299 300 remoteGtx.setAddress(new TestAddress()); 301 MethodCall meth = entry.getModifications().get(0); 303 304 meth.getArgs()[0] = remoteGtx; 305 306 307 List calls = dummy.getAllCalled(); 308 assertEquals(2, calls.size()); 309 310 311 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 312 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 313 314 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, remoteGtx); 316 try 317 { 318 cache._replicate(commitMethod); 319 fail(); 320 } 321 catch (Throwable t) 322 { 323 assertTrue(t instanceof RuntimeException ); 324 } 326 assertNull(mgr.getTransaction()); 328 assertEquals(2, calls.size()); 329 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 330 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 331 } 332 333 334 public void testRemoteRollbackNoPrepareTransaction() throws Throwable 335 { 336 MockInterceptor dummy = new MockInterceptor(); 337 dummy.setCache(cache); 338 339 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 340 341 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 342 343 mgr.begin(); 345 Transaction tx = mgr.getTransaction(); 346 347 cache.getCurrentTransaction(tx); 349 350 SamplePojo pojo = new SamplePojo(21, "test"); 351 352 cache.put("/one/two", "key1", pojo); 353 354 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 355 TransactionTable table = cache.getTransactionTable(); 356 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 357 assertNotNull(mgr.getTransaction()); 358 mgr.commit(); 359 360 361 GlobalTransaction remoteGtx = new GlobalTransaction(); 362 363 remoteGtx.setAddress(new TestAddress()); 364 MethodCall meth = entry.getModifications().get(0); 366 367 meth.getArgs()[0] = remoteGtx; 368 369 370 List calls = dummy.getAllCalled(); 371 assertEquals(2, calls.size()); 372 373 374 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 375 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 376 377 MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, remoteGtx); 379 380 cache._replicate(rollbackMethod); 381 assertTrue("Should be handled on the remote end without barfing, in the event of a rollback without a prepare", true); 382 383 assertNull(mgr.getTransaction()); 385 assertEquals(2, calls.size()); 386 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 387 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 388 } 389 390 391 public void testRemoteCommitTransaction() throws Exception 392 { 393 MockInterceptor dummy = new MockInterceptor(); 394 dummy.setCache(cache); 395 396 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 397 398 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 399 400 mgr.begin(); 402 Transaction tx = mgr.getTransaction(); 403 404 cache.getCurrentTransaction(tx); 406 407 SamplePojo pojo = new SamplePojo(21, "test"); 408 409 cache.put("/one/two", "key1", pojo); 410 411 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 412 TransactionTable table = cache.getTransactionTable(); 413 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 414 assertNotNull(mgr.getTransaction()); 415 mgr.commit(); 416 417 418 GlobalTransaction remoteGtx = new GlobalTransaction(); 419 420 remoteGtx.setAddress(new TestAddress()); 421 MethodCall meth = entry.getModifications().get(0); 423 424 meth.getArgs()[0] = remoteGtx; 425 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), false); 427 try 428 { 429 cache._replicate(prepareMethod); 430 } 431 catch (Throwable t) 432 { 433 fail(); 434 } 435 436 assertNull(mgr.getTransaction()); 438 439 assertNotNull(table.get(remoteGtx)); 441 assertNotNull(table.getLocalTransaction(remoteGtx)); 442 assertEquals(1, table.get(remoteGtx).getModifications().size()); 444 445 OptimisticTransactionEntry opEntry = (OptimisticTransactionEntry) table.get(gtx); 447 448 assertEquals(3, entry.getTransactionWorkSpace().getNodes().size()); 449 assertEquals(1, entry.getModifications().size()); 450 List calls = dummy.getAllCalled(); 451 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 452 453 454 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions()); 455 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions()); 456 457 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, remoteGtx); 459 try 460 { 461 cache._replicate(commitMethod); 462 } 463 catch (Throwable t) 464 { 465 fail(); 466 } 467 assertNull(mgr.getTransaction()); 469 assertEquals(MethodDeclarations.commitMethod, calls.get(3)); 470 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 471 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 472 } 473 474 475 public void testTwoWayRemoteCacheBroadcast() throws Exception 476 { 477 destroyCache(cache); 478 cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC); 479 480 MockInterceptor dummy = new MockInterceptor(); 481 dummy.setCache(cache); 482 483 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 484 485 CacheImpl cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC); 486 MockInterceptor dummy2 = new MockInterceptor(); 487 dummy.setCache(cache2); 488 489 cache2.setInterceptorChain(getAlteredInterceptorChain(dummy2, cache2, true)); 490 491 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 492 493 mgr.begin(); 495 Transaction tx = mgr.getTransaction(); 496 497 cache.getCurrentTransaction(tx); 499 500 SamplePojo pojo = new SamplePojo(21, "test"); 501 502 cache.put("/one/two", "key1", pojo); 503 504 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 505 TransactionTable table = cache.getTransactionTable(); 506 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 507 assertNotNull(mgr.getTransaction()); 508 mgr.commit(); 509 510 511 assertNull(mgr.getTransaction()); 512 513 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 515 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 516 517 518 assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions()); 519 assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions()); 520 521 522 List calls = dummy.getAllCalled(); 523 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 524 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 525 526 List calls2 = dummy2.getAllCalled(); 527 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls2.get(0)); 528 assertEquals(MethodDeclarations.commitMethod, calls2.get(1)); 529 530 destroyCache(cache2); 531 } 532 533 534 public void testFailurePrepareRemoteCacheBroadcast() throws Exception 535 { 536 destroyCache(cache); 537 cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC); 538 539 MockInterceptor dummy = new MockInterceptor(); 540 dummy.setCache(cache); 541 542 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 543 544 CacheImpl cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC); 545 MockFailureInterceptor dummy2 = new MockFailureInterceptor(); 546 List failures = new ArrayList (); 547 failures.add(MethodDeclarations.optimisticPrepareMethod); 548 dummy2.setFailurelist(failures); 549 dummy.setCache(cache2); 550 551 cache2.setInterceptorChain(getAlteredInterceptorChain(dummy2, cache2, true)); 552 553 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 554 555 mgr.begin(); 557 Transaction tx = mgr.getTransaction(); 558 559 cache.getCurrentTransaction(tx); 561 562 SamplePojo pojo = new SamplePojo(21, "test"); 563 564 cache.put("/one/two", "key1", pojo); 565 566 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 567 TransactionTable table = cache.getTransactionTable(); 568 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 569 assertNotNull(mgr.getTransaction()); 570 try 571 { 572 mgr.commit(); 573 } 574 catch (Exception e) 575 { 576 assertTrue(e instanceof RollbackException ); 577 } 578 579 580 assertNull(mgr.getTransaction()); 581 582 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 584 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 585 586 587 assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions()); 588 assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions()); 589 590 591 List calls = dummy.getAllCalled(); 592 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 593 assertEquals(MethodDeclarations.rollbackMethod, calls.get(1)); 594 595 List calls2 = dummy2.getAllCalled(); 597 assertEquals(MethodDeclarations.rollbackMethod, calls2.get(0)); 598 599 destroyCache(cache2); 600 } 601 602 603 public void testFailurePrepareLocalCacheBroadcast() throws Exception 604 { 605 606 destroyCache(cache); 607 cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC); 608 609 MockFailureInterceptor dummy = new MockFailureInterceptor(); 610 dummy.setCache(cache); 611 612 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 613 614 CacheImpl cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC); 615 MockInterceptor dummy2 = new MockInterceptor(); 616 dummy.setCache(cache2); 617 618 cache2.setInterceptorChain(getAlteredInterceptorChain(dummy2, cache2, true)); 619 620 List failures = new ArrayList (); 621 failures.add(MethodDeclarations.optimisticPrepareMethod); 622 dummy.setFailurelist(failures); 623 624 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 625 626 mgr.begin(); 628 Transaction tx = mgr.getTransaction(); 629 630 cache.getCurrentTransaction(tx); 632 633 SamplePojo pojo = new SamplePojo(21, "test"); 634 635 cache.put("/one/two", "key1", pojo); 636 637 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 638 TransactionTable table = cache.getTransactionTable(); 639 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 640 assertNotNull(mgr.getTransaction()); 641 try 642 { 643 mgr.commit(); 644 } 645 catch (Exception e) 646 { 647 assertTrue(e instanceof RollbackException ); 648 } 649 650 651 assertNull(mgr.getTransaction()); 652 653 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 655 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 656 657 658 assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions()); 659 assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions()); 660 661 662 List calls = dummy.getAllCalled(); 663 assertEquals(MethodDeclarations.rollbackMethod, calls.get(0)); 664 665 List calls2 = dummy2.getAllCalled(); 667 assertEquals(0, calls2.size()); 668 669 destroyCache(cache2); 670 } 671 672 static class TestAddress implements Address 673 { 674 public boolean isMulticastAddress() 675 { 676 return false; 677 } 678 679 public void readExternal(ObjectInput arg0) 680 { 681 } 682 683 public int size() 684 { 685 return 0; 686 } 687 688 public void writeExternal(ObjectOutput arg0) 689 { 690 } 691 692 public void writeTo(DataOutputStream arg0) 693 { 694 } 695 696 public void readFrom(DataInputStream arg0) 697 { 698 } 699 700 public int compareTo(Object arg0) 701 { 702 return 0; 703 } 704 } 705 706 } 707 | Popular Tags |