Search in sources :

Example 41 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class ExecuteFunctionNoAckOp method execute.

public static void execute(PoolImpl pool, String functionId, Object args, MemberMappedArgument memberMappedArg, boolean allServers, byte hasResult, boolean isFnSerializationReqd, boolean isHA, boolean optimizeForWrite, String[] groups) {
    List servers = null;
    AbstractOp op = new ExecuteFunctionNoAckOpImpl(functionId, args, memberMappedArg, hasResult, isFnSerializationReqd, isHA, optimizeForWrite, groups, allServers);
    try {
        // executeOn(ServerLocation server, Op op)
        if (allServers && groups.length == 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("ExecuteFunctionNoAckOp#execute : Sending Function Execution Message:" + op.getMessage() + " to all servers using pool: " + pool);
            }
            servers = pool.getConnectionSource().getAllServers();
            Iterator i = servers.iterator();
            while (i.hasNext()) {
                pool.executeOn((ServerLocation) i.next(), op);
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("ExecuteFunctionNoAckOp#execute : Sending Function Execution Message:" + op.getMessage() + " to server using pool: " + pool + " with groups:" + Arrays.toString(groups) + " all members:" + allServers);
            }
            pool.execute(op, 0);
        }
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("ExecuteFunctionNoAckOp#execute : Exception occurred while Sending Function Execution Message:" + op.getMessage() + " to server using pool: " + pool, ex);
        }
        if (ex.getMessage() != null)
            throw new FunctionException(ex.getMessage(), ex);
        else
            throw new FunctionException("Unexpected exception during function execution:", ex);
    }
}
Also used : Iterator(java.util.Iterator) FunctionException(org.apache.geode.cache.execute.FunctionException) List(java.util.List) FunctionException(org.apache.geode.cache.execute.FunctionException)

Example 42 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class ExecuteFunctionNoAckOp 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(PoolImpl pool, Function function, Object args, MemberMappedArgument memberMappedArg, boolean allServers, byte hasResult, boolean isFnSerializationReqd, String[] groups) {
    List servers = null;
    AbstractOp op = new ExecuteFunctionNoAckOpImpl(function, args, memberMappedArg, hasResult, isFnSerializationReqd, groups, allServers);
    try {
        // executeOn(ServerLocation server, Op op)
        if (allServers && groups.length == 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("ExecuteFunctionNoAckOp#execute : Sending Function Execution Message:" + op.getMessage() + " to all servers using pool: " + pool);
            }
            servers = pool.getConnectionSource().getAllServers();
            Iterator i = servers.iterator();
            while (i.hasNext()) {
                pool.executeOn((ServerLocation) i.next(), op);
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("ExecuteFunctionNoAckOp#execute : Sending Function Execution Message:" + op.getMessage() + " to server using pool: " + pool + " with groups:" + Arrays.toString(groups) + " all members:" + allServers);
            }
            pool.execute(op, 0);
        }
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("ExecuteFunctionNoAckOp#execute : Exception occurred while Sending Function Execution Message:" + op.getMessage() + " to server using pool: " + pool, ex);
        }
        if (ex.getMessage() != null)
            throw new FunctionException(ex.getMessage(), ex);
        else
            throw new FunctionException("Unexpected exception during function execution:", ex);
    }
}
Also used : Iterator(java.util.Iterator) FunctionException(org.apache.geode.cache.execute.FunctionException) List(java.util.List) FunctionException(org.apache.geode.cache.execute.FunctionException)

Example 43 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class QueryDataFunction method callFunction.

