Search in sources :

Example 6 with InternalResourceManager

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);
                }
            }
        }
    });
}
Also used : OffHeapMemoryMonitor(org.apache.geode.internal.cache.control.OffHeapMemoryMonitor) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VM(org.apache.geode.test.dunit.VM) CacheLoader(org.apache.geode.cache.CacheLoader) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 7 with InternalResourceManager

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);
}
Also used : ResourceListener(org.apache.geode.internal.cache.control.ResourceListener) ResourceEvent(org.apache.geode.internal.cache.control.ResourceEvent) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) ResourceManager(org.apache.geode.cache.control.ResourceManager) PartitionRegionInfo(org.apache.geode.cache.partition.PartitionRegionInfo) Cache(org.apache.geode.cache.Cache) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 8 with InternalResourceManager

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);
                }
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VM(org.apache.geode.test.dunit.VM) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 9 with InternalResourceManager

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);
        }
    });
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager)

Example 10 with InternalResourceManager

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());
        }
    };
}
Also used : HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem)

Aggregations

InternalResourceManager (org.apache.geode.internal.cache.control.InternalResourceManager)30 LowMemoryException (org.apache.geode.cache.LowMemoryException)12 HeapMemoryMonitor (org.apache.geode.internal.cache.control.HeapMemoryMonitor)11 Test (org.junit.Test)11 Host (org.apache.geode.test.dunit.Host)10 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)10 VM (org.apache.geode.test.dunit.VM)10 CacheException (org.apache.geode.cache.CacheException)9 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)9 FunctionException (org.apache.geode.cache.execute.FunctionException)9 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)9 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)9 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AttributesFactory (org.apache.geode.cache.AttributesFactory)8 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)8 ServerOperationException (org.apache.geode.cache.client.ServerOperationException)7 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)7 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)7 Region (org.apache.geode.cache.Region)6