Search in sources :

Example 11 with InternalFunctionInvocationTargetException

use of org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException in project geode by apache.

the class ExecuteFunctionOp method execute.

/**
   * Does a execute Function on a server using connections from the given pool to communicate with
   * the server.
   * 
   * @param pool the pool to use to communicate with the server.
   * @param function of the function to be executed
   * @param args specified arguments to the application function
   */
public static void execute(final PoolImpl pool, Function function, ServerFunctionExecutor executor, Object args, MemberMappedArgument memberMappedArg, boolean allServers, byte hasResult, ResultCollector rc, boolean isFnSerializationReqd, UserAttributes attributes, String[] groups) {
    final AbstractOp op = new ExecuteFunctionOpImpl(function, args, memberMappedArg, hasResult, rc, isFnSerializationReqd, (byte) 0, groups, allServers, executor.isIgnoreDepartedMembers());
    if (allServers && groups.length == 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("ExecuteFunctionOp#execute : Sending Function Execution Message:{} to all servers using pool: {}", op.getMessage(), pool);
        }
        List callableTasks = constructAndGetFunctionTasks(pool, function, args, memberMappedArg, hasResult, rc, isFnSerializationReqd, attributes);
        SingleHopClientExecutor.submitAll(callableTasks);
    } else {
        boolean reexecuteForServ = false;
        AbstractOp reexecOp = null;
        int retryAttempts = 0;
        boolean reexecute = false;
        int maxRetryAttempts = 0;
        if (function.isHA())
            maxRetryAttempts = pool.getRetryAttempts();
        final boolean isDebugEnabled = logger.isDebugEnabled();
        do {
            try {
                if (reexecuteForServ) {
                    if (isDebugEnabled) {
                        logger.debug("ExecuteFunctionOp#execute.reexecuteForServ : Sending Function Execution Message:{} to server using pool: {} with groups:{} all members:{} ignoreFailedMembers:{}", op.getMessage(), pool, Arrays.toString(groups), allServers, executor.isIgnoreDepartedMembers());
                    }
                    reexecOp = new ExecuteFunctionOpImpl(function, args, memberMappedArg, hasResult, rc, isFnSerializationReqd, (byte) 1, /* isReExecute */
                    groups, allServers, executor.isIgnoreDepartedMembers());
                    pool.execute(reexecOp, 0);
                } else {
                    if (isDebugEnabled) {
                        logger.debug("ExecuteFunctionOp#execute : Sending Function Execution Message:{} to server using pool: {} with groups:{} all members:{} ignoreFailedMembers:{}", op.getMessage(), pool, Arrays.toString(groups), allServers, executor.isIgnoreDepartedMembers());
                    }
                    pool.execute(op, 0);
                }
                reexecute = false;
                reexecuteForServ = false;
            } catch (InternalFunctionInvocationTargetException e) {
                if (isDebugEnabled) {
                    logger.debug("ExecuteFunctionOp#execute : Received InternalFunctionInvocationTargetException. The failed node is {}", e.getFailedNodeSet());
                }
                reexecute = true;
                rc.clearResults();
            } catch (ServerConnectivityException se) {
                retryAttempts++;
                if (isDebugEnabled) {
                    logger.debug("ExecuteFunctionOp#execute : Received ServerConnectivityException. The exception is {} The retryAttempt is : {} maxRetryAttempts  {}", se, retryAttempts, maxRetryAttempts);
                }
                if (se instanceof ServerOperationException) {
                    throw se;
                }
                if ((retryAttempts > maxRetryAttempts && maxRetryAttempts != -1))
                    throw se;
                reexecuteForServ = true;
                rc.clearResults();
            }
        } while (reexecuteForServ);
        if (reexecute && function.isHA()) {
            ExecuteFunctionOp.reexecute(pool, function, executor, rc, hasResult, isFnSerializationReqd, maxRetryAttempts - 1, groups, allServers);
        }
    }
}
Also used : ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) ServerOperationException(org.apache.geode.cache.client.ServerOperationException)

Example 12 with InternalFunctionInvocationTargetException

use of org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException in project geode by apache.

