Search in sources :

Example 21 with FunctionException

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

the class FunctionExecutionNodePruner method groupByBucket.

public static HashMap<Integer, HashSet> groupByBucket(PartitionedRegion pr, Set routingKeys, final boolean primaryMembersNeeded, final boolean hasRoutingObjects, final boolean isBucketSetAsFilter) {
    HashMap bucketToKeysMap = new HashMap();
    Iterator i = routingKeys.iterator();
    while (i.hasNext()) {
        final Integer bucketId;
        Object key = i.next();
        if (isBucketSetAsFilter) {
            bucketId = ((Integer) key);
        } else {
            if (hasRoutingObjects) {
                bucketId = Integer.valueOf(PartitionedRegionHelper.getHashKey(pr, key));
            } else {
                bucketId = Integer.valueOf(PartitionedRegionHelper.getHashKey(pr, Operation.FUNCTION_EXECUTION, key, null, null));
            }
        }
        InternalDistributedMember mem = null;
        if (primaryMembersNeeded) {
            mem = pr.getOrCreateNodeForBucketWrite(bucketId.intValue(), null);
        } else {
            mem = pr.getOrCreateNodeForBucketRead(bucketId.intValue());
        }
        if (mem == null) {
            throw new FunctionException(LocalizedStrings.PartitionedRegion_NO_TARGET_NODE_FOUND_FOR_KEY_0.toLocalizedString(key));
        }
        HashSet bucketKeys = (HashSet) bucketToKeysMap.get(bucketId);
        if (bucketKeys == null) {
            // faster if this was an ArrayList
            bucketKeys = new HashSet();
            bucketToKeysMap.put(bucketId, bucketKeys);
        }
        bucketKeys.add(key);
    }
    return bucketToKeysMap;
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) HashMap(java.util.HashMap) Iterator(java.util.Iterator) FunctionException(org.apache.geode.cache.execute.FunctionException) HashSet(java.util.HashSet)

Example 22 with FunctionException

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

the class AbstractExecution method handleException.

private void handleException(Throwable functionException, final Function fn, final FunctionContext cx, final ResultSender sender, DM dm) {
    FunctionStats stats = FunctionStats.getFunctionStats(fn.getId(), dm.getSystem());
    if (logger.isDebugEnabled()) {
        logger.debug("Exception occurred on local node while executing Function: {}", fn.getId(), functionException);
    }
    stats.endFunctionExecutionWithException(fn.hasResult());
    if (fn.hasResult()) {
        if (waitOnException || forwardExceptions) {
            if (functionException instanceof FunctionException && functionException.getCause() instanceof QueryInvalidException) {
                // Handle this exception differently since it can contain
                // non-serializable objects.
                // java.io.NotSerializableException: antlr.CommonToken
                // create a new FunctionException on the original one's message (not cause).
                functionException = new FunctionException(functionException.getLocalizedMessage());
            }
            sender.lastResult(functionException);
        } else {
            ((InternalResultSender) sender).setException(functionException);
        }
    } else {
        logger.warn(LocalizedMessage.create(LocalizedStrings.FunctionService_EXCEPTION_ON_LOCAL_NODE), functionException);
    }
}
Also used : QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) FunctionException(org.apache.geode.cache.execute.FunctionException)

Example 23 with FunctionException

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

the class AbstractExecution method executeFunctionOnLocalPRNode.

public void executeFunctionOnLocalPRNode(final Function fn, final FunctionContext cx, final PartitionedRegionFunctionResultSender sender, DM dm, boolean isTx) {
    if (dm instanceof DistributionManager && !isTx) {
        if (ServerConnection.isExecuteFunctionOnLocalNodeOnly().byteValue() == 1) {
            // executed locally
            ServerConnection.executeFunctionOnLocalNodeOnly((byte) 3);
            executeFunctionLocally(fn, cx, sender, dm);
            if (!sender.isLastResultReceived() && fn.hasResult()) {
                ((InternalResultSender) sender).setException(new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(fn.getId())));
            }
        } else {
            final DistributionManager newDM = (DistributionManager) dm;
            newDM.getFunctionExcecutor().execute(new Runnable() {

                public void run() {
                    executeFunctionLocally(fn, cx, sender, newDM);
                    if (!sender.isLastResultReceived() && fn.hasResult()) {
                        ((InternalResultSender) sender).setException(new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(fn.getId())));
                    }
                }
            });
        }
    } else {
        executeFunctionLocally(fn, cx, sender, dm);
        if (!sender.isLastResultReceived() && fn.hasResult()) {
            ((InternalResultSender) sender).setException(new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(fn.getId())));
        }
    }
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException) DistributionManager(org.apache.geode.distributed.internal.DistributionManager)

Example 24 with FunctionException

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

the class AbstractExecution method execute.

public ResultCollector execute(final String functionName) {
    if (functionName == null) {
        throw new FunctionException(LocalizedStrings.ExecuteFunction_THE_INPUT_FUNCTION_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL.toLocalizedString());
    }
    this.isFnSerializationReqd = false;
    Function functionObject = FunctionService.getFunction(functionName);
    if (functionObject == null) {
        throw new FunctionException(LocalizedStrings.ExecuteFunction_FUNCTION_NAMED_0_IS_NOT_REGISTERED.toLocalizedString(functionName));
    }
    return executeFunction(functionObject);
}
Also used : Function(org.apache.geode.cache.execute.Function) FunctionException(org.apache.geode.cache.execute.FunctionException)

Example 25 with FunctionException

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

the class MemberRegionFunction method execute.

@Override
public void execute(FunctionContext context) {
    Object[] args = (Object[]) context.getArguments();
    String region = (String) args[0];
    String functionId = (String) args[1];
    Cache cache = CacheFactory.getAnyInstance();
    try {
        Function function = FunctionService.getFunction(functionId);
        if (function == null) {
            context.getResultSender().lastResult("For region on a member did not get function " + functionId);
        }
        Execution execution = FunctionService.onRegion(cache.getRegion(region));
        if (execution == null) {
            context.getResultSender().lastResult("For region on a member could not execute");
        } else {
            execution.execute(function);
            context.getResultSender().lastResult("succeeded in executing on region " + region);
        }
    } catch (FunctionException e) {
        context.getResultSender().lastResult("FunctionException in MemberRegionFunction =" + e.getMessage());
    } catch (Exception e) {
        context.getResultSender().lastResult("Exception in MemberRegionFunction =" + e.getMessage());
    }
}
Also used : Function(org.apache.geode.cache.execute.Function) Execution(org.apache.geode.cache.execute.Execution) FunctionException(org.apache.geode.cache.execute.FunctionException) FunctionException(org.apache.geode.cache.execute.FunctionException) Cache(org.apache.geode.cache.Cache)

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