Search in sources :

Example 26 with EntryEvent

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

the class MultiVMRegionTestCase method testRemoteCacheListener.

/**
   * Tests that a {@link CacheListener} is invoked in a remote VM.
   */
// GEODE-153 & GEODE-932: time sensitive, waitForInvocation
@Category(FlakyTest.class)
// (waitForCriterion), 3 second timeouts
@Test
public void testRemoteCacheListener() throws Exception {
    assertTrue(getRegionAttributes().getScope().isDistributed());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object oldValue = "OLD_VALUE";
    final Object newValue = "NEW_VALUE";
    // final Object key2 = "KEY2";
    // final Object value2 = "VALUE2";
    SerializableRunnable populate = new CacheSerializableRunnable("Create Region and Put") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            region.put(key, oldValue);
        }
    };
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    vm0.invoke(populate);
    vm1.invoke(populate);
    vm1.invoke(new CacheSerializableRunnable("Set listener") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            listener = new TestCacheListener() {

                @Override
                public void afterUpdate2(EntryEvent event) {
                    assertEquals(Operation.UPDATE, event.getOperation());
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(event.getCallbackArgument(), event.getDistributedMember());
                    assertEquals(key, event.getKey());
                    assertEquals(oldValue, event.getOldValue());
                    assertEquals(newValue, event.getNewValue());
                    assertFalse(event.getOperation().isLoad());
                    assertFalse(event.getOperation().isLocalLoad());
                    assertFalse(event.getOperation().isNetLoad());
                    assertFalse(event.getOperation().isNetSearch());
                    if (event.getRegion().getAttributes().getOffHeap()) {
                        // since off heap always serializes the old value is serialized and available
                        assertEquals(oldValue, event.getSerializedOldValue().getDeserializedValue());
                    } else {
                        // since it was put originally in
                        assertEquals(null, event.getSerializedOldValue());
                    // this VM
                    }
                    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(event.getSerializedNewValue().getSerializedValue()));
                    try {
                        assertEquals(newValue, DataSerializer.readObject(dis));
                    } catch (Exception e) {
                        fail("Unexpected Exception", e);
                    }
                }
            };
            region.getAttributesMutator().addCacheListener(listener);
        }
    });
    // I see no reason to pause here.
    // The test used to pause here but only if no-ack.
    // But we have no operations to wait for.
    // The last thing we did was install a listener in vm1
    // and it is possible that vm0 does not yet know we have
    // a listener but for this test it does not matter.
    // So I'm commenting out the following pause:
    // pauseIfNecessary();
    // If needed then do a flushIfNecessary(region) after adding the cache listener
    vm0.invoke(new CacheSerializableRunnable("Update") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.put(key, newValue, getSystem().getDistributedMember());
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify Update") {

        @Override
        public void run2() throws CacheException {
            listener.waitForInvocation(3000, 10);
            // Setup listener for next test
            final Region region = getRootRegion().getSubregion(name);
            listener = new TestCacheListener() {

                @Override
                public void afterInvalidate2(EntryEvent event) {
                    assertEquals(Operation.INVALIDATE, event.getOperation());
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(event.getCallbackArgument(), event.getDistributedMember());
                    assertEquals(key, event.getKey());
                    assertEquals(newValue, event.getOldValue());
                    assertNull(event.getNewValue());
                    assertFalse(event.getOperation().isLoad());
                    assertFalse(event.getOperation().isLocalLoad());
                    assertFalse(event.getOperation().isNetLoad());
                    assertFalse(event.getOperation().isNetSearch());
                    assertNull(event.getSerializedNewValue());
                    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(event.getSerializedOldValue().getSerializedValue()));
                    try {
                        assertEquals(newValue, DataSerializer.readObject(dis));
                    } catch (Exception e) {
                        fail("Unexpected Exception", e);
                    }
                }
            };
            region.getAttributesMutator().addCacheListener(listener);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Invalidate") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.invalidate(key, getSystem().getDistributedMember());
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify Invalidate") {

        @Override
        public void run2() throws CacheException {
            listener.waitForInvocation(3000, 10);
            // Setup listener for next test
            final Region region = getRootRegion().getSubregion(name);
            listener = new TestCacheListener() {

                @Override
                public void afterDestroy2(EntryEvent event) {
                    assertTrue(event.getOperation().isDestroy());
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(event.getCallbackArgument(), event.getDistributedMember());
                    assertEquals(key, event.getKey());
                    assertNull(event.getOldValue());
                    assertNull(event.getNewValue());
                    assertFalse(event.getOperation().isLoad());
                    assertFalse(event.getOperation().isLocalLoad());
                    assertFalse(event.getOperation().isNetLoad());
                    assertFalse(event.getOperation().isNetSearch());
                    assertNull(event.getSerializedOldValue());
                    assertNull(event.getSerializedNewValue());
                }
            };
            region.getAttributesMutator().addCacheListener(listener);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Destroy") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.destroy(key, getSystem().getDistributedMember());
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify Destroy") {

        @Override
        public void run2() throws CacheException {
            listener.waitForInvocation(3000, 10);
            // Setup listener for next test
            final Region region = getRootRegion().getSubregion(name);
            listener = new TestCacheListener() {

                @Override
                public void afterRegionInvalidate2(RegionEvent event) {
                    assertEquals(Operation.REGION_INVALIDATE, event.getOperation());
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(event.getCallbackArgument(), event.getDistributedMember());
                }
            };
            region.getAttributesMutator().addCacheListener(listener);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Invalidate Region") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.invalidateRegion(getSystem().getDistributedMember());
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify Invalidate Region") {

        @Override
        public void run2() throws CacheException {
            listener.waitForInvocation(3000, 10);
            // Setup listener for next test
            final Region region = getRootRegion().getSubregion(name);
            listener = new TestCacheListener() {

                @Override
                public void afterRegionDestroy2(RegionEvent event) {
                    assertEquals(Operation.REGION_DESTROY, event.getOperation());
                    assertEquals(region, event.getRegion());
                    assertTrue(event.getOperation().isDistributed());
                    assertFalse(event.getOperation().isExpiration());
                    assertTrue(event.isOriginRemote());
                    assertEquals(event.getCallbackArgument(), event.getDistributedMember());
                }
            };
            region.getAttributesMutator().addCacheListener(listener);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Destroy Region") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.destroyRegion(getSystem().getDistributedMember());
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Verify Destroy Region") {

        @Override
        public void run2() throws CacheException {
            listener.waitForInvocation(3000, 10);
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) DataInputStream(java.io.DataInputStream) RegionEvent(org.apache.geode.cache.RegionEvent) 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) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) StoredObject(org.apache.geode.internal.offheap.StoredObject) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 27 with EntryEvent

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

use of org.apache.geode.cache.EntryEvent 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)

Example 29 with EntryEvent

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

the class MultiVMRegionTestCase method testLocalAndRemoteCacheWriters.

/**
   * Tests that, when given a choice, a local <code>CacheWriter</code> is invoked instead of a
   * remote one.
   */
@Test
public void testLocalAndRemoteCacheWriters() throws Exception {
    assertTrue(getRegionAttributes().getScope().isDistributed());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object oldValue = "OLD_VALUE";
    final Object newValue = "NEW_VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    vm0.invoke(new CacheSerializableRunnable("Create \"Local\" Region") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            writer = new TestCacheWriter() {

                @Override
                public void beforeUpdate2(EntryEvent event) throws CacheWriterException {
                }

                @Override
                public void beforeCreate2(EntryEvent event) throws CacheWriterException {
                }

                @Override
                public void beforeDestroy2(EntryEvent event) throws CacheWriterException {
                }

                @Override
                public void beforeRegionDestroy2(RegionEvent event) throws CacheWriterException {
                }
            };
            region.getAttributesMutator().setCacheWriter(writer);
        }
    });
    SerializableRunnable create = new CacheSerializableRunnable("Create \"Local\" Region") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            writer = new TestCacheWriter() {
            };
            region.getAttributesMutator().setCacheWriter(writer);
        }
    };
    vm1.invoke(create);
    SerializableRunnable verify = new SerializableRunnable("Verify no callback") {

        @Override
        public void run() {
            assertFalse(writer.wasInvoked());
        }
    };
    vm0.invoke(new CacheSerializableRunnable("Create entry") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.put(key, oldValue);
            assertTrue(writer.wasInvoked());
        }
    });
    vm1.invoke(verify);
    vm0.invoke(new CacheSerializableRunnable("Update entry") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.put(key, newValue);
            assertTrue(writer.wasInvoked());
        }
    });
    vm1.invoke(verify);
    vm0.invoke(new CacheSerializableRunnable("Destroy entry") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.destroy(key);
            assertTrue(writer.wasInvoked());
        }
    });
    vm1.invoke(verify);
    vm0.invoke(new CacheSerializableRunnable("Destroy region") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.destroyRegion();
            assertTrue(writer.wasInvoked());
        }
    });
    vm1.invoke(verify);
}
Also used : CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) Host(org.apache.geode.test.dunit.Host) RegionEvent(org.apache.geode.cache.RegionEvent) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 30 with EntryEvent

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

