Search in sources :

Example 11 with EntryEvent

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

the class PartitionedRegionEvictionDUnitTest method testEntryLRUDeadlock.

/**
   * Test that gets do not need to acquire a lock on the region entry when LRU is enabled. This is
   * bug 42265
   */
@Test
public void testEntryLRUDeadlock() {
    final Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    final String uniqName = getUniqueName();
    final int redundantCopies = 0;
    final int maxBuckets = 8;
    final int maxEntries = 16;
    final String name = uniqName + "-PR";
    final int extraEntries = 4;
    // final int heapPercentage = 66;
    // final int evictorInterval = 100;
    final SerializableRunnable create = new CacheSerializableRunnable("Create Entry LRU with local destroy on a partitioned Region") {

        public void run2() {
            final AttributesFactory factory = new AttributesFactory();
            factory.setOffHeap(isOffHeap());
            factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setTotalNumBuckets(maxBuckets).create());
            factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries, EvictionAction.LOCAL_DESTROY));
            final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
            assertNotNull(pr);
        }
    };
    vm0.invoke(create);
    final SerializableRunnable create2 = new SerializableRunnable("Create Entry LRU with local destroy on a partitioned Region") {

        public void run() {
            try {
                final AttributesFactory factory = new AttributesFactory();
                factory.setOffHeap(isOffHeap());
                factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setLocalMaxMemory(0).setTotalNumBuckets(maxBuckets).create());
                factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries));
                factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
                // this listener will cause a deadlock if the get ends up
                // locking the entry.
                factory.addCacheListener(new CacheListenerAdapter() {

                    @Override
                    public void afterCreate(EntryEvent event) {
                        Region region = event.getRegion();
                        Object key = event.getKey();
                        region.get(key);
                    }
                });
                final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
                assertNotNull(pr);
            } catch (final CacheException ex) {
                Assert.fail("While creating Partitioned region", ex);
            }
        }
    };
    vm1.invoke(create2);
    final SerializableRunnable doPuts = new SerializableRunnable("Do Puts") {

        public void run() {
            final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
            assertNotNull(pr);
            for (int counter = 0; counter <= maxEntries + extraEntries; counter++) {
                pr.put(new Integer(counter), "value");
            }
        }
    };
    vm0.invoke(doPuts);
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Host(org.apache.geode.test.dunit.Host) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 12 with EntryEvent

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

the class ConflationDUnitTest method createClientCache2CommonWriter.

/**
   * create client 2 with 2 regions with sharing a common writer and having a common listener
   * 
   * @throws Exception
   */
public static void createClientCache2CommonWriter(String host, Integer port) throws Exception {
    ConflationDUnitTest test = new ConflationDUnitTest();
    cache = test.createCache(createProperties1());
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setPoolName(createPool(host, "p1", port, true).getName());
    factory.addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            String val = (String) event.getNewValue();
            synchronized (ConflationDUnitTest.class) {
                if (val.equals(MARKER)) {
                    count++;
                } else {
                    counterCreate++;
                }
                if (2 == count) {
                    ConflationDUnitTest.class.notify();
                }
            }
        }

        public void afterUpdate(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            synchronized (this) {
                counterUpdate++;
            }
        }

        public void afterDestroy(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            synchronized (this) {
                if (!event.getKey().equals(MARKER)) {
                    counterDestroy++;
                }
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME1, attrs);
    cache.createRegion(REGION_NAME2, attrs);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) EntryEvent(org.apache.geode.cache.EntryEvent)

Example 13 with EntryEvent

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

the class ClientConflationDUnitTest method createClientCache.

/**
   * create client 2 with 2 regions each with a unique writer and unique listeners
   * 
   * @throws Exception
   */
public static void createClientCache(String host, Integer port, String conflation) throws Exception {
    ClientConflationDUnitTest test = new ClientConflationDUnitTest();
    cacheClient = test.createCache(createProperties1(conflation));
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    createPool2(host, factory, port);
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            synchronized (ClientConflationDUnitTest.class) {
                counterCreate1++;
            }
        }

        public void afterUpdate(EntryEvent event) {
            // getLogWriter().info("afterUpdate event = " + event, new Exception());
            synchronized (this) {
                counterUpdate1++;
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cacheClient.createRegion(REGION_NAME1, attrs);
    createPool2(host, factory, port);
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            synchronized (ClientConflationDUnitTest.class) {
                counterCreate2++;
            }
        }

        public void afterUpdate(EntryEvent event) {
            synchronized (this) {
                counterUpdate2++;
            }
        }
    });
    attrs = factory.create();
    cacheClient.createRegion(REGION_NAME2, attrs);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) EntryEvent(org.apache.geode.cache.EntryEvent)

Example 14 with EntryEvent

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

the class MyGatewayEventSubstitutionFilter method addCacheListenerAndCloseCache.

public static void addCacheListenerAndCloseCache(String regionName) {
    final Region region = cache.getRegion(Region.SEPARATOR + regionName);
    assertNotNull(region);
    CacheListenerAdapter cl = new CacheListenerAdapter() {

        @Override
        public void afterCreate(EntryEvent event) {
            if ((Long) event.getKey() == 900) {
                cache.getLogger().fine(" Gateway sender is killed by a test");
                cache.close();
                cache.getDistributedSystem().disconnect();
            }
        }
    };
    region.getAttributesMutator().addCacheListener(cl);
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region)

Example 15 with EntryEvent

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

the class ClientServerCCEDUnitTest method testClientDoesNotExpireEntryPrematurely.

@Test
public void testClientDoesNotExpireEntryPrematurely() throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String name = this.getUniqueName() + "Region";
    final String key = "testKey";
    int port = createServerRegion(vm0, name, true);
    vm0.invoke(new SerializableCallable("create old entry") {

        public Object call() throws Exception {
            LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
            r.put(key, "value");
            AbstractRegionEntry entry = (AbstractRegionEntry) r.basicGetEntry(key);
            // set an old timestamp in the entry - thirty minutes ago
            entry.getVersionStamp().setVersionTimeStamp(System.currentTimeMillis() - 1800000L);
            return null;
        }
    });
    createClientRegion(vm1, name, port, true, ClientRegionShortcut.CACHING_PROXY, false);
    vm1.invoke(new SerializableCallable("fetch entry and validate") {

        public Object call() throws Exception {
            final Long[] expirationTimeMillis = new Long[1];
            int expirationSeconds = 15;
            LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
            AttributesMutator mutator = r.getAttributesMutator();
            mutator.setEntryIdleTimeout(new ExpirationAttributes(expirationSeconds, ExpirationAction.LOCAL_DESTROY));
            mutator.addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterDestroy(EntryEvent event) {
                    expirationTimeMillis[0] = System.currentTimeMillis();
                }
            });
            // fetch the entry from the server and make sure it doesn't expire early
            if (!r.containsKey(key)) {
                r.get(key);
            }
            final long expirationTime = System.currentTimeMillis() + (expirationSeconds * 1000);
            Awaitility.await("waiting for object to expire").atMost(expirationSeconds * 2, TimeUnit.SECONDS).until(() -> {
                return expirationTimeMillis[0] != null;
            });
            disconnectFromDS();
            assertTrue("entry expired " + (expirationTime - expirationTimeMillis[0]) + " milliseconds early", expirationTimeMillis[0] >= expirationTime);
            return null;
        }
    });
    vm0.invoke(new SerializableRunnable() {

        public void run() {
            disconnectFromDS();
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) IgnoredException(org.apache.geode.test.dunit.IgnoredException) AbstractRegionEntry(org.apache.geode.internal.cache.AbstractRegionEntry) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

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