use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class MultiVMRegionTestCase method testNetSearchObservesTtl.
@Test
public void testNetSearchObservesTtl() throws Exception {
assumeTrue(getRegionAttributes().getPartitionAttributes() == null);
final String name = this.getUniqueName();
// ms
final int shortTimeout = 10;
// ms
final int longTimeout = 1000000;
final Object key = "KEY";
final Object value = "VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
// Other VM is from a different gemfire system
VM vm1 = host.getVM(2);
SerializableRunnable create = new CacheSerializableRunnable("Create with TTL") {
@Override
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setStatisticsEnabled(true);
ExpirationAttributes expire = new ExpirationAttributes(longTimeout, ExpirationAction.DESTROY);
factory.setEntryTimeToLive(expire);
Region region = null;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = createRegion(name, factory.create());
region.create(key, value);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
};
vm1.invoke(create);
// vm0 - Create region, short timeout
vm0.invoke(new CacheSerializableRunnable("Create with TTL") {
@Override
public void run2() throws CacheException {
RegionAttributes ra = getRegionAttributes();
AttributesFactory factory = new AttributesFactory(ra);
final boolean partitioned = ra.getPartitionAttributes() != null || ra.getDataPolicy().withPartitioning();
// MUST be nonmirrored, so turn off persistBackup if this is a disk region test
if (!partitioned) {
if (ra.getEvictionAttributes() == null || !ra.getEvictionAttributes().getAction().isOverflowToDisk()) {
factory.setDiskStoreName(null);
}
factory.setDataPolicy(DataPolicy.NORMAL);
}
factory.setStatisticsEnabled(true);
ExpirationAttributes expire = new ExpirationAttributes(shortTimeout, ExpirationAction.DESTROY);
factory.setEntryTimeToLive(expire);
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
createRegion(name, factory.create());
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
});
Wait.pause(shortTimeout * 10);
// Even though netSearch finds vm1's entry is not expired, it is considered
// expired with respect to vm0's attributes
vm0.invoke(new CacheSerializableRunnable("get(key), expect null") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
Object got = region.get(key);
assertNull(got);
}
});
// We see the object is actually still there
vm1.invoke(new CacheSerializableRunnable("get(key), expect value") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
Object got = region.get(key);
assertEquals(value, got);
}
});
}
use of org.apache.geode.cache.ExpirationAttributes 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);
}
}
});
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class MultiVMRegionTestCase method testEntryTtlDestroyEvent.
/**
* Tests that an entry in a distributed region that expires with a distributed destroy causes an
* event in other VM with isExpiration flag set.
*/
// GEODE-583: time sensitive, expiration, waitForCriterion, short
@Category(FlakyTest.class)
// timeouts
@Test
public void testEntryTtlDestroyEvent() throws Exception {
assumeTrue(getRegionAttributes().getPartitionAttributes() == null);
final String name = this.getUniqueName();
// ms
final int timeout = 22;
final Object key = "KEY";
final Object value = "VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
class DestroyListener extends TestCacheListener {
boolean eventIsExpiration = false;
@Override
public void afterDestroyBeforeAddEvent(EntryEvent event) {
eventIsExpiration = event.getOperation().isExpiration();
}
@Override
public void afterDestroy2(EntryEvent event) {
if (event.isOriginRemote()) {
assertTrue(!event.getDistributedMember().equals(getSystem().getDistributedMember()));
} else {
assertEquals(getSystem().getDistributedMember(), event.getDistributedMember());
}
assertEquals(Operation.EXPIRE_DESTROY, event.getOperation());
assertEquals(value, event.getOldValue());
eventIsExpiration = event.getOperation().isExpiration();
}
@Override
public void afterCreate2(EntryEvent event) {
// ignore
}
@Override
public void afterUpdate2(EntryEvent event) {
// ignore
}
}
SerializableRunnable createRegion = new CacheSerializableRunnable("Create with Listener") {
@Override
public void run2() throws CacheException {
AttributesFactory fac = new AttributesFactory(getRegionAttributes());
fac.addCacheListener(destroyListener = new DestroyListener());
createRegion(name, fac.create());
}
};
vm1.invoke(createRegion);
vm0.invoke(new CacheSerializableRunnable("Create with TTL") {
@Override
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setStatisticsEnabled(true);
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
factory.setEntryTimeToLive(expire);
if (!getRegionAttributes().getDataPolicy().withReplication()) {
factory.setDataPolicy(DataPolicy.NORMAL);
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
}
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
createRegion(name, factory.create());
ExpiryTask.suspendExpiration();
// suspend to make sure we can see that the put is distributed to this member
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
});
try {
// let region create finish before doing put
// pause(10);
vm1.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region region = getRootRegion().getSubregion(name);
DestroyListener dl = (DestroyListener) region.getAttributes().getCacheListeners()[0];
dl.enableEventHistory();
region.put(key, value);
// reset listener after create event
assertTrue(dl.wasInvoked());
List<CacheEvent> history = dl.getEventHistory();
CacheEvent ce = history.get(0);
dl.disableEventHistory();
assertEquals(Operation.CREATE, ce.getOperation());
return null;
}
});
vm0.invoke(new CacheSerializableRunnable("Check create received from vm1") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
WaitCriterion waitForCreate = new WaitCriterion() {
@Override
public boolean done() {
return region.getEntry(key) != null;
}
@Override
public String description() {
return "never saw create of " + key;
}
};
Wait.waitForCriterion(waitForCreate, 3000, 10, true);
}
});
} finally {
vm0.invoke(new CacheSerializableRunnable("resume expiration") {
@Override
public void run2() throws CacheException {
ExpiryTask.permitExpiration();
}
});
}
// now wait for it to expire
vm0.invoke(new CacheSerializableRunnable("Check local destroy") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
WaitCriterion waitForExpire = new WaitCriterion() {
@Override
public boolean done() {
return region.getEntry(key) == null;
}
@Override
public String description() {
return "never saw expire of " + key + " entry=" + region.getEntry(key);
}
};
Wait.waitForCriterion(waitForExpire, 4000, 10, true);
}
});
vm1.invoke(new CacheSerializableRunnable("Verify destroyed and event") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
WaitCriterion waitForExpire = new WaitCriterion() {
@Override
public boolean done() {
return region.getEntry(key) == null;
}
@Override
public String description() {
return "never saw expire of " + key + " entry=" + region.getEntry(key);
}
};
Wait.waitForCriterion(waitForExpire, 4000, 10, true);
assertTrue(destroyListener.waitForInvocation(555));
assertTrue(((DestroyListener) destroyListener).eventIsExpiration);
}
});
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class HARegionJUnitTest method createHARegion.
/**
* create the HARegion
*/
private Region createHARegion() throws TimeoutException, CacheWriterException, GatewayException, CacheExistsException, RegionExistsException, IOException, ClassNotFoundException {
AttributesFactory factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setScope(Scope.DISTRIBUTED_ACK);
ExpirationAttributes ea = new ExpirationAttributes(2000, ExpirationAction.LOCAL_INVALIDATE);
factory.setStatisticsEnabled(true);
;
factory.setCacheListener(new CacheListenerAdapter() {
@Override
public void afterInvalidate(EntryEvent event) {
}
});
RegionAttributes ra = factory.create();
Region region = HARegion.getInstance("HARegionJUnitTest_region", (GemFireCacheImpl) cache, null, ra);
region.getAttributesMutator().setEntryTimeToLive(ea);
return region;
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class PartitionedRegion method setEntryIdleTimeout.
/**
* Changes the idleTimeout expiration attributes for values in the region.
*
* @param idleTimeout the idleTimeout expiration attributes for entries
* @return the previous value of entry idleTimeout
* @throws IllegalArgumentException if idleTimeout is null or if the ExpirationAction is
* LOCAL_DESTROY and the region is {@link DataPolicy#withReplication replicated} or if the
* the ExpirationAction is LOCAL_INVALIDATE and the region is
* {@link DataPolicy#withReplication replicated}
* @see AttributesFactory#setStatisticsEnabled
* @throws IllegalStateException if statistics are disabled for this region.
*/
@Override
public ExpirationAttributes setEntryIdleTimeout(ExpirationAttributes idleTimeout) {
ExpirationAttributes attr = super.setEntryIdleTimeout(idleTimeout);
// Set to Bucket regions as well
if (this.getDataStore() != null) {
// not for accessors
for (Object o : this.getDataStore().getAllLocalBuckets()) {
Map.Entry entry = (Map.Entry) o;
Region bucketRegion = (Region) entry.getValue();
bucketRegion.getAttributesMutator().setEntryIdleTimeout(idleTimeout);
}
}
updatePRConfig(getPRConfigWithLatestExpirationAttributes(), false);
return attr;
}
Aggregations