use of org.apache.geode.internal.cache.control.HeapMemoryMonitor in project geode by apache.
the class MemoryThresholdsDUnitTest method testPRLoadRejection.
/**
* Test that a Partitioned Region loader invocation is rejected if the VM with the bucket is in a
* critical state.
*
* @throws Exception
*/
@Test
public void testPRLoadRejection() throws Exception {
final Host host = Host.getHost(0);
final VM accessor = host.getVM(1);
final VM ds1 = host.getVM(2);
final String rName = getUniqueName();
final float criticalHeapThresh = 0.90f;
final int fakeHeapMaxSize = 1000;
// Make sure the desired VMs will have a fresh DS.
AsyncInvocation d0 = accessor.invokeAsync(() -> disconnectFromDS());
AsyncInvocation d1 = ds1.invokeAsync(() -> disconnectFromDS());
d0.join();
assertFalse(d0.exceptionOccurred());
d1.join();
assertFalse(d1.exceptionOccurred());
CacheSerializableRunnable establishConnectivity = new CacheSerializableRunnable("establishcConnectivity") {
@Override
public void run2() throws CacheException {
getSystem();
}
};
ds1.invoke(establishConnectivity);
accessor.invoke(establishConnectivity);
ds1.invoke(createPR(rName, false, fakeHeapMaxSize, criticalHeapThresh));
accessor.invoke(createPR(rName, true, fakeHeapMaxSize, criticalHeapThresh));
final AtomicInteger expectedInvocations = new AtomicInteger(0);
Integer ex = (Integer) accessor.invoke(new SerializableCallable("Invoke loader from accessor, non-critical") {
public Object call() throws Exception {
Region<Integer, String> r = getCache().getRegion(rName);
Integer k = new Integer(1);
Integer expectedInvocations0 = new Integer(expectedInvocations.getAndIncrement());
// should load for new key
assertEquals(k.toString(), r.get(k, expectedInvocations0));
assertTrue(r.containsKey(k));
Integer expectedInvocations1 = new Integer(expectedInvocations.get());
// no load
assertEquals(k.toString(), r.get(k, expectedInvocations1));
// no load
assertEquals(k.toString(), r.get(k, expectedInvocations1));
return expectedInvocations1;
}
});
expectedInvocations.set(ex.intValue());
ex = (Integer) ds1.invoke(new SerializableCallable("Invoke loader from datastore, non-critical") {
public Object call() throws Exception {
Region<Integer, String> r = getCache().getRegion(rName);
Integer k = new Integer(2);
Integer expectedInvocations1 = new Integer(expectedInvocations.getAndIncrement());
// should load for new key
assertEquals(k.toString(), r.get(k, expectedInvocations1));
assertTrue(r.containsKey(k));
Integer expectedInvocations2 = new Integer(expectedInvocations.get());
// no load
assertEquals(k.toString(), r.get(k, expectedInvocations2));
// no load
assertEquals(k.toString(), r.get(k, expectedInvocations2));
String oldVal = r.remove(k);
assertFalse(r.containsKey(k));
assertEquals(k.toString(), oldVal);
return expectedInvocations2;
}
});
expectedInvocations.set(ex.intValue());
accessor.invoke(addExpectedException);
ds1.invoke(addExpectedException);
ex = (Integer) ds1.invoke(new SerializableCallable("Set critical state, assert local load behavior") {
public Object call() throws Exception {
// usage
long newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));
// above
// critical
// by
// 10%
assertTrue(newfakeHeapUsage > 0);
assertTrue(newfakeHeapUsage <= fakeHeapMaxSize);
HeapMemoryMonitor hmm = ((InternalResourceManager) getCache().getResourceManager()).getHeapMonitor();
hmm.updateStateAndSendEvent(newfakeHeapUsage);
assertTrue(hmm.getState().isCritical());
// reload with same key again and again
final Integer k = new Integer(2);
final Integer expectedInvocations3 = new Integer(expectedInvocations.getAndIncrement());
Region<Integer, String> r = getCache().getRegion(rName);
// load
assertEquals(k.toString(), r.get(k, expectedInvocations3));
assertFalse(r.containsKey(k));
Integer expectedInvocations4 = new Integer(expectedInvocations.getAndIncrement());
// load
assertEquals(k.toString(), r.get(k, expectedInvocations4));
assertFalse(r.containsKey(k));
Integer expectedInvocations5 = new Integer(expectedInvocations.get());
// load
assertEquals(k.toString(), r.get(k, expectedInvocations5));
assertFalse(r.containsKey(k));
return expectedInvocations5;
}
});
expectedInvocations.set(ex.intValue());
ex = (Integer) accessor.invoke(new SerializableCallable("During critical state on datastore, assert accesor load behavior") {
public Object call() throws Exception {
// reload with same key again and again
final Integer k = new Integer(2);
Integer expectedInvocations6 = new Integer(expectedInvocations.incrementAndGet());
Region<Integer, String> r = getCache().getRegion(rName);
// load
assertEquals(k.toString(), r.get(k, expectedInvocations6));
assertFalse(r.containsKey(k));
Integer expectedInvocations7 = new Integer(expectedInvocations.incrementAndGet());
// load
assertEquals(k.toString(), r.get(k, expectedInvocations7));
assertFalse(r.containsKey(k));
return expectedInvocations7;
}
});
expectedInvocations.set(ex.intValue());
ex = (Integer) ds1.invoke(new SerializableCallable("Set safe state on datastore, assert local load behavior") {
public Object call() throws Exception {
HeapMemoryMonitor hmm = ((InternalResourceManager) getCache().getResourceManager()).getHeapMonitor();
// below
int newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.3f));
// critical
// by
// 30%
assertTrue(fakeHeapMaxSize > 0);
getCache().getLoggerI18n().fine(addExpectedBelow);
hmm.updateStateAndSendEvent(newfakeHeapUsage);
getCache().getLoggerI18n().fine(removeExpectedBelow);
assertFalse(hmm.getState().isCritical());
// same key as previously used, this time is should stick
Integer k = new Integer(3);
Integer expectedInvocations8 = new Integer(expectedInvocations.incrementAndGet());
Region<Integer, String> r = getCache().getRegion(rName);
// last load for 3
assertEquals(k.toString(), r.get(k, expectedInvocations8));
assertTrue(r.containsKey(k));
return expectedInvocations8;
}
});
expectedInvocations.set(ex.intValue());
accessor.invoke(new SerializableCallable("Data store in safe state, assert load behavior, accessor sets critical state, assert load behavior") {
public Object call() throws Exception {
HeapMemoryMonitor hmm = ((InternalResourceManager) getCache().getResourceManager()).getHeapMonitor();
assertFalse(hmm.getState().isCritical());
Integer k = new Integer(4);
Integer expectedInvocations9 = new Integer(expectedInvocations.incrementAndGet());
Region<Integer, String> r = getCache().getRegion(rName);
// load for 4
assertEquals(k.toString(), r.get(k, expectedInvocations9));
assertTrue(r.containsKey(k));
// no load
assertEquals(k.toString(), r.get(k, expectedInvocations9));
// Go critical in accessor
getCache().getLoggerI18n().fine(addExpectedExString);
// usage
long newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));
// above
// critical
// by 10%
assertTrue(newfakeHeapUsage > 0);
assertTrue(newfakeHeapUsage <= fakeHeapMaxSize);
hmm.updateStateAndSendEvent(newfakeHeapUsage);
getCache().getLoggerI18n().fine(removeExpectedExString);
assertTrue(hmm.getState().isCritical());
k = new Integer(5);
Integer expectedInvocations10 = new Integer(expectedInvocations.incrementAndGet());
// load for key 5
assertEquals(k.toString(), r.get(k, expectedInvocations10));
assertTrue(r.containsKey(k));
// no load
assertEquals(k.toString(), r.get(k, expectedInvocations10));
// Clean up critical state
// below
newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.3f));
// critical by
// 30%
assertTrue(fakeHeapMaxSize > 0);
getCache().getLoggerI18n().fine(addExpectedBelow);
hmm.updateStateAndSendEvent(newfakeHeapUsage);
getCache().getLoggerI18n().fine(removeExpectedBelow);
assertFalse(hmm.getState().isCritical());
return expectedInvocations10;
}
});
accessor.invoke(removeExpectedException);
ds1.invoke(removeExpectedException);
}
use of org.apache.geode.internal.cache.control.HeapMemoryMonitor in project geode by apache.
the class MemoryThresholdsDUnitTest method testEvictionMemoryEventTolerance.
@Test
public void testEvictionMemoryEventTolerance() {
final Host host = Host.getHost(0);
final VM vm = host.getVM(0);
vm.invoke(new SerializableCallable() {
public Object call() throws Exception {
HeapMemoryMonitor.setTestDisableMemoryUpdates(false);
String vendor = System.getProperty("java.vendor");
boolean isSun = (vendor.contains("Sun") || vendor.contains("Oracle"));
int defaultTolerance = isSun ? 1 : 5;
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
InternalResourceManager irm = cache.getInternalResourceManager();
HeapMemoryMonitor hmm = irm.getHeapMonitor();
hmm.setTestMaxMemoryBytes(100);
HeapMemoryMonitor.setTestBytesUsedForThresholdSet(1);
irm.setEvictionHeapPercentage(50);
for (int i = 0; i < defaultTolerance; i++) {
hmm.updateStateAndSendEvent(55);
assertFalse(hmm.getState().isEviction());
}
hmm.updateStateAndSendEvent(55);
assertTrue(hmm.getState().isEviction());
hmm.updateStateAndSendEvent(45);
assertFalse(hmm.getState().isEviction());
HeapMemoryMonitor.setTestDisableMemoryUpdates(true);
return null;
}
});
}
use of org.apache.geode.internal.cache.control.HeapMemoryMonitor 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);
}
use of org.apache.geode.internal.cache.control.HeapMemoryMonitor in project geode by apache.
the class EvictionStatsDUnitTest method testMemLRUEvictionNDestroyNNumOverflowOnDiskCount.
@Test
public void testMemLRUEvictionNDestroyNNumOverflowOnDiskCount() {
// Ignore this excetion as this can happen if pool is shutting down
IgnoredException.addIgnoredException(java.util.concurrent.RejectedExecutionException.class.getName());
int localMaxMem = 50;
final int extraEntries = 6;
prepareScenario(EvictionAlgorithm.LRU_MEMORY);
putData("PR1", (2 * localMaxMem) + extraEntries);
putData("PR2", (2 * localMaxMem) + extraEntries);
dataStore1.invoke(new CacheSerializableRunnable("testEvictionCount") {
@Override
public void run2() throws CacheException {
PartitionedRegion pr1 = (PartitionedRegion) cache.getRegion("PR1");
LogWriterUtils.getLogWriter().info("dddd local" + pr1.getLocalMaxMemory());
LogWriterUtils.getLogWriter().info("dddd local evi" + ((AbstractLRURegionMap) pr1.entries)._getLruList().stats().getEvictions());
LogWriterUtils.getLogWriter().info("dddd local entries" + ((AbstractLRURegionMap) pr1.entries)._getLruList().stats().getCounter() / (1024 * 1024));
HeapMemoryMonitor hmm = ((InternalResourceManager) cache.getResourceManager()).getHeapMonitor();
long memused = hmm.getBytesUsed() / (1024 * 1024);
LogWriterUtils.getLogWriter().info("dddd local memused= " + memused);
assertTrue(((AbstractLRURegionMap) pr1.entries)._getLruList().stats().getEvictions() >= extraEntries / 2);
assertEquals(((AbstractLRURegionMap) pr1.entries)._getLruList().stats().getDestroys(), ((AbstractLRURegionMap) pr1.entries)._getLruList().stats().getEvictions());
PartitionedRegion pr2 = (PartitionedRegion) cache.getRegion("PR2");
assertTrue(((AbstractLRURegionMap) pr2.entries)._getLruList().stats().getEvictions() >= extraEntries / 2);
assertEquals(((AbstractLRURegionMap) pr2.entries)._getLruList().stats().getDestroys(), 0);
assertTrue(pr2.getDiskRegionStats().getNumOverflowOnDisk() >= extraEntries / 2);
}
});
dataStore2.invoke(new CacheSerializableRunnable("testEvictionCount") {
@Override
public void run2() throws CacheException {
PartitionedRegion pr1 = (PartitionedRegion) cache.getRegion("PR1");
assertTrue(((AbstractLRURegionMap) pr1.entries)._getLruList().stats().getEvictions() >= extraEntries / 2);
assertEquals(((AbstractLRURegionMap) pr1.entries)._getLruList().stats().getDestroys(), ((AbstractLRURegionMap) pr1.entries)._getLruList().stats().getEvictions());
PartitionedRegion pr2 = (PartitionedRegion) cache.getRegion("PR2");
assertTrue(((AbstractLRURegionMap) pr2.entries)._getLruList().stats().getEvictions() >= extraEntries / 2);
assertEquals(((AbstractLRURegionMap) pr2.entries)._getLruList().stats().getDestroys(), 0);
assertTrue(pr2.getDiskRegionStats().getNumOverflowOnDisk() >= extraEntries / 2);
}
});
}
use of org.apache.geode.internal.cache.control.HeapMemoryMonitor in project geode by apache.
the class PartitionedRegionEvictionDUnitTest method raiseFakeNotification.
protected void raiseFakeNotification() {
((GemFireCacheImpl) getCache()).getHeapEvictor().testAbortAfterLoopCount = 1;
HeapMemoryMonitor.setTestDisableMemoryUpdates(true);
System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "memoryEventTolerance", "0");
setEvictionPercentage(85);
HeapMemoryMonitor hmm = ((GemFireCacheImpl) getCache()).getInternalResourceManager().getHeapMonitor();
hmm.setTestMaxMemoryBytes(100);
hmm.updateStateAndSendEvent(90);
}
Aggregations