use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class MemberFunctionExecutionDUnitTest method excuteOnMembers_NoLastResult.
public static void excuteOnMembers_NoLastResult(Integer noOfMembers) {
assertNotNull(ds);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_NO_LASTRESULT);
InternalExecution memberExcution = null;
Execution executor = null;
Map memArgs = new HashMap();
if (noOfMembers.intValue() == 1) {
// Local VM
DistributedMember localmember = ds.getDistributedMember();
memberExcution = (InternalExecution) FunctionService.onMember(localmember);
memArgs.put(localmember.getId(), localmember.getId());
MemberMappedArgument args = new MemberMappedArgument("Key", memArgs);
executor = memberExcution.withMemberMappedArgument(args);
} else if (noOfMembers.intValue() == 5) {
memberExcution = (InternalExecution) FunctionService.onMembers();
Set memberSet = new HashSet(ds.getDistributionManager().getNormalDistributionManagerIds());
Iterator iter = memberSet.iterator();
while (iter.hasNext()) {
InternalDistributedMember member = (InternalDistributedMember) iter.next();
memArgs.put(member.getId(), member.getId());
}
MemberMappedArgument args = new MemberMappedArgument("Key", memArgs);
executor = memberExcution.withMemberMappedArgument(args);
} else {
Set memberSet = new HashSet(ds.getDistributionManager().getNormalDistributionManagerIds());
InternalDistributedMember localVM = ds.getDistributionManager().getDistributionManagerId();
memberSet.remove(localVM);
Iterator iter = memberSet.iterator();
while (iter.hasNext()) {
InternalDistributedMember member = (InternalDistributedMember) iter.next();
memArgs.put(member.getId(), member.getId());
}
MemberMappedArgument args = new MemberMappedArgument("Key", memArgs);
memberExcution = (InternalExecution) FunctionService.onMembers(memberSet);
executor = memberExcution.withMemberMappedArgument(args);
}
try {
ResultCollector rc = executor.execute(function.getId());
rc.getResult();
fail("Expcted FunctionException : Function did not send last result");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("did not send last result") || ex.getCause().getMessage().contains("did not send last result"));
}
}
use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class MemberFunctionExecutionDUnitTest method testBug45328.
@Test
public void testBug45328() throws Exception {
createDistributedSystemAndRegisterFunction();
ClassBuilder classBuilder = new ClassBuilder();
final String functionId = "MemberFunctionExecutionDUnitFunction";
// Create a class that has a function and then get a ClassLoader that includes it
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("import java.util.Properties;import org.apache.geode.cache.Declarable;");
stringBuffer.append("import org.apache.geode.cache.CacheFactory;");
stringBuffer.append("import org.apache.geode.cache.execute.Function;");
stringBuffer.append("import org.apache.geode.cache.execute.FunctionContext;");
stringBuffer.append("public class ").append(functionId).append(" implements Function, Declarable {");
stringBuffer.append("public void init(Properties props) {}");
stringBuffer.append("public boolean hasResult() {return true;}");
stringBuffer.append("public void execute(FunctionContext context) {context.getResultSender().lastResult(\"GOOD\");}");
stringBuffer.append("public String getId() {return \"MemberFunctionExecutionDUnitFunction\";}");
stringBuffer.append("public boolean optimizeForWrite() {return false;}");
stringBuffer.append("public boolean isHA() {return false;}}");
ClassLoader classLoader = classBuilder.createClassLoaderFromContent("MemberFunctionExecutionDUnitFunction", stringBuffer.toString());
@SuppressWarnings("unchecked") Class<Function> clazz = (Class<Function>) Class.forName(functionId, true, classLoader);
Constructor<Function> constructor = clazz.getConstructor();
Function function = (Function) constructor.newInstance();
Execution execution = FunctionService.onMembers();
ResultCollector resultCollector = execution.execute(function);
try {
resultCollector.getResult();
fail("Should have received FunctionException due to class not found");
} catch (FunctionException expected) {
LogWriterUtils.getLogWriter().warning("received wrong exception cause", expected.getCause());
assertTrue((expected.getCause() instanceof ClassNotFoundException));
}
}
use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class DumpDirectoryFiles method execute.
@Override
public void execute(FunctionContext context) {
RegionFunctionContext ctx = (RegionFunctionContext) context;
if (!(context.getArguments() instanceof String[])) {
throw new IllegalArgumentException("Arguments should be a string array");
}
String[] args = (String[]) context.getArguments();
if (args.length != 2) {
throw new IllegalArgumentException("Expected 2 arguments: exportLocation, indexName");
}
String exportLocation = args[0];
String indexName = args[1];
final Region<Object, Object> region = ctx.getDataSet();
LuceneService service = LuceneServiceProvider.get(ctx.getDataSet().getCache());
InternalLuceneIndex index = (InternalLuceneIndex) service.getIndex(indexName, region.getFullPath());
if (index == null) {
throw new IllegalStateException("Index not found for region " + region + " index " + indexName);
}
final RepositoryManager repoManager = index.getRepositoryManager();
try {
final Collection<IndexRepository> repositories = repoManager.getRepositories(ctx);
repositories.stream().forEach(repo -> {
final IndexWriter writer = repo.getWriter();
RegionDirectory directory = (RegionDirectory) writer.getDirectory();
FileSystem fs = directory.getFileSystem();
String bucketName = index.getName() + "_" + repo.getRegion().getFullPath();
bucketName = bucketName.replace("/", "_");
File bucketDirectory = new File(exportLocation, bucketName);
bucketDirectory.mkdirs();
fs.export(bucketDirectory);
});
context.getResultSender().lastResult(null);
} catch (BucketNotFoundException e) {
throw new FunctionException(e);
}
}
use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class RetrieveRegionFunction method retrieveRegionWithWhereClause.
/** ------------------------------------------ */
/** Retrieve region data with where clause */
/** ------------------------------------------ */
private void retrieveRegionWithWhereClause(InternalRegionFunctionContext context, LocalRegion localRegion, boolean partitioned, String where, String desc) {
String regionPath = localRegion.getFullPath();
String qstr = "select key, value from " + regionPath + ".entries where " + where;
logger.info(desc + ": " + qstr);
try {
Cache cache = CacheFactory.getAnyInstance();
QueryService queryService = cache.getQueryService();
Query query = queryService.newQuery(qstr);
SelectResults<Struct> results = (SelectResults<Struct>) (partitioned ? query.execute(context) : query.execute());
Iterator<Object[]> entries = getStructIteratorWrapper(results.asList().iterator());
InternalResultSender irs = (InternalResultSender) context.getResultSender();
StructStreamingResultSender sender = new StructStreamingResultSender(irs, null, entries, desc);
sender.send();
} catch (Exception e) {
throw new FunctionException(e);
}
}
use of org.apache.geode.cache.execute.FunctionException in project geode by apache.
the class PRClientServerRegionFunctionExecutionSelectorNoSingleHopDUnitTest method serverMultiKeyExecutionNoResult.
public static void serverMultiKeyExecutionNoResult(Boolean isByName) {
Region region = cache.getRegion(PartitionedRegionName);
assertNotNull(region);
final HashSet testKeysSet = new HashSet();
for (int i = (totalNumBuckets.intValue() * 2); i > 0; i--) {
testKeysSet.add("execKey-" + i);
}
DistributedSystem.setThreadsSocketPolicy(false);
Function function = new TestFunction(false, TEST_FUNCTION7);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(region);
try {
String msg = "<ExpectedException action=add>" + "FunctionException" + "</ExpectedException>";
cache.getLogger().info(msg);
int j = 0;
HashSet origVals = new HashSet();
for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
Integer val = new Integer(j++);
origVals.add(val);
region.put(i.next(), val);
}
ResultCollector rc1 = execute(dataSet, testKeysSet, Boolean.TRUE, function, isByName);
rc1.getResult();
Thread.sleep(20000);
fail("Test failed after the put operation");
} catch (FunctionException expected) {
expected.printStackTrace();
LogWriterUtils.getLogWriter().info("Exception : " + expected.getMessage());
assertTrue(expected.getMessage().startsWith((LocalizedStrings.ExecuteFunction_CANNOT_0_RESULTS_HASRESULT_FALSE.toLocalizedString("return any"))));
} catch (Exception notexpected) {
Assert.fail("Test failed during execute or sleeping", notexpected);
} finally {
cache.getLogger().info("<ExpectedException action=remove>" + "FunctionException" + "</ExpectedException>");
}
}
Aggregations