Search in sources :

Example 16 with EntryEvent

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

the class PartitionedRegionDUnitTest method testRegionInvalidationWithAdjunctMessages.

/**
   * Bug #47235 concerns assertion failures being thrown when there is a member that receives
   * adjunct messages (as in a WAN gateway, a peer with clients, etc).
   */
@Test
public void testRegionInvalidationWithAdjunctMessages() throws Exception {
    final String name = getUniqueName();
    VM vm1 = Host.getHost(0).getVM(1);
    Cache cache = getCache();
    RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
    Region pr = fact.create(name + "Region");
    pr.put("Object1", "Value1");
    vm1.invoke(new SerializableRunnable("create PR") {

        @Override
        public void run() {
            RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
            fact.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
            fact.addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterInvalidate(EntryEvent event) {
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("afterInvalidate invoked with " + event);
                    InvalidateInvoked = true;
                }
            });
            fact.create(name + "Region");
        }
    });
    try {
        pr.invalidateRegion();
        assertTrue("vm1 should have invoked the listener for an invalidateRegion operation", (Boolean) vm1.invoke(new SerializableCallable("getStatus") {

            public Object call() {
                return InvalidateInvoked;
            }
        }));
    } finally {
        disconnectAllFromDS();
    }
}
Also used : 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) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 17 with EntryEvent

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

the class TestDiskRegion method main1.

public static void main1(String[] args) throws Exception {
    DistributedSystem system = DistributedSystem.connect(new java.util.Properties());
    Cache cache = CacheFactory.create(system);
    AttributesFactory factory = new AttributesFactory();
    factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(2, (ObjectSizer) null, EvictionAction.OVERFLOW_TO_DISK));
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterUpdate(EntryEvent event) {
            System.out.println("UPDATE: " + event.getKey() + " -> (" + event.getOldValue() + " -> " + event.getNewValue() + ")");
        }
    });
    LocalRegion region = (LocalRegion) cache.createRegion("TestDiskRegion", factory.create());
    DiskRegion dr = region.getDiskRegion();
    DiskRegionStats diskStats = dr.getStats();
    LRUStatistics lruStats = getLRUStats(region);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Hit enter to perform action");
    for (int i = 0; true; i++) {
        br.readLine();
        // Thread.sleep(500);
        Object key = new Integer(i);
        Object value = new byte[200000];
        region.put(key, value);
        System.out.println(key + " -> " + value + " evictions = " + lruStats.getEvictions() + ", writes = " + diskStats.getWrites());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DiskRegionStats(org.apache.geode.internal.cache.DiskRegionStats) ObjectSizer(org.apache.geode.cache.util.ObjectSizer) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedSystem(org.apache.geode.distributed.DistributedSystem) AttributesFactory(org.apache.geode.cache.AttributesFactory) DiskRegion(org.apache.geode.internal.cache.DiskRegion) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) LRUStatistics(org.apache.geode.internal.cache.lru.LRUStatistics) BufferedReader(java.io.BufferedReader) Cache(org.apache.geode.cache.Cache)

Example 18 with EntryEvent

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

the class RemoteTransactionDUnitTest method testBug45556.

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

        private int count;

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

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

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

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

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

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

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

        @Override
        public Object call() throws Exception {
            Region rgn = getCache().getRegion(name);
            CountingListener cl = (CountingListener) rgn.getAttributes().getCacheListeners()[0];
            assertEquals(2, cl.count);
            return null;
        }
    });
}
Also used : Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionId(org.apache.geode.cache.TransactionId) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 19 with EntryEvent

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

the class RemoteTransactionDUnitTest method testNonInlineRemoteEvents.

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

        boolean assertException = false;

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

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

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

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

Example 20 with EntryEvent

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

the class FailoverDUnitTest method createClientCache.

public static void createClientCache(String hostName, Integer port1, Integer port2) throws Exception {
    PORT1 = port1.intValue();
    PORT2 = port2.intValue();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new FailoverDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    ClientServerTestCase.configureConnectionPoolWithName(factory, hostName, new int[] { PORT1, PORT2 }, true, -1, 2, null, "FailoverPool");
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterUpdate(EntryEvent event) {
            synchronized (this) {
                cache.getLogger().info("Event Received : key..." + event.getKey());
                cache.getLogger().info("Event Received : value..." + event.getNewValue());
            }
        }
    });
    cache.createRegion(regionName, factory.create());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

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