use of org.apache.geode.cache.RegionAttributes in project geode by apache.
the class RegionTestCase method testEntryTtlInvalidate.
/**
* Tests that an entry in a region expires with an invalidation after a given time to live.
*/
@Test
public void testEntryTtlInvalidate() throws CacheException {
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);
RegionAttributes attrs = factory.create();
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 {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
ExpiryTask.suspendExpiration();
Region.Entry entry = null;
long tilt;
try {
region.put(key, value);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key);
assertNotNull(entry.getValue());
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt);
}
use of org.apache.geode.cache.RegionAttributes in project geode by apache.
the class RegionTestCase method testRegionIdleInvalidate.
/**
* Tests that a region that remains idle for a given amount of time is invalidated. Also tests
* that accessing an entry of a region or a subregion counts as an access.
*/
@Test
public void testRegionIdleInvalidate() throws InterruptedException, CacheException {
if (getRegionAttributes().getPartitionAttributes() != null) {
// PR does not support INVALID ExpirationAction
return;
}
final String name = this.getUniqueName();
final String subname = this.getUniqueName() + "-SUB";
// ms
final int timeout = 22;
final Object key = "KEY";
final Object value = "VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(new CacheSerializableRunnable("testRegionIdleInvalidate") {
public void run2() throws CacheException {
TestCacheListener list = new TestCacheListener() {
private int createCount = 0;
public void afterInvalidate2(EntryEvent e) {
e.getRegion().getCache().getLogger().info("invalidate2 key=" + e.getKey());
}
public void afterRegionInvalidate2(RegionEvent e) {
}
public void afterUpdate2(EntryEvent e) {
// Clear the flag
this.wasInvoked();
}
public void afterCreate2(EntryEvent e) {
this.createCount++;
// we only expect one create; all the rest should be updates
assertEquals(1, this.createCount);
// Clear the flag
this.wasInvoked();
}
};
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
factory.setRegionIdleTimeout(expire);
factory.setStatisticsEnabled(true);
RegionAttributes subRegAttrs = factory.create();
factory.setCacheListener(list);
RegionAttributes attrs = factory.create();
Region region = null;
Region sub = null;
Region.Entry entry = null;
long tilt;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
ExpiryTask.suspendExpiration();
try {
region = createRegion(name, attrs);
region.put(key, value);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key);
assertEquals(value, entry.getValue());
sub = region.createSubregion(subname, subRegAttrs);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt, 10);
assertTrue(list.waitForInvocation(333));
// The next phase of the test verifies that a get will cause the
// expiration time to be extended.
// For this phase we don't worry about actually expiring but just
// making sure the expiration time gets extended.
final int EXPIRATION_MS = 9000;
region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(EXPIRATION_MS, ExpirationAction.INVALIDATE));
LocalRegion lr = (LocalRegion) region;
{
ExpiryTask expiryTask = lr.getRegionIdleExpiryTask();
region.put(key, value);
long createExpiry = expiryTask.getExpirationTime();
long changeTime = Wait.waitForExpiryClockToChange(lr, createExpiry - EXPIRATION_MS);
region.put(key, "VALUE2");
long putExpiry = expiryTask.getExpirationTime();
assertTrue("CLOCK went back in time! Expected putBaseExpiry=" + (putExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (putExpiry - EXPIRATION_MS - changeTime) >= 0);
assertTrue("expected putExpiry=" + putExpiry + " to be > than createExpiry=" + createExpiry, (putExpiry - createExpiry) > 0);
changeTime = Wait.waitForExpiryClockToChange(lr, putExpiry - EXPIRATION_MS);
region.get(key);
long getExpiry = expiryTask.getExpirationTime();
assertTrue("CLOCK went back in time! Expected getBaseExpiry=" + (getExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (getExpiry - EXPIRATION_MS - changeTime) >= 0);
assertTrue("expected getExpiry=" + getExpiry + " to be > than putExpiry=" + putExpiry, (getExpiry - putExpiry) > 0);
changeTime = Wait.waitForExpiryClockToChange(lr, getExpiry - EXPIRATION_MS);
sub.put(key, value);
long subPutExpiry = expiryTask.getExpirationTime();
assertTrue("CLOCK went back in time! Expected subPutBaseExpiry=" + (subPutExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subPutExpiry - EXPIRATION_MS - changeTime) >= 0);
assertTrue("expected subPutExpiry=" + subPutExpiry + " to be > than getExpiry=" + getExpiry, (subPutExpiry - getExpiry) > 0);
changeTime = Wait.waitForExpiryClockToChange(lr, subPutExpiry - EXPIRATION_MS);
sub.get(key);
long subGetExpiry = expiryTask.getExpirationTime();
assertTrue("CLOCK went back in time! Expected subGetBaseExpiry=" + (subGetExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subGetExpiry - EXPIRATION_MS - changeTime) >= 0);
assertTrue("expected subGetExpiry=" + subGetExpiry + " to be > than subPutExpiry=" + subPutExpiry, (subGetExpiry - subPutExpiry) > 0);
}
}
});
}
use of org.apache.geode.cache.RegionAttributes in project geode by apache.
the class RegionReliabilityTestCase method testCommitDistributionException.
@Test
public void testCommitDistributionException() throws Exception {
if (getRegionScope().isGlobal())
// skip test under Global
return;
if (getRegionScope().isDistributedNoAck())
// skip test under DistributedNoAck
return;
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);
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
RegionMembershipListener listener = new RegionMembershipListenerAdapter() {
public void afterRemoteRegionDeparture(RegionEvent event) {
synchronized (detectedDeparture_testCommitDistributionException) {
detectedDeparture_testCommitDistributionException[0] = Boolean.TRUE;
detectedDeparture_testCommitDistributionException.notify();
}
}
};
// 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.addCacheListener(listener);
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
// use vm1 to create role
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);
}
});
// define the afterReleaseLocalLocks callback
SerializableRunnableIF removeRequiredRole = new SerializableRunnableIF() {
public void run() {
Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {
public void run() {
getRootRegion(name).close();
}
});
try {
synchronized (detectedDeparture_testCommitDistributionException) {
while (detectedDeparture_testCommitDistributionException[0] == Boolean.FALSE) {
detectedDeparture_testCommitDistributionException.wait();
}
}
} catch (InterruptedException e) {
fail("interrupted");
}
}
};
// define the add and remove expected exceptions
final String expectedExceptions = "org.apache.geode.internal.cache.CommitReplyException";
SerializableRunnable addExpectedExceptions = new CacheSerializableRunnable("addExpectedExceptions") {
public void run2() throws CacheException {
getCache().getLogger().info("<ExpectedException action=add>" + expectedExceptions + "</ExpectedException>");
}
};
SerializableRunnable removeExpectedExceptions = new CacheSerializableRunnable("removeExpectedExceptions") {
public void run2() throws CacheException {
getCache().getLogger().info("<ExpectedException action=remove>" + expectedExceptions + "</ExpectedException>");
}
};
// perform the actual test...
CacheTransactionManager ctm = cache.getCacheTransactionManager();
ctm.begin();
TXStateInterface txStateProxy = ((TXManagerImpl) ctm).getTXState();
((TXStateProxyImpl) txStateProxy).forceLocalBootstrap();
TXState txState = (TXState) ((TXStateProxyImpl) txStateProxy).getRealDeal(null, null);
txState.setBeforeSend(() -> {
try {
removeRequiredRole.run();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
// now start a transaction and commit it
region.put("KEY", "VAL");
addExpectedExceptions.run();
Host.getHost(0).getVM(1).invoke(addExpectedExceptions);
try {
ctm.commit();
fail("Should have thrown CommitDistributionException");
} catch (CommitDistributionException e) {
// pass
} finally {
removeExpectedExceptions.run();
Host.getHost(0).getVM(1).invoke(removeExpectedExceptions);
}
}
use of org.apache.geode.cache.RegionAttributes in project geode by apache.
the class RegionReliabilityTestCase method testLimitedAccessWithLocalEntryExpiration.
/**
* Tests affect of LIMITED_ACCESS on local entry expiration actions.
*/
@Test
public void testLimitedAccessWithLocalEntryExpiration() 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.LIMITED_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();
// use vm1 to create role
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);
}
});
// test to make sure expiration is suspended
region.put("expireMe", "expireMe");
assertTrue(region.size() == 1);
Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Close Region") {
public void run2() throws CacheException {
Region region = getRootRegion(name);
region.close();
}
});
// TODO: waitForMemberTimeout(); ?
// set expiration and sleep
AttributesMutator mutator = region.getAttributesMutator();
mutator.setEntryTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
WaitCriterion wc1 = new WaitCriterion() {
public boolean done() {
return ((LocalRegion) region).basicEntries(false).size() == 0;
}
public String description() {
return "expected zero entries but have " + ((LocalRegion) region).basicEntries(false).size();
}
};
Wait.waitForCriterion(wc1, 30 * 1000, 10, true);
// create region again
Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
AttributesFactory fac = new AttributesFactory();
fac.setScope(getRegionScope());
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
}
});
region.put("expireMe", "expireMe");
waitForEntryDestroy(region, "expireMe");
assertTrue(region.size() == 0);
}
use of org.apache.geode.cache.RegionAttributes in project geode by apache.
the class RequiredRolesDUnitTest method testIsRoleInRegionMembership.
/**
* Tests RequiredRoles.isRoleInRegionMembership().
*/
@Test
public void testIsRoleInRegionMembership() throws Exception {
final String name = this.getUniqueName();
final int vm0 = 0;
final int vm1 = 1;
final int vm2 = 2;
final int vm3 = 3;
final String roleA = name + "-A";
final String roleC = name + "-C";
final String roleD = name + "-D";
// assign names to 4 vms...
final String[] requiredRoles = { roleA, roleC, roleD };
final String[] rolesProp = { "", roleA, roleA, roleC + "," + roleD };
final String[][] vmRoles = new String[][] { {}, { roleA }, { roleA }, { roleC, roleD } };
for (int i = 0; i < vmRoles.length; i++) {
final int vm = i;
Host.getHost(0).getVM(vm).invoke(new SerializableRunnable() {
public void run() {
Properties config = new Properties();
config.setProperty(ROLES, rolesProp[vm]);
getSystem(config);
}
});
}
// connect controller to system...
Properties config = new Properties();
config.setProperty(ROLES, "");
getSystem(config);
// create region in controller...
MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
AttributesFactory fac = new AttributesFactory();
fac.setMembershipAttributes(ra);
fac.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
// wait for memberTimeout to expire
waitForMemberTimeout();
// assert each role is missing
final Set requiredRolesSet = region.getAttributes().getMembershipAttributes().getRequiredRoles();
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertFalse(RequiredRoles.isRoleInRegionMembership(region, role));
}
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
AttributesFactory fac = new AttributesFactory();
fac.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
}
};
// create region in vm0... no gain for no role
Host.getHost(0).getVM(vm0).invoke(create);
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertFalse(RequiredRoles.isRoleInRegionMembership(region, role));
}
// create region in vm1... gain for 1st instance of redundant role
Host.getHost(0).getVM(vm1).invoke(create);
for (int i = 0; i < vmRoles[vm1].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm1][i]);
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
// create region in vm2... no gain for 2nd instance of redundant role
Host.getHost(0).getVM(vm2).invoke(create);
for (int i = 0; i < vmRoles[vm2].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm2][i]);
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
// create region in vm3... gain for 2 roles
Host.getHost(0).getVM(vm3).invoke(create);
for (int i = 0; i < vmRoles[vm3].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm3][i]);
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
SerializableRunnable destroy = new CacheSerializableRunnable("Destroy Region") {
public void run2() throws CacheException {
Region region = getRootRegion(name);
region.localDestroyRegion();
}
};
// destroy region in vm0... no loss of any role
Host.getHost(0).getVM(vm0).invoke(destroy);
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
// destroy region in vm1... nothing happens in 1st removal of redundant role
Host.getHost(0).getVM(vm1).invoke(destroy);
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
// destroy region in vm2... 2nd removal of redundant role is loss
Host.getHost(0).getVM(vm2).invoke(destroy);
for (int i = 0; i < vmRoles[vm2].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm2][i]);
assertFalse(RequiredRoles.isRoleInRegionMembership(region, role));
}
// destroy region in vm3... two more roles are in loss
Host.getHost(0).getVM(vm3).invoke(destroy);
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertFalse(RequiredRoles.isRoleInRegionMembership(region, role));
}
}
Aggregations