Search in sources :

Example 36 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class RemoteTransactionDUnitTest method testDestroyCreateConflation.

@Test
public void testDestroyCreateConflation() {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore = host.getVM(1);
    initAccessorAndDataStore(accessor, datastore, 0);
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region cust = getCache().getRegion(D_REFERENCE);
            cust.put("meow", "this is a meow, deal with it");
            cust.getAttributesMutator().addCacheListener(new OneUpdateCacheListener());
            cust.getAttributesMutator().setCacheWriter(new OneDestroyAndThenOneCreateCacheWriter());
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region cust = getCache().getRegion(D_REFERENCE);
            cust.getAttributesMutator().addCacheListener(new OneUpdateCacheListener());
            return null;
        }
    });
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.begin();
            Region cust = getCache().getRegion(D_REFERENCE);
            cust.destroy("meow");
            cust.create("meow", "this is the new meow, not the old meow");
            mgr.commit();
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region cust = getCache().getRegion(D_REFERENCE);
            OneUpdateCacheListener rat = (OneUpdateCacheListener) cust.getAttributes().getCacheListener();
            if (!rat.getSuccess()) {
                fail("The OneUpdateCacheListener didnt get an update");
            }
            return null;
        }
    });
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region cust = getCache().getRegion(D_REFERENCE);
            OneDestroyAndThenOneCreateCacheWriter wri = (OneDestroyAndThenOneCreateCacheWriter) cust.getAttributes().getCacheWriter();
            wri.checkSuccess();
            return null;
        }
    });
}
Also used : VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 37 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class RemoteTransactionDUnitTest method testNonInlineRemoteEvents.

/**
   * Install Listeners and verify that they are invoked after all tx events have been applied to the
   * cache see GEODE-278
   */
@Test
public void testNonInlineRemoteEvents() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String key1 = "nonInline-1";
    final String key2 = "nonInline-2";
    class NonInlineListener extends CacheListenerAdapter {

        boolean assertException = false;

        @Override
        public void afterCreate(EntryEvent event) {
            if (event.getKey().equals(key1)) {
                if (getCache().getRegion(D_REFERENCE).get(key2) == null) {
                    assertException = true;
                }
            }
        }
    }
    SerializableCallable createRegionWithListener = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            createRegion(false, 0, null);
            getCache().getRegion(D_REFERENCE).getAttributesMutator().addCacheListener(new NonInlineListener());
            return null;
        }
    };
    vm0.invoke(createRegionWithListener);
    vm1.invoke(createRegionWithListener);
    vm0.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region region = getCache().getRegion(D_REFERENCE);
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.begin();
            region.put(key1, "nonInlineValue-1");
            region.put(key2, "nonInlineValue-2");
            mgr.commit();
            return null;
        }
    });
    SerializableCallable verifyAssert = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            CacheListener[] listeners = getCache().getRegion(D_REFERENCE).getAttributes().getCacheListeners();
            for (CacheListener listener : listeners) {
                if (listener instanceof NonInlineListener) {
                    NonInlineListener l = (NonInlineListener) listener;
                    assertFalse(l.assertException);
                }
            }
            return null;
        }
    };
    vm0.invoke(verifyAssert);
    vm1.invoke(verifyAssert);
}
Also used : Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 38 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class RemoteTransactionDUnitTest method doTestTxFunction.

private void doTestTxFunction(final Executions e) {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore1 = host.getVM(1);
    VM datastore2 = host.getVM(2);
    initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
    SerializableCallable registerFunction = new SerializableCallable() {

        public Object call() throws Exception {
            FunctionService.registerFunction(new TXFunction());
            return null;
        }
    };
    accessor.invoke(registerFunction);
    datastore1.invoke(registerFunction);
    datastore2.invoke(registerFunction);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            PartitionedRegion custRegion = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            Set regions = new HashSet();
            regions.add(custRegion);
            regions.add(getGemfireCache().getRegion(ORDER));
            mgr.begin();
            try {
                switch(e) {
                    case OnRegion:
                        FunctionService.onRegion(custRegion).execute(TXFunction.id).getResult();
                        break;
                    case OnMember:
                        FunctionService.onMembers().execute(TXFunction.id).getResult();
                        break;
                }
                fail("Expected exception not thrown");
            } catch (TransactionException expected) {
            }
            try {
                InternalFunctionService.onRegions(regions).execute(TXFunction.id).getResult();
                fail("Expected exception not thrown");
            } catch (TransactionException expected) {
            }
            Set filter = new HashSet();
            filter.add(expectedCustId);
            switch(e) {
                case OnRegion:
                    FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
                    break;
                case OnMember:
                    DistributedMember owner = custRegion.getOwnerForKey(custRegion.getKeyInfo(expectedCustId));
                    FunctionService.onMember(owner).execute(TXFunction.id).getResult();
                    break;
            }
            TXStateProxy tx = mgr.internalSuspend();
            GemFireCacheImpl.getInstance().getLogger().warning("TX SUSPENDO:" + tx);
            assertNull(custRegion.get(expectedCustId));
            mgr.internalResume(tx);
            return null;
        }
    });
    final Integer txOnDatastore1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
    final Integer txOnDatastore2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
    assertEquals(1, txOnDatastore1 + txOnDatastore2);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTXMgr();
            mgr.commit();
            return null;
        }
    });
    datastore1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getGemfireCache().getRegion(CUSTOMER);
            assertEquals(expectedCustomer, custRegion.get(expectedCustId));
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            mgr.begin();
            PartitionedRegion custRegion = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            Set filter = new HashSet();
            filter.add(expectedCustId);
            switch(e) {
                case OnRegion:
                    FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
                    break;
                case OnMember:
                    DistributedMember owner = custRegion.getOwnerForKey(custRegion.getKeyInfo(expectedCustId));
                    FunctionService.onMember(owner).execute(TXFunction.id).getResult();
                    break;
            }
            TXStateProxy tx = mgr.internalSuspend();
            custRegion.put(expectedCustId, new Customer("Cust6", "updated6"));
            mgr.internalResume(tx);
            try {
                mgr.commit();
                fail("expected commit conflict not thrown");
            } catch (CommitConflictException expected) {
            }
            return null;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Customer(org.apache.geode.internal.cache.execute.data.Customer) Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionException(org.apache.geode.cache.TransactionException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet)