private static Object callFunction(final Object functionArgs, final Set<DistributedMember> members, final boolean zipResult) throws Exception {
    try {
        if (members.size() == 1) {
            DistributedMember member = members.iterator().next();
            ResultCollector collector = FunctionService.onMember(member).setArguments(functionArgs).execute(ManagementConstants.QUERY_DATA_FUNCTION);
            List list = (List) collector.getResult();
            Object object = null;
            if (list.size() > 0) {
                object = list.get(0);
            }
            if (object instanceof Throwable) {
                throw (Throwable) object;
            }
            QueryDataFunctionResult result = (QueryDataFunctionResult) object;
            if (zipResult) {
                // The result is already compressed
                return result.compressedBytes;
            } else {
                Object[] functionArgsList = (Object[]) functionArgs;
                boolean showMember = (Boolean) functionArgsList[DISPLAY_MEMBERWISE];
                if (showMember) {
                    // Added to show a single member similar to multiple
                    // member.
                    // Note , if no member is selected this is the code path executed. A
                    // random associated member is chosen.
                    List<String> decompressedList = new ArrayList<>();
                    decompressedList.add(BeanUtilFuncs.decompress(result.compressedBytes));
                    return wrapResult(decompressedList.toString());
                }
                return BeanUtilFuncs.decompress(result.compressedBytes);
            }
        } else {
            // More than 1 Member
            ResultCollector coll = FunctionService.onMembers(members).setArguments(functionArgs).execute(ManagementConstants.QUERY_DATA_FUNCTION);
            List list = (List) coll.getResult();
            Object object = list.get(0);
            if (object instanceof Throwable) {
                throw (Throwable) object;
            }
            Iterator<QueryDataFunctionResult> it = list.iterator();
            List<String> decompressedList = new ArrayList<>();
            while (it.hasNext()) {
                String decompressedStr;
                decompressedStr = BeanUtilFuncs.decompress(it.next().compressedBytes);
                decompressedList.add(decompressedStr);
            }
            if (zipResult) {
                return BeanUtilFuncs.compress(wrapResult(decompressedList.toString()));
            } else {
                return wrapResult(decompressedList.toString());
            }
        }
    } catch (FunctionException fe) {
        throw new Exception(ManagementStrings.QUERY__MSG__QUERY_EXEC.toLocalizedString(fe.getMessage()));
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable e) {
        SystemFailure.checkFailure();
        throw new Exception(ManagementStrings.QUERY__MSG__QUERY_EXEC.toLocalizedString(e.getMessage()));
    }
}
Also used : ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) FunctionException(org.apache.geode.cache.execute.FunctionException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) DistributedMember(org.apache.geode.distributed.DistributedMember) ArrayList(java.util.ArrayList) List(java.util.List) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) ResultCollector(org.apache.geode.cache.execute.ResultCollector)

Example 44 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class PartitionedRegion method executeOnMultipleNodes.

/**
   * Executes function on multiple nodes
   */
