Search in sources :

Example 6 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)

Example 7 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 8 with FunctionException

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

the class MemberFunctionExecutor method executeFunction.

@SuppressWarnings("unchecked")
private ResultCollector executeFunction(final Function function, ResultCollector resultCollector) {
    final DM dm = this.ds.getDistributionManager();
    final Set dest = new HashSet(this.members);
    if (dest.isEmpty()) {
        throw new FunctionException(LocalizedStrings.MemberFunctionExecutor_NO_MEMBER_FOUND_FOR_EXECUTING_FUNCTION_0.toLocalizedString(function.getId()));
    }
    validateExecution(function, dest);
    setExecutionNodes(dest);
    final InternalDistributedMember localVM = this.ds.getDistributionManager().getDistributionManagerId();
    final LocalResultCollector<?, ?> localRC = getLocalResultCollector(function, resultCollector);
    boolean remoteOnly = false;
    boolean localOnly = false;
    if (!dest.contains(localVM)) {
        remoteOnly = true;
    }
    if (dest.size() == 1 && dest.contains(localVM)) {
        localOnly = true;
    }
    final MemberFunctionResultSender resultSender = new MemberFunctionResultSender(dm, localRC, function, localOnly, remoteOnly, sender);
    if (dest.contains(localVM)) {
        // if member is local VM
        dest.remove(localVM);
        final FunctionContext context = new FunctionContextImpl(function.getId(), getArgumentsForMember(localVM.getId()), resultSender);
        boolean isTx = false;
        InternalCache cache = GemFireCacheImpl.getInstance();
        if (cache != null) {
            isTx = cache.getTxManager().getTXState() == null ? false : true;
        }
        executeFunctionOnLocalNode(function, context, resultSender, dm, isTx);
    }
    if (!dest.isEmpty()) {
        HashMap<InternalDistributedMember, Object> memberArgs = new HashMap<InternalDistributedMember, Object>();
        Iterator<DistributedMember> iter = dest.iterator();
        while (iter.hasNext()) {
            InternalDistributedMember recip = (InternalDistributedMember) iter.next();
            memberArgs.put(recip, getArgumentsForMember(recip.getId()));
        }
        Assert.assertTrue(memberArgs.size() == dest.size());
        MemberFunctionResultWaiter resultReciever = new MemberFunctionResultWaiter(this.ds, localRC, function, memberArgs, dest, resultSender);
        ResultCollector reply = resultReciever.getFunctionResultFrom(dest, function, this);
        return reply;
    }
    return localRC;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) FunctionException(org.apache.geode.cache.execute.FunctionException) DM(org.apache.geode.distributed.internal.DM) InternalCache(org.apache.geode.internal.cache.InternalCache) FunctionContext(org.apache.geode.cache.execute.FunctionContext) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet)

Example 9 with FunctionException

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

the class MemberFunctionResultSender method sendResult.

public void sendResult(Object oneResult) {
    if (!this.function.hasResult()) {
        throw new IllegalStateException(LocalizedStrings.ExecuteFunction_CANNOT_0_RESULTS_HASRESULT_FALSE.toLocalizedString("send"));
    }
    if (this.serverSender != null) {
        // Client-Server
        if (logger.isDebugEnabled()) {
            logger.debug("MemberFunctionResultSender sending result from local node to client {}", oneResult);
        }
        this.serverSender.sendResult(oneResult);
    } else {
        // P2P
        if (this.msg != null) {
            try {
                this.msg.sendReplyForOneResult(dm, oneResult, false, enableOrderedResultStreming);
            } catch (QueryException e) {
                throw new FunctionException(e);
            } catch (ForceReattemptException e) {
                throw new FunctionException(e);
            } catch (InterruptedException e) {
                throw new FunctionException(e);
            }
        } else {
            this.rc.addResult(this.dm.getDistributionManagerId(), oneResult);
            FunctionStats.getFunctionStats(function.getId(), this.dm.getSystem()).incResultsReceived();
        }
        // incrementing result sent stats.
        FunctionStats.getFunctionStats(function.getId(), this.dm.getSystem()).incResultsReturned();
    }
}
Also used : QueryException(org.apache.geode.cache.query.QueryException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) FunctionException(org.apache.geode.cache.execute.FunctionException)

Example 10 with FunctionException

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

the class MemberFunctionResultSender method lastResult.

public void lastResult(Object oneResult) {
    if (!this.function.hasResult()) {
        throw new IllegalStateException(LocalizedStrings.ExecuteFunction_CANNOT_0_RESULTS_HASRESULT_FALSE.toLocalizedString("send"));
    }
    if (this.serverSender != null) {
        // client-server
        if (this.localLastResultRecieved) {
            return;
        }
        if (onlyLocal) {
            this.serverSender.lastResult(oneResult);
            this.rc.endResults();
            this.localLastResultRecieved = true;
        } else {
            lastResult(oneResult, rc, false, true, this.dm.getId());
        }
    } else {
        // P2P
        if (this.msg != null) {
            try {
                this.msg.sendReplyForOneResult(dm, oneResult, true, enableOrderedResultStreming);
            } catch (QueryException e) {
                throw new FunctionException(e);
            } catch (ForceReattemptException e) {
                throw new FunctionException(e);
            } catch (InterruptedException e) {
                throw new FunctionException(e);
            }
        } else {
            if (this.localLastResultRecieved) {
                return;
            }
            if (onlyLocal) {
                this.rc.addResult(this.dm.getDistributionManagerId(), oneResult);
                this.rc.endResults();
                this.localLastResultRecieved = true;
            } else {
                // call a synchronized method as local node is also waiting to send lastResult
                lastResult(oneResult, rc, false, true, this.dm.getDistributionManagerId());
            }
            FunctionStats.getFunctionStats(function.getId(), this.dm.getSystem()).incResultsReceived();
        }
    }
    FunctionStats.getFunctionStats(function.getId(), this.dm.getSystem()).incResultsReturned();
}
Also used : QueryException(org.apache.geode.cache.query.QueryException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) FunctionException(org.apache.geode.cache.execute.FunctionException)

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