Search in sources :

Example 81 with FunctionException

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"));
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) HashMap(java.util.HashMap) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Execution(org.apache.geode.cache.execute.Execution) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet)

Example 82 with FunctionException

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));
    }
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException) ClassBuilder(org.apache.geode.internal.ClassBuilder) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Execution(org.apache.geode.cache.execute.Execution) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 83 with FunctionException

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);
    }
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) LuceneService(org.apache.geode.cache.lucene.LuceneService) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) IndexWriter(org.apache.lucene.index.IndexWriter) InternalLuceneIndex(org.apache.geode.cache.lucene.internal.InternalLuceneIndex) FileSystem(org.apache.geode.cache.lucene.internal.filesystem.FileSystem) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) File(java.io.File)

Example 84 with FunctionException

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);
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) FunctionException(org.apache.geode.cache.execute.FunctionException) InternalResultSender(org.apache.geode.internal.cache.execute.InternalResultSender) FunctionException(org.apache.geode.cache.execute.FunctionException) Cache(org.apache.geode.cache.Cache) Struct(org.apache.geode.cache.query.Struct)

Example 85 with FunctionException

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>");
    }
}
Also used : Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Execution(org.apache.geode.cache.execute.Execution) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Iterator(java.util.Iterator) FunctionException(org.apache.geode.cache.execute.FunctionException) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ResultCollector(org.apache.geode.cache.execute.ResultCollector) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) ServerException(java.rmi.ServerException) FunctionException(org.apache.geode.cache.execute.FunctionException) SocketException(java.net.SocketException) CacheClosedException(org.apache.geode.cache.CacheClosedException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) EOFException(java.io.EOFException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) HashSet(java.util.HashSet)

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