Search in sources :

Example 21 with InternalResourceManager

use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.

the class MemoryThresholdsDUnitTest 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.
   * 
   * @throws Exception
   */
@Test
public void testDRLoadRejection() throws Exception {
    final Host host = Host.getHost(0);
    final VM replicate1 = host.getVM(2);
    final VM replicate2 = host.getVM(3);
    final String rName = getUniqueName();
    final float criticalHeapThresh = 0.90f;
    final int fakeHeapMaxSize = 1000;
    // 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") {

        @Override
        public void run2() throws CacheException {
            getSystem();
        }
    };
    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();
            assertTrue(ds.getDistributionManager().getNormalDistributionManagerIds().size() >= 1);
            // 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));
            AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
            af.setScope(Scope.DISTRIBUTED_ACK);
            af.setDataPolicy(DataPolicy.REPLICATE);
            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 {
            Region<Integer, String> r = getCache().getRegion(rName);
            AttributesMutator<Integer, String> am = r.getAttributesMutator();
            am.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() {
                }
            });
            int expectedInvocations = 0;
            HeapMemoryMonitor hmm = ((InternalResourceManager) getCache().getResourceManager()).getHeapMonitor();
            assertFalse(hmm.getState().isCritical());
            {
                Integer k = new Integer(1);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
            }
            // usage
            long newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));
            // above
            // critical
            // by
            // 10%
            assertTrue(newfakeHeapUsage > 0);
            assertTrue(newfakeHeapUsage <= fakeHeapMaxSize);
            hmm.updateStateAndSendEvent(newfakeHeapUsage);
            assertTrue(hmm.getState().isCritical());
            {
                Integer k = new Integer(2);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
            }
            // 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());
            {
                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<Integer, String> 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 {
            Region<Integer, String> r = getCache().getRegion(rName);
            HeapMemoryMonitor hmm = ((InternalResourceManager) getCache().getResourceManager()).getHeapMonitor();
            assertFalse(hmm.getState().isCritical());
            int expectedInvocations = expected.intValue();
            {
                Integer k = new Integer(4);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
                assertFalse(hmm.getState().isCritical());
                assertTrue(r.containsKey(k));
            }
            // Place in a critical state for the next test
            // usage
            long newfakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh + 0.1f));
            // above
            // critical
            // by 10%
            assertTrue(newfakeHeapUsage > 0);
            assertTrue(newfakeHeapUsage <= fakeHeapMaxSize);
            hmm.updateStateAndSendEvent(newfakeHeapUsage);
            assertTrue(hmm.getState().isCritical());
            {
                Integer k = new Integer(5);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
                assertTrue(hmm.getState().isCritical());
                assertFalse(r.containsKey(k));
            }
            // 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());
            {
                Integer k = new Integer(6);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
                assertFalse(hmm.getState().isCritical());
                assertTrue(r.containsKey(k));
            }
            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);
}
Also used : Host(org.apache.geode.test.dunit.Host) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 22 with InternalResourceManager

use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.

the class MemoryThresholdsDUnitTest method testDisabledThresholds.

/**
   * test that disabling threshold does not cause remote event and remote DISABLED events are
   * delivered
   * 
   * @throws Exception
   */