the class ExecuteFunction66 method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection servConn, long start) throws IOException {
    Object function = null;
    Object args = null;
    MemberMappedArgument memberMappedArg = null;
    String[] groups = null;
    byte hasResult = 0;
    byte functionState = 0;
    boolean isReexecute = false;
    boolean allMembers = false;
    boolean ignoreFailedMembers = false;
    int functionTimeout = ConnectionImpl.DEFAULT_CLIENT_FUNCTION_TIMEOUT;
    try {
        byte[] bytes = clientMessage.getPart(0).getSerializedForm();
        functionState = bytes[0];
        if (bytes.length >= 5 && servConn.getClientVersion().ordinal() >= Version.GFE_8009.ordinal()) {
            functionTimeout = Part.decodeInt(bytes, 1);
        }
        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();
        }
        groups = getGroups(clientMessage);
        allMembers = getAllMembers(clientMessage);
        ignoreFailedMembers = getIgnoreFailedMembers(clientMessage);
    } 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);
        } else {
            writeException(clientMessage, exception, false, 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(LocalizedMessage.create(LocalizedStrings.TWO_ARG_COLON, new Object[] { 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());
        ServerToClientFunctionResultSender 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(functionTimeout);
        try {
            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;
            }
            /**
         * if cache is null, then either cache has not yet been created on this node or it is a
         * shutdown scenario.
         */
            DM dm = null;
            if (cache != null) {
                dm = cache.getDistributionManager();
            }
            if (groups != null && groups.length > 0) {
                executeFunctionOnGroups(function, args, groups, allMembers, functionObject, resultSender, ignoreFailedMembers);
            } else {
                executeFunctionaLocally(functionObject, context, (ServerToClientFunctionResultSender65) resultSender, dm, stats);
            }
            if (!functionObject.hasResult()) {
                writeReply(clientMessage, servConn);
            }
        } 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) {
        // 4> 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) DM(org.apache.geode.distributed.internal.DM) 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) ServerToClientFunctionResultSender(org.apache.geode.internal.cache.execute.ServerToClientFunctionResultSender) ChunkedMessage(org.apache.geode.internal.cache.tier.sockets.ChunkedMessage)

Example 13 with InternalFunctionInvocationTargetException

use of org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException in project geode by apache.

the class ExecuteRegionFunction method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection servConn, long start) throws IOException {
    String regionName = null;
    Object function = null;
    Object args = null;
    MemberMappedArgument memberMappedArg = null;
    Set filter = null;
    byte hasResult = 0;
    int filterSize = 0, partNumber = 0;
    CachedRegionHelper crHelper = servConn.getCachedRegionHelper();
    try {
        hasResult = clientMessage.getPart(0).getSerializedForm()[0];
        if (hasResult == 1) {
            servConn.setAsTrue(REQUIRES_RESPONSE);
            servConn.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
        }
        regionName = clientMessage.getPart(1).getString();
        function = clientMessage.getPart(2).getStringOrObject();
        args = clientMessage.getPart(3).getObject();
        Part part = clientMessage.getPart(4);
        if (part != null) {
            Object obj = part.getObject();
            if (obj instanceof MemberMappedArgument) {
                memberMappedArg = (MemberMappedArgument) obj;
            }
        }
        filterSize = clientMessage.getPart(5).getInt();
        if (filterSize != 0) {
            filter = new HashSet();
            partNumber = 6;
            for (int i = 0; i < filterSize; i++) {
                filter.add(clientMessage.getPart(partNumber + i).getStringOrObject());
            }
        }
    } catch (ClassNotFoundException exception) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), exception);
        if (hasResult == 1) {
            writeChunkedException(clientMessage, exception, servConn);
            servConn.setAsTrue(RESPONDED);
            return;
        }
    }
    if (function == null || regionName == null) {
        String message = null;
        if (function == null) {
            message = LocalizedStrings.ExecuteRegionFunction_THE_INPUT_0_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL.toLocalizedString("function");
        }
        if (regionName == null) {
            message = LocalizedStrings.ExecuteRegionFunction_THE_INPUT_0_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL.toLocalizedString("region");
        }
        logger.warn("{}: {}", servConn.getName(), message);
        sendError(hasResult, clientMessage, message, servConn);
        return;
    }
    Region region = crHelper.getRegion(regionName);
    if (region == null) {
        String message = LocalizedStrings.ExecuteRegionFunction_THE_REGION_NAMED_0_WAS_NOT_FOUND_DURING_EXECUTE_FUNCTION_REQUEST.toLocalizedString(regionName);
        logger.warn("{}: {}", servConn.getName(), message);
        sendError(hasResult, clientMessage, message, servConn);
        return;
    }
    HandShake handShake = (HandShake) servConn.getHandshake();
    int earlierClientReadTimeout = handShake.getClientReadTimeout();
    handShake.setClientReadTimeout(0);
    ServerToClientFunctionResultSender resultSender = null;
    Function functionObject = null;
    try {
        if (function instanceof String) {
            functionObject = FunctionService.getFunction((String) function);
            if (functionObject == null) {
                String message = LocalizedStrings.ExecuteRegionFunction_THE_FUNCTION_0_HAS_NOT_BEEN_REGISTERED.toLocalizedString(function);
                logger.warn("{}: {}", servConn.getName(), message);
                sendError(hasResult, clientMessage, message, servConn);
                return;
            }
        } else {
            functionObject = (Function) function;
        }
        this.securityService.authorizeDataWrite();
        // check if the caller is authorized to do this operation on server
        AuthorizeRequest authzRequest = servConn.getAuthzRequest();
        final String functionName = functionObject.getId();
        final String regionPath = region.getFullPath();
        ExecuteFunctionOperationContext executeContext = null;
        if (authzRequest != null) {
            executeContext = authzRequest.executeFunctionAuthorize(functionName, regionPath, filter, args, functionObject.optimizeForWrite());
        }
        // Construct execution
        AbstractExecution execution = (AbstractExecution) FunctionService.onRegion(region);
        ChunkedMessage m = servConn.getFunctionResponseMessage();
        m.setTransactionId(clientMessage.getTransactionId());
        resultSender = new ServerToClientFunctionResultSender(m, MessageType.EXECUTE_REGION_FUNCTION_RESULT, servConn, functionObject, executeContext);
        if (execution instanceof PartitionedRegionFunctionExecutor) {
            execution = new PartitionedRegionFunctionExecutor((PartitionedRegion) region, filter, args, memberMappedArg, resultSender, null, false);
        } else {
            execution = new DistributedRegionFunctionExecutor((DistributedRegion) region, filter, args, memberMappedArg, resultSender);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Executing Function: {} on Server: {} with Execution: {}", functionObject.getId(), servConn, execution);
        }
        if (hasResult == 1) {
            if (function instanceof String) {
                execution.execute((String) function).getResult();
            } else {
                execution.execute(functionObject).getResult();
            }
        } else {
            if (function instanceof String) {
                execution.execute((String) function);
            } else {
                execution.execute(functionObject);
            }
        }
    } catch (IOException ioe) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), ioe);
        final String message = LocalizedStrings.ExecuteRegionFunction_SERVER_COULD_NOT_SEND_THE_REPLY.toLocalizedString();
        sendException(hasResult, clientMessage, message, servConn, ioe);
    } catch (InternalFunctionInvocationTargetException internalfunctionException) {
        // 4> 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 (FunctionException fe) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), fe);
        String message = fe.getMessage();
        sendException(hasResult, clientMessage, message, servConn, fe);
    } catch (Exception e) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteRegionFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), e);
        String message = e.getMessage();
        sendException(hasResult, clientMessage, message, servConn, e);
    } finally {
        handShake.setClientReadTimeout(earlierClientReadTimeout);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) Function(org.apache.geode.cache.execute.Function) HandShake(org.apache.geode.internal.cache.tier.sockets.HandShake) MemberMappedArgument(org.apache.geode.internal.cache.execute.MemberMappedArgument) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) HashSet(java.util.HashSet) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) PartitionedRegionFunctionExecutor(org.apache.geode.internal.cache.execute.PartitionedRegionFunctionExecutor) ExecuteFunctionOperationContext(org.apache.geode.cache.operations.ExecuteFunctionOperationContext) FunctionException(org.apache.geode.cache.execute.FunctionException) IOException(java.io.IOException) DistributedRegionFunctionExecutor(org.apache.geode.internal.cache.execute.DistributedRegionFunctionExecutor) FunctionException(org.apache.geode.cache.execute.FunctionException) IOException(java.io.IOException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) Part(org.apache.geode.internal.cache.tier.sockets.Part) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ServerToClientFunctionResultSender(org.apache.geode.internal.cache.execute.ServerToClientFunctionResultSender) ChunkedMessage(org.apache.geode.internal.cache.tier.sockets.ChunkedMessage)

