Search in sources :

Example 86 with FunctionException

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

the class PRClientServerRegionFunctionExecutionSingleHopDUnitTest 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) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) IOException(java.io.IOException) EOFException(java.io.EOFException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) HashSet(java.util.HashSet)

Example 87 with FunctionException

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

the class FunctionCommands method executeFunction.

Result executeFunction(InternalCache cache, Set<DistributedMember> DsMembers, String functionId) {
    // unregister on a set of of members
    Function unregisterFunction = new UnregisterFunction();
    FunctionService.registerFunction(unregisterFunction);
    List resultList = null;
    if (DsMembers.isEmpty()) {
        return ResultBuilder.createInfoResult("No members for execution");
    }
    Object[] obj = new Object[1];
    obj[0] = functionId;
    Execution execution = FunctionService.onMembers(DsMembers).setArguments(obj);
    if (execution == null) {
        cache.getLogger().error("executeUnregister execution is null");
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.DESTROY_FUNCTION__MSG__CANNOT_EXECUTE);
        return (ResultBuilder.buildResult(errorResultData));
    }
    try {
        resultList = (ArrayList) execution.execute(unregisterFunction).getResult();
    } catch (FunctionException ex) {
        ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(ex.getMessage());
        return (ResultBuilder.buildResult(errorResultData));
    }
    String resultStr = ((String) resultList.get(0));
    if (resultStr.equals("Succeeded in unregistering")) {
        StringBuilder members = new StringBuilder();
        for (DistributedMember member : DsMembers) {
            members.append(member.getId());
            members.append(",");
        }
        return ResultBuilder.createInfoResult("Destroyed " + functionId + " Successfully on " + members.toString().substring(0, members.toString().length() - 1));
    } else {
        return ResultBuilder.createInfoResult("Failed in unregistering");
    }
}
Also used : Function(org.apache.geode.cache.execute.Function) ListFunctionFunction(org.apache.geode.management.internal.cli.functions.ListFunctionFunction) UnregisterFunction(org.apache.geode.management.internal.cli.functions.UnregisterFunction) UserFunctionExecution(org.apache.geode.management.internal.cli.functions.UserFunctionExecution) Execution(org.apache.geode.cache.execute.Execution) FunctionException(org.apache.geode.cache.execute.FunctionException) DistributedMember(org.apache.geode.distributed.DistributedMember) ArrayList(java.util.ArrayList) List(java.util.List) UnregisterFunction(org.apache.geode.management.internal.cli.functions.UnregisterFunction) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData)

Example 88 with FunctionException

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

the class FunctionCommands method executeAndGetResults.

void executeAndGetResults(String functionId, String filterString, String resultCollector, String[] arguments, InternalCache cache, DistributedMember member, TabularResultData resultTable, String onRegion) {
    StringBuilder resultMessege = new StringBuilder();
    try {
        Function function = new UserFunctionExecution();
        Object[] args = new Object[5];
        args[0] = functionId;
        if (filterString != null) {
            args[1] = filterString;
        }
        if (resultCollector != null) {
            args[2] = resultCollector;
        }
        if (arguments != null && arguments.length > 0) {
            args[3] = new String();
            for (String str : arguments) {
                // send via CSV separated value format
                if (str != null) {
                    args[3] = args[3] + str + ",";
                }
            }
        }
        args[4] = onRegion;
        Execution execution = FunctionService.onMember(member).setArguments(args);
        if (execution != null) {
            List<Object> results = (List<Object>) execution.execute(function).getResult();
            if (results != null) {
                for (Object resultObj : results) {
                    if (resultObj != null) {
                        if (resultObj instanceof String) {
                            resultMessege.append(((String) resultObj));
                        } else if (resultObj instanceof Exception) {
                            resultMessege.append(((Exception) resultObj).getMessage());
                        } else {
                            resultMessege.append(resultObj);
                        }
                    }
                }
            }
            toTabularResultData(resultTable, member.getId(), resultMessege.toString());
        } else {
            toTabularResultData(resultTable, member.getId(), CliStrings.EXECUTE_FUNCTION__MSG__ERROR_IN_RETRIEVING_EXECUTOR);
        }
    } catch (FunctionException e) {
        resultMessege.append(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__COULD_NOT_EXECUTE_FUNCTION_0_ON_MEMBER_1_ERROR_2, functionId, member.getId(), e.getMessage()));
        toTabularResultData(resultTable, member.getId(), resultMessege.toString());
    } catch (Exception e) {
        resultMessege.append(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__COULD_NOT_EXECUTE_FUNCTION_0_ON_MEMBER_1_ERROR_2, functionId, member.getId(), e.getMessage()));
        toTabularResultData(resultTable, member.getId(), resultMessege.toString());
    }
}
Also used : Function(org.apache.geode.cache.execute.Function) ListFunctionFunction(org.apache.geode.management.internal.cli.functions.ListFunctionFunction) UnregisterFunction(org.apache.geode.management.internal.cli.functions.UnregisterFunction) UserFunctionExecution(org.apache.geode.management.internal.cli.functions.UserFunctionExecution) UserFunctionExecution(org.apache.geode.management.internal.cli.functions.UserFunctionExecution) Execution(org.apache.geode.cache.execute.Execution) FunctionException(org.apache.geode.cache.execute.FunctionException) ArrayList(java.util.ArrayList) List(java.util.List) FunctionException(org.apache.geode.cache.execute.FunctionException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException)

