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();
}
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()));
}
}
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;
}
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();
}
}
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();
}
Aggregations