Search in sources :

Example 1 with TransactionId

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

the class RemoteTransactionDUnitTest method testBug45556.

@Test
public void testBug45556() {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore = host.getVM(1);
    final String name = getName();
    class CountingListener extends CacheListenerAdapter {

        private int count;

        @Override
        public void afterCreate(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("afterCreate invoked for " + event);
            count++;
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("afterUpdate invoked for " + event);
            count++;
        }
    }
    accessor.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region r = getCache().createRegionFactory(RegionShortcut.REPLICATE_PROXY).create(name);
            r.getAttributesMutator().addCacheListener(new CountingListener());
            return null;
        }
    });
    datastore.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region r = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(name);
            r.getAttributesMutator().addCacheListener(new CountingListener());
            r.put("key1", "value1");
            return null;
        }
    });
    final TransactionId txid = (TransactionId) accessor.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region r = getCache().getRegion(name);
            CacheTransactionManager tm = getCache().getCacheTransactionManager();
            getCache().getLogger().fine("SWAP:BeginTX");
            tm.begin();
            r.put("txkey", "txvalue");
            return tm.suspend();
        }
    });
    datastore.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region rgn = getCache().getRegion(name);
            assertNull(rgn.get("txkey"));
            TXManagerImpl txMgr = getGemfireCache().getTxManager();
            TXStateProxy tx = txMgr.getHostedTXState((TXId) txid);
            assertEquals(1, tx.getRegions().size());
            for (LocalRegion r : tx.getRegions()) {
                assertTrue(r instanceof DistributedRegion);
                TXRegionState rs = tx.readRegion(r);
                for (Object key : rs.getEntryKeys()) {
                    TXEntryState es = rs.readEntry(key);
                    assertEquals("txkey", key);
                    assertNotNull(es.getValue(key, r, false));
                    if (key.equals("txkey"))
                        assertTrue(es.isDirty());
                }
            }
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region rgn = getCache().getRegion(name);
            assertNull(rgn.get("txkey"));
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.resume(txid);
            mgr.commit();
            CountingListener cl = (CountingListener) rgn.getAttributes().getCacheListeners()[0];
            assertEquals(0, cl.count);
            assertEquals("txvalue", rgn.get("txkey"));
            return null;
        }
    });
    datastore.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region rgn = getCache().getRegion(name);
            CountingListener cl = (CountingListener) rgn.getAttributes().getCacheListeners()[0];
            assertEquals(2, cl.count);
            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) 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 2 with TransactionId

use of org.apache.geode.cache.TransactionId 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 3 with TransactionId

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

the class MultiVMRegionTestCase method testTXUpdateLoadNoConflict.

/**
   * Tests that the push of a loaded value does not cause a conflict on the side receiving the
   * update
   */