Example 89 with FunctionException

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

the class QueryUsingFunctionContextDUnitTest method testQueriesWithFilterKeysOnPRWithRebalancing.

// GEODE-575: ignores lots of exceptions, non-thread-safe test hooks
@Category(FlakyTest.class)
@Test
public void testQueriesWithFilterKeysOnPRWithRebalancing() {
    IgnoredException.addIgnoredException("QueryInvocationTargetException");
    IgnoredException.addIgnoredException("java.net.SocketException");
    IgnoredException.addIgnoredException("ServerConnectivityException");
    IgnoredException.addIgnoredException("FunctionException");
    IgnoredException.addIgnoredException("IOException");
    // Close cache on server1
    server1.invoke(new CacheSerializableRunnable("Set QueryObserver in cache on server1") {

        @Override
        public void run2() throws CacheException {
            class MyQueryObserver extends IndexTrackingQueryObserver {

                @Override
                public void startQuery(Query query) {
                    Region pr = CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1);
                    Region KeyRegion = null;
                    for (int i = 6; i < 9; i++) {
                        KeyRegion = ((PartitionedRegion) pr).getBucketRegion(i);
                        if (KeyRegion != null)
                            KeyRegion.destroyRegion();
                    }
                }
            }
            ;
            QueryObserverHolder.setInstance(new MyQueryObserver());
        }
    });
    client.invoke(new CacheSerializableRunnable("Run function on PR") {

        @Override
        public void run2() throws CacheException {
            Set filter = new HashSet();
            ResultCollector rcollector = null;
            filter.addAll(getFilter(6, 9));
            for (int i = 0; i < queries.length; i++) {
                try {
                    function = new TestQueryFunction("queryFunction");
                    rcollector = FunctionService.onRegion(CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1)).setArguments(queries[i]).withFilter(filter).execute(function);
                    // Should not come here, an exception is expected from above function call.
                    fail("Function call did not fail for query with function context");
                } catch (FunctionException ex) {
                    if (!((ex.getCause() instanceof QueryInvocationTargetException) || (ex.getCause() instanceof ServerConnectivityException))) {
                        if (ex.getCause() instanceof FunctionException) {
                            FunctionException fe = (FunctionException) ex.getCause();
                            if (!fe.getMessage().startsWith("IOException")) {
                                fail("Should have received an QueryInvocationTargetException but recieved" + ex.getMessage());
                            }
                        } else {
                            fail("Should have received an QueryInvocationTargetException but recieved" + ex.getMessage());
                        }
                    }
                }
            }
        // For loop ends here.
        }
    });
    // Close cache on server1
    server1.invoke(new CacheSerializableRunnable("Reset Query Observer on server1") {

        @Override
        public void run2() throws CacheException {
            QueryObserverHolder.reset();
        }
    });
}
Also used : LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) HashSet(java.util.HashSet) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) FunctionException(org.apache.geode.cache.execute.FunctionException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 90 with FunctionException

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

the class QueryUsingFunctionContextDUnitTest method testNonColocatedRegionQueries.

@Test
public void testNonColocatedRegionQueries() {
    IgnoredException.addIgnoredException("UnsupportedOperationException");
    client.invoke(new CacheSerializableRunnable("Test query on non-colocated regions on server") {

        @Override
        public void run2() throws CacheException {
            Set filter = new HashSet();
            filter.add(0);
            for (int i = 0; i < nonColocatedQueries.length; i++) {
                function = new TestQueryFunction("queryFunction-1");
                QueryUsingFunctionContextDUnitTest test = new QueryUsingFunctionContextDUnitTest();
                try {
                    ArrayList queryResults2 = test.runQueryOnClientUsingFunc(function, PartitionedRegionName1, filter, nonColocatedQueries[i]);
                    fail("Function call did not fail for query with function context");
                } catch (FunctionException e) {
                    if (!(e.getCause() instanceof UnsupportedOperationException)) {
                        Assert.fail("Should have received an UnsupportedOperationException but received", e);
                    }
                }
            }
        }
    });
}
Also used : LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) HashSet(java.util.HashSet) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

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