Search in sources :

Example 6 with EntryEvent

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

the class RegionTestCase method testCustomEntryIdleTimeout2.

/**
   * Verify that special entries don't expire but other entries in the region do
   */
@Test
public void testCustomEntryIdleTimeout2() {
    final String name = this.getUniqueName();
    // ms
    final int timeout = 20;
    final String key1 = "KEY1";
    final String key2 = "KEY2";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    factory.setEntryIdleTimeout(expire);
    ExpirationAttributes expire2 = new ExpirationAttributes(0, ExpirationAction.INVALIDATE);
    factory.setCustomEntryIdleTimeout(new TestExpiry(key2, expire2));
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterUpdate2(EntryEvent e) {
        }

        public void afterInvalidate2(EntryEvent e) {
        }
    };
    factory.addCacheListener(list);
    RegionAttributes attrs = factory.create();
    Region region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
    region.create(key2, value);
    // This value should NOT expire.
    Wait.pause(timeout * 2);
    assertTrue(region.get(key2).equals(value));
    // This value SHOULD expire
    // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
    ExpiryTask.suspendExpiration();
    Region.Entry entry = null;
    long tilt;
    try {
        region.create(key1, value);
        tilt = System.currentTimeMillis() + timeout;
        assertTrue(list.waitForInvocation(5000));
        entry = region.getEntry(key1);
        assertNotNull(entry.getValue());
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForInvalidate(entry, tilt);
    // First value should still be in there
    assertTrue(region.get(key2).equals(value));
    // Do it again with a put (I guess)
    ExpiryTask.suspendExpiration();
    try {
        region.put(key1, value);
        tilt = System.currentTimeMillis() + timeout;
        entry = region.getEntry(key1);
        assertNotNull(entry.getValue());
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForInvalidate(entry, tilt);
    // First value should still be in there
    assertTrue(region.get(key2).equals(value));
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 7 with EntryEvent

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

the class RegionTestCase method testCustomEntryIdleReset.

/**
   * Verify that a get or put resets the idle time on an entry
   */
@Test
public void testCustomEntryIdleReset() {
    final String name = this.getUniqueName();
    // ms
    final int timeout = 200 * 1000;
    final String key1 = "KEY1";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    // factory.setEntryIdleTimeout(expire);
    factory.setCustomEntryIdleTimeout(new TestExpiry(key1, expire));
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterUpdate2(EntryEvent e) {
        }

        public void afterInvalidate2(EntryEvent e) {
        }
    };
    factory.addCacheListener(list);
    RegionAttributes attrs = factory.create();
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        LocalRegion region = (LocalRegion) createRegion(name, attrs);
        // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
        ExpiryTask.suspendExpiration();
        try {
            region.create(key1, value);
            assertTrue(list.waitForInvocation(5000));
            Region.Entry entry = region.getEntry(key1);
            assertNotNull(entry.getValue());
            EntryExpiryTask eet = region.getEntryExpiryTask(key1);
            final long createExpiryTime = eet.getExpirationTime();
            Wait.waitForExpiryClockToChange(region);
            region.get(key1);
            assertSame(eet, region.getEntryExpiryTask(key1));
            final long getExpiryTime = eet.getExpirationTime();
            if (getExpiryTime - createExpiryTime <= 0L) {
                fail("get did not reset the expiration time. createExpiryTime=" + createExpiryTime + " getExpiryTime=" + getExpiryTime);
            }
            Wait.waitForExpiryClockToChange(region);
            region.put(key1, value);
            assertSame(eet, region.getEntryExpiryTask(key1));
            final long putExpiryTime = eet.getExpirationTime();
            if (putExpiryTime - getExpiryTime <= 0L) {
                fail("put did not reset the expiration time. getExpiryTime=" + getExpiryTime + " putExpiryTime=" + putExpiryTime);
            }
        } finally {
            ExpiryTask.permitExpiration();
        }
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) RegionAttributes(org.apache.geode.cache.RegionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) AttributesFactory(org.apache.geode.cache.AttributesFactory) Entry(org.apache.geode.cache.Region.Entry) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 8 with EntryEvent

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

the class RegionTestCase method testCloseRegion.

/**
   * Tests closing a region, and checks different behavior when this is a disk region with
   * persistBackup.
   */
@Test
public void testCloseRegion() throws CacheException {
    // @todo added a remote region to make sure close just does a localDestroy
    String name = this.getUniqueName();
    AttributesFactory fac = new AttributesFactory(getRegionAttributes());
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent event) {
        // do nothing
        }

        public void afterRegionDestroy2(RegionEvent re) {
            assertEquals(Operation.REGION_CLOSE, re.getOperation());
        }

        public void close2() {
        // okay
        }
    };
    fac.setCacheListener(list);
    RegionAttributes attrs = fac.create();
    Region region = createRegion(name, attrs);
    File diskDir = null;
    if (attrs.getDataPolicy().withPersistence()) {
        diskDir = getCache().findDiskStore(attrs.getDiskStoreName()).getDiskDirs()[0];
        // @todo We no longer start with a clean slate because the DiskStore hangs around.
        // If we want a clean slate then we need to destroy the DiskStore after each
        // test completes.
        // assert that if this is a disk region, the disk dirs are empty
        // to make sure we start with a clean slate
        getCache().getLogger().info("list=" + Arrays.toString(diskDir.list()));
    // assertIndexDetailsEquals("list="+Arrays.toString(diskDir.list()),
    // 0, diskDir.list().length);
    }
    for (int i = 0; i < 1000; i++) {
        region.put(new Integer(i), String.valueOf(i));
    }
    // reset wasInvoked after creates
    assertTrue(list.wasInvoked());
    // assert that if this is a disk region, the disk dirs are not empty
    if (attrs.getDataPolicy().withPersistence()) {
        assertTrue(diskDir.list().length > 0);
    }
    boolean persistent = region.getAttributes().getDataPolicy().withPersistence();
    region.close();
    // assert that if this is a disk region, the disk dirs are not empty
    if (attrs.getDataPolicy().withPersistence()) {
        assertTrue(diskDir.list().length > 0);
    }
    assertTrue(list.waitForInvocation(333));
    assertTrue(list.isClosed());
    assertTrue(region.isDestroyed());
    // if (persistent) {
    // // remove this when bug #41049 is fixed
    // return;
    // }
    // if this is a disk region, then check to see if recreating the region
    // repopulates with data
    region = createRegion(name, attrs);
    if (attrs.getDataPolicy().withPersistence()) {
        for (int i = 0; i < 1000; i++) {
            Region.Entry entry = region.getEntry(new Integer(i));
            assertNotNull("entry " + i + " not found", entry);
            assertEquals(String.valueOf(i), entry.getValue());
        }
        assertEquals(1000, region.keySet().size());
    } else {
        assertEquals(0, region.keySet().size());
    }
    region.localDestroyRegion();
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionEvent(org.apache.geode.cache.RegionEvent) File(java.io.File) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 9 with EntryEvent

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