Example 39 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class MultiVMRegionTestCase method testTXSimpleOps.

/**
   * Tests that an entry update is propagated to other caches that have that same entry defined.
   */
@Test
public void testTXSimpleOps() throws Exception {
    assumeTrue(supportsTransactions());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
    if (getRegionAttributes().getScope().isGlobal() || getRegionAttributes().getDataPolicy().withPersistence()) {
        // just make sure transactions are not allowed on global or shared regions
        Region rgn = createRegion(getUniqueName());
        txMgr.begin();
        try {
            rgn.put("testTXSimpleOpsKey1", "val");
            fail("expected UnsupportedOperationException");
        } catch (UnsupportedOperationException ok) {
        }
        txMgr.rollback();
        rgn.localDestroyRegion();
        return;
    }
    final String rgnName = getUniqueName();
    SerializableRunnable create = new SerializableRunnable("testTXSimpleOps: Create Region") {

        @Override
        public void run() {
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = new MyTransactionListener();
            txMgr2.addListener(tl);
            assertEquals(null, tl.lastEvent);
            assertEquals(0, tl.afterCommitCount);
            assertEquals(0, tl.afterFailedCommitCount);
            assertEquals(0, tl.afterRollbackCount);
            assertEquals(0, tl.closeCount);
            try {
                Region rgn = createRegion(rgnName);
                CountingDistCacheListener cacheListener = new CountingDistCacheListener();
                rgn.getAttributesMutator().addCacheListener(cacheListener);
                cacheListener.assertCount(0, 0, 0, 0);
                getSystem().getLogWriter().info("testTXSimpleOps: Created region");
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    SerializableRunnable newKey = new SerializableRunnable("testTXSimpleOps: Create Region & Create Key") {

        @Override
        public void run() {
            try {
                Region root = getRootRegion();
                Region rgn = root.getSubregion(rgnName);
                rgn.create("key", null);
                CountingDistCacheListener cacheListener = (CountingDistCacheListener) rgn.getAttributes().getCacheListener();
                cacheListener.assertCount(0, 0, 0, 0);
                getSystem().getLogWriter().info("testTXSimpleOps: Created Key");
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    Host host = Host.getHost(0);
    VM vm = host.getVM(0);
    vm.invoke(create);
    vm.invoke(newKey);
    int vmCount = host.getVMCount();
    for (int i = 1; i < vmCount; i++) {
        vm = host.getVM(i);
        vm.invoke(create);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm.invoke(newKey);
        }
    }
    try {
        Region rgn = createRegion(rgnName);
        DMStats dmStats = getSystem().getDistributionManager().getStats();
        long cmtMsgs = dmStats.getSentCommitMessages();
        long commitWaits = dmStats.getCommitWaits();
        txMgr.begin();
        rgn.put("key", "value");
        TransactionId txId = txMgr.getTransactionId();
        txMgr.commit();
        assertEquals(cmtMsgs + 1, dmStats.getSentCommitMessages());
        if (rgn.getAttributes().getScope().isAck()) {
            assertEquals(commitWaits + 1, dmStats.getCommitWaits());
        } else {
            assertEquals(commitWaits, dmStats.getCommitWaits());
        }
        getSystem().getLogWriter().info("testTXSimpleOps: Create/Put Value");
        Invoke.invokeInEveryVM(MultiVMRegionTestCase.class, "assertCacheCallbackEvents", new Object[] { rgnName, txId, "key", null, "value" });
        Invoke.invokeInEveryVMRepeatingIfNecessary(new CacheSerializableRunnable("testTXSimpleOps: Verify Received Value") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                assertNotNull("Could not find entry for 'key'", rgn1.getEntry("key"));
                assertEquals("value", rgn1.getEntry("key").getValue());
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                tl.checkAfterCommitCount(1);
                assertEquals(0, tl.afterFailedCommitCount);
                assertEquals(0, tl.afterRollbackCount);
                assertEquals(0, tl.closeCount);
                assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
                {
                    Collection events;
                    RegionAttributes attr = getRegionAttributes();
                    if (!attr.getDataPolicy().withReplication() || attr.getConcurrencyChecksEnabled()) {
                        events = tl.lastEvent.getPutEvents();
                    } else {
                        events = tl.lastEvent.getCreateEvents();
                    }
                    assertEquals(1, events.size());
                    EntryEvent ev = (EntryEvent) events.iterator().next();
                    assertEquals(tl.expectedTxId, ev.getTransactionId());
                    assertTrue(ev.getRegion() == rgn1);
                    assertEquals("key", ev.getKey());
                    assertEquals("value", ev.getNewValue());
                    assertEquals(null, ev.getOldValue());
                    assertTrue(!ev.getOperation().isLocalLoad());
                    assertTrue(!ev.getOperation().isNetLoad());
                    assertTrue(!ev.getOperation().isLoad());
                    assertTrue(!ev.getOperation().isNetSearch());
                    assertTrue(!ev.getOperation().isExpiration());
                    assertEquals(null, ev.getCallbackArgument());
                    assertEquals(true, ev.isCallbackArgumentAvailable());
                    assertTrue(ev.isOriginRemote());
                    assertTrue(ev.getOperation().isDistributed());
                }
                CountingDistCacheListener cdcL = (CountingDistCacheListener) rgn1.getAttributes().getCacheListeners()[0];
                cdcL.assertCount(0, 1, 0, 0);
            }
        }, getRepeatTimeoutMs());
        txMgr.begin();
        rgn.put("key", "value2");
        txId = txMgr.getTransactionId();
        txMgr.commit();
        getSystem().getLogWriter().info("testTXSimpleOps: Put(update) Value2");
        Invoke.invokeInEveryVM(MultiVMRegionTestCase.class, "assertCacheCallbackEvents", new Object[] { rgnName, txId, "key", "value", "value2" });
        Invoke.invokeInEveryVMRepeatingIfNecessary(new CacheSerializableRunnable("testTXSimpleOps: Verify Received Value") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                assertNotNull("Could not find entry for 'key'", rgn1.getEntry("key"));
                assertEquals("value2", rgn1.getEntry("key").getValue());
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                tl.checkAfterCommitCount(2);
                assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
                {
                    Collection events = tl.lastEvent.getPutEvents();
                    assertEquals(1, events.size());
                    EntryEvent ev = (EntryEvent) events.iterator().next();
                    assertEquals(tl.expectedTxId, ev.getTransactionId());
                    assertTrue(ev.getRegion() == rgn1);
                    assertEquals("key", ev.getKey());
                    assertEquals("value2", ev.getNewValue());
                    assertEquals("value", ev.getOldValue());
                    assertTrue(!ev.getOperation().isLocalLoad());
                    assertTrue(!ev.getOperation().isNetLoad());
                    assertTrue(!ev.getOperation().isLoad());
                    assertTrue(!ev.getOperation().isNetSearch());
                    assertTrue(!ev.getOperation().isExpiration());
                    assertEquals(null, ev.getCallbackArgument());
                    assertEquals(true, ev.isCallbackArgumentAvailable());
                    assertTrue(ev.isOriginRemote());
                    assertTrue(ev.getOperation().isDistributed());
                }
                CountingDistCacheListener cdcL = (CountingDistCacheListener) rgn1.getAttributes().getCacheListeners()[0];
                cdcL.assertCount(0, 2, 0, 0);
            }
        }, getRepeatTimeoutMs());
        txMgr.begin();
        rgn.invalidate("key");
        txId = txMgr.getTransactionId();
        txMgr.commit();
        getSystem().getLogWriter().info("testTXSimpleOps: invalidate key");
        // validate each of the CacheListeners EntryEvents
        Invoke.invokeInEveryVM(MultiVMRegionTestCase.class, "assertCacheCallbackEvents", new Object[] { rgnName, txId, "key", "value2", null });
        Invoke.invokeInEveryVMRepeatingIfNecessary(new CacheSerializableRunnable("testTXSimpleOps: Verify Received Value") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                assertNotNull("Could not find entry for 'key'", rgn1.getEntry("key"));
                assertTrue(rgn1.containsKey("key"));
                assertTrue(!rgn1.containsValueForKey("key"));
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                tl.checkAfterCommitCount(3);
                assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
                {
                    Collection events = tl.lastEvent.getInvalidateEvents();
                    assertEquals(1, events.size());
                    EntryEvent ev = (EntryEvent) events.iterator().next();
                    assertEquals(tl.expectedTxId, ev.getTransactionId());
                    assertTrue(ev.getRegion() == rgn1);
                    assertEquals("key", ev.getKey());
                    assertEquals(null, ev.getNewValue());
                    assertEquals("value2", ev.getOldValue());
                    assertTrue(!ev.getOperation().isLocalLoad());
                    assertTrue(!ev.getOperation().isNetLoad());
                    assertTrue(!ev.getOperation().isLoad());
                    assertTrue(!ev.getOperation().isNetSearch());
                    assertTrue(!ev.getOperation().isExpiration());
                    assertEquals(null, ev.getCallbackArgument());
                    assertEquals(true, ev.isCallbackArgumentAvailable());
                    assertTrue(ev.isOriginRemote());
                    assertTrue(ev.getOperation().isDistributed());
                }
                CountingDistCacheListener cdcL = (CountingDistCacheListener) rgn1.getAttributes().getCacheListeners()[0];
                cdcL.assertCount(0, 2, 1, 0);
            }
        }, getRepeatTimeoutMs());
        txMgr.begin();
        rgn.destroy("key");
        txId = txMgr.getTransactionId();
        txMgr.commit();
        getSystem().getLogWriter().info("testTXSimpleOps: destroy key");
        // validate each of the CacheListeners EntryEvents
        Invoke.invokeInEveryVM(MultiVMRegionTestCase.class, "assertCacheCallbackEvents", new Object[] { rgnName, txId, "key", null, null });
        Invoke.invokeInEveryVMRepeatingIfNecessary(new CacheSerializableRunnable("testTXSimpleOps: Verify Received Value") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                assertTrue(!rgn1.containsKey("key"));
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                tl.checkAfterCommitCount(4);
                assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
                {
                    Collection events = tl.lastEvent.getDestroyEvents();
                    assertEquals(1, events.size());
                    EntryEvent ev = (EntryEvent) events.iterator().next();
                    assertEquals(tl.expectedTxId, ev.getTransactionId());
                    assertTrue(ev.getRegion() == rgn1);
                    assertEquals("key", ev.getKey());
                    assertEquals(null, ev.getNewValue());
                    assertEquals(null, ev.getOldValue());
                    assertTrue(!ev.getOperation().isLocalLoad());
                    assertTrue(!ev.getOperation().isNetLoad());
                    assertTrue(!ev.getOperation().isLoad());
                    assertTrue(!ev.getOperation().isNetSearch());
                    assertTrue(!ev.getOperation().isExpiration());
                    assertEquals(null, ev.getCallbackArgument());
                    assertEquals(true, ev.isCallbackArgumentAvailable());
                    assertTrue(ev.isOriginRemote());
                    assertTrue(ev.getOperation().isDistributed());
                }
                CountingDistCacheListener cdcL = (CountingDistCacheListener) rgn1.getAttributes().getCacheListeners()[0];
                cdcL.assertCount(0, 2, 1, 1);
            }
        }, getRepeatTimeoutMs());
    } catch (Exception e) {
        CacheFactory.getInstance(getSystem()).close();
        getSystem().getLogWriter().fine("testTXSimpleOps: Caused exception in createRegion");
        throw e;
    }
}
Also used : DMStats(org.apache.geode.distributed.internal.DMStats) CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) TimeoutException(org.apache.geode.cache.TimeoutException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) EntryExistsException(org.apache.geode.cache.EntryExistsException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionId(org.apache.geode.cache.TransactionId) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 40 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class MultiVMRegionTestCase method testTXMultiRegion.

@Test
public void testTXMultiRegion() throws Exception {
    assumeTrue(supportsTransactions());
    assumeFalse(getRegionAttributes().getScope().isGlobal());
    assumeFalse(getRegionAttributes().getDataPolicy().withPersistence());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
    final String rgnName1 = getUniqueName() + "MR1";
    final String rgnName2 = getUniqueName() + "MR2";
    final String rgnName3 = getUniqueName() + "MR3";
    SerializableRunnable create1 = new SerializableRunnable("testTXMultiRegion: Create Region") {

        @Override
        public void run() {
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = new MyTransactionListener();
            txMgr2.setListener(tl);
            try {
                createRegion(rgnName1);
                getSystem().getLogWriter().info("testTXMultiRegion: Created region1");
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    SerializableRunnable newKey1 = new SerializableRunnable("testTXMultiRegion: Create Key") {

        @Override
        public void run() {
            try {
                Region rgn = getRootRegion("root").getSubregion(rgnName1);
                rgn.create("key", null);
                getSystem().getLogWriter().info("testTXMultiRegion: Created key");
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    SerializableRunnable create2 = new SerializableRunnable("testTXMultiRegion: Create Region") {

        @Override
        public void run() {
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = new MyTransactionListener();
            txMgr2.setListener(tl);
            try {
                createRegion(rgnName2);
                getSystem().getLogWriter().info("testTXMultiRegion: Created region2");
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    SerializableRunnable newKey2 = new SerializableRunnable("testTXMultiRegion: Create Key") {

        @Override
        public void run() {
            try {
                Region root = getRootRegion("root");
                Region rgn = root.getSubregion(rgnName2);
                rgn.create("key", null);
                getSystem().getLogWriter().info("testTXMultiRegion: Created Key");
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    SerializableRunnable create3 = new SerializableRunnable("testTXMultiRegion: Create Region") {

        @Override
        public void run() {
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = new MyTransactionListener();
            txMgr2.setListener(tl);
            try {
                createRegion(rgnName3);
                getSystem().getLogWriter().info("testTXMultiRegion: Created Region");
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    SerializableRunnable newKey3 = new SerializableRunnable("testTXMultiRegion: Create Key") {

        @Override
        public void run() {
            try {
                Region root = getRootRegion("root");
                Region rgn = root.getSubregion(rgnName3);
                rgn.create("key", null);
                getSystem().getLogWriter().info("testTXMultiRegion: Created Key");
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    SerializableRunnable check1_3 = new SerializableRunnable("testTXMultiRegion: check") {

        @Override
        public void run() {
            Region rgn1 = getRootRegion().getSubregion(rgnName1);
            {
                assertNotNull("Could not find entry for 'key'", rgn1.getEntry("key"));
                assertEquals("value1", rgn1.getEntry("key").getValue());
            }
            Region rgn3 = getRootRegion().getSubregion(rgnName3);
            {
                assertNotNull("Could not find entry for 'key'", rgn3.getEntry("key"));
                assertEquals("value3", rgn3.getEntry("key").getValue());
            }
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
            tl.checkAfterCommitCount(1);
            assertEquals(0, tl.afterFailedCommitCount);
            assertEquals(0, tl.afterRollbackCount);
            assertEquals(0, tl.closeCount);
            assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
            {
                Collection events;
                RegionAttributes attr = getRegionAttributes();
                if (!attr.getDataPolicy().withReplication() || attr.getConcurrencyChecksEnabled()) {
                    events = tl.lastEvent.getPutEvents();
                } else {
                    events = tl.lastEvent.getCreateEvents();
                }
                assertEquals(2, events.size());
                ArrayList eventList = new ArrayList(events);
                Collections.sort(eventList, new Comparator() {

                    @Override
                    public int compare(Object o1, Object o2) {
                        EntryEvent e1 = (EntryEvent) o1;
                        EntryEvent e2 = (EntryEvent) o2;
                        String s1 = e1.getRegion().getFullPath() + e1.getKey();
                        String s2 = e2.getRegion().getFullPath() + e2.getKey();
                        return s1.compareTo(s2);
                    }
                });
                Iterator it = eventList.iterator();
                EntryEvent ev;
                ev = (EntryEvent) it.next();
                assertSame(rgn1, ev.getRegion());
                // assertIndexDetailsEquals(tl.expectedTxId, ev.getTransactionId());
                assertEquals("key", ev.getKey());
                assertEquals("value1", ev.getNewValue());
                assertEquals(null, ev.getOldValue());
                assertTrue(!ev.getOperation().isLocalLoad());
                assertTrue(!ev.getOperation().isNetLoad());
                assertTrue(!ev.getOperation().isLoad());
                assertTrue(!ev.getOperation().isNetSearch());
                assertTrue(!ev.getOperation().isExpiration());
                assertEquals(null, ev.getCallbackArgument());
                assertEquals(true, ev.isCallbackArgumentAvailable());
                assertTrue(ev.isOriginRemote());
                assertTrue(ev.getOperation().isDistributed());
                ev = (EntryEvent) it.next();
                assertSame(rgn3, ev.getRegion());
                // assertIndexDetailsEquals(tl.expectedTxId, ev.getTransactionId());
                assertEquals("key", ev.getKey());
                assertEquals("value3", ev.getNewValue());
                assertEquals(null, ev.getOldValue());
                assertTrue(!ev.getOperation().isLocalLoad());
                assertTrue(!ev.getOperation().isNetLoad());
                assertTrue(!ev.getOperation().isLoad());
                assertTrue(!ev.getOperation().isNetSearch());
                assertTrue(!ev.getOperation().isExpiration());
                assertEquals(null, ev.getCallbackArgument());
                assertEquals(true, ev.isCallbackArgumentAvailable());
                assertTrue(ev.isOriginRemote());
                assertTrue(ev.getOperation().isDistributed());
            }
        }
    };
    SerializableRunnable check2_3 = new SerializableRunnable("testTXMultiRegion: check") {

        @Override
        public void run() {
            Region rgn2 = getRootRegion().getSubregion(rgnName2);
            {
                assertNotNull("Could not find entry for 'key'", rgn2.getEntry("key"));
                assertEquals("value2", rgn2.getEntry("key").getValue());
            }
            Region rgn3 = getRootRegion().getSubregion(rgnName3);
            {
                assertNotNull("Could not find entry for 'key'", rgn3.getEntry("key"));
                assertEquals("value3", rgn3.getEntry("key").getValue());
            }
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
            tl.checkAfterCommitCount(1);
            assertEquals(0, tl.afterFailedCommitCount);
            assertEquals(0, tl.afterRollbackCount);
            assertEquals(0, tl.closeCount);
            assertEquals(rgn2.getCache(), tl.lastEvent.getCache());
            {
                Collection events;
                RegionAttributes attr = getRegionAttributes();
                if (!attr.getDataPolicy().withReplication() || attr.getConcurrencyChecksEnabled()) {
                    events = tl.lastEvent.getPutEvents();
                } else {
                    events = tl.lastEvent.getCreateEvents();
                }
                assertEquals(2, events.size());
                ArrayList eventList = new ArrayList(events);
                Collections.sort(eventList, new Comparator() {

                    @Override
                    public int compare(Object o1, Object o2) {
                        EntryEvent e1 = (EntryEvent) o1;
                        EntryEvent e2 = (EntryEvent) o2;
                        String s1 = e1.getRegion().getFullPath() + e1.getKey();
                        String s2 = e2.getRegion().getFullPath() + e2.getKey();
                        return s1.compareTo(s2);
                    }
                });
                Iterator it = eventList.iterator();
                EntryEvent ev;
                ev = (EntryEvent) it.next();
                assertSame(rgn2, ev.getRegion());
                // assertIndexDetailsEquals(tl.expectedTxId, ev.getTransactionId());
                assertEquals("key", ev.getKey());
                assertEquals("value2", ev.getNewValue());
                assertEquals(null, ev.getOldValue());
                assertTrue(!ev.getOperation().isLocalLoad());
                assertTrue(!ev.getOperation().isNetLoad());
                assertTrue(!ev.getOperation().isLoad());
                assertTrue(!ev.getOperation().isNetSearch());
                assertTrue(!ev.getOperation().isExpiration());
                assertEquals(null, ev.getCallbackArgument());
                assertEquals(true, ev.isCallbackArgumentAvailable());
                assertTrue(ev.isOriginRemote());
                assertTrue(ev.getOperation().isDistributed());
                ev = (EntryEvent) it.next();
                assertSame(rgn3, ev.getRegion());
                // assertIndexDetailsEquals(tl.expectedTxId, ev.getTransactionId());
                assertEquals("key", ev.getKey());
                assertEquals("value3", ev.getNewValue());
                assertEquals(null, ev.getOldValue());
                assertTrue(!ev.getOperation().isLocalLoad());
                assertTrue(!ev.getOperation().isNetLoad());
                assertTrue(!ev.getOperation().isLoad());
                assertTrue(!ev.getOperation().isNetSearch());
                assertTrue(!ev.getOperation().isExpiration());
                assertEquals(null, ev.getCallbackArgument());
                assertEquals(true, ev.isCallbackArgumentAvailable());
                assertTrue(ev.isOriginRemote());
                assertTrue(ev.getOperation().isDistributed());
            }
        }
    };
    SerializableRunnable check1 = new SerializableRunnable("testTXMultiRegion: check") {

        @Override
        public void run() {
            Region rgn = getRootRegion().getSubregion(rgnName1);
            assertNotNull("Could not find entry for 'key'", rgn.getEntry("key"));
            assertEquals("value1", rgn.getEntry("key").getValue());
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
            tl.checkAfterCommitCount(1);
            assertEquals(0, tl.afterFailedCommitCount);
            assertEquals(0, tl.afterRollbackCount);
            assertEquals(0, tl.closeCount);
            assertEquals(rgn.getCache(), tl.lastEvent.getCache());
            {
                Collection events;
                RegionAttributes attr = getRegionAttributes();
                if (!attr.getDataPolicy().withReplication() || attr.getConcurrencyChecksEnabled()) {
                    events = tl.lastEvent.getPutEvents();
                } else {
                    events = tl.lastEvent.getCreateEvents();
                }
                assertEquals(1, events.size());
                EntryEvent ev = (EntryEvent) events.iterator().next();
                // assertIndexDetailsEquals(tl.expectedTxId, ev.getTransactionId());
                assertTrue(ev.getRegion() == rgn);
                assertEquals("key", ev.getKey());
                assertEquals("value1", ev.getNewValue());
                assertEquals(null, ev.getOldValue());
                assertTrue(!ev.getOperation().isLocalLoad());
                assertTrue(!ev.getOperation().isNetLoad());
                assertTrue(!ev.getOperation().isLoad());
                assertTrue(!ev.getOperation().isNetSearch());
                assertTrue(!ev.getOperation().isExpiration());
                assertEquals(null, ev.getCallbackArgument());
                assertEquals(true, ev.isCallbackArgumentAvailable());
                assertTrue(ev.isOriginRemote());
                assertTrue(ev.getOperation().isDistributed());
            }
        }
    };
    SerializableRunnable check2 = new SerializableRunnable("testTXMultiRegion: check") {

        @Override
        public void run() {
            Region rgn = getRootRegion().getSubregion(rgnName2);
            assertNotNull("Could not find entry for 'key'", rgn.getEntry("key"));
            assertEquals("value2", rgn.getEntry("key").getValue());
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
            tl.checkAfterCommitCount(1);
            assertEquals(0, tl.afterFailedCommitCount);
            assertEquals(0, tl.afterRollbackCount);
            assertEquals(0, tl.closeCount);
            assertEquals(rgn.getCache(), tl.lastEvent.getCache());
            {
                Collection events;
                RegionAttributes attr = getRegionAttributes();
                if (!attr.getDataPolicy().withReplication() || attr.getConcurrencyChecksEnabled()) {
                    events = tl.lastEvent.getPutEvents();
                } else {
                    events = tl.lastEvent.getCreateEvents();
                }
                assertEquals(1, events.size());
                EntryEvent ev = (EntryEvent) events.iterator().next();
                // assertIndexDetailsEquals(tl.expectedTxId, ev.getTransactionId());
                assertTrue(ev.getRegion() == rgn);
                assertEquals("key", ev.getKey());
                assertEquals("value2", ev.getNewValue());
                assertEquals(null, ev.getOldValue());
                assertTrue(!ev.getOperation().isLocalLoad());
                assertTrue(!ev.getOperation().isNetLoad());
                assertTrue(!ev.getOperation().isLoad());
                assertTrue(!ev.getOperation().isNetSearch());
                assertTrue(!ev.getOperation().isExpiration());
                assertEquals(null, ev.getCallbackArgument());
                assertEquals(true, ev.isCallbackArgumentAvailable());
                assertTrue(ev.isOriginRemote());
                assertTrue(ev.getOperation().isDistributed());
            }
        }
    };
    SerializableRunnable check3 = new SerializableRunnable("testTXMultiRegion: check") {

        @Override
        public void run() {
            Region rgn = getRootRegion().getSubregion(rgnName3);
            assertNotNull("Could not find entry for 'key'", rgn.getEntry("key"));
            assertEquals("value3", rgn.getEntry("key").getValue());
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
            tl.checkAfterCommitCount(1);
            assertEquals(0, tl.afterFailedCommitCount);
            assertEquals(0, tl.afterRollbackCount);
            assertEquals(0, tl.closeCount);
            assertEquals(rgn.getCache(), tl.lastEvent.getCache());
            {
                Collection events;
                RegionAttributes attr = getRegionAttributes();
                if (!attr.getDataPolicy().withReplication() || attr.getConcurrencyChecksEnabled()) {
                    events = tl.lastEvent.getPutEvents();
                } else {
                    events = tl.lastEvent.getCreateEvents();
                }
                assertEquals(1, events.size());
                EntryEvent ev = (EntryEvent) events.iterator().next();
                // assertIndexDetailsEquals(tl.expectedTxId, ev.getTransactionId());
                assertTrue(ev.getRegion() == rgn);
                assertEquals("key", ev.getKey());
                assertEquals("value3", ev.getNewValue());
                assertEquals(null, ev.getOldValue());
                assertTrue(!ev.getOperation().isLocalLoad());
                assertTrue(!ev.getOperation().isNetLoad());
                assertTrue(!ev.getOperation().isLoad());
                assertTrue(!ev.getOperation().isNetSearch());
                assertTrue(!ev.getOperation().isExpiration());
                assertEquals(null, ev.getCallbackArgument());
                assertEquals(true, ev.isCallbackArgumentAvailable());
                assertTrue(ev.isOriginRemote());
                assertTrue(ev.getOperation().isDistributed());
            }
        }
    };
    // GemFireVersion.waitForJavaDebugger(getLogWriter(), "CTRLR WAITING AFTER CREATE");
    VM vm0 = Host.getHost(0).getVM(0);
    VM vm1 = Host.getHost(0).getVM(1);
    VM vm2 = Host.getHost(0).getVM(2);
    VM vm3 = Host.getHost(0).getVM(3);
    try {
        DMStats dmStats = getSystem().getDistributionManager().getStats();
        Region rgn1;
        Region rgn2;
        Region rgn3;
        long cmtMsgs;
        // long sentMsgs;
        long commitWaits;
        // vm0->R1,R3 vm1->R1,R3 vm2->R2 vm3->R2,R3
        vm0.invoke(create1);
        vm0.invoke(newKey1);
        vm0.invoke(create3);
        vm0.invoke(newKey3);
        vm1.invoke(create1);
        vm1.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm1.invoke(newKey1);
            vm1.invoke(newKey3);
        }
        vm2.invoke(create2);
        vm2.invoke(newKey2);
        vm3.invoke(create2);
        vm3.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm3.invoke(newKey2);
            vm3.invoke(newKey3);
        }
        rgn1 = createRegion(rgnName1);
        rgn2 = createRegion(rgnName2);
        rgn3 = createRegion(rgnName3);
        cmtMsgs = dmStats.getSentCommitMessages();
        commitWaits = dmStats.getCommitWaits();
        txMgr.begin();
        rgn1.put("key", "value1");
        rgn2.put("key", "value2");
        rgn3.put("key", "value3");
        // TransactionId txId = txMgr.getTransactionId();
        getSystem().getLogWriter().info("testTXMultiRegion: vm0->R1,R3 vm1->R1,R3 vm2->R2 vm3->R2,R3");
        txMgr.commit();
        assertEquals(cmtMsgs + 3, dmStats.getSentCommitMessages());
        if (rgn1.getAttributes().getScope().isAck() || rgn2.getAttributes().getScope().isAck() || rgn3.getAttributes().getScope().isAck()) {
            assertEquals(commitWaits + 1, dmStats.getCommitWaits());
        } else {
            assertEquals(commitWaits, dmStats.getCommitWaits());
            // [bruce] changed from 200 to 2000 for mcast testing
            try {
                Thread.sleep(2000);
            } catch (InterruptedException chomp) {
                fail("interrupted");
            }
        }
        vm0.invoke(check1_3);
        vm1.invoke(check1_3);
        vm2.invoke(check2);
        vm3.invoke(check2_3);
        rgn1.destroyRegion();
        rgn2.destroyRegion();
        rgn3.destroyRegion();
        // vm0->R1,R3 vm1->R1,R3 vm2->R2,R3 vm3->R2,R3
        vm0.invoke(create1);
        vm0.invoke(newKey1);
        vm0.invoke(create3);
        vm0.invoke(newKey3);
        vm1.invoke(create1);
        vm1.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm1.invoke(newKey1);
            vm1.invoke(newKey3);
        }
        vm2.invoke(create2);
        vm2.invoke(newKey2);
        vm2.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm2.invoke(newKey3);
        }
        vm3.invoke(create2);
        vm3.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm3.invoke(newKey2);
            vm3.invoke(newKey3);
        }
        rgn1 = createRegion(rgnName1);
        rgn2 = createRegion(rgnName2);
        rgn3 = createRegion(rgnName3);
        cmtMsgs = dmStats.getSentCommitMessages();
        commitWaits = dmStats.getCommitWaits();
        txMgr.begin();
        rgn1.put("key", "value1");
        rgn2.put("key", "value2");
        rgn3.put("key", "value3");
        // txId = txMgr.getTransactionId();
        getSystem().getLogWriter().info("testTXMultiRegion: vm0->R1,R3 vm1->R1,R3 vm2->R2,R3 vm3->R2,R3");
        txMgr.commit();
        assertEquals(cmtMsgs + 2, dmStats.getSentCommitMessages());
        if (rgn1.getAttributes().getScope().isAck() || rgn2.getAttributes().getScope().isAck() || rgn3.getAttributes().getScope().isAck()) {
            assertEquals(commitWaits + 1, dmStats.getCommitWaits());
        } else {
            assertEquals(commitWaits, dmStats.getCommitWaits());
            // pause to give cmt message a chance to be processed
            try {
                Thread.sleep(200);
            } catch (InterruptedException chomp) {
                fail("interrupted");
            }
        }
        vm0.invoke(check1_3);
        vm1.invoke(check1_3);
        vm2.invoke(check2_3);
        vm3.invoke(check2_3);
        rgn1.destroyRegion();
        rgn2.destroyRegion();
        rgn3.destroyRegion();
        // vm0->R1,R3 vm1->R1,R3 vm2->R2 vm3->R1,R3
        vm0.invoke(create1);
        vm0.invoke(newKey1);
        vm0.invoke(create3);
        vm0.invoke(newKey3);
        vm1.invoke(create1);
        vm1.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm1.invoke(newKey1);
            vm1.invoke(newKey3);
        }
        vm2.invoke(create2);
        vm2.invoke(newKey2);
        vm3.invoke(create1);
        vm3.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm3.invoke(newKey1);
            vm3.invoke(newKey3);
        }
        rgn1 = createRegion(rgnName1);
        rgn2 = createRegion(rgnName2);
        rgn3 = createRegion(rgnName3);
        cmtMsgs = dmStats.getSentCommitMessages();
        commitWaits = dmStats.getCommitWaits();
        txMgr.begin();
        rgn1.put("key", "value1");
        rgn2.put("key", "value2");
        rgn3.put("key", "value3");
        // txId = txMgr.getTransactionId();
        getSystem().getLogWriter().info("testTXMultiRegion: vm0->R1,R3 vm1->R1,R3 vm2->R2 vm3->R1,R3");
        txMgr.commit();
        assertEquals(cmtMsgs + 2, dmStats.getSentCommitMessages());
        if (rgn1.getAttributes().getScope().isAck() || rgn2.getAttributes().getScope().isAck() || rgn3.getAttributes().getScope().isAck()) {
            assertEquals(commitWaits + 1, dmStats.getCommitWaits());
        } else {
            assertEquals(commitWaits, dmStats.getCommitWaits());
            // pause to give cmt message a chance to be processed
            try {
                Thread.sleep(200);
            } catch (InterruptedException chomp) {
                fail("interrupted");
            }
        }
        vm0.invoke(check1_3);
        vm1.invoke(check1_3);
        vm2.invoke(check2);
        vm3.invoke(check1_3);
        rgn1.destroyRegion();
        rgn2.destroyRegion();
        rgn3.destroyRegion();
        // vm0->R1 vm1->R1 vm2->R2 vm3->R3
        vm0.invoke(create1);
        vm0.invoke(newKey1);
        vm1.invoke(create1);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm1.invoke(newKey1);
        }
        vm2.invoke(create2);
        vm2.invoke(newKey2);
        vm3.invoke(create3);
        vm3.invoke(newKey3);
        rgn1 = createRegion(rgnName1);
        rgn2 = createRegion(rgnName2);
        rgn3 = createRegion(rgnName3);
        cmtMsgs = dmStats.getSentCommitMessages();
        commitWaits = dmStats.getCommitWaits();
        txMgr.begin();
        rgn1.put("key", "value1");
        rgn2.put("key", "value2");
        rgn3.put("key", "value3");
        // txId = txMgr.getTransactionId();
        getSystem().getLogWriter().info("testTXMultiRegion: vm0->R1 vm1->R1 vm2->R2 vm3->R3");
        txMgr.commit();
        assertEquals(cmtMsgs + 3, dmStats.getSentCommitMessages());
        if (rgn1.getAttributes().getScope().isAck() || rgn2.getAttributes().getScope().isAck() || rgn3.getAttributes().getScope().isAck()) {
            assertEquals(commitWaits + 1, dmStats.getCommitWaits());
        } else {
            assertEquals(commitWaits, dmStats.getCommitWaits());
            // pause to give cmt message a chance to be processed
            try {
                Thread.sleep(200);
            } catch (InterruptedException chomp) {
                fail("interrupted");
            }
        }
        vm0.invoke(check1);
        vm1.invoke(check1);
        vm2.invoke(check2);
        vm3.invoke(check3);
        rgn1.destroyRegion();
        rgn2.destroyRegion();
        rgn3.destroyRegion();
        // vm0->R1,R3 vm1->R2,R3 vm2->R2 vm3->R3
        vm0.invoke(create1);
        vm0.invoke(newKey1);
        vm0.invoke(create3);
        vm0.invoke(newKey3);
        vm1.invoke(create2);
        vm1.invoke(newKey2);
        vm1.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm1.invoke(newKey3);
        }
        vm2.invoke(create2);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm2.invoke(newKey2);
        }
        vm3.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm3.invoke(newKey3);
        }
        rgn1 = createRegion(rgnName1);
        rgn2 = createRegion(rgnName2);
        rgn3 = createRegion(rgnName3);
        cmtMsgs = dmStats.getSentCommitMessages();
        commitWaits = dmStats.getCommitWaits();
        txMgr.begin();
        rgn1.put("key", "value1");
        rgn2.put("key", "value2");
        rgn3.put("key", "value3");
        // txId = txMgr.getTransactionId();
        getSystem().getLogWriter().info("testTXMultiRegion: vm0->R1,R3 vm1->R2,R3 vm2->R2 vm3->R3");
        txMgr.commit();
        assertEquals(cmtMsgs + 4, dmStats.getSentCommitMessages());
        if (rgn1.getAttributes().getScope().isAck() || rgn2.getAttributes().getScope().isAck() || rgn3.getAttributes().getScope().isAck()) {
            assertEquals(commitWaits + 1, dmStats.getCommitWaits());
        } else {
            assertEquals(commitWaits, dmStats.getCommitWaits());
            // pause to give cmt message a chance to be processed
            try {
                Thread.sleep(200);
            } catch (InterruptedException chomp) {
                fail("interrupted");
            }
        }
        vm0.invoke(check1_3);
        vm1.invoke(check2_3);
        vm2.invoke(check2);
        vm3.invoke(check3);
        rgn1.destroyRegion();
        rgn2.destroyRegion();
        rgn3.destroyRegion();
        // vm0->R1,R3 vm1->R1,R3 vm2->R1,R3 vm3->R1,R3
        vm0.invoke(create1);
        vm0.invoke(newKey1);
        vm0.invoke(create3);
        vm0.invoke(newKey3);
        vm1.invoke(create1);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm1.invoke(newKey1);
        }
        vm1.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm1.invoke(newKey3);
        }
        vm2.invoke(create1);
        vm2.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm2.invoke(newKey1);
            vm2.invoke(newKey3);
        }
        vm3.invoke(create1);
        vm3.invoke(create3);
        if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
            vm3.invoke(newKey1);
            vm3.invoke(newKey3);
        }
        rgn1 = createRegion(rgnName1);
        rgn2 = createRegion(rgnName2);
        rgn3 = createRegion(rgnName3);
        cmtMsgs = dmStats.getSentCommitMessages();
        commitWaits = dmStats.getCommitWaits();
        txMgr.begin();
        rgn1.put("key", "value1");
        rgn2.put("key", "value2");
        rgn3.put("key", "value3");
        // txId = txMgr.getTransactionId();
        getSystem().getLogWriter().info("testTXMultiRegion: vm0->R1,R3 vm1->R1,R3 vm2->R1,R3 vm3->R1,R3");
        txMgr.commit();
        assertEquals(cmtMsgs + 1, dmStats.getSentCommitMessages());
        if (rgn1.getAttributes().getScope().isAck() || rgn2.getAttributes().getScope().isAck() || rgn3.getAttributes().getScope().isAck()) {
            assertEquals(commitWaits + 1, dmStats.getCommitWaits());
        } else {
            assertEquals(commitWaits, dmStats.getCommitWaits());
            // pause to give cmt message a chance to be processed
            try {
                Thread.sleep(200);
            } catch (InterruptedException chomp) {
                fail("interrupted");
            }
        }
        vm0.invoke(check1_3);
        vm1.invoke(check1_3);
        vm2.invoke(check1_3);
        vm3.invoke(check1_3);
        rgn1.destroyRegion();
        rgn2.destroyRegion();
        rgn3.destroyRegion();
    } catch (Exception e) {
        CacheFactory.getInstance(getSystem()).close();
        getSystem().getLogWriter().fine("testTXMultiRegion: Caused exception in createRegion");
        throw e;
    }
}
Also used : DMStats(org.apache.geode.distributed.internal.DMStats) CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ArrayList(java.util.ArrayList) TimeoutException(org.apache.geode.cache.TimeoutException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) EntryExistsException(org.apache.geode.cache.EntryExistsException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Comparator(java.util.Comparator) EntryEvent(org.apache.geode.cache.EntryEvent) VM(org.apache.geode.test.dunit.VM) Iterator(java.util.Iterator) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) StoredObject(org.apache.geode.internal.offheap.StoredObject) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Aggregations

CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)120 Region (org.apache.geode.cache.Region)81 Test (org.junit.Test)77 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)53 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)52 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)51 VM (org.apache.geode.test.dunit.VM)51 LocalRegion (org.apache.geode.internal.cache.LocalRegion)46 CommitConflictException (org.apache.geode.cache.CommitConflictException)45 Host (org.apache.geode.test.dunit.Host)45 CustId (org.apache.geode.internal.cache.execute.data.CustId)37 Customer (org.apache.geode.internal.cache.execute.data.Customer)34 BucketRegion (org.apache.geode.internal.cache.BucketRegion)27 AttributesFactory (org.apache.geode.cache.AttributesFactory)26 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)24 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)23 IgnoredException (org.apache.geode.test.dunit.IgnoredException)23 UnsupportedOperationInTransactionException (org.apache.geode.cache.UnsupportedOperationInTransactionException)22 CacheWriterException (org.apache.geode.cache.CacheWriterException)21 TransactionWriterException (org.apache.geode.cache.TransactionWriterException)21