use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionTestCase method testCustomIdleOnce.
/**
* Verify that expiry is calculatod only once on an entry
*/
@Test
public void testCustomIdleOnce() {
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.setEntryTimeToLive(expire);
factory.setCustomEntryTimeToLive(new CountExpiry(key2, expire));
factory.setStatisticsEnabled(true);
RegionAttributes attrs = factory.create();
synchronized (CountExpiry.class) {
CountExpiry.invokeCounts.clear();
}
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 {
if (region.getAttributes().getPartitionAttributes() == null)
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
// Random values should not expire
region.put(key1, value);
Wait.pause(timeout * 2);
assert (region.get(key1).equals(value));
// key2 *should* expire
ExpiryTask.suspendExpiration();
Region.Entry entry = null;
long tilt;
try {
region.put(key2, value);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key2);
assertNotNull(entry.getValue());
} finally {
ExpiryTask.permitExpiration();
if (region.getAttributes().getPartitionAttributes() != null)
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
waitForInvalidate(entry, tilt);
assert (region.get(key1).equals(value));
synchronized (CountExpiry.class) {
if (CountExpiry.invokeCounts.size() != 2) {
fail("CountExpiry not invoked correctly, size = " + CountExpiry.invokeCounts.size());
}
Integer i = (Integer) CountExpiry.invokeCounts.get(key1);
assertNotNull(i);
assertEquals(1, i.intValue());
i = (Integer) CountExpiry.invokeCounts.get(key2);
assertNotNull(i);
assertEquals(1, i.intValue());
}
// synchronized
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionTestCase method testEntryFromLoadTtlInvalidate.
// /**
// * Expire an entry with a ttl time. Set a new ttl time, create the
// * same entry again, make sure it observes the <em>new</em> ttl time.
// * Do this many times.
// */
// public void testEntryTtl4() {
// if (!supportsExpiration()) {
// return;
// }
// final String name = this.getUniqueName();
// final int timeout1 = 200; // ms
// final int timeout2 = 2000;
// final String key1 = "KEY1";
// final String value1 = "VALUE1";
// final String value2 = "VALUE2";
//
// 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 afterDestroy2(EntryEvent e) { }
// public void afterInvalidate2(EntryEvent e) { eventCount ++; }
// };
// eventCount = 0;
// 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);
// }
//
// for (int i = 0; i < 10; i ++) {
// // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
// ExpiryTask.suspendExpiration();
// Region.Entry entry = null;
// long tilt;
// try {
// region.create(key1, value1);
// tilt = System.currentTimeMillis() + timeout1;
// assertTrue(list.waitForInvocation(1000));
// entry = region.getEntry(key1);
// Assert.assertTrue(value1.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// // Do it again with a put (I guess)
// ExpiryTask.suspendExpiration();
// try {
// region.put(key1, value1);
// tilt = System.currentTimeMillis() + timeout1;
// entry = region.getEntry(key1);
// Assert.assertTrue(value1.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// // Change custom expiry for this region now...
// AttributesMutator mutt = region.getAttributesMutator();
// ExpirationAttributes expire2 =
// new ExpirationAttributes(timeout2, ExpirationAction.INVALIDATE);
// mutt.setEntryTimeToLive(expire2);
//
// ExpiryTask.suspendExpiration();
// try {
// region.put(key1, value2);
// tilt = System.currentTimeMillis() + timeout2;
// entry = region.getEntry(key1);
// Assert.assertTrue(value2.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// region.destroy(key1);
// }
// }
/**
* Tests that an entry whose value is loaded into a region expires with an invalidation after a
* given time to live.
*/
@Test
public void testEntryFromLoadTtlInvalidate() throws CacheException, InterruptedException {
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);
factory.setCacheLoader(new TestCacheLoader() {
public Object load2(LoaderHelper helper) {
return value;
}
});
RegionAttributes attrs = factory.create();
Region region = null;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = createRegion(name, attrs);
// DebuggerSupport.waitForJavaDebugger(getLogWriter(), "About to get");
ExpiryTask.suspendExpiration();
Region.Entry entry = null;
long tilt;
try {
region.get(key);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key);
assertNotNull(entry.getValue());
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionTestCase method testCustomEntryTtl3.
/**
* Expire an entry with a custom expiration. Set a new custom expiration, create the same entry
* again, make sure it observes the <em>new</em> expiration
*/
@Test
public void testCustomEntryTtl3() {
final String name = this.getUniqueName();
// ms
final int timeout1 = 20;
final int timeout2 = 40;
final String key1 = "KEY1";
final String value1 = "VALUE1";
final String value2 = "VALUE2";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire1 = new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
// factory.setEntryIdleTimeout(expire);
factory.setCustomEntryTimeToLive(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++;
}
};
// Disk regions are VERY slow, so we need to wait for the event...
WaitCriterion waitForEventCountToBeOne = new WaitCriterion() {
public boolean done() {
return eventCount == 1;
}
public String description() {
return "eventCount never became 1";
}
};
eventCount = 0;
factory.addCacheListener(list);
RegionAttributes attrs = factory.create();
Region region = null;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = createRegion(name, attrs);
// DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in
// invalidate");
ExpiryTask.suspendExpiration();
Region.Entry entry = null;
eventCount = 0;
long tilt1;
long tilt2;
try {
region.create(key1, value1);
tilt1 = System.currentTimeMillis() + timeout1;
entry = region.getEntry(key1);
assertTrue(list.waitForInvocation(1000));
Assert.assertTrue(value1.equals(entry.getValue()));
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt1, timeout1 / 2);
Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 100, true);
eventCount = 0;
// Do it again with a put (I guess)
ExpiryTask.suspendExpiration();
try {
region.put(key1, value1);
tilt1 = System.currentTimeMillis() + timeout1;
entry = region.getEntry(key1);
Assert.assertTrue(value1.equals(entry.getValue()));
assertTrue(list.waitForInvocation(10 * 1000));
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt1, timeout1 / 2);
Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 100, true);
eventCount = 0;
// Change custom expiry for this region now...
final String key2 = "KEY2";
AttributesMutator mutt = region.getAttributesMutator();
ExpirationAttributes expire2 = new ExpirationAttributes(timeout2, ExpirationAction.INVALIDATE);
mutt.setCustomEntryTimeToLive(new TestExpiry(key2, expire2));
ExpiryTask.suspendExpiration();
try {
region.put(key1, value1);
region.put(key2, value2);
tilt1 = System.currentTimeMillis() + timeout1;
tilt2 = tilt1 + timeout2 - timeout1;
entry = region.getEntry(key1);
Assert.assertTrue(value1.equals(entry.getValue()));
entry = region.getEntry(key2);
Assert.assertTrue(value2.equals(entry.getValue()));
assertTrue(list.waitForInvocation(1000));
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt2, timeout2 / 2);
Wait.waitForCriterion(waitForEventCountToBeOne, 10 * 1000, 100, true);
eventCount = 0;
// key1 should not be invalidated since we mutated to custom expiry to only expire key2
entry = region.getEntry(key1);
Assert.assertTrue(value1.equals(entry.getValue()));
// now mutate back to key1 and change the action
ExpirationAttributes expire3 = new ExpirationAttributes(timeout1, ExpirationAction.DESTROY);
mutt.setCustomEntryTimeToLive(new TestExpiry(key1, expire3));
waitForDestroy(entry, tilt1, timeout1 / 2);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class QueryTestUtils method createPartitionRegion.
public void createPartitionRegion(String name, Class constraint) {
ExpirationAttributes expiration = ExpirationAttributes.DEFAULT;
PartitionAttributesFactory paf = new PartitionAttributesFactory();
RegionFactory factory = cache.createRegionFactory(RegionShortcut.PARTITION).setPartitionAttributes(paf.create());
if (constraint != null) {
factory.setValueConstraint(constraint);
}
factory.create(name);
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionTestCase method testRegionExpirationAfterMutate.
@Test
public void testRegionExpirationAfterMutate() throws CacheException, InterruptedException {
if (getRegionAttributes().getPartitionAttributes() != null) {
return;
}
final String name = this.getUniqueName();
final Object key = "KEY";
final Object value = "VALUE";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setStatisticsEnabled(true);
RegionAttributes attrs = factory.create();
LocalRegion region = null;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = (LocalRegion) createRegion(name, attrs);
region.create(key, value);
// Now go from no timeout to a timeout
Region.Entry entry = region.getEntry(key);
assertEquals(value, entry.getValue());
region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(12000, /* ms */
ExpirationAction.INVALIDATE));
region.put(key, value);
long tilt = System.currentTimeMillis();
ExpiryTask expiryTask = region.getRegionIdleExpiryTask();
long mediumExpiryTime = expiryTask.getExpirationTime();
region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(999000, /* ms */
ExpirationAction.INVALIDATE));
expiryTask = region.getRegionIdleExpiryTask();
long hugeExpiryTime = expiryTask.getExpirationTime();
ExpiryTask.suspendExpiration();
long shortExpiryTime;
try {
region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(20, /* ms */
ExpirationAction.INVALIDATE));
expiryTask = region.getRegionIdleExpiryTask();
shortExpiryTime = expiryTask.getExpirationTime();
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt + 20, 10);
assertTrue("expected hugeExpiryTime=" + hugeExpiryTime + " to be > than mediumExpiryTime=" + mediumExpiryTime, (hugeExpiryTime - mediumExpiryTime) > 0);
assertTrue("expected mediumExpiryTime=" + mediumExpiryTime + " to be > than shortExpiryTime=" + shortExpiryTime, (mediumExpiryTime - shortExpiryTime) > 0);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
Aggregations