use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class MemoryThresholdsOffHeapDUnitTest method prRemotePutRejection.
private void prRemotePutRejection(boolean cacheClose, boolean localDestroy, final boolean useTx) throws Exception {
final Host host = Host.getHost(0);
final VM accessor = host.getVM(0);
final VM[] servers = new VM[3];
servers[0] = host.getVM(1);
servers[1] = host.getVM(2);
servers[2] = host.getVM(3);
final String regionName = "offHeapPRRemotePutRejection";
final int redundancy = 1;
startCacheServer(servers[0], 0f, 90f, regionName, true, /* createPR */
false, /* notifyBySubscription */
redundancy);
startCacheServer(servers[1], 0f, 90f, regionName, true, /* createPR */
false, /* notifyBySubscription */
redundancy);
startCacheServer(servers[2], 0f, 90f, regionName, true, /* createPR */
false, /* notifyBySubscription */
redundancy);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
getSystem(getOffHeapProperties());
getCache();
AttributesFactory factory = new AttributesFactory();
PartitionAttributesFactory paf = new PartitionAttributesFactory();
paf.setRedundantCopies(redundancy);
paf.setLocalMaxMemory(0);
paf.setTotalNumBuckets(11);
factory.setPartitionAttributes(paf.create());
factory.setOffHeap(true);
createRegion(regionName, factory.create());
return null;
}
});
doPuts(accessor, regionName, false, false);
final Range r1 = Range.DEFAULT;
doPutAlls(accessor, regionName, false, false, r1);
servers[0].invoke(addExpectedException);
servers[1].invoke(addExpectedException);
servers[2].invoke(addExpectedException);
setUsageAboveCriticalThreshold(servers[0], regionName);
final Set<InternalDistributedMember> criticalMembers = (Set) servers[0].invoke(new SerializableCallable() {
public Object call() throws Exception {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion().getSubregion(regionName);
final int hashKey = PartitionedRegionHelper.getHashKey(pr, null, "oh5", null, null);
return pr.getRegionAdvisor().getBucketOwners(hashKey);
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion().getSubregion(regionName);
WaitCriterion wc = new WaitCriterion() {
public String description() {
return "remote bucket not marked sick";
}
public boolean done() {
boolean keyFoundOnSickMember = false;
boolean caughtException = false;
for (int i = 0; i < 20; i++) {
Integer key = Integer.valueOf(i);
int hKey = PartitionedRegionHelper.getHashKey(pr, null, key, null, null);
Set<InternalDistributedMember> owners = pr.getRegionAdvisor().getBucketOwners(hKey);
final boolean hasCriticalOwners = owners.removeAll(criticalMembers);
if (hasCriticalOwners) {
keyFoundOnSickMember = true;
try {
if (useTx)
getCache().getCacheTransactionManager().begin();
pr.getCache().getLogger().fine("SWAP:putting in tx:" + useTx);
pr.put(key, "value");
if (useTx)
getCache().getCacheTransactionManager().commit();
} catch (LowMemoryException ex) {
caughtException = true;
if (useTx)
getCache().getCacheTransactionManager().rollback();
}
} else {
// puts on healthy member should continue
pr.put(key, "value");
}
}
return keyFoundOnSickMember && caughtException;
}
};
Wait.waitForCriterion(wc, 10000, 10, true);
return null;
}
});
{
Range r2 = new Range(r1, r1.width() + 1);
doPutAlls(accessor, regionName, false, true, r2);
}
// Find all VMs that have a critical region
SerializableCallable getMyId = new SerializableCallable() {
public Object call() throws Exception {
return ((GemFireCacheImpl) getCache()).getMyId();
}
};
final Set<VM> criticalServers = new HashSet<VM>();
for (final VM server : servers) {
DistributedMember member = (DistributedMember) server.invoke(getMyId);
if (criticalMembers.contains(member)) {
criticalServers.add(server);
}
}
if (localDestroy) {
// local destroy the region on sick members
for (final VM vm : criticalServers) {
vm.invoke(new SerializableCallable("local destroy sick member") {
public Object call() throws Exception {
Region r = getRootRegion().getSubregion(regionName);
LogWriterUtils.getLogWriter().info("PRLocalDestroy");
r.localDestroyRegion();
return null;
}
});
}
} else if (cacheClose) {
// close cache on sick members
for (final VM vm : criticalServers) {
vm.invoke(new SerializableCallable("close cache sick member") {
public Object call() throws Exception {
getCache().close();
return null;
}
});
}
} else {
setUsageBelowEviction(servers[0], regionName);
servers[0].invoke(removeExpectedException);
servers[1].invoke(removeExpectedException);
servers[2].invoke(removeExpectedException);
}
// do put all in a loop to allow distribution of message
accessor.invoke(new SerializableCallable("Put in a loop") {
public Object call() throws Exception {
final Region r = getRootRegion().getSubregion(regionName);
WaitCriterion wc = new WaitCriterion() {
public String description() {
return "pr should have gone un-critical";
}
public boolean done() {
boolean done = true;
for (int i = 0; i < 20; i++) {
try {
r.put(i, "value");
} catch (LowMemoryException e) {
// expected
done = false;
}
}
return done;
}
};
Wait.waitForCriterion(wc, 10000, 10, true);
return null;
}
});
doPutAlls(accessor, regionName, false, false, r1);
}
use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class MemoryThresholdsOffHeapDUnitTest method testLRLoadRejection.
/**
* Test that LocalRegion cache Loads are not stored in the Region if the VM is in a critical
* state, then test that they are allowed once the VM is no longer critical
*/
@Test
public void testLRLoadRejection() throws Exception {
final Host host = Host.getHost(0);
final VM vm = host.getVM(2);
final String rName = getUniqueName();
vm.invoke(() -> disconnectFromDS());
vm.invoke(new CacheSerializableRunnable("test LocalRegion load passthrough when critical") {
@Override
public void run2() throws CacheException {
getSystem(getOffHeapProperties());
InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
final OffHeapMemoryMonitor ohmm = irm.getOffHeapMonitor();
irm.setCriticalOffHeapPercentage(90f);
AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
af.setScope(Scope.LOCAL);
af.setOffHeap(true);
final AtomicInteger numLoaderInvocations = new AtomicInteger(0);
af.setCacheLoader(new CacheLoader<Integer, String>() {
public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
numLoaderInvocations.incrementAndGet();
return helper.getKey().toString();
}
public void close() {
}
});
final LocalRegion r = (LocalRegion) getCache().createRegion(rName, af.create());
assertFalse(ohmm.getState().isCritical());
int expectedInvocations = 0;
assertEquals(expectedInvocations++, numLoaderInvocations.get());
{
Integer k = new Integer(1);
assertEquals(k.toString(), r.get(k));
}
assertEquals(expectedInvocations++, numLoaderInvocations.get());
expectedInvocations++;
expectedInvocations++;
r.getAll(createRanges(10, 12));
assertEquals(expectedInvocations++, numLoaderInvocations.get());
getCache().getLoggerI18n().fine(addExpectedExString);
r.put("oh1", new byte[838860]);
r.put("oh3", new byte[157287]);
getCache().getLoggerI18n().fine(removeExpectedExString);
WaitCriterion wc = new WaitCriterion() {
public String description() {
return "expected region " + r + " to set memoryThresholdReached";
}
public boolean done() {
return r.memoryThresholdReached.get();
}
};
Wait.waitForCriterion(wc, 30 * 1000, 10, true);
{
Integer k = new Integer(2);
assertEquals(k.toString(), r.get(k));
}
assertEquals(expectedInvocations++, numLoaderInvocations.get());
expectedInvocations++;
expectedInvocations++;
r.getAll(createRanges(13, 15));
assertEquals(expectedInvocations++, numLoaderInvocations.get());
getCache().getLoggerI18n().fine(addExpectedBelow);
r.destroy("oh3");
getCache().getLoggerI18n().fine(removeExpectedBelow);
wc = new WaitCriterion() {
public String description() {
return "expected region " + r + " to unset memoryThresholdReached";
}
public boolean done() {
return !r.memoryThresholdReached.get();
}
};
Wait.waitForCriterion(wc, 30 * 1000, 10, true);
{
Integer k = new Integer(3);
assertEquals(k.toString(), r.get(k));
}
assertEquals(expectedInvocations++, numLoaderInvocations.get());
expectedInvocations++;
expectedInvocations++;
r.getAll(createRanges(16, 18));
assertEquals(expectedInvocations, numLoaderInvocations.get());
// Do extra validation that the entry doesn't exist in the local region
for (Integer i : createRanges(2, 2, 13, 15)) {
if (r.containsKey(i)) {
fail("Expected containsKey return false for key" + i);
}
if (r.getEntry(i) != null) {
fail("Expected getEntry to return null for key" + i);
}
}
}
});
}
use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class MemoryThresholdsOffHeapDUnitTest method doDistributedRegionRemotePutRejection.
/**
* test that puts in a server are rejected when a remote VM crosses critical threshold
*
* @throws Exception
*/
private void doDistributedRegionRemotePutRejection(boolean localDestroy, boolean cacheClose) throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM server2 = host.getVM(1);
final String regionName = "offHeapDRRemotePutRejection";
// set port to 0 in-order for system to pickup a random port.
startCacheServer(server1, 0f, 0f, regionName, false, /* createPR */
false, /* notifyBySubscription */
0);
startCacheServer(server2, 0f, 90f, regionName, false, /* createPR */
false, /* notifyBySubscription */
0);
registerTestMemoryThresholdListener(server1);
registerTestMemoryThresholdListener(server2);
doPuts(server1, regionName, false, /* catchRejectedException */
false);
doPutAlls(server1, regionName, false, /* catchRejectedException */
false, /* catchLowMemoryException */
Range.DEFAULT);
// make server2 critical
setUsageAboveCriticalThreshold(server2, regionName);
verifyListenerValue(server1, MemoryState.CRITICAL, 1, true);
verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
// make sure that local server1 puts are rejected
doPuts(server1, regionName, false, /* catchRejectedException */
true);
Range r1 = new Range(Range.DEFAULT, Range.DEFAULT.width() + 1);
doPutAlls(server1, regionName, false, /* catchRejectedException */
true, /* catchLowMemoryException */
r1);
if (localDestroy) {
// local destroy the region on sick member
server2.invoke(new SerializableCallable("local destroy") {
public Object call() throws Exception {
Region r = getRootRegion().getSubregion(regionName);
r.localDestroyRegion();
return null;
}
});
} else if (cacheClose) {
server2.invoke(new SerializableCallable() {
public Object call() throws Exception {
getCache().close();
return null;
}
});
} else {
setUsageBelowEviction(server2, regionName);
}
// wait for remote region destroyed message to be processed
server1.invoke(new SerializableCallable() {
public Object call() throws Exception {
WaitCriterion wc = new WaitCriterion() {
public String description() {
return "remote localRegionDestroyed message not received";
}
public boolean done() {
DistributedRegion dr = (DistributedRegion) getRootRegion().getSubregion(regionName);
return dr.getMemoryThresholdReachedMembers().size() == 0;
}
};
Wait.waitForCriterion(wc, 10000, 10, true);
return null;
}
});
// make sure puts succeed
doPuts(server1, regionName, false, /* catchRejectedException */
false);
Range r2 = new Range(r1, r1.width() + 1);
doPutAlls(server1, regionName, false, /* catchRejectedException */
false, /* catchLowMemoryException */
r2);
}
use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class MemoryThresholdsDUnitTest method prRemotePutRejection.
private void prRemotePutRejection(boolean cacheClose, boolean localDestroy, final boolean useTx) throws Exception {
final Host host = Host.getHost(0);
final VM accessor = host.getVM(0);
final VM server1 = host.getVM(1);
final VM server2 = host.getVM(2);
final VM server3 = host.getVM(3);
final String regionName = "testPrRejection";
final int redundancy = 1;
final ServerPorts ports1 = startCacheServer(server1, 80f, 90f, regionName, true, /* createPR */
false, /* notifyBySubscription */
redundancy);
ServerPorts ports2 = startCacheServer(server2, 80f, 90f, regionName, true, /* createPR */
false, /* notifyBySubscription */
redundancy);
ServerPorts ports3 = startCacheServer(server3, 80f, 90f, regionName, true, /* createPR */
false, /* notifyBySubscription */
redundancy);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
getSystem(getServerProperties());
getCache();
AttributesFactory factory = new AttributesFactory();
PartitionAttributesFactory paf = new PartitionAttributesFactory();
paf.setRedundantCopies(redundancy);
paf.setLocalMaxMemory(0);
paf.setTotalNumBuckets(11);
factory.setPartitionAttributes(paf.create());
createRegion(regionName, factory.create());
return null;
}
});
doPuts(accessor, regionName, false, false);
final Range r1 = Range.DEFAULT;
doPutAlls(accessor, regionName, false, false, r1);
SerializableCallable getMyId = new SerializableCallable() {
public Object call() throws Exception {
return ((GemFireCacheImpl) getCache()).getMyId();
}
};
final DistributedMember server1Id = (DistributedMember) server1.invoke(getMyId);
setUsageAboveCriticalThreshold(server1);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion().getSubregion(regionName);
final String regionPath = getRootRegion().getSubregion(regionName).getFullPath();
// server1 is sick, look for a key on server1, and attempt put again
WaitCriterion wc = new WaitCriterion() {
public String description() {
return "remote bucket not marked sick";
}
public boolean done() {
boolean keyFoundOnSickMember = false;
boolean caughtException = false;
for (int i = 0; i < 20; i++) {
Integer key = Integer.valueOf(i);
int hKey = PartitionedRegionHelper.getHashKey(pr, null, key, null, null);
Set<InternalDistributedMember> owners = pr.getRegionAdvisor().getBucketOwners(hKey);
if (owners.contains(server1Id)) {
keyFoundOnSickMember = true;
try {
if (useTx)
getCache().getCacheTransactionManager().begin();
pr.getCache().getLogger().fine("SWAP:putting in tx:" + useTx);
pr.put(key, "value");
if (useTx)
getCache().getCacheTransactionManager().commit();
} catch (LowMemoryException ex) {
caughtException = true;
if (useTx)
getCache().getCacheTransactionManager().rollback();
}
} else {
// puts on healthy member should continue
pr.put(key, "value");
}
}
return keyFoundOnSickMember && caughtException;
}
};
Wait.waitForCriterion(wc, 30000, 10, true);
return null;
}
});
{
Range r2 = new Range(r1, r1.width() + 1);
doPutAlls(accessor, regionName, false, true, r2);
}
if (localDestroy) {
// local destroy the region on sick member
server1.invoke(new SerializableCallable("local destroy sick member") {
public Object call() throws Exception {
Region r = getRootRegion().getSubregion(regionName);
LogWriterUtils.getLogWriter().info("PRLocalDestroy");
r.localDestroyRegion();
return null;
}
});
} else if (cacheClose) {
// close cache on sick member
server1.invoke(new SerializableCallable("close cache sick member") {
public Object call() throws Exception {
getCache().close();
return null;
}
});
} else {
setUsageBelowEviction(server1);
}
// do put all in a loop to allow distribution of message
accessor.invoke(new SerializableCallable("Put in a loop") {
public Object call() throws Exception {
final Region r = getRootRegion().getSubregion(regionName);
WaitCriterion wc = new WaitCriterion() {
public String description() {
return "pr should have gone un-critical";
}
public boolean done() {
boolean done = true;
for (int i = 0; i < 20; i++) {
try {
r.put(i, "value");
} catch (LowMemoryException e) {
// expected
done = false;
}
}
return done;
}
};
Wait.waitForCriterion(wc, 30000, 10, true);
return null;
}
});
doPutAlls(accessor, regionName, false, false, r1);
}
use of org.apache.geode.test.dunit.WaitCriterion in project geode by apache.
the class MultiVMRegionTestCase method testNoDataSerializer.
/**
* Tests what happens when one VM attempts to read an object for which it does not have a
* registered <code>DataSerializer</code>.
*
* @since GemFire 3.5
*/
@Test
public void testNoDataSerializer() throws Exception {
assertTrue(getRegionAttributes().getScope().isDistributed());
final String name = this.getUniqueName();
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
@Override
public void run2() throws CacheException {
createRegion(name);
}
};
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": before creates");
vm0.invoke(create);
vm1.invoke(create);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after creates");
final Object key = "KEY";
final Object key2 = "KEY2";
final int intValue = 3452;
final long longValue = 213421;
// final boolean[] wasInvoked = new boolean[1];
vm2.invoke(new SerializableRunnable("Disconnect from DS") {
@Override
public void run() {
disconnectFromDS();
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after vm2 disconnect");
try {
vm0.invoke(new CacheSerializableRunnable("Put int") {
@Override
public void run2() throws CacheException {
Class c = IntWrapper.IntWrapperSerializer.class;
IntWrapper.IntWrapperSerializer serializer = (IntWrapper.IntWrapperSerializer) DataSerializer.register(c);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Registered serializer id:" + serializer.getId() + " class:" + c.getName());
Region region = getRootRegion().getSubregion(name);
region.put(key, new IntWrapper(intValue));
flushIfNecessary(region);
assertTrue(serializer.wasInvoked);
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after vm0 put");
SerializableRunnable get = new CacheSerializableRunnable("Get int") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
// wait a while for the serializer to be registered
// A race condition exists in the product in which
// this thread can be stuck waiting in getSerializer
// for 60 seconds. So it only calls getSerializer once
// causing it to fail intermittently (see GEODE-376).
// To workaround this the test wets WAIT_MS to 1 ms.
// So the getSerializer will only block for 1 ms.
// This allows the WaitCriterion to make multiple calls
// of getSerializer and the subsequent calls will find
// the DataSerializer.
final int savVal = InternalDataSerializer.GetMarker.WAIT_MS;
InternalDataSerializer.GetMarker.WAIT_MS = 1;
try {
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
return InternalDataSerializer.getSerializer((byte) 120) != null;
}
@Override
public String description() {
return "DataSerializer with id 120 was never registered";
}
};
Wait.waitForCriterion(ev, 30 * 1000, 10, true);
} finally {
InternalDataSerializer.GetMarker.WAIT_MS = savVal;
}
IntWrapper value = (IntWrapper) region.get(key);
assertNotNull(InternalDataSerializer.getSerializer((byte) 120));
assertNotNull(value);
assertEquals(intValue, value.intValue);
}
};
vm1.invoke(get);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after vm1 get");
// Make sure that VMs that connect after registration can get the
// serializer
vm2.invoke(new SerializableRunnable("Connect to DS") {
@Override
public void run() {
// Register a DataSerializer before connecting to system
Class c = LongWrapper.LongWrapperSerializer.class;
DataSerializer.register(c);
getSystem();
}
});
vm2.invoke(create);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after vm2 create");
vm2.invoke(new CacheSerializableRunnable("Put long") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.put(key2, new LongWrapper(longValue));
flushIfNecessary(region);
LongWrapper.LongWrapperSerializer serializer = (LongWrapper.LongWrapperSerializer) InternalDataSerializer.getSerializer((byte) 121);
assertTrue(serializer.wasInvoked);
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after vm2 put");
vm2.invoke(get);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after vm2 get");
SerializableRunnable get2 = new CacheSerializableRunnable("Get long") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
LongWrapper value = (LongWrapper) region.get(key2);
assertNotNull(InternalDataSerializer.getSerializer((byte) 121));
assertNotNull(value);
assertEquals(longValue, value.longValue);
}
};
vm0.invoke(get2);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after vm0 get2");
vm1.invoke(get2);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after vm1 get2");
// wait a little while for other netsearch requests to return
// before unregistering the serializers that will be needed to process these
// responses.
} finally {
Wait.pause(1500);
unregisterAllSerializers();
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": after unregister");
}
}
Aggregations