use of org.apache.geode.internal.cache.control.InternalResourceManager 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.internal.cache.control.InternalResourceManager in project geode by apache.
the class ResourceManagerDUnitTest method testResourceManagerBasics.
/**
* Creates a cache in the controller and exercises all methods on the ResourceManager without
* having any partitioned regions defined.
*/
@Test
public void testResourceManagerBasics() {
Cache cache = getCache();
// verify that getResourceManager works
ResourceManager manager = cache.getResourceManager();
assertNotNull(manager);
// verify that getPartitionedRegionDetails returns empty set
Set<PartitionRegionInfo> detailsSet = PartitionRegionHelper.getPartitionRegionInfo(cache);
assertNotNull(detailsSet);
assertEquals(Collections.emptySet(), detailsSet);
ResourceListener listener = new ResourceListener() {
public void onEvent(ResourceEvent event) {
}
};
InternalResourceManager internalManager = (InternalResourceManager) manager;
// verify that addResourceListener works
internalManager.addResourceListener(listener);
Set<ResourceListener> listeners = internalManager.getResourceListeners(ResourceType.HEAP_MEMORY);
assertNotNull(listeners);
assertEquals(1 + SYSTEM_LISTENERS, listeners.size());
assertTrue(listeners.contains(listener));
// verify that repeat adds result in only one entry of the listener
internalManager.addResourceListener(ResourceType.HEAP_MEMORY, listener);
listeners = internalManager.getResourceListeners(ResourceType.HEAP_MEMORY);
assertEquals(1 + SYSTEM_LISTENERS, listeners.size());
// verify that removeResourceListener works
internalManager.removeResourceListener(listener);
listeners = internalManager.getResourceListeners(ResourceType.HEAP_MEMORY);
assertEquals(listeners.size(), SYSTEM_LISTENERS);
}
use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.
the class MemoryThresholdsDUnitTest 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
*
* @throws Exception
*/
@Test
public void testLRLoadRejection() throws Exception {
final Host host = Host.getHost(0);
final VM vm = host.getVM(2);
final String rName = getUniqueName();
final float criticalHeapThresh = 0.90f;
final int fakeHeapMaxSize = 1000;
vm.invoke(() -> disconnectFromDS());
vm.invoke(new CacheSerializableRunnable("test LocalRegion load passthrough when critical") {
@Override
public void run2() throws CacheException {
InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
HeapMemoryMonitor hmm = irm.getHeapMonitor();
// below
long fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.5f));
// critical
// by 50%
assertTrue(fakeHeapMaxSize > 0);
irm.getHeapMonitor().setTestMaxMemoryBytes(fakeHeapMaxSize);
HeapMemoryMonitor.setTestBytesUsedForThresholdSet(fakeHeapUsage);
irm.setCriticalHeapPercentage((criticalHeapThresh * 100.0f));
AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
af.setScope(Scope.LOCAL);
final AtomicInteger numLoaderInvocations = new AtomicInteger();
af.setCacheLoader(new CacheLoader<Integer, String>() {
public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
numLoaderInvocations.incrementAndGet();
return helper.getKey().toString();
}
public void close() {
}
});
Region<Integer, String> r = getCache().createRegion(rName, af.create());
assertFalse(hmm.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);
// usage above
fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));
// critical by
// 10%
assertTrue(fakeHeapUsage > 0);
assertTrue(fakeHeapUsage <= fakeHeapMaxSize);
hmm.updateStateAndSendEvent(fakeHeapUsage);
getCache().getLoggerI18n().fine(removeExpectedExString);
assertTrue(hmm.getState().isCritical());
{
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());
// below critical
fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.3f));
// by 30%
assertTrue(fakeHeapMaxSize > 0);
getCache().getLoggerI18n().fine(addExpectedBelow);
hmm.updateStateAndSendEvent(fakeHeapUsage);
getCache().getLoggerI18n().fine(removeExpectedBelow);
assertFalse(hmm.getState().isCritical());
{
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.internal.cache.control.InternalResourceManager in project geode by apache.
the class MemoryThresholdsDUnitTest method startCacheServer.
/**
* Starts up a CacheServer.
*
* @return a {@link ServerPorts} containing the CacheServer ports.
*/
private ServerPorts startCacheServer(VM server, final float evictionThreshold, final float criticalThreshold, final String regionName, final boolean createPR, final boolean notifyBySubscription, final int prRedundancy) throws Exception {
return (ServerPorts) server.invoke(new SerializableCallable() {
public Object call() throws Exception {
getSystem(getServerProperties());
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
InternalResourceManager irm = cache.getInternalResourceManager();
HeapMemoryMonitor hmm = irm.getHeapMonitor();
hmm.setTestMaxMemoryBytes(1000);
HeapMemoryMonitor.setTestBytesUsedForThresholdSet(500);
irm.setEvictionHeapPercentage(evictionThreshold);
irm.setCriticalHeapPercentage(criticalThreshold);
AttributesFactory factory = new AttributesFactory();
if (createPR) {
PartitionAttributesFactory paf = new PartitionAttributesFactory();
paf.setRedundantCopies(prRedundancy);
paf.setTotalNumBuckets(11);
factory.setPartitionAttributes(paf.create());
} else {
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
}
Region region = createRegion(regionName, factory.create());
if (createPR) {
assertTrue(region instanceof PartitionedRegion);
} else {
assertTrue(region instanceof DistributedRegion);
}
CacheServer cacheServer = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailableTCPPorts(1)[0];
cacheServer.setPort(port);
cacheServer.setNotifyBySubscription(notifyBySubscription);
cacheServer.start();
return new ServerPorts(port);
}
});
}
use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.
the class MemoryThresholdsDUnitTest method createPR.
private CacheSerializableRunnable createPR(final String rName, final boolean accessor, final int fakeHeapMaxSize, final float criticalHeapThresh) {
return new CacheSerializableRunnable("create PR accessor") {
@Override
public void run2() throws CacheException {
// Assert some level of connectivity
InternalDistributedSystem ds = getSystem();
assertTrue(ds.getDistributionManager().getNormalDistributionManagerIds().size() >= 2);
// below
final long fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.5f));
// critical
// by
// 50%
InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
HeapMemoryMonitor hmm = irm.getHeapMonitor();
assertTrue(fakeHeapMaxSize > 0);
hmm.setTestMaxMemoryBytes(fakeHeapMaxSize);
HeapMemoryMonitor.setTestBytesUsedForThresholdSet(fakeHeapUsage);
irm.setCriticalHeapPercentage((criticalHeapThresh * 100.0f));
assertFalse(hmm.getState().isCritical());
AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
if (!accessor) {
af.setCacheLoader(new CacheLoader<Integer, String>() {
final AtomicInteger numLoaderInvocations = new AtomicInteger();
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());
}
getCache().createRegion(rName, af.create());
}
};
}
Aggregations