Search in sources :

Example 51 with Entry

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

the class RegionTestCase method testRegionTtlInvalidate.

/**
   * Tests that a region expires with an invalidation after a given time to live.
   */
@Test
public void testRegionTtlInvalidate() throws CacheException, InterruptedException {
    if (getRegionAttributes().getPartitionAttributes() != null)
        return;
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    final String name = this.getUniqueName();
    vm0.invoke(new CacheSerializableRunnable("testRegionTtlInvalidate") {

        public void run2() throws CacheException {
            // ms
            final int timeout = 22;
            final Object key = "KEY";
            final Object value = "VALUE";
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
            factory.setRegionTimeToLive(expire);
            factory.setStatisticsEnabled(true);
            RegionAttributes attrs = factory.create();
            Region region = null;
            Region.Entry entry = null;
            long tilt;
            ExpiryTask.suspendExpiration();
            try {
                System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
                try {
                    region = createRegion(name, attrs);
                    region.put(key, value);
                    region.put("k2", "v2");
                    tilt = System.currentTimeMillis() + timeout;
                    entry = region.getEntry(key);
                    assertNotNull(entry.getValue());
                } finally {
                    System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
                }
            } finally {
                ExpiryTask.permitExpiration();
            }
            waitForInvalidate(entry, tilt, 10);
            waitForInvalidate(region.getEntry("k2"), tilt, 10);
        }
    });
}
Also used : Entry(org.apache.geode.cache.Region.Entry) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 52 with Entry

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

the class RegionTestCase method testInvalidateEntry.

/**
   * Tests invalidating a region entry
   */
@Test
public void testInvalidateEntry() throws CacheException {
    String name = this.getUniqueName();
    Object key = "KEY";
    Object value = "VALUE";
    Region region = createRegion(name);
    region.put(key, value);
    int beforeInvalidates = ((org.apache.geode.internal.cache.GemFireCacheImpl) getCache()).getCachePerfStats().getInvalidates();
    Region.Entry entry = region.getEntry(key);
    region.invalidate(key);
    if (entry.isLocal()) {
        assertNull(entry.getValue());
    }
    assertNull(region.get(key));
    int afterInvalidates = ((org.apache.geode.internal.cache.GemFireCacheImpl) getCache()).getCachePerfStats().getInvalidates();
    assertEquals("Invalidate CachePerfStats incorrect", beforeInvalidates + 1, afterInvalidates);
}
Also used : Entry(org.apache.geode.cache.Region.Entry) 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)

Example 53 with Entry

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

the class RegionTestCase method testLocalDestroyRegion.

/**
   * Tests locally destroying an entire region and that accessing it after it has been destory
   * causes a {@link RegionDestroyedException}.
   *
   * @see Region#localDestroyRegion
   */
@Test
public void testLocalDestroyRegion() throws CacheException {
    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.localDestroyRegion();
    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());
    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)

Example 54 with Entry

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

the class RegionTestCase method testRegionTtlDestroy.

/**
   * Tests that a region expires with a destruction after a given time to live.
   */
@Test
public void testRegionTtlDestroy() throws CacheException, InterruptedException {
    if (getRegionAttributes().getPartitionAttributes() != null)
        return;
    final String name = this.getUniqueName();
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
    factory.setRegionTimeToLive(expire);
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    Region region = null;
    long tilt;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    ExpiryTask.suspendExpiration();
    try {
        try {
            region = createRegion(name, attrs);
            assertFalse(region.isDestroyed());
            tilt = System.currentTimeMillis() + timeout;
            region.put(key, value);
            Region.Entry entry = region.getEntry(key);
            assertNotNull(entry.getValue());
        } finally {
            System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
        }
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForRegionDestroy(region, 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 55 with Entry

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

the class RegionTestCase method testEntryIdleTimeout3.

/**
   * Configure entry expiration with a idle 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 testEntryIdleTimeout3() {
    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.setEntryIdleTimeout(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.setEntryIdleTimeout(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.setEntryIdleTimeout(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.setEntryIdleTimeout(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)

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