the class RegionTestCase method testEntryTtl3.

/**
   * Configure entry expiration with a ttl time. Create an entry and records its scheduled
   * expiration time. Then mutate the region expiration configuration and confirm that the entry's
   * expiration time is rescheduled.
   */
@Test
public void testEntryTtl3() {
    final String name = this.getUniqueName();
    // test no longer waits for this expiration to happen
    // ms
    final int timeout1 = 500 * 1000;
    // ms
    final int timeout2 = 2000 * 1000;
    final String key1 = "KEY1";
    final String value1 = "VALUE1";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire1 = new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
    factory.setEntryTimeToLive(expire1);
    factory.setStatisticsEnabled(true);
    TestCacheListener list = new TestCacheListener() {

        public void afterCreate2(EntryEvent e) {
        }

        public void afterUpdate2(EntryEvent e) {
        }

        public void afterInvalidate2(EntryEvent e) {
            eventCount++;
        }
    };
    eventCount = 0;
    factory.addCacheListener(list);
    RegionAttributes attrs = factory.create();
    LocalRegion region;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = (LocalRegion) createRegion(name, attrs);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
    region.create(key1, value1);
    EntryExpiryTask eet = region.getEntryExpiryTask(key1);
    final long firstExpiryTime = eet.getExpirationTime();
    AttributesMutator mutt = region.getAttributesMutator();
    ExpirationAttributes expire2 = new ExpirationAttributes(timeout2, ExpirationAction.INVALIDATE);
    mutt.setEntryTimeToLive(expire2);
    eet = region.getEntryExpiryTask(key1);
    final long secondExpiryTime = eet.getExpirationTime();
    if ((secondExpiryTime - firstExpiryTime) <= 0) {
        fail("expiration time should have been greater after changing region config from 500 to 2000. firstExpiryTime=" + firstExpiryTime + " secondExpiryTime=" + secondExpiryTime);
    }
    // now set back to be more recent
    mutt = region.getAttributesMutator();
    ExpirationAttributes expire3 = new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
    mutt.setEntryTimeToLive(expire3);
    eet = region.getEntryExpiryTask(key1);
    final long thirdExpiryTime = eet.getExpirationTime();
    assertEquals(firstExpiryTime, thirdExpiryTime);
    // confirm that it still has not expired
    assertEquals(0, eventCount);
    // now set it to a really short time and make sure it expires immediately
    Wait.waitForExpiryClockToChange(region);
    final Region.Entry entry = region.getEntry(key1);
    mutt = region.getAttributesMutator();
    ExpirationAttributes expire4 = new ExpirationAttributes(1, ExpirationAction.INVALIDATE);
    mutt.setEntryTimeToLive(expire4);
    WaitCriterion wc = new WaitCriterion() {

        public boolean done() {
            return fetchEntryValue(entry) == null;
        }

        public String description() {
            return "entry never became invalid";
        }
    };
    Wait.waitForCriterion(wc, 10 * 1000, 10, true);
    WaitCriterion waitForEventCountToBeOne = new WaitCriterion() {

        public boolean done() {
            return eventCount == 1;
        }

        public String description() {
            return "eventCount never became 1";
        }
    };
    Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 10, true);
    eventCount = 0;
}
Also used : EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) RegionAttributes(org.apache.geode.cache.RegionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) Entry(org.apache.geode.cache.Region.Entry) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 10 with EntryEvent

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

