use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class PRFunctionExecutionTimeOutDUnitTest method testRemoteSingleKeyExecution_byName.
/**
* Test remote execution by a pure accessor. Then test it using timeout and multiple getResult.
*
* @throws Exception
*/
@Test
public void testRemoteSingleKeyExecution_byName() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM accessor = host.getVM(2);
final VM datastore = host.getVM(3);
getCache();
accessor.invoke(new SerializableCallable("Create PR") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
getCache().createRegion(rName, ra);
return Boolean.TRUE;
}
});
datastore.invoke(new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
Function function = new TestFunction(true, TEST_FUNCTION_TIMEOUT);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
});
Object o = accessor.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
final String testKey = "execKey";
final Set testKeysSet = new HashSet();
testKeysSet.add(testKey);
DistributedSystem.setThreadsSocketPolicy(false);
Function function = new TestFunction(true, TEST_FUNCTION_TIMEOUT);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(pr);
try {
dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
} catch (Exception expected) {
// No data should cause exec to throw
assertTrue(expected.getMessage().contains("No target node found for KEY = " + testKey));
}
pr.put(testKey, new Integer(1));
ResultCollector rs1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
assertEquals(Boolean.TRUE, ((List) rs1.getResult()).get(0));
try {
rs1.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
ResultCollector rs2 = dataSet.withFilter(testKeysSet).setArguments(testKey).execute(function.getId());
assertEquals(new Integer(1), ((List) rs2.getResult()).get(0));
try {
rs1.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
HashMap putData = new HashMap();
putData.put(testKey + "1", new Integer(2));
putData.put(testKey + "2", new Integer(3));
ResultCollector rs3 = dataSet.withFilter(testKeysSet).setArguments(putData).execute(function.getId());
assertEquals(Boolean.TRUE, ((List) rs3.getResult(4000, TimeUnit.MILLISECONDS)).get(0));
try {
rs1.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
assertEquals(new Integer(2), pr.get(testKey + "1"));
assertEquals(new Integer(3), pr.get(testKey + "2"));
ResultCollector rst1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
try {
rst1.getResult(1000, TimeUnit.MILLISECONDS);
fail("Did not get the expected timeout exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("All results not recieved in time provided."));
}
try {
rst1.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
ResultCollector rst2 = dataSet.withFilter(testKeysSet).setArguments(testKey).execute(function.getId());
try {
rst2.getResult(1000, TimeUnit.MILLISECONDS);
fail("Did not get the expected timeout exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("All results not recieved in time provided."));
}
try {
rst2.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
HashMap putDataTimeOut = new HashMap();
putDataTimeOut.put(testKey + "4", new Integer(4));
putDataTimeOut.put(testKey + "5", new Integer(5));
ResultCollector rst3 = dataSet.withFilter(testKeysSet).setArguments(putDataTimeOut).execute(function.getId());
try {
rst3.getResult(1000, TimeUnit.MILLISECONDS);
fail("Did not get the expected timeout exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("All results not recieved in time provided."));
}
try {
rst3.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class PRFunctionExecutionDUnitTest method testFunctionExecutionException_41779.
@Test
public void testFunctionExecutionException_41779() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore1 = host.getVM(0);
final VM datastore2 = host.getVM(1);
final VM datastore3 = host.getVM(2);
final VM datastore4 = host.getVM(3);
final Cache cache = getCache();
@SuppressWarnings("serial") final SerializableRunnable createFact = new SerializableRunnable("Create PR with Function Factory") {
public void run() {
RegionAttributes<?, ?> ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
@SuppressWarnings("unchecked") AttributesFactory<?, ?> raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
getCache().createRegionFactory(raf.create()).create(rName);
Function function = new TestFunctionException();
FunctionService.registerFunction(function);
}
};
// create stores on all VMs including controller
datastore1.invoke(createFact);
datastore2.invoke(createFact);
datastore3.invoke(createFact);
datastore4.invoke(createFact);
createFact.run();
InternalExecution exec = (InternalExecution) FunctionService.onRegion(cache.getRegion(rName));
exec.setWaitOnExceptionFlag(true);
try {
List results = (List) exec.execute(TestFunctionException.ID).getResult();
fail("expected a function exception");
} catch (FunctionException fe) {
// expect exceptions from all VMs
assertEquals("did not get expected number of exceptions: " + fe.getExceptions(), 5, fe.getExceptions().size());
}
}
use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class ReadWriteFile method readWriteFile.
public static String readWriteFile(String logFileName, String logToBeWritten, String logLevel, String onlyLogLevel, String startTime, String endTime) {
try {
long lineCount = 0;
BufferedReader input = null;
BufferedWriter output = null;
File logFileNameFile = new File(logFileName);
if (logFileNameFile.canRead() == false) {
return ("Cannot read logFileName=" + logFileName);
}
input = new BufferedReader(new FileReader(logFileName));
String line = null;
File logToBeWrittenToFile = new File(logToBeWritten);
output = new BufferedWriter(new FileWriter(logToBeWrittenToFile));
if (!logToBeWrittenToFile.exists()) {
if (input != null) {
input.close();
}
if (output != null) {
output.flush();
output.close();
}
return (logToBeWritten + " doesn not exist");
}
if (!logToBeWrittenToFile.isFile()) {
if (input != null) {
input.close();
}
if (output != null) {
output.flush();
output.close();
}
return (logToBeWritten + " is not a file");
}
if (!logToBeWrittenToFile.canWrite()) {
if (input != null) {
input.close();
}
if (output != null) {
output.flush();
output.close();
}
return ("can not write file " + logToBeWritten);
}
// build possible log levels based on user input
// get all the levels below the one mentioned by user
List<String> logLevels = new ArrayList<String>();
if (onlyLogLevel.toLowerCase().equals("false")) {
int[] intLogLevels = LogWriterImpl.allLevels;
for (int level : intLogLevels) {
if (level >= LogLevel.getLogWriterLevel(logLevel)) {
logLevels.add(LogWriterImpl.levelToString(level).toLowerCase());
}
}
} else {
logLevels.add(logLevel);
}
boolean timeRangeCheck = false;
boolean foundLogLevelTag = false;
boolean validateLogLevel = true;
while (input.ready() == true && (line = input.readLine()) != null) {
if (new File(logFileName).canRead() == false) {
return ("Cannot read logFileName=" + logFileName);
}
// boolean validateLogLevel = true;
lineCount++;
if (line.startsWith("[")) {
foundLogLevelTag = true;
} else {
foundLogLevelTag = false;
}
if (line.contains("[info ") && timeRangeCheck == false) {
String stTime = "";
int spaceCounter = 0;
for (int i = line.indexOf("[info ") + 6; i < line.length(); i++) {
if (line.charAt(i) == ' ') {
spaceCounter++;
}
if (spaceCounter > 2) {
break;
}
stTime = stTime + line.charAt(i);
}
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d = df.parse(stTime.substring(0, stTime.length() - 4));
Time fileStartTime = new Time(d.getTime());
File inputFile = new File(logFileName);
Time fileEndTime = new Time(inputFile.lastModified());
Time longStart = new Time(Long.valueOf(startTime));
Time longEnd = new Time(Long.valueOf(endTime));
long userStartTime = longStart.getTime();
long userEndTime = longEnd.getTime();
if ((fileStartTime.getTime() >= userStartTime && fileStartTime.getTime() <= userEndTime) || (fileEndTime.getTime() >= userStartTime && fileEndTime.getTime() <= userEndTime) || (fileStartTime.getTime() >= userStartTime && fileStartTime.getTime() <= userEndTime && fileEndTime.getTime() >= userStartTime && fileEndTime.getTime() <= userEndTime)) {
// set this so that no need to check time range for each line
timeRangeCheck = true;
} else {
// dont take this log file as this does not fit in time range
break;
}
}
if (foundLogLevelTag == true) {
validateLogLevel = checkLogLevel(line, logLevel, logLevels, foundLogLevelTag);
}
if (validateLogLevel) {
output.append(line);
output.newLine();
// frequency and at the same time will keep buffer under control
if ((lineCount % 1000) == 0) {
output.flush();
}
}
}
if (input != null) {
input.close();
}
if (output != null) {
output.flush();
output.close();
}
return ("Sucessfully written file " + logFileName);
} catch (FunctionException ex) {
return ("readWriteFile FunctionException " + ex.getMessage());
} catch (FileNotFoundException ex) {
return ("readWriteFile FileNotFoundException " + ex.getMessage());
} catch (IOException ex) {
return ("readWriteFile FileNotFoundException " + ex.getMessage());
} catch (Exception ex) {
return ("readWriteFile Exception " + ex.getMessage());
}
}
use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class ExecuteFunction65 method cmdExecute.
@Override
public void cmdExecute(Message clientMessage, ServerConnection servConn, long start) throws IOException {
Object function = null;
Object args = null;
MemberMappedArgument memberMappedArg = null;
byte hasResult = 0;
byte functionState = 0;
boolean isReexecute = false;
try {
functionState = clientMessage.getPart(0).getSerializedForm()[0];
if (functionState == AbstractExecution.HA_HASRESULT_NO_OPTIMIZEFORWRITE_REEXECUTE) {
functionState = AbstractExecution.HA_HASRESULT_NO_OPTIMIZEFORWRITE;
isReexecute = true;
} else if (functionState == AbstractExecution.HA_HASRESULT_OPTIMIZEFORWRITE_REEXECUTE) {
functionState = AbstractExecution.HA_HASRESULT_OPTIMIZEFORWRITE;
isReexecute = true;
}
if (functionState != 1) {
hasResult = (byte) ((functionState & 2) - 1);
} else {
hasResult = functionState;
}
if (hasResult == 1) {
servConn.setAsTrue(REQUIRES_RESPONSE);
servConn.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
}
function = clientMessage.getPart(1).getStringOrObject();
args = clientMessage.getPart(2).getObject();
Part part = clientMessage.getPart(3);
if (part != null) {
memberMappedArg = (MemberMappedArgument) part.getObject();
}
} catch (ClassNotFoundException exception) {
logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), exception);
if (hasResult == 1) {
writeChunkedException(clientMessage, exception, servConn);
servConn.setAsTrue(RESPONDED);
return;
}
}
if (function == null) {
final String message = LocalizedStrings.ExecuteFunction_THE_INPUT_FUNCTION_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL.toLocalizedString();
logger.warn("{}: {}", servConn.getName(), message);
sendError(hasResult, clientMessage, message, servConn);
return;
}
// Execute function on the cache
try {
Function functionObject = null;
if (function instanceof String) {
functionObject = FunctionService.getFunction((String) function);
if (functionObject == null) {
final String message = LocalizedStrings.ExecuteFunction_FUNCTION_NAMED_0_IS_NOT_REGISTERED.toLocalizedString(function);
logger.warn("{}: {}", servConn.getName(), message);
sendError(hasResult, clientMessage, message, servConn);
return;
} else {
byte functionStateOnServerSide = AbstractExecution.getFunctionState(functionObject.isHA(), functionObject.hasResult(), functionObject.optimizeForWrite());
if (logger.isDebugEnabled()) {
logger.debug("Function State on server side: {} on client: {}", functionStateOnServerSide, functionState);
}
if (functionStateOnServerSide != functionState) {
String message = LocalizedStrings.FunctionService_FUNCTION_ATTRIBUTE_MISMATCH_CLIENT_SERVER.toLocalizedString(function);
logger.warn("{}: {}", servConn.getName(), message);
sendError(hasResult, clientMessage, message, servConn);
return;
}
}
} else {
functionObject = (Function) function;
}
FunctionStats stats = FunctionStats.getFunctionStats(functionObject.getId());
this.securityService.authorizeDataWrite();
// check if the caller is authorized to do this operation on server
AuthorizeRequest authzRequest = servConn.getAuthzRequest();
ExecuteFunctionOperationContext executeContext = null;
if (authzRequest != null) {
executeContext = authzRequest.executeFunctionAuthorize(functionObject.getId(), null, null, args, functionObject.optimizeForWrite());
}
ChunkedMessage m = servConn.getFunctionResponseMessage();
m.setTransactionId(clientMessage.getTransactionId());
ResultSender resultSender = new ServerToClientFunctionResultSender65(m, MessageType.EXECUTE_FUNCTION_RESULT, servConn, functionObject, executeContext);
InternalDistributedMember localVM = (InternalDistributedMember) servConn.getCache().getDistributedSystem().getDistributedMember();
FunctionContext context = null;
if (memberMappedArg != null) {
context = new FunctionContextImpl(functionObject.getId(), memberMappedArg.getArgumentsForMember(localVM.getId()), resultSender, isReexecute);
} else {
context = new FunctionContextImpl(functionObject.getId(), args, resultSender, isReexecute);
}
HandShake handShake = (HandShake) servConn.getHandshake();
int earlierClientReadTimeout = handShake.getClientReadTimeout();
handShake.setClientReadTimeout(0);
try {
long startExecution = stats.startTime();
stats.startFunctionExecution(functionObject.hasResult());
if (logger.isDebugEnabled()) {
logger.debug("Executing Function on Server: {} with context: {}", servConn, context);
}
InternalCache cache = servConn.getCache();
HeapMemoryMonitor hmm = ((InternalResourceManager) cache.getResourceManager()).getHeapMonitor();
if (functionObject.optimizeForWrite() && cache != null && hmm.getState().isCritical() && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
Set<DistributedMember> sm = Collections.singleton((DistributedMember) cache.getMyId());
Exception e = new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(new Object[] { functionObject.getId(), sm }), sm);
sendException(hasResult, clientMessage, e.getMessage(), servConn, e);
return;
}
functionObject.execute(context);
if (!((ServerToClientFunctionResultSender65) resultSender).isLastResultReceived() && functionObject.hasResult()) {
throw new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(functionObject.getId()));
}
stats.endFunctionExecution(startExecution, functionObject.hasResult());
} catch (FunctionException functionException) {
stats.endFunctionExecutionWithException(functionObject.hasResult());
throw functionException;
} catch (Exception exception) {
stats.endFunctionExecutionWithException(functionObject.hasResult());
throw new FunctionException(exception);
} finally {
handShake.setClientReadTimeout(earlierClientReadTimeout);
}
} catch (IOException ioException) {
logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), ioException);
String message = LocalizedStrings.ExecuteFunction_SERVER_COULD_NOT_SEND_THE_REPLY.toLocalizedString();
sendException(hasResult, clientMessage, message, servConn, ioException);
} catch (InternalFunctionInvocationTargetException internalfunctionException) {
// 2> in case of HA member departed
if (logger.isDebugEnabled()) {
logger.debug(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, new Object[] { function }), internalfunctionException);
}
final String message = internalfunctionException.getMessage();
sendException(hasResult, clientMessage, message, servConn, internalfunctionException);
} catch (Exception e) {
logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), e);
final String message = e.getMessage();
sendException(hasResult, clientMessage, message, servConn, e);
}
}
use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class ExecuteRegionFunction66 method writeFunctionResponseException.
protected static void writeFunctionResponseException(Message origMsg, int messageType, String message, ServerConnection servConn, Throwable e) throws IOException {
ChunkedMessage functionResponseMsg = servConn.getFunctionResponseMessage();
ChunkedMessage chunkedResponseMsg = servConn.getChunkedResponseMessage();
int numParts = 0;
if (functionResponseMsg.headerHasBeenSent()) {
if (e instanceof FunctionException && e.getCause() instanceof InternalFunctionInvocationTargetException) {
functionResponseMsg.setNumberOfParts(3);
functionResponseMsg.addObjPart(e);
functionResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
InternalFunctionInvocationTargetException fe = (InternalFunctionInvocationTargetException) e.getCause();
functionResponseMsg.addObjPart(fe.getFailedNodeSet());
numParts = 3;
} else {
functionResponseMsg.setNumberOfParts(2);
functionResponseMsg.addObjPart(e);
functionResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
numParts = 2;
}
if (logger.isDebugEnabled()) {
logger.debug("{}: Sending exception chunk while reply in progress: ", servConn.getName(), e);
}
functionResponseMsg.setServerConnection(servConn);
functionResponseMsg.setLastChunkAndNumParts(true, numParts);
// functionResponseMsg.setLastChunk(true);
functionResponseMsg.sendChunk(servConn);
} else {
chunkedResponseMsg.setMessageType(messageType);
chunkedResponseMsg.setTransactionId(origMsg.getTransactionId());
chunkedResponseMsg.sendHeader();
if (e instanceof FunctionException && e.getCause() instanceof InternalFunctionInvocationTargetException) {
chunkedResponseMsg.setNumberOfParts(3);
chunkedResponseMsg.addObjPart(e);
chunkedResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
InternalFunctionInvocationTargetException fe = (InternalFunctionInvocationTargetException) e.getCause();
chunkedResponseMsg.addObjPart(fe.getFailedNodeSet());
numParts = 3;
} else {
chunkedResponseMsg.setNumberOfParts(2);
chunkedResponseMsg.addObjPart(e);
chunkedResponseMsg.addStringPart(BaseCommand.getExceptionTrace(e));
numParts = 2;
}
if (logger.isDebugEnabled()) {
logger.debug("{}: Sending exception chunk: ", servConn.getName(), e);
}
chunkedResponseMsg.setServerConnection(servConn);
chunkedResponseMsg.setLastChunkAndNumParts(true, numParts);
chunkedResponseMsg.sendChunk(servConn);
}
}
Aggregations