Search in sources :

Example 1 with HeapMemoryMonitor

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

the class ExecuteFunction65 method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection servConn, long start) throws IOException {
    Object function = null;
    Object args = null;
    MemberMappedArgument memberMappedArg = null;
    byte hasResult = 0;
    byte functionState = 0;
    boolean isReexecute = false;
    try {
        functionState = clientMessage.getPart(0).getSerializedForm()[0];
        if (functionState == AbstractExecution.HA_HASRESULT_NO_OPTIMIZEFORWRITE_REEXECUTE) {
            functionState = AbstractExecution.HA_HASRESULT_NO_OPTIMIZEFORWRITE;
            isReexecute = true;
        } else if (functionState == AbstractExecution.HA_HASRESULT_OPTIMIZEFORWRITE_REEXECUTE) {
            functionState = AbstractExecution.HA_HASRESULT_OPTIMIZEFORWRITE;
            isReexecute = true;
        }
        if (functionState != 1) {
            hasResult = (byte) ((functionState & 2) - 1);
        } else {
            hasResult = functionState;
        }
        if (hasResult == 1) {
            servConn.setAsTrue(REQUIRES_RESPONSE);
            servConn.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
        }
        function = clientMessage.getPart(1).getStringOrObject();
        args = clientMessage.getPart(2).getObject();
        Part part = clientMessage.getPart(3);
        if (part != null) {
            memberMappedArg = (MemberMappedArgument) part.getObject();
        }
    } catch (ClassNotFoundException exception) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), exception);
        if (hasResult == 1) {
            writeChunkedException(clientMessage, exception, servConn);
            servConn.setAsTrue(RESPONDED);
            return;
        }
    }
    if (function == null) {
        final String message = LocalizedStrings.ExecuteFunction_THE_INPUT_FUNCTION_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL.toLocalizedString();
        logger.warn("{}: {}", servConn.getName(), message);
        sendError(hasResult, clientMessage, message, servConn);
        return;
    }
    // Execute function on the cache
    try {
        Function functionObject = null;
        if (function instanceof String) {
            functionObject = FunctionService.getFunction((String) function);
            if (functionObject == null) {
                final String message = LocalizedStrings.ExecuteFunction_FUNCTION_NAMED_0_IS_NOT_REGISTERED.toLocalizedString(function);
                logger.warn("{}: {}", servConn.getName(), message);
                sendError(hasResult, clientMessage, message, servConn);
                return;
            } else {
                byte functionStateOnServerSide = AbstractExecution.getFunctionState(functionObject.isHA(), functionObject.hasResult(), functionObject.optimizeForWrite());
                if (logger.isDebugEnabled()) {
                    logger.debug("Function State on server side: {} on client: {}", functionStateOnServerSide, functionState);
                }
                if (functionStateOnServerSide != functionState) {
                    String message = LocalizedStrings.FunctionService_FUNCTION_ATTRIBUTE_MISMATCH_CLIENT_SERVER.toLocalizedString(function);
                    logger.warn("{}: {}", servConn.getName(), message);
                    sendError(hasResult, clientMessage, message, servConn);
                    return;
                }
            }
        } else {
            functionObject = (Function) function;
        }
        FunctionStats stats = FunctionStats.getFunctionStats(functionObject.getId());
        this.securityService.authorizeDataWrite();
        // check if the caller is authorized to do this operation on server
        AuthorizeRequest authzRequest = servConn.getAuthzRequest();
        ExecuteFunctionOperationContext executeContext = null;
        if (authzRequest != null) {
            executeContext = authzRequest.executeFunctionAuthorize(functionObject.getId(), null, null, args, functionObject.optimizeForWrite());
        }
        ChunkedMessage m = servConn.getFunctionResponseMessage();
        m.setTransactionId(clientMessage.getTransactionId());
        ResultSender resultSender = new ServerToClientFunctionResultSender65(m, MessageType.EXECUTE_FUNCTION_RESULT, servConn, functionObject, executeContext);
        InternalDistributedMember localVM = (InternalDistributedMember) servConn.getCache().getDistributedSystem().getDistributedMember();
        FunctionContext context = null;
        if (memberMappedArg != null) {
            context = new FunctionContextImpl(functionObject.getId(), memberMappedArg.getArgumentsForMember(localVM.getId()), resultSender, isReexecute);
        } else {
            context = new FunctionContextImpl(functionObject.getId(), args, resultSender, isReexecute);
        }
        HandShake handShake = (HandShake) servConn.getHandshake();
        int earlierClientReadTimeout = handShake.getClientReadTimeout();
        handShake.setClientReadTimeout(0);
        try {
            long startExecution = stats.startTime();
            stats.startFunctionExecution(functionObject.hasResult());
            if (logger.isDebugEnabled()) {
                logger.debug("Executing Function on Server: {} with context: {}", servConn, context);
            }
            InternalCache cache = servConn.getCache();
            HeapMemoryMonitor hmm = ((InternalResourceManager) cache.getResourceManager()).getHeapMonitor();
            if (functionObject.optimizeForWrite() && cache != null && hmm.getState().isCritical() && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
                Set<DistributedMember> sm = Collections.singleton((DistributedMember) cache.getMyId());
                Exception e = new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(new Object[] { functionObject.getId(), sm }), sm);
                sendException(hasResult, clientMessage, e.getMessage(), servConn, e);
                return;
            }
            functionObject.execute(context);
            if (!((ServerToClientFunctionResultSender65) resultSender).isLastResultReceived() && functionObject.hasResult()) {
                throw new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(functionObject.getId()));
            }
            stats.endFunctionExecution(startExecution, functionObject.hasResult());
        } catch (FunctionException functionException) {
            stats.endFunctionExecutionWithException(functionObject.hasResult());
            throw functionException;
        } catch (Exception exception) {
            stats.endFunctionExecutionWithException(functionObject.hasResult());
            throw new FunctionException(exception);
        } finally {
            handShake.setClientReadTimeout(earlierClientReadTimeout);
        }
    } catch (IOException ioException) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), ioException);
        String message = LocalizedStrings.ExecuteFunction_SERVER_COULD_NOT_SEND_THE_REPLY.toLocalizedString();
        sendException(hasResult, clientMessage, message, servConn, ioException);
    } catch (InternalFunctionInvocationTargetException internalfunctionException) {
        // 2> in case of HA member departed
        if (logger.isDebugEnabled()) {
            logger.debug(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, new Object[] { function }), internalfunctionException);
        }
        final String message = internalfunctionException.getMessage();
        sendException(hasResult, clientMessage, message, servConn, internalfunctionException);
    } catch (Exception e) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), e);
        final String message = e.getMessage();
        sendException(hasResult, clientMessage, message, servConn, e);
    }
}
Also used : AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) InternalCache(org.apache.geode.internal.cache.InternalCache) ResultSender(org.apache.geode.cache.execute.ResultSender) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) Function(org.apache.geode.cache.execute.Function) HandShake(org.apache.geode.internal.cache.tier.sockets.HandShake) MemberMappedArgument(org.apache.geode.internal.cache.execute.MemberMappedArgument) FunctionStats(org.apache.geode.internal.cache.execute.FunctionStats) LowMemoryException(org.apache.geode.cache.LowMemoryException) FunctionContextImpl(org.apache.geode.internal.cache.execute.FunctionContextImpl) ExecuteFunctionOperationContext(org.apache.geode.cache.operations.ExecuteFunctionOperationContext) ServerToClientFunctionResultSender65(org.apache.geode.internal.cache.execute.ServerToClientFunctionResultSender65) FunctionException(org.apache.geode.cache.execute.FunctionException) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) IOException(java.io.IOException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) FunctionException(org.apache.geode.cache.execute.FunctionException) LowMemoryException(org.apache.geode.cache.LowMemoryException) IOException(java.io.IOException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) Part(org.apache.geode.internal.cache.tier.sockets.Part) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) ChunkedMessage(org.apache.geode.internal.cache.tier.sockets.ChunkedMessage)

