Search in sources :

Example 61 with FunctionException

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

the class MultiRegionFunctionExecutor method executeFunction.

private ResultCollector executeFunction(final Function function, ResultCollector resultCollector) {
    InternalDistributedSystem ds = InternalDistributedSystem.getConnectedInstance();
    if (ds == null) {
        throw new IllegalStateException(LocalizedStrings.ExecuteFunction_DS_NOT_CREATED_OR_NOT_READY.toLocalizedString());
    }
    final DM dm = ds.getDistributionManager();
    final Map<InternalDistributedMember, Set<String>> memberToRegionMap = calculateMemberToRegionMap();
    final Set<InternalDistributedMember> dest = new HashSet<InternalDistributedMember>(memberToRegionMap.keySet());
    if (dest.isEmpty()) {
        throw new FunctionException(LocalizedStrings.MemberFunctionExecutor_NO_MEMBER_FOUND_FOR_EXECUTING_FUNCTION_0.toLocalizedString(function.getId()));
    }
    final InternalCache cache = GemFireCacheImpl.getInstance();
    if (function.optimizeForWrite() && cache != null && cache.getInternalResourceManager().getHeapMonitor().containsHeapCriticalMembers(dest) && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
        Set<InternalDistributedMember> hcm = cache.getResourceAdvisor().adviseCritialMembers();
        Set<DistributedMember> sm = SetUtils.intersection(hcm, dest);
        throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(function.getId(), sm), sm);
    }
    setExecutionNodes(dest);
    final InternalDistributedMember localVM = cache.getMyId();
    final LocalResultCollector<?, ?> localResultCollector = getLocalResultCollector(function, resultCollector);
    boolean remoteOnly = false;
    boolean localOnly = false;
    if (!dest.contains(localVM)) {
        remoteOnly = true;
    }
    if (dest.size() == 1 && dest.contains(localVM)) {
        localOnly = true;
    }
    validateExecution(function, dest);
    final MemberFunctionResultSender resultSender = new MemberFunctionResultSender(dm, localResultCollector, function, localOnly, remoteOnly, null);
    if (dest.contains(localVM)) {
        // if member is local VM
        dest.remove(localVM);
        Set<String> regionPathSet = memberToRegionMap.get(localVM);
        Set<Region> regions = new HashSet<Region>();
        if (regionPathSet != null) {
            InternalCache cache1 = GemFireCacheImpl.getInstance();
            for (String regionPath : regionPathSet) {
                regions.add(cache1.getRegion(regionPath));
            }
        }
        final FunctionContextImpl context = new MultiRegionFunctionContextImpl(function.getId(), getArgumentsForMember(localVM.getId()), resultSender, regions, this.isReExecute);
        boolean isTx = cache.getTxManager().getTXState() == null ? false : true;
        executeFunctionOnLocalNode(function, context, resultSender, dm, isTx);
    }
    if (!dest.isEmpty()) {
        HashMap<InternalDistributedMember, Object> memberArgs = new HashMap<InternalDistributedMember, Object>();
        for (InternalDistributedMember recip : dest) {
            memberArgs.put(recip, getArgumentsForMember(recip.getId()));
        }
        Assert.assertTrue(memberArgs.size() == dest.size());
        MultiRegionFunctionResultWaiter waiter = new MultiRegionFunctionResultWaiter(ds, localResultCollector, function, dest, memberArgs, resultSender, memberToRegionMap);
        return waiter.getFunctionResultFrom(dest, function, this);
    }
    return localResultCollector;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) DM(org.apache.geode.distributed.internal.DM) InternalCache(org.apache.geode.internal.cache.InternalCache) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) LowMemoryException(org.apache.geode.cache.LowMemoryException) HashSet(java.util.HashSet) FunctionException(org.apache.geode.cache.execute.FunctionException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 62 with FunctionException

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

the class PartitionedRegionFunctionExecutor method withBucketFilter.

public InternalExecution withBucketFilter(Set<Integer> bucketIDs) {
    if (bucketIDs == null) {
        throw new FunctionException(LocalizedStrings.ExecuteRegionFunction_THE_INPUT_0_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL.toLocalizedString("buckets as filter"));
    } else if (bucketIDs.isEmpty()) {
        throw new FunctionException("Bucket IDs list is empty");
    }
    Set<Integer> actualBucketSet = pr.getRegionAdvisor().getBucketSet();
    bucketIDs.retainAll(actualBucketSet);
    Iterator<Integer> it = bucketIDs.iterator();
    while (it.hasNext()) {
        int bid = it.next();
        if (!actualBucketSet.contains(bid)) {
            throw new FunctionException("Bucket " + bid + " does not exist.");
        }
    }
    if (bucketIDs.isEmpty()) {
        throw new FunctionException("No valid buckets to execute on");
    }
    return new PartitionedRegionFunctionExecutor(this, bucketIDs, true);
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException)

Example 63 with FunctionException

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

the class FunctionStreamingResultCollector method waitForCacheOrFunctionException.

/**
   * Waits for the response from the recipient
   * 
   * @throws CacheException if the recipient threw a cache exception during message processing
   * @throws ForceReattemptException if the recipient left the distributed system before the
   *         response was received.
   * @throws RegionDestroyedException if the peer has closed its copy of the region
   */
public boolean waitForCacheOrFunctionException(long timeout) throws CacheException, ForceReattemptException {
    boolean timedOut = false;
    try {
        if (timeout == 0) {
            waitForRepliesUninterruptibly();
            timedOut = true;
        } else {
            timedOut = waitForRepliesUninterruptibly(timeout);
        }
    } catch (ReplyException e) {
        removeMember(e.getSender(), true);
        Throwable t = e.getCause();
        if (t instanceof CacheException) {
            throw (CacheException) t;
        } else if (t instanceof RegionDestroyedException) {
            throw (RegionDestroyedException) t;
        } else if (t instanceof ForceReattemptException) {
            throw new ForceReattemptException("Peer requests reattempt", t);
        } else if (t instanceof PrimaryBucketException) {
            throw new PrimaryBucketException("Peer failed primary test", t);
        }
        if (t instanceof CancelException) {
            this.execution.failedNodes.add(e.getSender().getId());
            String msg = "PartitionResponse got remote CacheClosedException, throwing PartitionedRegionCommunicationException";
            logger.debug("{}, throwing ForceReattemptException", msg, t);
            throw (CancelException) t;
        }
        if (e.getCause() instanceof FunctionException) {
            throw (FunctionException) e.getCause();
        }
        e.handleAsUnexpected();
    }
    return timedOut;
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) CacheException(org.apache.geode.cache.CacheException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) FunctionException(org.apache.geode.cache.execute.FunctionException) CancelException(org.apache.geode.CancelException) PrimaryBucketException(org.apache.geode.internal.cache.PrimaryBucketException) ReplyException(org.apache.geode.distributed.internal.ReplyException)

Example 64 with FunctionException

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

the class FunctionStreamingResultCollector method getResult.

public Object getResult(long timeout, TimeUnit unit) throws FunctionException, InterruptedException {
    long timeoutInMillis = unit.toMillis(timeout);
    if (this.resultCollected) {
        throw new FunctionException(LocalizedStrings.ExecuteFunction_RESULTS_ALREADY_COLLECTED.toLocalizedString());
    }
    this.resultCollected = true;
    // Should convert it from unit to milliseconds
    if (this.userRC != null) {
        try {
            long timeBefore = System.currentTimeMillis();
            boolean isNotTimedOut;
            if (execution instanceof DistributedRegionFunctionExecutor || execution instanceof MultiRegionFunctionExecutor) {
                isNotTimedOut = this.waitForCacheOrFunctionException(timeoutInMillis);
            } else {
                isNotTimedOut = this.waitForRepliesUninterruptibly(timeoutInMillis);
            }
            if (!isNotTimedOut) {
                throw new FunctionException(LocalizedStrings.ExecuteFunction_RESULTS_NOT_COLLECTED_IN_TIME_PROVIDED.toLocalizedString());
            }
            long timeAfter = System.currentTimeMillis();
            timeoutInMillis = timeoutInMillis - (timeAfter - timeBefore);
            if (timeoutInMillis < 0)
                timeoutInMillis = 0;
            if (this.removedNodes != null) {
                if (this.removedNodes.size() != 0) {
                    // end the rc and clear it
                    clearResults();
                    this.execution = this.execution.setIsReExecute();
                    ResultCollector newRc = null;
                    if (execution.isFnSerializationReqd()) {
                        newRc = this.execution.execute(fn);
                    } else {
                        newRc = this.execution.execute(fn.getId());
                    }
                    return newRc.getResult(timeoutInMillis, unit);
                }
            }
            if (!this.execution.getWaitOnExceptionFlag() && this.fites.size() > 0) {
                throw new FunctionException(this.fites.get(0));
            }
        } catch (FunctionInvocationTargetException fite) {
            // function.
            if (!(execution instanceof DistributedRegionFunctionExecutor || execution instanceof MultiRegionFunctionExecutor) || !fn.isHA()) {
                throw new FunctionException(fite);
            } else if (execution.isClientServerMode()) {
                clearResults();
                FunctionInvocationTargetException iFITE = new InternalFunctionInvocationTargetException(fite.getMessage());
                throw new FunctionException(iFITE);
            } else {
                clearResults();
                this.execution = this.execution.setIsReExecute();
                ResultCollector newRc = null;
                if (execution.isFnSerializationReqd()) {
                    newRc = this.execution.execute(fn);
                } else {
                    newRc = this.execution.execute(fn.getId());
                }
                return newRc.getResult(timeoutInMillis, unit);
            }
        } catch (CacheClosedException e) {
            if (!(execution instanceof DistributedRegionFunctionExecutor || execution instanceof MultiRegionFunctionExecutor) || !fn.isHA()) {
                FunctionInvocationTargetException fite = new FunctionInvocationTargetException(e.getMessage());
                throw new FunctionException(fite);
            } else if (execution.isClientServerMode()) {
                clearResults();
                FunctionInvocationTargetException fite = new InternalFunctionInvocationTargetException(e.getMessage());
                throw new FunctionException(fite);
            } else {
                clearResults();
                this.execution = this.execution.setIsReExecute();
                ResultCollector newRc = null;
                if (execution.isFnSerializationReqd()) {
                    newRc = this.execution.execute(fn);
                } else {
                    newRc = this.execution.execute(fn.getId());
                }
                return newRc.getResult(timeoutInMillis, unit);
            }
        }// }
         catch (ForceReattemptException e) {
            if (!(execution instanceof DistributedRegionFunctionExecutor || execution instanceof MultiRegionFunctionExecutor) || !fn.isHA()) {
                FunctionInvocationTargetException fite = new FunctionInvocationTargetException(e.getMessage());
                throw new FunctionException(fite);
            } else if (execution.isClientServerMode()) {
                clearResults();
                FunctionInvocationTargetException fite = new InternalFunctionInvocationTargetException(e.getMessage());
                throw new FunctionException(fite);
            } else {
                clearResults();
                this.execution = this.execution.setIsReExecute();
                ResultCollector newRc = null;
                if (execution.isFnSerializationReqd()) {
                    newRc = this.execution.execute(fn);
                } else {
                    newRc = this.execution.execute(fn.getId());
                }
                return newRc.getResult(timeoutInMillis, unit);
            }
        } catch (ReplyException e) {
            if (!(execution.waitOnException || execution.forwardExceptions)) {
                throw new FunctionException(e.getCause());
            }
        }
        return this.userRC.getResult(timeoutInMillis, unit);
    }
    return null;
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) FunctionException(org.apache.geode.cache.execute.FunctionException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) ResultCollector(org.apache.geode.cache.execute.ResultCollector) ReplyException(org.apache.geode.distributed.internal.ReplyException)

Example 65 with FunctionException

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

the class FunctionStreamingResultCollector method getResult.

public Object getResult() throws FunctionException {
    if (this.resultCollected) {
        throw new FunctionException(LocalizedStrings.ExecuteFunction_RESULTS_ALREADY_COLLECTED.toLocalizedString());
    }
    this.resultCollected = true;
    if (this.userRC != null) {
        try {
            if (execution instanceof DistributedRegionFunctionExecutor || execution instanceof MultiRegionFunctionExecutor) {
                this.waitForCacheOrFunctionException(0);
            } else {
                waitForRepliesUninterruptibly(0);
            }
            if (this.removedNodes != null) {
                if (this.removedNodes.size() != 0) {
                    // end the rc and clear it
                    clearResults();
                    this.execution = this.execution.setIsReExecute();
                    ResultCollector newRc = null;
                    if (execution.isFnSerializationReqd()) {
                        newRc = this.execution.execute(fn);
                    } else {
                        newRc = this.execution.execute(fn.getId());
                    }
                    return newRc.getResult();
                }
            }
            if (!this.execution.getWaitOnExceptionFlag() && this.fites.size() > 0) {
                throw new FunctionException(this.fites.get(0));
            }
        } catch (FunctionInvocationTargetException fite) {
            if (!(execution instanceof DistributedRegionFunctionExecutor || execution instanceof MultiRegionFunctionExecutor) || !fn.isHA()) {
                throw new FunctionException(fite);
            } else if (execution.isClientServerMode()) {
                clearResults();
                FunctionInvocationTargetException iFITE = new InternalFunctionInvocationTargetException(fite.getMessage());
                throw new FunctionException(iFITE);
            } else {
                clearResults();
                this.execution = this.execution.setIsReExecute();
                ResultCollector newRc = null;
                if (execution.isFnSerializationReqd()) {
                    newRc = this.execution.execute(fn);
                } else {
                    newRc = this.execution.execute(fn.getId());
                }
                return newRc.getResult();
            }
        } catch (CacheClosedException e) {
            if (!(execution instanceof DistributedRegionFunctionExecutor || execution instanceof MultiRegionFunctionExecutor) || !fn.isHA()) {
                FunctionInvocationTargetException fite = new FunctionInvocationTargetException(e.getMessage());
                throw new FunctionException(fite);
            } else if (execution.isClientServerMode()) {
                clearResults();
                FunctionInvocationTargetException fite = new InternalFunctionInvocationTargetException(e.getMessage());
                throw new FunctionException(fite);
            } else {
                clearResults();
                this.execution = this.execution.setIsReExecute();
                ResultCollector newRc = null;
                if (execution.isFnSerializationReqd()) {
                    newRc = this.execution.execute(fn);
                } else {
                    newRc = this.execution.execute(fn.getId());
                }
                return newRc.getResult();
            }
        }// }
         catch (ForceReattemptException e) {
            if (!(execution instanceof DistributedRegionFunctionExecutor || execution instanceof MultiRegionFunctionExecutor) || !fn.isHA()) {
                FunctionInvocationTargetException fite = new FunctionInvocationTargetException(e.getMessage());
                throw new FunctionException(fite);
            } else if (execution.isClientServerMode()) {
                clearResults();
                FunctionInvocationTargetException fite = new InternalFunctionInvocationTargetException(e.getMessage());
                throw new FunctionException(fite);
            } else {
                clearResults();
                this.execution = this.execution.setIsReExecute();
                ResultCollector newRc = null;
                if (execution.isFnSerializationReqd()) {
                    newRc = this.execution.execute(fn);
                } else {
                    newRc = this.execution.execute(fn.getId());
                }
                return newRc.getResult();
            }
        } catch (ReplyException e) {
            if (!(execution.waitOnException || execution.forwardExceptions)) {
                throw new FunctionException(e.getCause());
            }
        }
        return this.userRC.getResult();
    }
    return null;
}
Also used : ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) FunctionException(org.apache.geode.cache.execute.FunctionException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) ResultCollector(org.apache.geode.cache.execute.ResultCollector) ReplyException(org.apache.geode.distributed.internal.ReplyException)

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