Search in sources :

Example 21 with TransactionId

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

the class RemoteTransactionDUnitTest method testExpirySuspend_bug45984.

@Test
public void testExpirySuspend_bug45984() {
    Host host = Host.getHost(0);
    VM vm1 = host.getVM(0);
    VM vm2 = host.getVM(1);
    final String regionName = getName();
    // create region with expiration
    vm1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            try {
                RegionFactory<String, String> rf = getCache().createRegionFactory();
                rf.setEntryTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
                rf.setScope(Scope.DISTRIBUTED_ACK);
                rf.create(regionName);
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
            }
            return null;
        }
    });
    // create replicate region
    vm2.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            final Region<String, String> r = getCache().getRegion(regionName);
            WaitCriterion wc2 = new WaitCriterion() {

                @Override
                public boolean done() {
                    return !r.containsKey("key") && !r.containsKey("nonTXKey");
                }

                @Override
                public String description() {
                    return "did not expire containsKey(key)=" + r.containsKey("key") + " r.containsKey(nonTXKey)=" + r.containsKey("nonTXKey");
                }
            };
            ExpiryTask.suspendExpiration();
            Region.Entry entry = null;
            long tilt;
            try {
                r.put("key", "value");
                LocalRegion lr = (LocalRegion) r;
                r.put("nonTXkey", "nonTXvalue");
                getCache().getCacheTransactionManager().begin();
                r.put("key", "newvalue");
                TXExpiryJUnitTest.waitForEntryExpiration(lr, "key");
            } finally {
                ExpiryTask.permitExpiration();
            }
            TransactionId tx = getCache().getCacheTransactionManager().suspend();
            // A remote tx will allow expiration to happen on the side that
            // is not hosting the tx. But it will not allow an expiration
            // initiated on the hosting jvm.
            // tx is hosted in vm2 so expiration can happen in vm1.
            Wait.waitForCriterion(wc2, 30000, 5, true);
            getCache().getCacheTransactionManager().resume(tx);
            assertTrue(r.containsKey("key"));
            getCache().getCacheTransactionManager().commit();
            Wait.waitForCriterion(wc2, 30000, 5, true);
            return null;
        }
    });
}
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) TransactionId(org.apache.geode.cache.TransactionId) Entry(org.apache.geode.cache.Region.Entry) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) ClientRegionFactory(org.apache.geode.cache.client.ClientRegionFactory) RegionFactory(org.apache.geode.cache.RegionFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 22 with TransactionId

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

the class RemoteTransactionDUnitTest method doSizeTest.

private void doSizeTest(final boolean isAccessor) {
    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);
    VM taskVM = isAccessor ? accessor : datastore1;
    taskVM.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getCache().getRegion(CUSTOMER);
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            mgr.begin();
            assertEquals(5, custRegion.size());
            assertNotNull(mgr.getTXState());
            return null;
        }
    });
    datastore1.invoke(verifyNoTxState);
    datastore2.invoke(verifyNoTxState);
    taskVM.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getCache().getRegion(CUSTOMER);
            Region orderRegion = getCache().getRegion(ORDER);
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            TransactionId txId = mgr.suspend();
            PartitionedRegion custPR = (PartitionedRegion) custRegion;
            int remoteKey = -1;
            for (int i = 100; i < 200; i++) {
                DistributedMember myId = custPR.getMyId();
                if (!myId.equals(custPR.getOwnerForKey(custPR.getKeyInfo(new CustId(i))))) {
                    remoteKey = i;
                    break;
                }
            }
            if (remoteKey == -1) {
                throw new IllegalStateException("expected non-negative key");
            }
            mgr.resume(txId);
            assertNotNull(mgr.getTXState());
            CustId custId = new CustId(remoteKey);
            OrderId orderId = new OrderId(remoteKey, custId);
            custRegion.put(custId, new Customer("customer" + remoteKey, "address" + remoteKey));
            getCache().getLogger().info("Putting " + custId + ", keyInfo:" + custPR.getKeyInfo(new CustId(remoteKey)));
            orderRegion.put(orderId, new Order("order" + remoteKey));
            assertEquals(6, custRegion.size());
            return mgr.getTransactionId();
        }
    });
    final Integer txOnDatastore1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
    final Integer txOnDatastore2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
    assertEquals(1, txOnDatastore1 + txOnDatastore2);
    taskVM.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.commit();
            return null;
        }
    });
    datastore1.invoke(verifyNoTxState);
    datastore2.invoke(verifyNoTxState);
    final Integer txOnDatastore1_1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
    final Integer txOnDatastore2_2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
    assertEquals(0, txOnDatastore1_1.intValue());
    assertEquals(0, txOnDatastore2_2.intValue());
}
Also used : Order(org.apache.geode.internal.cache.execute.data.Order) Customer(org.apache.geode.internal.cache.execute.data.Customer) Host(org.apache.geode.test.dunit.Host) OrderId(org.apache.geode.internal.cache.execute.data.OrderId) 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) TransactionId(org.apache.geode.cache.TransactionId) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CustId(org.apache.geode.internal.cache.execute.data.CustId) 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)

Aggregations

TransactionId (org.apache.geode.cache.TransactionId)22 Test (org.junit.Test)12 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)11 EntryEvent (org.apache.geode.cache.EntryEvent)7 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)7 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)6 CacheWriterException (org.apache.geode.cache.CacheWriterException)6 Region (org.apache.geode.cache.Region)6 IgnoredException (org.apache.geode.test.dunit.IgnoredException)6 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)6 CommitConflictException (org.apache.geode.cache.CommitConflictException)5 VM (org.apache.geode.test.dunit.VM)5 IOException (java.io.IOException)4 Collection (java.util.Collection)4 EntryExistsException (org.apache.geode.cache.EntryExistsException)4 TransactionEvent (org.apache.geode.cache.TransactionEvent)4 Host (org.apache.geode.test.dunit.Host)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NamingException (javax.naming.NamingException)3