private ResultCollector executeOnMultipleNodes(final Function function, final PartitionedRegionFunctionExecutor execution, ResultCollector rc, boolean isPRSingleHop, boolean isBucketSetAsFilter) {
    final Set routingKeys = execution.getFilter();
    final boolean primaryMembersNeeded = function.optimizeForWrite();
    HashMap<Integer, HashSet> bucketToKeysMap = FunctionExecutionNodePruner.groupByBucket(this, routingKeys, primaryMembersNeeded, false, isBucketSetAsFilter);
    HashMap<InternalDistributedMember, HashSet> memberToKeysMap = new HashMap<InternalDistributedMember, HashSet>();
    HashMap<InternalDistributedMember, HashSet<Integer>> memberToBuckets = FunctionExecutionNodePruner.groupByMemberToBuckets(this, bucketToKeysMap.keySet(), primaryMembersNeeded);
    if (isPRSingleHop && (memberToBuckets.size() > 1)) {
        // memberToBuckets.remove(getMyId()); // don't remove
        for (InternalDistributedMember targetNode : memberToBuckets.keySet()) {
            if (!targetNode.equals(getMyId())) {
                for (Integer bucketId : memberToBuckets.get(targetNode)) {
                    Set<ServerBucketProfile> profiles = this.getRegionAdvisor().getClientBucketProfiles(bucketId);
                    if (profiles != null) {
                        for (ServerBucketProfile profile : profiles) {
                            if (profile.getDistributedMember().equals(targetNode)) {
                                if (logger.isDebugEnabled()) {
                                    logger.debug("FunctionServiceSingleHop: Found multiple nodes.{}", getMyId());
                                }
                                throw new InternalFunctionInvocationTargetException(LocalizedStrings.PartitionedRegion_MULTIPLE_TARGET_NODE_FOUND_FOR.toLocalizedString());
                            }
                        }
                    }
                }
            }
        }
    }
    while (!execution.getFailedNodes().isEmpty()) {
        Set memberKeySet = memberToBuckets.keySet();
        RetryTimeKeeper retryTime = new RetryTimeKeeper(this.retryTimeout);
        Iterator iterator = memberKeySet.iterator();
        boolean hasRemovedNode = false;
        while (iterator.hasNext()) {
            if (execution.getFailedNodes().contains(((DistributedMember) iterator.next()).getId())) {
                hasRemovedNode = true;
            }
        }
        if (hasRemovedNode) {
            if (retryTime.overMaximum()) {
                PRHARedundancyProvider.timedOut(this, null, null, "doing function execution", this.retryTimeout);
            // NOTREACHED
            }
            retryTime.waitToRetryNode();
            memberToBuckets = FunctionExecutionNodePruner.groupByMemberToBuckets(this, bucketToKeysMap.keySet(), primaryMembersNeeded);
        } else {
            execution.clearFailedNodes();
        }
    }
    for (Map.Entry entry : memberToBuckets.entrySet()) {
        InternalDistributedMember member = (InternalDistributedMember) entry.getKey();
        HashSet<Integer> buckets = (HashSet) entry.getValue();
        for (Integer bucket : buckets) {
            HashSet keys = memberToKeysMap.get(member);
            if (keys == null) {
                keys = new HashSet();
            }
            keys.addAll(bucketToKeysMap.get(bucket));
            memberToKeysMap.put(member, keys);
        }
    }
    // memberToKeysMap.keySet().retainAll(memberToBuckets.keySet());
    if (memberToKeysMap.isEmpty()) {
        throw new FunctionException(LocalizedStrings.PartitionedRegion_NO_TARGET_NODE_FOUND_FOR_KEY_0.toLocalizedString(routingKeys));
    }
    Set<InternalDistributedMember> dest = memberToKeysMap.keySet();
    execution.validateExecution(function, dest);
    // added for the data aware procedure.
    execution.setExecutionNodes(dest);
    // end
    final HashSet localKeys = memberToKeysMap.remove(getMyId());
    HashSet<Integer> localBucketSet = null;
    boolean remoteOnly = false;
    if (localKeys == null) {
        remoteOnly = true;
    } else {
        localBucketSet = FunctionExecutionNodePruner.getBucketSet(PartitionedRegion.this, localKeys, false, isBucketSetAsFilter);
        remoteOnly = false;
    }
    final LocalResultCollector<?, ?> localResultCollector = execution.getLocalResultCollector(function, rc);
    final DM dm = getDistributionManager();
    final PartitionedRegionFunctionResultSender resultSender = new PartitionedRegionFunctionResultSender(dm, this, 0L, localResultCollector, execution.getServerResultSender(), memberToKeysMap.isEmpty(), remoteOnly, execution.isForwardExceptions(), function, localBucketSet);
    if (localKeys != null) {
        final RegionFunctionContextImpl prContext = new RegionFunctionContextImpl(function.getId(), PartitionedRegion.this, execution.getArgumentsForMember(getMyId().getId()), localKeys, ColocationHelper.constructAndGetAllColocatedLocalDataSet(PartitionedRegion.this, localBucketSet), localBucketSet, resultSender, execution.isReExecute());
        if (logger.isDebugEnabled()) {
            logger.debug("FunctionService: Executing on local node with keys.{}", localKeys);
        }
        execution.executeFunctionOnLocalPRNode(function, prContext, resultSender, dm, isTX());
    }
    if (!memberToKeysMap.isEmpty()) {
        HashMap<InternalDistributedMember, FunctionRemoteContext> recipMap = new HashMap<InternalDistributedMember, FunctionRemoteContext>();
        for (Map.Entry me : memberToKeysMap.entrySet()) {
            InternalDistributedMember recip = (InternalDistributedMember) me.getKey();
            HashSet memKeys = (HashSet) me.getValue();
            FunctionRemoteContext context = new FunctionRemoteContext(function, execution.getArgumentsForMember(recip.getId()), memKeys, FunctionExecutionNodePruner.getBucketSet(this, memKeys, false, isBucketSetAsFilter), execution.isReExecute(), execution.isFnSerializationReqd());
            recipMap.put(recip, context);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("FunctionService: Executing on remote nodes with member to keys map.{}", memberToKeysMap);
        }
        PartitionedRegionFunctionResultWaiter resultReciever = new PartitionedRegionFunctionResultWaiter(getSystem(), this.getPRId(), localResultCollector, function, resultSender);
        return resultReciever.getPartitionedDataFrom(recipMap, this, execution);
    }
    return localResultCollector;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FunctionRemoteContext(org.apache.geode.internal.cache.execute.FunctionRemoteContext) DM(org.apache.geode.distributed.internal.DM) ServerBucketProfile(org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile) PREntriesIterator(org.apache.geode.internal.cache.partitioned.PREntriesIterator) Iterator(java.util.Iterator) HashSet(java.util.HashSet) PartitionedRegionFunctionResultWaiter(org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultWaiter) FunctionException(org.apache.geode.cache.execute.FunctionException) EmptyRegionFunctionException(org.apache.geode.cache.execute.EmptyRegionFunctionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) RegionFunctionContextImpl(org.apache.geode.internal.cache.execute.RegionFunctionContextImpl) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) PartitionedRegionFunctionResultSender(org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultSender) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashMap(java.util.HashMap)

