Search in sources :

Example 61 with ExpirationAttributes

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

the class RegionExpirationDUnitTest method testWhenBothTtlAndIdleAreSet.

@Test
public void testWhenBothTtlAndIdleAreSet() throws InterruptedException, CacheException {
    String regionName = getUniqueName();
    int ttlTimeout = 4;
    int idleTimeout = 3;
    createWithExpiration(regionName, new ExpirationAttributes(ttlTimeout, ExpirationAction.DESTROY), new ExpirationAttributes(idleTimeout, ExpirationAction.INVALIDATE));
    Region region = getOrCreateRootRegion().getSubregion(regionName);
    region.create("key", "val");
    Thread.sleep(idleTimeout * 1000 + 200);
    assertFalse(region.isDestroyed());
    // Touch the lastAccessedTime
    assertNull(region.get("key"));
    // Next action that occurs should be ttl - the destroy
    Thread.sleep(((ttlTimeout - idleTimeout) * 1000) + 5000);
    assertTrue(region.isDestroyed());
}
Also used : Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 62 with ExpirationAttributes

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

the class RegionExpirationDUnitTest method _testRegionTTL.

/////////////// Utility methods ///////////////////////////
private void _testRegionTTL(final String regionName, final ExpirationAction action) throws InterruptedException {
    final int timeoutSecs = 10;
    final Object key = "key";
    final Object originalValue = "original value";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    LogWriterUtils.getLogWriter().info("vm0 is " + vm0.getPid() + ", vm1 is " + vm1);
    LogWriterUtils.getLogWriter().info("2: " + regionName + " action is " + action);
    final long tilt = System.currentTimeMillis() + timeoutSecs * 1000;
    // In vm0, create the region with ttl, and put value
    vm0.invoke(new CacheSerializableRunnable("Create Region & Key") {

        public void run2() throws CacheException {
            createWithExpiration(regionName, new ExpirationAttributes(timeoutSecs, action), null);
            Region region = getOrCreateRootRegion().getSubregion(regionName);
            region.put(key, originalValue);
            assertEquals(originalValue, region.get(key));
            assertTrue(region.containsValueForKey(key));
        }
    });
    // In vm1, create the region - no ttl
    vm1.invoke(new CacheSerializableRunnable("Create Region & Key") {

        public void run2() throws CacheException {
            createWithExpiration(regionName, null, null);
        }
    });
    // In vm1, do get() to cause netsearch
    vm1.invoke(new CacheSerializableRunnable("Get") {

        public void run2() throws CacheException {
            Region region = getOrCreateRootRegion().getSubregion(regionName);
            Object newVal = region.get(key);
            if (!originalValue.equals(newVal)) {
                assertTrue("expected original value but got " + newVal, System.currentTimeMillis() + 1000 >= tilt);
            }
            if (!region.containsValueForKey(key)) {
                assertTrue("Region doesn't hold key", System.currentTimeMillis() + 1000 >= tilt);
            }
        }
    });
    // Wait for expiration to occur
    Thread.sleep(timeoutSecs * 1000 + 2000);
    // In vm0, region should be absent (for destroy, localDestroy), or
    // entry invalid
    vm0.invoke(new CacheSerializableRunnable("Get") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            LogWriterUtils.getLogWriter().info("3: " + regionName + ", " + region + ", action is " + action);
            if (action.isInvalidate() || action.isLocalInvalidate()) {
                assertTrue(!region.containsValueForKey(key));
            } else {
                assertTrue(region == null);
            }
        }
    });
    // In vm1, region should be absent (for destroy), or entry invalid (invalidate).
    // For LOCAL_DESTROY or LOCAL_INVALIDATE, the value should be intact
    vm1.invoke(new CacheSerializableRunnable("Get") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            if (action.isInvalidate()) {
                assertTrue(region.containsKey(key));
                assertTrue(!region.containsValueForKey(key));
            } else if (action.isDestroy()) {
                assertTrue(region == null);
            } else {
                assertTrue(region.containsValueForKey(key));
                assertEquals(originalValue, region.get(key));
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes)

Example 63 with ExpirationAttributes

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

the class RegionReliabilityTestCase method testFullAccessWithLocalEntryExpiration.

/**
   * Tests affect of FULL_ACCESS on local entry expiration actions.
   */
// GEODE-447: time sensitive, expiration, waitForMemberTimeout is
@Category(FlakyTest.class)
// unimplemented
@Test
public void testFullAccessWithLocalEntryExpiration() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    // assign names to 4 vms...
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    final Region region = createExpiryRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    // test to make sure expiration is not suspended
    region.put("expireMe", "expireMe");
    assertTrue(region.size() == 1);
    // set expiration and sleep
    AttributesMutator mutator = region.getAttributesMutator();
    mutator.setEntryTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
    waitForEntryDestroy(region, "expireMe");
    assertTrue(region.size() == 0);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) Category(org.junit.experimental.categories.Category) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 64 with ExpirationAttributes

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

the class RegionReliabilityTestCase method testLimitedAccessWithLocalRegionExpiration.

/**
   * Tests affect of LIMITED_ACCESS on local region expiration actions.
   */
@Test
public void testLimitedAccessWithLocalRegionExpiration() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    // assign names to 4 vms...
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.LIMITED_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    final Region region = createExpiryRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    AttributesMutator mutator = region.getAttributesMutator();
    mutator.setRegionTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
    waitForRegionDestroy(region);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 65 with ExpirationAttributes

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

the class RegionTestCase method testCustomEntryIdleTimeout1.

/**
   * Verify that special entries expire but other entries in the region don't
   */
@Test
public void testCustomEntryIdleTimeout1() {
    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);
    factory.setCustomEntryIdleTimeout(new TestExpiry(key2, 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();
    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(key1, value);
    // This value should NOT expire.
    Wait.pause(timeout * 2);
    assertTrue(region.get(key1).equals(value));
    // This value SHOULD expire
    // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
    ExpiryTask.suspendExpiration();
    Region.Entry entry = null;
    long tilt;
    try {
        region.create(key2, value);
        tilt = System.currentTimeMillis() + timeout;
        assertTrue(list.waitForInvocation(5000));
        entry = region.getEntry(key2);
        assertNotNull(entry.getValue());
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForInvalidate(entry, tilt);
    // First value should still be in there
    assertTrue(region.get(key1).equals(value));
    // Do it again with a put (I guess)
    ExpiryTask.suspendExpiration();
    try {
        region.put(key2, value);
        tilt = System.currentTimeMillis() + timeout;
        entry = region.getEntry(key2);
        assertNotNull(entry.getValue());
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForInvalidate(entry, tilt);
    // First value should still be in there
    assertTrue(region.get(key1).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)

Aggregations

ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)84 Region (org.apache.geode.cache.Region)51 AttributesFactory (org.apache.geode.cache.AttributesFactory)50 Test (org.junit.Test)50 RegionAttributes (org.apache.geode.cache.RegionAttributes)41 LocalRegion (org.apache.geode.internal.cache.LocalRegion)41 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)35 Entry (org.apache.geode.cache.Region.Entry)21 EntryEvent (org.apache.geode.cache.EntryEvent)15 CacheException (org.apache.geode.cache.CacheException)12 AttributesMutator (org.apache.geode.cache.AttributesMutator)11 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)11 Host (org.apache.geode.test.dunit.Host)11 VM (org.apache.geode.test.dunit.VM)11 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)11 Properties (java.util.Properties)10 EntryExpiryTask (org.apache.geode.internal.cache.EntryExpiryTask)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 Cache (org.apache.geode.cache.Cache)8 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)8