Search in sources :

Example 11 with EntryExistsException

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

the class MultiVMRegionTestCase method testTXAlgebra.

@Ignore("TODO: test is disabled")
@Test
public void testTXAlgebra() throws Exception {
    assumeFalse(getRegionAttributes().getScope().isGlobal());
    assumeFalse(getRegionAttributes().getDataPolicy().withPersistence());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
    MyTransactionListener localTl = new MyTransactionListener();
    TransactionId myTXId;
    txMgr.addListener(localTl);
    assertEquals(null, localTl.lastEvent);
    localTl.assertCounts(0, 0, 0, 0);
    final String rgnName = getUniqueName();
    SerializableRunnable create = new SerializableRunnable("testTXAlgebra: Create Region") {

        @Override
        public void run() {
            CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
            MyTransactionListener tl = new MyTransactionListener();
            txMgr2.addListener(tl);
            assertEquals(null, tl.lastEvent);
            tl.assertCounts(0, 0, 0, 0);
            try {
                Region rgn = createRegion(rgnName);
                if (!getRegionAttributes().getDataPolicy().withReplication()) {
                    rgn.create("key", null);
                    getSystem().getLogWriter().info("testTXAlgebra: Created Key");
                }
            } catch (CacheException e) {
                fail("While creating region", e);
            }
        }
    };
    Invoke.invokeInEveryVM(create);
    // check C + P -> C
    try {
        AttributesFactory rgnAtts = new AttributesFactory(getRegionAttributes());
        rgnAtts.setDataPolicy(DataPolicy.NORMAL);
        Region rgn = createRegion(rgnName, rgnAtts.create());
        //// callbackVal.reset();
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        rgn.create("key", "value1");
        //// callbackVal.assertCreateCnt(1);
        try {
            rgn.create("key", "value2");
            fail("expected EntryExistsException");
        } catch (EntryExistsException ok) {
        }
        //// callbackVal.assertCreateCnt(1, /*remember*/ false);
        rgn.put("key", "value2");
        //// callbackVal.assertUpdateCnt(1);
        assertEquals("value2", rgn.getEntry("key").getValue());
        txMgr.commit();
        // Make sure commit did not trigger callbacks
        //// callbackVal.reAssert();
        Invoke.invokeInEveryVM(new CacheSerializableRunnable("testTXAlgebra: check: C+P->C") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                assertEquals("value2", rgn1.getEntry("key").getValue());
                tl.assertCounts(1, 0, 0, 0);
                {
                    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() == rgn1);
                    assertEquals("key", ev.getKey());
                    assertEquals("value2", ev.getNewValue());
                    assertNull(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());
                }
            }
        });
        assertEquals("value2", rgn.getEntry("key").getValue());
        {
            localTl.assertCounts(1, 0, 0, 0);
            Collection events = localTl.lastEvent.getCreateEvents();
            assertEquals(myTXId, localTl.lastEvent.getTransactionId());
            assertEquals(1, events.size());
            EntryEvent ev = (EntryEvent) events.iterator().next();
            assertEquals(myTXId, ev.getTransactionId());
            assertTrue(ev.getRegion() == rgn);
            assertEquals("key", ev.getKey());
            assertEquals("value2", ev.getNewValue());
            assertNull(ev.getOldValue());
            assertTrue(!ev.getOperation().isLocalLoad());
            assertTrue(!ev.getOperation().isNetLoad());
            assertTrue(!ev.getOperation().isLoad());
            assertTrue(!ev.getOperation().isNetSearch());
            assertEquals(null, ev.getCallbackArgument());
            assertEquals(true, ev.isCallbackArgumentAvailable());
            assertTrue(!ev.isOriginRemote());
            assertTrue(!ev.getOperation().isExpiration());
            assertTrue(ev.getOperation().isDistributed());
        }
        if (!getRegionAttributes().getDataPolicy().withReplication()) {
            rgn.invalidate("key");
            rgn.localDestroy("key");
        } else {
            rgn.destroy("key");
        }
        // Check C + DI -> C (invalid value)
        //// callbackVal.reset();
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        rgn.create("key", "value1");
        //// callbackVal.assertCreateCnt(1);
        rgn.invalidate("key");
        //// callbackVal.assertInvalidateCnt(1);
        assertTrue(rgn.containsKey("key"));
        assertTrue(!rgn.containsValueForKey("key"));
        txMgr.commit();
        //// callbackVal.reAssert();
        Invoke.invokeInEveryVM(new CacheSerializableRunnable("testTXAlgebra: check: C+DI->C (invalid value)") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                assertTrue(rgn1.containsKey("key"));
                assertTrue(!rgn1.containsValueForKey("key"));
                tl.assertCounts(2, 0, 0, 0);
                {
                    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() == rgn1);
                    assertEquals("key", ev.getKey());
                    assertNull(ev.getNewValue());
                    assertNull(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());
                }
            }
        });
        assertTrue(rgn.containsKey("key"));
        assertTrue(!rgn.containsValueForKey("key"));
        localTl.assertCounts(2, 0, 0, 0);
        {
            Collection events = localTl.lastEvent.getCreateEvents();
            assertEquals(myTXId, localTl.lastEvent.getTransactionId());
            assertEquals(1, events.size());
            EntryEvent ev = (EntryEvent) events.iterator().next();
            assertEquals(myTXId, ev.getTransactionId());
            assertTrue(ev.getRegion() == rgn);
            assertEquals("key", ev.getKey());
            assertNull(ev.getNewValue());
            assertNull(ev.getOldValue());
            assertTrue(!ev.getOperation().isLocalLoad());
            assertTrue(!ev.getOperation().isNetLoad());
            assertTrue(!ev.getOperation().isLoad());
            assertTrue(!ev.getOperation().isNetSearch());
            assertEquals(null, ev.getCallbackArgument());
            assertEquals(true, ev.isCallbackArgumentAvailable());
            assertTrue(!ev.isOriginRemote());
            assertTrue(!ev.getOperation().isExpiration());
            assertTrue(ev.getOperation().isDistributed());
        }
        // check (commited) LI + DI -> NOOP
        assertTrue(rgn.containsKey("key"));
        assertTrue(!rgn.containsValueForKey("key"));
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        rgn.invalidate("key");
        assertTrue(rgn.containsKey("key"));
        assertTrue(!rgn.containsValueForKey("key"));
        txMgr.commit();
        Invoke.invokeInEveryVM(new CacheSerializableRunnable("testTXAlgebra: check: committed LI + TX DI-> NOOP") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                assertEquals("value1", rgn1.getEntry("key").getValue());
                // There should be no change in counts
                tl.assertCounts(2, 0, 0, 0);
            }
        });
        assertTrue(rgn.containsKey("key"));
        assertTrue(!rgn.containsValueForKey("key"));
        // There should be no change in counts
        localTl.assertCounts(2, 0, 0, 0);
        if (!getRegionAttributes().getDataPolicy().withReplication()) {
            // check (exists commited) LI + DI -> LI
            rgn.put("key", "value1");
            assertTrue(rgn.containsKey("key"));
            assertEquals("value1", rgn.getEntry("key").getValue());
            txMgr.begin();
            myTXId = txMgr.getTransactionId();
            rgn.localInvalidate("key");
            rgn.invalidate("key");
            assertTrue(rgn.containsKey("key"));
            assertTrue(!rgn.containsValueForKey("key"));
            txMgr.commit();
            Invoke.invokeInEveryVM(new CacheSerializableRunnable("testTXAlgebra: check: TX LI + TX DI -> LI") {

                @Override
                public void run2() {
                    Region rgn1 = getRootRegion().getSubregion(rgnName);
                    CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                    MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                    assertTrue(rgn1.containsKey("key"));
                    assertTrue(rgn1.containsValueForKey("key"));
                    assertEquals("value1", rgn1.getEntry("key").getValue());
                    // nothing happened remotely
                    tl.assertCounts(2, 0, 0, 0);
                }
            });
            assertTrue(rgn.containsKey("key"));
            assertTrue(!rgn.containsValueForKey("key"));
            assertNull(rgn.getEntry("key").getValue());
            localTl.assertCounts(3, 0, 0, 0);
            {
                Collection events = localTl.lastEvent.getInvalidateEvents();
                assertEquals(1, events.size());
                EntryEvent ev = (EntryEvent) events.iterator().next();
                assertEquals(myTXId, ev.getTransactionId());
                assertTrue(ev.getRegion() == rgn);
                assertEquals("key", ev.getKey());
                assertNull(ev.getNewValue());
                assertEquals("value1", ev.getOldValue());
                assertTrue(!ev.getOperation().isLocalLoad());
                assertTrue(!ev.getOperation().isNetLoad());
                assertTrue(!ev.getOperation().isLoad());
                assertTrue(!ev.getOperation().isNetSearch());
                assertEquals(null, ev.getCallbackArgument());
                assertEquals(true, ev.isCallbackArgumentAvailable());
                assertTrue(!ev.isOriginRemote());
                assertTrue(!ev.getOperation().isExpiration());
                assertTrue(ev.getOperation().isDistributed());
            }
            rgn.invalidate("key");
            rgn.localDestroy("key");
        } else {
            rgn.destroy("key");
        }
        // check C + DD -> DD
        //// callbackVal.reset();
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        rgn.create("key", "value0");
        //// callbackVal.assertCreateCnt(1);
        rgn.destroy("key");
        //// callbackVal.assertDestroyCnt(1);
        assertTrue(!rgn.containsKey("key"));
        txMgr.commit();
        //// callbackVal.reAssert();
        Invoke.invokeInEveryVM(new CacheSerializableRunnable("testTXAlgebra: check: C+DD->DD") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                assertTrue(!rgn1.containsKey("key"));
                assertTrue(!rgn1.containsValueForKey("key"));
                tl.assertCounts(3, 0, 0, 0);
                {
                    Collection events = tl.lastEvent.getDestroyEvents();
                    assertEquals(1, events.size());
                    EntryEvent ev = (EntryEvent) events.iterator().next();
                    // assertIndexDetailsEquals(tl.expectedTxId, ev.getTransactionId());
                    assertTrue(ev.getRegion() == rgn1);
                    assertNull(ev.getKey());
                    assertNull(ev.getNewValue());
                    assertNull(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());
                }
            }
        });
        assertTrue(!rgn.containsKey("key"));
        // no change
        localTl.assertCounts(3, 0, 0, 0);
        // Check C + LI -> C
        if (!getRegionAttributes().getDataPolicy().withReplication()) {
            // assume that remote regions have same mirror type as local
            Invoke.invokeInEveryVM(new CacheSerializableRunnable("testTXAlgebra: C+LI-> entry creation") {

                @Override
                public void run2() {
                    Region rgn1 = getRootRegion().getSubregion(rgnName);
                    try {
                        rgn1.create("key", null);
                    } catch (CacheException e) {
                        fail("While creating key", e);
                    }
                }
            });
        }
        assertTrue(!rgn.containsKey("key"));
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        rgn.create("key", "value1");
        //// callbackVal.assertCreateCnt(1);
        rgn.localInvalidate("key");
        //// callbackVal.assertInvalidateCnt(1);
        assertTrue(rgn.containsKey("key"));
        assertTrue(!rgn.containsValueForKey("key"));
        txMgr.commit();
        //// callbackVal.reAssert();
        Invoke.invokeInEveryVM(new CacheSerializableRunnable("testTXAlgebra: check: C+LI->C (with value)") {

            @Override
            public void run2() {
                Region rgn1 = getRootRegion().getSubregion(rgnName);
                CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
                MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
                tl.assertCounts(4, 0, 0, 0);
                assertTrue(rgn1.containsKey("key"));
                assertEquals("value1", rgn1.getEntry("key").getValue());
                {
                    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() == rgn1);
                    assertEquals("key", ev.getKey());
                    assertEquals("value1", ev.getNewValue());
                    assertNull(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());
                }
            }
        });
        assertTrue(rgn.containsKey("key"));
        assertTrue(!rgn.containsValueForKey("key"));
        localTl.assertCounts(4, 0, 0, 0);
        {
            Collection events = localTl.lastEvent.getCreateEvents();
            assertEquals(myTXId, localTl.lastEvent.getTransactionId());
            assertEquals(1, events.size());
            EntryEvent ev = (EntryEvent) events.iterator().next();
            assertEquals(myTXId, ev.getTransactionId());
            assertTrue(ev.getRegion() == rgn);
            assertEquals("key", ev.getKey());
            assertNull(ev.getNewValue());
            assertNull(ev.getOldValue());
            assertTrue(!ev.getOperation().isLocalLoad());
            assertTrue(!ev.getOperation().isNetLoad());
            assertTrue(!ev.getOperation().isLoad());
            assertTrue(!ev.getOperation().isNetSearch());
            assertEquals(null, ev.getCallbackArgument());
            assertEquals(true, ev.isCallbackArgumentAvailable());
            assertTrue(!ev.isOriginRemote());
            assertTrue(!ev.getOperation().isExpiration());
            assertTrue(ev.getOperation().isDistributed());
        }
        rgn.destroy("key");
        // Check P + LI + C -> EX
        // Check C + LI + P -> C
        //// callbackVal.reset();
        txMgr.begin();
        myTXId = txMgr.getTransactionId();
        rgn.create("key", "value1");
        //// callbackVal.assertCreateCnt(1);
        rgn.localInvalidate("key");
        //// callbackVal.assertInvalidateCnt(1);
        try {
            rgn.create("key", "ex");
            fail("expected EntryExistsException");
        } catch (EntryExistsException ok) {
        }
        //// callbackVal.assertCreateCnt(1, /*remember*/ false);
        rgn.put("key", "value2");
        //// callbackVal.assertUpdateCnt(1);
        assertTrue(rgn.containsKey("key"));
        assertEquals("value2", rgn.getEntry("key").getValue());
        txMgr.commit();
        //// callbackVal.reAssert();
        assertTrue(rgn.containsKey("key"));
        assertEquals("value2", rgn.getEntry("key").getValue());
        localTl.assertCounts(5, 0, 0, 0);
        {
            Collection events = localTl.lastEvent.getCreateEvents();
            assertEquals(myTXId, localTl.lastEvent.getTransactionId());
            assertEquals(1, events.size());
            EntryEvent ev = (EntryEvent) events.iterator().next();
            assertEquals(myTXId, ev.getTransactionId());
            assertTrue(ev.getRegion() == rgn);
            assertEquals("key", ev.getKey());
            assertEquals("value2", ev.getNewValue());
            assertNull(ev.getOldValue());
            assertTrue(!ev.getOperation().isLocalLoad());
            assertTrue(!ev.getOperation().isNetLoad());
            assertTrue(!ev.getOperation().isLoad());
            assertTrue(!ev.getOperation().isNetSearch());
            assertEquals(null, ev.getCallbackArgument());
            assertEquals(true, ev.isCallbackArgumentAvailable());
            assertTrue(!ev.isOriginRemote());
            assertTrue(!ev.getOperation().isExpiration());
            assertTrue(ev.getOperation().isDistributed());
        }
        rgn.localDestroy("key");
    } catch (Exception e) {
        CacheFactory.getInstance(getSystem()).close();
        getSystem().getLogWriter().fine("testTXAlgebra: Caused exception in createRegion");
        throw e;
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) EntryExistsException(org.apache.geode.cache.EntryExistsException) 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) AttributesFactory(org.apache.geode.cache.AttributesFactory) 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) Ignore(org.junit.Ignore) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Aggregations

EntryExistsException (org.apache.geode.cache.EntryExistsException)11 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)6 Region (org.apache.geode.cache.Region)6 CacheException (org.apache.geode.cache.CacheException)5 Test (org.junit.Test)5 LocalRegion (org.apache.geode.internal.cache.LocalRegion)4 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)4 Cache (org.apache.geode.cache.Cache)3 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)3 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 Random (java.util.Random)2 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)2 CommitConflictException (org.apache.geode.cache.CommitConflictException)2 EntryEvent (org.apache.geode.cache.EntryEvent)2 TransactionId (org.apache.geode.cache.TransactionId)2 TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)2 TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1