Search in sources :

Example 11 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testEntryTtlInvalidate.

/**
   * Tests that an entry in a region expires with an invalidation after a given time to live.
   */
@Test
public void testEntryTtlInvalidate() throws CacheException {
    final String name = this.getUniqueName();
    // ms!
    final int timeout = 20;
    final String key = "KEY";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    factory.setEntryTimeToLive(expire);
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    Region region = null;
    /**
     * Crank up the expiration so test runs faster. This property only needs to be set while the
     * region is created
     */
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
    ExpiryTask.suspendExpiration();
    Region.Entry entry = null;
    long tilt;
    try {
        region.put(key, value);
        tilt = System.currentTimeMillis() + timeout;
        entry = region.getEntry(key);
        assertNotNull(entry.getValue());
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForInvalidate(entry, tilt);
}
Also used : 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) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 12 with Entry

use of org.apache.geode.cache.Region.Entry 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 13 with Entry

use of org.apache.geode.cache.Region.Entry 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 14 with Entry

use of org.apache.geode.cache.Region.Entry 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 15 with Entry

use of org.apache.geode.cache.Region.Entry in project geode by apache.

the class RegionTestCase method testDestroyRegion.

/**
   * Tests destroying an entire region and that accessing it after it has been destory causes a
   * {@link RegionDestroyedException}.
   *
   * @see Region#destroyRegion
   */
@Test
public void testDestroyRegion() throws CacheException {
    if (!supportsSubregions()) {
        return;
    }
    String name = this.getUniqueName();
    Object key = "KEY";
    Object value = "VALUE";
    Region region = createRegion(name);
    region.put(key, value);
    Region.Entry entry = region.getEntry(key);
    assertNotNull(entry);
    region.createSubregion("SUB", region.getAttributes());
    region.destroyRegion();
    assertTrue(entry.isDestroyed());
    assertTrue(region.isDestroyed());
    try {
        region.containsKey(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.containsValueForKey(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.create(key, value);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.create(key, value, "BLAH");
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.createSubregion("SUB", this.getRegionAttributes());
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.destroy(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.destroy(key, "BLAH");
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.destroyRegion();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.destroyRegion("ARG");
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.entrySet(false);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.get(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.get(key, "ARG");
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.containsKey(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    region.getAttributes();
    try {
        region.getAttributesMutator();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.containsKey(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.getCache();
    } catch (RegionDestroyedException ex) {
        fail("getCache() shouldn't have thrown a RegionDestroyedException");
    }
    try {
        region.getEntry(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.getDistributedLock(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    assertEquals(name, region.getName());
    region.getParentRegion();
    assertEquals("/root/" + name, region.getFullPath());
    assertEquals(name, region.getName());
    try {
        region.getRegionDistributedLock();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.getStatistics();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.getSubregion("SUB");
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    region.getUserAttribute();
    try {
        region.invalidate(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.invalidateRegion();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.keySet();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.localDestroy(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.localDestroyRegion();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.localInvalidate(key);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.localInvalidateRegion();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.put(key, value);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.put(key, value, "ARG");
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.setUserAttribute("ATTR");
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.subregions(true);
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
    try {
        region.values();
        fail("Should have thrown a RegionDestroyedException");
    } catch (RegionDestroyedException ex) {
    // pass..
    }
}
Also used : Entry(org.apache.geode.cache.Region.Entry) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

Entry (org.apache.geode.cache.Region.Entry)67 Test (org.junit.Test)52 Region (org.apache.geode.cache.Region)51 LocalRegion (org.apache.geode.internal.cache.LocalRegion)37 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)37 AttributesFactory (org.apache.geode.cache.AttributesFactory)22 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)21 RegionAttributes (org.apache.geode.cache.RegionAttributes)21 VM (org.apache.geode.test.dunit.VM)13 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)13 EntryEvent (org.apache.geode.cache.EntryEvent)12 NonTXEntry (org.apache.geode.internal.cache.LocalRegion.NonTXEntry)12 VersionTag (org.apache.geode.internal.cache.versions.VersionTag)12 Host (org.apache.geode.test.dunit.Host)11 VersionSource (org.apache.geode.internal.cache.versions.VersionSource)10 VersionStamp (org.apache.geode.internal.cache.versions.VersionStamp)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 Iterator (java.util.Iterator)7