use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.
the class MemoryThresholdsOffHeapDUnitTest method startCacheServer.
private int startCacheServer(VM server, final float evictionThreshold, final float criticalThreshold, final String regionName, final boolean createPR, final boolean notifyBySubscription, final int prRedundancy) throws Exception {
return (Integer) server.invoke(new SerializableCallable() {
public Object call() throws Exception {
getSystem(getOffHeapProperties());
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
InternalResourceManager irm = cache.getInternalResourceManager();
irm.setEvictionOffHeapPercentage(evictionThreshold);
irm.setCriticalOffHeapPercentage(criticalThreshold);
AttributesFactory factory = new AttributesFactory();
if (createPR) {
PartitionAttributesFactory paf = new PartitionAttributesFactory();
paf.setRedundantCopies(prRedundancy);
paf.setTotalNumBuckets(11);
factory.setPartitionAttributes(paf.create());
factory.setOffHeap(true);
} else {
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setOffHeap(true);
}
Region region = createRegion(regionName, factory.create());
if (createPR) {
assertTrue(region instanceof PartitionedRegion);
} else {
assertTrue(region instanceof DistributedRegion);
}
CacheServer cacheServer = getCache().addCacheServer();
cacheServer.setPort(0);
cacheServer.setNotifyBySubscription(notifyBySubscription);
cacheServer.start();
return cacheServer.getPort();
}
});
}
use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.
the class MemoryThresholdsOffHeapDUnitTest method createPR.
private CacheSerializableRunnable createPR(final String rName, final boolean accessor) {
return new CacheSerializableRunnable("create PR accessor") {
@Override
public void run2() throws CacheException {
// Assert some level of connectivity
getSystem(getOffHeapProperties());
InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
irm.setCriticalOffHeapPercentage(90f);
AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
if (!accessor) {
af.setCacheLoader(new CacheLoader<Integer, String>() {
final AtomicInteger numLoaderInvocations = new AtomicInteger(0);
public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
Integer expectedInvocations = (Integer) helper.getArgument();
final int actualInvocations = this.numLoaderInvocations.getAndIncrement();
if (expectedInvocations.intValue() != actualInvocations) {
throw new CacheLoaderException("Expected " + expectedInvocations + " invocations, actual is " + actualInvocations);
}
return helper.getKey().toString();
}
public void close() {
}
});
af.setPartitionAttributes(new PartitionAttributesFactory().create());
} else {
af.setPartitionAttributes(new PartitionAttributesFactory().setLocalMaxMemory(0).create());
}
af.setOffHeap(true);
getCache().createRegion(rName, af.create());
}
};
}
use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.
the class MemoryThresholdsOffHeapDUnitTest method testDRLoadRejection.
/**
* Test that DistributedRegion cacheLoade and netLoad are passed through to the calling thread if
* the local VM is in a critical state. Once the VM has moved to a safe state then test that they
* are allowed.
*/
// GEODE-438: test pollution, async actions, time sensitive,
@Category(FlakyTest.class)
// waitForCriterion, TODO: consider disconnect DS in setup
@Test
public void testDRLoadRejection() throws Exception {
final Host host = Host.getHost(0);
final VM replicate1 = host.getVM(1);
final VM replicate2 = host.getVM(2);
final String rName = getUniqueName();
// Make sure the desired VMs will have a fresh DS.
AsyncInvocation d1 = replicate1.invokeAsync(() -> disconnectFromDS());
AsyncInvocation d2 = replicate2.invokeAsync(() -> disconnectFromDS());
d1.join();
assertFalse(d1.exceptionOccurred());
d2.join();
assertFalse(d2.exceptionOccurred());
CacheSerializableRunnable establishConnectivity = new CacheSerializableRunnable("establishcConnectivity") {
@SuppressWarnings("synthetic-access")
@Override
public void run2() throws CacheException {
getSystem(getOffHeapProperties());
}
};
replicate1.invoke(establishConnectivity);
replicate2.invoke(establishConnectivity);
CacheSerializableRunnable createRegion = new CacheSerializableRunnable("create DistributedRegion") {
@Override
public void run2() throws CacheException {
// Assert some level of connectivity
InternalDistributedSystem ds = getSystem(getOffHeapProperties());
assertTrue(ds.getDistributionManager().getNormalDistributionManagerIds().size() >= 1);
InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
irm.setCriticalOffHeapPercentage(90f);
AttributesFactory af = new AttributesFactory();
af.setScope(Scope.DISTRIBUTED_ACK);
af.setDataPolicy(DataPolicy.REPLICATE);
af.setOffHeap(true);
Region region = getCache().createRegion(rName, af.create());
}
};
replicate1.invoke(createRegion);
replicate2.invoke(createRegion);
replicate1.invoke(addExpectedException);
replicate2.invoke(addExpectedException);
final Integer expected = (Integer) replicate1.invoke(new SerializableCallable("test Local DistributedRegion Load") {
public Object call() throws Exception {
final DistributedRegion r = (DistributedRegion) getCache().getRegion(rName);
AttributesMutator<Integer, String> am = r.getAttributesMutator();
am.setCacheLoader(new CacheLoader<Integer, String>() {
final AtomicInteger numLoaderInvocations = new AtomicInteger(0);
public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
Integer expectedInvocations = (Integer) helper.getArgument();
final int actualInvocations = this.numLoaderInvocations.getAndIncrement();
if (expectedInvocations.intValue() != actualInvocations) {
throw new CacheLoaderException("Expected " + expectedInvocations + " invocations, actual is " + actualInvocations);
}
return helper.getKey().toString();
}
public void close() {
}
});
int expectedInvocations = 0;
final OffHeapMemoryMonitor ohmm = ((InternalResourceManager) getCache().getResourceManager()).getOffHeapMonitor();
assertFalse(ohmm.getState().isCritical());
{
Integer k = new Integer(1);
assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
}
r.put("oh1", new byte[838860]);
r.put("oh3", new byte[157287]);
WaitCriterion wc = new WaitCriterion() {
public String description() {
return "expected region " + r + " to set memoryThreshold";
}
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, new Integer(expectedInvocations++)));
}
r.destroy("oh3");
wc = new WaitCriterion() {
public String description() {
return "expected region " + r + " to unset memoryThreshold";
}
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, new Integer(expectedInvocations++)));
}
return new Integer(expectedInvocations);
}
});
final CacheSerializableRunnable validateData1 = new CacheSerializableRunnable("Validate data 1") {
@Override
public void run2() throws CacheException {
Region r = getCache().getRegion(rName);
Integer i1 = new Integer(1);
assertTrue(r.containsKey(i1));
assertNotNull(r.getEntry(i1));
Integer i2 = new Integer(2);
assertFalse(r.containsKey(i2));
assertNull(r.getEntry(i2));
Integer i3 = new Integer(3);
assertTrue(r.containsKey(i3));
assertNotNull(r.getEntry(i3));
}
};
replicate1.invoke(validateData1);
replicate2.invoke(validateData1);
replicate2.invoke(new SerializableCallable("test DistributedRegion netLoad") {
public Object call() throws Exception {
final DistributedRegion r = (DistributedRegion) getCache().getRegion(rName);
final OffHeapMemoryMonitor ohmm = ((InternalResourceManager) getCache().getResourceManager()).getOffHeapMonitor();
assertFalse(ohmm.getState().isCritical());
int expectedInvocations = expected.intValue();
{
Integer k = new Integer(4);
assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
}
// Place in a critical state for the next test
r.put("oh3", new byte[157287]);
WaitCriterion wc = new WaitCriterion() {
public String description() {
return "expected region " + r + " to set memoryThreshold";
}
public boolean done() {
return r.memoryThresholdReached.get();
}
};
Wait.waitForCriterion(wc, 30 * 1000, 10, true);
{
Integer k = new Integer(5);
assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
}
r.destroy("oh3");
wc = new WaitCriterion() {
public String description() {
return "expected region " + r + " to unset memoryThreshold";
}
public boolean done() {
return !r.memoryThresholdReached.get();
}
};
Wait.waitForCriterion(wc, 30 * 1000, 10, true);
{
Integer k = new Integer(6);
assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
}
return new Integer(expectedInvocations);
}
});
replicate1.invoke(removeExpectedException);
replicate2.invoke(removeExpectedException);
final CacheSerializableRunnable validateData2 = new CacheSerializableRunnable("Validate data 2") {
@Override
public void run2() throws CacheException {
Region<Integer, String> r = getCache().getRegion(rName);
Integer i4 = new Integer(4);
assertTrue(r.containsKey(i4));
assertNotNull(r.getEntry(i4));
Integer i5 = new Integer(5);
assertFalse(r.containsKey(i5));
assertNull(r.getEntry(i5));
Integer i6 = new Integer(6);
assertTrue(r.containsKey(i6));
assertNotNull(r.getEntry(i6));
}
};
replicate1.invoke(validateData2);
replicate2.invoke(validateData2);
}
use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.
the class MemoryThresholdsOffHeapDUnitTest method doClientServerTest.
private void doClientServerTest(final String regionName, boolean createPR) throws Exception {
// create region on the server
final Host host = Host.getHost(0);
final VM server = host.getVM(0);
final VM client = host.getVM(1);
final Object bigKey = -1;
final Object smallKey = -2;
final int port = startCacheServer(server, 0f, 90f, regionName, createPR, false, 0);
startClient(client, server, port, regionName);
doPuts(client, regionName, false, /* catchServerException */
false);
doPutAlls(client, regionName, false, /* catchServerException */
false, /* catchLowMemoryException */
Range.DEFAULT);
// make the region sick in the server
final long bytesUsedAfterSmallKey = (long) server.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
InternalResourceManager irm = ((GemFireCacheImpl) getCache()).getInternalResourceManager();
final OffHeapMemoryMonitor ohm = irm.getOffHeapMonitor();
assertTrue(ohm.getState().isNormal());
getCache().getLoggerI18n().fine(addExpectedExString);
final LocalRegion r = (LocalRegion) getRootRegion().getSubregion(regionName);
final long bytesUsedAfterSmallKey;
{
OffHeapMemoryMonitorObserverImpl _testHook = new OffHeapMemoryMonitorObserverImpl();
ohm.testHook = _testHook;
try {
r.put(smallKey, "1234567890");
bytesUsedAfterSmallKey = _testHook.verifyBeginUpdateMemoryUsed(false);
} finally {
ohm.testHook = null;
}
}
{
final OffHeapMemoryMonitorObserverImpl th = new OffHeapMemoryMonitorObserverImpl();
ohm.testHook = th;
try {
r.put(bigKey, new byte[943720]);
th.verifyBeginUpdateMemoryUsed(bytesUsedAfterSmallKey + 943720 + 8, true);
WaitCriterion waitForCritical = new WaitCriterion() {
public boolean done() {
return th.checkUpdateStateAndSendEventBeforeProcess(bytesUsedAfterSmallKey + 943720 + 8, MemoryState.EVICTION_DISABLED_CRITICAL);
}
@Override
public String description() {
return null;
}
};
Wait.waitForCriterion(waitForCritical, 30 * 1000, 9, false);
th.validateUpdateStateAndSendEventBeforeProcess(bytesUsedAfterSmallKey + 943720 + 8, MemoryState.EVICTION_DISABLED_CRITICAL);
} finally {
ohm.testHook = null;
}
}
WaitCriterion wc;
if (r instanceof PartitionedRegion) {
final PartitionedRegion pr = (PartitionedRegion) r;
final int bucketId = PartitionedRegionHelper.getHashKey(pr, null, bigKey, null, null);
wc = new WaitCriterion() {
@Override
public String description() {
return "Expected to go critical: isCritical=" + ohm.getState().isCritical();
}
@Override
public boolean done() {
if (!ohm.getState().isCritical())
return false;
// Only done once the bucket has been marked sick
try {
pr.getRegionAdvisor().checkIfBucketSick(bucketId, bigKey);
return false;
} catch (LowMemoryException ignore) {
return true;
}
}
};
} else {
wc = new WaitCriterion() {
@Override
public String description() {
return "Expected to go critical: isCritical=" + ohm.getState().isCritical() + " memoryThresholdReached=" + r.memoryThresholdReached.get();
}
@Override
public boolean done() {
return ohm.getState().isCritical() && r.memoryThresholdReached.get();
}
};
}
Wait.waitForCriterion(wc, 30000, 9, true);
getCache().getLoggerI18n().fine(removeExpectedExString);
return bytesUsedAfterSmallKey;
}
});
// make sure client puts are rejected
doPuts(client, regionName, true, /* catchServerException */
false);
doPutAlls(client, regionName, true, /* catchServerException */
false, /* catchLowMemoryException */
new Range(Range.DEFAULT, Range.DEFAULT.width() + 1));
// make the region healthy in the server
server.invoke(new SerializableRunnable() {
public void run() {
InternalResourceManager irm = ((GemFireCacheImpl) getCache()).getInternalResourceManager();
final OffHeapMemoryMonitor ohm = irm.getOffHeapMonitor();
assertTrue(ohm.getState().isCritical());
getCache().getLogger().fine(MemoryThresholdsOffHeapDUnitTest.this.addExpectedBelow);
OffHeapMemoryMonitorObserverImpl _testHook = new OffHeapMemoryMonitorObserverImpl();
ohm.testHook = _testHook;
try {
getRootRegion().getSubregion(regionName).destroy(bigKey);
_testHook.verifyBeginUpdateMemoryUsed(bytesUsedAfterSmallKey, true);
} finally {
ohm.testHook = null;
}
WaitCriterion wc = new WaitCriterion() {
@Override
public String description() {
return "Expected to go normal";
}
@Override
public boolean done() {
return ohm.getState().isNormal();
}
};
Wait.waitForCriterion(wc, 30000, 9, true);
getCache().getLogger().fine(MemoryThresholdsOffHeapDUnitTest.this.removeExpectedBelow);
return;
}
});
}
use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.
the class EvictionTestBase method sendFakeNotification.
public void sendFakeNotification() {
HeapMemoryMonitor hmm = ((InternalResourceManager) cache.getResourceManager()).getHeapMonitor();
RegionEvictorTask.TEST_EVICTION_BURST_PAUSE_TIME_MILLIS = 0;
MemoryEvent event = new MemoryEvent(getResourceType(), MemoryState.NORMAL, MemoryState.EVICTION, cache.getDistributedSystem().getDistributedMember(), 90, true, hmm.getThresholds());
getEvictor().onEvent(event);
}
Aggregations