@Test
public void testDisabledThresholds() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final String regionName = "disableThresholdPr";
    ServerPorts ports1 = startCacheServer(server1, 0f, 0f, regionName, true, /* createPR */
    false, /* notifyBySubscription */
    0);
    ServerPorts ports2 = startCacheServer(server2, 0f, 0f, regionName, true, /* createPR */
    false, /* notifyBySubscription */
    0);
    registerTestMemoryThresholdListener(server1);
    registerTestMemoryThresholdListener(server2);
    setUsageAboveEvictionThreshold(server1);
    verifyListenerValue(server1, MemoryState.EVICTION, 0, false);
    verifyListenerValue(server2, MemoryState.EVICTION, 0, true);
    setThresholds(server1, 80f, 0f);
    verifyListenerValue(server1, MemoryState.EVICTION, 1, false);
    verifyListenerValue(server2, MemoryState.EVICTION, 1, true);
    setUsageAboveCriticalThreshold(server1);
    verifyListenerValue(server1, MemoryState.CRITICAL, 0, false);
    verifyListenerValue(server2, MemoryState.CRITICAL, 0, true);
    setThresholds(server1, 0f, 0f);
    verifyListenerValue(server1, MemoryState.EVICTION_DISABLED, 1, false);
    verifyListenerValue(server2, MemoryState.EVICTION_DISABLED, 1, true);
    setThresholds(server1, 0f, 90f);
    verifyListenerValue(server1, MemoryState.CRITICAL, 1, false);
    verifyListenerValue(server2, MemoryState.CRITICAL, 1, true);
    // verify that stats on server2 are not changed by events on server1
    server2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            InternalResourceManager irm = ((GemFireCacheImpl) getCache()).getInternalResourceManager();
            assertEquals(0, irm.getStats().getEvictionStartEvents());
            assertEquals(0, irm.getStats().getHeapCriticalEvents());
            assertEquals(0, irm.getStats().getCriticalThreshold());
            assertEquals(0, irm.getStats().getEvictionThreshold());
            return null;
        }
    });
}
Also used : VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Host(org.apache.geode.test.dunit.Host) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) 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 23 with InternalResourceManager

use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.

the class MemoryThresholdsDUnitTest method testCriticalMemoryEventTolerance.

@Test
public void testCriticalMemoryEventTolerance() {
    final Host host = Host.getHost(0);
    final VM vm = host.getVM(0);
    vm.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            int defaultTolerance = 1;
            HeapMemoryMonitor.setTestDisableMemoryUpdates(false);
            GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
            InternalResourceManager irm = cache.getInternalResourceManager();
            HeapMemoryMonitor hmm = irm.getHeapMonitor();
            hmm.setTestMaxMemoryBytes(100);
            HeapMemoryMonitor.setTestBytesUsedForThresholdSet(1);
            irm.setCriticalHeapPercentage(95);
            for (int i = 0; i < defaultTolerance; i++) {
                hmm.updateStateAndSendEvent(96);
                assertFalse(hmm.getState().isCritical());
            }
            getCache().getLoggerI18n().fine(addExpectedExString);
            hmm.updateStateAndSendEvent(96);
            assertTrue(hmm.getState().isCritical());
            getCache().getLoggerI18n().fine(removeExpectedExString);
            getCache().getLoggerI18n().fine(addExpectedBelow);
            hmm.updateStateAndSendEvent(92);
            getCache().getLoggerI18n().fine(removeExpectedBelow);
            assertFalse(hmm.getState().isCritical());
            HeapMemoryMonitor.setTestDisableMemoryUpdates(true);
            return null;
        }
    });
}
Also used : VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Host(org.apache.geode.test.dunit.Host) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) 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 24 with InternalResourceManager

use of org.apache.geode.internal.cache.control.InternalResourceManager in project geode by apache.

the class MemoryThresholdsDUnitTest 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);
    ServerPorts ports = startCacheServer(server, 0f, 90f, regionName, createPR, false, 0);
    startClient(client, server, ports.getPort(), regionName);
    doPuts(client, regionName, false, /* catchServerException */
    false);
    doPutAlls(client, regionName, false, /* catchServerException */
    false, /* catchLowMemoryException */
    Range.DEFAULT);
    // make the region sick in the server
    server.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
            irm.setCriticalHeapPercentage(90f);
            getCache().getLoggerI18n().fine(addExpectedExString);
            HeapMemoryMonitor.setTestBytesUsedForThresholdSet(950);
            irm.getHeapMonitor().updateStateAndSendEvent();
            getCache().getLoggerI18n().fine(removeExpectedExString);
            return null;
        }
    });
    // 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));
}
Also used : VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Host(org.apache.geode.test.dunit.Host) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager)

Example 25 with InternalResourceManager

use of org.apache.geode.internal.cache.control.InternalResourceManager 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;
        }
    });
}
Also used : VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Host(org.apache.geode.test.dunit.Host) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) 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)

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