@Ignore("TODO: this test always hits early out")
@Test
public void testTXUpdateLoadNoConflict() throws Exception {
    /*
     * this no longer holds true - we have load conflicts now
     * 
     */
    if (true) {
        return;
    }
    assumeTrue(supportsTransactions());
    assumeFalse(getRegionAttributes().getScope().isGlobal());
    assumeFalse(getRegionAttributes().getDataPolicy().withPersistence());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
    final String rgnName = getUniqueName();
    SerializableRunnable create = new SerializableRunnable("testTXUpdateLoadNoConflict: Create Region & Load value") {

        @Override
        public void run() {
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = new MyTransactionListener();
            txMgr2.addListener(tl);
            try {
                Region rgn = createRegion(rgnName);
                AttributesMutator mutator = rgn.getAttributesMutator();
                mutator.setCacheLoader(new CacheLoader() {

                    int count = 0;

                    @Override
                    public Object load(LoaderHelper helper) throws CacheLoaderException {
                        count++;
                        return "LV " + count;
                    }

                    @Override
                    public void close() {
                    }
                });
                Object value = rgn.get("key");
                assertEquals("LV 1", value);
                getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: loaded Key");
                flushIfNecessary(rgn);
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    VM vm0 = Host.getHost(0).getVM(0);
    try {
        MyTransactionListener tl = new MyTransactionListener();
        txMgr.addListener(tl);
        AttributesFactory rgnAtts = new AttributesFactory(getRegionAttributes());
        rgnAtts.setDataPolicy(DataPolicy.REPLICATE);
        Region rgn = createRegion(rgnName, rgnAtts.create());
        txMgr.begin();
        TransactionId myTXId = txMgr.getTransactionId();
        rgn.create("key", "txValue");
        vm0.invoke(create);
        {
            TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
            assertTrue(rgn.containsKey("key"));
            assertEquals("LV 1", rgn.getEntry("key").getValue());
            ((TXManagerImpl) txMgr).internalResume(tx);
        }
        // make sure transactional view is still correct
        assertEquals("txValue", rgn.getEntry("key").getValue());
        txMgr.commit();
        getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
        assertEquals("txValue", rgn.getEntry("key").getValue());
        {
            Collection events = tl.lastEvent.getCreateEvents();
            assertEquals(1, events.size());
            EntryEvent ev = (EntryEvent) events.iterator().next();
            assertEquals(myTXId, ev.getTransactionId());
            assertTrue(ev.getRegion() == rgn);
            assertEquals("key", ev.getKey());
            assertEquals("txValue", 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());
        }
        // Now setup recreate the region in the controller with NONE
        // so test can do local destroys.
        rgn.localDestroyRegion();
        rgnAtts.setDataPolicy(DataPolicy.NORMAL);
        rgn = createRegion(rgnName, rgnAtts.create());
        // now see if net loader is working
        Object v2 = rgn.get("key2");
        assertEquals("LV 2", v2);
        // now confirm that netload does not cause a conflict
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        rgn.create("key3", "txValue3");
        {
            TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
            // do a get outside of the transaction to force a net load
            Object v3 = rgn.get("key3");
            assertEquals("LV 3", v3);
            ((TXManagerImpl) txMgr).internalResume(tx);
        }
        // make sure transactional view is still correct
        assertEquals("txValue3", rgn.getEntry("key3").getValue());
        txMgr.commit();
        getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
        assertEquals("txValue3", rgn.getEntry("key3").getValue());
        {
            Collection events = tl.lastEvent.getCreateEvents();
            assertEquals(1, events.size());
            EntryEvent ev = (EntryEvent) events.iterator().next();
            assertEquals(myTXId, ev.getTransactionId());
            assertTrue(ev.getRegion() == rgn);
            assertEquals("key3", ev.getKey());
            assertEquals("txValue3", 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());
        }
        // now see if tx net loader is working
        // now confirm that netload does not cause a conflict
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        Object v4 = rgn.get("key4");
        assertEquals("LV 4", v4);
        assertEquals("LV 4", rgn.get("key4"));
        assertEquals("LV 4", rgn.getEntry("key4").getValue());
        txMgr.rollback();
        // confirm that netLoad is transactional
        assertEquals("LV 5", rgn.get("key4"));
        assertEquals("LV 5", rgn.getEntry("key4").getValue());
        // make sure non-tx netsearch works
        assertEquals("txValue", rgn.get("key"));
        assertEquals("txValue", rgn.getEntry("key").getValue());
        // make sure net-search result does not conflict with commit
        rgn.localInvalidate("key");
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        rgn.put("key", "new txValue");
        {
            TXStateProxy tx = ((TXManagerImpl) txMgr).internalSuspend();
            // do a get outside of the transaction to force a netsearch
            // does a netsearch
            assertEquals("txValue", rgn.get("key"));
            assertEquals("txValue", rgn.getEntry("key").getValue());
            ((TXManagerImpl) txMgr).internalResume(tx);
        }
        // make sure transactional view is still correct
        assertEquals("new txValue", rgn.getEntry("key").getValue());
        txMgr.commit();
        // give other side change to process commit
        flushIfNecessary(rgn);
        getSystem().getLogWriter().info("testTXUpdateLoadNoConflict: did commit");
        assertEquals("new txValue", rgn.getEntry("key").getValue());
        {
            Collection events = tl.lastEvent.getPutEvents();
            assertEquals(1, events.size());
            EntryEvent ev = (EntryEvent) events.iterator().next();
            assertEquals(myTXId, ev.getTransactionId());
            assertTrue(ev.getRegion() == rgn);
            assertEquals("key", ev.getKey());
            assertEquals("new txValue", 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());
        }
        // make sure tx local invalidate allows netsearch
        Object localCmtValue = rgn.getEntry("key").getValue();
        txMgr.begin();
        assertSame(localCmtValue, rgn.getEntry("key").getValue());
        rgn.localInvalidate("key");
        assertNull(rgn.getEntry("key").getValue());
        // now make sure a get will do a netsearch and find the value
        // in the other vm instead of the one in local cmt state
        Object txValue = rgn.get("key");
        assertNotSame(localCmtValue, txValue);
        assertSame(txValue, rgn.get("key"));
        assertNotSame(localCmtValue, rgn.getEntry("key").getValue());
        // make sure we did a search and not a load
        assertEquals(localCmtValue, rgn.getEntry("key").getValue());
        // now make sure that if we do a tx distributed invalidate
        // that we will do a load and not a search
        rgn.invalidate("key");
        assertNull(rgn.getEntry("key").getValue());
        txValue = rgn.get("key");
        assertEquals("LV 6", txValue);
        assertSame(txValue, rgn.get("key"));
        assertEquals("LV 6", rgn.getEntry("key").getValue());
        // now make sure after rollback that local cmt state has not changed
        txMgr.rollback();
        assertSame(localCmtValue, rgn.getEntry("key").getValue());
    } catch (Exception e) {
        CacheFactory.getInstance(getSystem()).close();
        getSystem().getLogWriter().fine("testTXUpdateLoadNoConflict: Caused exception in createRegion");
        throw e;
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) 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) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) 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) CacheLoader(org.apache.geode.cache.CacheLoader) StoredObject(org.apache.geode.internal.offheap.StoredObject) AttributesMutator(org.apache.geode.cache.AttributesMutator) Ignore(org.junit.Ignore) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 4 with TransactionId

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

the class TXJUnitTest method testPublicSuspendResume.

@Test
public void testPublicSuspendResume() {
    CacheTransactionManager txMgr = this.txMgr;
    assertTrue(!this.txMgr.exists());
    assertEquals(null, txMgr.suspend());
    TransactionId txId = null;
    try {
        txMgr.resume(txId);
        fail("expected IllegalStateException");
    } catch (IllegalStateException e) {
    }
    assertTrue(!this.txMgr.exists());
    this.txMgr.begin();
    TransactionId origId = this.txMgr.getTransactionId();
    assertTrue(this.txMgr.exists());
    {
        TransactionId tx = txMgr.suspend();
        assertTrue(!this.txMgr.exists());
        this.txMgr.begin();
        try {
            txMgr.resume(tx);
            fail("expected IllegalStateException");
        } catch (IllegalStateException expected) {
        }
        this.txMgr.rollback();
        assertTrue(!this.txMgr.exists());
        txMgr.resume(tx);
    }
    assertTrue(this.txMgr.exists());
    assertEquals(origId, this.txMgr.getTransactionId());
    this.txMgr.rollback();
}
Also used : CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionId(org.apache.geode.cache.TransactionId) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 5 with TransactionId

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

the class TXManagerImpl method internalResume.

/**
   * Activates the specified transaction on the calling thread.
   * 
   * @param tx the transaction to activate.
   * @throws IllegalStateException if this thread already has an active transaction
   */
public void internalResume(TXStateProxy tx) {
    if (tx != null) {
        TransactionId tid = getTransactionId();
        if (tid != null) {
            throw new java.lang.IllegalStateException(LocalizedStrings.TXManagerImpl_TRANSACTION_0_ALREADY_IN_PROGRESS.toLocalizedString(tid));
        }
        setTXState(tx);
        tx.resume();
    }
}
Also used : TransactionId(org.apache.geode.cache.TransactionId)

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