use of org.apache.geode.internal.cache.EntryExpiryTask in project geode by apache.
the class RegionTestCase method testEntryIdleReset.
/**
* Verify that accessing an entry resets its idle time
*
* @throws Exception
*/
@Test
public void testEntryIdleReset() throws Exception {
final String name = this.getUniqueName();
// Test no longer waits for this timeout to expire
// seconds
final int timeout = 90;
final String key = "KEY";
final String value = "VALUE";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
factory.setEntryIdleTimeout(expire);
factory.setStatisticsEnabled(true);
RegionAttributes attrs = factory.create();
LocalRegion region = (LocalRegion) createRegion(name, attrs);
region.create(key, null);
EntryExpiryTask eet = region.getEntryExpiryTask(key);
long createExpiryTime = eet.getExpirationTime();
Wait.waitForExpiryClockToChange(region);
// touch
region.get(key);
assertSame(eet, region.getEntryExpiryTask(key));
long getExpiryTime = eet.getExpirationTime();
if (getExpiryTime - createExpiryTime <= 0L) {
fail("get did not reset the expiration time. createExpiryTime=" + createExpiryTime + " getExpiryTime=" + getExpiryTime);
}
Wait.waitForExpiryClockToChange(region);
// touch
region.put(key, value);
assertSame(eet, region.getEntryExpiryTask(key));
long putExpiryTime = eet.getExpirationTime();
if (putExpiryTime - getExpiryTime <= 0L) {
fail("put did not reset the expiration time. getExpiryTime=" + getExpiryTime + " putExpiryTime=" + putExpiryTime);
}
// TODO other ops that should be validated?
// Now verify operations that do not modify the expiry time
Wait.waitForExpiryClockToChange(region);
// touch
region.invalidate(key);
assertSame(eet, region.getEntryExpiryTask(key));
long invalidateExpiryTime = eet.getExpirationTime();
if (region.getConcurrencyChecksEnabled()) {
if (putExpiryTime - getExpiryTime <= 0L) {
fail("invalidate did not reset the expiration time. putExpiryTime=" + putExpiryTime + " invalidateExpiryTime=" + invalidateExpiryTime);
}
} else {
if (invalidateExpiryTime != putExpiryTime) {
fail("invalidate did reset the expiration time. putExpiryTime=" + putExpiryTime + " invalidateExpiryTime=" + invalidateExpiryTime);
}
}
}
use of org.apache.geode.internal.cache.EntryExpiryTask 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);
}
}
use of org.apache.geode.internal.cache.EntryExpiryTask in project geode by apache.
the class RegionTestCase method testEntryTtl3.
/**
* Configure entry expiration with a ttl 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 testEntryTtl3() {
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.setEntryTimeToLive(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.setEntryTimeToLive(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.setEntryTimeToLive(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.setEntryTimeToLive(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.internal.cache.EntryExpiryTask in project geode by apache.
the class MultiVMRegionTestCase method getEntryExpiryTask.
private static EntryExpiryTask getEntryExpiryTask(Region r, Object key) {
EntryExpiryTask result = null;
try {
LocalRegion lr = (LocalRegion) r;
result = lr.getEntryExpiryTask(key);
} catch (EntryNotFoundException ignore) {
}
return result;
}
use of org.apache.geode.internal.cache.EntryExpiryTask in project geode by apache.
the class MultiVMRegionTestCase method testUpdateResetsIdleTime.
/**
* Tests to makes sure that a distributed update resets the expiration timer.
*/
@Test
public void testUpdateResetsIdleTime() throws Exception {
final String name = this.getUniqueName();
// test no longer waits for this timeout to expire
// seconds
final int timeout = 90;
final Object key = "KEY";
final Object value = "VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(new CacheSerializableRunnable("Create with Idle") {
@Override
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setStatisticsEnabled(true);
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
factory.setEntryIdleTimeout(expire);
LocalRegion region = (LocalRegion) createRegion(name, factory.create());
if (region.getDataPolicy().withPartitioning()) {
// Force all buckets to be created locally so the
// test will know that the create happens in this vm
// and the update (in vm1) is remote.
PartitionRegionHelper.assignBucketsToPartitions(region);
}
region.create(key, null);
EntryExpiryTask eet = region.getEntryExpiryTask(key);
region.create("createExpiryTime", eet.getExpirationTime());
Wait.waitForExpiryClockToChange(region);
}
});
vm1.invoke(new CacheSerializableRunnable("Create Region " + name) {
@Override
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setStatisticsEnabled(true);
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
factory.setEntryIdleTimeout(expire);
if (getRegionAttributes().getPartitionAttributes() != null) {
createRegion(name, factory.create());
} else {
createRegion(name);
}
}
});
vm1.invoke(new CacheSerializableRunnable("Update entry") {
@Override
public void run2() throws CacheException {
final Region r = getRootRegion().getSubregion(name);
assertNotNull(r);
r.put(key, value);
}
});
vm0.invoke(new CacheSerializableRunnable("Verify reset") {
@Override
public void run2() throws CacheException {
final LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
// wait for update to reach us from vm1 (needed if no-ack)
WaitCriterion waitForUpdate = new WaitCriterion() {
@Override
public boolean done() {
return value.equals(region.get(key));
}
@Override
public String description() {
return "never saw update of " + key;
}
};
Wait.waitForCriterion(waitForUpdate, 3000, 10, true);
EntryExpiryTask eet = region.getEntryExpiryTask(key);
long createExpiryTime = (Long) region.get("createExpiryTime");
long updateExpiryTime = eet.getExpirationTime();
if (updateExpiryTime - createExpiryTime <= 0L) {
fail("update did not reset the expiration time. createExpiryTime=" + createExpiryTime + " updateExpiryTime=" + updateExpiryTime);
}
}
});
}
Aggregations