Search in sources :

Example 1 with ExpiryTask

use of org.apache.geode.internal.cache.ExpiryTask in project geode by apache.

the class RegionTestCase method testRegionIdleInvalidate.

/**
   * Tests that a region that remains idle for a given amount of time is invalidated. Also tests
   * that accessing an entry of a region or a subregion counts as an access.
   */
@Test
public void testRegionIdleInvalidate() throws InterruptedException, CacheException {
    if (getRegionAttributes().getPartitionAttributes() != null) {
        // PR does not support INVALID ExpirationAction
        return;
    }
    final String name = this.getUniqueName();
    final String subname = this.getUniqueName() + "-SUB";
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    vm0.invoke(new CacheSerializableRunnable("testRegionIdleInvalidate") {

        public void run2() throws CacheException {
            TestCacheListener list = new TestCacheListener() {

                private int createCount = 0;

                public void afterInvalidate2(EntryEvent e) {
                    e.getRegion().getCache().getLogger().info("invalidate2 key=" + e.getKey());
                }

                public void afterRegionInvalidate2(RegionEvent e) {
                }

                public void afterUpdate2(EntryEvent e) {
                    // Clear the flag
                    this.wasInvoked();
                }

                public void afterCreate2(EntryEvent e) {
                    this.createCount++;
                    // we only expect one create; all the rest should be updates
                    assertEquals(1, this.createCount);
                    // Clear the flag
                    this.wasInvoked();
                }
            };
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
            factory.setRegionIdleTimeout(expire);
            factory.setStatisticsEnabled(true);
            RegionAttributes subRegAttrs = factory.create();
            factory.setCacheListener(list);
            RegionAttributes attrs = factory.create();
            Region region = null;
            Region sub = null;
            Region.Entry entry = null;
            long tilt;
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            ExpiryTask.suspendExpiration();
            try {
                region = createRegion(name, attrs);
                region.put(key, value);
                tilt = System.currentTimeMillis() + timeout;
                entry = region.getEntry(key);
                assertEquals(value, entry.getValue());
                sub = region.createSubregion(subname, subRegAttrs);
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
                ExpiryTask.permitExpiration();
            }
            waitForInvalidate(entry, tilt, 10);
            assertTrue(list.waitForInvocation(333));
            // The next phase of the test verifies that a get will cause the
            // expiration time to be extended.
            // For this phase we don't worry about actually expiring but just
            // making sure the expiration time gets extended.
            final int EXPIRATION_MS = 9000;
            region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(EXPIRATION_MS, ExpirationAction.INVALIDATE));
            LocalRegion lr = (LocalRegion) region;
            {
                ExpiryTask expiryTask = lr.getRegionIdleExpiryTask();
                region.put(key, value);
                long createExpiry = expiryTask.getExpirationTime();
                long changeTime = Wait.waitForExpiryClockToChange(lr, createExpiry - EXPIRATION_MS);
                region.put(key, "VALUE2");
                long putExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected putBaseExpiry=" + (putExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (putExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected putExpiry=" + putExpiry + " to be > than createExpiry=" + createExpiry, (putExpiry - createExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, putExpiry - EXPIRATION_MS);
                region.get(key);
                long getExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected getBaseExpiry=" + (getExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (getExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected getExpiry=" + getExpiry + " to be > than putExpiry=" + putExpiry, (getExpiry - putExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, getExpiry - EXPIRATION_MS);
                sub.put(key, value);
                long subPutExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected subPutBaseExpiry=" + (subPutExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subPutExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected subPutExpiry=" + subPutExpiry + " to be > than getExpiry=" + getExpiry, (subPutExpiry - getExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, subPutExpiry - EXPIRATION_MS);
                sub.get(key);
                long subGetExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected subGetBaseExpiry=" + (subGetExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subGetExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected subGetExpiry=" + subGetExpiry + " to be > than subPutExpiry=" + subPutExpiry, (subGetExpiry - subPutExpiry) > 0);
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) RegionEvent(org.apache.geode.cache.RegionEvent) Entry(org.apache.geode.cache.Region.Entry) ExpiryTask(org.apache.geode.internal.cache.ExpiryTask) EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) 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 2 with ExpiryTask

use of org.apache.geode.internal.cache.ExpiryTask in project geode by apache.

the class RegionTestCase method testRegionExpirationAfterMutate.

@Test
public void testRegionExpirationAfterMutate() throws CacheException, InterruptedException {
    if (getRegionAttributes().getPartitionAttributes() != null) {
        return;
    }
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    LocalRegion region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = (LocalRegion) createRegion(name, attrs);
        region.create(key, value);
        // Now go from no timeout to a timeout
        Region.Entry entry = region.getEntry(key);
        assertEquals(value, entry.getValue());
        region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(12000, /* ms */
        ExpirationAction.INVALIDATE));
        region.put(key, value);
        long tilt = System.currentTimeMillis();
        ExpiryTask expiryTask = region.getRegionIdleExpiryTask();
        long mediumExpiryTime = expiryTask.getExpirationTime();
        region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(999000, /* ms */
        ExpirationAction.INVALIDATE));
        expiryTask = region.getRegionIdleExpiryTask();
        long hugeExpiryTime = expiryTask.getExpirationTime();
        ExpiryTask.suspendExpiration();
        long shortExpiryTime;
        try {
            region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(20, /* ms */
            ExpirationAction.INVALIDATE));
            expiryTask = region.getRegionIdleExpiryTask();
            shortExpiryTime = expiryTask.getExpirationTime();
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForInvalidate(entry, tilt + 20, 10);
        assertTrue("expected hugeExpiryTime=" + hugeExpiryTime + " to be > than mediumExpiryTime=" + mediumExpiryTime, (hugeExpiryTime - mediumExpiryTime) > 0);
        assertTrue("expected mediumExpiryTime=" + mediumExpiryTime + " to be > than shortExpiryTime=" + shortExpiryTime, (mediumExpiryTime - shortExpiryTime) > 0);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : ExpiryTask(org.apache.geode.internal.cache.ExpiryTask) EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) LocalRegion(org.apache.geode.internal.cache.LocalRegion) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

AttributesFactory (org.apache.geode.cache.AttributesFactory)2 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)2 Region (org.apache.geode.cache.Region)2 Entry (org.apache.geode.cache.Region.Entry)2 RegionAttributes (org.apache.geode.cache.RegionAttributes)2 EntryExpiryTask (org.apache.geode.internal.cache.EntryExpiryTask)2 ExpiryTask (org.apache.geode.internal.cache.ExpiryTask)2 LocalRegion (org.apache.geode.internal.cache.LocalRegion)2 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)2 Test (org.junit.Test)2 CacheException (org.apache.geode.cache.CacheException)1 EntryEvent (org.apache.geode.cache.EntryEvent)1 RegionEvent (org.apache.geode.cache.RegionEvent)1 Host (org.apache.geode.test.dunit.Host)1 VM (org.apache.geode.test.dunit.VM)1