Example 45 with FunctionException

use of org.apache.geode.cache.execute.FunctionException in project geode by apache.

the class PRFunctionStreamingResultCollector method getResult.

@Override
public Object getResult() throws FunctionException {
    if (this.resultCollected) {
        throw new FunctionException("Result already collected");
    }
    this.resultCollected = true;
    if (this.hasResult) {
        try {
            this.waitForCacheOrFunctionException(0);
            if (!this.execution.getFailedNodes().isEmpty() && !this.execution.isClientServerMode()) {
                // end the rc and clear it
                endResults();
                clearResults();
                this.execution = this.execution.setIsReExecute();
                ResultCollector newRc = null;
                if (execution.isFnSerializationReqd()) {
                    newRc = this.execution.execute(this.fn);
                } else {
                    newRc = this.execution.execute(this.fn.getId());
                }
                return newRc.getResult();
            }
            if (!this.execution.getWaitOnExceptionFlag() && this.fites.size() > 0) {
                throw new FunctionException(this.fites.get(0));
            }
        } catch (FunctionInvocationTargetException fite) {
            // the function.
            if (!execution.getWaitOnExceptionFlag()) {
                if (!this.fn.isHA()) {
                    throw new FunctionException(fite);
                } else if (execution.isClientServerMode()) {
                    clearResults();
                    FunctionInvocationTargetException iFITE = new InternalFunctionInvocationTargetException(fite.getMessage(), this.execution.getFailedNodes());
                    throw new FunctionException(iFITE);
                } else {
                    clearResults();
                    this.execution = this.execution.setIsReExecute();
                    ResultCollector newRc = null;
                    if (execution.isFnSerializationReqd()) {
                        newRc = this.execution.execute(this.fn);
                    } else {
                        newRc = this.execution.execute(this.fn.getId());
                    }
                    return newRc.getResult();
                }
            }
        } catch (BucketMovedException e) {
            if (!execution.getWaitOnExceptionFlag()) {
                if (!this.fn.isHA()) {
                    // endResults();
                    FunctionInvocationTargetException fite = new FunctionInvocationTargetException(e.getMessage());
                    throw new FunctionException(fite);
                } else if (execution.isClientServerMode()) {
                    // endResults();
                    clearResults();
                    FunctionInvocationTargetException fite = new InternalFunctionInvocationTargetException(e.getMessage());
                    throw new FunctionException(fite);
                } else {
                    // endResults();
                    clearResults();
                    this.execution = this.execution.setIsReExecute();
                    ResultCollector newRc = null;
                    if (execution.isFnSerializationReqd()) {
                        newRc = this.execution.execute(this.fn);
                    } else {
                        newRc = this.execution.execute(this.fn.getId());
                    }
                    return newRc.getResult();
                }
            }
        } catch (CacheClosedException e) {
            if (!execution.getWaitOnExceptionFlag()) {
                if (!this.fn.isHA()) {
                    // endResults();
                    FunctionInvocationTargetException fite = new FunctionInvocationTargetException(e.getMessage());
                    throw new FunctionException(fite);
                } else if (execution.isClientServerMode()) {
                    // endResults();
                    clearResults();
                    FunctionInvocationTargetException fite = new InternalFunctionInvocationTargetException(e.getMessage(), this.execution.getFailedNodes());
                    throw new FunctionException(fite);
                } else {
                    // endResults();
                    clearResults();
                    this.execution = this.execution.setIsReExecute();
                    ResultCollector newRc = null;
                    if (execution.isFnSerializationReqd()) {
                        newRc = this.execution.execute(this.fn);
                    } else {
                        newRc = this.execution.execute(this.fn.getId());
                    }
                    return newRc.getResult();
                }
            }
        } catch (CacheException e) {
            // endResults();
            throw new FunctionException(e);
        } catch (ForceReattemptException e) {
            // the function.
            if (!this.fn.isHA()) {
                throw new FunctionException(e);
            } else if (execution.isClientServerMode()) {
                clearResults();
                FunctionInvocationTargetException iFITE = new InternalFunctionInvocationTargetException(e.getMessage(), this.execution.getFailedNodes());
                throw new FunctionException(iFITE);
            } else {
                clearResults();
                this.execution = this.execution.setIsReExecute();
                ResultCollector newRc = null;
                if (execution.isFnSerializationReqd()) {
                    newRc = this.execution.execute(this.fn);
                } else {
                    newRc = this.execution.execute(this.fn.getId());
                }
                return newRc.getResult();
            }
        }
    }
    return this.userRC.getResult();
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) CacheException(org.apache.geode.cache.CacheException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) InternalFunctionException(org.apache.geode.internal.cache.execute.InternalFunctionException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) BucketMovedException(org.apache.geode.internal.cache.execute.BucketMovedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) FunctionStreamingResultCollector(org.apache.geode.internal.cache.execute.FunctionStreamingResultCollector) ResultCollector(org.apache.geode.cache.execute.ResultCollector)

Aggregations

FunctionException (org.apache.geode.cache.execute.FunctionException)140 Function (org.apache.geode.cache.execute.Function)45 Execution (org.apache.geode.cache.execute.Execution)39 ResultCollector (org.apache.geode.cache.execute.ResultCollector)39 ArrayList (java.util.ArrayList)38 Test (org.junit.Test)38 HashSet (java.util.HashSet)36 CacheClosedException (org.apache.geode.cache.CacheClosedException)31 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)31 IOException (java.io.IOException)30 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)30 List (java.util.List)26 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)26 Host (org.apache.geode.test.dunit.Host)25 VM (org.apache.geode.test.dunit.VM)25 Region (org.apache.geode.cache.Region)24 IgnoredException (org.apache.geode.test.dunit.IgnoredException)24 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)24 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)22 Set (java.util.Set)21