Example 14 with InternalFunctionInvocationTargetException

use of org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException in project geode by apache.

the class ExecuteRegionFunction65 method writeFunctionResponseException.

protected static void writeFunctionResponseException(Message origMsg, int messageType, String message, ServerConnection servConn, Throwable e) throws IOException {
    ChunkedMessage functionResponseMsg = servConn.getFunctionResponseMessage();
    ChunkedMessage chunkedResponseMsg = servConn.getChunkedResponseMessage();
    int numParts = 0;
    if (functionResponseMsg.headerHasBeenSent()) {
        if (e instanceof FunctionException && e.getCause() instanceof InternalFunctionInvocationTargetException) {
            functionResponseMsg.setNumberOfParts(3);
            functionResponseMsg.addObjPart(e);
            functionResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
            InternalFunctionInvocationTargetException fe = (InternalFunctionInvocationTargetException) e.getCause();
            functionResponseMsg.addObjPart(fe.getFailedNodeSet());
            numParts = 3;
        } else {
            functionResponseMsg.setNumberOfParts(2);
            functionResponseMsg.addObjPart(e);
            functionResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
            numParts = 2;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("{}: Sending exception chunk while reply in progress: ", servConn.getName(), e);
        }
        functionResponseMsg.setServerConnection(servConn);
        functionResponseMsg.setLastChunkAndNumParts(true, numParts);
        // functionResponseMsg.setLastChunk(true);
        functionResponseMsg.sendChunk(servConn);
    } else {
        chunkedResponseMsg.setMessageType(messageType);
        chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
        chunkedResponseMsg.sendHeader();
        if (e instanceof FunctionException && e.getCause() instanceof InternalFunctionInvocationTargetException) {
            chunkedResponseMsg.setNumberOfParts(3);
            chunkedResponseMsg.addObjPart(e);
            chunkedResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
            InternalFunctionInvocationTargetException fe = (InternalFunctionInvocationTargetException) e.getCause();
            chunkedResponseMsg.addObjPart(fe.getFailedNodeSet());
            numParts = 3;
        } else {
            chunkedResponseMsg.setNumberOfParts(2);
            chunkedResponseMsg.addObjPart(e);
            chunkedResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
            numParts = 2;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("{}: Sending exception chunk: ", servConn.getName(), e);
        }
        chunkedResponseMsg.setServerConnection(servConn);
        chunkedResponseMsg.setLastChunkAndNumParts(true, numParts);
        chunkedResponseMsg.sendChunk(servConn);
    }
}
Also used : InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) ChunkedMessage(org.apache.geode.internal.cache.tier.sockets.ChunkedMessage)

