use of org.apache.geode.cache.ExpirationAttributes 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.cache.ExpirationAttributes in project geode by apache.
the class SystemFailureDUnitTest method createEntry.
/**
* Create a region with one entry in this test's region with the given name and attributes.
*/
private static void createEntry(String name, int ttl, ExpirationAction action, GenericListener l) throws CacheException {
Region region = getRegion();
AttributesFactory factory = new AttributesFactory(region.getAttributes());
factory.setStatisticsEnabled(true);
factory.setEntryTimeToLive(new ExpirationAttributes(ttl, action));
factory.setScope(SCOPE);
factory.setCacheListener(l);
Region sub = region.createSubregion(name, factory.create());
sub.create(name, new Integer(0), sub.getCache().getDistributedSystem().getDistributedMember());
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class ClientServerCCEDUnitTest method testClientDoesNotExpireEntryPrematurely.
@Test
public void testClientDoesNotExpireEntryPrematurely() throws Exception {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String name = this.getUniqueName() + "Region";
final String key = "testKey";
int port = createServerRegion(vm0, name, true);
vm0.invoke(new SerializableCallable("create old entry") {
public Object call() throws Exception {
LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
r.put(key, "value");
AbstractRegionEntry entry = (AbstractRegionEntry) r.basicGetEntry(key);
// set an old timestamp in the entry - thirty minutes ago
entry.getVersionStamp().setVersionTimeStamp(System.currentTimeMillis() - 1800000L);
return null;
}
});
createClientRegion(vm1, name, port, true, ClientRegionShortcut.CACHING_PROXY, false);
vm1.invoke(new SerializableCallable("fetch entry and validate") {
public Object call() throws Exception {
final Long[] expirationTimeMillis = new Long[1];
int expirationSeconds = 15;
LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
AttributesMutator mutator = r.getAttributesMutator();
mutator.setEntryIdleTimeout(new ExpirationAttributes(expirationSeconds, ExpirationAction.LOCAL_DESTROY));
mutator.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterDestroy(EntryEvent event) {
expirationTimeMillis[0] = System.currentTimeMillis();
}
});
// fetch the entry from the server and make sure it doesn't expire early
if (!r.containsKey(key)) {
r.get(key);
}
final long expirationTime = System.currentTimeMillis() + (expirationSeconds * 1000);
Awaitility.await("waiting for object to expire").atMost(expirationSeconds * 2, TimeUnit.SECONDS).until(() -> {
return expirationTimeMillis[0] != null;
});
disconnectFromDS();
assertTrue("entry expired " + (expirationTime - expirationTimeMillis[0]) + " milliseconds early", expirationTimeMillis[0] >= expirationTime);
return null;
}
});
vm0.invoke(new SerializableRunnable() {
public void run() {
disconnectFromDS();
}
});
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionReliabilityTestCase method testFullAccessWithLocalRegionExpiration.
/**
* Tests affect of FULL_ACCESS on local region expiration actions.
*/
@Test
public void testFullAccessWithLocalRegionExpiration() throws Exception {
final String name = this.getUniqueName();
final String roleA = name + "-A";
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();
AttributesMutator mutator = region.getAttributesMutator();
mutator.setRegionTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
waitForRegionDestroy(region);
}
use of org.apache.geode.cache.ExpirationAttributes in project geode by apache.
the class RegionReliabilityTestCase method testNoAccessWithLocalRegionExpiration.
/**
* Tests affect of NO_ACCESS on local region expiration actions.
*/
@Test
public void testNoAccessWithLocalRegionExpiration() 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.NO_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));
// sleep and make sure region does not expire
sleep(200);
assertFalse(region.isDestroyed());
// create region in vm1
Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
createConnection(new String[] { roleA });
AttributesFactory fac = new AttributesFactory();
fac.setScope(getRegionScope());
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
}
});
waitForRegionDestroy(region);
}
Aggregations