the class PartitionedRegionEvictionDUnitTest method testEntryLRUWithLocalDestroy.

@Test
public void testEntryLRUWithLocalDestroy() {
    final Host host = Host.getHost(0);
    final VM vm2 = host.getVM(2);
    final VM vm3 = host.getVM(3);
    final String uniqName = getUniqueName();
    final int redundantCopies = 1;
    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));
            factory.addCacheListener(new VerifiableCacheListener() {

                private long evictionDestroyEvents = 0;

                public void afterDestroy(EntryEvent e) {
                    System.out.println("EEEEEEEEEEEEEE key:" + e.getKey());
                    EntryEventImpl eei = (EntryEventImpl) e;
                    if (Operation.EVICT_DESTROY.equals(eei.getOperation())) {
                        evictionDestroyEvents++;
                    }
                }

                public boolean verify(long expectedEntries) {
                    return expectedEntries == evictionDestroyEvents;
                }
            });
            final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
            assertNotNull(pr);
        }
    };
    vm3.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).setTotalNumBuckets(8).create());
                factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries));
                final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
                assertNotNull(pr);
            } catch (final CacheException ex) {
                Assert.fail("While creating Partitioned region", ex);
            }
        }
    };
    vm2.invoke(create2);
    final SerializableRunnable createBuckets = new SerializableRunnable("Create Buckets") {

        public void run() {
            final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
            assertNotNull(pr);
            for (int counter = 1; counter <= maxEntries + extraEntries; counter++) {
                pr.put(new Integer(counter), new byte[1 * 1024 * 1024]);
            }
        }
    };
    vm3.invoke(createBuckets);
    final SerializableCallable assertBucketAttributesAndEviction = new SerializableCallable("Assert bucket attributes and eviction") {

        public Object call() throws Exception {
            try {
                final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
                assertNotNull(pr);
                long entriesEvicted = 0;
                for (final Iterator i = pr.getDataStore().getAllLocalBuckets().iterator(); i.hasNext(); ) {
                    final Map.Entry entry = (Map.Entry) i.next();
                    final BucketRegion bucketRegion = (BucketRegion) entry.getValue();
                    if (bucketRegion == null) {
                        continue;
                    }
                    assertTrue(bucketRegion.getAttributes().getEvictionAttributes().getAlgorithm().isLRUEntry());
                    assertTrue(bucketRegion.getAttributes().getEvictionAttributes().getAction().isLocalDestroy());
                }
                entriesEvicted = ((AbstractLRURegionMap) pr.entries)._getLruList().stats().getEvictions();
                return new Long(entriesEvicted);
            } finally {
            }
        }
    };
    final Long v2i = (Long) vm2.invoke(assertBucketAttributesAndEviction);
    final Long v3i = (Long) vm3.invoke(assertBucketAttributesAndEviction);
    final int totalEvicted = v2i.intValue() + v3i.intValue();
    assertEquals(2 * extraEntries, totalEvicted);
    final SerializableCallable assertListenerCount = new SerializableCallable("Assert that the number of listener invocations matches the expected total") {

        public Object call() throws Exception {
            final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
            assertNotNull(pr);
            RegionAttributes attrs = pr.getAttributes();
            assertNotNull(attrs);
            long entriesEvicted = ((AbstractLRURegionMap) pr.entries)._getLruList().stats().getEvictions();
            VerifiableCacheListener verifyMe = null;
            for (CacheListener listener : attrs.getCacheListeners()) {
                if (listener instanceof VerifiableCacheListener) {
                    verifyMe = ((VerifiableCacheListener) listener);
                }
            }
            // assert if unable to find the expected listener
            assertNotNull(verifyMe);
            return verifyMe.verify(entriesEvicted);
        }
    };
    assertTrue((Boolean) vm3.invoke(assertListenerCount));
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheListener(org.apache.geode.cache.CacheListener) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) Iterator(java.util.Iterator) 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) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Map(java.util.Map) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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