Example 2 with HeapMemoryMonitor

use of org.apache.geode.internal.cache.control.HeapMemoryMonitor 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 3 with HeapMemoryMonitor

use of org.apache.geode.internal.cache.control.HeapMemoryMonitor 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 4 with HeapMemoryMonitor

use of org.apache.geode.internal.cache.control.HeapMemoryMonitor 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)

Example 5 with HeapMemoryMonitor

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

the class EvictionDUnitTest method raiseFakeNotification.

protected void raiseFakeNotification() {
    ((GemFireCacheImpl) getCache()).getHeapEvictor().testAbortAfterLoopCount = 1;
    HeapMemoryMonitor.setTestDisableMemoryUpdates(true);
    System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "memoryEventTolerance", "0");
    getCache().getResourceManager().setEvictionHeapPercentage(EVICTION_HEAP_PERCENTAGE_FAKE_NOTIFICATION);
    HeapMemoryMonitor heapMemoryMonitor = ((GemFireCacheImpl) getCache()).getInternalResourceManager().getHeapMonitor();
    heapMemoryMonitor.setTestMaxMemoryBytes(TEST_MAX_MEMORY);
    heapMemoryMonitor.updateStateAndSendEvent(MEMORY_USED_FAKE_NOTIFICATION);
}
Also used : GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor)

Aggregations

HeapMemoryMonitor (org.apache.geode.internal.cache.control.HeapMemoryMonitor)15 InternalResourceManager (org.apache.geode.internal.cache.control.InternalResourceManager)11 LowMemoryException (org.apache.geode.cache.LowMemoryException)8 FunctionException (org.apache.geode.cache.execute.FunctionException)8 CacheException (org.apache.geode.cache.CacheException)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)5 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)5 Host (org.apache.geode.test.dunit.Host)5 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)5 VM (org.apache.geode.test.dunit.VM)5 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)5 Test (org.junit.Test)5 Region (org.apache.geode.cache.Region)4 ServerOperationException (org.apache.geode.cache.client.ServerOperationException)4 FunctionContext (org.apache.geode.cache.execute.FunctionContext)4 DistributedMember (org.apache.geode.distributed.DistributedMember)4 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)4 DistributedRegion (org.apache.geode.internal.cache.DistributedRegion)4