the class MultiVMRegionTestCase method versionTestTombstones.

public void versionTestTombstones() {
    disconnectAllFromDS();
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final int numEntries = 100;
    // create replicated regions in VM 0 and 1, then perform concurrent ops
    // on the same key while creating the region in VM2. Afterward make
    // sure that all three regions are consistent
    final long oldServerTimeout = TombstoneService.REPLICATE_TOMBSTONE_TIMEOUT;
    final long oldClientTimeout = TombstoneService.NON_REPLICATE_TOMBSTONE_TIMEOUT;
    final int oldExpiredTombstoneLimit = TombstoneService.EXPIRED_TOMBSTONE_LIMIT;
    final boolean oldIdleExpiration = TombstoneService.IDLE_EXPIRATION;
    final double oldLimit = TombstoneService.GC_MEMORY_THRESHOLD;
    final long oldMaxSleepTime = TombstoneService.MAX_SLEEP_TIME;
    try {
        SerializableRunnable setTimeout = new SerializableRunnable() {

            @Override
            public void run() {
                TombstoneService.REPLICATE_TOMBSTONE_TIMEOUT = 1000;
                TombstoneService.NON_REPLICATE_TOMBSTONE_TIMEOUT = 900;
                TombstoneService.EXPIRED_TOMBSTONE_LIMIT = numEntries;
                TombstoneService.IDLE_EXPIRATION = true;
                // turn this off so heap profile won't cause
                TombstoneService.GC_MEMORY_THRESHOLD = 0;
                // test to fail
                TombstoneService.MAX_SLEEP_TIME = 500;
            }
        };
        vm0.invoke(setTimeout);
        vm1.invoke(setTimeout);
        final String name = this.getUniqueName() + "-CC";
        SerializableRunnable createRegion = new SerializableRunnable("Create Region") {

            @Override
            public void run() {
                try {
                    RegionFactory f = getCache().createRegionFactory(getRegionAttributes());
                    CCRegion = (LocalRegion) f.create(name);
                    for (int i = 0; i < numEntries; i++) {
                        CCRegion.put("cckey" + i, "ccvalue");
                    }
                    if (CCRegion.getScope().isDistributedNoAck()) {
                        // flush the ops
                        sendSerialMessageToAll();
                    }
                } catch (CacheException ex) {
                    fail("While creating region", ex);
                }
            }
        };
        vm0.invoke(createRegion);
        vm1.invoke(createRegion);
        vm0.invoke(new SerializableRunnable("destroy entries and check tombstone count") {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < numEntries; i++) {
                        CCRegion.destroy("cckey" + i);
                        assertTrue("entry should not exist", !CCRegion.containsKey("cckey" + i));
                        assertTrue("entry should not contain a value", !CCRegion.containsValueForKey("cckey" + i));
                    }
                    checkCCRegionTombstoneCount("after destroys in this vm ", numEntries);
                    assertTrue("region should not contain a tombstone", !CCRegion.containsValue(Token.TOMBSTONE));
                    if (CCRegion.getScope().isDistributedNoAck()) {
                        // flush the ops
                        sendSerialMessageToAll();
                    }
                } catch (CacheException e) {
                    fail("while performing destroy operations", e);
                }
            }
        });
        vm1.invoke(new SerializableRunnable("check tombstone count(2)") {

            @Override
            public void run() {
                checkCCRegionTombstoneCount("after destroys in other vm ", numEntries);
                WaitCriterion waitForExpiration = new WaitCriterion() {

                    @Override
                    public boolean done() {
                        return CCRegion.getTombstoneCount() == 0;
                    }

                    @Override
                    public String description() {
                        return "Waiting for all tombstones to expire.  There are now " + CCRegion.getTombstoneCount() + " tombstones left out of " + numEntries + " initial tombstones. " + CCRegion.getCache().getTombstoneService();
                    }
                };
                try {
                    Wait.waitForCriterion(waitForExpiration, TombstoneService.REPLICATE_TOMBSTONE_TIMEOUT + (TombstoneService.MAX_SLEEP_TIME * 9), 100, true);
                } catch (AssertionError e) {
                    CCRegion.dumpBackingMap();
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("tombstone service state: " + CCRegion.getCache().getTombstoneService());
                    throw e;
                }
            }
        });
        vm0.invoke(new SerializableRunnable("create/destroy entries and check tombstone count") {

            @Override
            public void run() {
                final int origCount = CCRegion.getTombstoneCount();
                try {
                    WaitCriterion waitForExpiration = new WaitCriterion() {

                        @Override
                        public boolean done() {
                            return CCRegion.getTombstoneCount() == 0;
                        }

                        @Override
                        public String description() {
                            return "Waiting for all tombstones to expire.  There are now " + CCRegion.getTombstoneCount() + " tombstones left out of " + origCount + " initial tombstones. " + CCRegion.getCache().getTombstoneService();
                        }
                    };
                    Wait.waitForCriterion(waitForExpiration, TombstoneService.REPLICATE_TOMBSTONE_TIMEOUT + (TombstoneService.MAX_SLEEP_TIME * 9), 100, true);
                    logger.debug("creating tombstones.  current count={}", CCRegion.getTombstoneCount());
                    for (int i = 0; i < numEntries; i++) {
                        CCRegion.create("cckey" + i, i);
                        CCRegion.destroy("cckey" + i);
                    }
                    logger.debug("done creating tombstones.  current count={}", CCRegion.getTombstoneCount());
                    checkCCRegionTombstoneCount("after create+destroy in this vm ", numEntries);
                    assertEquals(0, CCRegion.size());
                    afterCreates = 0;
                    AttributesMutator m = CCRegion.getAttributesMutator();
                    m.addCacheListener(new CacheListenerAdapter() {

                        @Override
                        public void afterCreate(EntryEvent event) {
                            afterCreates++;
                        }
                    });
                    if (CCRegion.getScope().isDistributedNoAck()) {
                        // flush the ops
                        sendSerialMessageToAll();
                    }
                } catch (AssertionError e) {
                    CCRegion.dumpBackingMap();
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("tombstone service state: " + CCRegion.getCache().getTombstoneService());
                    throw e;
                } catch (CacheException e) {
                    fail("while performing create/destroy operations", e);
                }
            }
        });
        vm1.invoke(new SerializableRunnable("check tombstone count and install listener") {

            @Override
            public void run() {
                checkCCRegionTombstoneCount("after create+destroy in other vm ", numEntries);
                afterCreates = 0;
                AttributesMutator m = CCRegion.getAttributesMutator();
                m.addCacheListener(new CacheListenerAdapter() {

                    @Override
                    public void afterCreate(EntryEvent event) {
                        afterCreates++;
                    }
                });
            }
        });
        // Now check to see if tombstones are resurrected by a create.
        // The entries should be created okay and the callback should be afterCreate.
        // The tombstone count won't go down until the entries are swept, but then
        // the count should fall to zero.
        vm0.invoke(new SerializableRunnable("create entries and check afterCreate and tombstone count") {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < numEntries; i++) {
                        CCRegion.create("cckey" + i, i);
                    }
                    checkCCRegionTombstoneCount("after create in this vm", 0);
                    assertEquals("expected " + numEntries + " afterCreates", numEntries, afterCreates);
                    assertEquals(numEntries, CCRegion.size());
                    if (CCRegion.getScope().isDistributedNoAck()) {
                        // flush the ops
                        sendSerialMessageToAll();
                    }
                    WaitCriterion waitForExpiration = new WaitCriterion() {

                        @Override
                        public boolean done() {
                            return CCRegion.getCache().getTombstoneService().getScheduledTombstoneCount() == 0;
                        }

                        @Override
                        public String description() {
                            return "Waiting for all scheduled tombstones to be removed.  There are now " + CCRegion.getCache().getTombstoneService().getScheduledTombstoneCount() + " tombstones left out of " + numEntries + " initial tombstones. " + CCRegion.getCache().getTombstoneService();
                        }
                    };
                    Wait.waitForCriterion(waitForExpiration, TombstoneService.REPLICATE_TOMBSTONE_TIMEOUT * 5, 100, true);
                } catch (CacheException e) {
                    fail("while performing create operations", e);
                }
            }
        });
        vm1.invoke(new SerializableRunnable("check afterCreate and tombstone count") {

            @Override
            public void run() {
                checkCCRegionTombstoneCount("after create in other vm", 0);
                assertEquals("expected " + numEntries + " afterCreates", numEntries, afterCreates);
                assertEquals(numEntries, CCRegion.size());
                WaitCriterion waitForExpiration = new WaitCriterion() {

                    @Override
                    public boolean done() {
                        return CCRegion.getCache().getTombstoneService().getScheduledTombstoneCount() == 0;
                    }

                    @Override
                    public String description() {
                        return "Waiting for all scheduled tombstones to be removed.  There are now " + CCRegion.getCache().getTombstoneService().getScheduledTombstoneCount() + " tombstones left out of " + numEntries + " initial tombstones. " + CCRegion.getCache().getTombstoneService();
                    }
                };
                Wait.waitForCriterion(waitForExpiration, TombstoneService.REPLICATE_TOMBSTONE_TIMEOUT * 5, 100, true);
            }
        });
    } finally {
        SerializableRunnable resetTimeout = new SerializableRunnable() {

            @Override
            public void run() {
                TombstoneService.REPLICATE_TOMBSTONE_TIMEOUT = oldServerTimeout;
                TombstoneService.NON_REPLICATE_TOMBSTONE_TIMEOUT = oldClientTimeout;
                TombstoneService.EXPIRED_TOMBSTONE_LIMIT = oldExpiredTombstoneLimit;
                TombstoneService.IDLE_EXPIRATION = oldIdleExpiration;
                TombstoneService.GC_MEMORY_THRESHOLD = oldLimit;
                TombstoneService.MAX_SLEEP_TIME = oldMaxSleepTime;
            }
        };
        vm0.invoke(resetTimeout);
        vm1.invoke(resetTimeout);
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) RegionFactory(org.apache.geode.cache.RegionFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) AttributesMutator(org.apache.geode.cache.AttributesMutator)

Aggregations

EntryEvent (org.apache.geode.cache.EntryEvent)111 AttributesFactory (org.apache.geode.cache.AttributesFactory)75 Region (org.apache.geode.cache.Region)69 Test (org.junit.Test)66 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)55 RegionAttributes (org.apache.geode.cache.RegionAttributes)37 LocalRegion (org.apache.geode.internal.cache.LocalRegion)32 CacheException (org.apache.geode.cache.CacheException)30 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)30 VM (org.apache.geode.test.dunit.VM)29 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)26 CacheWriterException (org.apache.geode.cache.CacheWriterException)22 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)22 Host (org.apache.geode.test.dunit.Host)21 Properties (java.util.Properties)20 RegionEvent (org.apache.geode.cache.RegionEvent)18 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)17 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)15 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)15 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)15