use of org.apache.geode.cache.ExpirationAttributes 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;
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionTestCase method testCustomEntryIdleTimeout3.
/**
* Configure custome entry expiration with an 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 testCustomEntryIdleTimeout3() {
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.setCustomEntryIdleTimeout(new TestExpiry(key1, 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.setCustomEntryIdleTimeout(new TestExpiry(key1, 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.setCustomEntryIdleTimeout(new TestExpiry(key1, 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.setCustomEntryIdleTimeout(new TestExpiry(key1, 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;
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionTestCase method testEntryIdleTtl.
/**
* Verify that accessing an entry does not delay expiration due to TTL
*/
@Test
public void testEntryIdleTtl() {
final String name = this.getUniqueName();
// test no longer waits for this timeout to expire
// seconds
final int timeout = 2000;
final String key = "IDLE_TTL_KEY";
final String value = "IDLE_TTL_VALUE";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expireIdle = new ExpirationAttributes(timeout / 2, ExpirationAction.DESTROY);
factory.setEntryIdleTimeout(expireIdle);
ExpirationAttributes expireTtl = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
factory.setEntryTimeToLive(expireTtl);
factory.setStatisticsEnabled(true);
RegionAttributes attrs = factory.create();
LocalRegion region = (LocalRegion) createRegion(name, attrs);
region.create(key, value);
EntryExpiryTask eet = region.getEntryExpiryTask(key);
final long firstIdleExpiryTime = eet.getIdleExpirationTime();
final long firstTTLExpiryTime = eet.getTTLExpirationTime();
if ((firstIdleExpiryTime - firstTTLExpiryTime) >= 0) {
fail("idle should be less than ttl: idle=" + firstIdleExpiryTime + " ttl=" + firstTTLExpiryTime);
}
Wait.waitForExpiryClockToChange(region);
region.get(key);
eet = region.getEntryExpiryTask(key);
final long secondIdleExpiryTime = eet.getIdleExpirationTime();
final long secondTTLExpiryTime = eet.getTTLExpirationTime();
// make sure the get does not change the ttl expiry time
assertEquals(firstTTLExpiryTime, secondTTLExpiryTime);
// and does change the idle expiry time
if ((secondIdleExpiryTime - firstIdleExpiryTime) <= 0) {
fail("idle should have increased: idle=" + firstIdleExpiryTime + " idle2=" + secondIdleExpiryTime);
}
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionTestCase method testCustomEntryTtl2.
/**
* Verify that special entries don't expire but other entries in the region do
*/
@Test
public void testCustomEntryTtl2() {
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());
// factory.setEntryIdleTimeout(expire);
ExpirationAttributes expire2 = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
factory.setCustomEntryTimeToLive(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(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;
entry = region.getEntry(key2);
assertTrue(list.waitForInvocation(5000));
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));
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class LocalRegion method createExpiryTask.
/**
* If custom expiration returns non-null expiration attributes then create a CustomEntryExpiryTask
* for this region and the given entry and return it. Otherwise if the region is configured for
* expiration then create an EntryExpiryTask for this region and the given entry and return it.
* Null is returned if the expiration attributes indicate that expiration is disabled.
*/
private EntryExpiryTask createExpiryTask(RegionEntry regionEntry) {
if (regionEntry == null || regionEntry.isDestroyedOrRemoved()) {
return null;
}
if (this.customEntryIdleTimeout != null || this.customEntryTimeToLive != null) {
ExpiryRegionEntry expiryRegionEntry = new ExpiryRegionEntry(this, regionEntry);
ExpirationAttributes ttlAttributes = null;
ExpirationAttributes idleAttributes = null;
final RegionAttributes<?, ?> regionAttributes = this.getAttributes();
final CustomExpiry<?, ?> customTTL = regionAttributes.getCustomEntryTimeToLive();
if (customTTL != null) {
try {
ttlAttributes = customTTL.getExpiry(expiryRegionEntry);
if (ttlAttributes != null) {
this.checkEntryTimeoutAction("timeToLive", ttlAttributes.getAction());
}
} catch (RegionDestroyedException ignore) {
// Ignore - #42273
} catch (EntryNotFoundException ignore) {
// Ignore - #51933
} catch (EntryDestroyedException ignore) {
// Ignore - #51933
} catch (Exception e) {
logger.fatal(LocalizedMessage.create(LocalizedStrings.EntryExpiryTask_ERROR_CALCULATING_EXPIRATION_0, e.getMessage()), e);
}
}
if (ttlAttributes == null) {
ttlAttributes = regionAttributes.getEntryTimeToLive();
}
CustomExpiry<?, ?> customIdle = regionAttributes.getCustomEntryIdleTimeout();
if (customIdle != null) {
try {
idleAttributes = customIdle.getExpiry(expiryRegionEntry);
if (idleAttributes != null) {
this.checkEntryTimeoutAction("idleTimeout", idleAttributes.getAction());
}
} catch (RegionDestroyedException ignore) {
// Ignore - #42273
} catch (EntryNotFoundException ignore) {
// Ignore - #51933
} catch (EntryDestroyedException ignore) {
// Ignore - #51933
} catch (Exception e) {
logger.fatal(LocalizedMessage.create(LocalizedStrings.EntryExpiryTask_ERROR_CALCULATING_EXPIRATION_0, e.getMessage()), e);
}
}
if (idleAttributes == null) {
idleAttributes = regionAttributes.getEntryIdleTimeout();
}
final boolean ttlDisabled = ttlAttributes == null || ttlAttributes.getTimeout() == 0;
final boolean idleDisabled = idleAttributes == null || idleAttributes.getTimeout() == 0;
if (ttlDisabled && idleDisabled) {
return null;
} else if ((ttlDisabled || ttlAttributes.equals(regionAttributes.getEntryTimeToLive())) && (idleDisabled || idleAttributes.equals(regionAttributes.getEntryIdleTimeout()))) {
// no need for custom since we can just use the region's expiration attributes.
return new EntryExpiryTask(this, regionEntry);
} else {
return new CustomEntryExpiryTask(this, regionEntry, ttlAttributes, idleAttributes);
}
} else if (isEntryExpiryPossible()) {
return new EntryExpiryTask(this, regionEntry);
} else {
return null;
}
}
Aggregations