Search in sources :

Example 6 with PartitionedRegionFunctionResultSender

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

the class PartitionedRegion method executeOnSingleNode.

/**
   * Single key execution on single node
   * 
   * @since GemFire 6.0
   */
private ResultCollector executeOnSingleNode(final Function function, final PartitionedRegionFunctionExecutor execution, ResultCollector rc, boolean isPRSingleHop, boolean isBucketSetAsFilter) {
    final Set routingKeys = execution.getFilter();
    final Object key = routingKeys.iterator().next();
    final Integer bucketId;
    if (isBucketSetAsFilter) {
        bucketId = (Integer) key;
    } else {
        bucketId = PartitionedRegionHelper.getHashKey(this, Operation.FUNCTION_EXECUTION, key, null, null);
    }
    InternalDistributedMember targetNode = null;
    if (function.optimizeForWrite()) {
        targetNode = createBucket(bucketId, 0, null);
        HeapMemoryMonitor hmm = ((InternalResourceManager) cache.getResourceManager()).getHeapMonitor();
        if (hmm.isMemberHeapCritical(targetNode) && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
            Set<DistributedMember> sm = Collections.singleton((DistributedMember) targetNode);
            throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(function.getId(), sm), sm);
        }
    } else {
        targetNode = getOrCreateNodeForBucketRead(bucketId);
    }
    final DistributedMember localVm = getMyId();
    if (targetNode != null && isPRSingleHop && !localVm.equals(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 remote node.{}", localVm);
                    }
                    throw new InternalFunctionInvocationTargetException(LocalizedStrings.PartitionedRegion_MULTIPLE_TARGET_NODE_FOUND_FOR.toLocalizedString());
                }
            }
        }
    }
    if (targetNode == null) {
        throw new FunctionException(LocalizedStrings.PartitionedRegion_NO_TARGET_NODE_FOUND_FOR_KEY_0.toLocalizedString(key));
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Executing Function: {} setArguments={} on {}", function.getId(), execution.getArguments(), targetNode);
    }
    while (!execution.getFailedNodes().isEmpty()) {
        RetryTimeKeeper retryTime = new RetryTimeKeeper(this.retryTimeout);
        if (execution.getFailedNodes().contains(targetNode.getId())) {
            /*
         * if (retryTime.overMaximum()) { PRHARedundancyProvider.timedOut(this, null, null,
         * "doing function execution", this.retryTimeout); // NOTREACHED }
         */
            // Fix for Bug # 40083
            targetNode = null;
            while (targetNode == null) {
                if (retryTime.overMaximum()) {
                    PRHARedundancyProvider.timedOut(this, null, null, "doing function execution", this.retryTimeout);
                // NOTREACHED
                }
                retryTime.waitToRetryNode();
                if (function.optimizeForWrite()) {
                    targetNode = getOrCreateNodeForBucketWrite(bucketId, retryTime);
                } else {
                    targetNode = getOrCreateNodeForBucketRead(bucketId);
                }
            }
            if (targetNode == null) {
                throw new FunctionException(LocalizedStrings.PartitionedRegion_NO_TARGET_NODE_FOUND_FOR_KEY_0.toLocalizedString(key));
            }
        } else {
            execution.clearFailedNodes();
        }
    }
    final HashSet<Integer> buckets = new HashSet<Integer>();
    buckets.add(bucketId);
    final Set<InternalDistributedMember> singleMember = Collections.singleton(targetNode);
    execution.validateExecution(function, singleMember);
    execution.setExecutionNodes(singleMember);
    LocalResultCollector<?, ?> localRC = execution.getLocalResultCollector(function, rc);
    if (targetNode.equals(localVm)) {
        final DM dm = getDistributionManager();
        PartitionedRegionFunctionResultSender resultSender = new PartitionedRegionFunctionResultSender(dm, PartitionedRegion.this, 0, localRC, execution.getServerResultSender(), true, false, execution.isForwardExceptions(), function, buckets);
        final FunctionContext context = new RegionFunctionContextImpl(function.getId(), PartitionedRegion.this, execution.getArgumentsForMember(localVm.getId()), routingKeys, ColocationHelper.constructAndGetAllColocatedLocalDataSet(PartitionedRegion.this, buckets), buckets, resultSender, execution.isReExecute());
        execution.executeFunctionOnLocalPRNode(function, context, resultSender, dm, isTX());
        return localRC;
    } else {
        return executeFunctionOnRemoteNode(targetNode, function, execution.getArgumentsForMember(targetNode.getId()), routingKeys, function.isHA() ? rc : localRC, buckets, execution.getServerResultSender(), execution);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) FunctionException(org.apache.geode.cache.execute.FunctionException) EmptyRegionFunctionException(org.apache.geode.cache.execute.EmptyRegionFunctionException) DM(org.apache.geode.distributed.internal.DM) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) FunctionContext(org.apache.geode.cache.execute.FunctionContext) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerBucketProfile(org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) RegionFunctionContextImpl(org.apache.geode.internal.cache.execute.RegionFunctionContextImpl) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) PartitionedRegionFunctionResultSender(org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultSender) LowMemoryException(org.apache.geode.cache.LowMemoryException) HashSet(java.util.HashSet)

Aggregations

PartitionedRegionFunctionResultSender (org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultSender)6 DM (org.apache.geode.distributed.internal.DM)5 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)5 RegionFunctionContextImpl (org.apache.geode.internal.cache.execute.RegionFunctionContextImpl)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 EmptyRegionFunctionException (org.apache.geode.cache.execute.EmptyRegionFunctionException)4 ResultsSet (org.apache.geode.cache.query.internal.ResultsSet)4 FunctionRemoteContext (org.apache.geode.internal.cache.execute.FunctionRemoteContext)4 PartitionedRegionFunctionResultWaiter (org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultWaiter)4 Iterator (java.util.Iterator)3 FunctionException (org.apache.geode.cache.execute.FunctionException)3 ServerBucketProfile (org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile)3 InternalFunctionInvocationTargetException (org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException)3 PREntriesIterator (org.apache.geode.internal.cache.partitioned.PREntriesIterator)3 NoSuchElementException (java.util.NoSuchElementException)2 LowMemoryException (org.apache.geode.cache.LowMemoryException)2