Example 15 with InternalFunctionInvocationTargetException

use of org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException in project geode by apache.

the class ExecuteFunction 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;
    try {
        hasResult = clientMessage.getPart(0).getSerializedForm()[0];
        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 {
            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 ServerToClientFunctionResultSender(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);
        } else {
            context = new FunctionContextImpl(functionObject.getId(), args, resultSender);
        }
        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: " + servConn.toString() + "with context :" + context.toString());
            }
            InternalCache cache = servConn.getCache();
            HeapMemoryMonitor hmm = ((InternalResourceManager) cache.getResourceManager()).getHeapMonitor();
            if (functionObject.optimizeForWrite() && cache != null && hmm.getState().isCritical() && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
                Set<DistributedMember> sm = Collections.<DistributedMember>singleton(cache.getMyId());
                throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(new Object[] { functionObject.getId(), sm }), sm);
            }
            functionObject.execute(context);
            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) {
        // 4> 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) ServerToClientFunctionResultSender(org.apache.geode.internal.cache.execute.ServerToClientFunctionResultSender) 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) 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) ServerToClientFunctionResultSender(org.apache.geode.internal.cache.execute.ServerToClientFunctionResultSender) ChunkedMessage(org.apache.geode.internal.cache.tier.sockets.ChunkedMessage)

Aggregations

InternalFunctionInvocationTargetException (org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException)24 FunctionException (org.apache.geode.cache.execute.FunctionException)17 ChunkedMessage (org.apache.geode.internal.cache.tier.sockets.ChunkedMessage)10 IOException (java.io.IOException)9 CacheClosedException (org.apache.geode.cache.CacheClosedException)7 Function (org.apache.geode.cache.execute.Function)7 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)7 ExecuteFunctionOperationContext (org.apache.geode.cache.operations.ExecuteFunctionOperationContext)7 MemberMappedArgument (org.apache.geode.internal.cache.execute.MemberMappedArgument)7 HandShake (org.apache.geode.internal.cache.tier.sockets.HandShake)7 Part (org.apache.geode.internal.cache.tier.sockets.Part)7 AuthorizeRequest (org.apache.geode.internal.security.AuthorizeRequest)7 LowMemoryException (org.apache.geode.cache.LowMemoryException)6 Region (org.apache.geode.cache.Region)6 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 DistributedMember (org.apache.geode.distributed.DistributedMember)5 ServerToClientFunctionResultSender (org.apache.geode.internal.cache.execute.ServerToClientFunctionResultSender)5 DM (org.apache.geode.distributed.internal.DM)4