Search in sources :

Example 26 with InternalResourceManager

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();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) ProxyBucketRegion(org.apache.geode.internal.cache.ProxyBucketRegion) CacheServer(org.apache.geode.cache.server.CacheServer) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager)

Example 27 with InternalResourceManager

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());
        }
    };
}
Also used : 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) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager)

Example 28 with InternalResourceManager

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);
}
Also used : OffHeapMemoryMonitor(org.apache.geode.internal.cache.control.OffHeapMemoryMonitor) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) IgnoredException(org.apache.geode.test.dunit.IgnoredException) 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) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) 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) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) ProxyBucketRegion(org.apache.geode.internal.cache.ProxyBucketRegion) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 29 with InternalResourceManager

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;
        }
    });
}
Also used : OffHeapMemoryMonitor(org.apache.geode.internal.cache.control.OffHeapMemoryMonitor) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Range(org.apache.geode.cache.management.MemoryThresholdsDUnitTest.Range) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) LowMemoryException(org.apache.geode.cache.LowMemoryException)

Example 30 with InternalResourceManager

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);
}
Also used : MemoryEvent(org.apache.geode.internal.cache.control.MemoryEvent) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager)

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